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