31 #include <linux/kernel.h>
32 #include <linux/slab.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]
53 static void cpia2_usb_complete(
struct urb *
urb);
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);
68 {USB_DEVICE(0x0553, 0x0100)},
69 {USB_DEVICE(0x0553, 0x0140)},
70 {USB_DEVICE(0x0553, 0x0151)},
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
95 unsigned char *inbuff = cam->
workbuff->data;
97 DBG(
"Processing frame #%d, current:%d\n",
103 if ((inbuff[0] == 0xFF) && (inbuff[1] == 0xD8)) {
107 DBG(
"Start of frame not found\n");
153 DBG(
"Changed buffers, work:%d, current:%d\n",
157 DBG(
"Not enough data for an image.\n");
208 static void cpia2_usb_complete(
struct urb *
urb)
211 unsigned char *
cdata;
212 static int frame_ready =
false;
215 if (urb->status!=0) {
216 if (!(urb->status == -
ENOENT ||
220 DBG(
"urb->status = %d!\n", urb->status);
222 DBG(
"Stopping streaming\n");
227 LOG(
"Will now stop the streaming: streaming = %d, present=%d\n",
236 for (i = 0; i < urb->number_of_packets; i++) {
239 int n = urb->iso_frame_desc[
i].actual_length;
240 int st = urb->iso_frame_desc[
i].status;
245 DBG(
"workbuff full, searching\n");
269 cdata = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
272 LOG(
"cpia2 data error: [%d] len=%d, status = %d\n",
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);
297 if((0xFF == cdata[0] && 0xD8 == cdata[1]) ||
298 (0xD8 == cdata[0] && 0xFF == cdata[1] &&
304 DBG(
"workbuff not reading, status=%d\n",
310 ERR(
"buffer overflow! length: %d, n: %d\n",
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");
339 cdata+data_offset, n-data_offset);
341 }
else if (cam->
workbuff->length > 0) {
354 }
else if ((cam->
workbuff->length >= 2) &&
361 DBG(
"Workbuff image size = %d\n",cam->
workbuff->length);
375 ERR(
"%s: usb_submit_urb ret %d!\n", __func__, i);
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}
399 if (!video_is_registered(&cam->
vdev))
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];
424 reg =
cmd.buffer.block_data[0];
432 DBG(
"Enabling bulk xfer\n");
436 DBG(
"Enabling ISOC xfer\n");
441 cmd.buffer.block_data[0] =
reg;
464 if(alt == cam->
params.camera_state.stream_mode)
469 configure_transfer_mode(cam, alt);
471 cam->
params.camera_state.stream_mode = alt;
486 static int set_alternate(
struct camera_data *cam,
unsigned int alt)
525 if(cam->
sbuf[i].urb) {
530 if(cam->
sbuf[i].data) {
545 static int write_packet(
struct usb_device *
udev,
548 if (!registers || size <= 0)
552 usb_sndctrlpipe(udev, 0),
567 static int read_packet(
struct usb_device *udev,
568 u8 request,
u8 * registers,
u16 start,
size_t size)
570 if (!registers || size <= 0)
574 usb_rcvctrlpipe(udev, 0),
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__);
607 err = read_packet(udev, request, (
u8 *)registers, start, count);
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);
643 if (cam->
sbuf[i].data)
647 if (!cam->
sbuf[i].data) {
660 if(cam->
sbuf[i].urb) {
665 ERR(
"%s: usb_alloc_urb error!\n", __func__);
666 for (j = 0; j <
i; j++)
674 urb->pipe = usb_rcvisocpipe(cam->
dev, 1 );
675 urb->transfer_flags = URB_ISO_ASAP;
676 urb->transfer_buffer = cam->
sbuf[
i].data;
677 urb->complete = cpia2_usb_complete;
680 urb->transfer_buffer_length =
684 urb->iso_frame_desc[
fx].offset =
695 ERR(
"usb_submit_urb[%d]() = %d\n", i, err);
718 DBG(
"Flushing buffers\n");
728 old_alt = cam->
params.camera_state.stream_mode;
729 cam->
params.camera_state.stream_mode = 0;
733 ERR(
"cpia2_usb_change_streaming_alternate() = %d!\n", ret);
734 cam->
params.camera_state.stream_mode = old_alt;
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);
775 ret = set_alternate(cam, cam->
params.camera_state.stream_mode);
780 cam->
params.vp_params.user_effects);
781 ret = submit_urbs(cam);
798 configure_transfer_mode(cam, 0);
811 struct usb_device *udev = interface_to_usbdev(intf);
817 if (udev->descriptor.bNumConfigurations != 1)
819 interface = &intf->cur_altsetting->desc;
822 LOG(
"CPiA2 USB camera found\n");
833 ERR(
"%s: usb_set_interface error (ret = %d)\n", __func__, ret);
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);
861 ERR(
"%s: Failed to register cpia2 camera (ret = %d)\n", __func__, ret);
877 usb_set_intfdata(intf,
NULL);
879 DBG(
"Stopping stream\n");
883 DBG(
"Unregistering camera\n");
890 DBG(
"Wakeup waiting processes\n");
897 DBG(
"Releasing interface\n");
900 LOG(
"CPiA2 camera disconnected.\n");
914 dev_info(&intf->dev,
"going into suspend..\n");
928 cam->
params.camera_state.stream_mode);
932 dev_info(&intf->dev,
"coming out of suspend..\n");
943 return usb_register(&cpia2_driver);