summaryrefslogtreecommitdiff
path: root/drivers/media/video/gspca/finepix.c
blob: 65d3cbfe6b278e7446038696a2eaa8172d1695f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * Fujifilm Finepix subdriver
 *
 * Copyright (C) 2008 Frank Zago
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define MODULE_NAME "finepix"

#include "gspca.h"

MODULE_AUTHOR("Frank Zago <frank@zago.net>");
MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
MODULE_LICENSE("GPL");

/* Default timeout, in ms */
#define FPIX_TIMEOUT (HZ / 10)

/* Maximum transfer size to use. The windows driver reads by chunks of
 * 0x2000 bytes, so do the same. Note: reading more seems to work
 * too. */
#define FPIX_MAX_TRANSFER 0x2000

/* Structure to hold all of our device specific stuff */
struct usb_fpix {
	struct gspca_dev gspca_dev;	/* !! must be the first item */

	/*
	 * USB stuff
	 */
	struct usb_ctrlrequest ctrlreq;
	struct urb *control_urb;
	struct timer_list bulk_timer;

	enum {
		FPIX_NOP,	/* inactive, else streaming */
		FPIX_RESET,	/* must reset */
		FPIX_REQ_FRAME,	/* requesting a frame */
		FPIX_READ_FRAME,	/* reading frame */
	} state;

	/*
	 * Driver stuff
	 */
	struct delayed_work wqe;
	struct completion can_close;
	int streaming;
};

/* Delay after which claim the next frame. If the delay is too small,
 * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
 * will fail every 4 or 5 frames, but 30ms is perfect. */
#define NEXT_FRAME_DELAY  (((HZ * 30) + 999) / 1000)

#define dev_new_state(new_state) {				\
		PDEBUG(D_STREAM, "new state from %d to %d at %s:%d",	\
			dev->state, new_state, __func__, __LINE__);	\
		dev->state = new_state;					\
}

/* These cameras only support 320x200. */
static struct v4l2_pix_format fpix_mode[1] = {
	{ 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 0}
};

/* Reads part of a frame */
static void read_frame_part(struct usb_fpix *dev)
{
	int ret;

	PDEBUG(D_STREAM, "read_frame_part");

	/* Reads part of a frame */
	ret = usb_submit_urb(dev->gspca_dev.urb[0], GFP_ATOMIC);
	if (ret) {
		dev_new_state(FPIX_RESET);
		schedule_delayed_work(&dev->wqe, 1);
		PDEBUG(D_STREAM, "usb_submit_urb failed with %d",
			ret);
	} else {
		/* Sometimes we never get a callback, so use a timer.
		 * Is this masking a bug somewhere else? */
		dev->bulk_timer.expires = jiffies + msecs_to_jiffies(150);
		add_timer(&dev->bulk_timer);
	}
}

/* Callback for URBs. */
static void urb_callback(struct urb *urb)
{
	struct gspca_dev *gspca_dev = urb->context;
	struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;

	PDEBUG(D_PACK,
		"enter urb_callback - status=%d, length=%d",
		urb->status, urb->actual_length);

	if (dev->state == FPIX_READ_FRAME)
		del_timer(&dev->bulk_timer);

	if (urb->status != 0) {
		/* We kill a stuck urb every 50 frames on average, so don't
		 * display a log message for that. */
		if (urb->status != -ECONNRESET)
			PDEBUG(D_STREAM, "bad URB status %d", urb->status);
		dev_new_state(FPIX_RESET);
		schedule_delayed_work(&dev->wqe, 1);
	}

	switch (dev->state) {
	case FPIX_REQ_FRAME:
		dev_new_state(FPIX_READ_FRAME);
		read_frame_part(dev);
		break;

	case FPIX_READ_FRAME: {
		unsigned char *data = urb->transfer_buffer;
		struct gspca_frame *frame;

		frame = gspca_get_i_frame(&dev->gspca_dev);
		if (frame == NULL)
			gspca_dev->last_packet_type = DISCARD_PACKET;
		if (urb->actual_length < FPIX_MAX_TRANSFER ||
			(data[urb->actual_length-2] == 0xff &&
				data[urb->actual_length-1] == 0xd9)) {

			/* If the result is less than what was asked
			 * for, then it's the end of the
			 * frame. Sometime the jpeg is not complete,
			 * but there's nothing we can do. We also end
			 * here if the the jpeg ends right at the end
			 * of the frame. */
			if (frame)
				gspca_frame_add(gspca_dev, LAST_PACKET,
						frame,
						data, urb->actual_length);
			dev_new_state(FPIX_REQ_FRAME);
			schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY);
		} else {

			/* got a partial image */
			if (frame)
				gspca_frame_add(gspca_dev,
						gspca_dev->last_packet_type
								== LAST_PACKET
						? FIRST_PACKET : INTER_PACKET,
						frame,
					data, urb->actual_length);
			read_frame_part(dev);
		}
		break;
	    }

	case FPIX_NOP:
	case FPIX_RESET:
		PDEBUG(D_STREAM, "invalid state %d", dev->state);
		break;
	}
}

