Blame view

drivers/media/video/cpia_usb.c 15.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * cpia_usb CPiA USB driver
   *
   * Supports CPiA based parallel port Video Camera's.
   *
   * Copyright (C) 1999        Jochen Scharrlach <Jochen.Scharrlach@schwaben.de>
   * Copyright (C) 1999, 2000  Johannes Erdfelt <johannes@erdfelt.com>
   *
   * 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
   * (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
  
  /* define _CPIA_DEBUG_ for verbose debug output (see cpia.h) */
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
25
  /* #define _CPIA_DEBUG_  1 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/wait.h>
  #include <linux/list.h>
  #include <linux/slab.h>
  #include <linux/vmalloc.h>
  #include <linux/usb.h>
  
  #include "cpia.h"
  
  #define USB_REQ_CPIA_GRAB_FRAME			0xC1
  #define USB_REQ_CPIA_UPLOAD_FRAME		0xC2
  #define  WAIT_FOR_NEXT_FRAME			0
  #define  FORCE_FRAME_UPLOAD			1
  
  #define FRAMES_PER_DESC		10
  #define FRAME_SIZE_PER_DESC	960	/* Shouldn't be hardcoded */
  #define CPIA_NUMSBUF		2
  #define STREAM_BUF_SIZE		(PAGE_SIZE * 4)
  #define SCRATCH_BUF_SIZE	(STREAM_BUF_SIZE * 2)
  
  struct cpia_sbuf {
  	char *data;
  	struct urb *urb;
  };
  
  #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
  enum framebuf_status {
  	FRAME_EMPTY,
  	FRAME_READING,
  	FRAME_READY,
  	FRAME_ERROR,
  };
  
  struct framebuf {
  	int length;
  	enum framebuf_status status;
  	u8 data[FRAMEBUF_LEN];
  	struct framebuf *next;
  };
  
  struct usb_cpia {
  	/* Device structure */
  	struct usb_device *dev;
  
  	unsigned char iface;
  	wait_queue_head_t wq_stream;
  
  	int cursbuf;		/* Current receiving sbuf */
  	struct cpia_sbuf sbuf[CPIA_NUMSBUF];		/* Double buffering */
  
  	int streaming;
  	int open;
  	int present;
  	struct framebuf *buffers[3];
  	struct framebuf *curbuff, *workbuff;
  };
  
  static int cpia_usb_open(void *privdata);
  static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
88
  				     void *cbdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);
  static int cpia_usb_streamStart(void *privdata);
  static int cpia_usb_streamStop(void *privdata);
  static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);
  static int cpia_usb_close(void *privdata);
  
  #define ABOUT "USB driver for Vision CPiA based cameras"
  
  static struct cpia_camera_ops cpia_usb_ops = {
  	cpia_usb_open,
  	cpia_usb_registerCallback,
  	cpia_usb_transferCmd,
  	cpia_usb_streamStart,
  	cpia_usb_streamStop,
  	cpia_usb_streamRead,
  	cpia_usb_close,
  	0,
  	THIS_MODULE
  };
  
  static LIST_HEAD(cam_list);
  static spinlock_t cam_list_lock_usb;
