2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2008 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/videodev2.h>
49 #include <linux/version.h>
51 #include <media/videobuf-vmalloc.h>
52 #include <media/v4l2-common.h>
53 #include <media/v4l2-ioctl.h>
54 #include <linux/vmalloc.h>
55 #include <linux/usb.h>
57 #define FIRMWARE_FILE_NAME "f2255usb.bin"
61 /* vendor request in */
63 /* vendor request out */
64 #define S2255_VR_OUT 1
66 #define S2255_VR_FW 0x30
67 /* USB endpoint number for configuring the device */
68 #define S2255_CONFIG_EP 2
69 /* maximum time for DSP to start responding after last FW word loaded(ms) */
70 #define S2255_DSP_BOOTTIME 800
71 /* maximum time to wait for firmware to load (ms) */
72 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
73 #define S2255_DEF_BUFS 16
74 #define S2255_SETMODE_TIMEOUT 500
75 #define MAX_CHANNELS 4
76 #define S2255_MARKER_FRAME 0x2255DA4AL
77 #define S2255_MARKER_RESPONSE 0x2255ACACL
78 #define S2255_USB_XFER_SIZE (16 * 1024)
79 #define MAX_CHANNELS 4
80 #define MAX_PIPE_BUFFERS 1
82 /* maximum size is PAL full size plus room for the marker header(s) */
83 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
84 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
85 #define LINE_SZ_4CIFS_NTSC 640
86 #define LINE_SZ_2CIFS_NTSC 640
87 #define LINE_SZ_1CIFS_NTSC 320
88 #define LINE_SZ_4CIFS_PAL 704
89 #define LINE_SZ_2CIFS_PAL 704
90 #define LINE_SZ_1CIFS_PAL 352
91 #define NUM_LINES_4CIFS_NTSC 240
92 #define NUM_LINES_2CIFS_NTSC 240
93 #define NUM_LINES_1CIFS_NTSC 240
94 #define NUM_LINES_4CIFS_PAL 288
95 #define NUM_LINES_2CIFS_PAL 288
96 #define NUM_LINES_1CIFS_PAL 288
97 #define LINE_SZ_DEF 640
98 #define NUM_LINES_DEF 240
101 /* predefined settings */
102 #define FORMAT_NTSC 1
105 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
106 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
107 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
109 #define COLOR_YUVPL 1 /* YUV planar */
110 #define COLOR_YUVPK 2 /* YUV packed */
111 #define COLOR_Y8 4 /* monochrome */
112 #define COLOR_JPG 5 /* JPEG */
113 #define MASK_COLOR 0xff
114 #define MASK_JPG_QUALITY 0xff00
116 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
117 #define FDEC_1 1 /* capture every frame. default */
118 #define FDEC_2 2 /* capture every 2nd frame */
119 #define FDEC_3 3 /* capture every 3rd frame */
120 #define FDEC_5 5 /* capture every 5th frame */
122 /*-------------------------------------------------------
123 * Default mode parameters.
124 *-------------------------------------------------------*/
125 #define DEF_SCALE SCALE_4CIFS
126 #define DEF_COLOR COLOR_YUVPL
127 #define DEF_FDEC FDEC_1
129 #define DEF_CONTRAST 0x5c
130 #define DEF_SATURATION 0x80
133 /* usb config commands */
134 #define IN_DATA_TOKEN 0x2255c0de
135 #define CMD_2255 0xc2255000
136 #define CMD_SET_MODE (CMD_2255 | 0x10)
137 #define CMD_START (CMD_2255 | 0x20)
138 #define CMD_STOP (CMD_2255 | 0x30)
139 #define CMD_STATUS (CMD_2255 | 0x40)
142 u32 format; /* input video format (NTSC, PAL) */
143 u32 scale; /* output video scale */
144 u32 color; /* output video color format */
145 u32 fdec; /* frame decimation */
146 u32 bright; /* brightness */
147 u32 contrast; /* contrast */
148 u32 saturation; /* saturation */
149 u32 hue; /* hue (NTSC only)*/
150 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
151 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
152 u32 restart; /* if DSP requires restart */
156 #define S2255_READ_IDLE 0
157 #define S2255_READ_FRAME 1
159 /* frame structure */
160 struct s2255_framei {
162 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
163 void *lpvbits; /* image data */
164 unsigned long cur_size; /* current data copied to it */
167 /* image buffer structure */
168 struct s2255_bufferi {
169 unsigned long dwFrames; /* number of frames in buffer */
170 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
173 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
174 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
175 DEF_HUE, 0, DEF_USB_BLOCK, 0}
177 struct s2255_dmaqueue {
178 struct list_head active;
179 /* thread for acquisition */
180 struct task_struct *kthread;
182 struct s2255_dev *dev;
186 /* for firmware loading, fw_state */
187 #define S2255_FW_NOTLOADED 0
188 #define S2255_FW_LOADED_DSPWAIT 1
189 #define S2255_FW_SUCCESS 2
190 #define S2255_FW_FAILED 3
191 #define S2255_FW_DISCONNECTING 4
193 #define S2255_FW_MARKER 0x22552f2f
194 /* 2255 read states */
195 #define S2255_READ_IDLE 0
196 #define S2255_READ_FRAME 1
203 wait_queue_head_t wait_fw;
204 const struct firmware *fw;
207 struct s2255_pipeinfo {
208 u32 max_transfer_size;
209 u32 cur_transfer_size;
216 void *dev; /* back pointer to s2255_dev struct*/
223 struct s2255_fmt; /*forward declaration */
227 int users[MAX_CHANNELS];
229 struct mutex open_lock;
230 int resources[MAX_CHANNELS];
231 struct usb_device *udev;
232 struct usb_interface *interface;
235 struct s2255_dmaqueue vidq[MAX_CHANNELS];
236 struct video_device *vdev[MAX_CHANNELS];
237 struct list_head s2255_devlist;
238 struct timer_list timer;
239 struct s2255_fw *fw_data;
242 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
243 struct s2255_bufferi buffer[MAX_CHANNELS];
244 struct s2255_mode mode[MAX_CHANNELS];
245 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
246 int cur_frame[MAX_CHANNELS];
247 int last_frame[MAX_CHANNELS];
248 u32 cc; /* current channel */
249 int b_acquire[MAX_CHANNELS];
250 /* allocated image size */
251 unsigned long req_image_size[MAX_CHANNELS];
252 /* received packet size */
253 unsigned long pkt_size[MAX_CHANNELS];
254 int bad_payload[MAX_CHANNELS];
255 unsigned long frame_count[MAX_CHANNELS];
258 int jpg_size[MAX_CHANNELS];
259 /* if channel configured to default state */
260 int chn_configured[MAX_CHANNELS];
261 wait_queue_head_t wait_setmode[MAX_CHANNELS];
262 int setmode_ready[MAX_CHANNELS];
267 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
275 /* buffer for one video frame */
276 struct s2255_buffer {
277 /* common v4l buffer stuff -- must be first */
278 struct videobuf_buffer vb;
279 const struct s2255_fmt *fmt;
283 struct s2255_dev *dev;
284 const struct s2255_fmt *fmt;
287 struct videobuf_queue vb_vidq;
288 enum v4l2_buf_type type;
290 /* mode below is the desired mode.
291 mode in s2255_dev is the current mode that was last set */
292 struct s2255_mode mode;
293 int resources[MAX_CHANNELS];
296 #define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
297 #define S2255_MAJOR_VERSION 1
298 #define S2255_MINOR_VERSION 13
299 #define S2255_RELEASE 0
300 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
301 S2255_MINOR_VERSION, \
305 #define USB_S2255_VENDOR_ID 0x1943
306 #define USB_S2255_PRODUCT_ID 0x2255
307 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
308 /* frame prefix size (sent once every frame) */
309 #define PREFIX_SIZE 512
311 /* Channels on box are in reverse order */
312 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
314 static LIST_HEAD(s2255_devlist);
317 static int *s2255_debug = &debug;
319 static int s2255_start_readpipe(struct s2255_dev *dev);
320 static void s2255_stop_readpipe(struct s2255_dev *dev);
321 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
322 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
323 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
324 int chn, int jpgsize);
325 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
326 struct s2255_mode *mode);
327 static int s2255_board_shutdown(struct s2255_dev *dev);
328 static void s2255_exit_v4l(struct s2255_dev *dev);
329 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
330 static void s2255_destroy(struct kref *kref);
331 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
332 u16 index, u16 value, void *buf,
333 s32 buf_len, int bOut);
335 #define dprintk(level, fmt, arg...) \
337 if (*s2255_debug >= (level)) { \
338 printk(KERN_DEBUG "s2255: " fmt, ##arg); \
343 static struct usb_driver s2255_driver;
346 /* Declare static vars that will be used as parameters */
347 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
349 /* start video number */
350 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
352 module_param(debug, int, 0644);
353 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
354 module_param(vid_limit, int, 0644);
355 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
356 module_param(video_nr, int, 0644);
357 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
359 /* USB device table */
360 static struct usb_device_id s2255_table[] = {
361 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
362 { } /* Terminating entry */
364 MODULE_DEVICE_TABLE(usb, s2255_table);
367 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
369 /* supported controls */
370 static struct v4l2_queryctrl s2255_qctrl[] = {
372 .id = V4L2_CID_BRIGHTNESS,
373 .type = V4L2_CTRL_TYPE_INTEGER,
374 .name = "Brightness",
381 .id = V4L2_CID_CONTRAST,
382 .type = V4L2_CTRL_TYPE_INTEGER,
387 .default_value = DEF_CONTRAST,
390 .id = V4L2_CID_SATURATION,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 .name = "Saturation",
396 .default_value = DEF_SATURATION,
400 .type = V4L2_CTRL_TYPE_INTEGER,
405 .default_value = DEF_HUE,
410 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
413 static const struct s2255_fmt formats[] = {
415 .name = "4:2:2, planar, YUV422P",
416 .fourcc = V4L2_PIX_FMT_YUV422P,
420 .name = "4:2:2, packed, YUYV",
421 .fourcc = V4L2_PIX_FMT_YUYV,
425 .name = "4:2:2, packed, UYVY",
426 .fourcc = V4L2_PIX_FMT_UYVY,
430 .fourcc = V4L2_PIX_FMT_JPEG,
434 .fourcc = V4L2_PIX_FMT_GREY,
439 static int norm_maxw(struct video_device *vdev)
441 return (vdev->current_norm & V4L2_STD_NTSC) ?
442 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
445 static int norm_maxh(struct video_device *vdev)
447 return (vdev->current_norm & V4L2_STD_NTSC) ?
448 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
451 static int norm_minw(struct video_device *vdev)
453 return (vdev->current_norm & V4L2_STD_NTSC) ?
454 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
457 static int norm_minh(struct video_device *vdev)
459 return (vdev->current_norm & V4L2_STD_NTSC) ?
460 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
465 * TODO: fixme: move YUV reordering to hardware
466 * converts 2255 planar format to yuyv or uyvy
468 static void planar422p_to_yuv_packed(const unsigned char *in,
470 int width, int height,
476 unsigned long size = height * width;
478 pY = (unsigned char *)in;
479 pCr = (unsigned char *)in + height * width;
480 pCb = (unsigned char *)in + height * width + (height * width / 2);
481 for (i = 0; i < size * 2; i += 4) {
482 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
483 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
484 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
485 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
490 void s2255_reset_dsppower(struct s2255_dev *dev)
492 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
494 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
498 /* kickstarts the firmware loading. from probe
500 static void s2255_timer(unsigned long user_data)
502 struct s2255_fw *data = (struct s2255_fw *)user_data;
503 dprintk(100, "s2255 timer\n");
504 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
505 printk(KERN_ERR "s2255: can't submit urb\n");
506 atomic_set(&data->fw_state, S2255_FW_FAILED);
507 /* wake up anything waiting for the firmware */
508 wake_up(&data->wait_fw);
514 /* this loads the firmware asynchronously.
515 Originally this was done synchroously in probe.
516 But it is better to load it asynchronously here than block
517 inside the probe function. Blocking inside probe affects boot time.
518 FW loading is triggered by the timer in the probe function
520 static void s2255_fwchunk_complete(struct urb *urb)
522 struct s2255_fw *data = urb->context;
523 struct usb_device *udev = urb->dev;
525 dprintk(100, "udev %p urb %p", udev, urb);
527 dev_err(&udev->dev, "URB failed with status %d", urb->status);
528 atomic_set(&data->fw_state, S2255_FW_FAILED);
529 /* wake up anything waiting for the firmware */
530 wake_up(&data->wait_fw);
533 if (data->fw_urb == NULL) {
534 dev_err(&udev->dev, "s2255 disconnected\n");
535 atomic_set(&data->fw_state, S2255_FW_FAILED);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data->wait_fw);
540 #define CHUNK_SIZE 512
541 /* all USB transfers must be done with continuous kernel memory.
542 can't allocate more than 128k in current linux kernel, so
543 upload the firmware in chunks
545 if (data->fw_loaded < data->fw_size) {
546 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
547 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
549 if (len < CHUNK_SIZE)
550 memset(data->pfw_data, 0, CHUNK_SIZE);
552 dprintk(100, "completed len %d, loaded %d \n", len,
555 memcpy(data->pfw_data,
556 (char *) data->fw->data + data->fw_loaded, len);
558 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
559 data->pfw_data, CHUNK_SIZE,
560 s2255_fwchunk_complete, data);
561 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
562 dev_err(&udev->dev, "failed submit URB\n");
563 atomic_set(&data->fw_state, S2255_FW_FAILED);
564 /* wake up anything waiting for the firmware */
565 wake_up(&data->wait_fw);
568 data->fw_loaded += len;
570 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
572 dprintk(100, "2255 complete done\n");
577 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
579 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
580 struct s2255_buffer *buf;
581 unsigned long flags = 0;
583 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
584 spin_lock_irqsave(&dev->slock, flags);
586 if (list_empty(&dma_q->active)) {
587 dprintk(1, "No active queue to serve\n");
591 buf = list_entry(dma_q->active.next,
592 struct s2255_buffer, vb.queue);
594 if (!waitqueue_active(&buf->vb.done)) {
599 list_del(&buf->vb.queue);
600 do_gettimeofday(&buf->vb.ts);
601 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
602 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
603 wake_up(&buf->vb.done);
604 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
606 spin_unlock_irqrestore(&dev->slock, flags);
611 static const struct s2255_fmt *format_by_fourcc(int fourcc)
615 for (i = 0; i < ARRAY_SIZE(formats); i++) {
616 if (-1 == formats[i].fourcc)
618 if (formats[i].fourcc == fourcc)
627 /* video buffer vmalloc implementation based partly on VIVI driver which is
628 * Copyright (c) 2006 by
629 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
630 * Ted Walther <ted--a.t--enumera.com>
631 * John Sokol <sokol--a.t--videotechnology.com>
632 * http://v4l.videotechnology.com/
635 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
636 int chn, int jpgsize)
641 char *vbuf = videobuf_to_vmalloc(&buf->vb);
642 unsigned long last_frame;
643 struct s2255_framei *frm;
648 last_frame = dev->last_frame[chn];
649 if (last_frame != -1) {
650 frm = &dev->buffer[chn].frame[last_frame];
652 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
653 switch (buf->fmt->fourcc) {
654 case V4L2_PIX_FMT_YUYV:
655 case V4L2_PIX_FMT_UYVY:
656 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
661 case V4L2_PIX_FMT_GREY:
662 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
664 case V4L2_PIX_FMT_JPEG:
665 buf->vb.size = jpgsize;
666 memcpy(vbuf, tmpbuf, buf->vb.size);
668 case V4L2_PIX_FMT_YUV422P:
670 buf->vb.width * buf->vb.height * 2);
673 printk(KERN_DEBUG "s2255: unknown format?\n");
675 dev->last_frame[chn] = -1;
677 printk(KERN_ERR "s2255: =======no frame\n");
681 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
682 (unsigned long)vbuf, pos);
683 /* tell v4l buffer was filled */
685 buf->vb.field_count = dev->frame_count[chn] * 2;
686 do_gettimeofday(&ts);
688 buf->vb.state = VIDEOBUF_DONE;
692 /* ------------------------------------------------------------------
694 ------------------------------------------------------------------*/
696 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
699 struct s2255_fh *fh = vq->priv_data;
701 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
704 *count = S2255_DEF_BUFS;
706 while (*size * (*count) > vid_limit * 1024 * 1024)
712 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
714 dprintk(4, "%s\n", __func__);
716 videobuf_waiton(&buf->vb, 0, 0);
717 videobuf_vmalloc_free(&buf->vb);
718 buf->vb.state = VIDEOBUF_NEEDS_INIT;
721 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
722 enum v4l2_field field)
724 struct s2255_fh *fh = vq->priv_data;
725 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
727 dprintk(4, "%s, field=%d\n", __func__, field);
731 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
732 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
733 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
734 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
735 dprintk(4, "invalid buffer prepare\n");
739 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
741 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
742 dprintk(4, "invalid buffer prepare\n");
747 buf->vb.width = fh->width;
748 buf->vb.height = fh->height;
749 buf->vb.field = field;
752 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
753 rc = videobuf_iolock(vq, &buf->vb, NULL);
758 buf->vb.state = VIDEOBUF_PREPARED;
761 free_buffer(vq, buf);
765 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
767 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
768 struct s2255_fh *fh = vq->priv_data;
769 struct s2255_dev *dev = fh->dev;
770 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
772 dprintk(1, "%s\n", __func__);
774 buf->vb.state = VIDEOBUF_QUEUED;
775 list_add_tail(&buf->vb.queue, &vidq->active);
778 static void buffer_release(struct videobuf_queue *vq,
779 struct videobuf_buffer *vb)
781 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
782 struct s2255_fh *fh = vq->priv_data;
783 dprintk(4, "%s %d\n", __func__, fh->channel);
784 free_buffer(vq, buf);
787 static struct videobuf_queue_ops s2255_video_qops = {
788 .buf_setup = buffer_setup,
789 .buf_prepare = buffer_prepare,
790 .buf_queue = buffer_queue,
791 .buf_release = buffer_release,
795 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
798 mutex_lock(&dev->lock);
799 if (dev->resources[fh->channel]) {
800 /* no, someone else uses it */
801 mutex_unlock(&dev->lock);
804 /* it's free, grab it */
805 dev->resources[fh->channel] = 1;
806 fh->resources[fh->channel] = 1;
807 dprintk(1, "s2255: res: get\n");
808 mutex_unlock(&dev->lock);
812 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
814 return dev->resources[fh->channel];
817 static int res_check(struct s2255_fh *fh)
819 return fh->resources[fh->channel];
823 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
825 mutex_lock(&dev->lock);
826 dev->resources[fh->channel] = 0;
827 fh->resources[fh->channel] = 0;
828 mutex_unlock(&dev->lock);
829 dprintk(1, "res: put\n");
833 static int vidioc_querycap(struct file *file, void *priv,
834 struct v4l2_capability *cap)
836 struct s2255_fh *fh = file->private_data;
837 struct s2255_dev *dev = fh->dev;
838 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
839 strlcpy(cap->card, "s2255", sizeof(cap->card));
840 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
841 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(4, "try format %d \n", is_ntsc);
900 /* supports 3 sizes. see s2255drv.h */
901 dprintk(50, "width test %d, height %d\n",
902 f->fmt.pix.width, f->fmt.pix.height);
905 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
906 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
908 field = V4L2_FIELD_SEQ_TB;
909 } else if (!((field == V4L2_FIELD_INTERLACED) ||
910 (field == V4L2_FIELD_SEQ_TB) ||
911 (field == V4L2_FIELD_INTERLACED_TB))) {
912 dprintk(1, "unsupported field setting\n");
916 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
918 field = V4L2_FIELD_TOP;
919 } else if (!((field == V4L2_FIELD_TOP) ||
920 (field == V4L2_FIELD_BOTTOM))) {
921 dprintk(1, "unsupported field setting\n");
926 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
930 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
933 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
936 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
937 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
939 field = V4L2_FIELD_SEQ_TB;
940 } else if (!((field == V4L2_FIELD_INTERLACED) ||
941 (field == V4L2_FIELD_SEQ_TB) ||
942 (field == V4L2_FIELD_INTERLACED_TB))) {
943 dprintk(1, "unsupported field setting\n");
947 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
949 field = V4L2_FIELD_TOP;
950 } else if (!((field == V4L2_FIELD_TOP) ||
951 (field == V4L2_FIELD_BOTTOM))) {
952 dprintk(1, "unsupported field setting\n");
956 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
957 dprintk(50, "pal 704\n");
958 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
959 field = V4L2_FIELD_SEQ_TB;
960 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
961 dprintk(50, "pal 352A\n");
962 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
963 field = V4L2_FIELD_TOP;
964 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
965 dprintk(50, "pal 352B\n");
966 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
967 field = V4L2_FIELD_TOP;
969 dprintk(50, "pal 352C\n");
970 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
971 field = V4L2_FIELD_TOP;
975 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
976 f->fmt.pix.height, f->fmt.pix.field);
977 f->fmt.pix.field = field;
978 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
979 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
983 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
984 struct v4l2_format *f)
986 struct s2255_fh *fh = priv;
987 const struct s2255_fmt *fmt;
988 struct videobuf_queue *q = &fh->vb_vidq;
992 ret = vidioc_try_fmt_vid_cap(file, fh, f);
997 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1002 mutex_lock(&q->vb_lock);
1004 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1005 dprintk(1, "queue busy\n");
1010 if (res_locked(fh->dev, fh)) {
1011 dprintk(1, "can't change format after started\n");
1017 fh->width = f->fmt.pix.width;
1018 fh->height = f->fmt.pix.height;
1019 fh->vb_vidq.field = f->fmt.pix.field;
1021 norm = norm_minw(fh->dev->vdev[fh->channel]);
1022 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1023 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
1024 fh->mode.scale = SCALE_4CIFS;
1026 fh->mode.scale = SCALE_2CIFS;
1029 fh->mode.scale = SCALE_1CIFS;
1033 switch (fh->fmt->fourcc) {
1034 case V4L2_PIX_FMT_GREY:
1035 fh->mode.color = COLOR_Y8;
1037 case V4L2_PIX_FMT_JPEG:
1038 fh->mode.color = COLOR_JPG | (50 << 8);
1040 case V4L2_PIX_FMT_YUV422P:
1041 fh->mode.color = COLOR_YUVPL;
1043 case V4L2_PIX_FMT_YUYV:
1044 case V4L2_PIX_FMT_UYVY:
1046 fh->mode.color = COLOR_YUVPK;
1051 mutex_unlock(&q->vb_lock);
1055 static int vidioc_reqbufs(struct file *file, void *priv,
1056 struct v4l2_requestbuffers *p)
1059 struct s2255_fh *fh = priv;
1060 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1064 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1067 struct s2255_fh *fh = priv;
1068 rc = videobuf_querybuf(&fh->vb_vidq, p);
1072 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1075 struct s2255_fh *fh = priv;
1076 rc = videobuf_qbuf(&fh->vb_vidq, p);
1080 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1083 struct s2255_fh *fh = priv;
1084 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1088 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1089 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1091 struct s2255_fh *fh = priv;
1093 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1097 /* write to the configuration pipe, synchronously */
1098 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1105 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1106 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1111 static u32 get_transfer_size(struct s2255_mode *mode)
1113 int linesPerFrame = LINE_SZ_DEF;
1114 int pixelsPerLine = NUM_LINES_DEF;
1117 unsigned int mask_mult;
1122 if (mode->format == FORMAT_NTSC) {
1123 switch (mode->scale) {
1125 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1126 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1129 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1130 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1133 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1134 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1139 } else if (mode->format == FORMAT_PAL) {
1140 switch (mode->scale) {
1142 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1143 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1146 linesPerFrame = NUM_LINES_2CIFS_PAL;
1147 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1150 linesPerFrame = NUM_LINES_1CIFS_PAL;
1151 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1157 outImageSize = linesPerFrame * pixelsPerLine;
1158 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1159 /* 2 bytes/pixel if not monochrome */
1163 /* total bytes to send including prefix and 4K padding;
1164 must be a multiple of USB_READ_SIZE */
1165 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1166 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1167 /* if size not a multiple of USB_READ_SIZE */
1168 if (usbInSize & ~mask_mult)
1169 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1173 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1175 struct device *dev = &sdev->udev->dev;
1176 dev_info(dev, "------------------------------------------------\n");
1177 dev_info(dev, "verify mode\n");
1178 dev_info(dev, "format: %d\n", mode->format);
1179 dev_info(dev, "scale: %d\n", mode->scale);
1180 dev_info(dev, "fdec: %d\n", mode->fdec);
1181 dev_info(dev, "color: %d\n", mode->color);
1182 dev_info(dev, "bright: 0x%x\n", mode->bright);
1183 dev_info(dev, "restart: 0x%x\n", mode->restart);
1184 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1185 dev_info(dev, "single: 0x%x\n", mode->single);
1186 dev_info(dev, "------------------------------------------------\n");
1190 * set mode is the function which controls the DSP.
1191 * the restart parameter in struct s2255_mode should be set whenever
1192 * the image size could change via color format, video system or image
1194 * When the restart parameter is set, we sleep for ONE frame to allow the
1195 * DSP time to get the new frame
1197 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1198 struct s2255_mode *mode)
1202 unsigned long chn_rev;
1204 mutex_lock(&dev->lock);
1205 chn_rev = G_chnmap[chn];
1206 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1207 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1208 dev->mode[chn].scale);
1209 dprintk(2, "mode contrast %x\n", mode->contrast);
1212 dev->mode[chn] = *mode;
1213 dev->req_image_size[chn] = get_transfer_size(mode);
1214 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1216 buffer = kzalloc(512, GFP_KERNEL);
1217 if (buffer == NULL) {
1218 dev_err(&dev->udev->dev, "out of mem\n");
1219 mutex_unlock(&dev->lock);
1224 buffer[0] = IN_DATA_TOKEN;
1225 buffer[1] = (u32) chn_rev;
1226 buffer[2] = CMD_SET_MODE;
1227 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1228 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1230 dump_verify_mode(dev, mode);
1232 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1234 /* wait at least 3 frames before continuing */
1235 if (mode->restart) {
1236 dev->setmode_ready[chn] = 0;
1237 wait_event_timeout(dev->wait_setmode[chn],
1238 (dev->setmode_ready[chn] != 0),
1239 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1240 if (dev->setmode_ready[chn] != 1) {
1241 printk(KERN_DEBUG "s2255: no set mode response\n");
1246 /* clear the restart flag */
1247 dev->mode[chn].restart = 0;
1248 mutex_unlock(&dev->lock);
1252 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1255 struct s2255_fh *fh = priv;
1256 struct s2255_dev *dev = fh->dev;
1257 struct s2255_mode *new_mode;
1258 struct s2255_mode *old_mode;
1261 dprintk(4, "%s\n", __func__);
1262 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1263 dev_err(&dev->udev->dev, "invalid fh type0\n");
1266 if (i != fh->type) {
1267 dev_err(&dev->udev->dev, "invalid fh type1\n");
1271 if (!res_get(dev, fh)) {
1272 dev_err(&dev->udev->dev, "s2255: stream busy\n");
1276 /* send a set mode command everytime with restart.
1277 in case we switch resolutions or other parameters */
1279 new_mode = &fh->mode;
1280 old_mode = &fh->dev->mode[chn];
1282 if (new_mode->color != old_mode->color)
1283 new_mode->restart = 1;
1284 else if (new_mode->scale != old_mode->scale)
1285 new_mode->restart = 1;
1286 else if (new_mode->format != old_mode->format)
1287 new_mode->restart = 1;
1289 s2255_set_mode(dev, chn, new_mode);
1290 new_mode->restart = 0;
1291 *old_mode = *new_mode;
1292 dev->cur_fmt[chn] = fh->fmt;
1293 dprintk(1, "%s[%d]\n", __func__, chn);
1294 dev->last_frame[chn] = -1;
1295 dev->bad_payload[chn] = 0;
1296 dev->cur_frame[chn] = 0;
1297 dev->frame_count[chn] = 0;
1298 for (j = 0; j < SYS_FRAMES; j++) {
1299 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1300 dev->buffer[chn].frame[j].cur_size = 0;
1302 res = videobuf_streamon(&fh->vb_vidq);
1304 s2255_start_acquire(dev, chn);
1305 dev->b_acquire[chn] = 1;
1312 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1315 struct s2255_fh *fh = priv;
1316 struct s2255_dev *dev = fh->dev;
1318 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1319 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1320 printk(KERN_ERR "invalid fh type0\n");
1323 if (i != fh->type) {
1324 printk(KERN_ERR "invalid type i\n");
1327 s2255_stop_acquire(dev, fh->channel);
1328 res = videobuf_streamoff(&fh->vb_vidq);
1335 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1337 struct s2255_fh *fh = priv;
1338 struct s2255_mode *mode;
1339 struct videobuf_queue *q = &fh->vb_vidq;
1342 mutex_lock(&q->vb_lock);
1343 if (videobuf_queue_is_busy(q)) {
1344 dprintk(1, "queue busy\n");
1349 if (res_locked(fh->dev, fh)) {
1350 dprintk(1, "can't change standard after started\n");
1356 if (*i & V4L2_STD_NTSC) {
1357 dprintk(4, "vidioc_s_std NTSC\n");
1358 mode->format = FORMAT_NTSC;
1359 } else if (*i & V4L2_STD_PAL) {
1360 dprintk(4, "vidioc_s_std PAL\n");
1361 mode->format = FORMAT_PAL;
1366 mutex_unlock(&q->vb_lock);
1370 /* Sensoray 2255 is a multiple channel capture device.
1371 It does not have a "crossbar" of inputs.
1372 We use one V4L device per channel. The user must
1373 be aware that certain combinations are not allowed.
1374 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1375 at once in color(you can do full fps on 4 channels with greyscale.
1377 static int vidioc_enum_input(struct file *file, void *priv,
1378 struct v4l2_input *inp)
1380 if (inp->index != 0)
1383 inp->type = V4L2_INPUT_TYPE_CAMERA;
1384 inp->std = S2255_NORMS;
1385 strlcpy(inp->name, "Camera", sizeof(inp->name));
1389 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1394 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1401 /* --- controls ---------------------------------------------- */
1402 static int vidioc_queryctrl(struct file *file, void *priv,
1403 struct v4l2_queryctrl *qc)
1407 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1408 if (qc->id && qc->id == s2255_qctrl[i].id) {
1409 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1413 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1417 static int vidioc_g_ctrl(struct file *file, void *priv,
1418 struct v4l2_control *ctrl)
1422 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1423 if (ctrl->id == s2255_qctrl[i].id) {
1424 ctrl->value = qctl_regs[i];
1427 dprintk(4, "g_ctrl -EINVAL\n");
1432 static int vidioc_s_ctrl(struct file *file, void *priv,
1433 struct v4l2_control *ctrl)
1436 struct s2255_fh *fh = priv;
1437 struct s2255_dev *dev = fh->dev;
1438 struct s2255_mode *mode;
1440 dprintk(4, "vidioc_s_ctrl\n");
1441 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1442 if (ctrl->id == s2255_qctrl[i].id) {
1443 if (ctrl->value < s2255_qctrl[i].minimum ||
1444 ctrl->value > s2255_qctrl[i].maximum)
1447 qctl_regs[i] = ctrl->value;
1448 /* update the mode to the corresponding value */
1450 case V4L2_CID_BRIGHTNESS:
1451 mode->bright = ctrl->value;
1453 case V4L2_CID_CONTRAST:
1454 mode->contrast = ctrl->value;
1457 mode->hue = ctrl->value;
1459 case V4L2_CID_SATURATION:
1460 mode->saturation = ctrl->value;
1464 /* set mode here. Note: stream does not need restarted.
1465 some V4L programs restart stream unnecessarily
1468 s2255_set_mode(dev, fh->channel, mode);
1475 static int s2255_open(struct inode *inode, struct file *file)
1477 int minor = iminor(inode);
1478 struct s2255_dev *h, *dev = NULL;
1479 struct s2255_fh *fh;
1480 struct list_head *list;
1481 enum v4l2_buf_type type = 0;
1483 int cur_channel = -1;
1485 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1488 list_for_each(list, &s2255_devlist) {
1489 h = list_entry(list, struct s2255_dev, s2255_devlist);
1490 for (i = 0; i < MAX_CHANNELS; i++) {
1491 if (h->vdev[i]->minor == minor) {
1494 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1499 if ((NULL == dev) || (cur_channel == -1)) {
1501 printk(KERN_INFO "s2255: openv4l no dev\n");
1505 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1507 printk(KERN_INFO "disconnecting\n");
1510 kref_get(&dev->kref);
1511 mutex_lock(&dev->open_lock);
1513 dev->users[cur_channel]++;
1514 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1516 switch (atomic_read(&dev->fw_data->fw_state)) {
1517 case S2255_FW_FAILED:
1518 err("2255 firmware load failed. retrying.\n");
1519 s2255_fwload_start(dev, 1);
1520 wait_event_timeout(dev->fw_data->wait_fw,
1521 ((atomic_read(&dev->fw_data->fw_state)
1522 == S2255_FW_SUCCESS) ||
1523 (atomic_read(&dev->fw_data->fw_state)
1524 == S2255_FW_DISCONNECTING)),
1525 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1527 case S2255_FW_NOTLOADED:
1528 case S2255_FW_LOADED_DSPWAIT:
1529 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1530 driver loaded and then device immediately opened */
1531 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1532 wait_event_timeout(dev->fw_data->wait_fw,
1533 ((atomic_read(&dev->fw_data->fw_state)
1534 == S2255_FW_SUCCESS) ||
1535 (atomic_read(&dev->fw_data->fw_state)
1536 == S2255_FW_DISCONNECTING)),
1537 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1539 case S2255_FW_SUCCESS:
1543 state = atomic_read(&dev->fw_data->fw_state);
1544 if (state != S2255_FW_SUCCESS) {
1547 case S2255_FW_FAILED:
1548 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1551 case S2255_FW_DISCONNECTING:
1552 printk(KERN_INFO "%s: disconnecting\n", __func__);
1555 case S2255_FW_LOADED_DSPWAIT:
1556 case S2255_FW_NOTLOADED:
1557 printk(KERN_INFO "%s: firmware not loaded yet"
1558 "please try again later\n",
1563 printk(KERN_INFO "%s: unknown state\n", __func__);
1567 dev->users[cur_channel]--;
1568 mutex_unlock(&dev->open_lock);
1569 kref_put(&dev->kref, s2255_destroy);
1574 /* allocate + initialize per filehandle data */
1575 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1577 dev->users[cur_channel]--;
1578 mutex_unlock(&dev->open_lock);
1579 kref_put(&dev->kref, s2255_destroy);
1584 file->private_data = fh;
1586 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1587 fh->mode = dev->mode[cur_channel];
1588 fh->fmt = dev->cur_fmt[cur_channel];
1589 /* default 4CIF NTSC */
1590 fh->width = LINE_SZ_4CIFS_NTSC;
1591 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1592 fh->channel = cur_channel;
1594 /* configure channel to default state */
1595 if (!dev->chn_configured[cur_channel]) {
1596 s2255_set_mode(dev, cur_channel, &fh->mode);
1597 dev->chn_configured[cur_channel] = 1;
1601 /* Put all controls at a sane state */
1602 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1603 qctl_regs[i] = s2255_qctrl[i].default_value;
1605 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1606 minor, v4l2_type_names[type], dev->users[cur_channel]);
1607 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1608 (unsigned long)fh, (unsigned long)dev,
1609 (unsigned long)&dev->vidq[cur_channel]);
1610 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1611 list_empty(&dev->vidq[cur_channel].active));
1613 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1616 V4L2_FIELD_INTERLACED,
1617 sizeof(struct s2255_buffer), fh);
1619 mutex_unlock(&dev->open_lock);
1625 static unsigned int s2255_poll(struct file *file,
1626 struct poll_table_struct *wait)
1628 struct s2255_fh *fh = file->private_data;
1630 dprintk(100, "%s\n", __func__);
1632 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1635 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1639 static void s2255_destroy(struct kref *kref)
1641 struct s2255_dev *dev = to_s2255_dev(kref);
1642 struct list_head *list;
1645 printk(KERN_ERR "s2255drv: kref problem\n");
1648 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1649 wake_up(&dev->fw_data->wait_fw);
1650 for (i = 0; i < MAX_CHANNELS; i++) {
1651 dev->setmode_ready[i] = 1;
1652 wake_up(&dev->wait_setmode[i]);
1654 mutex_lock(&dev->open_lock);
1655 /* reset the DSP so firmware can be reload next time */
1656 s2255_reset_dsppower(dev);
1657 s2255_exit_v4l(dev);
1658 /* board shutdown stops the read pipe if it is running */
1659 s2255_board_shutdown(dev);
1660 /* make sure firmware still not trying to load */
1661 del_timer(&dev->timer); /* only started in .probe and .open */
1663 if (dev->fw_data->fw_urb) {
1664 dprintk(2, "kill fw_urb\n");
1665 usb_kill_urb(dev->fw_data->fw_urb);
1666 usb_free_urb(dev->fw_data->fw_urb);
1667 dev->fw_data->fw_urb = NULL;
1669 if (dev->fw_data->fw)
1670 release_firmware(dev->fw_data->fw);
1671 kfree(dev->fw_data->pfw_data);
1672 kfree(dev->fw_data);
1673 usb_put_dev(dev->udev);
1674 dprintk(1, "%s", __func__);
1677 while (!list_empty(&s2255_devlist)) {
1678 list = s2255_devlist.next;
1681 mutex_unlock(&dev->open_lock);
1684 static int s2255_close(struct inode *inode, struct file *file)
1686 struct s2255_fh *fh = file->private_data;
1687 struct s2255_dev *dev = fh->dev;
1688 int minor = iminor(inode);
1692 mutex_lock(&dev->open_lock);
1694 /* turn off stream */
1695 if (res_check(fh)) {
1696 if (dev->b_acquire[fh->channel])
1697 s2255_stop_acquire(dev, fh->channel);
1698 videobuf_streamoff(&fh->vb_vidq);
1702 videobuf_mmap_free(&fh->vb_vidq);
1703 dev->users[fh->channel]--;
1705 mutex_unlock(&dev->open_lock);
1707 kref_put(&dev->kref, s2255_destroy);
1708 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1709 minor, dev->users[fh->channel]);
1714 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1716 struct s2255_fh *fh = file->private_data;
1721 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1723 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1725 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1726 (unsigned long)vma->vm_start,
1727 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1732 static const struct file_operations s2255_fops_v4l = {
1733 .owner = THIS_MODULE,
1735 .release = s2255_close,
1737 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1738 .compat_ioctl = v4l_compat_ioctl32,
1739 .mmap = s2255_mmap_v4l,
1740 .llseek = no_llseek,
1743 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1744 .vidioc_querycap = vidioc_querycap,
1745 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1746 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1747 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1748 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1749 .vidioc_reqbufs = vidioc_reqbufs,
1750 .vidioc_querybuf = vidioc_querybuf,
1751 .vidioc_qbuf = vidioc_qbuf,
1752 .vidioc_dqbuf = vidioc_dqbuf,
1753 .vidioc_s_std = vidioc_s_std,
1754 .vidioc_enum_input = vidioc_enum_input,
1755 .vidioc_g_input = vidioc_g_input,
1756 .vidioc_s_input = vidioc_s_input,
1757 .vidioc_queryctrl = vidioc_queryctrl,
1758 .vidioc_g_ctrl = vidioc_g_ctrl,
1759 .vidioc_s_ctrl = vidioc_s_ctrl,
1760 .vidioc_streamon = vidioc_streamon,
1761 .vidioc_streamoff = vidioc_streamoff,
1762 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1763 .vidiocgmbuf = vidioc_cgmbuf,
1767 static struct video_device template = {
1769 .fops = &s2255_fops_v4l,
1770 .ioctl_ops = &s2255_ioctl_ops,
1772 .release = video_device_release,
1773 .tvnorms = S2255_NORMS,
1774 .current_norm = V4L2_STD_NTSC_M,
1777 static int s2255_probe_v4l(struct s2255_dev *dev)
1781 int cur_nr = video_nr;
1783 /* initialize all video 4 linux */
1784 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1785 /* register 4 video devices */
1786 for (i = 0; i < MAX_CHANNELS; i++) {
1787 INIT_LIST_HEAD(&dev->vidq[i].active);
1788 dev->vidq[i].dev = dev;
1789 dev->vidq[i].channel = i;
1790 dev->vidq[i].kthread = NULL;
1791 /* register 4 video devices */
1792 dev->vdev[i] = video_device_alloc();
1793 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1794 dev->vdev[i]->parent = &dev->interface->dev;
1796 ret = video_register_device(dev->vdev[i],
1800 ret = video_register_device(dev->vdev[i],
1803 video_set_drvdata(dev->vdev[i], dev);
1806 dev_err(&dev->udev->dev,
1807 "failed to register video device!\n");
1811 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1815 static void s2255_exit_v4l(struct s2255_dev *dev)
1819 for (i = 0; i < MAX_CHANNELS; i++) {
1820 if (-1 != dev->vdev[i]->minor) {
1821 video_unregister_device(dev->vdev[i]);
1822 printk(KERN_INFO "s2255 unregistered\n");
1824 video_device_release(dev->vdev[i]);
1825 printk(KERN_INFO "s2255 released\n");
1830 /* this function moves the usb stream read pipe data
1831 * into the system buffers.
1832 * returns 0 on success, EAGAIN if more data to process( call this
1835 * Received frame structure:
1836 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1837 * bytes 4-7: channel: 0-3
1838 * bytes 8-11: payload size: size of the frame
1839 * bytes 12-payloadsize+12: frame data
1841 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1847 unsigned long copy_size;
1850 struct s2255_framei *frm;
1851 unsigned char *pdata;
1853 dprintk(100, "buffer to user\n");
1855 idx = dev->cur_frame[dev->cc];
1856 frm = &dev->buffer[dev->cc].frame[idx];
1858 if (frm->ulState == S2255_READ_IDLE) {
1863 /* search for marker codes */
1864 pdata = (unsigned char *)pipe_info->transfer_buffer;
1865 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1866 switch (*(s32 *) pdata) {
1867 case S2255_MARKER_FRAME:
1868 pdword = (s32 *)pdata;
1869 dprintk(4, "found frame marker at offset:"
1870 " %d [%x %x]\n", jj, pdata[0],
1872 offset = jj + PREFIX_SIZE;
1875 if (cc >= MAX_CHANNELS) {
1881 dev->cc = G_chnmap[cc];
1882 payload = pdword[3];
1883 if (payload > dev->req_image_size[dev->cc]) {
1884 dev->bad_payload[dev->cc]++;
1885 /* discard the bad frame */
1888 dev->pkt_size[dev->cc] = payload;
1889 dev->jpg_size[dev->cc] = pdword[4];
1891 case S2255_MARKER_RESPONSE:
1892 pdword = (s32 *)pdata;
1893 pdata += DEF_USB_BLOCK;
1894 jj += DEF_USB_BLOCK;
1895 if (pdword[1] >= MAX_CHANNELS)
1897 cc = G_chnmap[pdword[1]];
1898 if (!(cc >= 0 && cc < MAX_CHANNELS))
1900 switch (pdword[2]) {
1902 /* check if channel valid */
1903 /* set mode ready */
1904 dev->setmode_ready[cc] = 1;
1905 wake_up(&dev->wait_setmode[cc]);
1906 dprintk(5, "setmode ready %d\n", cc);
1910 dev->chn_ready |= (1 << cc);
1911 if ((dev->chn_ready & 0x0f) != 0x0f)
1913 /* all channels ready */
1914 printk(KERN_INFO "s2255: fw loaded\n");
1915 atomic_set(&dev->fw_data->fw_state,
1917 wake_up(&dev->fw_data->wait_fw);
1920 printk(KERN_INFO "s2255 unknwn resp\n");
1934 idx = dev->cur_frame[dev->cc];
1935 frm = &dev->buffer[dev->cc].frame[idx];
1937 /* search done. now find out if should be acquiring on this channel */
1938 if (!dev->b_acquire[dev->cc]) {
1939 /* we found a frame, but this channel is turned off */
1940 frm->ulState = S2255_READ_IDLE;
1944 if (frm->ulState == S2255_READ_IDLE) {
1945 frm->ulState = S2255_READ_FRAME;
1949 /* skip the marker 512 bytes (and offset if out of sync) */
1950 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1953 if (frm->lpvbits == NULL) {
1954 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1955 frm, dev, dev->cc, idx);
1959 pdest = frm->lpvbits + frm->cur_size;
1961 copy_size = (pipe_info->cur_transfer_size - offset);
1963 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
1965 /* sanity check on pdest */
1966 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
1967 memcpy(pdest, psrc, copy_size);
1969 frm->cur_size += copy_size;
1970 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
1972 if (frm->cur_size >= size) {
1975 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
1977 dev->last_frame[cc] = dev->cur_frame[cc];
1978 dev->cur_frame[cc]++;
1979 /* end of system frame ring buffer, start at zero */
1980 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
1981 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
1982 dev->cur_frame[cc] = 0;
1984 if (dev->b_acquire[cc])
1985 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
1986 dev->frame_count[cc]++;
1987 frm->ulState = S2255_READ_IDLE;
1991 /* done successfully */
1995 static void s2255_read_video_callback(struct s2255_dev *dev,
1996 struct s2255_pipeinfo *pipe_info)
1999 dprintk(50, "callback read video \n");
2001 if (dev->cc >= MAX_CHANNELS) {
2003 dev_err(&dev->udev->dev, "invalid channel\n");
2006 /* otherwise copy to the system buffers */
2007 res = save_frame(dev, pipe_info);
2009 dprintk(4, "s2255: read callback failed\n");
2011 dprintk(50, "callback read video done\n");
2015 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2016 u16 Index, u16 Value, void *TransferBuffer,
2017 s32 TransferBufferLength, int bOut)
2021 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2023 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2025 Value, Index, TransferBuffer,
2026 TransferBufferLength, HZ * 5);
2028 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2029 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2030 Value, Index, TransferBuffer,
2031 TransferBufferLength, HZ * 5);
2037 * retrieve FX2 firmware version. future use.
2038 * @param dev pointer to device extension
2039 * @return -1 for fail, else returns firmware version as an int(16 bits)
2041 static int s2255_get_fx2fw(struct s2255_dev *dev)
2045 unsigned char transBuffer[64];
2046 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2049 dprintk(2, "get fw error: %x\n", ret);
2050 fw = transBuffer[0] + (transBuffer[1] << 8);
2051 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2056 * Create the system ring buffer to copy frames into from the
2059 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2062 unsigned long reqsize;
2063 dprintk(1, "create sys buffers\n");
2064 if (chn >= MAX_CHANNELS)
2067 dev->buffer[chn].dwFrames = SYS_FRAMES;
2069 /* always allocate maximum size(PAL) for system buffers */
2070 reqsize = SYS_FRAMES_MAXSIZE;
2072 if (reqsize > SYS_FRAMES_MAXSIZE)
2073 reqsize = SYS_FRAMES_MAXSIZE;
2075 for (i = 0; i < SYS_FRAMES; i++) {
2076 /* allocate the frames */
2077 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2079 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2080 &dev->buffer[chn].frame[i], chn, i,
2081 dev->buffer[chn].frame[i].lpvbits);
2082 dev->buffer[chn].frame[i].size = reqsize;
2083 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2084 printk(KERN_INFO "out of memory. using less frames\n");
2085 dev->buffer[chn].dwFrames = i;
2090 /* make sure internal states are set */
2091 for (i = 0; i < SYS_FRAMES; i++) {
2092 dev->buffer[chn].frame[i].ulState = 0;
2093 dev->buffer[chn].frame[i].cur_size = 0;
2096 dev->cur_frame[chn] = 0;
2097 dev->last_frame[chn] = -1;
2101 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2102 unsigned long channel)
2105 dprintk(1, "release sys buffers\n");
2106 for (i = 0; i < SYS_FRAMES; i++) {
2107 if (dev->buffer[channel].frame[i].lpvbits) {
2108 dprintk(1, "vfree %p\n",
2109 dev->buffer[channel].frame[i].lpvbits);
2110 vfree(dev->buffer[channel].frame[i].lpvbits);
2112 dev->buffer[channel].frame[i].lpvbits = NULL;
2117 static int s2255_board_init(struct s2255_dev *dev)
2120 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2122 dprintk(4, "board init: %p", dev);
2124 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2125 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2127 memset(pipe, 0, sizeof(*pipe));
2129 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2130 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2132 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2134 if (pipe->transfer_buffer == NULL) {
2135 dprintk(1, "out of memory!\n");
2141 /* query the firmware */
2142 fw_ver = s2255_get_fx2fw(dev);
2144 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2145 if (fw_ver < CUR_USB_FWVER)
2146 err("usb firmware not up to date %d\n", fw_ver);
2148 for (j = 0; j < MAX_CHANNELS; j++) {
2149 dev->b_acquire[j] = 0;
2150 dev->mode[j] = mode_def;
2151 dev->cur_fmt[j] = &formats[0];
2152 dev->mode[j].restart = 1;
2153 dev->req_image_size[j] = get_transfer_size(&mode_def);
2154 dev->frame_count[j] = 0;
2155 /* create the system buffers */
2156 s2255_create_sys_buffers(dev, j);
2158 /* start read pipe */
2159 s2255_start_readpipe(dev);
2161 dprintk(1, "S2255: board initialized\n");
2165 static int s2255_board_shutdown(struct s2255_dev *dev)
2169 dprintk(1, "S2255: board shutdown: %p", dev);
2171 for (i = 0; i < MAX_CHANNELS; i++) {
2172 if (dev->b_acquire[i])
2173 s2255_stop_acquire(dev, i);
2176 s2255_stop_readpipe(dev);
2178 for (i = 0; i < MAX_CHANNELS; i++)
2179 s2255_release_sys_buffers(dev, i);
2181 /* release transfer buffers */
2182 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2183 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2184 kfree(pipe->transfer_buffer);
2189 static void read_pipe_completion(struct urb *purb)
2191 struct s2255_pipeinfo *pipe_info;
2192 struct s2255_dev *dev;
2196 pipe_info = purb->context;
2197 dprintk(100, "read pipe completion %p, status %d\n", purb,
2199 if (pipe_info == NULL) {
2200 err("no context !");
2204 dev = pipe_info->dev;
2206 err("no context !");
2209 status = purb->status;
2211 dprintk(2, "read_pipe_completion: err\n");
2215 if (pipe_info->state == 0) {
2216 dprintk(2, "exiting USB pipe");
2220 s2255_read_video_callback(dev, pipe_info);
2222 pipe_info->err_count = 0;
2223 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2225 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2227 pipe_info->transfer_buffer,
2228 pipe_info->cur_transfer_size,
2229 read_pipe_completion, pipe_info);
2231 if (pipe_info->state != 0) {
2232 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2233 dev_err(&dev->udev->dev, "error submitting urb\n");
2234 usb_free_urb(pipe_info->stream_urb);
2237 dprintk(2, "read pipe complete state 0\n");
2242 static int s2255_start_readpipe(struct s2255_dev *dev)
2247 struct s2255_pipeinfo *pipe_info = dev->pipes;
2248 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2249 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2251 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2252 pipe_info->state = 1;
2253 pipe_info->buf_index = (u32) i;
2254 pipe_info->priority_set = 0;
2255 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2256 if (!pipe_info->stream_urb) {
2257 dev_err(&dev->udev->dev,
2258 "ReadStream: Unable to alloc URB");
2261 /* transfer buffer allocated in board_init */
2262 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2264 pipe_info->transfer_buffer,
2265 pipe_info->cur_transfer_size,
2266 read_pipe_completion, pipe_info);
2268 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2269 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2270 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2272 printk(KERN_ERR "s2255: start read pipe failed\n");
2280 /* starts acquisition process */
2281 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2283 unsigned char *buffer;
2285 unsigned long chn_rev;
2287 if (chn >= MAX_CHANNELS) {
2288 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2292 chn_rev = G_chnmap[chn];
2293 dprintk(1, "S2255: start acquire %lu \n", chn);
2295 buffer = kzalloc(512, GFP_KERNEL);
2296 if (buffer == NULL) {
2297 dev_err(&dev->udev->dev, "out of mem\n");
2301 dev->last_frame[chn] = -1;
2302 dev->bad_payload[chn] = 0;
2303 dev->cur_frame[chn] = 0;
2304 for (j = 0; j < SYS_FRAMES; j++) {
2305 dev->buffer[chn].frame[j].ulState = 0;
2306 dev->buffer[chn].frame[j].cur_size = 0;
2309 /* send the start command */
2310 *(u32 *) buffer = IN_DATA_TOKEN;
2311 *((u32 *) buffer + 1) = (u32) chn_rev;
2312 *((u32 *) buffer + 2) = (u32) CMD_START;
2313 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2315 dev_err(&dev->udev->dev, "CMD_START error\n");
2317 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2322 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2324 unsigned char *buffer;
2326 unsigned long chn_rev;
2328 if (chn >= MAX_CHANNELS) {
2329 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2332 chn_rev = G_chnmap[chn];
2334 buffer = kzalloc(512, GFP_KERNEL);
2335 if (buffer == NULL) {
2336 dev_err(&dev->udev->dev, "out of mem\n");
2340 /* send the stop command */
2341 dprintk(4, "stop acquire %lu\n", chn);
2342 *(u32 *) buffer = IN_DATA_TOKEN;
2343 *((u32 *) buffer + 1) = (u32) chn_rev;
2344 *((u32 *) buffer + 2) = CMD_STOP;
2345 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2348 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2350 dprintk(4, "stop acquire: releasing states \n");
2353 dev->b_acquire[chn] = 0;
2358 static void s2255_stop_readpipe(struct s2255_dev *dev)
2363 err("s2255: invalid device");
2366 dprintk(4, "stop read pipe\n");
2367 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2368 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2370 if (pipe_info->state == 0)
2372 pipe_info->state = 0;
2373 pipe_info->prev_state = 1;
2378 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2379 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2380 if (pipe_info->stream_urb) {
2382 usb_kill_urb(pipe_info->stream_urb);
2383 usb_free_urb(pipe_info->stream_urb);
2384 pipe_info->stream_urb = NULL;
2387 dprintk(2, "s2255 stop read pipe: %d\n", j);
2391 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2394 s2255_reset_dsppower(dev);
2395 dev->fw_data->fw_size = dev->fw_data->fw->size;
2396 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2397 memcpy(dev->fw_data->pfw_data,
2398 dev->fw_data->fw->data, CHUNK_SIZE);
2399 dev->fw_data->fw_loaded = CHUNK_SIZE;
2400 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2401 usb_sndbulkpipe(dev->udev, 2),
2402 dev->fw_data->pfw_data,
2403 CHUNK_SIZE, s2255_fwchunk_complete,
2405 mod_timer(&dev->timer, jiffies + HZ);
2408 /* standard usb probe function */
2409 static int s2255_probe(struct usb_interface *interface,
2410 const struct usb_device_id *id)
2412 struct s2255_dev *dev = NULL;
2413 struct usb_host_interface *iface_desc;
2414 struct usb_endpoint_descriptor *endpoint;
2416 int retval = -ENOMEM;
2420 dprintk(2, "s2255: probe\n");
2422 /* allocate memory for our device state and initialize it to zero */
2423 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2425 err("s2255: out of memory");
2429 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2433 mutex_init(&dev->lock);
2434 mutex_init(&dev->open_lock);
2436 /* grab usb_device and save it */
2437 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2438 if (dev->udev == NULL) {
2439 dev_err(&interface->dev, "null usb device\n");
2443 kref_init(&dev->kref);
2444 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2445 dev->udev, interface);
2446 dev->interface = interface;
2447 /* set up the endpoint information */
2448 iface_desc = interface->cur_altsetting;
2449 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2450 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2451 endpoint = &iface_desc->endpoint[i].desc;
2452 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2453 /* we found the bulk in endpoint */
2454 dev->read_endpoint = endpoint->bEndpointAddress;
2458 if (!dev->read_endpoint) {
2459 dev_err(&interface->dev, "Could not find bulk-in endpoint");
2464 usb_set_intfdata(interface, dev);
2466 dprintk(100, "after intfdata %p\n", dev);
2468 init_timer(&dev->timer);
2469 dev->timer.function = s2255_timer;
2470 dev->timer.data = (unsigned long)dev->fw_data;
2472 init_waitqueue_head(&dev->fw_data->wait_fw);
2473 for (i = 0; i < MAX_CHANNELS; i++)
2474 init_waitqueue_head(&dev->wait_setmode[i]);
2477 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2479 if (!dev->fw_data->fw_urb) {
2480 dev_err(&interface->dev, "out of memory!\n");
2483 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2484 if (!dev->fw_data->pfw_data) {
2485 dev_err(&interface->dev, "out of memory!\n");
2488 /* load the first chunk */
2489 if (request_firmware(&dev->fw_data->fw,
2490 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2491 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2494 /* check the firmware is valid */
2495 fw_size = dev->fw_data->fw->size;
2496 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2498 if (*pdata != S2255_FW_MARKER) {
2499 printk(KERN_INFO "Firmware invalid.\n");
2503 /* make sure firmware is the latest */
2505 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2506 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2508 /* loads v4l specific */
2509 s2255_probe_v4l(dev);
2510 usb_reset_device(dev->udev);
2511 /* load 2255 board specific */
2512 s2255_board_init(dev);
2514 dprintk(4, "before probe done %p\n", dev);
2515 spin_lock_init(&dev->slock);
2517 s2255_fwload_start(dev, 0);
2518 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2524 /* disconnect routine. when board is removed physically or with rmmod */
2525 static void s2255_disconnect(struct usb_interface *interface)
2527 struct s2255_dev *dev = NULL;
2529 dprintk(1, "s2255: disconnect interface %p\n", interface);
2530 dev = usb_get_intfdata(interface);
2533 * wake up any of the timers to allow open_lock to be
2536 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2537 wake_up(&dev->fw_data->wait_fw);
2538 for (i = 0; i < MAX_CHANNELS; i++) {
2539 dev->setmode_ready[i] = 1;
2540 wake_up(&dev->wait_setmode[i]);
2543 mutex_lock(&dev->open_lock);
2544 usb_set_intfdata(interface, NULL);
2545 mutex_unlock(&dev->open_lock);
2548 kref_put(&dev->kref, s2255_destroy);
2549 dprintk(1, "s2255drv: disconnect\n");
2550 dev_info(&interface->dev, "s2255usb now disconnected\n");
2554 static struct usb_driver s2255_driver = {
2556 .probe = s2255_probe,
2557 .disconnect = s2255_disconnect,
2558 .id_table = s2255_table,
2561 static int __init usb_s2255_init(void)
2565 /* register this driver with the USB subsystem */
2566 result = usb_register(&s2255_driver);
2569 err("usb_register failed. Error number %d", result);
2571 dprintk(2, "s2255_init: done\n");
2575 static void __exit usb_s2255_exit(void)
2577 usb_deregister(&s2255_driver);
2580 module_init(usb_s2255_init);
2581 module_exit(usb_s2255_exit);
2583 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2584 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2585 MODULE_LICENSE("GPL");