xref: /openbmc/linux/drivers/media/usb/gspca/gspca.h (revision 2359ccdd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GSPCAV2_H
3 #define GSPCAV2_H
4 
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/usb.h>
8 #include <linux/videodev2.h>
9 #include <media/v4l2-common.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 #include <linux/mutex.h>
13 
14 
15 
16 /* GSPCA debug codes */
17 
18 #define D_PROBE  1
19 #define D_CONF   2
20 #define D_STREAM 3
21 #define D_FRAM   4
22 #define D_PACK   5
23 #define D_USBI   6
24 #define D_USBO   7
25 
26 extern int gspca_debug;
27 
28 
29 #define gspca_dbg(gspca_dev, level, fmt, ...)			\
30 	v4l2_dbg(level, gspca_debug, &(gspca_dev)->v4l2_dev,	\
31 		 fmt, ##__VA_ARGS__)
32 
33 #define gspca_err(gspca_dev, fmt, ...)				\
34 	v4l2_err(&(gspca_dev)->v4l2_dev, fmt, ##__VA_ARGS__)
35 
36 #define GSPCA_MAX_FRAMES 16	/* maximum number of video frame buffers */
37 /* image transfers */
38 #define MAX_NURBS 4		/* max number of URBs */
39 
40 
41 /* used to list framerates supported by a camera mode (resolution) */
42 struct framerates {
43 	const u8 *rates;
44 	int nrates;
45 };
46 
47 /* device information - set at probe time */
48 struct cam {
49 	const struct v4l2_pix_format *cam_mode;	/* size nmodes */
50 	const struct framerates *mode_framerates; /* must have size nmodes,
51 						   * just like cam_mode */
52 	u32 bulk_size;		/* buffer size when image transfer by bulk */
53 	u32 input_flags;	/* value for ENUM_INPUT status flags */
54 	u8 nmodes;		/* size of cam_mode */
55 	u8 no_urb_create;	/* don't create transfer URBs */
56 	u8 bulk_nurbs;		/* number of URBs in bulk mode
57 				 * - cannot be > MAX_NURBS
58 				 * - when 0 and bulk_size != 0 means
59 				 *   1 URB and submit done by subdriver */
60 	u8 bulk;		/* image transfer by 0:isoc / 1:bulk */
61 	u8 npkt;		/* number of packets in an ISOC message
62 				 * 0 is the default value: 32 packets */
63 	u8 needs_full_bandwidth;/* Set this flag to notify the bandwidth calc.
64 				 * code that the cam fills all image buffers to
65 				 * the max, even when using compression. */
66 };
67 
68 struct gspca_dev;
69 struct gspca_frame;
70 
71 /* subdriver operations */
72 typedef int (*cam_op) (struct gspca_dev *);
73 typedef void (*cam_v_op) (struct gspca_dev *);
74 typedef int (*cam_cf_op) (struct gspca_dev *, const struct usb_device_id *);
75 typedef int (*cam_get_jpg_op) (struct gspca_dev *,
76 				struct v4l2_jpegcompression *);
77 typedef int (*cam_set_jpg_op) (struct gspca_dev *,
78 				const struct v4l2_jpegcompression *);
79 typedef int (*cam_get_reg_op) (struct gspca_dev *,
80 				struct v4l2_dbg_register *);
81 typedef int (*cam_set_reg_op) (struct gspca_dev *,
82 				const struct v4l2_dbg_register *);
83 typedef int (*cam_chip_info_op) (struct gspca_dev *,
84 				struct v4l2_dbg_chip_info *);
85 typedef void (*cam_streamparm_op) (struct gspca_dev *,
86 				  struct v4l2_streamparm *);
87 typedef void (*cam_pkt_op) (struct gspca_dev *gspca_dev,
88 				u8 *data,
89 				int len);
90 typedef int (*cam_int_pkt_op) (struct gspca_dev *gspca_dev,
91 				u8 *data,
92 				int len);
93 typedef void (*cam_format_op) (struct gspca_dev *gspca_dev,
94 				struct v4l2_format *fmt);
95 typedef int (*cam_frmsize_op) (struct gspca_dev *gspca_dev,
96 				struct v4l2_frmsizeenum *fsize);
97 
98 /* subdriver description */
99 struct sd_desc {
100 /* information */
101 	const char *name;	/* sub-driver name */
102 /* mandatory operations */
103 	cam_cf_op config;	/* called on probe */
104 	cam_op init;		/* called on probe and resume */
105 	cam_op init_controls;	/* called on probe */
106 	cam_op start;		/* called on stream on after URBs creation */
107 	cam_pkt_op pkt_scan;
108 /* optional operations */
109 	cam_op isoc_init;	/* called on stream on before getting the EP */
110 	cam_op isoc_nego;	/* called when URB submit failed with NOSPC */
111 	cam_v_op stopN;		/* called on stream off - main alt */
112 	cam_v_op stop0;		/* called on stream off & disconnect - alt 0 */
113 	cam_v_op dq_callback;	/* called when a frame has been dequeued */
114 	cam_get_jpg_op get_jcomp;
115 	cam_set_jpg_op set_jcomp;
116 	cam_streamparm_op get_streamparm;
117 	cam_streamparm_op set_streamparm;
118 	cam_format_op try_fmt;
119 	cam_frmsize_op enum_framesizes;
120 #ifdef CONFIG_VIDEO_ADV_DEBUG
121 	cam_set_reg_op set_register;
122 	cam_get_reg_op get_register;
123 	cam_chip_info_op get_chip_info;
124 #endif
125 #if IS_ENABLED(CONFIG_INPUT)
126 	cam_int_pkt_op int_pkt_scan;
127 	/* other_input makes the gspca core create gspca_dev->input even when
128 	   int_pkt_scan is NULL, for cams with non interrupt driven buttons */
129 	u8 other_input;
130 #endif
131 };
132 
133 /* packet types when moving from iso buf to frame buf */
134 enum gspca_packet_type {
135 	DISCARD_PACKET,
136 	FIRST_PACKET,
137 	INTER_PACKET,
138 	LAST_PACKET
139 };
140 
141 struct gspca_frame {
142 	__u8 *data;			/* frame buffer */
143 	int vma_use_count;
144 	struct v4l2_buffer v4l2_buf;
145 };
146 
147 struct gspca_dev {
148 	struct video_device vdev;	/* !! must be the first item */
149 	struct module *module;		/* subdriver handling the device */
150 	struct v4l2_device v4l2_dev;
151 	struct usb_device *dev;
152 	struct file *capt_file;		/* file doing video capture */
153 					/* protected by queue_lock */
154 #if IS_ENABLED(CONFIG_INPUT)
155 	struct input_dev *input_dev;
156 	char phys[64];			/* physical device path */
157 #endif
158 
159 	struct cam cam;				/* device information */
160 	const struct sd_desc *sd_desc;		/* subdriver description */
161 	struct v4l2_ctrl_handler ctrl_handler;
162 
163 	/* autogain and exposure or gain control cluster, these are global as
164 	   the autogain/exposure functions in autogain_functions.c use them */
165 	struct {
166 		struct v4l2_ctrl *autogain;
167 		struct v4l2_ctrl *exposure;
168 		struct v4l2_ctrl *gain;
169 		int exp_too_low_cnt, exp_too_high_cnt;
170 	};
171 
172 #define USB_BUF_SZ 64
173 	__u8 *usb_buf;				/* buffer for USB exchanges */
174 	struct urb *urb[MAX_NURBS];
175 #if IS_ENABLED(CONFIG_INPUT)
176 	struct urb *int_urb;
177 #endif
178 
179 	__u8 *frbuf;				/* buffer for nframes */
180 	struct gspca_frame frame[GSPCA_MAX_FRAMES];
181 	u8 *image;				/* image beeing filled */
182 	__u32 frsz;				/* frame size */
183 	u32 image_len;				/* current length of image */
184 	atomic_t fr_q;				/* next frame to queue */
185 	atomic_t fr_i;				/* frame being filled */
186 	signed char fr_queue[GSPCA_MAX_FRAMES];	/* frame queue */
187 	char nframes;				/* number of frames */
188 	u8 fr_o;				/* next frame to dequeue */
189 	__u8 last_packet_type;
190 	__s8 empty_packet;		/* if (-1) don't check empty packets */
191 	__u8 streaming;			/* protected by both mutexes (*) */
192 
193 	__u8 curr_mode;			/* current camera mode */
194 	struct v4l2_pix_format pixfmt;	/* current mode parameters */
195 	__u32 sequence;			/* frame sequence number */
196 
197 	wait_queue_head_t wq;		/* wait queue */
198 	struct mutex usb_lock;		/* usb exchange protection */
199 	struct mutex queue_lock;	/* ISOC queue protection */
200 	int usb_err;			/* USB error - protected by usb_lock */
201 	u16 pkt_size;			/* ISOC packet size */
202 #ifdef CONFIG_PM
203 	char frozen;			/* suspend - resume */
204 #endif
205 	char present;			/* device connected */
206 	char nbufread;			/* number of buffers for read() */
207 	char memory;			/* memory type (V4L2_MEMORY_xxx) */
208 	__u8 iface;			/* USB interface number */
209 	__u8 alt;			/* USB alternate setting */
210 	int xfer_ep;			/* USB transfer endpoint address */
211 	u8 audio;			/* presence of audio device */
212 
213 	/* (*) These variables are proteced by both usb_lock and queue_lock,
214 	   that is any code setting them is holding *both*, which means that
215 	   any code getting them needs to hold at least one of them */
216 };
217 
218 int gspca_dev_probe(struct usb_interface *intf,
219 		const struct usb_device_id *id,
220 		const struct sd_desc *sd_desc,
221 		int dev_size,
222 		struct module *module);
223 int gspca_dev_probe2(struct usb_interface *intf,
224 		const struct usb_device_id *id,
225 		const struct sd_desc *sd_desc,
226 		int dev_size,
227 		struct module *module);
228 void gspca_disconnect(struct usb_interface *intf);
229 void gspca_frame_add(struct gspca_dev *gspca_dev,
230 			enum gspca_packet_type packet_type,
231 			const u8 *data,
232 			int len);
233 #ifdef CONFIG_PM
234 int gspca_suspend(struct usb_interface *intf, pm_message_t message);
235 int gspca_resume(struct usb_interface *intf);
236 #endif
237 int gspca_expo_autogain(struct gspca_dev *gspca_dev, int avg_lum,
238 	int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee);
239 int gspca_coarse_grained_expo_autogain(struct gspca_dev *gspca_dev,
240 	int avg_lum, int desired_avg_lum, int deadzone);
241 
242 #endif /* GSPCAV2_H */
243