7d12e780e   David Howells   IRQ: Maintain reg...
111
  static void cpia_usb_complete(struct urb *urb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  {
  	int i;
  	char *cdata;
  	struct usb_cpia *ucpia;
  
  	if (!urb || !urb->context)
  		return;
  
  	ucpia = (struct usb_cpia *) urb->context;
  
  	if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
  		return;
  
  	if (ucpia->workbuff->status == FRAME_EMPTY) {
  		ucpia->workbuff->status = FRAME_READING;
  		ucpia->workbuff->length = 0;
  	}
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
129

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  	for (i = 0; i < urb->number_of_packets; i++) {
  		int n = urb->iso_frame_desc[i].actual_length;
  		int st = urb->iso_frame_desc[i].status;
  
  		cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  
  		if (st)
  			printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X
  ", i, n, st);
  
  		if (FRAMEBUF_LEN < ucpia->workbuff->length + n) {
  			printk(KERN_DEBUG "cpia: scratch buf overflow!scr_len: %d, n: %d
  ", ucpia->workbuff->length, n);
  			return;
  		}
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
145

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  		if (n) {
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
147
  			if ((ucpia->workbuff->length > 0) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  			    (0x19 == cdata[0] && 0x68 == cdata[1])) {
  				memcpy(ucpia->workbuff->data + ucpia->workbuff->length, cdata, n);
  				ucpia->workbuff->length += n;
  			} else
  				DBG("Ignoring packet!
  ");
  		} else {
  			if (ucpia->workbuff->length > 4 &&
  			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-1] &&
  			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-2] &&
  			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-3] &&
  			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-4]) {
  				ucpia->workbuff->status = FRAME_READY;
  				ucpia->curbuff = ucpia->workbuff;
  				ucpia->workbuff = ucpia->workbuff->next;
  				ucpia->workbuff->status = FRAME_EMPTY;
  				ucpia->workbuff->length = 0;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
165

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
168
169
170
171
172
173
174
  				if (waitqueue_active(&ucpia->wq_stream))
  					wake_up_interruptible(&ucpia->wq_stream);
  			}
  		}
  	}
  
  	/* resubmit */
  	urb->dev = ucpia->dev;
  	if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
7e28adb24   Harvey Harrison   V4L/DVB (7518): m...
175
176
  		printk(KERN_ERR "%s: usb_submit_urb ret %d
  ", __func__,  i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
182
183
  }
  
  static int cpia_usb_open(void *privdata)
  {
  	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
  	struct urb *urb;
  	int ret, retval = 0, fx, err;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
184

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
187
188
189
190
191
192
193
194
195
196
  	if (!ucpia)
  		return -EINVAL;
  
  	ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
  	if (!ucpia->sbuf[0].data)
  		return -EINVAL;
  
  	ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
  	if (!ucpia->sbuf[1].data) {
  		retval = -EINVAL;
  		goto error_0;
  	}
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
197

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
  	if (ret < 0) {
  		printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)
  ", ret);
  		retval = -EBUSY;
  		goto error_1;
  	}
  
  	ucpia->buffers[0]->status = FRAME_EMPTY;
  	ucpia->buffers[0]->length = 0;
  	ucpia->buffers[1]->status = FRAME_EMPTY;
  	ucpia->buffers[1]->length = 0;
  	ucpia->buffers[2]->status = FRAME_EMPTY;
  	ucpia->buffers[2]->length = 0;
  	ucpia->curbuff = ucpia->buffers[0];
  	ucpia->workbuff = ucpia->buffers[1];
  
  	/* We double buffer the Iso lists, and also know the polling
  	 * interval is every frame (1 == (1 << (bInterval -1))).
  	 */
  	urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  	if (!urb) {
  		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0
  ");
  		retval = -ENOMEM;
  		goto error_1;
  	}
  
  	ucpia->sbuf[0].urb = urb;
  	urb->dev = ucpia->dev;
  	urb->context = ucpia;
  	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
  	urb->transfer_flags = URB_ISO_ASAP;
  	urb->transfer_buffer = ucpia->sbuf[0].data;
  	urb->complete = cpia_usb_complete;
  	urb->number_of_packets = FRAMES_PER_DESC;
  	urb->interval = 1;
  	urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
  	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
  		urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
  		urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
  	}
  
  	urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  	if (!urb) {
  		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1
  ");
  		retval = -ENOMEM;
  		goto error_urb0;
  	}
  
  	ucpia->sbuf[1].urb = urb;
  	urb->dev = ucpia->dev;
  	urb->context = ucpia;
  	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
  	urb->transfer_flags = URB_ISO_ASAP;
  	urb->transfer_buffer = ucpia->sbuf[1].data;
  	urb->complete = cpia_usb_complete;
  	urb->number_of_packets = FRAMES_PER_DESC;
  	urb->interval = 1;
  	urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
  	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
  		urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
  		urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
  	}
  
  	/* queue the ISO urbs, and resubmit in the completion handler */
  	err = usb_submit_urb(ucpia->sbuf[0].urb, GFP_KERNEL);
  	if (err) {
  		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d
  ",
  			err);
  		goto error_urb1;
  	}
  	err = usb_submit_urb(ucpia->sbuf[1].urb, GFP_KERNEL);
  	if (err) {
  		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d
  ",
  			err);
  		goto error_urb1;
  	}
  
  	ucpia->streaming = 1;
  	ucpia->open = 1;
  
  	return 0;
  
  error_urb1:		/* free urb 1 */
  	usb_free_urb(ucpia->sbuf[1].urb);
  	ucpia->sbuf[1].urb = NULL;
  error_urb0:		/* free urb 0 */
  	usb_free_urb(ucpia->sbuf[0].urb);
  	ucpia->sbuf[0].urb = NULL;
  error_1:
  	kfree (ucpia->sbuf[1].data);
  	ucpia->sbuf[1].data = NULL;
  error_0:
  	kfree (ucpia->sbuf[0].data);
  	ucpia->sbuf[0].data = NULL;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
