au0828-core.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Driver for the Auvitek USB bridge
  3. *
  4. * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
  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. /*
  27. * 1 = General debug messages
  28. * 2 = USB handling
  29. * 4 = I2C related
  30. * 8 = Bridge related
  31. */
  32. int au0828_debug;
  33. module_param_named(debug, au0828_debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "enable debug messages");
  35. static unsigned int disable_usb_speed_check;
  36. module_param(disable_usb_speed_check, int, 0444);
  37. MODULE_PARM_DESC(disable_usb_speed_check,
  38. "override min bandwidth requirement of 480M bps");
  39. #define _AU0828_BULKPIPE 0x03
  40. #define _BULKPIPESIZE 0xffff
  41. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  42. u16 index, unsigned char *cp, u16 size);
  43. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  44. u16 index, unsigned char *cp, u16 size);
  45. /* USB Direction */
  46. #define CMD_REQUEST_IN 0x00
  47. #define CMD_REQUEST_OUT 0x01
  48. u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
  49. {
  50. recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
  51. dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]);
  52. return dev->ctrlmsg[0];
  53. }
  54. u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
  55. {
  56. dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
  57. return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
  58. dev->ctrlmsg, 0);
  59. }
  60. static void cmd_msg_dump(struct au0828_dev *dev)
  61. {
  62. int i;
  63. for (i = 0; i < sizeof(dev->ctrlmsg); i += 16)
  64. dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x "
  65. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  66. __func__,
  67. dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
  68. dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
  69. dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
  70. dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
  71. dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
  72. dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
  73. dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
  74. dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
  75. }
  76. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  77. u16 index, unsigned char *cp, u16 size)
  78. {
  79. int status = -ENODEV;
  80. mutex_lock(&dev->mutex);
  81. if (dev->usbdev) {
  82. /* cp must be memory that has been allocated by kmalloc */
  83. status = usb_control_msg(dev->usbdev,
  84. usb_sndctrlpipe(dev->usbdev, 0),
  85. request,
  86. USB_DIR_OUT | USB_TYPE_VENDOR |
  87. USB_RECIP_DEVICE,
  88. value, index,
  89. cp, size, 1000);
  90. status = min(status, 0);
  91. if (status < 0) {
  92. printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
  93. __func__, status);
  94. }
  95. }
  96. mutex_unlock(&dev->mutex);
  97. return status;
  98. }
  99. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  100. u16 index, unsigned char *cp, u16 size)
  101. {
  102. int status = -ENODEV;
  103. mutex_lock(&dev->mutex);
  104. if (dev->usbdev) {
  105. memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
  106. /* cp must be memory that has been allocated by kmalloc */
  107. status = usb_control_msg(dev->usbdev,
  108. usb_rcvctrlpipe(dev->usbdev, 0),
  109. request,
  110. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  111. value, index,
  112. cp, size, 1000);
  113. status = min(status, 0);
  114. if (status < 0) {
  115. printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
  116. __func__, status);
  117. } else
  118. cmd_msg_dump(dev);
  119. }
  120. mutex_unlock(&dev->mutex);
  121. return status;
  122. }
  123. static void au0828_usb_disconnect(struct usb_interface *interface)
  124. {
  125. struct au0828_dev *dev = usb_get_intfdata(interface);
  126. dprintk(1, "%s()\n", __func__);
  127. /* Digital TV */
  128. au0828_dvb_unregister(dev);
  129. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
  130. au0828_analog_unregister(dev);
  131. /* I2C */
  132. au0828_i2c_unregister(dev);
  133. v4l2_device_unregister(&dev->v4l2_dev);
  134. usb_set_intfdata(interface, NULL);
  135. mutex_lock(&dev->mutex);
  136. dev->usbdev = NULL;
  137. mutex_unlock(&dev->mutex);
  138. kfree(dev);
  139. }
  140. static int au0828_usb_probe(struct usb_interface *interface,
  141. const struct usb_device_id *id)
  142. {
  143. int ifnum, retval;
  144. struct au0828_dev *dev;
  145. struct usb_device *usbdev = interface_to_usbdev(interface);
  146. ifnum = interface->altsetting->desc.bInterfaceNumber;
  147. if (ifnum != 0)
  148. return -ENODEV;
  149. dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
  150. le16_to_cpu(usbdev->descriptor.idVendor),
  151. le16_to_cpu(usbdev->descriptor.idProduct),
  152. ifnum);
  153. /*
  154. * Make sure we have 480 Mbps of bandwidth, otherwise things like
  155. * video stream wouldn't likely work, since 12 Mbps is generally
  156. * not enough even for most Digital TV streams.
  157. */
  158. if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
  159. printk(KERN_ERR "au0828: Device initialization failed.\n");
  160. printk(KERN_ERR "au0828: Device must be connected to a "
  161. "high-speed USB 2.0 port.\n");
  162. return -ENODEV;
  163. }
  164. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  165. if (dev == NULL) {
  166. printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
  167. return -ENOMEM;
  168. }
  169. mutex_init(&dev->mutex);
  170. mutex_init(&dev->dvb.lock);
  171. dev->usbdev = usbdev;
  172. dev->boardnr = id->driver_info;
  173. /* Create the v4l2_device */
  174. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  175. if (retval) {
  176. printk(KERN_ERR "%s() v4l2_device_register failed\n",
  177. __func__);
  178. kfree(dev);
  179. return -EIO;
  180. }
  181. /* Power Up the bridge */
  182. au0828_write(dev, REG_600, 1 << 4);
  183. /* Bring up the GPIO's and supporting devices */
  184. au0828_gpio_setup(dev);
  185. /* I2C */
  186. au0828_i2c_register(dev);
  187. /* Setup */
  188. au0828_card_setup(dev);
  189. /* Analog TV */
  190. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
  191. au0828_analog_register(dev, interface);
  192. /* Digital TV */
  193. au0828_dvb_register(dev);
  194. /* Store the pointer to the au0828_dev so it can be accessed in
  195. au0828_usb_disconnect */
  196. usb_set_intfdata(interface, dev);
  197. printk(KERN_INFO "Registered device AU0828 [%s]\n",
  198. dev->board.name == NULL ? "Unset" : dev->board.name);
  199. return 0;
  200. }
  201. static struct usb_driver au0828_usb_driver = {
  202. .name = DRIVER_NAME,
  203. .probe = au0828_usb_probe,
  204. .disconnect = au0828_usb_disconnect,
  205. .id_table = au0828_usb_id_table,
  206. };
  207. static int __init au0828_init(void)
  208. {
  209. int ret;
  210. if (au0828_debug & 1)
  211. printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
  212. if (au0828_debug & 2)
  213. printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
  214. if (au0828_debug & 4)
  215. printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
  216. if (au0828_debug & 8)
  217. printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
  218. __func__);
  219. printk(KERN_INFO "au0828 driver loaded\n");
  220. ret = usb_register(&au0828_usb_driver);
  221. if (ret)
  222. printk(KERN_ERR "usb_register failed, error = %d\n", ret);
  223. return ret;
  224. }
  225. static void __exit au0828_exit(void)
  226. {
  227. usb_deregister(&au0828_usb_driver);
  228. }
  229. module_init(au0828_init);
  230. module_exit(au0828_exit);
  231. MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
  232. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  233. MODULE_LICENSE("GPL");