2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2010 by Sensoray Company Inc.
7 * Some video buffer code based on vivi driver:
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
16 * Example maximum bandwidth utilization:
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
20 * -full or half size Grey scale: all 4 channels at once
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
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.
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.
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.
44 #include <linux/module.h>
45 #include <linux/firmware.h>
46 #include <linux/kernel.h>
47 #include <linux/mutex.h>
48 #include <linux/slab.h>
49 #include <linux/videodev2.h>
50 #include <linux/version.h>
52 #include <linux/smp_lock.h>
53 #include <media/videobuf-vmalloc.h>
54 #include <media/v4l2-common.h>
55 #include <media/v4l2-device.h>
56 #include <media/v4l2-ioctl.h>
57 #include <linux/vmalloc.h>
58 #include <linux/usb.h>
60 #define S2255_MAJOR_VERSION 1
61 #define S2255_MINOR_VERSION 20
62 #define S2255_RELEASE 0
63 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
64 S2255_MINOR_VERSION, \
66 #define FIRMWARE_FILE_NAME "f2255usb.bin"
68 /* default JPEG quality */
69 #define S2255_DEF_JPEG_QUAL 50
70 /* vendor request in */
72 /* vendor request out */
73 #define S2255_VR_OUT 1
75 #define S2255_VR_FW 0x30
76 /* USB endpoint number for configuring the device */
77 #define S2255_CONFIG_EP 2
78 /* maximum time for DSP to start responding after last FW word loaded(ms) */
79 #define S2255_DSP_BOOTTIME 800
80 /* maximum time to wait for firmware to load (ms) */
81 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
82 #define S2255_DEF_BUFS 16
83 #define S2255_SETMODE_TIMEOUT 500
84 #define S2255_VIDSTATUS_TIMEOUT 350
85 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
86 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
87 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
88 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
89 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
90 #define S2255_USB_XFER_SIZE (16 * 1024)
91 #define MAX_CHANNELS 4
93 /* maximum size is PAL full size plus room for the marker header(s) */
94 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
95 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
96 #define LINE_SZ_4CIFS_NTSC 640
97 #define LINE_SZ_2CIFS_NTSC 640
98 #define LINE_SZ_1CIFS_NTSC 320
99 #define LINE_SZ_4CIFS_PAL 704
100 #define LINE_SZ_2CIFS_PAL 704
101 #define LINE_SZ_1CIFS_PAL 352
102 #define NUM_LINES_4CIFS_NTSC 240
103 #define NUM_LINES_2CIFS_NTSC 240
104 #define NUM_LINES_1CIFS_NTSC 240
105 #define NUM_LINES_4CIFS_PAL 288
106 #define NUM_LINES_2CIFS_PAL 288
107 #define NUM_LINES_1CIFS_PAL 288
108 #define LINE_SZ_DEF 640
109 #define NUM_LINES_DEF 240
112 /* predefined settings */
113 #define FORMAT_NTSC 1
116 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
117 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
118 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
119 /* SCALE_4CIFSI is the 2 fields interpolated into one */
120 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
122 #define COLOR_YUVPL 1 /* YUV planar */
123 #define COLOR_YUVPK 2 /* YUV packed */
124 #define COLOR_Y8 4 /* monochrome */
125 #define COLOR_JPG 5 /* JPEG */
127 #define MASK_COLOR 0x000000ff
128 #define MASK_JPG_QUALITY 0x0000ff00
129 #define MASK_INPUT_TYPE 0x000f0000
130 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
131 #define FDEC_1 1 /* capture every frame. default */
132 #define FDEC_2 2 /* capture every 2nd frame */
133 #define FDEC_3 3 /* capture every 3rd frame */
134 #define FDEC_5 5 /* capture every 5th frame */
136 /*-------------------------------------------------------
137 * Default mode parameters.
138 *-------------------------------------------------------*/
139 #define DEF_SCALE SCALE_4CIFS
140 #define DEF_COLOR COLOR_YUVPL
141 #define DEF_FDEC FDEC_1
143 #define DEF_CONTRAST 0x5c
144 #define DEF_SATURATION 0x80
147 /* usb config commands */
148 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
149 #define CMD_2255 cpu_to_le32(0xc2255000)
150 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
151 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
152 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
153 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
156 u32 format; /* input video format (NTSC, PAL) */
157 u32 scale; /* output video scale */
158 u32 color; /* output video color format */
159 u32 fdec; /* frame decimation */
160 u32 bright; /* brightness */
161 u32 contrast; /* contrast */
162 u32 saturation; /* saturation */
163 u32 hue; /* hue (NTSC only)*/
164 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
165 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
166 u32 restart; /* if DSP requires restart */
170 #define S2255_READ_IDLE 0
171 #define S2255_READ_FRAME 1
173 /* frame structure */
174 struct s2255_framei {
176 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
177 void *lpvbits; /* image data */
178 unsigned long cur_size; /* current data copied to it */
181 /* image buffer structure */
182 struct s2255_bufferi {
183 unsigned long dwFrames; /* number of frames in buffer */
184 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
187 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
188 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
189 DEF_HUE, 0, DEF_USB_BLOCK, 0}
191 struct s2255_dmaqueue {
192 struct list_head active;
193 struct s2255_dev *dev;
197 /* for firmware loading, fw_state */
198 #define S2255_FW_NOTLOADED 0
199 #define S2255_FW_LOADED_DSPWAIT 1
200 #define S2255_FW_SUCCESS 2
201 #define S2255_FW_FAILED 3
202 #define S2255_FW_DISCONNECTING 4
203 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
204 /* 2255 read states */
205 #define S2255_READ_IDLE 0
206 #define S2255_READ_FRAME 1
213 wait_queue_head_t wait_fw;
214 const struct firmware *fw;
217 struct s2255_pipeinfo {
218 u32 max_transfer_size;
219 u32 cur_transfer_size;
223 void *dev; /* back pointer to s2255_dev struct*/
228 struct s2255_fmt; /*forward declaration */
231 struct video_device vdev[MAX_CHANNELS];
232 struct v4l2_device v4l2_dev;
233 atomic_t channels; /* number of channels registered */
236 struct mutex open_lock;
237 int resources[MAX_CHANNELS];
238 struct usb_device *udev;
239 struct usb_interface *interface;
242 struct s2255_dmaqueue vidq[MAX_CHANNELS];
243 struct timer_list timer;
244 struct s2255_fw *fw_data;
245 struct s2255_pipeinfo pipe;
246 struct s2255_bufferi buffer[MAX_CHANNELS];
247 struct s2255_mode mode[MAX_CHANNELS];
248 /* jpeg compression */
249 struct v4l2_jpegcompression jc[MAX_CHANNELS];
250 /* capture parameters (for high quality mode full size) */
251 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
252 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
253 int cur_frame[MAX_CHANNELS];
254 int last_frame[MAX_CHANNELS];
255 u32 cc; /* current channel */
256 int b_acquire[MAX_CHANNELS];
257 /* allocated image size */
258 unsigned long req_image_size[MAX_CHANNELS];
259 /* received packet size */
260 unsigned long pkt_size[MAX_CHANNELS];
261 int bad_payload[MAX_CHANNELS];
262 unsigned long frame_count[MAX_CHANNELS];
265 int jpg_size[MAX_CHANNELS];
266 /* if channel configured to default state */
267 int chn_configured[MAX_CHANNELS];
268 wait_queue_head_t wait_setmode[MAX_CHANNELS];
269 int setmode_ready[MAX_CHANNELS];
270 /* video status items */
271 int vidstatus[MAX_CHANNELS];
272 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
273 int vidstatus_ready[MAX_CHANNELS];
276 /* dsp firmware version (f2255usb.bin) */
278 u16 pid; /* product id */
281 static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
283 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
292 /* buffer for one video frame */
293 struct s2255_buffer {
294 /* common v4l buffer stuff -- must be first */
295 struct videobuf_buffer vb;
296 const struct s2255_fmt *fmt;
300 struct s2255_dev *dev;
301 const struct s2255_fmt *fmt;
304 struct videobuf_queue vb_vidq;
305 enum v4l2_buf_type type;
307 /* mode below is the desired mode.
308 mode in s2255_dev is the current mode that was last set */
309 struct s2255_mode mode;
310 int resources[MAX_CHANNELS];
313 /* current cypress EEPROM firmware version */
314 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
315 /* current DSP FW version */
316 #define S2255_CUR_DSP_FWVER 8
317 /* Need DSP version 5+ for video status feature */
318 #define S2255_MIN_DSP_STATUS 5
319 #define S2255_MIN_DSP_COLORFILTER 8
320 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
322 /* private V4L2 controls */
325 * The following chart displays how COLORFILTER should be set
326 * =========================================================
327 * = fourcc = COLORFILTER =
328 * = ===============================
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, =
337 * =========================================================
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
345 * COLORFILTER is different from "color killer" and "color effects"
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)
352 /* frame prefix size (sent once every frame) */
353 #define PREFIX_SIZE 512
355 /* Channels on box are in reverse order */
356 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
359 static int *s2255_debug = &debug;
361 static int s2255_start_readpipe(struct s2255_dev *dev);
362 static void s2255_stop_readpipe(struct s2255_dev *dev);
363 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
364 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
365 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
366 int chn, int jpgsize);
367 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
368 struct s2255_mode *mode);
369 static int s2255_board_shutdown(struct s2255_dev *dev);
370 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
371 static void s2255_destroy(struct s2255_dev *dev);
372 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
373 u16 index, u16 value, void *buf,
374 s32 buf_len, int bOut);
376 /* dev_err macro with driver name */
377 #define S2255_DRIVER_NAME "s2255"
378 #define s2255_dev_err(dev, fmt, arg...) \
379 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
381 #define dprintk(level, fmt, arg...) \
383 if (*s2255_debug >= (level)) { \
384 printk(KERN_DEBUG S2255_DRIVER_NAME \
389 static struct usb_driver s2255_driver;
391 /* Declare static vars that will be used as parameters */
392 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
394 /* start video number */
395 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
397 module_param(debug, int, 0644);
398 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
399 module_param(vid_limit, int, 0644);
400 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
401 module_param(video_nr, int, 0644);
402 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
404 /* USB device table */
405 #define USB_SENSORAY_VID 0x1943
406 static struct usb_device_id s2255_table[] = {
407 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
408 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
409 { } /* Terminating entry */
411 MODULE_DEVICE_TABLE(usb, s2255_table);
413 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
416 static const struct s2255_fmt formats[] = {
418 .name = "4:2:2, planar, YUV422P",
419 .fourcc = V4L2_PIX_FMT_YUV422P,
423 .name = "4:2:2, packed, YUYV",
424 .fourcc = V4L2_PIX_FMT_YUYV,
428 .name = "4:2:2, packed, UYVY",
429 .fourcc = V4L2_PIX_FMT_UYVY,
433 .fourcc = V4L2_PIX_FMT_JPEG,
437 .fourcc = V4L2_PIX_FMT_GREY,
442 static int norm_maxw(struct video_device *vdev)
444 return (vdev->current_norm & V4L2_STD_NTSC) ?
445 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
448 static int norm_maxh(struct video_device *vdev)
450 return (vdev->current_norm & V4L2_STD_NTSC) ?
451 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
454 static int norm_minw(struct video_device *vdev)
456 return (vdev->current_norm & V4L2_STD_NTSC) ?
457 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
460 static int norm_minh(struct video_device *vdev)
462 return (vdev->current_norm & V4L2_STD_NTSC) ?
463 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
468 * TODO: fixme: move YUV reordering to hardware
469 * converts 2255 planar format to yuyv or uyvy
471 static void planar422p_to_yuv_packed(const unsigned char *in,
473 int width, int height,
479 unsigned long size = height * width;
481 pY = (unsigned char *)in;
482 pCr = (unsigned char *)in + height * width;
483 pCb = (unsigned char *)in + height * width + (height * width / 2);
484 for (i = 0; i < size * 2; i += 4) {
485 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
486 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
487 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
488 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
493 static void s2255_reset_dsppower(struct s2255_dev *dev)
495 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
497 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
501 /* kickstarts the firmware loading. from probe
503 static void s2255_timer(unsigned long user_data)
505 struct s2255_fw *data = (struct s2255_fw *)user_data;
506 dprintk(100, "%s\n", __func__);
507 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
508 printk(KERN_ERR "s2255: can't submit urb\n");
509 atomic_set(&data->fw_state, S2255_FW_FAILED);
510 /* wake up anything waiting for the firmware */
511 wake_up(&data->wait_fw);
517 /* this loads the firmware asynchronously.
518 Originally this was done synchroously in probe.
519 But it is better to load it asynchronously here than block
520 inside the probe function. Blocking inside probe affects boot time.
521 FW loading is triggered by the timer in the probe function
523 static void s2255_fwchunk_complete(struct urb *urb)
525 struct s2255_fw *data = urb->context;
526 struct usb_device *udev = urb->dev;
528 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
530 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
531 atomic_set(&data->fw_state, S2255_FW_FAILED);
532 /* wake up anything waiting for the firmware */
533 wake_up(&data->wait_fw);
536 if (data->fw_urb == NULL) {
537 s2255_dev_err(&udev->dev, "disconnected\n");
538 atomic_set(&data->fw_state, S2255_FW_FAILED);
539 /* wake up anything waiting for the firmware */
540 wake_up(&data->wait_fw);
543 #define CHUNK_SIZE 512
544 /* all USB transfers must be done with continuous kernel memory.
545 can't allocate more than 128k in current linux kernel, so
546 upload the firmware in chunks
548 if (data->fw_loaded < data->fw_size) {
549 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
550 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
552 if (len < CHUNK_SIZE)
553 memset(data->pfw_data, 0, CHUNK_SIZE);
555 dprintk(100, "completed len %d, loaded %d \n", len,
558 memcpy(data->pfw_data,
559 (char *) data->fw->data + data->fw_loaded, len);
561 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
562 data->pfw_data, CHUNK_SIZE,
563 s2255_fwchunk_complete, data);
564 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
565 dev_err(&udev->dev, "failed submit URB\n");
566 atomic_set(&data->fw_state, S2255_FW_FAILED);
567 /* wake up anything waiting for the firmware */
568 wake_up(&data->wait_fw);
571 data->fw_loaded += len;
573 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
574 dprintk(100, "%s: firmware upload complete\n", __func__);
580 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
582 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
583 struct s2255_buffer *buf;
584 unsigned long flags = 0;
586 spin_lock_irqsave(&dev->slock, flags);
587 if (list_empty(&dma_q->active)) {
588 dprintk(1, "No active queue to serve\n");
592 buf = list_entry(dma_q->active.next,
593 struct s2255_buffer, vb.queue);
594 list_del(&buf->vb.queue);
595 do_gettimeofday(&buf->vb.ts);
596 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
597 wake_up(&buf->vb.done);
598 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
600 spin_unlock_irqrestore(&dev->slock, flags);
604 static const struct s2255_fmt *format_by_fourcc(int fourcc)
607 for (i = 0; i < ARRAY_SIZE(formats); i++) {
608 if (-1 == formats[i].fourcc)
610 if (formats[i].fourcc == fourcc)
616 /* video buffer vmalloc implementation based partly on VIVI driver which is
617 * Copyright (c) 2006 by
618 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
619 * Ted Walther <ted--a.t--enumera.com>
620 * John Sokol <sokol--a.t--videotechnology.com>
621 * http://v4l.videotechnology.com/
624 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
625 int chn, int jpgsize)
630 char *vbuf = videobuf_to_vmalloc(&buf->vb);
631 unsigned long last_frame;
632 struct s2255_framei *frm;
637 last_frame = dev->last_frame[chn];
638 if (last_frame != -1) {
639 frm = &dev->buffer[chn].frame[last_frame];
641 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
642 switch (buf->fmt->fourcc) {
643 case V4L2_PIX_FMT_YUYV:
644 case V4L2_PIX_FMT_UYVY:
645 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
650 case V4L2_PIX_FMT_GREY:
651 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
653 case V4L2_PIX_FMT_JPEG:
654 buf->vb.size = jpgsize;
655 memcpy(vbuf, tmpbuf, buf->vb.size);
657 case V4L2_PIX_FMT_YUV422P:
659 buf->vb.width * buf->vb.height * 2);
662 printk(KERN_DEBUG "s2255: unknown format?\n");
664 dev->last_frame[chn] = -1;
666 printk(KERN_ERR "s2255: =======no frame\n");
670 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
671 (unsigned long)vbuf, pos);
672 /* tell v4l buffer was filled */
674 buf->vb.field_count = dev->frame_count[chn] * 2;
675 do_gettimeofday(&ts);
677 buf->vb.state = VIDEOBUF_DONE;
681 /* ------------------------------------------------------------------
683 ------------------------------------------------------------------*/
685 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
688 struct s2255_fh *fh = vq->priv_data;
690 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
693 *count = S2255_DEF_BUFS;
695 if (*size * *count > vid_limit * 1024 * 1024)
696 *count = (vid_limit * 1024 * 1024) / *size;
701 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
703 dprintk(4, "%s\n", __func__);
705 videobuf_vmalloc_free(&buf->vb);
706 buf->vb.state = VIDEOBUF_NEEDS_INIT;
709 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
710 enum v4l2_field field)
712 struct s2255_fh *fh = vq->priv_data;
713 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
715 dprintk(4, "%s, field=%d\n", __func__, field);
719 if ((fh->width < norm_minw(&fh->dev->vdev[fh->channel])) ||
720 (fh->width > norm_maxw(&fh->dev->vdev[fh->channel])) ||
721 (fh->height < norm_minh(&fh->dev->vdev[fh->channel])) ||
722 (fh->height > norm_maxh(&fh->dev->vdev[fh->channel]))) {
723 dprintk(4, "invalid buffer prepare\n");
727 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
729 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
730 dprintk(4, "invalid buffer prepare\n");
735 buf->vb.width = fh->width;
736 buf->vb.height = fh->height;
737 buf->vb.field = field;
739 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
740 rc = videobuf_iolock(vq, &buf->vb, NULL);
745 buf->vb.state = VIDEOBUF_PREPARED;
748 free_buffer(vq, buf);
752 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
754 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
755 struct s2255_fh *fh = vq->priv_data;
756 struct s2255_dev *dev = fh->dev;
757 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
758 dprintk(1, "%s\n", __func__);
759 buf->vb.state = VIDEOBUF_QUEUED;
760 list_add_tail(&buf->vb.queue, &vidq->active);
763 static void buffer_release(struct videobuf_queue *vq,
764 struct videobuf_buffer *vb)
766 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
767 struct s2255_fh *fh = vq->priv_data;
768 dprintk(4, "%s %d\n", __func__, fh->channel);
769 free_buffer(vq, buf);
772 static struct videobuf_queue_ops s2255_video_qops = {
773 .buf_setup = buffer_setup,
774 .buf_prepare = buffer_prepare,
775 .buf_queue = buffer_queue,
776 .buf_release = buffer_release,
780 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
783 mutex_lock(&dev->lock);
784 if (dev->resources[fh->channel]) {
785 /* no, someone else uses it */
786 mutex_unlock(&dev->lock);
789 /* it's free, grab it */
790 dev->resources[fh->channel] = 1;
791 fh->resources[fh->channel] = 1;
792 dprintk(1, "s2255: res: get\n");
793 mutex_unlock(&dev->lock);
797 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
799 return dev->resources[fh->channel];
802 static int res_check(struct s2255_fh *fh)
804 return fh->resources[fh->channel];
808 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
810 mutex_lock(&dev->lock);
811 dev->resources[fh->channel] = 0;
812 fh->resources[fh->channel] = 0;
813 mutex_unlock(&dev->lock);
814 dprintk(1, "res: put\n");
817 static int vidioc_querymenu(struct file *file, void *priv,
818 struct v4l2_querymenu *qmenu)
820 static const char *colorfilter[] = {
825 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
827 const char **menu_items = colorfilter;
828 for (i = 0; i < qmenu->index && menu_items[i]; i++)
829 ; /* do nothing (from v4l2-common.c) */
830 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
832 strlcpy(qmenu->name, menu_items[qmenu->index],
833 sizeof(qmenu->name));
836 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
839 static int vidioc_querycap(struct file *file, void *priv,
840 struct v4l2_capability *cap)
842 struct s2255_fh *fh = file->private_data;
843 struct s2255_dev *dev = fh->dev;
844 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
845 strlcpy(cap->card, "s2255", sizeof(cap->card));
846 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
847 cap->version = S2255_VERSION;
848 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
852 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
853 struct v4l2_fmtdesc *f)
859 if (index >= ARRAY_SIZE(formats))
862 dprintk(4, "name %s\n", formats[index].name);
863 strlcpy(f->description, formats[index].name, sizeof(f->description));
864 f->pixelformat = formats[index].fourcc;
868 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
869 struct v4l2_format *f)
871 struct s2255_fh *fh = priv;
873 f->fmt.pix.width = fh->width;
874 f->fmt.pix.height = fh->height;
875 f->fmt.pix.field = fh->vb_vidq.field;
876 f->fmt.pix.pixelformat = fh->fmt->fourcc;
877 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
878 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
882 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
883 struct v4l2_format *f)
885 const struct s2255_fmt *fmt;
886 enum v4l2_field field;
888 struct s2255_fh *fh = priv;
889 struct s2255_dev *dev = fh->dev;
893 (dev->vdev[fh->channel].current_norm & V4L2_STD_NTSC) ? 1 : 0;
895 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
900 field = f->fmt.pix.field;
901 if (field == V4L2_FIELD_ANY)
904 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
905 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
908 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
909 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
911 field = V4L2_FIELD_SEQ_TB;
912 } else if (!((field == V4L2_FIELD_INTERLACED) ||
913 (field == V4L2_FIELD_SEQ_TB) ||
914 (field == V4L2_FIELD_INTERLACED_TB))) {
915 dprintk(1, "unsupported field setting\n");
919 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
921 field = V4L2_FIELD_TOP;
922 } else if (!((field == V4L2_FIELD_TOP) ||
923 (field == V4L2_FIELD_BOTTOM))) {
924 dprintk(1, "unsupported field setting\n");
929 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
930 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
931 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
932 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
933 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
934 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
936 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
939 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
940 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
942 field = V4L2_FIELD_SEQ_TB;
943 } else if (!((field == V4L2_FIELD_INTERLACED) ||
944 (field == V4L2_FIELD_SEQ_TB) ||
945 (field == V4L2_FIELD_INTERLACED_TB))) {
946 dprintk(1, "unsupported field setting\n");
950 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
952 field = V4L2_FIELD_TOP;
953 } else if (!((field == V4L2_FIELD_TOP) ||
954 (field == V4L2_FIELD_BOTTOM))) {
955 dprintk(1, "unsupported field setting\n");
959 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
960 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
961 field = V4L2_FIELD_SEQ_TB;
962 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
963 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
964 field = V4L2_FIELD_TOP;
965 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
966 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
967 field = V4L2_FIELD_TOP;
969 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
970 field = V4L2_FIELD_TOP;
973 f->fmt.pix.field = field;
974 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
975 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
976 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
977 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
981 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
982 struct v4l2_format *f)
984 struct s2255_fh *fh = priv;
985 const struct s2255_fmt *fmt;
986 struct videobuf_queue *q = &fh->vb_vidq;
990 ret = vidioc_try_fmt_vid_cap(file, fh, f);
995 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1000 mutex_lock(&q->vb_lock);
1002 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1003 dprintk(1, "queue busy\n");
1008 if (res_locked(fh->dev, fh)) {
1009 dprintk(1, "%s: channel busy\n", __func__);
1015 fh->width = f->fmt.pix.width;
1016 fh->height = f->fmt.pix.height;
1017 fh->vb_vidq.field = f->fmt.pix.field;
1019 norm = norm_minw(&fh->dev->vdev[fh->channel]);
1020 if (fh->width > norm_minw(&fh->dev->vdev[fh->channel])) {
1021 if (fh->height > norm_minh(&fh->dev->vdev[fh->channel])) {
1022 if (fh->dev->cap_parm[fh->channel].capturemode &
1023 V4L2_MODE_HIGHQUALITY)
1024 fh->mode.scale = SCALE_4CIFSI;
1026 fh->mode.scale = SCALE_4CIFS;
1028 fh->mode.scale = SCALE_2CIFS;
1031 fh->mode.scale = SCALE_1CIFS;
1035 switch (fh->fmt->fourcc) {
1036 case V4L2_PIX_FMT_GREY:
1037 fh->mode.color &= ~MASK_COLOR;
1038 fh->mode.color |= COLOR_Y8;
1040 case V4L2_PIX_FMT_JPEG:
1041 fh->mode.color &= ~MASK_COLOR;
1042 fh->mode.color |= COLOR_JPG;
1043 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
1045 case V4L2_PIX_FMT_YUV422P:
1046 fh->mode.color &= ~MASK_COLOR;
1047 fh->mode.color |= COLOR_YUVPL;
1049 case V4L2_PIX_FMT_YUYV:
1050 case V4L2_PIX_FMT_UYVY:
1052 fh->mode.color &= ~MASK_COLOR;
1053 fh->mode.color |= COLOR_YUVPK;
1058 mutex_unlock(&q->vb_lock);
1062 static int vidioc_reqbufs(struct file *file, void *priv,
1063 struct v4l2_requestbuffers *p)
1066 struct s2255_fh *fh = priv;
1067 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1071 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1074 struct s2255_fh *fh = priv;
1075 rc = videobuf_querybuf(&fh->vb_vidq, p);
1079 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1082 struct s2255_fh *fh = priv;
1083 rc = videobuf_qbuf(&fh->vb_vidq, p);
1087 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1090 struct s2255_fh *fh = priv;
1091 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1095 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1096 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1098 struct s2255_fh *fh = priv;
1100 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1104 /* write to the configuration pipe, synchronously */
1105 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1112 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1113 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1118 static u32 get_transfer_size(struct s2255_mode *mode)
1120 int linesPerFrame = LINE_SZ_DEF;
1121 int pixelsPerLine = NUM_LINES_DEF;
1124 unsigned int mask_mult;
1129 if (mode->format == FORMAT_NTSC) {
1130 switch (mode->scale) {
1133 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1134 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1137 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1138 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1141 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1142 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1147 } else if (mode->format == FORMAT_PAL) {
1148 switch (mode->scale) {
1151 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1152 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1155 linesPerFrame = NUM_LINES_2CIFS_PAL;
1156 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1159 linesPerFrame = NUM_LINES_1CIFS_PAL;
1160 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1166 outImageSize = linesPerFrame * pixelsPerLine;
1167 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1168 /* 2 bytes/pixel if not monochrome */
1172 /* total bytes to send including prefix and 4K padding;
1173 must be a multiple of USB_READ_SIZE */
1174 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1175 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1176 /* if size not a multiple of USB_READ_SIZE */
1177 if (usbInSize & ~mask_mult)
1178 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1182 static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
1184 struct device *dev = &sdev->udev->dev;
1185 dev_info(dev, "------------------------------------------------\n");
1186 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1187 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
1188 dev_info(dev, "bright: 0x%x\n", mode->bright);
1189 dev_info(dev, "------------------------------------------------\n");
1193 * set mode is the function which controls the DSP.
1194 * the restart parameter in struct s2255_mode should be set whenever
1195 * the image size could change via color format, video system or image
1197 * When the restart parameter is set, we sleep for ONE frame to allow the
1198 * DSP time to get the new frame
1200 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1201 struct s2255_mode *mode)
1205 unsigned long chn_rev;
1206 mutex_lock(&dev->lock);
1207 chn_rev = G_chnmap[chn];
1208 dprintk(3, "%s channel %lu\n", __func__, chn);
1209 /* if JPEG, set the quality */
1210 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1211 mode->color &= ~MASK_COLOR;
1212 mode->color |= COLOR_JPG;
1213 mode->color &= ~MASK_JPG_QUALITY;
1214 mode->color |= (dev->jc[chn].quality << 8);
1217 dev->mode[chn] = *mode;
1218 dev->req_image_size[chn] = get_transfer_size(mode);
1219 dprintk(1, "%s: reqsize %ld\n", __func__, dev->req_image_size[chn]);
1220 buffer = kzalloc(512, GFP_KERNEL);
1221 if (buffer == NULL) {
1222 dev_err(&dev->udev->dev, "out of mem\n");
1223 mutex_unlock(&dev->lock);
1227 buffer[0] = IN_DATA_TOKEN;
1228 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1229 buffer[2] = CMD_SET_MODE;
1230 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1231 dev->setmode_ready[chn] = 0;
1232 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1234 s2255_print_cfg(dev, mode);
1236 /* wait at least 3 frames before continuing */
1237 if (mode->restart) {
1238 wait_event_timeout(dev->wait_setmode[chn],
1239 (dev->setmode_ready[chn] != 0),
1240 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1241 if (dev->setmode_ready[chn] != 1) {
1242 printk(KERN_DEBUG "s2255: no set mode response\n");
1246 /* clear the restart flag */
1247 dev->mode[chn].restart = 0;
1248 mutex_unlock(&dev->lock);
1249 dprintk(1, "%s chn %lu, result: %d\n", __func__, chn, res);
1253 static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1259 mutex_lock(&dev->lock);
1260 chn_rev = G_chnmap[chn];
1261 dprintk(4, "%s chan %lu\n", __func__, chn);
1262 buffer = kzalloc(512, GFP_KERNEL);
1263 if (buffer == NULL) {
1264 dev_err(&dev->udev->dev, "out of mem\n");
1265 mutex_unlock(&dev->lock);
1268 /* form the get vid status command */
1269 buffer[0] = IN_DATA_TOKEN;
1270 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1271 buffer[2] = CMD_STATUS;
1273 dev->vidstatus_ready[chn] = 0;
1274 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1276 wait_event_timeout(dev->wait_vidstatus[chn],
1277 (dev->vidstatus_ready[chn] != 0),
1278 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1279 if (dev->vidstatus_ready[chn] != 1) {
1280 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1283 *pstatus = dev->vidstatus[chn];
1284 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1285 mutex_unlock(&dev->lock);
1289 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1292 struct s2255_fh *fh = priv;
1293 struct s2255_dev *dev = fh->dev;
1294 struct s2255_mode *new_mode;
1295 struct s2255_mode *old_mode;
1298 dprintk(4, "%s\n", __func__);
1299 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1300 dev_err(&dev->udev->dev, "invalid fh type0\n");
1303 if (i != fh->type) {
1304 dev_err(&dev->udev->dev, "invalid fh type1\n");
1308 if (!res_get(dev, fh)) {
1309 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1313 /* send a set mode command everytime with restart.
1314 in case we switch resolutions or other parameters */
1316 new_mode = &fh->mode;
1317 old_mode = &fh->dev->mode[chn];
1319 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
1320 new_mode->restart = 1;
1321 else if (new_mode->scale != old_mode->scale)
1322 new_mode->restart = 1;
1323 else if (new_mode->format != old_mode->format)
1324 new_mode->restart = 1;
1326 s2255_set_mode(dev, chn, new_mode);
1327 new_mode->restart = 0;
1328 *old_mode = *new_mode;
1329 dev->cur_fmt[chn] = fh->fmt;
1330 dev->last_frame[chn] = -1;
1331 dev->bad_payload[chn] = 0;
1332 dev->cur_frame[chn] = 0;
1333 dev->frame_count[chn] = 0;
1334 for (j = 0; j < SYS_FRAMES; j++) {
1335 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1336 dev->buffer[chn].frame[j].cur_size = 0;
1338 res = videobuf_streamon(&fh->vb_vidq);
1340 s2255_start_acquire(dev, chn);
1341 dev->b_acquire[chn] = 1;
1348 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1350 struct s2255_fh *fh = priv;
1351 struct s2255_dev *dev = fh->dev;
1352 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1353 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1354 printk(KERN_ERR "invalid fh type0\n");
1357 if (i != fh->type) {
1358 printk(KERN_ERR "invalid type i\n");
1361 s2255_stop_acquire(dev, fh->channel);
1362 videobuf_streamoff(&fh->vb_vidq);
1367 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1369 struct s2255_fh *fh = priv;
1370 struct s2255_mode *mode;
1371 struct videobuf_queue *q = &fh->vb_vidq;
1373 mutex_lock(&q->vb_lock);
1374 if (videobuf_queue_is_busy(q)) {
1375 dprintk(1, "queue busy\n");
1379 if (res_locked(fh->dev, fh)) {
1380 dprintk(1, "can't change standard after started\n");
1385 if (*i & V4L2_STD_NTSC) {
1386 dprintk(4, "%s NTSC\n", __func__);
1387 /* if changing format, reset frame decimation/intervals */
1388 if (mode->format != FORMAT_NTSC) {
1389 mode->format = FORMAT_NTSC;
1390 mode->fdec = FDEC_1;
1392 } else if (*i & V4L2_STD_PAL) {
1393 dprintk(4, "%s PAL\n", __func__);
1394 mode->format = FORMAT_PAL;
1395 if (mode->format != FORMAT_PAL) {
1396 mode->format = FORMAT_PAL;
1397 mode->fdec = FDEC_1;
1403 mutex_unlock(&q->vb_lock);
1407 /* Sensoray 2255 is a multiple channel capture device.
1408 It does not have a "crossbar" of inputs.
1409 We use one V4L device per channel. The user must
1410 be aware that certain combinations are not allowed.
1411 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1412 at once in color(you can do full fps on 4 channels with greyscale.
1414 static int vidioc_enum_input(struct file *file, void *priv,
1415 struct v4l2_input *inp)
1417 struct s2255_fh *fh = priv;
1418 struct s2255_dev *dev = fh->dev;
1420 if (inp->index != 0)
1422 inp->type = V4L2_INPUT_TYPE_CAMERA;
1423 inp->std = S2255_NORMS;
1425 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1427 rc = s2255_cmd_status(dev, fh->channel, &status);
1428 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1430 inp->status = (status & 0x01) ? 0
1431 : V4L2_IN_ST_NO_SIGNAL;
1436 strlcpy(inp->name, "Composite", sizeof(inp->name));
1439 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1446 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1451 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1458 /* --- controls ---------------------------------------------- */
1459 static int vidioc_queryctrl(struct file *file, void *priv,
1460 struct v4l2_queryctrl *qc)
1462 struct s2255_fh *fh = priv;
1463 struct s2255_dev *dev = fh->dev;
1465 case V4L2_CID_BRIGHTNESS:
1466 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1468 case V4L2_CID_CONTRAST:
1469 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1471 case V4L2_CID_SATURATION:
1472 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1475 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1477 case V4L2_CID_PRIVATE_COLORFILTER:
1478 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1480 if ((dev->pid == 0x2257) && (fh->channel > 1))
1482 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1483 qc->type = V4L2_CTRL_TYPE_MENU;
1487 qc->default_value = 1;
1493 dprintk(4, "%s, id %d\n", __func__, qc->id);
1497 static int vidioc_g_ctrl(struct file *file, void *priv,
1498 struct v4l2_control *ctrl)
1500 struct s2255_fh *fh = priv;
1501 struct s2255_dev *dev = fh->dev;
1503 case V4L2_CID_BRIGHTNESS:
1504 ctrl->value = fh->mode.bright;
1506 case V4L2_CID_CONTRAST:
1507 ctrl->value = fh->mode.contrast;
1509 case V4L2_CID_SATURATION:
1510 ctrl->value = fh->mode.saturation;
1513 ctrl->value = fh->mode.hue;
1515 case V4L2_CID_PRIVATE_COLORFILTER:
1516 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1518 if ((dev->pid == 0x2257) && (fh->channel > 1))
1520 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1525 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1529 static int vidioc_s_ctrl(struct file *file, void *priv,
1530 struct v4l2_control *ctrl)
1532 struct s2255_fh *fh = priv;
1533 struct s2255_dev *dev = fh->dev;
1534 struct s2255_mode *mode;
1536 dprintk(4, "%s\n", __func__);
1537 /* update the mode to the corresponding value */
1539 case V4L2_CID_BRIGHTNESS:
1540 mode->bright = ctrl->value;
1542 case V4L2_CID_CONTRAST:
1543 mode->contrast = ctrl->value;
1546 mode->hue = ctrl->value;
1548 case V4L2_CID_SATURATION:
1549 mode->saturation = ctrl->value;
1551 case V4L2_CID_PRIVATE_COLORFILTER:
1552 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1554 if ((dev->pid == 0x2257) && (fh->channel > 1))
1556 mode->color &= ~MASK_INPUT_TYPE;
1557 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1563 /* set mode here. Note: stream does not need restarted.
1564 some V4L programs restart stream unnecessarily
1567 s2255_set_mode(dev, fh->channel, mode);
1571 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1572 struct v4l2_jpegcompression *jc)
1574 struct s2255_fh *fh = priv;
1575 struct s2255_dev *dev = fh->dev;
1576 *jc = dev->jc[fh->channel];
1577 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1581 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1582 struct v4l2_jpegcompression *jc)
1584 struct s2255_fh *fh = priv;
1585 struct s2255_dev *dev = fh->dev;
1586 if (jc->quality < 0 || jc->quality > 100)
1588 dev->jc[fh->channel].quality = jc->quality;
1589 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1593 static int vidioc_g_parm(struct file *file, void *priv,
1594 struct v4l2_streamparm *sp)
1596 struct s2255_fh *fh = priv;
1597 struct s2255_dev *dev = fh->dev;
1598 __u32 def_num, def_dem;
1599 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1601 memset(sp, 0, sizeof(struct v4l2_streamparm));
1602 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1603 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1604 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1605 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1606 sp->parm.capture.timeperframe.denominator = def_dem;
1607 switch (fh->mode.fdec) {
1610 sp->parm.capture.timeperframe.numerator = def_num;
1613 sp->parm.capture.timeperframe.numerator = def_num * 2;
1616 sp->parm.capture.timeperframe.numerator = def_num * 3;
1619 sp->parm.capture.timeperframe.numerator = def_num * 5;
1622 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1623 sp->parm.capture.capturemode,
1624 sp->parm.capture.timeperframe.numerator,
1625 sp->parm.capture.timeperframe.denominator);
1629 static int vidioc_s_parm(struct file *file, void *priv,
1630 struct v4l2_streamparm *sp)
1632 struct s2255_fh *fh = priv;
1633 struct s2255_dev *dev = fh->dev;
1635 __u32 def_num, def_dem;
1636 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1638 /* high quality capture mode requires a stream restart */
1639 if (dev->cap_parm[fh->channel].capturemode
1640 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1642 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1643 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1644 if (def_dem != sp->parm.capture.timeperframe.denominator)
1645 sp->parm.capture.timeperframe.numerator = def_num;
1646 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1647 sp->parm.capture.timeperframe.numerator = def_num;
1648 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1649 sp->parm.capture.timeperframe.numerator = def_num * 2;
1651 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1652 sp->parm.capture.timeperframe.numerator = def_num * 3;
1655 sp->parm.capture.timeperframe.numerator = def_num * 5;
1658 fh->mode.fdec = fdec;
1659 sp->parm.capture.timeperframe.denominator = def_dem;
1660 s2255_set_mode(dev, fh->channel, &fh->mode);
1661 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1663 sp->parm.capture.capturemode,
1664 sp->parm.capture.timeperframe.numerator,
1665 sp->parm.capture.timeperframe.denominator, fdec);
1669 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1670 struct v4l2_frmivalenum *fe)
1673 #define NUM_FRAME_ENUMS 4
1674 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1675 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1677 switch (fe->width) {
1679 if (fe->height != 240 && fe->height != 480)
1684 if (fe->height != 240)
1689 if (fe->height != 288 && fe->height != 576)
1693 if (fe->height != 288)
1699 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1700 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1701 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1702 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1703 fe->discrete.denominator);
1707 static int s2255_open(struct file *file)
1709 struct video_device *vdev = video_devdata(file);
1710 struct s2255_dev *dev = video_drvdata(file);
1711 struct s2255_fh *fh;
1712 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1714 int cur_channel = -1;
1716 dprintk(1, "s2255: open called (dev=%s)\n",
1717 video_device_node_name(vdev));
1719 for (i = 0; i < MAX_CHANNELS; i++)
1720 if (&dev->vdev[i] == vdev) {
1725 * open lock necessary to prevent multiple instances
1726 * of v4l-conf (or other programs) from simultaneously
1727 * reloading firmware.
1729 mutex_lock(&dev->open_lock);
1730 state = atomic_read(&dev->fw_data->fw_state);
1732 case S2255_FW_DISCONNECTING:
1733 mutex_unlock(&dev->open_lock);
1735 case S2255_FW_FAILED:
1736 s2255_dev_err(&dev->udev->dev,
1737 "firmware load failed. retrying.\n");
1738 s2255_fwload_start(dev, 1);
1739 wait_event_timeout(dev->fw_data->wait_fw,
1740 ((atomic_read(&dev->fw_data->fw_state)
1741 == S2255_FW_SUCCESS) ||
1742 (atomic_read(&dev->fw_data->fw_state)
1743 == S2255_FW_DISCONNECTING)),
1744 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1745 /* state may have changed, re-read */
1746 state = atomic_read(&dev->fw_data->fw_state);
1748 case S2255_FW_NOTLOADED:
1749 case S2255_FW_LOADED_DSPWAIT:
1750 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1751 driver loaded and then device immediately opened */
1752 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1753 wait_event_timeout(dev->fw_data->wait_fw,
1754 ((atomic_read(&dev->fw_data->fw_state)
1755 == S2255_FW_SUCCESS) ||
1756 (atomic_read(&dev->fw_data->fw_state)
1757 == S2255_FW_DISCONNECTING)),
1758 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1759 /* state may have changed, re-read */
1760 state = atomic_read(&dev->fw_data->fw_state);
1762 case S2255_FW_SUCCESS:
1766 /* state may have changed in above switch statement */
1768 case S2255_FW_SUCCESS:
1770 case S2255_FW_FAILED:
1771 printk(KERN_INFO "2255 firmware load failed.\n");
1772 mutex_unlock(&dev->open_lock);
1774 case S2255_FW_DISCONNECTING:
1775 printk(KERN_INFO "%s: disconnecting\n", __func__);
1776 mutex_unlock(&dev->open_lock);
1778 case S2255_FW_LOADED_DSPWAIT:
1779 case S2255_FW_NOTLOADED:
1780 printk(KERN_INFO "%s: firmware not loaded yet"
1781 "please try again later\n",
1784 * Timeout on firmware load means device unusable.
1785 * Set firmware failure state.
1786 * On next s2255_open the firmware will be reloaded.
1788 atomic_set(&dev->fw_data->fw_state,
1790 mutex_unlock(&dev->open_lock);
1793 printk(KERN_INFO "%s: unknown state\n", __func__);
1794 mutex_unlock(&dev->open_lock);
1797 mutex_unlock(&dev->open_lock);
1798 /* allocate + initialize per filehandle data */
1799 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1802 file->private_data = fh;
1804 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1805 fh->mode = dev->mode[cur_channel];
1806 fh->fmt = dev->cur_fmt[cur_channel];
1807 /* default 4CIF NTSC */
1808 fh->width = LINE_SZ_4CIFS_NTSC;
1809 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1810 fh->channel = cur_channel;
1811 /* configure channel to default state */
1812 if (!dev->chn_configured[cur_channel]) {
1813 s2255_set_mode(dev, cur_channel, &fh->mode);
1814 dev->chn_configured[cur_channel] = 1;
1816 dprintk(1, "%s: dev=%s type=%s\n", __func__,
1817 video_device_node_name(vdev), v4l2_type_names[type]);
1818 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
1819 (unsigned long)fh, (unsigned long)dev,
1820 (unsigned long)&dev->vidq[cur_channel]);
1821 dprintk(4, "%s: list_empty active=%d\n", __func__,
1822 list_empty(&dev->vidq[cur_channel].active));
1823 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1826 V4L2_FIELD_INTERLACED,
1827 sizeof(struct s2255_buffer), fh);
1832 static unsigned int s2255_poll(struct file *file,
1833 struct poll_table_struct *wait)
1835 struct s2255_fh *fh = file->private_data;
1837 dprintk(100, "%s\n", __func__);
1838 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1840 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1844 static void s2255_destroy(struct s2255_dev *dev)
1846 /* board shutdown stops the read pipe if it is running */
1847 s2255_board_shutdown(dev);
1848 /* make sure firmware still not trying to load */
1849 del_timer(&dev->timer); /* only started in .probe and .open */
1850 if (dev->fw_data->fw_urb) {
1851 usb_kill_urb(dev->fw_data->fw_urb);
1852 usb_free_urb(dev->fw_data->fw_urb);
1853 dev->fw_data->fw_urb = NULL;
1855 if (dev->fw_data->fw)
1856 release_firmware(dev->fw_data->fw);
1857 kfree(dev->fw_data->pfw_data);
1858 kfree(dev->fw_data);
1859 /* reset the DSP so firmware can be reloaded next time */
1860 s2255_reset_dsppower(dev);
1861 mutex_destroy(&dev->open_lock);
1862 mutex_destroy(&dev->lock);
1863 usb_put_dev(dev->udev);
1864 dprintk(1, "%s", __func__);
1868 static int s2255_release(struct file *file)
1870 struct s2255_fh *fh = file->private_data;
1871 struct s2255_dev *dev = fh->dev;
1872 struct video_device *vdev = video_devdata(file);
1875 /* turn off stream */
1876 if (res_check(fh)) {
1877 if (dev->b_acquire[fh->channel])
1878 s2255_stop_acquire(dev, fh->channel);
1879 videobuf_streamoff(&fh->vb_vidq);
1882 videobuf_mmap_free(&fh->vb_vidq);
1883 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
1888 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1890 struct s2255_fh *fh = file->private_data;
1895 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
1896 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1897 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
1898 (unsigned long)vma->vm_start,
1899 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1903 static const struct v4l2_file_operations s2255_fops_v4l = {
1904 .owner = THIS_MODULE,
1906 .release = s2255_release,
1908 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1909 .mmap = s2255_mmap_v4l,
1912 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1913 .vidioc_querymenu = vidioc_querymenu,
1914 .vidioc_querycap = vidioc_querycap,
1915 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1916 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1917 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1918 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1919 .vidioc_reqbufs = vidioc_reqbufs,
1920 .vidioc_querybuf = vidioc_querybuf,
1921 .vidioc_qbuf = vidioc_qbuf,
1922 .vidioc_dqbuf = vidioc_dqbuf,
1923 .vidioc_s_std = vidioc_s_std,
1924 .vidioc_enum_input = vidioc_enum_input,
1925 .vidioc_g_input = vidioc_g_input,
1926 .vidioc_s_input = vidioc_s_input,
1927 .vidioc_queryctrl = vidioc_queryctrl,
1928 .vidioc_g_ctrl = vidioc_g_ctrl,
1929 .vidioc_s_ctrl = vidioc_s_ctrl,
1930 .vidioc_streamon = vidioc_streamon,
1931 .vidioc_streamoff = vidioc_streamoff,
1932 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1933 .vidiocgmbuf = vidioc_cgmbuf,
1935 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1936 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1937 .vidioc_s_parm = vidioc_s_parm,
1938 .vidioc_g_parm = vidioc_g_parm,
1939 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1942 static void s2255_video_device_release(struct video_device *vdev)
1944 struct s2255_dev *dev = video_get_drvdata(vdev);
1945 dprintk(4, "%s, chnls: %d \n", __func__, atomic_read(&dev->channels));
1946 if (atomic_dec_and_test(&dev->channels))
1951 static struct video_device template = {
1953 .fops = &s2255_fops_v4l,
1954 .ioctl_ops = &s2255_ioctl_ops,
1955 .release = s2255_video_device_release,
1956 .tvnorms = S2255_NORMS,
1957 .current_norm = V4L2_STD_NTSC_M,
1960 static int s2255_probe_v4l(struct s2255_dev *dev)
1964 int cur_nr = video_nr;
1965 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1968 /* initialize all video 4 linux */
1969 /* register 4 video devices */
1970 for (i = 0; i < MAX_CHANNELS; i++) {
1971 INIT_LIST_HEAD(&dev->vidq[i].active);
1972 dev->vidq[i].dev = dev;
1973 dev->vidq[i].channel = i;
1974 /* register 4 video devices */
1975 memcpy(&dev->vdev[i], &template, sizeof(struct video_device));
1976 dev->vdev[i].v4l2_dev = &dev->v4l2_dev;
1977 video_set_drvdata(&dev->vdev[i], dev);
1979 ret = video_register_device(&dev->vdev[i],
1983 ret = video_register_device(&dev->vdev[i],
1987 dev_err(&dev->udev->dev,
1988 "failed to register video device!\n");
1991 atomic_inc(&dev->channels);
1992 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1993 video_device_node_name(&dev->vdev[i]));
1997 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1998 S2255_MAJOR_VERSION,
1999 S2255_MINOR_VERSION);
2000 /* if no channels registered, return error and probe will fail*/
2001 if (atomic_read(&dev->channels) == 0) {
2002 v4l2_device_unregister(&dev->v4l2_dev);
2005 if (atomic_read(&dev->channels) != MAX_CHANNELS)
2006 printk(KERN_WARNING "s2255: Not all channels available.\n");
2010 /* this function moves the usb stream read pipe data
2011 * into the system buffers.
2012 * returns 0 on success, EAGAIN if more data to process( call this
2015 * Received frame structure:
2016 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
2017 * bytes 4-7: channel: 0-3
2018 * bytes 8-11: payload size: size of the frame
2019 * bytes 12-payloadsize+12: frame data
2021 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2027 unsigned long copy_size;
2030 struct s2255_framei *frm;
2031 unsigned char *pdata;
2033 dprintk(100, "buffer to user\n");
2035 idx = dev->cur_frame[dev->cc];
2036 frm = &dev->buffer[dev->cc].frame[idx];
2038 if (frm->ulState == S2255_READ_IDLE) {
2041 __le32 *pdword; /*data from dsp is little endian */
2043 /* search for marker codes */
2044 pdata = (unsigned char *)pipe_info->transfer_buffer;
2045 pdword = (__le32 *)pdata;
2046 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
2048 case S2255_MARKER_FRAME:
2049 dprintk(4, "found frame marker at offset:"
2050 " %d [%x %x]\n", jj, pdata[0],
2052 offset = jj + PREFIX_SIZE;
2055 if (cc >= MAX_CHANNELS) {
2061 dev->cc = G_chnmap[cc];
2062 payload = pdword[3];
2063 if (payload > dev->req_image_size[dev->cc]) {
2064 dev->bad_payload[dev->cc]++;
2065 /* discard the bad frame */
2068 dev->pkt_size[dev->cc] = payload;
2069 dev->jpg_size[dev->cc] = pdword[4];
2071 case S2255_MARKER_RESPONSE:
2072 pdata += DEF_USB_BLOCK;
2073 jj += DEF_USB_BLOCK;
2074 if (pdword[1] >= MAX_CHANNELS)
2076 cc = G_chnmap[pdword[1]];
2077 if (cc >= MAX_CHANNELS)
2079 switch (pdword[2]) {
2080 case S2255_RESPONSE_SETMODE:
2081 /* check if channel valid */
2082 /* set mode ready */
2083 dev->setmode_ready[cc] = 1;
2084 wake_up(&dev->wait_setmode[cc]);
2085 dprintk(5, "setmode ready %d\n", cc);
2087 case S2255_RESPONSE_FW:
2088 dev->chn_ready |= (1 << cc);
2089 if ((dev->chn_ready & 0x0f) != 0x0f)
2091 /* all channels ready */
2092 printk(KERN_INFO "s2255: fw loaded\n");
2093 atomic_set(&dev->fw_data->fw_state,
2095 wake_up(&dev->fw_data->wait_fw);
2097 case S2255_RESPONSE_STATUS:
2098 dev->vidstatus[cc] = pdword[3];
2099 dev->vidstatus_ready[cc] = 1;
2100 wake_up(&dev->wait_vidstatus[cc]);
2101 dprintk(5, "got vidstatus %x chan %d\n",
2105 printk(KERN_INFO "s2255 unknown resp\n");
2119 idx = dev->cur_frame[dev->cc];
2120 frm = &dev->buffer[dev->cc].frame[idx];
2122 /* search done. now find out if should be acquiring on this channel */
2123 if (!dev->b_acquire[dev->cc]) {
2124 /* we found a frame, but this channel is turned off */
2125 frm->ulState = S2255_READ_IDLE;
2129 if (frm->ulState == S2255_READ_IDLE) {
2130 frm->ulState = S2255_READ_FRAME;
2134 /* skip the marker 512 bytes (and offset if out of sync) */
2135 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2138 if (frm->lpvbits == NULL) {
2139 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2140 frm, dev, dev->cc, idx);
2144 pdest = frm->lpvbits + frm->cur_size;
2146 copy_size = (pipe_info->cur_transfer_size - offset);
2148 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2150 /* sanity check on pdest */
2151 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2152 memcpy(pdest, psrc, copy_size);
2154 frm->cur_size += copy_size;
2155 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2157 if (frm->cur_size >= size) {
2160 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2162 dev->last_frame[cc] = dev->cur_frame[cc];
2163 dev->cur_frame[cc]++;
2164 /* end of system frame ring buffer, start at zero */
2165 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2166 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2167 dev->cur_frame[cc] = 0;
2169 if (dev->b_acquire[cc])
2170 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2171 dev->frame_count[cc]++;
2172 frm->ulState = S2255_READ_IDLE;
2176 /* done successfully */
2180 static void s2255_read_video_callback(struct s2255_dev *dev,
2181 struct s2255_pipeinfo *pipe_info)
2184 dprintk(50, "callback read video \n");
2186 if (dev->cc >= MAX_CHANNELS) {
2188 dev_err(&dev->udev->dev, "invalid channel\n");
2191 /* otherwise copy to the system buffers */
2192 res = save_frame(dev, pipe_info);
2194 dprintk(4, "s2255: read callback failed\n");
2196 dprintk(50, "callback read video done\n");
2200 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2201 u16 Index, u16 Value, void *TransferBuffer,
2202 s32 TransferBufferLength, int bOut)
2206 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2208 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2210 Value, Index, TransferBuffer,
2211 TransferBufferLength, HZ * 5);
2213 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2214 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2215 Value, Index, TransferBuffer,
2216 TransferBufferLength, HZ * 5);
2222 * retrieve FX2 firmware version. future use.
2223 * @param dev pointer to device extension
2224 * @return -1 for fail, else returns firmware version as an int(16 bits)
2226 static int s2255_get_fx2fw(struct s2255_dev *dev)
2230 unsigned char transBuffer[64];
2231 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2234 dprintk(2, "get fw error: %x\n", ret);
2235 fw = transBuffer[0] + (transBuffer[1] << 8);
2236 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2241 * Create the system ring buffer to copy frames into from the
2244 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2247 unsigned long reqsize;
2248 dprintk(1, "create sys buffers\n");
2249 if (chn >= MAX_CHANNELS)
2252 dev->buffer[chn].dwFrames = SYS_FRAMES;
2254 /* always allocate maximum size(PAL) for system buffers */
2255 reqsize = SYS_FRAMES_MAXSIZE;
2257 if (reqsize > SYS_FRAMES_MAXSIZE)
2258 reqsize = SYS_FRAMES_MAXSIZE;
2260 for (i = 0; i < SYS_FRAMES; i++) {
2261 /* allocate the frames */
2262 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2264 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2265 &dev->buffer[chn].frame[i], chn, i,
2266 dev->buffer[chn].frame[i].lpvbits);
2267 dev->buffer[chn].frame[i].size = reqsize;
2268 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2269 printk(KERN_INFO "out of memory. using less frames\n");
2270 dev->buffer[chn].dwFrames = i;
2275 /* make sure internal states are set */
2276 for (i = 0; i < SYS_FRAMES; i++) {
2277 dev->buffer[chn].frame[i].ulState = 0;
2278 dev->buffer[chn].frame[i].cur_size = 0;
2281 dev->cur_frame[chn] = 0;
2282 dev->last_frame[chn] = -1;
2286 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2287 unsigned long channel)
2290 dprintk(1, "release sys buffers\n");
2291 for (i = 0; i < SYS_FRAMES; i++) {
2292 if (dev->buffer[channel].frame[i].lpvbits) {
2293 dprintk(1, "vfree %p\n",
2294 dev->buffer[channel].frame[i].lpvbits);
2295 vfree(dev->buffer[channel].frame[i].lpvbits);
2297 dev->buffer[channel].frame[i].lpvbits = NULL;
2302 static int s2255_board_init(struct s2255_dev *dev)
2304 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2307 struct s2255_pipeinfo *pipe = &dev->pipe;
2308 dprintk(4, "board init: %p", dev);
2309 memset(pipe, 0, sizeof(*pipe));
2311 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2312 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2314 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2316 if (pipe->transfer_buffer == NULL) {
2317 dprintk(1, "out of memory!\n");
2320 /* query the firmware */
2321 fw_ver = s2255_get_fx2fw(dev);
2323 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2324 (fw_ver >> 8) & 0xff,
2327 if (fw_ver < S2255_CUR_USB_FWVER)
2328 dev_err(&dev->udev->dev,
2329 "usb firmware not up to date %d.%d\n",
2330 (fw_ver >> 8) & 0xff,
2333 for (j = 0; j < MAX_CHANNELS; j++) {
2334 dev->b_acquire[j] = 0;
2335 dev->mode[j] = mode_def;
2336 if (dev->pid == 0x2257 && j > 1)
2337 dev->mode[j].color |= (1 << 16);
2338 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2339 dev->cur_fmt[j] = &formats[0];
2340 dev->mode[j].restart = 1;
2341 dev->req_image_size[j] = get_transfer_size(&mode_def);
2342 dev->frame_count[j] = 0;
2343 /* create the system buffers */
2344 s2255_create_sys_buffers(dev, j);
2346 /* start read pipe */
2347 s2255_start_readpipe(dev);
2348 dprintk(1, "%s: success\n", __func__);
2352 static int s2255_board_shutdown(struct s2255_dev *dev)
2355 dprintk(1, "%s: dev: %p", __func__, dev);
2357 for (i = 0; i < MAX_CHANNELS; i++) {
2358 if (dev->b_acquire[i])
2359 s2255_stop_acquire(dev, i);
2362 s2255_stop_readpipe(dev);
2364 for (i = 0; i < MAX_CHANNELS; i++)
2365 s2255_release_sys_buffers(dev, i);
2366 /* release transfer buffer */
2367 kfree(dev->pipe.transfer_buffer);
2371 static void read_pipe_completion(struct urb *purb)
2373 struct s2255_pipeinfo *pipe_info;
2374 struct s2255_dev *dev;
2377 pipe_info = purb->context;
2378 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
2380 if (pipe_info == NULL) {
2381 dev_err(&purb->dev->dev, "no context!\n");
2385 dev = pipe_info->dev;
2387 dev_err(&purb->dev->dev, "no context!\n");
2390 status = purb->status;
2391 /* if shutting down, do not resubmit, exit immediately */
2392 if (status == -ESHUTDOWN) {
2393 dprintk(2, "%s: err shutdown\n", __func__);
2394 pipe_info->err_count++;
2398 if (pipe_info->state == 0) {
2399 dprintk(2, "%s: exiting USB pipe", __func__);
2404 s2255_read_video_callback(dev, pipe_info);
2406 pipe_info->err_count++;
2407 dprintk(1, "%s: failed URB %d\n", __func__, status);
2410 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2412 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2414 pipe_info->transfer_buffer,
2415 pipe_info->cur_transfer_size,
2416 read_pipe_completion, pipe_info);
2418 if (pipe_info->state != 0) {
2419 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2420 dev_err(&dev->udev->dev, "error submitting urb\n");
2423 dprintk(2, "%s :complete state 0\n", __func__);
2428 static int s2255_start_readpipe(struct s2255_dev *dev)
2432 struct s2255_pipeinfo *pipe_info = &dev->pipe;
2433 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2434 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
2435 pipe_info->state = 1;
2436 pipe_info->err_count = 0;
2437 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2438 if (!pipe_info->stream_urb) {
2439 dev_err(&dev->udev->dev,
2440 "ReadStream: Unable to alloc URB\n");
2443 /* transfer buffer allocated in board_init */
2444 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2446 pipe_info->transfer_buffer,
2447 pipe_info->cur_transfer_size,
2448 read_pipe_completion, pipe_info);
2449 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2451 printk(KERN_ERR "s2255: start read pipe failed\n");
2457 /* starts acquisition process */
2458 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2460 unsigned char *buffer;
2462 unsigned long chn_rev;
2464 if (chn >= MAX_CHANNELS) {
2465 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2468 chn_rev = G_chnmap[chn];
2469 buffer = kzalloc(512, GFP_KERNEL);
2470 if (buffer == NULL) {
2471 dev_err(&dev->udev->dev, "out of mem\n");
2475 dev->last_frame[chn] = -1;
2476 dev->bad_payload[chn] = 0;
2477 dev->cur_frame[chn] = 0;
2478 for (j = 0; j < SYS_FRAMES; j++) {
2479 dev->buffer[chn].frame[j].ulState = 0;
2480 dev->buffer[chn].frame[j].cur_size = 0;
2483 /* send the start command */
2484 *(__le32 *) buffer = IN_DATA_TOKEN;
2485 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2486 *((__le32 *) buffer + 2) = CMD_START;
2487 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2489 dev_err(&dev->udev->dev, "CMD_START error\n");
2491 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2496 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2498 unsigned char *buffer;
2500 unsigned long chn_rev;
2501 if (chn >= MAX_CHANNELS) {
2502 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2505 chn_rev = G_chnmap[chn];
2506 buffer = kzalloc(512, GFP_KERNEL);
2507 if (buffer == NULL) {
2508 dev_err(&dev->udev->dev, "out of mem\n");
2511 /* send the stop command */
2512 *(__le32 *) buffer = IN_DATA_TOKEN;
2513 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2514 *((__le32 *) buffer + 2) = CMD_STOP;
2515 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2517 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2519 dev->b_acquire[chn] = 0;
2520 dprintk(4, "%s: chn %lu, res %d\n", __func__, chn, res);
2524 static void s2255_stop_readpipe(struct s2255_dev *dev)
2526 struct s2255_pipeinfo *pipe = &dev->pipe;
2528 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2532 if (pipe->stream_urb) {
2534 usb_kill_urb(pipe->stream_urb);
2535 usb_free_urb(pipe->stream_urb);
2536 pipe->stream_urb = NULL;
2538 dprintk(4, "%s", __func__);
2542 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2545 s2255_reset_dsppower(dev);
2546 dev->fw_data->fw_size = dev->fw_data->fw->size;
2547 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2548 memcpy(dev->fw_data->pfw_data,
2549 dev->fw_data->fw->data, CHUNK_SIZE);
2550 dev->fw_data->fw_loaded = CHUNK_SIZE;
2551 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2552 usb_sndbulkpipe(dev->udev, 2),
2553 dev->fw_data->pfw_data,
2554 CHUNK_SIZE, s2255_fwchunk_complete,
2556 mod_timer(&dev->timer, jiffies + HZ);
2559 /* standard usb probe function */
2560 static int s2255_probe(struct usb_interface *interface,
2561 const struct usb_device_id *id)
2563 struct s2255_dev *dev = NULL;
2564 struct usb_host_interface *iface_desc;
2565 struct usb_endpoint_descriptor *endpoint;
2567 int retval = -ENOMEM;
2570 dprintk(2, "%s\n", __func__);
2571 /* allocate memory for our device state and initialize it to zero */
2572 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2574 s2255_dev_err(&interface->dev, "out of memory\n");
2577 atomic_set(&dev->channels, 0);
2578 dev->pid = id->idProduct;
2579 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2582 mutex_init(&dev->lock);
2583 mutex_init(&dev->open_lock);
2584 /* grab usb_device and save it */
2585 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2586 if (dev->udev == NULL) {
2587 dev_err(&interface->dev, "null usb device\n");
2591 dprintk(1, "dev: %p, udev %p interface %p\n", dev,
2592 dev->udev, interface);
2593 dev->interface = interface;
2594 /* set up the endpoint information */
2595 iface_desc = interface->cur_altsetting;
2596 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2597 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2598 endpoint = &iface_desc->endpoint[i].desc;
2599 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2600 /* we found the bulk in endpoint */
2601 dev->read_endpoint = endpoint->bEndpointAddress;
2605 if (!dev->read_endpoint) {
2606 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2609 init_timer(&dev->timer);
2610 dev->timer.function = s2255_timer;
2611 dev->timer.data = (unsigned long)dev->fw_data;
2612 init_waitqueue_head(&dev->fw_data->wait_fw);
2613 for (i = 0; i < MAX_CHANNELS; i++) {
2614 init_waitqueue_head(&dev->wait_setmode[i]);
2615 init_waitqueue_head(&dev->wait_vidstatus[i]);
2618 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2619 if (!dev->fw_data->fw_urb) {
2620 dev_err(&interface->dev, "out of memory!\n");
2624 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2625 if (!dev->fw_data->pfw_data) {
2626 dev_err(&interface->dev, "out of memory!\n");
2629 /* load the first chunk */
2630 if (request_firmware(&dev->fw_data->fw,
2631 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2632 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2635 /* check the firmware is valid */
2636 fw_size = dev->fw_data->fw->size;
2637 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2639 if (*pdata != S2255_FW_MARKER) {
2640 printk(KERN_INFO "Firmware invalid.\n");
2644 /* make sure firmware is the latest */
2646 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2647 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2648 dev->dsp_fw_ver = *pRel;
2649 if (*pRel < S2255_CUR_DSP_FWVER)
2650 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
2651 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
2652 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2653 "or above.\n", S2255_MIN_DSP_COLORFILTER);
2655 usb_reset_device(dev->udev);
2656 /* load 2255 board specific */
2657 retval = s2255_board_init(dev);
2659 goto errorBOARDINIT;
2660 spin_lock_init(&dev->slock);
2661 s2255_fwload_start(dev, 0);
2662 /* loads v4l specific */
2663 retval = s2255_probe_v4l(dev);
2665 goto errorBOARDINIT;
2666 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2669 s2255_board_shutdown(dev);
2671 release_firmware(dev->fw_data->fw);
2673 kfree(dev->fw_data->pfw_data);
2675 usb_free_urb(dev->fw_data->fw_urb);
2677 del_timer(&dev->timer);
2679 usb_put_dev(dev->udev);
2681 kfree(dev->fw_data);
2682 mutex_destroy(&dev->open_lock);
2683 mutex_destroy(&dev->lock);
2686 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
2690 /* disconnect routine. when board is removed physically or with rmmod */
2691 static void s2255_disconnect(struct usb_interface *interface)
2693 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
2695 int channels = atomic_read(&dev->channels);
2696 v4l2_device_unregister(&dev->v4l2_dev);
2697 /*see comments in the uvc_driver.c usb disconnect function */
2698 atomic_inc(&dev->channels);
2699 /* unregister each video device. */
2700 for (i = 0; i < channels; i++) {
2701 if (video_is_registered(&dev->vdev[i]))
2702 video_unregister_device(&dev->vdev[i]);
2704 /* wake up any of our timers */
2705 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2706 wake_up(&dev->fw_data->wait_fw);
2707 for (i = 0; i < MAX_CHANNELS; i++) {
2708 dev->setmode_ready[i] = 1;
2709 wake_up(&dev->wait_setmode[i]);
2710 dev->vidstatus_ready[i] = 1;
2711 wake_up(&dev->wait_vidstatus[i]);
2713 if (atomic_dec_and_test(&dev->channels))
2715 dev_info(&interface->dev, "%s\n", __func__);
2718 static struct usb_driver s2255_driver = {
2719 .name = S2255_DRIVER_NAME,
2720 .probe = s2255_probe,
2721 .disconnect = s2255_disconnect,
2722 .id_table = s2255_table,
2725 static int __init usb_s2255_init(void)
2728 /* register this driver with the USB subsystem */
2729 result = usb_register(&s2255_driver);
2731 pr_err(KBUILD_MODNAME
2732 ": usb_register failed. Error number %d\n", result);
2733 dprintk(2, "%s\n", __func__);
2737 static void __exit usb_s2255_exit(void)
2739 usb_deregister(&s2255_driver);
2742 module_init(usb_s2255_init);
2743 module_exit(usb_s2255_exit);
2745 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2746 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2747 MODULE_LICENSE("GPL");