au0828-core.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Driver for the Auvitek USB bridge
  3. *
  4. * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-common.h>
  24. #include <linux/mutex.h>
  25. #include "au0828.h"
  26. static unsigned int debug;
  27. module_param(debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "enable debug messages");
  29. #define _err(fmt, arg...)\
  30. do {\
  31. printk(KERN_ERR DRIVER_NAME "/0: " fmt, ## arg);\
  32. } while (0)
  33. #define _info(fmt, arg...)\
  34. do {\
  35. printk(KERN_INFO DRIVER_NAME "/0: " fmt, ## arg);\
  36. } while (0)
  37. #define _dbg(level, fmt, arg...)\
  38. do {\
  39. if (debug >= level) \
  40. printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
  41. } while (0)
  42. #define _AU0828_BULKPIPE 0x03
  43. #define _BULKPIPESIZE 0xffff
  44. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  45. u16 index, unsigned char *cp, u16 size);
  46. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  47. u16 index, unsigned char *cp, u16 size);
  48. /* USB Direction */
  49. #define CMD_REQUEST_IN 0x00
  50. #define CMD_REQUEST_OUT 0x01
  51. u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
  52. {
  53. recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
  54. _dbg(3,"%s(0x%x) = 0x%x\n", __FUNCTION__, reg, dev->ctrlmsg[0]);
  55. return dev->ctrlmsg[0];
  56. }
  57. u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
  58. {
  59. _dbg(3,"%s(0x%x, 0x%x)\n", __FUNCTION__, reg, val);
  60. return send_control_msg(dev, CMD_REQUEST_OUT, val, reg, dev->ctrlmsg, 0);
  61. }
  62. static void cmd_msg_dump(struct au0828_dev *dev)
  63. {
  64. int i;
  65. for (i = 0;i < sizeof(dev->ctrlmsg); i+=16)
  66. _dbg(1,"%s() %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
  67. "%02x %02x %02x %02x %02x %02x\n",
  68. __FUNCTION__,
  69. dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
  70. dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
  71. dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
  72. dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
  73. dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
  74. dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
  75. dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
  76. dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
  77. }
  78. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  79. u16 index, unsigned char *cp, u16 size)
  80. {
  81. int status = -ENODEV;
  82. mutex_lock(&dev->mutex);
  83. if (dev->usbdev) {
  84. /* cp must be memory that has been allocated by kmalloc */
  85. status = usb_control_msg(dev->usbdev,
  86. usb_sndctrlpipe(dev->usbdev, 0),
  87. request,
  88. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  89. value, index,
  90. cp, size, 1000);
  91. status = min(status, 0);
  92. if (status < 0) {
  93. _err("%s() Failed sending control message, error %d.\n",
  94. __FUNCTION__,
  95. status);
  96. }
  97. }
  98. mutex_unlock(&dev->mutex);
  99. return status;
  100. }
  101. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  102. u16 index, unsigned char *cp, u16 size)
  103. {
  104. int status = -ENODEV;
  105. mutex_lock(&dev->mutex);
  106. if (dev->usbdev) {
  107. memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
  108. /* cp must be memory that has been allocated by kmalloc */
  109. status = usb_control_msg(dev->usbdev,
  110. usb_rcvctrlpipe(dev->usbdev, 0),
  111. request,
  112. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  113. value, index,
  114. cp, size, 1000);
  115. status = min(status, 0);
  116. if (status < 0) {
  117. _err("%s() Failed receiving ctrl msg, error %d.\n",
  118. __FUNCTION__,
  119. status);
  120. }
  121. else
  122. if (debug > 4)
  123. cmd_msg_dump(dev);
  124. }
  125. mutex_unlock(&dev->mutex);
  126. return status;
  127. }
  128. static void au0828_usb_disconnect(struct usb_interface *interface)
  129. {
  130. struct au0828_dev *dev = usb_get_intfdata(interface);
  131. _dbg(1,"%s()\n", __FUNCTION__);
  132. /* Digital TV */
  133. au0828_dvb_unregister(dev);
  134. /* I2C */
  135. au0828_i2c_unregister(dev);
  136. usb_set_intfdata(interface, NULL);
  137. mutex_lock(&dev->mutex);
  138. dev->usbdev = NULL;
  139. mutex_unlock(&dev->mutex);
  140. kfree(dev);
  141. }
  142. static int au0828_usb_probe (struct usb_interface *interface,
  143. const struct usb_device_id *id)
  144. {
  145. int ifnum;
  146. struct au0828_dev *dev;
  147. struct usb_device *usbdev = interface_to_usbdev(interface);
  148. ifnum = interface->altsetting->desc.bInterfaceNumber;
  149. if (ifnum != 0)
  150. return -ENODEV;
  151. _dbg(1,"%s() vendor id 0x%x device id 0x%x ifnum:%d\n",
  152. __FUNCTION__,
  153. le16_to_cpu(usbdev->descriptor.idVendor),
  154. le16_to_cpu(usbdev->descriptor.idProduct),
  155. ifnum);
  156. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  157. if (dev == NULL) {
  158. _err("Unable to allocate memory\n");
  159. return -ENOMEM;
  160. }
  161. mutex_init(&dev->mutex);
  162. mutex_init(&dev->dvb.lock);
  163. dev->usbdev = usbdev;
  164. dev->board = id->driver_info;
  165. usb_set_intfdata(interface, dev);
  166. /* Power Up the bridge */
  167. au0828_write(dev, REG_600, 1 << 4);
  168. /* Bring up the GPIO's and supporting devices */
  169. au0828_gpio_setup(dev);
  170. /* I2C */
  171. au0828_i2c_register(dev);
  172. /* Setup */
  173. au0828_card_setup(dev);
  174. /* Digital TV */
  175. au0828_dvb_register(dev);
  176. _info("Registered device AU0828 [%s]\n",
  177. au0828_boards[dev->board].name == NULL ? "Unset" :
  178. au0828_boards[dev->board].name);
  179. return 0;
  180. }
  181. static struct usb_driver au0828_usb_driver = {
  182. .name = DRIVER_NAME,
  183. .probe = au0828_usb_probe,
  184. .disconnect = au0828_usb_disconnect,
  185. .id_table = au0828_usb_id_table,
  186. };
  187. static int __init au0828_init(void)
  188. {
  189. int ret;
  190. _info("au0828 driver loaded\n");
  191. ret = usb_register(&au0828_usb_driver);
  192. if (ret)
  193. _err("usb_register failed, error = %d\n", ret);
  194. return ret;
  195. }
  196. static void __exit au0828_exit(void)
  197. {
  198. usb_deregister(&au0828_usb_driver);
  199. }
  200. module_init(au0828_init);
  201. module_exit(au0828_exit);
  202. MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
  203. MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
  204. MODULE_LICENSE("GPL");