Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / cpia2 / cpia2_usb.c
blobdc5b07a20f693a50a4af0cdc7a2b93fb5ff3c2f7
1 /****************************************************************************
3 * Filename: cpia2_usb.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
8 * Description:
9 * This is a USB driver for CPia2 based video cameras.
10 * The infrastructure of this driver is based on the cpia usb driver by
11 * Jochen Scharrlach and Johannes Erdfeldt.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Stripped of 2.4 stuff ready for main kernel submit by
28 * Alan Cox <alan@lxorguk.ukuu.org.uk>
29 ****************************************************************************/
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/usb.h>
35 #include "cpia2.h"
37 static int frame_sizes[] = {
38 0, // USBIF_CMDONLY
39 0, // USBIF_BULK
40 128, // USBIF_ISO_1
41 384, // USBIF_ISO_2
42 640, // USBIF_ISO_3
43 768, // USBIF_ISO_4
44 896, // USBIF_ISO_5
45 1023, // USBIF_ISO_6
48 #define FRAMES_PER_DESC 10
49 #define FRAME_SIZE_PER_DESC frame_sizes[cam->cur_alt]
51 static void process_frame(struct camera_data *cam);
52 static void cpia2_usb_complete(struct urb *urb);
53 static int cpia2_usb_probe(struct usb_interface *intf,
54 const struct usb_device_id *id);
55 static void cpia2_usb_disconnect(struct usb_interface *intf);
57 static void free_sbufs(struct camera_data *cam);
58 static void add_APPn(struct camera_data *cam);
59 static void add_COM(struct camera_data *cam);
60 static int submit_urbs(struct camera_data *cam);
61 static int set_alternate(struct camera_data *cam, unsigned int alt);
62 static int configure_transfer_mode(struct camera_data *cam, unsigned int alt);
64 static struct usb_device_id cpia2_id_table[] = {
65 {USB_DEVICE(0x0553, 0x0100)},
66 {USB_DEVICE(0x0553, 0x0140)},
67 {USB_DEVICE(0x0553, 0x0151)}, /* STV0676 */
68 {} /* Terminating entry */
70 MODULE_DEVICE_TABLE(usb, cpia2_id_table);
72 static struct usb_driver cpia2_driver = {
73 .name = "cpia2",
74 .probe = cpia2_usb_probe,
75 .disconnect = cpia2_usb_disconnect,
76 .id_table = cpia2_id_table
80 /******************************************************************************
82 * process_frame
84 *****************************************************************************/
85 static void process_frame(struct camera_data *cam)
87 static int frame_count;
89 unsigned char *inbuff = cam->workbuff->data;
91 DBG("Processing frame #%d, current:%d\n",
92 cam->workbuff->num, cam->curbuff->num);
94 if(cam->workbuff->length > cam->workbuff->max_length)
95 cam->workbuff->max_length = cam->workbuff->length;
97 if ((inbuff[0] == 0xFF) && (inbuff[1] == 0xD8)) {
98 frame_count++;
99 } else {
100 cam->workbuff->status = FRAME_ERROR;
101 DBG("Start of frame not found\n");
102 return;
105 /***
106 * Now the output buffer should have a JPEG image in it.
107 ***/
108 if(!cam->first_image_seen) {
109 /* Always skip the first image after streaming
110 * starts. It is almost certainly corrupt. */
111 cam->first_image_seen = 1;
112 cam->workbuff->status = FRAME_EMPTY;
113 return;
115 if (cam->workbuff->length > 3) {
116 if(cam->mmapped &&
117 cam->workbuff->length < cam->workbuff->max_length) {
118 /* No junk in the buffers */
119 memset(cam->workbuff->data+cam->workbuff->length,
120 0, cam->workbuff->max_length-
121 cam->workbuff->length);
123 cam->workbuff->max_length = cam->workbuff->length;
124 cam->workbuff->status = FRAME_READY;
126 if(!cam->mmapped && cam->num_frames > 2) {
127 /* During normal reading, the most recent
128 * frame will be read. If the current frame
129 * hasn't started reading yet, it will never
130 * be read, so mark it empty. If the buffer is
131 * mmapped, or we have few buffers, we need to
132 * wait for the user to free the buffer.
134 * NOTE: This is not entirely foolproof with 3
135 * buffers, but it would take an EXTREMELY
136 * overloaded system to cause problems (possible
137 * image data corruption). Basically, it would
138 * need to take more time to execute cpia2_read
139 * than it would for the camera to send
140 * cam->num_frames-2 frames before problems
141 * could occur.
143 cam->curbuff->status = FRAME_EMPTY;
145 cam->curbuff = cam->workbuff;
146 cam->workbuff = cam->workbuff->next;
147 DBG("Changed buffers, work:%d, current:%d\n",
148 cam->workbuff->num, cam->curbuff->num);
149 return;
150 } else {
151 DBG("Not enough data for an image.\n");
154 cam->workbuff->status = FRAME_ERROR;
155 return;
158 /******************************************************************************
160 * add_APPn
162 * Adds a user specified APPn record
163 *****************************************************************************/
164 static void add_APPn(struct camera_data *cam)
166 if(cam->APP_len > 0) {
167 cam->workbuff->data[cam->workbuff->length++] = 0xFF;
168 cam->workbuff->data[cam->workbuff->length++] = 0xE0+cam->APPn;
169 cam->workbuff->data[cam->workbuff->length++] = 0;
170 cam->workbuff->data[cam->workbuff->length++] = cam->APP_len+2;
171 memcpy(cam->workbuff->data+cam->workbuff->length,
172 cam->APP_data, cam->APP_len);
173 cam->workbuff->length += cam->APP_len;
177 /******************************************************************************
179 * add_COM
181 * Adds a user specified COM record
182 *****************************************************************************/
183 static void add_COM(struct camera_data *cam)
185 if(cam->COM_len > 0) {
186 cam->workbuff->data[cam->workbuff->length++] = 0xFF;
187 cam->workbuff->data[cam->workbuff->length++] = 0xFE;
188 cam->workbuff->data[cam->workbuff->length++] = 0;
189 cam->workbuff->data[cam->workbuff->length++] = cam->COM_len+2;
190 memcpy(cam->workbuff->data+cam->workbuff->length,
191 cam->COM_data, cam->COM_len);
192 cam->workbuff->length += cam->COM_len;
196 /******************************************************************************
198 * cpia2_usb_complete
200 * callback when incoming packet is received
201 *****************************************************************************/
202 static void cpia2_usb_complete(struct urb *urb)
204 int i;
205 unsigned char *cdata;
206 static int frame_ready = false;
207 struct camera_data *cam = (struct camera_data *) urb->context;
209 if (urb->status!=0) {
210 if (!(urb->status == -ENOENT ||
211 urb->status == -ECONNRESET ||
212 urb->status == -ESHUTDOWN))
214 DBG("urb->status = %d!\n", urb->status);
216 DBG("Stopping streaming\n");
217 return;
220 if (!cam->streaming || !cam->present || cam->open_count == 0) {
221 LOG("Will now stop the streaming: streaming = %d, "
222 "present=%d, open_count=%d\n",
223 cam->streaming, cam->present, cam->open_count);
224 return;
227 /***
228 * Packet collater
229 ***/
230 //DBG("Collating %d packets\n", urb->number_of_packets);
231 for (i = 0; i < urb->number_of_packets; i++) {
232 u16 checksum, iso_checksum;
233 int j;
234 int n = urb->iso_frame_desc[i].actual_length;
235 int st = urb->iso_frame_desc[i].status;
237 if(cam->workbuff->status == FRAME_READY) {
238 struct framebuf *ptr;
239 /* Try to find an available buffer */
240 DBG("workbuff full, searching\n");
241 for (ptr = cam->workbuff->next;
242 ptr != cam->workbuff;
243 ptr = ptr->next)
245 if (ptr->status == FRAME_EMPTY) {
246 ptr->status = FRAME_READING;
247 ptr->length = 0;
248 break;
251 if (ptr == cam->workbuff)
252 break; /* No READING or EMPTY buffers left */
254 cam->workbuff = ptr;
257 if (cam->workbuff->status == FRAME_EMPTY ||
258 cam->workbuff->status == FRAME_ERROR) {
259 cam->workbuff->status = FRAME_READING;
260 cam->workbuff->length = 0;
263 //DBG(" Packet %d length = %d, status = %d\n", i, n, st);
264 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
266 if (st) {
267 LOG("cpia2 data error: [%d] len=%d, status = %d\n",
268 i, n, st);
269 if(!ALLOW_CORRUPT)
270 cam->workbuff->status = FRAME_ERROR;
271 continue;
274 if(n<=2)
275 continue;
277 checksum = 0;
278 for(j=0; j<n-2; ++j)
279 checksum += cdata[j];
280 iso_checksum = cdata[j] + cdata[j+1]*256;
281 if(checksum != iso_checksum) {
282 LOG("checksum mismatch: [%d] len=%d, calculated = %x, checksum = %x\n",
283 i, n, (int)checksum, (int)iso_checksum);
284 if(!ALLOW_CORRUPT) {
285 cam->workbuff->status = FRAME_ERROR;
286 continue;
289 n -= 2;
291 if(cam->workbuff->status != FRAME_READING) {
292 if((0xFF == cdata[0] && 0xD8 == cdata[1]) ||
293 (0xD8 == cdata[0] && 0xFF == cdata[1] &&
294 0 != cdata[2])) {
295 /* frame is skipped, but increment total
296 * frame count anyway */
297 cam->frame_count++;
299 DBG("workbuff not reading, status=%d\n",
300 cam->workbuff->status);
301 continue;
304 if (cam->frame_size < cam->workbuff->length + n) {
305 ERR("buffer overflow! length: %d, n: %d\n",
306 cam->workbuff->length, n);
307 cam->workbuff->status = FRAME_ERROR;
308 if(cam->workbuff->length > cam->workbuff->max_length)
309 cam->workbuff->max_length =
310 cam->workbuff->length;
311 continue;
314 if (cam->workbuff->length == 0) {
315 int data_offset;
316 if ((0xD8 == cdata[0]) && (0xFF == cdata[1])) {
317 data_offset = 1;
318 } else if((0xFF == cdata[0]) && (0xD8 == cdata[1])
319 && (0xFF == cdata[2])) {
320 data_offset = 2;
321 } else {
322 DBG("Ignoring packet, not beginning!\n");
323 continue;
325 DBG("Start of frame pattern found\n");
326 do_gettimeofday(&cam->workbuff->timestamp);
327 cam->workbuff->seq = cam->frame_count++;
328 cam->workbuff->data[0] = 0xFF;
329 cam->workbuff->data[1] = 0xD8;
330 cam->workbuff->length = 2;
331 add_APPn(cam);
332 add_COM(cam);
333 memcpy(cam->workbuff->data+cam->workbuff->length,
334 cdata+data_offset, n-data_offset);
335 cam->workbuff->length += n-data_offset;
336 } else if (cam->workbuff->length > 0) {
337 memcpy(cam->workbuff->data + cam->workbuff->length,
338 cdata, n);
339 cam->workbuff->length += n;
342 if ((cam->workbuff->length >= 3) &&
343 (cam->workbuff->data[cam->workbuff->length - 3] == 0xFF) &&
344 (cam->workbuff->data[cam->workbuff->length - 2] == 0xD9) &&
345 (cam->workbuff->data[cam->workbuff->length - 1] == 0xFF)) {
346 frame_ready = true;
347 cam->workbuff->data[cam->workbuff->length - 1] = 0;
348 cam->workbuff->length -= 1;
349 } else if ((cam->workbuff->length >= 2) &&
350 (cam->workbuff->data[cam->workbuff->length - 2] == 0xFF) &&
351 (cam->workbuff->data[cam->workbuff->length - 1] == 0xD9)) {
352 frame_ready = true;
355 if (frame_ready) {
356 DBG("Workbuff image size = %d\n",cam->workbuff->length);
357 process_frame(cam);
359 frame_ready = false;
361 if (waitqueue_active(&cam->wq_stream))
362 wake_up_interruptible(&cam->wq_stream);
366 if(cam->streaming) {
367 /* resubmit */
368 urb->dev = cam->dev;
369 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
370 ERR("%s: usb_submit_urb ret %d!\n", __func__, i);
374 /******************************************************************************
376 * configure_transfer_mode
378 *****************************************************************************/
379 static int configure_transfer_mode(struct camera_data *cam, unsigned int alt)
381 static unsigned char iso_regs[8][4] = {
382 {0x00, 0x00, 0x00, 0x00},
383 {0x00, 0x00, 0x00, 0x00},
384 {0xB9, 0x00, 0x00, 0x7E},
385 {0xB9, 0x00, 0x01, 0x7E},
386 {0xB9, 0x00, 0x02, 0x7E},
387 {0xB9, 0x00, 0x02, 0xFE},
388 {0xB9, 0x00, 0x03, 0x7E},
389 {0xB9, 0x00, 0x03, 0xFD}
391 struct cpia2_command cmd;
392 unsigned char reg;
394 if(!cam->present)
395 return -ENODEV;
397 /***
398 * Write the isoc registers according to the alternate selected
399 ***/
400 cmd.direction = TRANSFER_WRITE;
401 cmd.buffer.block_data[0] = iso_regs[alt][0];
402 cmd.buffer.block_data[1] = iso_regs[alt][1];
403 cmd.buffer.block_data[2] = iso_regs[alt][2];
404 cmd.buffer.block_data[3] = iso_regs[alt][3];
405 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
406 cmd.start = CPIA2_VC_USB_ISOLIM;
407 cmd.reg_count = 4;
408 cpia2_send_command(cam, &cmd);
410 /***
411 * Enable relevant streams before starting polling.
412 * First read USB Stream Config Register.
413 ***/
414 cmd.direction = TRANSFER_READ;
415 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
416 cmd.start = CPIA2_VC_USB_STRM;
417 cmd.reg_count = 1;
418 cpia2_send_command(cam, &cmd);
419 reg = cmd.buffer.block_data[0];
421 /* Clear iso, bulk, and int */
422 reg &= ~(CPIA2_VC_USB_STRM_BLK_ENABLE |
423 CPIA2_VC_USB_STRM_ISO_ENABLE |
424 CPIA2_VC_USB_STRM_INT_ENABLE);
426 if (alt == USBIF_BULK) {
427 DBG("Enabling bulk xfer\n");
428 reg |= CPIA2_VC_USB_STRM_BLK_ENABLE; /* Enable Bulk */
429 cam->xfer_mode = XFER_BULK;
430 } else if (alt >= USBIF_ISO_1) {
431 DBG("Enabling ISOC xfer\n");
432 reg |= CPIA2_VC_USB_STRM_ISO_ENABLE;
433 cam->xfer_mode = XFER_ISOC;
436 cmd.buffer.block_data[0] = reg;
437 cmd.direction = TRANSFER_WRITE;
438 cmd.start = CPIA2_VC_USB_STRM;
439 cmd.reg_count = 1;
440 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
441 cpia2_send_command(cam, &cmd);
443 return 0;
446 /******************************************************************************
448 * cpia2_usb_change_streaming_alternate
450 *****************************************************************************/
451 int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
452 unsigned int alt)
454 int ret = 0;
456 if(alt < USBIF_ISO_1 || alt > USBIF_ISO_6)
457 return -EINVAL;
459 if(alt == cam->params.camera_state.stream_mode)
460 return 0;
462 cpia2_usb_stream_pause(cam);
464 configure_transfer_mode(cam, alt);
466 cam->params.camera_state.stream_mode = alt;
468 /* Reset the camera to prevent image quality degradation */
469 cpia2_reset_camera(cam);
471 cpia2_usb_stream_resume(cam);
473 return ret;
476 /******************************************************************************
478 * set_alternate
480 *****************************************************************************/
481 static int set_alternate(struct camera_data *cam, unsigned int alt)
483 int ret = 0;
485 if(alt == cam->cur_alt)
486 return 0;
488 if (cam->cur_alt != USBIF_CMDONLY) {
489 DBG("Changing from alt %d to %d\n", cam->cur_alt, USBIF_CMDONLY);
490 ret = usb_set_interface(cam->dev, cam->iface, USBIF_CMDONLY);
491 if (ret != 0)
492 return ret;
494 if (alt != USBIF_CMDONLY) {
495 DBG("Changing from alt %d to %d\n", USBIF_CMDONLY, alt);
496 ret = usb_set_interface(cam->dev, cam->iface, alt);
497 if (ret != 0)
498 return ret;
501 cam->old_alt = cam->cur_alt;
502 cam->cur_alt = alt;
504 return ret;
507 /******************************************************************************
509 * free_sbufs
511 * Free all cam->sbuf[]. All non-NULL .data and .urb members that are non-NULL
512 * are assumed to be allocated. Non-NULL .urb members are also assumed to be
513 * submitted (and must therefore be killed before they are freed).
514 *****************************************************************************/
515 static void free_sbufs(struct camera_data *cam)
517 int i;
519 for (i = 0; i < NUM_SBUF; i++) {
520 if(cam->sbuf[i].urb) {
521 usb_kill_urb(cam->sbuf[i].urb);
522 usb_free_urb(cam->sbuf[i].urb);
523 cam->sbuf[i].urb = NULL;
525 if(cam->sbuf[i].data) {
526 kfree(cam->sbuf[i].data);
527 cam->sbuf[i].data = NULL;
532 /*******
533 * Convenience functions
534 *******/
535 /****************************************************************************
537 * write_packet
539 ***************************************************************************/
540 static int write_packet(struct usb_device *udev,
541 u8 request, u8 * registers, u16 start, size_t size)
543 if (!registers || size <= 0)
544 return -EINVAL;
546 return usb_control_msg(udev,
547 usb_sndctrlpipe(udev, 0),
548 request,
549 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
550 start, /* value */
551 0, /* index */
552 registers, /* buffer */
553 size,
554 HZ);
557 /****************************************************************************
559 * read_packet
561 ***************************************************************************/
562 static int read_packet(struct usb_device *udev,
563 u8 request, u8 * registers, u16 start, size_t size)
565 if (!registers || size <= 0)
566 return -EINVAL;
568 return usb_control_msg(udev,
569 usb_rcvctrlpipe(udev, 0),
570 request,
571 USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
572 start, /* value */
573 0, /* index */
574 registers, /* buffer */
575 size,
576 HZ);
579 /******************************************************************************
581 * cpia2_usb_transfer_cmd
583 *****************************************************************************/
584 int cpia2_usb_transfer_cmd(struct camera_data *cam,
585 void *registers,
586 u8 request, u8 start, u8 count, u8 direction)
588 int err = 0;
589 struct usb_device *udev = cam->dev;
591 if (!udev) {
592 ERR("%s: Internal driver error: udev is NULL\n", __func__);
593 return -EINVAL;
596 if (!registers) {
597 ERR("%s: Internal driver error: register array is NULL\n", __func__);
598 return -EINVAL;
601 if (direction == TRANSFER_READ) {
602 err = read_packet(udev, request, (u8 *)registers, start, count);
603 if (err > 0)
604 err = 0;
605 } else if (direction == TRANSFER_WRITE) {
606 err =write_packet(udev, request, (u8 *)registers, start, count);
607 if (err < 0) {
608 LOG("Control message failed, err val = %d\n", err);
609 LOG("Message: request = 0x%0X, start = 0x%0X\n",
610 request, start);
611 LOG("Message: count = %d, register[0] = 0x%0X\n",
612 count, ((unsigned char *) registers)[0]);
613 } else
614 err=0;
615 } else {
616 LOG("Unexpected first byte of direction: %d\n",
617 direction);
618 return -EINVAL;
621 if(err != 0)
622 LOG("Unexpected error: %d\n", err);
623 return err;
627 /******************************************************************************
629 * submit_urbs
631 *****************************************************************************/
632 static int submit_urbs(struct camera_data *cam)
634 struct urb *urb;
635 int fx, err, i, j;
637 for(i=0; i<NUM_SBUF; ++i) {
638 if (cam->sbuf[i].data)
639 continue;
640 cam->sbuf[i].data =
641 kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
642 if (!cam->sbuf[i].data) {
643 while (--i >= 0) {
644 kfree(cam->sbuf[i].data);
645 cam->sbuf[i].data = NULL;
647 return -ENOMEM;
651 /* We double buffer the Isoc lists, and also know the polling
652 * interval is every frame (1 == (1 << (bInterval -1))).
654 for(i=0; i<NUM_SBUF; ++i) {
655 if(cam->sbuf[i].urb) {
656 continue;
658 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
659 if (!urb) {
660 ERR("%s: usb_alloc_urb error!\n", __func__);
661 for (j = 0; j < i; j++)
662 usb_free_urb(cam->sbuf[j].urb);
663 return -ENOMEM;
666 cam->sbuf[i].urb = urb;
667 urb->dev = cam->dev;
668 urb->context = cam;
669 urb->pipe = usb_rcvisocpipe(cam->dev, 1 /*ISOC endpoint*/);
670 urb->transfer_flags = URB_ISO_ASAP;
671 urb->transfer_buffer = cam->sbuf[i].data;
672 urb->complete = cpia2_usb_complete;
673 urb->number_of_packets = FRAMES_PER_DESC;
674 urb->interval = 1;
675 urb->transfer_buffer_length =
676 FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
678 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
679 urb->iso_frame_desc[fx].offset =
680 FRAME_SIZE_PER_DESC * fx;
681 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
686 /* Queue the ISO urbs, and resubmit in the completion handler */
687 for(i=0; i<NUM_SBUF; ++i) {
688 err = usb_submit_urb(cam->sbuf[i].urb, GFP_KERNEL);
689 if (err) {
690 ERR("usb_submit_urb[%d]() = %d\n", i, err);
691 return err;
695 return 0;
698 /******************************************************************************
700 * cpia2_usb_stream_start
702 *****************************************************************************/
703 int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate)
705 int ret;
706 int old_alt;
708 if(cam->streaming)
709 return 0;
711 if (cam->flush) {
712 int i;
713 DBG("Flushing buffers\n");
714 for(i=0; i<cam->num_frames; ++i) {
715 cam->buffers[i].status = FRAME_EMPTY;
716 cam->buffers[i].length = 0;
718 cam->curbuff = &cam->buffers[0];
719 cam->workbuff = cam->curbuff->next;
720 cam->flush = false;
723 old_alt = cam->params.camera_state.stream_mode;
724 cam->params.camera_state.stream_mode = 0;
725 ret = cpia2_usb_change_streaming_alternate(cam, alternate);
726 if (ret < 0) {
727 int ret2;
728 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret);
729 cam->params.camera_state.stream_mode = old_alt;
730 ret2 = set_alternate(cam, USBIF_CMDONLY);
731 if (ret2 < 0) {
732 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already "
733 "failed. Then tried to call "
734 "set_alternate(USBIF_CMDONLY) = %d.\n",
735 alternate, ret, ret2);
737 } else {
738 cam->frame_count = 0;
739 cam->streaming = 1;
740 ret = cpia2_usb_stream_resume(cam);
742 return ret;
745 /******************************************************************************
747 * cpia2_usb_stream_pause
749 *****************************************************************************/
750 int cpia2_usb_stream_pause(struct camera_data *cam)
752 int ret = 0;
753 if(cam->streaming) {
754 ret = set_alternate(cam, USBIF_CMDONLY);
755 free_sbufs(cam);
757 return ret;
760 /******************************************************************************
762 * cpia2_usb_stream_resume
764 *****************************************************************************/
765 int cpia2_usb_stream_resume(struct camera_data *cam)
767 int ret = 0;
768 if(cam->streaming) {
769 cam->first_image_seen = 0;
770 ret = set_alternate(cam, cam->params.camera_state.stream_mode);
771 if(ret == 0) {
772 ret = submit_urbs(cam);
775 return ret;
778 /******************************************************************************
780 * cpia2_usb_stream_stop
782 *****************************************************************************/
783 int cpia2_usb_stream_stop(struct camera_data *cam)
785 int ret;
786 ret = cpia2_usb_stream_pause(cam);
787 cam->streaming = 0;
788 configure_transfer_mode(cam, 0);
789 return ret;
792 /******************************************************************************
794 * cpia2_usb_probe
796 * Probe and initialize.
797 *****************************************************************************/
798 static int cpia2_usb_probe(struct usb_interface *intf,
799 const struct usb_device_id *id)
801 struct usb_device *udev = interface_to_usbdev(intf);
802 struct usb_interface_descriptor *interface;
803 struct camera_data *cam;
804 int ret;
806 /* A multi-config CPiA2 camera? */
807 if (udev->descriptor.bNumConfigurations != 1)
808 return -ENODEV;
809 interface = &intf->cur_altsetting->desc;
811 /* If we get to this point, we found a CPiA2 camera */
812 LOG("CPiA2 USB camera found\n");
814 if((cam = cpia2_init_camera_struct()) == NULL)
815 return -ENOMEM;
817 cam->dev = udev;
818 cam->iface = interface->bInterfaceNumber;
820 ret = set_alternate(cam, USBIF_CMDONLY);
821 if (ret < 0) {
822 ERR("%s: usb_set_interface error (ret = %d)\n", __func__, ret);
823 kfree(cam);
824 return ret;
827 if ((ret = cpia2_register_camera(cam)) < 0) {
828 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__, ret);
829 kfree(cam);
830 return ret;
834 if((ret = cpia2_init_camera(cam)) < 0) {
835 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__, ret);
836 cpia2_unregister_camera(cam);
837 kfree(cam);
838 return ret;
840 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
841 cam->params.version.firmware_revision_hi,
842 cam->params.version.firmware_revision_lo,
843 cam->params.version.asic_id,
844 cam->params.version.asic_rev);
845 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
846 cam->params.pnp_id.vendor,
847 cam->params.pnp_id.product,
848 cam->params.pnp_id.device_revision);
849 LOG(" SensorID: %d.(version %d)\n",
850 cam->params.version.sensor_flags,
851 cam->params.version.sensor_rev);
853 usb_set_intfdata(intf, cam);
855 return 0;
858 /******************************************************************************
860 * cpia2_disconnect
862 *****************************************************************************/
863 static void cpia2_usb_disconnect(struct usb_interface *intf)
865 struct camera_data *cam = usb_get_intfdata(intf);
866 usb_set_intfdata(intf, NULL);
867 cam->present = 0;
869 DBG("Stopping stream\n");
870 cpia2_usb_stream_stop(cam);
872 DBG("Unregistering camera\n");
873 cpia2_unregister_camera(cam);
875 if(cam->buffers) {
876 DBG("Wakeup waiting processes\n");
877 cam->curbuff->status = FRAME_READY;
878 cam->curbuff->length = 0;
879 if (waitqueue_active(&cam->wq_stream))
880 wake_up_interruptible(&cam->wq_stream);
883 DBG("Releasing interface\n");
884 usb_driver_release_interface(&cpia2_driver, intf);
886 if (cam->open_count == 0) {
887 DBG("Freeing camera structure\n");
888 kfree(cam);
891 LOG("CPiA2 camera disconnected.\n");
895 /******************************************************************************
897 * usb_cpia2_init
899 *****************************************************************************/
900 int cpia2_usb_init(void)
902 return usb_register(&cpia2_driver);
905 /******************************************************************************
907 * usb_cpia_cleanup
909 *****************************************************************************/
910 void cpia2_usb_cleanup(void)
912 schedule_timeout(2 * HZ);
913 usb_deregister(&cpia2_driver);