Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / media / usb / uvc / uvcvideo.h
1 #ifndef _USB_VIDEO_H_
2 #define _USB_VIDEO_H_
3
4 #ifndef __KERNEL__
5 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
6 #endif /* __KERNEL__ */
7
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/v4l2-event.h>
17 #include <media/v4l2-fh.h>
18 #include <media/videobuf2-v4l2.h>
19
20 /* --------------------------------------------------------------------------
21  * UVC constants
22  */
23
24 #define UVC_TERM_INPUT                  0x0000
25 #define UVC_TERM_OUTPUT                 0x8000
26 #define UVC_TERM_DIRECTION(term)        ((term)->type & 0x8000)
27
28 #define UVC_ENTITY_TYPE(entity)         ((entity)->type & 0x7fff)
29 #define UVC_ENTITY_IS_UNIT(entity)      (((entity)->type & 0xff00) == 0)
30 #define UVC_ENTITY_IS_TERM(entity)      (((entity)->type & 0xff00) != 0)
31 #define UVC_ENTITY_IS_ITERM(entity) \
32         (UVC_ENTITY_IS_TERM(entity) && \
33         ((entity)->type & 0x8000) == UVC_TERM_INPUT)
34 #define UVC_ENTITY_IS_OTERM(entity) \
35         (UVC_ENTITY_IS_TERM(entity) && \
36         ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
37
38
39 /* ------------------------------------------------------------------------
40  * GUIDs
41  */
42 #define UVC_GUID_UVC_CAMERA \
43         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
44          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
45 #define UVC_GUID_UVC_OUTPUT \
46         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
47          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
48 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
49         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
50          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
51 #define UVC_GUID_UVC_PROCESSING \
52         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
53          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
54 #define UVC_GUID_UVC_SELECTOR \
55         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
56          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
57
58 #define UVC_GUID_FORMAT_MJPEG \
59         { 'M',  'J',  'P',  'G', 0x00, 0x00, 0x10, 0x00, \
60          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
61 #define UVC_GUID_FORMAT_YUY2 \
62         { 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
63          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
64 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
65         { 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
66          0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
67 #define UVC_GUID_FORMAT_NV12 \
68         { 'N',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
69          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
70 #define UVC_GUID_FORMAT_YV12 \
71         { 'Y',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
72          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
73 #define UVC_GUID_FORMAT_I420 \
74         { 'I',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
75          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
76 #define UVC_GUID_FORMAT_UYVY \
77         { 'U',  'Y',  'V',  'Y', 0x00, 0x00, 0x10, 0x00, \
78          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
79 #define UVC_GUID_FORMAT_Y800 \
80         { 'Y',  '8',  '0',  '0', 0x00, 0x00, 0x10, 0x00, \
81          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
82 #define UVC_GUID_FORMAT_Y8 \
83         { 'Y',  '8',  ' ',  ' ', 0x00, 0x00, 0x10, 0x00, \
84          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
85 #define UVC_GUID_FORMAT_Y10 \
86         { 'Y',  '1',  '0',  ' ', 0x00, 0x00, 0x10, 0x00, \
87          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
88 #define UVC_GUID_FORMAT_Y12 \
89         { 'Y',  '1',  '2',  ' ', 0x00, 0x00, 0x10, 0x00, \
90          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
91 #define UVC_GUID_FORMAT_Y16 \
92         { 'Y',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
93          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
94 #define UVC_GUID_FORMAT_BY8 \
95         { 'B',  'Y',  '8',  ' ', 0x00, 0x00, 0x10, 0x00, \
96          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
97 #define UVC_GUID_FORMAT_BA81 \
98         { 'B',  'A',  '8',  '1', 0x00, 0x00, 0x10, 0x00, \
99          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
100 #define UVC_GUID_FORMAT_GBRG \
101         { 'G',  'B',  'R',  'G', 0x00, 0x00, 0x10, 0x00, \
102          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
103 #define UVC_GUID_FORMAT_GRBG \
104         { 'G',  'R',  'B',  'G', 0x00, 0x00, 0x10, 0x00, \
105          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
106 #define UVC_GUID_FORMAT_RGGB \
107         { 'R',  'G',  'G',  'B', 0x00, 0x00, 0x10, 0x00, \
108          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
109 #define UVC_GUID_FORMAT_RGBP \
110         { 'R',  'G',  'B',  'P', 0x00, 0x00, 0x10, 0x00, \
111          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
112 #define UVC_GUID_FORMAT_BGR3 \
113         { 0x7d, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, \
114          0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}
115 #define UVC_GUID_FORMAT_M420 \
116         { 'M',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
117          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
118
119 #define UVC_GUID_FORMAT_H264 \
120         { 'H',  '2',  '6',  '4', 0x00, 0x00, 0x10, 0x00, \
121          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
122 #define UVC_GUID_FORMAT_Y8I \
123         { 'Y',  '8',  'I',  ' ', 0x00, 0x00, 0x10, 0x00, \
124          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
125 #define UVC_GUID_FORMAT_Y12I \
126         { 'Y',  '1',  '2',  'I', 0x00, 0x00, 0x10, 0x00, \
127          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
128 #define UVC_GUID_FORMAT_Z16 \
129         { 'Z',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
130          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
131 #define UVC_GUID_FORMAT_RW10 \
132         { 'R',  'W',  '1',  '0', 0x00, 0x00, 0x10, 0x00, \
133          0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
134
135 /* ------------------------------------------------------------------------
136  * Driver specific constants.
137  */
138
139 #define DRIVER_VERSION          "1.1.1"
140
141 /* Number of isochronous URBs. */
142 #define UVC_URBS                5
143 /* Maximum number of packets per URB. */
144 #define UVC_MAX_PACKETS         32
145 /* Maximum status buffer size in bytes of interrupt URB. */
146 #define UVC_MAX_STATUS_SIZE     16
147
148 #define UVC_CTRL_CONTROL_TIMEOUT        300
149 #define UVC_CTRL_STREAMING_TIMEOUT      5000
150
151 /* Maximum allowed number of control mappings per device */
152 #define UVC_MAX_CONTROL_MAPPINGS        1024
153 #define UVC_MAX_CONTROL_MENU_ENTRIES    32
154
155 /* Devices quirks */
156 #define UVC_QUIRK_STATUS_INTERVAL       0x00000001
157 #define UVC_QUIRK_PROBE_MINMAX          0x00000002
158 #define UVC_QUIRK_PROBE_EXTRAFIELDS     0x00000004
159 #define UVC_QUIRK_BUILTIN_ISIGHT        0x00000008
160 #define UVC_QUIRK_STREAM_NO_FID         0x00000010
161 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT  0x00000020
162 #define UVC_QUIRK_FIX_BANDWIDTH         0x00000080
163 #define UVC_QUIRK_PROBE_DEF             0x00000100
164 #define UVC_QUIRK_RESTRICT_FRAME_RATE   0x00000200
165 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT 0x00000400
166 #define UVC_QUIRK_FORCE_Y8              0x00000800
167
168 /* Format flags */
169 #define UVC_FMT_FLAG_COMPRESSED         0x00000001
170 #define UVC_FMT_FLAG_STREAM             0x00000002
171
172 /* ------------------------------------------------------------------------
173  * Structures.
174  */
175
176 struct uvc_device;
177
178 /* TODO: Put the most frequently accessed fields at the beginning of
179  * structures to maximize cache efficiency.
180  */
181 struct uvc_control_info {
182         struct list_head mappings;
183
184         __u8 entity[16];
185         __u8 index;     /* Bit index in bmControls */
186         __u8 selector;
187
188         __u16 size;
189         __u32 flags;
190 };
191
192 struct uvc_control_mapping {
193         struct list_head list;
194         struct list_head ev_subs;
195
196         __u32 id;
197         __u8 name[32];
198         __u8 entity[16];
199         __u8 selector;
200
201         __u8 size;
202         __u8 offset;
203         enum v4l2_ctrl_type v4l2_type;
204         __u32 data_type;
205
206         struct uvc_menu_info *menu_info;
207         __u32 menu_count;
208
209         __u32 master_id;
210         __s32 master_manual;
211         __u32 slave_ids[2];
212
213         __s32 (*get) (struct uvc_control_mapping *mapping, __u8 query,
214                       const __u8 *data);
215         void (*set) (struct uvc_control_mapping *mapping, __s32 value,
216                      __u8 *data);
217 };
218
219 struct uvc_control {
220         struct uvc_entity *entity;
221         struct uvc_control_info info;
222
223         __u8 index;     /* Used to match the uvc_control entry with a
224                            uvc_control_info. */
225         __u8 dirty:1,
226              loaded:1,
227              modified:1,
228              cached:1,
229              initialized:1;
230
231         __u8 *uvc_data;
232 };
233
234 struct uvc_format_desc {
235         char *name;
236         __u8 guid[16];
237         __u32 fcc;
238 };
239
240 /* The term 'entity' refers to both UVC units and UVC terminals.
241  *
242  * The type field is either the terminal type (wTerminalType in the terminal
243  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
244  * As the bDescriptorSubtype field is one byte long, the type value will
245  * always have a null MSB for units. All terminal types defined by the UVC
246  * specification have a non-null MSB, so it is safe to use the MSB to
247  * differentiate between units and terminals as long as the descriptor parsing
248  * code makes sure terminal types have a non-null MSB.
249  *
250  * For terminals, the type's most significant bit stores the terminal
251  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
252  * always be accessed with the UVC_ENTITY_* macros and never directly.
253  */
254
255 #define UVC_ENTITY_FLAG_DEFAULT         (1 << 0)
256
257 struct uvc_entity {
258         struct list_head list;          /* Entity as part of a UVC device. */
259         struct list_head chain;         /* Entity as part of a video device
260                                          * chain. */
261         unsigned int flags;
262
263         __u8 id;
264         __u16 type;
265         char name[64];
266
267         /* Media controller-related fields. */
268         struct video_device *vdev;
269         struct v4l2_subdev subdev;
270         unsigned int num_pads;
271         unsigned int num_links;
272         struct media_pad *pads;
273
274         union {
275                 struct {
276                         __u16 wObjectiveFocalLengthMin;
277                         __u16 wObjectiveFocalLengthMax;
278                         __u16 wOcularFocalLength;
279                         __u8  bControlSize;
280                         __u8  *bmControls;
281                 } camera;
282
283                 struct {
284                         __u8  bControlSize;
285                         __u8  *bmControls;
286                         __u8  bTransportModeSize;
287                         __u8  *bmTransportModes;
288                 } media;
289
290                 struct {
291                 } output;
292
293                 struct {
294                         __u16 wMaxMultiplier;
295                         __u8  bControlSize;
296                         __u8  *bmControls;
297                         __u8  bmVideoStandards;
298                 } processing;
299
300                 struct {
301                 } selector;
302
303                 struct {
304                         __u8  guidExtensionCode[16];
305                         __u8  bNumControls;
306                         __u8  bControlSize;
307                         __u8  *bmControls;
308                         __u8  *bmControlsType;
309                 } extension;
310         };
311
312         __u8 bNrInPins;
313         __u8 *baSourceID;
314
315         unsigned int ncontrols;
316         struct uvc_control *controls;
317 };
318
319 struct uvc_frame {
320         __u8  bFrameIndex;
321         __u8  bmCapabilities;
322         __u16 wWidth;
323         __u16 wHeight;
324         __u32 dwMinBitRate;
325         __u32 dwMaxBitRate;
326         __u32 dwMaxVideoFrameBufferSize;
327         __u8  bFrameIntervalType;
328         __u32 dwDefaultFrameInterval;
329         __u32 *dwFrameInterval;
330 };
331
332 struct uvc_format {
333         __u8 type;
334         __u8 index;
335         __u8 bpp;
336         __u8 colorspace;
337         __u32 fcc;
338         __u32 flags;
339
340         char name[32];
341
342         unsigned int nframes;
343         struct uvc_frame *frame;
344 };
345
346 struct uvc_streaming_header {
347         __u8 bNumFormats;
348         __u8 bEndpointAddress;
349         __u8 bTerminalLink;
350         __u8 bControlSize;
351         __u8 *bmaControls;
352         /* The following fields are used by input headers only. */
353         __u8 bmInfo;
354         __u8 bStillCaptureMethod;
355         __u8 bTriggerSupport;
356         __u8 bTriggerUsage;
357 };
358
359 enum uvc_buffer_state {
360         UVC_BUF_STATE_IDLE      = 0,
361         UVC_BUF_STATE_QUEUED    = 1,
362         UVC_BUF_STATE_ACTIVE    = 2,
363         UVC_BUF_STATE_READY     = 3,
364         UVC_BUF_STATE_DONE      = 4,
365         UVC_BUF_STATE_ERROR     = 5,
366 };
367
368 struct uvc_buffer {
369         struct vb2_v4l2_buffer buf;
370         struct list_head queue;
371
372         enum uvc_buffer_state state;
373         unsigned int error;
374
375         void *mem;
376         unsigned int length;
377         unsigned int bytesused;
378
379         u32 pts;
380 };
381
382 #define UVC_QUEUE_DISCONNECTED          (1 << 0)
383 #define UVC_QUEUE_DROP_CORRUPTED        (1 << 1)
384
385 struct uvc_video_queue {
386         struct vb2_queue queue;
387         struct mutex mutex;                     /* Protects queue */
388
389         unsigned int flags;
390         unsigned int buf_used;
391
392         spinlock_t irqlock;                     /* Protects irqqueue */
393         struct list_head irqqueue;
394 };
395
396 struct uvc_video_chain {
397         struct uvc_device *dev;
398         struct list_head list;
399
400         struct list_head entities;              /* All entities */
401         struct uvc_entity *processing;          /* Processing unit */
402         struct uvc_entity *selector;            /* Selector unit */
403
404         struct mutex ctrl_mutex;                /* Protects ctrl.info */
405
406         struct v4l2_prio_state prio;            /* V4L2 priority state */
407         u32 caps;                               /* V4L2 chain-wide caps */
408 };
409
410 struct uvc_stats_frame {
411         unsigned int size;              /* Number of bytes captured */
412         unsigned int first_data;        /* Index of the first non-empty packet */
413
414         unsigned int nb_packets;        /* Number of packets */
415         unsigned int nb_empty;          /* Number of empty packets */
416         unsigned int nb_invalid;        /* Number of packets with an invalid header */
417         unsigned int nb_errors;         /* Number of packets with the error bit set */
418
419         unsigned int nb_pts;            /* Number of packets with a PTS timestamp */
420         unsigned int nb_pts_diffs;      /* Number of PTS differences inside a frame */
421         unsigned int last_pts_diff;     /* Index of the last PTS difference */
422         bool has_initial_pts;           /* Whether the first non-empty packet has a PTS */
423         bool has_early_pts;             /* Whether a PTS is present before the first non-empty packet */
424         u32 pts;                        /* PTS of the last packet */
425
426         unsigned int nb_scr;            /* Number of packets with a SCR timestamp */
427         unsigned int nb_scr_diffs;      /* Number of SCR.STC differences inside a frame */
428         u16 scr_sof;                    /* SCR.SOF of the last packet */
429         u32 scr_stc;                    /* SCR.STC of the last packet */
430 };
431
432 struct uvc_stats_stream {
433         struct timespec start_ts;       /* Stream start timestamp */
434         struct timespec stop_ts;        /* Stream stop timestamp */
435
436         unsigned int nb_frames;         /* Number of frames */
437
438         unsigned int nb_packets;        /* Number of packets */
439         unsigned int nb_empty;          /* Number of empty packets */
440         unsigned int nb_invalid;        /* Number of packets with an invalid header */
441         unsigned int nb_errors;         /* Number of packets with the error bit set */
442
443         unsigned int nb_pts_constant;   /* Number of frames with constant PTS */
444         unsigned int nb_pts_early;      /* Number of frames with early PTS */
445         unsigned int nb_pts_initial;    /* Number of frames with initial PTS */
446
447         unsigned int nb_scr_count_ok;   /* Number of frames with at least one SCR per non empty packet */
448         unsigned int nb_scr_diffs_ok;   /* Number of frames with varying SCR.STC */
449         unsigned int scr_sof_count;     /* STC.SOF counter accumulated since stream start */
450         unsigned int scr_sof;           /* STC.SOF of the last packet */
451         unsigned int min_sof;           /* Minimum STC.SOF value */
452         unsigned int max_sof;           /* Maximum STC.SOF value */
453 };
454
455 struct uvc_streaming {
456         struct list_head list;
457         struct uvc_device *dev;
458         struct video_device vdev;
459         struct uvc_video_chain *chain;
460         atomic_t active;
461
462         struct usb_interface *intf;
463         int intfnum;
464         __u16 maxpsize;
465
466         struct uvc_streaming_header header;
467         enum v4l2_buf_type type;
468
469         unsigned int nformats;
470         struct uvc_format *format;
471
472         struct uvc_streaming_control ctrl;
473         struct uvc_format *def_format;
474         struct uvc_format *cur_format;
475         struct uvc_frame *cur_frame;
476
477         /* Protect access to ctrl, cur_format, cur_frame and hardware video
478          * probe control.
479          */
480         struct mutex mutex;
481
482         /* Buffers queue. */
483         unsigned int frozen : 1;
484         struct uvc_video_queue queue;
485         void (*decode) (struct urb *urb, struct uvc_streaming *video,
486                         struct uvc_buffer *buf);
487
488         /* Context data used by the bulk completion handler. */
489         struct {
490                 __u8 header[256];
491                 unsigned int header_size;
492                 int skip_payload;
493                 __u32 payload_size;
494                 __u32 max_payload_size;
495         } bulk;
496
497         struct urb *urb[UVC_URBS];
498         char *urb_buffer[UVC_URBS];
499         dma_addr_t urb_dma[UVC_URBS];
500         unsigned int urb_size;
501
502         __u32 sequence;
503         __u8 last_fid;
504
505         /* debugfs */
506         struct dentry *debugfs_dir;
507         struct {
508                 struct uvc_stats_frame frame;
509                 struct uvc_stats_stream stream;
510         } stats;
511
512         /* Timestamps support. */
513         struct uvc_clock {
514                 struct uvc_clock_sample {
515                         u32 dev_stc;
516                         u16 dev_sof;
517                         struct timespec host_ts;
518                         u16 host_sof;
519                 } *samples;
520
521                 unsigned int head;
522                 unsigned int count;
523                 unsigned int size;
524
525                 u16 last_sof;
526                 u16 sof_offset;
527
528                 spinlock_t lock;
529         } clock;
530 };
531
532 struct uvc_device {
533         struct usb_device *udev;
534         struct usb_interface *intf;
535         unsigned long warnings;
536         __u32 quirks;
537         int intfnum;
538         char name[32];
539
540         struct mutex lock;              /* Protects users */
541         unsigned int users;
542         atomic_t nmappings;
543
544         /* Video control interface */
545 #ifdef CONFIG_MEDIA_CONTROLLER
546         struct media_device mdev;
547 #endif
548         struct v4l2_device vdev;
549         __u16 uvc_version;
550         __u32 clock_frequency;
551
552         struct list_head entities;
553         struct list_head chains;
554
555         /* Video Streaming interfaces */
556         struct list_head streams;
557         atomic_t nstreams;
558
559         /* Status Interrupt Endpoint */
560         struct usb_host_endpoint *int_ep;
561         struct urb *int_urb;
562         __u8 *status;
563         struct input_dev *input;
564         char input_phys[64];
565 };
566
567 enum uvc_handle_state {
568         UVC_HANDLE_PASSIVE      = 0,
569         UVC_HANDLE_ACTIVE       = 1,
570 };
571
572 struct uvc_fh {
573         struct v4l2_fh vfh;
574         struct uvc_video_chain *chain;
575         struct uvc_streaming *stream;
576         enum uvc_handle_state state;
577 };
578
579 struct uvc_driver {
580         struct usb_driver driver;
581 };
582
583 /* ------------------------------------------------------------------------
584  * Debugging, printing and logging
585  */
586
587 #define UVC_TRACE_PROBE         (1 << 0)
588 #define UVC_TRACE_DESCR         (1 << 1)
589 #define UVC_TRACE_CONTROL       (1 << 2)
590 #define UVC_TRACE_FORMAT        (1 << 3)
591 #define UVC_TRACE_CAPTURE       (1 << 4)
592 #define UVC_TRACE_CALLS         (1 << 5)
593 #define UVC_TRACE_FRAME         (1 << 7)
594 #define UVC_TRACE_SUSPEND       (1 << 8)
595 #define UVC_TRACE_STATUS        (1 << 9)
596 #define UVC_TRACE_VIDEO         (1 << 10)
597 #define UVC_TRACE_STATS         (1 << 11)
598 #define UVC_TRACE_CLOCK         (1 << 12)
599
600 #define UVC_WARN_MINMAX         0
601 #define UVC_WARN_PROBE_DEF      1
602 #define UVC_WARN_XU_GET_RES     2
603
604 extern unsigned int uvc_clock_param;
605 extern unsigned int uvc_no_drop_param;
606 extern unsigned int uvc_trace_param;
607 extern unsigned int uvc_timeout_param;
608 extern unsigned int uvc_hw_timestamps_param;
609
610 #define uvc_trace(flag, msg...) \
611         do { \
612                 if (uvc_trace_param & flag) \
613                         printk(KERN_DEBUG "uvcvideo: " msg); \
614         } while (0)
615
616 #define uvc_warn_once(dev, warn, msg...) \
617         do { \
618                 if (!test_and_set_bit(warn, &dev->warnings)) \
619                         printk(KERN_INFO "uvcvideo: " msg); \
620         } while (0)
621
622 #define uvc_printk(level, msg...) \
623         printk(level "uvcvideo: " msg)
624
625 /* --------------------------------------------------------------------------
626  * Internal functions.
627  */
628
629 /* Core driver */
630 extern struct uvc_driver uvc_driver;
631
632 extern struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
633
634 /* Video buffers queue management. */
635 extern int uvc_queue_init(struct uvc_video_queue *queue,
636                 enum v4l2_buf_type type, int drop_corrupted);
637 extern void uvc_queue_release(struct uvc_video_queue *queue);
638 extern int uvc_request_buffers(struct uvc_video_queue *queue,
639                 struct v4l2_requestbuffers *rb);
640 extern int uvc_query_buffer(struct uvc_video_queue *queue,
641                 struct v4l2_buffer *v4l2_buf);
642 extern int uvc_create_buffers(struct uvc_video_queue *queue,
643                 struct v4l2_create_buffers *v4l2_cb);
644 extern int uvc_queue_buffer(struct uvc_video_queue *queue,
645                 struct v4l2_buffer *v4l2_buf);
646 extern int uvc_export_buffer(struct uvc_video_queue *queue,
647                 struct v4l2_exportbuffer *exp);
648 extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
649                 struct v4l2_buffer *v4l2_buf, int nonblocking);
650 extern int uvc_queue_streamon(struct uvc_video_queue *queue,
651                               enum v4l2_buf_type type);
652 extern int uvc_queue_streamoff(struct uvc_video_queue *queue,
653                                enum v4l2_buf_type type);
654 extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
655 extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
656                 struct uvc_buffer *buf);
657 extern int uvc_queue_mmap(struct uvc_video_queue *queue,
658                 struct vm_area_struct *vma);
659 extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
660                 struct file *file, poll_table *wait);
661 #ifndef CONFIG_MMU
662 extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
663                 unsigned long pgoff);
664 #endif
665 extern int uvc_queue_allocated(struct uvc_video_queue *queue);
666 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
667 {
668         return vb2_is_streaming(&queue->queue);
669 }
670
671 /* V4L2 interface */
672 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
673 extern const struct v4l2_file_operations uvc_fops;
674
675 /* Media controller */
676 extern int uvc_mc_register_entities(struct uvc_video_chain *chain);
677 extern void uvc_mc_cleanup_entity(struct uvc_entity *entity);
678
679 /* Video */
680 extern int uvc_video_init(struct uvc_streaming *stream);
681 extern int uvc_video_suspend(struct uvc_streaming *stream);
682 extern int uvc_video_resume(struct uvc_streaming *stream, int reset);
683 extern int uvc_video_enable(struct uvc_streaming *stream, int enable);
684 extern int uvc_probe_video(struct uvc_streaming *stream,
685                 struct uvc_streaming_control *probe);
686 extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
687                 __u8 intfnum, __u8 cs, void *data, __u16 size);
688 void uvc_video_clock_update(struct uvc_streaming *stream,
689                             struct vb2_v4l2_buffer *vbuf,
690                             struct uvc_buffer *buf);
691
692 /* Status */
693 extern int uvc_status_init(struct uvc_device *dev);
694 extern void uvc_status_cleanup(struct uvc_device *dev);
695 extern int uvc_status_start(struct uvc_device *dev, gfp_t flags);
696 extern void uvc_status_stop(struct uvc_device *dev);
697
698 /* Controls */
699 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
700
701 extern int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
702                 struct v4l2_queryctrl *v4l2_ctrl);
703 extern int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
704                 struct v4l2_querymenu *query_menu);
705
706 extern int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
707                 const struct uvc_control_mapping *mapping);
708 extern int uvc_ctrl_init_device(struct uvc_device *dev);
709 extern void uvc_ctrl_cleanup_device(struct uvc_device *dev);
710 extern int uvc_ctrl_restore_values(struct uvc_device *dev);
711
712 extern int uvc_ctrl_begin(struct uvc_video_chain *chain);
713 extern int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
714                         const struct v4l2_ext_control *xctrls,
715                         unsigned int xctrls_count);
716 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
717                         const struct v4l2_ext_control *xctrls,
718                         unsigned int xctrls_count)
719 {
720         return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
721 }
722 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
723 {
724         return __uvc_ctrl_commit(handle, 1, NULL, 0);
725 }
726
727 extern int uvc_ctrl_get(struct uvc_video_chain *chain,
728                 struct v4l2_ext_control *xctrl);
729 extern int uvc_ctrl_set(struct uvc_video_chain *chain,
730                 struct v4l2_ext_control *xctrl);
731
732 extern int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
733                 struct uvc_xu_control_query *xqry);
734
735 /* Utility functions */
736 extern void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
737                 unsigned int n_terms, unsigned int threshold);
738 extern uint32_t uvc_fraction_to_interval(uint32_t numerator,
739                 uint32_t denominator);
740 extern struct usb_host_endpoint *uvc_find_endpoint(
741                 struct usb_host_interface *alts, __u8 epaddr);
742
743 /* Quirks support */
744 void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
745                 struct uvc_buffer *buf);
746
747 /* debugfs and statistics */
748 int uvc_debugfs_init(void);
749 void uvc_debugfs_cleanup(void);
750 int uvc_debugfs_init_stream(struct uvc_streaming *stream);
751 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
752
753 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
754                             size_t size);
755
756 #endif