Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uvcvideo.h
Go to the documentation of this file.
1 #ifndef _USB_VIDEO_H_
2 #define _USB_VIDEO_H_
3 
4 #ifndef __KERNEL__
5 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
6 #endif /* __KERNEL__ */
7 
8 #include <linux/kernel.h>
9 #include <linux/poll.h>
10 #include <linux/usb.h>
11 #include <linux/usb/video.h>
12 #include <linux/uvcvideo.h>
13 #include <linux/videodev2.h>
14 #include <media/media-device.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-event.h>
17 #include <media/v4l2-fh.h>
18 #include <media/videobuf2-core.h>
19 
20 /* --------------------------------------------------------------------------
21  * UVC constants
22  */
23 
24 #define UVC_TERM_INPUT 0x0000
25 #define UVC_TERM_OUTPUT 0x8000
26 #define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
27 
28 #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
29 #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
30 #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
31 #define UVC_ENTITY_IS_ITERM(entity) \
32  (UVC_ENTITY_IS_TERM(entity) && \
33  ((entity)->type & 0x8000) == UVC_TERM_INPUT)
34 #define UVC_ENTITY_IS_OTERM(entity) \
35  (UVC_ENTITY_IS_TERM(entity) && \
36  ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
37 
38 
39 /* ------------------------------------------------------------------------
40  * GUIDs
41  */
42 #define UVC_GUID_UVC_CAMERA \
43  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
44  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
45 #define UVC_GUID_UVC_OUTPUT \
46  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
47  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
48 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
49  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
50  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
51 #define UVC_GUID_UVC_PROCESSING \
52  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
53  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
54 #define UVC_GUID_UVC_SELECTOR \
55  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
56  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
57 
58 #define UVC_GUID_FORMAT_MJPEG \
59  { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
60  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
61 #define UVC_GUID_FORMAT_YUY2 \
62  { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
63  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
64 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
65  { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
66  0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
67 #define UVC_GUID_FORMAT_NV12 \
68  { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
69  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
70 #define UVC_GUID_FORMAT_YV12 \
71  { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
72  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
73 #define UVC_GUID_FORMAT_I420 \
74  { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
75  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
76 #define UVC_GUID_FORMAT_UYVY \
77  { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
78  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
79 #define UVC_GUID_FORMAT_Y800 \
80  { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
81  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
82 #define UVC_GUID_FORMAT_Y8 \
83  { 'Y', '8', ' ', ' ', 0x00, 0x00, 0x10, 0x00, \
84  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
85 #define UVC_GUID_FORMAT_Y10 \
86  { 'Y', '1', '0', ' ', 0x00, 0x00, 0x10, 0x00, \
87  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
88 #define UVC_GUID_FORMAT_Y12 \
89  { 'Y', '1', '2', ' ', 0x00, 0x00, 0x10, 0x00, \
90  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
91 #define UVC_GUID_FORMAT_Y16 \
92  { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
93  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
94 #define UVC_GUID_FORMAT_BY8 \
95  { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
96  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
97 #define UVC_GUID_FORMAT_RGBP \
98  { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
99  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
100 #define UVC_GUID_FORMAT_M420 \
101  { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
102  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
103 
104 #define UVC_GUID_FORMAT_H264 \
105  { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, \
106  0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
107 
108 /* ------------------------------------------------------------------------
109  * Driver specific constants.
110  */
111 
112 #define DRIVER_VERSION "1.1.1"
113 
114 /* Number of isochronous URBs. */
115 #define UVC_URBS 5
116 /* Maximum number of packets per URB. */
117 #define UVC_MAX_PACKETS 32
118 /* Maximum number of video buffers. */
119 #define UVC_MAX_VIDEO_BUFFERS 32
120 /* Maximum status buffer size in bytes of interrupt URB. */
121 #define UVC_MAX_STATUS_SIZE 16
122 
123 #define UVC_CTRL_CONTROL_TIMEOUT 300
124 #define UVC_CTRL_STREAMING_TIMEOUT 5000
125 
126 /* Maximum allowed number of control mappings per device */
127 #define UVC_MAX_CONTROL_MAPPINGS 1024
128 #define UVC_MAX_CONTROL_MENU_ENTRIES 32
129 
130 /* Devices quirks */
131 #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
132 #define UVC_QUIRK_PROBE_MINMAX 0x00000002
133 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
134 #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
135 #define UVC_QUIRK_STREAM_NO_FID 0x00000010
136 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
137 #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
138 #define UVC_QUIRK_PROBE_DEF 0x00000100
139 #define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
140 
141 /* Format flags */
142 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
143 #define UVC_FMT_FLAG_STREAM 0x00000002
144 
145 /* ------------------------------------------------------------------------
146  * Structures.
147  */
148 
149 struct uvc_device;
150 
151 /* TODO: Put the most frequently accessed fields at the beginning of
152  * structures to maximize cache efficiency.
153  */
156 
158  __u8 index; /* Bit index in bmControls */
160 
163 };
164 
166  struct list_head list;
168 
170  __u8 name[32];
173 
178 
181 
185 
187  const __u8 *data);
189  __u8 *data);
190 };
191 
192 struct uvc_control {
195 
196  __u8 index; /* Used to match the uvc_control entry with a
197  uvc_control_info. */
199  loaded:1,
200  modified:1,
201  cached:1,
202  initialized:1;
203 
205 };
206 
208  char *name;
209  __u8 guid[16];
211 };
212 
213 /* The term 'entity' refers to both UVC units and UVC terminals.
214  *
215  * The type field is either the terminal type (wTerminalType in the terminal
216  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
217  * As the bDescriptorSubtype field is one byte long, the type value will
218  * always have a null MSB for units. All terminal types defined by the UVC
219  * specification have a non-null MSB, so it is safe to use the MSB to
220  * differentiate between units and terminals as long as the descriptor parsing
221  * code makes sure terminal types have a non-null MSB.
222  *
223  * For terminals, the type's most significant bit stores the terminal
224  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
225  * always be accessed with the UVC_ENTITY_* macros and never directly.
226  */
227 
228 struct uvc_entity {
229  struct list_head list; /* Entity as part of a UVC device. */
230  struct list_head chain; /* Entity as part of a video device
231  * chain. */
234  char name[64];
235 
236  /* Media controller-related fields. */
239  unsigned int num_pads;
240  unsigned int num_links;
241  struct media_pad *pads;
242 
243  union {
244  struct {
250  } camera;
251 
252  struct {
254  __u8 *bmControls;
257  } media;
258 
259  struct {
260  } output;
261 
262  struct {
265  __u8 *bmControls;
267  } processing;
268 
269  struct {
270  } selector;
271 
272  struct {
276  __u8 *bmControls;
278  } extension;
279  };
280 
283 
284  unsigned int ncontrols;
286 };
287 
288 struct uvc_frame {
299 };
300 
301 struct uvc_format {
308 
309  char name[32];
310 
311  unsigned int nframes;
312  struct uvc_frame *frame;
313 };
314 
321  /* The following fields are used by input headers only. */
326 };
327 
335 };
336 
337 struct uvc_buffer {
338  struct vb2_buffer buf;
339  struct list_head queue;
340 
342  unsigned int error;
343 
344  void *mem;
345  unsigned int length;
346  unsigned int bytesused;
347 
349 };
350 
351 #define UVC_QUEUE_DISCONNECTED (1 << 0)
352 #define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
353 
355  struct vb2_queue queue;
356  struct mutex mutex; /* Protects queue */
357 
358  unsigned int flags;
359  unsigned int buf_used;
360 
361  spinlock_t irqlock; /* Protects irqqueue */
363 };
364 
366  struct uvc_device *dev;
367  struct list_head list;
368 
369  struct list_head entities; /* All entities */
370  struct uvc_entity *processing; /* Processing unit */
371  struct uvc_entity *selector; /* Selector unit */
372 
373  struct mutex ctrl_mutex; /* Protects ctrl.info */
374 };
375 
377  unsigned int size; /* Number of bytes captured */
378  unsigned int first_data; /* Index of the first non-empty packet */
379 
380  unsigned int nb_packets; /* Number of packets */
381  unsigned int nb_empty; /* Number of empty packets */
382  unsigned int nb_invalid; /* Number of packets with an invalid header */
383  unsigned int nb_errors; /* Number of packets with the error bit set */
384 
385  unsigned int nb_pts; /* Number of packets with a PTS timestamp */
386  unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
387  unsigned int last_pts_diff; /* Index of the last PTS difference */
388  bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
389  bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
390  u32 pts; /* PTS of the last packet */
391 
392  unsigned int nb_scr; /* Number of packets with a SCR timestamp */
393  unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
394  u16 scr_sof; /* SCR.SOF of the last packet */
395  u32 scr_stc; /* SCR.STC of the last packet */
396 };
397 
399  struct timespec start_ts; /* Stream start timestamp */
400  struct timespec stop_ts; /* Stream stop timestamp */
401 
402  unsigned int nb_frames; /* Number of frames */
403 
404  unsigned int nb_packets; /* Number of packets */
405  unsigned int nb_empty; /* Number of empty packets */
406  unsigned int nb_invalid; /* Number of packets with an invalid header */
407  unsigned int nb_errors; /* Number of packets with the error bit set */
408 
409  unsigned int nb_pts_constant; /* Number of frames with constant PTS */
410  unsigned int nb_pts_early; /* Number of frames with early PTS */
411  unsigned int nb_pts_initial; /* Number of frames with initial PTS */
412 
413  unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
414  unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
415  unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
416  unsigned int scr_sof; /* STC.SOF of the last packet */
417  unsigned int min_sof; /* Minimum STC.SOF value */
418  unsigned int max_sof; /* Maximum STC.SOF value */
419 };
420 
422  struct list_head list;
423  struct uvc_device *dev;
427 
429  int intfnum;
431 
434 
435  unsigned int nformats;
437 
441  /* Protect access to ctrl, cur_format, cur_frame and hardware video
442  * probe control.
443  */
444  struct mutex mutex;
445 
446  /* Buffers queue. */
447  unsigned int frozen : 1;
449  void (*decode) (struct urb *urb, struct uvc_streaming *video,
450  struct uvc_buffer *buf);
451 
452  /* Context data used by the bulk completion handler. */
453  struct {
454  __u8 header[256];
455  unsigned int header_size;
459  } bulk;
460 
461  struct urb *urb[UVC_URBS];
464  unsigned int urb_size;
465 
468 
469  /* debugfs */
471  struct {
474  } stats;
475 
476  /* Timestamps support. */
477  struct uvc_clock {
483  } *samples;
484 
485  unsigned int head;
486  unsigned int count;
487  unsigned int size;
488 
491 
493  } clock;
494 };
495 
498 };
499 
500 struct uvc_device {
501  struct usb_device *udev;
503  unsigned long warnings;
505  int intfnum;
506  char name[32];
507 
511 
512  /* Video control interface */
513 #ifdef CONFIG_MEDIA_CONTROLLER
514  struct media_device mdev;
515 #endif
519 
522 
523  /* Video Streaming interfaces */
526 
527  /* Status Interrupt Endpoint */
528  struct usb_host_endpoint *int_ep;
529  struct urb *int_urb;
531  struct input_dev *input;
532  char input_phys[64];
533 };
534 
538 };
539 
540 struct uvc_fh {
541  struct v4l2_fh vfh;
545 };
546 
547 struct uvc_driver {
548  struct usb_driver driver;
549 };
550 
551 /* ------------------------------------------------------------------------
552  * Debugging, printing and logging
553  */
554 
555 #define UVC_TRACE_PROBE (1 << 0)
556 #define UVC_TRACE_DESCR (1 << 1)
557 #define UVC_TRACE_CONTROL (1 << 2)
558 #define UVC_TRACE_FORMAT (1 << 3)
559 #define UVC_TRACE_CAPTURE (1 << 4)
560 #define UVC_TRACE_CALLS (1 << 5)
561 #define UVC_TRACE_IOCTL (1 << 6)
562 #define UVC_TRACE_FRAME (1 << 7)
563 #define UVC_TRACE_SUSPEND (1 << 8)
564 #define UVC_TRACE_STATUS (1 << 9)
565 #define UVC_TRACE_VIDEO (1 << 10)
566 #define UVC_TRACE_STATS (1 << 11)
567 #define UVC_TRACE_CLOCK (1 << 12)
568 
569 #define UVC_WARN_MINMAX 0
570 #define UVC_WARN_PROBE_DEF 1
571 #define UVC_WARN_XU_GET_RES 2
572 
573 extern unsigned int uvc_clock_param;
574 extern unsigned int uvc_no_drop_param;
575 extern unsigned int uvc_trace_param;
576 extern unsigned int uvc_timeout_param;
577 
578 #define uvc_trace(flag, msg...) \
579  do { \
580  if (uvc_trace_param & flag) \
581  printk(KERN_DEBUG "uvcvideo: " msg); \
582  } while (0)
583 
584 #define uvc_warn_once(dev, warn, msg...) \
585  do { \
586  if (!test_and_set_bit(warn, &dev->warnings)) \
587  printk(KERN_INFO "uvcvideo: " msg); \
588  } while (0)
589 
590 #define uvc_printk(level, msg...) \
591  printk(level "uvcvideo: " msg)
592 
593 /* --------------------------------------------------------------------------
594  * Internal functions.
595  */
596 
597 /* Core driver */
598 extern struct uvc_driver uvc_driver;
599 
600 extern struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
601 
602 /* Video buffers queue management. */
603 extern int uvc_queue_init(struct uvc_video_queue *queue,
604  enum v4l2_buf_type type, int drop_corrupted);
605 extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
606  struct v4l2_requestbuffers *rb);
607 extern void uvc_free_buffers(struct uvc_video_queue *queue);
608 extern int uvc_query_buffer(struct uvc_video_queue *queue,
609  struct v4l2_buffer *v4l2_buf);
610 extern int uvc_queue_buffer(struct uvc_video_queue *queue,
611  struct v4l2_buffer *v4l2_buf);
612 extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
613  struct v4l2_buffer *v4l2_buf, int nonblocking);
614 extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
615 extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
617  struct uvc_buffer *buf);
618 extern int uvc_queue_mmap(struct uvc_video_queue *queue,
619  struct vm_area_struct *vma);
620 extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
621  struct file *file, poll_table *wait);
622 #ifndef CONFIG_MMU
623 extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
624  unsigned long pgoff);
625 #endif
626 extern int uvc_queue_allocated(struct uvc_video_queue *queue);
627 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
628 {
629  return vb2_is_streaming(&queue->queue);
630 }
631 
632 /* V4L2 interface */
633 extern const struct v4l2_file_operations uvc_fops;
634 
635 /* Media controller */
637 extern void uvc_mc_cleanup_entity(struct uvc_entity *entity);
638 
639 /* Video */
640 extern int uvc_video_init(struct uvc_streaming *stream);
641 extern int uvc_video_suspend(struct uvc_streaming *stream);
642 extern int uvc_video_resume(struct uvc_streaming *stream, int reset);
643 extern int uvc_video_enable(struct uvc_streaming *stream, int enable);
644 extern int uvc_probe_video(struct uvc_streaming *stream,
645  struct uvc_streaming_control *probe);
646 extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
647  __u8 intfnum, __u8 cs, void *data, __u16 size);
648 void uvc_video_clock_update(struct uvc_streaming *stream,
649  struct v4l2_buffer *v4l2_buf,
650  struct uvc_buffer *buf);
651 
652 /* Status */
653 extern int uvc_status_init(struct uvc_device *dev);
654 extern void uvc_status_cleanup(struct uvc_device *dev);
655 extern int uvc_status_start(struct uvc_device *dev);
656 extern void uvc_status_stop(struct uvc_device *dev);
657 extern int uvc_status_suspend(struct uvc_device *dev);
658 extern int uvc_status_resume(struct uvc_device *dev);
659 
660 /* Controls */
662 
663 extern int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
664  struct v4l2_queryctrl *v4l2_ctrl);
665 extern int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
666  struct v4l2_querymenu *query_menu);
667 
668 extern int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
669  const struct uvc_control_mapping *mapping);
670 extern int uvc_ctrl_init_device(struct uvc_device *dev);
671 extern void uvc_ctrl_cleanup_device(struct uvc_device *dev);
672 extern int uvc_ctrl_resume_device(struct uvc_device *dev);
673 
674 extern int uvc_ctrl_begin(struct uvc_video_chain *chain);
675 extern int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
676  const struct v4l2_ext_control *xctrls,
677  unsigned int xctrls_count);
678 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
679  const struct v4l2_ext_control *xctrls,
680  unsigned int xctrls_count)
681 {
682  return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
683 }
684 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
685 {
686  return __uvc_ctrl_commit(handle, 1, NULL, 0);
687 }
688 
689 extern int uvc_ctrl_get(struct uvc_video_chain *chain,
690  struct v4l2_ext_control *xctrl);
691 extern int uvc_ctrl_set(struct uvc_video_chain *chain,
692  struct v4l2_ext_control *xctrl);
693 
694 extern int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
695  struct uvc_xu_control_query *xqry);
696 
697 /* Utility functions */
698 extern void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
699  unsigned int n_terms, unsigned int threshold);
701  uint32_t denominator);
702 extern struct usb_host_endpoint *uvc_find_endpoint(
703  struct usb_host_interface *alts, __u8 epaddr);
704 
705 /* Quirks support */
706 void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
707  struct uvc_buffer *buf);
708 
709 /* debugfs and statistics */
710 int uvc_debugfs_init(void);
711 void uvc_debugfs_cleanup(void);
712 int uvc_debugfs_init_stream(struct uvc_streaming *stream);
713 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
714 
715 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
716  size_t size);
717 
718 #endif