Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6.git] / drivers / media / video / gspca / benq.c
1 /*
2  * Benq DC E300 subdriver
3  *
4  * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
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  * 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  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #define MODULE_NAME "benq"
24
25 #include "gspca.h"
26
27 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
28 MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
29 MODULE_LICENSE("GPL");
30
31 /* specific webcam descriptor */
32 struct sd {
33         struct gspca_dev gspca_dev;     /* !! must be the first item */
34 };
35
36 /* V4L2 controls supported by the driver */
37 static const struct ctrl sd_ctrls[] = {
38 };
39
40 static const struct v4l2_pix_format vga_mode[] = {
41         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
42                 .bytesperline = 320,
43                 .sizeimage = 320 * 240 * 3 / 8 + 590,
44                 .colorspace = V4L2_COLORSPACE_JPEG},
45 };
46
47 static void sd_isoc_irq(struct urb *urb);
48
49 /* -- write a register -- */
50 static void reg_w(struct gspca_dev *gspca_dev,
51                         u16 value, u16 index)
52 {
53         struct usb_device *dev = gspca_dev->dev;
54         int ret;
55
56         if (gspca_dev->usb_err < 0)
57                 return;
58         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
59                         0x02,
60                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
61                         value,
62                         index,
63                         NULL,
64                         0,
65                         500);
66         if (ret < 0) {
67                 pr_err("reg_w err %d\n", ret);
68                 gspca_dev->usb_err = ret;
69         }
70 }
71
72 /* this function is called at probe time */
73 static int sd_config(struct gspca_dev *gspca_dev,
74                         const struct usb_device_id *id)
75 {
76         gspca_dev->cam.cam_mode = vga_mode;
77         gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
78         gspca_dev->cam.no_urb_create = 1;
79         return 0;
80 }
81
82 /* this function is called at probe and resume time */
83 static int sd_init(struct gspca_dev *gspca_dev)
84 {
85         return 0;
86 }
87
88 /* -- start the camera -- */
89 static int sd_start(struct gspca_dev *gspca_dev)
90 {
91         struct urb *urb;
92         int i, n;
93
94         /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
95 #if MAX_NURBS < 4
96 #error "Not enough URBs in the gspca table"
97 #endif
98 #define SD_PKT_SZ 64
99 #define SD_NPKT 32
100         for (n = 0; n < 4; n++) {
101                 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
102                 if (!urb) {
103                         pr_err("usb_alloc_urb failed\n");
104                         return -ENOMEM;
105                 }
106                 gspca_dev->urb[n] = urb;
107                 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
108                                                 SD_PKT_SZ * SD_NPKT,
109                                                 GFP_KERNEL,
110                                                 &urb->transfer_dma);
111
112                 if (urb->transfer_buffer == NULL) {
113                         pr_err("usb_alloc_coherent failed\n");
114                         return -ENOMEM;
115                 }
116                 urb->dev = gspca_dev->dev;
117                 urb->context = gspca_dev;
118                 urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
119                 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
120                                         n & 1 ? 0x82 : 0x83);
121                 urb->transfer_flags = URB_ISO_ASAP
122                                         | URB_NO_TRANSFER_DMA_MAP;
123                 urb->interval = 1;
124                 urb->complete = sd_isoc_irq;
125                 urb->number_of_packets = SD_NPKT;
126                 for (i = 0; i < SD_NPKT; i++) {
127                         urb->iso_frame_desc[i].length = SD_PKT_SZ;
128                         urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
129                 }
130         }
131
132         return gspca_dev->usb_err;
133 }
134
135 static void sd_stopN(struct gspca_dev *gspca_dev)
136 {
137         struct usb_interface *intf;
138
139         reg_w(gspca_dev, 0x003c, 0x0003);
140         reg_w(gspca_dev, 0x003c, 0x0004);
141         reg_w(gspca_dev, 0x003c, 0x0005);
142         reg_w(gspca_dev, 0x003c, 0x0006);
143         reg_w(gspca_dev, 0x003c, 0x0007);
144
145         intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
146         usb_set_interface(gspca_dev->dev, gspca_dev->iface,
147                                         intf->num_altsetting - 1);
148 }
149
150 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
151                         u8 *data,               /* isoc packet */
152                         int len)                /* iso packet length */
153 {
154         /* unused */
155 }
156
157 /* reception of an URB */
158 static void sd_isoc_irq(struct urb *urb)
159 {
160         struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
161         struct urb *urb0;
162         u8 *data;
163         int i, st;
164
165         PDEBUG(D_PACK, "sd isoc irq");
166         if (!gspca_dev->streaming)
167                 return;
168         if (urb->status != 0) {
169                 if (urb->status == -ESHUTDOWN)
170                         return;         /* disconnection */
171 #ifdef CONFIG_PM
172                 if (gspca_dev->frozen)
173                         return;
174 #endif
175                 pr_err("urb status: %d\n", urb->status);
176                 return;
177         }
178
179         /* if this is a control URN (ep 0x83), wait */
180         if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
181                 return;
182
183         /* scan both received URBs */
184         if (urb == gspca_dev->urb[1])
185                 urb0 = gspca_dev->urb[0];
186         else
187                 urb0 = gspca_dev->urb[2];
188         for (i = 0; i < urb->number_of_packets; i++) {
189
190                 /* check the packet status and length */
191                 if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
192                     || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
193                         PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
194                                 urb0->iso_frame_desc[i].actual_length,
195                                 urb->iso_frame_desc[i].actual_length);
196                         gspca_dev->last_packet_type = DISCARD_PACKET;
197                         continue;
198                 }
199                 st = urb0->iso_frame_desc[i].status;
200                 if (st == 0)
201                         st = urb->iso_frame_desc[i].status;
202                 if (st) {
203                         pr_err("ISOC data error: [%d] status=%d\n",
204                                 i, st);
205                         gspca_dev->last_packet_type = DISCARD_PACKET;
206                         continue;
207                 }
208
209                 /*
210                  * The images are received in URBs of different endpoints
211                  * (0x83 and 0x82).
212                  * Image pieces in URBs of ep 0x83 are continuated in URBs of
213                  * ep 0x82 of the same index.
214                  * The packets in the URBs of endpoint 0x83 start with:
215                  *      - 80 ba/bb 00 00 = start of image followed by 'ff d8'
216                  *      - 04 ba/bb oo oo = image piece
217                  *              where 'oo oo' is the image offset
218                                                 (not cheked)
219                  *      - (other -> bad frame)
220                  * The images are JPEG encoded with full header and
221                  * normal ff escape.
222                  * The end of image ('ff d9') may occur in any URB.
223                  * (not cheked)
224                  */
225                 data = (u8 *) urb0->transfer_buffer
226                                         + urb0->iso_frame_desc[i].offset;
227                 if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
228
229                         /* new image */
230                         gspca_frame_add(gspca_dev, LAST_PACKET,
231                                         NULL, 0);
232                         gspca_frame_add(gspca_dev, FIRST_PACKET,
233                                         data + 4, SD_PKT_SZ - 4);
234                 } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
235                         gspca_frame_add(gspca_dev, INTER_PACKET,
236                                         data + 4, SD_PKT_SZ - 4);
237                 } else {
238                         gspca_dev->last_packet_type = DISCARD_PACKET;
239                         continue;
240                 }
241                 data = (u8 *) urb->transfer_buffer
242                                         + urb->iso_frame_desc[i].offset;
243                         gspca_frame_add(gspca_dev, INTER_PACKET,
244                                         data, SD_PKT_SZ);
245         }
246
247         /* resubmit the URBs */
248         st = usb_submit_urb(urb0, GFP_ATOMIC);
249         if (st < 0)
250                 pr_err("usb_submit_urb(0) ret %d\n", st);
251         st = usb_submit_urb(urb, GFP_ATOMIC);
252         if (st < 0)
253                 pr_err("usb_submit_urb() ret %d\n", st);
254 }
255
256 /* sub-driver description */
257 static const struct sd_desc sd_desc = {
258         .name = MODULE_NAME,
259         .ctrls = sd_ctrls,
260         .nctrls = ARRAY_SIZE(sd_ctrls),
261         .config = sd_config,
262         .init = sd_init,
263         .start = sd_start,
264         .stopN = sd_stopN,
265         .pkt_scan = sd_pkt_scan,
266 };
267
268 /* -- module initialisation -- */
269 static const struct usb_device_id device_table[] = {
270         {USB_DEVICE(0x04a5, 0x3035)},
271         {}
272 };
273 MODULE_DEVICE_TABLE(usb, device_table);
274
275 /* -- device connect -- */
276 static int sd_probe(struct usb_interface *intf,
277                         const struct usb_device_id *id)
278 {
279         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
280                                 THIS_MODULE);
281 }
282
283 static struct usb_driver sd_driver = {
284         .name = MODULE_NAME,
285         .id_table = device_table,
286         .probe = sd_probe,
287         .disconnect = gspca_disconnect,
288 #ifdef CONFIG_PM
289         .suspend = gspca_suspend,
290         .resume = gspca_resume,
291 #endif
292 };
293
294 module_usb_driver(sd_driver);