uvcvideo.h 24 KB

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