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 <johannes@erdfelt.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 /* define _CPIA_DEBUG_ for verbose debug output (see cpia.h) */
25 /* #define _CPIA_DEBUG_ 1 */
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/wait.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/vmalloc.h>
34 #include <linux/usb.h>
38 #define USB_REQ_CPIA_GRAB_FRAME 0xC1
39 #define USB_REQ_CPIA_UPLOAD_FRAME 0xC2
40 #define WAIT_FOR_NEXT_FRAME 0
41 #define FORCE_FRAME_UPLOAD 1
43 #define FRAMES_PER_DESC 10
44 #define FRAME_SIZE_PER_DESC 960 /* Shouldn't be hardcoded */
45 #define CPIA_NUMSBUF 2
46 #define STREAM_BUF_SIZE (PAGE_SIZE * 4)
47 #define SCRATCH_BUF_SIZE (STREAM_BUF_SIZE * 2)
54 #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
55 enum framebuf_status {
64 enum framebuf_status status;
65 u8 data[FRAMEBUF_LEN];
66 struct framebuf *next;
70 /* Device structure */
71 struct usb_device *dev;
74 wait_queue_head_t wq_stream;
76 int cursbuf; /* Current receiving sbuf */
77 struct cpia_sbuf sbuf[CPIA_NUMSBUF]; /* Double buffering */
82 struct framebuf *buffers[3];
83 struct framebuf *curbuff, *workbuff;
86 static int cpia_usb_open(void *privdata);
87 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
89 static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);
90 static int cpia_usb_streamStart(void *privdata);
91 static int cpia_usb_streamStop(void *privdata);
92 static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);
93 static int cpia_usb_close(void *privdata);
95 #define ABOUT "USB driver for Vision CPiA based cameras"
97 static struct cpia_camera_ops cpia_usb_ops = {
99 cpia_usb_registerCallback,
100 cpia_usb_transferCmd,
101 cpia_usb_streamStart,
109 static LIST_HEAD(cam_list);
110 static spinlock_t cam_list_lock_usb;
112 static void cpia_usb_complete(struct urb *urb)
116 struct usb_cpia *ucpia;
118 if (!urb || !urb->context)
121 ucpia = (struct usb_cpia *) urb->context;
123 if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
126 if (ucpia->workbuff->status == FRAME_EMPTY) {
127 ucpia->workbuff->status = FRAME_READING;
128 ucpia->workbuff->length = 0;
131 for (i = 0; i < urb->number_of_packets; i++) {
132 int n = urb->iso_frame_desc[i].actual_length;
133 int st = urb->iso_frame_desc[i].status;
135 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
138 printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);
140 if (FRAMEBUF_LEN < ucpia->workbuff->length + n) {
141 printk(KERN_DEBUG "cpia: scratch buf overflow!scr_len: %d, n: %d\n", ucpia->workbuff->length, n);
146 if ((ucpia->workbuff->length > 0) ||
147 (0x19 == cdata[0] && 0x68 == cdata[1])) {
148 memcpy(ucpia->workbuff->data + ucpia->workbuff->length, cdata, n);
149 ucpia->workbuff->length += n;
151 DBG("Ignoring packet!\n");
153 if (ucpia->workbuff->length > 4 &&
154 0xff == ucpia->workbuff->data[ucpia->workbuff->length-1] &&
155 0xff == ucpia->workbuff->data[ucpia->workbuff->length-2] &&
156 0xff == ucpia->workbuff->data[ucpia->workbuff->length-3] &&
157 0xff == ucpia->workbuff->data[ucpia->workbuff->length-4]) {
158 ucpia->workbuff->status = FRAME_READY;
159 ucpia->curbuff = ucpia->workbuff;
160 ucpia->workbuff = ucpia->workbuff->next;
161 ucpia->workbuff->status = FRAME_EMPTY;
162 ucpia->workbuff->length = 0;
164 if (waitqueue_active(&ucpia->wq_stream))
165 wake_up_interruptible(&ucpia->wq_stream);
171 static int cpia_usb_open(void *privdata)
173 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
175 int ret, retval = 0, fx, err;
180 ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
181 if (!ucpia->sbuf[0].data)
184 ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
185 if (!ucpia->sbuf[1].data) {
190 ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
192 printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);
197 ucpia->buffers[0]->status = FRAME_EMPTY;
198 ucpia->buffers[0]->length = 0;
199 ucpia->buffers[1]->status = FRAME_EMPTY;
200 ucpia->buffers[1]->length = 0;
201 ucpia->buffers[2]->status = FRAME_EMPTY;
202 ucpia->buffers[2]->length = 0;
203 ucpia->curbuff = ucpia->buffers[0];
204 ucpia->workbuff = ucpia->buffers[1];
206 /* We double buffer the Iso lists */
207 urb = usb_alloc_urb(FRAMES_PER_DESC);
209 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
214 ucpia->sbuf[0].urb = urb;
215 urb->dev = ucpia->dev;
216 urb->context = ucpia;
217 urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
218 urb->transfer_flags = USB_ISO_ASAP;
219 urb->transfer_buffer = ucpia->sbuf[0].data;
220 urb->complete = cpia_usb_complete;
221 urb->number_of_packets = FRAMES_PER_DESC;
222 urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
223 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
224 urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
225 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
228 urb = usb_alloc_urb(FRAMES_PER_DESC);
230 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1\n");
235 ucpia->sbuf[1].urb = urb;
236 urb->dev = ucpia->dev;
237 urb->context = ucpia;
238 urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
239 urb->transfer_flags = USB_ISO_ASAP;
240 urb->transfer_buffer = ucpia->sbuf[1].data;
241 urb->complete = cpia_usb_complete;
242 urb->number_of_packets = FRAMES_PER_DESC;
243 urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
244 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
245 urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
246 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
249 ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;
250 ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;
252 err = usb_submit_urb(ucpia->sbuf[0].urb);
254 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",
258 err = usb_submit_urb(ucpia->sbuf[1].urb);
260 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",
265 ucpia->streaming = 1;
270 error_urb1: /* free urb 1 */
271 usb_free_urb(ucpia->sbuf[1].urb);
272 ucpia->sbuf[1].urb = NULL;
273 error_urb0: /* free urb 0 */
274 usb_free_urb(ucpia->sbuf[0].urb);
275 ucpia->sbuf[0].urb = NULL;
277 kfree (ucpia->sbuf[1].data);
278 ucpia->sbuf[1].data = NULL;
280 kfree (ucpia->sbuf[0].data);
281 ucpia->sbuf[0].data = NULL;
287 // convenience functions
290 /****************************************************************************
294 ***************************************************************************/
295 static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
300 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
301 packet[1] + (packet[0] << 8),
302 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
303 packet[2] + (packet[3] << 8),
304 packet[4] + (packet[5] << 8), buf, size, HZ);
307 /****************************************************************************
311 ***************************************************************************/
312 static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
314 if (!packet || size <= 0)
317 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
318 packet[1] + (packet[0] << 8),
319 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
320 packet[2] + (packet[3] << 8),
321 packet[4] + (packet[5] << 8), buf, size, HZ);
324 static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
328 struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
329 struct usb_device *udev = ucpia->dev;
332 DBG("Internal driver error: udev is NULL\n");
337 DBG("Internal driver error: command is NULL\n");
341 databytes = (((int)command[7])<<8) | command[6];
343 if (command[0] == DATA_IN) {
347 DBG("Internal driver error: data is NULL\n");
351 err = ReadPacket(udev, command, buffer, 8);
355 memcpy(data, buffer, databytes);
356 } else if(command[0] == DATA_OUT)
357 WritePacket(udev, command, data, databytes);
359 DBG("Unexpected first byte of command: %x\n", command[0]);
366 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
372 static int cpia_usb_streamStart(void *privdata)
377 static int cpia_usb_streamStop(void *privdata)
382 static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
384 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
385 struct framebuf *mybuff;
387 if (!ucpia || !ucpia->present)
390 if (ucpia->curbuff->status != FRAME_READY)
391 interruptible_sleep_on(&ucpia->wq_stream);
393 DBG("Frame already waiting!\n");
395 mybuff = ucpia->curbuff;
400 if (mybuff->status != FRAME_READY || mybuff->length < 4) {
401 DBG("Something went wrong!\n");
405 memcpy(frame, mybuff->data, mybuff->length);
406 mybuff->status = FRAME_EMPTY;
408 /* DBG("read done, %d bytes, Header: %x/%x, Footer: %x%x%x%x\n", */
409 /* mybuff->length, frame[0], frame[1], */
410 /* frame[mybuff->length-4], frame[mybuff->length-3], */
411 /* frame[mybuff->length-2], frame[mybuff->length-1]); */
413 return mybuff->length;
416 static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
418 if (!ucpia->streaming)
421 ucpia->streaming = 0;
423 /* Set packet size to 0 */
427 ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
429 printk(KERN_ERR "usb_set_interface error (ret = %d)\n", ret);
434 /* Unschedule all of the iso td's */
435 if (ucpia->sbuf[1].urb) {
436 usb_unlink_urb(ucpia->sbuf[1].urb);
437 usb_free_urb(ucpia->sbuf[1].urb);
438 ucpia->sbuf[1].urb = NULL;
441 if (ucpia->sbuf[1].data) {
442 kfree(ucpia->sbuf[1].data);
443 ucpia->sbuf[1].data = NULL;
446 if (ucpia->sbuf[0].urb) {
447 usb_unlink_urb(ucpia->sbuf[0].urb);
448 usb_free_urb(ucpia->sbuf[0].urb);
449 ucpia->sbuf[0].urb = NULL;
452 if (ucpia->sbuf[0].data) {
453 kfree(ucpia->sbuf[0].data);
454 ucpia->sbuf[0].data = NULL;
458 static int cpia_usb_close(void *privdata)
460 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
467 /* ucpia->present = 0 protects against trying to reset the
468 * alt setting if camera is physically disconnected while open */
469 cpia_usb_free_resources(ucpia, ucpia->present);
474 int cpia_usb_init(void)
476 /* return -ENODEV; */
480 /* Probing and initializing */
482 static void *cpia_probe(struct usb_device *udev, unsigned int ifnum,
483 const struct usb_device_id *id)
485 struct usb_interface_descriptor *interface;
486 struct usb_cpia *ucpia;
487 struct cam_data *cam;
490 /* A multi-config CPiA camera? */
491 if (udev->descriptor.bNumConfigurations != 1)
494 interface = &udev->actconfig->interface[ifnum].altsetting[0];
496 printk(KERN_INFO "USB CPiA camera found\n");
498 ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
500 printk(KERN_ERR "couldn't kmalloc cpia struct\n");
504 memset(ucpia, 0, sizeof(*ucpia));
507 ucpia->iface = interface->bInterfaceNumber;
508 init_waitqueue_head(&ucpia->wq_stream);
510 ucpia->buffers[0] = vmalloc(sizeof(*ucpia->buffers[0]));
511 if (!ucpia->buffers[0]) {
512 printk(KERN_ERR "couldn't vmalloc frame buffer 0\n");
516 ucpia->buffers[1] = vmalloc(sizeof(*ucpia->buffers[1]));
517 if (!ucpia->buffers[1]) {
518 printk(KERN_ERR "couldn't vmalloc frame buffer 1\n");
522 ucpia->buffers[2] = vmalloc(sizeof(*ucpia->buffers[2]));
523 if (!ucpia->buffers[2]) {
524 printk(KERN_ERR "couldn't vmalloc frame buffer 2\n");
528 ucpia->buffers[0]->next = ucpia->buffers[1];
529 ucpia->buffers[1]->next = ucpia->buffers[2];
530 ucpia->buffers[2]->next = ucpia->buffers[0];
532 ret = usb_set_interface(udev, ucpia->iface, 0);
534 printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
538 /* Before register_camera, important */
541 cam = cpia_register_camera(&cpia_usb_ops, ucpia);
543 LOG("failed to cpia_register_camera\n");
547 spin_lock( &cam_list_lock_usb );
548 list_add( &cam->cam_data_list, &cam_list );
549 spin_unlock( &cam_list_lock_usb );
554 vfree(ucpia->buffers[2]);
555 ucpia->buffers[2] = NULL;
557 vfree(ucpia->buffers[1]);
558 ucpia->buffers[1] = NULL;
560 vfree(ucpia->buffers[0]);
561 ucpia->buffers[0] = NULL;
568 static void cpia_disconnect(struct usb_device *dev, void *ptr);
570 static struct usb_device_id cpia_id_table [] = {
571 { USB_DEVICE(0x0553, 0x0002) },
572 { USB_DEVICE(0x0813, 0x0001) },
573 { } /* Terminating entry */
576 MODULE_DEVICE_TABLE (usb, cpia_id_table);
577 MODULE_LICENSE("GPL");
580 static struct usb_driver cpia_driver = {
583 disconnect: cpia_disconnect,
584 id_table: cpia_id_table,
587 static void cpia_disconnect(struct usb_device *udev, void *ptr)
589 struct cam_data *cam = (struct cam_data *) ptr;
590 struct usb_cpia *ucpia = (struct usb_cpia *) cam->lowlevel_data;
592 spin_lock( &cam_list_lock_usb );
593 list_del(&cam->cam_data_list);
594 spin_unlock( &cam_list_lock_usb );
597 cpia_unregister_camera(cam);
599 cpia_usb_close(cam->lowlevel_data);
601 ucpia->curbuff->status = FRAME_ERROR;
602 if (waitqueue_active(&ucpia->wq_stream))
603 wake_up_interruptible(&ucpia->wq_stream);
604 usb_driver_release_interface(&cpia_driver,
605 &udev->actconfig->interface[0]);
606 ucpia->curbuff = ucpia->workbuff = NULL;
607 if (ucpia->buffers[2]) {
608 vfree(ucpia->buffers[2]);
609 ucpia->buffers[2] = NULL;
611 if (ucpia->buffers[1]) {
612 vfree(ucpia->buffers[1]);
613 ucpia->buffers[1] = NULL;
615 if (ucpia->buffers[0]) {
616 vfree(ucpia->buffers[0]);
617 ucpia->buffers[0] = NULL;
620 cam->lowlevel_data = NULL;
624 static int __init usb_cpia_init(void)
626 printk(KERN_INFO "%s v%d.%d.%d\n",ABOUT,
627 CPIA_USB_MAJ_VER,CPIA_USB_MIN_VER,CPIA_USB_PATCH_VER);
629 spin_lock_init(&cam_list_lock_usb);
630 return usb_register(&cpia_driver);
633 static void __exit usb_cpia_cleanup(void)
635 usb_deregister(&cpia_driver);
638 module_init (usb_cpia_init);
639 module_exit (usb_cpia_cleanup);