blob: d8ce7d75ff187e3ac3b84d4eec77021a2c27a2f7 [file] [log] [blame]
Federico Simoncellia3550ea2014-01-07 19:13:21 -03001/*
Federico Simoncellia3550ea2014-01-07 19:13:21 -03002 * Copyright (c) 2013 Lubomir Rintel
3 * All rights reserved.
Federico Simoncellia3550ea2014-01-07 19:13:21 -03004 *
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").
Lubomir Rintel5ae1e2b2016-06-01 10:03:44 -030016 *
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
Federico Simoncellia3550ea2014-01-07 19:13:21 -030043 */
44
Federico Simoncellia3550ea2014-01-07 19:13:21 -030045#include "usbtv.h"
46
47int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
48{
49 int ret;
50 int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
51 int i;
52
53 for (i = 0; i < size; i++) {
54 u16 index = regs[i][0];
55 u16 value = regs[i][1];
56
57 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
58 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Johan Hovold22365712020-01-13 18:18:18 +010059 value, index, NULL, 0, USB_CTRL_GET_TIMEOUT);
Federico Simoncellia3550ea2014-01-07 19:13:21 -030060 if (ret < 0)
61 return ret;
62 }
63
64 return 0;
65}
66
67static int usbtv_probe(struct usb_interface *intf,
68 const struct usb_device_id *id)
69{
70 int ret;
71 int size;
72 struct device *dev = &intf->dev;
73 struct usbtv *usbtv;
74
75 /* Checks that the device is what we think it is. */
76 if (intf->num_altsetting != 2)
77 return -ENODEV;
78 if (intf->altsetting[1].desc.bNumEndpoints != 4)
79 return -ENODEV;
80
81 /* Packet size is split into 11 bits of base size and count of
82 * extra multiplies of it.*/
83 size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
84 size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
85
86 /* Device structure */
87 usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
88 if (usbtv == NULL)
89 return -ENOMEM;
90 usbtv->dev = dev;
91 usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
92
93 usbtv->iso_size = size;
94
95 usb_set_intfdata(intf, usbtv);
96
97 ret = usbtv_video_init(usbtv);
98 if (ret < 0)
99 goto usbtv_video_fail;
100
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300101 ret = usbtv_audio_init(usbtv);
102 if (ret < 0)
103 goto usbtv_audio_fail;
104
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300105 /* for simplicity we exploit the v4l2_device reference counting */
106 v4l2_device_get(&usbtv->v4l2_dev);
107
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300108 dev_info(dev, "Fushicai USBTV007 Audio-Video Grabber\n");
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300109 return 0;
110
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300111usbtv_audio_fail:
Oliver Neukum50cd7752018-01-08 09:21:07 -0500112 /* we must not free at this point */
113 usb_get_dev(usbtv->udev);
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300114 usbtv_video_free(usbtv);
115
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300116usbtv_video_fail:
Alexey Khoroshilovebdae652014-05-23 17:47:07 -0300117 usb_set_intfdata(intf, NULL);
118 usb_put_dev(usbtv->udev);
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300119 kfree(usbtv);
120
121 return ret;
122}
123
124static void usbtv_disconnect(struct usb_interface *intf)
125{
126 struct usbtv *usbtv = usb_get_intfdata(intf);
Amber Thrall146af9c2014-09-20 01:03:15 -0300127
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300128 usb_set_intfdata(intf, NULL);
129
130 if (!usbtv)
131 return;
132
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300133 usbtv_audio_free(usbtv);
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300134 usbtv_video_free(usbtv);
135
136 usb_put_dev(usbtv->udev);
137 usbtv->udev = NULL;
138
139 /* the usbtv structure will be deallocated when v4l2 will be
140 done using it */
141 v4l2_device_put(&usbtv->v4l2_dev);
142}
143
Fengguang Wu9b058372014-02-04 06:02:02 -0300144static struct usb_device_id usbtv_id_table[] = {
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300145 { USB_DEVICE(0x1b71, 0x3002) },
Icenowy Zhengb4bfc8e2017-04-16 02:51:16 -0400146 { USB_DEVICE(0x1f71, 0x3301) },
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300147 {}
148};
149MODULE_DEVICE_TABLE(usb, usbtv_id_table);
150
Federico Simoncelli63ddf682014-08-11 18:42:22 -0300151MODULE_AUTHOR("Lubomir Rintel, Federico Simoncelli");
152MODULE_DESCRIPTION("Fushicai USBTV007 Audio-Video Grabber Driver");
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300153MODULE_LICENSE("Dual BSD/GPL");
154
Fengguang Wu9b058372014-02-04 06:02:02 -0300155static struct usb_driver usbtv_usb_driver = {
Federico Simoncellia3550ea2014-01-07 19:13:21 -0300156 .name = "usbtv",
157 .id_table = usbtv_id_table,
158 .probe = usbtv_probe,
159 .disconnect = usbtv_disconnect,
160};
161
162module_usb_driver(usbtv_usb_driver);