blob: b0ced2e1400644f872d56d86ec1570a2ef9d9888 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03002#ifndef GSPCAV2_H
3#define GSPCAV2_H
4
5#include <linux/module.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -03006#include <linux/kernel.h>
7#include <linux/usb.h>
8#include <linux/videodev2.h>
9#include <media/v4l2-common.h>
Hans de Goede8cd05842012-05-09 09:58:33 -030010#include <media/v4l2-ctrls.h>
Hans Verkuil23335652012-05-06 09:28:19 -030011#include <media/v4l2-device.h>
Hans Verkuil1f5965c2018-05-12 10:44:00 -040012#include <media/videobuf2-v4l2.h>
13#include <media/videobuf2-vmalloc.h>
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030014#include <linux/mutex.h>
15
Jean-Francois Moine335b3f82008-07-30 04:53:02 -030016
Theodore Kilgorec93396e2013-02-04 13:17:55 -030017
18/* GSPCA debug codes */
19
20#define D_PROBE 1
21#define D_CONF 2
22#define D_STREAM 3
23#define D_FRAM 4
24#define D_PACK 5
25#define D_USBI 6
26#define D_USBO 7
27
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030028extern int gspca_debug;
Joe Perches133a9fe2011-08-21 19:56:57 -030029
Theodore Kilgorec93396e2013-02-04 13:17:55 -030030
Joe Perches37d5efb2017-09-22 15:20:33 -040031#define gspca_dbg(gspca_dev, level, fmt, ...) \
32 v4l2_dbg(level, gspca_debug, &(gspca_dev)->v4l2_dev, \
33 fmt, ##__VA_ARGS__)
Theodore Kilgorec93396e2013-02-04 13:17:55 -030034
Joe Perches52173c5f2017-09-22 14:33:35 -040035#define gspca_err(gspca_dev, fmt, ...) \
36 v4l2_err(&(gspca_dev)->v4l2_dev, fmt, ##__VA_ARGS__)
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030037
38#define GSPCA_MAX_FRAMES 16 /* maximum number of video frame buffers */
Jean-Francois Moine0be010042008-09-04 07:01:50 -030039/* image transfers */
40#define MAX_NURBS 4 /* max number of URBs */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030041
Antonio Ospite28ffe772009-12-02 06:18:46 -030042
43/* used to list framerates supported by a camera mode (resolution) */
44struct framerates {
Antonio Ospite29b87f02010-01-17 03:27:04 -030045 const u8 *rates;
Antonio Ospite28ffe772009-12-02 06:18:46 -030046 int nrates;
47};
48
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030049/* device information - set at probe time */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030050struct cam {
Jean-Francois Moinecc611b82008-12-29 07:49:41 -030051 const struct v4l2_pix_format *cam_mode; /* size nmodes */
Jean-François Moine2bbf53b2010-12-28 07:01:04 -030052 const struct framerates *mode_framerates; /* must have size nmodes,
Antonio Ospite28ffe772009-12-02 06:18:46 -030053 * just like cam_mode */
Jean-Francois Moine47aaca92009-12-15 05:23:04 -030054 u32 bulk_size; /* buffer size when image transfer by bulk */
55 u32 input_flags; /* value for ENUM_INPUT status flags */
56 u8 nmodes; /* size of cam_mode */
57 u8 no_urb_create; /* don't create transfer URBs */
58 u8 bulk_nurbs; /* number of URBs in bulk mode
Antonio Ospitedff369a2008-11-14 07:53:32 -030059 * - cannot be > MAX_NURBS
60 * - when 0 and bulk_size != 0 means
61 * 1 URB and submit done by subdriver */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -030062 u8 bulk; /* image transfer by 0:isoc / 1:bulk */
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -030063 u8 npkt; /* number of packets in an ISOC message
64 * 0 is the default value: 32 packets */
Hans de Goedeeb3fb7c2012-01-01 16:35:01 -030065 u8 needs_full_bandwidth;/* Set this flag to notify the bandwidth calc.
66 * code that the cam fills all image buffers to
67 * the max, even when using compression. */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030068};
69
70struct gspca_dev;
71struct gspca_frame;
72
73/* subdriver operations */
74typedef int (*cam_op) (struct gspca_dev *);
75typedef void (*cam_v_op) (struct gspca_dev *);
76typedef int (*cam_cf_op) (struct gspca_dev *, const struct usb_device_id *);
Hans Verkuild88aab52012-09-17 05:05:25 -030077typedef int (*cam_get_jpg_op) (struct gspca_dev *,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030078 struct v4l2_jpegcompression *);
Hans Verkuild88aab52012-09-17 05:05:25 -030079typedef int (*cam_set_jpg_op) (struct gspca_dev *,
80 const struct v4l2_jpegcompression *);
Hans Verkuil977ba3b12013-03-24 08:28:46 -030081typedef int (*cam_get_reg_op) (struct gspca_dev *,
Brian Johnsonaf1d9af2009-07-19 05:29:20 -030082 struct v4l2_dbg_register *);
Hans Verkuil977ba3b12013-03-24 08:28:46 -030083typedef int (*cam_set_reg_op) (struct gspca_dev *,
84 const struct v4l2_dbg_register *);
Hans Verkuilb1c85cc2013-05-29 06:59:42 -030085typedef int (*cam_chip_info_op) (struct gspca_dev *,
86 struct v4l2_dbg_chip_info *);
Jean-François Moine668f44a2010-12-25 13:46:14 -030087typedef void (*cam_streamparm_op) (struct gspca_dev *,
Jim Paris627a5ef2008-12-10 06:02:42 -030088 struct v4l2_streamparm *);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030089typedef void (*cam_pkt_op) (struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -030090 u8 *data,
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030091 int len);
Márton Németh0274d422010-01-28 06:39:49 -030092typedef int (*cam_int_pkt_op) (struct gspca_dev *gspca_dev,
93 u8 *data,
94 int len);
Ondrej Zary7d687af2013-08-30 17:54:24 -030095typedef void (*cam_format_op) (struct gspca_dev *gspca_dev,
96 struct v4l2_format *fmt);
97typedef int (*cam_frmsize_op) (struct gspca_dev *gspca_dev,
98 struct v4l2_frmsizeenum *fsize);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -030099
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300100/* subdriver description */
101struct sd_desc {
102/* information */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300103 const char *name; /* sub-driver name */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300104/* mandatory operations */
Hans de Goedee2997a72008-04-23 08:09:12 -0300105 cam_cf_op config; /* called on probe */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300106 cam_op init; /* called on probe and resume */
Hans Verkuil62bba5d2012-05-06 09:28:17 -0300107 cam_op init_controls; /* called on probe */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300108 cam_op start; /* called on stream on after URBs creation */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300109 cam_pkt_op pkt_scan;
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300110/* optional operations */
Jean-Francois Moine1f53b0b2009-06-30 07:07:01 -0300111 cam_op isoc_init; /* called on stream on before getting the EP */
112 cam_op isoc_nego; /* called when URB submit failed with NOSPC */
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300113 cam_v_op stopN; /* called on stream off - main alt */
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300114 cam_v_op stop0; /* called on stream off & disconnect - alt 0 */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300115 cam_v_op dq_callback; /* called when a frame has been dequeued */
Hans Verkuild88aab52012-09-17 05:05:25 -0300116 cam_get_jpg_op get_jcomp;
117 cam_set_jpg_op set_jcomp;
Jim Paris627a5ef2008-12-10 06:02:42 -0300118 cam_streamparm_op get_streamparm;
119 cam_streamparm_op set_streamparm;
Ondrej Zary7d687af2013-08-30 17:54:24 -0300120 cam_format_op try_fmt;
121 cam_frmsize_op enum_framesizes;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300122#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil977ba3b12013-03-24 08:28:46 -0300123 cam_set_reg_op set_register;
124 cam_get_reg_op get_register;
Hans Verkuilb1c85cc2013-05-29 06:59:42 -0300125 cam_chip_info_op get_chip_info;
Brian Johnsonaf1d9af2009-07-19 05:29:20 -0300126#endif
Peter Senna Tschudin13a00fa2013-01-24 19:28:59 -0300127#if IS_ENABLED(CONFIG_INPUT)
Márton Németh0274d422010-01-28 06:39:49 -0300128 cam_int_pkt_op int_pkt_scan;
Hans de Goedeac82f592010-02-19 04:28:39 -0300129 /* other_input makes the gspca core create gspca_dev->input even when
130 int_pkt_scan is NULL, for cams with non interrupt driven buttons */
131 u8 other_input;
Márton Németh0274d422010-01-28 06:39:49 -0300132#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300133};
134
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300135/* packet types when moving from iso buf to frame buf */
Erik Andrene293e592008-10-03 08:46:50 -0300136enum gspca_packet_type {
137 DISCARD_PACKET,
138 FIRST_PACKET,
139 INTER_PACKET,
140 LAST_PACKET
141};
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300142
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400143struct gspca_buffer {
144 struct vb2_v4l2_buffer vb;
145 struct list_head list;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300146};
147
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400148static inline struct gspca_buffer *to_gspca_buffer(struct vb2_buffer *vb2)
149{
150 return container_of(vb2, struct gspca_buffer, vb.vb2_buf);
151}
152
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300153struct gspca_dev {
Jean-Francois Moinea12ca6a2008-11-19 06:37:53 -0300154 struct video_device vdev; /* !! must be the first item */
Jean-Francois Moine5c4fa0022008-11-18 15:52:31 -0300155 struct module *module; /* subdriver handling the device */
Hans Verkuil23335652012-05-06 09:28:19 -0300156 struct v4l2_device v4l2_dev;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300157 struct usb_device *dev;
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400158
Peter Senna Tschudin13a00fa2013-01-24 19:28:59 -0300159#if IS_ENABLED(CONFIG_INPUT)
Márton Németh0274d422010-01-28 06:39:49 -0300160 struct input_dev *input_dev;
161 char phys[64]; /* physical device path */
162#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300163
164 struct cam cam; /* device information */
165 const struct sd_desc *sd_desc; /* subdriver description */
Hans de Goedea8a47862012-05-09 11:19:00 -0300166 struct v4l2_ctrl_handler ctrl_handler;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300167
Hans de Goede8cd05842012-05-09 09:58:33 -0300168 /* autogain and exposure or gain control cluster, these are global as
169 the autogain/exposure functions in autogain_functions.c use them */
170 struct {
171 struct v4l2_ctrl *autogain;
172 struct v4l2_ctrl *exposure;
173 struct v4l2_ctrl *gain;
174 int exp_too_low_cnt, exp_too_high_cnt;
175 };
176
Jean-Francois Moine8295d992008-09-03 17:12:19 -0300177#define USB_BUF_SZ 64
178 __u8 *usb_buf; /* buffer for USB exchanges */
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300179 struct urb *urb[MAX_NURBS];
Peter Senna Tschudin13a00fa2013-01-24 19:28:59 -0300180#if IS_ENABLED(CONFIG_INPUT)
Márton Németh0274d422010-01-28 06:39:49 -0300181 struct urb *int_urb;
182#endif
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300183
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400184 u8 *image; /* image being filled */
Jean-François Moineb192ca92010-06-27 03:08:19 -0300185 u32 image_len; /* current length of image */
Jean-Francois Moineff374742008-10-23 07:29:51 -0300186 __u8 last_packet_type;
187 __s8 empty_packet; /* if (-1) don't check empty packets */
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400188 bool streaming;
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300189
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300190 __u8 curr_mode; /* current camera mode */
Ondrej Zary1966bc22013-08-30 17:54:23 -0300191 struct v4l2_pix_format pixfmt; /* current mode parameters */
Jean-Francois Moineff374742008-10-23 07:29:51 -0300192 __u32 sequence; /* frame sequence number */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300193
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400194 struct vb2_queue queue;
195
196 spinlock_t qlock;
197 struct list_head buf_list;
198
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300199 wait_queue_head_t wq; /* wait queue */
200 struct mutex usb_lock; /* usb exchange protection */
Jean-Francois Moine8c4ebae2009-12-02 07:14:33 -0300201 int usb_err; /* USB error - protected by usb_lock */
Jean-François Moine35680ba2010-07-14 06:30:18 -0300202 u16 pkt_size; /* ISOC packet size */
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300203#ifdef CONFIG_PM
204 char frozen; /* suspend - resume */
205#endif
Hans Verkuil1f5965c2018-05-12 10:44:00 -0400206 bool present;
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300207 char memory; /* memory type (V4L2_MEMORY_xxx) */
Jean-Francois Moineff374742008-10-23 07:29:51 -0300208 __u8 iface; /* USB interface number */
209 __u8 alt; /* USB alternate setting */
Antonio Ospite2fe15242014-06-25 06:27:56 -0300210 int xfer_ep; /* USB transfer endpoint address */
Jean-François Moine35680ba2010-07-14 06:30:18 -0300211 u8 audio; /* presence of audio device */
Hans Verkuila3d6e8c2012-05-06 09:28:27 -0300212
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 */
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300216};
217
218int gspca_dev_probe(struct usb_interface *intf,
219 const struct usb_device_id *id,
220 const struct sd_desc *sd_desc,
Jean-Francois Moined43fa322008-06-12 10:58:58 -0300221 int dev_size,
222 struct module *module);
Jean-François Moine44628642010-06-05 07:47:36 -0300223int 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);
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300228void gspca_disconnect(struct usb_interface *intf);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300229void gspca_frame_add(struct gspca_dev *gspca_dev,
230 enum gspca_packet_type packet_type,
231 const u8 *data,
232 int len);
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300233#ifdef CONFIG_PM
234int gspca_suspend(struct usb_interface *intf, pm_message_t message);
235int gspca_resume(struct usb_interface *intf);
236#endif
Hans de Goede8cd05842012-05-09 09:58:33 -0300237int gspca_expo_autogain(struct gspca_dev *gspca_dev, int avg_lum,
238 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee);
239int gspca_coarse_grained_expo_autogain(struct gspca_dev *gspca_dev,
Antonio Ospite751e78d2014-06-04 09:03:40 -0300240 int avg_lum, int desired_avg_lum, int deadzone);
Hans de Goede8cd05842012-05-09 09:58:33 -0300241
Jean-Francois Moine63eb9542008-04-12 09:58:09 -0300242#endif /* GSPCAV2_H */