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