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