Linux 2.4.0-test9pre4
[davej-history.git] / drivers / media / video / cpia_usb.c
blob79687ebba792569c20c91a15fcea0a021b849ae0
1 /*
2 * cpia_usb CPiA USB driver
4 * Supports CPiA based parallel port Video Camera's.
6 * Copyright (C) 1999 Jochen Scharrlach <Jochen.Scharrlach@schwaben.de>
7 * Copyright (C) 1999, 2000 Johannes Erdfelt <jerdfelt@valinux.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/wait.h>
28 #include <linux/sched.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/usb.h>
34 #include "cpia.h"
36 #define USB_REQ_CPIA_GRAB_FRAME 0xC1
37 #define USB_REQ_CPIA_UPLOAD_FRAME 0xC2
38 #define WAIT_FOR_NEXT_FRAME 0
39 #define FORCE_FRAME_UPLOAD 1
41 #define FRAMES_PER_DESC 10
42 #define FRAME_SIZE_PER_DESC 960 /* Shouldn't be hardcoded */
43 #define CPIA_NUMSBUF 2
44 #define STREAM_BUF_SIZE (PAGE_SIZE * 4)
45 #define SCRATCH_BUF_SIZE (STREAM_BUF_SIZE * 2)
47 struct cpia_sbuf {
48 char *data;
49 urb_t *urb;
52 #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
53 enum framebuf_status {
54 FRAME_EMPTY,
55 FRAME_READING,
56 FRAME_READY,
57 FRAME_ERROR,
60 struct framebuf {
61 int length;
62 enum framebuf_status status;
63 u8 data[FRAMEBUF_LEN];
64 struct framebuf *next;
67 struct usb_cpia {
68 /* Device structure */
69 struct usb_device *dev;
71 unsigned char iface;
72 wait_queue_head_t wq_stream;
74 int cursbuf; /* Current receiving sbuf */
75 struct cpia_sbuf sbuf[CPIA_NUMSBUF]; /* Double buffering */
77 int streaming;
78 int open;
79 int present;
80 struct framebuf *buffers[3];
81 struct framebuf *curbuff, *workbuff;
84 static int cpia_usb_open(void *privdata);
85 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
86 void *cbdata);
87 static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);
88 static int cpia_usb_streamStart(void *privdata);
89 static int cpia_usb_streamStop(void *privdata);
90 static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);
91 static int cpia_usb_close(void *privdata);
93 #define ABOUT "USB driver for Vision CPiA based cameras"
95 static struct cpia_camera_ops cpia_usb_ops = {
96 cpia_usb_open,
97 cpia_usb_registerCallback,
98 cpia_usb_transferCmd,
99 cpia_usb_streamStart,
100 cpia_usb_streamStop,
101 cpia_usb_streamRead,
102 cpia_usb_close,
106 static struct cam_data *cam_list;
108 static void cpia_usb_complete(struct urb *urb)
110 int i;
111 char *cdata;
112 struct usb_cpia *ucpia;
114 if (!urb || !urb->context)
115 return;
117 ucpia = (struct usb_cpia *) urb->context;
119 if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
120 return;
122 if (ucpia->workbuff->status == FRAME_EMPTY) {
123 ucpia->workbuff->status = FRAME_READING;
124 ucpia->workbuff->length = 0;
127 for (i = 0; i < urb->number_of_packets; i++) {
128 int n = urb->iso_frame_desc[i].actual_length;
129 int st = urb->iso_frame_desc[i].status;
131 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
133 if (st)
134 printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);
136 if (FRAMEBUF_LEN < ucpia->workbuff->length + n) {
137 printk(KERN_DEBUG "cpia: scratch buf overflow!scr_len: %d, n: %d\n", ucpia->workbuff->length, n);
138 return;
141 if (n) {
142 if ((ucpia->workbuff->length > 0) ||
143 (0x19 == cdata[0] && 0x68 == cdata[1])) {
144 memcpy(ucpia->workbuff->data + ucpia->workbuff->length, cdata, n);
145 ucpia->workbuff->length += n;
146 } else
147 DBG("Ignoring packet!\n");
148 } else {
149 if (ucpia->workbuff->length > 4 &&
150 0xff == ucpia->workbuff->data[ucpia->workbuff->length-1] &&
151 0xff == ucpia->workbuff->data[ucpia->workbuff->length-2] &&
152 0xff == ucpia->workbuff->data[ucpia->workbuff->length-3] &&
153 0xff == ucpia->workbuff->data[ucpia->workbuff->length-4]) {
154 ucpia->workbuff->status = FRAME_READY;
155 ucpia->curbuff = ucpia->workbuff;
156 ucpia->workbuff = ucpia->workbuff->next;
157 ucpia->workbuff->status = FRAME_EMPTY;
158 ucpia->workbuff->length = 0;
160 if (waitqueue_active(&ucpia->wq_stream))
161 wake_up_interruptible(&ucpia->wq_stream);
167 static int cpia_usb_open(void *privdata)
169 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
170 urb_t *urb;
171 int ret, retval = 0, fx, err;
173 if (!ucpia)
174 return -EINVAL;
176 ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
177 if (!ucpia->sbuf[0].data)
178 return -EINVAL;
180 ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
181 if (!ucpia->sbuf[1].data) {
182 retval = -EINVAL;
183 goto error_0;
186 ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
187 if (ret < 0) {
188 printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);
189 retval = -EBUSY;
190 goto error_1;
193 ucpia->buffers[0]->status = FRAME_EMPTY;
194 ucpia->buffers[0]->length = 0;
195 ucpia->buffers[1]->status = FRAME_EMPTY;
196 ucpia->buffers[1]->length = 0;
197 ucpia->buffers[2]->status = FRAME_EMPTY;
198 ucpia->buffers[2]->length = 0;
199 ucpia->curbuff = ucpia->buffers[0];
200 ucpia->workbuff = ucpia->buffers[1];
202 /* We double buffer the Iso lists */
203 urb = usb_alloc_urb(FRAMES_PER_DESC);
204 if (!urb) {
205 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
206 retval = -ENOMEM;
207 goto error_1;
210 ucpia->sbuf[0].urb = urb;
211 urb->dev = ucpia->dev;
212 urb->context = ucpia;
213 urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
214 urb->transfer_flags = USB_ISO_ASAP;
215 urb->transfer_buffer = ucpia->sbuf[0].data;
216 urb->complete = cpia_usb_complete;
217 urb->number_of_packets = FRAMES_PER_DESC;
218 urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
219 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
220 urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
221 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
224 urb = usb_alloc_urb(FRAMES_PER_DESC);
225 if (!urb) {
226 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1\n");
227 retval = -ENOMEM;
228 goto error_urb0;
231 ucpia->sbuf[1].urb = urb;
232 urb->dev = ucpia->dev;
233 urb->context = ucpia;
234 urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
235 urb->transfer_flags = USB_ISO_ASAP;
236 urb->transfer_buffer = ucpia->sbuf[1].data;
237 urb->complete = cpia_usb_complete;
238 urb->number_of_packets = FRAMES_PER_DESC;
239 urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
240 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
241 urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
242 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
245 ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;
246 ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;
248 err = usb_submit_urb(ucpia->sbuf[0].urb);
249 if (err) {
250 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",
251 err);
252 goto error_urb1;
254 err = usb_submit_urb(ucpia->sbuf[1].urb);
255 if (err) {
256 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",
257 err);
258 goto error_urb1;
261 ucpia->streaming = 1;
262 ucpia->open = 1;
264 return 0;
266 error_urb1: /* free urb 1 */
267 usb_free_urb(ucpia->sbuf[1].urb);
269 error_urb0: /* free urb 0 */
270 usb_free_urb(ucpia->sbuf[0].urb);
272 error_1:
273 kfree (ucpia->sbuf[1].data);
274 error_0:
275 kfree (ucpia->sbuf[0].data);
277 return retval;
281 // convenience functions
284 /****************************************************************************
286 * WritePacket
288 ***************************************************************************/
289 static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
291 if (!packet)
292 return -EINVAL;
294 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
295 packet[1] + (packet[0] << 8),
296 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
297 packet[2] + (packet[3] << 8),
298 packet[4] + (packet[5] << 8), buf, size, HZ);
301 /****************************************************************************
303 * ReadPacket
305 ***************************************************************************/
306 static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
308 if (!packet || size <= 0)
309 return -EINVAL;
311 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
312 packet[1] + (packet[0] << 8),
313 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
314 packet[2] + (packet[3] << 8),
315 packet[4] + (packet[5] << 8), buf, size, HZ);
318 static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
320 int err = 0;
321 int databytes;
322 struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
323 struct usb_device *udev = ucpia->dev;
325 if (!udev) {
326 DBG("Internal driver error: udev is NULL\n");
327 return -EINVAL;
330 if (!command) {
331 DBG("Internal driver error: command is NULL\n");
332 return -EINVAL;
335 databytes = (((int)command[7])<<8) | command[6];
337 if (command[0] == DATA_IN) {
338 u8 buffer[8];
340 if (!data) {
341 DBG("Internal driver error: data is NULL\n");
342 return -EINVAL;
345 err = ReadPacket(udev, command, buffer, 8);
346 if (err < 0)
347 return err;
349 memcpy(data, buffer, databytes);
350 } else if(command[0] == DATA_OUT)
351 WritePacket(udev, command, data, databytes);
352 else {
353 DBG("Unexpected first byte of command: %x\n", command[0]);
354 err = -EINVAL;
357 return 0;
360 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
361 void *cbdata)
363 return -ENODEV;
366 static int cpia_usb_streamStart(void *privdata)
368 return -ENODEV;
371 static int cpia_usb_streamStop(void *privdata)
373 return -ENODEV;
376 static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
378 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
379 struct framebuf *mybuff;
381 if (!ucpia || !ucpia->present)
382 return -1;
384 if (ucpia->curbuff->status != FRAME_READY)
385 interruptible_sleep_on(&ucpia->wq_stream);
386 else
387 DBG("Frame already waiting!\n");
389 mybuff = ucpia->curbuff;
391 if (!mybuff)
392 return -1;
394 if (mybuff->status != FRAME_READY || mybuff->length < 4) {
395 DBG("Something went wrong!\n");
396 return -1;
399 memcpy(frame, mybuff->data, mybuff->length);
400 mybuff->status = FRAME_EMPTY;
402 /* DBG("read done, %d bytes, Header: %x/%x, Footer: %x%x%x%x\n", */
403 /* mybuff->length, frame[0], frame[1], */
404 /* frame[mybuff->length-4], frame[mybuff->length-3], */
405 /* frame[mybuff->length-2], frame[mybuff->length-1]); */
407 return mybuff->length;
410 static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
412 if (!ucpia->streaming)
413 return;
415 ucpia->streaming = 0;
417 /* Set packet size to 0 */
418 if (try) {
419 int ret;
421 ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
422 if (ret < 0) {
423 printk(KERN_ERR "usb_set_interface error (ret = %d)\n", ret);
424 return;
428 /* Unschedule all of the iso td's */
429 if (ucpia->sbuf[1].urb) {
430 usb_unlink_urb(ucpia->sbuf[1].urb);
431 usb_free_urb(ucpia->sbuf[1].urb);
432 ucpia->sbuf[1].urb = NULL;
435 if (ucpia->sbuf[0].urb) {
436 usb_unlink_urb(ucpia->sbuf[0].urb);
437 usb_free_urb(ucpia->sbuf[0].urb);
438 ucpia->sbuf[0].urb = NULL;
442 static int cpia_usb_close(void *privdata)
444 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
446 ucpia->open = 0;
448 cpia_usb_free_resources(ucpia, 1);
450 if (!ucpia->present)
451 kfree(ucpia);
453 return 0;
456 int cpia_usb_init(void)
458 /* return -ENODEV; */
459 return 0;
462 /* Probing and initializing */
464 static void *cpia_probe(struct usb_device *udev, unsigned int ifnum)
466 struct usb_interface_descriptor *interface;
467 struct usb_cpia *ucpia;
468 struct cam_data *cam;
469 int ret;
471 /* A multi-config CPiA camera? */
472 if (udev->descriptor.bNumConfigurations != 1)
473 return NULL;
475 interface = &udev->actconfig->interface[ifnum].altsetting[0];
477 /* Is it a CPiA? */
478 if (udev->descriptor.idVendor != 0x0553)
479 return NULL;
480 if (udev->descriptor.idProduct != 0x0002)
481 return NULL;
483 /* We found a CPiA */
484 printk(KERN_INFO "USB CPiA camera found\n");
486 ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
487 if (!ucpia) {
488 printk(KERN_ERR "couldn't kmalloc cpia struct\n");
489 return NULL;
492 memset(ucpia, 0, sizeof(*ucpia));
494 ucpia->dev = udev;
495 ucpia->iface = interface->bInterfaceNumber;
496 init_waitqueue_head(&ucpia->wq_stream);
498 ucpia->buffers[0] = vmalloc(sizeof(*ucpia->buffers[0]));
499 if (!ucpia->buffers[0]) {
500 printk(KERN_ERR "couldn't vmalloc frame buffer 0\n");
501 goto fail_alloc_0;
504 ucpia->buffers[1] = vmalloc(sizeof(*ucpia->buffers[1]));
505 if (!ucpia->buffers[1]) {
506 printk(KERN_ERR "couldn't vmalloc frame buffer 1\n");
507 goto fail_alloc_1;
510 ucpia->buffers[2] = vmalloc(sizeof(*ucpia->buffers[2]));
511 if (!ucpia->buffers[2]) {
512 printk(KERN_ERR "couldn't vmalloc frame buffer 2\n");
513 goto fail_alloc_2;
516 ucpia->buffers[0]->next = ucpia->buffers[1];
517 ucpia->buffers[1]->next = ucpia->buffers[2];
518 ucpia->buffers[2]->next = ucpia->buffers[0];
520 ret = usb_set_interface(udev, ucpia->iface, 0);
521 if (ret < 0) {
522 printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
523 /* goto fail_all; */
526 /* Before register_camera, important */
527 ucpia->present = 1;
529 cam = cpia_register_camera(&cpia_usb_ops, ucpia);
530 if (!cam) {
531 LOG("failed to cpia_register_camera\n");
532 goto fail_all;
535 ADD_TO_LIST(cam_list, cam);
537 return cam;
539 fail_all:
540 vfree(ucpia->buffers[2]);
541 ucpia->buffers[2] = NULL;
542 fail_alloc_2:
543 vfree(ucpia->buffers[1]);
544 ucpia->buffers[1] = NULL;
545 fail_alloc_1:
546 vfree(ucpia->buffers[0]);
547 ucpia->buffers[0] = NULL;
548 fail_alloc_0:
550 return NULL;
553 static void cpia_disconnect(struct usb_device *dev, void *ptr);
555 static struct usb_driver cpia_driver = {
556 "cpia",
557 cpia_probe,
558 cpia_disconnect,
559 { NULL, NULL }
562 /* don't use dev, it may be NULL! (see usb_cpia_cleanup) */
563 /* _disconnect from usb_cpia_cleanup is not necessary since usb_deregister */
564 /* will do it for us as well as passing a udev structure - jerdfelt */
565 static void cpia_disconnect(struct usb_device *udev, void *ptr)
567 struct cam_data *cam = (struct cam_data *) ptr;
568 struct usb_cpia *ucpia = (struct usb_cpia *) cam->lowlevel_data;
570 REMOVE_FROM_LIST(cam);
572 /* Don't even try to reset the altsetting if we're disconnected */
573 cpia_usb_free_resources(ucpia, 0);
575 ucpia->present = 0;
577 cpia_unregister_camera(cam);
579 ucpia->curbuff->status = FRAME_ERROR;
581 if (waitqueue_active(&ucpia->wq_stream))
582 wake_up_interruptible(&ucpia->wq_stream);
584 usb_driver_release_interface(&cpia_driver,
585 &udev->actconfig->interface[0]);
587 ucpia->curbuff = ucpia->workbuff = NULL;
589 if (ucpia->buffers[2]) {
590 vfree(ucpia->buffers[2]);
591 ucpia->buffers[2] = NULL;
594 if (ucpia->buffers[1]) {
595 vfree(ucpia->buffers[1]);
596 ucpia->buffers[1] = NULL;
599 if (ucpia->buffers[0]) {
600 vfree(ucpia->buffers[0]);
601 ucpia->buffers[0] = NULL;
604 if (!ucpia->open)
605 kfree(ucpia);
608 static int __init usb_cpia_init(void)
610 cam_list = NULL;
612 return usb_register(&cpia_driver);
615 static void __exit usb_cpia_cleanup(void)
618 struct cam_data *cam;
620 while ((cam = cam_list) != NULL)
621 cpia_disconnect(NULL, cam);
624 usb_deregister(&cpia_driver);
628 module_init (usb_cpia_init);
629 module_exit (usb_cpia_cleanup);