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