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 bool 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 for (j
= 0; j
< i
; j
++)
666 usb_free_urb(cam
->sbuf
[j
].urb
);
670 cam
->sbuf
[i
].urb
= urb
;
673 urb
->pipe
= usb_rcvisocpipe(cam
->dev
, 1 /*ISOC endpoint*/);
674 urb
->transfer_flags
= URB_ISO_ASAP
;
675 urb
->transfer_buffer
= cam
->sbuf
[i
].data
;
676 urb
->complete
= cpia2_usb_complete
;
677 urb
->number_of_packets
= FRAMES_PER_DESC
;
679 urb
->transfer_buffer_length
=
680 FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
682 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
683 urb
->iso_frame_desc
[fx
].offset
=
684 FRAME_SIZE_PER_DESC
* fx
;
685 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
690 /* Queue the ISO urbs, and resubmit in the completion handler */
691 for(i
=0; i
<NUM_SBUF
; ++i
) {
692 err
= usb_submit_urb(cam
->sbuf
[i
].urb
, GFP_KERNEL
);
694 ERR("usb_submit_urb[%d]() = %d\n", i
, err
);
702 /******************************************************************************
704 * cpia2_usb_stream_start
706 *****************************************************************************/
707 int cpia2_usb_stream_start(struct camera_data
*cam
, unsigned int alternate
)
717 DBG("Flushing buffers\n");
718 for(i
=0; i
<cam
->num_frames
; ++i
) {
719 cam
->buffers
[i
].status
= FRAME_EMPTY
;
720 cam
->buffers
[i
].length
= 0;
722 cam
->curbuff
= &cam
->buffers
[0];
723 cam
->workbuff
= cam
->curbuff
->next
;
727 old_alt
= cam
->params
.camera_state
.stream_mode
;
728 cam
->params
.camera_state
.stream_mode
= 0;
729 ret
= cpia2_usb_change_streaming_alternate(cam
, alternate
);
732 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret
);
733 cam
->params
.camera_state
.stream_mode
= old_alt
;
734 ret2
= set_alternate(cam
, USBIF_CMDONLY
);
736 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already "
737 "failed. Then tried to call "
738 "set_alternate(USBIF_CMDONLY) = %d.\n",
739 alternate
, ret
, ret2
);
742 cam
->frame_count
= 0;
744 ret
= cpia2_usb_stream_resume(cam
);
749 /******************************************************************************
751 * cpia2_usb_stream_pause
753 *****************************************************************************/
754 int cpia2_usb_stream_pause(struct camera_data
*cam
)
759 ret
= set_alternate(cam
, USBIF_CMDONLY
);
764 /******************************************************************************
766 * cpia2_usb_stream_resume
768 *****************************************************************************/
769 int cpia2_usb_stream_resume(struct camera_data
*cam
)
773 cam
->first_image_seen
= 0;
774 ret
= set_alternate(cam
, cam
->params
.camera_state
.stream_mode
);
776 /* for some reason the user effects need to be set
777 again when starting streaming. */
778 cpia2_do_command(cam
, CPIA2_CMD_SET_USER_EFFECTS
, TRANSFER_WRITE
,
779 cam
->params
.vp_params
.user_effects
);
780 ret
= submit_urbs(cam
);
786 /******************************************************************************
788 * cpia2_usb_stream_stop
790 *****************************************************************************/
791 int cpia2_usb_stream_stop(struct camera_data
*cam
)
795 ret
= cpia2_usb_stream_pause(cam
);
797 configure_transfer_mode(cam
, 0);
801 /******************************************************************************
805 * Probe and initialize.
806 *****************************************************************************/
807 static int cpia2_usb_probe(struct usb_interface
*intf
,
808 const struct usb_device_id
*id
)
810 struct usb_device
*udev
= interface_to_usbdev(intf
);
811 struct usb_interface_descriptor
*interface
;
812 struct camera_data
*cam
;
815 /* A multi-config CPiA2 camera? */
816 if (udev
->descriptor
.bNumConfigurations
!= 1)
818 interface
= &intf
->cur_altsetting
->desc
;
820 /* If we get to this point, we found a CPiA2 camera */
821 LOG("CPiA2 USB camera found\n");
823 cam
= cpia2_init_camera_struct(intf
);
828 cam
->iface
= interface
->bInterfaceNumber
;
830 ret
= set_alternate(cam
, USBIF_CMDONLY
);
832 ERR("%s: usb_set_interface error (ret = %d)\n", __func__
, ret
);
838 if((ret
= cpia2_init_camera(cam
)) < 0) {
839 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__
, ret
);
843 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
844 cam
->params
.version
.firmware_revision_hi
,
845 cam
->params
.version
.firmware_revision_lo
,
846 cam
->params
.version
.asic_id
,
847 cam
->params
.version
.asic_rev
);
848 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
849 cam
->params
.pnp_id
.vendor
,
850 cam
->params
.pnp_id
.product
,
851 cam
->params
.pnp_id
.device_revision
);
852 LOG(" SensorID: %d.(version %d)\n",
853 cam
->params
.version
.sensor_flags
,
854 cam
->params
.version
.sensor_rev
);
856 usb_set_intfdata(intf
, cam
);
858 ret
= cpia2_register_camera(cam
);
860 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__
, ret
);
868 /******************************************************************************
872 *****************************************************************************/
873 static void cpia2_usb_disconnect(struct usb_interface
*intf
)
875 struct camera_data
*cam
= usb_get_intfdata(intf
);
876 usb_set_intfdata(intf
, NULL
);
878 DBG("Stopping stream\n");
879 cpia2_usb_stream_stop(cam
);
881 mutex_lock(&cam
->v4l2_lock
);
882 DBG("Unregistering camera\n");
883 cpia2_unregister_camera(cam
);
884 v4l2_device_disconnect(&cam
->v4l2_dev
);
885 mutex_unlock(&cam
->v4l2_lock
);
886 v4l2_device_put(&cam
->v4l2_dev
);
889 DBG("Wakeup waiting processes\n");
890 cam
->curbuff
->status
= FRAME_READY
;
891 cam
->curbuff
->length
= 0;
892 wake_up_interruptible(&cam
->wq_stream
);
895 DBG("Releasing interface\n");
896 usb_driver_release_interface(&cpia2_driver
, intf
);
898 LOG("CPiA2 camera disconnected.\n");
901 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
903 struct camera_data
*cam
= usb_get_intfdata(intf
);
905 mutex_lock(&cam
->v4l2_lock
);
906 if (cam
->streaming
) {
907 cpia2_usb_stream_stop(cam
);
910 mutex_unlock(&cam
->v4l2_lock
);
912 dev_info(&intf
->dev
, "going into suspend..\n");
916 /* Resume device - start device. */
917 static int cpia2_usb_resume(struct usb_interface
*intf
)
919 struct camera_data
*cam
= usb_get_intfdata(intf
);
921 mutex_lock(&cam
->v4l2_lock
);
922 v4l2_ctrl_handler_setup(&cam
->hdl
);
923 if (cam
->streaming
) {
925 cpia2_usb_stream_start(cam
,
926 cam
->params
.camera_state
.stream_mode
);
928 mutex_unlock(&cam
->v4l2_lock
);
930 dev_info(&intf
->dev
, "coming out of suspend..\n");
934 /******************************************************************************
938 *****************************************************************************/
939 int cpia2_usb_init(void)
941 return usb_register(&cpia2_driver
);
944 /******************************************************************************
948 *****************************************************************************/
949 void cpia2_usb_cleanup(void)
951 schedule_timeout(2 * HZ
);
952 usb_deregister(&cpia2_driver
);