SyntekUSBVideoCamera
stk11xx-usb.c
Go to the documentation of this file.
1 
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
41 #include <linux/mm.h>
42 
43 #include <linux/usb.h>
44 #include <media/v4l2-common.h>
45 #include <media/v4l2-ioctl.h>
46 
47 #include "stk11xx.h"
48 
49 
54 static int default_fps = -1;
55 
60 static int default_hflip = -1;
61 
66 static int default_vflip = -1;
67 
72 static int default_brightness = -1;
73 
78 static int default_whiteness = -1;
79 
84 static int default_contrast = -1;
85 
90 static int default_colour = -1;
91 
92 
97 static struct usb_device_id stk11xx_table[] = {
105 
107  { }
108 };
109 
110 
125 {
126  int i, j;
127  int ret = 0;
128  struct urb *urb;
129  struct usb_device *udev;
130 
131  if (dev == NULL)
132  return -EFAULT;
133 
134  if (dev->isoc_init_ok)
135  return 0;
136 
137  udev = dev->udev;
138 
139  STK_DEBUG("usb_stk11xx_isoc_init()\n");
140 
141  // Allocate URB structure
142  for (i=0; i<MAX_ISO_BUFS; i++) {
143  urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
144 
145  if (urb == NULL) {
146  STK_ERROR("Failed to allocate URB %d\n", i);
147  ret = -ENOMEM;
148  break;
149  }
150 
151  dev->isobuf[i].urb = urb;
152  }
153 
154  if (ret) {
155  while (i >= 0) {
156  if (dev->isobuf[i].urb != NULL)
157  usb_free_urb(dev->isobuf[i].urb);
158 
159  dev->isobuf[i].urb = NULL;
160  i--;
161  }
162 
163  return ret;
164  }
165 
166  // Init URB structure
167  for (i=0; i<MAX_ISO_BUFS; i++) {
168  urb = dev->isobuf[i].urb;
169 
170  urb->interval = 1;
171  urb->dev = udev;
172  urb->pipe = usb_rcvisocpipe(udev, dev->isoc_in_endpointAddr);
173  urb->transfer_flags = URB_ISO_ASAP;
174  urb->transfer_buffer = dev->isobuf[i].data;
175  urb->transfer_buffer_length = ISO_BUFFER_SIZE;
176  urb->complete = usb_stk11xx_isoc_handler;
177  urb->context = dev;
178  urb->start_frame = 0;
179  urb->number_of_packets = ISO_FRAMES_PER_DESC;
180 
181  for (j=0; j<ISO_FRAMES_PER_DESC; j++) {
182  urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
183  urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; //dev->isoc_in_size;
184  }
185  }
186 
187  STK_DEBUG("dev->isoc_in_size = %X\n", dev->isoc_in_size);
188  STK_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev->isoc_in_endpointAddr);
189 
190  // Link
191  for (i=0; i<MAX_ISO_BUFS; i++) {
192  ret = usb_submit_urb(dev->isobuf[i].urb, GFP_KERNEL);
193 
194  if (ret)
195  STK_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
196  else
197  STK_DEBUG("URB 0x%p submitted.\n", dev->isobuf[i].urb);
198 
199  switch (ret) {
200  case -ENOMEM:
201  STK_ERROR("ENOMEM\n");
202  break;
203  case -ENODEV:
204  STK_ERROR("ENODEV\n");
205  break;
206  case -ENXIO:
207  STK_ERROR("ENXIO\n");
208  break;
209  case -EINVAL:
210  STK_ERROR("EINVAL\n");
211  break;
212  case -EAGAIN:
213  STK_ERROR("EAGAIN\n");
214  break;
215  case -EFBIG:
216  STK_ERROR("EFBIG\n");
217  break;
218  case -EPIPE:
219  STK_ERROR("EPIPE\n");
220  break;
221  case -EMSGSIZE:
222  STK_ERROR("EMSGSIZE\n");
223  break;
224  }
225  }
226 
227  // All is done
228  dev->isoc_init_ok = 1;
229 
230  return 0;
231 }
232 
233 
243 void usb_stk11xx_isoc_handler(struct urb *urb)
244 {
245  int i;
246  int ret;
247  int skip;
248 
249  int awake = 0;
250  int framestatus;
251  int framelen;
252 
253  unsigned char *fill = NULL;
254  unsigned char *iso_buf = NULL;
255 
256  struct usb_stk11xx *dev;
257  struct stk11xx_frame_buf *framebuf;
258 
259  STK_STREAM("Isoc handler\n");
260 
261  dev = (struct usb_stk11xx *) urb->context;
262 
263  if (dev == NULL) {
264  STK_ERROR("isoc_handler called with NULL device !\n");
265  return;
266  }
267 
268  if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
269  STK_DEBUG("URB unlinked synchronuously !\n");
270  return;
271  }
272 
273  if (urb->status != -EINPROGRESS && urb->status != 0) {
274  const char *errmsg;
275 
276  errmsg = "Unknown";
277 
278  switch(urb->status) {
279  case -ENOSR:
280  errmsg = "Buffer error (overrun)";
281  break;
282 
283  case -EPIPE:
284  errmsg = "Stalled (device not responding)";
285  break;
286 
287  case -EOVERFLOW:
288  errmsg = "Babble (bad cable?)";
289  break;
290 
291  case -EPROTO:
292  errmsg = "Bit-stuff error (bad cable?)";
293  break;
294 
295  case -EILSEQ:
296  errmsg = "CRC/Timeout (could be anything)";
297  break;
298 
299  case -ETIMEDOUT:
300  errmsg = "NAK (device does not respond)";
301  break;
302  }
303 
304  STK_ERROR("isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
305 
306  dev->visoc_errors++;
307 
308  wake_up_interruptible(&dev->wait_frame);
309 
310  urb->dev = dev->udev;
311  ret = usb_submit_urb(urb, GFP_ATOMIC);
312 
313  if (ret != 0) {
314  STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
315  }
316 
317  return;
318  }
319 
320  framebuf = dev->fill_frame;
321 
322  if (framebuf == NULL) {
323  STK_ERROR("isoc_handler without valid fill frame !\n");
324 
325  wake_up_interruptible(&dev->wait_frame);
326 
327  urb->dev = dev->udev;
328  ret = usb_submit_urb(urb, GFP_ATOMIC);
329 
330  if (ret != 0) {
331  STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
332  }
333 
334  return;
335  }
336  else {
337  fill = framebuf->data + framebuf->filled;
338  }
339 
340  // Reset ISOC error counter
341  dev->visoc_errors = 0;
342 
343  // Compact data
344  for (i=0; i<urb->number_of_packets; i++) {
345  framestatus = urb->iso_frame_desc[i].status;
346  framelen = urb->iso_frame_desc[i].actual_length;
347  iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
348 
349  if (framestatus == 0) {
350  skip = 4;
351 
352  if (framelen > 4) {
353  // we found something informational from there
354  // the isoc frames have to type of headers
355  // type1: 00 xx 00 00 or 20 xx 00 00
356  // type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
357  // xx is a sequencer which has never been seen over 0x3f
358  //
359  // imho data written down looks like bayer, i see similarities after
360  // every 640 bytes
361  if (*iso_buf & 0x80) {
362  skip = 8;
363  }
364 
365  // Our buffer is full !!!
366  if (framelen - skip + framebuf->filled > dev->frame_size) {
367  STK_ERROR("Frame buffer overflow !\n");
368  framebuf->errors++;
369  }
370  // All is OK
371  else {
372  memcpy(fill, iso_buf + skip, framelen - skip);
373  fill += framelen - skip;
374  }
375 
376  // New size of our buffer
377  framebuf->filled += framelen - skip;
378  }
379 
380  STK_STREAM("URB : Length = %d - Skip = %d - Buffer size = %d\n",
381  framelen, skip, framebuf->filled);
382 
383  // Data is always follow by a frame with a length '4'
384  if (framelen == 4) {
385  if (framebuf->filled > 0) {
386  // Our buffer has enough data ?
387  if (framebuf->filled < dev->frame_size)
388  framebuf->errors++;
389 
390  // If there are errors, we skip a frame...
391  if (framebuf->errors == 0) {
392  if (stk11xx_next_frame(dev))
393  dev->vframes_dumped++;
394  }
395  else
396  dev->vframes_error++;
397 
398  awake = 1;
399  framebuf = dev->fill_frame;
400  framebuf->filled = 0;
401  framebuf->errors = 0;
402  fill = framebuf->data;
403  }
404  }
405  }
406  else {
407  STK_ERROR("Iso frame %d of USB has error %d\n", i, framestatus);
408  }
409  }
410 
411  if (awake == 1)
412  wake_up_interruptible(&dev->wait_frame);
413 
414  urb->dev = dev->udev;
415 
416  ret = usb_submit_urb(urb, GFP_ATOMIC);
417 
418  if (ret != 0) {
419  STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
420  }
421 }
422 
423 
432 {
433  int i;
434 
435  STK_DEBUG("Isoc cleanup\n");
436 
437  if (dev == NULL)
438  return;
439 
440  if (dev->isoc_init_ok == 0)
441  return;
442 
443  // Unlinking ISOC buffers
444  for (i=0; i<MAX_ISO_BUFS; i++) {
445  struct urb *urb;
446 
447  urb = dev->isobuf[i].urb;
448 
449  if (urb != 0) {
450  if (dev->isoc_init_ok)
451  usb_kill_urb(urb);
452 
453  usb_free_urb(urb);
454  dev->isobuf[i].urb = NULL;
455  }
456  }
457 
458  // All is done
459  dev->isoc_init_ok = 0;
460 }
461 
462 
463 
474 int usb_stk11xx_set_feature(struct usb_stk11xx *dev, int index)
475 {
476  int result;
477  struct usb_device *udev = dev->udev;
478 
479  result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
480  USB_REQ_SET_FEATURE,
481  USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
482  USB_DEVICE_REMOTE_WAKEUP,
483  index,
484  NULL,
485  0,
486  500);
487 
488  if (result < 0)
489  STK_ERROR("SET FEATURE fail !\n");
490  else
491  STK_DEBUG("SET FEATURE\n");
492 
493  return result;
494 }
495 
496 
507 {
508  int result;
509  struct usb_device *udev = dev->udev;
510 
511  result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
512  USB_REQ_SET_CONFIGURATION,
513  USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
514  0,
515  udev->config[0].desc.bConfigurationValue,
516  NULL,
517  0,
518  500);
519 
520  if (result < 0)
521  STK_ERROR("SET CONFIGURATION fail !\n");
522  else
523  STK_DEBUG("SET CONFIGURATION %d\n", udev->config[0].desc.bConfigurationValue);
524 
525  return result;
526 }
527 
528 
540 int usb_stk11xx_write_registry(struct usb_stk11xx *dev, __u16 index, __u16 value)
541 {
542  int result;
543  struct usb_device *udev = dev->udev;
544 
545  result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
546  0x01,
547  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
548  value,
549  index,
550  NULL,
551  0,
552  500);
553 
554  if (result < 0)
555  STK_ERROR("Write registry fails %02X = %02X", index, value);
556 
557  return result;
558 }
559 
560 
572 int usb_stk11xx_read_registry(struct usb_stk11xx *dev, __u16 index, int *value)
573 {
574  int result;
575 
576  struct usb_device *udev = dev->udev;
577 
578  *value = 0;
579 
580  result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
581  0x00,
582  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
583  0x00,
584  index,
585  (__u8 *) value,
586  sizeof(__u8),
587  500);
588 
589  if (result < 0)
590  STK_ERROR("Read registry fails %02X", index);
591 
592  return result;
593 }
594 
595 
607 {
608  switch (dev->webcam_model) {
609  case SYNTEK_STK_M811:
610  case SYNTEK_STK_A311:
611  dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
612  dev->vsettings.vflip = (default_vflip == -1) ? 1 : default_vflip;
613  dev->vsettings.hflip = (default_hflip == -1) ? 1 : default_hflip;
614 
617  dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
618  dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
619  break;
620 
621  case SYNTEK_STK_A821:
622  dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
623  dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
624  dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
625 
628  dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
629  dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
630  break;
631 
632  case SYNTEK_STK_6A31:
633  case SYNTEK_STK_6A33:
634  dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
635  dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
636  dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
637 
640  dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
641  dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
642  break;
643 
644  case SYNTEK_STK_6A51:
645  case SYNTEK_STK_6D51:
646  case SYNTEK_STK_6A54:
647  dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
648  dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
649  dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
650 
653  dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
654  dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
655  break;
656 
657  default:
658  return -1;
659  }
660 
661  dev->vsettings.default_brightness = dev->vsettings.brightness;
662  dev->vsettings.default_whiteness = dev->vsettings.whiteness;
663  dev->vsettings.default_contrast = dev->vsettings.contrast;
664  dev->vsettings.default_colour = dev->vsettings.colour;
665  dev->vsettings.default_hflip = dev->vsettings.hflip;
666  dev->vsettings.default_vflip = dev->vsettings.vflip;
667  return 0;
668 }
669 
670 
682 static int usb_stk11xx_probe(struct usb_interface *interface, const struct usb_device_id *id)
683 {
684  int i;
685  int err;
686  size_t buffer_size;
687 
688  int vendor_id;
689  int product_id;
690  int bNumInterfaces;
691  int webcam_model;
692  int webcam_type;
693 
694  struct usb_stk11xx *dev = NULL;
695  struct usb_device *udev = interface_to_usbdev(interface);
696  struct usb_host_interface *iface_desc;
697  struct usb_endpoint_descriptor *endpoint;
698 
699 
700  // Get USB VendorID and ProductID
701  vendor_id = le16_to_cpu(udev->descriptor.idVendor);
702  product_id = le16_to_cpu(udev->descriptor.idProduct);
703 
704  // Check if we can handle this device
705  STK_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
706  vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
707 
708  // The interface are probed one by one.
709  // We are interested in the video interface (always the interface '0')
710  // The interfaces '1' or '2' (if presents) are the audio control.
711  if (interface->cur_altsetting->desc.bInterfaceNumber > 0)
712  return -ENODEV;
713 
714  // Detect device
715  if (vendor_id == USB_SYNTEK1_VENDOR_ID) {
716  switch (product_id) {
718  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
719  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0xA311.\n");
720  webcam_model = SYNTEK_STK_A311;
721  webcam_type = STK11XX_SXGA;
722  break;
723 
725  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
726  STK_INFO("Syntek AVStream USB2.0 VGA WebCam - Product ID 0xA821.\n");
727  webcam_model = SYNTEK_STK_A821;
728  webcam_type = STK11XX_VGA;
729  break;
730 
732  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
733  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A31.\n");
734  webcam_model = SYNTEK_STK_6A31;
735  webcam_type = STK11XX_VGA;
736  break;
737 
739  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
740  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A33.\n");
741  webcam_model = SYNTEK_STK_6A33;
742  webcam_type = STK11XX_VGA;
743  break;
744 
746  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
747  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A51.\n");
748  webcam_model = SYNTEK_STK_6A51;
749  webcam_type = STK11XX_VGA;
750  break;
751 
753  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
754  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A54.\n");
755  webcam_model = SYNTEK_STK_6A54;
756  webcam_type = STK11XX_VGA;
757  break;
758 
760  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
761  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6D51.\n");
762  webcam_model = SYNTEK_STK_6D51;
763  webcam_type = STK11XX_VGA;
764  break;
765 
766  default:
767  STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
768  le16_to_cpu(udev->descriptor.idProduct));
769  return -ENODEV;
770  }
771  }
772  else if (vendor_id == USB_SYNTEK2_VENDOR_ID) {
773  switch (product_id) {
775  STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
776  STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x0501.\n");
777  webcam_model = SYNTEK_STK_M811;
778  webcam_type = STK11XX_SXGA;
779  break;
780 
781  default:
782  STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
783  le16_to_cpu(udev->descriptor.idProduct));
784  return -ENODEV;
785  }
786  }
787  else
788  return -ENODEV;
789 
790  // Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device
791  dev = kzalloc(sizeof(struct usb_stk11xx), GFP_KERNEL);
792 
793  if (dev == NULL) {
794  STK_ERROR("Out of memory !\n");
795  return -ENOMEM;
796  }
797 
798  // Init mutexes, spinlock, etc.
799 
800 #ifndef init_MUTEX
801  sema_init(&dev->mutex,1);
802 #else
803  init_MUTEX(&dev->mutex);
804 #endif
805  mutex_init(&dev->modlock);
806  spin_lock_init(&dev->spinlock);
807  init_waitqueue_head(&dev->wait_frame);
808 
809  // Save pointers
810  dev->webcam_model = webcam_model;
811  dev->webcam_type = webcam_type;
812  dev->udev = udev;
813  dev->interface = interface;
814 
815  // Read the product release
816  dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
817  STK_INFO("Release: %04x\n", dev->release);
818 
819  // How many interfaces (1 or 3) ?
820  bNumInterfaces = udev->config->desc.bNumInterfaces;
821  STK_INFO("Number of interfaces : %d\n", bNumInterfaces);
822 
823 
824  // Constructor
825  dev->nbuffers = 2;
826  dev->len_per_image = PAGE_ALIGN((1280 * 1024 * 4));
827 
828 
829  // Switch on the camera (to detect size of buffers)
831 
832 
833  // Set up the endpoint information
834  // use only the first int-in and isoc-in endpoints
835  // for the current alternate setting
836  iface_desc = interface->cur_altsetting;
837 
838  for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
839  endpoint = &iface_desc->endpoint[i].desc;
840 
841  if (!dev->int_in_endpointAddr
842  && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
843  && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
844  // we found an interrupt in endpoint
845  buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
846 
847  dev->int_in_size = buffer_size;
848  dev->int_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
849  }
850 
851  if (!dev->isoc_in_endpointAddr
852  && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
853  && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
854  // we found an isoc in endpoint
855  buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
856 
857  dev->isoc_in_size = buffer_size;
858  dev->isoc_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
859  }
860  }
861 
862  if (!(dev->int_in_endpointAddr && dev->isoc_in_endpointAddr)) {
863  STK_ERROR("Could not find both int-in and isoc-in endpoints");
864 
865  kfree(dev);
866 
867  return -ENODEV;
868  }
869 
870 
871  // Switch off camera
873 
874  // Initialize the video device
875  dev->vdev = video_device_alloc();
876 
877  if (!dev->vdev) {
878  kfree(dev);
879  return -ENOMEM;
880  }
881 
882  // Initialize the camera
884 
885  // Register the video device
887 
888  if (err) {
889  kfree(dev);
890  return err;
891  }
892 
893  // Create the entries in the sys filesystem
895 
896  // Save our data pointer in this interface device
897  usb_set_intfdata(interface, dev);
898 
899  // Default settings video device
901 
902  // Enable power management feature
903 // usb_autopm_enable(dev->interface);
904 
905  return 0;
906 }
907 
908 
915 static void usb_stk11xx_disconnect(struct usb_interface *interface)
916 {
917  struct usb_stk11xx *dev = usb_get_intfdata(interface);
918 
919  STK_INFO("Syntek USB2.0 Camera disconnected\n");
920 
921  // We got unplugged; this is signalled by an EPIPE error code
922  if (dev->vopen) {
923  STK_INFO("Disconnected while webcam is in use !\n");
924  dev->error_status = EPIPE;
925  }
926 
927  // Alert waiting processes
928  wake_up_interruptible(&dev->wait_frame);
929 
930  // Wait until device is closed
931  while (dev->vopen)
932  schedule();
933 
934  // Remove the entries in the sys filesystem
936 
937  // Unregister the video device
939 
940  usb_set_intfdata(interface, NULL);
941 
942  kfree(dev);
943 }
944 
945 #ifdef CONFIG_PM
946 int usb_stk11xx_suspend(struct usb_interface *interface, pm_message_t message)
947 {
948  struct usb_stk11xx *dev = usb_get_intfdata(interface);
949 
950  STK_INFO("Syntek USB2.0 Camera Suspend\n");
951 
952  mutex_lock(&dev->modlock);
953  if (dev->vopen) {
954  // Stop the video stream
956 
957  // ISOC and URB cleanup
959 
960  // Free memory
961  // stk11xx_free_buffers(dev);
962 
963  // Switch off the camera
965 
967  }
968  mutex_unlock(&dev->modlock);
969 
970  return 0;
971 }
972 
973 
974 int usb_stk11xx_resume(struct usb_interface *interface)
975 {
976  struct usb_stk11xx *dev = usb_get_intfdata(interface);
977 
978  STK_INFO("Syntek USB2.0 Camera Resume\n");
979 
980  mutex_lock(&dev->modlock);
981 
982  // Initialize the camera
984 
985  if (dev->vopen)
986  {
987  // Select the video mode
988  v4l_stk11xx_select_video_mode(dev, dev->view.x, dev->view.y);
989 
990  // Clear the buffers
992 
993  // Initialize the device
997 
998  // ISOC and URB init
1000 
1001  // Start the video stream
1003 
1004  // Video settings
1006  }
1007  mutex_unlock(&dev->modlock);
1008 
1009  return 0;
1010 }
1011 #endif // CONFIG_PM
1012 
1013 
1019 static struct usb_driver usb_stk11xx_driver = {
1020  .name = "usb_stk11xx_driver",
1021  .probe = usb_stk11xx_probe,
1022  .disconnect = usb_stk11xx_disconnect,
1023  .id_table = stk11xx_table,
1024 #ifdef CONFIG_PM
1025  .suspend = usb_stk11xx_suspend,
1026  .resume = usb_stk11xx_resume,
1027 #endif
1028 };
1029 
1030 
1035 static int fps;
1036 
1041 static int hflip = -1;
1042 
1047 static int vflip = -1;
1048 
1053 static int brightness = -1;
1054 
1059 static int whiteness = -1;
1060 
1065 static int contrast = -1;
1066 
1071 static int colour = -1;
1072 
1073 
1074 module_param(fps, int, 0444);
1075 module_param(hflip, int, 0444);
1076 module_param(vflip, int, 0444);
1080 module_param(contrast, int, 0444);
1081 module_param(colour, int, 0444);
1092 static int __init usb_stk11xx_init(void)
1093 {
1094  int result;
1095 
1096 
1097  STK_INFO("Syntek USB2.0 webcam driver startup\n");
1098 
1099  // Frame per second parameter
1100  if (fps) {
1101  if (fps < 9 || fps > 30) {
1102  STK_ERROR("Framerate out of bounds [10-30] !\n");
1103  return -EINVAL;
1104  }
1105 
1106  default_fps = fps;
1107  }
1108 
1109  // Horizontal flip value
1110  if ((hflip == 0) || (hflip == 1)) {
1111  STK_DEBUG("Set horizontal flip = %d\n", hflip);
1112 
1113  default_hflip = hflip;
1114  }
1115 
1116  // Vertical flip value
1117  if ((vflip == 0) || (vflip == 1)) {
1118  STK_DEBUG("Set vertical flip = %d\n", vflip);
1119 
1120  default_vflip = vflip;
1121  }
1122 
1123  // Brightness value
1124  if (brightness > -1) {
1125  STK_DEBUG("Set brightness = 0x%X\n", brightness);
1126 
1127  default_brightness = 0xffff & brightness;
1128  }
1129 
1130  // Whiteness value
1131  if (whiteness > -1) {
1132  STK_DEBUG("Set whiteness = 0x%X\n", whiteness);
1133 
1134  default_whiteness = 0xffff & whiteness;
1135  }
1136 
1137  // Contrast value
1138  if (contrast > -1) {
1139  STK_DEBUG("Set contrast = 0x%X\n", contrast);
1140 
1141  default_contrast = 0xffff & contrast;
1142  }
1143 
1144  // Colour value
1145  if (colour > -1) {
1146  STK_DEBUG("Set colour = 0x%X\n", colour);
1147 
1148  default_colour = 0xffff & colour;
1149  }
1150 
1151 
1152  // Register the driver with the USB subsystem
1153  result = usb_register(&usb_stk11xx_driver);
1154 
1155  if (result)
1156  STK_ERROR("usb_register failed ! Error number %d\n", result);
1157 
1158  STK_INFO(DRIVER_VERSION " : " DRIVER_DESC "\n");
1159 
1160  return result;
1161 }
1162 
1163 
1169 static void __exit usb_stk11xx_exit(void)
1170 {
1171  STK_INFO("usb_stk11xx_exit: Syntek USB2.0 webcam driver shutdown\n");
1172 
1173  // Deregister this driver with the USB subsystem
1174  usb_deregister(&usb_stk11xx_driver);
1175 }
1176 
1177 
1182 MODULE_PARM_DESC(fps, "Frames per second [5-30]");
1183 MODULE_PARM_DESC(hflip, "Horizontal image flip");
1184 MODULE_PARM_DESC(vflip, "Vertical image flip");
1185 MODULE_PARM_DESC(brightness, "Brightness setting");
1186 MODULE_PARM_DESC(whiteness, "Whiteness setting");
1187 MODULE_PARM_DESC(colour, "Colour setting");
1188 MODULE_PARM_DESC(contrast, "Contrast setting");
default_hflip
static int default_hflip
Definition: stk11xx-usb.c:60
usb_stk11xx::udev
struct usb_device * udev
Definition: stk11xx.h:303
USB_STK_A311_PRODUCT_ID
#define USB_STK_A311_PRODUCT_ID
Definition: stk11xx.h:48
MODULE_SUPPORTED_DEVICE
MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT)
List of supported device.
dev_stk11xx_initialize_device
int dev_stk11xx_initialize_device(struct usb_stk11xx *dev)
This function permits to initialize the device.
Definition: stk11xx-dev.c:63
usb_stk11xx::vframes_dumped
int vframes_dumped
Definition: stk11xx.h:326
MODULE_DESCRIPTION
MODULE_DESCRIPTION(DRIVER_DESC)
Define the description of the driver.
stk11xx_video::hflip
int hflip
Definition: stk11xx.h:285
default_brightness
static int default_brightness
Definition: stk11xx-usb.c:72
dev_stk11xx_camera_on
int dev_stk11xx_camera_on(struct usb_stk11xx *dev)
This function switchs on the camera.
Definition: stk11xx-dev.c:241
MODULE_AUTHOR
MODULE_AUTHOR(DRIVER_AUTHOR)
Driver is written by Nicolas VIVIEN.
colour
static int colour
Definition: stk11xx-usb.c:1071
DRIVER_AUTHOR
#define DRIVER_AUTHOR
Definition: stk11xx.h:42
dev_stk11xx_stop_stream
int dev_stk11xx_stop_stream(struct usb_stk11xx *dev)
This function sets the device to stop the stream.
Definition: stk11xx-dev.c:604
DRIVER_SUPPORT
#define DRIVER_SUPPORT
Definition: stk11xx.h:111
dev_stk11xx_camera_off
int dev_stk11xx_camera_off(struct usb_stk11xx *dev)
This function switchs off the camera.
Definition: stk11xx-dev.c:264
stk11xx_video::brightness
int brightness
Definition: stk11xx.h:278
USB_STK_6A33_PRODUCT_ID
#define USB_STK_6A33_PRODUCT_ID
Definition: stk11xx.h:51
default_fps
static int default_fps
Definition: stk11xx-usb.c:54
USB_STK_6A51_PRODUCT_ID
#define USB_STK_6A51_PRODUCT_ID
Definition: stk11xx.h:52
stk11xx_coord::x
int x
Definition: stk11xx.h:268
usb_stk11xx::int_in_endpointAddr
__u8 int_in_endpointAddr
Definition: stk11xx.h:312
usb_stk11xx::int_in_size
size_t int_in_size
Definition: stk11xx.h:311
STK_STREAM
#define STK_STREAM(str, args...)
Definition: stk11xx.h:172
default_vflip
static int default_vflip
Definition: stk11xx-usb.c:66
usb_stk11xx_default_settings
static int usb_stk11xx_default_settings(struct usb_stk11xx *dev)
Set the default value about the video settings.
Definition: stk11xx-usb.c:606
dev_stk11xx_start_stream
int dev_stk11xx_start_stream(struct usb_stk11xx *dev)
This function sets the device to start the stream.
Definition: stk11xx-dev.c:501
stk11xx.h
Driver for Syntek USB video camera.
stk11xx_remove_sysfs_files
void stk11xx_remove_sysfs_files(struct video_device *vdev)
Remove the 'sys' entries.
Definition: stk11xx-sysfs.c:530
usb_stk11xx_probe
static int usb_stk11xx_probe(struct usb_interface *interface, const struct usb_device_id *id)
Load the driver.
Definition: stk11xx-usb.c:682
usb_stk11xx_set_configuration
int usb_stk11xx_set_configuration(struct usb_stk11xx *dev)
Send the message SET_CONFIGURATION.
Definition: stk11xx-usb.c:506
dev_stk11xx_reconf_camera
int dev_stk11xx_reconf_camera(struct usb_stk11xx *dev)
Reconfigure the camera before the stream.
Definition: stk11xx-dev.c:552
STK_DEBUG
#define STK_DEBUG(str, args...)
Definition: stk11xx.h:149
brightness
static int brightness
Definition: stk11xx-usb.c:1053
default_whiteness
static int default_whiteness
Definition: stk11xx-usb.c:78
USB_STK_6D51_PRODUCT_ID
#define USB_STK_6D51_PRODUCT_ID
Definition: stk11xx.h:54
default_contrast
static int default_contrast
Definition: stk11xx-usb.c:84
stk11xx_frame_buf
Definition: stk11xx.h:247
stk11xx_video::colour
int colour
Definition: stk11xx.h:281
USB_SYNTEK1_VENDOR_ID
#define USB_SYNTEK1_VENDOR_ID
Definition: stk11xx.h:45
usb_stk11xx_driver
static struct usb_driver usb_stk11xx_driver
Definition: stk11xx-usb.c:1019
usb_stk11xx_init
static int __init usb_stk11xx_init(void)
Initialize the driver.
Definition: stk11xx-usb.c:1092
usb_stk11xx::webcam_type
int webcam_type
Definition: stk11xx.h:308
v4l_stk11xx_register_video_device
int v4l_stk11xx_register_video_device(struct usb_stk11xx *dev)
Register the video device.
Definition: stk11xx-v4l.c:1453
usb_stk11xx::release
int release
Definition: stk11xx.h:306
STK_ERROR
#define STK_ERROR(str, args...)
Definition: stk11xx.h:147
dev_stk11xx_camera_settings
int dev_stk11xx_camera_settings(struct usb_stk11xx *dev)
This function permits to modify the settings of the camera.
Definition: stk11xx-dev.c:342
MODULE_LICENSE
MODULE_LICENSE("GPL")
Driver is under licence GPL.
usb_stk11xx::mutex
struct semaphore mutex
Definition: stk11xx.h:330
ISO_FRAMES_PER_DESC
#define ISO_FRAMES_PER_DESC
Definition: stk11xx.h:84
dev_stk11xx_camera_asleep
int dev_stk11xx_camera_asleep(struct usb_stk11xx *dev)
Wake-up the camera.
Definition: stk11xx-dev.c:287
MODULE_DEVICE_TABLE
MODULE_DEVICE_TABLE(usb, stk11xx_table)
USB_STK_6A31_PRODUCT_ID
#define USB_STK_6A31_PRODUCT_ID
Definition: stk11xx.h:50
usb_stk11xx_set_feature
int usb_stk11xx_set_feature(struct usb_stk11xx *dev, int index)
Send the message SET_FEATURE and choose the interface.
Definition: stk11xx-usb.c:474
stk11xx_video::vflip
int vflip
Definition: stk11xx.h:286
module_init
module_init(usb_stk11xx_init)
Module initialize.
usb_stk11xx::interface
struct usb_interface * interface
Definition: stk11xx.h:304
usb_stk11xx_isoc_init
int usb_stk11xx_isoc_init(struct usb_stk11xx *dev)
Initilize an isochronous pipe.
Definition: stk11xx-usb.c:124
STK11XX_VGA
@ STK11XX_VGA
Definition: stk11xx.h:197
usb_stk11xx::webcam_model
int webcam_model
Definition: stk11xx.h:307
usb_stk11xx::isoc_in_size
size_t isoc_in_size
Definition: stk11xx.h:314
usb_stk11xx_write_registry
int usb_stk11xx_write_registry(struct usb_stk11xx *dev, __u16 index, __u16 value)
Write a 16-bits value to a 16-bits register.
Definition: stk11xx-usb.c:540
default_colour
static int default_colour
Definition: stk11xx-usb.c:90
usb_stk11xx::isoc_in_endpointAddr
__u8 isoc_in_endpointAddr
Definition: stk11xx.h:315
ISO_BUFFER_SIZE
#define ISO_BUFFER_SIZE
Definition: stk11xx.h:86
usb_stk11xx::vdev
struct video_device * vdev
Definition: stk11xx.h:302
stk11xx_next_frame
int stk11xx_next_frame(struct usb_stk11xx *dev)
Prepare the next frame.
Definition: stk11xx-buf.c:352
stk11xx_clear_buffers
int stk11xx_clear_buffers(struct usb_stk11xx *dev)
Clear current buffers.
Definition: stk11xx-buf.c:270
USB_STK_6A54_PRODUCT_ID
#define USB_STK_6A54_PRODUCT_ID
Definition: stk11xx.h:53
usb_stk11xx_disconnect
static void usb_stk11xx_disconnect(struct usb_interface *interface)
This function is called when the device is disconnected or when the kernel module is unloaded.
Definition: stk11xx-usb.c:915
usb_stk11xx_read_registry
int usb_stk11xx_read_registry(struct usb_stk11xx *dev, __u16 index, int *value)
Read a 16-bits value from a 16-bits register.
Definition: stk11xx-usb.c:572
DRIVER_DESC
#define DRIVER_DESC
Definition: stk11xx.h:41
DRIVER_VERSION
#define DRIVER_VERSION
Definition: stk11xx.h:39
USB_STK_0501_PRODUCT_ID
#define USB_STK_0501_PRODUCT_ID
Definition: stk11xx.h:56
STK11XX_SXGA
@ STK11XX_SXGA
Definition: stk11xx.h:198
usb_stk11xx::vopen
int vopen
Definition: stk11xx.h:323
usb_stk11xx::vframes_error
int vframes_error
Definition: stk11xx.h:325
STK11XX_PERCENT
#define STK11XX_PERCENT(x, y)
Definition: stk11xx.h:367
v4l_stk11xx_select_video_mode
int v4l_stk11xx_select_video_mode(struct usb_stk11xx *dev, int width, int height)
Select a video mode.
Definition: stk11xx-v4l.c:146
ISO_MAX_FRAME_SIZE
#define ISO_MAX_FRAME_SIZE
Definition: stk11xx.h:85
dev_stk11xx_init_camera
int dev_stk11xx_init_camera(struct usb_stk11xx *dev)
This function initializes the device for the stream.
Definition: stk11xx-dev.c:115
module_exit
module_exit(usb_stk11xx_exit)
Module exit.
stk11xx_video::fps
int fps
Definition: stk11xx.h:277
STK_INFO
#define STK_INFO(str, args...)
Definition: stk11xx.h:146
usb_stk11xx::wait_frame
wait_queue_head_t wait_frame
Definition: stk11xx.h:331
module_param
module_param(fps, int, 0444)
Module frame per second parameter.
usb_stk11xx_exit
static void __exit usb_stk11xx_exit(void)
Close the driver.
Definition: stk11xx-usb.c:1169
USB_SYNTEK2_VENDOR_ID
#define USB_SYNTEK2_VENDOR_ID
Definition: stk11xx.h:46
stk11xx_video::contrast
int contrast
Definition: stk11xx.h:279
stk11xx_create_sysfs_files
int stk11xx_create_sysfs_files(struct video_device *vdev)
Create the 'sys' entries.
Definition: stk11xx-sysfs.c:502
usb_stk11xx
Definition: stk11xx.h:301
vflip
static int vflip
Definition: stk11xx-usb.c:1047
usb_stk11xx::vsettings
struct stk11xx_video vsettings
Definition: stk11xx.h:319
usb_stk11xx_isoc_cleanup
void usb_stk11xx_isoc_cleanup(struct usb_stk11xx *dev)
Clean-up all the ISOC buffers.
Definition: stk11xx-usb.c:431
v4l_stk11xx_unregister_video_device
int v4l_stk11xx_unregister_video_device(struct usb_stk11xx *dev)
Unregister the video device.
Definition: stk11xx-v4l.c:1486
usb_stk11xx::visoc_errors
int visoc_errors
Definition: stk11xx.h:324
fps
static int fps
Definition: stk11xx-usb.c:1035
USB_STK_A821_PRODUCT_ID
#define USB_STK_A821_PRODUCT_ID
Definition: stk11xx.h:49
stk11xx_video::whiteness
int whiteness
Definition: stk11xx.h:280
stk11xx_coord::y
int y
Definition: stk11xx.h:269
usb_stk11xx::spinlock
spinlock_t spinlock
Definition: stk11xx.h:329
MODULE_PARM_DESC
MODULE_PARM_DESC(fps, "Frames per second [5-30]")
Description of 'fps' parameter.
usb_stk11xx_isoc_handler
void usb_stk11xx_isoc_handler(struct urb *urb)
ISOC handler.
Definition: stk11xx-usb.c:243
usb_stk11xx::modlock
struct mutex modlock
Definition: stk11xx.h:332
whiteness
static int whiteness
Definition: stk11xx-usb.c:1059
contrast
static int contrast
Definition: stk11xx-usb.c:1065
stk11xx_table
static struct usb_device_id stk11xx_table[]
Definition: stk11xx-usb.c:97
MAX_ISO_BUFS
#define MAX_ISO_BUFS
Definition: stk11xx.h:83
hflip
static int hflip
Definition: stk11xx-usb.c:1041