GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / usb / gadget / function / uvc.h
1 /*
2  *      uvc_gadget.h  --  USB Video Class Gadget driver
3  *
4  *      Copyright (C) 2009-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  */
12
13 #ifndef _UVC_GADGET_H_
14 #define _UVC_GADGET_H_
15
16 #include <linux/ioctl.h>
17 #include <linux/types.h>
18 #include <linux/usb/ch9.h>
19
20 #define UVC_EVENT_FIRST                 (V4L2_EVENT_PRIVATE_START + 0)
21 #define UVC_EVENT_CONNECT               (V4L2_EVENT_PRIVATE_START + 0)
22 #define UVC_EVENT_DISCONNECT            (V4L2_EVENT_PRIVATE_START + 1)
23 #define UVC_EVENT_STREAMON              (V4L2_EVENT_PRIVATE_START + 2)
24 #define UVC_EVENT_STREAMOFF             (V4L2_EVENT_PRIVATE_START + 3)
25 #define UVC_EVENT_SETUP                 (V4L2_EVENT_PRIVATE_START + 4)
26 #define UVC_EVENT_DATA                  (V4L2_EVENT_PRIVATE_START + 5)
27 #define UVC_EVENT_LAST                  (V4L2_EVENT_PRIVATE_START + 5)
28
29 struct uvc_request_data {
30         __s32 length;
31         __u8 data[60];
32 };
33
34 struct uvc_event {
35         union {
36                 enum usb_device_speed speed;
37                 struct usb_ctrlrequest req;
38                 struct uvc_request_data data;
39         };
40 };
41
42 #define UVCIOC_SEND_RESPONSE            _IOW('U', 1, struct uvc_request_data)
43
44 #define UVC_INTF_CONTROL                0
45 #define UVC_INTF_STREAMING              1
46
47 /* ------------------------------------------------------------------------
48  * Debugging, printing and logging
49  */
50
51 #ifdef __KERNEL__
52
53 #include <linux/usb.h>  /* For usb_endpoint_* */
54 #include <linux/usb/composite.h>
55 #include <linux/usb/gadget.h>
56 #include <linux/videodev2.h>
57 #include <media/v4l2-fh.h>
58 #include <media/v4l2-device.h>
59
60 #include "uvc_queue.h"
61
62 #define UVC_TRACE_PROBE                         (1 << 0)
63 #define UVC_TRACE_DESCR                         (1 << 1)
64 #define UVC_TRACE_CONTROL                       (1 << 2)
65 #define UVC_TRACE_FORMAT                        (1 << 3)
66 #define UVC_TRACE_CAPTURE                       (1 << 4)
67 #define UVC_TRACE_CALLS                         (1 << 5)
68 #define UVC_TRACE_IOCTL                         (1 << 6)
69 #define UVC_TRACE_FRAME                         (1 << 7)
70 #define UVC_TRACE_SUSPEND                       (1 << 8)
71 #define UVC_TRACE_STATUS                        (1 << 9)
72
73 #define UVC_WARN_MINMAX                         0
74 #define UVC_WARN_PROBE_DEF                      1
75
76 extern unsigned int uvc_gadget_trace_param;
77
78 #define uvc_trace(flag, msg...) \
79         do { \
80                 if (uvc_gadget_trace_param & flag) \
81                         printk(KERN_DEBUG "uvcvideo: " msg); \
82         } while (0)
83
84 #define uvc_warn_once(dev, warn, msg...) \
85         do { \
86                 if (!test_and_set_bit(warn, &dev->warnings)) \
87                         printk(KERN_INFO "uvcvideo: " msg); \
88         } while (0)
89
90 #define uvc_printk(level, msg...) \
91         printk(level "uvcvideo: " msg)
92
93 /* ------------------------------------------------------------------------
94  * Driver specific constants
95  */
96
97 #define UVC_NUM_REQUESTS                        4
98 #define UVC_MAX_REQUEST_SIZE                    64
99 #define UVC_MAX_EVENTS                          4
100
101 /* ------------------------------------------------------------------------
102  * Structures
103  */
104
105 struct uvc_video {
106         struct usb_ep *ep;
107
108         /* Frame parameters */
109         u8 bpp;
110         u32 fcc;
111         unsigned int width;
112         unsigned int height;
113         unsigned int imagesize;
114         struct mutex mutex;     /* protects frame parameters */
115
116         /* Requests */
117         unsigned int req_size;
118         struct usb_request *req[UVC_NUM_REQUESTS];
119         __u8 *req_buffer[UVC_NUM_REQUESTS];
120         struct list_head req_free;
121         spinlock_t req_lock;
122
123         void (*encode) (struct usb_request *req, struct uvc_video *video,
124                         struct uvc_buffer *buf);
125
126         /* Context data used by the completion handler */
127         __u32 payload_size;
128         __u32 max_payload_size;
129
130         struct uvc_video_queue queue;
131         unsigned int fid;
132 };
133
134 enum uvc_state {
135         UVC_STATE_DISCONNECTED,
136         UVC_STATE_CONNECTED,
137         UVC_STATE_STREAMING,
138 };
139
140 struct uvc_device {
141         struct video_device vdev;
142         struct v4l2_device v4l2_dev;
143         enum uvc_state state;
144         struct usb_function func;
145         struct uvc_video video;
146
147         /* Descriptors */
148         struct {
149                 const struct uvc_descriptor_header * const *fs_control;
150                 const struct uvc_descriptor_header * const *ss_control;
151                 const struct uvc_descriptor_header * const *fs_streaming;
152                 const struct uvc_descriptor_header * const *hs_streaming;
153                 const struct uvc_descriptor_header * const *ss_streaming;
154         } desc;
155
156         unsigned int control_intf;
157         struct usb_ep *control_ep;
158         struct usb_request *control_req;
159         void *control_buf;
160
161         unsigned int streaming_intf;
162
163         /* Events */
164         unsigned int event_length;
165         unsigned int event_setup_out : 1;
166 };
167
168 static inline struct uvc_device *to_uvc(struct usb_function *f)
169 {
170         return container_of(f, struct uvc_device, func);
171 }
172
173 struct uvc_file_handle {
174         struct v4l2_fh vfh;
175         struct uvc_video *device;
176 };
177
178 #define to_uvc_file_handle(handle) \
179         container_of(handle, struct uvc_file_handle, vfh)
180
181 /* ------------------------------------------------------------------------
182  * Functions
183  */
184
185 extern void uvc_function_setup_continue(struct uvc_device *uvc);
186 extern void uvc_endpoint_stream(struct uvc_device *dev);
187
188 extern void uvc_function_connect(struct uvc_device *uvc);
189 extern void uvc_function_disconnect(struct uvc_device *uvc);
190
191 #endif /* __KERNEL__ */
192
193 #endif /* _UVC_GADGET_H_ */
194