/* Request a new frame */
static void request_frame(struct usb_fpix *dev)
{
	int ret;
	struct gspca_dev *gspca_dev = &dev->gspca_dev;

	/* Setup command packet */
	memset(gspca_dev->usb_buf, 0, 12);
	gspca_dev->usb_buf[0] = 0xd3;
	gspca_dev->usb_buf[7] = 0x01;

	/* Request a frame */
	dev->ctrlreq.bRequestType =
		USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
	dev->ctrlreq.bRequest = USB_REQ_GET_STATUS;
	dev->ctrlreq.wValue = 0;
	dev->ctrlreq.wIndex = 0;
	dev->ctrlreq.wLength = cpu_to_le16(12);

	usb_fill_control_urb(dev->control_urb,
			     gspca_dev->dev,
			     usb_sndctrlpipe(gspca_dev->dev, 0),
			     (unsigned char *) &dev->ctrlreq,
			     gspca_dev->usb_buf,
			     12, urb_callback, gspca_dev);

	ret = usb_submit_urb(dev->control_urb, GFP_ATOMIC);
	if (ret) {
		dev_new_state(FPIX_RESET);
		schedule_delayed_work(&dev->wqe, 1);
		PDEBUG(D_STREAM, "usb_submit_urb failed with %d", ret);
	}
}

/*--------------------------------------------------------------------------*/

/* State machine. */
static void fpix_sm(struct work_struct *work)
{
	struct usb_fpix *dev = container_of(work, struct usb_fpix, wqe.work);

	PDEBUG(D_STREAM, "fpix_sm state %d", dev->state);

	/* verify that the device wasn't unplugged */
	if (!dev->gspca_dev.present) {
		PDEBUG(D_STREAM, "device is gone");
		dev_new_state(FPIX_NOP);
		complete(&dev->can_close);
		return;
	}

	if (!dev->streaming) {
		PDEBUG(D_STREAM, "stopping state machine");
		dev_new_state(FPIX_NOP);
		complete(&dev->can_close);
		return;
	}

	switch (dev->state) {
	case FPIX_RESET:
		dev_new_state(FPIX_REQ_FRAME);
		schedule_delayed_work(&dev->wqe, HZ / 10);
		break;

	case FPIX_REQ_FRAME:
		/* get an image */
		request_frame(dev);
		break;

	case FPIX_NOP:
	case FPIX_READ_FRAME:
		PDEBUG(D_STREAM, "invalid state %d", dev->state);
		break;
	}
}

/* this function is called at probe time */
static int sd_config(struct gspca_dev *gspca_dev,
		const struct usb_device_id *id)
{
	struct cam *cam = &gspca_dev->cam;

	cam->cam_mode = fpix_mode;
	cam->nmodes = 1;
	cam->epaddr = 0x01;	/* todo: correct for all cams? */
	cam->bulk_size = FPIX_MAX_TRANSFER;

/*	gspca_dev->nbalt = 1;	 * use bulk transfer */
	return 0;
}

/* Stop streaming and free the ressources allocated by sd_start. */
static void sd_stopN(struct gspca_dev *gspca_dev)
{
	struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;

	dev->streaming = 0;

	/* Stop the state machine */
	if (dev->state != FPIX_NOP)
		wait_for_completion(&dev->can_close);

	usb_free_urb(dev->control_urb);
	dev->control_urb = NULL;
}

/* Kill an URB that hasn't completed. */
static void timeout_kill(unsigned long data)
{
	struct urb *urb = (struct urb *) data;

	usb_unlink_urb(urb);
}

/* this function is called at probe and resume time */
static int sd_init(struct gspca_dev *gspca_dev)
{
	struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;

	INIT_DELAYED_WORK(&dev->wqe, fpix_sm);

	init_timer(&dev->bulk_timer);
	dev->bulk_timer.function = timeout_kill;

	return 0;
}

