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;
278 #define S2255_MAX_USERS 1
280 #define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
281 #define S2255_MAJOR_VERSION 1
282 #define S2255_MINOR_VERSION 13
283 #define S2255_RELEASE 0
284 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
285 S2255_MINOR_VERSION, \
289 #define USB_S2255_VENDOR_ID 0x1943
290 #define USB_S2255_PRODUCT_ID 0x2255
291 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
292 /* frame prefix size (sent once every frame) */
293 #define PREFIX_SIZE 512
295 /* Channels on box are in reverse order */
296 static unsigned long G_chnmap[MAX_CHANNELS] = { 3, 2, 1, 0 };
298 static LIST_HEAD(s2255_devlist);
301 static int *s2255_debug = &debug;
303 static int s2255_start_readpipe(struct s2255_dev *dev);
304 static void s2255_stop_readpipe(struct s2255_dev *dev);
305 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
306 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
307 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
309 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
310 struct s2255_mode *mode);
311 static int s2255_board_shutdown(struct s2255_dev *dev);
312 static void s2255_exit_v4l(struct s2255_dev *dev);
313 static void s2255_fwload_start(struct s2255_dev *dev);
315 #define dprintk(level, fmt, arg...) \
317 if (*s2255_debug >= (level)) { \
318 printk(KERN_DEBUG "s2255: " fmt, ##arg); \
323 static struct usb_driver s2255_driver;
326 /* Declare static vars that will be used as parameters */
327 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
329 /* start video number */
330 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
332 module_param(debug, int, 0);
333 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
334 module_param(vid_limit, int, 0);
335 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
336 module_param(video_nr, int, 0);
337 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
339 /* USB device table */
340 static struct usb_device_id s2255_table[] = {
341 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
342 { } /* Terminating entry */
344 MODULE_DEVICE_TABLE(usb, s2255_table);
347 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
349 /* supported controls */
350 static struct v4l2_queryctrl s2255_qctrl[] = {
352 .id = V4L2_CID_BRIGHTNESS,
353 .type = V4L2_CTRL_TYPE_INTEGER,
354 .name = "Brightness",
361 .id = V4L2_CID_CONTRAST,
362 .type = V4L2_CTRL_TYPE_INTEGER,
367 .default_value = DEF_CONTRAST,
370 .id = V4L2_CID_SATURATION,
371 .type = V4L2_CTRL_TYPE_INTEGER,
372 .name = "Saturation",
376 .default_value = DEF_SATURATION,
380 .type = V4L2_CTRL_TYPE_INTEGER,
385 .default_value = DEF_HUE,
390 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
393 static const struct s2255_fmt formats[] = {
395 .name = "4:2:2, planar, YUV422P",
396 .fourcc = V4L2_PIX_FMT_YUV422P,
400 .name = "4:2:2, packed, YUYV",
401 .fourcc = V4L2_PIX_FMT_YUYV,
405 .name = "4:2:2, packed, UYVY",
406 .fourcc = V4L2_PIX_FMT_UYVY,
410 .fourcc = V4L2_PIX_FMT_GREY,
415 static int norm_maxw(struct video_device *vdev)
417 return (vdev->current_norm & V4L2_STD_NTSC) ?
418 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
421 static int norm_maxh(struct video_device *vdev)
423 return (vdev->current_norm & V4L2_STD_NTSC) ?
424 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
427 static int norm_minw(struct video_device *vdev)
429 return (vdev->current_norm & V4L2_STD_NTSC) ?
430 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
433 static int norm_minh(struct video_device *vdev)
435 return (vdev->current_norm & V4L2_STD_NTSC) ?
436 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
440 /* converts 2255 planar format to yuyv or uyvy */
441 static void planar422p_to_yuv_packed(const unsigned char *in,
443 int width, int height,
449 unsigned long size = height * width;
451 pY = (unsigned char *)in;
452 pCr = (unsigned char *)in + height * width;
453 pCb = (unsigned char *)in + height * width + (height * width / 2);
454 for (i = 0; i < size * 2; i += 4) {
455 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
456 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
457 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
458 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
464 /* kickstarts the firmware loading. from probe
466 static void s2255_timer(unsigned long user_data)
468 struct s2255_fw *data = (struct s2255_fw *)user_data;
469 dprintk(100, "s2255 timer\n");
470 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
471 printk(KERN_ERR "s2255: can't submit urb\n");
473 release_firmware(data->fw);
480 /* called when DSP is up and running. DSP is guaranteed to
481 be running after S2255_DSP_BOOTTIME */
482 static void s2255_dsp_running(unsigned long user_data)
484 struct s2255_fw *data = (struct s2255_fw *)user_data;
485 dprintk(1, "dsp running\n");
486 atomic_set(&data->fw_state, S2255_FW_SUCCESS);
487 wake_up(&data->wait_fw);
488 printk(KERN_INFO "s2255: firmware loaded successfully\n");
493 /* this loads the firmware asynchronously.
494 Originally this was done synchroously in probe.
495 But it is better to load it asynchronously here than block
496 inside the probe function. Blocking inside probe affects boot time.
497 FW loading is triggered by the timer in the probe function
499 static void s2255_fwchunk_complete(struct urb *urb)
501 struct s2255_fw *data = urb->context;
502 struct usb_device *udev = urb->dev;
504 dprintk(100, "udev %p urb %p", udev, urb);
507 dev_err(&udev->dev, "URB failed with status %d", urb->status);
510 if (data->fw_urb == NULL) {
511 dev_err(&udev->dev, "early disconncect\n");
514 #define CHUNK_SIZE 512
515 /* all USB transfers must be done with continuous kernel memory.
516 can't allocate more than 128k in current linux kernel, so
517 upload the firmware in chunks
519 if (data->fw_loaded < data->fw_size) {
520 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
521 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
523 if (len < CHUNK_SIZE)
524 memset(data->pfw_data, 0, CHUNK_SIZE);
526 dprintk(100, "completed len %d, loaded %d \n", len,
529 memcpy(data->pfw_data,
530 (char *) data->fw->data + data->fw_loaded, len);
532 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
533 data->pfw_data, CHUNK_SIZE,
534 s2255_fwchunk_complete, data);
535 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
536 dev_err(&udev->dev, "failed submit URB\n");
537 atomic_set(&data->fw_state, S2255_FW_FAILED);
538 /* wake up anything waiting for the firmware */
539 wake_up(&data->wait_fw);
542 data->fw_loaded += len;
544 init_timer(&data->dsp_wait);
545 data->dsp_wait.function = s2255_dsp_running;
546 data->dsp_wait.data = (unsigned long)data;
547 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
548 mod_timer(&data->dsp_wait, msecs_to_jiffies(S2255_DSP_BOOTTIME)
551 dprintk(100, "2255 complete done\n");
556 static int s2255_got_frame(struct s2255_dev *dev, int chn)
558 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
559 struct s2255_buffer *buf;
560 unsigned long flags = 0;
562 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
563 spin_lock_irqsave(&dev->slock, flags);
565 if (list_empty(&dma_q->active)) {
566 dprintk(1, "No active queue to serve\n");
570 buf = list_entry(dma_q->active.next,
571 struct s2255_buffer, vb.queue);
573 if (!waitqueue_active(&buf->vb.done)) {
578 list_del(&buf->vb.queue);
579 do_gettimeofday(&buf->vb.ts);
580 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
582 s2255_fillbuff(dev, buf, dma_q->channel);
583 wake_up(&buf->vb.done);
584 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
586 spin_unlock_irqrestore(&dev->slock, flags);
591 static const struct s2255_fmt *format_by_fourcc(int fourcc)
595 for (i = 0; i < ARRAY_SIZE(formats); i++) {
596 if (-1 == formats[i].fourcc)
598 if (formats[i].fourcc == fourcc)
607 /* video buffer vmalloc implementation based partly on VIVI driver which is
608 * Copyright (c) 2006 by
609 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
610 * Ted Walther <ted--a.t--enumera.com>
611 * John Sokol <sokol--a.t--videotechnology.com>
612 * http://v4l.videotechnology.com/
615 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
621 char *vbuf = videobuf_to_vmalloc(&buf->vb);
622 unsigned long last_frame;
623 struct s2255_framei *frm;
628 last_frame = dev->last_frame[chn];
629 if (last_frame != -1) {
630 frm = &dev->buffer[chn].frame[last_frame];
632 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
633 switch (buf->fmt->fourcc) {
634 case V4L2_PIX_FMT_YUYV:
635 case V4L2_PIX_FMT_UYVY:
636 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
641 case V4L2_PIX_FMT_GREY:
642 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
644 case V4L2_PIX_FMT_YUV422P:
646 buf->vb.width * buf->vb.height * 2);
649 printk(KERN_DEBUG "s2255: unknown format?\n");
651 dev->last_frame[chn] = -1;
652 /* done with the frame, free it */
654 dprintk(4, "freeing buffer\n");
656 printk(KERN_ERR "s2255: =======no frame\n");
660 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
661 (unsigned long)vbuf, pos);
662 /* tell v4l buffer was filled */
664 buf->vb.field_count++;
665 do_gettimeofday(&ts);
667 buf->vb.state = VIDEOBUF_DONE;
671 /* ------------------------------------------------------------------
673 ------------------------------------------------------------------*/
675 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
678 struct s2255_fh *fh = vq->priv_data;
680 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
683 *count = S2255_DEF_BUFS;
685 while (*size * *count > vid_limit * 1024 * 1024)
691 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
693 dprintk(4, "%s\n", __func__);
695 videobuf_waiton(&buf->vb, 0, 0);
696 videobuf_vmalloc_free(&buf->vb);
697 buf->vb.state = VIDEOBUF_NEEDS_INIT;
700 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
701 enum v4l2_field field)
703 struct s2255_fh *fh = vq->priv_data;
704 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
706 dprintk(4, "%s, field=%d\n", __func__, field);
710 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
711 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
712 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
713 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
714 dprintk(4, "invalid buffer prepare\n");
718 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
720 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
721 dprintk(4, "invalid buffer prepare\n");
726 buf->vb.width = fh->width;
727 buf->vb.height = fh->height;
728 buf->vb.field = field;
731 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
732 rc = videobuf_iolock(vq, &buf->vb, NULL);
737 buf->vb.state = VIDEOBUF_PREPARED;
740 free_buffer(vq, buf);
744 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
746 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
747 struct s2255_fh *fh = vq->priv_data;
748 struct s2255_dev *dev = fh->dev;
749 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
751 dprintk(1, "%s\n", __func__);
753 buf->vb.state = VIDEOBUF_QUEUED;
754 list_add_tail(&buf->vb.queue, &vidq->active);
757 static void buffer_release(struct videobuf_queue *vq,
758 struct videobuf_buffer *vb)
760 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
761 struct s2255_fh *fh = vq->priv_data;
762 dprintk(4, "%s %d\n", __func__, fh->channel);
763 free_buffer(vq, buf);
766 static struct videobuf_queue_ops s2255_video_qops = {
767 .buf_setup = buffer_setup,
768 .buf_prepare = buffer_prepare,
769 .buf_queue = buffer_queue,
770 .buf_release = buffer_release,
774 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
777 mutex_lock(&dev->lock);
778 if (dev->resources[fh->channel]) {
779 /* no, someone else uses it */
780 mutex_unlock(&dev->lock);
783 /* it's free, grab it */
784 dev->resources[fh->channel] = 1;
785 dprintk(1, "res: get\n");
786 mutex_unlock(&dev->lock);
790 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
792 return (dev->resources[fh->channel]);
795 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
797 dev->resources[fh->channel] = 0;
798 dprintk(1, "res: put\n");
802 static int vidioc_querycap(struct file *file, void *priv,
803 struct v4l2_capability *cap)
805 struct s2255_fh *fh = file->private_data;
806 struct s2255_dev *dev = fh->dev;
807 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
808 strlcpy(cap->card, "s2255", sizeof(cap->card));
809 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
810 cap->version = S2255_VERSION;
811 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
815 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
816 struct v4l2_fmtdesc *f)
822 if (index >= ARRAY_SIZE(formats))
825 dprintk(4, "name %s\n", formats[index].name);
826 strlcpy(f->description, formats[index].name, sizeof(f->description));
827 f->pixelformat = formats[index].fourcc;
831 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
832 struct v4l2_format *f)
834 struct s2255_fh *fh = priv;
836 f->fmt.pix.width = fh->width;
837 f->fmt.pix.height = fh->height;
838 f->fmt.pix.field = fh->vb_vidq.field;
839 f->fmt.pix.pixelformat = fh->fmt->fourcc;
840 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
841 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
846 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
847 struct v4l2_format *f)
849 const struct s2255_fmt *fmt;
850 enum v4l2_field field;
852 struct s2255_fh *fh = priv;
853 struct s2255_dev *dev = fh->dev;
857 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
859 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
864 field = f->fmt.pix.field;
865 if (field == V4L2_FIELD_ANY)
868 dprintk(4, "try format %d \n", is_ntsc);
869 /* supports 3 sizes. see s2255drv.h */
870 dprintk(50, "width test %d, height %d\n",
871 f->fmt.pix.width, f->fmt.pix.height);
874 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
875 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
877 field = V4L2_FIELD_SEQ_TB;
878 } else if (!((field == V4L2_FIELD_INTERLACED) ||
879 (field == V4L2_FIELD_SEQ_TB) ||
880 (field == V4L2_FIELD_INTERLACED_TB))) {
881 dprintk(1, "unsupported field setting\n");
885 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
887 field = V4L2_FIELD_TOP;
888 } else if (!((field == V4L2_FIELD_TOP) ||
889 (field == V4L2_FIELD_BOTTOM))) {
890 dprintk(1, "unsupported field setting\n");
895 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
896 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
897 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
898 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
899 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
900 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
902 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
905 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
906 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 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_PAL;
918 field = V4L2_FIELD_TOP;
919 } else if (!((field == V4L2_FIELD_TOP) ||
920 (field == V4L2_FIELD_BOTTOM))) {
921 dprintk(1, "unsupported field setting\n");
925 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
926 dprintk(50, "pal 704\n");
927 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
928 field = V4L2_FIELD_SEQ_TB;
929 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
930 dprintk(50, "pal 352A\n");
931 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
932 field = V4L2_FIELD_TOP;
933 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
934 dprintk(50, "pal 352B\n");
935 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
936 field = V4L2_FIELD_TOP;
938 dprintk(50, "pal 352C\n");
939 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
940 field = V4L2_FIELD_TOP;
944 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
945 f->fmt.pix.height, f->fmt.pix.field);
946 f->fmt.pix.field = field;
947 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
948 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
952 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
953 struct v4l2_format *f)
955 struct s2255_fh *fh = priv;
956 const struct s2255_fmt *fmt;
957 struct videobuf_queue *q = &fh->vb_vidq;
961 ret = vidioc_try_fmt_vid_cap(file, fh, f);
966 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
971 mutex_lock(&q->vb_lock);
973 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
974 dprintk(1, "queue busy\n");
979 if (res_locked(fh->dev, fh)) {
980 dprintk(1, "can't change format after started\n");
986 fh->width = f->fmt.pix.width;
987 fh->height = f->fmt.pix.height;
988 fh->vb_vidq.field = f->fmt.pix.field;
990 norm = norm_minw(fh->dev->vdev[fh->channel]);
991 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
992 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
993 fh->mode.scale = SCALE_4CIFS;
995 fh->mode.scale = SCALE_2CIFS;
998 fh->mode.scale = SCALE_1CIFS;
1002 switch (fh->fmt->fourcc) {
1003 case V4L2_PIX_FMT_GREY:
1004 fh->mode.color = COLOR_Y8;
1006 case V4L2_PIX_FMT_YUV422P:
1007 fh->mode.color = COLOR_YUVPL;
1009 case V4L2_PIX_FMT_YUYV:
1010 case V4L2_PIX_FMT_UYVY:
1012 fh->mode.color = COLOR_YUVPK;
1017 mutex_unlock(&q->vb_lock);
1021 static int vidioc_reqbufs(struct file *file, void *priv,
1022 struct v4l2_requestbuffers *p)
1025 struct s2255_fh *fh = priv;
1026 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1030 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1033 struct s2255_fh *fh = priv;
1034 rc = videobuf_querybuf(&fh->vb_vidq, p);
1038 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1041 struct s2255_fh *fh = priv;
1042 rc = videobuf_qbuf(&fh->vb_vidq, p);
1046 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1049 struct s2255_fh *fh = priv;
1050 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1054 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1055 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1057 struct s2255_fh *fh = priv;
1059 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1063 /* write to the configuration pipe, synchronously */
1064 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1071 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1072 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1077 static u32 get_transfer_size(struct s2255_mode *mode)
1079 int linesPerFrame = LINE_SZ_DEF;
1080 int pixelsPerLine = NUM_LINES_DEF;
1083 unsigned int mask_mult;
1088 if (mode->format == FORMAT_NTSC) {
1089 switch (mode->scale) {
1091 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1092 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1095 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1096 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1099 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1100 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1105 } else if (mode->format == FORMAT_PAL) {
1106 switch (mode->scale) {
1108 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1109 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1112 linesPerFrame = NUM_LINES_2CIFS_PAL;
1113 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1116 linesPerFrame = NUM_LINES_1CIFS_PAL;
1117 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1123 outImageSize = linesPerFrame * pixelsPerLine;
1124 if (mode->color != COLOR_Y8) {
1125 /* 2 bytes/pixel if not monochrome */
1129 /* total bytes to send including prefix and 4K padding;
1130 must be a multiple of USB_READ_SIZE */
1131 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1132 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1133 /* if size not a multiple of USB_READ_SIZE */
1134 if (usbInSize & ~mask_mult)
1135 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1139 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1141 struct device *dev = &sdev->udev->dev;
1142 dev_info(dev, "------------------------------------------------\n");
1143 dev_info(dev, "verify mode\n");
1144 dev_info(dev, "format: %d\n", mode->format);
1145 dev_info(dev, "scale: %d\n", mode->scale);
1146 dev_info(dev, "fdec: %d\n", mode->fdec);
1147 dev_info(dev, "color: %d\n", mode->color);
1148 dev_info(dev, "bright: 0x%x\n", mode->bright);
1149 dev_info(dev, "restart: 0x%x\n", mode->restart);
1150 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1151 dev_info(dev, "single: 0x%x\n", mode->single);
1152 dev_info(dev, "------------------------------------------------\n");
1156 * set mode is the function which controls the DSP.
1157 * the restart parameter in struct s2255_mode should be set whenever
1158 * the image size could change via color format, video system or image
1160 * When the restart parameter is set, we sleep for ONE frame to allow the
1161 * DSP time to get the new frame
1163 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1164 struct s2255_mode *mode)
1168 unsigned long chn_rev;
1170 chn_rev = G_chnmap[chn];
1171 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1172 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1173 dev->mode[chn].scale);
1174 dprintk(2, "mode contrast %x\n", mode->contrast);
1177 dev->mode[chn] = *mode;
1178 dev->req_image_size[chn] = get_transfer_size(mode);
1179 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1181 buffer = kzalloc(512, GFP_KERNEL);
1182 if (buffer == NULL) {
1183 dev_err(&dev->udev->dev, "out of mem\n");
1188 buffer[0] = IN_DATA_TOKEN;
1189 buffer[1] = (u32) chn_rev;
1190 buffer[2] = CMD_SET_MODE;
1191 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1192 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1194 dump_verify_mode(dev, mode);
1196 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1198 /* wait at least 3 frames before continuing */
1202 /* clear the restart flag */
1203 dev->mode[chn].restart = 0;
1208 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1211 struct s2255_fh *fh = priv;
1212 struct s2255_dev *dev = fh->dev;
1213 struct s2255_mode *new_mode;
1214 struct s2255_mode *old_mode;
1217 dprintk(4, "%s\n", __func__);
1218 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1219 dev_err(&dev->udev->dev, "invalid fh type0\n");
1222 if (i != fh->type) {
1223 dev_err(&dev->udev->dev, "invalid fh type1\n");
1227 if (!res_get(dev, fh)) {
1228 dev_err(&dev->udev->dev, "res get busy\n");
1232 /* send a set mode command everytime with restart.
1233 in case we switch resolutions or other parameters */
1235 new_mode = &fh->mode;
1236 old_mode = &fh->dev->mode[chn];
1238 if (new_mode->color != old_mode->color)
1239 new_mode->restart = 1;
1240 else if (new_mode->scale != old_mode->scale)
1241 new_mode->restart = 1;
1242 else if (new_mode->format != old_mode->format)
1243 new_mode->restart = 1;
1245 s2255_set_mode(dev, chn, new_mode);
1246 new_mode->restart = 0;
1247 *old_mode = *new_mode;
1248 dev->cur_fmt[chn] = fh->fmt;
1249 dprintk(1, "%s[%d]\n", __func__, chn);
1250 dev->last_frame[chn] = -1;
1251 dev->bad_payload[chn] = 0;
1252 dev->cur_frame[chn] = 0;
1253 for (j = 0; j < SYS_FRAMES; j++) {
1254 dev->buffer[chn].frame[j].ulState = 0;
1255 dev->buffer[chn].frame[j].cur_size = 0;
1257 res = videobuf_streamon(&fh->vb_vidq);
1259 s2255_start_acquire(dev, chn);
1260 dev->b_acquire[chn] = 1;
1267 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1270 struct s2255_fh *fh = priv;
1271 struct s2255_dev *dev = fh->dev;
1273 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1274 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1275 printk(KERN_ERR "invalid fh type0\n");
1278 if (i != fh->type) {
1279 printk(KERN_ERR "invalid type i\n");
1282 s2255_stop_acquire(dev, fh->channel);
1283 res = videobuf_streamoff(&fh->vb_vidq);
1288 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1290 struct s2255_fh *fh = priv;
1291 struct s2255_mode *mode;
1292 struct videobuf_queue *q = &fh->vb_vidq;
1295 mutex_lock(&q->vb_lock);
1296 if (videobuf_queue_is_busy(q)) {
1297 dprintk(1, "queue busy\n");
1302 if (res_locked(fh->dev, fh)) {
1303 dprintk(1, "can't change standard after started\n");
1309 if (*i & V4L2_STD_NTSC) {
1310 dprintk(4, "vidioc_s_std NTSC\n");
1311 mode->format = FORMAT_NTSC;
1312 } else if (*i & V4L2_STD_PAL) {
1313 dprintk(4, "vidioc_s_std PAL\n");
1314 mode->format = FORMAT_PAL;
1319 mutex_unlock(&q->vb_lock);
1323 /* Sensoray 2255 is a multiple channel capture device.
1324 It does not have a "crossbar" of inputs.
1325 We use one V4L device per channel. The user must
1326 be aware that certain combinations are not allowed.
1327 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1328 at once in color(you can do full fps on 4 channels with greyscale.
1330 static int vidioc_enum_input(struct file *file, void *priv,
1331 struct v4l2_input *inp)
1333 if (inp->index != 0)
1336 inp->type = V4L2_INPUT_TYPE_CAMERA;
1337 inp->std = S2255_NORMS;
1338 strlcpy(inp->name, "Camera", sizeof(inp->name));
1342 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1347 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1354 /* --- controls ---------------------------------------------- */
1355 static int vidioc_queryctrl(struct file *file, void *priv,
1356 struct v4l2_queryctrl *qc)
1360 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1361 if (qc->id && qc->id == s2255_qctrl[i].id) {
1362 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1366 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1370 static int vidioc_g_ctrl(struct file *file, void *priv,
1371 struct v4l2_control *ctrl)
1375 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1376 if (ctrl->id == s2255_qctrl[i].id) {
1377 ctrl->value = qctl_regs[i];
1380 dprintk(4, "g_ctrl -EINVAL\n");
1385 static int vidioc_s_ctrl(struct file *file, void *priv,
1386 struct v4l2_control *ctrl)
1389 struct s2255_fh *fh = priv;
1390 struct s2255_dev *dev = fh->dev;
1391 struct s2255_mode *mode;
1393 dprintk(4, "vidioc_s_ctrl\n");
1394 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1395 if (ctrl->id == s2255_qctrl[i].id) {
1396 if (ctrl->value < s2255_qctrl[i].minimum ||
1397 ctrl->value > s2255_qctrl[i].maximum)
1400 qctl_regs[i] = ctrl->value;
1401 /* update the mode to the corresponding value */
1403 case V4L2_CID_BRIGHTNESS:
1404 mode->bright = ctrl->value;
1406 case V4L2_CID_CONTRAST:
1407 mode->contrast = ctrl->value;
1410 mode->hue = ctrl->value;
1412 case V4L2_CID_SATURATION:
1413 mode->saturation = ctrl->value;
1417 /* set mode here. Note: stream does not need restarted.
1418 some V4L programs restart stream unnecessarily
1421 s2255_set_mode(dev, fh->channel, mode);
1428 static int s2255_open(struct inode *inode, struct file *file)
1430 int minor = iminor(inode);
1431 struct s2255_dev *h, *dev = NULL;
1432 struct s2255_fh *fh;
1433 struct list_head *list;
1434 enum v4l2_buf_type type = 0;
1436 int cur_channel = -1;
1437 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1439 list_for_each(list, &s2255_devlist) {
1440 h = list_entry(list, struct s2255_dev, s2255_devlist);
1441 for (i = 0; i < MAX_CHANNELS; i++) {
1442 if (h->vdev[i]->minor == minor) {
1445 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1450 if ((NULL == dev) || (cur_channel == -1)) {
1451 dprintk(1, "s2255: openv4l no dev\n");
1455 mutex_lock(&dev->open_lock);
1457 dev->users[cur_channel]++;
1458 if (dev->users[cur_channel] > S2255_MAX_USERS) {
1459 dev->users[cur_channel]--;
1460 mutex_unlock(&dev->open_lock);
1461 printk(KERN_INFO "s2255drv: too many open handles!\n");
1465 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_FAILED) {
1466 err("2255 firmware load failed. retrying.\n");
1467 s2255_fwload_start(dev);
1468 wait_event_timeout(dev->fw_data->wait_fw,
1469 (atomic_read(&dev->fw_data->fw_state)
1470 != S2255_FW_NOTLOADED),
1471 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1472 if (atomic_read(&dev->fw_data->fw_state)
1473 != S2255_FW_SUCCESS) {
1474 printk(KERN_INFO "2255 FW load failed after 2 tries\n");
1475 mutex_unlock(&dev->open_lock);
1478 } else if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_NOTLOADED) {
1479 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1480 driver loaded and then device immediately opened */
1481 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1482 wait_event_timeout(dev->fw_data->wait_fw,
1483 (atomic_read(&dev->fw_data->fw_state)
1484 != S2255_FW_NOTLOADED),
1485 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1486 if (atomic_read(&dev->fw_data->fw_state)
1487 != S2255_FW_SUCCESS) {
1488 printk(KERN_INFO "2255 firmware not loaded"
1490 mutex_unlock(&dev->open_lock);
1495 /* allocate + initialize per filehandle data */
1496 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1498 mutex_unlock(&dev->open_lock);
1502 file->private_data = fh;
1504 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1505 fh->mode = dev->mode[cur_channel];
1506 fh->fmt = dev->cur_fmt[cur_channel];
1507 /* default 4CIF NTSC */
1508 fh->width = LINE_SZ_4CIFS_NTSC;
1509 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1510 fh->channel = cur_channel;
1512 /* Put all controls at a sane state */
1513 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1514 qctl_regs[i] = s2255_qctrl[i].default_value;
1516 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1517 minor, v4l2_type_names[type], dev->users[cur_channel]);
1518 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1519 (unsigned long)fh, (unsigned long)dev,
1520 (unsigned long)&dev->vidq[cur_channel]);
1521 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1522 list_empty(&dev->vidq[cur_channel].active));
1524 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1527 V4L2_FIELD_INTERLACED,
1528 sizeof(struct s2255_buffer), fh);
1530 kref_get(&dev->kref);
1531 mutex_unlock(&dev->open_lock);
1536 static unsigned int s2255_poll(struct file *file,
1537 struct poll_table_struct *wait)
1539 struct s2255_fh *fh = file->private_data;
1541 dprintk(100, "%s\n", __func__);
1543 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1546 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1550 static void s2255_destroy(struct kref *kref)
1552 struct s2255_dev *dev = to_s2255_dev(kref);
1554 printk(KERN_ERR "s2255drv: kref problem\n");
1557 /* prevent s2255_disconnect from racing s2255_open */
1558 mutex_lock(&dev->open_lock);
1559 s2255_exit_v4l(dev);
1560 /* device unregistered so no longer possible to open. open_mutex
1562 mutex_unlock(&dev->open_lock);
1564 /* board shutdown stops the read pipe if it is running */
1565 s2255_board_shutdown(dev);
1567 /* make sure firmware still not trying to load */
1568 if (dev->fw_data->fw_urb) {
1569 dprintk(2, "kill fw_urb\n");
1570 usb_kill_urb(dev->fw_data->fw_urb);
1571 usb_free_urb(dev->fw_data->fw_urb);
1572 dev->fw_data->fw_urb = NULL;
1575 /* make sure we aren't waiting for the DSP */
1576 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_LOADED_DSPWAIT) {
1577 /* if we are, wait for the wakeup for fw_success or timeout */
1578 wait_event_timeout(dev->fw_data->wait_fw,
1579 (atomic_read(&dev->fw_data->fw_state)
1580 == S2255_FW_SUCCESS),
1581 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1585 kfree(dev->fw_data->pfw_data);
1586 kfree(dev->fw_data);
1589 if (dev->fw_data->fw) {
1590 release_firmware(dev->fw_data->fw);
1591 dev->fw_data->fw = NULL;
1594 usb_put_dev(dev->udev);
1595 dprintk(1, "%s", __func__);
1599 static int s2255_close(struct inode *inode, struct file *file)
1601 struct s2255_fh *fh = file->private_data;
1602 struct s2255_dev *dev = fh->dev;
1603 int minor = iminor(inode);
1607 mutex_lock(&dev->open_lock);
1609 if (dev->b_acquire[fh->channel])
1610 s2255_stop_acquire(dev, fh->channel);
1612 videobuf_mmap_free(&fh->vb_vidq);
1614 dev->users[fh->channel]--;
1615 mutex_unlock(&dev->open_lock);
1617 kref_put(&dev->kref, s2255_destroy);
1618 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1619 minor, dev->users[fh->channel]);
1623 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1625 struct s2255_fh *fh = file->private_data;
1630 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1632 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1634 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1635 (unsigned long)vma->vm_start,
1636 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1641 static const struct file_operations s2255_fops_v4l = {
1642 .owner = THIS_MODULE,
1644 .release = s2255_close,
1646 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1647 .compat_ioctl = v4l_compat_ioctl32,
1648 .mmap = s2255_mmap_v4l,
1649 .llseek = no_llseek,
1652 static struct video_device template = {
1654 .type = VID_TYPE_CAPTURE,
1655 .fops = &s2255_fops_v4l,
1657 .release = video_device_release,
1658 .vidioc_querycap = vidioc_querycap,
1659 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1660 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1661 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1662 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1663 .vidioc_reqbufs = vidioc_reqbufs,
1664 .vidioc_querybuf = vidioc_querybuf,
1665 .vidioc_qbuf = vidioc_qbuf,
1666 .vidioc_dqbuf = vidioc_dqbuf,
1667 .vidioc_s_std = vidioc_s_std,
1668 .vidioc_enum_input = vidioc_enum_input,
1669 .vidioc_g_input = vidioc_g_input,
1670 .vidioc_s_input = vidioc_s_input,
1671 .vidioc_queryctrl = vidioc_queryctrl,
1672 .vidioc_g_ctrl = vidioc_g_ctrl,
1673 .vidioc_s_ctrl = vidioc_s_ctrl,
1674 .vidioc_streamon = vidioc_streamon,
1675 .vidioc_streamoff = vidioc_streamoff,
1676 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1677 .vidiocgmbuf = vidioc_cgmbuf,
1679 .tvnorms = S2255_NORMS,
1680 .current_norm = V4L2_STD_NTSC_M,
1683 static int s2255_probe_v4l(struct s2255_dev *dev)
1687 int cur_nr = video_nr;
1689 /* initialize all video 4 linux */
1690 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1691 /* register 4 video devices */
1692 for (i = 0; i < MAX_CHANNELS; i++) {
1693 INIT_LIST_HEAD(&dev->vidq[i].active);
1694 dev->vidq[i].dev = dev;
1695 dev->vidq[i].channel = i;
1696 dev->vidq[i].kthread = NULL;
1697 /* register 4 video devices */
1698 dev->vdev[i] = video_device_alloc();
1699 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1700 dev->vdev[i]->dev = &dev->interface->dev;
1702 ret = video_register_device(dev->vdev[i],
1706 ret = video_register_device(dev->vdev[i],
1709 dev->vdev[i]->priv = dev;
1712 dev_err(&dev->udev->dev,
1713 "failed to register video device!\n");
1717 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1721 static void s2255_exit_v4l(struct s2255_dev *dev)
1723 struct list_head *list;
1725 /* unregister the video devices */
1726 while (!list_empty(&s2255_devlist)) {
1727 list = s2255_devlist.next;
1730 for (i = 0; i < MAX_CHANNELS; i++) {
1731 if (-1 != dev->vdev[i]->minor)
1732 video_unregister_device(dev->vdev[i]);
1734 video_device_release(dev->vdev[i]);
1738 /* this function moves the usb stream read pipe data
1739 * into the system buffers.
1740 * returns 0 on success, EAGAIN if more data to process( call this
1743 * Received frame structure:
1744 * bytes 0-3: marker : 0x2255DA4AL (FRAME_MARKER)
1745 * bytes 4-7: channel: 0-3
1746 * bytes 8-11: payload size: size of the frame
1747 * bytes 12-payloadsize+12: frame data
1749 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1751 static int dbgsync; /* = 0; */
1757 unsigned long copy_size;
1760 struct s2255_framei *frm;
1761 unsigned char *pdata;
1762 unsigned long cur_size;
1764 struct s2255_bufferi *buf;
1765 dprintk(100, "buffer to user\n");
1767 idx = dev->cur_frame[dev->cc];
1768 buf = &dev->buffer[dev->cc];
1769 frm = &buf->frame[idx];
1771 if (frm->ulState == 0) {
1775 } else if (frm->ulState == 2) {
1776 /* system frame was not freed */
1777 dprintk(2, "sys frame not free. overrun ringbuf\n");
1784 if (*(s32 *) pipe_info->transfer_buffer != FRAME_MARKER) {
1787 dprintk(3, "not synched, discarding all packets"
1792 pdata = (unsigned char *)pipe_info->transfer_buffer;
1793 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12);
1795 if (*(s32 *) pdata == FRAME_MARKER) {
1798 "found frame marker at offset:"
1799 " %d [%x %x]\n", jj, pdata[0],
1803 cc = *(u32 *) (pdata + sizeof(u32));
1804 if (cc >= MAX_CHANNELS) {
1810 dev->cc = G_chnmap[cc];
1823 pword = (u32 *) pipe_info->transfer_buffer;
1826 if (cc >= MAX_CHANNELS) {
1827 printk("invalid channel found. "
1828 "throwing out data!\n");
1831 dev->cc = G_chnmap[cc];
1833 if (payload != dev->req_image_size[dev->cc]) {
1834 dprintk(1, "[%d][%d]unexpected payload: %d"
1835 "required: %lu \n", cc, dev->cc,
1836 payload, dev->req_image_size[dev->cc]);
1837 dev->bad_payload[dev->cc]++;
1838 /* discard the bad frame */
1844 /* search done. now find out if should be acquiring
1846 if (!dev->b_acquire[dev->cc]) {
1851 idx = dev->cur_frame[dev->cc];
1852 frm = &dev->buffer[dev->cc].frame[idx];
1854 if (frm->ulState == 0) {
1857 } else if (frm->ulState == 2) {
1858 /* system frame ring buffer overrun */
1859 dprintk(2, "sys frame overrun. overwriting frame %d %d\n",
1866 /* skip the marker 512 bytes (and offset if out of sync) */
1867 psrc = (u8 *)pipe_info->transfer_buffer + offset + PREFIX_SIZE;
1869 psrc = (u8 *)pipe_info->transfer_buffer;
1872 if (frm->lpvbits == NULL) {
1873 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1874 frm, dev, dev->cc, idx);
1878 pdest = frm->lpvbits + frm->cur_size;
1882 (pipe_info->cur_transfer_size - offset) - PREFIX_SIZE;
1883 if (copy_size > pipe_info->cur_transfer_size) {
1884 printk("invalid copy size, overflow!\n");
1888 copy_size = pipe_info->cur_transfer_size;
1891 cur_size = frm->cur_size;
1892 size = dev->req_image_size[dev->cc];
1894 if ((copy_size + cur_size) > size) {
1895 copy_size = size - cur_size;
1899 memcpy(pdest, psrc, copy_size);
1900 cur_size += copy_size;
1901 frm->cur_size += copy_size;
1902 dprintk(50, "cur_size size %lu size %lu \n", cur_size, size);
1904 if (cur_size >= (size - PREFIX_SIZE)) {
1907 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
1909 dev->last_frame[cc] = dev->cur_frame[cc];
1910 dev->cur_frame[cc]++;
1911 /* end of system frame ring buffer, start at zero */
1912 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
1913 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
1914 dev->cur_frame[cc] = 0;
1916 /* signal the semaphore for this channel */
1917 if (dev->b_acquire[cc])
1918 s2255_got_frame(dev, cc);
1919 dev->frame_count[cc]++;
1921 /* frame was truncated */
1923 /* return more data to process */
1926 /* done successfully */
1930 static void s2255_read_video_callback(struct s2255_dev *dev,
1931 struct s2255_pipeinfo *pipe_info)
1934 dprintk(50, "callback read video \n");
1936 if (dev->cc >= MAX_CHANNELS) {
1938 dev_err(&dev->udev->dev, "invalid channel\n");
1941 /* otherwise copy to the system buffers */
1942 res = save_frame(dev, pipe_info);
1944 save_frame(dev, pipe_info);
1946 dprintk(50, "callback read video done\n");
1950 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
1951 u16 Index, u16 Value, void *TransferBuffer,
1952 s32 TransferBufferLength, int bOut)
1956 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1958 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1960 Value, Index, TransferBuffer,
1961 TransferBufferLength, HZ * 5);
1963 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1964 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1965 Value, Index, TransferBuffer,
1966 TransferBufferLength, HZ * 5);
1972 * retrieve FX2 firmware version. future use.
1973 * @param dev pointer to device extension
1974 * @return -1 for fail, else returns firmware version as an int(16 bits)
1976 static int s2255_get_fx2fw(struct s2255_dev *dev)
1980 unsigned char transBuffer[64];
1981 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
1984 dprintk(2, "get fw error: %x\n", ret);
1985 fw = transBuffer[0] + (transBuffer[1] << 8);
1986 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
1991 * Create the system ring buffer to copy frames into from the
1994 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
1997 unsigned long reqsize;
1998 dprintk(1, "create sys buffers\n");
1999 if (chn >= MAX_CHANNELS)
2002 dev->buffer[chn].dwFrames = SYS_FRAMES;
2004 /* always allocate maximum size(PAL) for system buffers */
2005 reqsize = SYS_FRAMES_MAXSIZE;
2007 if (reqsize > SYS_FRAMES_MAXSIZE)
2008 reqsize = SYS_FRAMES_MAXSIZE;
2010 for (i = 0; i < SYS_FRAMES; i++) {
2011 /* allocate the frames */
2012 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2014 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2015 &dev->buffer[chn].frame[i], chn, i,
2016 dev->buffer[chn].frame[i].lpvbits);
2017 dev->buffer[chn].frame[i].size = reqsize;
2018 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2019 printk(KERN_INFO "out of memory. using less frames\n");
2020 dev->buffer[chn].dwFrames = i;
2025 /* make sure internal states are set */
2026 for (i = 0; i < SYS_FRAMES; i++) {
2027 dev->buffer[chn].frame[i].ulState = 0;
2028 dev->buffer[chn].frame[i].cur_size = 0;
2031 dev->cur_frame[chn] = 0;
2032 dev->last_frame[chn] = -1;
2036 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2037 unsigned long channel)
2040 dprintk(1, "release sys buffers\n");
2041 for (i = 0; i < SYS_FRAMES; i++) {
2042 if (dev->buffer[channel].frame[i].lpvbits) {
2043 dprintk(1, "vfree %p\n",
2044 dev->buffer[channel].frame[i].lpvbits);
2045 vfree(dev->buffer[channel].frame[i].lpvbits);
2047 dev->buffer[channel].frame[i].lpvbits = NULL;
2052 static int s2255_board_init(struct s2255_dev *dev)
2055 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2057 dprintk(4, "board init: %p", dev);
2059 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2060 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2062 memset(pipe, 0, sizeof(*pipe));
2064 pipe->cur_transfer_size = DEFAULT_PIPE_USBBLOCK;
2065 pipe->max_transfer_size = MAX_PIPE_USBBLOCK;
2067 if (pipe->cur_transfer_size > pipe->max_transfer_size)
2068 pipe->cur_transfer_size = pipe->max_transfer_size;
2069 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2071 if (pipe->transfer_buffer == NULL) {
2072 dprintk(1, "out of memory!\n");
2078 /* query the firmware */
2079 fw_ver = s2255_get_fx2fw(dev);
2081 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2082 if (fw_ver < CUR_USB_FWVER)
2083 err("usb firmware not up to date %d\n", fw_ver);
2085 for (j = 0; j < MAX_CHANNELS; j++) {
2086 dev->b_acquire[j] = 0;
2087 dev->mode[j] = mode_def;
2088 dev->cur_fmt[j] = &formats[0];
2089 dev->mode[j].restart = 1;
2090 dev->req_image_size[j] = get_transfer_size(&mode_def);
2091 dev->frame_count[j] = 0;
2092 /* create the system buffers */
2093 s2255_create_sys_buffers(dev, j);
2095 /* start read pipe */
2096 s2255_start_readpipe(dev);
2098 dprintk(1, "S2255: board initialized\n");
2102 static int s2255_board_shutdown(struct s2255_dev *dev)
2106 dprintk(1, "S2255: board shutdown: %p", dev);
2108 for (i = 0; i < MAX_CHANNELS; i++) {
2109 if (dev->b_acquire[i])
2110 s2255_stop_acquire(dev, i);
2113 s2255_stop_readpipe(dev);
2115 for (i = 0; i < MAX_CHANNELS; i++)
2116 s2255_release_sys_buffers(dev, i);
2118 /* release transfer buffers */
2119 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2120 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2121 kfree(pipe->transfer_buffer);
2126 static void read_pipe_completion(struct urb *purb)
2128 struct s2255_pipeinfo *pipe_info;
2129 struct s2255_dev *dev;
2133 pipe_info = purb->context;
2134 dprintk(100, "read pipe completion %p, status %d\n", purb,
2136 if (pipe_info == NULL) {
2137 err("no context !");
2141 dev = pipe_info->dev;
2143 err("no context !");
2146 status = purb->status;
2148 dprintk(2, "read_pipe_completion: err\n");
2152 if (pipe_info->state == 0) {
2153 dprintk(2, "exiting USB pipe");
2157 s2255_read_video_callback(dev, pipe_info);
2159 pipe_info->err_count = 0;
2160 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2162 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2164 pipe_info->transfer_buffer,
2165 pipe_info->cur_transfer_size,
2166 read_pipe_completion, pipe_info);
2168 if (pipe_info->state != 0) {
2169 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2170 dev_err(&dev->udev->dev, "error submitting urb\n");
2171 usb_free_urb(pipe_info->stream_urb);
2174 dprintk(2, "read pipe complete state 0\n");
2179 static int s2255_start_readpipe(struct s2255_dev *dev)
2184 struct s2255_pipeinfo *pipe_info = dev->pipes;
2185 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2186 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2188 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2189 pipe_info->state = 1;
2190 pipe_info->buf_index = (u32) i;
2191 pipe_info->priority_set = 0;
2192 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2193 if (!pipe_info->stream_urb) {
2194 dev_err(&dev->udev->dev,
2195 "ReadStream: Unable to alloc URB");
2198 /* transfer buffer allocated in board_init */
2199 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2201 pipe_info->transfer_buffer,
2202 pipe_info->cur_transfer_size,
2203 read_pipe_completion, pipe_info);
2205 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2206 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2207 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2209 printk(KERN_ERR "s2255: start read pipe failed\n");
2217 /* starts acquisition process */
2218 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2220 unsigned char *buffer;
2222 unsigned long chn_rev;
2224 if (chn >= MAX_CHANNELS) {
2225 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2229 chn_rev = G_chnmap[chn];
2230 dprintk(1, "S2255: start acquire %lu \n", chn);
2232 buffer = kzalloc(512, GFP_KERNEL);
2233 if (buffer == NULL) {
2234 dev_err(&dev->udev->dev, "out of mem\n");
2238 dev->last_frame[chn] = -1;
2239 dev->bad_payload[chn] = 0;
2240 dev->cur_frame[chn] = 0;
2241 for (j = 0; j < SYS_FRAMES; j++) {
2242 dev->buffer[chn].frame[j].ulState = 0;
2243 dev->buffer[chn].frame[j].cur_size = 0;
2246 /* send the start command */
2247 *(u32 *) buffer = IN_DATA_TOKEN;
2248 *((u32 *) buffer + 1) = (u32) chn_rev;
2249 *((u32 *) buffer + 2) = (u32) CMD_START;
2250 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2252 dev_err(&dev->udev->dev, "CMD_START error\n");
2254 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2259 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2261 unsigned char *buffer;
2263 unsigned long chn_rev;
2265 if (chn >= MAX_CHANNELS) {
2266 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2269 chn_rev = G_chnmap[chn];
2271 buffer = kzalloc(512, GFP_KERNEL);
2272 if (buffer == NULL) {
2273 dev_err(&dev->udev->dev, "out of mem\n");
2277 /* send the stop command */
2278 dprintk(4, "stop acquire %lu\n", chn);
2279 *(u32 *) buffer = IN_DATA_TOKEN;
2280 *((u32 *) buffer + 1) = (u32) chn_rev;
2281 *((u32 *) buffer + 2) = CMD_STOP;
2282 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2285 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2287 dprintk(4, "stop acquire: releasing states \n");
2290 dev->b_acquire[chn] = 0;
2295 static void s2255_stop_readpipe(struct s2255_dev *dev)
2300 err("s2255: invalid device");
2303 dprintk(4, "stop read pipe\n");
2304 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2305 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2307 if (pipe_info->state == 0)
2309 pipe_info->state = 0;
2310 pipe_info->prev_state = 1;
2315 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2316 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2317 if (pipe_info->stream_urb) {
2319 usb_kill_urb(pipe_info->stream_urb);
2320 usb_free_urb(pipe_info->stream_urb);
2321 pipe_info->stream_urb = NULL;
2324 dprintk(2, "s2255 stop read pipe: %d\n", j);
2328 static void s2255_fwload_start(struct s2255_dev *dev)
2330 dev->fw_data->fw_size = dev->fw_data->fw->size;
2331 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2332 memcpy(dev->fw_data->pfw_data,
2333 dev->fw_data->fw->data, CHUNK_SIZE);
2334 dev->fw_data->fw_loaded = CHUNK_SIZE;
2335 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2336 usb_sndbulkpipe(dev->udev, 2),
2337 dev->fw_data->pfw_data,
2338 CHUNK_SIZE, s2255_fwchunk_complete,
2340 mod_timer(&dev->timer, jiffies + HZ);
2343 /* standard usb probe function */
2344 static int s2255_probe(struct usb_interface *interface,
2345 const struct usb_device_id *id)
2347 struct s2255_dev *dev = NULL;
2348 struct usb_host_interface *iface_desc;
2349 struct usb_endpoint_descriptor *endpoint;
2351 int retval = -ENOMEM;
2353 dprintk(2, "s2255: probe\n");
2355 /* allocate memory for our device state and initialize it to zero */
2356 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2358 err("s2255: out of memory");
2362 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2366 mutex_init(&dev->lock);
2367 mutex_init(&dev->open_lock);
2369 /* grab usb_device and save it */
2370 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2371 if (dev->udev == NULL) {
2372 dev_err(&interface->dev, "null usb device\n");
2376 kref_init(&dev->kref);
2377 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2378 dev->udev, interface);
2379 dev->interface = interface;
2380 /* set up the endpoint information */
2381 iface_desc = interface->cur_altsetting;
2382 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2383 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2384 endpoint = &iface_desc->endpoint[i].desc;
2385 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2386 /* we found the bulk in endpoint */
2387 dev->read_endpoint = endpoint->bEndpointAddress;
2391 if (!dev->read_endpoint) {
2392 dev_err(&interface->dev, "Could not find bulk-in endpoint");
2397 usb_set_intfdata(interface, dev);
2399 dprintk(100, "after intfdata %p\n", dev);
2401 init_timer(&dev->timer);
2402 dev->timer.function = s2255_timer;
2403 dev->timer.data = (unsigned long)dev->fw_data;
2405 init_waitqueue_head(&dev->fw_data->wait_fw);
2408 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2410 if (!dev->fw_data->fw_urb) {
2411 dev_err(&interface->dev, "out of memory!\n");
2414 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2415 if (!dev->fw_data->pfw_data) {
2416 dev_err(&interface->dev, "out of memory!\n");
2419 /* load the first chunk */
2420 if (request_firmware(&dev->fw_data->fw,
2421 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2422 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2426 /* loads v4l specific */
2427 s2255_probe_v4l(dev);
2428 /* load 2255 board specific */
2429 s2255_board_init(dev);
2431 dprintk(4, "before probe done %p\n", dev);
2432 spin_lock_init(&dev->slock);
2434 s2255_fwload_start(dev);
2435 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2441 /* disconnect routine. when board is removed physically or with rmmod */
2442 static void s2255_disconnect(struct usb_interface *interface)
2444 struct s2255_dev *dev = NULL;
2445 dprintk(1, "s2255: disconnect interface %p\n", interface);
2446 dev = usb_get_intfdata(interface);
2448 kref_put(&dev->kref, s2255_destroy);
2449 dprintk(1, "s2255drv: disconnect\n");
2450 dev_info(&interface->dev, "s2255usb now disconnected\n");
2452 usb_set_intfdata(interface, NULL);
2455 static struct usb_driver s2255_driver = {
2457 .probe = s2255_probe,
2458 .disconnect = s2255_disconnect,
2459 .id_table = s2255_table,
2462 static int __init usb_s2255_init(void)
2466 /* register this driver with the USB subsystem */
2467 result = usb_register(&s2255_driver);
2470 err("usb_register failed. Error number %d", result);
2472 dprintk(2, "s2255_init: done\n");
2476 static void __exit usb_s2255_exit(void)
2478 usb_deregister(&s2255_driver);
2481 module_init(usb_s2255_init);
2482 module_exit(usb_s2255_exit);
2484 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2485 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2486 MODULE_LICENSE("GPL");