1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * uvc_gadget.h -- USB Video Class Gadget driver
4 *
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
8
9 #ifndef _UVC_GADGET_H_
10 #define _UVC_GADGET_H_
11
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/pm_qos.h>
15 #include <linux/spinlock.h>
16 #include <linux/usb/composite.h>
17 #include <linux/videodev2.h>
18 #include <linux/wait.h>
19
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-dev.h>
22 #include <media/v4l2-fh.h>
23
24 #include "uvc_queue.h"
25
26 struct usb_ep;
27 struct usb_request;
28 struct uvc_descriptor_header;
29 struct uvc_device;
30
31 /* ------------------------------------------------------------------------
32 * Debugging, printing and logging
33 */
34
35 #define UVC_TRACE_PROBE (1 << 0)
36 #define UVC_TRACE_DESCR (1 << 1)
37 #define UVC_TRACE_CONTROL (1 << 2)
38 #define UVC_TRACE_FORMAT (1 << 3)
39 #define UVC_TRACE_CAPTURE (1 << 4)
40 #define UVC_TRACE_CALLS (1 << 5)
41 #define UVC_TRACE_IOCTL (1 << 6)
42 #define UVC_TRACE_FRAME (1 << 7)
43 #define UVC_TRACE_SUSPEND (1 << 8)
44 #define UVC_TRACE_STATUS (1 << 9)
45
46 #define UVC_WARN_MINMAX 0
47 #define UVC_WARN_PROBE_DEF 1
48
49 extern unsigned int uvc_gadget_trace_param;
50
51 #define uvc_trace(flag, msg...) \
52 do { \
53 if (uvc_gadget_trace_param & flag) \
54 printk(KERN_DEBUG "uvcvideo: " msg); \
55 } while (0)
56
57 #define uvcg_dbg(f, fmt, args...) \
58 dev_dbg(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args)
59 #define uvcg_info(f, fmt, args...) \
60 dev_info(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args)
61 #define uvcg_warn(f, fmt, args...) \
62 dev_warn(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args)
63 #define uvcg_err(f, fmt, args...) \
64 dev_err(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args)
65
66 /* ------------------------------------------------------------------------
67 * Driver specific constants
68 */
69
70 #define UVC_MAX_REQUEST_SIZE 64
71 #define UVC_MAX_EVENTS 4
72
73 /* ------------------------------------------------------------------------
74 * Structures
75 */
76 struct uvc_request {
77 struct usb_request *req;
78 u8 *req_buffer;
79 struct uvc_video *video;
80 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
81 struct completion req_done;
82 #endif
83 };
84
85 struct uvc_video {
86 struct uvc_device *uvc;
87 struct usb_ep *ep;
88
89 struct work_struct pump;
90
91 /* Frame parameters */
92 u8 bpp;
93 u32 fcc;
94 unsigned int width;
95 unsigned int height;
96 unsigned int imagesize;
97 struct mutex mutex; /* protects frame parameters */
98
99 unsigned int uvc_num_requests;
100
101 /* Requests */
102 unsigned int req_size;
103 struct uvc_request *ureq;
104 struct list_head req_free;
105 spinlock_t req_lock;
106
107 void (*encode) (struct usb_request *req, struct uvc_video *video,
108 struct uvc_buffer *buf);
109
110 /* Context data used by the completion handler */
111 __u32 payload_size;
112 __u32 max_payload_size;
113
114 struct uvc_video_queue queue;
115 unsigned int fid;
116 };
117
118 enum uvc_state {
119 UVC_STATE_DISCONNECTED,
120 UVC_STATE_CONNECTED,
121 UVC_STATE_STREAMING,
122 };
123
124 struct uvc_device {
125 struct video_device vdev;
126 struct v4l2_device v4l2_dev;
127 enum uvc_state state;
128 struct usb_function func;
129 struct uvc_video video;
130 bool func_connected;
131 wait_queue_head_t func_connected_queue;
132 /* for creating and issuing QoS requests */
133 struct pm_qos_request pm_qos;
134
135 /* Descriptors */
136 struct {
137 const struct uvc_descriptor_header * const *fs_control;
138 const struct uvc_descriptor_header * const *ss_control;
139 const struct uvc_descriptor_header * const *fs_streaming;
140 const struct uvc_descriptor_header * const *hs_streaming;
141 const struct uvc_descriptor_header * const *ss_streaming;
142 } desc;
143
144 unsigned int control_intf;
145 struct usb_ep *control_ep;
146 struct usb_request *control_req;
147 void *control_buf;
148
149 unsigned int streaming_intf;
150
151 /* Events */
152 unsigned int event_length;
153 unsigned int event_setup_out : 1;
154 unsigned int event_suspend : 1;
155 };
156
to_uvc(struct usb_function * f)157 static inline struct uvc_device *to_uvc(struct usb_function *f)
158 {
159 return container_of(f, struct uvc_device, func);
160 }
161
162 struct uvc_file_handle {
163 struct v4l2_fh vfh;
164 struct uvc_video *device;
165 bool is_uvc_app_handle;
166 };
167
168 #define to_uvc_file_handle(handle) \
169 container_of(handle, struct uvc_file_handle, vfh)
170
171 /* ------------------------------------------------------------------------
172 * Functions
173 */
174
175 extern void uvc_function_setup_continue(struct uvc_device *uvc);
176 extern void uvc_endpoint_stream(struct uvc_device *dev);
177
178 extern void uvc_function_connect(struct uvc_device *uvc);
179 extern void uvc_function_disconnect(struct uvc_device *uvc);
180
181 #endif /* _UVC_GADGET_H_ */
182