static int sd_start(struct gspca_dev *gspca_dev)
{
	struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
	int ret;
	int size_ret;

	/* Reset bulk in endpoint */
	usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);

	/* Init the device */
	memset(gspca_dev->usb_buf, 0, 12);
	gspca_dev->usb_buf[0] = 0xc6;
	gspca_dev->usb_buf[8] = 0x20;

	ret = usb_control_msg(gspca_dev->dev,
			usb_sndctrlpipe(gspca_dev->dev, 0),
			USB_REQ_GET_STATUS,
			USB_DIR_OUT | USB_TYPE_CLASS |
			USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
			12, FPIX_TIMEOUT);

	if (ret != 12) {
		PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
		ret = -EIO;
		goto error;
	}

	/* Read the result of the command. Ignore the result, for it
	 * varies with the device. */
	ret = usb_bulk_msg(gspca_dev->dev,
			usb_rcvbulkpipe(gspca_dev->dev,
					gspca_dev->cam.epaddr),
			gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret,
			FPIX_TIMEOUT);
	if (ret != 0) {
		PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret);
		ret = -EIO;
		goto error;
	}

	/* Request a frame, but don't read it */
	memset(gspca_dev->usb_buf, 0, 12);
	gspca_dev->usb_buf[0] = 0xd3;
	gspca_dev->usb_buf[7] = 0x01;

	ret = usb_control_msg(gspca_dev->dev,
			usb_sndctrlpipe(gspca_dev->dev, 0),
			USB_REQ_GET_STATUS,
			USB_DIR_OUT | USB_TYPE_CLASS |
			USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
			12, FPIX_TIMEOUT);
	if (ret != 12) {
		PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
		ret = -EIO;
		goto error;
	}

	/* Again, reset bulk in endpoint */
	usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);

	/* Allocate a control URB */
	dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!dev->control_urb) {
		PDEBUG(D_STREAM, "No free urbs available");
		ret = -EIO;
		goto error;
	}

	/* Various initializations. */
	init_completion(&dev->can_close);
	dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];
	dev->gspca_dev.urb[0]->complete = urb_callback;
	dev->streaming = 1;

	/* Schedule a frame request. */
	dev_new_state(FPIX_REQ_FRAME);
	schedule_delayed_work(&dev->wqe, 1);

	return 0;

error:
	/* Free the ressources */
	sd_stopN(gspca_dev);
	return ret;
}

/* Table of supported USB devices */
static const __devinitdata struct usb_device_id device_table[] = {
	{USB_DEVICE(0x04cb, 0x0104)},
	{USB_DEVICE(0x04cb, 0x0109)},
	{USB_DEVICE(0x04cb, 0x010b)},
	{USB_DEVICE(0x04cb, 0x010f)},
	{USB_DEVICE(0x04cb, 0x0111)},
	{USB_DEVICE(0x04cb, 0x0113)},
	{USB_DEVICE(0x04cb, 0x0115)},
	{USB_DEVICE(0x04cb, 0x0117)},
	{USB_DEVICE(0x04cb, 0x0119)},
	{USB_DEVICE(0x04cb, 0x011b)},
	{USB_DEVICE(0x04cb, 0x011d)},
	{USB_DEVICE(0x04cb, 0x0121)},
	{USB_DEVICE(0x04cb, 0x0123)},
	{USB_DEVICE(0x04cb, 0x0125)},
	{USB_DEVICE(0x04cb, 0x0127)},
	{USB_DEVICE(0x04cb, 0x0129)},
	{USB_DEVICE(0x04cb, 0x012b)},
	{USB_DEVICE(0x04cb, 0x012d)},
	{USB_DEVICE(0x04cb, 0x012f)},
	{USB_DEVICE(0x04cb, 0x0131)},
	{USB_DEVICE(0x04cb, 0x013b)},
	{USB_DEVICE(0x04cb, 0x013d)},
	{USB_DEVICE(0x04cb, 0x013f)},
	{}
};

MODULE_DEVICE_TABLE(usb, device_table);

/* sub-driver description */
static const struct sd_desc sd_desc = {
	.name = MODULE_NAME,
	.config = sd_config,
	.init = sd_init,
	.start = sd_start,
	.stopN = sd_stopN,
};

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
	return gspca_dev_probe(intf, id,
			&sd_desc,
			sizeof(struct usb_fpix),
			THIS_MODULE);
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
#ifdef CONFIG_PM
	.suspend = gspca_suspend,
	.resume = gspca_resume,
#endif
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
	if (usb_register(&sd_driver) < 0)
		return -1;
	PDEBUG(D_PROBE, "registered");
	return 0;
}
static void __exit sd_mod_exit(void)
{
	usb_deregister(&sd_driver);
	PDEBUG(D_PROBE, "deregistered");
}

module_init(sd_mod_init);
module_exit(sd_mod_exit);