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 <linux/smp_lock.h>
52 #include <media/videobuf-vmalloc.h>
53 #include <media/v4l2-common.h>
54 #include <media/v4l2-ioctl.h>
55 #include <linux/vmalloc.h>
56 #include <linux/usb.h>
58 #define FIRMWARE_FILE_NAME "f2255usb.bin"
62 /* default JPEG quality */
63 #define S2255_DEF_JPEG_QUAL 50
64 /* vendor request in */
66 /* vendor request out */
67 #define S2255_VR_OUT 1
69 #define S2255_VR_FW 0x30
70 /* USB endpoint number for configuring the device */
71 #define S2255_CONFIG_EP 2
72 /* maximum time for DSP to start responding after last FW word loaded(ms) */
73 #define S2255_DSP_BOOTTIME 800
74 /* maximum time to wait for firmware to load (ms) */
75 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
76 #define S2255_DEF_BUFS 16
77 #define S2255_SETMODE_TIMEOUT 500
78 #define MAX_CHANNELS 4
79 #define S2255_MARKER_FRAME 0x2255DA4AL
80 #define S2255_MARKER_RESPONSE 0x2255ACACL
81 #define S2255_RESPONSE_SETMODE 0x01
82 #define S2255_RESPONSE_FW 0x10
83 #define S2255_USB_XFER_SIZE (16 * 1024)
84 #define MAX_CHANNELS 4
85 #define MAX_PIPE_BUFFERS 1
87 /* maximum size is PAL full size plus room for the marker header(s) */
88 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
89 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
90 #define LINE_SZ_4CIFS_NTSC 640
91 #define LINE_SZ_2CIFS_NTSC 640
92 #define LINE_SZ_1CIFS_NTSC 320
93 #define LINE_SZ_4CIFS_PAL 704
94 #define LINE_SZ_2CIFS_PAL 704
95 #define LINE_SZ_1CIFS_PAL 352
96 #define NUM_LINES_4CIFS_NTSC 240
97 #define NUM_LINES_2CIFS_NTSC 240
98 #define NUM_LINES_1CIFS_NTSC 240
99 #define NUM_LINES_4CIFS_PAL 288
100 #define NUM_LINES_2CIFS_PAL 288
101 #define NUM_LINES_1CIFS_PAL 288
102 #define LINE_SZ_DEF 640
103 #define NUM_LINES_DEF 240
106 /* predefined settings */
107 #define FORMAT_NTSC 1
110 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
111 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
112 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
113 /* SCALE_4CIFSI is the 2 fields interpolated into one */
114 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
116 #define COLOR_YUVPL 1 /* YUV planar */
117 #define COLOR_YUVPK 2 /* YUV packed */
118 #define COLOR_Y8 4 /* monochrome */
119 #define COLOR_JPG 5 /* JPEG */
120 #define MASK_COLOR 0xff
121 #define MASK_JPG_QUALITY 0xff00
123 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
124 #define FDEC_1 1 /* capture every frame. default */
125 #define FDEC_2 2 /* capture every 2nd frame */
126 #define FDEC_3 3 /* capture every 3rd frame */
127 #define FDEC_5 5 /* capture every 5th frame */
129 /*-------------------------------------------------------
130 * Default mode parameters.
131 *-------------------------------------------------------*/
132 #define DEF_SCALE SCALE_4CIFS
133 #define DEF_COLOR COLOR_YUVPL
134 #define DEF_FDEC FDEC_1
136 #define DEF_CONTRAST 0x5c
137 #define DEF_SATURATION 0x80
140 /* usb config commands */
141 #define IN_DATA_TOKEN 0x2255c0de
142 #define CMD_2255 0xc2255000
143 #define CMD_SET_MODE (CMD_2255 | 0x10)
144 #define CMD_START (CMD_2255 | 0x20)
145 #define CMD_STOP (CMD_2255 | 0x30)
146 #define CMD_STATUS (CMD_2255 | 0x40)
149 u32 format; /* input video format (NTSC, PAL) */
150 u32 scale; /* output video scale */
151 u32 color; /* output video color format */
152 u32 fdec; /* frame decimation */
153 u32 bright; /* brightness */
154 u32 contrast; /* contrast */
155 u32 saturation; /* saturation */
156 u32 hue; /* hue (NTSC only)*/
157 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
158 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
159 u32 restart; /* if DSP requires restart */
163 #define S2255_READ_IDLE 0
164 #define S2255_READ_FRAME 1
166 /* frame structure */
167 struct s2255_framei {
169 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
170 void *lpvbits; /* image data */
171 unsigned long cur_size; /* current data copied to it */
174 /* image buffer structure */
175 struct s2255_bufferi {
176 unsigned long dwFrames; /* number of frames in buffer */
177 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
180 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
181 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
182 DEF_HUE, 0, DEF_USB_BLOCK, 0}
184 struct s2255_dmaqueue {
185 struct list_head active;
186 struct s2255_dev *dev;
190 /* for firmware loading, fw_state */
191 #define S2255_FW_NOTLOADED 0
192 #define S2255_FW_LOADED_DSPWAIT 1
193 #define S2255_FW_SUCCESS 2
194 #define S2255_FW_FAILED 3
195 #define S2255_FW_DISCONNECTING 4
197 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
198 /* 2255 read states */
199 #define S2255_READ_IDLE 0
200 #define S2255_READ_FRAME 1
207 wait_queue_head_t wait_fw;
208 const struct firmware *fw;
211 struct s2255_pipeinfo {
212 u32 max_transfer_size;
213 u32 cur_transfer_size;
217 void *dev; /* back pointer to s2255_dev struct*/
222 struct s2255_fmt; /*forward declaration */
226 int users[MAX_CHANNELS];
228 struct mutex open_lock;
229 int resources[MAX_CHANNELS];
230 struct usb_device *udev;
231 struct usb_interface *interface;
234 struct s2255_dmaqueue vidq[MAX_CHANNELS];
235 struct video_device *vdev[MAX_CHANNELS];
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};
316 static int *s2255_debug = &debug;
318 static int s2255_start_readpipe(struct s2255_dev *dev);
319 static void s2255_stop_readpipe(struct s2255_dev *dev);
320 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
321 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
322 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
323 int chn, int jpgsize);
324 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
325 struct s2255_mode *mode);
326 static int s2255_board_shutdown(struct s2255_dev *dev);
327 static void s2255_exit_v4l(struct s2255_dev *dev);
328 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
329 static void s2255_destroy(struct kref *kref);
330 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
331 u16 index, u16 value, void *buf,
332 s32 buf_len, int bOut);
334 /* dev_err macro with driver name */
335 #define S2255_DRIVER_NAME "s2255"
336 #define s2255_dev_err(dev, fmt, arg...) \
337 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
339 #define dprintk(level, fmt, arg...) \
341 if (*s2255_debug >= (level)) { \
342 printk(KERN_DEBUG S2255_DRIVER_NAME \
347 static struct usb_driver s2255_driver;
350 /* Declare static vars that will be used as parameters */
351 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
353 /* start video number */
354 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
356 module_param(debug, int, 0644);
357 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
358 module_param(vid_limit, int, 0644);
359 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
360 module_param(video_nr, int, 0644);
361 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
363 /* USB device table */
364 static struct usb_device_id s2255_table[] = {
365 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
366 { } /* Terminating entry */
368 MODULE_DEVICE_TABLE(usb, s2255_table);
371 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
373 /* supported controls */
374 static struct v4l2_queryctrl s2255_qctrl[] = {
376 .id = V4L2_CID_BRIGHTNESS,
377 .type = V4L2_CTRL_TYPE_INTEGER,
378 .name = "Brightness",
385 .id = V4L2_CID_CONTRAST,
386 .type = V4L2_CTRL_TYPE_INTEGER,
391 .default_value = DEF_CONTRAST,
394 .id = V4L2_CID_SATURATION,
395 .type = V4L2_CTRL_TYPE_INTEGER,
396 .name = "Saturation",
400 .default_value = DEF_SATURATION,
404 .type = V4L2_CTRL_TYPE_INTEGER,
409 .default_value = DEF_HUE,
414 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
417 static const struct s2255_fmt formats[] = {
419 .name = "4:2:2, planar, YUV422P",
420 .fourcc = V4L2_PIX_FMT_YUV422P,
424 .name = "4:2:2, packed, YUYV",
425 .fourcc = V4L2_PIX_FMT_YUYV,
429 .name = "4:2:2, packed, UYVY",
430 .fourcc = V4L2_PIX_FMT_UYVY,
434 .fourcc = V4L2_PIX_FMT_JPEG,
438 .fourcc = V4L2_PIX_FMT_GREY,
443 static int norm_maxw(struct video_device *vdev)
445 return (vdev->current_norm & V4L2_STD_NTSC) ?
446 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
449 static int norm_maxh(struct video_device *vdev)
451 return (vdev->current_norm & V4L2_STD_NTSC) ?
452 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
455 static int norm_minw(struct video_device *vdev)
457 return (vdev->current_norm & V4L2_STD_NTSC) ?
458 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
461 static int norm_minh(struct video_device *vdev)
463 return (vdev->current_norm & V4L2_STD_NTSC) ?
464 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
469 * TODO: fixme: move YUV reordering to hardware
470 * converts 2255 planar format to yuyv or uyvy
472 static void planar422p_to_yuv_packed(const unsigned char *in,
474 int width, int height,
480 unsigned long size = height * width;
482 pY = (unsigned char *)in;
483 pCr = (unsigned char *)in + height * width;
484 pCb = (unsigned char *)in + height * width + (height * width / 2);
485 for (i = 0; i < size * 2; i += 4) {
486 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
487 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
488 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
489 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
494 static void s2255_reset_dsppower(struct s2255_dev *dev)
496 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
498 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
502 /* kickstarts the firmware loading. from probe
504 static void s2255_timer(unsigned long user_data)
506 struct s2255_fw *data = (struct s2255_fw *)user_data;
507 dprintk(100, "s2255 timer\n");
508 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
509 printk(KERN_ERR "s2255: can't submit urb\n");
510 atomic_set(&data->fw_state, S2255_FW_FAILED);
511 /* wake up anything waiting for the firmware */
512 wake_up(&data->wait_fw);
518 /* this loads the firmware asynchronously.
519 Originally this was done synchroously in probe.
520 But it is better to load it asynchronously here than block
521 inside the probe function. Blocking inside probe affects boot time.
522 FW loading is triggered by the timer in the probe function
524 static void s2255_fwchunk_complete(struct urb *urb)
526 struct s2255_fw *data = urb->context;
527 struct usb_device *udev = urb->dev;
529 dprintk(100, "udev %p urb %p", udev, urb);
531 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
532 atomic_set(&data->fw_state, S2255_FW_FAILED);
533 /* wake up anything waiting for the firmware */
534 wake_up(&data->wait_fw);
537 if (data->fw_urb == NULL) {
538 s2255_dev_err(&udev->dev, "disconnected\n");
539 atomic_set(&data->fw_state, S2255_FW_FAILED);
540 /* wake up anything waiting for the firmware */
541 wake_up(&data->wait_fw);
544 #define CHUNK_SIZE 512
545 /* all USB transfers must be done with continuous kernel memory.
546 can't allocate more than 128k in current linux kernel, so
547 upload the firmware in chunks
549 if (data->fw_loaded < data->fw_size) {
550 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
551 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
553 if (len < CHUNK_SIZE)
554 memset(data->pfw_data, 0, CHUNK_SIZE);
556 dprintk(100, "completed len %d, loaded %d \n", len,
559 memcpy(data->pfw_data,
560 (char *) data->fw->data + data->fw_loaded, len);
562 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
563 data->pfw_data, CHUNK_SIZE,
564 s2255_fwchunk_complete, data);
565 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
566 dev_err(&udev->dev, "failed submit URB\n");
567 atomic_set(&data->fw_state, S2255_FW_FAILED);
568 /* wake up anything waiting for the firmware */
569 wake_up(&data->wait_fw);
572 data->fw_loaded += len;
574 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
576 dprintk(100, "2255 complete done\n");
581 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
583 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
584 struct s2255_buffer *buf;
585 unsigned long flags = 0;
587 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
588 spin_lock_irqsave(&dev->slock, flags);
590 if (list_empty(&dma_q->active)) {
591 dprintk(1, "No active queue to serve\n");
595 buf = list_entry(dma_q->active.next,
596 struct s2255_buffer, vb.queue);
598 list_del(&buf->vb.queue);
599 do_gettimeofday(&buf->vb.ts);
600 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
601 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
602 wake_up(&buf->vb.done);
603 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
605 spin_unlock_irqrestore(&dev->slock, flags);
610 static const struct s2255_fmt *format_by_fourcc(int fourcc)
614 for (i = 0; i < ARRAY_SIZE(formats); i++) {
615 if (-1 == formats[i].fourcc)
617 if (formats[i].fourcc == fourcc)
626 /* video buffer vmalloc implementation based partly on VIVI driver which is
627 * Copyright (c) 2006 by
628 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
629 * Ted Walther <ted--a.t--enumera.com>
630 * John Sokol <sokol--a.t--videotechnology.com>
631 * http://v4l.videotechnology.com/
634 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
635 int chn, int jpgsize)
640 char *vbuf = videobuf_to_vmalloc(&buf->vb);
641 unsigned long last_frame;
642 struct s2255_framei *frm;
647 last_frame = dev->last_frame[chn];
648 if (last_frame != -1) {
649 frm = &dev->buffer[chn].frame[last_frame];
651 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
652 switch (buf->fmt->fourcc) {
653 case V4L2_PIX_FMT_YUYV:
654 case V4L2_PIX_FMT_UYVY:
655 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
660 case V4L2_PIX_FMT_GREY:
661 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
663 case V4L2_PIX_FMT_JPEG:
664 buf->vb.size = jpgsize;
665 memcpy(vbuf, tmpbuf, buf->vb.size);
667 case V4L2_PIX_FMT_YUV422P:
669 buf->vb.width * buf->vb.height * 2);
672 printk(KERN_DEBUG "s2255: unknown format?\n");
674 dev->last_frame[chn] = -1;
676 printk(KERN_ERR "s2255: =======no frame\n");
680 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
681 (unsigned long)vbuf, pos);
682 /* tell v4l buffer was filled */
684 buf->vb.field_count = dev->frame_count[chn] * 2;
685 do_gettimeofday(&ts);
687 buf->vb.state = VIDEOBUF_DONE;
691 /* ------------------------------------------------------------------
693 ------------------------------------------------------------------*/
695 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
698 struct s2255_fh *fh = vq->priv_data;
700 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
703 *count = S2255_DEF_BUFS;
705 while (*size * (*count) > vid_limit * 1024 * 1024)
711 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
713 dprintk(4, "%s\n", __func__);
715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
719 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
720 enum v4l2_field field)
722 struct s2255_fh *fh = vq->priv_data;
723 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
725 dprintk(4, "%s, field=%d\n", __func__, field);
729 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
730 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
731 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
732 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
733 dprintk(4, "invalid buffer prepare\n");
737 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
739 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
740 dprintk(4, "invalid buffer prepare\n");
745 buf->vb.width = fh->width;
746 buf->vb.height = fh->height;
747 buf->vb.field = field;
750 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
751 rc = videobuf_iolock(vq, &buf->vb, NULL);
756 buf->vb.state = VIDEOBUF_PREPARED;
759 free_buffer(vq, buf);
763 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
765 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
766 struct s2255_fh *fh = vq->priv_data;
767 struct s2255_dev *dev = fh->dev;
768 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
770 dprintk(1, "%s\n", __func__);
772 buf->vb.state = VIDEOBUF_QUEUED;
773 list_add_tail(&buf->vb.queue, &vidq->active);
776 static void buffer_release(struct videobuf_queue *vq,
777 struct videobuf_buffer *vb)
779 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
780 struct s2255_fh *fh = vq->priv_data;
781 dprintk(4, "%s %d\n", __func__, fh->channel);
782 free_buffer(vq, buf);
785 static struct videobuf_queue_ops s2255_video_qops = {
786 .buf_setup = buffer_setup,
787 .buf_prepare = buffer_prepare,
788 .buf_queue = buffer_queue,
789 .buf_release = buffer_release,
793 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
796 mutex_lock(&dev->lock);
797 if (dev->resources[fh->channel]) {
798 /* no, someone else uses it */
799 mutex_unlock(&dev->lock);
802 /* it's free, grab it */
803 dev->resources[fh->channel] = 1;
804 fh->resources[fh->channel] = 1;
805 dprintk(1, "s2255: res: get\n");
806 mutex_unlock(&dev->lock);
810 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
812 return dev->resources[fh->channel];
815 static int res_check(struct s2255_fh *fh)
817 return fh->resources[fh->channel];
821 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
823 mutex_lock(&dev->lock);
824 dev->resources[fh->channel] = 0;
825 fh->resources[fh->channel] = 0;
826 mutex_unlock(&dev->lock);
827 dprintk(1, "res: put\n");
831 static int vidioc_querycap(struct file *file, void *priv,
832 struct v4l2_capability *cap)
834 struct s2255_fh *fh = file->private_data;
835 struct s2255_dev *dev = fh->dev;
836 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
837 strlcpy(cap->card, "s2255", sizeof(cap->card));
838 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
839 cap->version = S2255_VERSION;
840 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
844 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
845 struct v4l2_fmtdesc *f)
851 if (index >= ARRAY_SIZE(formats))
854 dprintk(4, "name %s\n", formats[index].name);
855 strlcpy(f->description, formats[index].name, sizeof(f->description));
856 f->pixelformat = formats[index].fourcc;
860 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
861 struct v4l2_format *f)
863 struct s2255_fh *fh = priv;
865 f->fmt.pix.width = fh->width;
866 f->fmt.pix.height = fh->height;
867 f->fmt.pix.field = fh->vb_vidq.field;
868 f->fmt.pix.pixelformat = fh->fmt->fourcc;
869 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
870 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
874 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_format *f)
877 const struct s2255_fmt *fmt;
878 enum v4l2_field field;
880 struct s2255_fh *fh = priv;
881 struct s2255_dev *dev = fh->dev;
885 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
887 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
892 field = f->fmt.pix.field;
893 if (field == V4L2_FIELD_ANY)
896 dprintk(4, "try format %d \n", is_ntsc);
897 /* supports 3 sizes. see s2255drv.h */
898 dprintk(50, "width test %d, height %d\n",
899 f->fmt.pix.width, f->fmt.pix.height);
902 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
903 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
905 field = V4L2_FIELD_SEQ_TB;
906 } else if (!((field == V4L2_FIELD_INTERLACED) ||
907 (field == V4L2_FIELD_SEQ_TB) ||
908 (field == V4L2_FIELD_INTERLACED_TB))) {
909 dprintk(1, "unsupported field setting\n");
913 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
915 field = V4L2_FIELD_TOP;
916 } else if (!((field == V4L2_FIELD_TOP) ||
917 (field == V4L2_FIELD_BOTTOM))) {
918 dprintk(1, "unsupported field setting\n");
923 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
924 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
925 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
926 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
927 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
928 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
930 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
933 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
934 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
936 field = V4L2_FIELD_SEQ_TB;
937 } else if (!((field == V4L2_FIELD_INTERLACED) ||
938 (field == V4L2_FIELD_SEQ_TB) ||
939 (field == V4L2_FIELD_INTERLACED_TB))) {
940 dprintk(1, "unsupported field setting\n");
944 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
946 field = V4L2_FIELD_TOP;
947 } else if (!((field == V4L2_FIELD_TOP) ||
948 (field == V4L2_FIELD_BOTTOM))) {
949 dprintk(1, "unsupported field setting\n");
953 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
954 dprintk(50, "pal 704\n");
955 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
956 field = V4L2_FIELD_SEQ_TB;
957 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
958 dprintk(50, "pal 352A\n");
959 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
960 field = V4L2_FIELD_TOP;
961 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
962 dprintk(50, "pal 352B\n");
963 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
964 field = V4L2_FIELD_TOP;
966 dprintk(50, "pal 352C\n");
967 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
968 field = V4L2_FIELD_TOP;
972 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
973 f->fmt.pix.height, f->fmt.pix.field);
974 f->fmt.pix.field = field;
975 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
976 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
980 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
981 struct v4l2_format *f)
983 struct s2255_fh *fh = priv;
984 const struct s2255_fmt *fmt;
985 struct videobuf_queue *q = &fh->vb_vidq;
989 ret = vidioc_try_fmt_vid_cap(file, fh, f);
994 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
999 mutex_lock(&q->vb_lock);
1001 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1002 dprintk(1, "queue busy\n");
1007 if (res_locked(fh->dev, fh)) {
1008 dprintk(1, "can't change format after started\n");
1014 fh->width = f->fmt.pix.width;
1015 fh->height = f->fmt.pix.height;
1016 fh->vb_vidq.field = f->fmt.pix.field;
1018 norm = norm_minw(fh->dev->vdev[fh->channel]);
1019 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1020 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1021 if (fh->dev->cap_parm[fh->channel].capturemode &
1022 V4L2_MODE_HIGHQUALITY) {
1023 fh->mode.scale = SCALE_4CIFSI;
1024 dprintk(2, "scale 4CIFSI\n");
1026 fh->mode.scale = SCALE_4CIFS;
1027 dprintk(2, "scale 4CIFS\n");
1030 fh->mode.scale = SCALE_2CIFS;
1033 fh->mode.scale = SCALE_1CIFS;
1037 switch (fh->fmt->fourcc) {
1038 case V4L2_PIX_FMT_GREY:
1039 fh->mode.color = COLOR_Y8;
1041 case V4L2_PIX_FMT_JPEG:
1042 fh->mode.color = COLOR_JPG |
1043 (fh->dev->jc[fh->channel].quality << 8);
1045 case V4L2_PIX_FMT_YUV422P:
1046 fh->mode.color = COLOR_YUVPL;
1048 case V4L2_PIX_FMT_YUYV:
1049 case V4L2_PIX_FMT_UYVY:
1051 fh->mode.color = COLOR_YUVPK;
1056 mutex_unlock(&q->vb_lock);
1060 static int vidioc_reqbufs(struct file *file, void *priv,
1061 struct v4l2_requestbuffers *p)
1064 struct s2255_fh *fh = priv;
1065 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1069 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1072 struct s2255_fh *fh = priv;
1073 rc = videobuf_querybuf(&fh->vb_vidq, p);
1077 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1080 struct s2255_fh *fh = priv;
1081 rc = videobuf_qbuf(&fh->vb_vidq, p);
1085 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1088 struct s2255_fh *fh = priv;
1089 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1093 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1094 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1096 struct s2255_fh *fh = priv;
1098 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1102 /* write to the configuration pipe, synchronously */
1103 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1110 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1111 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1116 static u32 get_transfer_size(struct s2255_mode *mode)
1118 int linesPerFrame = LINE_SZ_DEF;
1119 int pixelsPerLine = NUM_LINES_DEF;
1122 unsigned int mask_mult;
1127 if (mode->format == FORMAT_NTSC) {
1128 switch (mode->scale) {
1131 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1132 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1135 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1136 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1139 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1140 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1145 } else if (mode->format == FORMAT_PAL) {
1146 switch (mode->scale) {
1149 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1150 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1153 linesPerFrame = NUM_LINES_2CIFS_PAL;
1154 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1157 linesPerFrame = NUM_LINES_1CIFS_PAL;
1158 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1164 outImageSize = linesPerFrame * pixelsPerLine;
1165 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1166 /* 2 bytes/pixel if not monochrome */
1170 /* total bytes to send including prefix and 4K padding;
1171 must be a multiple of USB_READ_SIZE */
1172 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1173 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1174 /* if size not a multiple of USB_READ_SIZE */
1175 if (usbInSize & ~mask_mult)
1176 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1180 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1182 struct device *dev = &sdev->udev->dev;
1183 dev_info(dev, "------------------------------------------------\n");
1184 dev_info(dev, "verify mode\n");
1185 dev_info(dev, "format: %d\n", mode->format);
1186 dev_info(dev, "scale: %d\n", mode->scale);
1187 dev_info(dev, "fdec: %d\n", mode->fdec);
1188 dev_info(dev, "color: %d\n", mode->color);
1189 dev_info(dev, "bright: 0x%x\n", mode->bright);
1190 dev_info(dev, "restart: 0x%x\n", mode->restart);
1191 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1192 dev_info(dev, "single: 0x%x\n", mode->single);
1193 dev_info(dev, "------------------------------------------------\n");
1197 * set mode is the function which controls the DSP.
1198 * the restart parameter in struct s2255_mode should be set whenever
1199 * the image size could change via color format, video system or image
1201 * When the restart parameter is set, we sleep for ONE frame to allow the
1202 * DSP time to get the new frame
1204 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1205 struct s2255_mode *mode)
1209 unsigned long chn_rev;
1211 mutex_lock(&dev->lock);
1212 chn_rev = G_chnmap[chn];
1213 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1214 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1215 dev->mode[chn].scale);
1216 dprintk(2, "mode contrast %x\n", mode->contrast);
1218 /* if JPEG, set the quality */
1219 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1220 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1223 dev->mode[chn] = *mode;
1224 dev->req_image_size[chn] = get_transfer_size(mode);
1225 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1227 buffer = kzalloc(512, GFP_KERNEL);
1228 if (buffer == NULL) {
1229 dev_err(&dev->udev->dev, "out of mem\n");
1230 mutex_unlock(&dev->lock);
1235 buffer[0] = IN_DATA_TOKEN;
1236 buffer[1] = (u32) chn_rev;
1237 buffer[2] = CMD_SET_MODE;
1238 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1239 dev->setmode_ready[chn] = 0;
1240 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1242 dump_verify_mode(dev, mode);
1244 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1246 /* wait at least 3 frames before continuing */
1247 if (mode->restart) {
1248 wait_event_timeout(dev->wait_setmode[chn],
1249 (dev->setmode_ready[chn] != 0),
1250 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1251 if (dev->setmode_ready[chn] != 1) {
1252 printk(KERN_DEBUG "s2255: no set mode response\n");
1257 /* clear the restart flag */
1258 dev->mode[chn].restart = 0;
1259 mutex_unlock(&dev->lock);
1263 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1266 struct s2255_fh *fh = priv;
1267 struct s2255_dev *dev = fh->dev;
1268 struct s2255_mode *new_mode;
1269 struct s2255_mode *old_mode;
1272 dprintk(4, "%s\n", __func__);
1273 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1274 dev_err(&dev->udev->dev, "invalid fh type0\n");
1277 if (i != fh->type) {
1278 dev_err(&dev->udev->dev, "invalid fh type1\n");
1282 if (!res_get(dev, fh)) {
1283 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1287 /* send a set mode command everytime with restart.
1288 in case we switch resolutions or other parameters */
1290 new_mode = &fh->mode;
1291 old_mode = &fh->dev->mode[chn];
1293 if (new_mode->color != old_mode->color)
1294 new_mode->restart = 1;
1295 else if (new_mode->scale != old_mode->scale)
1296 new_mode->restart = 1;
1297 else if (new_mode->format != old_mode->format)
1298 new_mode->restart = 1;
1300 s2255_set_mode(dev, chn, new_mode);
1301 new_mode->restart = 0;
1302 *old_mode = *new_mode;
1303 dev->cur_fmt[chn] = fh->fmt;
1304 dprintk(1, "%s[%d]\n", __func__, chn);
1305 dev->last_frame[chn] = -1;
1306 dev->bad_payload[chn] = 0;
1307 dev->cur_frame[chn] = 0;
1308 dev->frame_count[chn] = 0;
1309 for (j = 0; j < SYS_FRAMES; j++) {
1310 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1311 dev->buffer[chn].frame[j].cur_size = 0;
1313 res = videobuf_streamon(&fh->vb_vidq);
1315 s2255_start_acquire(dev, chn);
1316 dev->b_acquire[chn] = 1;
1323 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1325 struct s2255_fh *fh = priv;
1326 struct s2255_dev *dev = fh->dev;
1328 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1329 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1330 printk(KERN_ERR "invalid fh type0\n");
1333 if (i != fh->type) {
1334 printk(KERN_ERR "invalid type i\n");
1337 s2255_stop_acquire(dev, fh->channel);
1338 videobuf_streamoff(&fh->vb_vidq);
1343 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1345 struct s2255_fh *fh = priv;
1346 struct s2255_mode *mode;
1347 struct videobuf_queue *q = &fh->vb_vidq;
1350 mutex_lock(&q->vb_lock);
1351 if (videobuf_queue_is_busy(q)) {
1352 dprintk(1, "queue busy\n");
1357 if (res_locked(fh->dev, fh)) {
1358 dprintk(1, "can't change standard after started\n");
1364 if (*i & V4L2_STD_NTSC) {
1365 dprintk(4, "vidioc_s_std NTSC\n");
1366 mode->format = FORMAT_NTSC;
1367 } else if (*i & V4L2_STD_PAL) {
1368 dprintk(4, "vidioc_s_std PAL\n");
1369 mode->format = FORMAT_PAL;
1374 mutex_unlock(&q->vb_lock);
1378 /* Sensoray 2255 is a multiple channel capture device.
1379 It does not have a "crossbar" of inputs.
1380 We use one V4L device per channel. The user must
1381 be aware that certain combinations are not allowed.
1382 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1383 at once in color(you can do full fps on 4 channels with greyscale.
1385 static int vidioc_enum_input(struct file *file, void *priv,
1386 struct v4l2_input *inp)
1388 if (inp->index != 0)
1391 inp->type = V4L2_INPUT_TYPE_CAMERA;
1392 inp->std = S2255_NORMS;
1393 strlcpy(inp->name, "Camera", sizeof(inp->name));
1397 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1402 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1409 /* --- controls ---------------------------------------------- */
1410 static int vidioc_queryctrl(struct file *file, void *priv,
1411 struct v4l2_queryctrl *qc)
1415 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1416 if (qc->id && qc->id == s2255_qctrl[i].id) {
1417 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1421 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1425 static int vidioc_g_ctrl(struct file *file, void *priv,
1426 struct v4l2_control *ctrl)
1430 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1431 if (ctrl->id == s2255_qctrl[i].id) {
1432 ctrl->value = qctl_regs[i];
1435 dprintk(4, "g_ctrl -EINVAL\n");
1440 static int vidioc_s_ctrl(struct file *file, void *priv,
1441 struct v4l2_control *ctrl)
1444 struct s2255_fh *fh = priv;
1445 struct s2255_dev *dev = fh->dev;
1446 struct s2255_mode *mode;
1448 dprintk(4, "vidioc_s_ctrl\n");
1449 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1450 if (ctrl->id == s2255_qctrl[i].id) {
1451 if (ctrl->value < s2255_qctrl[i].minimum ||
1452 ctrl->value > s2255_qctrl[i].maximum)
1455 qctl_regs[i] = ctrl->value;
1456 /* update the mode to the corresponding value */
1458 case V4L2_CID_BRIGHTNESS:
1459 mode->bright = ctrl->value;
1461 case V4L2_CID_CONTRAST:
1462 mode->contrast = ctrl->value;
1465 mode->hue = ctrl->value;
1467 case V4L2_CID_SATURATION:
1468 mode->saturation = ctrl->value;
1472 /* set mode here. Note: stream does not need restarted.
1473 some V4L programs restart stream unnecessarily
1476 s2255_set_mode(dev, fh->channel, mode);
1483 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1484 struct v4l2_jpegcompression *jc)
1486 struct s2255_fh *fh = priv;
1487 struct s2255_dev *dev = fh->dev;
1488 *jc = dev->jc[fh->channel];
1489 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1493 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1494 struct v4l2_jpegcompression *jc)
1496 struct s2255_fh *fh = priv;
1497 struct s2255_dev *dev = fh->dev;
1498 if (jc->quality < 0 || jc->quality > 100)
1500 dev->jc[fh->channel].quality = jc->quality;
1501 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1505 static int vidioc_g_parm(struct file *file, void *priv,
1506 struct v4l2_streamparm *sp)
1508 struct s2255_fh *fh = priv;
1509 struct s2255_dev *dev = fh->dev;
1510 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1512 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1513 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1517 static int vidioc_s_parm(struct file *file, void *priv,
1518 struct v4l2_streamparm *sp)
1520 struct s2255_fh *fh = priv;
1521 struct s2255_dev *dev = fh->dev;
1523 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1526 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1527 dprintk(2, "setting param capture mode %d\n",
1528 sp->parm.capture.capturemode);
1531 static int s2255_open(struct file *file)
1533 int minor = video_devdata(file)->minor;
1534 struct video_device *vdev = video_devdata(file);
1535 struct s2255_dev *dev = video_drvdata(file);
1536 struct s2255_fh *fh;
1537 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1539 int cur_channel = -1;
1541 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1545 for (i = 0; i < MAX_CHANNELS; i++) {
1546 if (dev->vdev[i] == vdev) {
1552 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1554 printk(KERN_INFO "disconnecting\n");
1557 kref_get(&dev->kref);
1558 mutex_lock(&dev->open_lock);
1560 dev->users[cur_channel]++;
1561 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1563 switch (atomic_read(&dev->fw_data->fw_state)) {
1564 case S2255_FW_FAILED:
1565 s2255_dev_err(&dev->udev->dev,
1566 "firmware load failed. retrying.\n");
1567 s2255_fwload_start(dev, 1);
1568 wait_event_timeout(dev->fw_data->wait_fw,
1569 ((atomic_read(&dev->fw_data->fw_state)
1570 == S2255_FW_SUCCESS) ||
1571 (atomic_read(&dev->fw_data->fw_state)
1572 == S2255_FW_DISCONNECTING)),
1573 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1575 case S2255_FW_NOTLOADED:
1576 case S2255_FW_LOADED_DSPWAIT:
1577 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1578 driver loaded and then device immediately opened */
1579 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1580 wait_event_timeout(dev->fw_data->wait_fw,
1581 ((atomic_read(&dev->fw_data->fw_state)
1582 == S2255_FW_SUCCESS) ||
1583 (atomic_read(&dev->fw_data->fw_state)
1584 == S2255_FW_DISCONNECTING)),
1585 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1587 case S2255_FW_SUCCESS:
1591 state = atomic_read(&dev->fw_data->fw_state);
1592 if (state != S2255_FW_SUCCESS) {
1595 case S2255_FW_FAILED:
1596 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1599 case S2255_FW_DISCONNECTING:
1600 printk(KERN_INFO "%s: disconnecting\n", __func__);
1603 case S2255_FW_LOADED_DSPWAIT:
1604 case S2255_FW_NOTLOADED:
1605 printk(KERN_INFO "%s: firmware not loaded yet"
1606 "please try again later\n",
1611 printk(KERN_INFO "%s: unknown state\n", __func__);
1615 dev->users[cur_channel]--;
1616 mutex_unlock(&dev->open_lock);
1617 kref_put(&dev->kref, s2255_destroy);
1622 /* allocate + initialize per filehandle data */
1623 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1625 dev->users[cur_channel]--;
1626 mutex_unlock(&dev->open_lock);
1627 kref_put(&dev->kref, s2255_destroy);
1632 file->private_data = fh;
1634 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1635 fh->mode = dev->mode[cur_channel];
1636 fh->fmt = dev->cur_fmt[cur_channel];
1637 /* default 4CIF NTSC */
1638 fh->width = LINE_SZ_4CIFS_NTSC;
1639 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1640 fh->channel = cur_channel;
1642 /* configure channel to default state */
1643 if (!dev->chn_configured[cur_channel]) {
1644 s2255_set_mode(dev, cur_channel, &fh->mode);
1645 dev->chn_configured[cur_channel] = 1;
1649 /* Put all controls at a sane state */
1650 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1651 qctl_regs[i] = s2255_qctrl[i].default_value;
1653 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1654 minor, v4l2_type_names[type], dev->users[cur_channel]);
1655 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1656 (unsigned long)fh, (unsigned long)dev,
1657 (unsigned long)&dev->vidq[cur_channel]);
1658 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1659 list_empty(&dev->vidq[cur_channel].active));
1661 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1664 V4L2_FIELD_INTERLACED,
1665 sizeof(struct s2255_buffer), fh);
1667 mutex_unlock(&dev->open_lock);
1673 static unsigned int s2255_poll(struct file *file,
1674 struct poll_table_struct *wait)
1676 struct s2255_fh *fh = file->private_data;
1678 dprintk(100, "%s\n", __func__);
1680 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1683 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1687 static void s2255_destroy(struct kref *kref)
1689 struct s2255_dev *dev = to_s2255_dev(kref);
1692 printk(KERN_ERR "s2255drv: kref problem\n");
1695 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1696 wake_up(&dev->fw_data->wait_fw);
1697 for (i = 0; i < MAX_CHANNELS; i++) {
1698 dev->setmode_ready[i] = 1;
1699 wake_up(&dev->wait_setmode[i]);
1701 mutex_lock(&dev->open_lock);
1702 /* reset the DSP so firmware can be reload next time */
1703 s2255_reset_dsppower(dev);
1704 s2255_exit_v4l(dev);
1705 /* board shutdown stops the read pipe if it is running */
1706 s2255_board_shutdown(dev);
1707 /* make sure firmware still not trying to load */
1708 del_timer(&dev->timer); /* only started in .probe and .open */
1710 if (dev->fw_data->fw_urb) {
1711 dprintk(2, "kill fw_urb\n");
1712 usb_kill_urb(dev->fw_data->fw_urb);
1713 usb_free_urb(dev->fw_data->fw_urb);
1714 dev->fw_data->fw_urb = NULL;
1716 if (dev->fw_data->fw)
1717 release_firmware(dev->fw_data->fw);
1718 kfree(dev->fw_data->pfw_data);
1719 kfree(dev->fw_data);
1720 usb_put_dev(dev->udev);
1721 dprintk(1, "%s", __func__);
1723 mutex_unlock(&dev->open_lock);
1727 static int s2255_close(struct file *file)
1729 struct s2255_fh *fh = file->private_data;
1730 struct s2255_dev *dev = fh->dev;
1731 int minor = video_devdata(file)->minor;
1735 mutex_lock(&dev->open_lock);
1737 /* turn off stream */
1738 if (res_check(fh)) {
1739 if (dev->b_acquire[fh->channel])
1740 s2255_stop_acquire(dev, fh->channel);
1741 videobuf_streamoff(&fh->vb_vidq);
1745 videobuf_mmap_free(&fh->vb_vidq);
1746 dev->users[fh->channel]--;
1748 mutex_unlock(&dev->open_lock);
1750 kref_put(&dev->kref, s2255_destroy);
1751 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1752 minor, dev->users[fh->channel]);
1757 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1759 struct s2255_fh *fh = file->private_data;
1764 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1766 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1768 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1769 (unsigned long)vma->vm_start,
1770 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1775 static const struct v4l2_file_operations s2255_fops_v4l = {
1776 .owner = THIS_MODULE,
1778 .release = s2255_close,
1780 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1781 .mmap = s2255_mmap_v4l,
1784 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1785 .vidioc_querycap = vidioc_querycap,
1786 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1787 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1788 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1789 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1790 .vidioc_reqbufs = vidioc_reqbufs,
1791 .vidioc_querybuf = vidioc_querybuf,
1792 .vidioc_qbuf = vidioc_qbuf,
1793 .vidioc_dqbuf = vidioc_dqbuf,
1794 .vidioc_s_std = vidioc_s_std,
1795 .vidioc_enum_input = vidioc_enum_input,
1796 .vidioc_g_input = vidioc_g_input,
1797 .vidioc_s_input = vidioc_s_input,
1798 .vidioc_queryctrl = vidioc_queryctrl,
1799 .vidioc_g_ctrl = vidioc_g_ctrl,
1800 .vidioc_s_ctrl = vidioc_s_ctrl,
1801 .vidioc_streamon = vidioc_streamon,
1802 .vidioc_streamoff = vidioc_streamoff,
1803 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1804 .vidiocgmbuf = vidioc_cgmbuf,
1806 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1807 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1808 .vidioc_s_parm = vidioc_s_parm,
1809 .vidioc_g_parm = vidioc_g_parm,
1812 static struct video_device template = {
1814 .fops = &s2255_fops_v4l,
1815 .ioctl_ops = &s2255_ioctl_ops,
1817 .release = video_device_release,
1818 .tvnorms = S2255_NORMS,
1819 .current_norm = V4L2_STD_NTSC_M,
1822 static int s2255_probe_v4l(struct s2255_dev *dev)
1826 int cur_nr = video_nr;
1828 /* initialize all video 4 linux */
1829 /* register 4 video devices */
1830 for (i = 0; i < MAX_CHANNELS; i++) {
1831 INIT_LIST_HEAD(&dev->vidq[i].active);
1832 dev->vidq[i].dev = dev;
1833 dev->vidq[i].channel = i;
1834 /* register 4 video devices */
1835 dev->vdev[i] = video_device_alloc();
1836 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1837 dev->vdev[i]->parent = &dev->interface->dev;
1838 video_set_drvdata(dev->vdev[i], dev);
1840 ret = video_register_device(dev->vdev[i],
1844 ret = video_register_device(dev->vdev[i],
1847 video_set_drvdata(dev->vdev[i], dev);
1850 dev_err(&dev->udev->dev,
1851 "failed to register video device!\n");
1855 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1856 S2255_MAJOR_VERSION,
1857 S2255_MINOR_VERSION);
1861 static void s2255_exit_v4l(struct s2255_dev *dev)
1865 for (i = 0; i < MAX_CHANNELS; i++) {
1866 if (video_is_registered(dev->vdev[i])) {
1867 video_unregister_device(dev->vdev[i]);
1868 printk(KERN_INFO "s2255 unregistered\n");
1870 video_device_release(dev->vdev[i]);
1871 printk(KERN_INFO "s2255 released\n");
1876 /* this function moves the usb stream read pipe data
1877 * into the system buffers.
1878 * returns 0 on success, EAGAIN if more data to process( call this
1881 * Received frame structure:
1882 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1883 * bytes 4-7: channel: 0-3
1884 * bytes 8-11: payload size: size of the frame
1885 * bytes 12-payloadsize+12: frame data
1887 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1893 unsigned long copy_size;
1896 struct s2255_framei *frm;
1897 unsigned char *pdata;
1899 dprintk(100, "buffer to user\n");
1901 idx = dev->cur_frame[dev->cc];
1902 frm = &dev->buffer[dev->cc].frame[idx];
1904 if (frm->ulState == S2255_READ_IDLE) {
1909 /* search for marker codes */
1910 pdata = (unsigned char *)pipe_info->transfer_buffer;
1911 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1912 switch (*(s32 *) pdata) {
1913 case S2255_MARKER_FRAME:
1914 pdword = (s32 *)pdata;
1915 dprintk(4, "found frame marker at offset:"
1916 " %d [%x %x]\n", jj, pdata[0],
1918 offset = jj + PREFIX_SIZE;
1921 if (cc >= MAX_CHANNELS) {
1927 dev->cc = G_chnmap[cc];
1928 payload = pdword[3];
1929 if (payload > dev->req_image_size[dev->cc]) {
1930 dev->bad_payload[dev->cc]++;
1931 /* discard the bad frame */
1934 dev->pkt_size[dev->cc] = payload;
1935 dev->jpg_size[dev->cc] = pdword[4];
1937 case S2255_MARKER_RESPONSE:
1938 pdword = (s32 *)pdata;
1939 pdata += DEF_USB_BLOCK;
1940 jj += DEF_USB_BLOCK;
1941 if (pdword[1] >= MAX_CHANNELS)
1943 cc = G_chnmap[pdword[1]];
1944 if (cc >= MAX_CHANNELS)
1946 switch (pdword[2]) {
1947 case S2255_RESPONSE_SETMODE:
1948 /* check if channel valid */
1949 /* set mode ready */
1950 dev->setmode_ready[cc] = 1;
1951 wake_up(&dev->wait_setmode[cc]);
1952 dprintk(5, "setmode ready %d\n", cc);
1954 case S2255_RESPONSE_FW:
1956 dev->chn_ready |= (1 << cc);
1957 if ((dev->chn_ready & 0x0f) != 0x0f)
1959 /* all channels ready */
1960 printk(KERN_INFO "s2255: fw loaded\n");
1961 atomic_set(&dev->fw_data->fw_state,
1963 wake_up(&dev->fw_data->wait_fw);
1966 printk(KERN_INFO "s2255 unknown resp\n");
1980 idx = dev->cur_frame[dev->cc];
1981 frm = &dev->buffer[dev->cc].frame[idx];
1983 /* search done. now find out if should be acquiring on this channel */
1984 if (!dev->b_acquire[dev->cc]) {
1985 /* we found a frame, but this channel is turned off */
1986 frm->ulState = S2255_READ_IDLE;
1990 if (frm->ulState == S2255_READ_IDLE) {
1991 frm->ulState = S2255_READ_FRAME;
1995 /* skip the marker 512 bytes (and offset if out of sync) */
1996 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1999 if (frm->lpvbits == NULL) {
2000 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2001 frm, dev, dev->cc, idx);
2005 pdest = frm->lpvbits + frm->cur_size;
2007 copy_size = (pipe_info->cur_transfer_size - offset);
2009 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2011 /* sanity check on pdest */
2012 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2013 memcpy(pdest, psrc, copy_size);
2015 frm->cur_size += copy_size;
2016 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2018 if (frm->cur_size >= size) {
2021 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2023 dev->last_frame[cc] = dev->cur_frame[cc];
2024 dev->cur_frame[cc]++;
2025 /* end of system frame ring buffer, start at zero */
2026 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2027 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2028 dev->cur_frame[cc] = 0;
2030 if (dev->b_acquire[cc])
2031 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2032 dev->frame_count[cc]++;
2033 frm->ulState = S2255_READ_IDLE;
2037 /* done successfully */
2041 static void s2255_read_video_callback(struct s2255_dev *dev,
2042 struct s2255_pipeinfo *pipe_info)
2045 dprintk(50, "callback read video \n");
2047 if (dev->cc >= MAX_CHANNELS) {
2049 dev_err(&dev->udev->dev, "invalid channel\n");
2052 /* otherwise copy to the system buffers */
2053 res = save_frame(dev, pipe_info);
2055 dprintk(4, "s2255: read callback failed\n");
2057 dprintk(50, "callback read video done\n");
2061 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2062 u16 Index, u16 Value, void *TransferBuffer,
2063 s32 TransferBufferLength, int bOut)
2067 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2069 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2071 Value, Index, TransferBuffer,
2072 TransferBufferLength, HZ * 5);
2074 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2075 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2076 Value, Index, TransferBuffer,
2077 TransferBufferLength, HZ * 5);
2083 * retrieve FX2 firmware version. future use.
2084 * @param dev pointer to device extension
2085 * @return -1 for fail, else returns firmware version as an int(16 bits)
2087 static int s2255_get_fx2fw(struct s2255_dev *dev)
2091 unsigned char transBuffer[64];
2092 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2095 dprintk(2, "get fw error: %x\n", ret);
2096 fw = transBuffer[0] + (transBuffer[1] << 8);
2097 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2102 * Create the system ring buffer to copy frames into from the
2105 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2108 unsigned long reqsize;
2109 dprintk(1, "create sys buffers\n");
2110 if (chn >= MAX_CHANNELS)
2113 dev->buffer[chn].dwFrames = SYS_FRAMES;
2115 /* always allocate maximum size(PAL) for system buffers */
2116 reqsize = SYS_FRAMES_MAXSIZE;
2118 if (reqsize > SYS_FRAMES_MAXSIZE)
2119 reqsize = SYS_FRAMES_MAXSIZE;
2121 for (i = 0; i < SYS_FRAMES; i++) {
2122 /* allocate the frames */
2123 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2125 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2126 &dev->buffer[chn].frame[i], chn, i,
2127 dev->buffer[chn].frame[i].lpvbits);
2128 dev->buffer[chn].frame[i].size = reqsize;
2129 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2130 printk(KERN_INFO "out of memory. using less frames\n");
2131 dev->buffer[chn].dwFrames = i;
2136 /* make sure internal states are set */
2137 for (i = 0; i < SYS_FRAMES; i++) {
2138 dev->buffer[chn].frame[i].ulState = 0;
2139 dev->buffer[chn].frame[i].cur_size = 0;
2142 dev->cur_frame[chn] = 0;
2143 dev->last_frame[chn] = -1;
2147 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2148 unsigned long channel)
2151 dprintk(1, "release sys buffers\n");
2152 for (i = 0; i < SYS_FRAMES; i++) {
2153 if (dev->buffer[channel].frame[i].lpvbits) {
2154 dprintk(1, "vfree %p\n",
2155 dev->buffer[channel].frame[i].lpvbits);
2156 vfree(dev->buffer[channel].frame[i].lpvbits);
2158 dev->buffer[channel].frame[i].lpvbits = NULL;
2163 static int s2255_board_init(struct s2255_dev *dev)
2166 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2168 dprintk(4, "board init: %p", dev);
2170 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2171 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2173 memset(pipe, 0, sizeof(*pipe));
2175 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2176 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2178 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2180 if (pipe->transfer_buffer == NULL) {
2181 dprintk(1, "out of memory!\n");
2187 /* query the firmware */
2188 fw_ver = s2255_get_fx2fw(dev);
2190 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2191 (fw_ver >> 8) & 0xff,
2194 if (fw_ver < S2255_CUR_USB_FWVER)
2195 dev_err(&dev->udev->dev,
2196 "usb firmware not up to date %d.%d\n",
2197 (fw_ver >> 8) & 0xff,
2200 for (j = 0; j < MAX_CHANNELS; j++) {
2201 dev->b_acquire[j] = 0;
2202 dev->mode[j] = mode_def;
2203 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2204 dev->cur_fmt[j] = &formats[0];
2205 dev->mode[j].restart = 1;
2206 dev->req_image_size[j] = get_transfer_size(&mode_def);
2207 dev->frame_count[j] = 0;
2208 /* create the system buffers */
2209 s2255_create_sys_buffers(dev, j);
2211 /* start read pipe */
2212 s2255_start_readpipe(dev);
2214 dprintk(1, "S2255: board initialized\n");
2218 static int s2255_board_shutdown(struct s2255_dev *dev)
2222 dprintk(1, "S2255: board shutdown: %p", dev);
2224 for (i = 0; i < MAX_CHANNELS; i++) {
2225 if (dev->b_acquire[i])
2226 s2255_stop_acquire(dev, i);
2229 s2255_stop_readpipe(dev);
2231 for (i = 0; i < MAX_CHANNELS; i++)
2232 s2255_release_sys_buffers(dev, i);
2234 /* release transfer buffers */
2235 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2236 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2237 kfree(pipe->transfer_buffer);
2242 static void read_pipe_completion(struct urb *purb)
2244 struct s2255_pipeinfo *pipe_info;
2245 struct s2255_dev *dev;
2249 pipe_info = purb->context;
2250 dprintk(100, "read pipe completion %p, status %d\n", purb,
2252 if (pipe_info == NULL) {
2253 dev_err(&purb->dev->dev, "no context!\n");
2257 dev = pipe_info->dev;
2259 dev_err(&purb->dev->dev, "no context!\n");
2262 status = purb->status;
2263 /* if shutting down, do not resubmit, exit immediately */
2264 if (status == -ESHUTDOWN) {
2265 dprintk(2, "read_pipe_completion: err shutdown\n");
2266 pipe_info->err_count++;
2270 if (pipe_info->state == 0) {
2271 dprintk(2, "exiting USB pipe");
2276 s2255_read_video_callback(dev, pipe_info);
2278 pipe_info->err_count++;
2279 dprintk(1, "s2255drv: failed URB %d\n", status);
2282 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2284 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2286 pipe_info->transfer_buffer,
2287 pipe_info->cur_transfer_size,
2288 read_pipe_completion, pipe_info);
2290 if (pipe_info->state != 0) {
2291 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2292 dev_err(&dev->udev->dev, "error submitting urb\n");
2295 dprintk(2, "read pipe complete state 0\n");
2300 static int s2255_start_readpipe(struct s2255_dev *dev)
2305 struct s2255_pipeinfo *pipe_info = dev->pipes;
2306 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2307 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2309 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2310 pipe_info->state = 1;
2311 pipe_info->err_count = 0;
2312 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2313 if (!pipe_info->stream_urb) {
2314 dev_err(&dev->udev->dev,
2315 "ReadStream: Unable to alloc URB\n");
2318 /* transfer buffer allocated in board_init */
2319 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2321 pipe_info->transfer_buffer,
2322 pipe_info->cur_transfer_size,
2323 read_pipe_completion, pipe_info);
2325 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2326 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2328 printk(KERN_ERR "s2255: start read pipe failed\n");
2336 /* starts acquisition process */
2337 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2339 unsigned char *buffer;
2341 unsigned long chn_rev;
2343 if (chn >= MAX_CHANNELS) {
2344 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2348 chn_rev = G_chnmap[chn];
2349 dprintk(1, "S2255: start acquire %lu \n", chn);
2351 buffer = kzalloc(512, GFP_KERNEL);
2352 if (buffer == NULL) {
2353 dev_err(&dev->udev->dev, "out of mem\n");
2357 dev->last_frame[chn] = -1;
2358 dev->bad_payload[chn] = 0;
2359 dev->cur_frame[chn] = 0;
2360 for (j = 0; j < SYS_FRAMES; j++) {
2361 dev->buffer[chn].frame[j].ulState = 0;
2362 dev->buffer[chn].frame[j].cur_size = 0;
2365 /* send the start command */
2366 *(u32 *) buffer = IN_DATA_TOKEN;
2367 *((u32 *) buffer + 1) = (u32) chn_rev;
2368 *((u32 *) buffer + 2) = (u32) CMD_START;
2369 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2371 dev_err(&dev->udev->dev, "CMD_START error\n");
2373 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2378 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2380 unsigned char *buffer;
2382 unsigned long chn_rev;
2384 if (chn >= MAX_CHANNELS) {
2385 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2388 chn_rev = G_chnmap[chn];
2390 buffer = kzalloc(512, GFP_KERNEL);
2391 if (buffer == NULL) {
2392 dev_err(&dev->udev->dev, "out of mem\n");
2396 /* send the stop command */
2397 dprintk(4, "stop acquire %lu\n", chn);
2398 *(u32 *) buffer = IN_DATA_TOKEN;
2399 *((u32 *) buffer + 1) = (u32) chn_rev;
2400 *((u32 *) buffer + 2) = CMD_STOP;
2401 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2404 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2406 dprintk(4, "stop acquire: releasing states \n");
2409 dev->b_acquire[chn] = 0;
2414 static void s2255_stop_readpipe(struct s2255_dev *dev)
2419 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2422 dprintk(4, "stop read pipe\n");
2423 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2424 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2426 if (pipe_info->state == 0)
2428 pipe_info->state = 0;
2432 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2433 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2434 if (pipe_info->stream_urb) {
2436 usb_kill_urb(pipe_info->stream_urb);
2437 usb_free_urb(pipe_info->stream_urb);
2438 pipe_info->stream_urb = NULL;
2441 dprintk(2, "s2255 stop read pipe: %d\n", j);
2445 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2448 s2255_reset_dsppower(dev);
2449 dev->fw_data->fw_size = dev->fw_data->fw->size;
2450 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2451 memcpy(dev->fw_data->pfw_data,
2452 dev->fw_data->fw->data, CHUNK_SIZE);
2453 dev->fw_data->fw_loaded = CHUNK_SIZE;
2454 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2455 usb_sndbulkpipe(dev->udev, 2),
2456 dev->fw_data->pfw_data,
2457 CHUNK_SIZE, s2255_fwchunk_complete,
2459 mod_timer(&dev->timer, jiffies + HZ);
2462 /* standard usb probe function */
2463 static int s2255_probe(struct usb_interface *interface,
2464 const struct usb_device_id *id)
2466 struct s2255_dev *dev = NULL;
2467 struct usb_host_interface *iface_desc;
2468 struct usb_endpoint_descriptor *endpoint;
2470 int retval = -ENOMEM;
2474 dprintk(2, "s2255: probe\n");
2476 /* allocate memory for our device state and initialize it to zero */
2477 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2479 s2255_dev_err(&interface->dev, "out of memory\n");
2483 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2487 mutex_init(&dev->lock);
2488 mutex_init(&dev->open_lock);
2490 /* grab usb_device and save it */
2491 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2492 if (dev->udev == NULL) {
2493 dev_err(&interface->dev, "null usb device\n");
2497 kref_init(&dev->kref);
2498 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2499 dev->udev, interface);
2500 dev->interface = interface;
2501 /* set up the endpoint information */
2502 iface_desc = interface->cur_altsetting;
2503 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2504 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2505 endpoint = &iface_desc->endpoint[i].desc;
2506 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2507 /* we found the bulk in endpoint */
2508 dev->read_endpoint = endpoint->bEndpointAddress;
2512 if (!dev->read_endpoint) {
2513 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2518 usb_set_intfdata(interface, dev);
2520 dprintk(100, "after intfdata %p\n", dev);
2522 init_timer(&dev->timer);
2523 dev->timer.function = s2255_timer;
2524 dev->timer.data = (unsigned long)dev->fw_data;
2526 init_waitqueue_head(&dev->fw_data->wait_fw);
2527 for (i = 0; i < MAX_CHANNELS; i++)
2528 init_waitqueue_head(&dev->wait_setmode[i]);
2531 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2533 if (!dev->fw_data->fw_urb) {
2534 dev_err(&interface->dev, "out of memory!\n");
2537 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2538 if (!dev->fw_data->pfw_data) {
2539 dev_err(&interface->dev, "out of memory!\n");
2542 /* load the first chunk */
2543 if (request_firmware(&dev->fw_data->fw,
2544 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2545 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2548 /* check the firmware is valid */
2549 fw_size = dev->fw_data->fw->size;
2550 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2552 if (*pdata != S2255_FW_MARKER) {
2553 printk(KERN_INFO "Firmware invalid.\n");
2557 /* make sure firmware is the latest */
2559 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2560 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2562 /* loads v4l specific */
2563 s2255_probe_v4l(dev);
2564 usb_reset_device(dev->udev);
2565 /* load 2255 board specific */
2566 retval = s2255_board_init(dev);
2570 dprintk(4, "before probe done %p\n", dev);
2571 spin_lock_init(&dev->slock);
2573 s2255_fwload_start(dev, 0);
2574 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2580 /* disconnect routine. when board is removed physically or with rmmod */
2581 static void s2255_disconnect(struct usb_interface *interface)
2583 struct s2255_dev *dev = NULL;
2585 dprintk(1, "s2255: disconnect interface %p\n", interface);
2586 dev = usb_get_intfdata(interface);
2589 * wake up any of the timers to allow open_lock to be
2592 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2593 wake_up(&dev->fw_data->wait_fw);
2594 for (i = 0; i < MAX_CHANNELS; i++) {
2595 dev->setmode_ready[i] = 1;
2596 wake_up(&dev->wait_setmode[i]);
2599 mutex_lock(&dev->open_lock);
2600 usb_set_intfdata(interface, NULL);
2601 mutex_unlock(&dev->open_lock);
2604 kref_put(&dev->kref, s2255_destroy);
2605 dprintk(1, "s2255drv: disconnect\n");
2606 dev_info(&interface->dev, "s2255usb now disconnected\n");
2610 static struct usb_driver s2255_driver = {
2611 .name = S2255_DRIVER_NAME,
2612 .probe = s2255_probe,
2613 .disconnect = s2255_disconnect,
2614 .id_table = s2255_table,
2617 static int __init usb_s2255_init(void)
2621 /* register this driver with the USB subsystem */
2622 result = usb_register(&s2255_driver);
2625 pr_err(KBUILD_MODNAME
2626 ": usb_register failed. Error number %d\n", result);
2628 dprintk(2, "s2255_init: done\n");
2632 static void __exit usb_s2255_exit(void)
2634 usb_deregister(&s2255_driver);
2637 module_init(usb_s2255_init);
2638 module_exit(usb_s2255_exit);
2640 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2641 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2642 MODULE_LICENSE("GPL");