usbtv-core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Fushicai USBTV007 Audio-Video Grabber Driver
  3. *
  4. * Product web site:
  5. * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
  6. *
  7. * Following LWN articles were very useful in construction of this driver:
  8. * Video4Linux2 API series: http://lwn.net/Articles/203924/
  9. * videobuf2 API explanation: http://lwn.net/Articles/447435/
  10. * Thanks go to Jonathan Corbet for providing this quality documentation.
  11. * He is awesome.
  12. *
  13. * Copyright (c) 2013 Lubomir Rintel
  14. * All rights reserved.
  15. * No physical hardware was harmed running Windows during the
  16. * reverse-engineering activity
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions, and the following disclaimer,
  23. * without modification.
  24. * 2. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * Alternatively, this software may be distributed under the terms of the
  28. * GNU General Public License ("GPL").
  29. */
  30. #include "usbtv.h"
  31. int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
  32. {
  33. int ret;
  34. int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
  35. int i;
  36. for (i = 0; i < size; i++) {
  37. u16 index = regs[i][0];
  38. u16 value = regs[i][1];
  39. ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
  40. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  41. value, index, NULL, 0, 0);
  42. if (ret < 0)
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. static int usbtv_probe(struct usb_interface *intf,
  48. const struct usb_device_id *id)
  49. {
  50. int ret;
  51. int size;
  52. struct device *dev = &intf->dev;
  53. struct usbtv *usbtv;
  54. /* Checks that the device is what we think it is. */
  55. if (intf->num_altsetting != 2)
  56. return -ENODEV;
  57. if (intf->altsetting[1].desc.bNumEndpoints != 4)
  58. return -ENODEV;
  59. /* Packet size is split into 11 bits of base size and count of
  60. * extra multiplies of it.*/
  61. size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
  62. size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
  63. /* Device structure */
  64. usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
  65. if (usbtv == NULL)
  66. return -ENOMEM;
  67. usbtv->dev = dev;
  68. usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
  69. usbtv->iso_size = size;
  70. usb_set_intfdata(intf, usbtv);
  71. ret = usbtv_video_init(usbtv);
  72. if (ret < 0)
  73. goto usbtv_video_fail;
  74. ret = usbtv_audio_init(usbtv);
  75. if (ret < 0)
  76. goto usbtv_audio_fail;
  77. /* for simplicity we exploit the v4l2_device reference counting */
  78. v4l2_device_get(&usbtv->v4l2_dev);
  79. dev_info(dev, "Fushicai USBTV007 Audio-Video Grabber\n");
  80. return 0;
  81. usbtv_audio_fail:
  82. /* we must not free at this point */
  83. usb_get_dev(usbtv->udev);
  84. usbtv_video_free(usbtv);
  85. usbtv_video_fail:
  86. usb_set_intfdata(intf, NULL);
  87. usb_put_dev(usbtv->udev);
  88. kfree(usbtv);
  89. return ret;
  90. }
  91. static void usbtv_disconnect(struct usb_interface *intf)
  92. {
  93. struct usbtv *usbtv = usb_get_intfdata(intf);
  94. usb_set_intfdata(intf, NULL);
  95. if (!usbtv)
  96. return;
  97. usbtv_audio_free(usbtv);
  98. usbtv_video_free(usbtv);
  99. usb_put_dev(usbtv->udev);
  100. usbtv->udev = NULL;
  101. /* the usbtv structure will be deallocated when v4l2 will be
  102. done using it */
  103. v4l2_device_put(&usbtv->v4l2_dev);
  104. }
  105. static struct usb_device_id usbtv_id_table[] = {
  106. { USB_DEVICE(0x1b71, 0x3002) },
  107. { USB_DEVICE(0x1f71, 0x3301) },
  108. {}
  109. };
  110. MODULE_DEVICE_TABLE(usb, usbtv_id_table);
  111. MODULE_AUTHOR("Lubomir Rintel, Federico Simoncelli");
  112. MODULE_DESCRIPTION("Fushicai USBTV007 Audio-Video Grabber Driver");
  113. MODULE_LICENSE("Dual BSD/GPL");
  114. static struct usb_driver usbtv_usb_driver = {
  115. .name = "usbtv",
  116. .id_table = usbtv_id_table,
  117. .probe = usbtv_probe,
  118. .disconnect = usbtv_disconnect,
  119. };
  120. module_usb_driver(usbtv_usb_driver);