Kill st_fstype member.
[linux-2.6/linux-mips.git] / drivers / char / cpia_usb.c
blob6b67fbc81de1521ac53589f5d7e59cb826963029
1 /*
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>
33 #include "cpia.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)
46 struct cpia_sbuf {
47 char *data;
48 urb_t *urb;
51 #define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
52 enum framebuf_status {
53 FRAME_EMPTY,
54 FRAME_READING,
55 FRAME_READY,
56 FRAME_ERROR,
59 struct framebuf {
60 int length;
61 enum framebuf_status status;
62 u8 data[FRAMEBUF_LEN];
63 struct framebuf *next;
66 struct usb_cpia {
67 /* Device structure */
68 struct usb_device *dev;
70 unsigned char iface;
71 wait_queue_head_t wq_stream;
73 int cursbuf; /* Current receiving sbuf */
74 struct cpia_sbuf sbuf[CPIA_NUMSBUF]; /* Double buffering */
76 int streaming;
77 int open;
78 int present;
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),
85 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 = {
95 cpia_usb_open,
96 cpia_usb_registerCallback,
97 cpia_usb_transferCmd,
98 cpia_usb_streamStart,
99 cpia_usb_streamStop,
100 cpia_usb_streamRead,
101 cpia_usb_close,
105 static struct cam_data *cam_list;
107 static void cpia_usb_complete(struct urb *urb)
109 int i;
110 char *cdata;
111 struct usb_cpia *ucpia;
113 if (!urb || !urb->context)
114 return;
116 ucpia = (struct usb_cpia *) urb->context;
118 if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
119 return;
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;
132 if (st)
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);
137 return;
140 if (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;
145 } else
146 DBG("Ignoring packet!\n");
147 } else {
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;
169 urb_t *urb;
170 int ret, retval = 0, fx, err;
172 if (!ucpia)
173 return -EINVAL;
175 ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
176 if (!ucpia->sbuf[0].data)
177 return -EINVAL;
179 ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
180 if (!ucpia->sbuf[1].data) {
181 retval = -EINVAL;
182 goto error_0;
185 ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
186 if (ret < 0) {
187 printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);
188 retval = -EBUSY;
189 goto error_all;
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);
203 if (!urb) {
204 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
205 retval = -ENOMEM;
206 goto error_all;
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);
224 if (!urb) {
225 printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
226 retval = -ENOMEM;
227 goto error_all;
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);
248 if (err)
249 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",
250 err);
251 err = usb_submit_urb(ucpia->sbuf[1].urb);
252 if (err)
253 printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",
254 err);
256 ucpia->streaming = 1;
257 ucpia->open = 1;
259 return 0;
261 error_all:
262 kfree (ucpia->sbuf[1].data);
263 error_0:
264 kfree (ucpia->sbuf[0].data);
266 return retval;
270 // convenience functions
273 /****************************************************************************
275 * WritePacket
277 ***************************************************************************/
278 static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
280 if (!packet)
281 return -EINVAL;
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 /****************************************************************************
292 * ReadPacket
294 ***************************************************************************/
295 static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
297 if (!packet || size <= 0)
298 return -EINVAL;
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)
309 int err = 0;
310 int databytes;
311 struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
312 struct usb_device *udev = ucpia->dev;
314 if (!udev) {
315 DBG("Internal driver error: udev is NULL\n");
316 return -EINVAL;
319 if (!command) {
320 DBG("Internal driver error: command is NULL\n");
321 return -EINVAL;
324 databytes = (((int)command[7])<<8) | command[6];
326 if (command[0] == DATA_IN) {
327 u8 buffer[8];
329 if (!data) {
330 DBG("Internal driver error: data is NULL\n");
331 return -EINVAL;
334 err = ReadPacket(udev, command, buffer, 8);
335 if (err < 0)
336 return err;
338 memcpy(data, buffer, databytes);
339 } else if(command[0] == DATA_OUT)
340 WritePacket(udev, command, data, databytes);
341 else {
342 DBG("Unexpected first byte of command: %x\n", command[0]);
343 err = -EINVAL;
346 return 0;
349 static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
350 void *cbdata)
352 return -ENODEV;
355 static int cpia_usb_streamStart(void *privdata)
357 return -ENODEV;
360 static int cpia_usb_streamStop(void *privdata)
362 return -ENODEV;
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)
371 return -1;
373 if (ucpia->curbuff->status != FRAME_READY)
374 interruptible_sleep_on(&ucpia->wq_stream);
375 else
376 DBG("Frame already waiting!\n");
378 mybuff = ucpia->curbuff;
380 if (!mybuff)
381 return -1;
383 if (mybuff->status != FRAME_READY || mybuff->length < 4) {
384 DBG("Something went wrong!\n");
385 return -1;
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)
402 return;
404 ucpia->streaming = 0;
406 /* Set packet size to 0 */
407 if (try) {
408 int ret;
410 ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
411 if (ret < 0) {
412 printk(KERN_ERR "usb_set_interface error (ret = %d)\n", ret);
413 return;
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;
435 ucpia->open = 0;
437 cpia_usb_free_resources(ucpia, 1);
439 if (!ucpia->present)
440 kfree(ucpia);
442 return 0;
445 int cpia_usb_init(void)
447 /* return -ENODEV; */
448 return 0;
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;
458 int ret;
460 /* A multi-config CPiA camera? */
461 if (udev->descriptor.bNumConfigurations != 1)
462 return NULL;
464 interface = &udev->actconfig->interface[ifnum].altsetting[0];
466 /* Is it a CPiA? */
467 if (udev->descriptor.idVendor != 0x0553)
468 return NULL;
469 if (udev->descriptor.idProduct != 0x0002)
470 return NULL;
472 /* We found a CPiA */
473 printk(KERN_INFO "USB CPiA camera found\n");
475 ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
476 if (!ucpia) {
477 printk(KERN_ERR "couldn't kmalloc cpia struct\n");
478 return NULL;
481 memset(ucpia, 0, sizeof(*ucpia));
483 ucpia->dev = udev;
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");
490 goto fail_alloc_0;
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");
496 goto fail_alloc_1;
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");
502 goto fail_alloc_2;
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);
510 if (ret < 0) {
511 printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
512 /* goto fail_all; */
515 /* Before register_camera, important */
516 ucpia->present = 1;
518 cam = cpia_register_camera(&cpia_usb_ops, ucpia);
519 if (!cam) {
520 LOG("failed to cpia_register_camera\n");
521 goto fail_all;
524 ADD_TO_LIST(cam_list, cam);
526 return cam;
528 fail_all:
529 vfree(ucpia->buffers[2]);
530 ucpia->buffers[2] = NULL;
531 fail_alloc_2:
532 vfree(ucpia->buffers[1]);
533 ucpia->buffers[1] = NULL;
534 fail_alloc_1:
535 vfree(ucpia->buffers[0]);
536 ucpia->buffers[0] = NULL;
537 fail_alloc_0:
539 return NULL;
542 static void cpia_disconnect(struct usb_device *dev, void *ptr);
544 static struct usb_driver cpia_driver = {
545 "cpia",
546 cpia_probe,
547 cpia_disconnect,
548 { NULL, NULL }
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);
564 ucpia->present = 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;
593 if (!ucpia->open)
594 kfree(ucpia);
597 int usb_cpia_init(void)
599 cam_list = NULL;
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);
616 #ifdef MODULE
617 int init_module(void)
619 return usb_cpia_init();
622 void cleanup_module(void)
624 usb_cpia_cleanup();
626 #endif /* !MODULE */