au0828-core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 atomic_t au0828_instance = ATOMIC_INIT(0);
  36. #define _AU0828_BULKPIPE 0x03
  37. #define _BULKPIPESIZE 0xffff
  38. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  39. u16 index, unsigned char *cp, u16 size);
  40. static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  41. u16 index, unsigned char *cp, u16 size);
  42. /* USB Direction */
  43. #define CMD_REQUEST_IN 0x00
  44. #define CMD_REQUEST_OUT 0x01
  45. u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
  46. {
  47. recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
  48. dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]);
  49. return dev->ctrlmsg[0];
  50. }
  51. u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
  52. {
  53. dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
  54. return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
  55. dev->ctrlmsg, 0);
  56. }
  57. static void cmd_msg_dump(struct au0828_dev *dev)
  58. {
  59. int i;
  60. for (i = 0; i < sizeof(dev->ctrlmsg); i += 16)
  61. dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x "
  62. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  63. __func__,
  64. dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
  65. dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
  66. dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
  67. dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
  68. dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
  69. dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
  70. dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
  71. dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
  72. }
  73. static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
  74. u16 index, unsigned char *cp, u16 size)
  75. {
  76. int status = -ENODEV;
  77. mutex_lock(&dev->mutex);
  78. if (dev->usbdev) {
  79. /* cp must be memory that has been allocated by kmalloc */
  80. status = usb_control_msg(dev->usbdev,
  81. usb_sndctrlpipe(dev->usbdev, 0),
  82. request,
  83. USB_DIR_OUT | USB_TYPE_VENDOR |
  84. USB_RECIP_DEVICE,
  85. value, index,
  86. cp, size, 1000);
  87. status = min(status, 0);
  88. if (status < 0) {
  89. printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
  90. __func__, status);
  91. }
  92. }
  93. mutex_unlock(&dev->mutex);
  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, i;
  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. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  151. if (dev == NULL) {
  152. printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
  153. return -ENOMEM;
  154. }
  155. mutex_init(&dev->mutex);
  156. mutex_init(&dev->dvb.lock);
  157. dev->usbdev = usbdev;
  158. dev->boardnr = id->driver_info;
  159. usb_set_intfdata(interface, dev);
  160. /* Create the v4l2_device */
  161. i = atomic_inc_return(&au0828_instance) - 1;
  162. snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s-%03d",
  163. "au0828", i);
  164. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  165. if (retval) {
  166. printk(KERN_ERR "%s() v4l2_device_register failed\n",
  167. __func__);
  168. kfree(dev);
  169. return -EIO;
  170. }
  171. /* Power Up the bridge */
  172. au0828_write(dev, REG_600, 1 << 4);
  173. /* Bring up the GPIO's and supporting devices */
  174. au0828_gpio_setup(dev);
  175. /* I2C */
  176. au0828_i2c_register(dev);
  177. /* Setup */
  178. au0828_card_setup(dev);
  179. /* Analog TV */
  180. if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
  181. au0828_analog_register(dev, interface);
  182. /* Digital TV */
  183. au0828_dvb_register(dev);
  184. printk(KERN_INFO "Registered device AU0828 [%s]\n",
  185. dev->board.name == NULL ? "Unset" : dev->board.name);
  186. return 0;
  187. }
  188. static struct usb_driver au0828_usb_driver = {
  189. .name = DRIVER_NAME,
  190. .probe = au0828_usb_probe,
  191. .disconnect = au0828_usb_disconnect,
  192. .id_table = au0828_usb_id_table,
  193. };
  194. static int __init au0828_init(void)
  195. {
  196. int ret;
  197. if (au0828_debug & 1)
  198. printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
  199. if (au0828_debug & 2)
  200. printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
  201. if (au0828_debug & 4)
  202. printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
  203. if (au0828_debug & 8)
  204. printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
  205. __func__);
  206. printk(KERN_INFO "au0828 driver loaded\n");
  207. ret = usb_register(&au0828_usb_driver);
  208. if (ret)
  209. printk(KERN_ERR "usb_register failed, error = %d\n", ret);
  210. return ret;
  211. }
  212. static void __exit au0828_exit(void)
  213. {
  214. usb_deregister(&au0828_usb_driver);
  215. }
  216. module_init(au0828_init);
  217. module_exit(au0828_exit);
  218. MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
  219. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  220. MODULE_LICENSE("GPL");