au0828-core.c 7.5 KB

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