au0828-core.c 7.9 KB

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