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
19 * -full or half size Grey scale: all 4 channels at once
20 * -half size, color mode YUYV or YUV422P: all 4 channels at once
21 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <linux/module.h>
40 #include <linux/firmware.h>
41 #include <linux/kernel.h>
42 #include <linux/mutex.h>
43 #include <linux/slab.h>
44 #include <linux/videodev2.h>
46 #include <media/videobuf-vmalloc.h>
47 #include <media/v4l2-common.h>
48 #include <media/v4l2-device.h>
49 #include <media/v4l2-ioctl.h>
50 #include <linux/vmalloc.h>
51 #include <linux/usb.h>
53 #define S2255_VERSION "1.22.1"
54 #define FIRMWARE_FILE_NAME "f2255usb.bin"
56 /* default JPEG quality */
57 #define S2255_DEF_JPEG_QUAL 50
58 /* vendor request in */
60 /* vendor request out */
61 #define S2255_VR_OUT 1
63 #define S2255_VR_FW 0x30
64 /* USB endpoint number for configuring the device */
65 #define S2255_CONFIG_EP 2
66 /* maximum time for DSP to start responding after last FW word loaded(ms) */
67 #define S2255_DSP_BOOTTIME 800
68 /* maximum time to wait for firmware to load (ms) */
69 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
70 #define S2255_DEF_BUFS 16
71 #define S2255_SETMODE_TIMEOUT 500
72 #define S2255_VIDSTATUS_TIMEOUT 350
73 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
74 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
75 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
76 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
77 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
78 #define S2255_USB_XFER_SIZE (16 * 1024)
79 #define MAX_CHANNELS 4
81 /* maximum size is PAL full size plus room for the marker header(s) */
82 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
83 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
84 #define LINE_SZ_4CIFS_NTSC 640
85 #define LINE_SZ_2CIFS_NTSC 640
86 #define LINE_SZ_1CIFS_NTSC 320
87 #define LINE_SZ_4CIFS_PAL 704
88 #define LINE_SZ_2CIFS_PAL 704
89 #define LINE_SZ_1CIFS_PAL 352
90 #define NUM_LINES_4CIFS_NTSC 240
91 #define NUM_LINES_2CIFS_NTSC 240
92 #define NUM_LINES_1CIFS_NTSC 240
93 #define NUM_LINES_4CIFS_PAL 288
94 #define NUM_LINES_2CIFS_PAL 288
95 #define NUM_LINES_1CIFS_PAL 288
96 #define LINE_SZ_DEF 640
97 #define NUM_LINES_DEF 240
100 /* predefined settings */
101 #define FORMAT_NTSC 1
104 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
105 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
106 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
107 /* SCALE_4CIFSI is the 2 fields interpolated into one */
108 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
110 #define COLOR_YUVPL 1 /* YUV planar */
111 #define COLOR_YUVPK 2 /* YUV packed */
112 #define COLOR_Y8 4 /* monochrome */
113 #define COLOR_JPG 5 /* JPEG */
115 #define MASK_COLOR 0x000000ff
116 #define MASK_JPG_QUALITY 0x0000ff00
117 #define MASK_INPUT_TYPE 0x000f0000
118 /* frame decimation. */
119 #define FDEC_1 1 /* capture every frame. default */
120 #define FDEC_2 2 /* capture every 2nd frame */
121 #define FDEC_3 3 /* capture every 3rd frame */
122 #define FDEC_5 5 /* capture every 5th frame */
124 /*-------------------------------------------------------
125 * Default mode parameters.
126 *-------------------------------------------------------*/
127 #define DEF_SCALE SCALE_4CIFS
128 #define DEF_COLOR COLOR_YUVPL
129 #define DEF_FDEC FDEC_1
131 #define DEF_CONTRAST 0x5c
132 #define DEF_SATURATION 0x80
135 /* usb config commands */
136 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
137 #define CMD_2255 0xc2255000
138 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
139 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
140 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
141 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
144 u32 format; /* input video format (NTSC, PAL) */
145 u32 scale; /* output video scale */
146 u32 color; /* output video color format */
147 u32 fdec; /* frame decimation */
148 u32 bright; /* brightness */
149 u32 contrast; /* contrast */
150 u32 saturation; /* saturation */
151 u32 hue; /* hue (NTSC only)*/
152 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
153 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
154 u32 restart; /* if DSP requires restart */
158 #define S2255_READ_IDLE 0
159 #define S2255_READ_FRAME 1
161 /* frame structure */
162 struct s2255_framei {
164 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
165 void *lpvbits; /* image data */
166 unsigned long cur_size; /* current data copied to it */
169 /* image buffer structure */
170 struct s2255_bufferi {
171 unsigned long dwFrames; /* number of frames in buffer */
172 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
175 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
176 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
177 DEF_HUE, 0, DEF_USB_BLOCK, 0}
179 struct s2255_dmaqueue {
180 struct list_head active;
181 struct s2255_dev *dev;
184 /* for firmware loading, fw_state */
185 #define S2255_FW_NOTLOADED 0
186 #define S2255_FW_LOADED_DSPWAIT 1
187 #define S2255_FW_SUCCESS 2
188 #define S2255_FW_FAILED 3
189 #define S2255_FW_DISCONNECTING 4
190 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
191 /* 2255 read states */
192 #define S2255_READ_IDLE 0
193 #define S2255_READ_FRAME 1
200 wait_queue_head_t wait_fw;
201 const struct firmware *fw;
204 struct s2255_pipeinfo {
205 u32 max_transfer_size;
206 u32 cur_transfer_size;
210 void *dev; /* back pointer to s2255_dev struct*/
215 struct s2255_fmt; /*forward declaration */
218 struct s2255_channel {
219 struct video_device vdev;
221 struct s2255_dmaqueue vidq;
222 struct s2255_bufferi buffer;
223 struct s2255_mode mode;
224 /* jpeg compression */
225 struct v4l2_jpegcompression jc;
226 /* capture parameters (for high quality mode full size) */
227 struct v4l2_captureparm cap_parm;
232 /* allocated image size */
233 unsigned long req_image_size;
234 /* received packet size */
235 unsigned long pkt_size;
237 unsigned long frame_count;
240 /* if channel configured to default state */
242 wait_queue_head_t wait_setmode;
244 /* video status items */
246 wait_queue_head_t wait_vidstatus;
250 const struct s2255_fmt *fmt;
251 int idx; /* channel number on device, 0-3 */
256 struct s2255_channel channel[MAX_CHANNELS];
257 struct v4l2_device v4l2_dev;
258 atomic_t num_channels;
260 struct mutex lock; /* channels[].vdev.lock */
261 struct usb_device *udev;
262 struct usb_interface *interface;
264 struct timer_list timer;
265 struct s2255_fw *fw_data;
266 struct s2255_pipeinfo pipe;
267 u32 cc; /* current channel */
271 /* dsp firmware version (f2255usb.bin) */
273 u16 pid; /* product id */
276 static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
278 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
287 /* buffer for one video frame */
288 struct s2255_buffer {
289 /* common v4l buffer stuff -- must be first */
290 struct videobuf_buffer vb;
291 const struct s2255_fmt *fmt;
295 struct s2255_dev *dev;
296 struct videobuf_queue vb_vidq;
297 enum v4l2_buf_type type;
298 struct s2255_channel *channel;
302 /* current cypress EEPROM firmware version */
303 #define S2255_CUR_USB_FWVER ((3 << 8) | 12)
304 /* current DSP FW version */
305 #define S2255_CUR_DSP_FWVER 10104
306 /* Need DSP version 5+ for video status feature */
307 #define S2255_MIN_DSP_STATUS 5
308 #define S2255_MIN_DSP_COLORFILTER 8
309 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
311 /* private V4L2 controls */
314 * The following chart displays how COLORFILTER should be set
315 * =========================================================
316 * = fourcc = COLORFILTER =
317 * = ===============================
319 * =========================================================
320 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
321 * = = s-video or = composite =
322 * = = B/W camera = input =
323 * =========================================================
324 * = other = color, svideo = color, =
326 * =========================================================
329 * channels 0-3 on 2255 are composite
330 * channels 0-1 on 2257 are composite, 2-3 are s-video
331 * If COLORFILTER is 0 with a composite color camera connected,
332 * the output will appear monochrome but hatching
334 * COLORFILTER is different from "color killer" and "color effects"
337 #define S2255_V4L2_YC_ON 1
338 #define S2255_V4L2_YC_OFF 0
339 #define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
341 /* frame prefix size (sent once every frame) */
342 #define PREFIX_SIZE 512
344 /* Channels on box are in reverse order */
345 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
348 static int *s2255_debug = &debug;
350 static int s2255_start_readpipe(struct s2255_dev *dev);
351 static void s2255_stop_readpipe(struct s2255_dev *dev);
352 static int s2255_start_acquire(struct s2255_channel *channel);
353 static int s2255_stop_acquire(struct s2255_channel *channel);
354 static void s2255_fillbuff(struct s2255_channel *chn, struct s2255_buffer *buf,
356 static int s2255_set_mode(struct s2255_channel *chan, struct s2255_mode *mode);
357 static int s2255_board_shutdown(struct s2255_dev *dev);
358 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
359 static void s2255_destroy(struct s2255_dev *dev);
360 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
361 u16 index, u16 value, void *buf,
362 s32 buf_len, int bOut);
364 /* dev_err macro with driver name */
365 #define S2255_DRIVER_NAME "s2255"
366 #define s2255_dev_err(dev, fmt, arg...) \
367 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
369 #define dprintk(level, fmt, arg...) \
371 if (*s2255_debug >= (level)) { \
372 printk(KERN_DEBUG S2255_DRIVER_NAME \
377 static struct usb_driver s2255_driver;
379 /* Declare static vars that will be used as parameters */
380 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
382 /* start video number */
383 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
385 /* Enable jpeg capture. */
386 static int jpeg_enable = 1;
388 module_param(debug, int, 0644);
389 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
390 module_param(vid_limit, int, 0644);
391 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
392 module_param(video_nr, int, 0644);
393 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
394 module_param(jpeg_enable, int, 0644);
395 MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1");
397 /* USB device table */
398 #define USB_SENSORAY_VID 0x1943
399 static struct usb_device_id s2255_table[] = {
400 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
401 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
402 { } /* Terminating entry */
404 MODULE_DEVICE_TABLE(usb, s2255_table);
406 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
409 /* JPEG formats must be defined last to support jpeg_enable parameter */
410 static const struct s2255_fmt formats[] = {
412 .name = "4:2:2, planar, YUV422P",
413 .fourcc = V4L2_PIX_FMT_YUV422P,
417 .name = "4:2:2, packed, YUYV",
418 .fourcc = V4L2_PIX_FMT_YUYV,
422 .name = "4:2:2, packed, UYVY",
423 .fourcc = V4L2_PIX_FMT_UYVY,
427 .fourcc = V4L2_PIX_FMT_GREY,
431 .fourcc = V4L2_PIX_FMT_JPEG,
435 .fourcc = V4L2_PIX_FMT_MJPEG,
440 static int norm_maxw(struct video_device *vdev)
442 return (vdev->current_norm & V4L2_STD_NTSC) ?
443 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
446 static int norm_maxh(struct video_device *vdev)
448 return (vdev->current_norm & V4L2_STD_NTSC) ?
449 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
452 static int norm_minw(struct video_device *vdev)
454 return (vdev->current_norm & V4L2_STD_NTSC) ?
455 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
458 static int norm_minh(struct video_device *vdev)
460 return (vdev->current_norm & V4L2_STD_NTSC) ?
461 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
466 * TODO: fixme: move YUV reordering to hardware
467 * converts 2255 planar format to yuyv or uyvy
469 static void planar422p_to_yuv_packed(const unsigned char *in,
471 int width, int height,
477 unsigned long size = height * width;
479 pY = (unsigned char *)in;
480 pCr = (unsigned char *)in + height * width;
481 pCb = (unsigned char *)in + height * width + (height * width / 2);
482 for (i = 0; i < size * 2; i += 4) {
483 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
484 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
485 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
486 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
491 static void s2255_reset_dsppower(struct s2255_dev *dev)
493 s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1);
495 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
497 s2255_vendor_req(dev, 0x10, 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_channel *channel, int jpgsize)
582 struct s2255_dmaqueue *dma_q = &channel->vidq;
583 struct s2255_buffer *buf;
584 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
585 unsigned long flags = 0;
587 spin_lock_irqsave(&dev->slock, flags);
588 if (list_empty(&dma_q->active)) {
589 dprintk(1, "No active queue to serve\n");
593 buf = list_entry(dma_q->active.next,
594 struct s2255_buffer, vb.queue);
595 list_del(&buf->vb.queue);
596 v4l2_get_timestamp(&buf->vb.ts);
597 s2255_fillbuff(channel, buf, jpgsize);
598 wake_up(&buf->vb.done);
599 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
601 spin_unlock_irqrestore(&dev->slock, flags);
605 static const struct s2255_fmt *format_by_fourcc(int fourcc)
608 for (i = 0; i < ARRAY_SIZE(formats); i++) {
609 if (-1 == formats[i].fourcc)
611 if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) ||
612 (formats[i].fourcc == V4L2_PIX_FMT_MJPEG)))
614 if (formats[i].fourcc == fourcc)
620 /* video buffer vmalloc implementation based partly on VIVI driver which is
621 * Copyright (c) 2006 by
622 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
623 * Ted Walther <ted--a.t--enumera.com>
624 * John Sokol <sokol--a.t--videotechnology.com>
625 * http://v4l.videotechnology.com/
628 static void s2255_fillbuff(struct s2255_channel *channel,
629 struct s2255_buffer *buf, int jpgsize)
633 char *vbuf = videobuf_to_vmalloc(&buf->vb);
634 unsigned long last_frame;
638 last_frame = channel->last_frame;
639 if (last_frame != -1) {
641 (const char *)channel->buffer.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 case V4L2_PIX_FMT_MJPEG:
655 buf->vb.size = jpgsize;
656 memcpy(vbuf, tmpbuf, buf->vb.size);
658 case V4L2_PIX_FMT_YUV422P:
660 buf->vb.width * buf->vb.height * 2);
663 printk(KERN_DEBUG "s2255: unknown format?\n");
665 channel->last_frame = -1;
667 printk(KERN_ERR "s2255: =======no frame\n");
671 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
672 (unsigned long)vbuf, pos);
673 /* tell v4l buffer was filled */
675 buf->vb.field_count = channel->frame_count * 2;
676 v4l2_get_timestamp(&buf->vb.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;
689 struct s2255_channel *channel = fh->channel;
690 *size = channel->width * channel->height * (channel->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_channel *channel = fh->channel;
714 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
716 int w = channel->width;
717 int h = channel->height;
718 dprintk(4, "%s, field=%d\n", __func__, field);
719 if (channel->fmt == NULL)
722 if ((w < norm_minw(&channel->vdev)) ||
723 (w > norm_maxw(&channel->vdev)) ||
724 (h < norm_minh(&channel->vdev)) ||
725 (h > norm_maxh(&channel->vdev))) {
726 dprintk(4, "invalid buffer prepare\n");
729 buf->vb.size = w * h * (channel->fmt->depth >> 3);
730 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
731 dprintk(4, "invalid buffer prepare\n");
735 buf->fmt = channel->fmt;
738 buf->vb.field = field;
740 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
741 rc = videobuf_iolock(vq, &buf->vb, NULL);
746 buf->vb.state = VIDEOBUF_PREPARED;
749 free_buffer(vq, buf);
753 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
755 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
756 struct s2255_fh *fh = vq->priv_data;
757 struct s2255_channel *channel = fh->channel;
758 struct s2255_dmaqueue *vidq = &channel->vidq;
759 dprintk(1, "%s\n", __func__);
760 buf->vb.state = VIDEOBUF_QUEUED;
761 list_add_tail(&buf->vb.queue, &vidq->active);
764 static void buffer_release(struct videobuf_queue *vq,
765 struct videobuf_buffer *vb)
767 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
768 struct s2255_fh *fh = vq->priv_data;
769 dprintk(4, "%s %d\n", __func__, fh->channel->idx);
770 free_buffer(vq, buf);
773 static struct videobuf_queue_ops s2255_video_qops = {
774 .buf_setup = buffer_setup,
775 .buf_prepare = buffer_prepare,
776 .buf_queue = buffer_queue,
777 .buf_release = buffer_release,
781 static int res_get(struct s2255_fh *fh)
783 struct s2255_channel *channel = fh->channel;
785 if (channel->resources)
786 return 0; /* no, someone else uses it */
787 /* it's free, grab it */
788 channel->resources = 1;
790 dprintk(1, "s2255: res: get\n");
794 static int res_locked(struct s2255_fh *fh)
796 return fh->channel->resources;
799 static int res_check(struct s2255_fh *fh)
801 return fh->resources;
805 static void res_free(struct s2255_fh *fh)
807 struct s2255_channel *channel = fh->channel;
808 channel->resources = 0;
810 dprintk(1, "res: put\n");
813 static int vidioc_querymenu(struct file *file, void *priv,
814 struct v4l2_querymenu *qmenu)
816 static const char *colorfilter[] = {
821 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
823 const char **menu_items = colorfilter;
824 for (i = 0; i < qmenu->index && menu_items[i]; i++)
825 ; /* do nothing (from v4l2-common.c) */
826 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
828 strlcpy(qmenu->name, menu_items[qmenu->index],
829 sizeof(qmenu->name));
832 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
835 static int vidioc_querycap(struct file *file, void *priv,
836 struct v4l2_capability *cap)
838 struct s2255_fh *fh = file->private_data;
839 struct s2255_dev *dev = fh->dev;
840 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
841 strlcpy(cap->card, "s2255", sizeof(cap->card));
842 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
843 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
847 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
848 struct v4l2_fmtdesc *f)
850 int index = f->index;
852 if (index >= ARRAY_SIZE(formats))
854 if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) ||
855 (formats[index].fourcc == V4L2_PIX_FMT_MJPEG)))
857 dprintk(4, "name %s\n", formats[index].name);
858 strlcpy(f->description, formats[index].name, sizeof(f->description));
859 f->pixelformat = formats[index].fourcc;
863 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
864 struct v4l2_format *f)
866 struct s2255_fh *fh = priv;
867 struct s2255_channel *channel = fh->channel;
869 f->fmt.pix.width = channel->width;
870 f->fmt.pix.height = channel->height;
871 f->fmt.pix.field = fh->vb_vidq.field;
872 f->fmt.pix.pixelformat = channel->fmt->fourcc;
873 f->fmt.pix.bytesperline = f->fmt.pix.width * (channel->fmt->depth >> 3);
874 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
878 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
879 struct v4l2_format *f)
881 const struct s2255_fmt *fmt;
882 enum v4l2_field field;
884 struct s2255_fh *fh = priv;
885 struct s2255_channel *channel = fh->channel;
888 (channel->vdev.current_norm & V4L2_STD_NTSC) ? 1 : 0;
890 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
895 field = f->fmt.pix.field;
896 if (field == V4L2_FIELD_ANY)
899 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
900 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
903 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
904 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
906 field = V4L2_FIELD_SEQ_TB;
907 } else if (!((field == V4L2_FIELD_INTERLACED) ||
908 (field == V4L2_FIELD_SEQ_TB) ||
909 (field == V4L2_FIELD_INTERLACED_TB))) {
910 dprintk(1, "unsupported field setting\n");
914 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
916 field = V4L2_FIELD_TOP;
917 } else if (!((field == V4L2_FIELD_TOP) ||
918 (field == V4L2_FIELD_BOTTOM))) {
919 dprintk(1, "unsupported field setting\n");
924 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
925 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
926 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
934 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
935 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
937 field = V4L2_FIELD_SEQ_TB;
938 } else if (!((field == V4L2_FIELD_INTERLACED) ||
939 (field == V4L2_FIELD_SEQ_TB) ||
940 (field == V4L2_FIELD_INTERLACED_TB))) {
941 dprintk(1, "unsupported field setting\n");
945 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
947 field = V4L2_FIELD_TOP;
948 } else if (!((field == V4L2_FIELD_TOP) ||
949 (field == V4L2_FIELD_BOTTOM))) {
950 dprintk(1, "unsupported field setting\n");
954 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
955 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
956 field = V4L2_FIELD_SEQ_TB;
957 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
958 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
959 field = V4L2_FIELD_TOP;
960 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
961 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
962 field = V4L2_FIELD_TOP;
964 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
965 field = V4L2_FIELD_TOP;
968 f->fmt.pix.field = field;
969 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
970 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
971 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
972 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
976 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
977 struct v4l2_format *f)
979 struct s2255_fh *fh = priv;
980 struct s2255_channel *channel = fh->channel;
981 const struct s2255_fmt *fmt;
982 struct videobuf_queue *q = &fh->vb_vidq;
983 struct s2255_mode mode;
986 ret = vidioc_try_fmt_vid_cap(file, fh, f);
991 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
996 mutex_lock(&q->vb_lock);
998 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
999 dprintk(1, "queue busy\n");
1004 if (res_locked(fh)) {
1005 dprintk(1, "%s: channel busy\n", __func__);
1009 mode = channel->mode;
1011 channel->width = f->fmt.pix.width;
1012 channel->height = f->fmt.pix.height;
1013 fh->vb_vidq.field = f->fmt.pix.field;
1015 if (channel->width > norm_minw(&channel->vdev)) {
1016 if (channel->height > norm_minh(&channel->vdev)) {
1017 if (channel->cap_parm.capturemode &
1018 V4L2_MODE_HIGHQUALITY)
1019 mode.scale = SCALE_4CIFSI;
1021 mode.scale = SCALE_4CIFS;
1023 mode.scale = SCALE_2CIFS;
1026 mode.scale = SCALE_1CIFS;
1029 switch (channel->fmt->fourcc) {
1030 case V4L2_PIX_FMT_GREY:
1031 mode.color &= ~MASK_COLOR;
1032 mode.color |= COLOR_Y8;
1034 case V4L2_PIX_FMT_JPEG:
1035 case V4L2_PIX_FMT_MJPEG:
1036 mode.color &= ~MASK_COLOR;
1037 mode.color |= COLOR_JPG;
1038 mode.color |= (channel->jc.quality << 8);
1040 case V4L2_PIX_FMT_YUV422P:
1041 mode.color &= ~MASK_COLOR;
1042 mode.color |= COLOR_YUVPL;
1044 case V4L2_PIX_FMT_YUYV:
1045 case V4L2_PIX_FMT_UYVY:
1047 mode.color &= ~MASK_COLOR;
1048 mode.color |= COLOR_YUVPK;
1051 if ((mode.color & MASK_COLOR) != (channel->mode.color & MASK_COLOR))
1053 else if (mode.scale != channel->mode.scale)
1055 else if (mode.format != channel->mode.format)
1057 channel->mode = mode;
1058 (void) s2255_set_mode(channel, &mode);
1061 mutex_unlock(&q->vb_lock);
1065 static int vidioc_reqbufs(struct file *file, void *priv,
1066 struct v4l2_requestbuffers *p)
1069 struct s2255_fh *fh = priv;
1070 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1074 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1077 struct s2255_fh *fh = priv;
1078 rc = videobuf_querybuf(&fh->vb_vidq, p);
1082 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1085 struct s2255_fh *fh = priv;
1086 rc = videobuf_qbuf(&fh->vb_vidq, p);
1090 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1093 struct s2255_fh *fh = priv;
1094 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1098 /* write to the configuration pipe, synchronously */
1099 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1106 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1107 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1112 static u32 get_transfer_size(struct s2255_mode *mode)
1114 int linesPerFrame = LINE_SZ_DEF;
1115 int pixelsPerLine = NUM_LINES_DEF;
1118 unsigned int mask_mult;
1123 if (mode->format == FORMAT_NTSC) {
1124 switch (mode->scale) {
1127 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1128 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1131 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1132 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1135 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1136 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1141 } else if (mode->format == FORMAT_PAL) {
1142 switch (mode->scale) {
1145 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1146 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1149 linesPerFrame = NUM_LINES_2CIFS_PAL;
1150 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1153 linesPerFrame = NUM_LINES_1CIFS_PAL;
1154 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1160 outImageSize = linesPerFrame * pixelsPerLine;
1161 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1162 /* 2 bytes/pixel if not monochrome */
1166 /* total bytes to send including prefix and 4K padding;
1167 must be a multiple of USB_READ_SIZE */
1168 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1169 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1170 /* if size not a multiple of USB_READ_SIZE */
1171 if (usbInSize & ~mask_mult)
1172 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1176 static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
1178 struct device *dev = &sdev->udev->dev;
1179 dev_info(dev, "------------------------------------------------\n");
1180 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1181 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
1182 dev_info(dev, "bright: 0x%x\n", mode->bright);
1183 dev_info(dev, "------------------------------------------------\n");
1187 * set mode is the function which controls the DSP.
1188 * the restart parameter in struct s2255_mode should be set whenever
1189 * the image size could change via color format, video system or image
1191 * When the restart parameter is set, we sleep for ONE frame to allow the
1192 * DSP time to get the new frame
1194 static int s2255_set_mode(struct s2255_channel *channel,
1195 struct s2255_mode *mode)
1199 unsigned long chn_rev;
1200 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1201 chn_rev = G_chnmap[channel->idx];
1202 dprintk(3, "%s channel: %d\n", __func__, channel->idx);
1203 /* if JPEG, set the quality */
1204 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1205 mode->color &= ~MASK_COLOR;
1206 mode->color |= COLOR_JPG;
1207 mode->color &= ~MASK_JPG_QUALITY;
1208 mode->color |= (channel->jc.quality << 8);
1211 channel->mode = *mode;
1212 channel->req_image_size = get_transfer_size(mode);
1213 dprintk(1, "%s: reqsize %ld\n", __func__, channel->req_image_size);
1214 buffer = kzalloc(512, GFP_KERNEL);
1215 if (buffer == NULL) {
1216 dev_err(&dev->udev->dev, "out of mem\n");
1220 buffer[0] = IN_DATA_TOKEN;
1221 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1222 buffer[2] = CMD_SET_MODE;
1223 memcpy(&buffer[3], &channel->mode, sizeof(struct s2255_mode));
1224 channel->setmode_ready = 0;
1225 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1227 s2255_print_cfg(dev, mode);
1229 /* wait at least 3 frames before continuing */
1230 if (mode->restart) {
1231 wait_event_timeout(channel->wait_setmode,
1232 (channel->setmode_ready != 0),
1233 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1234 if (channel->setmode_ready != 1) {
1235 printk(KERN_DEBUG "s2255: no set mode response\n");
1239 /* clear the restart flag */
1240 channel->mode.restart = 0;
1241 dprintk(1, "%s chn %d, result: %d\n", __func__, channel->idx, res);
1245 static int s2255_cmd_status(struct s2255_channel *channel, u32 *pstatus)
1250 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1251 chn_rev = G_chnmap[channel->idx];
1252 dprintk(4, "%s chan %d\n", __func__, channel->idx);
1253 buffer = kzalloc(512, GFP_KERNEL);
1254 if (buffer == NULL) {
1255 dev_err(&dev->udev->dev, "out of mem\n");
1258 /* form the get vid status command */
1259 buffer[0] = IN_DATA_TOKEN;
1260 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1261 buffer[2] = CMD_STATUS;
1263 channel->vidstatus_ready = 0;
1264 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1266 wait_event_timeout(channel->wait_vidstatus,
1267 (channel->vidstatus_ready != 0),
1268 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1269 if (channel->vidstatus_ready != 1) {
1270 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1273 *pstatus = channel->vidstatus;
1274 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1278 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1281 struct s2255_fh *fh = priv;
1282 struct s2255_dev *dev = fh->dev;
1283 struct s2255_channel *channel = fh->channel;
1285 dprintk(4, "%s\n", __func__);
1286 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1287 dev_err(&dev->udev->dev, "invalid fh type0\n");
1290 if (i != fh->type) {
1291 dev_err(&dev->udev->dev, "invalid fh type1\n");
1296 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1299 channel->last_frame = -1;
1300 channel->bad_payload = 0;
1301 channel->cur_frame = 0;
1302 channel->frame_count = 0;
1303 for (j = 0; j < SYS_FRAMES; j++) {
1304 channel->buffer.frame[j].ulState = S2255_READ_IDLE;
1305 channel->buffer.frame[j].cur_size = 0;
1307 res = videobuf_streamon(&fh->vb_vidq);
1309 s2255_start_acquire(channel);
1310 channel->b_acquire = 1;
1317 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1319 struct s2255_fh *fh = priv;
1320 dprintk(4, "%s\n, channel: %d", __func__, fh->channel->idx);
1321 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1322 printk(KERN_ERR "invalid fh type0\n");
1325 if (i != fh->type) {
1326 printk(KERN_ERR "invalid type i\n");
1329 s2255_stop_acquire(fh->channel);
1330 videobuf_streamoff(&fh->vb_vidq);
1335 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1337 struct s2255_fh *fh = priv;
1338 struct s2255_mode mode;
1339 struct videobuf_queue *q = &fh->vb_vidq;
1341 mutex_lock(&q->vb_lock);
1342 if (videobuf_queue_is_busy(q)) {
1343 dprintk(1, "queue busy\n");
1347 if (res_locked(fh)) {
1348 dprintk(1, "can't change standard after started\n");
1352 mode = fh->channel->mode;
1353 if (*i & V4L2_STD_NTSC) {
1354 dprintk(4, "%s NTSC\n", __func__);
1355 /* if changing format, reset frame decimation/intervals */
1356 if (mode.format != FORMAT_NTSC) {
1358 mode.format = FORMAT_NTSC;
1361 } else if (*i & V4L2_STD_PAL) {
1362 dprintk(4, "%s PAL\n", __func__);
1363 if (mode.format != FORMAT_PAL) {
1365 mode.format = FORMAT_PAL;
1372 s2255_set_mode(fh->channel, &mode);
1374 mutex_unlock(&q->vb_lock);
1378 /* Sensoray 2255 is a multiple channel capture device.
1379 It does not have a "crossbar" of inputs.
1380 We use one V4L device per channel. The user must
1381 be aware that certain combinations are not allowed.
1382 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1383 at once in color(you can do full fps on 4 channels with greyscale.
1385 static int vidioc_enum_input(struct file *file, void *priv,
1386 struct v4l2_input *inp)
1388 struct s2255_fh *fh = priv;
1389 struct s2255_dev *dev = fh->dev;
1390 struct s2255_channel *channel = fh->channel;
1392 if (inp->index != 0)
1394 inp->type = V4L2_INPUT_TYPE_CAMERA;
1395 inp->std = S2255_NORMS;
1397 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1399 rc = s2255_cmd_status(fh->channel, &status);
1400 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1402 inp->status = (status & 0x01) ? 0
1403 : V4L2_IN_ST_NO_SIGNAL;
1408 strlcpy(inp->name, "Composite", sizeof(inp->name));
1411 strlcpy(inp->name, (channel->idx < 2) ? "Composite" : "S-Video",
1418 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1423 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1430 /* --- controls ---------------------------------------------- */
1431 static int vidioc_queryctrl(struct file *file, void *priv,
1432 struct v4l2_queryctrl *qc)
1434 struct s2255_fh *fh = priv;
1435 struct s2255_channel *channel = fh->channel;
1436 struct s2255_dev *dev = fh->dev;
1438 case V4L2_CID_BRIGHTNESS:
1439 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1441 case V4L2_CID_CONTRAST:
1442 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1444 case V4L2_CID_SATURATION:
1445 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1448 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1450 case V4L2_CID_PRIVATE_COLORFILTER:
1451 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1453 if ((dev->pid == 0x2257) && (channel->idx > 1))
1455 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1456 qc->type = V4L2_CTRL_TYPE_MENU;
1460 qc->default_value = 1;
1466 dprintk(4, "%s, id %d\n", __func__, qc->id);
1470 static int vidioc_g_ctrl(struct file *file, void *priv,
1471 struct v4l2_control *ctrl)
1473 struct s2255_fh *fh = priv;
1474 struct s2255_dev *dev = fh->dev;
1475 struct s2255_channel *channel = fh->channel;
1477 case V4L2_CID_BRIGHTNESS:
1478 ctrl->value = channel->mode.bright;
1480 case V4L2_CID_CONTRAST:
1481 ctrl->value = channel->mode.contrast;
1483 case V4L2_CID_SATURATION:
1484 ctrl->value = channel->mode.saturation;
1487 ctrl->value = channel->mode.hue;
1489 case V4L2_CID_PRIVATE_COLORFILTER:
1490 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1492 if ((dev->pid == 0x2257) && (channel->idx > 1))
1494 ctrl->value = !((channel->mode.color & MASK_INPUT_TYPE) >> 16);
1499 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1503 static int vidioc_s_ctrl(struct file *file, void *priv,
1504 struct v4l2_control *ctrl)
1506 struct s2255_fh *fh = priv;
1507 struct s2255_channel *channel = fh->channel;
1508 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1509 struct s2255_mode mode;
1510 mode = channel->mode;
1511 dprintk(4, "%s\n", __func__);
1512 /* update the mode to the corresponding value */
1514 case V4L2_CID_BRIGHTNESS:
1515 mode.bright = ctrl->value;
1517 case V4L2_CID_CONTRAST:
1518 mode.contrast = ctrl->value;
1521 mode.hue = ctrl->value;
1523 case V4L2_CID_SATURATION:
1524 mode.saturation = ctrl->value;
1526 case V4L2_CID_PRIVATE_COLORFILTER:
1527 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1529 if ((dev->pid == 0x2257) && (channel->idx > 1))
1531 mode.color &= ~MASK_INPUT_TYPE;
1532 mode.color |= ((ctrl->value ? 0 : 1) << 16);
1538 /* set mode here. Note: stream does not need restarted.
1539 some V4L programs restart stream unnecessarily
1542 s2255_set_mode(fh->channel, &mode);
1546 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1547 struct v4l2_jpegcompression *jc)
1549 struct s2255_fh *fh = priv;
1550 struct s2255_channel *channel = fh->channel;
1552 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1556 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1557 const struct v4l2_jpegcompression *jc)
1559 struct s2255_fh *fh = priv;
1560 struct s2255_channel *channel = fh->channel;
1561 if (jc->quality < 0 || jc->quality > 100)
1563 channel->jc.quality = jc->quality;
1564 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1568 static int vidioc_g_parm(struct file *file, void *priv,
1569 struct v4l2_streamparm *sp)
1571 struct s2255_fh *fh = priv;
1572 __u32 def_num, def_dem;
1573 struct s2255_channel *channel = fh->channel;
1574 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1576 memset(sp, 0, sizeof(struct v4l2_streamparm));
1577 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1578 sp->parm.capture.capturemode = channel->cap_parm.capturemode;
1579 def_num = (channel->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1580 def_dem = (channel->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1581 sp->parm.capture.timeperframe.denominator = def_dem;
1582 switch (channel->mode.fdec) {
1585 sp->parm.capture.timeperframe.numerator = def_num;
1588 sp->parm.capture.timeperframe.numerator = def_num * 2;
1591 sp->parm.capture.timeperframe.numerator = def_num * 3;
1594 sp->parm.capture.timeperframe.numerator = def_num * 5;
1597 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1598 sp->parm.capture.capturemode,
1599 sp->parm.capture.timeperframe.numerator,
1600 sp->parm.capture.timeperframe.denominator);
1604 static int vidioc_s_parm(struct file *file, void *priv,
1605 struct v4l2_streamparm *sp)
1607 struct s2255_fh *fh = priv;
1608 struct s2255_channel *channel = fh->channel;
1609 struct s2255_mode mode;
1611 __u32 def_num, def_dem;
1612 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1614 mode = channel->mode;
1615 /* high quality capture mode requires a stream restart */
1616 if (channel->cap_parm.capturemode
1617 != sp->parm.capture.capturemode && res_locked(fh))
1619 def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
1620 def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
1621 if (def_dem != sp->parm.capture.timeperframe.denominator)
1622 sp->parm.capture.timeperframe.numerator = def_num;
1623 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1624 sp->parm.capture.timeperframe.numerator = def_num;
1625 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1626 sp->parm.capture.timeperframe.numerator = def_num * 2;
1628 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1629 sp->parm.capture.timeperframe.numerator = def_num * 3;
1632 sp->parm.capture.timeperframe.numerator = def_num * 5;
1636 sp->parm.capture.timeperframe.denominator = def_dem;
1637 s2255_set_mode(channel, &mode);
1638 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1640 sp->parm.capture.capturemode,
1641 sp->parm.capture.timeperframe.numerator,
1642 sp->parm.capture.timeperframe.denominator, fdec);
1646 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1647 struct v4l2_frmivalenum *fe)
1650 #define NUM_FRAME_ENUMS 4
1651 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1652 if (fe->index >= NUM_FRAME_ENUMS)
1654 switch (fe->width) {
1656 if (fe->height != 240 && fe->height != 480)
1661 if (fe->height != 240)
1666 if (fe->height != 288 && fe->height != 576)
1670 if (fe->height != 288)
1676 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1677 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1678 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1679 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1680 fe->discrete.denominator);
1684 static int __s2255_open(struct file *file)
1686 struct video_device *vdev = video_devdata(file);
1687 struct s2255_channel *channel = video_drvdata(file);
1688 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1689 struct s2255_fh *fh;
1690 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1692 dprintk(1, "s2255: open called (dev=%s)\n",
1693 video_device_node_name(vdev));
1694 state = atomic_read(&dev->fw_data->fw_state);
1696 case S2255_FW_DISCONNECTING:
1698 case S2255_FW_FAILED:
1699 s2255_dev_err(&dev->udev->dev,
1700 "firmware load failed. retrying.\n");
1701 s2255_fwload_start(dev, 1);
1702 wait_event_timeout(dev->fw_data->wait_fw,
1703 ((atomic_read(&dev->fw_data->fw_state)
1704 == S2255_FW_SUCCESS) ||
1705 (atomic_read(&dev->fw_data->fw_state)
1706 == S2255_FW_DISCONNECTING)),
1707 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1708 /* state may have changed, re-read */
1709 state = atomic_read(&dev->fw_data->fw_state);
1711 case S2255_FW_NOTLOADED:
1712 case S2255_FW_LOADED_DSPWAIT:
1713 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1714 driver loaded and then device immediately opened */
1715 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1716 wait_event_timeout(dev->fw_data->wait_fw,
1717 ((atomic_read(&dev->fw_data->fw_state)
1718 == S2255_FW_SUCCESS) ||
1719 (atomic_read(&dev->fw_data->fw_state)
1720 == S2255_FW_DISCONNECTING)),
1721 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1722 /* state may have changed, re-read */
1723 state = atomic_read(&dev->fw_data->fw_state);
1725 case S2255_FW_SUCCESS:
1729 /* state may have changed in above switch statement */
1731 case S2255_FW_SUCCESS:
1733 case S2255_FW_FAILED:
1734 printk(KERN_INFO "2255 firmware load failed.\n");
1736 case S2255_FW_DISCONNECTING:
1737 printk(KERN_INFO "%s: disconnecting\n", __func__);
1739 case S2255_FW_LOADED_DSPWAIT:
1740 case S2255_FW_NOTLOADED:
1741 printk(KERN_INFO "%s: firmware not loaded yet"
1742 "please try again later\n",
1745 * Timeout on firmware load means device unusable.
1746 * Set firmware failure state.
1747 * On next s2255_open the firmware will be reloaded.
1749 atomic_set(&dev->fw_data->fw_state,
1753 printk(KERN_INFO "%s: unknown state\n", __func__);
1756 /* allocate + initialize per filehandle data */
1757 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1760 file->private_data = fh;
1762 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1763 fh->channel = channel;
1764 if (!channel->configured) {
1765 /* configure channel to default state */
1766 channel->fmt = &formats[0];
1767 s2255_set_mode(channel, &channel->mode);
1768 channel->configured = 1;
1770 dprintk(1, "%s: dev=%s type=%s\n", __func__,
1771 video_device_node_name(vdev), v4l2_type_names[type]);
1772 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
1773 (unsigned long)fh, (unsigned long)dev,
1774 (unsigned long)&channel->vidq);
1775 dprintk(4, "%s: list_empty active=%d\n", __func__,
1776 list_empty(&channel->vidq.active));
1777 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1780 V4L2_FIELD_INTERLACED,
1781 sizeof(struct s2255_buffer),
1786 static int s2255_open(struct file *file)
1788 struct video_device *vdev = video_devdata(file);
1791 if (mutex_lock_interruptible(vdev->lock))
1792 return -ERESTARTSYS;
1793 ret = __s2255_open(file);
1794 mutex_unlock(vdev->lock);
1798 static unsigned int s2255_poll(struct file *file,
1799 struct poll_table_struct *wait)
1801 struct s2255_fh *fh = file->private_data;
1802 struct s2255_dev *dev = fh->dev;
1804 dprintk(100, "%s\n", __func__);
1805 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1807 mutex_lock(&dev->lock);
1808 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1809 mutex_unlock(&dev->lock);
1813 static void s2255_destroy(struct s2255_dev *dev)
1815 /* board shutdown stops the read pipe if it is running */
1816 s2255_board_shutdown(dev);
1817 /* make sure firmware still not trying to load */
1818 del_timer(&dev->timer); /* only started in .probe and .open */
1819 if (dev->fw_data->fw_urb) {
1820 usb_kill_urb(dev->fw_data->fw_urb);
1821 usb_free_urb(dev->fw_data->fw_urb);
1822 dev->fw_data->fw_urb = NULL;
1824 release_firmware(dev->fw_data->fw);
1825 kfree(dev->fw_data->pfw_data);
1826 kfree(dev->fw_data);
1827 /* reset the DSP so firmware can be reloaded next time */
1828 s2255_reset_dsppower(dev);
1829 mutex_destroy(&dev->lock);
1830 usb_put_dev(dev->udev);
1831 v4l2_device_unregister(&dev->v4l2_dev);
1832 dprintk(1, "%s", __func__);
1836 static int s2255_release(struct file *file)
1838 struct s2255_fh *fh = file->private_data;
1839 struct s2255_dev *dev = fh->dev;
1840 struct video_device *vdev = video_devdata(file);
1841 struct s2255_channel *channel = fh->channel;
1844 mutex_lock(&dev->lock);
1845 /* turn off stream */
1846 if (res_check(fh)) {
1847 if (channel->b_acquire)
1848 s2255_stop_acquire(fh->channel);
1849 videobuf_streamoff(&fh->vb_vidq);
1852 videobuf_mmap_free(&fh->vb_vidq);
1853 mutex_unlock(&dev->lock);
1854 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
1859 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1861 struct s2255_fh *fh = file->private_data;
1862 struct s2255_dev *dev;
1868 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
1869 if (mutex_lock_interruptible(&dev->lock))
1870 return -ERESTARTSYS;
1871 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1872 mutex_unlock(&dev->lock);
1873 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
1874 (unsigned long)vma->vm_start,
1875 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1879 static const struct v4l2_file_operations s2255_fops_v4l = {
1880 .owner = THIS_MODULE,
1882 .release = s2255_release,
1884 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1885 .mmap = s2255_mmap_v4l,
1888 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1889 .vidioc_querymenu = vidioc_querymenu,
1890 .vidioc_querycap = vidioc_querycap,
1891 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1892 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1893 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1894 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1895 .vidioc_reqbufs = vidioc_reqbufs,
1896 .vidioc_querybuf = vidioc_querybuf,
1897 .vidioc_qbuf = vidioc_qbuf,
1898 .vidioc_dqbuf = vidioc_dqbuf,
1899 .vidioc_s_std = vidioc_s_std,
1900 .vidioc_enum_input = vidioc_enum_input,
1901 .vidioc_g_input = vidioc_g_input,
1902 .vidioc_s_input = vidioc_s_input,
1903 .vidioc_queryctrl = vidioc_queryctrl,
1904 .vidioc_g_ctrl = vidioc_g_ctrl,
1905 .vidioc_s_ctrl = vidioc_s_ctrl,
1906 .vidioc_streamon = vidioc_streamon,
1907 .vidioc_streamoff = vidioc_streamoff,
1908 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1909 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1910 .vidioc_s_parm = vidioc_s_parm,
1911 .vidioc_g_parm = vidioc_g_parm,
1912 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1915 static void s2255_video_device_release(struct video_device *vdev)
1917 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1918 dprintk(4, "%s, chnls: %d \n", __func__,
1919 atomic_read(&dev->num_channels));
1920 if (atomic_dec_and_test(&dev->num_channels))
1925 static struct video_device template = {
1927 .fops = &s2255_fops_v4l,
1928 .ioctl_ops = &s2255_ioctl_ops,
1929 .release = s2255_video_device_release,
1930 .tvnorms = S2255_NORMS,
1931 .current_norm = V4L2_STD_NTSC_M,
1934 static int s2255_probe_v4l(struct s2255_dev *dev)
1938 int cur_nr = video_nr;
1939 struct s2255_channel *channel;
1940 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1943 /* initialize all video 4 linux */
1944 /* register 4 video devices */
1945 for (i = 0; i < MAX_CHANNELS; i++) {
1946 channel = &dev->channel[i];
1947 INIT_LIST_HEAD(&channel->vidq.active);
1948 channel->vidq.dev = dev;
1949 /* register 4 video devices */
1950 channel->vdev = template;
1951 channel->vdev.lock = &dev->lock;
1952 channel->vdev.v4l2_dev = &dev->v4l2_dev;
1953 video_set_drvdata(&channel->vdev, channel);
1955 ret = video_register_device(&channel->vdev,
1959 ret = video_register_device(&channel->vdev,
1964 dev_err(&dev->udev->dev,
1965 "failed to register video device!\n");
1968 atomic_inc(&dev->num_channels);
1969 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1970 video_device_node_name(&channel->vdev));
1973 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %s\n",
1975 /* if no channels registered, return error and probe will fail*/
1976 if (atomic_read(&dev->num_channels) == 0) {
1977 v4l2_device_unregister(&dev->v4l2_dev);
1980 if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
1981 printk(KERN_WARNING "s2255: Not all channels available.\n");
1985 /* this function moves the usb stream read pipe data
1986 * into the system buffers.
1987 * returns 0 on success, EAGAIN if more data to process( call this
1990 * Received frame structure:
1991 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1992 * bytes 4-7: channel: 0-3
1993 * bytes 8-11: payload size: size of the frame
1994 * bytes 12-payloadsize+12: frame data
1996 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2002 unsigned long copy_size;
2005 struct s2255_framei *frm;
2006 unsigned char *pdata;
2007 struct s2255_channel *channel;
2008 dprintk(100, "buffer to user\n");
2009 channel = &dev->channel[dev->cc];
2010 idx = channel->cur_frame;
2011 frm = &channel->buffer.frame[idx];
2012 if (frm->ulState == S2255_READ_IDLE) {
2015 __le32 *pdword; /*data from dsp is little endian */
2017 /* search for marker codes */
2018 pdata = (unsigned char *)pipe_info->transfer_buffer;
2019 pdword = (__le32 *)pdata;
2020 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
2022 case S2255_MARKER_FRAME:
2023 dprintk(4, "found frame marker at offset:"
2024 " %d [%x %x]\n", jj, pdata[0],
2026 offset = jj + PREFIX_SIZE;
2028 cc = le32_to_cpu(pdword[1]);
2029 if (cc >= MAX_CHANNELS) {
2035 dev->cc = G_chnmap[cc];
2036 channel = &dev->channel[dev->cc];
2037 payload = le32_to_cpu(pdword[3]);
2038 if (payload > channel->req_image_size) {
2039 channel->bad_payload++;
2040 /* discard the bad frame */
2043 channel->pkt_size = payload;
2044 channel->jpg_size = le32_to_cpu(pdword[4]);
2046 case S2255_MARKER_RESPONSE:
2048 pdata += DEF_USB_BLOCK;
2049 jj += DEF_USB_BLOCK;
2050 if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
2052 cc = G_chnmap[le32_to_cpu(pdword[1])];
2053 if (cc >= MAX_CHANNELS)
2055 channel = &dev->channel[cc];
2056 switch (pdword[2]) {
2057 case S2255_RESPONSE_SETMODE:
2058 /* check if channel valid */
2059 /* set mode ready */
2060 channel->setmode_ready = 1;
2061 wake_up(&channel->wait_setmode);
2062 dprintk(5, "setmode ready %d\n", cc);
2064 case S2255_RESPONSE_FW:
2065 dev->chn_ready |= (1 << cc);
2066 if ((dev->chn_ready & 0x0f) != 0x0f)
2068 /* all channels ready */
2069 printk(KERN_INFO "s2255: fw loaded\n");
2070 atomic_set(&dev->fw_data->fw_state,
2072 wake_up(&dev->fw_data->wait_fw);
2074 case S2255_RESPONSE_STATUS:
2075 channel->vidstatus = le32_to_cpu(pdword[3]);
2076 channel->vidstatus_ready = 1;
2077 wake_up(&channel->wait_vidstatus);
2078 dprintk(5, "got vidstatus %x chan %d\n",
2079 le32_to_cpu(pdword[3]), cc);
2082 printk(KERN_INFO "s2255 unknown resp\n");
2094 channel = &dev->channel[dev->cc];
2095 idx = channel->cur_frame;
2096 frm = &channel->buffer.frame[idx];
2097 /* search done. now find out if should be acquiring on this channel */
2098 if (!channel->b_acquire) {
2099 /* we found a frame, but this channel is turned off */
2100 frm->ulState = S2255_READ_IDLE;
2104 if (frm->ulState == S2255_READ_IDLE) {
2105 frm->ulState = S2255_READ_FRAME;
2109 /* skip the marker 512 bytes (and offset if out of sync) */
2110 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2113 if (frm->lpvbits == NULL) {
2114 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2115 frm, dev, dev->cc, idx);
2119 pdest = frm->lpvbits + frm->cur_size;
2121 copy_size = (pipe_info->cur_transfer_size - offset);
2123 size = channel->pkt_size - PREFIX_SIZE;
2125 /* sanity check on pdest */
2126 if ((copy_size + frm->cur_size) < channel->req_image_size)
2127 memcpy(pdest, psrc, copy_size);
2129 frm->cur_size += copy_size;
2130 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2132 if (frm->cur_size >= size) {
2133 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2135 channel->last_frame = channel->cur_frame;
2136 channel->cur_frame++;
2137 /* end of system frame ring buffer, start at zero */
2138 if ((channel->cur_frame == SYS_FRAMES) ||
2139 (channel->cur_frame == channel->buffer.dwFrames))
2140 channel->cur_frame = 0;
2142 if (channel->b_acquire)
2143 s2255_got_frame(channel, channel->jpg_size);
2144 channel->frame_count++;
2145 frm->ulState = S2255_READ_IDLE;
2149 /* done successfully */
2153 static void s2255_read_video_callback(struct s2255_dev *dev,
2154 struct s2255_pipeinfo *pipe_info)
2157 dprintk(50, "callback read video \n");
2159 if (dev->cc >= MAX_CHANNELS) {
2161 dev_err(&dev->udev->dev, "invalid channel\n");
2164 /* otherwise copy to the system buffers */
2165 res = save_frame(dev, pipe_info);
2167 dprintk(4, "s2255: read callback failed\n");
2169 dprintk(50, "callback read video done\n");
2173 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2174 u16 Index, u16 Value, void *TransferBuffer,
2175 s32 TransferBufferLength, int bOut)
2179 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2181 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2183 Value, Index, TransferBuffer,
2184 TransferBufferLength, HZ * 5);
2186 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2187 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2188 Value, Index, TransferBuffer,
2189 TransferBufferLength, HZ * 5);
2195 * retrieve FX2 firmware version. future use.
2196 * @param dev pointer to device extension
2197 * @return -1 for fail, else returns firmware version as an int(16 bits)
2199 static int s2255_get_fx2fw(struct s2255_dev *dev)
2203 unsigned char transBuffer[64];
2204 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2207 dprintk(2, "get fw error: %x\n", ret);
2208 fw = transBuffer[0] + (transBuffer[1] << 8);
2209 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2214 * Create the system ring buffer to copy frames into from the
2217 static int s2255_create_sys_buffers(struct s2255_channel *channel)
2220 unsigned long reqsize;
2221 dprintk(1, "create sys buffers\n");
2222 channel->buffer.dwFrames = SYS_FRAMES;
2223 /* always allocate maximum size(PAL) for system buffers */
2224 reqsize = SYS_FRAMES_MAXSIZE;
2226 if (reqsize > SYS_FRAMES_MAXSIZE)
2227 reqsize = SYS_FRAMES_MAXSIZE;
2229 for (i = 0; i < SYS_FRAMES; i++) {
2230 /* allocate the frames */
2231 channel->buffer.frame[i].lpvbits = vmalloc(reqsize);
2232 dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n",
2233 &channel->buffer.frame[i], channel->idx, i,
2234 channel->buffer.frame[i].lpvbits);
2235 channel->buffer.frame[i].size = reqsize;
2236 if (channel->buffer.frame[i].lpvbits == NULL) {
2237 printk(KERN_INFO "out of memory. using less frames\n");
2238 channel->buffer.dwFrames = i;
2243 /* make sure internal states are set */
2244 for (i = 0; i < SYS_FRAMES; i++) {
2245 channel->buffer.frame[i].ulState = 0;
2246 channel->buffer.frame[i].cur_size = 0;
2249 channel->cur_frame = 0;
2250 channel->last_frame = -1;
2254 static int s2255_release_sys_buffers(struct s2255_channel *channel)
2257 dprintk(1, "release sys buffers\n");
2258 for (i = 0; i < SYS_FRAMES; i++) {
2259 if (channel->buffer.frame[i].lpvbits) {
2260 dprintk(1, "vfree %p\n",
2261 channel->buffer.frame[i].lpvbits);
2262 vfree(channel->buffer.frame[i].lpvbits);
2264 channel->buffer.frame[i].lpvbits = NULL;
2269 static int s2255_board_init(struct s2255_dev *dev)
2271 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2274 struct s2255_pipeinfo *pipe = &dev->pipe;
2275 dprintk(4, "board init: %p", dev);
2276 memset(pipe, 0, sizeof(*pipe));
2278 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2279 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2281 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2283 if (pipe->transfer_buffer == NULL) {
2284 dprintk(1, "out of memory!\n");
2287 /* query the firmware */
2288 fw_ver = s2255_get_fx2fw(dev);
2290 printk(KERN_INFO "s2255: usb firmware version %d.%d\n",
2291 (fw_ver >> 8) & 0xff,
2294 if (fw_ver < S2255_CUR_USB_FWVER)
2295 printk(KERN_INFO "s2255: newer USB firmware available\n");
2297 for (j = 0; j < MAX_CHANNELS; j++) {
2298 struct s2255_channel *channel = &dev->channel[j];
2299 channel->b_acquire = 0;
2300 channel->mode = mode_def;
2301 if (dev->pid == 0x2257 && j > 1)
2302 channel->mode.color |= (1 << 16);
2303 channel->jc.quality = S2255_DEF_JPEG_QUAL;
2304 channel->width = LINE_SZ_4CIFS_NTSC;
2305 channel->height = NUM_LINES_4CIFS_NTSC * 2;
2306 channel->fmt = &formats[0];
2307 channel->mode.restart = 1;
2308 channel->req_image_size = get_transfer_size(&mode_def);
2309 channel->frame_count = 0;
2310 /* create the system buffers */
2311 s2255_create_sys_buffers(channel);
2313 /* start read pipe */
2314 s2255_start_readpipe(dev);
2315 dprintk(1, "%s: success\n", __func__);
2319 static int s2255_board_shutdown(struct s2255_dev *dev)
2322 dprintk(1, "%s: dev: %p", __func__, dev);
2324 for (i = 0; i < MAX_CHANNELS; i++) {
2325 if (dev->channel[i].b_acquire)
2326 s2255_stop_acquire(&dev->channel[i]);
2328 s2255_stop_readpipe(dev);
2329 for (i = 0; i < MAX_CHANNELS; i++)
2330 s2255_release_sys_buffers(&dev->channel[i]);
2331 /* release transfer buffer */
2332 kfree(dev->pipe.transfer_buffer);
2336 static void read_pipe_completion(struct urb *purb)
2338 struct s2255_pipeinfo *pipe_info;
2339 struct s2255_dev *dev;
2342 pipe_info = purb->context;
2343 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
2345 if (pipe_info == NULL) {
2346 dev_err(&purb->dev->dev, "no context!\n");
2350 dev = pipe_info->dev;
2352 dev_err(&purb->dev->dev, "no context!\n");
2355 status = purb->status;
2356 /* if shutting down, do not resubmit, exit immediately */
2357 if (status == -ESHUTDOWN) {
2358 dprintk(2, "%s: err shutdown\n", __func__);
2359 pipe_info->err_count++;
2363 if (pipe_info->state == 0) {
2364 dprintk(2, "%s: exiting USB pipe", __func__);
2369 s2255_read_video_callback(dev, pipe_info);
2371 pipe_info->err_count++;
2372 dprintk(1, "%s: failed URB %d\n", __func__, status);
2375 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2377 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2379 pipe_info->transfer_buffer,
2380 pipe_info->cur_transfer_size,
2381 read_pipe_completion, pipe_info);
2383 if (pipe_info->state != 0) {
2384 if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC)) {
2385 dev_err(&dev->udev->dev, "error submitting urb\n");
2388 dprintk(2, "%s :complete state 0\n", __func__);
2393 static int s2255_start_readpipe(struct s2255_dev *dev)
2397 struct s2255_pipeinfo *pipe_info = &dev->pipe;
2398 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2399 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
2400 pipe_info->state = 1;
2401 pipe_info->err_count = 0;
2402 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2403 if (!pipe_info->stream_urb) {
2404 dev_err(&dev->udev->dev,
2405 "ReadStream: Unable to alloc URB\n");
2408 /* transfer buffer allocated in board_init */
2409 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2411 pipe_info->transfer_buffer,
2412 pipe_info->cur_transfer_size,
2413 read_pipe_completion, pipe_info);
2414 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2416 printk(KERN_ERR "s2255: start read pipe failed\n");
2422 /* starts acquisition process */
2423 static int s2255_start_acquire(struct s2255_channel *channel)
2425 unsigned char *buffer;
2427 unsigned long chn_rev;
2429 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2430 chn_rev = G_chnmap[channel->idx];
2431 buffer = kzalloc(512, GFP_KERNEL);
2432 if (buffer == NULL) {
2433 dev_err(&dev->udev->dev, "out of mem\n");
2437 channel->last_frame = -1;
2438 channel->bad_payload = 0;
2439 channel->cur_frame = 0;
2440 for (j = 0; j < SYS_FRAMES; j++) {
2441 channel->buffer.frame[j].ulState = 0;
2442 channel->buffer.frame[j].cur_size = 0;
2445 /* send the start command */
2446 *(__le32 *) buffer = IN_DATA_TOKEN;
2447 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2448 *((__le32 *) buffer + 2) = CMD_START;
2449 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2451 dev_err(&dev->udev->dev, "CMD_START error\n");
2453 dprintk(2, "start acquire exit[%d] %d \n", channel->idx, res);
2458 static int s2255_stop_acquire(struct s2255_channel *channel)
2460 unsigned char *buffer;
2462 unsigned long chn_rev;
2463 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2464 chn_rev = G_chnmap[channel->idx];
2465 buffer = kzalloc(512, GFP_KERNEL);
2466 if (buffer == NULL) {
2467 dev_err(&dev->udev->dev, "out of mem\n");
2470 /* send the stop command */
2471 *(__le32 *) buffer = IN_DATA_TOKEN;
2472 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2473 *((__le32 *) buffer + 2) = CMD_STOP;
2474 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2476 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2478 channel->b_acquire = 0;
2479 dprintk(4, "%s: chn %d, res %d\n", __func__, channel->idx, res);
2483 static void s2255_stop_readpipe(struct s2255_dev *dev)
2485 struct s2255_pipeinfo *pipe = &dev->pipe;
2488 if (pipe->stream_urb) {
2490 usb_kill_urb(pipe->stream_urb);
2491 usb_free_urb(pipe->stream_urb);
2492 pipe->stream_urb = NULL;
2494 dprintk(4, "%s", __func__);
2498 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2501 s2255_reset_dsppower(dev);
2502 dev->fw_data->fw_size = dev->fw_data->fw->size;
2503 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2504 memcpy(dev->fw_data->pfw_data,
2505 dev->fw_data->fw->data, CHUNK_SIZE);
2506 dev->fw_data->fw_loaded = CHUNK_SIZE;
2507 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2508 usb_sndbulkpipe(dev->udev, 2),
2509 dev->fw_data->pfw_data,
2510 CHUNK_SIZE, s2255_fwchunk_complete,
2512 mod_timer(&dev->timer, jiffies + HZ);
2515 /* standard usb probe function */
2516 static int s2255_probe(struct usb_interface *interface,
2517 const struct usb_device_id *id)
2519 struct s2255_dev *dev = NULL;
2520 struct usb_host_interface *iface_desc;
2521 struct usb_endpoint_descriptor *endpoint;
2523 int retval = -ENOMEM;
2526 dprintk(2, "%s\n", __func__);
2527 /* allocate memory for our device state and initialize it to zero */
2528 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2530 s2255_dev_err(&interface->dev, "out of memory\n");
2533 atomic_set(&dev->num_channels, 0);
2534 dev->pid = id->idProduct;
2535 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2538 mutex_init(&dev->lock);
2539 /* grab usb_device and save it */
2540 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2541 if (dev->udev == NULL) {
2542 dev_err(&interface->dev, "null usb device\n");
2546 dprintk(1, "dev: %p, udev %p interface %p\n", dev,
2547 dev->udev, interface);
2548 dev->interface = interface;
2549 /* set up the endpoint information */
2550 iface_desc = interface->cur_altsetting;
2551 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2552 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2553 endpoint = &iface_desc->endpoint[i].desc;
2554 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2555 /* we found the bulk in endpoint */
2556 dev->read_endpoint = endpoint->bEndpointAddress;
2560 if (!dev->read_endpoint) {
2561 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2564 init_timer(&dev->timer);
2565 dev->timer.function = s2255_timer;
2566 dev->timer.data = (unsigned long)dev->fw_data;
2567 init_waitqueue_head(&dev->fw_data->wait_fw);
2568 for (i = 0; i < MAX_CHANNELS; i++) {
2569 struct s2255_channel *channel = &dev->channel[i];
2570 dev->channel[i].idx = i;
2571 init_waitqueue_head(&channel->wait_setmode);
2572 init_waitqueue_head(&channel->wait_vidstatus);
2575 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2576 if (!dev->fw_data->fw_urb) {
2577 dev_err(&interface->dev, "out of memory!\n");
2581 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2582 if (!dev->fw_data->pfw_data) {
2583 dev_err(&interface->dev, "out of memory!\n");
2586 /* load the first chunk */
2587 if (request_firmware(&dev->fw_data->fw,
2588 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2589 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2592 /* check the firmware is valid */
2593 fw_size = dev->fw_data->fw->size;
2594 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2596 if (*pdata != S2255_FW_MARKER) {
2597 printk(KERN_INFO "Firmware invalid.\n");
2601 /* make sure firmware is the latest */
2603 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2604 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2605 dev->dsp_fw_ver = le32_to_cpu(*pRel);
2606 if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER)
2607 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
2608 if (dev->pid == 0x2257 &&
2609 dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
2610 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2611 " or above.\n", S2255_MIN_DSP_COLORFILTER);
2613 usb_reset_device(dev->udev);
2614 /* load 2255 board specific */
2615 retval = s2255_board_init(dev);
2617 goto errorBOARDINIT;
2618 spin_lock_init(&dev->slock);
2619 s2255_fwload_start(dev, 0);
2620 /* loads v4l specific */
2621 retval = s2255_probe_v4l(dev);
2623 goto errorBOARDINIT;
2624 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2627 s2255_board_shutdown(dev);
2629 release_firmware(dev->fw_data->fw);
2631 kfree(dev->fw_data->pfw_data);
2633 usb_free_urb(dev->fw_data->fw_urb);
2635 del_timer(&dev->timer);
2637 usb_put_dev(dev->udev);
2639 kfree(dev->fw_data);
2640 mutex_destroy(&dev->lock);
2643 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
2647 /* disconnect routine. when board is removed physically or with rmmod */
2648 static void s2255_disconnect(struct usb_interface *interface)
2650 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
2652 int channels = atomic_read(&dev->num_channels);
2653 mutex_lock(&dev->lock);
2654 v4l2_device_disconnect(&dev->v4l2_dev);
2655 mutex_unlock(&dev->lock);
2656 /*see comments in the uvc_driver.c usb disconnect function */
2657 atomic_inc(&dev->num_channels);
2658 /* unregister each video device. */
2659 for (i = 0; i < channels; i++)
2660 video_unregister_device(&dev->channel[i].vdev);
2661 /* wake up any of our timers */
2662 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2663 wake_up(&dev->fw_data->wait_fw);
2664 for (i = 0; i < MAX_CHANNELS; i++) {
2665 dev->channel[i].setmode_ready = 1;
2666 wake_up(&dev->channel[i].wait_setmode);
2667 dev->channel[i].vidstatus_ready = 1;
2668 wake_up(&dev->channel[i].wait_vidstatus);
2670 if (atomic_dec_and_test(&dev->num_channels))
2672 dev_info(&interface->dev, "%s\n", __func__);
2675 static struct usb_driver s2255_driver = {
2676 .name = S2255_DRIVER_NAME,
2677 .probe = s2255_probe,
2678 .disconnect = s2255_disconnect,
2679 .id_table = s2255_table,
2682 module_usb_driver(s2255_driver);
2684 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2685 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2686 MODULE_LICENSE("GPL");
2687 MODULE_VERSION(S2255_VERSION);
2688 MODULE_FIRMWARE(FIRMWARE_FILE_NAME);