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>
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)
52 #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
53 enum framebuf_status
{
62 enum framebuf_status status
;
63 u8 data
[FRAMEBUF_LEN
];
64 struct framebuf
*next
;
68 /* Device structure */
69 struct usb_device
*dev
;
72 wait_queue_head_t wq_stream
;
74 int cursbuf
; /* Current receiving sbuf */
75 struct cpia_sbuf sbuf
[CPIA_NUMSBUF
]; /* Double buffering */
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
),
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
= {
97 cpia_usb_registerCallback
,
106 static struct cam_data
*cam_list
;
108 static void cpia_usb_complete(struct urb
*urb
)
112 struct usb_cpia
*ucpia
;
114 if (!urb
|| !urb
->context
)
117 ucpia
= (struct usb_cpia
*) urb
->context
;
119 if (!ucpia
->dev
|| !ucpia
->streaming
|| !ucpia
->present
|| !ucpia
->open
)
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
;
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
);
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
;
147 DBG("Ignoring packet!\n");
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
;
171 int ret
, retval
= 0, fx
, err
;
176 ucpia
->sbuf
[0].data
= kmalloc(FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
177 if (!ucpia
->sbuf
[0].data
)
180 ucpia
->sbuf
[1].data
= kmalloc(FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
181 if (!ucpia
->sbuf
[1].data
) {
186 ret
= usb_set_interface(ucpia
->dev
, ucpia
->iface
, 3);
188 printk(KERN_ERR
"cpia_usb_open: usb_set_interface error (ret = %d)\n", ret
);
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
);
205 printk(KERN_ERR
"cpia_init_isoc: usb_alloc_urb 0\n");
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
);
226 printk(KERN_ERR
"cpia_init_isoc: usb_alloc_urb 1\n");
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
);
250 printk(KERN_ERR
"cpia_init_isoc: usb_submit_urb 0 ret %d\n",
254 err
= usb_submit_urb(ucpia
->sbuf
[1].urb
);
256 printk(KERN_ERR
"cpia_init_isoc: usb_submit_urb 1 ret %d\n",
261 ucpia
->streaming
= 1;
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
);
273 kfree (ucpia
->sbuf
[1].data
);
275 kfree (ucpia
->sbuf
[0].data
);
281 // convenience functions
284 /****************************************************************************
288 ***************************************************************************/
289 static int WritePacket(struct usb_device
*udev
, const u8
*packet
, u8
*buf
, size_t size
)
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 /****************************************************************************
305 ***************************************************************************/
306 static int ReadPacket(struct usb_device
*udev
, u8
*packet
, u8
*buf
, size_t size
)
308 if (!packet
|| size
<= 0)
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
)
322 struct usb_cpia
*ucpia
= (struct usb_cpia
*)privdata
;
323 struct usb_device
*udev
= ucpia
->dev
;
326 DBG("Internal driver error: udev is NULL\n");
331 DBG("Internal driver error: command is NULL\n");
335 databytes
= (((int)command
[7])<<8) | command
[6];
337 if (command
[0] == DATA_IN
) {
341 DBG("Internal driver error: data is NULL\n");
345 err
= ReadPacket(udev
, command
, buffer
, 8);
349 memcpy(data
, buffer
, databytes
);
350 } else if(command
[0] == DATA_OUT
)
351 WritePacket(udev
, command
, data
, databytes
);
353 DBG("Unexpected first byte of command: %x\n", command
[0]);
360 static int cpia_usb_registerCallback(void *privdata
, void (*cb
) (void *cbdata
),
366 static int cpia_usb_streamStart(void *privdata
)
371 static int cpia_usb_streamStop(void *privdata
)
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
)
384 if (ucpia
->curbuff
->status
!= FRAME_READY
)
385 interruptible_sleep_on(&ucpia
->wq_stream
);
387 DBG("Frame already waiting!\n");
389 mybuff
= ucpia
->curbuff
;
394 if (mybuff
->status
!= FRAME_READY
|| mybuff
->length
< 4) {
395 DBG("Something went wrong!\n");
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
)
415 ucpia
->streaming
= 0;
417 /* Set packet size to 0 */
421 ret
= usb_set_interface(ucpia
->dev
, ucpia
->iface
, 0);
423 printk(KERN_ERR
"usb_set_interface error (ret = %d)\n", ret
);
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
;
448 cpia_usb_free_resources(ucpia
, 1);
456 int cpia_usb_init(void)
458 /* return -ENODEV; */
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
;
471 /* A multi-config CPiA camera? */
472 if (udev
->descriptor
.bNumConfigurations
!= 1)
475 interface
= &udev
->actconfig
->interface
[ifnum
].altsetting
[0];
478 if (udev
->descriptor
.idVendor
!= 0x0553)
480 if (udev
->descriptor
.idProduct
!= 0x0002)
483 /* We found a CPiA */
484 printk(KERN_INFO
"USB CPiA camera found\n");
486 ucpia
= kmalloc(sizeof(*ucpia
), GFP_KERNEL
);
488 printk(KERN_ERR
"couldn't kmalloc cpia struct\n");
492 memset(ucpia
, 0, sizeof(*ucpia
));
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");
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");
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");
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);
522 printk(KERN_ERR
"cpia_probe: usb_set_interface error (ret = %d)\n", ret
);
526 /* Before register_camera, important */
529 cam
= cpia_register_camera(&cpia_usb_ops
, ucpia
);
531 LOG("failed to cpia_register_camera\n");
535 ADD_TO_LIST(cam_list
, cam
);
540 vfree(ucpia
->buffers
[2]);
541 ucpia
->buffers
[2] = NULL
;
543 vfree(ucpia
->buffers
[1]);
544 ucpia
->buffers
[1] = NULL
;
546 vfree(ucpia
->buffers
[0]);
547 ucpia
->buffers
[0] = NULL
;
553 static void cpia_disconnect(struct usb_device
*dev
, void *ptr
);
555 static struct usb_driver cpia_driver
= {
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);
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
;
608 static int __init
usb_cpia_init(void)
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
);