1 /* SPDX-License-Identifier: GPL-2.0 */
6 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
7 #endif /* __KERNEL__ */
9 #include <linux/atomic.h>
10 #include <linux/kernel.h>
11 #include <linux/poll.h>
12 #include <linux/usb.h>
13 #include <linux/usb/video.h>
14 #include <linux/uvcvideo.h>
15 #include <linux/videodev2.h>
16 #include <linux/workqueue.h>
17 #include <media/media-device.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-fh.h>
21 #include <media/videobuf2-v4l2.h>
23 /* --------------------------------------------------------------------------
27 #define UVC_TERM_INPUT 0x0000
28 #define UVC_TERM_OUTPUT 0x8000
29 #define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
31 #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
32 #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
33 #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
34 #define UVC_ENTITY_IS_ITERM(entity) \
35 (UVC_ENTITY_IS_TERM(entity) && \
36 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
37 #define UVC_ENTITY_IS_OTERM(entity) \
38 (UVC_ENTITY_IS_TERM(entity) && \
39 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
41 #define UVC_EXT_GPIO_UNIT 0x7ffe
42 #define UVC_EXT_GPIO_UNIT_ID 0x100
44 /* ------------------------------------------------------------------------
45 * Driver specific constants.
48 #define DRIVER_VERSION "1.1.1"
50 /* Number of isochronous URBs. */
52 /* Maximum number of packets per URB. */
53 #define UVC_MAX_PACKETS 32
55 #define UVC_CTRL_CONTROL_TIMEOUT 5000
56 #define UVC_CTRL_STREAMING_TIMEOUT 5000
58 /* Maximum allowed number of control mappings per device */
59 #define UVC_MAX_CONTROL_MAPPINGS 1024
60 #define UVC_MAX_CONTROL_MENU_ENTRIES 32
63 #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
64 #define UVC_QUIRK_PROBE_MINMAX 0x00000002
65 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
66 #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
67 #define UVC_QUIRK_STREAM_NO_FID 0x00000010
68 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
69 #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
70 #define UVC_QUIRK_PROBE_DEF 0x00000100
71 #define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
72 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT 0x00000400
73 #define UVC_QUIRK_FORCE_Y8 0x00000800
74 #define UVC_QUIRK_FORCE_BPP 0x00001000
75 #define UVC_QUIRK_WAKE_AUTOSUSPEND 0x00002000
76 #define UVC_QUIRK_NO_RESET_RESUME 0x00004000
77 #define UVC_QUIRK_DISABLE_AUTOSUSPEND 0x00008000
78 #define UVC_QUIRK_INVALID_DEVICE_SOF 0x00010000
81 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
82 #define UVC_FMT_FLAG_STREAM 0x00000002
84 /* ------------------------------------------------------------------------
92 struct uvc_video_chain;
95 * TODO: Put the most frequently accessed fields at the beginning of
96 * structures to maximize cache efficiency.
98 struct uvc_control_info {
99 struct list_head mappings;
102 u8 index; /* Bit index in bmControls */
109 struct uvc_control_mapping {
110 struct list_head list;
111 struct list_head ev_subs;
120 enum v4l2_ctrl_type v4l2_type;
123 const u32 *menu_mapping;
124 const char (*menu_names)[UVC_MENU_NAME_LEN];
125 unsigned long menu_mask;
131 const struct uvc_control_mapping *(*filter_mapping)
132 (struct uvc_video_chain *chain,
133 struct uvc_control *ctrl);
134 s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
136 void (*set)(struct uvc_control_mapping *mapping, s32 value,
141 struct uvc_entity *entity;
142 struct uvc_control_info info;
144 u8 index; /* Used to match the uvc_control entry with a uvc_control_info. */
153 struct uvc_fh *handle; /* File handle that last changed the control. */
157 * The term 'entity' refers to both UVC units and UVC terminals.
159 * The type field is either the terminal type (wTerminalType in the terminal
160 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
161 * As the bDescriptorSubtype field is one byte long, the type value will
162 * always have a null MSB for units. All terminal types defined by the UVC
163 * specification have a non-null MSB, so it is safe to use the MSB to
164 * differentiate between units and terminals as long as the descriptor parsing
165 * code makes sure terminal types have a non-null MSB.
167 * For terminals, the type's most significant bit stores the terminal
168 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
169 * always be accessed with the UVC_ENTITY_* macros and never directly.
172 #define UVC_ENTITY_FLAG_DEFAULT (1 << 0)
175 struct list_head list; /* Entity as part of a UVC device. */
176 struct list_head chain; /* Entity as part of a video device chain. */
180 * Entities exposed by the UVC device use IDs 0-255, extra entities
181 * implemented by the driver (such as the GPIO entity) use IDs 256 and
189 /* Media controller-related fields. */
190 struct video_device *vdev;
191 struct v4l2_subdev subdev;
192 unsigned int num_pads;
193 unsigned int num_links;
194 struct media_pad *pads;
198 u16 wObjectiveFocalLengthMin;
199 u16 wObjectiveFocalLengthMax;
200 u16 wOcularFocalLength;
208 u8 bTransportModeSize;
209 u8 *bmTransportModes;
235 struct gpio_desc *gpio_privacy;
243 int (*get_info)(struct uvc_device *dev, struct uvc_entity *entity,
245 int (*get_cur)(struct uvc_device *dev, struct uvc_entity *entity,
246 u8 cs, void *data, u16 size);
248 unsigned int ncontrols;
249 struct uvc_control *controls;
259 u32 dwMaxVideoFrameBufferSize;
260 u8 bFrameIntervalType;
261 u32 dwDefaultFrameInterval;
262 const u32 *dwFrameInterval;
269 enum v4l2_colorspace colorspace;
270 enum v4l2_xfer_func xfer_func;
271 enum v4l2_ycbcr_encoding ycbcr_enc;
275 unsigned int nframes;
276 const struct uvc_frame *frames;
279 struct uvc_streaming_header {
285 /* The following fields are used by input headers only. */
287 u8 bStillCaptureMethod;
292 enum uvc_buffer_state {
293 UVC_BUF_STATE_IDLE = 0,
294 UVC_BUF_STATE_QUEUED = 1,
295 UVC_BUF_STATE_ACTIVE = 2,
296 UVC_BUF_STATE_READY = 3,
297 UVC_BUF_STATE_DONE = 4,
298 UVC_BUF_STATE_ERROR = 5,
302 struct vb2_v4l2_buffer buf;
303 struct list_head queue;
305 enum uvc_buffer_state state;
310 unsigned int bytesused;
314 /* Asynchronous buffer handling. */
318 #define UVC_QUEUE_DISCONNECTED (1 << 0)
319 #define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
321 struct uvc_video_queue {
322 struct vb2_queue queue;
323 struct mutex mutex; /* Protects queue */
326 unsigned int buf_used;
328 spinlock_t irqlock; /* Protects irqqueue */
329 struct list_head irqqueue;
332 struct uvc_video_chain {
333 struct uvc_device *dev;
334 struct list_head list;
336 struct list_head entities; /* All entities */
337 struct uvc_entity *processing; /* Processing unit */
338 struct uvc_entity *selector; /* Selector unit */
340 struct mutex ctrl_mutex; /* Protects ctrl.info */
342 struct v4l2_prio_state prio; /* V4L2 priority state */
343 u32 caps; /* V4L2 chain-wide caps */
344 u8 ctrl_class_bitmap; /* Bitmap of valid classes */
347 struct uvc_stats_frame {
348 unsigned int size; /* Number of bytes captured */
349 unsigned int first_data; /* Index of the first non-empty packet */
351 unsigned int nb_packets; /* Number of packets */
352 unsigned int nb_empty; /* Number of empty packets */
353 unsigned int nb_invalid; /* Number of packets with an invalid header */
354 unsigned int nb_errors; /* Number of packets with the error bit set */
356 unsigned int nb_pts; /* Number of packets with a PTS timestamp */
357 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
358 unsigned int last_pts_diff; /* Index of the last PTS difference */
359 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
360 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
361 u32 pts; /* PTS of the last packet */
363 unsigned int nb_scr; /* Number of packets with a SCR timestamp */
364 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
365 u16 scr_sof; /* SCR.SOF of the last packet */
366 u32 scr_stc; /* SCR.STC of the last packet */
369 struct uvc_stats_stream {
370 ktime_t start_ts; /* Stream start timestamp */
371 ktime_t stop_ts; /* Stream stop timestamp */
373 unsigned int nb_frames; /* Number of frames */
375 unsigned int nb_packets; /* Number of packets */
376 unsigned int nb_empty; /* Number of empty packets */
377 unsigned int nb_invalid; /* Number of packets with an invalid header */
378 unsigned int nb_errors; /* Number of packets with the error bit set */
380 unsigned int nb_pts_constant; /* Number of frames with constant PTS */
381 unsigned int nb_pts_early; /* Number of frames with early PTS */
382 unsigned int nb_pts_initial; /* Number of frames with initial PTS */
384 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
385 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
386 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
387 unsigned int scr_sof; /* STC.SOF of the last packet */
388 unsigned int min_sof; /* Minimum STC.SOF value */
389 unsigned int max_sof; /* Maximum STC.SOF value */
392 #define UVC_METADATA_BUF_SIZE 10240
395 * struct uvc_copy_op: Context structure to schedule asynchronous memcpy
397 * @buf: active buf object for this operation
398 * @dst: copy destination address
399 * @src: copy source address
403 struct uvc_buffer *buf;
410 * struct uvc_urb - URB context management structure
412 * @urb: the URB described by this context structure
413 * @stream: UVC streaming context
414 * @buffer: memory storage for the URB
415 * @dma: Allocated DMA handle
416 * @sgt: sgt_table with the urb locations in memory
417 * @async_operations: counter to indicate the number of copy operations
418 * @copy_operations: work descriptors for asynchronous copy operations
419 * @work: work queue entry for asynchronous decode
423 struct uvc_streaming *stream;
427 struct sg_table *sgt;
429 unsigned int async_operations;
430 struct uvc_copy_op copy_operations[UVC_MAX_PACKETS];
431 struct work_struct work;
434 struct uvc_streaming {
435 struct list_head list;
436 struct uvc_device *dev;
437 struct video_device vdev;
438 struct uvc_video_chain *chain;
441 struct usb_interface *intf;
445 struct uvc_streaming_header header;
446 enum v4l2_buf_type type;
448 unsigned int nformats;
449 const struct uvc_format *formats;
451 struct uvc_streaming_control ctrl;
452 const struct uvc_format *def_format;
453 const struct uvc_format *cur_format;
454 const struct uvc_frame *cur_frame;
457 * Protect access to ctrl, cur_format, cur_frame and hardware video
463 unsigned int frozen : 1;
464 struct uvc_video_queue queue;
465 struct workqueue_struct *async_wq;
466 void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
467 struct uvc_buffer *meta_buf);
470 struct video_device vdev;
471 struct uvc_video_queue queue;
475 /* Context data used by the bulk completion handler. */
478 unsigned int header_size;
481 u32 max_payload_size;
484 struct uvc_urb uvc_urb[UVC_URBS];
485 unsigned int urb_size;
491 struct dentry *debugfs_dir;
493 struct uvc_stats_frame frame;
494 struct uvc_stats_stream stream;
497 /* Timestamps support. */
499 struct uvc_clock_sample {
509 unsigned int last_sof_overflow;
520 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \
521 for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \
522 (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \
525 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
527 return uvc_urb - &uvc_urb->stream->uvc_urb[0];
530 struct uvc_device_info {
536 struct uvc_status_streaming {
540 struct uvc_status_control {
551 struct uvc_status_control control;
552 struct uvc_status_streaming streaming;
557 struct usb_device *udev;
558 struct usb_interface *intf;
559 unsigned long warnings;
564 const struct uvc_device_info *info;
566 struct mutex lock; /* Protects users */
570 /* Video control interface */
571 #ifdef CONFIG_MEDIA_CONTROLLER
572 struct media_device mdev;
574 struct v4l2_device vdev;
578 struct list_head entities;
579 struct list_head chains;
581 /* Video Streaming interfaces */
582 struct list_head streams;
585 /* Status Interrupt Endpoint */
586 struct usb_host_endpoint *int_ep;
588 struct uvc_status *status;
591 struct input_dev *input;
594 struct uvc_ctrl_work {
595 struct work_struct work;
597 struct uvc_video_chain *chain;
598 struct uvc_control *ctrl;
602 struct uvc_entity *gpio_unit;
605 enum uvc_handle_state {
606 UVC_HANDLE_PASSIVE = 0,
607 UVC_HANDLE_ACTIVE = 1,
612 struct uvc_video_chain *chain;
613 struct uvc_streaming *stream;
614 enum uvc_handle_state state;
618 struct usb_driver driver;
621 /* ------------------------------------------------------------------------
622 * Debugging, printing and logging
625 #define UVC_DBG_PROBE (1 << 0)
626 #define UVC_DBG_DESCR (1 << 1)
627 #define UVC_DBG_CONTROL (1 << 2)
628 #define UVC_DBG_FORMAT (1 << 3)
629 #define UVC_DBG_CAPTURE (1 << 4)
630 #define UVC_DBG_CALLS (1 << 5)
631 #define UVC_DBG_FRAME (1 << 7)
632 #define UVC_DBG_SUSPEND (1 << 8)
633 #define UVC_DBG_STATUS (1 << 9)
634 #define UVC_DBG_VIDEO (1 << 10)
635 #define UVC_DBG_STATS (1 << 11)
636 #define UVC_DBG_CLOCK (1 << 12)
638 #define UVC_WARN_MINMAX 0
639 #define UVC_WARN_PROBE_DEF 1
640 #define UVC_WARN_XU_GET_RES 2
642 extern unsigned int uvc_clock_param;
643 extern unsigned int uvc_no_drop_param;
644 extern unsigned int uvc_dbg_param;
645 extern unsigned int uvc_timeout_param;
646 extern unsigned int uvc_hw_timestamps_param;
648 #define uvc_dbg(_dev, flag, fmt, ...) \
650 if (uvc_dbg_param & UVC_DBG_##flag) \
651 dev_printk(KERN_DEBUG, &(_dev)->udev->dev, fmt, \
655 #define uvc_dbg_cont(flag, fmt, ...) \
657 if (uvc_dbg_param & UVC_DBG_##flag) \
658 pr_cont(fmt, ##__VA_ARGS__); \
661 #define uvc_warn_once(_dev, warn, fmt, ...) \
663 if (!test_and_set_bit(warn, &(_dev)->warnings)) \
664 dev_info(&(_dev)->udev->dev, fmt, ##__VA_ARGS__); \
667 /* --------------------------------------------------------------------------
668 * Internal functions.
672 extern struct uvc_driver uvc_driver;
674 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
676 /* Video buffers queue management. */
677 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
679 void uvc_queue_release(struct uvc_video_queue *queue);
680 int uvc_request_buffers(struct uvc_video_queue *queue,
681 struct v4l2_requestbuffers *rb);
682 int uvc_query_buffer(struct uvc_video_queue *queue,
683 struct v4l2_buffer *v4l2_buf);
684 int uvc_create_buffers(struct uvc_video_queue *queue,
685 struct v4l2_create_buffers *v4l2_cb);
686 int uvc_queue_buffer(struct uvc_video_queue *queue,
687 struct media_device *mdev,
688 struct v4l2_buffer *v4l2_buf);
689 int uvc_export_buffer(struct uvc_video_queue *queue,
690 struct v4l2_exportbuffer *exp);
691 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
692 struct v4l2_buffer *v4l2_buf, int nonblocking);
693 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
694 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
695 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
696 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
697 struct uvc_buffer *buf);
698 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
699 void uvc_queue_buffer_release(struct uvc_buffer *buf);
700 int uvc_queue_mmap(struct uvc_video_queue *queue,
701 struct vm_area_struct *vma);
702 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
705 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
706 unsigned long pgoff);
708 int uvc_queue_allocated(struct uvc_video_queue *queue);
709 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
711 return vb2_is_streaming(&queue->queue);
714 static inline struct uvc_streaming *
715 uvc_queue_to_stream(struct uvc_video_queue *queue)
717 return container_of(queue, struct uvc_streaming, queue);
721 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
722 extern const struct v4l2_file_operations uvc_fops;
724 /* Media controller */
725 int uvc_mc_register_entities(struct uvc_video_chain *chain);
726 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
729 int uvc_video_init(struct uvc_streaming *stream);
730 int uvc_video_suspend(struct uvc_streaming *stream);
731 int uvc_video_resume(struct uvc_streaming *stream, int reset);
732 int uvc_video_start_streaming(struct uvc_streaming *stream);
733 void uvc_video_stop_streaming(struct uvc_streaming *stream);
734 int uvc_probe_video(struct uvc_streaming *stream,
735 struct uvc_streaming_control *probe);
736 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
737 u8 intfnum, u8 cs, void *data, u16 size);
738 void uvc_video_clock_update(struct uvc_streaming *stream,
739 struct vb2_v4l2_buffer *vbuf,
740 struct uvc_buffer *buf);
741 int uvc_meta_register(struct uvc_streaming *stream);
743 int uvc_register_video_device(struct uvc_device *dev,
744 struct uvc_streaming *stream,
745 struct video_device *vdev,
746 struct uvc_video_queue *queue,
747 enum v4l2_buf_type type,
748 const struct v4l2_file_operations *fops,
749 const struct v4l2_ioctl_ops *ioctl_ops);
752 int uvc_status_init(struct uvc_device *dev);
753 void uvc_status_unregister(struct uvc_device *dev);
754 void uvc_status_cleanup(struct uvc_device *dev);
755 int uvc_status_start(struct uvc_device *dev, gfp_t flags);
756 void uvc_status_stop(struct uvc_device *dev);
759 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
761 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
762 struct v4l2_queryctrl *v4l2_ctrl);
763 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
764 struct v4l2_querymenu *query_menu);
766 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
767 const struct uvc_control_mapping *mapping);
768 int uvc_ctrl_init_device(struct uvc_device *dev);
769 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
770 int uvc_ctrl_restore_values(struct uvc_device *dev);
771 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
772 struct uvc_control *ctrl, const u8 *data);
773 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
774 struct uvc_control *ctrl, const u8 *data);
776 int uvc_ctrl_begin(struct uvc_video_chain *chain);
777 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
778 struct v4l2_ext_controls *ctrls);
779 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
780 struct v4l2_ext_controls *ctrls)
782 return __uvc_ctrl_commit(handle, 0, ctrls);
784 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
786 return __uvc_ctrl_commit(handle, 1, NULL);
789 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
790 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
791 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
792 const struct v4l2_ext_controls *ctrls,
793 unsigned long ioctl);
795 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
796 struct uvc_xu_control_query *xqry);
798 /* Utility functions */
799 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
801 u16 uvc_endpoint_max_bpi(struct usb_device *dev, struct usb_host_endpoint *ep);
804 void uvc_video_decode_isight(struct uvc_urb *uvc_urb,
805 struct uvc_buffer *buf,
806 struct uvc_buffer *meta_buf);
808 /* debugfs and statistics */
809 void uvc_debugfs_init(void);
810 void uvc_debugfs_cleanup(void);
811 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
812 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
814 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,