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 * Stripped of 2.4 stuff ready for main kernel submit by
24 * Alan Cox <alan@lxorguk.ukuu.org.uk>
25 ****************************************************************************/
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/usb.h>
30 #include <linux/module.h>
34 static int frame_sizes
[] = {
45 #define FRAMES_PER_DESC 10
46 #define FRAME_SIZE_PER_DESC frame_sizes[cam->cur_alt]
48 static void process_frame(struct camera_data
*cam
);
49 static void cpia2_usb_complete(struct urb
*urb
);
50 static int cpia2_usb_probe(struct usb_interface
*intf
,
51 const struct usb_device_id
*id
);
52 static void cpia2_usb_disconnect(struct usb_interface
*intf
);
53 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
);
54 static int cpia2_usb_resume(struct usb_interface
*intf
);
56 static void free_sbufs(struct camera_data
*cam
);
57 static void add_APPn(struct camera_data
*cam
);
58 static void add_COM(struct camera_data
*cam
);
59 static int submit_urbs(struct camera_data
*cam
);
60 static int set_alternate(struct camera_data
*cam
, unsigned int alt
);
61 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
);
63 static const struct usb_device_id cpia2_id_table
[] = {
64 {USB_DEVICE(0x0553, 0x0100)},
65 {USB_DEVICE(0x0553, 0x0140)},
66 {USB_DEVICE(0x0553, 0x0151)}, /* STV0676 */
67 {} /* Terminating entry */
69 MODULE_DEVICE_TABLE(usb
, cpia2_id_table
);
71 static struct usb_driver cpia2_driver
= {
73 .probe
= cpia2_usb_probe
,
74 .disconnect
= cpia2_usb_disconnect
,
75 .suspend
= cpia2_usb_suspend
,
76 .resume
= cpia2_usb_resume
,
77 .reset_resume
= cpia2_usb_resume
,
78 .id_table
= cpia2_id_table
82 /******************************************************************************
86 *****************************************************************************/
87 static void process_frame(struct camera_data
*cam
)
89 static int frame_count
;
91 unsigned char *inbuff
= cam
->workbuff
->data
;
93 DBG("Processing frame #%d, current:%d\n",
94 cam
->workbuff
->num
, cam
->curbuff
->num
);
96 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
97 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
99 if ((inbuff
[0] == 0xFF) && (inbuff
[1] == 0xD8)) {
102 cam
->workbuff
->status
= FRAME_ERROR
;
103 DBG("Start of frame not found\n");
108 * Now the output buffer should have a JPEG image in it.
110 if(!cam
->first_image_seen
) {
111 /* Always skip the first image after streaming
112 * starts. It is almost certainly corrupt. */
113 cam
->first_image_seen
= 1;
114 cam
->workbuff
->status
= FRAME_EMPTY
;
117 if (cam
->workbuff
->length
> 3) {
119 cam
->workbuff
->length
< cam
->workbuff
->max_length
) {
120 /* No junk in the buffers */
121 memset(cam
->workbuff
->data
+cam
->workbuff
->length
,
122 0, cam
->workbuff
->max_length
-
123 cam
->workbuff
->length
);
125 cam
->workbuff
->max_length
= cam
->workbuff
->length
;
126 cam
->workbuff
->status
= FRAME_READY
;
128 if(!cam
->mmapped
&& cam
->num_frames
> 2) {
129 /* During normal reading, the most recent
130 * frame will be read. If the current frame
131 * hasn't started reading yet, it will never
132 * be read, so mark it empty. If the buffer is
133 * mmapped, or we have few buffers, we need to
134 * wait for the user to free the buffer.
136 * NOTE: This is not entirely foolproof with 3
137 * buffers, but it would take an EXTREMELY
138 * overloaded system to cause problems (possible
139 * image data corruption). Basically, it would
140 * need to take more time to execute cpia2_read
141 * than it would for the camera to send
142 * cam->num_frames-2 frames before problems
145 cam
->curbuff
->status
= FRAME_EMPTY
;
147 cam
->curbuff
= cam
->workbuff
;
148 cam
->workbuff
= cam
->workbuff
->next
;
149 DBG("Changed buffers, work:%d, current:%d\n",
150 cam
->workbuff
->num
, cam
->curbuff
->num
);
153 DBG("Not enough data for an image.\n");
156 cam
->workbuff
->status
= FRAME_ERROR
;
160 /******************************************************************************
164 * Adds a user specified APPn record
165 *****************************************************************************/
166 static void add_APPn(struct camera_data
*cam
)
168 if(cam
->APP_len
> 0) {
169 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
170 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xE0+cam
->APPn
;
171 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
172 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->APP_len
+2;
173 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
174 cam
->APP_data
, cam
->APP_len
);
175 cam
->workbuff
->length
+= cam
->APP_len
;
179 /******************************************************************************
183 * Adds a user specified COM record
184 *****************************************************************************/
185 static void add_COM(struct camera_data
*cam
)
187 if(cam
->COM_len
> 0) {
188 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFF;
189 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0xFE;
190 cam
->workbuff
->data
[cam
->workbuff
->length
++] = 0;
191 cam
->workbuff
->data
[cam
->workbuff
->length
++] = cam
->COM_len
+2;
192 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
193 cam
->COM_data
, cam
->COM_len
);
194 cam
->workbuff
->length
+= cam
->COM_len
;
198 /******************************************************************************
202 * callback when incoming packet is received
203 *****************************************************************************/
204 static void cpia2_usb_complete(struct urb
*urb
)
207 unsigned char *cdata
;
208 static bool frame_ready
= false;
209 struct camera_data
*cam
= (struct camera_data
*) urb
->context
;
211 if (urb
->status
!=0) {
212 if (!(urb
->status
== -ENOENT
||
213 urb
->status
== -ECONNRESET
||
214 urb
->status
== -ESHUTDOWN
))
216 DBG("urb->status = %d!\n", urb
->status
);
218 DBG("Stopping streaming\n");
222 if (!cam
->streaming
|| !video_is_registered(&cam
->vdev
)) {
223 LOG("Will now stop the streaming: streaming = %d, present=%d\n",
224 cam
->streaming
, video_is_registered(&cam
->vdev
));
231 //DBG("Collating %d packets\n", urb->number_of_packets);
232 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
233 u16 checksum
, iso_checksum
;
235 int n
= urb
->iso_frame_desc
[i
].actual_length
;
236 int st
= urb
->iso_frame_desc
[i
].status
;
238 if(cam
->workbuff
->status
== FRAME_READY
) {
239 struct framebuf
*ptr
;
240 /* Try to find an available buffer */
241 DBG("workbuff full, searching\n");
242 for (ptr
= cam
->workbuff
->next
;
243 ptr
!= cam
->workbuff
;
246 if (ptr
->status
== FRAME_EMPTY
) {
247 ptr
->status
= FRAME_READING
;
252 if (ptr
== cam
->workbuff
)
253 break; /* No READING or EMPTY buffers left */
258 if (cam
->workbuff
->status
== FRAME_EMPTY
||
259 cam
->workbuff
->status
== FRAME_ERROR
) {
260 cam
->workbuff
->status
= FRAME_READING
;
261 cam
->workbuff
->length
= 0;
264 //DBG(" Packet %d length = %d, status = %d\n", i, n, st);
265 cdata
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
268 LOG("cpia2 data error: [%d] len=%d, status = %d\n",
271 cam
->workbuff
->status
= FRAME_ERROR
;
280 checksum
+= cdata
[j
];
281 iso_checksum
= cdata
[j
] + cdata
[j
+1]*256;
282 if(checksum
!= iso_checksum
) {
283 LOG("checksum mismatch: [%d] len=%d, calculated = %x, checksum = %x\n",
284 i
, n
, (int)checksum
, (int)iso_checksum
);
286 cam
->workbuff
->status
= FRAME_ERROR
;
292 if(cam
->workbuff
->status
!= FRAME_READING
) {
293 if((0xFF == cdata
[0] && 0xD8 == cdata
[1]) ||
294 (0xD8 == cdata
[0] && 0xFF == cdata
[1] &&
296 /* frame is skipped, but increment total
297 * frame count anyway */
300 DBG("workbuff not reading, status=%d\n",
301 cam
->workbuff
->status
);
305 if (cam
->frame_size
< cam
->workbuff
->length
+ n
) {
306 ERR("buffer overflow! length: %d, n: %d\n",
307 cam
->workbuff
->length
, n
);
308 cam
->workbuff
->status
= FRAME_ERROR
;
309 if(cam
->workbuff
->length
> cam
->workbuff
->max_length
)
310 cam
->workbuff
->max_length
=
311 cam
->workbuff
->length
;
315 if (cam
->workbuff
->length
== 0) {
317 if ((0xD8 == cdata
[0]) && (0xFF == cdata
[1])) {
319 } else if((0xFF == cdata
[0]) && (0xD8 == cdata
[1])
320 && (0xFF == cdata
[2])) {
323 DBG("Ignoring packet, not beginning!\n");
326 DBG("Start of frame pattern found\n");
327 v4l2_get_timestamp(&cam
->workbuff
->timestamp
);
328 cam
->workbuff
->seq
= cam
->frame_count
++;
329 cam
->workbuff
->data
[0] = 0xFF;
330 cam
->workbuff
->data
[1] = 0xD8;
331 cam
->workbuff
->length
= 2;
334 memcpy(cam
->workbuff
->data
+cam
->workbuff
->length
,
335 cdata
+data_offset
, n
-data_offset
);
336 cam
->workbuff
->length
+= n
-data_offset
;
337 } else if (cam
->workbuff
->length
> 0) {
338 memcpy(cam
->workbuff
->data
+ cam
->workbuff
->length
,
340 cam
->workbuff
->length
+= n
;
343 if ((cam
->workbuff
->length
>= 3) &&
344 (cam
->workbuff
->data
[cam
->workbuff
->length
- 3] == 0xFF) &&
345 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xD9) &&
346 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xFF)) {
348 cam
->workbuff
->data
[cam
->workbuff
->length
- 1] = 0;
349 cam
->workbuff
->length
-= 1;
350 } else if ((cam
->workbuff
->length
>= 2) &&
351 (cam
->workbuff
->data
[cam
->workbuff
->length
- 2] == 0xFF) &&
352 (cam
->workbuff
->data
[cam
->workbuff
->length
- 1] == 0xD9)) {
357 DBG("Workbuff image size = %d\n",cam
->workbuff
->length
);
362 if (waitqueue_active(&cam
->wq_stream
))
363 wake_up_interruptible(&cam
->wq_stream
);
370 if ((i
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0)
371 ERR("%s: usb_submit_urb ret %d!\n", __func__
, i
);
375 /******************************************************************************
377 * configure_transfer_mode
379 *****************************************************************************/
380 static int configure_transfer_mode(struct camera_data
*cam
, unsigned int alt
)
382 static unsigned char iso_regs
[8][4] = {
383 {0x00, 0x00, 0x00, 0x00},
384 {0x00, 0x00, 0x00, 0x00},
385 {0xB9, 0x00, 0x00, 0x7E},
386 {0xB9, 0x00, 0x01, 0x7E},
387 {0xB9, 0x00, 0x02, 0x7E},
388 {0xB9, 0x00, 0x02, 0xFE},
389 {0xB9, 0x00, 0x03, 0x7E},
390 {0xB9, 0x00, 0x03, 0xFD}
392 struct cpia2_command cmd
;
395 if (!video_is_registered(&cam
->vdev
))
399 * Write the isoc registers according to the alternate selected
401 cmd
.direction
= TRANSFER_WRITE
;
402 cmd
.buffer
.block_data
[0] = iso_regs
[alt
][0];
403 cmd
.buffer
.block_data
[1] = iso_regs
[alt
][1];
404 cmd
.buffer
.block_data
[2] = iso_regs
[alt
][2];
405 cmd
.buffer
.block_data
[3] = iso_regs
[alt
][3];
406 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
407 cmd
.start
= CPIA2_VC_USB_ISOLIM
;
409 cpia2_send_command(cam
, &cmd
);
412 * Enable relevant streams before starting polling.
413 * First read USB Stream Config Register.
415 cmd
.direction
= TRANSFER_READ
;
416 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
417 cmd
.start
= CPIA2_VC_USB_STRM
;
419 cpia2_send_command(cam
, &cmd
);
420 reg
= cmd
.buffer
.block_data
[0];
422 /* Clear iso, bulk, and int */
423 reg
&= ~(CPIA2_VC_USB_STRM_BLK_ENABLE
|
424 CPIA2_VC_USB_STRM_ISO_ENABLE
|
425 CPIA2_VC_USB_STRM_INT_ENABLE
);
427 if (alt
== USBIF_BULK
) {
428 DBG("Enabling bulk xfer\n");
429 reg
|= CPIA2_VC_USB_STRM_BLK_ENABLE
; /* Enable Bulk */
430 cam
->xfer_mode
= XFER_BULK
;
431 } else if (alt
>= USBIF_ISO_1
) {
432 DBG("Enabling ISOC xfer\n");
433 reg
|= CPIA2_VC_USB_STRM_ISO_ENABLE
;
434 cam
->xfer_mode
= XFER_ISOC
;
437 cmd
.buffer
.block_data
[0] = reg
;
438 cmd
.direction
= TRANSFER_WRITE
;
439 cmd
.start
= CPIA2_VC_USB_STRM
;
441 cmd
.req_mode
= CAMERAACCESS_TYPE_BLOCK
| CAMERAACCESS_VC
;
442 cpia2_send_command(cam
, &cmd
);
447 /******************************************************************************
449 * cpia2_usb_change_streaming_alternate
451 *****************************************************************************/
452 int cpia2_usb_change_streaming_alternate(struct camera_data
*cam
,
457 if(alt
< USBIF_ISO_1
|| alt
> USBIF_ISO_6
)
460 if(alt
== cam
->params
.camera_state
.stream_mode
)
463 cpia2_usb_stream_pause(cam
);
465 configure_transfer_mode(cam
, alt
);
467 cam
->params
.camera_state
.stream_mode
= alt
;
469 /* Reset the camera to prevent image quality degradation */
470 cpia2_reset_camera(cam
);
472 cpia2_usb_stream_resume(cam
);
477 /******************************************************************************
481 *****************************************************************************/
482 static int set_alternate(struct camera_data
*cam
, unsigned int alt
)
486 if(alt
== cam
->cur_alt
)
489 if (cam
->cur_alt
!= USBIF_CMDONLY
) {
490 DBG("Changing from alt %d to %d\n", cam
->cur_alt
, USBIF_CMDONLY
);
491 ret
= usb_set_interface(cam
->dev
, cam
->iface
, USBIF_CMDONLY
);
495 if (alt
!= USBIF_CMDONLY
) {
496 DBG("Changing from alt %d to %d\n", USBIF_CMDONLY
, alt
);
497 ret
= usb_set_interface(cam
->dev
, cam
->iface
, alt
);
502 cam
->old_alt
= cam
->cur_alt
;
508 /******************************************************************************
512 * Free all cam->sbuf[]. All non-NULL .data and .urb members that are non-NULL
513 * are assumed to be allocated. Non-NULL .urb members are also assumed to be
514 * submitted (and must therefore be killed before they are freed).
515 *****************************************************************************/
516 static void free_sbufs(struct camera_data
*cam
)
520 for (i
= 0; i
< NUM_SBUF
; i
++) {
521 if(cam
->sbuf
[i
].urb
) {
522 usb_kill_urb(cam
->sbuf
[i
].urb
);
523 usb_free_urb(cam
->sbuf
[i
].urb
);
524 cam
->sbuf
[i
].urb
= NULL
;
526 if(cam
->sbuf
[i
].data
) {
527 kfree(cam
->sbuf
[i
].data
);
528 cam
->sbuf
[i
].data
= NULL
;
534 * Convenience functions
536 /****************************************************************************
540 ***************************************************************************/
541 static int write_packet(struct usb_device
*udev
,
542 u8 request
, u8
* registers
, u16 start
, size_t size
)
547 if (!registers
|| size
<= 0)
550 buf
= kmemdup(registers
, size
, GFP_KERNEL
);
554 ret
= usb_control_msg(udev
,
555 usb_sndctrlpipe(udev
, 0),
557 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
568 /****************************************************************************
572 ***************************************************************************/
573 static int read_packet(struct usb_device
*udev
,
574 u8 request
, u8
* registers
, u16 start
, size_t size
)
579 if (!registers
|| size
<= 0)
582 buf
= kmalloc(size
, GFP_KERNEL
);
586 ret
= usb_control_msg(udev
,
587 usb_rcvctrlpipe(udev
, 0),
589 USB_DIR_IN
|USB_TYPE_VENDOR
|USB_RECIP_DEVICE
,
597 memcpy(registers
, buf
, size
);
604 /******************************************************************************
606 * cpia2_usb_transfer_cmd
608 *****************************************************************************/
609 int cpia2_usb_transfer_cmd(struct camera_data
*cam
,
611 u8 request
, u8 start
, u8 count
, u8 direction
)
614 struct usb_device
*udev
= cam
->dev
;
617 ERR("%s: Internal driver error: udev is NULL\n", __func__
);
622 ERR("%s: Internal driver error: register array is NULL\n", __func__
);
626 if (direction
== TRANSFER_READ
) {
627 err
= read_packet(udev
, request
, (u8
*)registers
, start
, count
);
630 } else if (direction
== TRANSFER_WRITE
) {
631 err
=write_packet(udev
, request
, (u8
*)registers
, start
, count
);
633 LOG("Control message failed, err val = %d\n", err
);
634 LOG("Message: request = 0x%0X, start = 0x%0X\n",
636 LOG("Message: count = %d, register[0] = 0x%0X\n",
637 count
, ((unsigned char *) registers
)[0]);
641 LOG("Unexpected first byte of direction: %d\n",
647 LOG("Unexpected error: %d\n", err
);
652 /******************************************************************************
656 *****************************************************************************/
657 static int submit_urbs(struct camera_data
*cam
)
662 for(i
=0; i
<NUM_SBUF
; ++i
) {
663 if (cam
->sbuf
[i
].data
)
666 kmalloc_array(FRAME_SIZE_PER_DESC
, FRAMES_PER_DESC
,
668 if (!cam
->sbuf
[i
].data
) {
670 kfree(cam
->sbuf
[i
].data
);
671 cam
->sbuf
[i
].data
= NULL
;
677 /* We double buffer the Isoc lists, and also know the polling
678 * interval is every frame (1 == (1 << (bInterval -1))).
680 for(i
=0; i
<NUM_SBUF
; ++i
) {
681 if(cam
->sbuf
[i
].urb
) {
684 urb
= usb_alloc_urb(FRAMES_PER_DESC
, GFP_KERNEL
);
686 for (j
= 0; j
< i
; j
++)
687 usb_free_urb(cam
->sbuf
[j
].urb
);
691 cam
->sbuf
[i
].urb
= urb
;
694 urb
->pipe
= usb_rcvisocpipe(cam
->dev
, 1 /*ISOC endpoint*/);
695 urb
->transfer_flags
= URB_ISO_ASAP
;
696 urb
->transfer_buffer
= cam
->sbuf
[i
].data
;
697 urb
->complete
= cpia2_usb_complete
;
698 urb
->number_of_packets
= FRAMES_PER_DESC
;
700 urb
->transfer_buffer_length
=
701 FRAME_SIZE_PER_DESC
* FRAMES_PER_DESC
;
703 for (fx
= 0; fx
< FRAMES_PER_DESC
; fx
++) {
704 urb
->iso_frame_desc
[fx
].offset
=
705 FRAME_SIZE_PER_DESC
* fx
;
706 urb
->iso_frame_desc
[fx
].length
= FRAME_SIZE_PER_DESC
;
711 /* Queue the ISO urbs, and resubmit in the completion handler */
712 for(i
=0; i
<NUM_SBUF
; ++i
) {
713 err
= usb_submit_urb(cam
->sbuf
[i
].urb
, GFP_KERNEL
);
715 ERR("usb_submit_urb[%d]() = %d\n", i
, err
);
723 /******************************************************************************
725 * cpia2_usb_stream_start
727 *****************************************************************************/
728 int cpia2_usb_stream_start(struct camera_data
*cam
, unsigned int alternate
)
738 DBG("Flushing buffers\n");
739 for(i
=0; i
<cam
->num_frames
; ++i
) {
740 cam
->buffers
[i
].status
= FRAME_EMPTY
;
741 cam
->buffers
[i
].length
= 0;
743 cam
->curbuff
= &cam
->buffers
[0];
744 cam
->workbuff
= cam
->curbuff
->next
;
748 old_alt
= cam
->params
.camera_state
.stream_mode
;
749 cam
->params
.camera_state
.stream_mode
= 0;
750 ret
= cpia2_usb_change_streaming_alternate(cam
, alternate
);
753 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret
);
754 cam
->params
.camera_state
.stream_mode
= old_alt
;
755 ret2
= set_alternate(cam
, USBIF_CMDONLY
);
757 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already failed. Then tried to call set_alternate(USBIF_CMDONLY) = %d.\n",
758 alternate
, ret
, ret2
);
761 cam
->frame_count
= 0;
763 ret
= cpia2_usb_stream_resume(cam
);
768 /******************************************************************************
770 * cpia2_usb_stream_pause
772 *****************************************************************************/
773 int cpia2_usb_stream_pause(struct camera_data
*cam
)
778 ret
= set_alternate(cam
, USBIF_CMDONLY
);
783 /******************************************************************************
785 * cpia2_usb_stream_resume
787 *****************************************************************************/
788 int cpia2_usb_stream_resume(struct camera_data
*cam
)
792 cam
->first_image_seen
= 0;
793 ret
= set_alternate(cam
, cam
->params
.camera_state
.stream_mode
);
795 /* for some reason the user effects need to be set
796 again when starting streaming. */
797 cpia2_do_command(cam
, CPIA2_CMD_SET_USER_EFFECTS
, TRANSFER_WRITE
,
798 cam
->params
.vp_params
.user_effects
);
799 ret
= submit_urbs(cam
);
805 /******************************************************************************
807 * cpia2_usb_stream_stop
809 *****************************************************************************/
810 int cpia2_usb_stream_stop(struct camera_data
*cam
)
814 ret
= cpia2_usb_stream_pause(cam
);
816 configure_transfer_mode(cam
, 0);
820 /******************************************************************************
824 * Probe and initialize.
825 *****************************************************************************/
826 static int cpia2_usb_probe(struct usb_interface
*intf
,
827 const struct usb_device_id
*id
)
829 struct usb_device
*udev
= interface_to_usbdev(intf
);
830 struct usb_interface_descriptor
*interface
;
831 struct camera_data
*cam
;
834 /* A multi-config CPiA2 camera? */
835 if (udev
->descriptor
.bNumConfigurations
!= 1)
837 interface
= &intf
->cur_altsetting
->desc
;
839 /* If we get to this point, we found a CPiA2 camera */
840 LOG("CPiA2 USB camera found\n");
842 cam
= cpia2_init_camera_struct(intf
);
847 cam
->iface
= interface
->bInterfaceNumber
;
849 ret
= set_alternate(cam
, USBIF_CMDONLY
);
851 ERR("%s: usb_set_interface error (ret = %d)\n", __func__
, ret
);
857 if((ret
= cpia2_init_camera(cam
)) < 0) {
858 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__
, ret
);
862 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
863 cam
->params
.version
.firmware_revision_hi
,
864 cam
->params
.version
.firmware_revision_lo
,
865 cam
->params
.version
.asic_id
,
866 cam
->params
.version
.asic_rev
);
867 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
868 cam
->params
.pnp_id
.vendor
,
869 cam
->params
.pnp_id
.product
,
870 cam
->params
.pnp_id
.device_revision
);
871 LOG(" SensorID: %d.(version %d)\n",
872 cam
->params
.version
.sensor_flags
,
873 cam
->params
.version
.sensor_rev
);
875 usb_set_intfdata(intf
, cam
);
877 ret
= cpia2_register_camera(cam
);
879 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__
, ret
);
887 /******************************************************************************
891 *****************************************************************************/
892 static void cpia2_usb_disconnect(struct usb_interface
*intf
)
894 struct camera_data
*cam
= usb_get_intfdata(intf
);
895 usb_set_intfdata(intf
, NULL
);
897 DBG("Stopping stream\n");
898 cpia2_usb_stream_stop(cam
);
900 mutex_lock(&cam
->v4l2_lock
);
901 DBG("Unregistering camera\n");
902 cpia2_unregister_camera(cam
);
903 v4l2_device_disconnect(&cam
->v4l2_dev
);
904 mutex_unlock(&cam
->v4l2_lock
);
905 v4l2_device_put(&cam
->v4l2_dev
);
908 DBG("Wakeup waiting processes\n");
909 cam
->curbuff
->status
= FRAME_READY
;
910 cam
->curbuff
->length
= 0;
911 wake_up_interruptible(&cam
->wq_stream
);
914 LOG("CPiA2 camera disconnected.\n");
917 static int cpia2_usb_suspend(struct usb_interface
*intf
, pm_message_t message
)
919 struct camera_data
*cam
= usb_get_intfdata(intf
);
921 mutex_lock(&cam
->v4l2_lock
);
922 if (cam
->streaming
) {
923 cpia2_usb_stream_stop(cam
);
926 mutex_unlock(&cam
->v4l2_lock
);
928 dev_info(&intf
->dev
, "going into suspend..\n");
932 /* Resume device - start device. */
933 static int cpia2_usb_resume(struct usb_interface
*intf
)
935 struct camera_data
*cam
= usb_get_intfdata(intf
);
937 mutex_lock(&cam
->v4l2_lock
);
938 v4l2_ctrl_handler_setup(&cam
->hdl
);
939 if (cam
->streaming
) {
941 cpia2_usb_stream_start(cam
,
942 cam
->params
.camera_state
.stream_mode
);
944 mutex_unlock(&cam
->v4l2_lock
);
946 dev_info(&intf
->dev
, "coming out of suspend..\n");
950 /******************************************************************************
954 *****************************************************************************/
955 int cpia2_usb_init(void)
957 return usb_register(&cpia2_driver
);
960 /******************************************************************************
964 *****************************************************************************/
965 void cpia2_usb_cleanup(void)
967 schedule_timeout(2 * HZ
);
968 usb_deregister(&cpia2_driver
);