2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2008 by Sensoray Company Inc.
7 * Some video buffer code based on vivi driver:
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
16 * Example maximum bandwidth utilization:
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
20 * -full or half size Grey scale: all 4 channels at once
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/module.h>
45 #include <linux/firmware.h>
46 #include <linux/kernel.h>
47 #include <linux/mutex.h>
48 #include <linux/videodev2.h>
49 #include <linux/version.h>
51 #include <media/videobuf-vmalloc.h>
52 #include <media/v4l2-common.h>
53 #include <media/v4l2-ioctl.h>
54 #include <linux/vmalloc.h>
55 #include <linux/usb.h>
57 #define FIRMWARE_FILE_NAME "f2255usb.bin"
61 /* default JPEG quality */
62 #define S2255_DEF_JPEG_QUAL 50
63 /* vendor request in */
65 /* vendor request out */
66 #define S2255_VR_OUT 1
68 #define S2255_VR_FW 0x30
69 /* USB endpoint number for configuring the device */
70 #define S2255_CONFIG_EP 2
71 /* maximum time for DSP to start responding after last FW word loaded(ms) */
72 #define S2255_DSP_BOOTTIME 800
73 /* maximum time to wait for firmware to load (ms) */
74 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
75 #define S2255_DEF_BUFS 16
76 #define S2255_SETMODE_TIMEOUT 500
77 #define MAX_CHANNELS 4
78 #define S2255_MARKER_FRAME 0x2255DA4AL
79 #define S2255_MARKER_RESPONSE 0x2255ACACL
80 #define S2255_RESPONSE_SETMODE 0x01
81 #define S2255_RESPONSE_FW 0x10
82 #define S2255_USB_XFER_SIZE (16 * 1024)
83 #define MAX_CHANNELS 4
84 #define MAX_PIPE_BUFFERS 1
86 /* maximum size is PAL full size plus room for the marker header(s) */
87 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
88 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
89 #define LINE_SZ_4CIFS_NTSC 640
90 #define LINE_SZ_2CIFS_NTSC 640
91 #define LINE_SZ_1CIFS_NTSC 320
92 #define LINE_SZ_4CIFS_PAL 704
93 #define LINE_SZ_2CIFS_PAL 704
94 #define LINE_SZ_1CIFS_PAL 352
95 #define NUM_LINES_4CIFS_NTSC 240
96 #define NUM_LINES_2CIFS_NTSC 240
97 #define NUM_LINES_1CIFS_NTSC 240
98 #define NUM_LINES_4CIFS_PAL 288
99 #define NUM_LINES_2CIFS_PAL 288
100 #define NUM_LINES_1CIFS_PAL 288
101 #define LINE_SZ_DEF 640
102 #define NUM_LINES_DEF 240
105 /* predefined settings */
106 #define FORMAT_NTSC 1
109 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
110 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
111 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
112 /* SCALE_4CIFSI is the 2 fields interpolated into one */
113 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
115 #define COLOR_YUVPL 1 /* YUV planar */
116 #define COLOR_YUVPK 2 /* YUV packed */
117 #define COLOR_Y8 4 /* monochrome */
118 #define COLOR_JPG 5 /* JPEG */
119 #define MASK_COLOR 0xff
120 #define MASK_JPG_QUALITY 0xff00
122 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
123 #define FDEC_1 1 /* capture every frame. default */
124 #define FDEC_2 2 /* capture every 2nd frame */
125 #define FDEC_3 3 /* capture every 3rd frame */
126 #define FDEC_5 5 /* capture every 5th frame */
128 /*-------------------------------------------------------
129 * Default mode parameters.
130 *-------------------------------------------------------*/
131 #define DEF_SCALE SCALE_4CIFS
132 #define DEF_COLOR COLOR_YUVPL
133 #define DEF_FDEC FDEC_1
135 #define DEF_CONTRAST 0x5c
136 #define DEF_SATURATION 0x80
139 /* usb config commands */
140 #define IN_DATA_TOKEN 0x2255c0de
141 #define CMD_2255 0xc2255000
142 #define CMD_SET_MODE (CMD_2255 | 0x10)
143 #define CMD_START (CMD_2255 | 0x20)
144 #define CMD_STOP (CMD_2255 | 0x30)
145 #define CMD_STATUS (CMD_2255 | 0x40)
148 u32 format; /* input video format (NTSC, PAL) */
149 u32 scale; /* output video scale */
150 u32 color; /* output video color format */
151 u32 fdec; /* frame decimation */
152 u32 bright; /* brightness */
153 u32 contrast; /* contrast */
154 u32 saturation; /* saturation */
155 u32 hue; /* hue (NTSC only)*/
156 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
157 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
158 u32 restart; /* if DSP requires restart */
162 #define S2255_READ_IDLE 0
163 #define S2255_READ_FRAME 1
165 /* frame structure */
166 struct s2255_framei {
168 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
169 void *lpvbits; /* image data */
170 unsigned long cur_size; /* current data copied to it */
173 /* image buffer structure */
174 struct s2255_bufferi {
175 unsigned long dwFrames; /* number of frames in buffer */
176 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
179 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
180 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
181 DEF_HUE, 0, DEF_USB_BLOCK, 0}
183 struct s2255_dmaqueue {
184 struct list_head active;
185 struct s2255_dev *dev;
189 /* for firmware loading, fw_state */
190 #define S2255_FW_NOTLOADED 0
191 #define S2255_FW_LOADED_DSPWAIT 1
192 #define S2255_FW_SUCCESS 2
193 #define S2255_FW_FAILED 3
194 #define S2255_FW_DISCONNECTING 4
196 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
197 /* 2255 read states */
198 #define S2255_READ_IDLE 0
199 #define S2255_READ_FRAME 1
206 wait_queue_head_t wait_fw;
207 const struct firmware *fw;
210 struct s2255_pipeinfo {
211 u32 max_transfer_size;
212 u32 cur_transfer_size;
216 void *dev; /* back pointer to s2255_dev struct*/
221 struct s2255_fmt; /*forward declaration */
225 int users[MAX_CHANNELS];
227 struct mutex open_lock;
228 int resources[MAX_CHANNELS];
229 struct usb_device *udev;
230 struct usb_interface *interface;
233 struct s2255_dmaqueue vidq[MAX_CHANNELS];
234 struct video_device *vdev[MAX_CHANNELS];
235 struct list_head s2255_devlist;
236 struct timer_list timer;
237 struct s2255_fw *fw_data;
238 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
239 struct s2255_bufferi buffer[MAX_CHANNELS];
240 struct s2255_mode mode[MAX_CHANNELS];
241 /* jpeg compression */
242 struct v4l2_jpegcompression jc[MAX_CHANNELS];
243 /* capture parameters (for high quality mode full size) */
244 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
245 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
246 int cur_frame[MAX_CHANNELS];
247 int last_frame[MAX_CHANNELS];
248 u32 cc; /* current channel */
249 int b_acquire[MAX_CHANNELS];
250 /* allocated image size */
251 unsigned long req_image_size[MAX_CHANNELS];
252 /* received packet size */
253 unsigned long pkt_size[MAX_CHANNELS];
254 int bad_payload[MAX_CHANNELS];
255 unsigned long frame_count[MAX_CHANNELS];
258 int jpg_size[MAX_CHANNELS];
259 /* if channel configured to default state */
260 int chn_configured[MAX_CHANNELS];
261 wait_queue_head_t wait_setmode[MAX_CHANNELS];
262 int setmode_ready[MAX_CHANNELS];
267 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
275 /* buffer for one video frame */
276 struct s2255_buffer {
277 /* common v4l buffer stuff -- must be first */
278 struct videobuf_buffer vb;
279 const struct s2255_fmt *fmt;
283 struct s2255_dev *dev;
284 const struct s2255_fmt *fmt;
287 struct videobuf_queue vb_vidq;
288 enum v4l2_buf_type type;
290 /* mode below is the desired mode.
291 mode in s2255_dev is the current mode that was last set */
292 struct s2255_mode mode;
293 int resources[MAX_CHANNELS];
296 /* current cypress EEPROM firmware version */
297 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
298 #define S2255_MAJOR_VERSION 1
299 #define S2255_MINOR_VERSION 14
300 #define S2255_RELEASE 0
301 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
302 S2255_MINOR_VERSION, \
306 #define USB_S2255_VENDOR_ID 0x1943
307 #define USB_S2255_PRODUCT_ID 0x2255
308 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
309 /* frame prefix size (sent once every frame) */
310 #define PREFIX_SIZE 512
312 /* Channels on box are in reverse order */
313 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
315 static LIST_HEAD(s2255_devlist);
318 static int *s2255_debug = &debug;
320 static int s2255_start_readpipe(struct s2255_dev *dev);
321 static void s2255_stop_readpipe(struct s2255_dev *dev);
322 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
323 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
324 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
325 int chn, int jpgsize);
326 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
327 struct s2255_mode *mode);
328 static int s2255_board_shutdown(struct s2255_dev *dev);
329 static void s2255_exit_v4l(struct s2255_dev *dev);
330 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
331 static void s2255_destroy(struct kref *kref);
332 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
333 u16 index, u16 value, void *buf,
334 s32 buf_len, int bOut);
336 /* dev_err macro with driver name */
337 #define S2255_DRIVER_NAME "s2255"
338 #define s2255_dev_err(dev, fmt, arg...) \
339 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
341 #define dprintk(level, fmt, arg...) \
343 if (*s2255_debug >= (level)) { \
344 printk(KERN_DEBUG S2255_DRIVER_NAME \
349 static struct usb_driver s2255_driver;
352 /* Declare static vars that will be used as parameters */
353 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
355 /* start video number */
356 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
358 module_param(debug, int, 0644);
359 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
360 module_param(vid_limit, int, 0644);
361 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
362 module_param(video_nr, int, 0644);
363 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
365 /* USB device table */
366 static struct usb_device_id s2255_table[] = {
367 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
368 { } /* Terminating entry */
370 MODULE_DEVICE_TABLE(usb, s2255_table);
373 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
375 /* supported controls */
376 static struct v4l2_queryctrl s2255_qctrl[] = {
378 .id = V4L2_CID_BRIGHTNESS,
379 .type = V4L2_CTRL_TYPE_INTEGER,
380 .name = "Brightness",
387 .id = V4L2_CID_CONTRAST,
388 .type = V4L2_CTRL_TYPE_INTEGER,
393 .default_value = DEF_CONTRAST,
396 .id = V4L2_CID_SATURATION,
397 .type = V4L2_CTRL_TYPE_INTEGER,
398 .name = "Saturation",
402 .default_value = DEF_SATURATION,
406 .type = V4L2_CTRL_TYPE_INTEGER,
411 .default_value = DEF_HUE,
416 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
419 static const struct s2255_fmt formats[] = {
421 .name = "4:2:2, planar, YUV422P",
422 .fourcc = V4L2_PIX_FMT_YUV422P,
426 .name = "4:2:2, packed, YUYV",
427 .fourcc = V4L2_PIX_FMT_YUYV,
431 .name = "4:2:2, packed, UYVY",
432 .fourcc = V4L2_PIX_FMT_UYVY,
436 .fourcc = V4L2_PIX_FMT_JPEG,
440 .fourcc = V4L2_PIX_FMT_GREY,
445 static int norm_maxw(struct video_device *vdev)
447 return (vdev->current_norm & V4L2_STD_NTSC) ?
448 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
451 static int norm_maxh(struct video_device *vdev)
453 return (vdev->current_norm & V4L2_STD_NTSC) ?
454 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
457 static int norm_minw(struct video_device *vdev)
459 return (vdev->current_norm & V4L2_STD_NTSC) ?
460 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
463 static int norm_minh(struct video_device *vdev)
465 return (vdev->current_norm & V4L2_STD_NTSC) ?
466 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
471 * TODO: fixme: move YUV reordering to hardware
472 * converts 2255 planar format to yuyv or uyvy
474 static void planar422p_to_yuv_packed(const unsigned char *in,
476 int width, int height,
482 unsigned long size = height * width;
484 pY = (unsigned char *)in;
485 pCr = (unsigned char *)in + height * width;
486 pCb = (unsigned char *)in + height * width + (height * width / 2);
487 for (i = 0; i < size * 2; i += 4) {
488 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
489 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
490 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
491 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
496 static void s2255_reset_dsppower(struct s2255_dev *dev)
498 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
500 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
504 /* kickstarts the firmware loading. from probe
506 static void s2255_timer(unsigned long user_data)
508 struct s2255_fw *data = (struct s2255_fw *)user_data;
509 dprintk(100, "s2255 timer\n");
510 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
511 printk(KERN_ERR "s2255: can't submit urb\n");
512 atomic_set(&data->fw_state, S2255_FW_FAILED);
513 /* wake up anything waiting for the firmware */
514 wake_up(&data->wait_fw);
520 /* this loads the firmware asynchronously.
521 Originally this was done synchroously in probe.
522 But it is better to load it asynchronously here than block
523 inside the probe function. Blocking inside probe affects boot time.
524 FW loading is triggered by the timer in the probe function
526 static void s2255_fwchunk_complete(struct urb *urb)
528 struct s2255_fw *data = urb->context;
529 struct usb_device *udev = urb->dev;
531 dprintk(100, "udev %p urb %p", udev, urb);
533 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
534 atomic_set(&data->fw_state, S2255_FW_FAILED);
535 /* wake up anything waiting for the firmware */
536 wake_up(&data->wait_fw);
539 if (data->fw_urb == NULL) {
540 s2255_dev_err(&udev->dev, "disconnected\n");
541 atomic_set(&data->fw_state, S2255_FW_FAILED);
542 /* wake up anything waiting for the firmware */
543 wake_up(&data->wait_fw);
546 #define CHUNK_SIZE 512
547 /* all USB transfers must be done with continuous kernel memory.
548 can't allocate more than 128k in current linux kernel, so
549 upload the firmware in chunks
551 if (data->fw_loaded < data->fw_size) {
552 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
553 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
555 if (len < CHUNK_SIZE)
556 memset(data->pfw_data, 0, CHUNK_SIZE);
558 dprintk(100, "completed len %d, loaded %d \n", len,
561 memcpy(data->pfw_data,
562 (char *) data->fw->data + data->fw_loaded, len);
564 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
565 data->pfw_data, CHUNK_SIZE,
566 s2255_fwchunk_complete, data);
567 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
568 dev_err(&udev->dev, "failed submit URB\n");
569 atomic_set(&data->fw_state, S2255_FW_FAILED);
570 /* wake up anything waiting for the firmware */
571 wake_up(&data->wait_fw);
574 data->fw_loaded += len;
576 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
578 dprintk(100, "2255 complete done\n");
583 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
585 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
586 struct s2255_buffer *buf;
587 unsigned long flags = 0;
589 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
590 spin_lock_irqsave(&dev->slock, flags);
592 if (list_empty(&dma_q->active)) {
593 dprintk(1, "No active queue to serve\n");
597 buf = list_entry(dma_q->active.next,
598 struct s2255_buffer, vb.queue);
600 if (!waitqueue_active(&buf->vb.done)) {
605 list_del(&buf->vb.queue);
606 do_gettimeofday(&buf->vb.ts);
607 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
608 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
609 wake_up(&buf->vb.done);
610 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
612 spin_unlock_irqrestore(&dev->slock, flags);
617 static const struct s2255_fmt *format_by_fourcc(int fourcc)
621 for (i = 0; i < ARRAY_SIZE(formats); i++) {
622 if (-1 == formats[i].fourcc)
624 if (formats[i].fourcc == fourcc)
633 /* video buffer vmalloc implementation based partly on VIVI driver which is
634 * Copyright (c) 2006 by
635 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
636 * Ted Walther <ted--a.t--enumera.com>
637 * John Sokol <sokol--a.t--videotechnology.com>
638 * http://v4l.videotechnology.com/
641 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
642 int chn, int jpgsize)
647 char *vbuf = videobuf_to_vmalloc(&buf->vb);
648 unsigned long last_frame;
649 struct s2255_framei *frm;
654 last_frame = dev->last_frame[chn];
655 if (last_frame != -1) {
656 frm = &dev->buffer[chn].frame[last_frame];
658 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
659 switch (buf->fmt->fourcc) {
660 case V4L2_PIX_FMT_YUYV:
661 case V4L2_PIX_FMT_UYVY:
662 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
667 case V4L2_PIX_FMT_GREY:
668 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
670 case V4L2_PIX_FMT_JPEG:
671 buf->vb.size = jpgsize;
672 memcpy(vbuf, tmpbuf, buf->vb.size);
674 case V4L2_PIX_FMT_YUV422P:
676 buf->vb.width * buf->vb.height * 2);
679 printk(KERN_DEBUG "s2255: unknown format?\n");
681 dev->last_frame[chn] = -1;
683 printk(KERN_ERR "s2255: =======no frame\n");
687 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
688 (unsigned long)vbuf, pos);
689 /* tell v4l buffer was filled */
691 buf->vb.field_count = dev->frame_count[chn] * 2;
692 do_gettimeofday(&ts);
694 buf->vb.state = VIDEOBUF_DONE;
698 /* ------------------------------------------------------------------
700 ------------------------------------------------------------------*/
702 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
705 struct s2255_fh *fh = vq->priv_data;
707 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
710 *count = S2255_DEF_BUFS;
712 while (*size * (*count) > vid_limit * 1024 * 1024)
718 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
720 dprintk(4, "%s\n", __func__);
722 videobuf_vmalloc_free(&buf->vb);
723 buf->vb.state = VIDEOBUF_NEEDS_INIT;
726 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
727 enum v4l2_field field)
729 struct s2255_fh *fh = vq->priv_data;
730 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
732 dprintk(4, "%s, field=%d\n", __func__, field);
736 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
737 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
738 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
739 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
740 dprintk(4, "invalid buffer prepare\n");
744 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
746 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
747 dprintk(4, "invalid buffer prepare\n");
752 buf->vb.width = fh->width;
753 buf->vb.height = fh->height;
754 buf->vb.field = field;
757 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
758 rc = videobuf_iolock(vq, &buf->vb, NULL);
763 buf->vb.state = VIDEOBUF_PREPARED;
766 free_buffer(vq, buf);
770 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
772 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
773 struct s2255_fh *fh = vq->priv_data;
774 struct s2255_dev *dev = fh->dev;
775 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
777 dprintk(1, "%s\n", __func__);
779 buf->vb.state = VIDEOBUF_QUEUED;
780 list_add_tail(&buf->vb.queue, &vidq->active);
783 static void buffer_release(struct videobuf_queue *vq,
784 struct videobuf_buffer *vb)
786 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
787 struct s2255_fh *fh = vq->priv_data;
788 dprintk(4, "%s %d\n", __func__, fh->channel);
789 free_buffer(vq, buf);
792 static struct videobuf_queue_ops s2255_video_qops = {
793 .buf_setup = buffer_setup,
794 .buf_prepare = buffer_prepare,
795 .buf_queue = buffer_queue,
796 .buf_release = buffer_release,
800 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
803 mutex_lock(&dev->lock);
804 if (dev->resources[fh->channel]) {
805 /* no, someone else uses it */
806 mutex_unlock(&dev->lock);
809 /* it's free, grab it */
810 dev->resources[fh->channel] = 1;
811 fh->resources[fh->channel] = 1;
812 dprintk(1, "s2255: res: get\n");
813 mutex_unlock(&dev->lock);
817 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
819 return dev->resources[fh->channel];
822 static int res_check(struct s2255_fh *fh)
824 return fh->resources[fh->channel];
828 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
830 mutex_lock(&dev->lock);
831 dev->resources[fh->channel] = 0;
832 fh->resources[fh->channel] = 0;
833 mutex_unlock(&dev->lock);
834 dprintk(1, "res: put\n");
838 static int vidioc_querycap(struct file *file, void *priv,
839 struct v4l2_capability *cap)
841 struct s2255_fh *fh = file->private_data;
842 struct s2255_dev *dev = fh->dev;
843 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
844 strlcpy(cap->card, "s2255", sizeof(cap->card));
845 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
846 cap->version = S2255_VERSION;
847 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
851 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
852 struct v4l2_fmtdesc *f)
858 if (index >= ARRAY_SIZE(formats))
861 dprintk(4, "name %s\n", formats[index].name);
862 strlcpy(f->description, formats[index].name, sizeof(f->description));
863 f->pixelformat = formats[index].fourcc;
867 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
868 struct v4l2_format *f)
870 struct s2255_fh *fh = priv;
872 f->fmt.pix.width = fh->width;
873 f->fmt.pix.height = fh->height;
874 f->fmt.pix.field = fh->vb_vidq.field;
875 f->fmt.pix.pixelformat = fh->fmt->fourcc;
876 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
877 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
881 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
882 struct v4l2_format *f)
884 const struct s2255_fmt *fmt;
885 enum v4l2_field field;
887 struct s2255_fh *fh = priv;
888 struct s2255_dev *dev = fh->dev;
892 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
894 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
899 field = f->fmt.pix.field;
900 if (field == V4L2_FIELD_ANY)
903 dprintk(4, "try format %d \n", is_ntsc);
904 /* supports 3 sizes. see s2255drv.h */
905 dprintk(50, "width test %d, height %d\n",
906 f->fmt.pix.width, f->fmt.pix.height);
909 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
910 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
912 field = V4L2_FIELD_SEQ_TB;
913 } else if (!((field == V4L2_FIELD_INTERLACED) ||
914 (field == V4L2_FIELD_SEQ_TB) ||
915 (field == V4L2_FIELD_INTERLACED_TB))) {
916 dprintk(1, "unsupported field setting\n");
920 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
922 field = V4L2_FIELD_TOP;
923 } else if (!((field == V4L2_FIELD_TOP) ||
924 (field == V4L2_FIELD_BOTTOM))) {
925 dprintk(1, "unsupported field setting\n");
930 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
931 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
932 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
933 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
934 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
935 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
937 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
940 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
941 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
943 field = V4L2_FIELD_SEQ_TB;
944 } else if (!((field == V4L2_FIELD_INTERLACED) ||
945 (field == V4L2_FIELD_SEQ_TB) ||
946 (field == V4L2_FIELD_INTERLACED_TB))) {
947 dprintk(1, "unsupported field setting\n");
951 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
953 field = V4L2_FIELD_TOP;
954 } else if (!((field == V4L2_FIELD_TOP) ||
955 (field == V4L2_FIELD_BOTTOM))) {
956 dprintk(1, "unsupported field setting\n");
960 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
961 dprintk(50, "pal 704\n");
962 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
963 field = V4L2_FIELD_SEQ_TB;
964 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
965 dprintk(50, "pal 352A\n");
966 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
967 field = V4L2_FIELD_TOP;
968 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
969 dprintk(50, "pal 352B\n");
970 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
971 field = V4L2_FIELD_TOP;
973 dprintk(50, "pal 352C\n");
974 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
975 field = V4L2_FIELD_TOP;
979 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
980 f->fmt.pix.height, f->fmt.pix.field);
981 f->fmt.pix.field = field;
982 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
983 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
987 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
988 struct v4l2_format *f)
990 struct s2255_fh *fh = priv;
991 const struct s2255_fmt *fmt;
992 struct videobuf_queue *q = &fh->vb_vidq;
996 ret = vidioc_try_fmt_vid_cap(file, fh, f);
1001 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1006 mutex_lock(&q->vb_lock);
1008 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1009 dprintk(1, "queue busy\n");
1014 if (res_locked(fh->dev, fh)) {
1015 dprintk(1, "can't change format after started\n");
1021 fh->width = f->fmt.pix.width;
1022 fh->height = f->fmt.pix.height;
1023 fh->vb_vidq.field = f->fmt.pix.field;
1025 norm = norm_minw(fh->dev->vdev[fh->channel]);
1026 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1027 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1028 if (fh->dev->cap_parm[fh->channel].capturemode &
1029 V4L2_MODE_HIGHQUALITY) {
1030 fh->mode.scale = SCALE_4CIFSI;
1031 dprintk(2, "scale 4CIFSI\n");
1033 fh->mode.scale = SCALE_4CIFS;
1034 dprintk(2, "scale 4CIFS\n");
1037 fh->mode.scale = SCALE_2CIFS;
1040 fh->mode.scale = SCALE_1CIFS;
1044 switch (fh->fmt->fourcc) {
1045 case V4L2_PIX_FMT_GREY:
1046 fh->mode.color = COLOR_Y8;
1048 case V4L2_PIX_FMT_JPEG:
1049 fh->mode.color = COLOR_JPG |
1050 (fh->dev->jc[fh->channel].quality << 8);
1052 case V4L2_PIX_FMT_YUV422P:
1053 fh->mode.color = COLOR_YUVPL;
1055 case V4L2_PIX_FMT_YUYV:
1056 case V4L2_PIX_FMT_UYVY:
1058 fh->mode.color = COLOR_YUVPK;
1063 mutex_unlock(&q->vb_lock);
1067 static int vidioc_reqbufs(struct file *file, void *priv,
1068 struct v4l2_requestbuffers *p)
1071 struct s2255_fh *fh = priv;
1072 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1076 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1079 struct s2255_fh *fh = priv;
1080 rc = videobuf_querybuf(&fh->vb_vidq, p);
1084 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1087 struct s2255_fh *fh = priv;
1088 rc = videobuf_qbuf(&fh->vb_vidq, p);
1092 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1095 struct s2255_fh *fh = priv;
1096 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1100 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1101 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1103 struct s2255_fh *fh = priv;
1105 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1109 /* write to the configuration pipe, synchronously */
1110 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1117 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1118 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1123 static u32 get_transfer_size(struct s2255_mode *mode)
1125 int linesPerFrame = LINE_SZ_DEF;
1126 int pixelsPerLine = NUM_LINES_DEF;
1129 unsigned int mask_mult;
1134 if (mode->format == FORMAT_NTSC) {
1135 switch (mode->scale) {
1138 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1139 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1142 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1143 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1146 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1147 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1152 } else if (mode->format == FORMAT_PAL) {
1153 switch (mode->scale) {
1156 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1157 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1160 linesPerFrame = NUM_LINES_2CIFS_PAL;
1161 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1164 linesPerFrame = NUM_LINES_1CIFS_PAL;
1165 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1171 outImageSize = linesPerFrame * pixelsPerLine;
1172 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1173 /* 2 bytes/pixel if not monochrome */
1177 /* total bytes to send including prefix and 4K padding;
1178 must be a multiple of USB_READ_SIZE */
1179 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1180 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1181 /* if size not a multiple of USB_READ_SIZE */
1182 if (usbInSize & ~mask_mult)
1183 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1187 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1189 struct device *dev = &sdev->udev->dev;
1190 dev_info(dev, "------------------------------------------------\n");
1191 dev_info(dev, "verify mode\n");
1192 dev_info(dev, "format: %d\n", mode->format);
1193 dev_info(dev, "scale: %d\n", mode->scale);
1194 dev_info(dev, "fdec: %d\n", mode->fdec);
1195 dev_info(dev, "color: %d\n", mode->color);
1196 dev_info(dev, "bright: 0x%x\n", mode->bright);
1197 dev_info(dev, "restart: 0x%x\n", mode->restart);
1198 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1199 dev_info(dev, "single: 0x%x\n", mode->single);
1200 dev_info(dev, "------------------------------------------------\n");
1204 * set mode is the function which controls the DSP.
1205 * the restart parameter in struct s2255_mode should be set whenever
1206 * the image size could change via color format, video system or image
1208 * When the restart parameter is set, we sleep for ONE frame to allow the
1209 * DSP time to get the new frame
1211 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1212 struct s2255_mode *mode)
1216 unsigned long chn_rev;
1218 mutex_lock(&dev->lock);
1219 chn_rev = G_chnmap[chn];
1220 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1221 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1222 dev->mode[chn].scale);
1223 dprintk(2, "mode contrast %x\n", mode->contrast);
1225 /* if JPEG, set the quality */
1226 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1227 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1230 dev->mode[chn] = *mode;
1231 dev->req_image_size[chn] = get_transfer_size(mode);
1232 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1234 buffer = kzalloc(512, GFP_KERNEL);
1235 if (buffer == NULL) {
1236 dev_err(&dev->udev->dev, "out of mem\n");
1237 mutex_unlock(&dev->lock);
1242 buffer[0] = IN_DATA_TOKEN;
1243 buffer[1] = (u32) chn_rev;
1244 buffer[2] = CMD_SET_MODE;
1245 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1246 dev->setmode_ready[chn] = 0;
1247 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1249 dump_verify_mode(dev, mode);
1251 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1253 /* wait at least 3 frames before continuing */
1254 if (mode->restart) {
1255 wait_event_timeout(dev->wait_setmode[chn],
1256 (dev->setmode_ready[chn] != 0),
1257 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1258 if (dev->setmode_ready[chn] != 1) {
1259 printk(KERN_DEBUG "s2255: no set mode response\n");
1264 /* clear the restart flag */
1265 dev->mode[chn].restart = 0;
1266 mutex_unlock(&dev->lock);
1270 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1273 struct s2255_fh *fh = priv;
1274 struct s2255_dev *dev = fh->dev;
1275 struct s2255_mode *new_mode;
1276 struct s2255_mode *old_mode;
1279 dprintk(4, "%s\n", __func__);
1280 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1281 dev_err(&dev->udev->dev, "invalid fh type0\n");
1284 if (i != fh->type) {
1285 dev_err(&dev->udev->dev, "invalid fh type1\n");
1289 if (!res_get(dev, fh)) {
1290 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1294 /* send a set mode command everytime with restart.
1295 in case we switch resolutions or other parameters */
1297 new_mode = &fh->mode;
1298 old_mode = &fh->dev->mode[chn];
1300 if (new_mode->color != old_mode->color)
1301 new_mode->restart = 1;
1302 else if (new_mode->scale != old_mode->scale)
1303 new_mode->restart = 1;
1304 else if (new_mode->format != old_mode->format)
1305 new_mode->restart = 1;
1307 s2255_set_mode(dev, chn, new_mode);
1308 new_mode->restart = 0;
1309 *old_mode = *new_mode;
1310 dev->cur_fmt[chn] = fh->fmt;
1311 dprintk(1, "%s[%d]\n", __func__, chn);
1312 dev->last_frame[chn] = -1;
1313 dev->bad_payload[chn] = 0;
1314 dev->cur_frame[chn] = 0;
1315 dev->frame_count[chn] = 0;
1316 for (j = 0; j < SYS_FRAMES; j++) {
1317 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1318 dev->buffer[chn].frame[j].cur_size = 0;
1320 res = videobuf_streamon(&fh->vb_vidq);
1322 s2255_start_acquire(dev, chn);
1323 dev->b_acquire[chn] = 1;
1330 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1332 struct s2255_fh *fh = priv;
1333 struct s2255_dev *dev = fh->dev;
1335 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1336 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1337 printk(KERN_ERR "invalid fh type0\n");
1340 if (i != fh->type) {
1341 printk(KERN_ERR "invalid type i\n");
1344 s2255_stop_acquire(dev, fh->channel);
1345 videobuf_streamoff(&fh->vb_vidq);
1350 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1352 struct s2255_fh *fh = priv;
1353 struct s2255_mode *mode;
1354 struct videobuf_queue *q = &fh->vb_vidq;
1357 mutex_lock(&q->vb_lock);
1358 if (videobuf_queue_is_busy(q)) {
1359 dprintk(1, "queue busy\n");
1364 if (res_locked(fh->dev, fh)) {
1365 dprintk(1, "can't change standard after started\n");
1371 if (*i & V4L2_STD_NTSC) {
1372 dprintk(4, "vidioc_s_std NTSC\n");
1373 mode->format = FORMAT_NTSC;
1374 } else if (*i & V4L2_STD_PAL) {
1375 dprintk(4, "vidioc_s_std PAL\n");
1376 mode->format = FORMAT_PAL;
1381 mutex_unlock(&q->vb_lock);
1385 /* Sensoray 2255 is a multiple channel capture device.
1386 It does not have a "crossbar" of inputs.
1387 We use one V4L device per channel. The user must
1388 be aware that certain combinations are not allowed.
1389 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1390 at once in color(you can do full fps on 4 channels with greyscale.
1392 static int vidioc_enum_input(struct file *file, void *priv,
1393 struct v4l2_input *inp)
1395 if (inp->index != 0)
1398 inp->type = V4L2_INPUT_TYPE_CAMERA;
1399 inp->std = S2255_NORMS;
1400 strlcpy(inp->name, "Camera", sizeof(inp->name));
1404 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1409 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1416 /* --- controls ---------------------------------------------- */
1417 static int vidioc_queryctrl(struct file *file, void *priv,
1418 struct v4l2_queryctrl *qc)
1422 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1423 if (qc->id && qc->id == s2255_qctrl[i].id) {
1424 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1428 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1432 static int vidioc_g_ctrl(struct file *file, void *priv,
1433 struct v4l2_control *ctrl)
1437 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1438 if (ctrl->id == s2255_qctrl[i].id) {
1439 ctrl->value = qctl_regs[i];
1442 dprintk(4, "g_ctrl -EINVAL\n");
1447 static int vidioc_s_ctrl(struct file *file, void *priv,
1448 struct v4l2_control *ctrl)
1451 struct s2255_fh *fh = priv;
1452 struct s2255_dev *dev = fh->dev;
1453 struct s2255_mode *mode;
1455 dprintk(4, "vidioc_s_ctrl\n");
1456 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1457 if (ctrl->id == s2255_qctrl[i].id) {
1458 if (ctrl->value < s2255_qctrl[i].minimum ||
1459 ctrl->value > s2255_qctrl[i].maximum)
1462 qctl_regs[i] = ctrl->value;
1463 /* update the mode to the corresponding value */
1465 case V4L2_CID_BRIGHTNESS:
1466 mode->bright = ctrl->value;
1468 case V4L2_CID_CONTRAST:
1469 mode->contrast = ctrl->value;
1472 mode->hue = ctrl->value;
1474 case V4L2_CID_SATURATION:
1475 mode->saturation = ctrl->value;
1479 /* set mode here. Note: stream does not need restarted.
1480 some V4L programs restart stream unnecessarily
1483 s2255_set_mode(dev, fh->channel, mode);
1490 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1491 struct v4l2_jpegcompression *jc)
1493 struct s2255_fh *fh = priv;
1494 struct s2255_dev *dev = fh->dev;
1495 *jc = dev->jc[fh->channel];
1496 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1500 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1501 struct v4l2_jpegcompression *jc)
1503 struct s2255_fh *fh = priv;
1504 struct s2255_dev *dev = fh->dev;
1505 if (jc->quality < 0 || jc->quality > 100)
1507 dev->jc[fh->channel].quality = jc->quality;
1508 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1512 static int vidioc_g_parm(struct file *file, void *priv,
1513 struct v4l2_streamparm *sp)
1515 struct s2255_fh *fh = priv;
1516 struct s2255_dev *dev = fh->dev;
1517 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1519 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1520 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1524 static int vidioc_s_parm(struct file *file, void *priv,
1525 struct v4l2_streamparm *sp)
1527 struct s2255_fh *fh = priv;
1528 struct s2255_dev *dev = fh->dev;
1530 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1533 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1534 dprintk(2, "setting param capture mode %d\n",
1535 sp->parm.capture.capturemode);
1538 static int s2255_open(struct file *file)
1540 int minor = video_devdata(file)->minor;
1541 struct s2255_dev *h, *dev = NULL;
1542 struct s2255_fh *fh;
1543 struct list_head *list;
1544 enum v4l2_buf_type type = 0;
1546 int cur_channel = -1;
1548 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1551 list_for_each(list, &s2255_devlist) {
1552 h = list_entry(list, struct s2255_dev, s2255_devlist);
1553 for (i = 0; i < MAX_CHANNELS; i++) {
1554 if (h->vdev[i]->minor == minor) {
1557 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1562 if ((NULL == dev) || (cur_channel == -1)) {
1564 printk(KERN_INFO "s2255: openv4l no dev\n");
1568 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1570 printk(KERN_INFO "disconnecting\n");
1573 kref_get(&dev->kref);
1574 mutex_lock(&dev->open_lock);
1576 dev->users[cur_channel]++;
1577 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1579 switch (atomic_read(&dev->fw_data->fw_state)) {
1580 case S2255_FW_FAILED:
1581 s2255_dev_err(&dev->udev->dev,
1582 "firmware load failed. retrying.\n");
1583 s2255_fwload_start(dev, 1);
1584 wait_event_timeout(dev->fw_data->wait_fw,
1585 ((atomic_read(&dev->fw_data->fw_state)
1586 == S2255_FW_SUCCESS) ||
1587 (atomic_read(&dev->fw_data->fw_state)
1588 == S2255_FW_DISCONNECTING)),
1589 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1591 case S2255_FW_NOTLOADED:
1592 case S2255_FW_LOADED_DSPWAIT:
1593 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1594 driver loaded and then device immediately opened */
1595 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1596 wait_event_timeout(dev->fw_data->wait_fw,
1597 ((atomic_read(&dev->fw_data->fw_state)
1598 == S2255_FW_SUCCESS) ||
1599 (atomic_read(&dev->fw_data->fw_state)
1600 == S2255_FW_DISCONNECTING)),
1601 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1603 case S2255_FW_SUCCESS:
1607 state = atomic_read(&dev->fw_data->fw_state);
1608 if (state != S2255_FW_SUCCESS) {
1611 case S2255_FW_FAILED:
1612 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1615 case S2255_FW_DISCONNECTING:
1616 printk(KERN_INFO "%s: disconnecting\n", __func__);
1619 case S2255_FW_LOADED_DSPWAIT:
1620 case S2255_FW_NOTLOADED:
1621 printk(KERN_INFO "%s: firmware not loaded yet"
1622 "please try again later\n",
1627 printk(KERN_INFO "%s: unknown state\n", __func__);
1631 dev->users[cur_channel]--;
1632 mutex_unlock(&dev->open_lock);
1633 kref_put(&dev->kref, s2255_destroy);
1638 /* allocate + initialize per filehandle data */
1639 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1641 dev->users[cur_channel]--;
1642 mutex_unlock(&dev->open_lock);
1643 kref_put(&dev->kref, s2255_destroy);
1648 file->private_data = fh;
1650 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1651 fh->mode = dev->mode[cur_channel];
1652 fh->fmt = dev->cur_fmt[cur_channel];
1653 /* default 4CIF NTSC */
1654 fh->width = LINE_SZ_4CIFS_NTSC;
1655 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1656 fh->channel = cur_channel;
1658 /* configure channel to default state */
1659 if (!dev->chn_configured[cur_channel]) {
1660 s2255_set_mode(dev, cur_channel, &fh->mode);
1661 dev->chn_configured[cur_channel] = 1;
1665 /* Put all controls at a sane state */
1666 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1667 qctl_regs[i] = s2255_qctrl[i].default_value;
1669 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1670 minor, v4l2_type_names[type], dev->users[cur_channel]);
1671 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1672 (unsigned long)fh, (unsigned long)dev,
1673 (unsigned long)&dev->vidq[cur_channel]);
1674 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1675 list_empty(&dev->vidq[cur_channel].active));
1677 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1680 V4L2_FIELD_INTERLACED,
1681 sizeof(struct s2255_buffer), fh);
1683 mutex_unlock(&dev->open_lock);
1689 static unsigned int s2255_poll(struct file *file,
1690 struct poll_table_struct *wait)
1692 struct s2255_fh *fh = file->private_data;
1694 dprintk(100, "%s\n", __func__);
1696 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1699 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1703 static void s2255_destroy(struct kref *kref)
1705 struct s2255_dev *dev = to_s2255_dev(kref);
1706 struct list_head *list;
1709 printk(KERN_ERR "s2255drv: kref problem\n");
1712 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1713 wake_up(&dev->fw_data->wait_fw);
1714 for (i = 0; i < MAX_CHANNELS; i++) {
1715 dev->setmode_ready[i] = 1;
1716 wake_up(&dev->wait_setmode[i]);
1718 mutex_lock(&dev->open_lock);
1719 /* reset the DSP so firmware can be reload next time */
1720 s2255_reset_dsppower(dev);
1721 s2255_exit_v4l(dev);
1722 /* board shutdown stops the read pipe if it is running */
1723 s2255_board_shutdown(dev);
1724 /* make sure firmware still not trying to load */
1725 del_timer(&dev->timer); /* only started in .probe and .open */
1727 if (dev->fw_data->fw_urb) {
1728 dprintk(2, "kill fw_urb\n");
1729 usb_kill_urb(dev->fw_data->fw_urb);
1730 usb_free_urb(dev->fw_data->fw_urb);
1731 dev->fw_data->fw_urb = NULL;
1733 if (dev->fw_data->fw)
1734 release_firmware(dev->fw_data->fw);
1735 kfree(dev->fw_data->pfw_data);
1736 kfree(dev->fw_data);
1737 usb_put_dev(dev->udev);
1738 dprintk(1, "%s", __func__);
1740 while (!list_empty(&s2255_devlist)) {
1741 list = s2255_devlist.next;
1744 mutex_unlock(&dev->open_lock);
1748 static int s2255_close(struct file *file)
1750 struct s2255_fh *fh = file->private_data;
1751 struct s2255_dev *dev = fh->dev;
1752 int minor = video_devdata(file)->minor;
1756 mutex_lock(&dev->open_lock);
1758 /* turn off stream */
1759 if (res_check(fh)) {
1760 if (dev->b_acquire[fh->channel])
1761 s2255_stop_acquire(dev, fh->channel);
1762 videobuf_streamoff(&fh->vb_vidq);
1766 videobuf_mmap_free(&fh->vb_vidq);
1767 dev->users[fh->channel]--;
1769 mutex_unlock(&dev->open_lock);
1771 kref_put(&dev->kref, s2255_destroy);
1772 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1773 minor, dev->users[fh->channel]);
1778 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1780 struct s2255_fh *fh = file->private_data;
1785 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1787 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1789 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1790 (unsigned long)vma->vm_start,
1791 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1796 static const struct v4l2_file_operations s2255_fops_v4l = {
1797 .owner = THIS_MODULE,
1799 .release = s2255_close,
1801 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1802 .mmap = s2255_mmap_v4l,
1805 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1806 .vidioc_querycap = vidioc_querycap,
1807 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1808 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1809 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1810 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1811 .vidioc_reqbufs = vidioc_reqbufs,
1812 .vidioc_querybuf = vidioc_querybuf,
1813 .vidioc_qbuf = vidioc_qbuf,
1814 .vidioc_dqbuf = vidioc_dqbuf,
1815 .vidioc_s_std = vidioc_s_std,
1816 .vidioc_enum_input = vidioc_enum_input,
1817 .vidioc_g_input = vidioc_g_input,
1818 .vidioc_s_input = vidioc_s_input,
1819 .vidioc_queryctrl = vidioc_queryctrl,
1820 .vidioc_g_ctrl = vidioc_g_ctrl,
1821 .vidioc_s_ctrl = vidioc_s_ctrl,
1822 .vidioc_streamon = vidioc_streamon,
1823 .vidioc_streamoff = vidioc_streamoff,
1824 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1825 .vidiocgmbuf = vidioc_cgmbuf,
1827 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1828 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1829 .vidioc_s_parm = vidioc_s_parm,
1830 .vidioc_g_parm = vidioc_g_parm,
1833 static struct video_device template = {
1835 .fops = &s2255_fops_v4l,
1836 .ioctl_ops = &s2255_ioctl_ops,
1838 .release = video_device_release,
1839 .tvnorms = S2255_NORMS,
1840 .current_norm = V4L2_STD_NTSC_M,
1843 static int s2255_probe_v4l(struct s2255_dev *dev)
1847 int cur_nr = video_nr;
1849 /* initialize all video 4 linux */
1850 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1851 /* register 4 video devices */
1852 for (i = 0; i < MAX_CHANNELS; i++) {
1853 INIT_LIST_HEAD(&dev->vidq[i].active);
1854 dev->vidq[i].dev = dev;
1855 dev->vidq[i].channel = i;
1856 /* register 4 video devices */
1857 dev->vdev[i] = video_device_alloc();
1858 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1859 dev->vdev[i]->parent = &dev->interface->dev;
1861 ret = video_register_device(dev->vdev[i],
1865 ret = video_register_device(dev->vdev[i],
1868 video_set_drvdata(dev->vdev[i], dev);
1871 dev_err(&dev->udev->dev,
1872 "failed to register video device!\n");
1876 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1877 S2255_MAJOR_VERSION,
1878 S2255_MINOR_VERSION);
1882 static void s2255_exit_v4l(struct s2255_dev *dev)
1886 for (i = 0; i < MAX_CHANNELS; i++) {
1887 if (-1 != dev->vdev[i]->minor) {
1888 video_unregister_device(dev->vdev[i]);
1889 printk(KERN_INFO "s2255 unregistered\n");
1891 video_device_release(dev->vdev[i]);
1892 printk(KERN_INFO "s2255 released\n");
1897 /* this function moves the usb stream read pipe data
1898 * into the system buffers.
1899 * returns 0 on success, EAGAIN if more data to process( call this
1902 * Received frame structure:
1903 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1904 * bytes 4-7: channel: 0-3
1905 * bytes 8-11: payload size: size of the frame
1906 * bytes 12-payloadsize+12: frame data
1908 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1914 unsigned long copy_size;
1917 struct s2255_framei *frm;
1918 unsigned char *pdata;
1920 dprintk(100, "buffer to user\n");
1922 idx = dev->cur_frame[dev->cc];
1923 frm = &dev->buffer[dev->cc].frame[idx];
1925 if (frm->ulState == S2255_READ_IDLE) {
1930 /* search for marker codes */
1931 pdata = (unsigned char *)pipe_info->transfer_buffer;
1932 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1933 switch (*(s32 *) pdata) {
1934 case S2255_MARKER_FRAME:
1935 pdword = (s32 *)pdata;
1936 dprintk(4, "found frame marker at offset:"
1937 " %d [%x %x]\n", jj, pdata[0],
1939 offset = jj + PREFIX_SIZE;
1942 if (cc >= MAX_CHANNELS) {
1948 dev->cc = G_chnmap[cc];
1949 payload = pdword[3];
1950 if (payload > dev->req_image_size[dev->cc]) {
1951 dev->bad_payload[dev->cc]++;
1952 /* discard the bad frame */
1955 dev->pkt_size[dev->cc] = payload;
1956 dev->jpg_size[dev->cc] = pdword[4];
1958 case S2255_MARKER_RESPONSE:
1959 pdword = (s32 *)pdata;
1960 pdata += DEF_USB_BLOCK;
1961 jj += DEF_USB_BLOCK;
1962 if (pdword[1] >= MAX_CHANNELS)
1964 cc = G_chnmap[pdword[1]];
1965 if (!(cc >= 0 && cc < MAX_CHANNELS))
1967 switch (pdword[2]) {
1968 case S2255_RESPONSE_SETMODE:
1969 /* check if channel valid */
1970 /* set mode ready */
1971 dev->setmode_ready[cc] = 1;
1972 wake_up(&dev->wait_setmode[cc]);
1973 dprintk(5, "setmode ready %d\n", cc);
1975 case S2255_RESPONSE_FW:
1977 dev->chn_ready |= (1 << cc);
1978 if ((dev->chn_ready & 0x0f) != 0x0f)
1980 /* all channels ready */
1981 printk(KERN_INFO "s2255: fw loaded\n");
1982 atomic_set(&dev->fw_data->fw_state,
1984 wake_up(&dev->fw_data->wait_fw);
1987 printk(KERN_INFO "s2255 unknwn resp\n");
2001 idx = dev->cur_frame[dev->cc];
2002 frm = &dev->buffer[dev->cc].frame[idx];
2004 /* search done. now find out if should be acquiring on this channel */
2005 if (!dev->b_acquire[dev->cc]) {
2006 /* we found a frame, but this channel is turned off */
2007 frm->ulState = S2255_READ_IDLE;
2011 if (frm->ulState == S2255_READ_IDLE) {
2012 frm->ulState = S2255_READ_FRAME;
2016 /* skip the marker 512 bytes (and offset if out of sync) */
2017 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2020 if (frm->lpvbits == NULL) {
2021 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2022 frm, dev, dev->cc, idx);
2026 pdest = frm->lpvbits + frm->cur_size;
2028 copy_size = (pipe_info->cur_transfer_size - offset);
2030 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2032 /* sanity check on pdest */
2033 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2034 memcpy(pdest, psrc, copy_size);
2036 frm->cur_size += copy_size;
2037 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2039 if (frm->cur_size >= size) {
2042 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2044 dev->last_frame[cc] = dev->cur_frame[cc];
2045 dev->cur_frame[cc]++;
2046 /* end of system frame ring buffer, start at zero */
2047 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2048 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2049 dev->cur_frame[cc] = 0;
2051 if (dev->b_acquire[cc])
2052 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2053 dev->frame_count[cc]++;
2054 frm->ulState = S2255_READ_IDLE;
2058 /* done successfully */
2062 static void s2255_read_video_callback(struct s2255_dev *dev,
2063 struct s2255_pipeinfo *pipe_info)
2066 dprintk(50, "callback read video \n");
2068 if (dev->cc >= MAX_CHANNELS) {
2070 dev_err(&dev->udev->dev, "invalid channel\n");
2073 /* otherwise copy to the system buffers */
2074 res = save_frame(dev, pipe_info);
2076 dprintk(4, "s2255: read callback failed\n");
2078 dprintk(50, "callback read video done\n");
2082 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2083 u16 Index, u16 Value, void *TransferBuffer,
2084 s32 TransferBufferLength, int bOut)
2088 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2090 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2092 Value, Index, TransferBuffer,
2093 TransferBufferLength, HZ * 5);
2095 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2096 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2097 Value, Index, TransferBuffer,
2098 TransferBufferLength, HZ * 5);
2104 * retrieve FX2 firmware version. future use.
2105 * @param dev pointer to device extension
2106 * @return -1 for fail, else returns firmware version as an int(16 bits)
2108 static int s2255_get_fx2fw(struct s2255_dev *dev)
2112 unsigned char transBuffer[64];
2113 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2116 dprintk(2, "get fw error: %x\n", ret);
2117 fw = transBuffer[0] + (transBuffer[1] << 8);
2118 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2123 * Create the system ring buffer to copy frames into from the
2126 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2129 unsigned long reqsize;
2130 dprintk(1, "create sys buffers\n");
2131 if (chn >= MAX_CHANNELS)
2134 dev->buffer[chn].dwFrames = SYS_FRAMES;
2136 /* always allocate maximum size(PAL) for system buffers */
2137 reqsize = SYS_FRAMES_MAXSIZE;
2139 if (reqsize > SYS_FRAMES_MAXSIZE)
2140 reqsize = SYS_FRAMES_MAXSIZE;
2142 for (i = 0; i < SYS_FRAMES; i++) {
2143 /* allocate the frames */
2144 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2146 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2147 &dev->buffer[chn].frame[i], chn, i,
2148 dev->buffer[chn].frame[i].lpvbits);
2149 dev->buffer[chn].frame[i].size = reqsize;
2150 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2151 printk(KERN_INFO "out of memory. using less frames\n");
2152 dev->buffer[chn].dwFrames = i;
2157 /* make sure internal states are set */
2158 for (i = 0; i < SYS_FRAMES; i++) {
2159 dev->buffer[chn].frame[i].ulState = 0;
2160 dev->buffer[chn].frame[i].cur_size = 0;
2163 dev->cur_frame[chn] = 0;
2164 dev->last_frame[chn] = -1;
2168 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2169 unsigned long channel)
2172 dprintk(1, "release sys buffers\n");
2173 for (i = 0; i < SYS_FRAMES; i++) {
2174 if (dev->buffer[channel].frame[i].lpvbits) {
2175 dprintk(1, "vfree %p\n",
2176 dev->buffer[channel].frame[i].lpvbits);
2177 vfree(dev->buffer[channel].frame[i].lpvbits);
2179 dev->buffer[channel].frame[i].lpvbits = NULL;
2184 static int s2255_board_init(struct s2255_dev *dev)
2187 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2189 dprintk(4, "board init: %p", dev);
2191 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2192 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2194 memset(pipe, 0, sizeof(*pipe));
2196 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2197 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2199 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2201 if (pipe->transfer_buffer == NULL) {
2202 dprintk(1, "out of memory!\n");
2208 /* query the firmware */
2209 fw_ver = s2255_get_fx2fw(dev);
2211 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2212 (fw_ver >> 8) & 0xff,
2215 if (fw_ver < S2255_CUR_USB_FWVER)
2216 dev_err(&dev->udev->dev,
2217 "usb firmware not up to date %d.%d\n",
2218 (fw_ver >> 8) & 0xff,
2221 for (j = 0; j < MAX_CHANNELS; j++) {
2222 dev->b_acquire[j] = 0;
2223 dev->mode[j] = mode_def;
2224 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2225 dev->cur_fmt[j] = &formats[0];
2226 dev->mode[j].restart = 1;
2227 dev->req_image_size[j] = get_transfer_size(&mode_def);
2228 dev->frame_count[j] = 0;
2229 /* create the system buffers */
2230 s2255_create_sys_buffers(dev, j);
2232 /* start read pipe */
2233 s2255_start_readpipe(dev);
2235 dprintk(1, "S2255: board initialized\n");
2239 static int s2255_board_shutdown(struct s2255_dev *dev)
2243 dprintk(1, "S2255: board shutdown: %p", dev);
2245 for (i = 0; i < MAX_CHANNELS; i++) {
2246 if (dev->b_acquire[i])
2247 s2255_stop_acquire(dev, i);
2250 s2255_stop_readpipe(dev);
2252 for (i = 0; i < MAX_CHANNELS; i++)
2253 s2255_release_sys_buffers(dev, i);
2255 /* release transfer buffers */
2256 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2257 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2258 kfree(pipe->transfer_buffer);
2263 static void read_pipe_completion(struct urb *purb)
2265 struct s2255_pipeinfo *pipe_info;
2266 struct s2255_dev *dev;
2270 pipe_info = purb->context;
2271 dprintk(100, "read pipe completion %p, status %d\n", purb,
2273 if (pipe_info == NULL) {
2274 dev_err(&purb->dev->dev, "no context!\n");
2278 dev = pipe_info->dev;
2280 dev_err(&purb->dev->dev, "no context!\n");
2283 status = purb->status;
2285 dprintk(2, "read_pipe_completion: err\n");
2289 if (pipe_info->state == 0) {
2290 dprintk(2, "exiting USB pipe");
2294 s2255_read_video_callback(dev, pipe_info);
2296 pipe_info->err_count = 0;
2297 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2299 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2301 pipe_info->transfer_buffer,
2302 pipe_info->cur_transfer_size,
2303 read_pipe_completion, pipe_info);
2305 if (pipe_info->state != 0) {
2306 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2307 dev_err(&dev->udev->dev, "error submitting urb\n");
2308 usb_free_urb(pipe_info->stream_urb);
2311 dprintk(2, "read pipe complete state 0\n");
2316 static int s2255_start_readpipe(struct s2255_dev *dev)
2321 struct s2255_pipeinfo *pipe_info = dev->pipes;
2322 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2323 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2325 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2326 pipe_info->state = 1;
2327 pipe_info->err_count = 0;
2328 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2329 if (!pipe_info->stream_urb) {
2330 dev_err(&dev->udev->dev,
2331 "ReadStream: Unable to alloc URB\n");
2334 /* transfer buffer allocated in board_init */
2335 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2337 pipe_info->transfer_buffer,
2338 pipe_info->cur_transfer_size,
2339 read_pipe_completion, pipe_info);
2341 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2342 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2344 printk(KERN_ERR "s2255: start read pipe failed\n");
2352 /* starts acquisition process */
2353 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2355 unsigned char *buffer;
2357 unsigned long chn_rev;
2359 if (chn >= MAX_CHANNELS) {
2360 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2364 chn_rev = G_chnmap[chn];
2365 dprintk(1, "S2255: start acquire %lu \n", chn);
2367 buffer = kzalloc(512, GFP_KERNEL);
2368 if (buffer == NULL) {
2369 dev_err(&dev->udev->dev, "out of mem\n");
2373 dev->last_frame[chn] = -1;
2374 dev->bad_payload[chn] = 0;
2375 dev->cur_frame[chn] = 0;
2376 for (j = 0; j < SYS_FRAMES; j++) {
2377 dev->buffer[chn].frame[j].ulState = 0;
2378 dev->buffer[chn].frame[j].cur_size = 0;
2381 /* send the start command */
2382 *(u32 *) buffer = IN_DATA_TOKEN;
2383 *((u32 *) buffer + 1) = (u32) chn_rev;
2384 *((u32 *) buffer + 2) = (u32) CMD_START;
2385 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2387 dev_err(&dev->udev->dev, "CMD_START error\n");
2389 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2394 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2396 unsigned char *buffer;
2398 unsigned long chn_rev;
2400 if (chn >= MAX_CHANNELS) {
2401 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2404 chn_rev = G_chnmap[chn];
2406 buffer = kzalloc(512, GFP_KERNEL);
2407 if (buffer == NULL) {
2408 dev_err(&dev->udev->dev, "out of mem\n");
2412 /* send the stop command */
2413 dprintk(4, "stop acquire %lu\n", chn);
2414 *(u32 *) buffer = IN_DATA_TOKEN;
2415 *((u32 *) buffer + 1) = (u32) chn_rev;
2416 *((u32 *) buffer + 2) = CMD_STOP;
2417 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2420 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2422 dprintk(4, "stop acquire: releasing states \n");
2425 dev->b_acquire[chn] = 0;
2430 static void s2255_stop_readpipe(struct s2255_dev *dev)
2435 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2438 dprintk(4, "stop read pipe\n");
2439 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2440 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2442 if (pipe_info->state == 0)
2444 pipe_info->state = 0;
2448 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2449 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2450 if (pipe_info->stream_urb) {
2452 usb_kill_urb(pipe_info->stream_urb);
2453 usb_free_urb(pipe_info->stream_urb);
2454 pipe_info->stream_urb = NULL;
2457 dprintk(2, "s2255 stop read pipe: %d\n", j);
2461 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2464 s2255_reset_dsppower(dev);
2465 dev->fw_data->fw_size = dev->fw_data->fw->size;
2466 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2467 memcpy(dev->fw_data->pfw_data,
2468 dev->fw_data->fw->data, CHUNK_SIZE);
2469 dev->fw_data->fw_loaded = CHUNK_SIZE;
2470 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2471 usb_sndbulkpipe(dev->udev, 2),
2472 dev->fw_data->pfw_data,
2473 CHUNK_SIZE, s2255_fwchunk_complete,
2475 mod_timer(&dev->timer, jiffies + HZ);
2478 /* standard usb probe function */
2479 static int s2255_probe(struct usb_interface *interface,
2480 const struct usb_device_id *id)
2482 struct s2255_dev *dev = NULL;
2483 struct usb_host_interface *iface_desc;
2484 struct usb_endpoint_descriptor *endpoint;
2486 int retval = -ENOMEM;
2490 dprintk(2, "s2255: probe\n");
2492 /* allocate memory for our device state and initialize it to zero */
2493 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2495 s2255_dev_err(&interface->dev, "out of memory\n");
2499 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2503 mutex_init(&dev->lock);
2504 mutex_init(&dev->open_lock);
2506 /* grab usb_device and save it */
2507 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2508 if (dev->udev == NULL) {
2509 dev_err(&interface->dev, "null usb device\n");
2513 kref_init(&dev->kref);
2514 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2515 dev->udev, interface);
2516 dev->interface = interface;
2517 /* set up the endpoint information */
2518 iface_desc = interface->cur_altsetting;
2519 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2520 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2521 endpoint = &iface_desc->endpoint[i].desc;
2522 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2523 /* we found the bulk in endpoint */
2524 dev->read_endpoint = endpoint->bEndpointAddress;
2528 if (!dev->read_endpoint) {
2529 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2534 usb_set_intfdata(interface, dev);
2536 dprintk(100, "after intfdata %p\n", dev);
2538 init_timer(&dev->timer);
2539 dev->timer.function = s2255_timer;
2540 dev->timer.data = (unsigned long)dev->fw_data;
2542 init_waitqueue_head(&dev->fw_data->wait_fw);
2543 for (i = 0; i < MAX_CHANNELS; i++)
2544 init_waitqueue_head(&dev->wait_setmode[i]);
2547 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2549 if (!dev->fw_data->fw_urb) {
2550 dev_err(&interface->dev, "out of memory!\n");
2553 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2554 if (!dev->fw_data->pfw_data) {
2555 dev_err(&interface->dev, "out of memory!\n");
2558 /* load the first chunk */
2559 if (request_firmware(&dev->fw_data->fw,
2560 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2561 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2564 /* check the firmware is valid */
2565 fw_size = dev->fw_data->fw->size;
2566 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2568 if (*pdata != S2255_FW_MARKER) {
2569 printk(KERN_INFO "Firmware invalid.\n");
2573 /* make sure firmware is the latest */
2575 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2576 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2578 /* loads v4l specific */
2579 s2255_probe_v4l(dev);
2580 usb_reset_device(dev->udev);
2581 /* load 2255 board specific */
2582 retval = s2255_board_init(dev);
2586 dprintk(4, "before probe done %p\n", dev);
2587 spin_lock_init(&dev->slock);
2589 s2255_fwload_start(dev, 0);
2590 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2596 /* disconnect routine. when board is removed physically or with rmmod */
2597 static void s2255_disconnect(struct usb_interface *interface)
2599 struct s2255_dev *dev = NULL;
2601 dprintk(1, "s2255: disconnect interface %p\n", interface);
2602 dev = usb_get_intfdata(interface);
2605 * wake up any of the timers to allow open_lock to be
2608 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2609 wake_up(&dev->fw_data->wait_fw);
2610 for (i = 0; i < MAX_CHANNELS; i++) {
2611 dev->setmode_ready[i] = 1;
2612 wake_up(&dev->wait_setmode[i]);
2615 mutex_lock(&dev->open_lock);
2616 usb_set_intfdata(interface, NULL);
2617 mutex_unlock(&dev->open_lock);
2620 kref_put(&dev->kref, s2255_destroy);
2621 dprintk(1, "s2255drv: disconnect\n");
2622 dev_info(&interface->dev, "s2255usb now disconnected\n");
2626 static struct usb_driver s2255_driver = {
2627 .name = S2255_DRIVER_NAME,
2628 .probe = s2255_probe,
2629 .disconnect = s2255_disconnect,
2630 .id_table = s2255_table,
2633 static int __init usb_s2255_init(void)
2637 /* register this driver with the USB subsystem */
2638 result = usb_register(&s2255_driver);
2641 pr_err(KBUILD_MODNAME
2642 ": usb_register failed. Error number %d\n", result);
2644 dprintk(2, "s2255_init: done\n");
2648 static void __exit usb_s2255_exit(void)
2650 usb_deregister(&s2255_driver);
2653 module_init(usb_s2255_init);
2654 module_exit(usb_s2255_exit);
2656 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2657 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2658 MODULE_LICENSE("GPL");