xref: /openbmc/linux/drivers/usb/gadget/function/uvc.h (revision da2ef666)
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/spinlock.h>
15 #include <linux/usb/composite.h>
16 #include <linux/videodev2.h>
17 
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-fh.h>
21 
22 #include "uvc_queue.h"
23 
24 struct usb_ep;
25 struct usb_request;
26 struct uvc_descriptor_header;
27 
28 /* ------------------------------------------------------------------------
29  * Debugging, printing and logging
30  */
31 
32 #define UVC_TRACE_PROBE				(1 << 0)
33 #define UVC_TRACE_DESCR				(1 << 1)
34 #define UVC_TRACE_CONTROL			(1 << 2)
35 #define UVC_TRACE_FORMAT			(1 << 3)
36 #define UVC_TRACE_CAPTURE			(1 << 4)
37 #define UVC_TRACE_CALLS				(1 << 5)
38 #define UVC_TRACE_IOCTL				(1 << 6)
39 #define UVC_TRACE_FRAME				(1 << 7)
40 #define UVC_TRACE_SUSPEND			(1 << 8)
41 #define UVC_TRACE_STATUS			(1 << 9)
42 
43 #define UVC_WARN_MINMAX				0
44 #define UVC_WARN_PROBE_DEF			1
45 
46 extern unsigned int uvc_gadget_trace_param;
47 
48 #define uvc_trace(flag, msg...) \
49 	do { \
50 		if (uvc_gadget_trace_param & flag) \
51 			printk(KERN_DEBUG "uvcvideo: " msg); \
52 	} while (0)
53 
54 #define uvc_warn_once(dev, warn, msg...) \
55 	do { \
56 		if (!test_and_set_bit(warn, &dev->warnings)) \
57 			printk(KERN_INFO "uvcvideo: " msg); \
58 	} while (0)
59 
60 #define uvc_printk(level, msg...) \
61 	printk(level "uvcvideo: " msg)
62 
63 /* ------------------------------------------------------------------------
64  * Driver specific constants
65  */
66 
67 #define UVC_NUM_REQUESTS			4
68 #define UVC_MAX_REQUEST_SIZE			64
69 #define UVC_MAX_EVENTS				4
70 
71 /* ------------------------------------------------------------------------
72  * Structures
73  */
74 
75 struct uvc_video {
76 	struct usb_ep *ep;
77 
78 	/* Frame parameters */
79 	u8 bpp;
80 	u32 fcc;
81 	unsigned int width;
82 	unsigned int height;
83 	unsigned int imagesize;
84 	struct mutex mutex;	/* protects frame parameters */
85 
86 	/* Requests */
87 	unsigned int req_size;
88 	struct usb_request *req[UVC_NUM_REQUESTS];
89 	__u8 *req_buffer[UVC_NUM_REQUESTS];
90 	struct list_head req_free;
91 	spinlock_t req_lock;
92 
93 	void (*encode) (struct usb_request *req, struct uvc_video *video,
94 			struct uvc_buffer *buf);
95 
96 	/* Context data used by the completion handler */
97 	__u32 payload_size;
98 	__u32 max_payload_size;
99 
100 	struct uvc_video_queue queue;
101 	unsigned int fid;
102 };
103 
104 enum uvc_state {
105 	UVC_STATE_DISCONNECTED,
106 	UVC_STATE_CONNECTED,
107 	UVC_STATE_STREAMING,
108 };
109 
110 struct uvc_device {
111 	struct video_device vdev;
112 	struct v4l2_device v4l2_dev;
113 	enum uvc_state state;
114 	struct usb_function func;
115 	struct uvc_video video;
116 
117 	/* Descriptors */
118 	struct {
119 		const struct uvc_descriptor_header * const *fs_control;
120 		const struct uvc_descriptor_header * const *ss_control;
121 		const struct uvc_descriptor_header * const *fs_streaming;
122 		const struct uvc_descriptor_header * const *hs_streaming;
123 		const struct uvc_descriptor_header * const *ss_streaming;
124 	} desc;
125 
126 	unsigned int control_intf;
127 	struct usb_ep *control_ep;
128 	struct usb_request *control_req;
129 	void *control_buf;
130 
131 	unsigned int streaming_intf;
132 
133 	/* Events */
134 	unsigned int event_length;
135 	unsigned int event_setup_out : 1;
136 };
137 
138 static inline struct uvc_device *to_uvc(struct usb_function *f)
139 {
140 	return container_of(f, struct uvc_device, func);
141 }
142 
143 struct uvc_file_handle {
144 	struct v4l2_fh vfh;
145 	struct uvc_video *device;
146 };
147 
148 #define to_uvc_file_handle(handle) \
149 	container_of(handle, struct uvc_file_handle, vfh)
150 
151 /* ------------------------------------------------------------------------
152  * Functions
153  */
154 
155 extern void uvc_function_setup_continue(struct uvc_device *uvc);
156 extern void uvc_endpoint_stream(struct uvc_device *dev);
157 
158 extern void uvc_function_connect(struct uvc_device *uvc);
159 extern void uvc_function_disconnect(struct uvc_device *uvc);
160 
161 #endif /* _UVC_GADGET_H_ */
162