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