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>
50 #include <media/videobuf-vmalloc.h>
51 #include <media/v4l2-common.h>
52 #include <linux/vmalloc.h>
53 #include <linux/usb.h>
55 #define FIRMWARE_FILE_NAME "f2255usb.bin"
59 /* vendor request in */
61 /* vendor request out */
62 #define S2255_VR_OUT 1
64 #define S2255_VR_FW 0x30
65 /* USB endpoint number for configuring the device */
66 #define S2255_CONFIG_EP 2
67 /* maximum time for DSP to start responding after last FW word loaded(ms) */
68 #define S2255_DSP_BOOTTIME 400
69 /* maximum time to wait for firmware to load (ms) */
70 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
71 #define S2255_DEF_BUFS 16
72 #define MAX_CHANNELS 4
73 #define FRAME_MARKER 0x2255DA4AL
74 #define MAX_PIPE_USBBLOCK (40 * 1024)
75 #define DEFAULT_PIPE_USBBLOCK (16 * 1024)
76 #define MAX_CHANNELS 4
77 #define MAX_PIPE_BUFFERS 1
79 /* maximum size is PAL full size plus room for the marker header(s) */
80 #define SYS_FRAMES_MAXSIZE (720 * 288 * 2 * 2 + 4096)
81 #define DEF_USB_BLOCK (4096)
82 #define LINE_SZ_4CIFS_NTSC 640
83 #define LINE_SZ_2CIFS_NTSC 640
84 #define LINE_SZ_1CIFS_NTSC 320
85 #define LINE_SZ_4CIFS_PAL 704
86 #define LINE_SZ_2CIFS_PAL 704
87 #define LINE_SZ_1CIFS_PAL 352
88 #define NUM_LINES_4CIFS_NTSC 240
89 #define NUM_LINES_2CIFS_NTSC 240
90 #define NUM_LINES_1CIFS_NTSC 240
91 #define NUM_LINES_4CIFS_PAL 288
92 #define NUM_LINES_2CIFS_PAL 288
93 #define NUM_LINES_1CIFS_PAL 288
94 #define LINE_SZ_DEF 640
95 #define NUM_LINES_DEF 240
98 /* predefined settings */
102 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
103 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
104 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
106 #define COLOR_YUVPL 1 /* YUV planar */
107 #define COLOR_YUVPK 2 /* YUV packed */
108 #define COLOR_Y8 4 /* monochrome */
110 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
111 #define FDEC_1 1 /* capture every frame. default */
112 #define FDEC_2 2 /* capture every 2nd frame */
113 #define FDEC_3 3 /* capture every 3rd frame */
114 #define FDEC_5 5 /* capture every 5th frame */
116 /*-------------------------------------------------------
117 * Default mode parameters.
118 *-------------------------------------------------------*/
119 #define DEF_SCALE SCALE_4CIFS
120 #define DEF_COLOR COLOR_YUVPL
121 #define DEF_FDEC FDEC_1
123 #define DEF_CONTRAST 0x5c
124 #define DEF_SATURATION 0x80
127 /* usb config commands */
128 #define IN_DATA_TOKEN 0x2255c0de
129 #define CMD_2255 0xc2255000
130 #define CMD_SET_MODE (CMD_2255 | 0x10)
131 #define CMD_START (CMD_2255 | 0x20)
132 #define CMD_STOP (CMD_2255 | 0x30)
133 #define CMD_STATUS (CMD_2255 | 0x40)
136 u32 format; /* input video format (NTSC, PAL) */
137 u32 scale; /* output video scale */
138 u32 color; /* output video color format */
139 u32 fdec; /* frame decimation */
140 u32 bright; /* brightness */
141 u32 contrast; /* contrast */
142 u32 saturation; /* saturation */
143 u32 hue; /* hue (NTSC only)*/
144 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
145 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
146 u32 restart; /* if DSP requires restart */
149 /* frame structure */
150 #define FRAME_STATE_UNUSED 0
151 #define FRAME_STATE_FILLING 1
152 #define FRAME_STATE_FULL 2
155 struct s2255_framei {
158 unsigned long ulState; /* ulState ==0 unused, 1 being filled, 2 full */
159 void *lpvbits; /* image data */
160 unsigned long cur_size; /* current data copied to it */
163 /* image buffer structure */
164 struct s2255_bufferi {
165 unsigned long dwFrames; /* number of frames in buffer */
166 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
169 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
170 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
171 DEF_HUE, 0, DEF_USB_BLOCK, 0}
173 struct s2255_dmaqueue {
174 struct list_head active;
175 /* thread for acquisition */
176 struct task_struct *kthread;
178 struct s2255_dev *dev;
182 /* for firmware loading, fw_state */
183 #define S2255_FW_NOTLOADED 0
184 #define S2255_FW_LOADED_DSPWAIT 1
185 #define S2255_FW_SUCCESS 2
186 #define S2255_FW_FAILED 3
194 wait_queue_head_t wait_fw;
195 struct timer_list dsp_wait;
196 const struct firmware *fw;
199 struct s2255_pipeinfo {
200 u32 max_transfer_size;
201 u32 cur_transfer_size;
208 void *dev; /* back pointer to s2255_dev struct*/
215 struct s2255_fmt; /*forward declaration */
219 int users[MAX_CHANNELS];
221 struct mutex open_lock;
222 int resources[MAX_CHANNELS];
223 struct usb_device *udev;
224 struct usb_interface *interface;
227 struct s2255_dmaqueue vidq[MAX_CHANNELS];
228 struct video_device *vdev[MAX_CHANNELS];
229 struct list_head s2255_devlist;
230 struct timer_list timer;
231 struct s2255_fw *fw_data;
234 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
235 struct s2255_bufferi buffer[MAX_CHANNELS];
236 struct s2255_mode mode[MAX_CHANNELS];
237 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
238 int cur_frame[MAX_CHANNELS];
239 int last_frame[MAX_CHANNELS];
240 u32 cc; /* current channel */
241 int b_acquire[MAX_CHANNELS];
242 unsigned long req_image_size[MAX_CHANNELS];
243 int bad_payload[MAX_CHANNELS];
244 unsigned long frame_count[MAX_CHANNELS];
249 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
257 /* buffer for one video frame */
258 struct s2255_buffer {
259 /* common v4l buffer stuff -- must be first */
260 struct videobuf_buffer vb;
261 const struct s2255_fmt *fmt;
265 struct s2255_dev *dev;
266 unsigned int resources;
267 const struct s2255_fmt *fmt;
270 struct videobuf_queue vb_vidq;
271 enum v4l2_buf_type type;
273 /* mode below is the desired mode.
274 mode in s2255_dev is the current mode that was last set */
275 struct s2255_mode mode;
279 * TODO: fixme S2255_MAX_USERS. Do not limit open driver handles.
280 * Limit V4L to one stream at a time.
282 #define S2255_MAX_USERS 1
284 #define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
285 #define S2255_MAJOR_VERSION 1
286 #define S2255_MINOR_VERSION 13
287 #define S2255_RELEASE 0
288 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
289 S2255_MINOR_VERSION, \
293 #define USB_S2255_VENDOR_ID 0x1943
294 #define USB_S2255_PRODUCT_ID 0x2255
295 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
296 /* frame prefix size (sent once every frame) */
297 #define PREFIX_SIZE 512
299 /* Channels on box are in reverse order */
300 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
302 static LIST_HEAD(s2255_devlist);
305 static int *s2255_debug = &debug;
307 static int s2255_start_readpipe(struct s2255_dev *dev);
308 static void s2255_stop_readpipe(struct s2255_dev *dev);
309 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
310 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
311 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
313 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
314 struct s2255_mode *mode);
315 static int s2255_board_shutdown(struct s2255_dev *dev);
316 static void s2255_exit_v4l(struct s2255_dev *dev);
317 static void s2255_fwload_start(struct s2255_dev *dev);
319 #define dprintk(level, fmt, arg...) \
321 if (*s2255_debug >= (level)) { \
322 printk(KERN_DEBUG "s2255: " fmt, ##arg); \
327 static struct usb_driver s2255_driver;
330 /* Declare static vars that will be used as parameters */
331 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
333 /* start video number */
334 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
336 module_param(debug, int, 0644);
337 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
338 module_param(vid_limit, int, 0644);
339 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
340 module_param(video_nr, int, 0644);
341 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
343 /* USB device table */
344 static struct usb_device_id s2255_table[] = {
345 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
346 { } /* Terminating entry */
348 MODULE_DEVICE_TABLE(usb, s2255_table);
351 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
353 /* supported controls */
354 static struct v4l2_queryctrl s2255_qctrl[] = {
356 .id = V4L2_CID_BRIGHTNESS,
357 .type = V4L2_CTRL_TYPE_INTEGER,
358 .name = "Brightness",
365 .id = V4L2_CID_CONTRAST,
366 .type = V4L2_CTRL_TYPE_INTEGER,
371 .default_value = DEF_CONTRAST,
374 .id = V4L2_CID_SATURATION,
375 .type = V4L2_CTRL_TYPE_INTEGER,
376 .name = "Saturation",
380 .default_value = DEF_SATURATION,
384 .type = V4L2_CTRL_TYPE_INTEGER,
389 .default_value = DEF_HUE,
394 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
397 static const struct s2255_fmt formats[] = {
399 .name = "4:2:2, planar, YUV422P",
400 .fourcc = V4L2_PIX_FMT_YUV422P,
404 .name = "4:2:2, packed, YUYV",
405 .fourcc = V4L2_PIX_FMT_YUYV,
409 .name = "4:2:2, packed, UYVY",
410 .fourcc = V4L2_PIX_FMT_UYVY,
414 .fourcc = V4L2_PIX_FMT_GREY,
419 static int norm_maxw(struct video_device *vdev)
421 return (vdev->current_norm & V4L2_STD_NTSC) ?
422 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
425 static int norm_maxh(struct video_device *vdev)
427 return (vdev->current_norm & V4L2_STD_NTSC) ?
428 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
431 static int norm_minw(struct video_device *vdev)
433 return (vdev->current_norm & V4L2_STD_NTSC) ?
434 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
437 static int norm_minh(struct video_device *vdev)
439 return (vdev->current_norm & V4L2_STD_NTSC) ?
440 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
445 * TODO: fixme: move YUV reordering to hardware
446 * converts 2255 planar format to yuyv or uyvy
448 static void planar422p_to_yuv_packed(const unsigned char *in,
450 int width, int height,
456 unsigned long size = height * width;
458 pY = (unsigned char *)in;
459 pCr = (unsigned char *)in + height * width;
460 pCb = (unsigned char *)in + height * width + (height * width / 2);
461 for (i = 0; i < size * 2; i += 4) {
462 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
463 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
464 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
465 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
471 /* kickstarts the firmware loading. from probe
473 static void s2255_timer(unsigned long user_data)
475 struct s2255_fw *data = (struct s2255_fw *)user_data;
476 dprintk(100, "s2255 timer\n");
477 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
478 printk(KERN_ERR "s2255: can't submit urb\n");
480 release_firmware(data->fw);
487 /* called when DSP is up and running. DSP is guaranteed to
488 be running after S2255_DSP_BOOTTIME */
489 static void s2255_dsp_running(unsigned long user_data)
491 struct s2255_fw *data = (struct s2255_fw *)user_data;
492 dprintk(1, "dsp running\n");
493 atomic_set(&data->fw_state, S2255_FW_SUCCESS);
494 wake_up(&data->wait_fw);
495 printk(KERN_INFO "s2255: firmware loaded successfully\n");
500 /* this loads the firmware asynchronously.
501 Originally this was done synchroously in probe.
502 But it is better to load it asynchronously here than block
503 inside the probe function. Blocking inside probe affects boot time.
504 FW loading is triggered by the timer in the probe function
506 static void s2255_fwchunk_complete(struct urb *urb)
508 struct s2255_fw *data = urb->context;
509 struct usb_device *udev = urb->dev;
511 dprintk(100, "udev %p urb %p", udev, urb);
512 /* TODO: fixme. reflect change in status */
514 dev_err(&udev->dev, "URB failed with status %d", urb->status);
517 if (data->fw_urb == NULL) {
518 dev_err(&udev->dev, "early disconncect\n");
521 #define CHUNK_SIZE 512
522 /* all USB transfers must be done with continuous kernel memory.
523 can't allocate more than 128k in current linux kernel, so
524 upload the firmware in chunks
526 if (data->fw_loaded < data->fw_size) {
527 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
528 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
530 if (len < CHUNK_SIZE)
531 memset(data->pfw_data, 0, CHUNK_SIZE);
533 dprintk(100, "completed len %d, loaded %d \n", len,
536 memcpy(data->pfw_data,
537 (char *) data->fw->data + data->fw_loaded, len);
539 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
540 data->pfw_data, CHUNK_SIZE,
541 s2255_fwchunk_complete, data);
542 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
543 dev_err(&udev->dev, "failed submit URB\n");
544 atomic_set(&data->fw_state, S2255_FW_FAILED);
545 /* wake up anything waiting for the firmware */
546 wake_up(&data->wait_fw);
549 data->fw_loaded += len;
551 init_timer(&data->dsp_wait);
552 data->dsp_wait.function = s2255_dsp_running;
553 data->dsp_wait.data = (unsigned long)data;
554 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
555 mod_timer(&data->dsp_wait, msecs_to_jiffies(S2255_DSP_BOOTTIME)
558 dprintk(100, "2255 complete done\n");
563 static int s2255_got_frame(struct s2255_dev *dev, int chn)
565 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
566 struct s2255_buffer *buf;
567 unsigned long flags = 0;
569 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
570 spin_lock_irqsave(&dev->slock, flags);
572 if (list_empty(&dma_q->active)) {
573 dprintk(1, "No active queue to serve\n");
577 buf = list_entry(dma_q->active.next,
578 struct s2255_buffer, vb.queue);
580 if (!waitqueue_active(&buf->vb.done)) {
585 list_del(&buf->vb.queue);
586 do_gettimeofday(&buf->vb.ts);
587 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
589 s2255_fillbuff(dev, buf, dma_q->channel);
590 wake_up(&buf->vb.done);
591 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
593 spin_unlock_irqrestore(&dev->slock, flags);
598 static const struct s2255_fmt *format_by_fourcc(int fourcc)
602 for (i = 0; i < ARRAY_SIZE(formats); i++) {
603 if (-1 == formats[i].fourcc)
605 if (formats[i].fourcc == fourcc)
614 /* video buffer vmalloc implementation based partly on VIVI driver which is
615 * Copyright (c) 2006 by
616 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
617 * Ted Walther <ted--a.t--enumera.com>
618 * John Sokol <sokol--a.t--videotechnology.com>
619 * http://v4l.videotechnology.com/
622 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
628 char *vbuf = videobuf_to_vmalloc(&buf->vb);
629 unsigned long last_frame;
630 struct s2255_framei *frm;
635 last_frame = dev->last_frame[chn];
636 if (last_frame != -1) {
637 frm = &dev->buffer[chn].frame[last_frame];
639 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
640 switch (buf->fmt->fourcc) {
641 case V4L2_PIX_FMT_YUYV:
642 case V4L2_PIX_FMT_UYVY:
643 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
648 case V4L2_PIX_FMT_GREY:
649 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
651 case V4L2_PIX_FMT_YUV422P:
653 buf->vb.width * buf->vb.height * 2);
656 printk(KERN_DEBUG "s2255: unknown format?\n");
658 dev->last_frame[chn] = -1;
659 /* done with the frame, free it */
661 dprintk(4, "freeing buffer\n");
663 printk(KERN_ERR "s2255: =======no frame\n");
667 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
668 (unsigned long)vbuf, pos);
669 /* tell v4l buffer was filled */
671 buf->vb.field_count++;
672 do_gettimeofday(&ts);
674 buf->vb.state = VIDEOBUF_DONE;
678 /* ------------------------------------------------------------------
680 ------------------------------------------------------------------*/
682 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
685 struct s2255_fh *fh = vq->priv_data;
687 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
690 *count = S2255_DEF_BUFS;
692 while (*size * (*count) > vid_limit * 1024 * 1024)
698 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
700 dprintk(4, "%s\n", __func__);
702 videobuf_waiton(&buf->vb, 0, 0);
703 videobuf_vmalloc_free(&buf->vb);
704 buf->vb.state = VIDEOBUF_NEEDS_INIT;
707 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
708 enum v4l2_field field)
710 struct s2255_fh *fh = vq->priv_data;
711 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
713 dprintk(4, "%s, field=%d\n", __func__, field);
717 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
718 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
719 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
720 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
721 dprintk(4, "invalid buffer prepare\n");
725 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
727 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
728 dprintk(4, "invalid buffer prepare\n");
733 buf->vb.width = fh->width;
734 buf->vb.height = fh->height;
735 buf->vb.field = field;
738 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
739 rc = videobuf_iolock(vq, &buf->vb, NULL);
744 buf->vb.state = VIDEOBUF_PREPARED;
747 free_buffer(vq, buf);
751 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
753 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
754 struct s2255_fh *fh = vq->priv_data;
755 struct s2255_dev *dev = fh->dev;
756 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
758 dprintk(1, "%s\n", __func__);
760 buf->vb.state = VIDEOBUF_QUEUED;
761 list_add_tail(&buf->vb.queue, &vidq->active);
764 static void buffer_release(struct videobuf_queue *vq,
765 struct videobuf_buffer *vb)
767 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
768 struct s2255_fh *fh = vq->priv_data;
769 dprintk(4, "%s %d\n", __func__, fh->channel);
770 free_buffer(vq, buf);
773 static struct videobuf_queue_ops s2255_video_qops = {
774 .buf_setup = buffer_setup,
775 .buf_prepare = buffer_prepare,
776 .buf_queue = buffer_queue,
777 .buf_release = buffer_release,
781 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
784 mutex_lock(&dev->lock);
785 if (dev->resources[fh->channel]) {
786 /* no, someone else uses it */
787 mutex_unlock(&dev->lock);
790 /* it's free, grab it */
791 dev->resources[fh->channel] = 1;
792 dprintk(1, "res: get\n");
793 mutex_unlock(&dev->lock);
797 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
799 return dev->resources[fh->channel];
802 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
804 dev->resources[fh->channel] = 0;
805 dprintk(1, "res: put\n");
809 static int vidioc_querycap(struct file *file, void *priv,
810 struct v4l2_capability *cap)
812 struct s2255_fh *fh = file->private_data;
813 struct s2255_dev *dev = fh->dev;
814 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
815 strlcpy(cap->card, "s2255", sizeof(cap->card));
816 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
817 sizeof(cap->bus_info));
818 cap->version = S2255_VERSION;
819 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
823 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
824 struct v4l2_fmtdesc *f)
830 if (index >= ARRAY_SIZE(formats))
833 dprintk(4, "name %s\n", formats[index].name);
834 strlcpy(f->description, formats[index].name, sizeof(f->description));
835 f->pixelformat = formats[index].fourcc;
839 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
840 struct v4l2_format *f)
842 struct s2255_fh *fh = priv;
844 f->fmt.pix.width = fh->width;
845 f->fmt.pix.height = fh->height;
846 f->fmt.pix.field = fh->vb_vidq.field;
847 f->fmt.pix.pixelformat = fh->fmt->fourcc;
848 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
849 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
853 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
854 struct v4l2_format *f)
856 const struct s2255_fmt *fmt;
857 enum v4l2_field field;
859 struct s2255_fh *fh = priv;
860 struct s2255_dev *dev = fh->dev;
864 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
866 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
871 field = f->fmt.pix.field;
872 if (field == V4L2_FIELD_ANY)
875 dprintk(4, "try format %d \n", is_ntsc);
876 /* supports 3 sizes. see s2255drv.h */
877 dprintk(50, "width test %d, height %d\n",
878 f->fmt.pix.width, f->fmt.pix.height);
881 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
882 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
884 field = V4L2_FIELD_SEQ_TB;
885 } else if (!((field == V4L2_FIELD_INTERLACED) ||
886 (field == V4L2_FIELD_SEQ_TB) ||
887 (field == V4L2_FIELD_INTERLACED_TB))) {
888 dprintk(1, "unsupported field setting\n");
892 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
894 field = V4L2_FIELD_TOP;
895 } else if (!((field == V4L2_FIELD_TOP) ||
896 (field == V4L2_FIELD_BOTTOM))) {
897 dprintk(1, "unsupported field setting\n");
902 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
903 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
904 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
905 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
906 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
907 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
909 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
912 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
913 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
915 field = V4L2_FIELD_SEQ_TB;
916 } else if (!((field == V4L2_FIELD_INTERLACED) ||
917 (field == V4L2_FIELD_SEQ_TB) ||
918 (field == V4L2_FIELD_INTERLACED_TB))) {
919 dprintk(1, "unsupported field setting\n");
923 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
925 field = V4L2_FIELD_TOP;
926 } else if (!((field == V4L2_FIELD_TOP) ||
927 (field == V4L2_FIELD_BOTTOM))) {
928 dprintk(1, "unsupported field setting\n");
932 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
933 dprintk(50, "pal 704\n");
934 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
935 field = V4L2_FIELD_SEQ_TB;
936 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
937 dprintk(50, "pal 352A\n");
938 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
939 field = V4L2_FIELD_TOP;
940 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
941 dprintk(50, "pal 352B\n");
942 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
943 field = V4L2_FIELD_TOP;
945 dprintk(50, "pal 352C\n");
946 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
947 field = V4L2_FIELD_TOP;
951 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
952 f->fmt.pix.height, f->fmt.pix.field);
953 f->fmt.pix.field = field;
954 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
955 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
959 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
960 struct v4l2_format *f)
962 struct s2255_fh *fh = priv;
963 const struct s2255_fmt *fmt;
964 struct videobuf_queue *q = &fh->vb_vidq;
968 ret = vidioc_try_fmt_vid_cap(file, fh, f);
973 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
978 mutex_lock(&q->vb_lock);
980 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
981 dprintk(1, "queue busy\n");
986 if (res_locked(fh->dev, fh)) {
987 dprintk(1, "can't change format after started\n");
993 fh->width = f->fmt.pix.width;
994 fh->height = f->fmt.pix.height;
995 fh->vb_vidq.field = f->fmt.pix.field;
997 norm = norm_minw(fh->dev->vdev[fh->channel]);
998 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
999 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
1000 fh->mode.scale = SCALE_4CIFS;
1002 fh->mode.scale = SCALE_2CIFS;
1005 fh->mode.scale = SCALE_1CIFS;
1009 switch (fh->fmt->fourcc) {
1010 case V4L2_PIX_FMT_GREY:
1011 fh->mode.color = COLOR_Y8;
1013 case V4L2_PIX_FMT_YUV422P:
1014 fh->mode.color = COLOR_YUVPL;
1016 case V4L2_PIX_FMT_YUYV:
1017 case V4L2_PIX_FMT_UYVY:
1019 fh->mode.color = COLOR_YUVPK;
1024 mutex_unlock(&q->vb_lock);
1028 static int vidioc_reqbufs(struct file *file, void *priv,
1029 struct v4l2_requestbuffers *p)
1032 struct s2255_fh *fh = priv;
1033 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1037 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1040 struct s2255_fh *fh = priv;
1041 rc = videobuf_querybuf(&fh->vb_vidq, p);
1045 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1048 struct s2255_fh *fh = priv;
1049 rc = videobuf_qbuf(&fh->vb_vidq, p);
1053 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1056 struct s2255_fh *fh = priv;
1057 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1061 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1062 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1064 struct s2255_fh *fh = priv;
1066 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1070 /* write to the configuration pipe, synchronously */
1071 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1078 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1079 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1084 static u32 get_transfer_size(struct s2255_mode *mode)
1086 int linesPerFrame = LINE_SZ_DEF;
1087 int pixelsPerLine = NUM_LINES_DEF;
1090 unsigned int mask_mult;
1095 if (mode->format == FORMAT_NTSC) {
1096 switch (mode->scale) {
1098 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1099 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1102 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1103 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1106 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1107 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1112 } else if (mode->format == FORMAT_PAL) {
1113 switch (mode->scale) {
1115 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1116 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1119 linesPerFrame = NUM_LINES_2CIFS_PAL;
1120 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1123 linesPerFrame = NUM_LINES_1CIFS_PAL;
1124 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1130 outImageSize = linesPerFrame * pixelsPerLine;
1131 if (mode->color != COLOR_Y8) {
1132 /* 2 bytes/pixel if not monochrome */
1136 /* total bytes to send including prefix and 4K padding;
1137 must be a multiple of USB_READ_SIZE */
1138 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1139 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1140 /* if size not a multiple of USB_READ_SIZE */
1141 if (usbInSize & ~mask_mult)
1142 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1146 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1148 struct device *dev = &sdev->udev->dev;
1149 dev_info(dev, "------------------------------------------------\n");
1150 dev_info(dev, "verify mode\n");
1151 dev_info(dev, "format: %d\n", mode->format);
1152 dev_info(dev, "scale: %d\n", mode->scale);
1153 dev_info(dev, "fdec: %d\n", mode->fdec);
1154 dev_info(dev, "color: %d\n", mode->color);
1155 dev_info(dev, "bright: 0x%x\n", mode->bright);
1156 dev_info(dev, "restart: 0x%x\n", mode->restart);
1157 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1158 dev_info(dev, "single: 0x%x\n", mode->single);
1159 dev_info(dev, "------------------------------------------------\n");
1163 * set mode is the function which controls the DSP.
1164 * the restart parameter in struct s2255_mode should be set whenever
1165 * the image size could change via color format, video system or image
1167 * When the restart parameter is set, we sleep for ONE frame to allow the
1168 * DSP time to get the new frame
1170 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1171 struct s2255_mode *mode)
1175 unsigned long chn_rev;
1177 chn_rev = G_chnmap[chn];
1178 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1179 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1180 dev->mode[chn].scale);
1181 dprintk(2, "mode contrast %x\n", mode->contrast);
1184 dev->mode[chn] = *mode;
1185 dev->req_image_size[chn] = get_transfer_size(mode);
1186 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1188 buffer = kzalloc(512, GFP_KERNEL);
1189 if (buffer == NULL) {
1190 dev_err(&dev->udev->dev, "out of mem\n");
1195 buffer[0] = IN_DATA_TOKEN;
1196 buffer[1] = (u32) chn_rev;
1197 buffer[2] = CMD_SET_MODE;
1198 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1199 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1201 dump_verify_mode(dev, mode);
1203 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1205 /* wait at least 3 frames before continuing */
1209 /* clear the restart flag */
1210 dev->mode[chn].restart = 0;
1215 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1218 struct s2255_fh *fh = priv;
1219 struct s2255_dev *dev = fh->dev;
1220 struct s2255_mode *new_mode;
1221 struct s2255_mode *old_mode;
1224 dprintk(4, "%s\n", __func__);
1225 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1226 dev_err(&dev->udev->dev, "invalid fh type0\n");
1229 if (i != fh->type) {
1230 dev_err(&dev->udev->dev, "invalid fh type1\n");
1234 if (!res_get(dev, fh)) {
1235 dev_err(&dev->udev->dev, "res get busy\n");
1239 /* send a set mode command everytime with restart.
1240 in case we switch resolutions or other parameters */
1242 new_mode = &fh->mode;
1243 old_mode = &fh->dev->mode[chn];
1245 if (new_mode->color != old_mode->color)
1246 new_mode->restart = 1;
1247 else if (new_mode->scale != old_mode->scale)
1248 new_mode->restart = 1;
1249 else if (new_mode->format != old_mode->format)
1250 new_mode->restart = 1;
1252 s2255_set_mode(dev, chn, new_mode);
1253 new_mode->restart = 0;
1254 *old_mode = *new_mode;
1255 dev->cur_fmt[chn] = fh->fmt;
1256 dprintk(1, "%s[%d]\n", __func__, chn);
1257 dev->last_frame[chn] = -1;
1258 dev->bad_payload[chn] = 0;
1259 dev->cur_frame[chn] = 0;
1260 for (j = 0; j < SYS_FRAMES; j++) {
1261 dev->buffer[chn].frame[j].ulState = 0;
1262 dev->buffer[chn].frame[j].cur_size = 0;
1264 res = videobuf_streamon(&fh->vb_vidq);
1266 s2255_start_acquire(dev, chn);
1267 dev->b_acquire[chn] = 1;
1274 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1277 struct s2255_fh *fh = priv;
1278 struct s2255_dev *dev = fh->dev;
1280 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1281 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1282 printk(KERN_ERR "invalid fh type0\n");
1285 if (i != fh->type) {
1286 printk(KERN_ERR "invalid type i\n");
1289 s2255_stop_acquire(dev, fh->channel);
1290 res = videobuf_streamoff(&fh->vb_vidq);
1295 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1297 struct s2255_fh *fh = priv;
1298 struct s2255_mode *mode;
1299 struct videobuf_queue *q = &fh->vb_vidq;
1302 mutex_lock(&q->vb_lock);
1303 if (videobuf_queue_is_busy(q)) {
1304 dprintk(1, "queue busy\n");
1309 if (res_locked(fh->dev, fh)) {
1310 dprintk(1, "can't change standard after started\n");
1316 if (*i & V4L2_STD_NTSC) {
1317 dprintk(4, "vidioc_s_std NTSC\n");
1318 mode->format = FORMAT_NTSC;
1319 } else if (*i & V4L2_STD_PAL) {
1320 dprintk(4, "vidioc_s_std PAL\n");
1321 mode->format = FORMAT_PAL;
1326 mutex_unlock(&q->vb_lock);
1330 /* Sensoray 2255 is a multiple channel capture device.
1331 It does not have a "crossbar" of inputs.
1332 We use one V4L device per channel. The user must
1333 be aware that certain combinations are not allowed.
1334 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1335 at once in color(you can do full fps on 4 channels with greyscale.
1337 static int vidioc_enum_input(struct file *file, void *priv,
1338 struct v4l2_input *inp)
1340 if (inp->index != 0)
1343 inp->type = V4L2_INPUT_TYPE_CAMERA;
1344 inp->std = S2255_NORMS;
1345 strlcpy(inp->name, "Camera", sizeof(inp->name));
1349 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1354 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1361 /* --- controls ---------------------------------------------- */
1362 static int vidioc_queryctrl(struct file *file, void *priv,
1363 struct v4l2_queryctrl *qc)
1367 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1368 if (qc->id && qc->id == s2255_qctrl[i].id) {
1369 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1373 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1377 static int vidioc_g_ctrl(struct file *file, void *priv,
1378 struct v4l2_control *ctrl)
1382 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1383 if (ctrl->id == s2255_qctrl[i].id) {
1384 ctrl->value = qctl_regs[i];
1387 dprintk(4, "g_ctrl -EINVAL\n");
1392 static int vidioc_s_ctrl(struct file *file, void *priv,
1393 struct v4l2_control *ctrl)
1396 struct s2255_fh *fh = priv;
1397 struct s2255_dev *dev = fh->dev;
1398 struct s2255_mode *mode;
1400 dprintk(4, "vidioc_s_ctrl\n");
1401 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1402 if (ctrl->id == s2255_qctrl[i].id) {
1403 if (ctrl->value < s2255_qctrl[i].minimum ||
1404 ctrl->value > s2255_qctrl[i].maximum)
1407 qctl_regs[i] = ctrl->value;
1408 /* update the mode to the corresponding value */
1410 case V4L2_CID_BRIGHTNESS:
1411 mode->bright = ctrl->value;
1413 case V4L2_CID_CONTRAST:
1414 mode->contrast = ctrl->value;
1417 mode->hue = ctrl->value;
1419 case V4L2_CID_SATURATION:
1420 mode->saturation = ctrl->value;
1424 /* set mode here. Note: stream does not need restarted.
1425 some V4L programs restart stream unnecessarily
1428 s2255_set_mode(dev, fh->channel, mode);
1435 static int s2255_open(struct inode *inode, struct file *file)
1437 int minor = iminor(inode);
1438 struct s2255_dev *h, *dev = NULL;
1439 struct s2255_fh *fh;
1440 struct list_head *list;
1441 enum v4l2_buf_type type = 0;
1443 int cur_channel = -1;
1444 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1446 list_for_each(list, &s2255_devlist) {
1447 h = list_entry(list, struct s2255_dev, s2255_devlist);
1448 for (i = 0; i < MAX_CHANNELS; i++) {
1449 if (h->vdev[i]->minor == minor) {
1452 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1457 if ((NULL == dev) || (cur_channel == -1)) {
1458 dprintk(1, "s2255: openv4l no dev\n");
1462 mutex_lock(&dev->open_lock);
1464 dev->users[cur_channel]++;
1465 if (dev->users[cur_channel] > S2255_MAX_USERS) {
1466 dev->users[cur_channel]--;
1467 mutex_unlock(&dev->open_lock);
1468 printk(KERN_INFO "s2255drv: too many open handles!\n");
1472 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_FAILED) {
1473 err("2255 firmware load failed. retrying.\n");
1474 s2255_fwload_start(dev);
1475 wait_event_timeout(dev->fw_data->wait_fw,
1476 (atomic_read(&dev->fw_data->fw_state)
1477 != S2255_FW_NOTLOADED),
1478 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1479 if (atomic_read(&dev->fw_data->fw_state)
1480 != S2255_FW_SUCCESS) {
1481 printk(KERN_INFO "2255 FW load failed after 2 tries\n");
1482 mutex_unlock(&dev->open_lock);
1485 } else if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_NOTLOADED) {
1486 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1487 driver loaded and then device immediately opened */
1488 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1489 wait_event_timeout(dev->fw_data->wait_fw,
1490 (atomic_read(&dev->fw_data->fw_state)
1491 != S2255_FW_NOTLOADED),
1492 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1493 if (atomic_read(&dev->fw_data->fw_state)
1494 != S2255_FW_SUCCESS) {
1495 printk(KERN_INFO "2255 firmware not loaded"
1497 mutex_unlock(&dev->open_lock);
1502 /* allocate + initialize per filehandle data */
1503 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1505 mutex_unlock(&dev->open_lock);
1509 file->private_data = fh;
1511 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1512 fh->mode = dev->mode[cur_channel];
1513 fh->fmt = dev->cur_fmt[cur_channel];
1514 /* default 4CIF NTSC */
1515 fh->width = LINE_SZ_4CIFS_NTSC;
1516 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1517 fh->channel = cur_channel;
1519 /* Put all controls at a sane state */
1520 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1521 qctl_regs[i] = s2255_qctrl[i].default_value;
1523 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1524 minor, v4l2_type_names[type], dev->users[cur_channel]);
1525 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1526 (unsigned long)fh, (unsigned long)dev,
1527 (unsigned long)&dev->vidq[cur_channel]);
1528 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1529 list_empty(&dev->vidq[cur_channel].active));
1531 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1534 V4L2_FIELD_INTERLACED,
1535 sizeof(struct s2255_buffer), fh);
1537 kref_get(&dev->kref);
1538 mutex_unlock(&dev->open_lock);
1543 static unsigned int s2255_poll(struct file *file,
1544 struct poll_table_struct *wait)
1546 struct s2255_fh *fh = file->private_data;
1548 dprintk(100, "%s\n", __func__);
1550 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1553 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1557 static void s2255_destroy(struct kref *kref)
1559 struct s2255_dev *dev = to_s2255_dev(kref);
1561 printk(KERN_ERR "s2255drv: kref problem\n");
1564 /* prevent s2255_disconnect from racing s2255_open */
1565 mutex_lock(&dev->open_lock);
1566 s2255_exit_v4l(dev);
1567 /* device unregistered so no longer possible to open. open_mutex
1569 mutex_unlock(&dev->open_lock);
1571 /* board shutdown stops the read pipe if it is running */
1572 s2255_board_shutdown(dev);
1574 /* make sure firmware still not trying to load */
1575 if (dev->fw_data->fw_urb) {
1576 dprintk(2, "kill fw_urb\n");
1577 usb_kill_urb(dev->fw_data->fw_urb);
1578 usb_free_urb(dev->fw_data->fw_urb);
1579 dev->fw_data->fw_urb = NULL;
1582 * TODO: fixme(above, below): potentially leaving timers alive.
1583 * do not ignore timeout below if
1587 /* make sure we aren't waiting for the DSP */
1588 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_LOADED_DSPWAIT) {
1589 /* if we are, wait for the wakeup for fw_success or timeout */
1590 wait_event_timeout(dev->fw_data->wait_fw,
1591 (atomic_read(&dev->fw_data->fw_state)
1592 == S2255_FW_SUCCESS),
1593 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1597 if (dev->fw_data->fw)
1598 release_firmware(dev->fw_data->fw);
1599 kfree(dev->fw_data->pfw_data);
1600 kfree(dev->fw_data);
1603 usb_put_dev(dev->udev);
1604 dprintk(1, "%s", __func__);
1608 static int s2255_close(struct inode *inode, struct file *file)
1610 struct s2255_fh *fh = file->private_data;
1611 struct s2255_dev *dev = fh->dev;
1612 int minor = iminor(inode);
1616 mutex_lock(&dev->open_lock);
1618 if (dev->b_acquire[fh->channel])
1619 s2255_stop_acquire(dev, fh->channel);
1621 videobuf_mmap_free(&fh->vb_vidq);
1623 dev->users[fh->channel]--;
1624 mutex_unlock(&dev->open_lock);
1626 kref_put(&dev->kref, s2255_destroy);
1627 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1628 minor, dev->users[fh->channel]);
1632 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1634 struct s2255_fh *fh = file->private_data;
1639 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1641 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1643 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1644 (unsigned long)vma->vm_start,
1645 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1650 static const struct file_operations s2255_fops_v4l = {
1651 .owner = THIS_MODULE,
1653 .release = s2255_close,
1655 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1656 .compat_ioctl = v4l_compat_ioctl32,
1657 .mmap = s2255_mmap_v4l,
1658 .llseek = no_llseek,
1661 static struct video_device template = {
1663 .type = VID_TYPE_CAPTURE,
1664 .fops = &s2255_fops_v4l,
1666 .release = video_device_release,
1667 .vidioc_querycap = vidioc_querycap,
1668 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1669 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1670 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1671 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1672 .vidioc_reqbufs = vidioc_reqbufs,
1673 .vidioc_querybuf = vidioc_querybuf,
1674 .vidioc_qbuf = vidioc_qbuf,
1675 .vidioc_dqbuf = vidioc_dqbuf,
1676 .vidioc_s_std = vidioc_s_std,
1677 .vidioc_enum_input = vidioc_enum_input,
1678 .vidioc_g_input = vidioc_g_input,
1679 .vidioc_s_input = vidioc_s_input,
1680 .vidioc_queryctrl = vidioc_queryctrl,
1681 .vidioc_g_ctrl = vidioc_g_ctrl,
1682 .vidioc_s_ctrl = vidioc_s_ctrl,
1683 .vidioc_streamon = vidioc_streamon,
1684 .vidioc_streamoff = vidioc_streamoff,
1685 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1686 .vidiocgmbuf = vidioc_cgmbuf,
1688 .tvnorms = S2255_NORMS,
1689 .current_norm = V4L2_STD_NTSC_M,
1692 static int s2255_probe_v4l(struct s2255_dev *dev)
1696 int cur_nr = video_nr;
1698 /* initialize all video 4 linux */
1699 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1700 /* register 4 video devices */
1701 for (i = 0; i < MAX_CHANNELS; i++) {
1702 INIT_LIST_HEAD(&dev->vidq[i].active);
1703 dev->vidq[i].dev = dev;
1704 dev->vidq[i].channel = i;
1705 dev->vidq[i].kthread = NULL;
1706 /* register 4 video devices */
1707 dev->vdev[i] = video_device_alloc();
1708 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1709 dev->vdev[i]->dev = &dev->interface->dev;
1711 ret = video_register_device(dev->vdev[i],
1715 ret = video_register_device(dev->vdev[i],
1718 dev->vdev[i]->priv = dev;
1721 dev_err(&dev->udev->dev,
1722 "failed to register video device!\n");
1726 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1730 static void s2255_exit_v4l(struct s2255_dev *dev)
1732 struct list_head *list;
1734 /* unregister the video devices */
1735 while (!list_empty(&s2255_devlist)) {
1736 list = s2255_devlist.next;
1739 for (i = 0; i < MAX_CHANNELS; i++) {
1740 if (-1 != dev->vdev[i]->minor)
1741 video_unregister_device(dev->vdev[i]);
1743 video_device_release(dev->vdev[i]);
1747 /* this function moves the usb stream read pipe data
1748 * into the system buffers.
1749 * returns 0 on success, EAGAIN if more data to process( call this
1752 * Received frame structure:
1753 * bytes 0-3: marker : 0x2255DA4AL (FRAME_MARKER)
1754 * bytes 4-7: channel: 0-3
1755 * bytes 8-11: payload size: size of the frame
1756 * bytes 12-payloadsize+12: frame data
1758 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1760 static int dbgsync; /* = 0; */
1766 unsigned long copy_size;
1769 struct s2255_framei *frm;
1770 unsigned char *pdata;
1771 unsigned long cur_size;
1773 struct s2255_bufferi *buf;
1774 dprintk(100, "buffer to user\n");
1776 idx = dev->cur_frame[dev->cc];
1777 buf = &dev->buffer[dev->cc];
1778 frm = &buf->frame[idx];
1780 if (frm->ulState == 0) {
1784 } else if (frm->ulState == 2) {
1785 /* system frame was not freed */
1786 dprintk(2, "sys frame not free. overrun ringbuf\n");
1793 if (*(s32 *) pipe_info->transfer_buffer != FRAME_MARKER) {
1796 dprintk(3, "not synched, discarding all packets"
1801 pdata = (unsigned char *)pipe_info->transfer_buffer;
1802 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12);
1804 if (*(s32 *) pdata == FRAME_MARKER) {
1807 "found frame marker at offset:"
1808 " %d [%x %x]\n", jj, pdata[0],
1812 cc = *(u32 *) (pdata + sizeof(u32));
1813 if (cc >= MAX_CHANNELS) {
1819 dev->cc = G_chnmap[cc];
1832 pword = (u32 *) pipe_info->transfer_buffer;
1835 if (cc >= MAX_CHANNELS) {
1836 printk("invalid channel found. "
1837 "throwing out data!\n");
1840 dev->cc = G_chnmap[cc];
1842 if (payload != dev->req_image_size[dev->cc]) {
1843 dprintk(1, "[%d][%d]unexpected payload: %d"
1844 "required: %lu \n", cc, dev->cc,
1845 payload, dev->req_image_size[dev->cc]);
1846 dev->bad_payload[dev->cc]++;
1847 /* discard the bad frame */
1853 /* search done. now find out if should be acquiring
1855 if (!dev->b_acquire[dev->cc]) {
1860 idx = dev->cur_frame[dev->cc];
1861 frm = &dev->buffer[dev->cc].frame[idx];
1863 if (frm->ulState == 0) {
1866 } else if (frm->ulState == 2) {
1867 /* system frame ring buffer overrun */
1868 dprintk(2, "sys frame overrun. overwriting frame %d %d\n",
1875 /* skip the marker 512 bytes (and offset if out of sync) */
1876 psrc = (u8 *)pipe_info->transfer_buffer + offset + PREFIX_SIZE;
1878 psrc = (u8 *)pipe_info->transfer_buffer;
1881 if (frm->lpvbits == NULL) {
1882 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1883 frm, dev, dev->cc, idx);
1887 pdest = frm->lpvbits + frm->cur_size;
1891 (pipe_info->cur_transfer_size - offset) - PREFIX_SIZE;
1892 if (copy_size > pipe_info->cur_transfer_size) {
1893 printk("invalid copy size, overflow!\n");
1897 copy_size = pipe_info->cur_transfer_size;
1900 cur_size = frm->cur_size;
1901 size = dev->req_image_size[dev->cc];
1903 if ((copy_size + cur_size) > size) {
1904 copy_size = size - cur_size;
1908 memcpy(pdest, psrc, copy_size);
1909 cur_size += copy_size;
1910 frm->cur_size += copy_size;
1911 dprintk(50, "cur_size size %lu size %lu \n", cur_size, size);
1913 if (cur_size >= (size - PREFIX_SIZE)) {
1916 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
1918 dev->last_frame[cc] = dev->cur_frame[cc];
1919 dev->cur_frame[cc]++;
1920 /* end of system frame ring buffer, start at zero */
1921 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
1922 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
1923 dev->cur_frame[cc] = 0;
1925 /* signal the semaphore for this channel */
1926 if (dev->b_acquire[cc])
1927 s2255_got_frame(dev, cc);
1928 dev->frame_count[cc]++;
1930 /* frame was truncated */
1932 /* return more data to process */
1935 /* done successfully */
1939 static void s2255_read_video_callback(struct s2255_dev *dev,
1940 struct s2255_pipeinfo *pipe_info)
1943 dprintk(50, "callback read video \n");
1945 if (dev->cc >= MAX_CHANNELS) {
1947 dev_err(&dev->udev->dev, "invalid channel\n");
1950 /* otherwise copy to the system buffers */
1951 res = save_frame(dev, pipe_info);
1953 save_frame(dev, pipe_info);
1955 dprintk(50, "callback read video done\n");
1959 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
1960 u16 Index, u16 Value, void *TransferBuffer,
1961 s32 TransferBufferLength, int bOut)
1965 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1967 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1969 Value, Index, TransferBuffer,
1970 TransferBufferLength, HZ * 5);
1972 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1973 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1974 Value, Index, TransferBuffer,
1975 TransferBufferLength, HZ * 5);
1981 * retrieve FX2 firmware version. future use.
1982 * @param dev pointer to device extension
1983 * @return -1 for fail, else returns firmware version as an int(16 bits)
1985 static int s2255_get_fx2fw(struct s2255_dev *dev)
1989 unsigned char transBuffer[64];
1990 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
1993 dprintk(2, "get fw error: %x\n", ret);
1994 fw = transBuffer[0] + (transBuffer[1] << 8);
1995 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2000 * Create the system ring buffer to copy frames into from the
2003 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2006 unsigned long reqsize;
2007 dprintk(1, "create sys buffers\n");
2008 if (chn >= MAX_CHANNELS)
2011 dev->buffer[chn].dwFrames = SYS_FRAMES;
2013 /* always allocate maximum size(PAL) for system buffers */
2014 reqsize = SYS_FRAMES_MAXSIZE;
2016 if (reqsize > SYS_FRAMES_MAXSIZE)
2017 reqsize = SYS_FRAMES_MAXSIZE;
2019 for (i = 0; i < SYS_FRAMES; i++) {
2020 /* allocate the frames */
2021 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2023 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2024 &dev->buffer[chn].frame[i], chn, i,
2025 dev->buffer[chn].frame[i].lpvbits);
2026 dev->buffer[chn].frame[i].size = reqsize;
2027 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2028 printk(KERN_INFO "out of memory. using less frames\n");
2029 dev->buffer[chn].dwFrames = i;
2034 /* make sure internal states are set */
2035 for (i = 0; i < SYS_FRAMES; i++) {
2036 dev->buffer[chn].frame[i].ulState = 0;
2037 dev->buffer[chn].frame[i].cur_size = 0;
2040 dev->cur_frame[chn] = 0;
2041 dev->last_frame[chn] = -1;
2045 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2046 unsigned long channel)
2049 dprintk(1, "release sys buffers\n");
2050 for (i = 0; i < SYS_FRAMES; i++) {
2051 if (dev->buffer[channel].frame[i].lpvbits) {
2052 dprintk(1, "vfree %p\n",
2053 dev->buffer[channel].frame[i].lpvbits);
2054 vfree(dev->buffer[channel].frame[i].lpvbits);
2056 dev->buffer[channel].frame[i].lpvbits = NULL;
2061 static int s2255_board_init(struct s2255_dev *dev)
2064 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2066 dprintk(4, "board init: %p", dev);
2068 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2069 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2071 memset(pipe, 0, sizeof(*pipe));
2073 pipe->cur_transfer_size = DEFAULT_PIPE_USBBLOCK;
2074 pipe->max_transfer_size = MAX_PIPE_USBBLOCK;
2076 if (pipe->cur_transfer_size > pipe->max_transfer_size)
2077 pipe->cur_transfer_size = pipe->max_transfer_size;
2078 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2080 if (pipe->transfer_buffer == NULL) {
2081 dprintk(1, "out of memory!\n");
2087 /* query the firmware */
2088 fw_ver = s2255_get_fx2fw(dev);
2090 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2091 if (fw_ver < CUR_USB_FWVER)
2092 err("usb firmware not up to date %d\n", fw_ver);
2094 for (j = 0; j < MAX_CHANNELS; j++) {
2095 dev->b_acquire[j] = 0;
2096 dev->mode[j] = mode_def;
2097 dev->cur_fmt[j] = &formats[0];
2098 dev->mode[j].restart = 1;
2099 dev->req_image_size[j] = get_transfer_size(&mode_def);
2100 dev->frame_count[j] = 0;
2101 /* create the system buffers */
2102 s2255_create_sys_buffers(dev, j);
2104 /* start read pipe */
2105 s2255_start_readpipe(dev);
2107 dprintk(1, "S2255: board initialized\n");
2111 static int s2255_board_shutdown(struct s2255_dev *dev)
2115 dprintk(1, "S2255: board shutdown: %p", dev);
2117 for (i = 0; i < MAX_CHANNELS; i++) {
2118 if (dev->b_acquire[i])
2119 s2255_stop_acquire(dev, i);
2122 s2255_stop_readpipe(dev);
2124 for (i = 0; i < MAX_CHANNELS; i++)
2125 s2255_release_sys_buffers(dev, i);
2127 /* release transfer buffers */
2128 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2129 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2130 kfree(pipe->transfer_buffer);
2135 static void read_pipe_completion(struct urb *purb)
2137 struct s2255_pipeinfo *pipe_info;
2138 struct s2255_dev *dev;
2142 pipe_info = purb->context;
2143 dprintk(100, "read pipe completion %p, status %d\n", purb,
2145 if (pipe_info == NULL) {
2146 err("no context !");
2150 dev = pipe_info->dev;
2152 err("no context !");
2155 status = purb->status;
2157 dprintk(2, "read_pipe_completion: err\n");
2161 if (pipe_info->state == 0) {
2162 dprintk(2, "exiting USB pipe");
2166 s2255_read_video_callback(dev, pipe_info);
2168 pipe_info->err_count = 0;
2169 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2171 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2173 pipe_info->transfer_buffer,
2174 pipe_info->cur_transfer_size,
2175 read_pipe_completion, pipe_info);
2177 if (pipe_info->state != 0) {
2178 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2179 dev_err(&dev->udev->dev, "error submitting urb\n");
2180 usb_free_urb(pipe_info->stream_urb);
2183 dprintk(2, "read pipe complete state 0\n");
2188 static int s2255_start_readpipe(struct s2255_dev *dev)
2193 struct s2255_pipeinfo *pipe_info = dev->pipes;
2194 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2195 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2197 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2198 pipe_info->state = 1;
2199 pipe_info->buf_index = (u32) i;
2200 pipe_info->priority_set = 0;
2201 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2202 if (!pipe_info->stream_urb) {
2203 dev_err(&dev->udev->dev,
2204 "ReadStream: Unable to alloc URB");
2207 /* transfer buffer allocated in board_init */
2208 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2210 pipe_info->transfer_buffer,
2211 pipe_info->cur_transfer_size,
2212 read_pipe_completion, pipe_info);
2214 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2215 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2216 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2218 printk(KERN_ERR "s2255: start read pipe failed\n");
2226 /* starts acquisition process */
2227 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2229 unsigned char *buffer;
2231 unsigned long chn_rev;
2233 if (chn >= MAX_CHANNELS) {
2234 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2238 chn_rev = G_chnmap[chn];
2239 dprintk(1, "S2255: start acquire %lu \n", chn);
2241 buffer = kzalloc(512, GFP_KERNEL);
2242 if (buffer == NULL) {
2243 dev_err(&dev->udev->dev, "out of mem\n");
2247 dev->last_frame[chn] = -1;
2248 dev->bad_payload[chn] = 0;
2249 dev->cur_frame[chn] = 0;
2250 for (j = 0; j < SYS_FRAMES; j++) {
2251 dev->buffer[chn].frame[j].ulState = 0;
2252 dev->buffer[chn].frame[j].cur_size = 0;
2255 /* send the start command */
2256 *(u32 *) buffer = IN_DATA_TOKEN;
2257 *((u32 *) buffer + 1) = (u32) chn_rev;
2258 *((u32 *) buffer + 2) = (u32) CMD_START;
2259 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2261 dev_err(&dev->udev->dev, "CMD_START error\n");
2263 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2268 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2270 unsigned char *buffer;
2272 unsigned long chn_rev;
2274 if (chn >= MAX_CHANNELS) {
2275 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2278 chn_rev = G_chnmap[chn];
2280 buffer = kzalloc(512, GFP_KERNEL);
2281 if (buffer == NULL) {
2282 dev_err(&dev->udev->dev, "out of mem\n");
2286 /* send the stop command */
2287 dprintk(4, "stop acquire %lu\n", chn);
2288 *(u32 *) buffer = IN_DATA_TOKEN;
2289 *((u32 *) buffer + 1) = (u32) chn_rev;
2290 *((u32 *) buffer + 2) = CMD_STOP;
2291 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2294 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2296 dprintk(4, "stop acquire: releasing states \n");
2299 dev->b_acquire[chn] = 0;
2304 static void s2255_stop_readpipe(struct s2255_dev *dev)
2309 err("s2255: invalid device");
2312 dprintk(4, "stop read pipe\n");
2313 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2314 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2316 if (pipe_info->state == 0)
2318 pipe_info->state = 0;
2319 pipe_info->prev_state = 1;
2324 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2325 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2326 if (pipe_info->stream_urb) {
2328 usb_kill_urb(pipe_info->stream_urb);
2329 usb_free_urb(pipe_info->stream_urb);
2330 pipe_info->stream_urb = NULL;
2333 dprintk(2, "s2255 stop read pipe: %d\n", j);
2337 static void s2255_fwload_start(struct s2255_dev *dev)
2339 dev->fw_data->fw_size = dev->fw_data->fw->size;
2340 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2341 memcpy(dev->fw_data->pfw_data,
2342 dev->fw_data->fw->data, CHUNK_SIZE);
2343 dev->fw_data->fw_loaded = CHUNK_SIZE;
2344 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2345 usb_sndbulkpipe(dev->udev, 2),
2346 dev->fw_data->pfw_data,
2347 CHUNK_SIZE, s2255_fwchunk_complete,
2349 mod_timer(&dev->timer, jiffies + HZ);
2352 /* standard usb probe function */
2353 static int s2255_probe(struct usb_interface *interface,
2354 const struct usb_device_id *id)
2356 struct s2255_dev *dev = NULL;
2357 struct usb_host_interface *iface_desc;
2358 struct usb_endpoint_descriptor *endpoint;
2360 int retval = -ENOMEM;
2362 dprintk(2, "s2255: probe\n");
2364 /* allocate memory for our device state and initialize it to zero */
2365 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2367 err("s2255: out of memory");
2371 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2375 mutex_init(&dev->lock);
2376 mutex_init(&dev->open_lock);
2378 /* grab usb_device and save it */
2379 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2380 if (dev->udev == NULL) {
2381 dev_err(&interface->dev, "null usb device\n");
2385 kref_init(&dev->kref);
2386 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2387 dev->udev, interface);
2388 dev->interface = interface;
2389 /* set up the endpoint information */
2390 iface_desc = interface->cur_altsetting;
2391 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2392 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2393 endpoint = &iface_desc->endpoint[i].desc;
2394 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2395 /* we found the bulk in endpoint */
2396 dev->read_endpoint = endpoint->bEndpointAddress;
2400 if (!dev->read_endpoint) {
2401 dev_err(&interface->dev, "Could not find bulk-in endpoint");
2406 usb_set_intfdata(interface, dev);
2408 dprintk(100, "after intfdata %p\n", dev);
2410 init_timer(&dev->timer);
2411 dev->timer.function = s2255_timer;
2412 dev->timer.data = (unsigned long)dev->fw_data;
2414 init_waitqueue_head(&dev->fw_data->wait_fw);
2417 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2419 if (!dev->fw_data->fw_urb) {
2420 dev_err(&interface->dev, "out of memory!\n");
2423 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2424 if (!dev->fw_data->pfw_data) {
2425 dev_err(&interface->dev, "out of memory!\n");
2428 /* load the first chunk */
2429 if (request_firmware(&dev->fw_data->fw,
2430 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2431 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2435 /* loads v4l specific */
2436 s2255_probe_v4l(dev);
2437 /* load 2255 board specific */
2438 s2255_board_init(dev);
2440 dprintk(4, "before probe done %p\n", dev);
2441 spin_lock_init(&dev->slock);
2443 s2255_fwload_start(dev);
2444 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2450 /* disconnect routine. when board is removed physically or with rmmod */
2451 static void s2255_disconnect(struct usb_interface *interface)
2453 struct s2255_dev *dev = NULL;
2454 dprintk(1, "s2255: disconnect interface %p\n", interface);
2455 dev = usb_get_intfdata(interface);
2457 kref_put(&dev->kref, s2255_destroy);
2458 dprintk(1, "s2255drv: disconnect\n");
2459 dev_info(&interface->dev, "s2255usb now disconnected\n");
2461 usb_set_intfdata(interface, NULL);
2464 static struct usb_driver s2255_driver = {
2466 .probe = s2255_probe,
2467 .disconnect = s2255_disconnect,
2468 .id_table = s2255_table,
2471 static int __init usb_s2255_init(void)
2475 /* register this driver with the USB subsystem */
2476 result = usb_register(&s2255_driver);
2479 err("usb_register failed. Error number %d", result);
2481 dprintk(2, "s2255_init: done\n");
2485 static void __exit usb_s2255_exit(void)
2487 usb_deregister(&s2255_driver);
2490 module_init(usb_s2255_init);
2491 module_exit(usb_s2255_exit);
2493 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2494 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2495 MODULE_LICENSE("GPL");