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