au0828-core.c 6.7 KB

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