uvcvideo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. ********************************************************************************
  3. * USB UVC Driver
  4. *
  5. * (c) Copyright 2010-2015, All winners Co,Ld.
  6. * All Right Reserved
  7. *
  8. * FileName : usbvideo.h
  9. *
  10. * Author : Kingvan.Tong
  11. *
  12. * Date : 2013.03.26
  13. *
  14. * Description : USB Webcam
  15. *
  16. * Others : NULL
  17. *
  18. * History:
  19. * <time> <version > <author> <desc>
  20. * 2013.03.26 1.0 Kingvan.Tong build this file
  21. *
  22. ********************************************************************************
  23. */
  24. #ifndef _USB_VIDEO_H_
  25. #define _USB_VIDEO_H_
  26. #include "usb_host_common.h"
  27. #include "videodev2.h"
  28. /* --------------------------------------------------------------------------
  29. * UVC constants
  30. */
  31. #define UVC_TERM_INPUT 0x0000
  32. #define UVC_TERM_OUTPUT 0x8000
  33. #define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
  34. #define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
  35. #define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
  36. #define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
  37. #define UVC_ENTITY_IS_ITERM(entity) \
  38. (UVC_ENTITY_IS_TERM(entity) && \
  39. ((entity)->type & 0x8000) == UVC_TERM_INPUT)
  40. #define UVC_ENTITY_IS_OTERM(entity) \
  41. (UVC_ENTITY_IS_TERM(entity) && \
  42. ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
  43. /* ------------------------------------------------------------------------
  44. * GUIDs
  45. */
  46. #define UVC_GUID_UVC_CAMERA \
  47. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
  49. #define UVC_GUID_UVC_OUTPUT \
  50. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
  52. #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
  53. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
  55. #define UVC_GUID_UVC_PROCESSING \
  56. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
  58. #define UVC_GUID_UVC_SELECTOR \
  59. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
  61. #define UVC_GUID_FORMAT_MJPEG \
  62. { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
  63. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  64. #define UVC_GUID_FORMAT_YUY2 \
  65. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
  66. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  67. #define UVC_GUID_FORMAT_YUY2_ISIGHT \
  68. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
  69. 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
  70. #define UVC_GUID_FORMAT_NV12 \
  71. { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
  72. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  73. #define UVC_GUID_FORMAT_YV12 \
  74. { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
  75. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  76. #define UVC_GUID_FORMAT_I420 \
  77. { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
  78. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  79. #define UVC_GUID_FORMAT_UYVY \
  80. { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
  81. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  82. #define UVC_GUID_FORMAT_Y800 \
  83. { 'Y', '8', '0', '0', 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_RGBP \
  92. { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
  93. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  94. #define UVC_GUID_FORMAT_M420 \
  95. { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
  96. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
  97. /* ------------------------------------------------------------------------
  98. * Driver specific constants.
  99. */
  100. #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  101. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(1, 1, 0)
  102. #define DRIVER_VERSION "v1.1.0"
  103. /* Number of isochronous URBs. */
  104. #define UVC_URBS 3//for otg
  105. /* Maximum number of packets per URB. */
  106. #define UVC_MAX_PACKETS 1024//for otg
  107. /* Maximum number of video buffers. */
  108. #define UVC_MAX_VIDEO_BUFFERS 32
  109. /* Maximum status buffer size in bytes of interrupt URB. */
  110. #define UVC_MAX_STATUS_SIZE 16
  111. #define UVC_CTRL_CONTROL_TIMEOUT 300
  112. #define UVC_CTRL_STREAMING_TIMEOUT 5000
  113. /* Maximum allowed number of control mappings per device */
  114. #define UVC_MAX_CONTROL_MAPPINGS 1024
  115. /* Devices quirks */
  116. #define UVC_QUIRK_STATUS_INTERVAL 0x00000001
  117. #define UVC_QUIRK_PROBE_MINMAX 0x00000002
  118. #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
  119. #define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
  120. #define UVC_QUIRK_STREAM_NO_FID 0x00000010
  121. #define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
  122. #define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
  123. #define UVC_QUIRK_PROBE_DEF 0x00000100
  124. #define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
  125. /* Format flags */
  126. #define UVC_FMT_FLAG_COMPRESSED 0x00000001
  127. #define UVC_FMT_FLAG_STREAM 0x00000002
  128. /* UVC device state */
  129. typedef enum _UVCDev_state{
  130. UVC_DEV_OFFLINE= 0, /* UVCDev已经拔出 */
  131. UVC_DEV_ONLINE, /* UVCDev已经添加 */
  132. UVC_DEV_DIED, /* UVCDev不可用 */
  133. UVC_DEV_RESET /* UVCDev正在被reset */
  134. }UVCDev_State_t;
  135. /* ------------------------------------------------------------------------
  136. * Structures.
  137. */
  138. /* The term 'entity' refers to both UVC units and UVC terminals.
  139. *
  140. * The type field is either the terminal type (wTerminalType in the terminal
  141. * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
  142. * As the bDescriptorSubtype field is one byte long, the type value will
  143. * always have a null MSB for units. All terminal types defined by the UVC
  144. * specification have a non-null MSB, so it is safe to use the MSB to
  145. * differentiate between units and terminals as long as the descriptor parsing
  146. * code makes sure terminal types have a non-null MSB.
  147. *
  148. * For terminals, the type's most significant bit stores the terminal
  149. * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
  150. * always be accessed with the UVC_ENTITY_* macros and never directly.
  151. */
  152. struct uvc_entity {
  153. // struct list_head list; /* Entity as part of a UVC device. */
  154. // struct list_head chain; /* Entity as part of a video device
  155. // * chain. */
  156. __u8 id;
  157. __u16 type;
  158. char name[64];
  159. // /* Media controller-related fields. */
  160. // struct video_device *vdev;
  161. // struct v4l2_subdev subdev;
  162. // unsigned int num_pads;
  163. // unsigned int num_links;
  164. // struct media_pad *pads;
  165. union {
  166. struct {
  167. __u16 wObjectiveFocalLengthMin;
  168. __u16 wObjectiveFocalLengthMax;
  169. __u16 wOcularFocalLength;
  170. __u8 bControlSize;
  171. __u8 *bmControls;
  172. } camera;
  173. struct {
  174. __u8 bControlSize;
  175. __u8 *bmControls;
  176. __u8 bTransportModeSize;
  177. __u8 *bmTransportModes;
  178. } media;
  179. //struct {
  180. //} output;
  181. struct {
  182. __u16 wMaxMultiplier;
  183. __u8 bControlSize;
  184. __u8 *bmControls;
  185. __u8 bmVideoStandards;
  186. } processing;
  187. //struct {
  188. //} selector;
  189. struct {
  190. __u8 guidExtensionCode[16];
  191. __u8 bNumControls;
  192. __u8 bControlSize;
  193. __u8 *bmControls;
  194. __u8 *bmControlsType;
  195. } extension;
  196. }un;
  197. __u8 bNrInPins;
  198. // __u8 *baSourceID;
  199. // unsigned int ncontrols;
  200. // struct uvc_control *controls;
  201. };
  202. struct uvc_frame {
  203. __u8 bFrameIndex;
  204. __u8 bmCapabilities;
  205. __u16 wWidth;
  206. __u16 wHeight;
  207. __u32 dwMinBitRate;
  208. __u32 dwMaxBitRate;
  209. __u32 dwMaxVideoFrameBufferSize;
  210. __u8 bFrameIntervalType;
  211. __u32 dwDefaultFrameInterval;
  212. __u32 *dwFrameInterval;
  213. };
  214. struct uvc_format {
  215. __u8 type;
  216. __u8 index;
  217. __u8 bpp;
  218. __u8 colorspace;
  219. __u32 fcc;
  220. __u32 flags;
  221. char name[32];
  222. unsigned int nframes;
  223. struct uvc_frame *frame;
  224. };
  225. struct uvc_format_desc {
  226. char *name;
  227. __u8 guid[16];
  228. __u32 fcc;
  229. };
  230. struct uvc_streaming_header {
  231. __u8 bNumFormats;
  232. __u8 bEndpointAddress;
  233. __u8 bTerminalLink;
  234. __u8 bControlSize;
  235. // __u8 *bmaControls;
  236. /* The following fields are used by input headers only. */
  237. __u8 bmInfo;
  238. __u8 bStillCaptureMethod;
  239. __u8 bTriggerSupport;
  240. __u8 bTriggerUsage;
  241. };
  242. enum uvc_buffer_state {
  243. UVC_BUF_STATE_IDLE = 0,
  244. UVC_BUF_STATE_QUEUED = 1,
  245. UVC_BUF_STATE_ACTIVE = 2,
  246. UVC_BUF_STATE_READY = 3,
  247. UVC_BUF_STATE_DONE = 4,
  248. UVC_BUF_STATE_ERROR = 5,
  249. };
  250. struct v4l2_buffer {
  251. __u32 mem_buf;
  252. __u32 length;
  253. //__u32 offset;
  254. __u32 bytesused;
  255. };
  256. struct uvc_buffer {
  257. /* Touched by interrupt handler. */
  258. struct v4l2_buffer buf;
  259. enum uvc_buffer_state state;
  260. unsigned int error;
  261. };
  262. typedef __s32 (* uvc_irq_t)(__u32* addr0, __u32* addr1, __u32* addr2 );
  263. typedef struct _UVCDev {
  264. struct usb_host_virt_dev *pusb_dev; /* UVCDev 对应的Public USB Device */
  265. struct usb_interface *pusb_intf; /* Public usb interface */
  266. /* device information */
  267. __u8 InterfaceNo; /* 接口号 */
  268. __u8 SubClass; /* 子类 */
  269. __u8 Protocol; /* 传输协议 */
  270. __u32 DevType; /* 设备类型 */
  271. __u32 DevNo; /* 设备在 UVC 管理中的编号 */
  272. /* device manager */
  273. UVCDev_State_t State; /* Dev当前所处的连接状态 */
  274. /* transport */
  275. __u32 CtrlIn; /* ctrl in pipe */
  276. __u32 CtrlOut; /* ctrl out pipe */
  277. __u32 IntIn; /* interrupt in pipe */
  278. __u32 BlukIn; /* bulk in pipe */
  279. __u32 IsoIn; /* iso in pipe */
  280. __u8 EpInterval; /* int 传输主机查询设备的周期 */
  281. __u32 OnceTransferLength; /* 中断ep的最大传输包大小 */
  282. __u32 busy; /* 主机是否正在处理命令 */
  283. __u32 quirks;
  284. __u16 uvc_version;
  285. __u32 clock_frequency;
  286. struct uvc_entity entities[UVC_VC_MAX+1];
  287. struct uvc_streaming* streams; //注意要释放
  288. void *Extern; /* 对应具体的UVC设备 */
  289. __u32 frame_len; //add by Kingvan
  290. __u32 frame_buf0; //add by Kingvan
  291. __u32 frame_buf1; //add by Kingvan
  292. __u32 frame_buf2; //add by Kingvan
  293. //void (*exchange_buf) (struct _UVCDev *UVCDev,
  294. // __u32* addr0, __u32* addr1, __u32* addr2 );//add by Kingvan
  295. uvc_irq_t exchange_buf;//add by Kingvan
  296. __s32 (*active_buf) (struct uvc_streaming *stream);//add by Kingvan
  297. __u32 urb_mem[UVC_URBS];
  298. }UVCDev_t;
  299. struct uvc_streaming {
  300. // struct list_head list;
  301. UVCDev_t *dev;
  302. // struct video_device *vdev;
  303. // struct uvc_video_chain *chain;
  304. // atomic_t active;
  305. struct usb_interface *intf;
  306. int intfnum;
  307. __u16 maxpsize;
  308. struct uvc_streaming_header header;
  309. enum v4l2_buf_type type;
  310. unsigned int nformats;
  311. struct uvc_format *format; //注意释放
  312. struct uvc_streaming_control ctrl;
  313. struct uvc_format *cur_format;
  314. struct uvc_frame *cur_frame;
  315. // /* Protect access to ctrl, cur_format, cur_frame and hardware video
  316. // * probe control.
  317. // */
  318. // struct mutex mutex;
  319. // unsigned int frozen : 1;
  320. // struct uvc_video_queue queue;
  321. void (*decode) (struct urb *urb, struct uvc_streaming *video,
  322. struct uvc_buffer *buf);
  323. /* Context data used by the bulk completion handler. */
  324. struct {
  325. __u8 header[256];
  326. unsigned int header_size;
  327. int skip_payload;
  328. __u32 payload_size;
  329. __u32 max_payload_size;
  330. } bulk;
  331. struct urb *urb[UVC_URBS];
  332. char *urb_buffer[UVC_URBS];
  333. // dma_addr_t urb_dma[UVC_URBS];
  334. unsigned int urb_size;
  335. __u32 sequence;
  336. __u8 last_fid;
  337. struct uvc_buffer buf;
  338. };
  339. enum uvc_handle_state {
  340. UVC_HANDLE_PASSIVE = 0,
  341. UVC_HANDLE_ACTIVE = 1,
  342. };
  343. struct uvc_fh {
  344. //struct uvc_video_chain *chain;
  345. struct uvc_streaming *stream;
  346. enum uvc_handle_state state;
  347. };
  348. /* Utility functions */
  349. extern void uvc_simplify_fraction(__u32 *numerator, __u32 *denominator,
  350. __u32 n_terms, __u32 threshold);
  351. extern __u32 uvc_fraction_to_interval(__u32 numerator,
  352. __u32 denominator);
  353. extern struct usb_host_virt_endpoint *uvc_find_endpoint(
  354. struct usb_host_virt_interface *alts, __u8 epaddr);
  355. /* Video */
  356. extern int uvc_video_init(struct uvc_streaming *stream);
  357. extern __s32 uvc_video_suspend(struct uvc_streaming *stream);
  358. extern __s32 uvc_video_resume(struct uvc_streaming *stream);
  359. extern int uvc_video_enable(struct uvc_streaming *stream, __s32 enable);
  360. extern int uvc_probe_video(struct uvc_streaming *stream,
  361. struct uvc_streaming_control *probe);
  362. extern int uvc_commit_video(struct uvc_streaming *stream,
  363. struct uvc_streaming_control *ctrl);
  364. extern int uvc_query_ctrl(UVCDev_t *dev, __u8 query, __u8 unit,
  365. __u8 intfnum, __u8 cs, void *data, __u16 size);
  366. extern void* usbWebcam_init(void);
  367. extern __s32 usbWebcam_exit(void* arg);
  368. extern __s32 usbWebcam_remove(UVCDev_t * UVCDev);
  369. extern __s32 usbWebcam_probe(UVCDev_t *UVCDev);
  370. #endif