2 * Fujifilm Finepix subdriver
4 * Copyright (C) 2008 Frank Zago
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
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #define MODULE_NAME "finepix"
23 MODULE_AUTHOR("Frank Zago <frank@zago.net>");
24 MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
25 MODULE_LICENSE("GPL");
27 /* Default timeout, in ms */
28 #define FPIX_TIMEOUT 250
30 /* Maximum transfer size to use. The windows driver reads by chunks of
31 * 0x2000 bytes, so do the same. Note: reading more seems to work
33 #define FPIX_MAX_TRANSFER 0x2000
35 /* Structure to hold all of our device specific stuff */
37 struct gspca_dev gspca_dev
; /* !! must be the first item */
39 struct work_struct work_struct
;
42 /* Delay after which claim the next frame. If the delay is too small,
43 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
44 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
45 * 30ms is bad while 35ms is perfect. */
46 #define NEXT_FRAME_DELAY 35
48 /* These cameras only support 320x200. */
49 static const struct v4l2_pix_format fpix_mode
[1] = {
50 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
52 .sizeimage
= 320 * 240 * 3 / 8 + 590,
53 .colorspace
= V4L2_COLORSPACE_SRGB
,
57 /* send a command to the webcam */
58 static int command(struct gspca_dev
*gspca_dev
,
59 int order
) /* 0: reset, 1: frame request */
61 static u8 order_values
[2][12] = {
62 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
63 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
66 memcpy(gspca_dev
->usb_buf
, order_values
[order
], 12);
67 return usb_control_msg(gspca_dev
->dev
,
68 usb_sndctrlpipe(gspca_dev
->dev
, 0),
70 USB_DIR_OUT
| USB_TYPE_CLASS
|
71 USB_RECIP_INTERFACE
, 0, 0, gspca_dev
->usb_buf
,
76 * This function is called as a workqueue function and runs whenever the camera
77 * is streaming data. Because it is a workqueue function it is allowed to sleep
78 * so we can use synchronous USB calls. To avoid possible collisions with other
79 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
80 * performing USB operations using it. In practice we don't really need this
81 * as the camera doesn't provide any controls.
83 static void dostream(struct work_struct
*work
)
85 struct usb_fpix
*dev
= container_of(work
, struct usb_fpix
, work_struct
);
86 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
87 struct urb
*urb
= gspca_dev
->urb
[0];
88 u8
*data
= urb
->transfer_buffer
;
92 gspca_dbg(gspca_dev
, D_STREAM
, "dostream started\n");
94 /* loop reading a frame */
96 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
98 if (gspca_dev
->frozen
)
102 /* request a frame */
103 mutex_lock(&gspca_dev
->usb_lock
);
104 ret
= command(gspca_dev
, 1);
105 mutex_unlock(&gspca_dev
->usb_lock
);
109 if (gspca_dev
->frozen
)
112 if (!gspca_dev
->present
|| !gspca_dev
->streaming
)
115 /* the frame comes in parts */
117 ret
= usb_bulk_msg(gspca_dev
->dev
,
123 /* Most of the time we get a timeout
124 * error. Just restart. */
128 if (gspca_dev
->frozen
)
131 if (!gspca_dev
->present
|| !gspca_dev
->streaming
)
133 if (len
< FPIX_MAX_TRANSFER
||
134 (data
[len
- 2] == 0xff &&
135 data
[len
- 1] == 0xd9)) {
137 /* If the result is less than what was asked
138 * for, then it's the end of the
139 * frame. Sometimes the jpeg is not complete,
140 * but there's nothing we can do. We also end
141 * here if the the jpeg ends right at the end
143 gspca_frame_add(gspca_dev
, LAST_PACKET
,
148 /* got a partial image */
149 gspca_frame_add(gspca_dev
,
150 gspca_dev
->last_packet_type
152 ? FIRST_PACKET
: INTER_PACKET
,
156 /* We must wait before trying reading the next
157 * frame. If we don't, or if the delay is too short,
158 * the camera will disconnect. */
159 msleep(NEXT_FRAME_DELAY
);
163 gspca_dbg(gspca_dev
, D_STREAM
, "dostream stopped\n");
166 /* this function is called at probe time */
167 static int sd_config(struct gspca_dev
*gspca_dev
,
168 const struct usb_device_id
*id
)
170 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
171 struct cam
*cam
= &gspca_dev
->cam
;
173 cam
->cam_mode
= fpix_mode
;
176 cam
->bulk_size
= FPIX_MAX_TRANSFER
;
178 INIT_WORK(&dev
->work_struct
, dostream
);
183 /* this function is called at probe and resume time */
184 static int sd_init(struct gspca_dev
*gspca_dev
)
189 /* start the camera */
190 static int sd_start(struct gspca_dev
*gspca_dev
)
192 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
195 /* Init the device */
196 ret
= command(gspca_dev
, 0);
198 pr_err("init failed %d\n", ret
);
202 /* Read the result of the command. Ignore the result, for it
203 * varies with the device. */
204 ret
= usb_bulk_msg(gspca_dev
->dev
,
205 gspca_dev
->urb
[0]->pipe
,
206 gspca_dev
->urb
[0]->transfer_buffer
,
207 FPIX_MAX_TRANSFER
, &len
,
210 pr_err("usb_bulk_msg failed %d\n", ret
);
214 /* Request a frame, but don't read it */
215 ret
= command(gspca_dev
, 1);
217 pr_err("frame request failed %d\n", ret
);
221 /* Again, reset bulk in endpoint */
222 usb_clear_halt(gspca_dev
->dev
, gspca_dev
->urb
[0]->pipe
);
224 schedule_work(&dev
->work_struct
);
229 /* called on streamoff with alt==0 and on disconnect */
230 /* the usb_lock is held at entry - restore on exit */
231 static void sd_stop0(struct gspca_dev
*gspca_dev
)
233 struct usb_fpix
*dev
= (struct usb_fpix
*) gspca_dev
;
235 /* wait for the work queue to terminate */
236 mutex_unlock(&gspca_dev
->usb_lock
);
237 flush_work(&dev
->work_struct
);
238 mutex_lock(&gspca_dev
->usb_lock
);
241 /* Table of supported USB devices */
242 static const struct usb_device_id device_table
[] = {
243 {USB_DEVICE(0x04cb, 0x0104)},
244 {USB_DEVICE(0x04cb, 0x0109)},
245 {USB_DEVICE(0x04cb, 0x010b)},
246 {USB_DEVICE(0x04cb, 0x010f)},
247 {USB_DEVICE(0x04cb, 0x0111)},
248 {USB_DEVICE(0x04cb, 0x0113)},
249 {USB_DEVICE(0x04cb, 0x0115)},
250 {USB_DEVICE(0x04cb, 0x0117)},
251 {USB_DEVICE(0x04cb, 0x0119)},
252 {USB_DEVICE(0x04cb, 0x011b)},
253 {USB_DEVICE(0x04cb, 0x011d)},
254 {USB_DEVICE(0x04cb, 0x0121)},
255 {USB_DEVICE(0x04cb, 0x0123)},
256 {USB_DEVICE(0x04cb, 0x0125)},
257 {USB_DEVICE(0x04cb, 0x0127)},
258 {USB_DEVICE(0x04cb, 0x0129)},
259 {USB_DEVICE(0x04cb, 0x012b)},
260 {USB_DEVICE(0x04cb, 0x012d)},
261 {USB_DEVICE(0x04cb, 0x012f)},
262 {USB_DEVICE(0x04cb, 0x0131)},
263 {USB_DEVICE(0x04cb, 0x013b)},
264 {USB_DEVICE(0x04cb, 0x013d)},
265 {USB_DEVICE(0x04cb, 0x013f)},
269 MODULE_DEVICE_TABLE(usb
, device_table
);
271 /* sub-driver description */
272 static const struct sd_desc sd_desc
= {
280 /* -- device connect -- */
281 static int sd_probe(struct usb_interface
*intf
,
282 const struct usb_device_id
*id
)
284 return gspca_dev_probe(intf
, id
,
286 sizeof(struct usb_fpix
),
290 static struct usb_driver sd_driver
= {
292 .id_table
= device_table
,
294 .disconnect
= gspca_disconnect
,
296 .suspend
= gspca_suspend
,
297 .resume
= gspca_resume
,
298 .reset_resume
= gspca_resume
,
302 module_usb_driver(sd_driver
);