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