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