1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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>
35
36#include "cpia.h"
37
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
42
43#define FRAMES_PER_DESC 10
44#define FRAME_SIZE_PER_DESC 960
45#define CPIA_NUMSBUF 2
46#define STREAM_BUF_SIZE (PAGE_SIZE * 4)
47#define SCRATCH_BUF_SIZE (STREAM_BUF_SIZE * 2)
48
49struct cpia_sbuf {
50 char *data;
51 struct urb *urb;
52};
53
54#define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
55enum framebuf_status {
56 FRAME_EMPTY,
57 FRAME_READING,
58 FRAME_READY,
59 FRAME_ERROR,
60};
61
62struct framebuf {
63 int length;
64 enum framebuf_status status;
65 u8 data[FRAMEBUF_LEN];
66 struct framebuf *next;
67};
68
69struct usb_cpia {
70
71 struct usb_device *dev;
72
73 unsigned char iface;
74 wait_queue_head_t wq_stream;
75
76 int cursbuf;
77 struct cpia_sbuf sbuf[CPIA_NUMSBUF];
78
79 int streaming;
80 int open;
81 int present;
82 struct framebuf *buffers[3];
83 struct framebuf *curbuff, *workbuff;
84};
85
86static int cpia_usb_open(void *privdata);
87static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
88 void *cbdata);
89static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);
90static int cpia_usb_streamStart(void *privdata);
91static int cpia_usb_streamStop(void *privdata);
92static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);
93static int cpia_usb_close(void *privdata);
94
95#define ABOUT "USB driver for Vision CPiA based cameras"
96
97static struct cpia_camera_ops cpia_usb_ops = {
98 cpia_usb_open,
99 cpia_usb_registerCallback,
100 cpia_usb_transferCmd,
101 cpia_usb_streamStart,
102 cpia_usb_streamStop,
103 cpia_usb_streamRead,
104 cpia_usb_close,
105 0,
106 THIS_MODULE
107};
108
109static LIST_HEAD(cam_list);
110static spinlock_t cam_list_lock_usb;
111
112static void cpia_usb_complete(struct urb *urb)
113{
114 int i;
115 char *cdata;
116 struct usb_cpia *ucpia;
117
118 if (!urb || !urb->context)
119 return;
120
121 ucpia = (struct usb_cpia *) urb->context;
122
123 if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
124 return;
125
126 if (ucpia->workbuff->status == FRAME_EMPTY) {
127 ucpia->workbuff->status = FRAME_READING;
128 ucpia->workbuff->length = 0;
129 }
130
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;
134
135 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
136
137 if (st)
138 printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);
139
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);
142 return;
143 }
144
145 if (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;
150 } else
151 DBG("Ignoring packet!\n");
152 } else {
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;
163
164 if (waitqueue_active(&ucpia->wq_stream))
165 wake_up_interruptible(&ucpia->wq_stream);
166 }
167 }
168 }
169}
170
171static int cpia_usb_open(void *privdata)
172{
173 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
174 struct urb *urb;
175 int ret, retval = 0, fx, err;
176
177 if (!ucpia)
178 return -EINVAL;
179
180 ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
181 if (!ucpia->sbuf[0].data)
182 return -EINVAL;
183
184 ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
185 if (!ucpia->sbuf[1].data) {
186 retval = -EINVAL;
187 goto error_0;
188 }
189
190 ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
191 if (ret < 0) {
192 printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);
193 retval = -EBUSY;
194 goto error_1;
195 }
196
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];
205
206
207 urb = usb_alloc_urb(FRAMES_PER_DESC);
208 if (!urb) {
209 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
210 retval = -ENOMEM;
211 goto error_1;
212 }
213
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;
226 }
227
228 urb = usb_alloc_urb(FRAMES_PER_DESC);
229 if (!urb) {
230 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1\n");
231 retval = -ENOMEM;
232 goto error_urb0;
233 }
234
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;
247 }
248
249 ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;
250 ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;
251
252 err = usb_submit_urb(ucpia->sbuf[0].urb);
253 if (err) {
254 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",
255 err);
256 goto error_urb1;
257 }
258 err = usb_submit_urb(ucpia->sbuf[1].urb);
259 if (err) {
260 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",
261 err);
262 goto error_urb1;
263 }
264
265 ucpia->streaming = 1;
266 ucpia->open = 1;
267
268 return 0;
269
270error_urb1:
271 usb_free_urb(ucpia->sbuf[1].urb);
272 ucpia->sbuf[1].urb = NULL;
273error_urb0:
274 usb_free_urb(ucpia->sbuf[0].urb);
275 ucpia->sbuf[0].urb = NULL;
276error_1:
277 kfree (ucpia->sbuf[1].data);
278 ucpia->sbuf[1].data = NULL;
279error_0:
280 kfree (ucpia->sbuf[0].data);
281 ucpia->sbuf[0].data = NULL;
282
283 return retval;
284}
285
286
287
288
289
290
291
292
293
294
295static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
296{
297 if (!packet)
298 return -EINVAL;
299
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);
305}
306
307
308
309
310
311
312static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
313{
314 if (!packet || size <= 0)
315 return -EINVAL;
316
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);
322}
323
324static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
325{
326 int err = 0;
327 int databytes;
328 struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
329 struct usb_device *udev = ucpia->dev;
330
331 if (!udev) {
332 DBG("Internal driver error: udev is NULL\n");
333 return -EINVAL;
334 }
335
336 if (!command) {
337 DBG("Internal driver error: command is NULL\n");
338 return -EINVAL;
339 }
340
341 databytes = (((int)command[7])<<8) | command[6];
342
343 if (command[0] == DATA_IN) {
344 u8 buffer[8];
345
346 if (!data) {
347 DBG("Internal driver error: data is NULL\n");
348 return -EINVAL;
349 }
350
351 err = ReadPacket(udev, command, buffer, 8);
352 if (err < 0)
353 return err;
354
355 memcpy(data, buffer, databytes);
356 } else if(command[0] == DATA_OUT)
357 WritePacket(udev, command, data, databytes);
358 else {
359 DBG("Unexpected first byte of command: %x\n", command[0]);
360 err = -EINVAL;
361 }
362
363 return 0;
364}
365
366static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
367 void *cbdata)
368{
369 return -ENODEV;
370}
371
372static int cpia_usb_streamStart(void *privdata)
373{
374 return -ENODEV;
375}
376
377static int cpia_usb_streamStop(void *privdata)
378{
379 return -ENODEV;
380}
381
382static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
383{
384 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
385 struct framebuf *mybuff;
386
387 if (!ucpia || !ucpia->present)
388 return -1;
389
390 if (ucpia->curbuff->status != FRAME_READY)
391 interruptible_sleep_on(&ucpia->wq_stream);
392 else
393 DBG("Frame already waiting!\n");
394
395 mybuff = ucpia->curbuff;
396
397 if (!mybuff)
398 return -1;
399
400 if (mybuff->status != FRAME_READY || mybuff->length < 4) {
401 DBG("Something went wrong!\n");
402 return -1;
403 }
404
405 memcpy(frame, mybuff->data, mybuff->length);
406 mybuff->status = FRAME_EMPTY;
407
408
409
410
411
412
413 return mybuff->length;
414}
415
416static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
417{
418 if (!ucpia->streaming)
419 return;
420
421 ucpia->streaming = 0;
422
423
424 if (try) {
425 int ret;
426
427 ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
428 if (ret < 0) {
429 printk(KERN_ERR "usb_set_interface error (ret = %d)\n", ret);
430 return;
431 }
432 }
433
434
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;
439 }
440
441 if (ucpia->sbuf[1].data) {
442 kfree(ucpia->sbuf[1].data);
443 ucpia->sbuf[1].data = NULL;
444 }
445
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;
450 }
451
452 if (ucpia->sbuf[0].data) {
453 kfree(ucpia->sbuf[0].data);
454 ucpia->sbuf[0].data = NULL;
455 }
456}
457
458static int cpia_usb_close(void *privdata)
459{
460 struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
461
462 if(!ucpia)
463 return -ENODEV;
464
465 ucpia->open = 0;
466
467
468
469 cpia_usb_free_resources(ucpia, ucpia->present);
470
471 return 0;
472}
473
474int cpia_usb_init(void)
475{
476
477 return 0;
478}
479
480
481
482static void *cpia_probe(struct usb_device *udev, unsigned int ifnum,
483 const struct usb_device_id *id)
484{
485 struct usb_interface_descriptor *interface;
486 struct usb_cpia *ucpia;
487 struct cam_data *cam;
488 int ret;
489
490
491 if (udev->descriptor.bNumConfigurations != 1)
492 return NULL;
493
494 interface = &udev->actconfig->interface[ifnum].altsetting[0];
495
496 printk(KERN_INFO "USB CPiA camera found\n");
497
498 ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
499 if (!ucpia) {
500 printk(KERN_ERR "couldn't kmalloc cpia struct\n");
501 return NULL;
502 }
503
504 memset(ucpia, 0, sizeof(*ucpia));
505
506 ucpia->dev = udev;
507 ucpia->iface = interface->bInterfaceNumber;
508 init_waitqueue_head(&ucpia->wq_stream);
509
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");
513 goto fail_alloc_0;
514 }
515
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");
519 goto fail_alloc_1;
520 }
521
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");
525 goto fail_alloc_2;
526 }
527
528 ucpia->buffers[0]->next = ucpia->buffers[1];
529 ucpia->buffers[1]->next = ucpia->buffers[2];
530 ucpia->buffers[2]->next = ucpia->buffers[0];
531
532 ret = usb_set_interface(udev, ucpia->iface, 0);
533 if (ret < 0) {
534 printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
535
536 }
537
538
539 ucpia->present = 1;
540
541 cam = cpia_register_camera(&cpia_usb_ops, ucpia);
542 if (!cam) {
543 LOG("failed to cpia_register_camera\n");
544 goto fail_all;
545 }
546
547 spin_lock( &cam_list_lock_usb );
548 list_add( &cam->cam_data_list, &cam_list );
549 spin_unlock( &cam_list_lock_usb );
550
551 return cam;
552
553fail_all:
554 vfree(ucpia->buffers[2]);
555 ucpia->buffers[2] = NULL;
556fail_alloc_2:
557 vfree(ucpia->buffers[1]);
558 ucpia->buffers[1] = NULL;
559fail_alloc_1:
560 vfree(ucpia->buffers[0]);
561 ucpia->buffers[0] = NULL;
562fail_alloc_0:
563 kfree(ucpia);
564
565 return NULL;
566}
567
568static void cpia_disconnect(struct usb_device *dev, void *ptr);
569
570static struct usb_device_id cpia_id_table [] = {
571 { USB_DEVICE(0x0553, 0x0002) },
572 { USB_DEVICE(0x0813, 0x0001) },
573 { }
574};
575
576MODULE_DEVICE_TABLE (usb, cpia_id_table);
577MODULE_LICENSE("GPL");
578
579
580static struct usb_driver cpia_driver = {
581 name: "cpia",
582 probe: cpia_probe,
583 disconnect: cpia_disconnect,
584 id_table: cpia_id_table,
585};
586
587static void cpia_disconnect(struct usb_device *udev, void *ptr)
588{
589 struct cam_data *cam = (struct cam_data *) ptr;
590 struct usb_cpia *ucpia = (struct usb_cpia *) cam->lowlevel_data;
591
592 spin_lock( &cam_list_lock_usb );
593 list_del(&cam->cam_data_list);
594 spin_unlock( &cam_list_lock_usb );
595
596 ucpia->present = 0;
597 cpia_unregister_camera(cam);
598 if(ucpia->open)
599 cpia_usb_close(cam->lowlevel_data);
600
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;
610 }
611 if (ucpia->buffers[1]) {
612 vfree(ucpia->buffers[1]);
613 ucpia->buffers[1] = NULL;
614 }
615 if (ucpia->buffers[0]) {
616 vfree(ucpia->buffers[0]);
617 ucpia->buffers[0] = NULL;
618 }
619
620 cam->lowlevel_data = NULL;
621 kfree(ucpia);
622}
623
624static int __init usb_cpia_init(void)
625{
626 printk(KERN_INFO "%s v%d.%d.%d\n",ABOUT,
627 CPIA_USB_MAJ_VER,CPIA_USB_MIN_VER,CPIA_USB_PATCH_VER);
628
629 spin_lock_init(&cam_list_lock_usb);
630 return usb_register(&cpia_driver);
631}
632
633static void __exit usb_cpia_cleanup(void)
634{
635 usb_deregister(&cpia_driver);
636}
637
638module_init (usb_cpia_init);
639module_exit (usb_cpia_cleanup);
640
641