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-ioctl.h>
56 #include <linux/vmalloc.h>
57 #include <linux/usb.h>
59 #define S2255_MAJOR_VERSION 1
60 #define S2255_MINOR_VERSION 19
61 #define S2255_RELEASE 0
62 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
63 S2255_MINOR_VERSION, \
65 #define FIRMWARE_FILE_NAME "f2255usb.bin"
67 /* default JPEG quality */
68 #define S2255_DEF_JPEG_QUAL 50
69 /* vendor request in */
71 /* vendor request out */
72 #define S2255_VR_OUT 1
74 #define S2255_VR_FW 0x30
75 /* USB endpoint number for configuring the device */
76 #define S2255_CONFIG_EP 2
77 /* maximum time for DSP to start responding after last FW word loaded(ms) */
78 #define S2255_DSP_BOOTTIME 800
79 /* maximum time to wait for firmware to load (ms) */
80 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
81 #define S2255_DEF_BUFS 16
82 #define S2255_SETMODE_TIMEOUT 500
83 #define S2255_VIDSTATUS_TIMEOUT 350
84 #define MAX_CHANNELS 4
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];
234 struct mutex open_lock;
235 int resources[MAX_CHANNELS];
236 struct usb_device *udev;
237 struct usb_interface *interface;
240 struct s2255_dmaqueue vidq[MAX_CHANNELS];
241 struct timer_list timer;
242 struct s2255_fw *fw_data;
243 struct s2255_pipeinfo pipe;
244 struct s2255_bufferi buffer[MAX_CHANNELS];
245 struct s2255_mode mode[MAX_CHANNELS];
246 /* jpeg compression */
247 struct v4l2_jpegcompression jc[MAX_CHANNELS];
248 /* capture parameters (for high quality mode full size) */
249 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
250 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
251 int cur_frame[MAX_CHANNELS];
252 int last_frame[MAX_CHANNELS];
253 u32 cc; /* current channel */
254 int b_acquire[MAX_CHANNELS];
255 /* allocated image size */
256 unsigned long req_image_size[MAX_CHANNELS];
257 /* received packet size */
258 unsigned long pkt_size[MAX_CHANNELS];
259 int bad_payload[MAX_CHANNELS];
260 unsigned long frame_count[MAX_CHANNELS];
263 int jpg_size[MAX_CHANNELS];
264 /* if channel configured to default state */
265 int chn_configured[MAX_CHANNELS];
266 wait_queue_head_t wait_setmode[MAX_CHANNELS];
267 int setmode_ready[MAX_CHANNELS];
268 /* video status items */
269 int vidstatus[MAX_CHANNELS];
270 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
271 int vidstatus_ready[MAX_CHANNELS];
274 /* dsp firmware version (f2255usb.bin) */
276 u16 pid; /* product id */
279 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
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 const struct s2255_fmt *fmt;
299 struct videobuf_queue vb_vidq;
300 enum v4l2_buf_type type;
302 /* mode below is the desired mode.
303 mode in s2255_dev is the current mode that was last set */
304 struct s2255_mode mode;
305 int resources[MAX_CHANNELS];
308 /* current cypress EEPROM firmware version */
309 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
310 /* current DSP FW version */
311 #define S2255_CUR_DSP_FWVER 8
312 /* Need DSP version 5+ for video status feature */
313 #define S2255_MIN_DSP_STATUS 5
314 #define S2255_MIN_DSP_COLORFILTER 8
315 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
317 /* private V4L2 controls */
320 * The following chart displays how COLORFILTER should be set
321 * =========================================================
322 * = fourcc = COLORFILTER =
323 * = ===============================
325 * =========================================================
326 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
327 * = = s-video or = composite =
328 * = = B/W camera = input =
329 * =========================================================
330 * = other = color, svideo = color, =
332 * =========================================================
335 * channels 0-3 on 2255 are composite
336 * channels 0-1 on 2257 are composite, 2-3 are s-video
337 * If COLORFILTER is 0 with a composite color camera connected,
338 * the output will appear monochrome but hatching
340 * COLORFILTER is different from "color killer" and "color effects"
343 #define S2255_V4L2_YC_ON 1
344 #define S2255_V4L2_YC_OFF 0
345 #define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
347 /* frame prefix size (sent once every frame) */
348 #define PREFIX_SIZE 512
350 /* Channels on box are in reverse order */
351 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
354 static int *s2255_debug = &debug;
356 static int s2255_start_readpipe(struct s2255_dev *dev);
357 static void s2255_stop_readpipe(struct s2255_dev *dev);
358 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
359 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
360 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
361 int chn, int jpgsize);
362 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
363 struct s2255_mode *mode);
364 static int s2255_board_shutdown(struct s2255_dev *dev);
365 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
366 static void s2255_destroy(struct kref *kref);
367 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
368 u16 index, u16 value, void *buf,
369 s32 buf_len, int bOut);
371 /* dev_err macro with driver name */
372 #define S2255_DRIVER_NAME "s2255"
373 #define s2255_dev_err(dev, fmt, arg...) \
374 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
376 #define dprintk(level, fmt, arg...) \
378 if (*s2255_debug >= (level)) { \
379 printk(KERN_DEBUG S2255_DRIVER_NAME \
384 static struct usb_driver s2255_driver;
386 /* Declare static vars that will be used as parameters */
387 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
389 /* start video number */
390 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
392 module_param(debug, int, 0644);
393 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
394 module_param(vid_limit, int, 0644);
395 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
396 module_param(video_nr, int, 0644);
397 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
399 /* USB device table */
400 #define USB_SENSORAY_VID 0x1943
401 static struct usb_device_id s2255_table[] = {
402 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
403 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
404 { } /* Terminating entry */
406 MODULE_DEVICE_TABLE(usb, s2255_table);
408 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
411 static const struct s2255_fmt formats[] = {
413 .name = "4:2:2, planar, YUV422P",
414 .fourcc = V4L2_PIX_FMT_YUV422P,
418 .name = "4:2:2, packed, YUYV",
419 .fourcc = V4L2_PIX_FMT_YUYV,
423 .name = "4:2:2, packed, UYVY",
424 .fourcc = V4L2_PIX_FMT_UYVY,
428 .fourcc = V4L2_PIX_FMT_JPEG,
432 .fourcc = V4L2_PIX_FMT_GREY,
437 static int norm_maxw(struct video_device *vdev)
439 return (vdev->current_norm & V4L2_STD_NTSC) ?
440 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
443 static int norm_maxh(struct video_device *vdev)
445 return (vdev->current_norm & V4L2_STD_NTSC) ?
446 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
449 static int norm_minw(struct video_device *vdev)
451 return (vdev->current_norm & V4L2_STD_NTSC) ?
452 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
455 static int norm_minh(struct video_device *vdev)
457 return (vdev->current_norm & V4L2_STD_NTSC) ?
458 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
463 * TODO: fixme: move YUV reordering to hardware
464 * converts 2255 planar format to yuyv or uyvy
466 static void planar422p_to_yuv_packed(const unsigned char *in,
468 int width, int height,
474 unsigned long size = height * width;
476 pY = (unsigned char *)in;
477 pCr = (unsigned char *)in + height * width;
478 pCb = (unsigned char *)in + height * width + (height * width / 2);
479 for (i = 0; i < size * 2; i += 4) {
480 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
481 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
482 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
483 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
488 static void s2255_reset_dsppower(struct s2255_dev *dev)
490 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
492 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
496 /* kickstarts the firmware loading. from probe
498 static void s2255_timer(unsigned long user_data)
500 struct s2255_fw *data = (struct s2255_fw *)user_data;
501 dprintk(100, "%s\n", __func__);
502 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
503 printk(KERN_ERR "s2255: can't submit urb\n");
504 atomic_set(&data->fw_state, S2255_FW_FAILED);
505 /* wake up anything waiting for the firmware */
506 wake_up(&data->wait_fw);
512 /* this loads the firmware asynchronously.
513 Originally this was done synchroously in probe.
514 But it is better to load it asynchronously here than block
515 inside the probe function. Blocking inside probe affects boot time.
516 FW loading is triggered by the timer in the probe function
518 static void s2255_fwchunk_complete(struct urb *urb)
520 struct s2255_fw *data = urb->context;
521 struct usb_device *udev = urb->dev;
523 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
525 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
526 atomic_set(&data->fw_state, S2255_FW_FAILED);
527 /* wake up anything waiting for the firmware */
528 wake_up(&data->wait_fw);
531 if (data->fw_urb == NULL) {
532 s2255_dev_err(&udev->dev, "disconnected\n");
533 atomic_set(&data->fw_state, S2255_FW_FAILED);
534 /* wake up anything waiting for the firmware */
535 wake_up(&data->wait_fw);
538 #define CHUNK_SIZE 512
539 /* all USB transfers must be done with continuous kernel memory.
540 can't allocate more than 128k in current linux kernel, so
541 upload the firmware in chunks
543 if (data->fw_loaded < data->fw_size) {
544 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
545 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
547 if (len < CHUNK_SIZE)
548 memset(data->pfw_data, 0, CHUNK_SIZE);
550 dprintk(100, "completed len %d, loaded %d \n", len,
553 memcpy(data->pfw_data,
554 (char *) data->fw->data + data->fw_loaded, len);
556 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
557 data->pfw_data, CHUNK_SIZE,
558 s2255_fwchunk_complete, data);
559 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
560 dev_err(&udev->dev, "failed submit URB\n");
561 atomic_set(&data->fw_state, S2255_FW_FAILED);
562 /* wake up anything waiting for the firmware */
563 wake_up(&data->wait_fw);
566 data->fw_loaded += len;
568 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
569 dprintk(100, "%s: firmware upload complete\n", __func__);
575 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
577 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
578 struct s2255_buffer *buf;
579 unsigned long flags = 0;
581 spin_lock_irqsave(&dev->slock, flags);
582 if (list_empty(&dma_q->active)) {
583 dprintk(1, "No active queue to serve\n");
587 buf = list_entry(dma_q->active.next,
588 struct s2255_buffer, vb.queue);
589 list_del(&buf->vb.queue);
590 do_gettimeofday(&buf->vb.ts);
591 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
592 wake_up(&buf->vb.done);
593 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
595 spin_unlock_irqrestore(&dev->slock, flags);
599 static const struct s2255_fmt *format_by_fourcc(int fourcc)
602 for (i = 0; i < ARRAY_SIZE(formats); i++) {
603 if (-1 == formats[i].fourcc)
605 if (formats[i].fourcc == fourcc)
611 /* video buffer vmalloc implementation based partly on VIVI driver which is
612 * Copyright (c) 2006 by
613 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
614 * Ted Walther <ted--a.t--enumera.com>
615 * John Sokol <sokol--a.t--videotechnology.com>
616 * http://v4l.videotechnology.com/
619 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
620 int chn, int jpgsize)
625 char *vbuf = videobuf_to_vmalloc(&buf->vb);
626 unsigned long last_frame;
627 struct s2255_framei *frm;
632 last_frame = dev->last_frame[chn];
633 if (last_frame != -1) {
634 frm = &dev->buffer[chn].frame[last_frame];
636 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
637 switch (buf->fmt->fourcc) {
638 case V4L2_PIX_FMT_YUYV:
639 case V4L2_PIX_FMT_UYVY:
640 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
645 case V4L2_PIX_FMT_GREY:
646 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
648 case V4L2_PIX_FMT_JPEG:
649 buf->vb.size = jpgsize;
650 memcpy(vbuf, tmpbuf, buf->vb.size);
652 case V4L2_PIX_FMT_YUV422P:
654 buf->vb.width * buf->vb.height * 2);
657 printk(KERN_DEBUG "s2255: unknown format?\n");
659 dev->last_frame[chn] = -1;
661 printk(KERN_ERR "s2255: =======no frame\n");
665 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
666 (unsigned long)vbuf, pos);
667 /* tell v4l buffer was filled */
669 buf->vb.field_count = dev->frame_count[chn] * 2;
670 do_gettimeofday(&ts);
672 buf->vb.state = VIDEOBUF_DONE;
676 /* ------------------------------------------------------------------
678 ------------------------------------------------------------------*/
680 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
683 struct s2255_fh *fh = vq->priv_data;
685 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
688 *count = S2255_DEF_BUFS;
690 if (*size * *count > vid_limit * 1024 * 1024)
691 *count = (vid_limit * 1024 * 1024) / *size;
696 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
698 dprintk(4, "%s\n", __func__);
700 videobuf_vmalloc_free(&buf->vb);
701 buf->vb.state = VIDEOBUF_NEEDS_INIT;
704 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
705 enum v4l2_field field)
707 struct s2255_fh *fh = vq->priv_data;
708 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
710 dprintk(4, "%s, field=%d\n", __func__, field);
714 if ((fh->width < norm_minw(&fh->dev->vdev[fh->channel])) ||
715 (fh->width > norm_maxw(&fh->dev->vdev[fh->channel])) ||
716 (fh->height < norm_minh(&fh->dev->vdev[fh->channel])) ||
717 (fh->height > norm_maxh(&fh->dev->vdev[fh->channel]))) {
718 dprintk(4, "invalid buffer prepare\n");
722 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
724 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
725 dprintk(4, "invalid buffer prepare\n");
730 buf->vb.width = fh->width;
731 buf->vb.height = fh->height;
732 buf->vb.field = field;
734 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
735 rc = videobuf_iolock(vq, &buf->vb, NULL);
740 buf->vb.state = VIDEOBUF_PREPARED;
743 free_buffer(vq, buf);
747 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
749 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
750 struct s2255_fh *fh = vq->priv_data;
751 struct s2255_dev *dev = fh->dev;
752 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
753 dprintk(1, "%s\n", __func__);
754 buf->vb.state = VIDEOBUF_QUEUED;
755 list_add_tail(&buf->vb.queue, &vidq->active);
758 static void buffer_release(struct videobuf_queue *vq,
759 struct videobuf_buffer *vb)
761 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
762 struct s2255_fh *fh = vq->priv_data;
763 dprintk(4, "%s %d\n", __func__, fh->channel);
764 free_buffer(vq, buf);
767 static struct videobuf_queue_ops s2255_video_qops = {
768 .buf_setup = buffer_setup,
769 .buf_prepare = buffer_prepare,
770 .buf_queue = buffer_queue,
771 .buf_release = buffer_release,
775 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
778 mutex_lock(&dev->lock);
779 if (dev->resources[fh->channel]) {
780 /* no, someone else uses it */
781 mutex_unlock(&dev->lock);
784 /* it's free, grab it */
785 dev->resources[fh->channel] = 1;
786 fh->resources[fh->channel] = 1;
787 dprintk(1, "s2255: res: get\n");
788 mutex_unlock(&dev->lock);
792 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
794 return dev->resources[fh->channel];
797 static int res_check(struct s2255_fh *fh)
799 return fh->resources[fh->channel];
803 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
805 mutex_lock(&dev->lock);
806 dev->resources[fh->channel] = 0;
807 fh->resources[fh->channel] = 0;
808 mutex_unlock(&dev->lock);
809 dprintk(1, "res: put\n");
812 static int vidioc_querymenu(struct file *file, void *priv,
813 struct v4l2_querymenu *qmenu)
815 static const char *colorfilter[] = {
820 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
822 const char **menu_items = colorfilter;
823 for (i = 0; i < qmenu->index && menu_items[i]; i++)
824 ; /* do nothing (from v4l2-common.c) */
825 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
827 strlcpy(qmenu->name, menu_items[qmenu->index],
828 sizeof(qmenu->name));
831 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
834 static int vidioc_querycap(struct file *file, void *priv,
835 struct v4l2_capability *cap)
837 struct s2255_fh *fh = file->private_data;
838 struct s2255_dev *dev = fh->dev;
839 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
840 strlcpy(cap->card, "s2255", sizeof(cap->card));
841 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
842 cap->version = S2255_VERSION;
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)
854 if (index >= ARRAY_SIZE(formats))
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;
868 f->fmt.pix.width = fh->width;
869 f->fmt.pix.height = fh->height;
870 f->fmt.pix.field = fh->vb_vidq.field;
871 f->fmt.pix.pixelformat = fh->fmt->fourcc;
872 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
873 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
877 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_format *f)
880 const struct s2255_fmt *fmt;
881 enum v4l2_field field;
883 struct s2255_fh *fh = priv;
884 struct s2255_dev *dev = fh->dev;
888 (dev->vdev[fh->channel].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 const struct s2255_fmt *fmt;
981 struct videobuf_queue *q = &fh->vb_vidq;
985 ret = vidioc_try_fmt_vid_cap(file, fh, f);
990 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
995 mutex_lock(&q->vb_lock);
997 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
998 dprintk(1, "queue busy\n");
1003 if (res_locked(fh->dev, fh)) {
1004 dprintk(1, "%s: channel busy\n", __func__);
1010 fh->width = f->fmt.pix.width;
1011 fh->height = f->fmt.pix.height;
1012 fh->vb_vidq.field = f->fmt.pix.field;
1014 norm = norm_minw(&fh->dev->vdev[fh->channel]);
1015 if (fh->width > norm_minw(&fh->dev->vdev[fh->channel])) {
1016 if (fh->height > norm_minh(&fh->dev->vdev[fh->channel])) {
1017 if (fh->dev->cap_parm[fh->channel].capturemode &
1018 V4L2_MODE_HIGHQUALITY)
1019 fh->mode.scale = SCALE_4CIFSI;
1021 fh->mode.scale = SCALE_4CIFS;
1023 fh->mode.scale = SCALE_2CIFS;
1026 fh->mode.scale = SCALE_1CIFS;
1030 switch (fh->fmt->fourcc) {
1031 case V4L2_PIX_FMT_GREY:
1032 fh->mode.color &= ~MASK_COLOR;
1033 fh->mode.color |= COLOR_Y8;
1035 case V4L2_PIX_FMT_JPEG:
1036 fh->mode.color &= ~MASK_COLOR;
1037 fh->mode.color |= COLOR_JPG;
1038 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
1040 case V4L2_PIX_FMT_YUV422P:
1041 fh->mode.color &= ~MASK_COLOR;
1042 fh->mode.color |= COLOR_YUVPL;
1044 case V4L2_PIX_FMT_YUYV:
1045 case V4L2_PIX_FMT_UYVY:
1047 fh->mode.color &= ~MASK_COLOR;
1048 fh->mode.color |= COLOR_YUVPK;
1053 mutex_unlock(&q->vb_lock);
1057 static int vidioc_reqbufs(struct file *file, void *priv,
1058 struct v4l2_requestbuffers *p)
1061 struct s2255_fh *fh = priv;
1062 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1066 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1069 struct s2255_fh *fh = priv;
1070 rc = videobuf_querybuf(&fh->vb_vidq, p);
1074 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1077 struct s2255_fh *fh = priv;
1078 rc = videobuf_qbuf(&fh->vb_vidq, p);
1082 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1085 struct s2255_fh *fh = priv;
1086 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1090 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1091 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1093 struct s2255_fh *fh = priv;
1095 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1099 /* write to the configuration pipe, synchronously */
1100 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1107 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1108 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1113 static u32 get_transfer_size(struct s2255_mode *mode)
1115 int linesPerFrame = LINE_SZ_DEF;
1116 int pixelsPerLine = NUM_LINES_DEF;
1119 unsigned int mask_mult;
1124 if (mode->format == FORMAT_NTSC) {
1125 switch (mode->scale) {
1128 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1129 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1132 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1133 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1136 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1137 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1142 } else if (mode->format == FORMAT_PAL) {
1143 switch (mode->scale) {
1146 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1147 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1150 linesPerFrame = NUM_LINES_2CIFS_PAL;
1151 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1154 linesPerFrame = NUM_LINES_1CIFS_PAL;
1155 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1161 outImageSize = linesPerFrame * pixelsPerLine;
1162 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1163 /* 2 bytes/pixel if not monochrome */
1167 /* total bytes to send including prefix and 4K padding;
1168 must be a multiple of USB_READ_SIZE */
1169 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1170 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1171 /* if size not a multiple of USB_READ_SIZE */
1172 if (usbInSize & ~mask_mult)
1173 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1177 static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
1179 struct device *dev = &sdev->udev->dev;
1180 dev_info(dev, "------------------------------------------------\n");
1181 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1182 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
1183 dev_info(dev, "bright: 0x%x\n", mode->bright);
1184 dev_info(dev, "------------------------------------------------\n");
1188 * set mode is the function which controls the DSP.
1189 * the restart parameter in struct s2255_mode should be set whenever
1190 * the image size could change via color format, video system or image
1192 * When the restart parameter is set, we sleep for ONE frame to allow the
1193 * DSP time to get the new frame
1195 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1196 struct s2255_mode *mode)
1200 unsigned long chn_rev;
1201 mutex_lock(&dev->lock);
1202 chn_rev = G_chnmap[chn];
1203 dprintk(3, "%s channel %lu\n", __func__, chn);
1204 /* if JPEG, set the quality */
1205 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1206 mode->color &= ~MASK_COLOR;
1207 mode->color |= COLOR_JPG;
1208 mode->color &= ~MASK_JPG_QUALITY;
1209 mode->color |= (dev->jc[chn].quality << 8);
1212 dev->mode[chn] = *mode;
1213 dev->req_image_size[chn] = get_transfer_size(mode);
1214 dprintk(1, "%s: reqsize %ld\n", __func__, dev->req_image_size[chn]);
1215 buffer = kzalloc(512, GFP_KERNEL);
1216 if (buffer == NULL) {
1217 dev_err(&dev->udev->dev, "out of mem\n");
1218 mutex_unlock(&dev->lock);
1222 buffer[0] = IN_DATA_TOKEN;
1223 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1224 buffer[2] = CMD_SET_MODE;
1225 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1226 dev->setmode_ready[chn] = 0;
1227 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1229 s2255_print_cfg(dev, mode);
1231 /* wait at least 3 frames before continuing */
1232 if (mode->restart) {
1233 wait_event_timeout(dev->wait_setmode[chn],
1234 (dev->setmode_ready[chn] != 0),
1235 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1236 if (dev->setmode_ready[chn] != 1) {
1237 printk(KERN_DEBUG "s2255: no set mode response\n");
1241 /* clear the restart flag */
1242 dev->mode[chn].restart = 0;
1243 mutex_unlock(&dev->lock);
1244 dprintk(1, "%s chn %lu, result: %d\n", __func__, chn, res);
1248 static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1254 mutex_lock(&dev->lock);
1255 chn_rev = G_chnmap[chn];
1256 dprintk(4, "%s chan %lu\n", __func__, chn);
1257 buffer = kzalloc(512, GFP_KERNEL);
1258 if (buffer == NULL) {
1259 dev_err(&dev->udev->dev, "out of mem\n");
1260 mutex_unlock(&dev->lock);
1263 /* form the get vid status command */
1264 buffer[0] = IN_DATA_TOKEN;
1265 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1266 buffer[2] = CMD_STATUS;
1268 dev->vidstatus_ready[chn] = 0;
1269 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1271 wait_event_timeout(dev->wait_vidstatus[chn],
1272 (dev->vidstatus_ready[chn] != 0),
1273 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1274 if (dev->vidstatus_ready[chn] != 1) {
1275 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1278 *pstatus = dev->vidstatus[chn];
1279 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1280 mutex_unlock(&dev->lock);
1284 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1287 struct s2255_fh *fh = priv;
1288 struct s2255_dev *dev = fh->dev;
1289 struct s2255_mode *new_mode;
1290 struct s2255_mode *old_mode;
1293 dprintk(4, "%s\n", __func__);
1294 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1295 dev_err(&dev->udev->dev, "invalid fh type0\n");
1298 if (i != fh->type) {
1299 dev_err(&dev->udev->dev, "invalid fh type1\n");
1303 if (!res_get(dev, fh)) {
1304 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1308 /* send a set mode command everytime with restart.
1309 in case we switch resolutions or other parameters */
1311 new_mode = &fh->mode;
1312 old_mode = &fh->dev->mode[chn];
1314 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
1315 new_mode->restart = 1;
1316 else if (new_mode->scale != old_mode->scale)
1317 new_mode->restart = 1;
1318 else if (new_mode->format != old_mode->format)
1319 new_mode->restart = 1;
1321 s2255_set_mode(dev, chn, new_mode);
1322 new_mode->restart = 0;
1323 *old_mode = *new_mode;
1324 dev->cur_fmt[chn] = fh->fmt;
1325 dev->last_frame[chn] = -1;
1326 dev->bad_payload[chn] = 0;
1327 dev->cur_frame[chn] = 0;
1328 dev->frame_count[chn] = 0;
1329 for (j = 0; j < SYS_FRAMES; j++) {
1330 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1331 dev->buffer[chn].frame[j].cur_size = 0;
1333 res = videobuf_streamon(&fh->vb_vidq);
1335 s2255_start_acquire(dev, chn);
1336 dev->b_acquire[chn] = 1;
1343 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1345 struct s2255_fh *fh = priv;
1346 struct s2255_dev *dev = fh->dev;
1347 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1348 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1349 printk(KERN_ERR "invalid fh type0\n");
1352 if (i != fh->type) {
1353 printk(KERN_ERR "invalid type i\n");
1356 s2255_stop_acquire(dev, fh->channel);
1357 videobuf_streamoff(&fh->vb_vidq);
1362 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1364 struct s2255_fh *fh = priv;
1365 struct s2255_mode *mode;
1366 struct videobuf_queue *q = &fh->vb_vidq;
1368 mutex_lock(&q->vb_lock);
1369 if (videobuf_queue_is_busy(q)) {
1370 dprintk(1, "queue busy\n");
1374 if (res_locked(fh->dev, fh)) {
1375 dprintk(1, "can't change standard after started\n");
1380 if (*i & V4L2_STD_NTSC) {
1381 dprintk(4, "%s NTSC\n", __func__);
1382 /* if changing format, reset frame decimation/intervals */
1383 if (mode->format != FORMAT_NTSC) {
1384 mode->format = FORMAT_NTSC;
1385 mode->fdec = FDEC_1;
1387 } else if (*i & V4L2_STD_PAL) {
1388 dprintk(4, "%s PAL\n", __func__);
1389 mode->format = FORMAT_PAL;
1390 if (mode->format != FORMAT_PAL) {
1391 mode->format = FORMAT_PAL;
1392 mode->fdec = FDEC_1;
1398 mutex_unlock(&q->vb_lock);
1402 /* Sensoray 2255 is a multiple channel capture device.
1403 It does not have a "crossbar" of inputs.
1404 We use one V4L device per channel. The user must
1405 be aware that certain combinations are not allowed.
1406 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1407 at once in color(you can do full fps on 4 channels with greyscale.
1409 static int vidioc_enum_input(struct file *file, void *priv,
1410 struct v4l2_input *inp)
1412 struct s2255_fh *fh = priv;
1413 struct s2255_dev *dev = fh->dev;
1415 if (inp->index != 0)
1417 inp->type = V4L2_INPUT_TYPE_CAMERA;
1418 inp->std = S2255_NORMS;
1420 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1422 rc = s2255_cmd_status(dev, fh->channel, &status);
1423 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1425 inp->status = (status & 0x01) ? 0
1426 : V4L2_IN_ST_NO_SIGNAL;
1431 strlcpy(inp->name, "Composite", sizeof(inp->name));
1434 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1441 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1446 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1453 /* --- controls ---------------------------------------------- */
1454 static int vidioc_queryctrl(struct file *file, void *priv,
1455 struct v4l2_queryctrl *qc)
1457 struct s2255_fh *fh = priv;
1458 struct s2255_dev *dev = fh->dev;
1460 case V4L2_CID_BRIGHTNESS:
1461 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1463 case V4L2_CID_CONTRAST:
1464 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1466 case V4L2_CID_SATURATION:
1467 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1470 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1472 case V4L2_CID_PRIVATE_COLORFILTER:
1473 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1475 if ((dev->pid == 0x2257) && (fh->channel > 1))
1477 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1478 qc->type = V4L2_CTRL_TYPE_MENU;
1482 qc->default_value = 1;
1488 dprintk(4, "%s, id %d\n", __func__, qc->id);
1492 static int vidioc_g_ctrl(struct file *file, void *priv,
1493 struct v4l2_control *ctrl)
1495 struct s2255_fh *fh = priv;
1496 struct s2255_dev *dev = fh->dev;
1498 case V4L2_CID_BRIGHTNESS:
1499 ctrl->value = fh->mode.bright;
1501 case V4L2_CID_CONTRAST:
1502 ctrl->value = fh->mode.contrast;
1504 case V4L2_CID_SATURATION:
1505 ctrl->value = fh->mode.saturation;
1508 ctrl->value = fh->mode.hue;
1510 case V4L2_CID_PRIVATE_COLORFILTER:
1511 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1513 if ((dev->pid == 0x2257) && (fh->channel > 1))
1515 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1520 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1524 static int vidioc_s_ctrl(struct file *file, void *priv,
1525 struct v4l2_control *ctrl)
1527 struct s2255_fh *fh = priv;
1528 struct s2255_dev *dev = fh->dev;
1529 struct s2255_mode *mode;
1531 dprintk(4, "%s\n", __func__);
1532 /* update the mode to the corresponding value */
1534 case V4L2_CID_BRIGHTNESS:
1535 mode->bright = ctrl->value;
1537 case V4L2_CID_CONTRAST:
1538 mode->contrast = ctrl->value;
1541 mode->hue = ctrl->value;
1543 case V4L2_CID_SATURATION:
1544 mode->saturation = ctrl->value;
1546 case V4L2_CID_PRIVATE_COLORFILTER:
1547 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1549 if ((dev->pid == 0x2257) && (fh->channel > 1))
1551 mode->color &= ~MASK_INPUT_TYPE;
1552 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1558 /* set mode here. Note: stream does not need restarted.
1559 some V4L programs restart stream unnecessarily
1562 s2255_set_mode(dev, fh->channel, mode);
1566 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1567 struct v4l2_jpegcompression *jc)
1569 struct s2255_fh *fh = priv;
1570 struct s2255_dev *dev = fh->dev;
1571 *jc = dev->jc[fh->channel];
1572 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1576 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1577 struct v4l2_jpegcompression *jc)
1579 struct s2255_fh *fh = priv;
1580 struct s2255_dev *dev = fh->dev;
1581 if (jc->quality < 0 || jc->quality > 100)
1583 dev->jc[fh->channel].quality = jc->quality;
1584 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1588 static int vidioc_g_parm(struct file *file, void *priv,
1589 struct v4l2_streamparm *sp)
1591 struct s2255_fh *fh = priv;
1592 struct s2255_dev *dev = fh->dev;
1593 __u32 def_num, def_dem;
1594 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1596 memset(sp, 0, sizeof(struct v4l2_streamparm));
1597 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1598 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1599 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1600 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1601 sp->parm.capture.timeperframe.denominator = def_dem;
1602 switch (fh->mode.fdec) {
1605 sp->parm.capture.timeperframe.numerator = def_num;
1608 sp->parm.capture.timeperframe.numerator = def_num * 2;
1611 sp->parm.capture.timeperframe.numerator = def_num * 3;
1614 sp->parm.capture.timeperframe.numerator = def_num * 5;
1617 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1618 sp->parm.capture.capturemode,
1619 sp->parm.capture.timeperframe.numerator,
1620 sp->parm.capture.timeperframe.denominator);
1624 static int vidioc_s_parm(struct file *file, void *priv,
1625 struct v4l2_streamparm *sp)
1627 struct s2255_fh *fh = priv;
1628 struct s2255_dev *dev = fh->dev;
1630 __u32 def_num, def_dem;
1631 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1633 /* high quality capture mode requires a stream restart */
1634 if (dev->cap_parm[fh->channel].capturemode
1635 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1637 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1638 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1639 if (def_dem != sp->parm.capture.timeperframe.denominator)
1640 sp->parm.capture.timeperframe.numerator = def_num;
1641 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1642 sp->parm.capture.timeperframe.numerator = def_num;
1643 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1644 sp->parm.capture.timeperframe.numerator = def_num * 2;
1646 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1647 sp->parm.capture.timeperframe.numerator = def_num * 3;
1650 sp->parm.capture.timeperframe.numerator = def_num * 5;
1653 fh->mode.fdec = fdec;
1654 sp->parm.capture.timeperframe.denominator = def_dem;
1655 s2255_set_mode(dev, fh->channel, &fh->mode);
1656 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1658 sp->parm.capture.capturemode,
1659 sp->parm.capture.timeperframe.numerator,
1660 sp->parm.capture.timeperframe.denominator, fdec);
1664 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1665 struct v4l2_frmivalenum *fe)
1668 #define NUM_FRAME_ENUMS 4
1669 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1670 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1672 switch (fe->width) {
1674 if (fe->height != 240 && fe->height != 480)
1679 if (fe->height != 240)
1684 if (fe->height != 288 && fe->height != 576)
1688 if (fe->height != 288)
1694 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1695 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1696 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1697 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1698 fe->discrete.denominator);
1702 static int s2255_open(struct file *file)
1704 struct video_device *vdev = video_devdata(file);
1705 struct s2255_dev *dev = video_drvdata(file);
1706 struct s2255_fh *fh;
1707 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1709 int cur_channel = -1;
1711 dprintk(1, "s2255: open called (dev=%s)\n",
1712 video_device_node_name(vdev));
1713 for (i = 0; i < MAX_CHANNELS; i++)
1714 if (&dev->vdev[i] == vdev) {
1719 * open lock necessary to prevent multiple instances
1720 * of v4l-conf (or other programs) from simultaneously
1721 * reloading firmware.
1723 mutex_lock(&dev->open_lock);
1724 state = atomic_read(&dev->fw_data->fw_state);
1726 case S2255_FW_DISCONNECTING:
1727 mutex_unlock(&dev->open_lock);
1729 case S2255_FW_FAILED:
1730 s2255_dev_err(&dev->udev->dev,
1731 "firmware load failed. retrying.\n");
1732 s2255_fwload_start(dev, 1);
1733 wait_event_timeout(dev->fw_data->wait_fw,
1734 ((atomic_read(&dev->fw_data->fw_state)
1735 == S2255_FW_SUCCESS) ||
1736 (atomic_read(&dev->fw_data->fw_state)
1737 == S2255_FW_DISCONNECTING)),
1738 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1739 /* state may have changed, re-read */
1740 state = atomic_read(&dev->fw_data->fw_state);
1742 case S2255_FW_NOTLOADED:
1743 case S2255_FW_LOADED_DSPWAIT:
1744 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1745 driver loaded and then device immediately opened */
1746 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1747 wait_event_timeout(dev->fw_data->wait_fw,
1748 ((atomic_read(&dev->fw_data->fw_state)
1749 == S2255_FW_SUCCESS) ||
1750 (atomic_read(&dev->fw_data->fw_state)
1751 == S2255_FW_DISCONNECTING)),
1752 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1753 /* state may have changed, re-read */
1754 state = atomic_read(&dev->fw_data->fw_state);
1756 case S2255_FW_SUCCESS:
1760 mutex_unlock(&dev->open_lock);
1761 /* state may have changed in above switch statement */
1763 case S2255_FW_SUCCESS:
1765 case S2255_FW_FAILED:
1766 printk(KERN_INFO "2255 firmware load failed.\n");
1768 case S2255_FW_DISCONNECTING:
1769 printk(KERN_INFO "%s: disconnecting\n", __func__);
1771 case S2255_FW_LOADED_DSPWAIT:
1772 case S2255_FW_NOTLOADED:
1773 printk(KERN_INFO "%s: firmware not loaded yet"
1774 "please try again later\n",
1778 printk(KERN_INFO "%s: unknown state\n", __func__);
1781 /* allocate + initialize per filehandle data */
1782 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1785 file->private_data = fh;
1787 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1788 fh->mode = dev->mode[cur_channel];
1789 fh->fmt = dev->cur_fmt[cur_channel];
1790 /* default 4CIF NTSC */
1791 fh->width = LINE_SZ_4CIFS_NTSC;
1792 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1793 fh->channel = cur_channel;
1794 /* configure channel to default state */
1795 if (!dev->chn_configured[cur_channel]) {
1796 s2255_set_mode(dev, cur_channel, &fh->mode);
1797 dev->chn_configured[cur_channel] = 1;
1799 dprintk(1, "%s: dev=%s type=%s\n", __func__,
1800 video_device_node_name(vdev), v4l2_type_names[type]);
1801 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
1802 (unsigned long)fh, (unsigned long)dev,
1803 (unsigned long)&dev->vidq[cur_channel]);
1804 dprintk(4, "%s: list_empty active=%d\n", __func__,
1805 list_empty(&dev->vidq[cur_channel].active));
1806 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1809 V4L2_FIELD_INTERLACED,
1810 sizeof(struct s2255_buffer), fh);
1815 static unsigned int s2255_poll(struct file *file,
1816 struct poll_table_struct *wait)
1818 struct s2255_fh *fh = file->private_data;
1820 dprintk(100, "%s\n", __func__);
1821 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1823 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1827 static void s2255_destroy(struct kref *kref)
1829 struct s2255_dev *dev = to_s2255_dev(kref);
1830 /* board shutdown stops the read pipe if it is running */
1831 s2255_board_shutdown(dev);
1832 /* make sure firmware still not trying to load */
1833 del_timer(&dev->timer); /* only started in .probe and .open */
1834 if (dev->fw_data->fw_urb) {
1835 usb_kill_urb(dev->fw_data->fw_urb);
1836 usb_free_urb(dev->fw_data->fw_urb);
1837 dev->fw_data->fw_urb = NULL;
1839 if (dev->fw_data->fw)
1840 release_firmware(dev->fw_data->fw);
1841 kfree(dev->fw_data->pfw_data);
1842 kfree(dev->fw_data);
1843 /* reset the DSP so firmware can be reloaded next time */
1844 s2255_reset_dsppower(dev);
1845 mutex_destroy(&dev->open_lock);
1846 mutex_destroy(&dev->lock);
1847 usb_put_dev(dev->udev);
1848 dprintk(1, "%s", __func__);
1852 static int s2255_release(struct file *file)
1854 struct s2255_fh *fh = file->private_data;
1855 struct s2255_dev *dev = fh->dev;
1856 struct video_device *vdev = video_devdata(file);
1859 /* turn off stream */
1860 if (res_check(fh)) {
1861 if (dev->b_acquire[fh->channel])
1862 s2255_stop_acquire(dev, fh->channel);
1863 videobuf_streamoff(&fh->vb_vidq);
1866 videobuf_mmap_free(&fh->vb_vidq);
1867 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
1872 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1874 struct s2255_fh *fh = file->private_data;
1879 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
1880 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1881 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
1882 (unsigned long)vma->vm_start,
1883 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1887 static const struct v4l2_file_operations s2255_fops_v4l = {
1888 .owner = THIS_MODULE,
1890 .release = s2255_release,
1892 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1893 .mmap = s2255_mmap_v4l,
1896 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1897 .vidioc_querymenu = vidioc_querymenu,
1898 .vidioc_querycap = vidioc_querycap,
1899 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1900 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1901 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1902 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1903 .vidioc_reqbufs = vidioc_reqbufs,
1904 .vidioc_querybuf = vidioc_querybuf,
1905 .vidioc_qbuf = vidioc_qbuf,
1906 .vidioc_dqbuf = vidioc_dqbuf,
1907 .vidioc_s_std = vidioc_s_std,
1908 .vidioc_enum_input = vidioc_enum_input,
1909 .vidioc_g_input = vidioc_g_input,
1910 .vidioc_s_input = vidioc_s_input,
1911 .vidioc_queryctrl = vidioc_queryctrl,
1912 .vidioc_g_ctrl = vidioc_g_ctrl,
1913 .vidioc_s_ctrl = vidioc_s_ctrl,
1914 .vidioc_streamon = vidioc_streamon,
1915 .vidioc_streamoff = vidioc_streamoff,
1916 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1917 .vidiocgmbuf = vidioc_cgmbuf,
1919 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1920 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1921 .vidioc_s_parm = vidioc_s_parm,
1922 .vidioc_g_parm = vidioc_g_parm,
1923 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1926 static void s2255_video_device_release(struct video_device *vdev)
1928 struct s2255_dev *dev = video_get_drvdata(vdev);
1929 kref_put(&dev->kref, s2255_destroy);
1933 static struct video_device template = {
1935 .fops = &s2255_fops_v4l,
1936 .ioctl_ops = &s2255_ioctl_ops,
1937 .release = s2255_video_device_release,
1938 .tvnorms = S2255_NORMS,
1939 .current_norm = V4L2_STD_NTSC_M,
1942 static int s2255_probe_v4l(struct s2255_dev *dev)
1946 int cur_nr = video_nr;
1948 /* initialize all video 4 linux */
1949 /* register 4 video devices */
1950 for (i = 0; i < MAX_CHANNELS; i++) {
1951 INIT_LIST_HEAD(&dev->vidq[i].active);
1952 dev->vidq[i].dev = dev;
1953 dev->vidq[i].channel = i;
1954 /* register 4 video devices */
1955 memcpy(&dev->vdev[i], &template, sizeof(struct video_device));
1956 dev->vdev[i].parent = &dev->interface->dev;
1958 ret = video_register_device(&dev->vdev[i],
1962 ret = video_register_device(&dev->vdev[i],
1965 video_set_drvdata(&dev->vdev[i], dev);
1968 dev_err(&dev->udev->dev,
1969 "failed to register video device!\n");
1973 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1974 S2255_MAJOR_VERSION,
1975 S2255_MINOR_VERSION);
1979 /* this function moves the usb stream read pipe data
1980 * into the system buffers.
1981 * returns 0 on success, EAGAIN if more data to process( call this
1984 * Received frame structure:
1985 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1986 * bytes 4-7: channel: 0-3
1987 * bytes 8-11: payload size: size of the frame
1988 * bytes 12-payloadsize+12: frame data
1990 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1996 unsigned long copy_size;
1999 struct s2255_framei *frm;
2000 unsigned char *pdata;
2002 dprintk(100, "buffer to user\n");
2004 idx = dev->cur_frame[dev->cc];
2005 frm = &dev->buffer[dev->cc].frame[idx];
2007 if (frm->ulState == S2255_READ_IDLE) {
2010 __le32 *pdword; /*data from dsp is little endian */
2012 /* search for marker codes */
2013 pdata = (unsigned char *)pipe_info->transfer_buffer;
2014 pdword = (__le32 *)pdata;
2015 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
2017 case S2255_MARKER_FRAME:
2018 dprintk(4, "found frame marker at offset:"
2019 " %d [%x %x]\n", jj, pdata[0],
2021 offset = jj + PREFIX_SIZE;
2024 if (cc >= MAX_CHANNELS) {
2030 dev->cc = G_chnmap[cc];
2031 payload = pdword[3];
2032 if (payload > dev->req_image_size[dev->cc]) {
2033 dev->bad_payload[dev->cc]++;
2034 /* discard the bad frame */
2037 dev->pkt_size[dev->cc] = payload;
2038 dev->jpg_size[dev->cc] = pdword[4];
2040 case S2255_MARKER_RESPONSE:
2041 pdata += DEF_USB_BLOCK;
2042 jj += DEF_USB_BLOCK;
2043 if (pdword[1] >= MAX_CHANNELS)
2045 cc = G_chnmap[pdword[1]];
2046 if (cc >= MAX_CHANNELS)
2048 switch (pdword[2]) {
2049 case S2255_RESPONSE_SETMODE:
2050 /* check if channel valid */
2051 /* set mode ready */
2052 dev->setmode_ready[cc] = 1;
2053 wake_up(&dev->wait_setmode[cc]);
2054 dprintk(5, "setmode ready %d\n", cc);
2056 case S2255_RESPONSE_FW:
2058 dev->chn_ready |= (1 << cc);
2059 if ((dev->chn_ready & 0x0f) != 0x0f)
2061 /* all channels ready */
2062 printk(KERN_INFO "s2255: fw loaded\n");
2063 atomic_set(&dev->fw_data->fw_state,
2065 wake_up(&dev->fw_data->wait_fw);
2067 case S2255_RESPONSE_STATUS:
2068 dev->vidstatus[cc] = pdword[3];
2069 dev->vidstatus_ready[cc] = 1;
2070 wake_up(&dev->wait_vidstatus[cc]);
2071 dprintk(5, "got vidstatus %x chan %d\n",
2075 printk(KERN_INFO "s2255 unknown resp\n");
2089 idx = dev->cur_frame[dev->cc];
2090 frm = &dev->buffer[dev->cc].frame[idx];
2092 /* search done. now find out if should be acquiring on this channel */
2093 if (!dev->b_acquire[dev->cc]) {
2094 /* we found a frame, but this channel is turned off */
2095 frm->ulState = S2255_READ_IDLE;
2099 if (frm->ulState == S2255_READ_IDLE) {
2100 frm->ulState = S2255_READ_FRAME;
2104 /* skip the marker 512 bytes (and offset if out of sync) */
2105 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2108 if (frm->lpvbits == NULL) {
2109 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2110 frm, dev, dev->cc, idx);
2114 pdest = frm->lpvbits + frm->cur_size;
2116 copy_size = (pipe_info->cur_transfer_size - offset);
2118 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2120 /* sanity check on pdest */
2121 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2122 memcpy(pdest, psrc, copy_size);
2124 frm->cur_size += copy_size;
2125 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2127 if (frm->cur_size >= size) {
2130 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2132 dev->last_frame[cc] = dev->cur_frame[cc];
2133 dev->cur_frame[cc]++;
2134 /* end of system frame ring buffer, start at zero */
2135 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2136 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2137 dev->cur_frame[cc] = 0;
2139 if (dev->b_acquire[cc])
2140 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2141 dev->frame_count[cc]++;
2142 frm->ulState = S2255_READ_IDLE;
2146 /* done successfully */
2150 static void s2255_read_video_callback(struct s2255_dev *dev,
2151 struct s2255_pipeinfo *pipe_info)
2154 dprintk(50, "callback read video \n");
2156 if (dev->cc >= MAX_CHANNELS) {
2158 dev_err(&dev->udev->dev, "invalid channel\n");
2161 /* otherwise copy to the system buffers */
2162 res = save_frame(dev, pipe_info);
2164 dprintk(4, "s2255: read callback failed\n");
2166 dprintk(50, "callback read video done\n");
2170 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2171 u16 Index, u16 Value, void *TransferBuffer,
2172 s32 TransferBufferLength, int bOut)
2176 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2178 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2180 Value, Index, TransferBuffer,
2181 TransferBufferLength, HZ * 5);
2183 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2184 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2185 Value, Index, TransferBuffer,
2186 TransferBufferLength, HZ * 5);
2192 * retrieve FX2 firmware version. future use.
2193 * @param dev pointer to device extension
2194 * @return -1 for fail, else returns firmware version as an int(16 bits)
2196 static int s2255_get_fx2fw(struct s2255_dev *dev)
2200 unsigned char transBuffer[64];
2201 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2204 dprintk(2, "get fw error: %x\n", ret);
2205 fw = transBuffer[0] + (transBuffer[1] << 8);
2206 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2211 * Create the system ring buffer to copy frames into from the
2214 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2217 unsigned long reqsize;
2218 dprintk(1, "create sys buffers\n");
2219 if (chn >= MAX_CHANNELS)
2222 dev->buffer[chn].dwFrames = SYS_FRAMES;
2224 /* always allocate maximum size(PAL) for system buffers */
2225 reqsize = SYS_FRAMES_MAXSIZE;
2227 if (reqsize > SYS_FRAMES_MAXSIZE)
2228 reqsize = SYS_FRAMES_MAXSIZE;
2230 for (i = 0; i < SYS_FRAMES; i++) {
2231 /* allocate the frames */
2232 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2234 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2235 &dev->buffer[chn].frame[i], chn, i,
2236 dev->buffer[chn].frame[i].lpvbits);
2237 dev->buffer[chn].frame[i].size = reqsize;
2238 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2239 printk(KERN_INFO "out of memory. using less frames\n");
2240 dev->buffer[chn].dwFrames = i;
2245 /* make sure internal states are set */
2246 for (i = 0; i < SYS_FRAMES; i++) {
2247 dev->buffer[chn].frame[i].ulState = 0;
2248 dev->buffer[chn].frame[i].cur_size = 0;
2251 dev->cur_frame[chn] = 0;
2252 dev->last_frame[chn] = -1;
2256 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2257 unsigned long channel)
2260 dprintk(1, "release sys buffers\n");
2261 for (i = 0; i < SYS_FRAMES; i++) {
2262 if (dev->buffer[channel].frame[i].lpvbits) {
2263 dprintk(1, "vfree %p\n",
2264 dev->buffer[channel].frame[i].lpvbits);
2265 vfree(dev->buffer[channel].frame[i].lpvbits);
2267 dev->buffer[channel].frame[i].lpvbits = NULL;
2272 static int s2255_board_init(struct s2255_dev *dev)
2274 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2277 struct s2255_pipeinfo *pipe = &dev->pipe;
2278 dprintk(4, "board init: %p", dev);
2279 memset(pipe, 0, sizeof(*pipe));
2281 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2282 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2284 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2286 if (pipe->transfer_buffer == NULL) {
2287 dprintk(1, "out of memory!\n");
2290 /* query the firmware */
2291 fw_ver = s2255_get_fx2fw(dev);
2293 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2294 (fw_ver >> 8) & 0xff,
2297 if (fw_ver < S2255_CUR_USB_FWVER)
2298 dev_err(&dev->udev->dev,
2299 "usb firmware not up to date %d.%d\n",
2300 (fw_ver >> 8) & 0xff,
2303 for (j = 0; j < MAX_CHANNELS; j++) {
2304 dev->b_acquire[j] = 0;
2305 dev->mode[j] = mode_def;
2306 if (dev->pid == 0x2257 && j > 1)
2307 dev->mode[j].color |= (1 << 16);
2308 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2309 dev->cur_fmt[j] = &formats[0];
2310 dev->mode[j].restart = 1;
2311 dev->req_image_size[j] = get_transfer_size(&mode_def);
2312 dev->frame_count[j] = 0;
2313 /* create the system buffers */
2314 s2255_create_sys_buffers(dev, j);
2316 /* start read pipe */
2317 s2255_start_readpipe(dev);
2318 dprintk(1, "%s: success\n", __func__);
2322 static int s2255_board_shutdown(struct s2255_dev *dev)
2325 dprintk(1, "%s: dev: %p", __func__, dev);
2327 for (i = 0; i < MAX_CHANNELS; i++) {
2328 if (dev->b_acquire[i])
2329 s2255_stop_acquire(dev, i);
2332 s2255_stop_readpipe(dev);
2334 for (i = 0; i < MAX_CHANNELS; i++)
2335 s2255_release_sys_buffers(dev, i);
2336 /* release transfer buffer */
2337 kfree(dev->pipe.transfer_buffer);
2341 static void read_pipe_completion(struct urb *purb)
2343 struct s2255_pipeinfo *pipe_info;
2344 struct s2255_dev *dev;
2347 pipe_info = purb->context;
2348 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
2350 if (pipe_info == NULL) {
2351 dev_err(&purb->dev->dev, "no context!\n");
2355 dev = pipe_info->dev;
2357 dev_err(&purb->dev->dev, "no context!\n");
2360 status = purb->status;
2361 /* if shutting down, do not resubmit, exit immediately */
2362 if (status == -ESHUTDOWN) {
2363 dprintk(2, "%s: err shutdown\n", __func__);
2364 pipe_info->err_count++;
2368 if (pipe_info->state == 0) {
2369 dprintk(2, "%s: exiting USB pipe", __func__);
2374 s2255_read_video_callback(dev, pipe_info);
2376 pipe_info->err_count++;
2377 dprintk(1, "%s: failed URB %d\n", __func__, status);
2380 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2382 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2384 pipe_info->transfer_buffer,
2385 pipe_info->cur_transfer_size,
2386 read_pipe_completion, pipe_info);
2388 if (pipe_info->state != 0) {
2389 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2390 dev_err(&dev->udev->dev, "error submitting urb\n");
2393 dprintk(2, "%s :complete state 0\n", __func__);
2398 static int s2255_start_readpipe(struct s2255_dev *dev)
2402 struct s2255_pipeinfo *pipe_info = &dev->pipe;
2403 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2404 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
2405 pipe_info->state = 1;
2406 pipe_info->err_count = 0;
2407 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2408 if (!pipe_info->stream_urb) {
2409 dev_err(&dev->udev->dev,
2410 "ReadStream: Unable to alloc URB\n");
2413 /* transfer buffer allocated in board_init */
2414 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2416 pipe_info->transfer_buffer,
2417 pipe_info->cur_transfer_size,
2418 read_pipe_completion, pipe_info);
2419 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2421 printk(KERN_ERR "s2255: start read pipe failed\n");
2427 /* starts acquisition process */
2428 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2430 unsigned char *buffer;
2432 unsigned long chn_rev;
2434 if (chn >= MAX_CHANNELS) {
2435 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2438 chn_rev = G_chnmap[chn];
2439 buffer = kzalloc(512, GFP_KERNEL);
2440 if (buffer == NULL) {
2441 dev_err(&dev->udev->dev, "out of mem\n");
2445 dev->last_frame[chn] = -1;
2446 dev->bad_payload[chn] = 0;
2447 dev->cur_frame[chn] = 0;
2448 for (j = 0; j < SYS_FRAMES; j++) {
2449 dev->buffer[chn].frame[j].ulState = 0;
2450 dev->buffer[chn].frame[j].cur_size = 0;
2453 /* send the start command */
2454 *(__le32 *) buffer = IN_DATA_TOKEN;
2455 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2456 *((__le32 *) buffer + 2) = CMD_START;
2457 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2459 dev_err(&dev->udev->dev, "CMD_START error\n");
2461 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2466 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2468 unsigned char *buffer;
2470 unsigned long chn_rev;
2471 if (chn >= MAX_CHANNELS) {
2472 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2475 chn_rev = G_chnmap[chn];
2476 buffer = kzalloc(512, GFP_KERNEL);
2477 if (buffer == NULL) {
2478 dev_err(&dev->udev->dev, "out of mem\n");
2481 /* send the stop command */
2482 *(__le32 *) buffer = IN_DATA_TOKEN;
2483 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2484 *((__le32 *) buffer + 2) = CMD_STOP;
2485 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2487 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2489 dev->b_acquire[chn] = 0;
2490 dprintk(4, "%s: chn %lu, res %d\n", __func__, chn, res);
2494 static void s2255_stop_readpipe(struct s2255_dev *dev)
2496 struct s2255_pipeinfo *pipe = &dev->pipe;
2498 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2502 if (pipe->stream_urb) {
2504 usb_kill_urb(pipe->stream_urb);
2505 usb_free_urb(pipe->stream_urb);
2506 pipe->stream_urb = NULL;
2508 dprintk(4, "%s", __func__);
2512 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2515 s2255_reset_dsppower(dev);
2516 dev->fw_data->fw_size = dev->fw_data->fw->size;
2517 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2518 memcpy(dev->fw_data->pfw_data,
2519 dev->fw_data->fw->data, CHUNK_SIZE);
2520 dev->fw_data->fw_loaded = CHUNK_SIZE;
2521 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2522 usb_sndbulkpipe(dev->udev, 2),
2523 dev->fw_data->pfw_data,
2524 CHUNK_SIZE, s2255_fwchunk_complete,
2526 mod_timer(&dev->timer, jiffies + HZ);
2529 /* standard usb probe function */
2530 static int s2255_probe(struct usb_interface *interface,
2531 const struct usb_device_id *id)
2533 struct s2255_dev *dev = NULL;
2534 struct usb_host_interface *iface_desc;
2535 struct usb_endpoint_descriptor *endpoint;
2537 int retval = -ENOMEM;
2540 dprintk(2, "%s\n", __func__);
2541 /* allocate memory for our device state and initialize it to zero */
2542 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2544 s2255_dev_err(&interface->dev, "out of memory\n");
2547 kref_init(&dev->kref);
2548 dev->pid = id->idProduct;
2549 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2552 mutex_init(&dev->lock);
2553 mutex_init(&dev->open_lock);
2554 /* grab usb_device and save it */
2555 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2556 if (dev->udev == NULL) {
2557 dev_err(&interface->dev, "null usb device\n");
2561 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2562 dev->udev, interface);
2563 dev->interface = interface;
2564 /* set up the endpoint information */
2565 iface_desc = interface->cur_altsetting;
2566 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2567 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2568 endpoint = &iface_desc->endpoint[i].desc;
2569 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2570 /* we found the bulk in endpoint */
2571 dev->read_endpoint = endpoint->bEndpointAddress;
2575 if (!dev->read_endpoint) {
2576 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2580 usb_set_intfdata(interface, dev);
2581 init_timer(&dev->timer);
2582 dev->timer.function = s2255_timer;
2583 dev->timer.data = (unsigned long)dev->fw_data;
2584 init_waitqueue_head(&dev->fw_data->wait_fw);
2585 for (i = 0; i < MAX_CHANNELS; i++) {
2586 init_waitqueue_head(&dev->wait_setmode[i]);
2587 init_waitqueue_head(&dev->wait_vidstatus[i]);
2590 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2591 if (!dev->fw_data->fw_urb) {
2592 dev_err(&interface->dev, "out of memory!\n");
2596 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2597 if (!dev->fw_data->pfw_data) {
2598 dev_err(&interface->dev, "out of memory!\n");
2601 /* load the first chunk */
2602 if (request_firmware(&dev->fw_data->fw,
2603 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2604 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2607 /* check the firmware is valid */
2608 fw_size = dev->fw_data->fw->size;
2609 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2611 if (*pdata != S2255_FW_MARKER) {
2612 printk(KERN_INFO "Firmware invalid.\n");
2616 /* make sure firmware is the latest */
2618 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2619 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2620 dev->dsp_fw_ver = *pRel;
2621 if (*pRel < S2255_CUR_DSP_FWVER)
2622 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
2623 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
2624 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2625 "or above.\n", S2255_MIN_DSP_COLORFILTER);
2627 usb_reset_device(dev->udev);
2628 /* load 2255 board specific */
2629 retval = s2255_board_init(dev);
2631 goto errorBOARDINIT;
2632 spin_lock_init(&dev->slock);
2633 s2255_fwload_start(dev, 0);
2634 /* kref for each vdev. Released on video_device_release callback */
2635 for (i = 0; i < MAX_CHANNELS; i++)
2636 kref_get(&dev->kref);
2637 /* loads v4l specific */
2638 retval = s2255_probe_v4l(dev);
2641 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2644 for (i = 0; i < MAX_CHANNELS; i++)
2645 if (video_is_registered(&dev->vdev[i]))
2646 video_unregister_device(&dev->vdev[i]);
2648 s2255_board_shutdown(dev);
2650 release_firmware(dev->fw_data->fw);
2652 kfree(dev->fw_data->pfw_data);
2654 usb_free_urb(dev->fw_data->fw_urb);
2656 del_timer(&dev->timer);
2658 usb_put_dev(dev->udev);
2660 kfree(dev->fw_data);
2661 mutex_destroy(&dev->open_lock);
2662 mutex_destroy(&dev->lock);
2665 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
2670 /* disconnect routine. when board is removed physically or with rmmod */
2671 static void s2255_disconnect(struct usb_interface *interface)
2673 struct s2255_dev *dev = NULL;
2675 dev = usb_get_intfdata(interface);
2676 /* unregister each video device. */
2677 for (i = 0; i < MAX_CHANNELS; i++)
2678 if (video_is_registered(&dev->vdev[i]))
2679 video_unregister_device(&dev->vdev[i]);
2680 /* wake up any of our timers */
2681 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2682 wake_up(&dev->fw_data->wait_fw);
2683 for (i = 0; i < MAX_CHANNELS; i++) {
2684 dev->setmode_ready[i] = 1;
2685 wake_up(&dev->wait_setmode[i]);
2686 dev->vidstatus_ready[i] = 1;
2687 wake_up(&dev->wait_vidstatus[i]);
2689 usb_set_intfdata(interface, NULL);
2690 kref_put(&dev->kref, s2255_destroy);
2691 dev_info(&interface->dev, "%s\n", __func__);
2694 static struct usb_driver s2255_driver = {
2695 .name = S2255_DRIVER_NAME,
2696 .probe = s2255_probe,
2697 .disconnect = s2255_disconnect,
2698 .id_table = s2255_table,
2701 static int __init usb_s2255_init(void)
2704 /* register this driver with the USB subsystem */
2705 result = usb_register(&s2255_driver);
2707 pr_err(KBUILD_MODNAME
2708 ": usb_register failed. Error number %d\n", result);
2709 dprintk(2, "%s\n", __func__);
2713 static void __exit usb_s2255_exit(void)
2715 usb_deregister(&s2255_driver);
2718 module_init(usb_s2255_init);
2719 module_exit(usb_s2255_exit);
2721 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2722 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2723 MODULE_LICENSE("GPL");