]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/gspca/finepix.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-omap-h63xx.git] / drivers / media / video / gspca / finepix.c
1 /*
2  * Fujifilm Finepix subdriver
3  *
4  * Copyright (C) 2008 Frank Zago
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 MODULE_NAME "finepix"
22
23 #include "gspca.h"
24
25 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
26 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
27 MODULE_LICENSE("GPL");
28
29 /* Default timeout, in ms */
30 #define FPIX_TIMEOUT (HZ / 10)
31
32 /* Maximum transfer size to use. The windows driver reads by chunks of
33  * 0x2000 bytes, so do the same. Note: reading more seems to work
34  * too. */
35 #define FPIX_MAX_TRANSFER 0x2000
36
37 /* Structure to hold all of our device specific stuff */
38 struct usb_fpix {
39         struct gspca_dev gspca_dev;     /* !! must be the first item */
40
41         /*
42          * USB stuff
43          */
44         struct usb_ctrlrequest ctrlreq;
45         struct urb *control_urb;
46         struct timer_list bulk_timer;
47
48         enum {
49                 FPIX_NOP,       /* inactive, else streaming */
50                 FPIX_RESET,     /* must reset */
51                 FPIX_REQ_FRAME, /* requesting a frame */
52                 FPIX_READ_FRAME,        /* reading frame */
53         } state;
54
55         /*
56          * Driver stuff
57          */
58         struct delayed_work wqe;
59         struct completion can_close;
60         int streaming;
61 };
62
63 /* Delay after which claim the next frame. If the delay is too small,
64  * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
65  * will fail every 4 or 5 frames, but 30ms is perfect. */
66 #define NEXT_FRAME_DELAY  (((HZ * 30) + 999) / 1000)
67
68 #define dev_new_state(new_state) {                              \
69                 PDEBUG(D_STREAM, "new state from %d to %d at %s:%d",    \
70                         dev->state, new_state, __func__, __LINE__);     \
71                 dev->state = new_state;                                 \
72 }
73
74 /* These cameras only support 320x200. */
75 static struct v4l2_pix_format fpix_mode[1] = {
76         { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
77                 .bytesperline = 320,
78                 .sizeimage = 320 * 240 * 3 / 8 + 590,
79                 .colorspace = V4L2_COLORSPACE_SRGB,
80                 .priv = 0}
81 };
82
83 /* Reads part of a frame */
84 static void read_frame_part(struct usb_fpix *dev)
85 {
86         int ret;
87
88         PDEBUG(D_STREAM, "read_frame_part");
89
90         /* Reads part of a frame */
91         ret = usb_submit_urb(dev->gspca_dev.urb[0], GFP_ATOMIC);
92         if (ret) {
93                 dev_new_state(FPIX_RESET);
94                 schedule_delayed_work(&dev->wqe, 1);
95                 PDEBUG(D_STREAM, "usb_submit_urb failed with %d",
96                         ret);
97         } else {
98                 /* Sometimes we never get a callback, so use a timer.
99                  * Is this masking a bug somewhere else? */
100                 dev->bulk_timer.expires = jiffies + msecs_to_jiffies(150);
101                 add_timer(&dev->bulk_timer);
102         }
103 }
104
105 /* Callback for URBs. */
106 static void urb_callback(struct urb *urb)
107 {
108         struct gspca_dev *gspca_dev = urb->context;
109         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
110
111         PDEBUG(D_PACK,
112                 "enter urb_callback - status=%d, length=%d",
113                 urb->status, urb->actual_length);
114
115         if (dev->state == FPIX_READ_FRAME)
116                 del_timer(&dev->bulk_timer);
117
118         if (urb->status != 0) {
119                 /* We kill a stuck urb every 50 frames on average, so don't
120                  * display a log message for that. */
121                 if (urb->status != -ECONNRESET)
122                         PDEBUG(D_STREAM, "bad URB status %d", urb->status);
123                 dev_new_state(FPIX_RESET);
124                 schedule_delayed_work(&dev->wqe, 1);
125         }
126
127         switch (dev->state) {
128         case FPIX_REQ_FRAME:
129                 dev_new_state(FPIX_READ_FRAME);
130                 read_frame_part(dev);
131                 break;
132
133         case FPIX_READ_FRAME: {
134                 unsigned char *data = urb->transfer_buffer;
135                 struct gspca_frame *frame;
136
137                 frame = gspca_get_i_frame(&dev->gspca_dev);
138                 if (frame == NULL)
139                         gspca_dev->last_packet_type = DISCARD_PACKET;
140                 if (urb->actual_length < FPIX_MAX_TRANSFER ||
141                         (data[urb->actual_length-2] == 0xff &&
142                                 data[urb->actual_length-1] == 0xd9)) {
143
144                         /* If the result is less than what was asked
145                          * for, then it's the end of the
146                          * frame. Sometime the jpeg is not complete,
147                          * but there's nothing we can do. We also end
148                          * here if the the jpeg ends right at the end
149                          * of the frame. */
150                         if (frame)
151                                 gspca_frame_add(gspca_dev, LAST_PACKET,
152                                                 frame,
153                                                 data, urb->actual_length);
154                         dev_new_state(FPIX_REQ_FRAME);
155                         schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY);
156                 } else {
157
158                         /* got a partial image */
159                         if (frame)
160                                 gspca_frame_add(gspca_dev,
161                                                 gspca_dev->last_packet_type
162                                                                 == LAST_PACKET
163                                                 ? FIRST_PACKET : INTER_PACKET,
164                                                 frame,
165                                         data, urb->actual_length);
166                         read_frame_part(dev);
167                 }
168                 break;
169             }
170
171         case FPIX_NOP:
172         case FPIX_RESET:
173                 PDEBUG(D_STREAM, "invalid state %d", dev->state);
174                 break;
175         }
176 }
177
178 /* Request a new frame */
179 static void request_frame(struct usb_fpix *dev)
180 {
181         int ret;
182         struct gspca_dev *gspca_dev = &dev->gspca_dev;
183
184         /* Setup command packet */
185         memset(gspca_dev->usb_buf, 0, 12);
186         gspca_dev->usb_buf[0] = 0xd3;
187         gspca_dev->usb_buf[7] = 0x01;
188
189         /* Request a frame */
190         dev->ctrlreq.bRequestType =
191                 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
192         dev->ctrlreq.bRequest = USB_REQ_GET_STATUS;
193         dev->ctrlreq.wValue = 0;
194         dev->ctrlreq.wIndex = 0;
195         dev->ctrlreq.wLength = cpu_to_le16(12);
196
197         usb_fill_control_urb(dev->control_urb,
198                              gspca_dev->dev,
199                              usb_sndctrlpipe(gspca_dev->dev, 0),
200                              (unsigned char *) &dev->ctrlreq,
201                              gspca_dev->usb_buf,
202                              12, urb_callback, gspca_dev);
203
204         ret = usb_submit_urb(dev->control_urb, GFP_ATOMIC);
205         if (ret) {
206                 dev_new_state(FPIX_RESET);
207                 schedule_delayed_work(&dev->wqe, 1);
208                 PDEBUG(D_STREAM, "usb_submit_urb failed with %d", ret);
209         }
210 }
211
212 /*--------------------------------------------------------------------------*/
213
214 /* State machine. */
215 static void fpix_sm(struct work_struct *work)
216 {
217         struct usb_fpix *dev = container_of(work, struct usb_fpix, wqe.work);
218
219         PDEBUG(D_STREAM, "fpix_sm state %d", dev->state);
220
221         /* verify that the device wasn't unplugged */
222         if (!dev->gspca_dev.present) {
223                 PDEBUG(D_STREAM, "device is gone");
224                 dev_new_state(FPIX_NOP);
225                 complete(&dev->can_close);
226                 return;
227         }
228
229         if (!dev->streaming) {
230                 PDEBUG(D_STREAM, "stopping state machine");
231                 dev_new_state(FPIX_NOP);
232                 complete(&dev->can_close);
233                 return;
234         }
235
236         switch (dev->state) {
237         case FPIX_RESET:
238                 dev_new_state(FPIX_REQ_FRAME);
239                 schedule_delayed_work(&dev->wqe, HZ / 10);
240                 break;
241
242         case FPIX_REQ_FRAME:
243                 /* get an image */
244                 request_frame(dev);
245                 break;
246
247         case FPIX_NOP:
248         case FPIX_READ_FRAME:
249                 PDEBUG(D_STREAM, "invalid state %d", dev->state);
250                 break;
251         }
252 }
253
254 /* this function is called at probe time */
255 static int sd_config(struct gspca_dev *gspca_dev,
256                 const struct usb_device_id *id)
257 {
258         struct cam *cam = &gspca_dev->cam;
259
260         cam->cam_mode = fpix_mode;
261         cam->nmodes = 1;
262         cam->epaddr = 0x01;     /* todo: correct for all cams? */
263         cam->bulk_size = FPIX_MAX_TRANSFER;
264
265 /*      gspca_dev->nbalt = 1;    * use bulk transfer */
266         return 0;
267 }
268
269 /* Stop streaming and free the ressources allocated by sd_start. */
270 static void sd_stopN(struct gspca_dev *gspca_dev)
271 {
272         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
273
274         dev->streaming = 0;
275
276         /* Stop the state machine */
277         if (dev->state != FPIX_NOP)
278                 wait_for_completion(&dev->can_close);
279 }
280
281 /* called on streamoff with alt 0 and disconnect */
282 static void sd_stop0(struct gspca_dev *gspca_dev)
283 {
284         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
285
286         usb_free_urb(dev->control_urb);
287         dev->control_urb = NULL;
288 }
289
290 /* Kill an URB that hasn't completed. */
291 static void timeout_kill(unsigned long data)
292 {
293         struct urb *urb = (struct urb *) data;
294
295         usb_unlink_urb(urb);
296 }
297
298 /* this function is called at probe and resume time */
299 static int sd_init(struct gspca_dev *gspca_dev)
300 {
301         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
302
303         INIT_DELAYED_WORK(&dev->wqe, fpix_sm);
304
305         init_timer(&dev->bulk_timer);
306         dev->bulk_timer.function = timeout_kill;
307
308         return 0;
309 }
310
311 static int sd_start(struct gspca_dev *gspca_dev)
312 {
313         struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
314         int ret;
315         int size_ret;
316
317         /* Reset bulk in endpoint */
318         usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
319
320         /* Init the device */
321         memset(gspca_dev->usb_buf, 0, 12);
322         gspca_dev->usb_buf[0] = 0xc6;
323         gspca_dev->usb_buf[8] = 0x20;
324
325         ret = usb_control_msg(gspca_dev->dev,
326                         usb_sndctrlpipe(gspca_dev->dev, 0),
327                         USB_REQ_GET_STATUS,
328                         USB_DIR_OUT | USB_TYPE_CLASS |
329                         USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
330                         12, FPIX_TIMEOUT);
331
332         if (ret != 12) {
333                 PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
334                 ret = -EIO;
335                 goto error;
336         }
337
338         /* Read the result of the command. Ignore the result, for it
339          * varies with the device. */
340         ret = usb_bulk_msg(gspca_dev->dev,
341                         usb_rcvbulkpipe(gspca_dev->dev,
342                                         gspca_dev->cam.epaddr),
343                         gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret,
344                         FPIX_TIMEOUT);
345         if (ret != 0) {
346                 PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret);
347                 ret = -EIO;
348                 goto error;
349         }
350
351         /* Request a frame, but don't read it */
352         memset(gspca_dev->usb_buf, 0, 12);
353         gspca_dev->usb_buf[0] = 0xd3;
354         gspca_dev->usb_buf[7] = 0x01;
355
356         ret = usb_control_msg(gspca_dev->dev,
357                         usb_sndctrlpipe(gspca_dev->dev, 0),
358                         USB_REQ_GET_STATUS,
359                         USB_DIR_OUT | USB_TYPE_CLASS |
360                         USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
361                         12, FPIX_TIMEOUT);
362         if (ret != 12) {
363                 PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
364                 ret = -EIO;
365                 goto error;
366         }
367
368         /* Again, reset bulk in endpoint */
369         usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
370
371         /* Allocate a control URB */
372         dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);
373         if (!dev->control_urb) {
374                 PDEBUG(D_STREAM, "No free urbs available");
375                 ret = -EIO;
376                 goto error;
377         }
378
379         /* Various initializations. */
380         init_completion(&dev->can_close);
381         dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];
382         dev->gspca_dev.urb[0]->complete = urb_callback;
383         dev->streaming = 1;
384
385         /* Schedule a frame request. */
386         dev_new_state(FPIX_REQ_FRAME);
387         schedule_delayed_work(&dev->wqe, 1);
388
389         return 0;
390
391 error:
392         /* Free the ressources */
393         sd_stopN(gspca_dev);
394         sd_stop0(gspca_dev);
395         return ret;
396 }
397
398 /* Table of supported USB devices */
399 static const __devinitdata struct usb_device_id device_table[] = {
400         {USB_DEVICE(0x04cb, 0x0104)},
401         {USB_DEVICE(0x04cb, 0x0109)},
402         {USB_DEVICE(0x04cb, 0x010b)},
403         {USB_DEVICE(0x04cb, 0x010f)},
404         {USB_DEVICE(0x04cb, 0x0111)},
405         {USB_DEVICE(0x04cb, 0x0113)},
406         {USB_DEVICE(0x04cb, 0x0115)},
407         {USB_DEVICE(0x04cb, 0x0117)},
408         {USB_DEVICE(0x04cb, 0x0119)},
409         {USB_DEVICE(0x04cb, 0x011b)},
410         {USB_DEVICE(0x04cb, 0x011d)},
411         {USB_DEVICE(0x04cb, 0x0121)},
412         {USB_DEVICE(0x04cb, 0x0123)},
413         {USB_DEVICE(0x04cb, 0x0125)},
414         {USB_DEVICE(0x04cb, 0x0127)},
415         {USB_DEVICE(0x04cb, 0x0129)},
416         {USB_DEVICE(0x04cb, 0x012b)},
417         {USB_DEVICE(0x04cb, 0x012d)},
418         {USB_DEVICE(0x04cb, 0x012f)},
419         {USB_DEVICE(0x04cb, 0x0131)},
420         {USB_DEVICE(0x04cb, 0x013b)},
421         {USB_DEVICE(0x04cb, 0x013d)},
422         {USB_DEVICE(0x04cb, 0x013f)},
423         {}
424 };
425
426 MODULE_DEVICE_TABLE(usb, device_table);
427
428 /* sub-driver description */
429 static const struct sd_desc sd_desc = {
430         .name = MODULE_NAME,
431         .config = sd_config,
432         .init = sd_init,
433         .start = sd_start,
434         .stopN = sd_stopN,
435         .stop0 = sd_stop0,
436 };
437
438 /* -- device connect -- */
439 static int sd_probe(struct usb_interface *intf,
440                 const struct usb_device_id *id)
441 {
442         return gspca_dev_probe(intf, id,
443                         &sd_desc,
444                         sizeof(struct usb_fpix),
445                         THIS_MODULE);
446 }
447
448 static struct usb_driver sd_driver = {
449         .name = MODULE_NAME,
450         .id_table = device_table,
451         .probe = sd_probe,
452         .disconnect = gspca_disconnect,
453 #ifdef CONFIG_PM
454         .suspend = gspca_suspend,
455         .resume = gspca_resume,
456 #endif
457 };
458
459 /* -- module insert / remove -- */
460 static int __init sd_mod_init(void)
461 {
462         if (usb_register(&sd_driver) < 0)
463                 return -1;
464         PDEBUG(D_PROBE, "registered");
465         return 0;
466 }
467 static void __exit sd_mod_exit(void)
468 {
469         usb_deregister(&sd_driver);
470         PDEBUG(D_PROBE, "deregistered");
471 }
472
473 module_init(sd_mod_init);
474 module_exit(sd_mod_exit);