297

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  	return retval;
  }
  
  //
  // convenience functions
  //
  
  /****************************************************************************
   *
   *  WritePacket
   *
   ***************************************************************************/
  static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
  {
  	if (!packet)
  		return -EINVAL;
  
  	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  			 packet[1] + (packet[0] << 8),
  			 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
318
  			 packet[2] + (packet[3] << 8),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  			 packet[4] + (packet[5] << 8), buf, size, 1000);
  }
  
  /****************************************************************************
   *
   *  ReadPacket
   *
   ***************************************************************************/
  static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
  {
  	if (!packet || size <= 0)
  		return -EINVAL;
  
  	return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  			 packet[1] + (packet[0] << 8),
  			 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
335
  			 packet[2] + (packet[3] << 8),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  			 packet[4] + (packet[5] << 8), buf, size, 1000);
  }
  
  static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
  {
  	int err = 0;
  	int databytes;
  	struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
  	struct usb_device *udev = ucpia->dev;
  
  	if (!udev) {
  		DBG("Internal driver error: udev is NULL
  ");
  		return -EINVAL;
  	}
  
  	if (!command) {
  		DBG("Internal driver error: command is NULL
  ");
  		return -EINVAL;
  	}
  
  	databytes = (((int)command[7])<<8) | command[6];
  
  	if (command[0] == DATA_IN) {
  		u8 buffer[8];
  
  		if (!data) {
  			DBG("Internal driver error: data is NULL
  ");
  			return -EINVAL;
  		}
  
  		err = ReadPacket(udev, command, buffer, 8);
  		if (err < 0)
  			return err;
  
  		memcpy(data, buffer, databytes);
  	} else if(command[0] == DATA_OUT)
  		WritePacket(udev, command, data, databytes);
  	else {
  		DBG("Unexpected first byte of command: %x
  ", command[0]);
  		err = -EINVAL;
  	}
  
  	return 0;
  }
  
  static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
  	void *cbdata)
  {
  	return -ENODEV;
  }
  
  static int cpia_usb_streamStart(void *privdata)
  {
  	return -ENODEV;
  }
  
  static int cpia_usb_streamStop(void *privdata)
  {
  	return -ENODEV;
  }
  
  static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
  {
  	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
  	struct framebuf *mybuff;
  
  	if (!ucpia || !ucpia->present)
  		return -1;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
408

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
411
412
413
414
415
416
417
418
  	if (ucpia->curbuff->status != FRAME_READY)
  		interruptible_sleep_on(&ucpia->wq_stream);
  	else
  		DBG("Frame already waiting!
  ");
  
  	mybuff = ucpia->curbuff;
  
  	if (!mybuff)
  		return -1;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
419

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
427
  	if (mybuff->status != FRAME_READY || mybuff->length < 4) {
  		DBG("Something went wrong!
  ");
  		return -1;
  	}
  
  	memcpy(frame, mybuff->data, mybuff->length);
  	mybuff->status = FRAME_EMPTY;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
428

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  /*   DBG("read done, %d bytes, Header: %x/%x, Footer: %x%x%x%x
  ",  */
  /*       mybuff->length, frame[0], frame[1], */
  /*       frame[mybuff->length-4], frame[mybuff->length-3],  */
  /*       frame[mybuff->length-2], frame[mybuff->length-1]); */
  
  	return mybuff->length;
  }
  
  static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
  {
  	if (!ucpia->streaming)
  		return;
  
  	ucpia->streaming = 0;
  
  	/* Set packet size to 0 */
  	if (try) {
  		int ret;
  
  		ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
  		if (ret < 0) {
  			printk(KERN_ERR "usb_set_interface error (ret = %d)
  ", ret);
  			return;
  		}
  	}
  
  	/* Unschedule all of the iso td's */
  	if (ucpia->sbuf[1].urb) {
  		usb_kill_urb(ucpia->sbuf[1].urb);
  		usb_free_urb(ucpia->sbuf[1].urb);
  		ucpia->sbuf[1].urb = NULL;
  	}
f9101210e   Jesper Juhl   [PATCH] vfree and...
463
464
  	kfree(ucpia->sbuf[1].data);
  	ucpia->sbuf[1].data = NULL;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
465

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
470
  	if (ucpia->sbuf[0].urb) {
  		usb_kill_urb(ucpia->sbuf[0].urb);
  		usb_free_urb(ucpia->sbuf[0].urb);
  		ucpia->sbuf[0].urb = NULL;
  	}
f9101210e   Jesper Juhl   [PATCH] vfree and...
471
472
  	kfree(ucpia->sbuf[0].data);
  	ucpia->sbuf[0].data = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  }
  
  static int cpia_usb_close(void *privdata)
  {
  	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
  
  	if(!ucpia)
  		return -ENODEV;
  
  	ucpia->open = 0;
  
  	/* ucpia->present = 0 protects against trying to reset the
  	 * alt setting if camera is physically disconnected while open */
  	cpia_usb_free_resources(ucpia, ucpia->present);
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
492
493
494
495
496
497
498
499
  /* Probing and initializing */
  
  static int cpia_probe(struct usb_interface *intf,
  		      const struct usb_device_id *id)
  {
  	struct usb_device *udev = interface_to_usbdev(intf);
  	struct usb_host_interface *interface;
  	struct usb_cpia *ucpia;
  	struct cam_data *cam;
  	int ret;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
500

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
503
504
505
506
507
508
  	/* A multi-config CPiA camera? */
  	if (udev->descriptor.bNumConfigurations != 1)
  		return -ENODEV;
  
  	interface = intf->cur_altsetting;
  
  	printk(KERN_INFO "USB CPiA camera found
  ");
7408187d2   Panagiotis Issaris   (7408187d223f63d4...
509
  	ucpia = kzalloc(sizeof(*ucpia), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
  	if (!ucpia) {
  		printk(KERN_ERR "couldn't kmalloc cpia struct
  ");
  		return -ENOMEM;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
  	ucpia->dev = udev;
  	ucpia->iface = interface->desc.bInterfaceNumber;
  	init_waitqueue_head(&ucpia->wq_stream);
  
  	ucpia->buffers[0] = vmalloc(sizeof(*ucpia->buffers[0]));
  	if (!ucpia->buffers[0]) {
  		printk(KERN_ERR "couldn't vmalloc frame buffer 0
  ");
  		goto fail_alloc_0;
  	}
  
  	ucpia->buffers[1] = vmalloc(sizeof(*ucpia->buffers[1]));
  	if (!ucpia->buffers[1]) {
  		printk(KERN_ERR "couldn't vmalloc frame buffer 1
  ");
  		goto fail_alloc_1;
  	}
  
  	ucpia->buffers[2] = vmalloc(sizeof(*ucpia->buffers[2]));
  	if (!ucpia->buffers[2]) {
  		printk(KERN_ERR "couldn't vmalloc frame buffer 2
  ");
  		goto fail_alloc_2;
  	}
  
  	ucpia->buffers[0]->next = ucpia->buffers[1];
  	ucpia->buffers[1]->next = ucpia->buffers[2];
  	ucpia->buffers[2]->next = ucpia->buffers[0];
  
  	ret = usb_set_interface(udev, ucpia->iface, 0);
  	if (ret < 0) {
  		printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)
  ", ret);
  		/* goto fail_all; */
  	}
  
  	/* Before register_camera, important */
  	ucpia->present = 1;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
553

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  	cam = cpia_register_camera(&cpia_usb_ops, ucpia);
  	if (!cam) {
  		LOG("failed to cpia_register_camera
  ");
  		goto fail_all;
  	}
  
  	spin_lock( &cam_list_lock_usb );
  	list_add( &cam->cam_data_list, &cam_list );
  	spin_unlock( &cam_list_lock_usb );
  
  	usb_set_intfdata(intf, cam);
  	return 0;
  
  fail_all:
  	vfree(ucpia->buffers[2]);
  	ucpia->buffers[2] = NULL;
  fail_alloc_2:
  	vfree(ucpia->buffers[1]);
  	ucpia->buffers[1] = NULL;
  fail_alloc_1:
  	vfree(ucpia->buffers[0]);
  	ucpia->buffers[0] = NULL;
  fail_alloc_0:
  	kfree(ucpia);
  	return -EIO;
  }
  
  static void cpia_disconnect(struct usb_interface *intf);
  
  static struct usb_device_id cpia_id_table [] = {
  	{ USB_DEVICE(0x0553, 0x0002) },
  	{ USB_DEVICE(0x0813, 0x0001) },
  	{ }					/* Terminating entry */
  };
  
  MODULE_DEVICE_TABLE (usb, cpia_id_table);
  MODULE_LICENSE("GPL");
  
  
  static struct usb_driver cpia_driver = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
597
598
599
600
601
602
603
604
605
  	.name		= "cpia",
  	.probe		= cpia_probe,
  	.disconnect	= cpia_disconnect,
  	.id_table	= cpia_id_table,
  };
  
  static void cpia_disconnect(struct usb_interface *intf)
  {
  	struct cam_data *cam = usb_get_intfdata(intf);
  	struct usb_cpia *ucpia;
  	struct usb_device *udev;
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
606

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
607
608
609
610
611
612
613
614
  	usb_set_intfdata(intf, NULL);
  	if (!cam)
  		return;
  
  	ucpia = (struct usb_cpia *) cam->lowlevel_data;
  	spin_lock( &cam_list_lock_usb );
  	list_del(&cam->cam_data_list);
  	spin_unlock( &cam_list_lock_usb );
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
615

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
617
618
619
620
621
622
623
624
625
626
627
628
629
  	ucpia->present = 0;
  
  	cpia_unregister_camera(cam);
  	if(ucpia->open)
  		cpia_usb_close(cam->lowlevel_data);
  
  	ucpia->curbuff->status = FRAME_ERROR;
  
  	if (waitqueue_active(&ucpia->wq_stream))
  		wake_up_interruptible(&ucpia->wq_stream);
  
  	udev = interface_to_usbdev(intf);
  
  	ucpia->curbuff = ucpia->workbuff = NULL;
f9101210e   Jesper Juhl   [PATCH] vfree and...
630
631
  	vfree(ucpia->buffers[2]);
  	ucpia->buffers[2] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632

f9101210e   Jesper Juhl   [PATCH] vfree and...
633
634
  	vfree(ucpia->buffers[1]);
  	ucpia->buffers[1] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635

f9101210e   Jesper Juhl   [PATCH] vfree and...
636
637
  	vfree(ucpia->buffers[0]);
  	ucpia->buffers[0] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
639
640
641
642
643
644
  
  	cam->lowlevel_data = NULL;
  	kfree(ucpia);
  }
  
  static int __init usb_cpia_init(void)
  {
d56410e0a   Mauro Carvalho Chehab   V4L/DVB (3599b): ...
645
646
  	printk(KERN_INFO "%s v%d.%d.%d
  ",ABOUT,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  	       CPIA_USB_MAJ_VER,CPIA_USB_MIN_VER,CPIA_USB_PATCH_VER);
  
  	spin_lock_init(&cam_list_lock_usb);
  	return usb_register(&cpia_driver);
  }
  
  static void __exit usb_cpia_cleanup(void)
  {
  	usb_deregister(&cpia_driver);
  }
  
  
  module_init (usb_cpia_init);
  module_exit (usb_cpia_cleanup);