powerpc: use consistent types in mktree
[zen-stable.git] / drivers / media / video / s2255drv.c
blob9e3262c0ba371b1c935100504b8e6444964f7d51
1 /*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2008 by Sensoray Company Inc.
5 * Dean Anderson
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
25 * at once.
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/module.h>
45 #include <linux/firmware.h>
46 #include <linux/kernel.h>
47 #include <linux/mutex.h>
48 #include <linux/videodev2.h>
49 #include <linux/version.h>
50 #include <linux/mm.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 */
65 #define S2255_VR_IN 0
66 /* vendor request out */
67 #define S2255_VR_OUT 1
68 /* firmware query */
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
86 #define SYS_FRAMES 4
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
108 #define FORMAT_PAL 2
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
135 #define DEF_BRIGHT 0
136 #define DEF_CONTRAST 0x5c
137 #define DEF_SATURATION 0x80
138 #define DEF_HUE 0
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)
148 struct s2255_mode {
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 {
168 unsigned long size;
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;
187 int channel;
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
201 struct s2255_fw {
202 int fw_loaded;
203 int fw_size;
204 struct urb *fw_urb;
205 atomic_t fw_state;
206 void *pfw_data;
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;
214 u8 *transfer_buffer;
215 u32 state;
216 void *stream_urb;
217 void *dev; /* back pointer to s2255_dev struct*/
218 u32 err_count;
219 u32 idx;
222 struct s2255_fmt; /*forward declaration */
224 struct s2255_dev {
225 int frames;
226 int users[MAX_CHANNELS];
227 struct mutex lock;
228 struct mutex open_lock;
229 int resources[MAX_CHANNELS];
230 struct usb_device *udev;
231 struct usb_interface *interface;
232 u8 read_endpoint;
234 struct s2255_dmaqueue vidq[MAX_CHANNELS];
235 struct video_device *vdev[MAX_CHANNELS];
236 struct list_head s2255_devlist;
237 struct timer_list timer;
238 struct s2255_fw *fw_data;
239 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
240 struct s2255_bufferi buffer[MAX_CHANNELS];
241 struct s2255_mode mode[MAX_CHANNELS];
242 /* jpeg compression */
243 struct v4l2_jpegcompression jc[MAX_CHANNELS];
244 /* capture parameters (for high quality mode full size) */
245 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
246 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
247 int cur_frame[MAX_CHANNELS];
248 int last_frame[MAX_CHANNELS];
249 u32 cc; /* current channel */
250 int b_acquire[MAX_CHANNELS];
251 /* allocated image size */
252 unsigned long req_image_size[MAX_CHANNELS];
253 /* received packet size */
254 unsigned long pkt_size[MAX_CHANNELS];
255 int bad_payload[MAX_CHANNELS];
256 unsigned long frame_count[MAX_CHANNELS];
257 int frame_ready;
258 /* if JPEG image */
259 int jpg_size[MAX_CHANNELS];
260 /* if channel configured to default state */
261 int chn_configured[MAX_CHANNELS];
262 wait_queue_head_t wait_setmode[MAX_CHANNELS];
263 int setmode_ready[MAX_CHANNELS];
264 int chn_ready;
265 struct kref kref;
266 spinlock_t slock;
268 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
270 struct s2255_fmt {
271 char *name;
272 u32 fourcc;
273 int depth;
276 /* buffer for one video frame */
277 struct s2255_buffer {
278 /* common v4l buffer stuff -- must be first */
279 struct videobuf_buffer vb;
280 const struct s2255_fmt *fmt;
283 struct s2255_fh {
284 struct s2255_dev *dev;
285 const struct s2255_fmt *fmt;
286 unsigned int width;
287 unsigned int height;
288 struct videobuf_queue vb_vidq;
289 enum v4l2_buf_type type;
290 int channel;
291 /* mode below is the desired mode.
292 mode in s2255_dev is the current mode that was last set */
293 struct s2255_mode mode;
294 int resources[MAX_CHANNELS];
297 /* current cypress EEPROM firmware version */
298 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
299 #define S2255_MAJOR_VERSION 1
300 #define S2255_MINOR_VERSION 14
301 #define S2255_RELEASE 0
302 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
303 S2255_MINOR_VERSION, \
304 S2255_RELEASE)
306 /* vendor ids */
307 #define USB_S2255_VENDOR_ID 0x1943
308 #define USB_S2255_PRODUCT_ID 0x2255
309 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
310 /* frame prefix size (sent once every frame) */
311 #define PREFIX_SIZE 512
313 /* Channels on box are in reverse order */
314 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
316 static LIST_HEAD(s2255_devlist);
318 static int debug;
319 static int *s2255_debug = &debug;
321 static int s2255_start_readpipe(struct s2255_dev *dev);
322 static void s2255_stop_readpipe(struct s2255_dev *dev);
323 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
324 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
325 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
326 int chn, int jpgsize);
327 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
328 struct s2255_mode *mode);
329 static int s2255_board_shutdown(struct s2255_dev *dev);
330 static void s2255_exit_v4l(struct s2255_dev *dev);
331 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
332 static void s2255_destroy(struct kref *kref);
333 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
334 u16 index, u16 value, void *buf,
335 s32 buf_len, int bOut);
337 /* dev_err macro with driver name */
338 #define S2255_DRIVER_NAME "s2255"
339 #define s2255_dev_err(dev, fmt, arg...) \
340 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
342 #define dprintk(level, fmt, arg...) \
343 do { \
344 if (*s2255_debug >= (level)) { \
345 printk(KERN_DEBUG S2255_DRIVER_NAME \
346 ": " fmt, ##arg); \
348 } while (0)
350 static struct usb_driver s2255_driver;
353 /* Declare static vars that will be used as parameters */
354 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
356 /* start video number */
357 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
359 module_param(debug, int, 0644);
360 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
361 module_param(vid_limit, int, 0644);
362 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
363 module_param(video_nr, int, 0644);
364 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
366 /* USB device table */
367 static struct usb_device_id s2255_table[] = {
368 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
369 { } /* Terminating entry */
371 MODULE_DEVICE_TABLE(usb, s2255_table);
374 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
376 /* supported controls */
377 static struct v4l2_queryctrl s2255_qctrl[] = {
379 .id = V4L2_CID_BRIGHTNESS,
380 .type = V4L2_CTRL_TYPE_INTEGER,
381 .name = "Brightness",
382 .minimum = -127,
383 .maximum = 128,
384 .step = 1,
385 .default_value = 0,
386 .flags = 0,
387 }, {
388 .id = V4L2_CID_CONTRAST,
389 .type = V4L2_CTRL_TYPE_INTEGER,
390 .name = "Contrast",
391 .minimum = 0,
392 .maximum = 255,
393 .step = 0x1,
394 .default_value = DEF_CONTRAST,
395 .flags = 0,
396 }, {
397 .id = V4L2_CID_SATURATION,
398 .type = V4L2_CTRL_TYPE_INTEGER,
399 .name = "Saturation",
400 .minimum = 0,
401 .maximum = 255,
402 .step = 0x1,
403 .default_value = DEF_SATURATION,
404 .flags = 0,
405 }, {
406 .id = V4L2_CID_HUE,
407 .type = V4L2_CTRL_TYPE_INTEGER,
408 .name = "Hue",
409 .minimum = 0,
410 .maximum = 255,
411 .step = 0x1,
412 .default_value = DEF_HUE,
413 .flags = 0,
417 static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
419 /* image formats. */
420 static const struct s2255_fmt formats[] = {
422 .name = "4:2:2, planar, YUV422P",
423 .fourcc = V4L2_PIX_FMT_YUV422P,
424 .depth = 16
426 }, {
427 .name = "4:2:2, packed, YUYV",
428 .fourcc = V4L2_PIX_FMT_YUYV,
429 .depth = 16
431 }, {
432 .name = "4:2:2, packed, UYVY",
433 .fourcc = V4L2_PIX_FMT_UYVY,
434 .depth = 16
435 }, {
436 .name = "JPG",
437 .fourcc = V4L2_PIX_FMT_JPEG,
438 .depth = 24
439 }, {
440 .name = "8bpp GREY",
441 .fourcc = V4L2_PIX_FMT_GREY,
442 .depth = 8
446 static int norm_maxw(struct video_device *vdev)
448 return (vdev->current_norm & V4L2_STD_NTSC) ?
449 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
452 static int norm_maxh(struct video_device *vdev)
454 return (vdev->current_norm & V4L2_STD_NTSC) ?
455 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
458 static int norm_minw(struct video_device *vdev)
460 return (vdev->current_norm & V4L2_STD_NTSC) ?
461 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
464 static int norm_minh(struct video_device *vdev)
466 return (vdev->current_norm & V4L2_STD_NTSC) ?
467 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
472 * TODO: fixme: move YUV reordering to hardware
473 * converts 2255 planar format to yuyv or uyvy
475 static void planar422p_to_yuv_packed(const unsigned char *in,
476 unsigned char *out,
477 int width, int height,
478 int fmt)
480 unsigned char *pY;
481 unsigned char *pCb;
482 unsigned char *pCr;
483 unsigned long size = height * width;
484 unsigned int i;
485 pY = (unsigned char *)in;
486 pCr = (unsigned char *)in + height * width;
487 pCb = (unsigned char *)in + height * width + (height * width / 2);
488 for (i = 0; i < size * 2; i += 4) {
489 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
490 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
491 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
492 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
494 return;
497 static void s2255_reset_dsppower(struct s2255_dev *dev)
499 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
500 msleep(10);
501 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
502 return;
505 /* kickstarts the firmware loading. from probe
507 static void s2255_timer(unsigned long user_data)
509 struct s2255_fw *data = (struct s2255_fw *)user_data;
510 dprintk(100, "s2255 timer\n");
511 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
512 printk(KERN_ERR "s2255: can't submit urb\n");
513 atomic_set(&data->fw_state, S2255_FW_FAILED);
514 /* wake up anything waiting for the firmware */
515 wake_up(&data->wait_fw);
516 return;
521 /* this loads the firmware asynchronously.
522 Originally this was done synchroously in probe.
523 But it is better to load it asynchronously here than block
524 inside the probe function. Blocking inside probe affects boot time.
525 FW loading is triggered by the timer in the probe function
527 static void s2255_fwchunk_complete(struct urb *urb)
529 struct s2255_fw *data = urb->context;
530 struct usb_device *udev = urb->dev;
531 int len;
532 dprintk(100, "udev %p urb %p", udev, urb);
533 if (urb->status) {
534 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
535 atomic_set(&data->fw_state, S2255_FW_FAILED);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data->wait_fw);
538 return;
540 if (data->fw_urb == NULL) {
541 s2255_dev_err(&udev->dev, "disconnected\n");
542 atomic_set(&data->fw_state, S2255_FW_FAILED);
543 /* wake up anything waiting for the firmware */
544 wake_up(&data->wait_fw);
545 return;
547 #define CHUNK_SIZE 512
548 /* all USB transfers must be done with continuous kernel memory.
549 can't allocate more than 128k in current linux kernel, so
550 upload the firmware in chunks
552 if (data->fw_loaded < data->fw_size) {
553 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
554 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
556 if (len < CHUNK_SIZE)
557 memset(data->pfw_data, 0, CHUNK_SIZE);
559 dprintk(100, "completed len %d, loaded %d \n", len,
560 data->fw_loaded);
562 memcpy(data->pfw_data,
563 (char *) data->fw->data + data->fw_loaded, len);
565 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
566 data->pfw_data, CHUNK_SIZE,
567 s2255_fwchunk_complete, data);
568 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
569 dev_err(&udev->dev, "failed submit URB\n");
570 atomic_set(&data->fw_state, S2255_FW_FAILED);
571 /* wake up anything waiting for the firmware */
572 wake_up(&data->wait_fw);
573 return;
575 data->fw_loaded += len;
576 } else {
577 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
579 dprintk(100, "2255 complete done\n");
580 return;
584 static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
586 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
587 struct s2255_buffer *buf;
588 unsigned long flags = 0;
589 int rc = 0;
590 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
591 spin_lock_irqsave(&dev->slock, flags);
593 if (list_empty(&dma_q->active)) {
594 dprintk(1, "No active queue to serve\n");
595 rc = -1;
596 goto unlock;
598 buf = list_entry(dma_q->active.next,
599 struct s2255_buffer, vb.queue);
601 if (!waitqueue_active(&buf->vb.done)) {
602 /* no one active */
603 rc = -1;
604 goto unlock;
606 list_del(&buf->vb.queue);
607 do_gettimeofday(&buf->vb.ts);
608 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
609 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
610 wake_up(&buf->vb.done);
611 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
612 unlock:
613 spin_unlock_irqrestore(&dev->slock, flags);
614 return 0;
618 static const struct s2255_fmt *format_by_fourcc(int fourcc)
620 unsigned int i;
622 for (i = 0; i < ARRAY_SIZE(formats); i++) {
623 if (-1 == formats[i].fourcc)
624 continue;
625 if (formats[i].fourcc == fourcc)
626 return formats + i;
628 return NULL;
634 /* video buffer vmalloc implementation based partly on VIVI driver which is
635 * Copyright (c) 2006 by
636 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
637 * Ted Walther <ted--a.t--enumera.com>
638 * John Sokol <sokol--a.t--videotechnology.com>
639 * http://v4l.videotechnology.com/
642 static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
643 int chn, int jpgsize)
645 int pos = 0;
646 struct timeval ts;
647 const char *tmpbuf;
648 char *vbuf = videobuf_to_vmalloc(&buf->vb);
649 unsigned long last_frame;
650 struct s2255_framei *frm;
652 if (!vbuf)
653 return;
655 last_frame = dev->last_frame[chn];
656 if (last_frame != -1) {
657 frm = &dev->buffer[chn].frame[last_frame];
658 tmpbuf =
659 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
660 switch (buf->fmt->fourcc) {
661 case V4L2_PIX_FMT_YUYV:
662 case V4L2_PIX_FMT_UYVY:
663 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
664 vbuf, buf->vb.width,
665 buf->vb.height,
666 buf->fmt->fourcc);
667 break;
668 case V4L2_PIX_FMT_GREY:
669 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
670 break;
671 case V4L2_PIX_FMT_JPEG:
672 buf->vb.size = jpgsize;
673 memcpy(vbuf, tmpbuf, buf->vb.size);
674 break;
675 case V4L2_PIX_FMT_YUV422P:
676 memcpy(vbuf, tmpbuf,
677 buf->vb.width * buf->vb.height * 2);
678 break;
679 default:
680 printk(KERN_DEBUG "s2255: unknown format?\n");
682 dev->last_frame[chn] = -1;
683 } else {
684 printk(KERN_ERR "s2255: =======no frame\n");
685 return;
688 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
689 (unsigned long)vbuf, pos);
690 /* tell v4l buffer was filled */
692 buf->vb.field_count = dev->frame_count[chn] * 2;
693 do_gettimeofday(&ts);
694 buf->vb.ts = ts;
695 buf->vb.state = VIDEOBUF_DONE;
699 /* ------------------------------------------------------------------
700 Videobuf operations
701 ------------------------------------------------------------------*/
703 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
704 unsigned int *size)
706 struct s2255_fh *fh = vq->priv_data;
708 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
710 if (0 == *count)
711 *count = S2255_DEF_BUFS;
713 while (*size * (*count) > vid_limit * 1024 * 1024)
714 (*count)--;
716 return 0;
719 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
721 dprintk(4, "%s\n", __func__);
723 videobuf_vmalloc_free(&buf->vb);
724 buf->vb.state = VIDEOBUF_NEEDS_INIT;
727 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
728 enum v4l2_field field)
730 struct s2255_fh *fh = vq->priv_data;
731 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
732 int rc;
733 dprintk(4, "%s, field=%d\n", __func__, field);
734 if (fh->fmt == NULL)
735 return -EINVAL;
737 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
738 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
739 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
740 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
741 dprintk(4, "invalid buffer prepare\n");
742 return -EINVAL;
745 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
747 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
748 dprintk(4, "invalid buffer prepare\n");
749 return -EINVAL;
752 buf->fmt = fh->fmt;
753 buf->vb.width = fh->width;
754 buf->vb.height = fh->height;
755 buf->vb.field = field;
758 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
759 rc = videobuf_iolock(vq, &buf->vb, NULL);
760 if (rc < 0)
761 goto fail;
764 buf->vb.state = VIDEOBUF_PREPARED;
765 return 0;
766 fail:
767 free_buffer(vq, buf);
768 return rc;
771 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
773 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
774 struct s2255_fh *fh = vq->priv_data;
775 struct s2255_dev *dev = fh->dev;
776 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
778 dprintk(1, "%s\n", __func__);
780 buf->vb.state = VIDEOBUF_QUEUED;
781 list_add_tail(&buf->vb.queue, &vidq->active);
784 static void buffer_release(struct videobuf_queue *vq,
785 struct videobuf_buffer *vb)
787 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
788 struct s2255_fh *fh = vq->priv_data;
789 dprintk(4, "%s %d\n", __func__, fh->channel);
790 free_buffer(vq, buf);
793 static struct videobuf_queue_ops s2255_video_qops = {
794 .buf_setup = buffer_setup,
795 .buf_prepare = buffer_prepare,
796 .buf_queue = buffer_queue,
797 .buf_release = buffer_release,
801 static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
803 /* is it free? */
804 mutex_lock(&dev->lock);
805 if (dev->resources[fh->channel]) {
806 /* no, someone else uses it */
807 mutex_unlock(&dev->lock);
808 return 0;
810 /* it's free, grab it */
811 dev->resources[fh->channel] = 1;
812 fh->resources[fh->channel] = 1;
813 dprintk(1, "s2255: res: get\n");
814 mutex_unlock(&dev->lock);
815 return 1;
818 static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
820 return dev->resources[fh->channel];
823 static int res_check(struct s2255_fh *fh)
825 return fh->resources[fh->channel];
829 static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
831 mutex_lock(&dev->lock);
832 dev->resources[fh->channel] = 0;
833 fh->resources[fh->channel] = 0;
834 mutex_unlock(&dev->lock);
835 dprintk(1, "res: put\n");
839 static int vidioc_querycap(struct file *file, void *priv,
840 struct v4l2_capability *cap)
842 struct s2255_fh *fh = file->private_data;
843 struct s2255_dev *dev = fh->dev;
844 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
845 strlcpy(cap->card, "s2255", sizeof(cap->card));
846 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
847 cap->version = S2255_VERSION;
848 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
849 return 0;
852 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
853 struct v4l2_fmtdesc *f)
855 int index = 0;
856 if (f)
857 index = f->index;
859 if (index >= ARRAY_SIZE(formats))
860 return -EINVAL;
862 dprintk(4, "name %s\n", formats[index].name);
863 strlcpy(f->description, formats[index].name, sizeof(f->description));
864 f->pixelformat = formats[index].fourcc;
865 return 0;
868 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
869 struct v4l2_format *f)
871 struct s2255_fh *fh = priv;
873 f->fmt.pix.width = fh->width;
874 f->fmt.pix.height = fh->height;
875 f->fmt.pix.field = fh->vb_vidq.field;
876 f->fmt.pix.pixelformat = fh->fmt->fourcc;
877 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
878 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
879 return 0;
882 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
883 struct v4l2_format *f)
885 const struct s2255_fmt *fmt;
886 enum v4l2_field field;
887 int b_any_field = 0;
888 struct s2255_fh *fh = priv;
889 struct s2255_dev *dev = fh->dev;
890 int is_ntsc;
892 is_ntsc =
893 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
895 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
897 if (fmt == NULL)
898 return -EINVAL;
900 field = f->fmt.pix.field;
901 if (field == V4L2_FIELD_ANY)
902 b_any_field = 1;
904 dprintk(4, "try format %d \n", is_ntsc);
905 /* supports 3 sizes. see s2255drv.h */
906 dprintk(50, "width test %d, height %d\n",
907 f->fmt.pix.width, f->fmt.pix.height);
908 if (is_ntsc) {
909 /* NTSC */
910 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
911 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
912 if (b_any_field) {
913 field = V4L2_FIELD_SEQ_TB;
914 } else if (!((field == V4L2_FIELD_INTERLACED) ||
915 (field == V4L2_FIELD_SEQ_TB) ||
916 (field == V4L2_FIELD_INTERLACED_TB))) {
917 dprintk(1, "unsupported field setting\n");
918 return -EINVAL;
920 } else {
921 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
922 if (b_any_field) {
923 field = V4L2_FIELD_TOP;
924 } else if (!((field == V4L2_FIELD_TOP) ||
925 (field == V4L2_FIELD_BOTTOM))) {
926 dprintk(1, "unsupported field setting\n");
927 return -EINVAL;
931 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
932 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
933 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
934 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
935 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
936 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
937 else
938 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
939 } else {
940 /* PAL */
941 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
942 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
943 if (b_any_field) {
944 field = V4L2_FIELD_SEQ_TB;
945 } else if (!((field == V4L2_FIELD_INTERLACED) ||
946 (field == V4L2_FIELD_SEQ_TB) ||
947 (field == V4L2_FIELD_INTERLACED_TB))) {
948 dprintk(1, "unsupported field setting\n");
949 return -EINVAL;
951 } else {
952 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
953 if (b_any_field) {
954 field = V4L2_FIELD_TOP;
955 } else if (!((field == V4L2_FIELD_TOP) ||
956 (field == V4L2_FIELD_BOTTOM))) {
957 dprintk(1, "unsupported field setting\n");
958 return -EINVAL;
961 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
962 dprintk(50, "pal 704\n");
963 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
964 field = V4L2_FIELD_SEQ_TB;
965 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
966 dprintk(50, "pal 352A\n");
967 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
968 field = V4L2_FIELD_TOP;
969 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
970 dprintk(50, "pal 352B\n");
971 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
972 field = V4L2_FIELD_TOP;
973 } else {
974 dprintk(50, "pal 352C\n");
975 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
976 field = V4L2_FIELD_TOP;
980 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
981 f->fmt.pix.height, f->fmt.pix.field);
982 f->fmt.pix.field = field;
983 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
984 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
985 return 0;
988 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
989 struct v4l2_format *f)
991 struct s2255_fh *fh = priv;
992 const struct s2255_fmt *fmt;
993 struct videobuf_queue *q = &fh->vb_vidq;
994 int ret;
995 int norm;
997 ret = vidioc_try_fmt_vid_cap(file, fh, f);
999 if (ret < 0)
1000 return ret;
1002 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1004 if (fmt == NULL)
1005 return -EINVAL;
1007 mutex_lock(&q->vb_lock);
1009 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1010 dprintk(1, "queue busy\n");
1011 ret = -EBUSY;
1012 goto out_s_fmt;
1015 if (res_locked(fh->dev, fh)) {
1016 dprintk(1, "can't change format after started\n");
1017 ret = -EBUSY;
1018 goto out_s_fmt;
1021 fh->fmt = fmt;
1022 fh->width = f->fmt.pix.width;
1023 fh->height = f->fmt.pix.height;
1024 fh->vb_vidq.field = f->fmt.pix.field;
1025 fh->type = f->type;
1026 norm = norm_minw(fh->dev->vdev[fh->channel]);
1027 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1028 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1029 if (fh->dev->cap_parm[fh->channel].capturemode &
1030 V4L2_MODE_HIGHQUALITY) {
1031 fh->mode.scale = SCALE_4CIFSI;
1032 dprintk(2, "scale 4CIFSI\n");
1033 } else {
1034 fh->mode.scale = SCALE_4CIFS;
1035 dprintk(2, "scale 4CIFS\n");
1037 } else
1038 fh->mode.scale = SCALE_2CIFS;
1040 } else {
1041 fh->mode.scale = SCALE_1CIFS;
1044 /* color mode */
1045 switch (fh->fmt->fourcc) {
1046 case V4L2_PIX_FMT_GREY:
1047 fh->mode.color = COLOR_Y8;
1048 break;
1049 case V4L2_PIX_FMT_JPEG:
1050 fh->mode.color = COLOR_JPG |
1051 (fh->dev->jc[fh->channel].quality << 8);
1052 break;
1053 case V4L2_PIX_FMT_YUV422P:
1054 fh->mode.color = COLOR_YUVPL;
1055 break;
1056 case V4L2_PIX_FMT_YUYV:
1057 case V4L2_PIX_FMT_UYVY:
1058 default:
1059 fh->mode.color = COLOR_YUVPK;
1060 break;
1062 ret = 0;
1063 out_s_fmt:
1064 mutex_unlock(&q->vb_lock);
1065 return ret;
1068 static int vidioc_reqbufs(struct file *file, void *priv,
1069 struct v4l2_requestbuffers *p)
1071 int rc;
1072 struct s2255_fh *fh = priv;
1073 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1074 return rc;
1077 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1079 int rc;
1080 struct s2255_fh *fh = priv;
1081 rc = videobuf_querybuf(&fh->vb_vidq, p);
1082 return rc;
1085 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1087 int rc;
1088 struct s2255_fh *fh = priv;
1089 rc = videobuf_qbuf(&fh->vb_vidq, p);
1090 return rc;
1093 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1095 int rc;
1096 struct s2255_fh *fh = priv;
1097 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1098 return rc;
1101 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1102 static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1104 struct s2255_fh *fh = priv;
1106 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1108 #endif
1110 /* write to the configuration pipe, synchronously */
1111 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1112 int size)
1114 int pipe;
1115 int done;
1116 long retval = -1;
1117 if (udev) {
1118 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1119 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1121 return retval;
1124 static u32 get_transfer_size(struct s2255_mode *mode)
1126 int linesPerFrame = LINE_SZ_DEF;
1127 int pixelsPerLine = NUM_LINES_DEF;
1128 u32 outImageSize;
1129 u32 usbInSize;
1130 unsigned int mask_mult;
1132 if (mode == NULL)
1133 return 0;
1135 if (mode->format == FORMAT_NTSC) {
1136 switch (mode->scale) {
1137 case SCALE_4CIFS:
1138 case SCALE_4CIFSI:
1139 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1140 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1141 break;
1142 case SCALE_2CIFS:
1143 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1144 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1145 break;
1146 case SCALE_1CIFS:
1147 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1148 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1149 break;
1150 default:
1151 break;
1153 } else if (mode->format == FORMAT_PAL) {
1154 switch (mode->scale) {
1155 case SCALE_4CIFS:
1156 case SCALE_4CIFSI:
1157 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1158 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1159 break;
1160 case SCALE_2CIFS:
1161 linesPerFrame = NUM_LINES_2CIFS_PAL;
1162 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1163 break;
1164 case SCALE_1CIFS:
1165 linesPerFrame = NUM_LINES_1CIFS_PAL;
1166 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1167 break;
1168 default:
1169 break;
1172 outImageSize = linesPerFrame * pixelsPerLine;
1173 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1174 /* 2 bytes/pixel if not monochrome */
1175 outImageSize *= 2;
1178 /* total bytes to send including prefix and 4K padding;
1179 must be a multiple of USB_READ_SIZE */
1180 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1181 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1182 /* if size not a multiple of USB_READ_SIZE */
1183 if (usbInSize & ~mask_mult)
1184 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1185 return usbInSize;
1188 static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1190 struct device *dev = &sdev->udev->dev;
1191 dev_info(dev, "------------------------------------------------\n");
1192 dev_info(dev, "verify mode\n");
1193 dev_info(dev, "format: %d\n", mode->format);
1194 dev_info(dev, "scale: %d\n", mode->scale);
1195 dev_info(dev, "fdec: %d\n", mode->fdec);
1196 dev_info(dev, "color: %d\n", mode->color);
1197 dev_info(dev, "bright: 0x%x\n", mode->bright);
1198 dev_info(dev, "restart: 0x%x\n", mode->restart);
1199 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1200 dev_info(dev, "single: 0x%x\n", mode->single);
1201 dev_info(dev, "------------------------------------------------\n");
1205 * set mode is the function which controls the DSP.
1206 * the restart parameter in struct s2255_mode should be set whenever
1207 * the image size could change via color format, video system or image
1208 * size.
1209 * When the restart parameter is set, we sleep for ONE frame to allow the
1210 * DSP time to get the new frame
1212 static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1213 struct s2255_mode *mode)
1215 int res;
1216 u32 *buffer;
1217 unsigned long chn_rev;
1219 mutex_lock(&dev->lock);
1220 chn_rev = G_chnmap[chn];
1221 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1222 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1223 dev->mode[chn].scale);
1224 dprintk(2, "mode contrast %x\n", mode->contrast);
1226 /* if JPEG, set the quality */
1227 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1228 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1230 /* save the mode */
1231 dev->mode[chn] = *mode;
1232 dev->req_image_size[chn] = get_transfer_size(mode);
1233 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1235 buffer = kzalloc(512, GFP_KERNEL);
1236 if (buffer == NULL) {
1237 dev_err(&dev->udev->dev, "out of mem\n");
1238 mutex_unlock(&dev->lock);
1239 return -ENOMEM;
1242 /* set the mode */
1243 buffer[0] = IN_DATA_TOKEN;
1244 buffer[1] = (u32) chn_rev;
1245 buffer[2] = CMD_SET_MODE;
1246 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1247 dev->setmode_ready[chn] = 0;
1248 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1249 if (debug)
1250 dump_verify_mode(dev, mode);
1251 kfree(buffer);
1252 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1254 /* wait at least 3 frames before continuing */
1255 if (mode->restart) {
1256 wait_event_timeout(dev->wait_setmode[chn],
1257 (dev->setmode_ready[chn] != 0),
1258 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1259 if (dev->setmode_ready[chn] != 1) {
1260 printk(KERN_DEBUG "s2255: no set mode response\n");
1261 res = -EFAULT;
1265 /* clear the restart flag */
1266 dev->mode[chn].restart = 0;
1267 mutex_unlock(&dev->lock);
1268 return res;
1271 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1273 int res;
1274 struct s2255_fh *fh = priv;
1275 struct s2255_dev *dev = fh->dev;
1276 struct s2255_mode *new_mode;
1277 struct s2255_mode *old_mode;
1278 int chn;
1279 int j;
1280 dprintk(4, "%s\n", __func__);
1281 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1282 dev_err(&dev->udev->dev, "invalid fh type0\n");
1283 return -EINVAL;
1285 if (i != fh->type) {
1286 dev_err(&dev->udev->dev, "invalid fh type1\n");
1287 return -EINVAL;
1290 if (!res_get(dev, fh)) {
1291 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1292 return -EBUSY;
1295 /* send a set mode command everytime with restart.
1296 in case we switch resolutions or other parameters */
1297 chn = fh->channel;
1298 new_mode = &fh->mode;
1299 old_mode = &fh->dev->mode[chn];
1301 if (new_mode->color != old_mode->color)
1302 new_mode->restart = 1;
1303 else if (new_mode->scale != old_mode->scale)
1304 new_mode->restart = 1;
1305 else if (new_mode->format != old_mode->format)
1306 new_mode->restart = 1;
1308 s2255_set_mode(dev, chn, new_mode);
1309 new_mode->restart = 0;
1310 *old_mode = *new_mode;
1311 dev->cur_fmt[chn] = fh->fmt;
1312 dprintk(1, "%s[%d]\n", __func__, chn);
1313 dev->last_frame[chn] = -1;
1314 dev->bad_payload[chn] = 0;
1315 dev->cur_frame[chn] = 0;
1316 dev->frame_count[chn] = 0;
1317 for (j = 0; j < SYS_FRAMES; j++) {
1318 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1319 dev->buffer[chn].frame[j].cur_size = 0;
1321 res = videobuf_streamon(&fh->vb_vidq);
1322 if (res == 0) {
1323 s2255_start_acquire(dev, chn);
1324 dev->b_acquire[chn] = 1;
1325 } else {
1326 res_free(dev, fh);
1328 return res;
1331 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1333 struct s2255_fh *fh = priv;
1334 struct s2255_dev *dev = fh->dev;
1336 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1337 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1338 printk(KERN_ERR "invalid fh type0\n");
1339 return -EINVAL;
1341 if (i != fh->type) {
1342 printk(KERN_ERR "invalid type i\n");
1343 return -EINVAL;
1345 s2255_stop_acquire(dev, fh->channel);
1346 videobuf_streamoff(&fh->vb_vidq);
1347 res_free(dev, fh);
1348 return 0;
1351 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1353 struct s2255_fh *fh = priv;
1354 struct s2255_mode *mode;
1355 struct videobuf_queue *q = &fh->vb_vidq;
1356 int ret = 0;
1358 mutex_lock(&q->vb_lock);
1359 if (videobuf_queue_is_busy(q)) {
1360 dprintk(1, "queue busy\n");
1361 ret = -EBUSY;
1362 goto out_s_std;
1365 if (res_locked(fh->dev, fh)) {
1366 dprintk(1, "can't change standard after started\n");
1367 ret = -EBUSY;
1368 goto out_s_std;
1370 mode = &fh->mode;
1372 if (*i & V4L2_STD_NTSC) {
1373 dprintk(4, "vidioc_s_std NTSC\n");
1374 mode->format = FORMAT_NTSC;
1375 } else if (*i & V4L2_STD_PAL) {
1376 dprintk(4, "vidioc_s_std PAL\n");
1377 mode->format = FORMAT_PAL;
1378 } else {
1379 ret = -EINVAL;
1381 out_s_std:
1382 mutex_unlock(&q->vb_lock);
1383 return ret;
1386 /* Sensoray 2255 is a multiple channel capture device.
1387 It does not have a "crossbar" of inputs.
1388 We use one V4L device per channel. The user must
1389 be aware that certain combinations are not allowed.
1390 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1391 at once in color(you can do full fps on 4 channels with greyscale.
1393 static int vidioc_enum_input(struct file *file, void *priv,
1394 struct v4l2_input *inp)
1396 if (inp->index != 0)
1397 return -EINVAL;
1399 inp->type = V4L2_INPUT_TYPE_CAMERA;
1400 inp->std = S2255_NORMS;
1401 strlcpy(inp->name, "Camera", sizeof(inp->name));
1402 return 0;
1405 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1407 *i = 0;
1408 return 0;
1410 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1412 if (i > 0)
1413 return -EINVAL;
1414 return 0;
1417 /* --- controls ---------------------------------------------- */
1418 static int vidioc_queryctrl(struct file *file, void *priv,
1419 struct v4l2_queryctrl *qc)
1421 int i;
1423 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1424 if (qc->id && qc->id == s2255_qctrl[i].id) {
1425 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
1426 return 0;
1429 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1430 return -EINVAL;
1433 static int vidioc_g_ctrl(struct file *file, void *priv,
1434 struct v4l2_control *ctrl)
1436 int i;
1438 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1439 if (ctrl->id == s2255_qctrl[i].id) {
1440 ctrl->value = qctl_regs[i];
1441 return 0;
1443 dprintk(4, "g_ctrl -EINVAL\n");
1445 return -EINVAL;
1448 static int vidioc_s_ctrl(struct file *file, void *priv,
1449 struct v4l2_control *ctrl)
1451 int i;
1452 struct s2255_fh *fh = priv;
1453 struct s2255_dev *dev = fh->dev;
1454 struct s2255_mode *mode;
1455 mode = &fh->mode;
1456 dprintk(4, "vidioc_s_ctrl\n");
1457 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1458 if (ctrl->id == s2255_qctrl[i].id) {
1459 if (ctrl->value < s2255_qctrl[i].minimum ||
1460 ctrl->value > s2255_qctrl[i].maximum)
1461 return -ERANGE;
1463 qctl_regs[i] = ctrl->value;
1464 /* update the mode to the corresponding value */
1465 switch (ctrl->id) {
1466 case V4L2_CID_BRIGHTNESS:
1467 mode->bright = ctrl->value;
1468 break;
1469 case V4L2_CID_CONTRAST:
1470 mode->contrast = ctrl->value;
1471 break;
1472 case V4L2_CID_HUE:
1473 mode->hue = ctrl->value;
1474 break;
1475 case V4L2_CID_SATURATION:
1476 mode->saturation = ctrl->value;
1477 break;
1479 mode->restart = 0;
1480 /* set mode here. Note: stream does not need restarted.
1481 some V4L programs restart stream unnecessarily
1482 after a s_crtl.
1484 s2255_set_mode(dev, fh->channel, mode);
1485 return 0;
1488 return -EINVAL;
1491 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1492 struct v4l2_jpegcompression *jc)
1494 struct s2255_fh *fh = priv;
1495 struct s2255_dev *dev = fh->dev;
1496 *jc = dev->jc[fh->channel];
1497 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1498 return 0;
1501 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1502 struct v4l2_jpegcompression *jc)
1504 struct s2255_fh *fh = priv;
1505 struct s2255_dev *dev = fh->dev;
1506 if (jc->quality < 0 || jc->quality > 100)
1507 return -EINVAL;
1508 dev->jc[fh->channel].quality = jc->quality;
1509 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1510 return 0;
1513 static int vidioc_g_parm(struct file *file, void *priv,
1514 struct v4l2_streamparm *sp)
1516 struct s2255_fh *fh = priv;
1517 struct s2255_dev *dev = fh->dev;
1518 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1519 return -EINVAL;
1520 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1521 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1522 return 0;
1525 static int vidioc_s_parm(struct file *file, void *priv,
1526 struct v4l2_streamparm *sp)
1528 struct s2255_fh *fh = priv;
1529 struct s2255_dev *dev = fh->dev;
1531 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1532 return -EINVAL;
1534 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1535 dprintk(2, "setting param capture mode %d\n",
1536 sp->parm.capture.capturemode);
1537 return 0;
1539 static int s2255_open(struct file *file)
1541 int minor = video_devdata(file)->minor;
1542 struct s2255_dev *h, *dev = NULL;
1543 struct s2255_fh *fh;
1544 struct list_head *list;
1545 enum v4l2_buf_type type = 0;
1546 int i = 0;
1547 int cur_channel = -1;
1548 int state;
1549 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1551 lock_kernel();
1552 list_for_each(list, &s2255_devlist) {
1553 h = list_entry(list, struct s2255_dev, s2255_devlist);
1554 for (i = 0; i < MAX_CHANNELS; i++) {
1555 if (h->vdev[i]->minor == minor) {
1556 cur_channel = i;
1557 dev = h;
1558 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1563 if ((NULL == dev) || (cur_channel == -1)) {
1564 unlock_kernel();
1565 printk(KERN_INFO "s2255: openv4l no dev\n");
1566 return -ENODEV;
1569 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1570 unlock_kernel();
1571 printk(KERN_INFO "disconnecting\n");
1572 return -ENODEV;
1574 kref_get(&dev->kref);
1575 mutex_lock(&dev->open_lock);
1577 dev->users[cur_channel]++;
1578 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1580 switch (atomic_read(&dev->fw_data->fw_state)) {
1581 case S2255_FW_FAILED:
1582 s2255_dev_err(&dev->udev->dev,
1583 "firmware load failed. retrying.\n");
1584 s2255_fwload_start(dev, 1);
1585 wait_event_timeout(dev->fw_data->wait_fw,
1586 ((atomic_read(&dev->fw_data->fw_state)
1587 == S2255_FW_SUCCESS) ||
1588 (atomic_read(&dev->fw_data->fw_state)
1589 == S2255_FW_DISCONNECTING)),
1590 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1591 break;
1592 case S2255_FW_NOTLOADED:
1593 case S2255_FW_LOADED_DSPWAIT:
1594 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1595 driver loaded and then device immediately opened */
1596 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1597 wait_event_timeout(dev->fw_data->wait_fw,
1598 ((atomic_read(&dev->fw_data->fw_state)
1599 == S2255_FW_SUCCESS) ||
1600 (atomic_read(&dev->fw_data->fw_state)
1601 == S2255_FW_DISCONNECTING)),
1602 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1603 break;
1604 case S2255_FW_SUCCESS:
1605 default:
1606 break;
1608 state = atomic_read(&dev->fw_data->fw_state);
1609 if (state != S2255_FW_SUCCESS) {
1610 int rc;
1611 switch (state) {
1612 case S2255_FW_FAILED:
1613 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1614 rc = -ENODEV;
1615 break;
1616 case S2255_FW_DISCONNECTING:
1617 printk(KERN_INFO "%s: disconnecting\n", __func__);
1618 rc = -ENODEV;
1619 break;
1620 case S2255_FW_LOADED_DSPWAIT:
1621 case S2255_FW_NOTLOADED:
1622 printk(KERN_INFO "%s: firmware not loaded yet"
1623 "please try again later\n",
1624 __func__);
1625 rc = -EAGAIN;
1626 break;
1627 default:
1628 printk(KERN_INFO "%s: unknown state\n", __func__);
1629 rc = -EFAULT;
1630 break;
1632 dev->users[cur_channel]--;
1633 mutex_unlock(&dev->open_lock);
1634 kref_put(&dev->kref, s2255_destroy);
1635 unlock_kernel();
1636 return rc;
1639 /* allocate + initialize per filehandle data */
1640 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1641 if (NULL == fh) {
1642 dev->users[cur_channel]--;
1643 mutex_unlock(&dev->open_lock);
1644 kref_put(&dev->kref, s2255_destroy);
1645 unlock_kernel();
1646 return -ENOMEM;
1649 file->private_data = fh;
1650 fh->dev = dev;
1651 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1652 fh->mode = dev->mode[cur_channel];
1653 fh->fmt = dev->cur_fmt[cur_channel];
1654 /* default 4CIF NTSC */
1655 fh->width = LINE_SZ_4CIFS_NTSC;
1656 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1657 fh->channel = cur_channel;
1659 /* configure channel to default state */
1660 if (!dev->chn_configured[cur_channel]) {
1661 s2255_set_mode(dev, cur_channel, &fh->mode);
1662 dev->chn_configured[cur_channel] = 1;
1666 /* Put all controls at a sane state */
1667 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1668 qctl_regs[i] = s2255_qctrl[i].default_value;
1670 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1671 minor, v4l2_type_names[type], dev->users[cur_channel]);
1672 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1673 (unsigned long)fh, (unsigned long)dev,
1674 (unsigned long)&dev->vidq[cur_channel]);
1675 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1676 list_empty(&dev->vidq[cur_channel].active));
1678 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1679 NULL, &dev->slock,
1680 fh->type,
1681 V4L2_FIELD_INTERLACED,
1682 sizeof(struct s2255_buffer), fh);
1684 mutex_unlock(&dev->open_lock);
1685 unlock_kernel();
1686 return 0;
1690 static unsigned int s2255_poll(struct file *file,
1691 struct poll_table_struct *wait)
1693 struct s2255_fh *fh = file->private_data;
1694 int rc;
1695 dprintk(100, "%s\n", __func__);
1697 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1698 return POLLERR;
1700 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1701 return rc;
1704 static void s2255_destroy(struct kref *kref)
1706 struct s2255_dev *dev = to_s2255_dev(kref);
1707 struct list_head *list;
1708 int i;
1709 if (!dev) {
1710 printk(KERN_ERR "s2255drv: kref problem\n");
1711 return;
1713 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1714 wake_up(&dev->fw_data->wait_fw);
1715 for (i = 0; i < MAX_CHANNELS; i++) {
1716 dev->setmode_ready[i] = 1;
1717 wake_up(&dev->wait_setmode[i]);
1719 mutex_lock(&dev->open_lock);
1720 /* reset the DSP so firmware can be reload next time */
1721 s2255_reset_dsppower(dev);
1722 s2255_exit_v4l(dev);
1723 /* board shutdown stops the read pipe if it is running */
1724 s2255_board_shutdown(dev);
1725 /* make sure firmware still not trying to load */
1726 del_timer(&dev->timer); /* only started in .probe and .open */
1728 if (dev->fw_data->fw_urb) {
1729 dprintk(2, "kill fw_urb\n");
1730 usb_kill_urb(dev->fw_data->fw_urb);
1731 usb_free_urb(dev->fw_data->fw_urb);
1732 dev->fw_data->fw_urb = NULL;
1734 if (dev->fw_data->fw)
1735 release_firmware(dev->fw_data->fw);
1736 kfree(dev->fw_data->pfw_data);
1737 kfree(dev->fw_data);
1738 usb_put_dev(dev->udev);
1739 dprintk(1, "%s", __func__);
1741 while (!list_empty(&s2255_devlist)) {
1742 list = s2255_devlist.next;
1743 list_del(list);
1745 mutex_unlock(&dev->open_lock);
1746 kfree(dev);
1749 static int s2255_close(struct file *file)
1751 struct s2255_fh *fh = file->private_data;
1752 struct s2255_dev *dev = fh->dev;
1753 int minor = video_devdata(file)->minor;
1754 if (!dev)
1755 return -ENODEV;
1757 mutex_lock(&dev->open_lock);
1759 /* turn off stream */
1760 if (res_check(fh)) {
1761 if (dev->b_acquire[fh->channel])
1762 s2255_stop_acquire(dev, fh->channel);
1763 videobuf_streamoff(&fh->vb_vidq);
1764 res_free(dev, fh);
1767 videobuf_mmap_free(&fh->vb_vidq);
1768 dev->users[fh->channel]--;
1770 mutex_unlock(&dev->open_lock);
1772 kref_put(&dev->kref, s2255_destroy);
1773 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1774 minor, dev->users[fh->channel]);
1775 kfree(fh);
1776 return 0;
1779 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1781 struct s2255_fh *fh = file->private_data;
1782 int ret;
1784 if (!fh)
1785 return -ENODEV;
1786 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1788 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1790 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1791 (unsigned long)vma->vm_start,
1792 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1794 return ret;
1797 static const struct v4l2_file_operations s2255_fops_v4l = {
1798 .owner = THIS_MODULE,
1799 .open = s2255_open,
1800 .release = s2255_close,
1801 .poll = s2255_poll,
1802 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1803 .mmap = s2255_mmap_v4l,
1806 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1807 .vidioc_querycap = vidioc_querycap,
1808 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1809 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1810 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1811 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1812 .vidioc_reqbufs = vidioc_reqbufs,
1813 .vidioc_querybuf = vidioc_querybuf,
1814 .vidioc_qbuf = vidioc_qbuf,
1815 .vidioc_dqbuf = vidioc_dqbuf,
1816 .vidioc_s_std = vidioc_s_std,
1817 .vidioc_enum_input = vidioc_enum_input,
1818 .vidioc_g_input = vidioc_g_input,
1819 .vidioc_s_input = vidioc_s_input,
1820 .vidioc_queryctrl = vidioc_queryctrl,
1821 .vidioc_g_ctrl = vidioc_g_ctrl,
1822 .vidioc_s_ctrl = vidioc_s_ctrl,
1823 .vidioc_streamon = vidioc_streamon,
1824 .vidioc_streamoff = vidioc_streamoff,
1825 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1826 .vidiocgmbuf = vidioc_cgmbuf,
1827 #endif
1828 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1829 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1830 .vidioc_s_parm = vidioc_s_parm,
1831 .vidioc_g_parm = vidioc_g_parm,
1834 static struct video_device template = {
1835 .name = "s2255v",
1836 .fops = &s2255_fops_v4l,
1837 .ioctl_ops = &s2255_ioctl_ops,
1838 .minor = -1,
1839 .release = video_device_release,
1840 .tvnorms = S2255_NORMS,
1841 .current_norm = V4L2_STD_NTSC_M,
1844 static int s2255_probe_v4l(struct s2255_dev *dev)
1846 int ret;
1847 int i;
1848 int cur_nr = video_nr;
1850 /* initialize all video 4 linux */
1851 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1852 /* register 4 video devices */
1853 for (i = 0; i < MAX_CHANNELS; i++) {
1854 INIT_LIST_HEAD(&dev->vidq[i].active);
1855 dev->vidq[i].dev = dev;
1856 dev->vidq[i].channel = i;
1857 /* register 4 video devices */
1858 dev->vdev[i] = video_device_alloc();
1859 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
1860 dev->vdev[i]->parent = &dev->interface->dev;
1861 if (video_nr == -1)
1862 ret = video_register_device(dev->vdev[i],
1863 VFL_TYPE_GRABBER,
1864 video_nr);
1865 else
1866 ret = video_register_device(dev->vdev[i],
1867 VFL_TYPE_GRABBER,
1868 cur_nr + i);
1869 video_set_drvdata(dev->vdev[i], dev);
1871 if (ret != 0) {
1872 dev_err(&dev->udev->dev,
1873 "failed to register video device!\n");
1874 return ret;
1877 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1878 S2255_MAJOR_VERSION,
1879 S2255_MINOR_VERSION);
1880 return ret;
1883 static void s2255_exit_v4l(struct s2255_dev *dev)
1886 int i;
1887 for (i = 0; i < MAX_CHANNELS; i++) {
1888 if (-1 != dev->vdev[i]->minor) {
1889 video_unregister_device(dev->vdev[i]);
1890 printk(KERN_INFO "s2255 unregistered\n");
1891 } else {
1892 video_device_release(dev->vdev[i]);
1893 printk(KERN_INFO "s2255 released\n");
1898 /* this function moves the usb stream read pipe data
1899 * into the system buffers.
1900 * returns 0 on success, EAGAIN if more data to process( call this
1901 * function again).
1903 * Received frame structure:
1904 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1905 * bytes 4-7: channel: 0-3
1906 * bytes 8-11: payload size: size of the frame
1907 * bytes 12-payloadsize+12: frame data
1909 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1911 char *pdest;
1912 u32 offset = 0;
1913 int bframe = 0;
1914 char *psrc;
1915 unsigned long copy_size;
1916 unsigned long size;
1917 s32 idx = -1;
1918 struct s2255_framei *frm;
1919 unsigned char *pdata;
1921 dprintk(100, "buffer to user\n");
1923 idx = dev->cur_frame[dev->cc];
1924 frm = &dev->buffer[dev->cc].frame[idx];
1926 if (frm->ulState == S2255_READ_IDLE) {
1927 int jj;
1928 unsigned int cc;
1929 s32 *pdword;
1930 int payload;
1931 /* search for marker codes */
1932 pdata = (unsigned char *)pipe_info->transfer_buffer;
1933 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1934 switch (*(s32 *) pdata) {
1935 case S2255_MARKER_FRAME:
1936 pdword = (s32 *)pdata;
1937 dprintk(4, "found frame marker at offset:"
1938 " %d [%x %x]\n", jj, pdata[0],
1939 pdata[1]);
1940 offset = jj + PREFIX_SIZE;
1941 bframe = 1;
1942 cc = pdword[1];
1943 if (cc >= MAX_CHANNELS) {
1944 printk(KERN_ERR
1945 "bad channel\n");
1946 return -EINVAL;
1948 /* reverse it */
1949 dev->cc = G_chnmap[cc];
1950 payload = pdword[3];
1951 if (payload > dev->req_image_size[dev->cc]) {
1952 dev->bad_payload[dev->cc]++;
1953 /* discard the bad frame */
1954 return -EINVAL;
1956 dev->pkt_size[dev->cc] = payload;
1957 dev->jpg_size[dev->cc] = pdword[4];
1958 break;
1959 case S2255_MARKER_RESPONSE:
1960 pdword = (s32 *)pdata;
1961 pdata += DEF_USB_BLOCK;
1962 jj += DEF_USB_BLOCK;
1963 if (pdword[1] >= MAX_CHANNELS)
1964 break;
1965 cc = G_chnmap[pdword[1]];
1966 if (!(cc >= 0 && cc < MAX_CHANNELS))
1967 break;
1968 switch (pdword[2]) {
1969 case S2255_RESPONSE_SETMODE:
1970 /* check if channel valid */
1971 /* set mode ready */
1972 dev->setmode_ready[cc] = 1;
1973 wake_up(&dev->wait_setmode[cc]);
1974 dprintk(5, "setmode ready %d\n", cc);
1975 break;
1976 case S2255_RESPONSE_FW:
1978 dev->chn_ready |= (1 << cc);
1979 if ((dev->chn_ready & 0x0f) != 0x0f)
1980 break;
1981 /* all channels ready */
1982 printk(KERN_INFO "s2255: fw loaded\n");
1983 atomic_set(&dev->fw_data->fw_state,
1984 S2255_FW_SUCCESS);
1985 wake_up(&dev->fw_data->wait_fw);
1986 break;
1987 default:
1988 printk(KERN_INFO "s2255 unknwn resp\n");
1990 default:
1991 pdata++;
1992 break;
1994 if (bframe)
1995 break;
1996 } /* for */
1997 if (!bframe)
1998 return -EINVAL;
2002 idx = dev->cur_frame[dev->cc];
2003 frm = &dev->buffer[dev->cc].frame[idx];
2005 /* search done. now find out if should be acquiring on this channel */
2006 if (!dev->b_acquire[dev->cc]) {
2007 /* we found a frame, but this channel is turned off */
2008 frm->ulState = S2255_READ_IDLE;
2009 return -EINVAL;
2012 if (frm->ulState == S2255_READ_IDLE) {
2013 frm->ulState = S2255_READ_FRAME;
2014 frm->cur_size = 0;
2017 /* skip the marker 512 bytes (and offset if out of sync) */
2018 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2021 if (frm->lpvbits == NULL) {
2022 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2023 frm, dev, dev->cc, idx);
2024 return -ENOMEM;
2027 pdest = frm->lpvbits + frm->cur_size;
2029 copy_size = (pipe_info->cur_transfer_size - offset);
2031 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
2033 /* sanity check on pdest */
2034 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2035 memcpy(pdest, psrc, copy_size);
2037 frm->cur_size += copy_size;
2038 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2040 if (frm->cur_size >= size) {
2042 u32 cc = dev->cc;
2043 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2044 cc, idx);
2045 dev->last_frame[cc] = dev->cur_frame[cc];
2046 dev->cur_frame[cc]++;
2047 /* end of system frame ring buffer, start at zero */
2048 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2049 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2050 dev->cur_frame[cc] = 0;
2051 /* frame ready */
2052 if (dev->b_acquire[cc])
2053 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
2054 dev->frame_count[cc]++;
2055 frm->ulState = S2255_READ_IDLE;
2056 frm->cur_size = 0;
2059 /* done successfully */
2060 return 0;
2063 static void s2255_read_video_callback(struct s2255_dev *dev,
2064 struct s2255_pipeinfo *pipe_info)
2066 int res;
2067 dprintk(50, "callback read video \n");
2069 if (dev->cc >= MAX_CHANNELS) {
2070 dev->cc = 0;
2071 dev_err(&dev->udev->dev, "invalid channel\n");
2072 return;
2074 /* otherwise copy to the system buffers */
2075 res = save_frame(dev, pipe_info);
2076 if (res != 0)
2077 dprintk(4, "s2255: read callback failed\n");
2079 dprintk(50, "callback read video done\n");
2080 return;
2083 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2084 u16 Index, u16 Value, void *TransferBuffer,
2085 s32 TransferBufferLength, int bOut)
2087 int r;
2088 if (!bOut) {
2089 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2090 Request,
2091 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2092 USB_DIR_IN,
2093 Value, Index, TransferBuffer,
2094 TransferBufferLength, HZ * 5);
2095 } else {
2096 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2097 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2098 Value, Index, TransferBuffer,
2099 TransferBufferLength, HZ * 5);
2101 return r;
2105 * retrieve FX2 firmware version. future use.
2106 * @param dev pointer to device extension
2107 * @return -1 for fail, else returns firmware version as an int(16 bits)
2109 static int s2255_get_fx2fw(struct s2255_dev *dev)
2111 int fw;
2112 int ret;
2113 unsigned char transBuffer[64];
2114 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2115 S2255_VR_IN);
2116 if (ret < 0)
2117 dprintk(2, "get fw error: %x\n", ret);
2118 fw = transBuffer[0] + (transBuffer[1] << 8);
2119 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2120 return fw;
2124 * Create the system ring buffer to copy frames into from the
2125 * usb read pipe.
2127 static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2129 unsigned long i;
2130 unsigned long reqsize;
2131 dprintk(1, "create sys buffers\n");
2132 if (chn >= MAX_CHANNELS)
2133 return -1;
2135 dev->buffer[chn].dwFrames = SYS_FRAMES;
2137 /* always allocate maximum size(PAL) for system buffers */
2138 reqsize = SYS_FRAMES_MAXSIZE;
2140 if (reqsize > SYS_FRAMES_MAXSIZE)
2141 reqsize = SYS_FRAMES_MAXSIZE;
2143 for (i = 0; i < SYS_FRAMES; i++) {
2144 /* allocate the frames */
2145 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2147 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2148 &dev->buffer[chn].frame[i], chn, i,
2149 dev->buffer[chn].frame[i].lpvbits);
2150 dev->buffer[chn].frame[i].size = reqsize;
2151 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2152 printk(KERN_INFO "out of memory. using less frames\n");
2153 dev->buffer[chn].dwFrames = i;
2154 break;
2158 /* make sure internal states are set */
2159 for (i = 0; i < SYS_FRAMES; i++) {
2160 dev->buffer[chn].frame[i].ulState = 0;
2161 dev->buffer[chn].frame[i].cur_size = 0;
2164 dev->cur_frame[chn] = 0;
2165 dev->last_frame[chn] = -1;
2166 return 0;
2169 static int s2255_release_sys_buffers(struct s2255_dev *dev,
2170 unsigned long channel)
2172 unsigned long i;
2173 dprintk(1, "release sys buffers\n");
2174 for (i = 0; i < SYS_FRAMES; i++) {
2175 if (dev->buffer[channel].frame[i].lpvbits) {
2176 dprintk(1, "vfree %p\n",
2177 dev->buffer[channel].frame[i].lpvbits);
2178 vfree(dev->buffer[channel].frame[i].lpvbits);
2180 dev->buffer[channel].frame[i].lpvbits = NULL;
2182 return 0;
2185 static int s2255_board_init(struct s2255_dev *dev)
2187 int j;
2188 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2189 int fw_ver;
2190 dprintk(4, "board init: %p", dev);
2192 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2193 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2195 memset(pipe, 0, sizeof(*pipe));
2196 pipe->dev = dev;
2197 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2198 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2200 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2201 GFP_KERNEL);
2202 if (pipe->transfer_buffer == NULL) {
2203 dprintk(1, "out of memory!\n");
2204 return -ENOMEM;
2209 /* query the firmware */
2210 fw_ver = s2255_get_fx2fw(dev);
2212 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2213 (fw_ver >> 8) & 0xff,
2214 fw_ver & 0xff);
2216 if (fw_ver < S2255_CUR_USB_FWVER)
2217 dev_err(&dev->udev->dev,
2218 "usb firmware not up to date %d.%d\n",
2219 (fw_ver >> 8) & 0xff,
2220 fw_ver & 0xff);
2222 for (j = 0; j < MAX_CHANNELS; j++) {
2223 dev->b_acquire[j] = 0;
2224 dev->mode[j] = mode_def;
2225 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
2226 dev->cur_fmt[j] = &formats[0];
2227 dev->mode[j].restart = 1;
2228 dev->req_image_size[j] = get_transfer_size(&mode_def);
2229 dev->frame_count[j] = 0;
2230 /* create the system buffers */
2231 s2255_create_sys_buffers(dev, j);
2233 /* start read pipe */
2234 s2255_start_readpipe(dev);
2236 dprintk(1, "S2255: board initialized\n");
2237 return 0;
2240 static int s2255_board_shutdown(struct s2255_dev *dev)
2242 u32 i;
2244 dprintk(1, "S2255: board shutdown: %p", dev);
2246 for (i = 0; i < MAX_CHANNELS; i++) {
2247 if (dev->b_acquire[i])
2248 s2255_stop_acquire(dev, i);
2251 s2255_stop_readpipe(dev);
2253 for (i = 0; i < MAX_CHANNELS; i++)
2254 s2255_release_sys_buffers(dev, i);
2256 /* release transfer buffers */
2257 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2258 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2259 kfree(pipe->transfer_buffer);
2261 return 0;
2264 static void read_pipe_completion(struct urb *purb)
2266 struct s2255_pipeinfo *pipe_info;
2267 struct s2255_dev *dev;
2268 int status;
2269 int pipe;
2271 pipe_info = purb->context;
2272 dprintk(100, "read pipe completion %p, status %d\n", purb,
2273 purb->status);
2274 if (pipe_info == NULL) {
2275 dev_err(&purb->dev->dev, "no context!\n");
2276 return;
2279 dev = pipe_info->dev;
2280 if (dev == NULL) {
2281 dev_err(&purb->dev->dev, "no context!\n");
2282 return;
2284 status = purb->status;
2285 /* if shutting down, do not resubmit, exit immediately */
2286 if (status == -ESHUTDOWN) {
2287 dprintk(2, "read_pipe_completion: err shutdown\n");
2288 pipe_info->err_count++;
2289 return;
2292 if (pipe_info->state == 0) {
2293 dprintk(2, "exiting USB pipe");
2294 return;
2297 if (status == 0)
2298 s2255_read_video_callback(dev, pipe_info);
2299 else {
2300 pipe_info->err_count++;
2301 dprintk(1, "s2255drv: failed URB %d\n", status);
2304 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2305 /* reuse urb */
2306 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2307 pipe,
2308 pipe_info->transfer_buffer,
2309 pipe_info->cur_transfer_size,
2310 read_pipe_completion, pipe_info);
2312 if (pipe_info->state != 0) {
2313 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2314 dev_err(&dev->udev->dev, "error submitting urb\n");
2316 } else {
2317 dprintk(2, "read pipe complete state 0\n");
2319 return;
2322 static int s2255_start_readpipe(struct s2255_dev *dev)
2324 int pipe;
2325 int retval;
2326 int i;
2327 struct s2255_pipeinfo *pipe_info = dev->pipes;
2328 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2329 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2331 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2332 pipe_info->state = 1;
2333 pipe_info->err_count = 0;
2334 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2335 if (!pipe_info->stream_urb) {
2336 dev_err(&dev->udev->dev,
2337 "ReadStream: Unable to alloc URB\n");
2338 return -ENOMEM;
2340 /* transfer buffer allocated in board_init */
2341 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2342 pipe,
2343 pipe_info->transfer_buffer,
2344 pipe_info->cur_transfer_size,
2345 read_pipe_completion, pipe_info);
2347 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2348 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2349 if (retval) {
2350 printk(KERN_ERR "s2255: start read pipe failed\n");
2351 return retval;
2355 return 0;
2358 /* starts acquisition process */
2359 static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2361 unsigned char *buffer;
2362 int res;
2363 unsigned long chn_rev;
2364 int j;
2365 if (chn >= MAX_CHANNELS) {
2366 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2367 return -1;
2370 chn_rev = G_chnmap[chn];
2371 dprintk(1, "S2255: start acquire %lu \n", chn);
2373 buffer = kzalloc(512, GFP_KERNEL);
2374 if (buffer == NULL) {
2375 dev_err(&dev->udev->dev, "out of mem\n");
2376 return -ENOMEM;
2379 dev->last_frame[chn] = -1;
2380 dev->bad_payload[chn] = 0;
2381 dev->cur_frame[chn] = 0;
2382 for (j = 0; j < SYS_FRAMES; j++) {
2383 dev->buffer[chn].frame[j].ulState = 0;
2384 dev->buffer[chn].frame[j].cur_size = 0;
2387 /* send the start command */
2388 *(u32 *) buffer = IN_DATA_TOKEN;
2389 *((u32 *) buffer + 1) = (u32) chn_rev;
2390 *((u32 *) buffer + 2) = (u32) CMD_START;
2391 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2392 if (res != 0)
2393 dev_err(&dev->udev->dev, "CMD_START error\n");
2395 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2396 kfree(buffer);
2397 return 0;
2400 static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2402 unsigned char *buffer;
2403 int res;
2404 unsigned long chn_rev;
2406 if (chn >= MAX_CHANNELS) {
2407 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2408 return -1;
2410 chn_rev = G_chnmap[chn];
2412 buffer = kzalloc(512, GFP_KERNEL);
2413 if (buffer == NULL) {
2414 dev_err(&dev->udev->dev, "out of mem\n");
2415 return -ENOMEM;
2418 /* send the stop command */
2419 dprintk(4, "stop acquire %lu\n", chn);
2420 *(u32 *) buffer = IN_DATA_TOKEN;
2421 *((u32 *) buffer + 1) = (u32) chn_rev;
2422 *((u32 *) buffer + 2) = CMD_STOP;
2423 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2425 if (res != 0)
2426 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2428 dprintk(4, "stop acquire: releasing states \n");
2430 kfree(buffer);
2431 dev->b_acquire[chn] = 0;
2433 return res;
2436 static void s2255_stop_readpipe(struct s2255_dev *dev)
2438 int j;
2440 if (dev == NULL) {
2441 s2255_dev_err(&dev->udev->dev, "invalid device\n");
2442 return;
2444 dprintk(4, "stop read pipe\n");
2445 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2446 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2447 if (pipe_info) {
2448 if (pipe_info->state == 0)
2449 continue;
2450 pipe_info->state = 0;
2454 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2455 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2456 if (pipe_info->stream_urb) {
2457 /* cancel urb */
2458 usb_kill_urb(pipe_info->stream_urb);
2459 usb_free_urb(pipe_info->stream_urb);
2460 pipe_info->stream_urb = NULL;
2463 dprintk(2, "s2255 stop read pipe: %d\n", j);
2464 return;
2467 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2469 if (reset)
2470 s2255_reset_dsppower(dev);
2471 dev->fw_data->fw_size = dev->fw_data->fw->size;
2472 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2473 memcpy(dev->fw_data->pfw_data,
2474 dev->fw_data->fw->data, CHUNK_SIZE);
2475 dev->fw_data->fw_loaded = CHUNK_SIZE;
2476 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2477 usb_sndbulkpipe(dev->udev, 2),
2478 dev->fw_data->pfw_data,
2479 CHUNK_SIZE, s2255_fwchunk_complete,
2480 dev->fw_data);
2481 mod_timer(&dev->timer, jiffies + HZ);
2484 /* standard usb probe function */
2485 static int s2255_probe(struct usb_interface *interface,
2486 const struct usb_device_id *id)
2488 struct s2255_dev *dev = NULL;
2489 struct usb_host_interface *iface_desc;
2490 struct usb_endpoint_descriptor *endpoint;
2491 int i;
2492 int retval = -ENOMEM;
2493 __le32 *pdata;
2494 int fw_size;
2496 dprintk(2, "s2255: probe\n");
2498 /* allocate memory for our device state and initialize it to zero */
2499 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2500 if (dev == NULL) {
2501 s2255_dev_err(&interface->dev, "out of memory\n");
2502 goto error;
2505 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2506 if (!dev->fw_data)
2507 goto error;
2509 mutex_init(&dev->lock);
2510 mutex_init(&dev->open_lock);
2512 /* grab usb_device and save it */
2513 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2514 if (dev->udev == NULL) {
2515 dev_err(&interface->dev, "null usb device\n");
2516 retval = -ENODEV;
2517 goto error;
2519 kref_init(&dev->kref);
2520 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2521 dev->udev, interface);
2522 dev->interface = interface;
2523 /* set up the endpoint information */
2524 iface_desc = interface->cur_altsetting;
2525 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2526 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2527 endpoint = &iface_desc->endpoint[i].desc;
2528 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2529 /* we found the bulk in endpoint */
2530 dev->read_endpoint = endpoint->bEndpointAddress;
2534 if (!dev->read_endpoint) {
2535 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2536 goto error;
2539 /* set intfdata */
2540 usb_set_intfdata(interface, dev);
2542 dprintk(100, "after intfdata %p\n", dev);
2544 init_timer(&dev->timer);
2545 dev->timer.function = s2255_timer;
2546 dev->timer.data = (unsigned long)dev->fw_data;
2548 init_waitqueue_head(&dev->fw_data->wait_fw);
2549 for (i = 0; i < MAX_CHANNELS; i++)
2550 init_waitqueue_head(&dev->wait_setmode[i]);
2553 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2555 if (!dev->fw_data->fw_urb) {
2556 dev_err(&interface->dev, "out of memory!\n");
2557 goto error;
2559 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2560 if (!dev->fw_data->pfw_data) {
2561 dev_err(&interface->dev, "out of memory!\n");
2562 goto error;
2564 /* load the first chunk */
2565 if (request_firmware(&dev->fw_data->fw,
2566 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2567 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2568 goto error;
2570 /* check the firmware is valid */
2571 fw_size = dev->fw_data->fw->size;
2572 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2574 if (*pdata != S2255_FW_MARKER) {
2575 printk(KERN_INFO "Firmware invalid.\n");
2576 retval = -ENODEV;
2577 goto error;
2578 } else {
2579 /* make sure firmware is the latest */
2580 __le32 *pRel;
2581 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2582 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2584 /* loads v4l specific */
2585 s2255_probe_v4l(dev);
2586 usb_reset_device(dev->udev);
2587 /* load 2255 board specific */
2588 retval = s2255_board_init(dev);
2589 if (retval)
2590 goto error;
2592 dprintk(4, "before probe done %p\n", dev);
2593 spin_lock_init(&dev->slock);
2595 s2255_fwload_start(dev, 0);
2596 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2597 return 0;
2598 error:
2599 return retval;
2602 /* disconnect routine. when board is removed physically or with rmmod */
2603 static void s2255_disconnect(struct usb_interface *interface)
2605 struct s2255_dev *dev = NULL;
2606 int i;
2607 dprintk(1, "s2255: disconnect interface %p\n", interface);
2608 dev = usb_get_intfdata(interface);
2611 * wake up any of the timers to allow open_lock to be
2612 * acquired sooner
2614 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2615 wake_up(&dev->fw_data->wait_fw);
2616 for (i = 0; i < MAX_CHANNELS; i++) {
2617 dev->setmode_ready[i] = 1;
2618 wake_up(&dev->wait_setmode[i]);
2621 mutex_lock(&dev->open_lock);
2622 usb_set_intfdata(interface, NULL);
2623 mutex_unlock(&dev->open_lock);
2625 if (dev) {
2626 kref_put(&dev->kref, s2255_destroy);
2627 dprintk(1, "s2255drv: disconnect\n");
2628 dev_info(&interface->dev, "s2255usb now disconnected\n");
2632 static struct usb_driver s2255_driver = {
2633 .name = S2255_DRIVER_NAME,
2634 .probe = s2255_probe,
2635 .disconnect = s2255_disconnect,
2636 .id_table = s2255_table,
2639 static int __init usb_s2255_init(void)
2641 int result;
2643 /* register this driver with the USB subsystem */
2644 result = usb_register(&s2255_driver);
2646 if (result)
2647 pr_err(KBUILD_MODNAME
2648 ": usb_register failed. Error number %d\n", result);
2650 dprintk(2, "s2255_init: done\n");
2651 return result;
2654 static void __exit usb_s2255_exit(void)
2656 usb_deregister(&s2255_driver);
2659 module_init(usb_s2255_init);
2660 module_exit(usb_s2255_exit);
2662 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2663 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2664 MODULE_LICENSE("GPL");