e89e48b8f7283bfe00b376978065e7f2a64f01c2
[pandora-kernel.git] / drivers / media / usb / usbtv / usbtv-core.c
1 /*
2  * Fushicai USBTV007 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
31 #include <linux/module.h>
32
33 #include "usbtv.h"
34
35 int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
36 {
37         int ret;
38         int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
39         int i;
40
41         for (i = 0; i < size; i++) {
42                 u16 index = regs[i][0];
43                 u16 value = regs[i][1];
44
45                 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
46                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
47                         value, index, NULL, 0, 0);
48                 if (ret < 0)
49                         return ret;
50         }
51
52         return 0;
53 }
54
55 static int usbtv_probe(struct usb_interface *intf,
56         const struct usb_device_id *id)
57 {
58         int ret;
59         int size;
60         struct device *dev = &intf->dev;
61         struct usbtv *usbtv;
62
63         /* Checks that the device is what we think it is. */
64         if (intf->num_altsetting != 2)
65                 return -ENODEV;
66         if (intf->altsetting[1].desc.bNumEndpoints != 4)
67                 return -ENODEV;
68
69         /* Packet size is split into 11 bits of base size and count of
70          * extra multiplies of it.*/
71         size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
72         size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
73
74         /* Device structure */
75         usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
76         if (usbtv == NULL)
77                 return -ENOMEM;
78         usbtv->dev = dev;
79         usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
80
81         usbtv->iso_size = size;
82
83         usb_set_intfdata(intf, usbtv);
84
85         ret = usbtv_video_init(usbtv);
86         if (ret < 0)
87                 goto usbtv_video_fail;
88
89         /* for simplicity we exploit the v4l2_device reference counting */
90         v4l2_device_get(&usbtv->v4l2_dev);
91
92         dev_info(dev, "Fushicai USBTV007 Video Grabber\n");
93         return 0;
94
95 usbtv_video_fail:
96         kfree(usbtv);
97
98         return ret;
99 }
100
101 static void usbtv_disconnect(struct usb_interface *intf)
102 {
103         struct usbtv *usbtv = usb_get_intfdata(intf);
104         usb_set_intfdata(intf, NULL);
105
106         if (!usbtv)
107                 return;
108
109         usbtv_video_free(usbtv);
110
111         usb_put_dev(usbtv->udev);
112         usbtv->udev = NULL;
113
114         /* the usbtv structure will be deallocated when v4l2 will be
115            done using it */
116         v4l2_device_put(&usbtv->v4l2_dev);
117 }
118
119 struct usb_device_id usbtv_id_table[] = {
120         { USB_DEVICE(0x1b71, 0x3002) },
121         {}
122 };
123 MODULE_DEVICE_TABLE(usb, usbtv_id_table);
124
125 MODULE_AUTHOR("Lubomir Rintel");
126 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver");
127 MODULE_LICENSE("Dual BSD/GPL");
128
129 struct usb_driver usbtv_usb_driver = {
130         .name = "usbtv",
131         .id_table = usbtv_id_table,
132         .probe = usbtv_probe,
133         .disconnect = usbtv_disconnect,
134 };
135
136 module_usb_driver(usbtv_usb_driver);