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