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