aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/gspca/finepix.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/gspca/finepix.c')
-rw-r--r--drivers/media/video/gspca/finepix.c433
1 files changed, 140 insertions, 293 deletions
diff --git a/drivers/media/video/gspca/finepix.c b/drivers/media/video/gspca/finepix.c
index afc8b2dd307b..00e6863ed666 100644
--- a/drivers/media/video/gspca/finepix.c
+++ b/drivers/media/video/gspca/finepix.c
@@ -27,7 +27,7 @@ MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
27MODULE_LICENSE("GPL"); 27MODULE_LICENSE("GPL");
28 28
29/* Default timeout, in ms */ 29/* Default timeout, in ms */
30#define FPIX_TIMEOUT (HZ / 10) 30#define FPIX_TIMEOUT 250
31 31
32/* Maximum transfer size to use. The windows driver reads by chunks of 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 33 * 0x2000 bytes, so do the same. Note: reading more seems to work
@@ -38,38 +38,15 @@ MODULE_LICENSE("GPL");
38struct usb_fpix { 38struct usb_fpix {
39 struct gspca_dev gspca_dev; /* !! must be the first item */ 39 struct gspca_dev gspca_dev; /* !! must be the first item */
40 40
41 /* 41 struct work_struct work_struct;
42 * USB stuff 42 struct workqueue_struct *work_thread;
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}; 43};
62 44
63/* Delay after which claim the next frame. If the delay is too small, 45/* 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 46 * 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. */ 47 * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
66#define NEXT_FRAME_DELAY (((HZ * 30) + 999) / 1000) 48 * 30ms is bad while 35ms is perfect. */
67 49#define NEXT_FRAME_DELAY 35
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 50
74/* These cameras only support 320x200. */ 51/* These cameras only support 320x200. */
75static const struct v4l2_pix_format fpix_mode[1] = { 52static const struct v4l2_pix_format fpix_mode[1] = {
@@ -80,316 +57,183 @@ static const struct v4l2_pix_format fpix_mode[1] = {
80 .priv = 0} 57 .priv = 0}
81}; 58};
82 59
83/* Reads part of a frame */ 60/* send a command to the webcam */
84static void read_frame_part(struct usb_fpix *dev) 61static int command(struct gspca_dev *gspca_dev,
62 int order) /* 0: reset, 1: frame request */
85{ 63{
86 int ret; 64 static u8 order_values[2][12] = {
65 {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
66 {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
67 };
87 68
88 PDEBUG(D_STREAM, "read_frame_part"); 69 memcpy(gspca_dev->usb_buf, order_values[order], 12);
89 70 return usb_control_msg(gspca_dev->dev,
90 /* Reads part of a frame */ 71 usb_sndctrlpipe(gspca_dev->dev, 0),
91 ret = usb_submit_urb(dev->gspca_dev.urb[0], GFP_ATOMIC); 72 USB_REQ_GET_STATUS,
92 if (ret) { 73 USB_DIR_OUT | USB_TYPE_CLASS |
93 dev_new_state(FPIX_RESET); 74 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
94 schedule_delayed_work(&dev->wqe, 1); 75 12, FPIX_TIMEOUT);
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} 76}
104 77
105/* Callback for URBs. */ 78/* workqueue */
106static void urb_callback(struct urb *urb) 79static void dostream(struct work_struct *work)
107{ 80{
108 struct gspca_dev *gspca_dev = urb->context; 81 struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
109 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; 82 struct gspca_dev *gspca_dev = &dev->gspca_dev;
110 83 struct urb *urb = gspca_dev->urb[0];
111 PDEBUG(D_PACK, 84 u8 *data = urb->transfer_buffer;
112 "enter urb_callback - status=%d, length=%d", 85 struct gspca_frame *frame;
113 urb->status, urb->actual_length); 86 int ret = 0;
114 87 int len;
115 if (dev->state == FPIX_READ_FRAME) 88
116 del_timer(&dev->bulk_timer); 89 /* synchronize with the main driver */
117 90 mutex_lock(&gspca_dev->usb_lock);
118 if (urb->status != 0) { 91 mutex_unlock(&gspca_dev->usb_lock);
119 /* We kill a stuck urb every 50 frames on average, so don't 92 PDEBUG(D_STREAM, "dostream started");
120 * display a log message for that. */ 93
121 if (urb->status != -ECONNRESET) 94 /* loop reading a frame */
122 PDEBUG(D_STREAM, "bad URB status %d", urb->status); 95again:
123 dev_new_state(FPIX_RESET); 96 while (gspca_dev->present && gspca_dev->streaming) {
124 schedule_delayed_work(&dev->wqe, 1); 97
125 } 98 /* request a frame */
126 99 mutex_lock(&gspca_dev->usb_lock);
127 switch (dev->state) { 100 ret = command(gspca_dev, 1);
128 case FPIX_REQ_FRAME: 101 mutex_unlock(&gspca_dev->usb_lock);
129 dev_new_state(FPIX_READ_FRAME); 102 if (ret < 0)
130 read_frame_part(dev); 103 break;
131 break; 104 if (!gspca_dev->present || !gspca_dev->streaming)
132 105 break;
133 case FPIX_READ_FRAME: { 106
134 unsigned char *data = urb->transfer_buffer; 107 /* the frame comes in parts */
135 struct gspca_frame *frame; 108 for (;;) {
136 109 ret = usb_bulk_msg(gspca_dev->dev,
137 frame = gspca_get_i_frame(&dev->gspca_dev); 110 urb->pipe,
138 if (frame == NULL) 111 data,
139 gspca_dev->last_packet_type = DISCARD_PACKET; 112 FPIX_MAX_TRANSFER,
140 if (urb->actual_length < FPIX_MAX_TRANSFER || 113 &len, FPIX_TIMEOUT);
141 (data[urb->actual_length-2] == 0xff && 114 if (ret < 0) {
142 data[urb->actual_length-1] == 0xd9)) { 115 /* Most of the time we get a timeout
143 116 * error. Just restart. */
144 /* If the result is less than what was asked 117 goto again;
145 * for, then it's the end of the 118 }
146 * frame. Sometime the jpeg is not complete, 119 if (!gspca_dev->present || !gspca_dev->streaming)
147 * but there's nothing we can do. We also end 120 goto out;
148 * here if the the jpeg ends right at the end 121 frame = gspca_get_i_frame(&dev->gspca_dev);
149 * of the frame. */ 122 if (frame == NULL)
150 if (frame) 123 gspca_dev->last_packet_type = DISCARD_PACKET;
151 gspca_frame_add(gspca_dev, LAST_PACKET, 124
152 frame, 125 if (len < FPIX_MAX_TRANSFER ||
153 data, urb->actual_length); 126 (data[len - 2] == 0xff &&
154 dev_new_state(FPIX_REQ_FRAME); 127 data[len - 1] == 0xd9)) {
155 schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY); 128
156 } else { 129 /* If the result is less than what was asked
130 * for, then it's the end of the
131 * frame. Sometimes the jpeg is not complete,
132 * but there's nothing we can do. We also end
133 * here if the the jpeg ends right at the end
134 * of the frame. */
135 if (frame)
136 frame = gspca_frame_add(gspca_dev,
137 LAST_PACKET,
138 frame,
139 data, len);
140 break;
141 }
157 142
158 /* got a partial image */ 143 /* got a partial image */
159 if (frame) 144 if (frame)
160 gspca_frame_add(gspca_dev, 145 gspca_frame_add(gspca_dev,
161 gspca_dev->last_packet_type 146 gspca_dev->last_packet_type
162 == LAST_PACKET 147 == LAST_PACKET
163 ? FIRST_PACKET : INTER_PACKET, 148 ? FIRST_PACKET : INTER_PACKET,
164 frame, 149 frame, data, len);
165 data, urb->actual_length);
166 read_frame_part(dev);
167 } 150 }
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 151
178/* Request a new frame */ 152 /* We must wait before trying reading the next
179static void request_frame(struct usb_fpix *dev) 153 * frame. If we don't, or if the delay is too short,
180{ 154 * the camera will disconnect. */
181 int ret; 155 msleep(NEXT_FRAME_DELAY);
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. */
215static 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 } 156 }
235 157
236 switch (dev->state) { 158out:
237 case FPIX_RESET: 159 PDEBUG(D_STREAM, "dostream stopped");
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} 160}
253 161
254/* this function is called at probe time */ 162/* this function is called at probe time */
255static int sd_config(struct gspca_dev *gspca_dev, 163static int sd_config(struct gspca_dev *gspca_dev,
256 const struct usb_device_id *id) 164 const struct usb_device_id *id)
257{ 165{
166 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
258 struct cam *cam = &gspca_dev->cam; 167 struct cam *cam = &gspca_dev->cam;
259 168
260 cam->cam_mode = fpix_mode; 169 cam->cam_mode = fpix_mode;
261 cam->nmodes = 1; 170 cam->nmodes = 1;
262 cam->epaddr = 0x01; /* todo: correct for all cams? */
263 cam->bulk_size = FPIX_MAX_TRANSFER; 171 cam->bulk_size = FPIX_MAX_TRANSFER;
264 172
265/* gspca_dev->nbalt = 1; * use bulk transfer */ 173 INIT_WORK(&dev->work_struct, dostream);
266 return 0;
267}
268
269/* Stop streaming and free the ressources allocated by sd_start. */
270static 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 */
282static 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. */
291static void timeout_kill(unsigned long data)
292{
293 struct urb *urb = (struct urb *) data;
294 174
295 usb_unlink_urb(urb); 175 return 0;
296} 176}
297 177
298/* this function is called at probe and resume time */ 178/* this function is called at probe and resume time */
299static int sd_init(struct gspca_dev *gspca_dev) 179static int sd_init(struct gspca_dev *gspca_dev)
300{ 180{
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; 181 return 0;
309} 182}
310 183
184/* start the camera */
311static int sd_start(struct gspca_dev *gspca_dev) 185static int sd_start(struct gspca_dev *gspca_dev)
312{ 186{
313 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; 187 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
314 int ret; 188 int ret, len;
315 int size_ret;
316 189
317 /* Init the device */ 190 /* Init the device */
318 memset(gspca_dev->usb_buf, 0, 12); 191 ret = command(gspca_dev, 0);
319 gspca_dev->usb_buf[0] = 0xc6; 192 if (ret < 0) {
320 gspca_dev->usb_buf[8] = 0x20; 193 PDEBUG(D_STREAM, "init failed %d", ret);
321 194 return ret;
322 ret = usb_control_msg(gspca_dev->dev,
323 usb_sndctrlpipe(gspca_dev->dev, 0),
324 USB_REQ_GET_STATUS,
325 USB_DIR_OUT | USB_TYPE_CLASS |
326 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
327 12, FPIX_TIMEOUT);
328
329 if (ret != 12) {
330 PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
331 ret = -EIO;
332 goto error;
333 } 195 }
334 196
335 /* Read the result of the command. Ignore the result, for it 197 /* Read the result of the command. Ignore the result, for it
336 * varies with the device. */ 198 * varies with the device. */
337 ret = usb_bulk_msg(gspca_dev->dev, 199 ret = usb_bulk_msg(gspca_dev->dev,
338 usb_rcvbulkpipe(gspca_dev->dev, 200 gspca_dev->urb[0]->pipe,
339 gspca_dev->cam.epaddr), 201 gspca_dev->urb[0]->transfer_buffer,
340 gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret, 202 FPIX_MAX_TRANSFER, &len,
341 FPIX_TIMEOUT); 203 FPIX_TIMEOUT);
342 if (ret != 0) { 204 if (ret < 0) {
343 PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret); 205 PDEBUG(D_STREAM, "usb_bulk_msg failed %d", ret);
344 ret = -EIO; 206 return ret;
345 goto error;
346 } 207 }
347 208
348 /* Request a frame, but don't read it */ 209 /* Request a frame, but don't read it */
349 memset(gspca_dev->usb_buf, 0, 12); 210 ret = command(gspca_dev, 1);
350 gspca_dev->usb_buf[0] = 0xd3; 211 if (ret < 0) {
351 gspca_dev->usb_buf[7] = 0x01; 212 PDEBUG(D_STREAM, "frame request failed %d", ret);
352 213 return ret;
353 ret = usb_control_msg(gspca_dev->dev,
354 usb_sndctrlpipe(gspca_dev->dev, 0),
355 USB_REQ_GET_STATUS,
356 USB_DIR_OUT | USB_TYPE_CLASS |
357 USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
358 12, FPIX_TIMEOUT);
359 if (ret != 12) {
360 PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
361 ret = -EIO;
362 goto error;
363 } 214 }
364 215
365 /* Again, reset bulk in endpoint */ 216 /* Again, reset bulk in endpoint */
366 usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr); 217 usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
367
368 /* Allocate a control URB */
369 dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);
370 if (!dev->control_urb) {
371 PDEBUG(D_STREAM, "No free urbs available");
372 ret = -EIO;
373 goto error;
374 }
375
376 /* Various initializations. */
377 init_completion(&dev->can_close);
378 dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];
379 dev->gspca_dev.urb[0]->complete = urb_callback;
380 dev->streaming = 1;
381 218
382 /* Schedule a frame request. */ 219 /* Start the workqueue function to do the streaming */
383 dev_new_state(FPIX_REQ_FRAME); 220 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
384 schedule_delayed_work(&dev->wqe, 1); 221 queue_work(dev->work_thread, &dev->work_struct);
385 222
386 return 0; 223 return 0;
224}
225
226/* called on streamoff with alt==0 and on disconnect */
227/* the usb_lock is held at entry - restore on exit */
228static void sd_stop0(struct gspca_dev *gspca_dev)
229{
230 struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
387 231
388error: 232 /* wait for the work queue to terminate */
389 /* Free the ressources */ 233 mutex_unlock(&gspca_dev->usb_lock);
390 sd_stopN(gspca_dev); 234 destroy_workqueue(dev->work_thread);
391 sd_stop0(gspca_dev); 235 mutex_lock(&gspca_dev->usb_lock);
392 return ret; 236 dev->work_thread = NULL;
393} 237}
394 238
395/* Table of supported USB devices */ 239/* Table of supported USB devices */
@@ -424,12 +268,11 @@ MODULE_DEVICE_TABLE(usb, device_table);
424 268
425/* sub-driver description */ 269/* sub-driver description */
426static const struct sd_desc sd_desc = { 270static const struct sd_desc sd_desc = {
427 .name = MODULE_NAME, 271 .name = MODULE_NAME,
428 .config = sd_config, 272 .config = sd_config,
429 .init = sd_init, 273 .init = sd_init,
430 .start = sd_start, 274 .start = sd_start,
431 .stopN = sd_stopN, 275 .stop0 = sd_stop0,
432 .stop0 = sd_stop0,
433}; 276};
434 277
435/* -- device connect -- */ 278/* -- device connect -- */
@@ -443,24 +286,28 @@ static int sd_probe(struct usb_interface *intf,
443} 286}
444 287
445static struct usb_driver sd_driver = { 288static struct usb_driver sd_driver = {
446 .name = MODULE_NAME, 289 .name = MODULE_NAME,
447 .id_table = device_table, 290 .id_table = device_table,
448 .probe = sd_probe, 291 .probe = sd_probe,
449 .disconnect = gspca_disconnect, 292 .disconnect = gspca_disconnect,
450#ifdef CONFIG_PM 293#ifdef CONFIG_PM
451 .suspend = gspca_suspend, 294 .suspend = gspca_suspend,
452 .resume = gspca_resume, 295 .resume = gspca_resume,
453#endif 296#endif
454}; 297};
455 298
456/* -- module insert / remove -- */ 299/* -- module insert / remove -- */
457static int __init sd_mod_init(void) 300static int __init sd_mod_init(void)
458{ 301{
459 if (usb_register(&sd_driver) < 0) 302 int ret;
460 return -1; 303
304 ret = usb_register(&sd_driver);
305 if (ret < 0)
306 return ret;
461 PDEBUG(D_PROBE, "registered"); 307 PDEBUG(D_PROBE, "registered");
462 return 0; 308 return 0;
463} 309}
310
464static void __exit sd_mod_exit(void) 311static void __exit sd_mod_exit(void)
465{ 312{
466 usb_deregister(&sd_driver); 313 usb_deregister(&sd_driver);