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