uvcvideo.h 23 KB

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