Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/drivers/media/usb/cpia2/cpia2_usb.c

  1 /****************************************************************************
  2  *
  3  *  Filename: cpia2_usb.c
  4  *
  5  *  Copyright 2001, STMicrolectronics, Inc.
  6  *      Contact:  steve.miller@st.com
  7  *
  8  *  Description:
  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.
 12  *
 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.
 17  *
 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.
 22  *
 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.
 26  *
 27  *  Stripped of 2.4 stuff ready for main kernel submit by
 28  *              Alan Cox <alan@lxorguk.ukuu.org.uk>
 29  ****************************************************************************/
 30 
 31 #include <linux/kernel.h>
 32 #include <linux/slab.h>
 33 #include <linux/usb.h>
 34 #include <linux/module.h>
 35 
 36 #include "cpia2.h"
 37 
 38 static int frame_sizes[] = {
 39         0,      // USBIF_CMDONLY
 40         0,      // USBIF_BULK
 41         128,    // USBIF_ISO_1
 42         384,    // USBIF_ISO_2
 43         640,    // USBIF_ISO_3
 44         768,    // USBIF_ISO_4
 45         896,    // USBIF_ISO_5
 46         1023,   // USBIF_ISO_6
 47 };
 48 
 49 #define FRAMES_PER_DESC    10
 50 #define FRAME_SIZE_PER_DESC   frame_sizes[cam->cur_alt]
 51 
 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);
 59 
 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);
 66 
 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 */
 72 };
 73 MODULE_DEVICE_TABLE(usb, cpia2_id_table);
 74 
 75 static struct usb_driver cpia2_driver = {
 76         .name           = "cpia2",
 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
 83 };
 84 
 85 
 86 /******************************************************************************
 87  *
 88  *  process_frame
 89  *
 90  *****************************************************************************/
 91 static void process_frame(struct camera_data *cam)
 92 {
 93         static int frame_count;
 94 
 95         unsigned char *inbuff = cam->workbuff->data;
 96 
 97         DBG("Processing frame #%d, current:%d\n",
 98             cam->workbuff->num, cam->curbuff->num);
 99 
100         if(cam->workbuff->length > cam->workbuff->max_length)
101                 cam->workbuff->max_length = cam->workbuff->length;
102 
103         if ((inbuff[0] == 0xFF) && (inbuff[1] == 0xD8)) {
104                 frame_count++;
105         } else {
106                 cam->workbuff->status = FRAME_ERROR;
107                 DBG("Start of frame not found\n");
108                 return;
109         }
110 
111         /***
112          * Now the output buffer should have a JPEG image in it.
113          ***/
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;
119                 return;
120         }
121         if (cam->workbuff->length > 3) {
122                 if(cam->mmapped &&
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);
128                 }
129                 cam->workbuff->max_length = cam->workbuff->length;
130                 cam->workbuff->status = FRAME_READY;
131 
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.
139                          *
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
147                          * could occur.
148                          */
149                         cam->curbuff->status = FRAME_EMPTY;
150                 }
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);
155                 return;
156         } else {
157                 DBG("Not enough data for an image.\n");
158         }
159 
160         cam->workbuff->status = FRAME_ERROR;
161         return;
162 }
163 
164 /******************************************************************************
165  *
166  *  add_APPn
167  *
168  *  Adds a user specified APPn record
169  *****************************************************************************/
170 static void add_APPn(struct camera_data *cam)
171 {
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;
180         }
181 }
182 
183 /******************************************************************************
184  *
185  *  add_COM
186  *
187  *  Adds a user specified COM record
188  *****************************************************************************/
189 static void add_COM(struct camera_data *cam)
190 {
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;
199         }
200 }
201 
202 /******************************************************************************
203  *
204  *  cpia2_usb_complete
205  *
206  *  callback when incoming packet is received
207  *****************************************************************************/
208 static void cpia2_usb_complete(struct urb *urb)
209 {
210         int i;
211         unsigned char *cdata;
212         static bool frame_ready = false;
213         struct camera_data *cam = (struct camera_data *) urb->context;
214 
215         if (urb->status!=0) {
216                 if (!(urb->status == -ENOENT ||
217                       urb->status == -ECONNRESET ||
218                       urb->status == -ESHUTDOWN))
219                 {
220                         DBG("urb->status = %d!\n", urb->status);
221                 }
222                 DBG("Stopping streaming\n");
223                 return;
224         }
225 
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));
229                 return;
230         }
231 
232         /***
233          * Packet collater
234          ***/
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;
238                 int j;
239                 int n = urb->iso_frame_desc[i].actual_length;
240                 int st = urb->iso_frame_desc[i].status;
241 
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;
248                              ptr = ptr->next)
249                         {
250                                 if (ptr->status == FRAME_EMPTY) {
251                                         ptr->status = FRAME_READING;
252                                         ptr->length = 0;
253                                         break;
254                                 }
255                         }
256                         if (ptr == cam->workbuff)
257                                 break; /* No READING or EMPTY buffers left */
258 
259                         cam->workbuff = ptr;
260                 }
261 
262                 if (cam->workbuff->status == FRAME_EMPTY ||
263                     cam->workbuff->status == FRAME_ERROR) {
264                         cam->workbuff->status = FRAME_READING;
265                         cam->workbuff->length = 0;
266                 }
267 
268                 //DBG("   Packet %d length = %d, status = %d\n", i, n, st);
269                 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
270 
271                 if (st) {
272                         LOG("cpia2 data error: [%d] len=%d, status = %d\n",
273                             i, n, st);
274                         if(!ALLOW_CORRUPT)
275                                 cam->workbuff->status = FRAME_ERROR;
276                         continue;
277                 }
278 
279                 if(n<=2)
280                         continue;
281 
282                 checksum = 0;
283                 for(j=0; j<n-2; ++j)
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);
289                         if(!ALLOW_CORRUPT) {
290                                 cam->workbuff->status = FRAME_ERROR;
291                                 continue;
292                         }
293                 }
294                 n -= 2;
295 
296                 if(cam->workbuff->status != FRAME_READING) {
297                         if((0xFF == cdata[0] && 0xD8 == cdata[1]) ||
298                            (0xD8 == cdata[0] && 0xFF == cdata[1] &&
299                             0 != cdata[2])) {
300                                 /* frame is skipped, but increment total
301                                  * frame count anyway */
302                                 cam->frame_count++;
303                         }
304                         DBG("workbuff not reading, status=%d\n",
305                             cam->workbuff->status);
306                         continue;
307                 }
308 
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;
316                         continue;
317                 }
318 
319                 if (cam->workbuff->length == 0) {
320                         int data_offset;
321                         if ((0xD8 == cdata[0]) && (0xFF == cdata[1])) {
322                                 data_offset = 1;
323                         } else if((0xFF == cdata[0]) && (0xD8 == cdata[1])
324                                   && (0xFF == cdata[2])) {
325                                 data_offset = 2;
326                         } else {
327                                 DBG("Ignoring packet, not beginning!\n");
328                                 continue;
329                         }
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;
336                         add_APPn(cam);
337                         add_COM(cam);
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,
343                                cdata, n);
344                         cam->workbuff->length += n;
345                 }
346 
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)) {
351                         frame_ready = true;
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)) {
357                         frame_ready = true;
358                 }
359 
360                 if (frame_ready) {
361                         DBG("Workbuff image size = %d\n",cam->workbuff->length);
362                         process_frame(cam);
363 
364                         frame_ready = false;
365 
366                         if (waitqueue_active(&cam->wq_stream))
367                                 wake_up_interruptible(&cam->wq_stream);
368                 }
369         }
370 
371         if(cam->streaming) {
372                 /* resubmit */
373                 urb->dev = cam->dev;
374                 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
375                         ERR("%s: usb_submit_urb ret %d!\n", __func__, i);
376         }
377 }
378 
379 /******************************************************************************
380  *
381  * configure_transfer_mode
382  *
383  *****************************************************************************/
384 static int configure_transfer_mode(struct camera_data *cam, unsigned int alt)
385 {
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}
395         };
396         struct cpia2_command cmd;
397         unsigned char reg;
398 
399         if (!video_is_registered(&cam->vdev))
400                 return -ENODEV;
401 
402         /***
403          * Write the isoc registers according to the alternate selected
404          ***/
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;
412         cmd.reg_count = 4;
413         cpia2_send_command(cam, &cmd);
414 
415         /***
416          * Enable relevant streams before starting polling.
417          * First read USB Stream Config Register.
418          ***/
419         cmd.direction = TRANSFER_READ;
420         cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
421         cmd.start = CPIA2_VC_USB_STRM;
422         cmd.reg_count = 1;
423         cpia2_send_command(cam, &cmd);
424         reg = cmd.buffer.block_data[0];
425 
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);
430 
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;
439         }
440 
441         cmd.buffer.block_data[0] = reg;
442         cmd.direction = TRANSFER_WRITE;
443         cmd.start = CPIA2_VC_USB_STRM;
444         cmd.reg_count = 1;
445         cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
446         cpia2_send_command(cam, &cmd);
447 
448         return 0;
449 }
450 
451 /******************************************************************************
452  *
453  * cpia2_usb_change_streaming_alternate
454  *
455  *****************************************************************************/
456 int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
457                                          unsigned int alt)
458 {
459         int ret = 0;
460 
461         if(alt < USBIF_ISO_1 || alt > USBIF_ISO_6)
462                 return -EINVAL;
463 
464         if(alt == cam->params.camera_state.stream_mode)
465                 return 0;
466 
467         cpia2_usb_stream_pause(cam);
468 
469         configure_transfer_mode(cam, alt);
470 
471         cam->params.camera_state.stream_mode = alt;
472 
473         /* Reset the camera to prevent image quality degradation */
474         cpia2_reset_camera(cam);
475 
476         cpia2_usb_stream_resume(cam);
477 
478         return ret;
479 }
480 
481 /******************************************************************************
482  *
483  * set_alternate
484  *
485  *****************************************************************************/
486 static int set_alternate(struct camera_data *cam, unsigned int alt)
487 {
488         int ret = 0;
489 
490         if(alt == cam->cur_alt)
491                 return 0;
492 
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);
496                 if (ret != 0)
497                         return ret;
498         }
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);
502                 if (ret != 0)
503                         return ret;
504         }
505 
506         cam->old_alt = cam->cur_alt;
507         cam->cur_alt = alt;
508 
509         return ret;
510 }
511 
512 /******************************************************************************
513  *
514  * free_sbufs
515  *
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)
521 {
522         int i;
523 
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;
529                 }
530                 if(cam->sbuf[i].data) {
531                         kfree(cam->sbuf[i].data);
532                         cam->sbuf[i].data = NULL;
533                 }
534         }
535 }
536 
537 /*******
538 * Convenience functions
539 *******/
540 /****************************************************************************
541  *
542  *  write_packet
543  *
544  ***************************************************************************/
545 static int write_packet(struct usb_device *udev,
546                         u8 request, u8 * registers, u16 start, size_t size)
547 {
548         unsigned char *buf;
549         int ret;
550 
551         if (!registers || size <= 0)
552                 return -EINVAL;
553 
554         buf = kmalloc(size, GFP_KERNEL);
555         if (!buf)
556                 return -ENOMEM;
557 
558         memcpy(buf, registers, size);
559 
560         ret = usb_control_msg(udev,
561                                usb_sndctrlpipe(udev, 0),
562                                request,
563                                USB_TYPE_VENDOR | USB_RECIP_DEVICE,
564                                start,   /* value */
565                                0,       /* index */
566                                buf,     /* buffer */
567                                size,
568                                HZ);
569 
570         kfree(buf);
571         return ret;
572 }
573 
574 /****************************************************************************
575  *
576  *  read_packet
577  *
578  ***************************************************************************/
579 static int read_packet(struct usb_device *udev,
580                        u8 request, u8 * registers, u16 start, size_t size)
581 {
582         unsigned char *buf;
583         int ret;
584 
585         if (!registers || size <= 0)
586                 return -EINVAL;
587 
588         buf = kmalloc(size, GFP_KERNEL);
589         if (!buf)
590                 return -ENOMEM;
591 
592         ret = usb_control_msg(udev,
593                                usb_rcvctrlpipe(udev, 0),
594                                request,
595                                USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
596                                start,   /* value */
597                                0,       /* index */
598                                buf,     /* buffer */
599                                size,
600                                HZ);
601 
602         if (ret >= 0)
603                 memcpy(registers, buf, size);
604 
605         kfree(buf);
606 
607         return ret;
608 }
609 
610 /******************************************************************************
611  *
612  *  cpia2_usb_transfer_cmd
613  *
614  *****************************************************************************/
615 int cpia2_usb_transfer_cmd(struct camera_data *cam,
616                            void *registers,
617                            u8 request, u8 start, u8 count, u8 direction)
618 {
619         int err = 0;
620         struct usb_device *udev = cam->dev;
621 
622         if (!udev) {
623                 ERR("%s: Internal driver error: udev is NULL\n", __func__);
624                 return -EINVAL;
625         }
626 
627         if (!registers) {
628                 ERR("%s: Internal driver error: register array is NULL\n", __func__);
629                 return -EINVAL;
630         }
631 
632         if (direction == TRANSFER_READ) {
633                 err = read_packet(udev, request, (u8 *)registers, start, count);
634                 if (err > 0)
635                         err = 0;
636         } else if (direction == TRANSFER_WRITE) {
637                 err =write_packet(udev, request, (u8 *)registers, start, count);
638                 if (err < 0) {
639                         LOG("Control message failed, err val = %d\n", err);
640                         LOG("Message: request = 0x%0X, start = 0x%0X\n",
641                             request, start);
642                         LOG("Message: count = %d, register[0] = 0x%0X\n",
643                             count, ((unsigned char *) registers)[0]);
644                 } else
645                         err=0;
646         } else {
647                 LOG("Unexpected first byte of direction: %d\n",
648                        direction);
649                 return -EINVAL;
650         }
651 
652         if(err != 0)
653                 LOG("Unexpected error: %d\n", err);
654         return err;
655 }
656 
657 
658 /******************************************************************************
659  *
660  *  submit_urbs
661  *
662  *****************************************************************************/
663 static int submit_urbs(struct camera_data *cam)
664 {
665         struct urb *urb;
666         int fx, err, i, j;
667 
668         for(i=0; i<NUM_SBUF; ++i) {
669                 if (cam->sbuf[i].data)
670                         continue;
671                 cam->sbuf[i].data =
672                     kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
673                 if (!cam->sbuf[i].data) {
674                         while (--i >= 0) {
675                                 kfree(cam->sbuf[i].data);
676                                 cam->sbuf[i].data = NULL;
677                         }
678                         return -ENOMEM;
679                 }
680         }
681 
682         /* We double buffer the Isoc lists, and also know the polling
683          * interval is every frame (1 == (1 << (bInterval -1))).
684          */
685         for(i=0; i<NUM_SBUF; ++i) {
686                 if(cam->sbuf[i].urb) {
687                         continue;
688                 }
689                 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
690                 if (!urb) {
691                         for (j = 0; j < i; j++)
692                                 usb_free_urb(cam->sbuf[j].urb);
693                         return -ENOMEM;
694                 }
695 
696                 cam->sbuf[i].urb = urb;
697                 urb->dev = cam->dev;
698                 urb->context = cam;
699                 urb->pipe = usb_rcvisocpipe(cam->dev, 1 /*ISOC endpoint*/);
700                 urb->transfer_flags = URB_ISO_ASAP;
701                 urb->transfer_buffer = cam->sbuf[i].data;
702                 urb->complete = cpia2_usb_complete;
703                 urb->number_of_packets = FRAMES_PER_DESC;
704                 urb->interval = 1;
705                 urb->transfer_buffer_length =
706                         FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
707 
708                 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
709                         urb->iso_frame_desc[fx].offset =
710                                 FRAME_SIZE_PER_DESC * fx;
711                         urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
712                 }
713         }
714 
715 
716         /* Queue the ISO urbs, and resubmit in the completion handler */
717         for(i=0; i<NUM_SBUF; ++i) {
718                 err = usb_submit_urb(cam->sbuf[i].urb, GFP_KERNEL);
719                 if (err) {
720                         ERR("usb_submit_urb[%d]() = %d\n", i, err);
721                         return err;
722                 }
723         }
724 
725         return 0;
726 }
727 
728 /******************************************************************************
729  *
730  *  cpia2_usb_stream_start
731  *
732  *****************************************************************************/
733 int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate)
734 {
735         int ret;
736         int old_alt;
737 
738         if(cam->streaming)
739                 return 0;
740 
741         if (cam->flush) {
742                 int i;
743                 DBG("Flushing buffers\n");
744                 for(i=0; i<cam->num_frames; ++i) {
745                         cam->buffers[i].status = FRAME_EMPTY;
746                         cam->buffers[i].length = 0;
747                 }
748                 cam->curbuff = &cam->buffers[0];
749                 cam->workbuff = cam->curbuff->next;
750                 cam->flush = false;
751         }
752 
753         old_alt = cam->params.camera_state.stream_mode;
754         cam->params.camera_state.stream_mode = 0;
755         ret = cpia2_usb_change_streaming_alternate(cam, alternate);
756         if (ret < 0) {
757                 int ret2;
758                 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret);
759                 cam->params.camera_state.stream_mode = old_alt;
760                 ret2 = set_alternate(cam, USBIF_CMDONLY);
761                 if (ret2 < 0) {
762                         ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already "
763                             "failed. Then tried to call "
764                             "set_alternate(USBIF_CMDONLY) = %d.\n",
765                             alternate, ret, ret2);
766                 }
767         } else {
768                 cam->frame_count = 0;
769                 cam->streaming = 1;
770                 ret = cpia2_usb_stream_resume(cam);
771         }
772         return ret;
773 }
774 
775 /******************************************************************************
776  *
777  *  cpia2_usb_stream_pause
778  *
779  *****************************************************************************/
780 int cpia2_usb_stream_pause(struct camera_data *cam)
781 {
782         int ret = 0;
783         if(cam->streaming) {
784                 free_sbufs(cam);
785                 ret = set_alternate(cam, USBIF_CMDONLY);
786         }
787         return ret;
788 }
789 
790 /******************************************************************************
791  *
792  *  cpia2_usb_stream_resume
793  *
794  *****************************************************************************/
795 int cpia2_usb_stream_resume(struct camera_data *cam)
796 {
797         int ret = 0;
798         if(cam->streaming) {
799                 cam->first_image_seen = 0;
800                 ret = set_alternate(cam, cam->params.camera_state.stream_mode);
801                 if(ret == 0) {
802                         /* for some reason the user effects need to be set
803                            again when starting streaming. */
804                         cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
805                                         cam->params.vp_params.user_effects);
806                         ret = submit_urbs(cam);
807                 }
808         }
809         return ret;
810 }
811 
812 /******************************************************************************
813  *
814  *  cpia2_usb_stream_stop
815  *
816  *****************************************************************************/
817 int cpia2_usb_stream_stop(struct camera_data *cam)
818 {
819         int ret;
820 
821         ret = cpia2_usb_stream_pause(cam);
822         cam->streaming = 0;
823         configure_transfer_mode(cam, 0);
824         return ret;
825 }
826 
827 /******************************************************************************
828  *
829  *  cpia2_usb_probe
830  *
831  *  Probe and initialize.
832  *****************************************************************************/
833 static int cpia2_usb_probe(struct usb_interface *intf,
834                            const struct usb_device_id *id)
835 {
836         struct usb_device *udev = interface_to_usbdev(intf);
837         struct usb_interface_descriptor *interface;
838         struct camera_data *cam;
839         int ret;
840 
841         /* A multi-config CPiA2 camera? */
842         if (udev->descriptor.bNumConfigurations != 1)
843                 return -ENODEV;
844         interface = &intf->cur_altsetting->desc;
845 
846         /* If we get to this point, we found a CPiA2 camera */
847         LOG("CPiA2 USB camera found\n");
848 
849         cam = cpia2_init_camera_struct(intf);
850         if (cam == NULL)
851                 return -ENOMEM;
852 
853         cam->dev = udev;
854         cam->iface = interface->bInterfaceNumber;
855 
856         ret = set_alternate(cam, USBIF_CMDONLY);
857         if (ret < 0) {
858                 ERR("%s: usb_set_interface error (ret = %d)\n", __func__, ret);
859                 kfree(cam);
860                 return ret;
861         }
862 
863 
864         if((ret = cpia2_init_camera(cam)) < 0) {
865                 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__, ret);
866                 kfree(cam);
867                 return ret;
868         }
869         LOG("  CPiA Version: %d.%02d (%d.%d)\n",
870                cam->params.version.firmware_revision_hi,
871                cam->params.version.firmware_revision_lo,
872                cam->params.version.asic_id,
873                cam->params.version.asic_rev);
874         LOG("  CPiA PnP-ID: %04x:%04x:%04x\n",
875                cam->params.pnp_id.vendor,
876                cam->params.pnp_id.product,
877                cam->params.pnp_id.device_revision);
878         LOG("  SensorID: %d.(version %d)\n",
879                cam->params.version.sensor_flags,
880                cam->params.version.sensor_rev);
881 
882         usb_set_intfdata(intf, cam);
883 
884         ret = cpia2_register_camera(cam);
885         if (ret < 0) {
886                 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__, ret);
887                 kfree(cam);
888                 return ret;
889         }
890 
891         return 0;
892 }
893 
894 /******************************************************************************
895  *
896  *  cpia2_disconnect
897  *
898  *****************************************************************************/
899 static void cpia2_usb_disconnect(struct usb_interface *intf)
900 {
901         struct camera_data *cam = usb_get_intfdata(intf);
902         usb_set_intfdata(intf, NULL);
903 
904         DBG("Stopping stream\n");
905         cpia2_usb_stream_stop(cam);
906 
907         mutex_lock(&cam->v4l2_lock);
908         DBG("Unregistering camera\n");
909         cpia2_unregister_camera(cam);
910         v4l2_device_disconnect(&cam->v4l2_dev);
911         mutex_unlock(&cam->v4l2_lock);
912         v4l2_device_put(&cam->v4l2_dev);
913 
914         if(cam->buffers) {
915                 DBG("Wakeup waiting processes\n");
916                 cam->curbuff->status = FRAME_READY;
917                 cam->curbuff->length = 0;
918                 wake_up_interruptible(&cam->wq_stream);
919         }
920 
921         DBG("Releasing interface\n");
922         usb_driver_release_interface(&cpia2_driver, intf);
923 
924         LOG("CPiA2 camera disconnected.\n");
925 }
926 
927 static int cpia2_usb_suspend(struct usb_interface *intf, pm_message_t message)
928 {
929         struct camera_data *cam = usb_get_intfdata(intf);
930 
931         mutex_lock(&cam->v4l2_lock);
932         if (cam->streaming) {
933                 cpia2_usb_stream_stop(cam);
934                 cam->streaming = 1;
935         }
936         mutex_unlock(&cam->v4l2_lock);
937 
938         dev_info(&intf->dev, "going into suspend..\n");
939         return 0;
940 }
941 
942 /* Resume device - start device. */
943 static int cpia2_usb_resume(struct usb_interface *intf)
944 {
945         struct camera_data *cam = usb_get_intfdata(intf);
946 
947         mutex_lock(&cam->v4l2_lock);
948         v4l2_ctrl_handler_setup(&cam->hdl);
949         if (cam->streaming) {
950                 cam->streaming = 0;
951                 cpia2_usb_stream_start(cam,
952                                 cam->params.camera_state.stream_mode);
953         }
954         mutex_unlock(&cam->v4l2_lock);
955 
956         dev_info(&intf->dev, "coming out of suspend..\n");
957         return 0;
958 }
959 
960 /******************************************************************************
961  *
962  *  usb_cpia2_init
963  *
964  *****************************************************************************/
965 int cpia2_usb_init(void)
966 {
967         return usb_register(&cpia2_driver);
968 }
969 
970 /******************************************************************************
971  *
972  *  usb_cpia_cleanup
973  *
974  *****************************************************************************/
975 void cpia2_usb_cleanup(void)
976 {
977         schedule_timeout(2 * HZ);
978         usb_deregister(&cpia2_driver);
979 }
980 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us