1 /*
2 * cpia_usb CPiA USB driver
3 *
4 * Supports CPiA based parallel port Video Camera's.
5 *
6 * Copyright (C) 1999 Jochen Scharrlach <Jochen.Scharrlach@schwaben.de>
7 * Copyright (C) 1999, 2000 Johannes Erdfelt <jerdfelt@valinux.com>
8 *
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.
13 *
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.
18 *
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.
22 */
23
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>
33
34 #include "cpia.h"
35
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
40
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)
46
47 struct cpia_sbuf {
48 char *data;
49 urb_t *urb;
50 };
51
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,
58 };
59
60 struct framebuf {
61 int length;
62 enum framebuf_status status;
63 u8 data[FRAMEBUF_LEN];
64 struct framebuf *next;
65 };
66
67 struct usb_cpia {
68 /* Device structure */
69 struct usb_device *dev;
70
71 unsigned char iface;
72 wait_queue_head_t wq_stream;
73
74 int cursbuf; /* Current receiving sbuf */
75 struct cpia_sbuf sbuf[CPIA_NUMSBUF]; /* Double buffering */
76
77 int streaming;
78 int open;
79 int present;
80 struct framebuf *buffers[3];
81 struct framebuf *curbuff, *workbuff;
82 };
83
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);
92
93 #define ABOUT "USB driver for Vision CPiA based cameras"
94
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,
103 0
104 };
105
106 static struct cam_data *cam_list;
107
108 static void cpia_usb_complete(struct urb *urb)
109 {
110 int i;
111 char *cdata;
112 struct usb_cpia *ucpia;
113
114 if (!urb || !urb->context)
115 return;
116
117 ucpia = (struct usb_cpia *) urb->context;
118
119 if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
120 return;
121
122 if (ucpia->workbuff->status == FRAME_EMPTY) {
123 ucpia->workbuff->status = FRAME_READING;
124 ucpia->workbuff->length = 0;
125 }
126
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;
130
131 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
132
133 if (st)
134 printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);
135
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;
139 }
140
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;
159
160 if (waitqueue_active(&ucpia->wq_stream))
161 wake_up_interruptible(&ucpia->wq_stream);
162 }
163 }
164 }
165 }
166
167 static int cpia_usb_open(void *privdata)
168 {
169 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
170 urb_t *urb;
171 int ret, retval = 0, fx, err;
172
173 if (!ucpia)
174 return -EINVAL;
175
176 ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
177 if (!ucpia->sbuf[0].data)
178 return -EINVAL;
179
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;
184 }
185
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;
191 }
192
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];
201
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;
208 }
209
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;
222 }
223
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;
229 }
230
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;
243 }
244
245 ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;
246 ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;
247
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;
253 }
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;
259 }
260
261 ucpia->streaming = 1;
262 ucpia->open = 1;
263
264 return 0;
265
266 error_urb1: /* free urb 1 */
267 usb_free_urb(ucpia->sbuf[1].urb);
268
269 error_urb0: /* free urb 0 */
270 usb_free_urb(ucpia->sbuf[0].urb);
271
272 error_1:
273 kfree (ucpia->sbuf[1].data);
274 error_0:
275 kfree (ucpia->sbuf[0].data);
276
277 return retval;
278 }
279
280 //
281 // convenience functions
282 //
283
284 /****************************************************************************
285 *
286 * WritePacket
287 *
288 ***************************************************************************/
289 static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
290 {
291 if (!packet)
292 return -EINVAL;
293
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);
299 }
300
301 /****************************************************************************
302 *
303 * ReadPacket
304 *
305 ***************************************************************************/
306 static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
307 {
308 if (!packet || size <= 0)
309 return -EINVAL;
310
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);
316 }
317
318 static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
319 {
320 int err = 0;
321 int databytes;
322 struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
323 struct usb_device *udev = ucpia->dev;
324
325 if (!udev) {
326 DBG("Internal driver error: udev is NULL\n");
327 return -EINVAL;
328 }
329
330 if (!command) {
331 DBG("Internal driver error: command is NULL\n");
332 return -EINVAL;
333 }
334
335 databytes = (((int)command[7])<<8) | command[6];
336
337 if (command[0] == DATA_IN) {
338 u8 buffer[8];
339
340 if (!data) {
341 DBG("Internal driver error: data is NULL\n");
342 return -EINVAL;
343 }
344
345 err = ReadPacket(udev, command, buffer, 8);
346 if (err < 0)
347 return err;
348
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;
355 }
356
357 return 0;
358 }
359
360 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
361 void *cbdata)
362 {
363 return -ENODEV;
364 }
365
366 static int cpia_usb_streamStart(void *privdata)
367 {
368 return -ENODEV;
369 }
370
371 static int cpia_usb_streamStop(void *privdata)
372 {
373 return -ENODEV;
374 }
375
376 static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
377 {
378 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
379 struct framebuf *mybuff;
380
381 if (!ucpia || !ucpia->present)
382 return -1;
383
384 if (ucpia->curbuff->status != FRAME_READY)
385 interruptible_sleep_on(&ucpia->wq_stream);
386 else
387 DBG("Frame already waiting!\n");
388
389 mybuff = ucpia->curbuff;
390
391 if (!mybuff)
392 return -1;
393
394 if (mybuff->status != FRAME_READY || mybuff->length < 4) {
395 DBG("Something went wrong!\n");
396 return -1;
397 }
398
399 memcpy(frame, mybuff->data, mybuff->length);
400 mybuff->status = FRAME_EMPTY;
401
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]); */
406
407 return mybuff->length;
408 }
409
410 static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
411 {
412 if (!ucpia->streaming)
413 return;
414
415 ucpia->streaming = 0;
416
417 /* Set packet size to 0 */
418 if (try) {
419 int ret;
420
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;
425 }
426 }
427
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;
433 }
434
435 if (ucpia->sbuf[1].data) {
436 kfree(ucpia->sbuf[1].data);
437 ucpia->sbuf[1].data = NULL;
438 }
439
440 if (ucpia->sbuf[0].urb) {
441 usb_unlink_urb(ucpia->sbuf[0].urb);
442 usb_free_urb(ucpia->sbuf[0].urb);
443 ucpia->sbuf[0].urb = NULL;
444 }
445
446 if (ucpia->sbuf[0].data) {
447 kfree(ucpia->sbuf[0].data);
448 ucpia->sbuf[0].data = NULL;
449 }
450 }
451
452 static int cpia_usb_close(void *privdata)
453 {
454 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
455
456 ucpia->open = 0;
457
458 cpia_usb_free_resources(ucpia, 1);
459
460 if (!ucpia->present)
461 kfree(ucpia);
462
463 return 0;
464 }
465
466 int cpia_usb_init(void)
467 {
468 /* return -ENODEV; */
469 return 0;
470 }
471
472 /* Probing and initializing */
473
474 static void *cpia_probe(struct usb_device *udev, unsigned int ifnum,
475 const struct usb_device_id *id)
476 {
477 struct usb_interface_descriptor *interface;
478 struct usb_cpia *ucpia;
479 struct cam_data *cam;
480 int ret;
481
482 /* A multi-config CPiA camera? */
483 if (udev->descriptor.bNumConfigurations != 1)
484 return NULL;
485
486 interface = &udev->actconfig->interface[ifnum].altsetting[0];
487
488 printk(KERN_INFO "USB CPiA camera found\n");
489
490 ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
491 if (!ucpia) {
492 printk(KERN_ERR "couldn't kmalloc cpia struct\n");
493 return NULL;
494 }
495
496 memset(ucpia, 0, sizeof(*ucpia));
497
498 ucpia->dev = udev;
499 ucpia->iface = interface->bInterfaceNumber;
500 init_waitqueue_head(&ucpia->wq_stream);
501
502 ucpia->buffers[0] = vmalloc(sizeof(*ucpia->buffers[0]));
503 if (!ucpia->buffers[0]) {
504 printk(KERN_ERR "couldn't vmalloc frame buffer 0\n");
505 goto fail_alloc_0;
506 }
507
508 ucpia->buffers[1] = vmalloc(sizeof(*ucpia->buffers[1]));
509 if (!ucpia->buffers[1]) {
510 printk(KERN_ERR "couldn't vmalloc frame buffer 1\n");
511 goto fail_alloc_1;
512 }
513
514 ucpia->buffers[2] = vmalloc(sizeof(*ucpia->buffers[2]));
515 if (!ucpia->buffers[2]) {
516 printk(KERN_ERR "couldn't vmalloc frame buffer 2\n");
517 goto fail_alloc_2;
518 }
519
520 ucpia->buffers[0]->next = ucpia->buffers[1];
521 ucpia->buffers[1]->next = ucpia->buffers[2];
522 ucpia->buffers[2]->next = ucpia->buffers[0];
523
524 ret = usb_set_interface(udev, ucpia->iface, 0);
525 if (ret < 0) {
526 printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
527 /* goto fail_all; */
528 }
529
530 /* Before register_camera, important */
531 ucpia->present = 1;
532
533 cam = cpia_register_camera(&cpia_usb_ops, ucpia);
534 if (!cam) {
535 LOG("failed to cpia_register_camera\n");
536 goto fail_all;
537 }
538
539 ADD_TO_LIST(cam_list, cam);
540
541 return cam;
542
543 fail_all:
544 vfree(ucpia->buffers[2]);
545 ucpia->buffers[2] = NULL;
546 fail_alloc_2:
547 vfree(ucpia->buffers[1]);
548 ucpia->buffers[1] = NULL;
549 fail_alloc_1:
550 vfree(ucpia->buffers[0]);
551 ucpia->buffers[0] = NULL;
552 fail_alloc_0:
553
554 return NULL;
555 }
556
557 static void cpia_disconnect(struct usb_device *dev, void *ptr);
558
559 static struct usb_device_id cpia_id_table [] = {
560 { USB_DEVICE(0x0553, 0x0002) },
561 { } /* Terminating entry */
562 };
563
564 MODULE_DEVICE_TABLE (usb, cpia_id_table);
565
566 static struct usb_driver cpia_driver = {
567 name: "cpia",
568 probe: cpia_probe,
569 disconnect: cpia_disconnect,
570 id_table: cpia_id_table,
571 };
572
573 /* don't use dev, it may be NULL! (see usb_cpia_cleanup) */
574 /* _disconnect from usb_cpia_cleanup is not necessary since usb_deregister */
575 /* will do it for us as well as passing a udev structure - jerdfelt */
576 static void cpia_disconnect(struct usb_device *udev, void *ptr)
577 {
578 struct cam_data *cam = (struct cam_data *) ptr;
579 struct usb_cpia *ucpia = (struct usb_cpia *) cam->lowlevel_data;
580
581 REMOVE_FROM_LIST(cam);
582
583 /* Don't even try to reset the altsetting if we're disconnected */
584 cpia_usb_free_resources(ucpia, 0);
585
586 ucpia->present = 0;
587
588 cpia_unregister_camera(cam);
589
590 ucpia->curbuff->status = FRAME_ERROR;
591
592 if (waitqueue_active(&ucpia->wq_stream))
593 wake_up_interruptible(&ucpia->wq_stream);
594
595 usb_driver_release_interface(&cpia_driver,
596 &udev->actconfig->interface[0]);
597
598 ucpia->curbuff = ucpia->workbuff = NULL;
599
600 if (ucpia->buffers[2]) {
601 vfree(ucpia->buffers[2]);
602 ucpia->buffers[2] = NULL;
603 }
604
605 if (ucpia->buffers[1]) {
606 vfree(ucpia->buffers[1]);
607 ucpia->buffers[1] = NULL;
608 }
609
610 if (ucpia->buffers[0]) {
611 vfree(ucpia->buffers[0]);
612 ucpia->buffers[0] = NULL;
613 }
614
615 if (!ucpia->open)
616 kfree(ucpia);
617 }
618
619 static int __init usb_cpia_init(void)
620 {
621 cam_list = NULL;
622
623 return usb_register(&cpia_driver);
624 }
625
626 static void __exit usb_cpia_cleanup(void)
627 {
628 /*
629 struct cam_data *cam;
630
631 while ((cam = cam_list) != NULL)
632 cpia_disconnect(NULL, cam);
633 */
634
635 usb_deregister(&cpia_driver);
636 }
637
638
639 module_init (usb_cpia_init);
640 module_exit (usb_cpia_cleanup);
641
642
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.