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
78 #define UVC_FMT_FLAG_COMPRESSED 0x00000001
79 #define UVC_FMT_FLAG_STREAM 0x00000002
81 /* ------------------------------------------------------------------------
90 * TODO: Put the most frequently accessed fields at the beginning of
91 * structures to maximize cache efficiency.
93 struct uvc_control_info {
94 struct list_head mappings;
97 u8 index; /* Bit index in bmControls */
104 struct uvc_control_mapping {
105 struct list_head list;
106 struct list_head ev_subs;
115 enum v4l2_ctrl_type v4l2_type;
118 const u32 *menu_mapping;
119 const char (*menu_names)[UVC_MENU_NAME_LEN];
120 unsigned long menu_mask;
126 s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
128 void (*set)(struct uvc_control_mapping *mapping, s32 value,
133 struct uvc_entity *entity;
134 struct uvc_control_info info;
136 u8 index; /* Used to match the uvc_control entry with a uvc_control_info. */
145 struct uvc_fh *handle; /* File handle that last changed the control. */
149 * The term 'entity' refers to both UVC units and UVC terminals.
151 * The type field is either the terminal type (wTerminalType in the terminal
152 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
153 * As the bDescriptorSubtype field is one byte long, the type value will
154 * always have a null MSB for units. All terminal types defined by the UVC
155 * specification have a non-null MSB, so it is safe to use the MSB to
156 * differentiate between units and terminals as long as the descriptor parsing
157 * code makes sure terminal types have a non-null MSB.
159 * For terminals, the type's most significant bit stores the terminal
160 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
161 * always be accessed with the UVC_ENTITY_* macros and never directly.
164 #define UVC_ENTITY_FLAG_DEFAULT (1 << 0)
167 struct list_head list; /* Entity as part of a UVC device. */
168 struct list_head chain; /* Entity as part of a video device chain. */
172 * Entities exposed by the UVC device use IDs 0-255, extra entities
173 * implemented by the driver (such as the GPIO entity) use IDs 256 and
181 /* Media controller-related fields. */
182 struct video_device *vdev;
183 struct v4l2_subdev subdev;
184 unsigned int num_pads;
185 unsigned int num_links;
186 struct media_pad *pads;
190 u16 wObjectiveFocalLengthMin;
191 u16 wObjectiveFocalLengthMax;
192 u16 wOcularFocalLength;
200 u8 bTransportModeSize;
201 u8 *bmTransportModes;
227 struct gpio_desc *gpio_privacy;
235 int (*get_info)(struct uvc_device *dev, struct uvc_entity *entity,
237 int (*get_cur)(struct uvc_device *dev, struct uvc_entity *entity,
238 u8 cs, void *data, u16 size);
240 unsigned int ncontrols;
241 struct uvc_control *controls;
251 u32 dwMaxVideoFrameBufferSize;
252 u8 bFrameIntervalType;
253 u32 dwDefaultFrameInterval;
254 const u32 *dwFrameInterval;
261 enum v4l2_colorspace colorspace;
262 enum v4l2_xfer_func xfer_func;
263 enum v4l2_ycbcr_encoding ycbcr_enc;
267 unsigned int nframes;
268 const struct uvc_frame *frames;
271 struct uvc_streaming_header {
277 /* The following fields are used by input headers only. */
279 u8 bStillCaptureMethod;
284 enum uvc_buffer_state {
285 UVC_BUF_STATE_IDLE = 0,
286 UVC_BUF_STATE_QUEUED = 1,
287 UVC_BUF_STATE_ACTIVE = 2,
288 UVC_BUF_STATE_READY = 3,
289 UVC_BUF_STATE_DONE = 4,
290 UVC_BUF_STATE_ERROR = 5,
294 struct vb2_v4l2_buffer buf;
295 struct list_head queue;
297 enum uvc_buffer_state state;
302 unsigned int bytesused;
306 /* Asynchronous buffer handling. */
310 #define UVC_QUEUE_DISCONNECTED (1 << 0)
311 #define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
313 struct uvc_video_queue {
314 struct vb2_queue queue;
315 struct mutex mutex; /* Protects queue */
318 unsigned int buf_used;
320 spinlock_t irqlock; /* Protects irqqueue */
321 struct list_head irqqueue;
324 struct uvc_video_chain {
325 struct uvc_device *dev;
326 struct list_head list;
328 struct list_head entities; /* All entities */
329 struct uvc_entity *processing; /* Processing unit */
330 struct uvc_entity *selector; /* Selector unit */
332 struct mutex ctrl_mutex; /* Protects ctrl.info */
334 struct v4l2_prio_state prio; /* V4L2 priority state */
335 u32 caps; /* V4L2 chain-wide caps */
336 u8 ctrl_class_bitmap; /* Bitmap of valid classes */
339 struct uvc_stats_frame {
340 unsigned int size; /* Number of bytes captured */
341 unsigned int first_data; /* Index of the first non-empty packet */
343 unsigned int nb_packets; /* Number of packets */
344 unsigned int nb_empty; /* Number of empty packets */
345 unsigned int nb_invalid; /* Number of packets with an invalid header */
346 unsigned int nb_errors; /* Number of packets with the error bit set */
348 unsigned int nb_pts; /* Number of packets with a PTS timestamp */
349 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
350 unsigned int last_pts_diff; /* Index of the last PTS difference */
351 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
352 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
353 u32 pts; /* PTS of the last packet */
355 unsigned int nb_scr; /* Number of packets with a SCR timestamp */
356 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
357 u16 scr_sof; /* SCR.SOF of the last packet */
358 u32 scr_stc; /* SCR.STC of the last packet */
361 struct uvc_stats_stream {
362 ktime_t start_ts; /* Stream start timestamp */
363 ktime_t stop_ts; /* Stream stop timestamp */
365 unsigned int nb_frames; /* Number of frames */
367 unsigned int nb_packets; /* Number of packets */
368 unsigned int nb_empty; /* Number of empty packets */
369 unsigned int nb_invalid; /* Number of packets with an invalid header */
370 unsigned int nb_errors; /* Number of packets with the error bit set */
372 unsigned int nb_pts_constant; /* Number of frames with constant PTS */
373 unsigned int nb_pts_early; /* Number of frames with early PTS */
374 unsigned int nb_pts_initial; /* Number of frames with initial PTS */
376 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
377 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
378 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
379 unsigned int scr_sof; /* STC.SOF of the last packet */
380 unsigned int min_sof; /* Minimum STC.SOF value */
381 unsigned int max_sof; /* Maximum STC.SOF value */
384 #define UVC_METADATA_BUF_SIZE 10240
387 * struct uvc_copy_op: Context structure to schedule asynchronous memcpy
389 * @buf: active buf object for this operation
390 * @dst: copy destination address
391 * @src: copy source address
395 struct uvc_buffer *buf;
402 * struct uvc_urb - URB context management structure
404 * @urb: the URB described by this context structure
405 * @stream: UVC streaming context
406 * @buffer: memory storage for the URB
407 * @dma: Allocated DMA handle
408 * @sgt: sgt_table with the urb locations in memory
409 * @async_operations: counter to indicate the number of copy operations
410 * @copy_operations: work descriptors for asynchronous copy operations
411 * @work: work queue entry for asynchronous decode
415 struct uvc_streaming *stream;
419 struct sg_table *sgt;
421 unsigned int async_operations;
422 struct uvc_copy_op copy_operations[UVC_MAX_PACKETS];
423 struct work_struct work;
426 struct uvc_streaming {
427 struct list_head list;
428 struct uvc_device *dev;
429 struct video_device vdev;
430 struct uvc_video_chain *chain;
433 struct usb_interface *intf;
437 struct uvc_streaming_header header;
438 enum v4l2_buf_type type;
440 unsigned int nformats;
441 const struct uvc_format *formats;
443 struct uvc_streaming_control ctrl;
444 const struct uvc_format *def_format;
445 const struct uvc_format *cur_format;
446 const struct uvc_frame *cur_frame;
449 * Protect access to ctrl, cur_format, cur_frame and hardware video
455 unsigned int frozen : 1;
456 struct uvc_video_queue queue;
457 struct workqueue_struct *async_wq;
458 void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
459 struct uvc_buffer *meta_buf);
462 struct video_device vdev;
463 struct uvc_video_queue queue;
467 /* Context data used by the bulk completion handler. */
470 unsigned int header_size;
473 u32 max_payload_size;
476 struct uvc_urb uvc_urb[UVC_URBS];
477 unsigned int urb_size;
483 struct dentry *debugfs_dir;
485 struct uvc_stats_frame frame;
486 struct uvc_stats_stream stream;
489 /* Timestamps support. */
491 struct uvc_clock_sample {
511 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \
512 for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \
513 (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \
516 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
518 return uvc_urb - &uvc_urb->stream->uvc_urb[0];
521 struct uvc_device_info {
525 const struct uvc_control_mapping **mappings;
528 struct uvc_status_streaming {
532 struct uvc_status_control {
543 struct uvc_status_control control;
544 struct uvc_status_streaming streaming;
549 struct usb_device *udev;
550 struct usb_interface *intf;
551 unsigned long warnings;
556 const struct uvc_device_info *info;
558 struct mutex lock; /* Protects users */
562 /* Video control interface */
563 #ifdef CONFIG_MEDIA_CONTROLLER
564 struct media_device mdev;
566 struct v4l2_device vdev;
570 struct list_head entities;
571 struct list_head chains;
573 /* Video Streaming interfaces */
574 struct list_head streams;
577 /* Status Interrupt Endpoint */
578 struct usb_host_endpoint *int_ep;
580 struct uvc_status *status;
583 struct input_dev *input;
586 struct uvc_ctrl_work {
587 struct work_struct work;
589 struct uvc_video_chain *chain;
590 struct uvc_control *ctrl;
594 struct uvc_entity *gpio_unit;
597 enum uvc_handle_state {
598 UVC_HANDLE_PASSIVE = 0,
599 UVC_HANDLE_ACTIVE = 1,
604 struct uvc_video_chain *chain;
605 struct uvc_streaming *stream;
606 enum uvc_handle_state state;
610 struct usb_driver driver;
613 /* ------------------------------------------------------------------------
614 * Debugging, printing and logging
617 #define UVC_DBG_PROBE (1 << 0)
618 #define UVC_DBG_DESCR (1 << 1)
619 #define UVC_DBG_CONTROL (1 << 2)
620 #define UVC_DBG_FORMAT (1 << 3)
621 #define UVC_DBG_CAPTURE (1 << 4)
622 #define UVC_DBG_CALLS (1 << 5)
623 #define UVC_DBG_FRAME (1 << 7)
624 #define UVC_DBG_SUSPEND (1 << 8)
625 #define UVC_DBG_STATUS (1 << 9)
626 #define UVC_DBG_VIDEO (1 << 10)
627 #define UVC_DBG_STATS (1 << 11)
628 #define UVC_DBG_CLOCK (1 << 12)
630 #define UVC_WARN_MINMAX 0
631 #define UVC_WARN_PROBE_DEF 1
632 #define UVC_WARN_XU_GET_RES 2
634 extern unsigned int uvc_clock_param;
635 extern unsigned int uvc_no_drop_param;
636 extern unsigned int uvc_dbg_param;
637 extern unsigned int uvc_timeout_param;
638 extern unsigned int uvc_hw_timestamps_param;
640 #define uvc_dbg(_dev, flag, fmt, ...) \
642 if (uvc_dbg_param & UVC_DBG_##flag) \
643 dev_printk(KERN_DEBUG, &(_dev)->udev->dev, fmt, \
647 #define uvc_dbg_cont(flag, fmt, ...) \
649 if (uvc_dbg_param & UVC_DBG_##flag) \
650 pr_cont(fmt, ##__VA_ARGS__); \
653 #define uvc_warn_once(_dev, warn, fmt, ...) \
655 if (!test_and_set_bit(warn, &(_dev)->warnings)) \
656 dev_info(&(_dev)->udev->dev, fmt, ##__VA_ARGS__); \
659 /* --------------------------------------------------------------------------
660 * Internal functions.
664 extern struct uvc_driver uvc_driver;
666 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
668 /* Video buffers queue management. */
669 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
671 void uvc_queue_release(struct uvc_video_queue *queue);
672 int uvc_request_buffers(struct uvc_video_queue *queue,
673 struct v4l2_requestbuffers *rb);
674 int uvc_query_buffer(struct uvc_video_queue *queue,
675 struct v4l2_buffer *v4l2_buf);
676 int uvc_create_buffers(struct uvc_video_queue *queue,
677 struct v4l2_create_buffers *v4l2_cb);
678 int uvc_queue_buffer(struct uvc_video_queue *queue,
679 struct media_device *mdev,
680 struct v4l2_buffer *v4l2_buf);
681 int uvc_export_buffer(struct uvc_video_queue *queue,
682 struct v4l2_exportbuffer *exp);
683 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
684 struct v4l2_buffer *v4l2_buf, int nonblocking);
685 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
686 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
687 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
688 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
689 struct uvc_buffer *buf);
690 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
691 void uvc_queue_buffer_release(struct uvc_buffer *buf);
692 int uvc_queue_mmap(struct uvc_video_queue *queue,
693 struct vm_area_struct *vma);
694 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
697 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
698 unsigned long pgoff);
700 int uvc_queue_allocated(struct uvc_video_queue *queue);
701 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
703 return vb2_is_streaming(&queue->queue);
706 static inline struct uvc_streaming *
707 uvc_queue_to_stream(struct uvc_video_queue *queue)
709 return container_of(queue, struct uvc_streaming, queue);
713 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
714 extern const struct v4l2_file_operations uvc_fops;
716 /* Media controller */
717 int uvc_mc_register_entities(struct uvc_video_chain *chain);
718 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
721 int uvc_video_init(struct uvc_streaming *stream);
722 int uvc_video_suspend(struct uvc_streaming *stream);
723 int uvc_video_resume(struct uvc_streaming *stream, int reset);
724 int uvc_video_start_streaming(struct uvc_streaming *stream);
725 void uvc_video_stop_streaming(struct uvc_streaming *stream);
726 int uvc_probe_video(struct uvc_streaming *stream,
727 struct uvc_streaming_control *probe);
728 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
729 u8 intfnum, u8 cs, void *data, u16 size);
730 void uvc_video_clock_update(struct uvc_streaming *stream,
731 struct vb2_v4l2_buffer *vbuf,
732 struct uvc_buffer *buf);
733 int uvc_meta_register(struct uvc_streaming *stream);
735 int uvc_register_video_device(struct uvc_device *dev,
736 struct uvc_streaming *stream,
737 struct video_device *vdev,
738 struct uvc_video_queue *queue,
739 enum v4l2_buf_type type,
740 const struct v4l2_file_operations *fops,
741 const struct v4l2_ioctl_ops *ioctl_ops);
744 int uvc_status_init(struct uvc_device *dev);
745 void uvc_status_unregister(struct uvc_device *dev);
746 void uvc_status_cleanup(struct uvc_device *dev);
747 int uvc_status_start(struct uvc_device *dev, gfp_t flags);
748 void uvc_status_stop(struct uvc_device *dev);
751 extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited;
752 extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11;
753 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
755 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
756 struct v4l2_queryctrl *v4l2_ctrl);
757 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
758 struct v4l2_querymenu *query_menu);
760 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
761 const struct uvc_control_mapping *mapping);
762 int uvc_ctrl_init_device(struct uvc_device *dev);
763 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
764 int uvc_ctrl_restore_values(struct uvc_device *dev);
765 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
766 struct uvc_control *ctrl, const u8 *data);
767 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
768 struct uvc_control *ctrl, const u8 *data);
770 int uvc_ctrl_begin(struct uvc_video_chain *chain);
771 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
772 struct v4l2_ext_controls *ctrls);
773 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
774 struct v4l2_ext_controls *ctrls)
776 return __uvc_ctrl_commit(handle, 0, ctrls);
778 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
780 return __uvc_ctrl_commit(handle, 1, NULL);
783 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
784 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
785 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
786 const struct v4l2_ext_controls *ctrls,
787 unsigned long ioctl);
789 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
790 struct uvc_xu_control_query *xqry);
792 /* Utility functions */
793 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
795 u16 uvc_endpoint_max_bpi(struct usb_device *dev, struct usb_host_endpoint *ep);
798 void uvc_video_decode_isight(struct uvc_urb *uvc_urb,
799 struct uvc_buffer *buf,
800 struct uvc_buffer *meta_buf);
802 /* debugfs and statistics */
803 void uvc_debugfs_init(void);
804 void uvc_debugfs_cleanup(void);
805 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
806 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
808 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,