au0828-core.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. 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. }
  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)
  78. {
  79. int status = -ENODEV;
  80. if (dev->usbdev) {
  81. /* cp must be memory that has been allocated by kmalloc */
  82. status = usb_control_msg(dev->usbdev,
  83. usb_sndctrlpipe(dev->usbdev, 0),
  84. request,
  85. USB_DIR_OUT | USB_TYPE_VENDOR |
  86. USB_RECIP_DEVICE,
  87. value, index, NULL, 0, 1000);
  88. status = min(status, 0);
  89. if (status < 0) {
  90. printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
  91. __func__, status);
  92. }
  93. }
  94. return status;
  95. }
  96. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  97. u16 index, unsigned char *cp, u16 size)
  98. {
  99. int status = -ENODEV;
  100. mutex_lock(&dev->mutex);
  101. if (dev->usbdev) {
  102. memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
  103. /* cp must be memory that has been allocated by kmalloc */
  104. status = usb_control_msg(dev->usbdev,
  105. usb_rcvctrlpipe(dev->usbdev, 0),
  106. request,
  107. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  108. value, index,
  109. cp, size, 1000);
  110. status = min(status, 0);
  111. if (status < 0) {
  112. printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
  113. __func__, status);
  114. } else
  115. cmd_msg_dump(dev);
  116. }
  117. mutex_unlock(&dev->mutex);
  118. return status;
  119. }
  120. static void au0828_usb_disconnect(struct usb_interface *interface)
  121. {
  122. struct au0828_dev *dev = usb_get_intfdata(interface);
  123. dprintk(1, "%s()\n", __func__);
  124. /* Digital TV */
  125. au0828_dvb_unregister(dev);
  126. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
  127. au0828_analog_unregister(dev);
  128. /* I2C */
  129. au0828_i2c_unregister(dev);
  130. v4l2_device_unregister(&dev->v4l2_dev);
  131. usb_set_intfdata(interface, NULL);
  132. mutex_lock(&dev->mutex);
  133. dev->usbdev = NULL;
  134. mutex_unlock(&dev->mutex);
  135. kfree(dev);
  136. }
  137. static int au0828_usb_probe(struct usb_interface *interface,
  138. const struct usb_device_id *id)
  139. {
  140. int ifnum, retval;
  141. struct au0828_dev *dev;
  142. struct usb_device *usbdev = interface_to_usbdev(interface);
  143. ifnum = interface->altsetting->desc.bInterfaceNumber;
  144. if (ifnum != 0)
  145. return -ENODEV;
  146. dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
  147. le16_to_cpu(usbdev->descriptor.idVendor),
  148. le16_to_cpu(usbdev->descriptor.idProduct),
  149. ifnum);
  150. /*
  151. * Make sure we have 480 Mbps of bandwidth, otherwise things like
  152. * video stream wouldn't likely work, since 12 Mbps is generally
  153. * not enough even for most Digital TV streams.
  154. */
  155. if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
  156. printk(KERN_ERR "au0828: Device initialization failed.\n");
  157. printk(KERN_ERR "au0828: Device must be connected to a "
  158. "high-speed USB 2.0 port.\n");
  159. return -ENODEV;
  160. }
  161. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  162. if (dev == NULL) {
  163. printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
  164. return -ENOMEM;
  165. }
  166. mutex_init(&dev->lock);
  167. mutex_lock(&dev->lock);
  168. mutex_init(&dev->mutex);
  169. mutex_init(&dev->dvb.lock);
  170. dev->usbdev = usbdev;
  171. dev->boardnr = id->driver_info;
  172. /* Create the v4l2_device */
  173. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  174. if (retval) {
  175. printk(KERN_ERR "%s() v4l2_device_register failed\n",
  176. __func__);
  177. mutex_unlock(&dev->lock);
  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. mutex_unlock(&dev->lock);
  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");