au0828-core.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "au0828.h"
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-common.h>
  26. #include <linux/mutex.h>
  27. /*
  28. * 1 = General debug messages
  29. * 2 = USB handling
  30. * 4 = I2C related
  31. * 8 = Bridge related
  32. * 16 = IR related
  33. */
  34. int au0828_debug;
  35. module_param_named(debug, au0828_debug, int, 0644);
  36. MODULE_PARM_DESC(debug,
  37. "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
  38. static unsigned int disable_usb_speed_check;
  39. module_param(disable_usb_speed_check, int, 0444);
  40. MODULE_PARM_DESC(disable_usb_speed_check,
  41. "override min bandwidth requirement of 480M bps");
  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);
  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. u8 result = 0;
  54. recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
  55. dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
  56. return result;
  57. }
  58. u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
  59. {
  60. dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
  61. return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
  62. }
  63. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  64. u16 index)
  65. {
  66. int status = -ENODEV;
  67. if (dev->usbdev) {
  68. /* cp must be memory that has been allocated by kmalloc */
  69. status = usb_control_msg(dev->usbdev,
  70. usb_sndctrlpipe(dev->usbdev, 0),
  71. request,
  72. USB_DIR_OUT | USB_TYPE_VENDOR |
  73. USB_RECIP_DEVICE,
  74. value, index, NULL, 0, 1000);
  75. status = min(status, 0);
  76. if (status < 0) {
  77. pr_err("%s() Failed sending control message, error %d.\n",
  78. __func__, status);
  79. }
  80. }
  81. return status;
  82. }
  83. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  84. u16 index, unsigned char *cp, u16 size)
  85. {
  86. int status = -ENODEV;
  87. mutex_lock(&dev->mutex);
  88. if (dev->usbdev) {
  89. status = usb_control_msg(dev->usbdev,
  90. usb_rcvctrlpipe(dev->usbdev, 0),
  91. request,
  92. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  93. value, index,
  94. dev->ctrlmsg, size, 1000);
  95. status = min(status, 0);
  96. if (status < 0) {
  97. pr_err("%s() Failed receiving control message, error %d.\n",
  98. __func__, status);
  99. }
  100. /* the host controller requires heap allocated memory, which
  101. is why we didn't just pass "cp" into usb_control_msg */
  102. memcpy(cp, dev->ctrlmsg, size);
  103. }
  104. mutex_unlock(&dev->mutex);
  105. return status;
  106. }
  107. static void au0828_usb_release(struct au0828_dev *dev)
  108. {
  109. /* I2C */
  110. au0828_i2c_unregister(dev);
  111. kfree(dev);
  112. }
  113. #ifdef CONFIG_VIDEO_AU0828_V4L2
  114. static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
  115. {
  116. struct au0828_dev *dev =
  117. container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
  118. v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
  119. v4l2_device_unregister(&dev->v4l2_dev);
  120. au0828_usb_release(dev);
  121. }
  122. #endif
  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. /* there is a small window after disconnect, before
  128. dev->usbdev is NULL, for poll (e.g: IR) try to access
  129. the device and fill the dmesg with error messages.
  130. Set the status so poll routines can check and avoid
  131. access after disconnect.
  132. */
  133. set_bit(DEV_DISCONNECTED, &dev->dev_state);
  134. au0828_rc_unregister(dev);
  135. /* Digital TV */
  136. au0828_dvb_unregister(dev);
  137. usb_set_intfdata(interface, NULL);
  138. mutex_lock(&dev->mutex);
  139. dev->usbdev = NULL;
  140. mutex_unlock(&dev->mutex);
  141. #ifdef CONFIG_VIDEO_AU0828_V4L2
  142. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) {
  143. au0828_analog_unregister(dev);
  144. v4l2_device_disconnect(&dev->v4l2_dev);
  145. v4l2_device_put(&dev->v4l2_dev);
  146. return;
  147. }
  148. #endif
  149. au0828_usb_release(dev);
  150. }
  151. static int au0828_usb_probe(struct usb_interface *interface,
  152. const struct usb_device_id *id)
  153. {
  154. int ifnum;
  155. int retval = 0;
  156. struct au0828_dev *dev;
  157. struct usb_device *usbdev = interface_to_usbdev(interface);
  158. ifnum = interface->altsetting->desc.bInterfaceNumber;
  159. if (ifnum != 0)
  160. return -ENODEV;
  161. dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
  162. le16_to_cpu(usbdev->descriptor.idVendor),
  163. le16_to_cpu(usbdev->descriptor.idProduct),
  164. ifnum);
  165. /*
  166. * Make sure we have 480 Mbps of bandwidth, otherwise things like
  167. * video stream wouldn't likely work, since 12 Mbps is generally
  168. * not enough even for most Digital TV streams.
  169. */
  170. if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
  171. pr_err("au0828: Device initialization failed.\n");
  172. pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
  173. return -ENODEV;
  174. }
  175. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  176. if (dev == NULL) {
  177. pr_err("%s() Unable to allocate memory\n", __func__);
  178. return -ENOMEM;
  179. }
  180. mutex_init(&dev->lock);
  181. mutex_lock(&dev->lock);
  182. mutex_init(&dev->mutex);
  183. mutex_init(&dev->dvb.lock);
  184. dev->usbdev = usbdev;
  185. dev->boardnr = id->driver_info;
  186. dev->board = au0828_boards[dev->boardnr];
  187. #ifdef CONFIG_VIDEO_AU0828_V4L2
  188. dev->v4l2_dev.release = au0828_usb_v4l2_release;
  189. /* Create the v4l2_device */
  190. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  191. if (retval) {
  192. pr_err("%s() v4l2_device_register failed\n",
  193. __func__);
  194. mutex_unlock(&dev->lock);
  195. kfree(dev);
  196. return retval;
  197. }
  198. /* This control handler will inherit the controls from au8522 */
  199. retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
  200. if (retval) {
  201. pr_err("%s() v4l2_ctrl_handler_init failed\n",
  202. __func__);
  203. mutex_unlock(&dev->lock);
  204. kfree(dev);
  205. return retval;
  206. }
  207. dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
  208. #endif
  209. /* Power Up the bridge */
  210. au0828_write(dev, REG_600, 1 << 4);
  211. /* Bring up the GPIO's and supporting devices */
  212. au0828_gpio_setup(dev);
  213. /* I2C */
  214. au0828_i2c_register(dev);
  215. /* Setup */
  216. au0828_card_setup(dev);
  217. #ifdef CONFIG_VIDEO_AU0828_V4L2
  218. /* Analog TV */
  219. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
  220. au0828_analog_register(dev, interface);
  221. #endif
  222. /* Digital TV */
  223. retval = au0828_dvb_register(dev);
  224. if (retval)
  225. pr_err("%s() au0282_dev_register failed\n",
  226. __func__);
  227. /* Remote controller */
  228. au0828_rc_register(dev);
  229. /*
  230. * Store the pointer to the au0828_dev so it can be accessed in
  231. * au0828_usb_disconnect
  232. */
  233. usb_set_intfdata(interface, dev);
  234. pr_info("Registered device AU0828 [%s]\n",
  235. dev->board.name == NULL ? "Unset" : dev->board.name);
  236. mutex_unlock(&dev->lock);
  237. return retval;
  238. }
  239. static int au0828_suspend(struct usb_interface *interface,
  240. pm_message_t message)
  241. {
  242. struct au0828_dev *dev = usb_get_intfdata(interface);
  243. if (!dev)
  244. return 0;
  245. pr_info("Suspend\n");
  246. au0828_rc_suspend(dev);
  247. au0828_v4l2_suspend(dev);
  248. au0828_dvb_suspend(dev);
  249. /* FIXME: should suspend also ATV/DTV */
  250. return 0;
  251. }
  252. static int au0828_resume(struct usb_interface *interface)
  253. {
  254. struct au0828_dev *dev = usb_get_intfdata(interface);
  255. if (!dev)
  256. return 0;
  257. pr_info("Resume\n");
  258. /* Power Up the bridge */
  259. au0828_write(dev, REG_600, 1 << 4);
  260. /* Bring up the GPIO's and supporting devices */
  261. au0828_gpio_setup(dev);
  262. au0828_rc_resume(dev);
  263. au0828_v4l2_resume(dev);
  264. au0828_dvb_resume(dev);
  265. /* FIXME: should resume also ATV/DTV */
  266. return 0;
  267. }
  268. static struct usb_driver au0828_usb_driver = {
  269. .name = KBUILD_MODNAME,
  270. .probe = au0828_usb_probe,
  271. .disconnect = au0828_usb_disconnect,
  272. .id_table = au0828_usb_id_table,
  273. .suspend = au0828_suspend,
  274. .resume = au0828_resume,
  275. .reset_resume = au0828_resume,
  276. };
  277. static int __init au0828_init(void)
  278. {
  279. int ret;
  280. if (au0828_debug & 1)
  281. pr_info("%s() Debugging is enabled\n", __func__);
  282. if (au0828_debug & 2)
  283. pr_info("%s() USB Debugging is enabled\n", __func__);
  284. if (au0828_debug & 4)
  285. pr_info("%s() I2C Debugging is enabled\n", __func__);
  286. if (au0828_debug & 8)
  287. pr_info("%s() Bridge Debugging is enabled\n",
  288. __func__);
  289. if (au0828_debug & 16)
  290. pr_info("%s() IR Debugging is enabled\n",
  291. __func__);
  292. pr_info("au0828 driver loaded\n");
  293. ret = usb_register(&au0828_usb_driver);
  294. if (ret)
  295. pr_err("usb_register failed, error = %d\n", ret);
  296. return ret;
  297. }
  298. static void __exit au0828_exit(void)
  299. {
  300. usb_deregister(&au0828_usb_driver);
  301. }
  302. module_init(au0828_init);
  303. module_exit(au0828_exit);
  304. MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
  305. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  306. MODULE_LICENSE("GPL");
  307. MODULE_VERSION("0.0.3");