1 /****************************************************************************
3 * Filename: cpia2_usb.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
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>
34 #include <linux/module.h>
38 static int frame_sizes
[] = {
49 #define FRAMES_PER_DESC 10
50 #define FRAME_SIZE_PER_DESC frame_sizes[cam->cur_alt]
52 static void process_frame(struct camera_data
*cam
);
53 static void cpia2_usb_complete(struct urb
*urb
);
54 static int cpia2_usb_probe(struct usb_interface
*intf
,
55 const struct usb_device_id
*id
);
56 static void cpia2_usb_disconnect(struct usb_interface
*intf
);
57 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
);
58 static int cpia2_usb_resume(struct usb_interface
*intf
);
60 static void free_sbufs(struct camera_data
*cam
);
61 static void add_APPn(struct camera_data
*cam
);
62 static void add_COM(struct camera_data
*cam
);
63 static int submit_urbs(struct camera_data
*cam
);
64 static int set_alternate(struct camera_data
*cam
, unsigned int alt
);
65 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
);
67 static struct usb_device_id cpia2_id_table
[] = {
68 {USB_DEVICE(0x0553, 0x0100)},
69 {USB_DEVICE(0x0553, 0x0140)},
70 {USB_DEVICE(0x0553, 0x0151)}, /* STV0676 */
71 {} /* Terminating entry */
73 MODULE_DEVICE_TABLE(usb
, cpia2_id_table
);
75 static struct usb_driver cpia2_driver
= {
77 .probe
= cpia2_usb_probe
,
78 .disconnect
= cpia2_usb_disconnect
,
79 .suspend
= cpia2_usb_suspend
,
80 .resume
= cpia2_usb_resume
,
81 .reset_resume
= cpia2_usb_resume
,
82 .id_table
= cpia2_id_table
86 /******************************************************************************
90 *****************************************************************************/
91 static void process_frame(struct camera_data
*cam
)
93 static int frame_count
;
95 unsigned char *inbuff
= cam
->workbuff
->data
;
97 DBG("Processing frame #%d, current:%d\n",
98 cam
->workbuff
->num
, cam
->curbuff
->num
);
100 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
101 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
103 if ((inbuff
[0] == 0xFF) && (inbuff
[1] == 0xD8)) {
106 cam
->workbuff
->status
= FRAME_ERROR
;
107 DBG("Start of frame not found\n");
112 * Now the output buffer should have a JPEG image in it.
114 if(!cam
->first_image_seen
) {
115 /* Always skip the first image after streaming
116 * starts. It is almost certainly corrupt. */
117 cam
->first_image_seen
= 1;
118 cam
->workbuff
->status
= FRAME_EMPTY
;
121 if (cam
->workbuff
->length
> 3) {
123 cam
->workbuff
->length
< cam
->workbuff
->max_length
) {
124 /* No junk in the buffers */
125 memset(cam
->workbuff
->data
+cam
->workbuff
->length
,
126 0, cam
->workbuff
->max_length
-
127 cam
->workbuff
->length
);
129 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
130 cam
->workbuff
->status
= FRAME_READY
;
132 if(!cam
->mmapped
&& cam
->num_frames
> 2) {
133 /* During normal reading, the most recent
134 * frame will be read. If the current frame
135 * hasn't started reading yet, it will never
136 * be read, so mark it empty. If the buffer is
137 * mmapped, or we have few buffers, we need to
138 * wait for the user to free the buffer.
140 * NOTE: This is not entirely foolproof with 3
141 * buffers, but it would take an EXTREMELY
142 * overloaded system to cause problems (possible
143 * image data corruption). Basically, it would
144 * need to take more time to execute cpia2_read
145 * than it would for the camera to send
146 * cam->num_frames-2 frames before problems
149 cam
->curbuff
->status
= FRAME_EMPTY
;
151 cam
->curbuff
= cam
->workbuff
;
152 cam
->workbuff
= cam
->workbuff
->next
;
153 DBG("Changed buffers, work:%d, current:%d\n",
154 cam
->workbuff
->num
, cam
->curbuff
->num
);
157 DBG("Not enough data for an image.\n");
160 cam
->workbuff
->status
= FRAME_ERROR
;
164 /******************************************************************************
168 * Adds a user specified APPn record
169 *****************************************************************************/
170 static void add_APPn(struct camera_data
*cam
)
172 if(cam
->APP_len
> 0) {
173 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
174 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xE0+cam
->APPn
;
175 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
176 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->APP_len
+2;
177 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
178 cam
->APP_data
, cam
->APP_len
);
179 cam
->workbuff
->length
+= cam
->APP_len
;
183 /******************************************************************************
187 * Adds a user specified COM record
188 *****************************************************************************/
189 static void add_COM(struct camera_data
*cam
)
191 if(cam
->COM_len
> 0) {
192 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
193 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFE;
194 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
195 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->COM_len
+2;
196 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
197 cam
->COM_data
, cam
->COM_len
);
198 cam
->workbuff
->length
+= cam
->COM_len
;
202 /******************************************************************************
206 * callback when incoming packet is received
207 *****************************************************************************/
208 static void cpia2_usb_complete(struct urb
*urb
)
211 unsigned char *cdata
;
212 static int frame_ready
= false;
213 struct camera_data
*cam
= (struct camera_data
*) urb
->context
;
215 if (urb
->status
!=0) {
216 if (!(urb
->status
== -ENOENT
||
217 urb
->status
== -ECONNRESET
||
218 urb
->status
== -ESHUTDOWN
))
220 DBG("urb->status = %d!\n", urb
->status
);
222 DBG("Stopping streaming\n");
226 if (!cam
->streaming
|| !video_is_registered(&cam
->vdev
)) {
227 LOG("Will now stop the streaming: streaming = %d, present=%d\n",
228 cam
->streaming
, video_is_registered(&cam
->vdev
));
235 //DBG("Collating %d packets\n", urb->number_of_packets);
236 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
237 u16 checksum
, iso_checksum
;
239 int n
= urb
->iso_frame_desc
[i
].actual_length
;
240 int st
= urb
->iso_frame_desc
[i
].status
;
242 if(cam
->workbuff
->status
== FRAME_READY
) {
243 struct framebuf
*ptr
;
244 /* Try to find an available buffer */
245 DBG("workbuff full, searching\n");
246 for (ptr
= cam
->workbuff
->next
;
247 ptr
!= cam
->workbuff
;
250 if (ptr
->status
== FRAME_EMPTY
) {
251 ptr
->status
= FRAME_READING
;
256 if (ptr
== cam
->workbuff
)
257 break; /* No READING or EMPTY buffers left */
262 if (cam
->workbuff
->status
== FRAME_EMPTY
||
263 cam
->workbuff
->status
== FRAME_ERROR
) {
264 cam
->workbuff
->status
= FRAME_READING
;
265 cam
->workbuff
->length
= 0;
268 //DBG(" Packet %d length = %d, status = %d\n", i, n, st);
269 cdata
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
272 LOG("cpia2 data error: [%d] len=%d, status = %d\n",
275 cam
->workbuff
->status
= FRAME_ERROR
;
284 checksum
+= cdata
[j
];
285 iso_checksum
= cdata
[j
] + cdata
[j
+1]*256;
286 if(checksum
!= iso_checksum
) {
287 LOG("checksum mismatch: [%d] len=%d, calculated = %x, checksum = %x\n",
288 i
, n
, (int)checksum
, (int)iso_checksum
);
290 cam
->workbuff
->status
= FRAME_ERROR
;
296 if(cam
->workbuff
->status
!= FRAME_READING
) {
297 if((0xFF == cdata
[0] && 0xD8 == cdata
[1]) ||
298 (0xD8 == cdata
[0] && 0xFF == cdata
[1] &&
300 /* frame is skipped, but increment total
301 * frame count anyway */
304 DBG("workbuff not reading, status=%d\n",
305 cam
->workbuff
->status
);
309 if (cam
->frame_size
< cam
->workbuff
->length
+ n
) {
310 ERR("buffer overflow! length: %d, n: %d\n",
311 cam
->workbuff
->length
, n
);
312 cam
->workbuff
->status
= FRAME_ERROR
;
313 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
314 cam
->workbuff
->max_length
=
315 cam
->workbuff
->length
;
319 if (cam
->workbuff
->length
== 0) {
321 if ((0xD8 == cdata
[0]) && (0xFF == cdata
[1])) {
323 } else if((0xFF == cdata
[0]) && (0xD8 == cdata
[1])
324 && (0xFF == cdata
[2])) {
327 DBG("Ignoring packet, not beginning!\n");
330 DBG("Start of frame pattern found\n");
331 v4l2_get_timestamp(&cam
->workbuff
->timestamp
);
332 cam
->workbuff
->seq
= cam
->frame_count
++;
333 cam
->workbuff
->data
[0] = 0xFF;
334 cam
->workbuff
->data
[1] = 0xD8;
335 cam
->workbuff
->length
= 2;
338 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
339 cdata
+data_offset
, n
-data_offset
);
340 cam
->workbuff
->length
+= n
-data_offset
;
341 } else if (cam
->workbuff
->length
> 0) {
342 memcpy(cam
->workbuff
->data
+ cam
->workbuff
->length
,
344 cam
->workbuff
->length
+= n
;
347 if ((cam
->workbuff
->length
>= 3) &&
348 (cam
->workbuff
->data
[cam
->workbuff
->length
- 3] == 0xFF) &&
349 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xD9) &&
350 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xFF)) {
352 cam
->workbuff
->data
[cam
->workbuff
->length
- 1] = 0;
353 cam
->workbuff
->length
-= 1;
354 } else if ((cam
->workbuff
->length
>= 2) &&
355 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xFF) &&
356 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xD9)) {
361 DBG("Workbuff image size = %d\n",cam
->workbuff
->length
);
366 if (waitqueue_active(&cam
->wq_stream
))
367 wake_up_interruptible(&cam
->wq_stream
);
374 if ((i
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0)
375 ERR("%s: usb_submit_urb ret %d!\n", __func__
, i
);
379 /******************************************************************************
381 * configure_transfer_mode
383 *****************************************************************************/
384 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
)
386 static unsigned char iso_regs
[8][4] = {
387 {0x00, 0x00, 0x00, 0x00},
388 {0x00, 0x00, 0x00, 0x00},
389 {0xB9, 0x00, 0x00, 0x7E},
390 {0xB9, 0x00, 0x01, 0x7E},
391 {0xB9, 0x00, 0x02, 0x7E},
392 {0xB9, 0x00, 0x02, 0xFE},
393 {0xB9, 0x00, 0x03, 0x7E},
394 {0xB9, 0x00, 0x03, 0xFD}
396 struct cpia2_command cmd
;
399 if (!video_is_registered(&cam
->vdev
))
403 * Write the isoc registers according to the alternate selected
405 cmd
.direction
= TRANSFER_WRITE
;
406 cmd
.buffer
.block_data
[0] = iso_regs
[alt
][0];
407 cmd
.buffer
.block_data
[1] = iso_regs
[alt
][1];
408 cmd
.buffer
.block_data
[2] = iso_regs
[alt
][2];
409 cmd
.buffer
.block_data
[3] = iso_regs
[alt
][3];
410 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
411 cmd
.start
= CPIA2_VC_USB_ISOLIM
;
413 cpia2_send_command(cam
, &cmd
);
416 * Enable relevant streams before starting polling.
417 * First read USB Stream Config Register.
419 cmd
.direction
= TRANSFER_READ
;
420 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
421 cmd
.start
= CPIA2_VC_USB_STRM
;
423 cpia2_send_command(cam
, &cmd
);
424 reg
= cmd
.buffer
.block_data
[0];
426 /* Clear iso, bulk, and int */
427 reg
&= ~(CPIA2_VC_USB_STRM_BLK_ENABLE
|
428 CPIA2_VC_USB_STRM_ISO_ENABLE
|
429 CPIA2_VC_USB_STRM_INT_ENABLE
);
431 if (alt
== USBIF_BULK
) {
432 DBG("Enabling bulk xfer\n");
433 reg
|= CPIA2_VC_USB_STRM_BLK_ENABLE
; /* Enable Bulk */
434 cam
->xfer_mode
= XFER_BULK
;
435 } else if (alt
>= USBIF_ISO_1
) {
436 DBG("Enabling ISOC xfer\n");
437 reg
|= CPIA2_VC_USB_STRM_ISO_ENABLE
;
438 cam
->xfer_mode
= XFER_ISOC
;
441 cmd
.buffer
.block_data
[0] = reg
;
442 cmd
.direction
= TRANSFER_WRITE
;
443 cmd
.start
= CPIA2_VC_USB_STRM
;
445 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
446 cpia2_send_command(cam
, &cmd
);
451 /******************************************************************************
453 * cpia2_usb_change_streaming_alternate
455 *****************************************************************************/
456 int cpia2_usb_change_streaming_alternate(struct camera_data
*cam
,
461 if(alt
< USBIF_ISO_1
|| alt
> USBIF_ISO_6
)
464 if(alt
== cam
->params
.camera_state
.stream_mode
)
467 cpia2_usb_stream_pause(cam
);
469 configure_transfer_mode(cam
, alt
);
471 cam
->params
.camera_state
.stream_mode
= alt
;
473 /* Reset the camera to prevent image quality degradation */
474 cpia2_reset_camera(cam
);
476 cpia2_usb_stream_resume(cam
);
481 /******************************************************************************
485 *****************************************************************************/
486 static int set_alternate(struct camera_data
*cam
, unsigned int alt
)
490 if(alt
== cam
->cur_alt
)
493 if (cam
->cur_alt
!= USBIF_CMDONLY
) {
494 DBG("Changing from alt %d to %d\n", cam
->cur_alt
, USBIF_CMDONLY
);
495 ret
= usb_set_interface(cam
->dev
, cam
->iface
, USBIF_CMDONLY
);
499 if (alt
!= USBIF_CMDONLY
) {
500 DBG("Changing from alt %d to %d\n", USBIF_CMDONLY
, alt
);
501 ret
= usb_set_interface(cam
->dev
, cam
->iface
, alt
);
506 cam
->old_alt
= cam
->cur_alt
;
512 /******************************************************************************
516 * Free all cam->sbuf[]. All non-NULL .data and .urb members that are non-NULL
517 * are assumed to be allocated. Non-NULL .urb members are also assumed to be
518 * submitted (and must therefore be killed before they are freed).
519 *****************************************************************************/
520 static void free_sbufs(struct camera_data
*cam
)
524 for (i
= 0; i
< NUM_SBUF
; i
++) {
525 if(cam
->sbuf
[i
].urb
) {
526 usb_kill_urb(cam
->sbuf
[i
].urb
);
527 usb_free_urb(cam
->sbuf
[i
].urb
);
528 cam
->sbuf
[i
].urb
= NULL
;
530 if(cam
->sbuf
[i
].data
) {
531 kfree(cam
->sbuf
[i
].data
);
532 cam
->sbuf
[i
].data
= NULL
;
538 * Convenience functions
540 /****************************************************************************
544 ***************************************************************************/
545 static int write_packet(struct usb_device
*udev
,
546 u8 request
, u8
* registers
, u16 start
, size_t size
)
548 if (!registers
|| size
<= 0)
551 return usb_control_msg(udev
,
552 usb_sndctrlpipe(udev
, 0),
554 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
557 registers
, /* buffer */
562 /****************************************************************************
566 ***************************************************************************/
567 static int read_packet(struct usb_device
*udev
,
568 u8 request
, u8
* registers
, u16 start
, size_t size
)
570 if (!registers
|| size
<= 0)
573 return usb_control_msg(udev
,
574 usb_rcvctrlpipe(udev
, 0),
576 USB_DIR_IN
|USB_TYPE_VENDOR
|USB_RECIP_DEVICE
,
579 registers
, /* buffer */
584 /******************************************************************************
586 * cpia2_usb_transfer_cmd
588 *****************************************************************************/
589 int cpia2_usb_transfer_cmd(struct camera_data
*cam
,
591 u8 request
, u8 start
, u8 count
, u8 direction
)
594 struct usb_device
*udev
= cam
->dev
;
597 ERR("%s: Internal driver error: udev is NULL\n", __func__
);
602 ERR("%s: Internal driver error: register array is NULL\n", __func__
);
606 if (direction
== TRANSFER_READ
) {
607 err
= read_packet(udev
, request
, (u8
*)registers
, start
, count
);
610 } else if (direction
== TRANSFER_WRITE
) {
611 err
=write_packet(udev
, request
, (u8
*)registers
, start
, count
);
613 LOG("Control message failed, err val = %d\n", err
);
614 LOG("Message: request = 0x%0X, start = 0x%0X\n",
616 LOG("Message: count = %d, register[0] = 0x%0X\n",
617 count
, ((unsigned char *) registers
)[0]);
621 LOG("Unexpected first byte of direction: %d\n",
627 LOG("Unexpected error: %d\n", err
);
632 /******************************************************************************
636 *****************************************************************************/
637 static int submit_urbs(struct camera_data
*cam
)
642 for(i
=0; i
<NUM_SBUF
; ++i
) {
643 if (cam
->sbuf
[i
].data
)
646 kmalloc(FRAMES_PER_DESC
* FRAME_SIZE_PER_DESC
, GFP_KERNEL
);
647 if (!cam
->sbuf
[i
].data
) {
649 kfree(cam
->sbuf
[i
].data
);
650 cam
->sbuf
[i
].data
= NULL
;
656 /* We double buffer the Isoc lists, and also know the polling
657 * interval is every frame (1 == (1 << (bInterval -1))).
659 for(i
=0; i
<NUM_SBUF
; ++i
) {
660 if(cam
->sbuf
[i
].urb
) {
663 urb
= usb_alloc_urb(FRAMES_PER_DESC
, GFP_KERNEL
);
665 ERR("%s: usb_alloc_urb error!\n", __func__
);
666 for (j
= 0; j
< i
; j
++)
667 usb_free_urb(cam
->sbuf
[j
].urb
);
671 cam
->sbuf
[i
].urb
= urb
;
674 urb
->pipe
= usb_rcvisocpipe(cam
->dev
, 1 /*ISOC endpoint*/);
675 urb
->transfer_flags
= URB_ISO_ASAP
;
676 urb
->transfer_buffer
= cam
->sbuf
[i
].data
;
677 urb
->complete
= cpia2_usb_complete
;
678 urb
->number_of_packets
= FRAMES_PER_DESC
;
680 urb
->transfer_buffer_length
=
681 FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
683 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
684 urb
->iso_frame_desc
[fx
].offset
=
685 FRAME_SIZE_PER_DESC
* fx
;
686 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
691 /* Queue the ISO urbs, and resubmit in the completion handler */
692 for(i
=0; i
<NUM_SBUF
; ++i
) {
693 err
= usb_submit_urb(cam
->sbuf
[i
].urb
, GFP_KERNEL
);
695 ERR("usb_submit_urb[%d]() = %d\n", i
, err
);
703 /******************************************************************************
705 * cpia2_usb_stream_start
707 *****************************************************************************/
708 int cpia2_usb_stream_start(struct camera_data
*cam
, unsigned int alternate
)
718 DBG("Flushing buffers\n");
719 for(i
=0; i
<cam
->num_frames
; ++i
) {
720 cam
->buffers
[i
].status
= FRAME_EMPTY
;
721 cam
->buffers
[i
].length
= 0;
723 cam
->curbuff
= &cam
->buffers
[0];
724 cam
->workbuff
= cam
->curbuff
->next
;
728 old_alt
= cam
->params
.camera_state
.stream_mode
;
729 cam
->params
.camera_state
.stream_mode
= 0;
730 ret
= cpia2_usb_change_streaming_alternate(cam
, alternate
);
733 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret
);
734 cam
->params
.camera_state
.stream_mode
= old_alt
;
735 ret2
= set_alternate(cam
, USBIF_CMDONLY
);
737 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already "
738 "failed. Then tried to call "
739 "set_alternate(USBIF_CMDONLY) = %d.\n",
740 alternate
, ret
, ret2
);
743 cam
->frame_count
= 0;
745 ret
= cpia2_usb_stream_resume(cam
);
750 /******************************************************************************
752 * cpia2_usb_stream_pause
754 *****************************************************************************/
755 int cpia2_usb_stream_pause(struct camera_data
*cam
)
760 ret
= set_alternate(cam
, USBIF_CMDONLY
);
765 /******************************************************************************
767 * cpia2_usb_stream_resume
769 *****************************************************************************/
770 int cpia2_usb_stream_resume(struct camera_data
*cam
)
774 cam
->first_image_seen
= 0;
775 ret
= set_alternate(cam
, cam
->params
.camera_state
.stream_mode
);
777 /* for some reason the user effects need to be set
778 again when starting streaming. */
779 cpia2_do_command(cam
, CPIA2_CMD_SET_USER_EFFECTS
, TRANSFER_WRITE
,
780 cam
->params
.vp_params
.user_effects
);
781 ret
= submit_urbs(cam
);
787 /******************************************************************************
789 * cpia2_usb_stream_stop
791 *****************************************************************************/
792 int cpia2_usb_stream_stop(struct camera_data
*cam
)
796 ret
= cpia2_usb_stream_pause(cam
);
798 configure_transfer_mode(cam
, 0);
802 /******************************************************************************
806 * Probe and initialize.
807 *****************************************************************************/
808 static int cpia2_usb_probe(struct usb_interface
*intf
,
809 const struct usb_device_id
*id
)
811 struct usb_device
*udev
= interface_to_usbdev(intf
);
812 struct usb_interface_descriptor
*interface
;
813 struct camera_data
*cam
;
816 /* A multi-config CPiA2 camera? */
817 if (udev
->descriptor
.bNumConfigurations
!= 1)
819 interface
= &intf
->cur_altsetting
->desc
;
821 /* If we get to this point, we found a CPiA2 camera */
822 LOG("CPiA2 USB camera found\n");
824 cam
= cpia2_init_camera_struct(intf
);
829 cam
->iface
= interface
->bInterfaceNumber
;
831 ret
= set_alternate(cam
, USBIF_CMDONLY
);
833 ERR("%s: usb_set_interface error (ret = %d)\n", __func__
, ret
);
839 if((ret
= cpia2_init_camera(cam
)) < 0) {
840 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__
, ret
);
844 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
845 cam
->params
.version
.firmware_revision_hi
,
846 cam
->params
.version
.firmware_revision_lo
,
847 cam
->params
.version
.asic_id
,
848 cam
->params
.version
.asic_rev
);
849 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
850 cam
->params
.pnp_id
.vendor
,
851 cam
->params
.pnp_id
.product
,
852 cam
->params
.pnp_id
.device_revision
);
853 LOG(" SensorID: %d.(version %d)\n",
854 cam
->params
.version
.sensor_flags
,
855 cam
->params
.version
.sensor_rev
);
857 usb_set_intfdata(intf
, cam
);
859 ret
= cpia2_register_camera(cam
);
861 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__
, ret
);
869 /******************************************************************************
873 *****************************************************************************/
874 static void cpia2_usb_disconnect(struct usb_interface
*intf
)
876 struct camera_data
*cam
= usb_get_intfdata(intf
);
877 usb_set_intfdata(intf
, NULL
);
879 DBG("Stopping stream\n");
880 cpia2_usb_stream_stop(cam
);
882 mutex_lock(&cam
->v4l2_lock
);
883 DBG("Unregistering camera\n");
884 cpia2_unregister_camera(cam
);
885 v4l2_device_disconnect(&cam
->v4l2_dev
);
886 mutex_unlock(&cam
->v4l2_lock
);
887 v4l2_device_put(&cam
->v4l2_dev
);
890 DBG("Wakeup waiting processes\n");
891 cam
->curbuff
->status
= FRAME_READY
;
892 cam
->curbuff
->length
= 0;
893 if (waitqueue_active(&cam
->wq_stream
))
894 wake_up_interruptible(&cam
->wq_stream
);
897 DBG("Releasing interface\n");
898 usb_driver_release_interface(&cpia2_driver
, intf
);
900 LOG("CPiA2 camera disconnected.\n");
903 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
905 struct camera_data
*cam
= usb_get_intfdata(intf
);
907 mutex_lock(&cam
->v4l2_lock
);
908 if (cam
->streaming
) {
909 cpia2_usb_stream_stop(cam
);
912 mutex_unlock(&cam
->v4l2_lock
);
914 dev_info(&intf
->dev
, "going into suspend..\n");
918 /* Resume device - start device. */
919 static int cpia2_usb_resume(struct usb_interface
*intf
)
921 struct camera_data
*cam
= usb_get_intfdata(intf
);
923 mutex_lock(&cam
->v4l2_lock
);
924 v4l2_ctrl_handler_setup(&cam
->hdl
);
925 if (cam
->streaming
) {
927 cpia2_usb_stream_start(cam
,
928 cam
->params
.camera_state
.stream_mode
);
930 mutex_unlock(&cam
->v4l2_lock
);
932 dev_info(&intf
->dev
, "coming out of suspend..\n");
936 /******************************************************************************
940 *****************************************************************************/
941 int cpia2_usb_init(void)
943 return usb_register(&cpia2_driver
);
946 /******************************************************************************
950 *****************************************************************************/
951 void cpia2_usb_cleanup(void)
953 schedule_timeout(2 * HZ
);
954 usb_deregister(&cpia2_driver
);