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 FIRMWARE_FILE_NAME "f2255usb.bin"
63 /* default JPEG quality */
64 #define S2255_DEF_JPEG_QUAL 50
65 /* vendor request in */
67 /* vendor request out */
68 #define S2255_VR_OUT 1
70 #define S2255_VR_FW 0x30
71 /* USB endpoint number for configuring the device */
72 #define S2255_CONFIG_EP 2
73 /* maximum time for DSP to start responding after last FW word loaded(ms) */
74 #define S2255_DSP_BOOTTIME 800
75 /* maximum time to wait for firmware to load (ms) */
76 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
77 #define S2255_DEF_BUFS 16
78 #define S2255_SETMODE_TIMEOUT 500
79 #define S2255_VIDSTATUS_TIMEOUT 350
80 #define MAX_CHANNELS 4
81 #define S2255_MARKER_FRAME 0x2255DA4AL
82 #define S2255_MARKER_RESPONSE 0x2255ACACL
83 #define S2255_RESPONSE_SETMODE 0x01
84 #define S2255_RESPONSE_FW 0x10
85 #define S2255_RESPONSE_STATUS 0x20
86 #define S2255_USB_XFER_SIZE (16 * 1024)
87 #define MAX_CHANNELS 4
88 #define MAX_PIPE_BUFFERS 1
90 /* maximum size is PAL full size plus room for the marker header(s) */
91 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
92 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
93 #define LINE_SZ_4CIFS_NTSC 640
94 #define LINE_SZ_2CIFS_NTSC 640
95 #define LINE_SZ_1CIFS_NTSC 320
96 #define LINE_SZ_4CIFS_PAL 704
97 #define LINE_SZ_2CIFS_PAL 704
98 #define LINE_SZ_1CIFS_PAL 352
99 #define NUM_LINES_4CIFS_NTSC 240
100 #define NUM_LINES_2CIFS_NTSC 240
101 #define NUM_LINES_1CIFS_NTSC 240
102 #define NUM_LINES_4CIFS_PAL 288
103 #define NUM_LINES_2CIFS_PAL 288
104 #define NUM_LINES_1CIFS_PAL 288
105 #define LINE_SZ_DEF 640
106 #define NUM_LINES_DEF 240
109 /* predefined settings */
110 #define FORMAT_NTSC 1
113 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
114 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
115 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
116 /* SCALE_4CIFSI is the 2 fields interpolated into one */
117 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
119 #define COLOR_YUVPL 1 /* YUV planar */
120 #define COLOR_YUVPK 2 /* YUV packed */
121 #define COLOR_Y8 4 /* monochrome */
122 #define COLOR_JPG 5 /* JPEG */
123 #define MASK_COLOR 0xff
124 #define MASK_JPG_QUALITY 0xff00
126 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
127 #define FDEC_1 1 /* capture every frame. default */
128 #define FDEC_2 2 /* capture every 2nd frame */
129 #define FDEC_3 3 /* capture every 3rd frame */
130 #define FDEC_5 5 /* capture every 5th frame */
132 /*-------------------------------------------------------
133 * Default mode parameters.
134 *-------------------------------------------------------*/
135 #define DEF_SCALE SCALE_4CIFS
136 #define DEF_COLOR COLOR_YUVPL
137 #define DEF_FDEC FDEC_1
139 #define DEF_CONTRAST 0x5c
140 #define DEF_SATURATION 0x80
143 /* usb config commands */
144 #define IN_DATA_TOKEN 0x2255c0de
145 #define CMD_2255 0xc2255000
146 #define CMD_SET_MODE (CMD_2255 | 0x10)
147 #define CMD_START (CMD_2255 | 0x20)
148 #define CMD_STOP (CMD_2255 | 0x30)
149 #define CMD_STATUS (CMD_2255 | 0x40)
152 u32 format; /* input video format (NTSC, PAL) */
153 u32 scale; /* output video scale */
154 u32 color; /* output video color format */
155 u32 fdec; /* frame decimation */
156 u32 bright; /* brightness */
157 u32 contrast; /* contrast */
158 u32 saturation; /* saturation */
159 u32 hue; /* hue (NTSC only)*/
160 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
161 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
162 u32 restart; /* if DSP requires restart */
166 #define S2255_READ_IDLE 0
167 #define S2255_READ_FRAME 1
169 /* frame structure */
170 struct s2255_framei {
172 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
173 void *lpvbits; /* image data */
174 unsigned long cur_size; /* current data copied to it */
177 /* image buffer structure */
178 struct s2255_bufferi {
179 unsigned long dwFrames; /* number of frames in buffer */
180 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
183 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
184 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
185 DEF_HUE, 0, DEF_USB_BLOCK, 0}
187 struct s2255_dmaqueue {
188 struct list_head active;
189 struct s2255_dev *dev;
193 /* for firmware loading, fw_state */
194 #define S2255_FW_NOTLOADED 0
195 #define S2255_FW_LOADED_DSPWAIT 1
196 #define S2255_FW_SUCCESS 2
197 #define S2255_FW_FAILED 3
198 #define S2255_FW_DISCONNECTING 4
200 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
201 /* 2255 read states */
202 #define S2255_READ_IDLE 0
203 #define S2255_READ_FRAME 1
210 wait_queue_head_t wait_fw;
211 const struct firmware *fw;
214 struct s2255_pipeinfo {
215 u32 max_transfer_size;
216 u32 cur_transfer_size;
220 void *dev; /* back pointer to s2255_dev struct*/
225 struct s2255_fmt; /*forward declaration */
229 int users[MAX_CHANNELS];
231 struct mutex open_lock;
232 int resources[MAX_CHANNELS];
233 struct usb_device *udev;
234 struct usb_interface *interface;
237 struct s2255_dmaqueue vidq[MAX_CHANNELS];
238 struct video_device *vdev[MAX_CHANNELS];
239 struct timer_list timer;
240 struct s2255_fw *fw_data;
241 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
242 struct s2255_bufferi buffer[MAX_CHANNELS];
243 struct s2255_mode mode[MAX_CHANNELS];
244 /* jpeg compression */
245 struct v4l2_jpegcompression jc[MAX_CHANNELS];
246 /* capture parameters (for high quality mode full size) */
247 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
248 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
249 int cur_frame[MAX_CHANNELS];
250 int last_frame[MAX_CHANNELS];
251 u32 cc; /* current channel */
252 int b_acquire[MAX_CHANNELS];
253 /* allocated image size */
254 unsigned long req_image_size[MAX_CHANNELS];
255 /* received packet size */
256 unsigned long pkt_size[MAX_CHANNELS];
257 int bad_payload[MAX_CHANNELS];
258 unsigned long frame_count[MAX_CHANNELS];
261 int jpg_size[MAX_CHANNELS];
262 /* if channel configured to default state */
263 int chn_configured[MAX_CHANNELS];
264 wait_queue_head_t wait_setmode[MAX_CHANNELS];
265 int setmode_ready[MAX_CHANNELS];
266 /* video status items */
267 int vidstatus[MAX_CHANNELS];
268 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
269 int vidstatus_ready[MAX_CHANNELS];
274 /* dsp firmware version (f2255usb.bin) */
277 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
285 /* buffer for one video frame */
286 struct s2255_buffer {
287 /* common v4l buffer stuff -- must be first */
288 struct videobuf_buffer vb;
289 const struct s2255_fmt *fmt;
293 struct s2255_dev *dev;
294 const struct s2255_fmt *fmt;
297 struct videobuf_queue vb_vidq;
298 enum v4l2_buf_type type;
300 /* mode below is the desired mode.
301 mode in s2255_dev is the current mode that was last set */
302 struct s2255_mode mode;
303 int resources[MAX_CHANNELS];
306 /* current cypress EEPROM firmware version */
307 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
308 /* current DSP FW version */
309 #define S2255_CUR_DSP_FWVER 5
310 /* Need DSP version 5+ for video status feature */
311 #define S2255_MIN_DSP_STATUS 5
312 #define S2255_MAJOR_VERSION 1
313 #define S2255_MINOR_VERSION 15
314 #define S2255_RELEASE 0
315 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
316 S2255_MINOR_VERSION, \
320 #define USB_S2255_VENDOR_ID 0x1943
321 #define USB_S2255_PRODUCT_ID 0x2255
322 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
323 /* frame prefix size (sent once every frame) */
324 #define PREFIX_SIZE 512
326 /* Channels on box are in reverse order */
327 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
330 static int *s2255_debug = &debug;
332 static int s2255_start_readpipe(struct s2255_dev *dev);
333 static void s2255_stop_readpipe(struct s2255_dev *dev);
334 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
335 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
336 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
337 int chn, int jpgsize);
338 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
339 struct s2255_mode *mode);
340 static int s2255_board_shutdown(struct s2255_dev *dev);
341 static void s2255_exit_v4l(struct s2255_dev *dev);
342 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
343 static void s2255_destroy(struct kref *kref);
344 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
345 u16 index, u16 value, void *buf,
346 s32 buf_len, int bOut);
348 /* dev_err macro with driver name */
349 #define S2255_DRIVER_NAME "s2255"
350 #define s2255_dev_err(dev, fmt, arg...) \
351 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
353 #define dprintk(level, fmt, arg...) \
355 if (*s2255_debug >= (level)) { \
356 printk(KERN_DEBUG S2255_DRIVER_NAME \
361 static struct usb_driver s2255_driver;
364 /* Declare static vars that will be used as parameters */
365 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
367 /* start video number */
368 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
370 module_param(debug, int, 0644);
371 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
372 module_param(vid_limit, int, 0644);
373 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
374 module_param(video_nr, int, 0644);
375 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
377 /* USB device table */
378 static struct usb_device_id s2255_table[] = {
379 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
380 { } /* Terminating entry */
382 MODULE_DEVICE_TABLE(usb, s2255_table);
385 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
387 /* supported controls */
388 static struct v4l2_queryctrl s2255_qctrl[] = {
390 .id = V4L2_CID_BRIGHTNESS,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 .name = "Brightness",
399 .id = V4L2_CID_CONTRAST,
400 .type = V4L2_CTRL_TYPE_INTEGER,
405 .default_value = DEF_CONTRAST,
408 .id = V4L2_CID_SATURATION,
409 .type = V4L2_CTRL_TYPE_INTEGER,
410 .name = "Saturation",
414 .default_value = DEF_SATURATION,
418 .type = V4L2_CTRL_TYPE_INTEGER,
423 .default_value = DEF_HUE,
428 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
431 static const struct s2255_fmt formats[] = {
433 .name = "4:2:2, planar, YUV422P",
434 .fourcc = V4L2_PIX_FMT_YUV422P,
438 .name = "4:2:2, packed, YUYV",
439 .fourcc = V4L2_PIX_FMT_YUYV,
443 .name = "4:2:2, packed, UYVY",
444 .fourcc = V4L2_PIX_FMT_UYVY,
448 .fourcc = V4L2_PIX_FMT_JPEG,
452 .fourcc = V4L2_PIX_FMT_GREY,
457 static int norm_maxw(struct video_device *vdev)
459 return (vdev->current_norm & V4L2_STD_NTSC) ?
460 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
463 static int norm_maxh(struct video_device *vdev)
465 return (vdev->current_norm & V4L2_STD_NTSC) ?
466 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
469 static int norm_minw(struct video_device *vdev)
471 return (vdev->current_norm & V4L2_STD_NTSC) ?
472 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
475 static int norm_minh(struct video_device *vdev)
477 return (vdev->current_norm & V4L2_STD_NTSC) ?
478 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
483 * TODO: fixme: move YUV reordering to hardware
484 * converts 2255 planar format to yuyv or uyvy
486 static void planar422p_to_yuv_packed(const unsigned char *in,
488 int width, int height,
494 unsigned long size = height * width;
496 pY = (unsigned char *)in;
497 pCr = (unsigned char *)in + height * width;
498 pCb = (unsigned char *)in + height * width + (height * width / 2);
499 for (i = 0; i < size * 2; i += 4) {
500 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
501 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
502 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
503 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
508 static void s2255_reset_dsppower(struct s2255_dev *dev)
510 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
512 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
516 /* kickstarts the firmware loading. from probe
518 static void s2255_timer(unsigned long user_data)
520 struct s2255_fw *data = (struct s2255_fw *)user_data;
521 dprintk(100, "s2255 timer\n");
522 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
523 printk(KERN_ERR "s2255: can't submit urb\n");
524 atomic_set(&data->fw_state, S2255_FW_FAILED);
525 /* wake up anything waiting for the firmware */
526 wake_up(&data->wait_fw);
532 /* this loads the firmware asynchronously.
533 Originally this was done synchroously in probe.
534 But it is better to load it asynchronously here than block
535 inside the probe function. Blocking inside probe affects boot time.
536 FW loading is triggered by the timer in the probe function
538 static void s2255_fwchunk_complete(struct urb *urb)
540 struct s2255_fw *data = urb->context;
541 struct usb_device *udev = urb->dev;
543 dprintk(100, "udev %p urb %p", udev, urb);
545 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
546 atomic_set(&data->fw_state, S2255_FW_FAILED);
547 /* wake up anything waiting for the firmware */
548 wake_up(&data->wait_fw);
551 if (data->fw_urb == NULL) {
552 s2255_dev_err(&udev->dev, "disconnected\n");
553 atomic_set(&data->fw_state, S2255_FW_FAILED);
554 /* wake up anything waiting for the firmware */
555 wake_up(&data->wait_fw);
558 #define CHUNK_SIZE 512
559 /* all USB transfers must be done with continuous kernel memory.
560 can't allocate more than 128k in current linux kernel, so
561 upload the firmware in chunks
563 if (data->fw_loaded < data->fw_size) {
564 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
565 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
567 if (len < CHUNK_SIZE)
568 memset(data->pfw_data, 0, CHUNK_SIZE);
570 dprintk(100, "completed len %d, loaded %d \n", len,
573 memcpy(data->pfw_data,
574 (char *) data->fw->data + data->fw_loaded, len);
576 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
577 data->pfw_data, CHUNK_SIZE,
578 s2255_fwchunk_complete, data);
579 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
580 dev_err(&udev->dev, "failed submit URB\n");
581 atomic_set(&data->fw_state, S2255_FW_FAILED);
582 /* wake up anything waiting for the firmware */
583 wake_up(&data->wait_fw);
586 data->fw_loaded += len;
588 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
590 dprintk(100, "2255 complete done\n");
595 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
597 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
598 struct s2255_buffer *buf;
599 unsigned long flags = 0;
601 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
602 spin_lock_irqsave(&dev->slock, flags);
604 if (list_empty(&dma_q->active)) {
605 dprintk(1, "No active queue to serve\n");
609 buf = list_entry(dma_q->active.next,
610 struct s2255_buffer, vb.queue);
612 list_del(&buf->vb.queue);
613 do_gettimeofday(&buf->vb.ts);
614 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
615 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
616 wake_up(&buf->vb.done);
617 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
619 spin_unlock_irqrestore(&dev->slock, flags);
624 static const struct s2255_fmt *format_by_fourcc(int fourcc)
628 for (i = 0; i < ARRAY_SIZE(formats); i++) {
629 if (-1 == formats[i].fourcc)
631 if (formats[i].fourcc == fourcc)
640 /* video buffer vmalloc implementation based partly on VIVI driver which is
641 * Copyright (c) 2006 by
642 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
643 * Ted Walther <ted--a.t--enumera.com>
644 * John Sokol <sokol--a.t--videotechnology.com>
645 * http://v4l.videotechnology.com/
648 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
649 int chn, int jpgsize)
654 char *vbuf = videobuf_to_vmalloc(&buf->vb);
655 unsigned long last_frame;
656 struct s2255_framei *frm;
661 last_frame = dev->last_frame[chn];
662 if (last_frame != -1) {
663 frm = &dev->buffer[chn].frame[last_frame];
665 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
666 switch (buf->fmt->fourcc) {
667 case V4L2_PIX_FMT_YUYV:
668 case V4L2_PIX_FMT_UYVY:
669 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
674 case V4L2_PIX_FMT_GREY:
675 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
677 case V4L2_PIX_FMT_JPEG:
678 buf->vb.size = jpgsize;
679 memcpy(vbuf, tmpbuf, buf->vb.size);
681 case V4L2_PIX_FMT_YUV422P:
683 buf->vb.width * buf->vb.height * 2);
686 printk(KERN_DEBUG "s2255: unknown format?\n");
688 dev->last_frame[chn] = -1;
690 printk(KERN_ERR "s2255: =======no frame\n");
694 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
695 (unsigned long)vbuf, pos);
696 /* tell v4l buffer was filled */
698 buf->vb.field_count = dev->frame_count[chn] * 2;
699 do_gettimeofday(&ts);
701 buf->vb.state = VIDEOBUF_DONE;
705 /* ------------------------------------------------------------------
707 ------------------------------------------------------------------*/
709 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
712 struct s2255_fh *fh = vq->priv_data;
714 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
717 *count = S2255_DEF_BUFS;
719 while (*size * (*count) > vid_limit * 1024 * 1024)
725 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
727 dprintk(4, "%s\n", __func__);
729 videobuf_vmalloc_free(&buf->vb);
730 buf->vb.state = VIDEOBUF_NEEDS_INIT;
733 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
734 enum v4l2_field field)
736 struct s2255_fh *fh = vq->priv_data;
737 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
739 dprintk(4, "%s, field=%d\n", __func__, field);
743 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
744 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
745 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
746 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
747 dprintk(4, "invalid buffer prepare\n");
751 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
753 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
754 dprintk(4, "invalid buffer prepare\n");
759 buf->vb.width = fh->width;
760 buf->vb.height = fh->height;
761 buf->vb.field = field;
764 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
765 rc = videobuf_iolock(vq, &buf->vb, NULL);
770 buf->vb.state = VIDEOBUF_PREPARED;
773 free_buffer(vq, buf);
777 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
779 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
780 struct s2255_fh *fh = vq->priv_data;
781 struct s2255_dev *dev = fh->dev;
782 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
784 dprintk(1, "%s\n", __func__);
786 buf->vb.state = VIDEOBUF_QUEUED;
787 list_add_tail(&buf->vb.queue, &vidq->active);
790 static void buffer_release(struct videobuf_queue *vq,
791 struct videobuf_buffer *vb)
793 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
794 struct s2255_fh *fh = vq->priv_data;
795 dprintk(4, "%s %d\n", __func__, fh->channel);
796 free_buffer(vq, buf);
799 static struct videobuf_queue_ops s2255_video_qops = {
800 .buf_setup = buffer_setup,
801 .buf_prepare = buffer_prepare,
802 .buf_queue = buffer_queue,
803 .buf_release = buffer_release,
807 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
810 mutex_lock(&dev->lock);
811 if (dev->resources[fh->channel]) {
812 /* no, someone else uses it */
813 mutex_unlock(&dev->lock);
816 /* it's free, grab it */
817 dev->resources[fh->channel] = 1;
818 fh->resources[fh->channel] = 1;
819 dprintk(1, "s2255: res: get\n");
820 mutex_unlock(&dev->lock);
824 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
826 return dev->resources[fh->channel];
829 static int res_check(struct s2255_fh *fh)
831 return fh->resources[fh->channel];
835 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
837 mutex_lock(&dev->lock);
838 dev->resources[fh->channel] = 0;
839 fh->resources[fh->channel] = 0;
840 mutex_unlock(&dev->lock);
841 dprintk(1, "res: put\n");
845 static int vidioc_querycap(struct file *file, void *priv,
846 struct v4l2_capability *cap)
848 struct s2255_fh *fh = file->private_data;
849 struct s2255_dev *dev = fh->dev;
850 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
851 strlcpy(cap->card, "s2255", sizeof(cap->card));
852 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
853 cap->version = S2255_VERSION;
854 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
858 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
859 struct v4l2_fmtdesc *f)
865 if (index >= ARRAY_SIZE(formats))
868 dprintk(4, "name %s\n", formats[index].name);
869 strlcpy(f->description, formats[index].name, sizeof(f->description));
870 f->pixelformat = formats[index].fourcc;
874 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_format *f)
877 struct s2255_fh *fh = priv;
879 f->fmt.pix.width = fh->width;
880 f->fmt.pix.height = fh->height;
881 f->fmt.pix.field = fh->vb_vidq.field;
882 f->fmt.pix.pixelformat = fh->fmt->fourcc;
883 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
884 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
888 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
889 struct v4l2_format *f)
891 const struct s2255_fmt *fmt;
892 enum v4l2_field field;
894 struct s2255_fh *fh = priv;
895 struct s2255_dev *dev = fh->dev;
899 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
901 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
906 field = f->fmt.pix.field;
907 if (field == V4L2_FIELD_ANY)
910 dprintk(4, "try format %d \n", is_ntsc);
911 /* supports 3 sizes. see s2255drv.h */
912 dprintk(50, "width test %d, height %d\n",
913 f->fmt.pix.width, f->fmt.pix.height);
916 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
917 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
919 field = V4L2_FIELD_SEQ_TB;
920 } else if (!((field == V4L2_FIELD_INTERLACED) ||
921 (field == V4L2_FIELD_SEQ_TB) ||
922 (field == V4L2_FIELD_INTERLACED_TB))) {
923 dprintk(1, "unsupported field setting\n");
927 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
929 field = V4L2_FIELD_TOP;
930 } else if (!((field == V4L2_FIELD_TOP) ||
931 (field == V4L2_FIELD_BOTTOM))) {
932 dprintk(1, "unsupported field setting\n");
937 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
938 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
939 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
940 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
941 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
942 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
944 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
947 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
948 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
950 field = V4L2_FIELD_SEQ_TB;
951 } else if (!((field == V4L2_FIELD_INTERLACED) ||
952 (field == V4L2_FIELD_SEQ_TB) ||
953 (field == V4L2_FIELD_INTERLACED_TB))) {
954 dprintk(1, "unsupported field setting\n");
958 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
960 field = V4L2_FIELD_TOP;
961 } else if (!((field == V4L2_FIELD_TOP) ||
962 (field == V4L2_FIELD_BOTTOM))) {
963 dprintk(1, "unsupported field setting\n");
967 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
968 dprintk(50, "pal 704\n");
969 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
970 field = V4L2_FIELD_SEQ_TB;
971 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
972 dprintk(50, "pal 352A\n");
973 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
974 field = V4L2_FIELD_TOP;
975 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
976 dprintk(50, "pal 352B\n");
977 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
978 field = V4L2_FIELD_TOP;
980 dprintk(50, "pal 352C\n");
981 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
982 field = V4L2_FIELD_TOP;
986 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
987 f->fmt.pix.height, f->fmt.pix.field);
988 f->fmt.pix.field = field;
989 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
990 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
994 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
995 struct v4l2_format *f)
997 struct s2255_fh *fh = priv;
998 const struct s2255_fmt *fmt;
999 struct videobuf_queue *q = &fh->vb_vidq;
1003 ret = vidioc_try_fmt_vid_cap(file, fh, f);
1008 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1013 mutex_lock(&q->vb_lock);
1015 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1016 dprintk(1, "queue busy\n");
1021 if (res_locked(fh->dev, fh)) {
1022 dprintk(1, "can't change format after started\n");
1028 fh->width = f->fmt.pix.width;
1029 fh->height = f->fmt.pix.height;
1030 fh->vb_vidq.field = f->fmt.pix.field;
1032 norm = norm_minw(fh->dev->vdev[fh->channel]);
1033 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1034 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1035 if (fh->dev->cap_parm[fh->channel].capturemode &
1036 V4L2_MODE_HIGHQUALITY) {
1037 fh->mode.scale = SCALE_4CIFSI;
1038 dprintk(2, "scale 4CIFSI\n");
1040 fh->mode.scale = SCALE_4CIFS;
1041 dprintk(2, "scale 4CIFS\n");
1044 fh->mode.scale = SCALE_2CIFS;
1047 fh->mode.scale = SCALE_1CIFS;
1051 switch (fh->fmt->fourcc) {
1052 case V4L2_PIX_FMT_GREY:
1053 fh->mode.color = COLOR_Y8;
1055 case V4L2_PIX_FMT_JPEG:
1056 fh->mode.color = COLOR_JPG |
1057 (fh->dev->jc[fh->channel].quality << 8);
1059 case V4L2_PIX_FMT_YUV422P:
1060 fh->mode.color = COLOR_YUVPL;
1062 case V4L2_PIX_FMT_YUYV:
1063 case V4L2_PIX_FMT_UYVY:
1065 fh->mode.color = COLOR_YUVPK;
1070 mutex_unlock(&q->vb_lock);
1074 static int vidioc_reqbufs(struct file *file, void *priv,
1075 struct v4l2_requestbuffers *p)
1078 struct s2255_fh *fh = priv;
1079 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1083 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1086 struct s2255_fh *fh = priv;
1087 rc = videobuf_querybuf(&fh->vb_vidq, p);
1091 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1094 struct s2255_fh *fh = priv;
1095 rc = videobuf_qbuf(&fh->vb_vidq, p);
1099 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1102 struct s2255_fh *fh = priv;
1103 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1107 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1108 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1110 struct s2255_fh *fh = priv;
1112 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1116 /* write to the configuration pipe, synchronously */
1117 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1124 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1125 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1130 static u32 get_transfer_size(struct s2255_mode *mode)
1132 int linesPerFrame = LINE_SZ_DEF;
1133 int pixelsPerLine = NUM_LINES_DEF;
1136 unsigned int mask_mult;
1141 if (mode->format == FORMAT_NTSC) {
1142 switch (mode->scale) {
1145 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1146 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1149 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1150 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1153 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1154 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1159 } else if (mode->format == FORMAT_PAL) {
1160 switch (mode->scale) {
1163 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1164 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1167 linesPerFrame = NUM_LINES_2CIFS_PAL;
1168 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1171 linesPerFrame = NUM_LINES_1CIFS_PAL;
1172 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1178 outImageSize = linesPerFrame * pixelsPerLine;
1179 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1180 /* 2 bytes/pixel if not monochrome */
1184 /* total bytes to send including prefix and 4K padding;
1185 must be a multiple of USB_READ_SIZE */
1186 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1187 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1188 /* if size not a multiple of USB_READ_SIZE */
1189 if (usbInSize & ~mask_mult)
1190 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1194 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1196 struct device *dev = &sdev->udev->dev;
1197 dev_info(dev, "------------------------------------------------\n");
1198 dev_info(dev, "verify mode\n");
1199 dev_info(dev, "format: %d\n", mode->format);
1200 dev_info(dev, "scale: %d\n", mode->scale);
1201 dev_info(dev, "fdec: %d\n", mode->fdec);
1202 dev_info(dev, "color: %d\n", mode->color);
1203 dev_info(dev, "bright: 0x%x\n", mode->bright);
1204 dev_info(dev, "restart: 0x%x\n", mode->restart);
1205 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1206 dev_info(dev, "single: 0x%x\n", mode->single);
1207 dev_info(dev, "------------------------------------------------\n");
1211 * set mode is the function which controls the DSP.
1212 * the restart parameter in struct s2255_mode should be set whenever
1213 * the image size could change via color format, video system or image
1215 * When the restart parameter is set, we sleep for ONE frame to allow the
1216 * DSP time to get the new frame
1218 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1219 struct s2255_mode *mode)
1223 unsigned long chn_rev;
1225 mutex_lock(&dev->lock);
1226 chn_rev = G_chnmap[chn];
1227 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1228 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1229 dev->mode[chn].scale);
1230 dprintk(2, "mode contrast %x\n", mode->contrast);
1232 /* if JPEG, set the quality */
1233 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1234 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1237 dev->mode[chn] = *mode;
1238 dev->req_image_size[chn] = get_transfer_size(mode);
1239 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1241 buffer = kzalloc(512, GFP_KERNEL);
1242 if (buffer == NULL) {
1243 dev_err(&dev->udev->dev, "out of mem\n");
1244 mutex_unlock(&dev->lock);
1249 buffer[0] = IN_DATA_TOKEN;
1250 buffer[1] = (u32) chn_rev;
1251 buffer[2] = CMD_SET_MODE;
1252 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1253 dev->setmode_ready[chn] = 0;
1254 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1256 dump_verify_mode(dev, mode);
1258 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1260 /* wait at least 3 frames before continuing */
1261 if (mode->restart) {
1262 wait_event_timeout(dev->wait_setmode[chn],
1263 (dev->setmode_ready[chn] != 0),
1264 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1265 if (dev->setmode_ready[chn] != 1) {
1266 printk(KERN_DEBUG "s2255: no set mode response\n");
1271 /* clear the restart flag */
1272 dev->mode[chn].restart = 0;
1273 mutex_unlock(&dev->lock);
1277 static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1283 mutex_lock(&dev->lock);
1284 chn_rev = G_chnmap[chn];
1285 dprintk(4, "%s chan %d\n", __func__, chn_rev);
1286 buffer = kzalloc(512, GFP_KERNEL);
1287 if (buffer == NULL) {
1288 dev_err(&dev->udev->dev, "out of mem\n");
1289 mutex_unlock(&dev->lock);
1292 /* form the get vid status command */
1293 buffer[0] = IN_DATA_TOKEN;
1294 buffer[1] = chn_rev;
1295 buffer[2] = CMD_STATUS;
1297 dev->vidstatus_ready[chn] = 0;
1298 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1300 wait_event_timeout(dev->wait_vidstatus[chn],
1301 (dev->vidstatus_ready[chn] != 0),
1302 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1303 if (dev->vidstatus_ready[chn] != 1) {
1304 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1307 *pstatus = dev->vidstatus[chn];
1308 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1309 mutex_unlock(&dev->lock);
1313 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1316 struct s2255_fh *fh = priv;
1317 struct s2255_dev *dev = fh->dev;
1318 struct s2255_mode *new_mode;
1319 struct s2255_mode *old_mode;
1322 dprintk(4, "%s\n", __func__);
1323 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1324 dev_err(&dev->udev->dev, "invalid fh type0\n");
1327 if (i != fh->type) {
1328 dev_err(&dev->udev->dev, "invalid fh type1\n");
1332 if (!res_get(dev, fh)) {
1333 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1337 /* send a set mode command everytime with restart.
1338 in case we switch resolutions or other parameters */
1340 new_mode = &fh->mode;
1341 old_mode = &fh->dev->mode[chn];
1343 if (new_mode->color != old_mode->color)
1344 new_mode->restart = 1;
1345 else if (new_mode->scale != old_mode->scale)
1346 new_mode->restart = 1;
1347 else if (new_mode->format != old_mode->format)
1348 new_mode->restart = 1;
1350 s2255_set_mode(dev, chn, new_mode);
1351 new_mode->restart = 0;
1352 *old_mode = *new_mode;
1353 dev->cur_fmt[chn] = fh->fmt;
1354 dprintk(1, "%s[%d]\n", __func__, chn);
1355 dev->last_frame[chn] = -1;
1356 dev->bad_payload[chn] = 0;
1357 dev->cur_frame[chn] = 0;
1358 dev->frame_count[chn] = 0;
1359 for (j = 0; j < SYS_FRAMES; j++) {
1360 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1361 dev->buffer[chn].frame[j].cur_size = 0;
1363 res = videobuf_streamon(&fh->vb_vidq);
1365 s2255_start_acquire(dev, chn);
1366 dev->b_acquire[chn] = 1;
1373 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1375 struct s2255_fh *fh = priv;
1376 struct s2255_dev *dev = fh->dev;
1378 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1379 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1380 printk(KERN_ERR "invalid fh type0\n");
1383 if (i != fh->type) {
1384 printk(KERN_ERR "invalid type i\n");
1387 s2255_stop_acquire(dev, fh->channel);
1388 videobuf_streamoff(&fh->vb_vidq);
1393 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1395 struct s2255_fh *fh = priv;
1396 struct s2255_mode *mode;
1397 struct videobuf_queue *q = &fh->vb_vidq;
1400 mutex_lock(&q->vb_lock);
1401 if (videobuf_queue_is_busy(q)) {
1402 dprintk(1, "queue busy\n");
1407 if (res_locked(fh->dev, fh)) {
1408 dprintk(1, "can't change standard after started\n");
1414 if (*i & V4L2_STD_NTSC) {
1415 dprintk(4, "vidioc_s_std NTSC\n");
1416 mode->format = FORMAT_NTSC;
1417 } else if (*i & V4L2_STD_PAL) {
1418 dprintk(4, "vidioc_s_std PAL\n");
1419 mode->format = FORMAT_PAL;
1424 mutex_unlock(&q->vb_lock);
1428 /* Sensoray 2255 is a multiple channel capture device.
1429 It does not have a "crossbar" of inputs.
1430 We use one V4L device per channel. The user must
1431 be aware that certain combinations are not allowed.
1432 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1433 at once in color(you can do full fps on 4 channels with greyscale.
1435 static int vidioc_enum_input(struct file *file, void *priv,
1436 struct v4l2_input *inp)
1438 struct s2255_fh *fh = priv;
1439 struct s2255_dev *dev = fh->dev;
1442 if (inp->index != 0)
1445 inp->type = V4L2_INPUT_TYPE_CAMERA;
1446 inp->std = S2255_NORMS;
1448 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1450 rc = s2255_cmd_status(dev, fh->channel, &status);
1451 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1453 inp->status = (status & 0x01) ? 0
1454 : V4L2_IN_ST_NO_SIGNAL;
1456 strlcpy(inp->name, "Camera", sizeof(inp->name));
1460 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1465 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1472 /* --- controls ---------------------------------------------- */
1473 static int vidioc_queryctrl(struct file *file, void *priv,
1474 struct v4l2_queryctrl *qc)
1478 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1479 if (qc->id && qc->id == s2255_qctrl[i].id) {
1480 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1484 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1488 static int vidioc_g_ctrl(struct file *file, void *priv,
1489 struct v4l2_control *ctrl)
1493 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1494 if (ctrl->id == s2255_qctrl[i].id) {
1495 ctrl->value = qctl_regs[i];
1498 dprintk(4, "g_ctrl -EINVAL\n");
1503 static int vidioc_s_ctrl(struct file *file, void *priv,
1504 struct v4l2_control *ctrl)
1507 struct s2255_fh *fh = priv;
1508 struct s2255_dev *dev = fh->dev;
1509 struct s2255_mode *mode;
1511 dprintk(4, "vidioc_s_ctrl\n");
1512 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1513 if (ctrl->id == s2255_qctrl[i].id) {
1514 if (ctrl->value < s2255_qctrl[i].minimum ||
1515 ctrl->value > s2255_qctrl[i].maximum)
1518 qctl_regs[i] = ctrl->value;
1519 /* update the mode to the corresponding value */
1521 case V4L2_CID_BRIGHTNESS:
1522 mode->bright = ctrl->value;
1524 case V4L2_CID_CONTRAST:
1525 mode->contrast = ctrl->value;
1528 mode->hue = ctrl->value;
1530 case V4L2_CID_SATURATION:
1531 mode->saturation = ctrl->value;
1535 /* set mode here. Note: stream does not need restarted.
1536 some V4L programs restart stream unnecessarily
1539 s2255_set_mode(dev, 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_dev *dev = fh->dev;
1551 *jc = dev->jc[fh->channel];
1552 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1556 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1557 struct v4l2_jpegcompression *jc)
1559 struct s2255_fh *fh = priv;
1560 struct s2255_dev *dev = fh->dev;
1561 if (jc->quality < 0 || jc->quality > 100)
1563 dev->jc[fh->channel].quality = jc->quality;
1564 dprintk(2, "setting jpeg quality %d\n", 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 struct s2255_dev *dev = fh->dev;
1573 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1575 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1576 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1580 static int vidioc_s_parm(struct file *file, void *priv,
1581 struct v4l2_streamparm *sp)
1583 struct s2255_fh *fh = priv;
1584 struct s2255_dev *dev = fh->dev;
1586 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1589 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1590 dprintk(2, "setting param capture mode %d\n",
1591 sp->parm.capture.capturemode);
1594 static int s2255_open(struct file *file)
1596 struct video_device *vdev = video_devdata(file);
1597 struct s2255_dev *dev = video_drvdata(file);
1598 struct s2255_fh *fh;
1599 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1601 int cur_channel = -1;
1604 dprintk(1, "s2255: open called (dev=%s)\n",
1605 video_device_node_name(vdev));
1609 for (i = 0; i < MAX_CHANNELS; i++) {
1610 if (dev->vdev[i] == vdev) {
1616 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1618 printk(KERN_INFO "disconnecting\n");
1621 kref_get(&dev->kref);
1622 mutex_lock(&dev->open_lock);
1624 dev->users[cur_channel]++;
1625 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1627 switch (atomic_read(&dev->fw_data->fw_state)) {
1628 case S2255_FW_FAILED:
1629 s2255_dev_err(&dev->udev->dev,
1630 "firmware load failed. retrying.\n");
1631 s2255_fwload_start(dev, 1);
1632 wait_event_timeout(dev->fw_data->wait_fw,
1633 ((atomic_read(&dev->fw_data->fw_state)
1634 == S2255_FW_SUCCESS) ||
1635 (atomic_read(&dev->fw_data->fw_state)
1636 == S2255_FW_DISCONNECTING)),
1637 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1639 case S2255_FW_NOTLOADED:
1640 case S2255_FW_LOADED_DSPWAIT:
1641 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1642 driver loaded and then device immediately opened */
1643 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1644 wait_event_timeout(dev->fw_data->wait_fw,
1645 ((atomic_read(&dev->fw_data->fw_state)
1646 == S2255_FW_SUCCESS) ||
1647 (atomic_read(&dev->fw_data->fw_state)
1648 == S2255_FW_DISCONNECTING)),
1649 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1651 case S2255_FW_SUCCESS:
1655 state = atomic_read(&dev->fw_data->fw_state);
1656 if (state != S2255_FW_SUCCESS) {
1659 case S2255_FW_FAILED:
1660 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1663 case S2255_FW_DISCONNECTING:
1664 printk(KERN_INFO "%s: disconnecting\n", __func__);
1667 case S2255_FW_LOADED_DSPWAIT:
1668 case S2255_FW_NOTLOADED:
1669 printk(KERN_INFO "%s: firmware not loaded yet"
1670 "please try again later\n",
1675 printk(KERN_INFO "%s: unknown state\n", __func__);
1679 dev->users[cur_channel]--;
1680 mutex_unlock(&dev->open_lock);
1681 kref_put(&dev->kref, s2255_destroy);
1686 /* allocate + initialize per filehandle data */
1687 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1689 dev->users[cur_channel]--;
1690 mutex_unlock(&dev->open_lock);
1691 kref_put(&dev->kref, s2255_destroy);
1696 file->private_data = fh;
1698 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1699 fh->mode = dev->mode[cur_channel];
1700 fh->fmt = dev->cur_fmt[cur_channel];
1701 /* default 4CIF NTSC */
1702 fh->width = LINE_SZ_4CIFS_NTSC;
1703 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1704 fh->channel = cur_channel;
1706 /* configure channel to default state */
1707 if (!dev->chn_configured[cur_channel]) {
1708 s2255_set_mode(dev, cur_channel, &fh->mode);
1709 dev->chn_configured[cur_channel] = 1;
1713 /* Put all controls at a sane state */
1714 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1715 qctl_regs[i] = s2255_qctrl[i].default_value;
1717 dprintk(1, "s2255drv: open dev=%s type=%s users=%d\n",
1718 video_device_node_name(vdev), v4l2_type_names[type],
1719 dev->users[cur_channel]);
1720 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1721 (unsigned long)fh, (unsigned long)dev,
1722 (unsigned long)&dev->vidq[cur_channel]);
1723 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1724 list_empty(&dev->vidq[cur_channel].active));
1726 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1729 V4L2_FIELD_INTERLACED,
1730 sizeof(struct s2255_buffer), fh);
1732 mutex_unlock(&dev->open_lock);
1738 static unsigned int s2255_poll(struct file *file,
1739 struct poll_table_struct *wait)
1741 struct s2255_fh *fh = file->private_data;
1743 dprintk(100, "%s\n", __func__);
1745 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1748 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1752 static void s2255_destroy(struct kref *kref)
1754 struct s2255_dev *dev = to_s2255_dev(kref);
1757 printk(KERN_ERR "s2255drv: kref problem\n");
1760 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1761 wake_up(&dev->fw_data->wait_fw);
1762 for (i = 0; i < MAX_CHANNELS; i++) {
1763 dev->setmode_ready[i] = 1;
1764 wake_up(&dev->wait_setmode[i]);
1765 dev->vidstatus_ready[i] = 1;
1766 wake_up(&dev->wait_vidstatus[i]);
1768 mutex_lock(&dev->open_lock);
1769 /* reset the DSP so firmware can be reload next time */
1770 s2255_reset_dsppower(dev);
1771 s2255_exit_v4l(dev);
1772 /* board shutdown stops the read pipe if it is running */
1773 s2255_board_shutdown(dev);
1774 /* make sure firmware still not trying to load */
1775 del_timer(&dev->timer); /* only started in .probe and .open */
1777 if (dev->fw_data->fw_urb) {
1778 dprintk(2, "kill fw_urb\n");
1779 usb_kill_urb(dev->fw_data->fw_urb);
1780 usb_free_urb(dev->fw_data->fw_urb);
1781 dev->fw_data->fw_urb = NULL;
1783 if (dev->fw_data->fw)
1784 release_firmware(dev->fw_data->fw);
1785 kfree(dev->fw_data->pfw_data);
1786 kfree(dev->fw_data);
1787 usb_put_dev(dev->udev);
1788 dprintk(1, "%s", __func__);
1790 mutex_unlock(&dev->open_lock);
1794 static int s2255_close(struct file *file)
1796 struct s2255_fh *fh = file->private_data;
1797 struct s2255_dev *dev = fh->dev;
1798 struct video_device *vdev = video_devdata(file);
1803 mutex_lock(&dev->open_lock);
1805 /* turn off stream */
1806 if (res_check(fh)) {
1807 if (dev->b_acquire[fh->channel])
1808 s2255_stop_acquire(dev, fh->channel);
1809 videobuf_streamoff(&fh->vb_vidq);
1813 videobuf_mmap_free(&fh->vb_vidq);
1814 dev->users[fh->channel]--;
1816 mutex_unlock(&dev->open_lock);
1818 kref_put(&dev->kref, s2255_destroy);
1819 dprintk(1, "s2255: close called (dev=%s, users=%d)\n",
1820 video_device_node_name(vdev), dev->users[fh->channel]);
1825 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1827 struct s2255_fh *fh = file->private_data;
1832 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1834 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1836 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1837 (unsigned long)vma->vm_start,
1838 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1843 static const struct v4l2_file_operations s2255_fops_v4l = {
1844 .owner = THIS_MODULE,
1846 .release = s2255_close,
1848 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1849 .mmap = s2255_mmap_v4l,
1852 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1853 .vidioc_querycap = vidioc_querycap,
1854 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1855 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1856 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1857 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1858 .vidioc_reqbufs = vidioc_reqbufs,
1859 .vidioc_querybuf = vidioc_querybuf,
1860 .vidioc_qbuf = vidioc_qbuf,
1861 .vidioc_dqbuf = vidioc_dqbuf,
1862 .vidioc_s_std = vidioc_s_std,
1863 .vidioc_enum_input = vidioc_enum_input,
1864 .vidioc_g_input = vidioc_g_input,
1865 .vidioc_s_input = vidioc_s_input,
1866 .vidioc_queryctrl = vidioc_queryctrl,
1867 .vidioc_g_ctrl = vidioc_g_ctrl,
1868 .vidioc_s_ctrl = vidioc_s_ctrl,
1869 .vidioc_streamon = vidioc_streamon,
1870 .vidioc_streamoff = vidioc_streamoff,
1871 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1872 .vidiocgmbuf = vidioc_cgmbuf,
1874 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1875 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1876 .vidioc_s_parm = vidioc_s_parm,
1877 .vidioc_g_parm = vidioc_g_parm,
1880 static struct video_device template = {
1882 .fops = &s2255_fops_v4l,
1883 .ioctl_ops = &s2255_ioctl_ops,
1884 .release = video_device_release,
1885 .tvnorms = S2255_NORMS,
1886 .current_norm = V4L2_STD_NTSC_M,
1889 static int s2255_probe_v4l(struct s2255_dev *dev)
1893 int cur_nr = video_nr;
1895 /* initialize all video 4 linux */
1896 /* register 4 video devices */
1897 for (i = 0; i < MAX_CHANNELS; i++) {
1898 INIT_LIST_HEAD(&dev->vidq[i].active);
1899 dev->vidq[i].dev = dev;
1900 dev->vidq[i].channel = i;
1901 /* register 4 video devices */
1902 dev->vdev[i] = video_device_alloc();
1903 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1904 dev->vdev[i]->parent = &dev->interface->dev;
1905 video_set_drvdata(dev->vdev[i], dev);
1907 ret = video_register_device(dev->vdev[i],
1911 ret = video_register_device(dev->vdev[i],
1914 video_set_drvdata(dev->vdev[i], dev);
1917 dev_err(&dev->udev->dev,
1918 "failed to register video device!\n");
1922 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1923 S2255_MAJOR_VERSION,
1924 S2255_MINOR_VERSION);
1928 static void s2255_exit_v4l(struct s2255_dev *dev)
1932 for (i = 0; i < MAX_CHANNELS; i++) {
1933 if (video_is_registered(dev->vdev[i])) {
1934 video_unregister_device(dev->vdev[i]);
1935 printk(KERN_INFO "s2255 unregistered\n");
1937 video_device_release(dev->vdev[i]);
1938 printk(KERN_INFO "s2255 released\n");
1943 /* this function moves the usb stream read pipe data
1944 * into the system buffers.
1945 * returns 0 on success, EAGAIN if more data to process( call this
1948 * Received frame structure:
1949 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1950 * bytes 4-7: channel: 0-3
1951 * bytes 8-11: payload size: size of the frame
1952 * bytes 12-payloadsize+12: frame data
1954 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1960 unsigned long copy_size;
1963 struct s2255_framei *frm;
1964 unsigned char *pdata;
1966 dprintk(100, "buffer to user\n");
1968 idx = dev->cur_frame[dev->cc];
1969 frm = &dev->buffer[dev->cc].frame[idx];
1971 if (frm->ulState == S2255_READ_IDLE) {
1976 /* search for marker codes */
1977 pdata = (unsigned char *)pipe_info->transfer_buffer;
1978 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1979 switch (*(s32 *) pdata) {
1980 case S2255_MARKER_FRAME:
1981 pdword = (s32 *)pdata;
1982 dprintk(4, "found frame marker at offset:"
1983 " %d [%x %x]\n", jj, pdata[0],
1985 offset = jj + PREFIX_SIZE;
1988 if (cc >= MAX_CHANNELS) {
1994 dev->cc = G_chnmap[cc];
1995 payload = pdword[3];
1996 if (payload > dev->req_image_size[dev->cc]) {
1997 dev->bad_payload[dev->cc]++;
1998 /* discard the bad frame */
2001 dev->pkt_size[dev->cc] = payload;
2002 dev->jpg_size[dev->cc] = pdword[4];
2004 case S2255_MARKER_RESPONSE:
2005 pdword = (s32 *)pdata;
2006 pdata += DEF_USB_BLOCK;
2007 jj += DEF_USB_BLOCK;
2008 if (pdword[1] >= MAX_CHANNELS)
2010 cc = G_chnmap[pdword[1]];
2011 if (cc >= MAX_CHANNELS)
2013 switch (pdword[2]) {
2014 case S2255_RESPONSE_SETMODE:
2015 /* check if channel valid */
2016 /* set mode ready */
2017 dev->setmode_ready[cc] = 1;
2018 wake_up(&dev->wait_setmode[cc]);
2019 dprintk(5, "setmode ready %d\n", cc);
2021 case S2255_RESPONSE_FW:
2023 dev->chn_ready |= (1 << cc);
2024 if ((dev->chn_ready & 0x0f) != 0x0f)
2026 /* all channels ready */
2027 printk(KERN_INFO "s2255: fw loaded\n");
2028 atomic_set(&dev->fw_data->fw_state,
2030 wake_up(&dev->fw_data->wait_fw);
2032 case S2255_RESPONSE_STATUS:
2033 dev->vidstatus[cc] = pdword[3];
2034 dev->vidstatus_ready[cc] = 1;
2035 wake_up(&dev->wait_vidstatus[cc]);
2036 dprintk(5, "got vidstatus %x chan %d\n",
2040 printk(KERN_INFO "s2255 unknown resp\n");
2054 idx = dev->cur_frame[dev->cc];
2055 frm = &dev->buffer[dev->cc].frame[idx];
2057 /* search done. now find out if should be acquiring on this channel */
2058 if (!dev->b_acquire[dev->cc]) {
2059 /* we found a frame, but this channel is turned off */
2060 frm->ulState = S2255_READ_IDLE;
2064 if (frm->ulState == S2255_READ_IDLE) {
2065 frm->ulState = S2255_READ_FRAME;
2069 /* skip the marker 512 bytes (and offset if out of sync) */
2070 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2073 if (frm->lpvbits == NULL) {
2074 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2075 frm, dev, dev->cc, idx);
2079 pdest = frm->lpvbits + frm->cur_size;
2081 copy_size = (pipe_info->cur_transfer_size - offset);
2083 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2085 /* sanity check on pdest */
2086 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2087 memcpy(pdest, psrc, copy_size);
2089 frm->cur_size += copy_size;
2090 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2092 if (frm->cur_size >= size) {
2095 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2097 dev->last_frame[cc] = dev->cur_frame[cc];
2098 dev->cur_frame[cc]++;
2099 /* end of system frame ring buffer, start at zero */
2100 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2101 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2102 dev->cur_frame[cc] = 0;
2104 if (dev->b_acquire[cc])
2105 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2106 dev->frame_count[cc]++;
2107 frm->ulState = S2255_READ_IDLE;
2111 /* done successfully */
2115 static void s2255_read_video_callback(struct s2255_dev *dev,
2116 struct s2255_pipeinfo *pipe_info)
2119 dprintk(50, "callback read video \n");
2121 if (dev->cc >= MAX_CHANNELS) {
2123 dev_err(&dev->udev->dev, "invalid channel\n");
2126 /* otherwise copy to the system buffers */
2127 res = save_frame(dev, pipe_info);
2129 dprintk(4, "s2255: read callback failed\n");
2131 dprintk(50, "callback read video done\n");
2135 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2136 u16 Index, u16 Value, void *TransferBuffer,
2137 s32 TransferBufferLength, int bOut)
2141 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2143 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2145 Value, Index, TransferBuffer,
2146 TransferBufferLength, HZ * 5);
2148 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2149 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2150 Value, Index, TransferBuffer,
2151 TransferBufferLength, HZ * 5);
2157 * retrieve FX2 firmware version. future use.
2158 * @param dev pointer to device extension
2159 * @return -1 for fail, else returns firmware version as an int(16 bits)
2161 static int s2255_get_fx2fw(struct s2255_dev *dev)
2165 unsigned char transBuffer[64];
2166 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2169 dprintk(2, "get fw error: %x\n", ret);
2170 fw = transBuffer[0] + (transBuffer[1] << 8);
2171 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2176 * Create the system ring buffer to copy frames into from the
2179 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2182 unsigned long reqsize;
2183 dprintk(1, "create sys buffers\n");
2184 if (chn >= MAX_CHANNELS)
2187 dev->buffer[chn].dwFrames = SYS_FRAMES;
2189 /* always allocate maximum size(PAL) for system buffers */
2190 reqsize = SYS_FRAMES_MAXSIZE;
2192 if (reqsize > SYS_FRAMES_MAXSIZE)
2193 reqsize = SYS_FRAMES_MAXSIZE;
2195 for (i = 0; i < SYS_FRAMES; i++) {
2196 /* allocate the frames */
2197 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2199 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2200 &dev->buffer[chn].frame[i], chn, i,
2201 dev->buffer[chn].frame[i].lpvbits);
2202 dev->buffer[chn].frame[i].size = reqsize;
2203 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2204 printk(KERN_INFO "out of memory. using less frames\n");
2205 dev->buffer[chn].dwFrames = i;
2210 /* make sure internal states are set */
2211 for (i = 0; i < SYS_FRAMES; i++) {
2212 dev->buffer[chn].frame[i].ulState = 0;
2213 dev->buffer[chn].frame[i].cur_size = 0;
2216 dev->cur_frame[chn] = 0;
2217 dev->last_frame[chn] = -1;
2221 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2222 unsigned long channel)
2225 dprintk(1, "release sys buffers\n");
2226 for (i = 0; i < SYS_FRAMES; i++) {
2227 if (dev->buffer[channel].frame[i].lpvbits) {
2228 dprintk(1, "vfree %p\n",
2229 dev->buffer[channel].frame[i].lpvbits);
2230 vfree(dev->buffer[channel].frame[i].lpvbits);
2232 dev->buffer[channel].frame[i].lpvbits = NULL;
2237 static int s2255_board_init(struct s2255_dev *dev)
2240 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2242 dprintk(4, "board init: %p", dev);
2244 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2245 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2247 memset(pipe, 0, sizeof(*pipe));
2249 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2250 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2252 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2254 if (pipe->transfer_buffer == NULL) {
2255 dprintk(1, "out of memory!\n");
2261 /* query the firmware */
2262 fw_ver = s2255_get_fx2fw(dev);
2264 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2265 (fw_ver >> 8) & 0xff,
2268 if (fw_ver < S2255_CUR_USB_FWVER)
2269 dev_err(&dev->udev->dev,
2270 "usb firmware not up to date %d.%d\n",
2271 (fw_ver >> 8) & 0xff,
2274 for (j = 0; j < MAX_CHANNELS; j++) {
2275 dev->b_acquire[j] = 0;
2276 dev->mode[j] = mode_def;
2277 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2278 dev->cur_fmt[j] = &formats[0];
2279 dev->mode[j].restart = 1;
2280 dev->req_image_size[j] = get_transfer_size(&mode_def);
2281 dev->frame_count[j] = 0;
2282 /* create the system buffers */
2283 s2255_create_sys_buffers(dev, j);
2285 /* start read pipe */
2286 s2255_start_readpipe(dev);
2288 dprintk(1, "S2255: board initialized\n");
2292 static int s2255_board_shutdown(struct s2255_dev *dev)
2296 dprintk(1, "S2255: board shutdown: %p", dev);
2298 for (i = 0; i < MAX_CHANNELS; i++) {
2299 if (dev->b_acquire[i])
2300 s2255_stop_acquire(dev, i);
2303 s2255_stop_readpipe(dev);
2305 for (i = 0; i < MAX_CHANNELS; i++)
2306 s2255_release_sys_buffers(dev, i);
2308 /* release transfer buffers */
2309 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2310 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2311 kfree(pipe->transfer_buffer);
2316 static void read_pipe_completion(struct urb *purb)
2318 struct s2255_pipeinfo *pipe_info;
2319 struct s2255_dev *dev;
2323 pipe_info = purb->context;
2324 dprintk(100, "read pipe completion %p, status %d\n", purb,
2326 if (pipe_info == NULL) {
2327 dev_err(&purb->dev->dev, "no context!\n");
2331 dev = pipe_info->dev;
2333 dev_err(&purb->dev->dev, "no context!\n");
2336 status = purb->status;
2337 /* if shutting down, do not resubmit, exit immediately */
2338 if (status == -ESHUTDOWN) {
2339 dprintk(2, "read_pipe_completion: err shutdown\n");
2340 pipe_info->err_count++;
2344 if (pipe_info->state == 0) {
2345 dprintk(2, "exiting USB pipe");
2350 s2255_read_video_callback(dev, pipe_info);
2352 pipe_info->err_count++;
2353 dprintk(1, "s2255drv: failed URB %d\n", status);
2356 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2358 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2360 pipe_info->transfer_buffer,
2361 pipe_info->cur_transfer_size,
2362 read_pipe_completion, pipe_info);
2364 if (pipe_info->state != 0) {
2365 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2366 dev_err(&dev->udev->dev, "error submitting urb\n");
2369 dprintk(2, "read pipe complete state 0\n");
2374 static int s2255_start_readpipe(struct s2255_dev *dev)
2379 struct s2255_pipeinfo *pipe_info = dev->pipes;
2380 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2381 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2383 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2384 pipe_info->state = 1;
2385 pipe_info->err_count = 0;
2386 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2387 if (!pipe_info->stream_urb) {
2388 dev_err(&dev->udev->dev,
2389 "ReadStream: Unable to alloc URB\n");
2392 /* transfer buffer allocated in board_init */
2393 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2395 pipe_info->transfer_buffer,
2396 pipe_info->cur_transfer_size,
2397 read_pipe_completion, pipe_info);
2399 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2400 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2402 printk(KERN_ERR "s2255: start read pipe failed\n");
2410 /* starts acquisition process */
2411 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2413 unsigned char *buffer;
2415 unsigned long chn_rev;
2417 if (chn >= MAX_CHANNELS) {
2418 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2422 chn_rev = G_chnmap[chn];
2423 dprintk(1, "S2255: start acquire %lu \n", chn);
2425 buffer = kzalloc(512, GFP_KERNEL);
2426 if (buffer == NULL) {
2427 dev_err(&dev->udev->dev, "out of mem\n");
2431 dev->last_frame[chn] = -1;
2432 dev->bad_payload[chn] = 0;
2433 dev->cur_frame[chn] = 0;
2434 for (j = 0; j < SYS_FRAMES; j++) {
2435 dev->buffer[chn].frame[j].ulState = 0;
2436 dev->buffer[chn].frame[j].cur_size = 0;
2439 /* send the start command */
2440 *(u32 *) buffer = IN_DATA_TOKEN;
2441 *((u32 *) buffer + 1) = (u32) chn_rev;
2442 *((u32 *) buffer + 2) = (u32) CMD_START;
2443 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2445 dev_err(&dev->udev->dev, "CMD_START error\n");
2447 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2452 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2454 unsigned char *buffer;
2456 unsigned long chn_rev;
2458 if (chn >= MAX_CHANNELS) {
2459 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2462 chn_rev = G_chnmap[chn];
2464 buffer = kzalloc(512, GFP_KERNEL);
2465 if (buffer == NULL) {
2466 dev_err(&dev->udev->dev, "out of mem\n");
2470 /* send the stop command */
2471 dprintk(4, "stop acquire %lu\n", chn);
2472 *(u32 *) buffer = IN_DATA_TOKEN;
2473 *((u32 *) buffer + 1) = (u32) chn_rev;
2474 *((u32 *) buffer + 2) = CMD_STOP;
2475 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2478 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2480 dprintk(4, "stop acquire: releasing states \n");
2483 dev->b_acquire[chn] = 0;
2488 static void s2255_stop_readpipe(struct s2255_dev *dev)
2493 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2496 dprintk(4, "stop read pipe\n");
2497 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2498 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2500 if (pipe_info->state == 0)
2502 pipe_info->state = 0;
2506 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2507 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2508 if (pipe_info->stream_urb) {
2510 usb_kill_urb(pipe_info->stream_urb);
2511 usb_free_urb(pipe_info->stream_urb);
2512 pipe_info->stream_urb = NULL;
2515 dprintk(2, "s2255 stop read pipe: %d\n", j);
2519 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2522 s2255_reset_dsppower(dev);
2523 dev->fw_data->fw_size = dev->fw_data->fw->size;
2524 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2525 memcpy(dev->fw_data->pfw_data,
2526 dev->fw_data->fw->data, CHUNK_SIZE);
2527 dev->fw_data->fw_loaded = CHUNK_SIZE;
2528 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2529 usb_sndbulkpipe(dev->udev, 2),
2530 dev->fw_data->pfw_data,
2531 CHUNK_SIZE, s2255_fwchunk_complete,
2533 mod_timer(&dev->timer, jiffies + HZ);
2536 /* standard usb probe function */
2537 static int s2255_probe(struct usb_interface *interface,
2538 const struct usb_device_id *id)
2540 struct s2255_dev *dev = NULL;
2541 struct usb_host_interface *iface_desc;
2542 struct usb_endpoint_descriptor *endpoint;
2544 int retval = -ENOMEM;
2548 dprintk(2, "s2255: probe\n");
2550 /* allocate memory for our device state and initialize it to zero */
2551 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2553 s2255_dev_err(&interface->dev, "out of memory\n");
2557 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2561 mutex_init(&dev->lock);
2562 mutex_init(&dev->open_lock);
2564 /* grab usb_device and save it */
2565 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2566 if (dev->udev == NULL) {
2567 dev_err(&interface->dev, "null usb device\n");
2571 kref_init(&dev->kref);
2572 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2573 dev->udev, interface);
2574 dev->interface = interface;
2575 /* set up the endpoint information */
2576 iface_desc = interface->cur_altsetting;
2577 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2578 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2579 endpoint = &iface_desc->endpoint[i].desc;
2580 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2581 /* we found the bulk in endpoint */
2582 dev->read_endpoint = endpoint->bEndpointAddress;
2586 if (!dev->read_endpoint) {
2587 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2592 usb_set_intfdata(interface, dev);
2594 dprintk(100, "after intfdata %p\n", dev);
2596 init_timer(&dev->timer);
2597 dev->timer.function = s2255_timer;
2598 dev->timer.data = (unsigned long)dev->fw_data;
2600 init_waitqueue_head(&dev->fw_data->wait_fw);
2601 for (i = 0; i < MAX_CHANNELS; i++) {
2602 init_waitqueue_head(&dev->wait_setmode[i]);
2603 init_waitqueue_head(&dev->wait_vidstatus[i]);
2606 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2608 if (!dev->fw_data->fw_urb) {
2609 dev_err(&interface->dev, "out of memory!\n");
2612 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2613 if (!dev->fw_data->pfw_data) {
2614 dev_err(&interface->dev, "out of memory!\n");
2617 /* load the first chunk */
2618 if (request_firmware(&dev->fw_data->fw,
2619 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2620 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2623 /* check the firmware is valid */
2624 fw_size = dev->fw_data->fw->size;
2625 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2627 if (*pdata != S2255_FW_MARKER) {
2628 printk(KERN_INFO "Firmware invalid.\n");
2632 /* make sure firmware is the latest */
2634 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2635 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2636 dev->dsp_fw_ver = *pRel;
2637 if (*pRel < S2255_CUR_DSP_FWVER)
2638 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
2640 /* loads v4l specific */
2641 s2255_probe_v4l(dev);
2642 usb_reset_device(dev->udev);
2643 /* load 2255 board specific */
2644 retval = s2255_board_init(dev);
2648 dprintk(4, "before probe done %p\n", dev);
2649 spin_lock_init(&dev->slock);
2651 s2255_fwload_start(dev, 0);
2652 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2658 /* disconnect routine. when board is removed physically or with rmmod */
2659 static void s2255_disconnect(struct usb_interface *interface)
2661 struct s2255_dev *dev = NULL;
2663 dprintk(1, "s2255: disconnect interface %p\n", interface);
2664 dev = usb_get_intfdata(interface);
2667 * wake up any of the timers to allow open_lock to be
2670 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2671 wake_up(&dev->fw_data->wait_fw);
2672 for (i = 0; i < MAX_CHANNELS; i++) {
2673 dev->setmode_ready[i] = 1;
2674 wake_up(&dev->wait_setmode[i]);
2675 dev->vidstatus_ready[i] = 1;
2676 wake_up(&dev->wait_vidstatus[i]);
2679 mutex_lock(&dev->open_lock);
2680 usb_set_intfdata(interface, NULL);
2681 mutex_unlock(&dev->open_lock);
2684 kref_put(&dev->kref, s2255_destroy);
2685 dprintk(1, "s2255drv: disconnect\n");
2686 dev_info(&interface->dev, "s2255usb now disconnected\n");
2690 static struct usb_driver s2255_driver = {
2691 .name = S2255_DRIVER_NAME,
2692 .probe = s2255_probe,
2693 .disconnect = s2255_disconnect,
2694 .id_table = s2255_table,
2697 static int __init usb_s2255_init(void)
2701 /* register this driver with the USB subsystem */
2702 result = usb_register(&s2255_driver);
2705 pr_err(KBUILD_MODNAME
2706 ": usb_register failed. Error number %d\n", result);
2708 dprintk(2, "s2255_init: done\n");
2712 static void __exit usb_s2255_exit(void)
2714 usb_deregister(&s2255_driver);
2717 module_init(usb_s2255_init);
2718 module_exit(usb_s2255_exit);
2720 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2721 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2722 MODULE_LICENSE("GPL");