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/wait.h>
27 #include <linux/sched.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/usb.h>
35 #define USB_REQ_CPIA_GRAB_FRAME 0xC1
36 #define USB_REQ_CPIA_UPLOAD_FRAME 0xC2
37 #define WAIT_FOR_NEXT_FRAME 0
38 #define FORCE_FRAME_UPLOAD 1
40 #define FRAMES_PER_DESC 10
41 #define FRAME_SIZE_PER_DESC 960 /* Shouldn't be hardcoded */
42 #define CPIA_NUMSBUF 2
43 #define STREAM_BUF_SIZE (PAGE_SIZE * 4)
44 #define SCRATCH_BUF_SIZE (STREAM_BUF_SIZE * 2)
51 #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
52 enum framebuf_status
{
61 enum framebuf_status status
;
62 u8 data
[FRAMEBUF_LEN
];
63 struct framebuf
*next
;
67 /* Device structure */
68 struct usb_device
*dev
;
71 wait_queue_head_t wq_stream
;
73 int cursbuf
; /* Current receiving sbuf */
74 struct cpia_sbuf sbuf
[CPIA_NUMSBUF
]; /* Double buffering */
79 struct framebuf
*buffers
[3];
80 struct framebuf
*curbuff
, *workbuff
;
83 static int cpia_usb_open(void *privdata
);
84 static int cpia_usb_registerCallback(void *privdata
, void (*cb
) (void *cbdata
),
86 static int cpia_usb_transferCmd(void *privdata
, u8
*command
, u8
*data
);
87 static int cpia_usb_streamStart(void *privdata
);
88 static int cpia_usb_streamStop(void *privdata
);
89 static int cpia_usb_streamRead(void *privdata
, u8
*frame
, int noblock
);
90 static int cpia_usb_close(void *privdata
);
92 #define ABOUT "USB driver for Vision CPiA based cameras"
94 static struct cpia_camera_ops cpia_usb_ops
= {
96 cpia_usb_registerCallback
,
105 static struct cam_data
*cam_list
;
107 static void cpia_usb_complete(struct urb
*urb
)
111 struct usb_cpia
*ucpia
;
113 if (!urb
|| !urb
->context
)
116 ucpia
= (struct usb_cpia
*) urb
->context
;
118 if (!ucpia
->dev
|| !ucpia
->streaming
|| !ucpia
->present
|| !ucpia
->open
)
121 if (ucpia
->workbuff
->status
== FRAME_EMPTY
) {
122 ucpia
->workbuff
->status
= FRAME_READING
;
123 ucpia
->workbuff
->length
= 0;
126 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
127 int n
= urb
->iso_frame_desc
[i
].actual_length
;
128 int st
= urb
->iso_frame_desc
[i
].status
;
130 cdata
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
133 printk(KERN_DEBUG
"cpia data error: [%d] len=%d, status=%X\n", i
, n
, st
);
135 if (FRAMEBUF_LEN
< ucpia
->workbuff
->length
+ n
) {
136 printk(KERN_DEBUG
"cpia: scratch buf overflow!scr_len: %d, n: %d\n", ucpia
->workbuff
->length
, n
);
141 if ((ucpia
->workbuff
->length
> 0) ||
142 (0x19 == cdata
[0] && 0x68 == cdata
[1])) {
143 memcpy(ucpia
->workbuff
->data
+ ucpia
->workbuff
->length
, cdata
, n
);
144 ucpia
->workbuff
->length
+= n
;
146 DBG("Ignoring packet!\n");
148 if (ucpia
->workbuff
->length
> 4 &&
149 0xff == ucpia
->workbuff
->data
[ucpia
->workbuff
->length
-1] &&
150 0xff == ucpia
->workbuff
->data
[ucpia
->workbuff
->length
-2] &&
151 0xff == ucpia
->workbuff
->data
[ucpia
->workbuff
->length
-3] &&
152 0xff == ucpia
->workbuff
->data
[ucpia
->workbuff
->length
-4]) {
153 ucpia
->workbuff
->status
= FRAME_READY
;
154 ucpia
->curbuff
= ucpia
->workbuff
;
155 ucpia
->workbuff
= ucpia
->workbuff
->next
;
156 ucpia
->workbuff
->status
= FRAME_EMPTY
;
157 ucpia
->workbuff
->length
= 0;
159 if (waitqueue_active(&ucpia
->wq_stream
))
160 wake_up_interruptible(&ucpia
->wq_stream
);
166 static int cpia_usb_open(void *privdata
)
168 struct usb_cpia
*ucpia
= (struct usb_cpia
*) privdata
;
170 int ret
, retval
= 0, fx
, err
;
175 ucpia
->sbuf
[0].data
= kmalloc(FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
176 if (!ucpia
->sbuf
[0].data
)
179 ucpia
->sbuf
[1].data
= kmalloc(FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
180 if (!ucpia
->sbuf
[1].data
) {
185 ret
= usb_set_interface(ucpia
->dev
, ucpia
->iface
, 3);
187 printk(KERN_ERR
"cpia_usb_open: usb_set_interface error (ret = %d)\n", ret
);
192 ucpia
->buffers
[0]->status
= FRAME_EMPTY
;
193 ucpia
->buffers
[0]->length
= 0;
194 ucpia
->buffers
[1]->status
= FRAME_EMPTY
;
195 ucpia
->buffers
[1]->length
= 0;
196 ucpia
->buffers
[2]->status
= FRAME_EMPTY
;
197 ucpia
->buffers
[2]->length
= 0;
198 ucpia
->curbuff
= ucpia
->buffers
[0];
199 ucpia
->workbuff
= ucpia
->buffers
[1];
201 /* We double buffer the Iso lists */
202 urb
= usb_alloc_urb(FRAMES_PER_DESC
);
204 printk(KERN_ERR
"cpia_init_isoc: usb_alloc_urb 0\n");
209 ucpia
->sbuf
[0].urb
= urb
;
210 urb
->dev
= ucpia
->dev
;
211 urb
->context
= ucpia
;
212 urb
->pipe
= usb_rcvisocpipe(ucpia
->dev
, 1);
213 urb
->transfer_flags
= USB_ISO_ASAP
;
214 urb
->transfer_buffer
= ucpia
->sbuf
[0].data
;
215 urb
->complete
= cpia_usb_complete
;
216 urb
->number_of_packets
= FRAMES_PER_DESC
;
217 urb
->transfer_buffer_length
= FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
218 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
219 urb
->iso_frame_desc
[fx
].offset
= FRAME_SIZE_PER_DESC
* fx
;
220 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
223 urb
= usb_alloc_urb(FRAMES_PER_DESC
);
225 printk(KERN_ERR
"cpia_init_isoc: usb_alloc_urb 0\n");
230 ucpia
->sbuf
[1].urb
= urb
;
231 urb
->dev
= ucpia
->dev
;
232 urb
->context
= ucpia
;
233 urb
->pipe
= usb_rcvisocpipe(ucpia
->dev
, 1);
234 urb
->transfer_flags
= USB_ISO_ASAP
;
235 urb
->transfer_buffer
= ucpia
->sbuf
[1].data
;
236 urb
->complete
= cpia_usb_complete
;
237 urb
->number_of_packets
= FRAMES_PER_DESC
;
238 urb
->transfer_buffer_length
= FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
239 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
240 urb
->iso_frame_desc
[fx
].offset
= FRAME_SIZE_PER_DESC
* fx
;
241 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
244 ucpia
->sbuf
[1].urb
->next
= ucpia
->sbuf
[0].urb
;
245 ucpia
->sbuf
[0].urb
->next
= ucpia
->sbuf
[1].urb
;
247 err
= usb_submit_urb(ucpia
->sbuf
[0].urb
);
249 printk(KERN_ERR
"cpia_init_isoc: usb_submit_urb 0 ret %d\n",
251 err
= usb_submit_urb(ucpia
->sbuf
[1].urb
);
253 printk(KERN_ERR
"cpia_init_isoc: usb_submit_urb 1 ret %d\n",
256 ucpia
->streaming
= 1;
262 kfree (ucpia
->sbuf
[1].data
);
264 kfree (ucpia
->sbuf
[0].data
);
270 // convenience functions
273 /****************************************************************************
277 ***************************************************************************/
278 static int WritePacket(struct usb_device
*udev
, const u8
*packet
, u8
*buf
, size_t size
)
283 return usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
284 packet
[1] + (packet
[0] << 8),
285 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
286 packet
[2] + (packet
[3] << 8),
287 packet
[4] + (packet
[5] << 8), buf
, size
, HZ
);
290 /****************************************************************************
294 ***************************************************************************/
295 static int ReadPacket(struct usb_device
*udev
, u8
*packet
, u8
*buf
, size_t size
)
297 if (!packet
|| size
<= 0)
300 return usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
301 packet
[1] + (packet
[0] << 8),
302 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
303 packet
[2] + (packet
[3] << 8),
304 packet
[4] + (packet
[5] << 8), buf
, size
, HZ
);
307 static int cpia_usb_transferCmd(void *privdata
, u8
*command
, u8
*data
)
311 struct usb_cpia
*ucpia
= (struct usb_cpia
*)privdata
;
312 struct usb_device
*udev
= ucpia
->dev
;
315 DBG("Internal driver error: udev is NULL\n");
320 DBG("Internal driver error: command is NULL\n");
324 databytes
= (((int)command
[7])<<8) | command
[6];
326 if (command
[0] == DATA_IN
) {
330 DBG("Internal driver error: data is NULL\n");
334 err
= ReadPacket(udev
, command
, buffer
, 8);
338 memcpy(data
, buffer
, databytes
);
339 } else if(command
[0] == DATA_OUT
)
340 WritePacket(udev
, command
, data
, databytes
);
342 DBG("Unexpected first byte of command: %x\n", command
[0]);
349 static int cpia_usb_registerCallback(void *privdata
, void (*cb
) (void *cbdata
),
355 static int cpia_usb_streamStart(void *privdata
)
360 static int cpia_usb_streamStop(void *privdata
)
365 static int cpia_usb_streamRead(void *privdata
, u8
*frame
, int noblock
)
367 struct usb_cpia
*ucpia
= (struct usb_cpia
*) privdata
;
368 struct framebuf
*mybuff
;
370 if (!ucpia
|| !ucpia
->present
)
373 if (ucpia
->curbuff
->status
!= FRAME_READY
)
374 interruptible_sleep_on(&ucpia
->wq_stream
);
376 DBG("Frame already waiting!\n");
378 mybuff
= ucpia
->curbuff
;
383 if (mybuff
->status
!= FRAME_READY
|| mybuff
->length
< 4) {
384 DBG("Something went wrong!\n");
388 memcpy(frame
, mybuff
->data
, mybuff
->length
);
389 mybuff
->status
= FRAME_EMPTY
;
391 /* DBG("read done, %d bytes, Header: %x/%x, Footer: %x%x%x%x\n", */
392 /* mybuff->length, frame[0], frame[1], */
393 /* frame[mybuff->length-4], frame[mybuff->length-3], */
394 /* frame[mybuff->length-2], frame[mybuff->length-1]); */
396 return mybuff
->length
;
399 static void cpia_usb_free_resources(struct usb_cpia
*ucpia
, int try)
401 if (!ucpia
->streaming
)
404 ucpia
->streaming
= 0;
406 /* Set packet size to 0 */
410 ret
= usb_set_interface(ucpia
->dev
, ucpia
->iface
, 0);
412 printk(KERN_ERR
"usb_set_interface error (ret = %d)\n", ret
);
417 /* Unschedule all of the iso td's */
418 if (ucpia
->sbuf
[1].urb
) {
419 usb_unlink_urb(ucpia
->sbuf
[1].urb
);
420 usb_free_urb(ucpia
->sbuf
[1].urb
);
421 ucpia
->sbuf
[1].urb
= NULL
;
424 if (ucpia
->sbuf
[0].urb
) {
425 usb_unlink_urb(ucpia
->sbuf
[0].urb
);
426 usb_free_urb(ucpia
->sbuf
[0].urb
);
427 ucpia
->sbuf
[0].urb
= NULL
;
431 static int cpia_usb_close(void *privdata
)
433 struct usb_cpia
*ucpia
= (struct usb_cpia
*) privdata
;
437 cpia_usb_free_resources(ucpia
, 1);
445 int cpia_usb_init(void)
447 /* return -ENODEV; */
451 /* Probing and initializing */
453 static void *cpia_probe(struct usb_device
*udev
, unsigned int ifnum
)
455 struct usb_interface_descriptor
*interface
;
456 struct usb_cpia
*ucpia
;
457 struct cam_data
*cam
;
460 /* A multi-config CPiA camera? */
461 if (udev
->descriptor
.bNumConfigurations
!= 1)
464 interface
= &udev
->actconfig
->interface
[ifnum
].altsetting
[0];
467 if (udev
->descriptor
.idVendor
!= 0x0553)
469 if (udev
->descriptor
.idProduct
!= 0x0002)
472 /* We found a CPiA */
473 printk(KERN_INFO
"USB CPiA camera found\n");
475 ucpia
= kmalloc(sizeof(*ucpia
), GFP_KERNEL
);
477 printk(KERN_ERR
"couldn't kmalloc cpia struct\n");
481 memset(ucpia
, 0, sizeof(*ucpia
));
484 ucpia
->iface
= interface
->bInterfaceNumber
;
485 init_waitqueue_head(&ucpia
->wq_stream
);
487 ucpia
->buffers
[0] = vmalloc(sizeof(*ucpia
->buffers
[0]));
488 if (!ucpia
->buffers
[0]) {
489 printk(KERN_ERR
"couldn't vmalloc frame buffer 0\n");
493 ucpia
->buffers
[1] = vmalloc(sizeof(*ucpia
->buffers
[1]));
494 if (!ucpia
->buffers
[1]) {
495 printk(KERN_ERR
"couldn't vmalloc frame buffer 1\n");
499 ucpia
->buffers
[2] = vmalloc(sizeof(*ucpia
->buffers
[2]));
500 if (!ucpia
->buffers
[2]) {
501 printk(KERN_ERR
"couldn't vmalloc frame buffer 2\n");
505 ucpia
->buffers
[0]->next
= ucpia
->buffers
[1];
506 ucpia
->buffers
[1]->next
= ucpia
->buffers
[2];
507 ucpia
->buffers
[2]->next
= ucpia
->buffers
[0];
509 ret
= usb_set_interface(udev
, ucpia
->iface
, 0);
511 printk(KERN_ERR
"cpia_probe: usb_set_interface error (ret = %d)\n", ret
);
515 /* Before register_camera, important */
518 cam
= cpia_register_camera(&cpia_usb_ops
, ucpia
);
520 LOG("failed to cpia_register_camera\n");
524 ADD_TO_LIST(cam_list
, cam
);
529 vfree(ucpia
->buffers
[2]);
530 ucpia
->buffers
[2] = NULL
;
532 vfree(ucpia
->buffers
[1]);
533 ucpia
->buffers
[1] = NULL
;
535 vfree(ucpia
->buffers
[0]);
536 ucpia
->buffers
[0] = NULL
;
542 static void cpia_disconnect(struct usb_device
*dev
, void *ptr
);
544 static struct usb_driver cpia_driver
= {
551 /* don't use dev, it may be NULL! (see usb_cpia_cleanup) */
552 /* _disconnect from usb_cpia_cleanup is not necessary since usb_deregister */
553 /* will do it for us as well as passing a udev structure - jerdfelt */
554 static void cpia_disconnect(struct usb_device
*udev
, void *ptr
)
556 struct cam_data
*cam
= (struct cam_data
*) ptr
;
557 struct usb_cpia
*ucpia
= (struct usb_cpia
*) cam
->lowlevel_data
;
559 REMOVE_FROM_LIST(cam
);
561 /* Don't even try to reset the altsetting if we're disconnected */
562 cpia_usb_free_resources(ucpia
, 0);
566 cpia_unregister_camera(cam
);
568 ucpia
->curbuff
->status
= FRAME_ERROR
;
570 if (waitqueue_active(&ucpia
->wq_stream
))
571 wake_up_interruptible(&ucpia
->wq_stream
);
573 usb_driver_release_interface(&cpia_driver
,
574 &udev
->actconfig
->interface
[0]);
576 ucpia
->curbuff
= ucpia
->workbuff
= NULL
;
578 if (ucpia
->buffers
[2]) {
579 vfree(ucpia
->buffers
[2]);
580 ucpia
->buffers
[2] = NULL
;
583 if (ucpia
->buffers
[1]) {
584 vfree(ucpia
->buffers
[1]);
585 ucpia
->buffers
[1] = NULL
;
588 if (ucpia
->buffers
[0]) {
589 vfree(ucpia
->buffers
[0]);
590 ucpia
->buffers
[0] = NULL
;
597 int usb_cpia_init(void)
601 return usb_register(&cpia_driver
);
604 void usb_cpia_cleanup(void)
607 struct cam_data *cam;
609 while ((cam = cam_list) != NULL)
610 cpia_disconnect(NULL, cam);
613 usb_deregister(&cpia_driver
);
617 int init_module(void)
619 return usb_cpia_init();
622 void cleanup_module(void)