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_xu_control_mapping {
51 enum v4l2_ctrl_type v4l2_type;
55 struct uvc_xu_control {
62 #define UVCIOC_CTRL_ADD _IOW('U', 1, struct uvc_xu_control_info)
63 #define UVCIOC_CTRL_MAP _IOWR('U', 2, struct uvc_xu_control_mapping)
64 #define UVCIOC_CTRL_GET _IOWR('U', 3, struct uvc_xu_control)
65 #define UVCIOC_CTRL_SET _IOW('U', 4, struct uvc_xu_control)
69 #include <linux/poll.h>
70 #include <linux/usb/video.h>
72 /* --------------------------------------------------------------------------
76 #define UVC_TERM_INPUT 0x0000
77 #define UVC_TERM_OUTPUT 0x8000
79 #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
80 #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
81 #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
82 #define UVC_ENTITY_IS_ITERM(entity) \
83 (((entity)->type & 0x8000) == UVC_TERM_INPUT)
84 #define UVC_ENTITY_IS_OTERM(entity) \
85 (((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
88 /* ------------------------------------------------------------------------
91 #define UVC_GUID_UVC_CAMERA \
92 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
94 #define UVC_GUID_UVC_OUTPUT \
95 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
97 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
98 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
100 #define UVC_GUID_UVC_PROCESSING \
101 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
103 #define UVC_GUID_UVC_SELECTOR \
104 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
107 #define UVC_GUID_FORMAT_MJPEG \
108 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
109 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
110 #define UVC_GUID_FORMAT_YUY2 \
111 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
112 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
113 #define UVC_GUID_FORMAT_NV12 \
114 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
115 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
116 #define UVC_GUID_FORMAT_YV12 \
117 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
118 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
119 #define UVC_GUID_FORMAT_I420 \
120 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
121 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
122 #define UVC_GUID_FORMAT_UYVY \
123 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
124 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
125 #define UVC_GUID_FORMAT_Y800 \
126 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
127 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
128 #define UVC_GUID_FORMAT_BY8 \
129 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
130 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
133 /* ------------------------------------------------------------------------
134 * Driver specific constants.
137 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 0)
139 /* Number of isochronous URBs. */
141 /* Maximum number of packets per URB. */
142 #define UVC_MAX_PACKETS 32
143 /* Maximum number of video buffers. */
144 #define UVC_MAX_VIDEO_BUFFERS 32
145 /* Maximum status buffer size in bytes of interrupt URB. */
146 #define UVC_MAX_STATUS_SIZE 16
148 #define UVC_CTRL_CONTROL_TIMEOUT 300
149 #define UVC_CTRL_STREAMING_TIMEOUT 1000
152 #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
153 #define UVC_QUIRK_PROBE_MINMAX 0x00000002
154 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
155 #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
156 #define UVC_QUIRK_STREAM_NO_FID 0x00000010
157 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
158 #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
159 #define UVC_QUIRK_PROBE_DEF 0x00000100
162 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
163 #define UVC_FMT_FLAG_STREAM 0x00000002
165 /* ------------------------------------------------------------------------
171 /* TODO: Put the most frequently accessed fields at the beginning of
172 * structures to maximize cache efficiency.
174 struct uvc_streaming_control {
178 __u32 dwFrameInterval;
182 __u16 wCompWindowSize;
184 __u32 dwMaxVideoFrameSize;
185 __u32 dwMaxPayloadTransferSize;
186 __u32 dwClockFrequency;
188 __u8 bPreferedVersion;
193 struct uvc_menu_info {
198 struct uvc_control_info {
199 struct list_head list;
200 struct list_head mappings;
210 struct uvc_control_mapping {
211 struct list_head list;
213 struct uvc_control_info *ctrl;
222 enum v4l2_ctrl_type v4l2_type;
225 struct uvc_menu_info *menu_info;
228 __s32 (*get) (struct uvc_control_mapping *mapping, __u8 query,
230 void (*set) (struct uvc_control_mapping *mapping, __s32 value,
235 struct uvc_entity *entity;
236 struct uvc_control_info *info;
238 __u8 index; /* Used to match the uvc_control entry with a
247 struct uvc_format_desc {
253 /* The term 'entity' refers to both UVC units and UVC terminals.
255 * The type field is either the terminal type (wTerminalType in the terminal
256 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
257 * As the bDescriptorSubtype field is one byte long, the type value will
258 * always have a null MSB for units. All terminal types defined by the UVC
259 * specification have a non-null MSB, so it is safe to use the MSB to
260 * differentiate between units and terminals as long as the descriptor parsing
261 * code makes sure terminal types have a non-null MSB.
263 * For terminals, the type's most significant bit stores the terminal
264 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
265 * always be accessed with the UVC_ENTITY_* macros and never directly.
269 struct list_head list; /* Entity as part of a UVC device. */
270 struct list_head chain; /* Entity as part of a video device
278 __u16 wObjectiveFocalLengthMin;
279 __u16 wObjectiveFocalLengthMax;
280 __u16 wOcularFocalLength;
288 __u8 bTransportModeSize;
289 __u8 *bmTransportModes;
298 __u16 wMaxMultiplier;
301 __u8 bmVideoStandards;
310 __u8 guidExtensionCode[16];
316 __u8 *bmControlsType;
320 unsigned int ncontrols;
321 struct uvc_control *controls;
331 __u32 dwMaxVideoFrameBufferSize;
332 __u8 bFrameIntervalType;
333 __u32 dwDefaultFrameInterval;
334 __u32 *dwFrameInterval;
347 unsigned int nframes;
348 struct uvc_frame *frame;
351 struct uvc_streaming_header {
353 __u8 bEndpointAddress;
357 /* The following fields are used by input headers only. */
359 __u8 bStillCaptureMethod;
360 __u8 bTriggerSupport;
364 struct uvc_streaming {
365 struct list_head list;
367 struct usb_interface *intf;
371 struct uvc_streaming_header header;
372 enum v4l2_buf_type type;
374 unsigned int nformats;
375 struct uvc_format *format;
377 struct uvc_streaming_control ctrl;
378 struct uvc_format *cur_format;
379 struct uvc_frame *cur_frame;
384 enum uvc_buffer_state {
385 UVC_BUF_STATE_IDLE = 0,
386 UVC_BUF_STATE_QUEUED = 1,
387 UVC_BUF_STATE_ACTIVE = 2,
388 UVC_BUF_STATE_DONE = 3,
389 UVC_BUF_STATE_ERROR = 4,
393 unsigned long vma_use_count;
394 struct list_head stream;
396 /* Touched by interrupt handler. */
397 struct v4l2_buffer buf;
398 struct list_head queue;
399 wait_queue_head_t wait;
400 enum uvc_buffer_state state;
403 #define UVC_QUEUE_STREAMING (1 << 0)
404 #define UVC_QUEUE_DISCONNECTED (1 << 1)
405 #define UVC_QUEUE_DROP_INCOMPLETE (1 << 2)
407 struct uvc_video_queue {
408 enum v4l2_buf_type type;
415 unsigned int buf_size;
416 unsigned int buf_used;
417 struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
418 struct mutex mutex; /* protects buffers and mainqueue */
419 spinlock_t irqlock; /* protects irqqueue */
421 struct list_head mainqueue;
422 struct list_head irqqueue;
425 struct uvc_video_device {
426 struct uvc_device *dev;
427 struct video_device *vdev;
429 unsigned int frozen : 1;
431 struct list_head iterms; /* Input terminals */
432 struct uvc_entity *oterm; /* Output terminal */
433 struct uvc_entity *sterm; /* USB streaming terminal */
434 struct uvc_entity *processing;
435 struct uvc_entity *selector;
436 struct list_head extensions;
437 struct mutex ctrl_mutex;
439 struct uvc_video_queue queue;
441 /* Video streaming object, must always be non-NULL. */
442 struct uvc_streaming *streaming;
444 void (*decode) (struct urb *urb, struct uvc_video_device *video,
445 struct uvc_buffer *buf);
447 /* Context data used by the bulk completion handler. */
450 unsigned int header_size;
453 __u32 max_payload_size;
456 struct urb *urb[UVC_URBS];
457 char *urb_buffer[UVC_URBS];
458 dma_addr_t urb_dma[UVC_URBS];
459 unsigned int urb_size;
464 enum uvc_device_state {
465 UVC_DEV_DISCONNECTED = 1,
469 struct usb_device *udev;
470 struct usb_interface *intf;
471 unsigned long warnings;
476 enum uvc_device_state state;
478 struct list_head list;
481 /* Video control interface */
483 __u32 clock_frequency;
485 struct list_head entities;
487 struct uvc_video_device video;
489 /* Status Interrupt Endpoint */
490 struct usb_host_endpoint *int_ep;
493 struct input_dev *input;
496 /* Video Streaming interfaces */
497 struct list_head streaming;
500 enum uvc_handle_state {
501 UVC_HANDLE_PASSIVE = 0,
502 UVC_HANDLE_ACTIVE = 1,
506 struct uvc_video_device *device;
507 enum uvc_handle_state state;
511 struct usb_driver driver;
513 struct mutex open_mutex; /* protects from open/disconnect race */
515 struct list_head devices; /* struct uvc_device list */
516 struct list_head controls; /* struct uvc_control_info list */
517 struct mutex ctrl_mutex; /* protects controls and devices
521 /* ------------------------------------------------------------------------
522 * Debugging, printing and logging
525 #define UVC_TRACE_PROBE (1 << 0)
526 #define UVC_TRACE_DESCR (1 << 1)
527 #define UVC_TRACE_CONTROL (1 << 2)
528 #define UVC_TRACE_FORMAT (1 << 3)
529 #define UVC_TRACE_CAPTURE (1 << 4)
530 #define UVC_TRACE_CALLS (1 << 5)
531 #define UVC_TRACE_IOCTL (1 << 6)
532 #define UVC_TRACE_FRAME (1 << 7)
533 #define UVC_TRACE_SUSPEND (1 << 8)
534 #define UVC_TRACE_STATUS (1 << 9)
536 #define UVC_WARN_MINMAX 0
537 #define UVC_WARN_PROBE_DEF 1
539 extern unsigned int uvc_no_drop_param;
540 extern unsigned int uvc_trace_param;
542 #define uvc_trace(flag, msg...) \
544 if (uvc_trace_param & flag) \
545 printk(KERN_DEBUG "uvcvideo: " msg); \
548 #define uvc_warn_once(dev, warn, msg...) \
550 if (!test_and_set_bit(warn, &dev->warnings)) \
551 printk(KERN_INFO "uvcvideo: " msg); \
554 #define uvc_printk(level, msg...) \
555 printk(level "uvcvideo: " msg)
557 #define UVC_GUID_FORMAT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" \
558 "%02x%02x%02x%02x%02x%02x"
559 #define UVC_GUID_ARGS(guid) \
560 (guid)[3], (guid)[2], (guid)[1], (guid)[0], \
561 (guid)[5], (guid)[4], \
562 (guid)[7], (guid)[6], \
563 (guid)[8], (guid)[9], \
564 (guid)[10], (guid)[11], (guid)[12], \
565 (guid)[13], (guid)[14], (guid)[15]
567 /* --------------------------------------------------------------------------
568 * Internal functions.
572 extern struct uvc_driver uvc_driver;
573 extern void uvc_delete(struct kref *kref);
575 /* Video buffers queue management. */
576 extern void uvc_queue_init(struct uvc_video_queue *queue,
577 enum v4l2_buf_type type);
578 extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
579 unsigned int nbuffers, unsigned int buflength);
580 extern int uvc_free_buffers(struct uvc_video_queue *queue);
581 extern int uvc_query_buffer(struct uvc_video_queue *queue,
582 struct v4l2_buffer *v4l2_buf);
583 extern int uvc_queue_buffer(struct uvc_video_queue *queue,
584 struct v4l2_buffer *v4l2_buf);
585 extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
586 struct v4l2_buffer *v4l2_buf, int nonblocking);
587 extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
588 extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
589 extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
590 struct uvc_buffer *buf);
591 extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
592 struct file *file, poll_table *wait);
593 extern int uvc_queue_allocated(struct uvc_video_queue *queue);
594 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
596 return queue->flags & UVC_QUEUE_STREAMING;
600 extern const struct v4l2_file_operations uvc_fops;
603 extern int uvc_video_init(struct uvc_video_device *video);
604 extern int uvc_video_suspend(struct uvc_video_device *video);
605 extern int uvc_video_resume(struct uvc_video_device *video);
606 extern int uvc_video_enable(struct uvc_video_device *video, int enable);
607 extern int uvc_probe_video(struct uvc_video_device *video,
608 struct uvc_streaming_control *probe);
609 extern int uvc_commit_video(struct uvc_video_device *video,
610 struct uvc_streaming_control *ctrl);
611 extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
612 __u8 intfnum, __u8 cs, void *data, __u16 size);
615 extern int uvc_status_init(struct uvc_device *dev);
616 extern void uvc_status_cleanup(struct uvc_device *dev);
617 extern int uvc_status_start(struct uvc_device *dev);
618 extern void uvc_status_stop(struct uvc_device *dev);
619 extern int uvc_status_suspend(struct uvc_device *dev);
620 extern int uvc_status_resume(struct uvc_device *dev);
623 extern struct uvc_control *uvc_find_control(struct uvc_video_device *video,
624 __u32 v4l2_id, struct uvc_control_mapping **mapping);
625 extern int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
626 struct v4l2_queryctrl *v4l2_ctrl);
628 extern int uvc_ctrl_add_info(struct uvc_control_info *info);
629 extern int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping);
630 extern int uvc_ctrl_init_device(struct uvc_device *dev);
631 extern void uvc_ctrl_cleanup_device(struct uvc_device *dev);
632 extern int uvc_ctrl_resume_device(struct uvc_device *dev);
633 extern void uvc_ctrl_init(void);
635 extern int uvc_ctrl_begin(struct uvc_video_device *video);
636 extern int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback);
637 static inline int uvc_ctrl_commit(struct uvc_video_device *video)
639 return __uvc_ctrl_commit(video, 0);
641 static inline int uvc_ctrl_rollback(struct uvc_video_device *video)
643 return __uvc_ctrl_commit(video, 1);
646 extern int uvc_ctrl_get(struct uvc_video_device *video,
647 struct v4l2_ext_control *xctrl);
648 extern int uvc_ctrl_set(struct uvc_video_device *video,
649 struct v4l2_ext_control *xctrl);
651 extern int uvc_xu_ctrl_query(struct uvc_video_device *video,
652 struct uvc_xu_control *ctrl, int set);
654 /* Utility functions */
655 extern void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
656 unsigned int n_terms, unsigned int threshold);
657 extern uint32_t uvc_fraction_to_interval(uint32_t numerator,
658 uint32_t denominator);
659 extern struct usb_host_endpoint *uvc_find_endpoint(
660 struct usb_host_interface *alts, __u8 epaddr);
663 void uvc_video_decode_isight(struct urb *urb, struct uvc_video_device *video,
664 struct uvc_buffer *buf);
666 #endif /* __KERNEL__ */