]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
38f993ad DA |
2 | /* |
3 | * s2255drv.c - a driver for the Sensoray 2255 USB video capture device | |
4 | * | |
d86c6a8c | 5 | * Copyright (C) 2007-2014 by Sensoray Company Inc. |
38f993ad DA |
6 | * Dean Anderson |
7 | * | |
8 | * Some video buffer code based on vivi driver: | |
9 | * | |
10 | * Sensoray 2255 device supports 4 simultaneous channels. | |
11 | * The channels are not "crossbar" inputs, they are physically | |
12 | * attached to separate video decoders. | |
13 | * | |
14 | * Because of USB2.0 bandwidth limitations. There is only a | |
15 | * certain amount of data which may be transferred at one time. | |
16 | * | |
17 | * Example maximum bandwidth utilization: | |
18 | * | |
19 | * -full size, color mode YUYV or YUV422P: 2 channels at once | |
38f993ad | 20 | * -full or half size Grey scale: all 4 channels at once |
38f993ad | 21 | * -half size, color mode YUYV or YUV422P: all 4 channels at once |
38f993ad DA |
22 | * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels |
23 | * at once. | |
38f993ad DA |
24 | */ |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/firmware.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/mutex.h> | |
5a0e3ad6 | 30 | #include <linux/slab.h> |
38f993ad | 31 | #include <linux/videodev2.h> |
feb75f07 | 32 | #include <linux/mm.h> |
44d06d82 HV |
33 | #include <linux/vmalloc.h> |
34 | #include <linux/usb.h> | |
2d700715 | 35 | #include <media/videobuf2-v4l2.h> |
340a30c5 | 36 | #include <media/videobuf2-vmalloc.h> |
38f993ad | 37 | #include <media/v4l2-common.h> |
3a67b5cc | 38 | #include <media/v4l2-device.h> |
35ea11ff | 39 | #include <media/v4l2-ioctl.h> |
192f1e78 | 40 | #include <media/v4l2-ctrls.h> |
44d06d82 | 41 | #include <media/v4l2-event.h> |
38f993ad | 42 | |
340a30c5 | 43 | #define S2255_VERSION "1.25.1" |
38f993ad DA |
44 | #define FIRMWARE_FILE_NAME "f2255usb.bin" |
45 | ||
22b88d48 DA |
46 | /* default JPEG quality */ |
47 | #define S2255_DEF_JPEG_QUAL 50 | |
38f993ad DA |
48 | /* vendor request in */ |
49 | #define S2255_VR_IN 0 | |
50 | /* vendor request out */ | |
51 | #define S2255_VR_OUT 1 | |
52 | /* firmware query */ | |
53 | #define S2255_VR_FW 0x30 | |
54 | /* USB endpoint number for configuring the device */ | |
55 | #define S2255_CONFIG_EP 2 | |
56 | /* maximum time for DSP to start responding after last FW word loaded(ms) */ | |
14d96260 | 57 | #define S2255_DSP_BOOTTIME 800 |
38f993ad | 58 | /* maximum time to wait for firmware to load (ms) */ |
3f8d6f73 | 59 | #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME) |
9da62eb0 | 60 | #define S2255_MIN_BUFS 2 |
14d96260 | 61 | #define S2255_SETMODE_TIMEOUT 500 |
4de39f5d | 62 | #define S2255_VIDSTATUS_TIMEOUT 350 |
3fa00605 DA |
63 | #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL) |
64 | #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL) | |
65 | #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01) | |
66 | #define S2255_RESPONSE_FW cpu_to_le32(0x10) | |
67 | #define S2255_RESPONSE_STATUS cpu_to_le32(0x20) | |
14d96260 | 68 | #define S2255_USB_XFER_SIZE (16 * 1024) |
38f993ad | 69 | #define MAX_CHANNELS 4 |
38f993ad DA |
70 | #define SYS_FRAMES 4 |
71 | /* maximum size is PAL full size plus room for the marker header(s) */ | |
14d96260 DA |
72 | #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096) |
73 | #define DEF_USB_BLOCK S2255_USB_XFER_SIZE | |
38f993ad DA |
74 | #define LINE_SZ_4CIFS_NTSC 640 |
75 | #define LINE_SZ_2CIFS_NTSC 640 | |
76 | #define LINE_SZ_1CIFS_NTSC 320 | |
77 | #define LINE_SZ_4CIFS_PAL 704 | |
78 | #define LINE_SZ_2CIFS_PAL 704 | |
79 | #define LINE_SZ_1CIFS_PAL 352 | |
80 | #define NUM_LINES_4CIFS_NTSC 240 | |
81 | #define NUM_LINES_2CIFS_NTSC 240 | |
82 | #define NUM_LINES_1CIFS_NTSC 240 | |
83 | #define NUM_LINES_4CIFS_PAL 288 | |
84 | #define NUM_LINES_2CIFS_PAL 288 | |
85 | #define NUM_LINES_1CIFS_PAL 288 | |
86 | #define LINE_SZ_DEF 640 | |
87 | #define NUM_LINES_DEF 240 | |
88 | ||
89 | ||
90 | /* predefined settings */ | |
91 | #define FORMAT_NTSC 1 | |
92 | #define FORMAT_PAL 2 | |
93 | ||
94 | #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */ | |
95 | #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */ | |
96 | #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */ | |
7d853532 DA |
97 | /* SCALE_4CIFSI is the 2 fields interpolated into one */ |
98 | #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */ | |
38f993ad DA |
99 | |
100 | #define COLOR_YUVPL 1 /* YUV planar */ | |
101 | #define COLOR_YUVPK 2 /* YUV packed */ | |
102 | #define COLOR_Y8 4 /* monochrome */ | |
14d96260 | 103 | #define COLOR_JPG 5 /* JPEG */ |
38f993ad | 104 | |
5a34d9df DA |
105 | #define MASK_COLOR 0x000000ff |
106 | #define MASK_JPG_QUALITY 0x0000ff00 | |
107 | #define MASK_INPUT_TYPE 0x000f0000 | |
8a8cc952 | 108 | /* frame decimation. */ |
38f993ad DA |
109 | #define FDEC_1 1 /* capture every frame. default */ |
110 | #define FDEC_2 2 /* capture every 2nd frame */ | |
111 | #define FDEC_3 3 /* capture every 3rd frame */ | |
112 | #define FDEC_5 5 /* capture every 5th frame */ | |
113 | ||
114 | /*------------------------------------------------------- | |
115 | * Default mode parameters. | |
116 | *-------------------------------------------------------*/ | |
117 | #define DEF_SCALE SCALE_4CIFS | |
118 | #define DEF_COLOR COLOR_YUVPL | |
119 | #define DEF_FDEC FDEC_1 | |
120 | #define DEF_BRIGHT 0 | |
121 | #define DEF_CONTRAST 0x5c | |
122 | #define DEF_SATURATION 0x80 | |
123 | #define DEF_HUE 0 | |
124 | ||
125 | /* usb config commands */ | |
3fa00605 | 126 | #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de) |
3b2a6306 | 127 | #define CMD_2255 0xc2255000 |
3fa00605 DA |
128 | #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10)) |
129 | #define CMD_START cpu_to_le32((CMD_2255 | 0x20)) | |
130 | #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30)) | |
131 | #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40)) | |
38f993ad DA |
132 | |
133 | struct s2255_mode { | |
134 | u32 format; /* input video format (NTSC, PAL) */ | |
135 | u32 scale; /* output video scale */ | |
136 | u32 color; /* output video color format */ | |
137 | u32 fdec; /* frame decimation */ | |
138 | u32 bright; /* brightness */ | |
139 | u32 contrast; /* contrast */ | |
140 | u32 saturation; /* saturation */ | |
141 | u32 hue; /* hue (NTSC only)*/ | |
142 | u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/ | |
143 | u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */ | |
144 | u32 restart; /* if DSP requires restart */ | |
145 | }; | |
146 | ||
38f993ad | 147 | |
14d96260 DA |
148 | #define S2255_READ_IDLE 0 |
149 | #define S2255_READ_FRAME 1 | |
38f993ad | 150 | |
14d96260 | 151 | /* frame structure */ |
38f993ad DA |
152 | struct s2255_framei { |
153 | unsigned long size; | |
14d96260 | 154 | unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/ |
38f993ad DA |
155 | void *lpvbits; /* image data */ |
156 | unsigned long cur_size; /* current data copied to it */ | |
157 | }; | |
158 | ||
159 | /* image buffer structure */ | |
160 | struct s2255_bufferi { | |
161 | unsigned long dwFrames; /* number of frames in buffer */ | |
162 | struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */ | |
163 | }; | |
164 | ||
165 | #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \ | |
166 | DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \ | |
3f8d6f73 | 167 | DEF_HUE, 0, DEF_USB_BLOCK, 0} |
38f993ad | 168 | |
38f993ad DA |
169 | /* for firmware loading, fw_state */ |
170 | #define S2255_FW_NOTLOADED 0 | |
171 | #define S2255_FW_LOADED_DSPWAIT 1 | |
172 | #define S2255_FW_SUCCESS 2 | |
173 | #define S2255_FW_FAILED 3 | |
f78d92c9 | 174 | #define S2255_FW_DISCONNECTING 4 |
deaf53e3 | 175 | #define S2255_FW_MARKER cpu_to_le32(0x22552f2f) |
14d96260 DA |
176 | /* 2255 read states */ |
177 | #define S2255_READ_IDLE 0 | |
178 | #define S2255_READ_FRAME 1 | |
38f993ad DA |
179 | struct s2255_fw { |
180 | int fw_loaded; | |
181 | int fw_size; | |
182 | struct urb *fw_urb; | |
183 | atomic_t fw_state; | |
184 | void *pfw_data; | |
185 | wait_queue_head_t wait_fw; | |
38f993ad DA |
186 | const struct firmware *fw; |
187 | }; | |
188 | ||
189 | struct s2255_pipeinfo { | |
190 | u32 max_transfer_size; | |
191 | u32 cur_transfer_size; | |
192 | u8 *transfer_buffer; | |
38f993ad | 193 | u32 state; |
38f993ad DA |
194 | void *stream_urb; |
195 | void *dev; /* back pointer to s2255_dev struct*/ | |
196 | u32 err_count; | |
38f993ad | 197 | u32 idx; |
38f993ad DA |
198 | }; |
199 | ||
200 | struct s2255_fmt; /*forward declaration */ | |
fe85ce90 DA |
201 | struct s2255_dev; |
202 | ||
5e950faf DA |
203 | /* 2255 video channel */ |
204 | struct s2255_vc { | |
f5402007 | 205 | struct s2255_dev *dev; |
fe85ce90 | 206 | struct video_device vdev; |
192f1e78 | 207 | struct v4l2_ctrl_handler hdl; |
7041dec7 | 208 | struct v4l2_ctrl *jpegqual_ctrl; |
fe85ce90 | 209 | int resources; |
d86c6a8c | 210 | struct list_head buf_list; |
fe85ce90 DA |
211 | struct s2255_bufferi buffer; |
212 | struct s2255_mode mode; | |
469af77a | 213 | v4l2_std_id std; |
fe85ce90 | 214 | /* jpeg compression */ |
7041dec7 | 215 | unsigned jpegqual; |
fe85ce90 DA |
216 | /* capture parameters (for high quality mode full size) */ |
217 | struct v4l2_captureparm cap_parm; | |
218 | int cur_frame; | |
219 | int last_frame; | |
fe85ce90 DA |
220 | /* allocated image size */ |
221 | unsigned long req_image_size; | |
222 | /* received packet size */ | |
223 | unsigned long pkt_size; | |
224 | int bad_payload; | |
225 | unsigned long frame_count; | |
226 | /* if JPEG image */ | |
227 | int jpg_size; | |
228 | /* if channel configured to default state */ | |
229 | int configured; | |
230 | wait_queue_head_t wait_setmode; | |
231 | int setmode_ready; | |
232 | /* video status items */ | |
233 | int vidstatus; | |
234 | wait_queue_head_t wait_vidstatus; | |
235 | int vidstatus_ready; | |
236 | unsigned int width; | |
237 | unsigned int height; | |
340a30c5 | 238 | enum v4l2_field field; |
fe85ce90 DA |
239 | const struct s2255_fmt *fmt; |
240 | int idx; /* channel number on device, 0-3 */ | |
340a30c5 | 241 | struct vb2_queue vb_vidq; |
242 | struct mutex vb_lock; /* streaming lock */ | |
243 | spinlock_t qlock; | |
fe85ce90 DA |
244 | }; |
245 | ||
38f993ad DA |
246 | |
247 | struct s2255_dev { | |
5e950faf | 248 | struct s2255_vc vc[MAX_CHANNELS]; |
f5402007 | 249 | struct v4l2_device v4l2_dev; |
fe85ce90 | 250 | atomic_t num_channels; |
38f993ad | 251 | int frames; |
a19a5cd7 | 252 | struct mutex lock; /* channels[].vdev.lock */ |
47d8c881 | 253 | struct mutex cmdlock; /* protects cmdbuf */ |
38f993ad DA |
254 | struct usb_device *udev; |
255 | struct usb_interface *interface; | |
256 | u8 read_endpoint; | |
38f993ad DA |
257 | struct timer_list timer; |
258 | struct s2255_fw *fw_data; | |
ab85c6a3 | 259 | struct s2255_pipeinfo pipe; |
38f993ad | 260 | u32 cc; /* current channel */ |
38f993ad | 261 | int frame_ready; |
14d96260 | 262 | int chn_ready; |
4de39f5d DA |
263 | /* dsp firmware version (f2255usb.bin) */ |
264 | int dsp_fw_ver; | |
5a34d9df | 265 | u16 pid; /* product id */ |
47d8c881 DA |
266 | #define S2255_CMDBUF_SIZE 512 |
267 | __le32 *cmdbuf; | |
38f993ad | 268 | }; |
65c6edb3 | 269 | |
65c6edb3 DA |
270 | static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev) |
271 | { | |
272 | return container_of(v4l2_dev, struct s2255_dev, v4l2_dev); | |
273 | } | |
38f993ad DA |
274 | |
275 | struct s2255_fmt { | |
276 | char *name; | |
277 | u32 fourcc; | |
278 | int depth; | |
279 | }; | |
280 | ||
281 | /* buffer for one video frame */ | |
282 | struct s2255_buffer { | |
283 | /* common v4l buffer stuff -- must be first */ | |
2d700715 | 284 | struct vb2_v4l2_buffer vb; |
340a30c5 | 285 | struct list_head list; |
38f993ad DA |
286 | }; |
287 | ||
38f993ad | 288 | |
abce21f4 | 289 | /* current cypress EEPROM firmware version */ |
8a8cc952 | 290 | #define S2255_CUR_USB_FWVER ((3 << 8) | 12) |
4de39f5d | 291 | /* current DSP FW version */ |
8a8cc952 | 292 | #define S2255_CUR_DSP_FWVER 10104 |
4de39f5d | 293 | /* Need DSP version 5+ for video status feature */ |
5a34d9df DA |
294 | #define S2255_MIN_DSP_STATUS 5 |
295 | #define S2255_MIN_DSP_COLORFILTER 8 | |
469af77a | 296 | #define S2255_NORMS (V4L2_STD_ALL) |
5a34d9df DA |
297 | |
298 | /* private V4L2 controls */ | |
299 | ||
300 | /* | |
301 | * The following chart displays how COLORFILTER should be set | |
302 | * ========================================================= | |
303 | * = fourcc = COLORFILTER = | |
304 | * = =============================== | |
305 | * = = 0 = 1 = | |
306 | * ========================================================= | |
307 | * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome= | |
308 | * = = s-video or = composite = | |
309 | * = = B/W camera = input = | |
310 | * ========================================================= | |
311 | * = other = color, svideo = color, = | |
312 | * = = = composite = | |
313 | * ========================================================= | |
314 | * | |
315 | * Notes: | |
316 | * channels 0-3 on 2255 are composite | |
317 | * channels 0-1 on 2257 are composite, 2-3 are s-video | |
318 | * If COLORFILTER is 0 with a composite color camera connected, | |
319 | * the output will appear monochrome but hatching | |
320 | * will occur. | |
321 | * COLORFILTER is different from "color killer" and "color effects" | |
322 | * for reasons above. | |
323 | */ | |
324 | #define S2255_V4L2_YC_ON 1 | |
325 | #define S2255_V4L2_YC_OFF 0 | |
192f1e78 | 326 | #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0) |
5a34d9df | 327 | |
38f993ad DA |
328 | /* frame prefix size (sent once every frame) */ |
329 | #define PREFIX_SIZE 512 | |
330 | ||
331 | /* Channels on box are in reverse order */ | |
3f8d6f73 | 332 | static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0}; |
38f993ad | 333 | |
38f993ad | 334 | static int debug; |
38f993ad DA |
335 | |
336 | static int s2255_start_readpipe(struct s2255_dev *dev); | |
337 | static void s2255_stop_readpipe(struct s2255_dev *dev); | |
5e950faf DA |
338 | static int s2255_start_acquire(struct s2255_vc *vc); |
339 | static int s2255_stop_acquire(struct s2255_vc *vc); | |
340 | static void s2255_fillbuff(struct s2255_vc *vc, struct s2255_buffer *buf, | |
fe85ce90 | 341 | int jpgsize); |
5e950faf | 342 | static int s2255_set_mode(struct s2255_vc *vc, struct s2255_mode *mode); |
38f993ad | 343 | static int s2255_board_shutdown(struct s2255_dev *dev); |
e2a06704 | 344 | static void s2255_fwload_start(struct s2255_dev *dev); |
d62e85a0 | 345 | static void s2255_destroy(struct s2255_dev *dev); |
14d96260 DA |
346 | static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req, |
347 | u16 index, u16 value, void *buf, | |
348 | s32 buf_len, int bOut); | |
38f993ad | 349 | |
be9ed511 MCC |
350 | /* dev_err macro with driver name */ |
351 | #define S2255_DRIVER_NAME "s2255" | |
352 | #define s2255_dev_err(dev, fmt, arg...) \ | |
353 | dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg) | |
354 | ||
f5402007 | 355 | #define dprintk(dev, level, fmt, arg...) \ |
356 | v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg) | |
38f993ad | 357 | |
38f993ad DA |
358 | static struct usb_driver s2255_driver; |
359 | ||
38f993ad DA |
360 | /* start video number */ |
361 | static int video_nr = -1; /* /dev/videoN, -1 for autodetect */ | |
362 | ||
e42e28f9 SLD |
363 | /* Enable jpeg capture. */ |
364 | static int jpeg_enable = 1; | |
365 | ||
3f8d6f73 | 366 | module_param(debug, int, 0644); |
38f993ad | 367 | MODULE_PARM_DESC(debug, "Debug level(0-100) default 0"); |
3f8d6f73 | 368 | module_param(video_nr, int, 0644); |
38f993ad | 369 | MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)"); |
e42e28f9 SLD |
370 | module_param(jpeg_enable, int, 0644); |
371 | MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1"); | |
38f993ad DA |
372 | |
373 | /* USB device table */ | |
5a34d9df | 374 | #define USB_SENSORAY_VID 0x1943 |
7fb2e072 | 375 | static const struct usb_device_id s2255_table[] = { |
5a34d9df DA |
376 | {USB_DEVICE(USB_SENSORAY_VID, 0x2255)}, |
377 | {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/ | |
38f993ad DA |
378 | { } /* Terminating entry */ |
379 | }; | |
380 | MODULE_DEVICE_TABLE(usb, s2255_table); | |
381 | ||
38f993ad DA |
382 | #define BUFFER_TIMEOUT msecs_to_jiffies(400) |
383 | ||
38f993ad | 384 | /* image formats. */ |
e42e28f9 | 385 | /* JPEG formats must be defined last to support jpeg_enable parameter */ |
38f993ad DA |
386 | static const struct s2255_fmt formats[] = { |
387 | { | |
38f993ad DA |
388 | .name = "4:2:2, packed, YUYV", |
389 | .fourcc = V4L2_PIX_FMT_YUYV, | |
390 | .depth = 16 | |
391 | ||
392 | }, { | |
393 | .name = "4:2:2, packed, UYVY", | |
394 | .fourcc = V4L2_PIX_FMT_UYVY, | |
395 | .depth = 16 | |
5c632b22 HV |
396 | }, { |
397 | .name = "4:2:2, planar, YUV422P", | |
398 | .fourcc = V4L2_PIX_FMT_YUV422P, | |
399 | .depth = 16 | |
400 | ||
e42e28f9 SLD |
401 | }, { |
402 | .name = "8bpp GREY", | |
403 | .fourcc = V4L2_PIX_FMT_GREY, | |
404 | .depth = 8 | |
14d96260 DA |
405 | }, { |
406 | .name = "JPG", | |
407 | .fourcc = V4L2_PIX_FMT_JPEG, | |
408 | .depth = 24 | |
d0ef8540 SLD |
409 | }, { |
410 | .name = "MJPG", | |
411 | .fourcc = V4L2_PIX_FMT_MJPEG, | |
412 | .depth = 24 | |
38f993ad DA |
413 | } |
414 | }; | |
415 | ||
5e950faf | 416 | static int norm_maxw(struct s2255_vc *vc) |
38f993ad | 417 | { |
5e950faf | 418 | return (vc->std & V4L2_STD_525_60) ? |
38f993ad DA |
419 | LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL; |
420 | } | |
421 | ||
5e950faf | 422 | static int norm_maxh(struct s2255_vc *vc) |
38f993ad | 423 | { |
5e950faf | 424 | return (vc->std & V4L2_STD_525_60) ? |
38f993ad DA |
425 | (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2); |
426 | } | |
427 | ||
5e950faf | 428 | static int norm_minw(struct s2255_vc *vc) |
38f993ad | 429 | { |
5e950faf | 430 | return (vc->std & V4L2_STD_525_60) ? |
38f993ad DA |
431 | LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL; |
432 | } | |
433 | ||
5e950faf | 434 | static int norm_minh(struct s2255_vc *vc) |
38f993ad | 435 | { |
5e950faf | 436 | return (vc->std & V4L2_STD_525_60) ? |
38f993ad DA |
437 | (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL); |
438 | } | |
439 | ||
440 | ||
3f8d6f73 DA |
441 | /* |
442 | * TODO: fixme: move YUV reordering to hardware | |
443 | * converts 2255 planar format to yuyv or uyvy | |
444 | */ | |
38f993ad DA |
445 | static void planar422p_to_yuv_packed(const unsigned char *in, |
446 | unsigned char *out, | |
447 | int width, int height, | |
448 | int fmt) | |
449 | { | |
450 | unsigned char *pY; | |
451 | unsigned char *pCb; | |
452 | unsigned char *pCr; | |
453 | unsigned long size = height * width; | |
454 | unsigned int i; | |
455 | pY = (unsigned char *)in; | |
456 | pCr = (unsigned char *)in + height * width; | |
457 | pCb = (unsigned char *)in + height * width + (height * width / 2); | |
458 | for (i = 0; i < size * 2; i += 4) { | |
459 | out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++; | |
460 | out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++; | |
461 | out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++; | |
462 | out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++; | |
463 | } | |
464 | return; | |
465 | } | |
466 | ||
d45b9b8a | 467 | static void s2255_reset_dsppower(struct s2255_dev *dev) |
14d96260 | 468 | { |
8a8cc952 | 469 | s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1); |
e2a06704 | 470 | msleep(50); |
14d96260 | 471 | s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1); |
752eb7ae | 472 | msleep(600); |
473 | s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1); | |
14d96260 DA |
474 | return; |
475 | } | |
38f993ad DA |
476 | |
477 | /* kickstarts the firmware loading. from probe | |
478 | */ | |
74ee0477 | 479 | static void s2255_timer(struct timer_list *t) |
38f993ad | 480 | { |
74ee0477 KC |
481 | struct s2255_dev *dev = from_timer(dev, t, timer); |
482 | struct s2255_fw *data = dev->fw_data; | |
38f993ad | 483 | if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) { |
f5402007 | 484 | pr_err("s2255: can't submit urb\n"); |
f78d92c9 DA |
485 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
486 | /* wake up anything waiting for the firmware */ | |
487 | wake_up(&data->wait_fw); | |
38f993ad DA |
488 | return; |
489 | } | |
490 | } | |
491 | ||
38f993ad DA |
492 | |
493 | /* this loads the firmware asynchronously. | |
0b84caab | 494 | Originally this was done synchronously in probe. |
38f993ad DA |
495 | But it is better to load it asynchronously here than block |
496 | inside the probe function. Blocking inside probe affects boot time. | |
497 | FW loading is triggered by the timer in the probe function | |
498 | */ | |
499 | static void s2255_fwchunk_complete(struct urb *urb) | |
500 | { | |
501 | struct s2255_fw *data = urb->context; | |
502 | struct usb_device *udev = urb->dev; | |
503 | int len; | |
38f993ad | 504 | if (urb->status) { |
be9ed511 | 505 | dev_err(&udev->dev, "URB failed with status %d\n", urb->status); |
f78d92c9 DA |
506 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
507 | /* wake up anything waiting for the firmware */ | |
508 | wake_up(&data->wait_fw); | |
38f993ad DA |
509 | return; |
510 | } | |
511 | if (data->fw_urb == NULL) { | |
be9ed511 | 512 | s2255_dev_err(&udev->dev, "disconnected\n"); |
f78d92c9 DA |
513 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
514 | /* wake up anything waiting for the firmware */ | |
515 | wake_up(&data->wait_fw); | |
38f993ad DA |
516 | return; |
517 | } | |
518 | #define CHUNK_SIZE 512 | |
519 | /* all USB transfers must be done with continuous kernel memory. | |
520 | can't allocate more than 128k in current linux kernel, so | |
521 | upload the firmware in chunks | |
522 | */ | |
523 | if (data->fw_loaded < data->fw_size) { | |
524 | len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ? | |
525 | data->fw_size % CHUNK_SIZE : CHUNK_SIZE; | |
526 | ||
527 | if (len < CHUNK_SIZE) | |
528 | memset(data->pfw_data, 0, CHUNK_SIZE); | |
529 | ||
38f993ad DA |
530 | memcpy(data->pfw_data, |
531 | (char *) data->fw->data + data->fw_loaded, len); | |
532 | ||
533 | usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2), | |
534 | data->pfw_data, CHUNK_SIZE, | |
535 | s2255_fwchunk_complete, data); | |
536 | if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) { | |
537 | dev_err(&udev->dev, "failed submit URB\n"); | |
538 | atomic_set(&data->fw_state, S2255_FW_FAILED); | |
539 | /* wake up anything waiting for the firmware */ | |
540 | wake_up(&data->wait_fw); | |
541 | return; | |
542 | } | |
543 | data->fw_loaded += len; | |
f5402007 | 544 | } else |
38f993ad | 545 | atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT); |
38f993ad DA |
546 | return; |
547 | ||
548 | } | |
549 | ||
9694fbec | 550 | static void s2255_got_frame(struct s2255_vc *vc, int jpgsize) |
38f993ad | 551 | { |
38f993ad | 552 | struct s2255_buffer *buf; |
5e950faf | 553 | struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev); |
38f993ad | 554 | unsigned long flags = 0; |
9694fbec | 555 | |
340a30c5 | 556 | spin_lock_irqsave(&vc->qlock, flags); |
5e950faf | 557 | if (list_empty(&vc->buf_list)) { |
f5402007 | 558 | dprintk(dev, 1, "No active queue to serve\n"); |
9694fbec | 559 | spin_unlock_irqrestore(&vc->qlock, flags); |
560 | return; | |
38f993ad | 561 | } |
5e950faf | 562 | buf = list_entry(vc->buf_list.next, |
340a30c5 | 563 | struct s2255_buffer, list); |
564 | list_del(&buf->list); | |
d6dd645e | 565 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); |
2d700715 JS |
566 | buf->vb.field = vc->field; |
567 | buf->vb.sequence = vc->frame_count; | |
9694fbec | 568 | spin_unlock_irqrestore(&vc->qlock, flags); |
569 | ||
5e950faf | 570 | s2255_fillbuff(vc, buf, jpgsize); |
9694fbec | 571 | /* tell v4l buffer was filled */ |
2d700715 | 572 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); |
340a30c5 | 573 | dprintk(dev, 2, "%s: [buf] [%p]\n", __func__, buf); |
38f993ad DA |
574 | } |
575 | ||
38f993ad DA |
576 | static const struct s2255_fmt *format_by_fourcc(int fourcc) |
577 | { | |
578 | unsigned int i; | |
38f993ad DA |
579 | for (i = 0; i < ARRAY_SIZE(formats); i++) { |
580 | if (-1 == formats[i].fourcc) | |
581 | continue; | |
f5402007 | 582 | if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) || |
583 | (formats[i].fourcc == V4L2_PIX_FMT_MJPEG))) | |
584 | continue; | |
38f993ad DA |
585 | if (formats[i].fourcc == fourcc) |
586 | return formats + i; | |
587 | } | |
588 | return NULL; | |
589 | } | |
590 | ||
38f993ad DA |
591 | /* video buffer vmalloc implementation based partly on VIVI driver which is |
592 | * Copyright (c) 2006 by | |
593 | * Mauro Carvalho Chehab <mchehab--a.t--infradead.org> | |
594 | * Ted Walther <ted--a.t--enumera.com> | |
595 | * John Sokol <sokol--a.t--videotechnology.com> | |
596 | * http://v4l.videotechnology.com/ | |
597 | * | |
598 | */ | |
5e950faf | 599 | static void s2255_fillbuff(struct s2255_vc *vc, |
fe85ce90 | 600 | struct s2255_buffer *buf, int jpgsize) |
38f993ad DA |
601 | { |
602 | int pos = 0; | |
38f993ad | 603 | const char *tmpbuf; |
2d700715 | 604 | char *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
38f993ad | 605 | unsigned long last_frame; |
5e950faf | 606 | struct s2255_dev *dev = vc->dev; |
38f993ad DA |
607 | |
608 | if (!vbuf) | |
609 | return; | |
5e950faf | 610 | last_frame = vc->last_frame; |
38f993ad | 611 | if (last_frame != -1) { |
38f993ad | 612 | tmpbuf = |
5e950faf | 613 | (const char *)vc->buffer.frame[last_frame].lpvbits; |
8bf405a0 | 614 | switch (vc->fmt->fourcc) { |
38f993ad DA |
615 | case V4L2_PIX_FMT_YUYV: |
616 | case V4L2_PIX_FMT_UYVY: | |
617 | planar422p_to_yuv_packed((const unsigned char *)tmpbuf, | |
340a30c5 | 618 | vbuf, vc->width, |
619 | vc->height, | |
8bf405a0 | 620 | vc->fmt->fourcc); |
38f993ad DA |
621 | break; |
622 | case V4L2_PIX_FMT_GREY: | |
340a30c5 | 623 | memcpy(vbuf, tmpbuf, vc->width * vc->height); |
38f993ad | 624 | break; |
14d96260 | 625 | case V4L2_PIX_FMT_JPEG: |
d0ef8540 | 626 | case V4L2_PIX_FMT_MJPEG: |
2d700715 | 627 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, jpgsize); |
340a30c5 | 628 | memcpy(vbuf, tmpbuf, jpgsize); |
14d96260 | 629 | break; |
38f993ad DA |
630 | case V4L2_PIX_FMT_YUV422P: |
631 | memcpy(vbuf, tmpbuf, | |
340a30c5 | 632 | vc->width * vc->height * 2); |
38f993ad DA |
633 | break; |
634 | default: | |
f5402007 | 635 | pr_info("s2255: unknown format?\n"); |
38f993ad | 636 | } |
5e950faf | 637 | vc->last_frame = -1; |
38f993ad | 638 | } else { |
f5402007 | 639 | pr_err("s2255: =======no frame\n"); |
38f993ad | 640 | return; |
38f993ad | 641 | } |
86f181c7 MCC |
642 | dprintk(dev, 2, "s2255fill at : Buffer %p size= %d\n", |
643 | vbuf, pos); | |
38f993ad DA |
644 | } |
645 | ||
646 | ||
647 | /* ------------------------------------------------------------------ | |
648 | Videobuf operations | |
649 | ------------------------------------------------------------------*/ | |
650 | ||
df9ecb0c | 651 | static int queue_setup(struct vb2_queue *vq, |
340a30c5 | 652 | unsigned int *nbuffers, unsigned int *nplanes, |
36c0f8b3 | 653 | unsigned int sizes[], struct device *alloc_devs[]) |
38f993ad | 654 | { |
340a30c5 | 655 | struct s2255_vc *vc = vb2_get_drv_priv(vq); |
9da62eb0 DA |
656 | if (*nbuffers < S2255_MIN_BUFS) |
657 | *nbuffers = S2255_MIN_BUFS; | |
340a30c5 | 658 | *nplanes = 1; |
659 | sizes[0] = vc->width * vc->height * (vc->fmt->depth >> 3); | |
38f993ad DA |
660 | return 0; |
661 | } | |
662 | ||
340a30c5 | 663 | static int buffer_prepare(struct vb2_buffer *vb) |
38f993ad | 664 | { |
340a30c5 | 665 | struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue); |
2d700715 JS |
666 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
667 | struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb); | |
5e950faf DA |
668 | int w = vc->width; |
669 | int h = vc->height; | |
340a30c5 | 670 | unsigned long size; |
671 | ||
672 | dprintk(vc->dev, 4, "%s\n", __func__); | |
5e950faf | 673 | if (vc->fmt == NULL) |
38f993ad DA |
674 | return -EINVAL; |
675 | ||
5e950faf DA |
676 | if ((w < norm_minw(vc)) || |
677 | (w > norm_maxw(vc)) || | |
678 | (h < norm_minh(vc)) || | |
679 | (h > norm_maxh(vc))) { | |
92cde477 | 680 | dprintk(vc->dev, 4, "invalid buffer prepare\n"); |
38f993ad DA |
681 | return -EINVAL; |
682 | } | |
340a30c5 | 683 | size = w * h * (vc->fmt->depth >> 3); |
684 | if (vb2_plane_size(vb, 0) < size) { | |
92cde477 | 685 | dprintk(vc->dev, 4, "invalid buffer prepare\n"); |
38f993ad DA |
686 | return -EINVAL; |
687 | } | |
688 | ||
2d700715 | 689 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); |
38f993ad | 690 | return 0; |
38f993ad DA |
691 | } |
692 | ||
340a30c5 | 693 | static void buffer_queue(struct vb2_buffer *vb) |
38f993ad | 694 | { |
2d700715 JS |
695 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
696 | struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb); | |
340a30c5 | 697 | struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue); |
698 | unsigned long flags = 0; | |
92cde477 | 699 | dprintk(vc->dev, 1, "%s\n", __func__); |
340a30c5 | 700 | spin_lock_irqsave(&vc->qlock, flags); |
701 | list_add_tail(&buf->list, &vc->buf_list); | |
702 | spin_unlock_irqrestore(&vc->qlock, flags); | |
38f993ad DA |
703 | } |
704 | ||
340a30c5 | 705 | static int start_streaming(struct vb2_queue *vq, unsigned int count); |
e37559b2 | 706 | static void stop_streaming(struct vb2_queue *vq); |
38f993ad | 707 | |
1bc17717 | 708 | static const struct vb2_ops s2255_video_qops = { |
340a30c5 | 709 | .queue_setup = queue_setup, |
38f993ad DA |
710 | .buf_prepare = buffer_prepare, |
711 | .buf_queue = buffer_queue, | |
340a30c5 | 712 | .start_streaming = start_streaming, |
713 | .stop_streaming = stop_streaming, | |
714 | .wait_prepare = vb2_ops_wait_prepare, | |
715 | .wait_finish = vb2_ops_wait_finish, | |
38f993ad DA |
716 | }; |
717 | ||
38f993ad DA |
718 | static int vidioc_querycap(struct file *file, void *priv, |
719 | struct v4l2_capability *cap) | |
720 | { | |
340a30c5 | 721 | struct s2255_vc *vc = video_drvdata(file); |
722 | struct s2255_dev *dev = vc->dev; | |
39696009 | 723 | |
c0decac1 MCC |
724 | strscpy(cap->driver, "s2255", sizeof(cap->driver)); |
725 | strscpy(cap->card, "s2255", sizeof(cap->card)); | |
e22ed887 | 726 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
340a30c5 | 727 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | |
728 | V4L2_CAP_READWRITE; | |
39696009 | 729 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
38f993ad DA |
730 | return 0; |
731 | } | |
732 | ||
733 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | |
734 | struct v4l2_fmtdesc *f) | |
735 | { | |
6c61ac63 | 736 | int index = f->index; |
38f993ad DA |
737 | |
738 | if (index >= ARRAY_SIZE(formats)) | |
739 | return -EINVAL; | |
6c61ac63 DC |
740 | if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) || |
741 | (formats[index].fourcc == V4L2_PIX_FMT_MJPEG))) | |
742 | return -EINVAL; | |
c0decac1 | 743 | strscpy(f->description, formats[index].name, sizeof(f->description)); |
38f993ad DA |
744 | f->pixelformat = formats[index].fourcc; |
745 | return 0; | |
746 | } | |
747 | ||
748 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | |
749 | struct v4l2_format *f) | |
750 | { | |
340a30c5 | 751 | struct s2255_vc *vc = video_drvdata(file); |
5e950faf | 752 | int is_ntsc = vc->std & V4L2_STD_525_60; |
38f993ad | 753 | |
5e950faf DA |
754 | f->fmt.pix.width = vc->width; |
755 | f->fmt.pix.height = vc->height; | |
92513611 HV |
756 | if (f->fmt.pix.height >= |
757 | (is_ntsc ? NUM_LINES_1CIFS_NTSC : NUM_LINES_1CIFS_PAL) * 2) | |
758 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
759 | else | |
760 | f->fmt.pix.field = V4L2_FIELD_TOP; | |
5e950faf DA |
761 | f->fmt.pix.pixelformat = vc->fmt->fourcc; |
762 | f->fmt.pix.bytesperline = f->fmt.pix.width * (vc->fmt->depth >> 3); | |
38f993ad | 763 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
29ceb110 HV |
764 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
765 | f->fmt.pix.priv = 0; | |
3f8d6f73 | 766 | return 0; |
38f993ad DA |
767 | } |
768 | ||
769 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | |
770 | struct v4l2_format *f) | |
771 | { | |
772 | const struct s2255_fmt *fmt; | |
773 | enum v4l2_field field; | |
340a30c5 | 774 | struct s2255_vc *vc = video_drvdata(file); |
5e950faf | 775 | int is_ntsc = vc->std & V4L2_STD_525_60; |
38f993ad DA |
776 | |
777 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
778 | ||
779 | if (fmt == NULL) | |
780 | return -EINVAL; | |
781 | ||
782 | field = f->fmt.pix.field; | |
38f993ad | 783 | |
92cde477 | 784 | dprintk(vc->dev, 50, "%s NTSC: %d suggested width: %d, height: %d\n", |
85b85482 | 785 | __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height); |
38f993ad DA |
786 | if (is_ntsc) { |
787 | /* NTSC */ | |
788 | if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) { | |
789 | f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2; | |
92513611 | 790 | field = V4L2_FIELD_INTERLACED; |
38f993ad DA |
791 | } else { |
792 | f->fmt.pix.height = NUM_LINES_1CIFS_NTSC; | |
92513611 | 793 | field = V4L2_FIELD_TOP; |
38f993ad DA |
794 | } |
795 | if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC) | |
796 | f->fmt.pix.width = LINE_SZ_4CIFS_NTSC; | |
38f993ad DA |
797 | else |
798 | f->fmt.pix.width = LINE_SZ_1CIFS_NTSC; | |
799 | } else { | |
800 | /* PAL */ | |
801 | if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) { | |
802 | f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2; | |
92513611 | 803 | field = V4L2_FIELD_INTERLACED; |
38f993ad DA |
804 | } else { |
805 | f->fmt.pix.height = NUM_LINES_1CIFS_PAL; | |
92513611 | 806 | field = V4L2_FIELD_TOP; |
38f993ad | 807 | } |
92513611 | 808 | if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) |
38f993ad | 809 | f->fmt.pix.width = LINE_SZ_4CIFS_PAL; |
92513611 | 810 | else |
38f993ad | 811 | f->fmt.pix.width = LINE_SZ_1CIFS_PAL; |
38f993ad | 812 | } |
38f993ad DA |
813 | f->fmt.pix.field = field; |
814 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; | |
815 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; | |
29ceb110 HV |
816 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
817 | f->fmt.pix.priv = 0; | |
92cde477 | 818 | dprintk(vc->dev, 50, "%s: set width %d height %d field %d\n", __func__, |
85b85482 | 819 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field); |
38f993ad DA |
820 | return 0; |
821 | } | |
822 | ||
823 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | |
824 | struct v4l2_format *f) | |
825 | { | |
340a30c5 | 826 | struct s2255_vc *vc = video_drvdata(file); |
38f993ad | 827 | const struct s2255_fmt *fmt; |
340a30c5 | 828 | struct vb2_queue *q = &vc->vb_vidq; |
fe85ce90 | 829 | struct s2255_mode mode; |
38f993ad | 830 | int ret; |
38f993ad | 831 | |
340a30c5 | 832 | ret = vidioc_try_fmt_vid_cap(file, vc, f); |
38f993ad DA |
833 | |
834 | if (ret < 0) | |
3f8d6f73 | 835 | return ret; |
38f993ad DA |
836 | |
837 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
838 | ||
839 | if (fmt == NULL) | |
840 | return -EINVAL; | |
841 | ||
340a30c5 | 842 | if (vb2_is_busy(q)) { |
92cde477 | 843 | dprintk(vc->dev, 1, "queue busy\n"); |
340a30c5 | 844 | return -EBUSY; |
38f993ad DA |
845 | } |
846 | ||
5e950faf DA |
847 | mode = vc->mode; |
848 | vc->fmt = fmt; | |
849 | vc->width = f->fmt.pix.width; | |
850 | vc->height = f->fmt.pix.height; | |
340a30c5 | 851 | vc->field = f->fmt.pix.field; |
5e950faf DA |
852 | if (vc->width > norm_minw(vc)) { |
853 | if (vc->height > norm_minh(vc)) { | |
854 | if (vc->cap_parm.capturemode & | |
85b85482 | 855 | V4L2_MODE_HIGHQUALITY) |
fe85ce90 | 856 | mode.scale = SCALE_4CIFSI; |
85b85482 | 857 | else |
fe85ce90 | 858 | mode.scale = SCALE_4CIFS; |
7d853532 | 859 | } else |
fe85ce90 | 860 | mode.scale = SCALE_2CIFS; |
38f993ad DA |
861 | |
862 | } else { | |
fe85ce90 | 863 | mode.scale = SCALE_1CIFS; |
38f993ad | 864 | } |
38f993ad | 865 | /* color mode */ |
5e950faf | 866 | switch (vc->fmt->fourcc) { |
38f993ad | 867 | case V4L2_PIX_FMT_GREY: |
fe85ce90 DA |
868 | mode.color &= ~MASK_COLOR; |
869 | mode.color |= COLOR_Y8; | |
38f993ad | 870 | break; |
14d96260 | 871 | case V4L2_PIX_FMT_JPEG: |
d0ef8540 | 872 | case V4L2_PIX_FMT_MJPEG: |
fe85ce90 DA |
873 | mode.color &= ~MASK_COLOR; |
874 | mode.color |= COLOR_JPG; | |
5e950faf | 875 | mode.color |= (vc->jpegqual << 8); |
14d96260 | 876 | break; |
38f993ad | 877 | case V4L2_PIX_FMT_YUV422P: |
fe85ce90 DA |
878 | mode.color &= ~MASK_COLOR; |
879 | mode.color |= COLOR_YUVPL; | |
38f993ad DA |
880 | break; |
881 | case V4L2_PIX_FMT_YUYV: | |
882 | case V4L2_PIX_FMT_UYVY: | |
883 | default: | |
fe85ce90 DA |
884 | mode.color &= ~MASK_COLOR; |
885 | mode.color |= COLOR_YUVPK; | |
38f993ad DA |
886 | break; |
887 | } | |
5e950faf | 888 | if ((mode.color & MASK_COLOR) != (vc->mode.color & MASK_COLOR)) |
fe85ce90 | 889 | mode.restart = 1; |
5e950faf | 890 | else if (mode.scale != vc->mode.scale) |
fe85ce90 | 891 | mode.restart = 1; |
5e950faf | 892 | else if (mode.format != vc->mode.format) |
fe85ce90 | 893 | mode.restart = 1; |
5e950faf DA |
894 | vc->mode = mode; |
895 | (void) s2255_set_mode(vc, &mode); | |
340a30c5 | 896 | return 0; |
38f993ad DA |
897 | } |
898 | ||
38f993ad | 899 | |
38f993ad DA |
900 | /* write to the configuration pipe, synchronously */ |
901 | static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf, | |
902 | int size) | |
903 | { | |
904 | int pipe; | |
905 | int done; | |
906 | long retval = -1; | |
907 | if (udev) { | |
908 | pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP); | |
909 | retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500); | |
910 | } | |
911 | return retval; | |
912 | } | |
913 | ||
914 | static u32 get_transfer_size(struct s2255_mode *mode) | |
915 | { | |
916 | int linesPerFrame = LINE_SZ_DEF; | |
917 | int pixelsPerLine = NUM_LINES_DEF; | |
918 | u32 outImageSize; | |
919 | u32 usbInSize; | |
920 | unsigned int mask_mult; | |
921 | ||
922 | if (mode == NULL) | |
923 | return 0; | |
924 | ||
925 | if (mode->format == FORMAT_NTSC) { | |
926 | switch (mode->scale) { | |
927 | case SCALE_4CIFS: | |
7d853532 | 928 | case SCALE_4CIFSI: |
38f993ad DA |
929 | linesPerFrame = NUM_LINES_4CIFS_NTSC * 2; |
930 | pixelsPerLine = LINE_SZ_4CIFS_NTSC; | |
931 | break; | |
932 | case SCALE_2CIFS: | |
933 | linesPerFrame = NUM_LINES_2CIFS_NTSC; | |
934 | pixelsPerLine = LINE_SZ_2CIFS_NTSC; | |
935 | break; | |
936 | case SCALE_1CIFS: | |
937 | linesPerFrame = NUM_LINES_1CIFS_NTSC; | |
938 | pixelsPerLine = LINE_SZ_1CIFS_NTSC; | |
939 | break; | |
940 | default: | |
941 | break; | |
942 | } | |
943 | } else if (mode->format == FORMAT_PAL) { | |
944 | switch (mode->scale) { | |
945 | case SCALE_4CIFS: | |
7d853532 | 946 | case SCALE_4CIFSI: |
38f993ad DA |
947 | linesPerFrame = NUM_LINES_4CIFS_PAL * 2; |
948 | pixelsPerLine = LINE_SZ_4CIFS_PAL; | |
949 | break; | |
950 | case SCALE_2CIFS: | |
951 | linesPerFrame = NUM_LINES_2CIFS_PAL; | |
952 | pixelsPerLine = LINE_SZ_2CIFS_PAL; | |
953 | break; | |
954 | case SCALE_1CIFS: | |
955 | linesPerFrame = NUM_LINES_1CIFS_PAL; | |
956 | pixelsPerLine = LINE_SZ_1CIFS_PAL; | |
957 | break; | |
958 | default: | |
959 | break; | |
960 | } | |
961 | } | |
962 | outImageSize = linesPerFrame * pixelsPerLine; | |
14d96260 | 963 | if ((mode->color & MASK_COLOR) != COLOR_Y8) { |
38f993ad DA |
964 | /* 2 bytes/pixel if not monochrome */ |
965 | outImageSize *= 2; | |
966 | } | |
967 | ||
968 | /* total bytes to send including prefix and 4K padding; | |
969 | must be a multiple of USB_READ_SIZE */ | |
970 | usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */ | |
971 | mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1; | |
972 | /* if size not a multiple of USB_READ_SIZE */ | |
973 | if (usbInSize & ~mask_mult) | |
974 | usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK); | |
975 | return usbInSize; | |
976 | } | |
977 | ||
85b85482 | 978 | static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode) |
38f993ad DA |
979 | { |
980 | struct device *dev = &sdev->udev->dev; | |
981 | dev_info(dev, "------------------------------------------------\n"); | |
85b85482 DA |
982 | dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale); |
983 | dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color); | |
38f993ad | 984 | dev_info(dev, "bright: 0x%x\n", mode->bright); |
38f993ad DA |
985 | dev_info(dev, "------------------------------------------------\n"); |
986 | } | |
987 | ||
988 | /* | |
989 | * set mode is the function which controls the DSP. | |
990 | * the restart parameter in struct s2255_mode should be set whenever | |
991 | * the image size could change via color format, video system or image | |
992 | * size. | |
993 | * When the restart parameter is set, we sleep for ONE frame to allow the | |
994 | * DSP time to get the new frame | |
995 | */ | |
5e950faf | 996 | static int s2255_set_mode(struct s2255_vc *vc, |
38f993ad DA |
997 | struct s2255_mode *mode) |
998 | { | |
999 | int res; | |
38f993ad | 1000 | unsigned long chn_rev; |
5e950faf | 1001 | struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev); |
0b84caab | 1002 | int i; |
47d8c881 | 1003 | __le32 *buffer = dev->cmdbuf; |
0b84caab | 1004 | |
47d8c881 | 1005 | mutex_lock(&dev->cmdlock); |
5e950faf DA |
1006 | chn_rev = G_chnmap[vc->idx]; |
1007 | dprintk(dev, 3, "%s channel: %d\n", __func__, vc->idx); | |
22b88d48 | 1008 | /* if JPEG, set the quality */ |
5a34d9df DA |
1009 | if ((mode->color & MASK_COLOR) == COLOR_JPG) { |
1010 | mode->color &= ~MASK_COLOR; | |
1011 | mode->color |= COLOR_JPG; | |
1012 | mode->color &= ~MASK_JPG_QUALITY; | |
5e950faf | 1013 | mode->color |= (vc->jpegqual << 8); |
5a34d9df | 1014 | } |
38f993ad | 1015 | /* save the mode */ |
5e950faf DA |
1016 | vc->mode = *mode; |
1017 | vc->req_image_size = get_transfer_size(mode); | |
1018 | dprintk(dev, 1, "%s: reqsize %ld\n", __func__, vc->req_image_size); | |
38f993ad DA |
1019 | /* set the mode */ |
1020 | buffer[0] = IN_DATA_TOKEN; | |
3fa00605 | 1021 | buffer[1] = (__le32) cpu_to_le32(chn_rev); |
38f993ad | 1022 | buffer[2] = CMD_SET_MODE; |
0b84caab | 1023 | for (i = 0; i < sizeof(struct s2255_mode) / sizeof(u32); i++) |
5e950faf DA |
1024 | buffer[3 + i] = cpu_to_le32(((u32 *)&vc->mode)[i]); |
1025 | vc->setmode_ready = 0; | |
38f993ad DA |
1026 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
1027 | if (debug) | |
85b85482 | 1028 | s2255_print_cfg(dev, mode); |
38f993ad | 1029 | /* wait at least 3 frames before continuing */ |
14d96260 | 1030 | if (mode->restart) { |
5e950faf DA |
1031 | wait_event_timeout(vc->wait_setmode, |
1032 | (vc->setmode_ready != 0), | |
14d96260 | 1033 | msecs_to_jiffies(S2255_SETMODE_TIMEOUT)); |
5e950faf | 1034 | if (vc->setmode_ready != 1) { |
f5402007 | 1035 | dprintk(dev, 0, "s2255: no set mode response\n"); |
14d96260 DA |
1036 | res = -EFAULT; |
1037 | } | |
1038 | } | |
38f993ad | 1039 | /* clear the restart flag */ |
5e950faf DA |
1040 | vc->mode.restart = 0; |
1041 | dprintk(dev, 1, "%s chn %d, result: %d\n", __func__, vc->idx, res); | |
47d8c881 | 1042 | mutex_unlock(&dev->cmdlock); |
38f993ad DA |
1043 | return res; |
1044 | } | |
1045 | ||
5e950faf | 1046 | static int s2255_cmd_status(struct s2255_vc *vc, u32 *pstatus) |
4de39f5d DA |
1047 | { |
1048 | int res; | |
4de39f5d | 1049 | u32 chn_rev; |
5e950faf | 1050 | struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev); |
47d8c881 DA |
1051 | __le32 *buffer = dev->cmdbuf; |
1052 | ||
1053 | mutex_lock(&dev->cmdlock); | |
5e950faf DA |
1054 | chn_rev = G_chnmap[vc->idx]; |
1055 | dprintk(dev, 4, "%s chan %d\n", __func__, vc->idx); | |
4de39f5d DA |
1056 | /* form the get vid status command */ |
1057 | buffer[0] = IN_DATA_TOKEN; | |
3fa00605 | 1058 | buffer[1] = (__le32) cpu_to_le32(chn_rev); |
4de39f5d DA |
1059 | buffer[2] = CMD_STATUS; |
1060 | *pstatus = 0; | |
5e950faf | 1061 | vc->vidstatus_ready = 0; |
4de39f5d | 1062 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
5e950faf DA |
1063 | wait_event_timeout(vc->wait_vidstatus, |
1064 | (vc->vidstatus_ready != 0), | |
4de39f5d | 1065 | msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT)); |
5e950faf | 1066 | if (vc->vidstatus_ready != 1) { |
f5402007 | 1067 | dprintk(dev, 0, "s2255: no vidstatus response\n"); |
4de39f5d DA |
1068 | res = -EFAULT; |
1069 | } | |
5e950faf | 1070 | *pstatus = vc->vidstatus; |
f5402007 | 1071 | dprintk(dev, 4, "%s, vid status %d\n", __func__, *pstatus); |
47d8c881 | 1072 | mutex_unlock(&dev->cmdlock); |
4de39f5d DA |
1073 | return res; |
1074 | } | |
1075 | ||
340a30c5 | 1076 | static int start_streaming(struct vb2_queue *vq, unsigned int count) |
38f993ad | 1077 | { |
340a30c5 | 1078 | struct s2255_vc *vc = vb2_get_drv_priv(vq); |
38f993ad | 1079 | int j; |
92cde477 | 1080 | |
5e950faf DA |
1081 | vc->last_frame = -1; |
1082 | vc->bad_payload = 0; | |
1083 | vc->cur_frame = 0; | |
1084 | vc->frame_count = 0; | |
38f993ad | 1085 | for (j = 0; j < SYS_FRAMES; j++) { |
5e950faf DA |
1086 | vc->buffer.frame[j].ulState = S2255_READ_IDLE; |
1087 | vc->buffer.frame[j].cur_size = 0; | |
38f993ad | 1088 | } |
340a30c5 | 1089 | return s2255_start_acquire(vc); |
38f993ad DA |
1090 | } |
1091 | ||
340a30c5 | 1092 | /* abort streaming and wait for last buffer */ |
e37559b2 | 1093 | static void stop_streaming(struct vb2_queue *vq) |
38f993ad | 1094 | { |
340a30c5 | 1095 | struct s2255_vc *vc = vb2_get_drv_priv(vq); |
1096 | struct s2255_buffer *buf, *node; | |
1097 | unsigned long flags; | |
1098 | (void) s2255_stop_acquire(vc); | |
1099 | spin_lock_irqsave(&vc->qlock, flags); | |
1100 | list_for_each_entry_safe(buf, node, &vc->buf_list, list) { | |
1101 | list_del(&buf->list); | |
2d700715 | 1102 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
340a30c5 | 1103 | dprintk(vc->dev, 2, "[%p/%d] done\n", |
2d700715 | 1104 | buf, buf->vb.vb2_buf.index); |
340a30c5 | 1105 | } |
1106 | spin_unlock_irqrestore(&vc->qlock, flags); | |
38f993ad DA |
1107 | } |
1108 | ||
314527ac | 1109 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id i) |
38f993ad | 1110 | { |
340a30c5 | 1111 | struct s2255_vc *vc = video_drvdata(file); |
fe85ce90 | 1112 | struct s2255_mode mode; |
340a30c5 | 1113 | struct vb2_queue *q = &vc->vb_vidq; |
1114 | ||
1115 | /* | |
1116 | * Changing the standard implies a format change, which is not allowed | |
1117 | * while buffers for use with streaming have already been allocated. | |
1118 | */ | |
1119 | if (vb2_is_busy(q)) | |
1120 | return -EBUSY; | |
469af77a | 1121 | |
92cde477 | 1122 | mode = vc->mode; |
314527ac | 1123 | if (i & V4L2_STD_525_60) { |
92cde477 | 1124 | dprintk(vc->dev, 4, "%s 60 Hz\n", __func__); |
e6b44bc5 | 1125 | /* if changing format, reset frame decimation/intervals */ |
fe85ce90 DA |
1126 | if (mode.format != FORMAT_NTSC) { |
1127 | mode.restart = 1; | |
1128 | mode.format = FORMAT_NTSC; | |
1129 | mode.fdec = FDEC_1; | |
5e950faf DA |
1130 | vc->width = LINE_SZ_4CIFS_NTSC; |
1131 | vc->height = NUM_LINES_4CIFS_NTSC * 2; | |
e6b44bc5 | 1132 | } |
314527ac | 1133 | } else if (i & V4L2_STD_625_50) { |
92cde477 | 1134 | dprintk(vc->dev, 4, "%s 50 Hz\n", __func__); |
fe85ce90 DA |
1135 | if (mode.format != FORMAT_PAL) { |
1136 | mode.restart = 1; | |
1137 | mode.format = FORMAT_PAL; | |
1138 | mode.fdec = FDEC_1; | |
5e950faf DA |
1139 | vc->width = LINE_SZ_4CIFS_PAL; |
1140 | vc->height = NUM_LINES_4CIFS_PAL * 2; | |
e6b44bc5 | 1141 | } |
340a30c5 | 1142 | } else |
1143 | return -EINVAL; | |
92cde477 | 1144 | vc->std = i; |
fe85ce90 | 1145 | if (mode.restart) |
92cde477 | 1146 | s2255_set_mode(vc, &mode); |
340a30c5 | 1147 | return 0; |
38f993ad DA |
1148 | } |
1149 | ||
469af77a HV |
1150 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *i) |
1151 | { | |
340a30c5 | 1152 | struct s2255_vc *vc = video_drvdata(file); |
469af77a | 1153 | |
92cde477 | 1154 | *i = vc->std; |
469af77a HV |
1155 | return 0; |
1156 | } | |
1157 | ||
38f993ad DA |
1158 | /* Sensoray 2255 is a multiple channel capture device. |
1159 | It does not have a "crossbar" of inputs. | |
1160 | We use one V4L device per channel. The user must | |
1161 | be aware that certain combinations are not allowed. | |
1162 | For instance, you cannot do full FPS on more than 2 channels(2 videodevs) | |
1163 | at once in color(you can do full fps on 4 channels with greyscale. | |
1164 | */ | |
1165 | static int vidioc_enum_input(struct file *file, void *priv, | |
1166 | struct v4l2_input *inp) | |
1167 | { | |
340a30c5 | 1168 | struct s2255_vc *vc = video_drvdata(file); |
92cde477 | 1169 | struct s2255_dev *dev = vc->dev; |
4de39f5d | 1170 | u32 status = 0; |
92cde477 | 1171 | |
38f993ad DA |
1172 | if (inp->index != 0) |
1173 | return -EINVAL; | |
38f993ad DA |
1174 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
1175 | inp->std = S2255_NORMS; | |
4de39f5d DA |
1176 | inp->status = 0; |
1177 | if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) { | |
1178 | int rc; | |
92cde477 | 1179 | rc = s2255_cmd_status(vc, &status); |
f5402007 | 1180 | dprintk(dev, 4, "s2255_cmd_status rc: %d status %x\n", |
1181 | rc, status); | |
4de39f5d DA |
1182 | if (rc == 0) |
1183 | inp->status = (status & 0x01) ? 0 | |
1184 | : V4L2_IN_ST_NO_SIGNAL; | |
1185 | } | |
5a34d9df DA |
1186 | switch (dev->pid) { |
1187 | case 0x2255: | |
1188 | default: | |
c0decac1 | 1189 | strscpy(inp->name, "Composite", sizeof(inp->name)); |
5a34d9df DA |
1190 | break; |
1191 | case 0x2257: | |
c0decac1 | 1192 | strscpy(inp->name, (vc->idx < 2) ? "Composite" : "S-Video", |
5a34d9df DA |
1193 | sizeof(inp->name)); |
1194 | break; | |
1195 | } | |
3f8d6f73 | 1196 | return 0; |
38f993ad DA |
1197 | } |
1198 | ||
1199 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) | |
1200 | { | |
1201 | *i = 0; | |
1202 | return 0; | |
1203 | } | |
1204 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) | |
1205 | { | |
1206 | if (i > 0) | |
1207 | return -EINVAL; | |
1208 | return 0; | |
1209 | } | |
1210 | ||
192f1e78 | 1211 | static int s2255_s_ctrl(struct v4l2_ctrl *ctrl) |
38f993ad | 1212 | { |
5e950faf DA |
1213 | struct s2255_vc *vc = |
1214 | container_of(ctrl->handler, struct s2255_vc, hdl); | |
fe85ce90 | 1215 | struct s2255_mode mode; |
5e950faf | 1216 | mode = vc->mode; |
2e70db9a DA |
1217 | /* update the mode to the corresponding value */ |
1218 | switch (ctrl->id) { | |
1219 | case V4L2_CID_BRIGHTNESS: | |
192f1e78 | 1220 | mode.bright = ctrl->val; |
2e70db9a DA |
1221 | break; |
1222 | case V4L2_CID_CONTRAST: | |
192f1e78 | 1223 | mode.contrast = ctrl->val; |
2e70db9a DA |
1224 | break; |
1225 | case V4L2_CID_HUE: | |
192f1e78 | 1226 | mode.hue = ctrl->val; |
2e70db9a DA |
1227 | break; |
1228 | case V4L2_CID_SATURATION: | |
192f1e78 | 1229 | mode.saturation = ctrl->val; |
2e70db9a | 1230 | break; |
192f1e78 | 1231 | case V4L2_CID_S2255_COLORFILTER: |
fe85ce90 | 1232 | mode.color &= ~MASK_INPUT_TYPE; |
192f1e78 | 1233 | mode.color |= !ctrl->val << 16; |
5a34d9df | 1234 | break; |
7041dec7 | 1235 | case V4L2_CID_JPEG_COMPRESSION_QUALITY: |
5e950faf | 1236 | vc->jpegqual = ctrl->val; |
7041dec7 | 1237 | return 0; |
2e70db9a DA |
1238 | default: |
1239 | return -EINVAL; | |
38f993ad | 1240 | } |
fe85ce90 | 1241 | mode.restart = 0; |
2e70db9a DA |
1242 | /* set mode here. Note: stream does not need restarted. |
1243 | some V4L programs restart stream unnecessarily | |
1244 | after a s_crtl. | |
1245 | */ | |
5e950faf | 1246 | s2255_set_mode(vc, &mode); |
2e70db9a | 1247 | return 0; |
38f993ad DA |
1248 | } |
1249 | ||
22b88d48 DA |
1250 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
1251 | struct v4l2_jpegcompression *jc) | |
1252 | { | |
340a30c5 | 1253 | struct s2255_vc *vc = video_drvdata(file); |
7041dec7 HV |
1254 | |
1255 | memset(jc, 0, sizeof(*jc)); | |
5e950faf | 1256 | jc->quality = vc->jpegqual; |
92cde477 | 1257 | dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality); |
22b88d48 DA |
1258 | return 0; |
1259 | } | |
1260 | ||
1261 | static int vidioc_s_jpegcomp(struct file *file, void *priv, | |
d88aab53 | 1262 | const struct v4l2_jpegcompression *jc) |
22b88d48 | 1263 | { |
340a30c5 | 1264 | struct s2255_vc *vc = video_drvdata(file); |
1265 | ||
22b88d48 DA |
1266 | if (jc->quality < 0 || jc->quality > 100) |
1267 | return -EINVAL; | |
5e950faf | 1268 | v4l2_ctrl_s_ctrl(vc->jpegqual_ctrl, jc->quality); |
92cde477 | 1269 | dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality); |
22b88d48 DA |
1270 | return 0; |
1271 | } | |
7d853532 DA |
1272 | |
1273 | static int vidioc_g_parm(struct file *file, void *priv, | |
1274 | struct v4l2_streamparm *sp) | |
1275 | { | |
e6b44bc5 | 1276 | __u32 def_num, def_dem; |
340a30c5 | 1277 | struct s2255_vc *vc = video_drvdata(file); |
1278 | ||
7d853532 DA |
1279 | if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1280 | return -EINVAL; | |
e6b44bc5 | 1281 | sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
5e950faf | 1282 | sp->parm.capture.capturemode = vc->cap_parm.capturemode; |
340a30c5 | 1283 | sp->parm.capture.readbuffers = S2255_MIN_BUFS; |
5e950faf DA |
1284 | def_num = (vc->mode.format == FORMAT_NTSC) ? 1001 : 1000; |
1285 | def_dem = (vc->mode.format == FORMAT_NTSC) ? 30000 : 25000; | |
e6b44bc5 | 1286 | sp->parm.capture.timeperframe.denominator = def_dem; |
5e950faf | 1287 | switch (vc->mode.fdec) { |
e6b44bc5 DA |
1288 | default: |
1289 | case FDEC_1: | |
1290 | sp->parm.capture.timeperframe.numerator = def_num; | |
1291 | break; | |
1292 | case FDEC_2: | |
1293 | sp->parm.capture.timeperframe.numerator = def_num * 2; | |
1294 | break; | |
1295 | case FDEC_3: | |
1296 | sp->parm.capture.timeperframe.numerator = def_num * 3; | |
1297 | break; | |
1298 | case FDEC_5: | |
1299 | sp->parm.capture.timeperframe.numerator = def_num * 5; | |
1300 | break; | |
1301 | } | |
92cde477 | 1302 | dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d\n", |
f5402007 | 1303 | __func__, |
e6b44bc5 DA |
1304 | sp->parm.capture.capturemode, |
1305 | sp->parm.capture.timeperframe.numerator, | |
1306 | sp->parm.capture.timeperframe.denominator); | |
7d853532 DA |
1307 | return 0; |
1308 | } | |
1309 | ||
1310 | static int vidioc_s_parm(struct file *file, void *priv, | |
1311 | struct v4l2_streamparm *sp) | |
1312 | { | |
340a30c5 | 1313 | struct s2255_vc *vc = video_drvdata(file); |
fe85ce90 | 1314 | struct s2255_mode mode; |
e6b44bc5 DA |
1315 | int fdec = FDEC_1; |
1316 | __u32 def_num, def_dem; | |
7d853532 DA |
1317 | if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1318 | return -EINVAL; | |
5e950faf | 1319 | mode = vc->mode; |
e6b44bc5 | 1320 | /* high quality capture mode requires a stream restart */ |
340a30c5 | 1321 | if ((vc->cap_parm.capturemode != sp->parm.capture.capturemode) |
1322 | && vb2_is_streaming(&vc->vb_vidq)) | |
e6b44bc5 | 1323 | return -EBUSY; |
fe85ce90 DA |
1324 | def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000; |
1325 | def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000; | |
e6b44bc5 DA |
1326 | if (def_dem != sp->parm.capture.timeperframe.denominator) |
1327 | sp->parm.capture.timeperframe.numerator = def_num; | |
1328 | else if (sp->parm.capture.timeperframe.numerator <= def_num) | |
1329 | sp->parm.capture.timeperframe.numerator = def_num; | |
1330 | else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) { | |
1331 | sp->parm.capture.timeperframe.numerator = def_num * 2; | |
1332 | fdec = FDEC_2; | |
1333 | } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) { | |
1334 | sp->parm.capture.timeperframe.numerator = def_num * 3; | |
1335 | fdec = FDEC_3; | |
1336 | } else { | |
1337 | sp->parm.capture.timeperframe.numerator = def_num * 5; | |
1338 | fdec = FDEC_5; | |
1339 | } | |
fe85ce90 | 1340 | mode.fdec = fdec; |
e6b44bc5 | 1341 | sp->parm.capture.timeperframe.denominator = def_dem; |
340a30c5 | 1342 | sp->parm.capture.readbuffers = S2255_MIN_BUFS; |
5e950faf | 1343 | s2255_set_mode(vc, &mode); |
92cde477 | 1344 | dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n", |
e6b44bc5 DA |
1345 | __func__, |
1346 | sp->parm.capture.capturemode, | |
1347 | sp->parm.capture.timeperframe.numerator, | |
1348 | sp->parm.capture.timeperframe.denominator, fdec); | |
1349 | return 0; | |
1350 | } | |
7d853532 | 1351 | |
05e5d44b HV |
1352 | #define NUM_SIZE_ENUMS 3 |
1353 | static const struct v4l2_frmsize_discrete ntsc_sizes[] = { | |
1354 | { 640, 480 }, | |
1355 | { 640, 240 }, | |
1356 | { 320, 240 }, | |
1357 | }; | |
1358 | static const struct v4l2_frmsize_discrete pal_sizes[] = { | |
1359 | { 704, 576 }, | |
1360 | { 704, 288 }, | |
1361 | { 352, 288 }, | |
1362 | }; | |
1363 | ||
1364 | static int vidioc_enum_framesizes(struct file *file, void *priv, | |
1365 | struct v4l2_frmsizeenum *fe) | |
1366 | { | |
340a30c5 | 1367 | struct s2255_vc *vc = video_drvdata(file); |
5e950faf | 1368 | int is_ntsc = vc->std & V4L2_STD_525_60; |
05e5d44b HV |
1369 | const struct s2255_fmt *fmt; |
1370 | ||
1371 | if (fe->index >= NUM_SIZE_ENUMS) | |
1372 | return -EINVAL; | |
1373 | ||
1374 | fmt = format_by_fourcc(fe->pixel_format); | |
1375 | if (fmt == NULL) | |
1376 | return -EINVAL; | |
1377 | fe->type = V4L2_FRMSIZE_TYPE_DISCRETE; | |
1378 | fe->discrete = is_ntsc ? ntsc_sizes[fe->index] : pal_sizes[fe->index]; | |
1379 | return 0; | |
1380 | } | |
1381 | ||
e6b44bc5 DA |
1382 | static int vidioc_enum_frameintervals(struct file *file, void *priv, |
1383 | struct v4l2_frmivalenum *fe) | |
1384 | { | |
340a30c5 | 1385 | struct s2255_vc *vc = video_drvdata(file); |
05e5d44b HV |
1386 | const struct s2255_fmt *fmt; |
1387 | const struct v4l2_frmsize_discrete *sizes; | |
5e950faf | 1388 | int is_ntsc = vc->std & V4L2_STD_525_60; |
e6b44bc5 DA |
1389 | #define NUM_FRAME_ENUMS 4 |
1390 | int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5}; | |
05e5d44b HV |
1391 | int i; |
1392 | ||
199ab8fe | 1393 | if (fe->index >= NUM_FRAME_ENUMS) |
e6b44bc5 | 1394 | return -EINVAL; |
05e5d44b HV |
1395 | |
1396 | fmt = format_by_fourcc(fe->pixel_format); | |
1397 | if (fmt == NULL) | |
e6b44bc5 | 1398 | return -EINVAL; |
05e5d44b HV |
1399 | |
1400 | sizes = is_ntsc ? ntsc_sizes : pal_sizes; | |
1401 | for (i = 0; i < NUM_SIZE_ENUMS; i++, sizes++) | |
1402 | if (fe->width == sizes->width && | |
1403 | fe->height == sizes->height) | |
1404 | break; | |
1405 | if (i == NUM_SIZE_ENUMS) | |
1406 | return -EINVAL; | |
1407 | ||
e6b44bc5 DA |
1408 | fe->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
1409 | fe->discrete.denominator = is_ntsc ? 30000 : 25000; | |
1410 | fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index]; | |
92cde477 | 1411 | dprintk(vc->dev, 4, "%s discrete %d/%d\n", __func__, |
f5402007 | 1412 | fe->discrete.numerator, |
e6b44bc5 | 1413 | fe->discrete.denominator); |
7d853532 DA |
1414 | return 0; |
1415 | } | |
e6b44bc5 | 1416 | |
340a30c5 | 1417 | static int s2255_open(struct file *file) |
38f993ad | 1418 | { |
5e950faf | 1419 | struct s2255_vc *vc = video_drvdata(file); |
340a30c5 | 1420 | struct s2255_dev *dev = vc->dev; |
14d96260 | 1421 | int state; |
340a30c5 | 1422 | int rc = 0; |
1423 | ||
1424 | rc = v4l2_fh_open(file); | |
1425 | if (rc != 0) | |
1426 | return rc; | |
1427 | ||
1428 | dprintk(dev, 1, "s2255: %s\n", __func__); | |
ff7e22df DA |
1429 | state = atomic_read(&dev->fw_data->fw_state); |
1430 | switch (state) { | |
1431 | case S2255_FW_DISCONNECTING: | |
14d96260 | 1432 | return -ENODEV; |
14d96260 | 1433 | case S2255_FW_FAILED: |
be9ed511 MCC |
1434 | s2255_dev_err(&dev->udev->dev, |
1435 | "firmware load failed. retrying.\n"); | |
e2a06704 | 1436 | s2255_fwload_start(dev); |
38f993ad | 1437 | wait_event_timeout(dev->fw_data->wait_fw, |
14d96260 DA |
1438 | ((atomic_read(&dev->fw_data->fw_state) |
1439 | == S2255_FW_SUCCESS) || | |
1440 | (atomic_read(&dev->fw_data->fw_state) | |
1441 | == S2255_FW_DISCONNECTING)), | |
38f993ad | 1442 | msecs_to_jiffies(S2255_LOAD_TIMEOUT)); |
ff7e22df DA |
1443 | /* state may have changed, re-read */ |
1444 | state = atomic_read(&dev->fw_data->fw_state); | |
14d96260 DA |
1445 | break; |
1446 | case S2255_FW_NOTLOADED: | |
1447 | case S2255_FW_LOADED_DSPWAIT: | |
38f993ad DA |
1448 | /* give S2255_LOAD_TIMEOUT time for firmware to load in case |
1449 | driver loaded and then device immediately opened */ | |
f5402007 | 1450 | pr_info("%s waiting for firmware load\n", __func__); |
38f993ad | 1451 | wait_event_timeout(dev->fw_data->wait_fw, |
14d96260 DA |
1452 | ((atomic_read(&dev->fw_data->fw_state) |
1453 | == S2255_FW_SUCCESS) || | |
1454 | (atomic_read(&dev->fw_data->fw_state) | |
1455 | == S2255_FW_DISCONNECTING)), | |
eb78deec | 1456 | msecs_to_jiffies(S2255_LOAD_TIMEOUT)); |
ff7e22df DA |
1457 | /* state may have changed, re-read */ |
1458 | state = atomic_read(&dev->fw_data->fw_state); | |
14d96260 DA |
1459 | break; |
1460 | case S2255_FW_SUCCESS: | |
1461 | default: | |
1462 | break; | |
1463 | } | |
ff7e22df DA |
1464 | /* state may have changed in above switch statement */ |
1465 | switch (state) { | |
1466 | case S2255_FW_SUCCESS: | |
1467 | break; | |
1468 | case S2255_FW_FAILED: | |
f5402007 | 1469 | pr_info("2255 firmware load failed.\n"); |
ff7e22df DA |
1470 | return -ENODEV; |
1471 | case S2255_FW_DISCONNECTING: | |
f5402007 | 1472 | pr_info("%s: disconnecting\n", __func__); |
ff7e22df DA |
1473 | return -ENODEV; |
1474 | case S2255_FW_LOADED_DSPWAIT: | |
1475 | case S2255_FW_NOTLOADED: | |
f5402007 | 1476 | pr_info("%s: firmware not loaded, please retry\n", |
1477 | __func__); | |
eb78deec DA |
1478 | /* |
1479 | * Timeout on firmware load means device unusable. | |
1480 | * Set firmware failure state. | |
1481 | * On next s2255_open the firmware will be reloaded. | |
1482 | */ | |
1483 | atomic_set(&dev->fw_data->fw_state, | |
1484 | S2255_FW_FAILED); | |
ff7e22df DA |
1485 | return -EAGAIN; |
1486 | default: | |
f5402007 | 1487 | pr_info("%s: unknown state\n", __func__); |
ff7e22df | 1488 | return -EFAULT; |
38f993ad | 1489 | } |
5e950faf | 1490 | if (!vc->configured) { |
fe85ce90 | 1491 | /* configure channel to default state */ |
5e950faf DA |
1492 | vc->fmt = &formats[0]; |
1493 | s2255_set_mode(vc, &vc->mode); | |
1494 | vc->configured = 1; | |
14d96260 | 1495 | } |
38f993ad DA |
1496 | return 0; |
1497 | } | |
1498 | ||
d62e85a0 | 1499 | static void s2255_destroy(struct s2255_dev *dev) |
38f993ad | 1500 | { |
f5402007 | 1501 | dprintk(dev, 1, "%s", __func__); |
38f993ad DA |
1502 | /* board shutdown stops the read pipe if it is running */ |
1503 | s2255_board_shutdown(dev); | |
38f993ad | 1504 | /* make sure firmware still not trying to load */ |
9f6be2bc | 1505 | del_timer_sync(&dev->timer); /* only started in .probe and .open */ |
38f993ad | 1506 | if (dev->fw_data->fw_urb) { |
38f993ad DA |
1507 | usb_kill_urb(dev->fw_data->fw_urb); |
1508 | usb_free_urb(dev->fw_data->fw_urb); | |
1509 | dev->fw_data->fw_urb = NULL; | |
1510 | } | |
3fc82fa0 | 1511 | release_firmware(dev->fw_data->fw); |
f78d92c9 DA |
1512 | kfree(dev->fw_data->pfw_data); |
1513 | kfree(dev->fw_data); | |
ff7e22df DA |
1514 | /* reset the DSP so firmware can be reloaded next time */ |
1515 | s2255_reset_dsppower(dev); | |
ff7e22df | 1516 | mutex_destroy(&dev->lock); |
38f993ad | 1517 | usb_put_dev(dev->udev); |
fe85ce90 | 1518 | v4l2_device_unregister(&dev->v4l2_dev); |
47d8c881 | 1519 | kfree(dev->cmdbuf); |
b7732a32 | 1520 | kfree(dev); |
38f993ad DA |
1521 | } |
1522 | ||
bec43661 | 1523 | static const struct v4l2_file_operations s2255_fops_v4l = { |
38f993ad DA |
1524 | .owner = THIS_MODULE, |
1525 | .open = s2255_open, | |
340a30c5 | 1526 | .release = vb2_fop_release, |
1527 | .poll = vb2_fop_poll, | |
a19a5cd7 | 1528 | .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */ |
340a30c5 | 1529 | .mmap = vb2_fop_mmap, |
1530 | .read = vb2_fop_read, | |
38f993ad DA |
1531 | }; |
1532 | ||
a399810c | 1533 | static const struct v4l2_ioctl_ops s2255_ioctl_ops = { |
38f993ad DA |
1534 | .vidioc_querycap = vidioc_querycap, |
1535 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | |
1536 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
1537 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
1538 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
340a30c5 | 1539 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
1540 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1541 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1542 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
38f993ad | 1543 | .vidioc_s_std = vidioc_s_std, |
469af77a | 1544 | .vidioc_g_std = vidioc_g_std, |
38f993ad DA |
1545 | .vidioc_enum_input = vidioc_enum_input, |
1546 | .vidioc_g_input = vidioc_g_input, | |
1547 | .vidioc_s_input = vidioc_s_input, | |
340a30c5 | 1548 | .vidioc_streamon = vb2_ioctl_streamon, |
1549 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
22b88d48 DA |
1550 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
1551 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, | |
7d853532 DA |
1552 | .vidioc_s_parm = vidioc_s_parm, |
1553 | .vidioc_g_parm = vidioc_g_parm, | |
05e5d44b | 1554 | .vidioc_enum_framesizes = vidioc_enum_framesizes, |
e6b44bc5 | 1555 | .vidioc_enum_frameintervals = vidioc_enum_frameintervals, |
44d06d82 HV |
1556 | .vidioc_log_status = v4l2_ctrl_log_status, |
1557 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
1558 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
a399810c HV |
1559 | }; |
1560 | ||
ff7e22df DA |
1561 | static void s2255_video_device_release(struct video_device *vdev) |
1562 | { | |
fe85ce90 | 1563 | struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev); |
5e950faf DA |
1564 | struct s2255_vc *vc = |
1565 | container_of(vdev, struct s2255_vc, vdev); | |
192f1e78 | 1566 | |
f5402007 | 1567 | dprintk(dev, 4, "%s, chnls: %d\n", __func__, |
fe85ce90 | 1568 | atomic_read(&dev->num_channels)); |
192f1e78 | 1569 | |
5e950faf | 1570 | v4l2_ctrl_handler_free(&vc->hdl); |
f5402007 | 1571 | |
fe85ce90 | 1572 | if (atomic_dec_and_test(&dev->num_channels)) |
d62e85a0 | 1573 | s2255_destroy(dev); |
ff7e22df DA |
1574 | return; |
1575 | } | |
1576 | ||
86844942 | 1577 | static const struct video_device template = { |
a399810c | 1578 | .name = "s2255v", |
a399810c HV |
1579 | .fops = &s2255_fops_v4l, |
1580 | .ioctl_ops = &s2255_ioctl_ops, | |
ff7e22df | 1581 | .release = s2255_video_device_release, |
38f993ad | 1582 | .tvnorms = S2255_NORMS, |
38f993ad DA |
1583 | }; |
1584 | ||
192f1e78 HV |
1585 | static const struct v4l2_ctrl_ops s2255_ctrl_ops = { |
1586 | .s_ctrl = s2255_s_ctrl, | |
1587 | }; | |
1588 | ||
1589 | static const struct v4l2_ctrl_config color_filter_ctrl = { | |
1590 | .ops = &s2255_ctrl_ops, | |
1591 | .name = "Color Filter", | |
1592 | .id = V4L2_CID_S2255_COLORFILTER, | |
1593 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1594 | .max = 1, | |
1595 | .step = 1, | |
1596 | .def = 1, | |
1597 | }; | |
1598 | ||
38f993ad DA |
1599 | static int s2255_probe_v4l(struct s2255_dev *dev) |
1600 | { | |
1601 | int ret; | |
1602 | int i; | |
1603 | int cur_nr = video_nr; | |
5e950faf | 1604 | struct s2255_vc *vc; |
340a30c5 | 1605 | struct vb2_queue *q; |
1606 | ||
65c6edb3 DA |
1607 | ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev); |
1608 | if (ret) | |
1609 | return ret; | |
38f993ad | 1610 | /* initialize all video 4 linux */ |
38f993ad DA |
1611 | /* register 4 video devices */ |
1612 | for (i = 0; i < MAX_CHANNELS; i++) { | |
5e950faf DA |
1613 | vc = &dev->vc[i]; |
1614 | INIT_LIST_HEAD(&vc->buf_list); | |
192f1e78 | 1615 | |
5e950faf DA |
1616 | v4l2_ctrl_handler_init(&vc->hdl, 6); |
1617 | v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops, | |
192f1e78 | 1618 | V4L2_CID_BRIGHTNESS, -127, 127, 1, DEF_BRIGHT); |
5e950faf | 1619 | v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops, |
192f1e78 | 1620 | V4L2_CID_CONTRAST, 0, 255, 1, DEF_CONTRAST); |
5e950faf | 1621 | v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops, |
192f1e78 | 1622 | V4L2_CID_SATURATION, 0, 255, 1, DEF_SATURATION); |
5e950faf | 1623 | v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops, |
192f1e78 | 1624 | V4L2_CID_HUE, 0, 255, 1, DEF_HUE); |
5e950faf | 1625 | vc->jpegqual_ctrl = v4l2_ctrl_new_std(&vc->hdl, |
7041dec7 HV |
1626 | &s2255_ctrl_ops, |
1627 | V4L2_CID_JPEG_COMPRESSION_QUALITY, | |
1628 | 0, 100, 1, S2255_DEF_JPEG_QUAL); | |
192f1e78 | 1629 | if (dev->dsp_fw_ver >= S2255_MIN_DSP_COLORFILTER && |
5e950faf DA |
1630 | (dev->pid != 0x2257 || vc->idx <= 1)) |
1631 | v4l2_ctrl_new_custom(&vc->hdl, &color_filter_ctrl, | |
f5402007 | 1632 | NULL); |
5e950faf DA |
1633 | if (vc->hdl.error) { |
1634 | ret = vc->hdl.error; | |
1635 | v4l2_ctrl_handler_free(&vc->hdl); | |
192f1e78 HV |
1636 | dev_err(&dev->udev->dev, "couldn't register control\n"); |
1637 | break; | |
1638 | } | |
340a30c5 | 1639 | q = &vc->vb_vidq; |
1640 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1641 | q->io_modes = VB2_MMAP | VB2_READ | VB2_USERPTR; | |
1642 | q->drv_priv = vc; | |
1643 | q->lock = &vc->vb_lock; | |
1644 | q->buf_struct_size = sizeof(struct s2255_buffer); | |
1645 | q->mem_ops = &vb2_vmalloc_memops; | |
1646 | q->ops = &s2255_video_qops; | |
ade48681 | 1647 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
340a30c5 | 1648 | ret = vb2_queue_init(q); |
1649 | if (ret != 0) { | |
1650 | dev_err(&dev->udev->dev, | |
1651 | "%s vb2_queue_init 0x%x\n", __func__, ret); | |
1652 | break; | |
1653 | } | |
1654 | /* register video devices */ | |
5e950faf | 1655 | vc->vdev = template; |
340a30c5 | 1656 | vc->vdev.queue = q; |
5e950faf DA |
1657 | vc->vdev.ctrl_handler = &vc->hdl; |
1658 | vc->vdev.lock = &dev->lock; | |
1659 | vc->vdev.v4l2_dev = &dev->v4l2_dev; | |
5e950faf | 1660 | video_set_drvdata(&vc->vdev, vc); |
38f993ad | 1661 | if (video_nr == -1) |
5e950faf | 1662 | ret = video_register_device(&vc->vdev, |
38f993ad DA |
1663 | VFL_TYPE_GRABBER, |
1664 | video_nr); | |
1665 | else | |
5e950faf | 1666 | ret = video_register_device(&vc->vdev, |
38f993ad DA |
1667 | VFL_TYPE_GRABBER, |
1668 | cur_nr + i); | |
fe85ce90 | 1669 | |
3a67b5cc | 1670 | if (ret) { |
38f993ad DA |
1671 | dev_err(&dev->udev->dev, |
1672 | "failed to register video device!\n"); | |
3a67b5cc | 1673 | break; |
38f993ad | 1674 | } |
fe85ce90 | 1675 | atomic_inc(&dev->num_channels); |
65c6edb3 | 1676 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", |
5e950faf | 1677 | video_device_node_name(&vc->vdev)); |
3a67b5cc | 1678 | |
38f993ad | 1679 | } |
f5402007 | 1680 | pr_info("Sensoray 2255 V4L driver Revision: %s\n", |
1681 | S2255_VERSION); | |
3a67b5cc | 1682 | /* if no channels registered, return error and probe will fail*/ |
fe85ce90 | 1683 | if (atomic_read(&dev->num_channels) == 0) { |
65c6edb3 | 1684 | v4l2_device_unregister(&dev->v4l2_dev); |
3a67b5cc | 1685 | return ret; |
65c6edb3 | 1686 | } |
fe85ce90 | 1687 | if (atomic_read(&dev->num_channels) != MAX_CHANNELS) |
f5402007 | 1688 | pr_warn("s2255: Not all channels available.\n"); |
3a67b5cc | 1689 | return 0; |
38f993ad DA |
1690 | } |
1691 | ||
38f993ad DA |
1692 | /* this function moves the usb stream read pipe data |
1693 | * into the system buffers. | |
1694 | * returns 0 on success, EAGAIN if more data to process( call this | |
1695 | * function again). | |
1696 | * | |
1697 | * Received frame structure: | |
14d96260 | 1698 | * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME) |
38f993ad DA |
1699 | * bytes 4-7: channel: 0-3 |
1700 | * bytes 8-11: payload size: size of the frame | |
1701 | * bytes 12-payloadsize+12: frame data | |
1702 | */ | |
1703 | static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info) | |
1704 | { | |
38f993ad DA |
1705 | char *pdest; |
1706 | u32 offset = 0; | |
14d96260 | 1707 | int bframe = 0; |
38f993ad DA |
1708 | char *psrc; |
1709 | unsigned long copy_size; | |
1710 | unsigned long size; | |
1711 | s32 idx = -1; | |
1712 | struct s2255_framei *frm; | |
1713 | unsigned char *pdata; | |
5e950faf | 1714 | struct s2255_vc *vc; |
f5402007 | 1715 | dprintk(dev, 100, "buffer to user\n"); |
5e950faf DA |
1716 | vc = &dev->vc[dev->cc]; |
1717 | idx = vc->cur_frame; | |
1718 | frm = &vc->buffer.frame[idx]; | |
14d96260 DA |
1719 | if (frm->ulState == S2255_READ_IDLE) { |
1720 | int jj; | |
1721 | unsigned int cc; | |
3fa00605 | 1722 | __le32 *pdword; /*data from dsp is little endian */ |
14d96260 DA |
1723 | int payload; |
1724 | /* search for marker codes */ | |
1725 | pdata = (unsigned char *)pipe_info->transfer_buffer; | |
3fa00605 | 1726 | pdword = (__le32 *)pdata; |
14d96260 | 1727 | for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) { |
3fa00605 | 1728 | switch (*pdword) { |
14d96260 | 1729 | case S2255_MARKER_FRAME: |
f5402007 | 1730 | dprintk(dev, 4, "marker @ offset: %d [%x %x]\n", |
1731 | jj, pdata[0], pdata[1]); | |
14d96260 DA |
1732 | offset = jj + PREFIX_SIZE; |
1733 | bframe = 1; | |
3b2a6306 | 1734 | cc = le32_to_cpu(pdword[1]); |
14d96260 | 1735 | if (cc >= MAX_CHANNELS) { |
f5402007 | 1736 | dprintk(dev, 0, |
1737 | "bad channel\n"); | |
14d96260 DA |
1738 | return -EINVAL; |
1739 | } | |
1740 | /* reverse it */ | |
1741 | dev->cc = G_chnmap[cc]; | |
5e950faf | 1742 | vc = &dev->vc[dev->cc]; |
3b2a6306 | 1743 | payload = le32_to_cpu(pdword[3]); |
5e950faf DA |
1744 | if (payload > vc->req_image_size) { |
1745 | vc->bad_payload++; | |
14d96260 DA |
1746 | /* discard the bad frame */ |
1747 | return -EINVAL; | |
1748 | } | |
5e950faf DA |
1749 | vc->pkt_size = payload; |
1750 | vc->jpg_size = le32_to_cpu(pdword[4]); | |
14d96260 DA |
1751 | break; |
1752 | case S2255_MARKER_RESPONSE: | |
fe85ce90 | 1753 | |
14d96260 DA |
1754 | pdata += DEF_USB_BLOCK; |
1755 | jj += DEF_USB_BLOCK; | |
3b2a6306 | 1756 | if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS) |
14d96260 | 1757 | break; |
3b2a6306 | 1758 | cc = G_chnmap[le32_to_cpu(pdword[1])]; |
f14a2972 | 1759 | if (cc >= MAX_CHANNELS) |
14d96260 | 1760 | break; |
5e950faf | 1761 | vc = &dev->vc[cc]; |
14d96260 | 1762 | switch (pdword[2]) { |
abce21f4 | 1763 | case S2255_RESPONSE_SETMODE: |
14d96260 DA |
1764 | /* check if channel valid */ |
1765 | /* set mode ready */ | |
5e950faf DA |
1766 | vc->setmode_ready = 1; |
1767 | wake_up(&vc->wait_setmode); | |
f5402007 | 1768 | dprintk(dev, 5, "setmode rdy %d\n", cc); |
38f993ad | 1769 | break; |
abce21f4 | 1770 | case S2255_RESPONSE_FW: |
14d96260 DA |
1771 | dev->chn_ready |= (1 << cc); |
1772 | if ((dev->chn_ready & 0x0f) != 0x0f) | |
1773 | break; | |
1774 | /* all channels ready */ | |
f5402007 | 1775 | pr_info("s2255: fw loaded\n"); |
14d96260 DA |
1776 | atomic_set(&dev->fw_data->fw_state, |
1777 | S2255_FW_SUCCESS); | |
1778 | wake_up(&dev->fw_data->wait_fw); | |
1779 | break; | |
4de39f5d | 1780 | case S2255_RESPONSE_STATUS: |
5e950faf DA |
1781 | vc->vidstatus = le32_to_cpu(pdword[3]); |
1782 | vc->vidstatus_ready = 1; | |
1783 | wake_up(&vc->wait_vidstatus); | |
f5402007 | 1784 | dprintk(dev, 5, "vstat %x chan %d\n", |
3b2a6306 | 1785 | le32_to_cpu(pdword[3]), cc); |
4de39f5d | 1786 | break; |
14d96260 | 1787 | default: |
f5402007 | 1788 | pr_info("s2255 unknown resp\n"); |
38f993ad | 1789 | } |
ec33fbd5 MCC |
1790 | pdata++; |
1791 | break; | |
14d96260 | 1792 | default: |
38f993ad | 1793 | pdata++; |
14d96260 | 1794 | break; |
38f993ad | 1795 | } |
14d96260 DA |
1796 | if (bframe) |
1797 | break; | |
1798 | } /* for */ | |
1799 | if (!bframe) | |
1800 | return -EINVAL; | |
38f993ad | 1801 | } |
5e950faf DA |
1802 | vc = &dev->vc[dev->cc]; |
1803 | idx = vc->cur_frame; | |
1804 | frm = &vc->buffer.frame[idx]; | |
14d96260 | 1805 | /* search done. now find out if should be acquiring on this channel */ |
340a30c5 | 1806 | if (!vb2_is_streaming(&vc->vb_vidq)) { |
14d96260 DA |
1807 | /* we found a frame, but this channel is turned off */ |
1808 | frm->ulState = S2255_READ_IDLE; | |
1809 | return -EINVAL; | |
38f993ad DA |
1810 | } |
1811 | ||
14d96260 DA |
1812 | if (frm->ulState == S2255_READ_IDLE) { |
1813 | frm->ulState = S2255_READ_FRAME; | |
1814 | frm->cur_size = 0; | |
38f993ad DA |
1815 | } |
1816 | ||
14d96260 DA |
1817 | /* skip the marker 512 bytes (and offset if out of sync) */ |
1818 | psrc = (u8 *)pipe_info->transfer_buffer + offset; | |
1819 | ||
1820 | ||
38f993ad | 1821 | if (frm->lpvbits == NULL) { |
f5402007 | 1822 | dprintk(dev, 1, "s2255 frame buffer == NULL.%p %p %d %d", |
38f993ad DA |
1823 | frm, dev, dev->cc, idx); |
1824 | return -ENOMEM; | |
1825 | } | |
1826 | ||
1827 | pdest = frm->lpvbits + frm->cur_size; | |
1828 | ||
14d96260 | 1829 | copy_size = (pipe_info->cur_transfer_size - offset); |
38f993ad | 1830 | |
5e950faf | 1831 | size = vc->pkt_size - PREFIX_SIZE; |
38f993ad | 1832 | |
14d96260 | 1833 | /* sanity check on pdest */ |
5e950faf | 1834 | if ((copy_size + frm->cur_size) < vc->req_image_size) |
14d96260 | 1835 | memcpy(pdest, psrc, copy_size); |
38f993ad | 1836 | |
38f993ad | 1837 | frm->cur_size += copy_size; |
f5402007 | 1838 | dprintk(dev, 4, "cur_size: %lu, size: %lu\n", frm->cur_size, size); |
14d96260 DA |
1839 | |
1840 | if (frm->cur_size >= size) { | |
f5402007 | 1841 | dprintk(dev, 2, "******[%d]Buffer[%d]full*******\n", |
fe85ce90 | 1842 | dev->cc, idx); |
5e950faf DA |
1843 | vc->last_frame = vc->cur_frame; |
1844 | vc->cur_frame++; | |
38f993ad | 1845 | /* end of system frame ring buffer, start at zero */ |
5e950faf DA |
1846 | if ((vc->cur_frame == SYS_FRAMES) || |
1847 | (vc->cur_frame == vc->buffer.dwFrames)) | |
1848 | vc->cur_frame = 0; | |
14d96260 | 1849 | /* frame ready */ |
340a30c5 | 1850 | if (vb2_is_streaming(&vc->vb_vidq)) |
5e950faf DA |
1851 | s2255_got_frame(vc, vc->jpg_size); |
1852 | vc->frame_count++; | |
14d96260 DA |
1853 | frm->ulState = S2255_READ_IDLE; |
1854 | frm->cur_size = 0; | |
1855 | ||
38f993ad DA |
1856 | } |
1857 | /* done successfully */ | |
1858 | return 0; | |
1859 | } | |
1860 | ||
1861 | static void s2255_read_video_callback(struct s2255_dev *dev, | |
1862 | struct s2255_pipeinfo *pipe_info) | |
1863 | { | |
1864 | int res; | |
f5402007 | 1865 | dprintk(dev, 50, "callback read video\n"); |
38f993ad DA |
1866 | |
1867 | if (dev->cc >= MAX_CHANNELS) { | |
1868 | dev->cc = 0; | |
1869 | dev_err(&dev->udev->dev, "invalid channel\n"); | |
1870 | return; | |
1871 | } | |
1872 | /* otherwise copy to the system buffers */ | |
1873 | res = save_frame(dev, pipe_info); | |
14d96260 | 1874 | if (res != 0) |
f5402007 | 1875 | dprintk(dev, 4, "s2255: read callback failed\n"); |
38f993ad | 1876 | |
f5402007 | 1877 | dprintk(dev, 50, "callback read video done\n"); |
38f993ad DA |
1878 | return; |
1879 | } | |
1880 | ||
1881 | static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request, | |
1882 | u16 Index, u16 Value, void *TransferBuffer, | |
1883 | s32 TransferBufferLength, int bOut) | |
1884 | { | |
1885 | int r; | |
db65c49e MCC |
1886 | unsigned char *buf; |
1887 | ||
1888 | buf = kmalloc(TransferBufferLength, GFP_KERNEL); | |
1889 | if (!buf) | |
1890 | return -ENOMEM; | |
1891 | ||
38f993ad DA |
1892 | if (!bOut) { |
1893 | r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), | |
1894 | Request, | |
1895 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | | |
1896 | USB_DIR_IN, | |
db65c49e | 1897 | Value, Index, buf, |
38f993ad | 1898 | TransferBufferLength, HZ * 5); |
db65c49e MCC |
1899 | |
1900 | if (r >= 0) | |
1901 | memcpy(TransferBuffer, buf, TransferBufferLength); | |
38f993ad | 1902 | } else { |
db65c49e | 1903 | memcpy(buf, TransferBuffer, TransferBufferLength); |
38f993ad DA |
1904 | r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), |
1905 | Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
db65c49e | 1906 | Value, Index, buf, |
38f993ad DA |
1907 | TransferBufferLength, HZ * 5); |
1908 | } | |
db65c49e | 1909 | kfree(buf); |
38f993ad DA |
1910 | return r; |
1911 | } | |
1912 | ||
1913 | /* | |
1914 | * retrieve FX2 firmware version. future use. | |
1915 | * @param dev pointer to device extension | |
1916 | * @return -1 for fail, else returns firmware version as an int(16 bits) | |
1917 | */ | |
1918 | static int s2255_get_fx2fw(struct s2255_dev *dev) | |
1919 | { | |
1920 | int fw; | |
1921 | int ret; | |
1922 | unsigned char transBuffer[64]; | |
1923 | ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2, | |
1924 | S2255_VR_IN); | |
1925 | if (ret < 0) | |
f5402007 | 1926 | dprintk(dev, 2, "get fw error: %x\n", ret); |
38f993ad | 1927 | fw = transBuffer[0] + (transBuffer[1] << 8); |
f5402007 | 1928 | dprintk(dev, 2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]); |
38f993ad DA |
1929 | return fw; |
1930 | } | |
1931 | ||
1932 | /* | |
1933 | * Create the system ring buffer to copy frames into from the | |
1934 | * usb read pipe. | |
1935 | */ | |
5e950faf | 1936 | static int s2255_create_sys_buffers(struct s2255_vc *vc) |
38f993ad DA |
1937 | { |
1938 | unsigned long i; | |
1939 | unsigned long reqsize; | |
5e950faf | 1940 | vc->buffer.dwFrames = SYS_FRAMES; |
38f993ad DA |
1941 | /* always allocate maximum size(PAL) for system buffers */ |
1942 | reqsize = SYS_FRAMES_MAXSIZE; | |
1943 | ||
1944 | if (reqsize > SYS_FRAMES_MAXSIZE) | |
1945 | reqsize = SYS_FRAMES_MAXSIZE; | |
1946 | ||
1947 | for (i = 0; i < SYS_FRAMES; i++) { | |
1948 | /* allocate the frames */ | |
5e950faf DA |
1949 | vc->buffer.frame[i].lpvbits = vmalloc(reqsize); |
1950 | vc->buffer.frame[i].size = reqsize; | |
1951 | if (vc->buffer.frame[i].lpvbits == NULL) { | |
f5402007 | 1952 | pr_info("out of memory. using less frames\n"); |
5e950faf | 1953 | vc->buffer.dwFrames = i; |
38f993ad DA |
1954 | break; |
1955 | } | |
1956 | } | |
1957 | ||
1958 | /* make sure internal states are set */ | |
1959 | for (i = 0; i < SYS_FRAMES; i++) { | |
5e950faf DA |
1960 | vc->buffer.frame[i].ulState = 0; |
1961 | vc->buffer.frame[i].cur_size = 0; | |
38f993ad DA |
1962 | } |
1963 | ||
5e950faf DA |
1964 | vc->cur_frame = 0; |
1965 | vc->last_frame = -1; | |
38f993ad DA |
1966 | return 0; |
1967 | } | |
1968 | ||
5e950faf | 1969 | static int s2255_release_sys_buffers(struct s2255_vc *vc) |
38f993ad DA |
1970 | { |
1971 | unsigned long i; | |
38f993ad | 1972 | for (i = 0; i < SYS_FRAMES; i++) { |
83f56f7c | 1973 | vfree(vc->buffer.frame[i].lpvbits); |
5e950faf | 1974 | vc->buffer.frame[i].lpvbits = NULL; |
38f993ad DA |
1975 | } |
1976 | return 0; | |
1977 | } | |
1978 | ||
1979 | static int s2255_board_init(struct s2255_dev *dev) | |
1980 | { | |
38f993ad DA |
1981 | struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT; |
1982 | int fw_ver; | |
ab85c6a3 DA |
1983 | int j; |
1984 | struct s2255_pipeinfo *pipe = &dev->pipe; | |
f5402007 | 1985 | dprintk(dev, 4, "board init: %p", dev); |
ab85c6a3 DA |
1986 | memset(pipe, 0, sizeof(*pipe)); |
1987 | pipe->dev = dev; | |
1988 | pipe->cur_transfer_size = S2255_USB_XFER_SIZE; | |
1989 | pipe->max_transfer_size = S2255_USB_XFER_SIZE; | |
1990 | ||
1991 | pipe->transfer_buffer = kzalloc(pipe->max_transfer_size, | |
1992 | GFP_KERNEL); | |
1993 | if (pipe->transfer_buffer == NULL) { | |
f5402007 | 1994 | dprintk(dev, 1, "out of memory!\n"); |
ab85c6a3 | 1995 | return -ENOMEM; |
38f993ad | 1996 | } |
38f993ad DA |
1997 | /* query the firmware */ |
1998 | fw_ver = s2255_get_fx2fw(dev); | |
1999 | ||
f5402007 | 2000 | pr_info("s2255: usb firmware version %d.%d\n", |
2001 | (fw_ver >> 8) & 0xff, | |
2002 | fw_ver & 0xff); | |
abce21f4 DA |
2003 | |
2004 | if (fw_ver < S2255_CUR_USB_FWVER) | |
f5402007 | 2005 | pr_info("s2255: newer USB firmware available\n"); |
38f993ad DA |
2006 | |
2007 | for (j = 0; j < MAX_CHANNELS; j++) { | |
5e950faf | 2008 | struct s2255_vc *vc = &dev->vc[j]; |
5e950faf | 2009 | vc->mode = mode_def; |
5a34d9df | 2010 | if (dev->pid == 0x2257 && j > 1) |
5e950faf DA |
2011 | vc->mode.color |= (1 << 16); |
2012 | vc->jpegqual = S2255_DEF_JPEG_QUAL; | |
2013 | vc->width = LINE_SZ_4CIFS_NTSC; | |
2014 | vc->height = NUM_LINES_4CIFS_NTSC * 2; | |
2015 | vc->std = V4L2_STD_NTSC_M; | |
2016 | vc->fmt = &formats[0]; | |
2017 | vc->mode.restart = 1; | |
2018 | vc->req_image_size = get_transfer_size(&mode_def); | |
2019 | vc->frame_count = 0; | |
38f993ad | 2020 | /* create the system buffers */ |
5e950faf | 2021 | s2255_create_sys_buffers(vc); |
38f993ad DA |
2022 | } |
2023 | /* start read pipe */ | |
2024 | s2255_start_readpipe(dev); | |
f5402007 | 2025 | dprintk(dev, 1, "%s: success\n", __func__); |
38f993ad DA |
2026 | return 0; |
2027 | } | |
2028 | ||
2029 | static int s2255_board_shutdown(struct s2255_dev *dev) | |
2030 | { | |
2031 | u32 i; | |
f5402007 | 2032 | dprintk(dev, 1, "%s: dev: %p", __func__, dev); |
38f993ad DA |
2033 | |
2034 | for (i = 0; i < MAX_CHANNELS; i++) { | |
340a30c5 | 2035 | if (vb2_is_streaming(&dev->vc[i].vb_vidq)) |
5e950faf | 2036 | s2255_stop_acquire(&dev->vc[i]); |
38f993ad | 2037 | } |
38f993ad | 2038 | s2255_stop_readpipe(dev); |
38f993ad | 2039 | for (i = 0; i < MAX_CHANNELS; i++) |
5e950faf | 2040 | s2255_release_sys_buffers(&dev->vc[i]); |
ab85c6a3 DA |
2041 | /* release transfer buffer */ |
2042 | kfree(dev->pipe.transfer_buffer); | |
38f993ad DA |
2043 | return 0; |
2044 | } | |
2045 | ||
2046 | static void read_pipe_completion(struct urb *purb) | |
2047 | { | |
2048 | struct s2255_pipeinfo *pipe_info; | |
2049 | struct s2255_dev *dev; | |
2050 | int status; | |
2051 | int pipe; | |
38f993ad | 2052 | pipe_info = purb->context; |
38f993ad | 2053 | if (pipe_info == NULL) { |
be9ed511 | 2054 | dev_err(&purb->dev->dev, "no context!\n"); |
38f993ad DA |
2055 | return; |
2056 | } | |
38f993ad DA |
2057 | dev = pipe_info->dev; |
2058 | if (dev == NULL) { | |
be9ed511 | 2059 | dev_err(&purb->dev->dev, "no context!\n"); |
38f993ad DA |
2060 | return; |
2061 | } | |
2062 | status = purb->status; | |
b02064ca DA |
2063 | /* if shutting down, do not resubmit, exit immediately */ |
2064 | if (status == -ESHUTDOWN) { | |
f5402007 | 2065 | dprintk(dev, 2, "%s: err shutdown\n", __func__); |
b02064ca | 2066 | pipe_info->err_count++; |
38f993ad DA |
2067 | return; |
2068 | } | |
2069 | ||
2070 | if (pipe_info->state == 0) { | |
f5402007 | 2071 | dprintk(dev, 2, "%s: exiting USB pipe", __func__); |
38f993ad DA |
2072 | return; |
2073 | } | |
2074 | ||
b02064ca DA |
2075 | if (status == 0) |
2076 | s2255_read_video_callback(dev, pipe_info); | |
2077 | else { | |
2078 | pipe_info->err_count++; | |
f5402007 | 2079 | dprintk(dev, 1, "%s: failed URB %d\n", __func__, status); |
b02064ca | 2080 | } |
38f993ad | 2081 | |
38f993ad DA |
2082 | pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint); |
2083 | /* reuse urb */ | |
2084 | usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev, | |
2085 | pipe, | |
2086 | pipe_info->transfer_buffer, | |
2087 | pipe_info->cur_transfer_size, | |
2088 | read_pipe_completion, pipe_info); | |
2089 | ||
2090 | if (pipe_info->state != 0) { | |
f5402007 | 2091 | if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC)) |
38f993ad | 2092 | dev_err(&dev->udev->dev, "error submitting urb\n"); |
38f993ad | 2093 | } else { |
f5402007 | 2094 | dprintk(dev, 2, "%s :complete state 0\n", __func__); |
38f993ad DA |
2095 | } |
2096 | return; | |
2097 | } | |
2098 | ||
2099 | static int s2255_start_readpipe(struct s2255_dev *dev) | |
2100 | { | |
2101 | int pipe; | |
2102 | int retval; | |
ab85c6a3 | 2103 | struct s2255_pipeinfo *pipe_info = &dev->pipe; |
38f993ad | 2104 | pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint); |
f5402007 | 2105 | dprintk(dev, 2, "%s: IN %d\n", __func__, dev->read_endpoint); |
ab85c6a3 DA |
2106 | pipe_info->state = 1; |
2107 | pipe_info->err_count = 0; | |
2108 | pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL); | |
fc56da79 | 2109 | if (!pipe_info->stream_urb) |
ab85c6a3 | 2110 | return -ENOMEM; |
ab85c6a3 DA |
2111 | /* transfer buffer allocated in board_init */ |
2112 | usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev, | |
2113 | pipe, | |
2114 | pipe_info->transfer_buffer, | |
2115 | pipe_info->cur_transfer_size, | |
2116 | read_pipe_completion, pipe_info); | |
ab85c6a3 DA |
2117 | retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL); |
2118 | if (retval) { | |
f5402007 | 2119 | pr_err("s2255: start read pipe failed\n"); |
ab85c6a3 DA |
2120 | return retval; |
2121 | } | |
38f993ad DA |
2122 | return 0; |
2123 | } | |
2124 | ||
2125 | /* starts acquisition process */ | |
5e950faf | 2126 | static int s2255_start_acquire(struct s2255_vc *vc) |
38f993ad | 2127 | { |
38f993ad DA |
2128 | int res; |
2129 | unsigned long chn_rev; | |
2130 | int j; | |
5e950faf | 2131 | struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev); |
47d8c881 | 2132 | __le32 *buffer = dev->cmdbuf; |
38f993ad | 2133 | |
47d8c881 DA |
2134 | mutex_lock(&dev->cmdlock); |
2135 | chn_rev = G_chnmap[vc->idx]; | |
5e950faf DA |
2136 | vc->last_frame = -1; |
2137 | vc->bad_payload = 0; | |
2138 | vc->cur_frame = 0; | |
38f993ad | 2139 | for (j = 0; j < SYS_FRAMES; j++) { |
5e950faf DA |
2140 | vc->buffer.frame[j].ulState = 0; |
2141 | vc->buffer.frame[j].cur_size = 0; | |
38f993ad DA |
2142 | } |
2143 | ||
2144 | /* send the start command */ | |
47d8c881 DA |
2145 | buffer[0] = IN_DATA_TOKEN; |
2146 | buffer[1] = (__le32) cpu_to_le32(chn_rev); | |
2147 | buffer[2] = CMD_START; | |
38f993ad DA |
2148 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
2149 | if (res != 0) | |
2150 | dev_err(&dev->udev->dev, "CMD_START error\n"); | |
2151 | ||
5e950faf | 2152 | dprintk(dev, 2, "start acquire exit[%d] %d\n", vc->idx, res); |
47d8c881 | 2153 | mutex_unlock(&dev->cmdlock); |
6a5b63b3 | 2154 | return res; |
38f993ad DA |
2155 | } |
2156 | ||
5e950faf | 2157 | static int s2255_stop_acquire(struct s2255_vc *vc) |
38f993ad | 2158 | { |
38f993ad DA |
2159 | int res; |
2160 | unsigned long chn_rev; | |
5e950faf | 2161 | struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev); |
47d8c881 DA |
2162 | __le32 *buffer = dev->cmdbuf; |
2163 | ||
2164 | mutex_lock(&dev->cmdlock); | |
5e950faf | 2165 | chn_rev = G_chnmap[vc->idx]; |
38f993ad | 2166 | /* send the stop command */ |
47d8c881 DA |
2167 | buffer[0] = IN_DATA_TOKEN; |
2168 | buffer[1] = (__le32) cpu_to_le32(chn_rev); | |
2169 | buffer[2] = CMD_STOP; | |
2170 | ||
38f993ad | 2171 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
38f993ad DA |
2172 | if (res != 0) |
2173 | dev_err(&dev->udev->dev, "CMD_STOP error\n"); | |
47d8c881 | 2174 | |
5e950faf | 2175 | dprintk(dev, 4, "%s: chn %d, res %d\n", __func__, vc->idx, res); |
47d8c881 | 2176 | mutex_unlock(&dev->cmdlock); |
14d96260 | 2177 | return res; |
38f993ad DA |
2178 | } |
2179 | ||
2180 | static void s2255_stop_readpipe(struct s2255_dev *dev) | |
2181 | { | |
ab85c6a3 | 2182 | struct s2255_pipeinfo *pipe = &dev->pipe; |
8b661b50 | 2183 | |
ab85c6a3 DA |
2184 | pipe->state = 0; |
2185 | if (pipe->stream_urb) { | |
2186 | /* cancel urb */ | |
2187 | usb_kill_urb(pipe->stream_urb); | |
2188 | usb_free_urb(pipe->stream_urb); | |
2189 | pipe->stream_urb = NULL; | |
38f993ad | 2190 | } |
f5402007 | 2191 | dprintk(dev, 4, "%s", __func__); |
38f993ad DA |
2192 | return; |
2193 | } | |
2194 | ||
e2a06704 | 2195 | static void s2255_fwload_start(struct s2255_dev *dev) |
38f993ad | 2196 | { |
e2a06704 | 2197 | s2255_reset_dsppower(dev); |
38f993ad DA |
2198 | dev->fw_data->fw_size = dev->fw_data->fw->size; |
2199 | atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED); | |
2200 | memcpy(dev->fw_data->pfw_data, | |
2201 | dev->fw_data->fw->data, CHUNK_SIZE); | |
2202 | dev->fw_data->fw_loaded = CHUNK_SIZE; | |
2203 | usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev, | |
2204 | usb_sndbulkpipe(dev->udev, 2), | |
2205 | dev->fw_data->pfw_data, | |
2206 | CHUNK_SIZE, s2255_fwchunk_complete, | |
2207 | dev->fw_data); | |
2208 | mod_timer(&dev->timer, jiffies + HZ); | |
2209 | } | |
2210 | ||
2211 | /* standard usb probe function */ | |
2212 | static int s2255_probe(struct usb_interface *interface, | |
2213 | const struct usb_device_id *id) | |
2214 | { | |
2215 | struct s2255_dev *dev = NULL; | |
2216 | struct usb_host_interface *iface_desc; | |
2217 | struct usb_endpoint_descriptor *endpoint; | |
2218 | int i; | |
2219 | int retval = -ENOMEM; | |
14d96260 DA |
2220 | __le32 *pdata; |
2221 | int fw_size; | |
47d8c881 | 2222 | |
38f993ad DA |
2223 | /* allocate memory for our device state and initialize it to zero */ |
2224 | dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL); | |
2225 | if (dev == NULL) { | |
be9ed511 | 2226 | s2255_dev_err(&interface->dev, "out of memory\n"); |
ff7e22df | 2227 | return -ENOMEM; |
38f993ad | 2228 | } |
47d8c881 DA |
2229 | |
2230 | dev->cmdbuf = kzalloc(S2255_CMDBUF_SIZE, GFP_KERNEL); | |
2231 | if (dev->cmdbuf == NULL) { | |
2232 | s2255_dev_err(&interface->dev, "out of memory\n"); | |
e21c94e7 | 2233 | goto errorFWDATA1; |
47d8c881 DA |
2234 | } |
2235 | ||
fe85ce90 | 2236 | atomic_set(&dev->num_channels, 0); |
ff3ec57d | 2237 | dev->pid = id->idProduct; |
38f993ad DA |
2238 | dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL); |
2239 | if (!dev->fw_data) | |
ff7e22df | 2240 | goto errorFWDATA1; |
38f993ad | 2241 | mutex_init(&dev->lock); |
47d8c881 | 2242 | mutex_init(&dev->cmdlock); |
38f993ad DA |
2243 | /* grab usb_device and save it */ |
2244 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); | |
2245 | if (dev->udev == NULL) { | |
2246 | dev_err(&interface->dev, "null usb device\n"); | |
2247 | retval = -ENODEV; | |
ff7e22df | 2248 | goto errorUDEV; |
38f993ad | 2249 | } |
f5402007 | 2250 | dev_dbg(&interface->dev, "dev: %p, udev %p interface %p\n", |
2251 | dev, dev->udev, interface); | |
38f993ad DA |
2252 | dev->interface = interface; |
2253 | /* set up the endpoint information */ | |
2254 | iface_desc = interface->cur_altsetting; | |
f5402007 | 2255 | dev_dbg(&interface->dev, "num EP: %d\n", |
2256 | iface_desc->desc.bNumEndpoints); | |
38f993ad DA |
2257 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
2258 | endpoint = &iface_desc->endpoint[i].desc; | |
2259 | if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) { | |
2260 | /* we found the bulk in endpoint */ | |
2261 | dev->read_endpoint = endpoint->bEndpointAddress; | |
2262 | } | |
2263 | } | |
2264 | ||
2265 | if (!dev->read_endpoint) { | |
be9ed511 | 2266 | dev_err(&interface->dev, "Could not find bulk-in endpoint\n"); |
ff7e22df | 2267 | goto errorEP; |
38f993ad | 2268 | } |
74ee0477 | 2269 | timer_setup(&dev->timer, s2255_timer, 0); |
38f993ad | 2270 | init_waitqueue_head(&dev->fw_data->wait_fw); |
4de39f5d | 2271 | for (i = 0; i < MAX_CHANNELS; i++) { |
5e950faf DA |
2272 | struct s2255_vc *vc = &dev->vc[i]; |
2273 | vc->idx = i; | |
2274 | vc->dev = dev; | |
2275 | init_waitqueue_head(&vc->wait_setmode); | |
2276 | init_waitqueue_head(&vc->wait_vidstatus); | |
340a30c5 | 2277 | spin_lock_init(&vc->qlock); |
2278 | mutex_init(&vc->vb_lock); | |
4de39f5d | 2279 | } |
38f993ad DA |
2280 | |
2281 | dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL); | |
fc56da79 | 2282 | if (!dev->fw_data->fw_urb) |
ff7e22df | 2283 | goto errorFWURB; |
ff7e22df | 2284 | |
38f993ad DA |
2285 | dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL); |
2286 | if (!dev->fw_data->pfw_data) { | |
2287 | dev_err(&interface->dev, "out of memory!\n"); | |
ff7e22df | 2288 | goto errorFWDATA2; |
38f993ad DA |
2289 | } |
2290 | /* load the first chunk */ | |
2291 | if (request_firmware(&dev->fw_data->fw, | |
2292 | FIRMWARE_FILE_NAME, &dev->udev->dev)) { | |
f5402007 | 2293 | dev_err(&interface->dev, "sensoray 2255 failed to get firmware\n"); |
ff7e22df | 2294 | goto errorREQFW; |
38f993ad | 2295 | } |
14d96260 DA |
2296 | /* check the firmware is valid */ |
2297 | fw_size = dev->fw_data->fw->size; | |
2298 | pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8]; | |
38f993ad | 2299 | |
14d96260 | 2300 | if (*pdata != S2255_FW_MARKER) { |
f5402007 | 2301 | dev_err(&interface->dev, "Firmware invalid.\n"); |
14d96260 | 2302 | retval = -ENODEV; |
ff7e22df | 2303 | goto errorFWMARKER; |
14d96260 DA |
2304 | } else { |
2305 | /* make sure firmware is the latest */ | |
2306 | __le32 *pRel; | |
2307 | pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4]; | |
f5402007 | 2308 | pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel)); |
3b2a6306 DC |
2309 | dev->dsp_fw_ver = le32_to_cpu(*pRel); |
2310 | if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER) | |
f5402007 | 2311 | pr_info("s2255: f2255usb.bin out of date.\n"); |
3b2a6306 DC |
2312 | if (dev->pid == 0x2257 && |
2313 | dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER) | |
f5402007 | 2314 | pr_warn("2257 needs firmware %d or above.\n", |
2315 | S2255_MIN_DSP_COLORFILTER); | |
14d96260 | 2316 | } |
14d96260 | 2317 | usb_reset_device(dev->udev); |
38f993ad | 2318 | /* load 2255 board specific */ |
abce21f4 DA |
2319 | retval = s2255_board_init(dev); |
2320 | if (retval) | |
ff7e22df | 2321 | goto errorBOARDINIT; |
e2a06704 | 2322 | s2255_fwload_start(dev); |
ff7e22df DA |
2323 | /* loads v4l specific */ |
2324 | retval = s2255_probe_v4l(dev); | |
2325 | if (retval) | |
3a67b5cc | 2326 | goto errorBOARDINIT; |
38f993ad DA |
2327 | dev_info(&interface->dev, "Sensoray 2255 detected\n"); |
2328 | return 0; | |
ff7e22df DA |
2329 | errorBOARDINIT: |
2330 | s2255_board_shutdown(dev); | |
2331 | errorFWMARKER: | |
2332 | release_firmware(dev->fw_data->fw); | |
2333 | errorREQFW: | |
2334 | kfree(dev->fw_data->pfw_data); | |
2335 | errorFWDATA2: | |
2336 | usb_free_urb(dev->fw_data->fw_urb); | |
2337 | errorFWURB: | |
9f6be2bc | 2338 | del_timer_sync(&dev->timer); |
ff7e22df DA |
2339 | errorEP: |
2340 | usb_put_dev(dev->udev); | |
2341 | errorUDEV: | |
2342 | kfree(dev->fw_data); | |
ff7e22df DA |
2343 | mutex_destroy(&dev->lock); |
2344 | errorFWDATA1: | |
47d8c881 | 2345 | kfree(dev->cmdbuf); |
ff7e22df | 2346 | kfree(dev); |
f5402007 | 2347 | pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval); |
38f993ad DA |
2348 | return retval; |
2349 | } | |
2350 | ||
2351 | /* disconnect routine. when board is removed physically or with rmmod */ | |
2352 | static void s2255_disconnect(struct usb_interface *interface) | |
2353 | { | |
65c6edb3 | 2354 | struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface)); |
14d96260 | 2355 | int i; |
fe85ce90 | 2356 | int channels = atomic_read(&dev->num_channels); |
a19a5cd7 | 2357 | mutex_lock(&dev->lock); |
fe85ce90 | 2358 | v4l2_device_disconnect(&dev->v4l2_dev); |
a19a5cd7 | 2359 | mutex_unlock(&dev->lock); |
d62e85a0 | 2360 | /*see comments in the uvc_driver.c usb disconnect function */ |
fe85ce90 | 2361 | atomic_inc(&dev->num_channels); |
ff7e22df | 2362 | /* unregister each video device. */ |
fe85ce90 | 2363 | for (i = 0; i < channels; i++) |
5e950faf | 2364 | video_unregister_device(&dev->vc[i].vdev); |
ff7e22df | 2365 | /* wake up any of our timers */ |
14d96260 DA |
2366 | atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING); |
2367 | wake_up(&dev->fw_data->wait_fw); | |
2368 | for (i = 0; i < MAX_CHANNELS; i++) { | |
5e950faf DA |
2369 | dev->vc[i].setmode_ready = 1; |
2370 | wake_up(&dev->vc[i].wait_setmode); | |
2371 | dev->vc[i].vidstatus_ready = 1; | |
2372 | wake_up(&dev->vc[i].wait_vidstatus); | |
14d96260 | 2373 | } |
fe85ce90 | 2374 | if (atomic_dec_and_test(&dev->num_channels)) |
d62e85a0 | 2375 | s2255_destroy(dev); |
ff7e22df | 2376 | dev_info(&interface->dev, "%s\n", __func__); |
38f993ad DA |
2377 | } |
2378 | ||
2379 | static struct usb_driver s2255_driver = { | |
be9ed511 | 2380 | .name = S2255_DRIVER_NAME, |
38f993ad DA |
2381 | .probe = s2255_probe, |
2382 | .disconnect = s2255_disconnect, | |
2383 | .id_table = s2255_table, | |
2384 | }; | |
2385 | ||
ecb3b2b3 | 2386 | module_usb_driver(s2255_driver); |
38f993ad DA |
2387 | |
2388 | MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver"); | |
2389 | MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)"); | |
2390 | MODULE_LICENSE("GPL"); | |
64dc3c1a | 2391 | MODULE_VERSION(S2255_VERSION); |
1bec982d | 2392 | MODULE_FIRMWARE(FIRMWARE_FILE_NAME); |