4 #include <linux/kernel.h>
5 #include <linux/videodev2.h>
11 /* Data types for UVC control data */
12 #define UVC_CTRL_DATA_TYPE_RAW 0
13 #define UVC_CTRL_DATA_TYPE_SIGNED 1
14 #define UVC_CTRL_DATA_TYPE_UNSIGNED 2
15 #define UVC_CTRL_DATA_TYPE_BOOLEAN 3
16 #define UVC_CTRL_DATA_TYPE_ENUM 4
17 #define UVC_CTRL_DATA_TYPE_BITMASK 5
20 #define UVC_CONTROL_SET_CUR (1 << 0)
21 #define UVC_CONTROL_GET_CUR (1 << 1)
22 #define UVC_CONTROL_GET_MIN (1 << 2)
23 #define UVC_CONTROL_GET_MAX (1 << 3)
24 #define UVC_CONTROL_GET_RES (1 << 4)
25 #define UVC_CONTROL_GET_DEF (1 << 5)
26 /* Control should be saved at suspend and restored at resume. */
27 #define UVC_CONTROL_RESTORE (1 << 6)
28 /* Control can be updated by the camera. */
29 #define UVC_CONTROL_AUTO_UPDATE (1 << 7)
31 #define UVC_CONTROL_GET_RANGE (UVC_CONTROL_GET_CUR | UVC_CONTROL_GET_MIN | \
32 UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES | \
35 struct uvc_xu_control_info
{
43 struct uvc_menu_info
{
48 struct uvc_xu_control_mapping_old
{
52 struct uvc_xu_control_mapping
{
60 enum v4l2_ctrl_type v4l2_type
;
63 struct uvc_menu_info __user
*menu_info
;
69 struct uvc_xu_control
{
76 #define UVCIOC_CTRL_ADD _IOW('U', 1, struct uvc_xu_control_info)
77 #define UVCIOC_CTRL_MAP_OLD _IOWR('U', 2, struct uvc_xu_control_mapping_old)
78 #define UVCIOC_CTRL_MAP _IOWR('U', 2, struct uvc_xu_control_mapping)
79 #define UVCIOC_CTRL_GET _IOWR('U', 3, struct uvc_xu_control)
80 #define UVCIOC_CTRL_SET _IOW('U', 4, struct uvc_xu_control)
84 #include <linux/poll.h>
85 #include <linux/usb/video.h>
87 /* --------------------------------------------------------------------------
91 #define UVC_TERM_INPUT 0x0000
92 #define UVC_TERM_OUTPUT 0x8000
93 #define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
95 #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
96 #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
97 #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
98 #define UVC_ENTITY_IS_ITERM(entity) \
99 (UVC_ENTITY_IS_TERM(entity) && \
100 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
101 #define UVC_ENTITY_IS_OTERM(entity) \
102 (UVC_ENTITY_IS_TERM(entity) && \
103 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
106 /* ------------------------------------------------------------------------
109 #define UVC_GUID_UVC_CAMERA \
110 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
112 #define UVC_GUID_UVC_OUTPUT \
113 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
115 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
116 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
118 #define UVC_GUID_UVC_PROCESSING \
119 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
121 #define UVC_GUID_UVC_SELECTOR \
122 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
125 #define UVC_GUID_FORMAT_MJPEG \
126 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
127 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
128 #define UVC_GUID_FORMAT_YUY2 \
129 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
130 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
131 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
132 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
133 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
134 #define UVC_GUID_FORMAT_NV12 \
135 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
136 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
137 #define UVC_GUID_FORMAT_YV12 \
138 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
139 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
140 #define UVC_GUID_FORMAT_I420 \
141 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
142 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
143 #define UVC_GUID_FORMAT_UYVY \
144 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
145 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
146 #define UVC_GUID_FORMAT_Y800 \
147 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
148 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
149 #define UVC_GUID_FORMAT_Y16 \
150 { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
151 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
152 #define UVC_GUID_FORMAT_BY8 \
153 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
154 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
156 /* ------------------------------------------------------------------------
157 * Driver specific constants.
160 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(1, 0, 0)
161 #define DRIVER_VERSION "v1.0.0"
163 /* Number of isochronous URBs. */
165 /* Maximum number of packets per URB. */
166 #define UVC_MAX_PACKETS 32
167 /* Maximum number of video buffers. */
168 #define UVC_MAX_VIDEO_BUFFERS 32
169 /* Maximum status buffer size in bytes of interrupt URB. */
170 #define UVC_MAX_STATUS_SIZE 16
172 #define UVC_CTRL_CONTROL_TIMEOUT 300
173 #define UVC_CTRL_STREAMING_TIMEOUT 5000
175 /* Maximum allowed number of control mappings per device */
176 #define UVC_MAX_CONTROL_MAPPINGS 1024
179 #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
180 #define UVC_QUIRK_PROBE_MINMAX 0x00000002
181 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
182 #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
183 #define UVC_QUIRK_STREAM_NO_FID 0x00000010
184 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
185 #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
186 #define UVC_QUIRK_PROBE_DEF 0x00000100
187 #define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
190 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
191 #define UVC_FMT_FLAG_STREAM 0x00000002
193 /* ------------------------------------------------------------------------
199 /* TODO: Put the most frequently accessed fields at the beginning of
200 * structures to maximize cache efficiency.
202 struct uvc_control_info
{
203 struct list_head mappings
;
206 __u8 index
; /* Bit index in bmControls */
213 struct uvc_control_mapping
{
214 struct list_head list
;
216 struct uvc_control_info
*ctrl
;
225 enum v4l2_ctrl_type v4l2_type
;
228 struct uvc_menu_info
*menu_info
;
231 __s32 (*get
) (struct uvc_control_mapping
*mapping
, __u8 query
,
233 void (*set
) (struct uvc_control_mapping
*mapping
, __s32 value
,
238 struct uvc_entity
*entity
;
239 struct uvc_control_info info
;
241 __u8 index
; /* Used to match the uvc_control entry with a
252 struct uvc_format_desc
{
258 /* The term 'entity' refers to both UVC units and UVC terminals.
260 * The type field is either the terminal type (wTerminalType in the terminal
261 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
262 * As the bDescriptorSubtype field is one byte long, the type value will
263 * always have a null MSB for units. All terminal types defined by the UVC
264 * specification have a non-null MSB, so it is safe to use the MSB to
265 * differentiate between units and terminals as long as the descriptor parsing
266 * code makes sure terminal types have a non-null MSB.
268 * For terminals, the type's most significant bit stores the terminal
269 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
270 * always be accessed with the UVC_ENTITY_* macros and never directly.
274 struct list_head list
; /* Entity as part of a UVC device. */
275 struct list_head chain
; /* Entity as part of a video device
283 __u16 wObjectiveFocalLengthMin
;
284 __u16 wObjectiveFocalLengthMax
;
285 __u16 wOcularFocalLength
;
293 __u8 bTransportModeSize
;
294 __u8
*bmTransportModes
;
301 __u16 wMaxMultiplier
;
304 __u8 bmVideoStandards
;
311 __u8 guidExtensionCode
[16];
315 __u8
*bmControlsType
;
322 unsigned int ncontrols
;
323 struct uvc_control
*controls
;
333 __u32 dwMaxVideoFrameBufferSize
;
334 __u8 bFrameIntervalType
;
335 __u32 dwDefaultFrameInterval
;
336 __u32
*dwFrameInterval
;
349 unsigned int nframes
;
350 struct uvc_frame
*frame
;
353 struct uvc_streaming_header
{
355 __u8 bEndpointAddress
;
359 /* The following fields are used by input headers only. */
361 __u8 bStillCaptureMethod
;
362 __u8 bTriggerSupport
;
366 enum uvc_buffer_state
{
367 UVC_BUF_STATE_IDLE
= 0,
368 UVC_BUF_STATE_QUEUED
= 1,
369 UVC_BUF_STATE_ACTIVE
= 2,
370 UVC_BUF_STATE_READY
= 3,
371 UVC_BUF_STATE_DONE
= 4,
372 UVC_BUF_STATE_ERROR
= 5,
376 unsigned long vma_use_count
;
377 struct list_head stream
;
379 /* Touched by interrupt handler. */
380 struct v4l2_buffer buf
;
381 struct list_head queue
;
382 wait_queue_head_t wait
;
383 enum uvc_buffer_state state
;
387 #define UVC_QUEUE_STREAMING (1 << 0)
388 #define UVC_QUEUE_DISCONNECTED (1 << 1)
389 #define UVC_QUEUE_DROP_CORRUPTED (1 << 2)
391 struct uvc_video_queue
{
392 enum v4l2_buf_type type
;
398 unsigned int buf_size
;
399 unsigned int buf_used
;
400 struct uvc_buffer buffer
[UVC_MAX_VIDEO_BUFFERS
];
401 struct mutex mutex
; /* protects buffers and mainqueue */
402 spinlock_t irqlock
; /* protects irqqueue */
404 struct list_head mainqueue
;
405 struct list_head irqqueue
;
408 struct uvc_video_chain
{
409 struct uvc_device
*dev
;
410 struct list_head list
;
412 struct list_head entities
; /* All entities */
413 struct uvc_entity
*processing
; /* Processing unit */
414 struct uvc_entity
*selector
; /* Selector unit */
416 struct mutex ctrl_mutex
; /* Protects ctrl.info */
419 struct uvc_streaming
{
420 struct list_head list
;
421 struct uvc_device
*dev
;
422 struct video_device
*vdev
;
423 struct uvc_video_chain
*chain
;
426 struct usb_interface
*intf
;
430 struct uvc_streaming_header header
;
431 enum v4l2_buf_type type
;
433 unsigned int nformats
;
434 struct uvc_format
*format
;
436 struct uvc_streaming_control ctrl
;
437 struct uvc_format
*cur_format
;
438 struct uvc_frame
*cur_frame
;
439 /* Protect access to ctrl, cur_format, cur_frame and hardware video
444 unsigned int frozen
: 1;
445 struct uvc_video_queue queue
;
446 void (*decode
) (struct urb
*urb
, struct uvc_streaming
*video
,
447 struct uvc_buffer
*buf
);
449 /* Context data used by the bulk completion handler. */
452 unsigned int header_size
;
455 __u32 max_payload_size
;
458 struct urb
*urb
[UVC_URBS
];
459 char *urb_buffer
[UVC_URBS
];
460 dma_addr_t urb_dma
[UVC_URBS
];
461 unsigned int urb_size
;
467 enum uvc_device_state
{
468 UVC_DEV_DISCONNECTED
= 1,
472 struct usb_device
*udev
;
473 struct usb_interface
*intf
;
474 unsigned long warnings
;
479 enum uvc_device_state state
;
483 /* Video control interface */
485 __u32 clock_frequency
;
487 struct list_head entities
;
488 struct list_head chains
;
490 /* Video Streaming interfaces */
491 struct list_head streams
;
494 /* Status Interrupt Endpoint */
495 struct usb_host_endpoint
*int_ep
;
498 struct input_dev
*input
;
502 enum uvc_handle_state
{
503 UVC_HANDLE_PASSIVE
= 0,
504 UVC_HANDLE_ACTIVE
= 1,
508 struct uvc_video_chain
*chain
;
509 struct uvc_streaming
*stream
;
510 enum uvc_handle_state state
;
514 struct usb_driver driver
;
517 /* ------------------------------------------------------------------------
518 * Debugging, printing and logging
521 #define UVC_TRACE_PROBE (1 << 0)
522 #define UVC_TRACE_DESCR (1 << 1)
523 #define UVC_TRACE_CONTROL (1 << 2)
524 #define UVC_TRACE_FORMAT (1 << 3)
525 #define UVC_TRACE_CAPTURE (1 << 4)
526 #define UVC_TRACE_CALLS (1 << 5)
527 #define UVC_TRACE_IOCTL (1 << 6)
528 #define UVC_TRACE_FRAME (1 << 7)
529 #define UVC_TRACE_SUSPEND (1 << 8)
530 #define UVC_TRACE_STATUS (1 << 9)
531 #define UVC_TRACE_VIDEO (1 << 10)
533 #define UVC_WARN_MINMAX 0
534 #define UVC_WARN_PROBE_DEF 1
536 extern unsigned int uvc_clock_param
;
537 extern unsigned int uvc_no_drop_param
;
538 extern unsigned int uvc_trace_param
;
539 extern unsigned int uvc_timeout_param
;
541 #define uvc_trace(flag, msg...) \
543 if (uvc_trace_param & flag) \
544 printk(KERN_DEBUG "uvcvideo: " msg); \
547 #define uvc_warn_once(dev, warn, msg...) \
549 if (!test_and_set_bit(warn, &dev->warnings)) \
550 printk(KERN_INFO "uvcvideo: " msg); \
553 #define uvc_printk(level, msg...) \
554 printk(level "uvcvideo: " msg)
556 /* --------------------------------------------------------------------------
557 * Internal functions.
561 extern struct uvc_driver uvc_driver
;
563 /* Video buffers queue management. */
564 extern void uvc_queue_init(struct uvc_video_queue
*queue
,
565 enum v4l2_buf_type type
, int drop_corrupted
);
566 extern int uvc_alloc_buffers(struct uvc_video_queue
*queue
,
567 unsigned int nbuffers
, unsigned int buflength
);
568 extern int uvc_free_buffers(struct uvc_video_queue
*queue
);
569 extern int uvc_query_buffer(struct uvc_video_queue
*queue
,
570 struct v4l2_buffer
*v4l2_buf
);
571 extern int uvc_queue_buffer(struct uvc_video_queue
*queue
,
572 struct v4l2_buffer
*v4l2_buf
);
573 extern int uvc_dequeue_buffer(struct uvc_video_queue
*queue
,
574 struct v4l2_buffer
*v4l2_buf
, int nonblocking
);
575 extern int uvc_queue_enable(struct uvc_video_queue
*queue
, int enable
);
576 extern void uvc_queue_cancel(struct uvc_video_queue
*queue
, int disconnect
);
577 extern struct uvc_buffer
*uvc_queue_next_buffer(struct uvc_video_queue
*queue
,
578 struct uvc_buffer
*buf
);
579 extern int uvc_queue_mmap(struct uvc_video_queue
*queue
,
580 struct vm_area_struct
*vma
);
581 extern unsigned int uvc_queue_poll(struct uvc_video_queue
*queue
,
582 struct file
*file
, poll_table
*wait
);
583 extern int uvc_queue_allocated(struct uvc_video_queue
*queue
);
584 static inline int uvc_queue_streaming(struct uvc_video_queue
*queue
)
586 return queue
->flags
& UVC_QUEUE_STREAMING
;
590 extern const struct v4l2_file_operations uvc_fops
;
593 extern int uvc_video_init(struct uvc_streaming
*stream
);
594 extern int uvc_video_suspend(struct uvc_streaming
*stream
);
595 extern int uvc_video_resume(struct uvc_streaming
*stream
);
596 extern int uvc_video_enable(struct uvc_streaming
*stream
, int enable
);
597 extern int uvc_probe_video(struct uvc_streaming
*stream
,
598 struct uvc_streaming_control
*probe
);
599 extern int uvc_commit_video(struct uvc_streaming
*stream
,
600 struct uvc_streaming_control
*ctrl
);
601 extern int uvc_query_ctrl(struct uvc_device
*dev
, __u8 query
, __u8 unit
,
602 __u8 intfnum
, __u8 cs
, void *data
, __u16 size
);
605 extern int uvc_status_init(struct uvc_device
*dev
);
606 extern void uvc_status_cleanup(struct uvc_device
*dev
);
607 extern int uvc_status_start(struct uvc_device
*dev
);
608 extern void uvc_status_stop(struct uvc_device
*dev
);
609 extern int uvc_status_suspend(struct uvc_device
*dev
);
610 extern int uvc_status_resume(struct uvc_device
*dev
);
613 extern int uvc_query_v4l2_ctrl(struct uvc_video_chain
*chain
,
614 struct v4l2_queryctrl
*v4l2_ctrl
);
615 extern int uvc_query_v4l2_menu(struct uvc_video_chain
*chain
,
616 struct v4l2_querymenu
*query_menu
);
618 extern int uvc_ctrl_add_mapping(struct uvc_video_chain
*chain
,
619 const struct uvc_control_mapping
*mapping
);
620 extern int uvc_ctrl_init_device(struct uvc_device
*dev
);
621 extern void uvc_ctrl_cleanup_device(struct uvc_device
*dev
);
622 extern int uvc_ctrl_resume_device(struct uvc_device
*dev
);
624 extern int uvc_ctrl_begin(struct uvc_video_chain
*chain
);
625 extern int __uvc_ctrl_commit(struct uvc_video_chain
*chain
, int rollback
);
626 static inline int uvc_ctrl_commit(struct uvc_video_chain
*chain
)
628 return __uvc_ctrl_commit(chain
, 0);
630 static inline int uvc_ctrl_rollback(struct uvc_video_chain
*chain
)
632 return __uvc_ctrl_commit(chain
, 1);
635 extern int uvc_ctrl_get(struct uvc_video_chain
*chain
,
636 struct v4l2_ext_control
*xctrl
);
637 extern int uvc_ctrl_set(struct uvc_video_chain
*chain
,
638 struct v4l2_ext_control
*xctrl
);
640 extern int uvc_xu_ctrl_query(struct uvc_video_chain
*chain
,
641 struct uvc_xu_control
*ctrl
, int set
);
643 /* Utility functions */
644 extern void uvc_simplify_fraction(uint32_t *numerator
, uint32_t *denominator
,
645 unsigned int n_terms
, unsigned int threshold
);
646 extern uint32_t uvc_fraction_to_interval(uint32_t numerator
,
647 uint32_t denominator
);
648 extern struct usb_host_endpoint
*uvc_find_endpoint(
649 struct usb_host_interface
*alts
, __u8 epaddr
);
652 void uvc_video_decode_isight(struct urb
*urb
, struct uvc_streaming
*stream
,
653 struct uvc_buffer
*buf
);
655 #endif /* __KERNEL__ */