5 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
6 #endif /* __KERNEL__ */
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/videobuf2-core.h>
18 /* --------------------------------------------------------------------------
22 #define UVC_TERM_INPUT 0x0000
23 #define UVC_TERM_OUTPUT 0x8000
24 #define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
26 #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
27 #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
28 #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
29 #define UVC_ENTITY_IS_ITERM(entity) \
30 (UVC_ENTITY_IS_TERM(entity) && \
31 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
32 #define UVC_ENTITY_IS_OTERM(entity) \
33 (UVC_ENTITY_IS_TERM(entity) && \
34 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
37 /* ------------------------------------------------------------------------
40 #define UVC_GUID_UVC_CAMERA \
41 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
43 #define UVC_GUID_UVC_OUTPUT \
44 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
46 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
47 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
49 #define UVC_GUID_UVC_PROCESSING \
50 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
52 #define UVC_GUID_UVC_SELECTOR \
53 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
56 #define UVC_GUID_FORMAT_MJPEG \
57 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
58 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
59 #define UVC_GUID_FORMAT_YUY2 \
60 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
61 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
62 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
63 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
64 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
65 #define UVC_GUID_FORMAT_NV12 \
66 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
67 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
68 #define UVC_GUID_FORMAT_YV12 \
69 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
70 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
71 #define UVC_GUID_FORMAT_I420 \
72 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
73 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
74 #define UVC_GUID_FORMAT_UYVY \
75 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
76 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
77 #define UVC_GUID_FORMAT_Y800 \
78 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
79 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
80 #define UVC_GUID_FORMAT_Y16 \
81 { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
82 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
83 #define UVC_GUID_FORMAT_BY8 \
84 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
85 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
86 #define UVC_GUID_FORMAT_RGBP \
87 { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
88 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
89 #define UVC_GUID_FORMAT_M420 \
90 { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
91 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
93 #define UVC_GUID_FORMAT_H264 \
94 { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, \
95 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
97 /* ------------------------------------------------------------------------
98 * Driver specific constants.
101 #define DRIVER_VERSION "1.1.1"
103 /* Number of isochronous URBs. */
105 /* Maximum number of packets per URB. */
106 #define UVC_MAX_PACKETS 32
107 /* Maximum number of video buffers. */
108 #define UVC_MAX_VIDEO_BUFFERS 32
109 /* Maximum status buffer size in bytes of interrupt URB. */
110 #define UVC_MAX_STATUS_SIZE 16
112 #define UVC_CTRL_CONTROL_TIMEOUT 300
113 #define UVC_CTRL_STREAMING_TIMEOUT 5000
115 /* Maximum allowed number of control mappings per device */
116 #define UVC_MAX_CONTROL_MAPPINGS 1024
117 #define UVC_MAX_CONTROL_MENU_ENTRIES 32
120 #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
121 #define UVC_QUIRK_PROBE_MINMAX 0x00000002
122 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
123 #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
124 #define UVC_QUIRK_STREAM_NO_FID 0x00000010
125 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
126 #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
127 #define UVC_QUIRK_PROBE_DEF 0x00000100
128 #define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
131 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
132 #define UVC_FMT_FLAG_STREAM 0x00000002
134 /* ------------------------------------------------------------------------
140 /* TODO: Put the most frequently accessed fields at the beginning of
141 * structures to maximize cache efficiency.
143 struct uvc_control_info
{
144 struct list_head mappings
;
147 __u8 index
; /* Bit index in bmControls */
154 struct uvc_control_mapping
{
155 struct list_head list
;
157 struct uvc_control_info
*ctrl
;
166 enum v4l2_ctrl_type v4l2_type
;
169 struct uvc_menu_info
*menu_info
;
172 __s32 (*get
) (struct uvc_control_mapping
*mapping
, __u8 query
,
174 void (*set
) (struct uvc_control_mapping
*mapping
, __s32 value
,
179 struct uvc_entity
*entity
;
180 struct uvc_control_info info
;
182 __u8 index
; /* Used to match the uvc_control entry with a
193 struct uvc_format_desc
{
199 /* The term 'entity' refers to both UVC units and UVC terminals.
201 * The type field is either the terminal type (wTerminalType in the terminal
202 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
203 * As the bDescriptorSubtype field is one byte long, the type value will
204 * always have a null MSB for units. All terminal types defined by the UVC
205 * specification have a non-null MSB, so it is safe to use the MSB to
206 * differentiate between units and terminals as long as the descriptor parsing
207 * code makes sure terminal types have a non-null MSB.
209 * For terminals, the type's most significant bit stores the terminal
210 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
211 * always be accessed with the UVC_ENTITY_* macros and never directly.
215 struct list_head list
; /* Entity as part of a UVC device. */
216 struct list_head chain
; /* Entity as part of a video device
222 /* Media controller-related fields. */
223 struct video_device
*vdev
;
224 struct v4l2_subdev subdev
;
225 unsigned int num_pads
;
226 unsigned int num_links
;
227 struct media_pad
*pads
;
231 __u16 wObjectiveFocalLengthMin
;
232 __u16 wObjectiveFocalLengthMax
;
233 __u16 wOcularFocalLength
;
241 __u8 bTransportModeSize
;
242 __u8
*bmTransportModes
;
249 __u16 wMaxMultiplier
;
252 __u8 bmVideoStandards
;
259 __u8 guidExtensionCode
[16];
263 __u8
*bmControlsType
;
270 unsigned int ncontrols
;
271 struct uvc_control
*controls
;
281 __u32 dwMaxVideoFrameBufferSize
;
282 __u8 bFrameIntervalType
;
283 __u32 dwDefaultFrameInterval
;
284 __u32
*dwFrameInterval
;
297 unsigned int nframes
;
298 struct uvc_frame
*frame
;
301 struct uvc_streaming_header
{
303 __u8 bEndpointAddress
;
307 /* The following fields are used by input headers only. */
309 __u8 bStillCaptureMethod
;
310 __u8 bTriggerSupport
;
314 enum uvc_buffer_state
{
315 UVC_BUF_STATE_IDLE
= 0,
316 UVC_BUF_STATE_QUEUED
= 1,
317 UVC_BUF_STATE_ACTIVE
= 2,
318 UVC_BUF_STATE_READY
= 3,
319 UVC_BUF_STATE_DONE
= 4,
320 UVC_BUF_STATE_ERROR
= 5,
324 struct vb2_buffer buf
;
325 struct list_head queue
;
327 enum uvc_buffer_state state
;
332 unsigned int bytesused
;
337 #define UVC_QUEUE_DISCONNECTED (1 << 0)
338 #define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
340 struct uvc_video_queue
{
341 struct vb2_queue queue
;
342 struct mutex mutex
; /* Protects queue */
345 unsigned int buf_used
;
347 spinlock_t irqlock
; /* Protects irqqueue */
348 struct list_head irqqueue
;
351 struct uvc_video_chain
{
352 struct uvc_device
*dev
;
353 struct list_head list
;
355 struct list_head entities
; /* All entities */
356 struct uvc_entity
*processing
; /* Processing unit */
357 struct uvc_entity
*selector
; /* Selector unit */
359 struct mutex ctrl_mutex
; /* Protects ctrl.info */
362 struct uvc_stats_frame
{
363 unsigned int size
; /* Number of bytes captured */
364 unsigned int first_data
; /* Index of the first non-empty packet */
366 unsigned int nb_packets
; /* Number of packets */
367 unsigned int nb_empty
; /* Number of empty packets */
368 unsigned int nb_invalid
; /* Number of packets with an invalid header */
369 unsigned int nb_errors
; /* Number of packets with the error bit set */
371 unsigned int nb_pts
; /* Number of packets with a PTS timestamp */
372 unsigned int nb_pts_diffs
; /* Number of PTS differences inside a frame */
373 unsigned int last_pts_diff
; /* Index of the last PTS difference */
374 bool has_initial_pts
; /* Whether the first non-empty packet has a PTS */
375 bool has_early_pts
; /* Whether a PTS is present before the first non-empty packet */
376 u32 pts
; /* PTS of the last packet */
378 unsigned int nb_scr
; /* Number of packets with a SCR timestamp */
379 unsigned int nb_scr_diffs
; /* Number of SCR.STC differences inside a frame */
380 u16 scr_sof
; /* SCR.SOF of the last packet */
381 u32 scr_stc
; /* SCR.STC of the last packet */
384 struct uvc_stats_stream
{
385 struct timespec start_ts
; /* Stream start timestamp */
386 struct timespec stop_ts
; /* Stream stop timestamp */
388 unsigned int nb_frames
; /* Number of frames */
390 unsigned int nb_packets
; /* Number of packets */
391 unsigned int nb_empty
; /* Number of empty packets */
392 unsigned int nb_invalid
; /* Number of packets with an invalid header */
393 unsigned int nb_errors
; /* Number of packets with the error bit set */
395 unsigned int nb_pts_constant
; /* Number of frames with constant PTS */
396 unsigned int nb_pts_early
; /* Number of frames with early PTS */
397 unsigned int nb_pts_initial
; /* Number of frames with initial PTS */
399 unsigned int nb_scr_count_ok
; /* Number of frames with at least one SCR per non empty packet */
400 unsigned int nb_scr_diffs_ok
; /* Number of frames with varying SCR.STC */
401 unsigned int scr_sof_count
; /* STC.SOF counter accumulated since stream start */
402 unsigned int scr_sof
; /* STC.SOF of the last packet */
403 unsigned int min_sof
; /* Minimum STC.SOF value */
404 unsigned int max_sof
; /* Maximum STC.SOF value */
407 struct uvc_streaming
{
408 struct list_head list
;
409 struct uvc_device
*dev
;
410 struct video_device
*vdev
;
411 struct uvc_video_chain
*chain
;
414 struct usb_interface
*intf
;
418 struct uvc_streaming_header header
;
419 enum v4l2_buf_type type
;
421 unsigned int nformats
;
422 struct uvc_format
*format
;
424 struct uvc_streaming_control ctrl
;
425 struct uvc_format
*cur_format
;
426 struct uvc_frame
*cur_frame
;
427 /* Protect access to ctrl, cur_format, cur_frame and hardware video
433 unsigned int frozen
: 1;
434 struct uvc_video_queue queue
;
435 void (*decode
) (struct urb
*urb
, struct uvc_streaming
*video
,
436 struct uvc_buffer
*buf
);
438 /* Context data used by the bulk completion handler. */
441 unsigned int header_size
;
444 __u32 max_payload_size
;
447 struct urb
*urb
[UVC_URBS
];
448 char *urb_buffer
[UVC_URBS
];
449 dma_addr_t urb_dma
[UVC_URBS
];
450 unsigned int urb_size
;
456 struct dentry
*debugfs_dir
;
458 struct uvc_stats_frame frame
;
459 struct uvc_stats_stream stream
;
462 /* Timestamps support. */
464 struct uvc_clock_sample
{
467 struct timespec host_ts
;
482 enum uvc_device_state
{
483 UVC_DEV_DISCONNECTED
= 1,
487 struct usb_device
*udev
;
488 struct usb_interface
*intf
;
489 unsigned long warnings
;
494 enum uvc_device_state state
;
498 /* Video control interface */
499 #ifdef CONFIG_MEDIA_CONTROLLER
500 struct media_device mdev
;
502 struct v4l2_device vdev
;
504 __u32 clock_frequency
;
506 struct list_head entities
;
507 struct list_head chains
;
509 /* Video Streaming interfaces */
510 struct list_head streams
;
513 /* Status Interrupt Endpoint */
514 struct usb_host_endpoint
*int_ep
;
517 struct input_dev
*input
;
521 enum uvc_handle_state
{
522 UVC_HANDLE_PASSIVE
= 0,
523 UVC_HANDLE_ACTIVE
= 1,
527 struct uvc_video_chain
*chain
;
528 struct uvc_streaming
*stream
;
529 enum uvc_handle_state state
;
533 struct usb_driver driver
;
536 /* ------------------------------------------------------------------------
537 * Debugging, printing and logging
540 #define UVC_TRACE_PROBE (1 << 0)
541 #define UVC_TRACE_DESCR (1 << 1)
542 #define UVC_TRACE_CONTROL (1 << 2)
543 #define UVC_TRACE_FORMAT (1 << 3)
544 #define UVC_TRACE_CAPTURE (1 << 4)
545 #define UVC_TRACE_CALLS (1 << 5)
546 #define UVC_TRACE_IOCTL (1 << 6)
547 #define UVC_TRACE_FRAME (1 << 7)
548 #define UVC_TRACE_SUSPEND (1 << 8)
549 #define UVC_TRACE_STATUS (1 << 9)
550 #define UVC_TRACE_VIDEO (1 << 10)
551 #define UVC_TRACE_STATS (1 << 11)
552 #define UVC_TRACE_CLOCK (1 << 12)
554 #define UVC_WARN_MINMAX 0
555 #define UVC_WARN_PROBE_DEF 1
556 #define UVC_WARN_XU_GET_RES 2
558 extern unsigned int uvc_clock_param
;
559 extern unsigned int uvc_no_drop_param
;
560 extern unsigned int uvc_trace_param
;
561 extern unsigned int uvc_timeout_param
;
563 #define uvc_trace(flag, msg...) \
565 if (uvc_trace_param & flag) \
566 printk(KERN_DEBUG "uvcvideo: " msg); \
569 #define uvc_warn_once(dev, warn, msg...) \
571 if (!test_and_set_bit(warn, &dev->warnings)) \
572 printk(KERN_INFO "uvcvideo: " msg); \
575 #define uvc_printk(level, msg...) \
576 printk(level "uvcvideo: " msg)
578 /* --------------------------------------------------------------------------
579 * Internal functions.
583 extern struct uvc_driver uvc_driver
;
585 extern struct uvc_entity
*uvc_entity_by_id(struct uvc_device
*dev
, int id
);
587 /* Video buffers queue management. */
588 extern void uvc_queue_init(struct uvc_video_queue
*queue
,
589 enum v4l2_buf_type type
, int drop_corrupted
);
590 extern int uvc_alloc_buffers(struct uvc_video_queue
*queue
,
591 struct v4l2_requestbuffers
*rb
);
592 extern void uvc_free_buffers(struct uvc_video_queue
*queue
);
593 extern int uvc_query_buffer(struct uvc_video_queue
*queue
,
594 struct v4l2_buffer
*v4l2_buf
);
595 extern int uvc_queue_buffer(struct uvc_video_queue
*queue
,
596 struct v4l2_buffer
*v4l2_buf
);
597 extern int uvc_dequeue_buffer(struct uvc_video_queue
*queue
,
598 struct v4l2_buffer
*v4l2_buf
, int nonblocking
);
599 extern int uvc_queue_enable(struct uvc_video_queue
*queue
, int enable
);
600 extern void uvc_queue_cancel(struct uvc_video_queue
*queue
, int disconnect
);
601 extern struct uvc_buffer
*uvc_queue_next_buffer(struct uvc_video_queue
*queue
,
602 struct uvc_buffer
*buf
);
603 extern int uvc_queue_mmap(struct uvc_video_queue
*queue
,
604 struct vm_area_struct
*vma
);
605 extern unsigned int uvc_queue_poll(struct uvc_video_queue
*queue
,
606 struct file
*file
, poll_table
*wait
);
608 extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue
*queue
,
609 unsigned long pgoff
);
611 extern int uvc_queue_allocated(struct uvc_video_queue
*queue
);
612 static inline int uvc_queue_streaming(struct uvc_video_queue
*queue
)
614 return vb2_is_streaming(&queue
->queue
);
618 extern const struct v4l2_file_operations uvc_fops
;
620 /* Media controller */
621 extern int uvc_mc_register_entities(struct uvc_video_chain
*chain
);
622 extern void uvc_mc_cleanup_entity(struct uvc_entity
*entity
);
625 extern int uvc_video_init(struct uvc_streaming
*stream
);
626 extern int uvc_video_suspend(struct uvc_streaming
*stream
);
627 extern int uvc_video_resume(struct uvc_streaming
*stream
, int reset
);
628 extern int uvc_video_enable(struct uvc_streaming
*stream
, int enable
);
629 extern int uvc_probe_video(struct uvc_streaming
*stream
,
630 struct uvc_streaming_control
*probe
);
631 extern int uvc_query_ctrl(struct uvc_device
*dev
, __u8 query
, __u8 unit
,
632 __u8 intfnum
, __u8 cs
, void *data
, __u16 size
);
633 void uvc_video_clock_update(struct uvc_streaming
*stream
,
634 struct v4l2_buffer
*v4l2_buf
,
635 struct uvc_buffer
*buf
);
638 extern int uvc_status_init(struct uvc_device
*dev
);
639 extern void uvc_status_cleanup(struct uvc_device
*dev
);
640 extern int uvc_status_start(struct uvc_device
*dev
);
641 extern void uvc_status_stop(struct uvc_device
*dev
);
642 extern int uvc_status_suspend(struct uvc_device
*dev
);
643 extern int uvc_status_resume(struct uvc_device
*dev
);
646 extern int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
647 struct v4l2_queryctrl
*v4l2_ctrl
);
648 extern int uvc_query_v4l2_menu(struct uvc_video_chain
*chain
,
649 struct v4l2_querymenu
*query_menu
);
651 extern int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
652 const struct uvc_control_mapping
*mapping
);
653 extern int uvc_ctrl_init_device(struct uvc_device
*dev
);
654 extern void uvc_ctrl_cleanup_device(struct uvc_device
*dev
);
655 extern int uvc_ctrl_resume_device(struct uvc_device
*dev
);
657 extern int uvc_ctrl_begin(struct uvc_video_chain
*chain
);
658 extern int __uvc_ctrl_commit(struct uvc_video_chain
*chain
, int rollback
);
659 static inline int uvc_ctrl_commit(struct uvc_video_chain
*chain
)
661 return __uvc_ctrl_commit(chain
, 0);
663 static inline int uvc_ctrl_rollback(struct uvc_video_chain
*chain
)
665 return __uvc_ctrl_commit(chain
, 1);
668 extern int uvc_ctrl_get(struct uvc_video_chain
*chain
,
669 struct v4l2_ext_control
*xctrl
);
670 extern int uvc_ctrl_set(struct uvc_video_chain
*chain
,
671 struct v4l2_ext_control
*xctrl
);
673 extern int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
674 struct uvc_xu_control_query
*xqry
);
676 /* Utility functions */
677 extern void uvc_simplify_fraction(uint32_t *numerator
, uint32_t *denominator
,
678 unsigned int n_terms
, unsigned int threshold
);
679 extern uint32_t uvc_fraction_to_interval(uint32_t numerator
,
680 uint32_t denominator
);
681 extern struct usb_host_endpoint
*uvc_find_endpoint(
682 struct usb_host_interface
*alts
, __u8 epaddr
);
685 void uvc_video_decode_isight(struct urb
*urb
, struct uvc_streaming
*stream
,
686 struct uvc_buffer
*buf
);
688 /* debugfs and statistics */
689 int uvc_debugfs_init(void);
690 void uvc_debugfs_cleanup(void);
691 int uvc_debugfs_init_stream(struct uvc_streaming
*stream
);
692 void uvc_debugfs_cleanup_stream(struct uvc_streaming
*stream
);
694 size_t uvc_video_stats_dump(struct uvc_streaming
*stream
, char *buf
,