1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /****************************************************************************
4 * Filename: cpia2_usb.c
6 * Copyright 2001, STMicrolectronics, Inc.
7 * Contact: steve.miller@st.com
10 * This is a USB driver for CPia2 based video cameras.
11 * The infrastructure of this driver is based on the cpia usb driver by
12 * Jochen Scharrlach and Johannes Erdfeldt.
14 * Stripped of 2.4 stuff ready for main kernel submit by
15 * Alan Cox <alan@lxorguk.ukuu.org.uk>
16 ****************************************************************************/
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/module.h>
25 static int frame_sizes
[] = {
36 #define FRAMES_PER_DESC 10
37 #define FRAME_SIZE_PER_DESC frame_sizes[cam->cur_alt]
39 static void process_frame(struct camera_data
*cam
);
40 static void cpia2_usb_complete(struct urb
*urb
);
41 static int cpia2_usb_probe(struct usb_interface
*intf
,
42 const struct usb_device_id
*id
);
43 static void cpia2_usb_disconnect(struct usb_interface
*intf
);
44 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
);
45 static int cpia2_usb_resume(struct usb_interface
*intf
);
47 static void free_sbufs(struct camera_data
*cam
);
48 static void add_APPn(struct camera_data
*cam
);
49 static void add_COM(struct camera_data
*cam
);
50 static int submit_urbs(struct camera_data
*cam
);
51 static int set_alternate(struct camera_data
*cam
, unsigned int alt
);
52 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
);
54 static const struct usb_device_id cpia2_id_table
[] = {
55 {USB_DEVICE(0x0553, 0x0100)},
56 {USB_DEVICE(0x0553, 0x0140)},
57 {USB_DEVICE(0x0553, 0x0151)}, /* STV0676 */
58 {} /* Terminating entry */
60 MODULE_DEVICE_TABLE(usb
, cpia2_id_table
);
62 static struct usb_driver cpia2_driver
= {
64 .probe
= cpia2_usb_probe
,
65 .disconnect
= cpia2_usb_disconnect
,
66 .suspend
= cpia2_usb_suspend
,
67 .resume
= cpia2_usb_resume
,
68 .reset_resume
= cpia2_usb_resume
,
69 .id_table
= cpia2_id_table
73 /******************************************************************************
77 *****************************************************************************/
78 static void process_frame(struct camera_data
*cam
)
80 static int frame_count
;
82 unsigned char *inbuff
= cam
->workbuff
->data
;
84 DBG("Processing frame #%d, current:%d\n",
85 cam
->workbuff
->num
, cam
->curbuff
->num
);
87 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
88 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
90 if ((inbuff
[0] == 0xFF) && (inbuff
[1] == 0xD8)) {
93 cam
->workbuff
->status
= FRAME_ERROR
;
94 DBG("Start of frame not found\n");
99 * Now the output buffer should have a JPEG image in it.
101 if(!cam
->first_image_seen
) {
102 /* Always skip the first image after streaming
103 * starts. It is almost certainly corrupt. */
104 cam
->first_image_seen
= 1;
105 cam
->workbuff
->status
= FRAME_EMPTY
;
108 if (cam
->workbuff
->length
> 3) {
110 cam
->workbuff
->length
< cam
->workbuff
->max_length
) {
111 /* No junk in the buffers */
112 memset(cam
->workbuff
->data
+cam
->workbuff
->length
,
113 0, cam
->workbuff
->max_length
-
114 cam
->workbuff
->length
);
116 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
117 cam
->workbuff
->status
= FRAME_READY
;
119 if(!cam
->mmapped
&& cam
->num_frames
> 2) {
120 /* During normal reading, the most recent
121 * frame will be read. If the current frame
122 * hasn't started reading yet, it will never
123 * be read, so mark it empty. If the buffer is
124 * mmapped, or we have few buffers, we need to
125 * wait for the user to free the buffer.
127 * NOTE: This is not entirely foolproof with 3
128 * buffers, but it would take an EXTREMELY
129 * overloaded system to cause problems (possible
130 * image data corruption). Basically, it would
131 * need to take more time to execute cpia2_read
132 * than it would for the camera to send
133 * cam->num_frames-2 frames before problems
136 cam
->curbuff
->status
= FRAME_EMPTY
;
138 cam
->curbuff
= cam
->workbuff
;
139 cam
->workbuff
= cam
->workbuff
->next
;
140 DBG("Changed buffers, work:%d, current:%d\n",
141 cam
->workbuff
->num
, cam
->curbuff
->num
);
144 DBG("Not enough data for an image.\n");
147 cam
->workbuff
->status
= FRAME_ERROR
;
151 /******************************************************************************
155 * Adds a user specified APPn record
156 *****************************************************************************/
157 static void add_APPn(struct camera_data
*cam
)
159 if(cam
->APP_len
> 0) {
160 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
161 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xE0+cam
->APPn
;
162 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
163 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->APP_len
+2;
164 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
165 cam
->APP_data
, cam
->APP_len
);
166 cam
->workbuff
->length
+= cam
->APP_len
;
170 /******************************************************************************
174 * Adds a user specified COM record
175 *****************************************************************************/
176 static void add_COM(struct camera_data
*cam
)
178 if(cam
->COM_len
> 0) {
179 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
180 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFE;
181 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
182 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->COM_len
+2;
183 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
184 cam
->COM_data
, cam
->COM_len
);
185 cam
->workbuff
->length
+= cam
->COM_len
;
189 /******************************************************************************
193 * callback when incoming packet is received
194 *****************************************************************************/
195 static void cpia2_usb_complete(struct urb
*urb
)
198 unsigned char *cdata
;
199 static bool frame_ready
= false;
200 struct camera_data
*cam
= (struct camera_data
*) urb
->context
;
202 if (urb
->status
!=0) {
203 if (!(urb
->status
== -ENOENT
||
204 urb
->status
== -ECONNRESET
||
205 urb
->status
== -ESHUTDOWN
))
207 DBG("urb->status = %d!\n", urb
->status
);
209 DBG("Stopping streaming\n");
213 if (!cam
->streaming
|| !video_is_registered(&cam
->vdev
)) {
214 LOG("Will now stop the streaming: streaming = %d, present=%d\n",
215 cam
->streaming
, video_is_registered(&cam
->vdev
));
222 //DBG("Collating %d packets\n", urb->number_of_packets);
223 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
224 u16 checksum
, iso_checksum
;
226 int n
= urb
->iso_frame_desc
[i
].actual_length
;
227 int st
= urb
->iso_frame_desc
[i
].status
;
229 if(cam
->workbuff
->status
== FRAME_READY
) {
230 struct framebuf
*ptr
;
231 /* Try to find an available buffer */
232 DBG("workbuff full, searching\n");
233 for (ptr
= cam
->workbuff
->next
;
234 ptr
!= cam
->workbuff
;
237 if (ptr
->status
== FRAME_EMPTY
) {
238 ptr
->status
= FRAME_READING
;
243 if (ptr
== cam
->workbuff
)
244 break; /* No READING or EMPTY buffers left */
249 if (cam
->workbuff
->status
== FRAME_EMPTY
||
250 cam
->workbuff
->status
== FRAME_ERROR
) {
251 cam
->workbuff
->status
= FRAME_READING
;
252 cam
->workbuff
->length
= 0;
255 //DBG(" Packet %d length = %d, status = %d\n", i, n, st);
256 cdata
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
259 LOG("cpia2 data error: [%d] len=%d, status = %d\n",
262 cam
->workbuff
->status
= FRAME_ERROR
;
271 checksum
+= cdata
[j
];
272 iso_checksum
= cdata
[j
] + cdata
[j
+1]*256;
273 if(checksum
!= iso_checksum
) {
274 LOG("checksum mismatch: [%d] len=%d, calculated = %x, checksum = %x\n",
275 i
, n
, (int)checksum
, (int)iso_checksum
);
277 cam
->workbuff
->status
= FRAME_ERROR
;
283 if(cam
->workbuff
->status
!= FRAME_READING
) {
284 if((0xFF == cdata
[0] && 0xD8 == cdata
[1]) ||
285 (0xD8 == cdata
[0] && 0xFF == cdata
[1] &&
287 /* frame is skipped, but increment total
288 * frame count anyway */
291 DBG("workbuff not reading, status=%d\n",
292 cam
->workbuff
->status
);
296 if (cam
->frame_size
< cam
->workbuff
->length
+ n
) {
297 ERR("buffer overflow! length: %d, n: %d\n",
298 cam
->workbuff
->length
, n
);
299 cam
->workbuff
->status
= FRAME_ERROR
;
300 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
301 cam
->workbuff
->max_length
=
302 cam
->workbuff
->length
;
306 if (cam
->workbuff
->length
== 0) {
308 if ((0xD8 == cdata
[0]) && (0xFF == cdata
[1])) {
310 } else if((0xFF == cdata
[0]) && (0xD8 == cdata
[1])
311 && (0xFF == cdata
[2])) {
314 DBG("Ignoring packet, not beginning!\n");
317 DBG("Start of frame pattern found\n");
318 cam
->workbuff
->ts
= ktime_get_ns();
319 cam
->workbuff
->seq
= cam
->frame_count
++;
320 cam
->workbuff
->data
[0] = 0xFF;
321 cam
->workbuff
->data
[1] = 0xD8;
322 cam
->workbuff
->length
= 2;
325 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
326 cdata
+data_offset
, n
-data_offset
);
327 cam
->workbuff
->length
+= n
-data_offset
;
328 } else if (cam
->workbuff
->length
> 0) {
329 memcpy(cam
->workbuff
->data
+ cam
->workbuff
->length
,
331 cam
->workbuff
->length
+= n
;
334 if ((cam
->workbuff
->length
>= 3) &&
335 (cam
->workbuff
->data
[cam
->workbuff
->length
- 3] == 0xFF) &&
336 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xD9) &&
337 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xFF)) {
339 cam
->workbuff
->data
[cam
->workbuff
->length
- 1] = 0;
340 cam
->workbuff
->length
-= 1;
341 } else if ((cam
->workbuff
->length
>= 2) &&
342 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xFF) &&
343 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xD9)) {
348 DBG("Workbuff image size = %d\n",cam
->workbuff
->length
);
353 if (waitqueue_active(&cam
->wq_stream
))
354 wake_up_interruptible(&cam
->wq_stream
);
361 if ((i
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0)
362 ERR("%s: usb_submit_urb ret %d!\n", __func__
, i
);
366 /******************************************************************************
368 * configure_transfer_mode
370 *****************************************************************************/
371 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
)
373 static unsigned char iso_regs
[8][4] = {
374 {0x00, 0x00, 0x00, 0x00},
375 {0x00, 0x00, 0x00, 0x00},
376 {0xB9, 0x00, 0x00, 0x7E},
377 {0xB9, 0x00, 0x01, 0x7E},
378 {0xB9, 0x00, 0x02, 0x7E},
379 {0xB9, 0x00, 0x02, 0xFE},
380 {0xB9, 0x00, 0x03, 0x7E},
381 {0xB9, 0x00, 0x03, 0xFD}
383 struct cpia2_command cmd
;
386 if (!video_is_registered(&cam
->vdev
))
390 * Write the isoc registers according to the alternate selected
392 cmd
.direction
= TRANSFER_WRITE
;
393 cmd
.buffer
.block_data
[0] = iso_regs
[alt
][0];
394 cmd
.buffer
.block_data
[1] = iso_regs
[alt
][1];
395 cmd
.buffer
.block_data
[2] = iso_regs
[alt
][2];
396 cmd
.buffer
.block_data
[3] = iso_regs
[alt
][3];
397 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
398 cmd
.start
= CPIA2_VC_USB_ISOLIM
;
400 cpia2_send_command(cam
, &cmd
);
403 * Enable relevant streams before starting polling.
404 * First read USB Stream Config Register.
406 cmd
.direction
= TRANSFER_READ
;
407 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
408 cmd
.start
= CPIA2_VC_USB_STRM
;
410 cpia2_send_command(cam
, &cmd
);
411 reg
= cmd
.buffer
.block_data
[0];
413 /* Clear iso, bulk, and int */
414 reg
&= ~(CPIA2_VC_USB_STRM_BLK_ENABLE
|
415 CPIA2_VC_USB_STRM_ISO_ENABLE
|
416 CPIA2_VC_USB_STRM_INT_ENABLE
);
418 if (alt
== USBIF_BULK
) {
419 DBG("Enabling bulk xfer\n");
420 reg
|= CPIA2_VC_USB_STRM_BLK_ENABLE
; /* Enable Bulk */
421 cam
->xfer_mode
= XFER_BULK
;
422 } else if (alt
>= USBIF_ISO_1
) {
423 DBG("Enabling ISOC xfer\n");
424 reg
|= CPIA2_VC_USB_STRM_ISO_ENABLE
;
425 cam
->xfer_mode
= XFER_ISOC
;
428 cmd
.buffer
.block_data
[0] = reg
;
429 cmd
.direction
= TRANSFER_WRITE
;
430 cmd
.start
= CPIA2_VC_USB_STRM
;
432 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
433 cpia2_send_command(cam
, &cmd
);
438 /******************************************************************************
440 * cpia2_usb_change_streaming_alternate
442 *****************************************************************************/
443 int cpia2_usb_change_streaming_alternate(struct camera_data
*cam
,
448 if(alt
< USBIF_ISO_1
|| alt
> USBIF_ISO_6
)
451 if(alt
== cam
->params
.camera_state
.stream_mode
)
454 cpia2_usb_stream_pause(cam
);
456 configure_transfer_mode(cam
, alt
);
458 cam
->params
.camera_state
.stream_mode
= alt
;
460 /* Reset the camera to prevent image quality degradation */
461 cpia2_reset_camera(cam
);
463 cpia2_usb_stream_resume(cam
);
468 /******************************************************************************
472 *****************************************************************************/
473 static int set_alternate(struct camera_data
*cam
, unsigned int alt
)
477 if(alt
== cam
->cur_alt
)
480 if (cam
->cur_alt
!= USBIF_CMDONLY
) {
481 DBG("Changing from alt %d to %d\n", cam
->cur_alt
, USBIF_CMDONLY
);
482 ret
= usb_set_interface(cam
->dev
, cam
->iface
, USBIF_CMDONLY
);
486 if (alt
!= USBIF_CMDONLY
) {
487 DBG("Changing from alt %d to %d\n", USBIF_CMDONLY
, alt
);
488 ret
= usb_set_interface(cam
->dev
, cam
->iface
, alt
);
493 cam
->old_alt
= cam
->cur_alt
;
499 /******************************************************************************
503 * Free all cam->sbuf[]. All non-NULL .data and .urb members that are non-NULL
504 * are assumed to be allocated. Non-NULL .urb members are also assumed to be
505 * submitted (and must therefore be killed before they are freed).
506 *****************************************************************************/
507 static void free_sbufs(struct camera_data
*cam
)
511 for (i
= 0; i
< NUM_SBUF
; i
++) {
512 if(cam
->sbuf
[i
].urb
) {
513 usb_kill_urb(cam
->sbuf
[i
].urb
);
514 usb_free_urb(cam
->sbuf
[i
].urb
);
515 cam
->sbuf
[i
].urb
= NULL
;
517 if(cam
->sbuf
[i
].data
) {
518 kfree(cam
->sbuf
[i
].data
);
519 cam
->sbuf
[i
].data
= NULL
;
525 * Convenience functions
527 /****************************************************************************
531 ***************************************************************************/
532 static int write_packet(struct usb_device
*udev
,
533 u8 request
, u8
* registers
, u16 start
, size_t size
)
538 if (!registers
|| size
<= 0)
541 buf
= kmemdup(registers
, size
, GFP_KERNEL
);
545 ret
= usb_control_msg(udev
,
546 usb_sndctrlpipe(udev
, 0),
548 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
559 /****************************************************************************
563 ***************************************************************************/
564 static int read_packet(struct usb_device
*udev
,
565 u8 request
, u8
* registers
, u16 start
, size_t size
)
570 if (!registers
|| size
<= 0)
573 buf
= kmalloc(size
, GFP_KERNEL
);
577 ret
= usb_control_msg(udev
,
578 usb_rcvctrlpipe(udev
, 0),
580 USB_DIR_IN
|USB_TYPE_VENDOR
|USB_RECIP_DEVICE
,
588 memcpy(registers
, buf
, size
);
595 /******************************************************************************
597 * cpia2_usb_transfer_cmd
599 *****************************************************************************/
600 int cpia2_usb_transfer_cmd(struct camera_data
*cam
,
602 u8 request
, u8 start
, u8 count
, u8 direction
)
605 struct usb_device
*udev
= cam
->dev
;
608 ERR("%s: Internal driver error: udev is NULL\n", __func__
);
613 ERR("%s: Internal driver error: register array is NULL\n", __func__
);
617 if (direction
== TRANSFER_READ
) {
618 err
= read_packet(udev
, request
, (u8
*)registers
, start
, count
);
621 } else if (direction
== TRANSFER_WRITE
) {
622 err
=write_packet(udev
, request
, (u8
*)registers
, start
, count
);
624 LOG("Control message failed, err val = %d\n", err
);
625 LOG("Message: request = 0x%0X, start = 0x%0X\n",
627 LOG("Message: count = %d, register[0] = 0x%0X\n",
628 count
, ((unsigned char *) registers
)[0]);
632 LOG("Unexpected first byte of direction: %d\n",
638 LOG("Unexpected error: %d\n", err
);
643 /******************************************************************************
647 *****************************************************************************/
648 static int submit_urbs(struct camera_data
*cam
)
653 for(i
=0; i
<NUM_SBUF
; ++i
) {
654 if (cam
->sbuf
[i
].data
)
657 kmalloc_array(FRAME_SIZE_PER_DESC
, FRAMES_PER_DESC
,
659 if (!cam
->sbuf
[i
].data
) {
661 kfree(cam
->sbuf
[i
].data
);
662 cam
->sbuf
[i
].data
= NULL
;
668 /* We double buffer the Isoc lists, and also know the polling
669 * interval is every frame (1 == (1 << (bInterval -1))).
671 for(i
=0; i
<NUM_SBUF
; ++i
) {
672 if(cam
->sbuf
[i
].urb
) {
675 urb
= usb_alloc_urb(FRAMES_PER_DESC
, GFP_KERNEL
);
677 for (j
= 0; j
< i
; j
++)
678 usb_free_urb(cam
->sbuf
[j
].urb
);
679 for (j
= 0; j
< NUM_SBUF
; j
++) {
680 kfree(cam
->sbuf
[j
].data
);
681 cam
->sbuf
[j
].data
= NULL
;
686 cam
->sbuf
[i
].urb
= urb
;
689 urb
->pipe
= usb_rcvisocpipe(cam
->dev
, 1 /*ISOC endpoint*/);
690 urb
->transfer_flags
= URB_ISO_ASAP
;
691 urb
->transfer_buffer
= cam
->sbuf
[i
].data
;
692 urb
->complete
= cpia2_usb_complete
;
693 urb
->number_of_packets
= FRAMES_PER_DESC
;
695 urb
->transfer_buffer_length
=
696 FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
698 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
699 urb
->iso_frame_desc
[fx
].offset
=
700 FRAME_SIZE_PER_DESC
* fx
;
701 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
706 /* Queue the ISO urbs, and resubmit in the completion handler */
707 for(i
=0; i
<NUM_SBUF
; ++i
) {
708 err
= usb_submit_urb(cam
->sbuf
[i
].urb
, GFP_KERNEL
);
710 ERR("usb_submit_urb[%d]() = %d\n", i
, err
);
718 /******************************************************************************
720 * cpia2_usb_stream_start
722 *****************************************************************************/
723 int cpia2_usb_stream_start(struct camera_data
*cam
, unsigned int alternate
)
733 DBG("Flushing buffers\n");
734 for(i
=0; i
<cam
->num_frames
; ++i
) {
735 cam
->buffers
[i
].status
= FRAME_EMPTY
;
736 cam
->buffers
[i
].length
= 0;
738 cam
->curbuff
= &cam
->buffers
[0];
739 cam
->workbuff
= cam
->curbuff
->next
;
743 old_alt
= cam
->params
.camera_state
.stream_mode
;
744 cam
->params
.camera_state
.stream_mode
= 0;
745 ret
= cpia2_usb_change_streaming_alternate(cam
, alternate
);
748 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret
);
749 cam
->params
.camera_state
.stream_mode
= old_alt
;
750 ret2
= set_alternate(cam
, USBIF_CMDONLY
);
752 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already failed. Then tried to call set_alternate(USBIF_CMDONLY) = %d.\n",
753 alternate
, ret
, ret2
);
756 cam
->frame_count
= 0;
758 ret
= cpia2_usb_stream_resume(cam
);
763 /******************************************************************************
765 * cpia2_usb_stream_pause
767 *****************************************************************************/
768 int cpia2_usb_stream_pause(struct camera_data
*cam
)
773 ret
= set_alternate(cam
, USBIF_CMDONLY
);
778 /******************************************************************************
780 * cpia2_usb_stream_resume
782 *****************************************************************************/
783 int cpia2_usb_stream_resume(struct camera_data
*cam
)
787 cam
->first_image_seen
= 0;
788 ret
= set_alternate(cam
, cam
->params
.camera_state
.stream_mode
);
790 /* for some reason the user effects need to be set
791 again when starting streaming. */
792 cpia2_do_command(cam
, CPIA2_CMD_SET_USER_EFFECTS
, TRANSFER_WRITE
,
793 cam
->params
.vp_params
.user_effects
);
794 ret
= submit_urbs(cam
);
800 /******************************************************************************
802 * cpia2_usb_stream_stop
804 *****************************************************************************/
805 int cpia2_usb_stream_stop(struct camera_data
*cam
)
809 ret
= cpia2_usb_stream_pause(cam
);
811 configure_transfer_mode(cam
, 0);
815 /******************************************************************************
819 * Probe and initialize.
820 *****************************************************************************/
821 static int cpia2_usb_probe(struct usb_interface
*intf
,
822 const struct usb_device_id
*id
)
824 struct usb_device
*udev
= interface_to_usbdev(intf
);
825 struct usb_interface_descriptor
*interface
;
826 struct camera_data
*cam
;
829 /* A multi-config CPiA2 camera? */
830 if (udev
->descriptor
.bNumConfigurations
!= 1)
832 interface
= &intf
->cur_altsetting
->desc
;
834 /* If we get to this point, we found a CPiA2 camera */
835 LOG("CPiA2 USB camera found\n");
837 cam
= cpia2_init_camera_struct(intf
);
842 cam
->iface
= interface
->bInterfaceNumber
;
844 ret
= set_alternate(cam
, USBIF_CMDONLY
);
846 ERR("%s: usb_set_interface error (ret = %d)\n", __func__
, ret
);
852 if((ret
= cpia2_init_camera(cam
)) < 0) {
853 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__
, ret
);
857 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
858 cam
->params
.version
.firmware_revision_hi
,
859 cam
->params
.version
.firmware_revision_lo
,
860 cam
->params
.version
.asic_id
,
861 cam
->params
.version
.asic_rev
);
862 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
863 cam
->params
.pnp_id
.vendor
,
864 cam
->params
.pnp_id
.product
,
865 cam
->params
.pnp_id
.device_revision
);
866 LOG(" SensorID: %d.(version %d)\n",
867 cam
->params
.version
.sensor_flags
,
868 cam
->params
.version
.sensor_rev
);
870 usb_set_intfdata(intf
, cam
);
872 ret
= cpia2_register_camera(cam
);
874 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__
, ret
);
882 /******************************************************************************
886 *****************************************************************************/
887 static void cpia2_usb_disconnect(struct usb_interface
*intf
)
889 struct camera_data
*cam
= usb_get_intfdata(intf
);
890 usb_set_intfdata(intf
, NULL
);
892 DBG("Stopping stream\n");
893 cpia2_usb_stream_stop(cam
);
895 mutex_lock(&cam
->v4l2_lock
);
896 DBG("Unregistering camera\n");
897 cpia2_unregister_camera(cam
);
898 v4l2_device_disconnect(&cam
->v4l2_dev
);
899 mutex_unlock(&cam
->v4l2_lock
);
902 DBG("Wakeup waiting processes\n");
903 cam
->curbuff
->status
= FRAME_READY
;
904 cam
->curbuff
->length
= 0;
905 wake_up_interruptible(&cam
->wq_stream
);
908 v4l2_device_put(&cam
->v4l2_dev
);
910 LOG("CPiA2 camera disconnected.\n");
913 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
915 struct camera_data
*cam
= usb_get_intfdata(intf
);
917 mutex_lock(&cam
->v4l2_lock
);
918 if (cam
->streaming
) {
919 cpia2_usb_stream_stop(cam
);
922 mutex_unlock(&cam
->v4l2_lock
);
924 dev_info(&intf
->dev
, "going into suspend..\n");
928 /* Resume device - start device. */
929 static int cpia2_usb_resume(struct usb_interface
*intf
)
931 struct camera_data
*cam
= usb_get_intfdata(intf
);
933 mutex_lock(&cam
->v4l2_lock
);
934 v4l2_ctrl_handler_setup(&cam
->hdl
);
935 if (cam
->streaming
) {
937 cpia2_usb_stream_start(cam
,
938 cam
->params
.camera_state
.stream_mode
);
940 mutex_unlock(&cam
->v4l2_lock
);
942 dev_info(&intf
->dev
, "coming out of suspend..\n");
946 /******************************************************************************
950 *****************************************************************************/
951 int cpia2_usb_init(void)
953 return usb_register(&cpia2_driver
);
956 /******************************************************************************
960 *****************************************************************************/
961 void cpia2_usb_cleanup(void)
963 schedule_timeout(2 * HZ
);
964 usb_deregister(&cpia2_driver
);