usbtv-core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2013 Lubomir Rintel
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL").
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * Fushicai USBTV007 Audio-Video Grabber Driver
  31. *
  32. * Product web site:
  33. * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
  34. *
  35. * Following LWN articles were very useful in construction of this driver:
  36. * Video4Linux2 API series: http://lwn.net/Articles/203924/
  37. * videobuf2 API explanation: http://lwn.net/Articles/447435/
  38. * Thanks go to Jonathan Corbet for providing this quality documentation.
  39. * He is awesome.
  40. *
  41. * No physical hardware was harmed running Windows during the
  42. * reverse-engineering activity
  43. */
  44. #include "usbtv.h"
  45. int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
  46. {
  47. int ret;
  48. int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
  49. int i;
  50. for (i = 0; i < size; i++) {
  51. u16 index = regs[i][0];
  52. u16 value = regs[i][1];
  53. ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
  54. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  55. value, index, NULL, 0, USB_CTRL_GET_TIMEOUT);
  56. if (ret < 0)
  57. return ret;
  58. }
  59. return 0;
  60. }
  61. static int usbtv_probe(struct usb_interface *intf,
  62. const struct usb_device_id *id)
  63. {
  64. int ret;
  65. int size;
  66. struct device *dev = &intf->dev;
  67. struct usbtv *usbtv;
  68. struct usb_host_endpoint *ep;
  69. /* Checks that the device is what we think it is. */
  70. if (intf->num_altsetting != 2)
  71. return -ENODEV;
  72. if (intf->altsetting[1].desc.bNumEndpoints != 4)
  73. return -ENODEV;
  74. ep = &intf->altsetting[1].endpoint[0];
  75. /* Packet size is split into 11 bits of base size and count of
  76. * extra multiplies of it.*/
  77. size = usb_endpoint_maxp(&ep->desc);
  78. size = size * usb_endpoint_maxp_mult(&ep->desc);
  79. /* Device structure */
  80. usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
  81. if (usbtv == NULL)
  82. return -ENOMEM;
  83. usbtv->dev = dev;
  84. usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
  85. usbtv->iso_size = size;
  86. usb_set_intfdata(intf, usbtv);
  87. ret = usbtv_video_init(usbtv);
  88. if (ret < 0)
  89. goto usbtv_video_fail;
  90. ret = usbtv_audio_init(usbtv);
  91. if (ret < 0)
  92. goto usbtv_audio_fail;
  93. /* for simplicity we exploit the v4l2_device reference counting */
  94. v4l2_device_get(&usbtv->v4l2_dev);
  95. dev_info(dev, "Fushicai USBTV007 Audio-Video Grabber\n");
  96. return 0;
  97. usbtv_audio_fail:
  98. /* we must not free at this point */
  99. usb_get_dev(usbtv->udev);
  100. usbtv_video_free(usbtv);
  101. usbtv_video_fail:
  102. usb_set_intfdata(intf, NULL);
  103. usb_put_dev(usbtv->udev);
  104. kfree(usbtv);
  105. return ret;
  106. }
  107. static void usbtv_disconnect(struct usb_interface *intf)
  108. {
  109. struct usbtv *usbtv = usb_get_intfdata(intf);
  110. usb_set_intfdata(intf, NULL);
  111. if (!usbtv)
  112. return;
  113. usbtv_audio_free(usbtv);
  114. usbtv_video_free(usbtv);
  115. usb_put_dev(usbtv->udev);
  116. usbtv->udev = NULL;
  117. /* the usbtv structure will be deallocated when v4l2 will be
  118. done using it */
  119. v4l2_device_put(&usbtv->v4l2_dev);
  120. }
  121. static const struct usb_device_id usbtv_id_table[] = {
  122. { USB_DEVICE(0x1b71, 0x3002) },
  123. { USB_DEVICE(0x1f71, 0x3301) },
  124. { USB_DEVICE(0x1f71, 0x3306) },
  125. {}
  126. };
  127. MODULE_DEVICE_TABLE(usb, usbtv_id_table);
  128. MODULE_AUTHOR("Lubomir Rintel, Federico Simoncelli");
  129. MODULE_DESCRIPTION("Fushicai USBTV007 Audio-Video Grabber Driver");
  130. MODULE_LICENSE("Dual BSD/GPL");
  131. static struct usb_driver usbtv_usb_driver = {
  132. .name = "usbtv",
  133. .id_table = usbtv_id_table,
  134. .probe = usbtv_probe,
  135. .disconnect = usbtv_disconnect,
  136. };
  137. module_usb_driver(usbtv_usb_driver);