[media] s2255drv: adding MJPEG format
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / s2255drv.c
blob46506d8381a2562c4593f524cef204b325aed2b7
1 /*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2010 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/slab.h>
49 #include <linux/videodev2.h>
50 #include <linux/version.h>
51 #include <linux/mm.h>
52 #include <media/videobuf-vmalloc.h>
53 #include <media/v4l2-common.h>
54 #include <media/v4l2-device.h>
55 #include <media/v4l2-ioctl.h>
56 #include <linux/vmalloc.h>
57 #include <linux/usb.h>
59 #define S2255_MAJOR_VERSION 1
60 #define S2255_MINOR_VERSION 21
61 #define S2255_RELEASE 0
62 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
63 S2255_MINOR_VERSION, \
64 S2255_RELEASE)
65 #define FIRMWARE_FILE_NAME "f2255usb.bin"
67 /* default JPEG quality */
68 #define S2255_DEF_JPEG_QUAL 50
69 /* vendor request in */
70 #define S2255_VR_IN 0
71 /* vendor request out */
72 #define S2255_VR_OUT 1
73 /* firmware query */
74 #define S2255_VR_FW 0x30
75 /* USB endpoint number for configuring the device */
76 #define S2255_CONFIG_EP 2
77 /* maximum time for DSP to start responding after last FW word loaded(ms) */
78 #define S2255_DSP_BOOTTIME 800
79 /* maximum time to wait for firmware to load (ms) */
80 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
81 #define S2255_DEF_BUFS 16
82 #define S2255_SETMODE_TIMEOUT 500
83 #define S2255_VIDSTATUS_TIMEOUT 350
84 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
85 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
86 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
87 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
88 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
89 #define S2255_USB_XFER_SIZE (16 * 1024)
90 #define MAX_CHANNELS 4
91 #define SYS_FRAMES 4
92 /* maximum size is PAL full size plus room for the marker header(s) */
93 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
94 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
95 #define LINE_SZ_4CIFS_NTSC 640
96 #define LINE_SZ_2CIFS_NTSC 640
97 #define LINE_SZ_1CIFS_NTSC 320
98 #define LINE_SZ_4CIFS_PAL 704
99 #define LINE_SZ_2CIFS_PAL 704
100 #define LINE_SZ_1CIFS_PAL 352
101 #define NUM_LINES_4CIFS_NTSC 240
102 #define NUM_LINES_2CIFS_NTSC 240
103 #define NUM_LINES_1CIFS_NTSC 240
104 #define NUM_LINES_4CIFS_PAL 288
105 #define NUM_LINES_2CIFS_PAL 288
106 #define NUM_LINES_1CIFS_PAL 288
107 #define LINE_SZ_DEF 640
108 #define NUM_LINES_DEF 240
111 /* predefined settings */
112 #define FORMAT_NTSC 1
113 #define FORMAT_PAL 2
115 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
116 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
117 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
118 /* SCALE_4CIFSI is the 2 fields interpolated into one */
119 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
121 #define COLOR_YUVPL 1 /* YUV planar */
122 #define COLOR_YUVPK 2 /* YUV packed */
123 #define COLOR_Y8 4 /* monochrome */
124 #define COLOR_JPG 5 /* JPEG */
126 #define MASK_COLOR 0x000000ff
127 #define MASK_JPG_QUALITY 0x0000ff00
128 #define MASK_INPUT_TYPE 0x000f0000
129 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
130 #define FDEC_1 1 /* capture every frame. default */
131 #define FDEC_2 2 /* capture every 2nd frame */
132 #define FDEC_3 3 /* capture every 3rd frame */
133 #define FDEC_5 5 /* capture every 5th frame */
135 /*-------------------------------------------------------
136 * Default mode parameters.
137 *-------------------------------------------------------*/
138 #define DEF_SCALE SCALE_4CIFS
139 #define DEF_COLOR COLOR_YUVPL
140 #define DEF_FDEC FDEC_1
141 #define DEF_BRIGHT 0
142 #define DEF_CONTRAST 0x5c
143 #define DEF_SATURATION 0x80
144 #define DEF_HUE 0
146 /* usb config commands */
147 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
148 #define CMD_2255 cpu_to_le32(0xc2255000)
149 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
150 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
151 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
152 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
154 struct s2255_mode {
155 u32 format; /* input video format (NTSC, PAL) */
156 u32 scale; /* output video scale */
157 u32 color; /* output video color format */
158 u32 fdec; /* frame decimation */
159 u32 bright; /* brightness */
160 u32 contrast; /* contrast */
161 u32 saturation; /* saturation */
162 u32 hue; /* hue (NTSC only)*/
163 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
164 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
165 u32 restart; /* if DSP requires restart */
169 #define S2255_READ_IDLE 0
170 #define S2255_READ_FRAME 1
172 /* frame structure */
173 struct s2255_framei {
174 unsigned long size;
175 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
176 void *lpvbits; /* image data */
177 unsigned long cur_size; /* current data copied to it */
180 /* image buffer structure */
181 struct s2255_bufferi {
182 unsigned long dwFrames; /* number of frames in buffer */
183 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
186 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
187 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
188 DEF_HUE, 0, DEF_USB_BLOCK, 0}
190 struct s2255_dmaqueue {
191 struct list_head active;
192 struct s2255_dev *dev;
195 /* for firmware loading, fw_state */
196 #define S2255_FW_NOTLOADED 0
197 #define S2255_FW_LOADED_DSPWAIT 1
198 #define S2255_FW_SUCCESS 2
199 #define S2255_FW_FAILED 3
200 #define S2255_FW_DISCONNECTING 4
201 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
202 /* 2255 read states */
203 #define S2255_READ_IDLE 0
204 #define S2255_READ_FRAME 1
205 struct s2255_fw {
206 int fw_loaded;
207 int fw_size;
208 struct urb *fw_urb;
209 atomic_t fw_state;
210 void *pfw_data;
211 wait_queue_head_t wait_fw;
212 const struct firmware *fw;
215 struct s2255_pipeinfo {
216 u32 max_transfer_size;
217 u32 cur_transfer_size;
218 u8 *transfer_buffer;
219 u32 state;
220 void *stream_urb;
221 void *dev; /* back pointer to s2255_dev struct*/
222 u32 err_count;
223 u32 idx;
226 struct s2255_fmt; /*forward declaration */
227 struct s2255_dev;
229 struct s2255_channel {
230 struct video_device vdev;
231 int resources;
232 struct s2255_dmaqueue vidq;
233 struct s2255_bufferi buffer;
234 struct s2255_mode mode;
235 /* jpeg compression */
236 struct v4l2_jpegcompression jc;
237 /* capture parameters (for high quality mode full size) */
238 struct v4l2_captureparm cap_parm;
239 int cur_frame;
240 int last_frame;
242 int b_acquire;
243 /* allocated image size */
244 unsigned long req_image_size;
245 /* received packet size */
246 unsigned long pkt_size;
247 int bad_payload;
248 unsigned long frame_count;
249 /* if JPEG image */
250 int jpg_size;
251 /* if channel configured to default state */
252 int configured;
253 wait_queue_head_t wait_setmode;
254 int setmode_ready;
255 /* video status items */
256 int vidstatus;
257 wait_queue_head_t wait_vidstatus;
258 int vidstatus_ready;
259 unsigned int width;
260 unsigned int height;
261 const struct s2255_fmt *fmt;
262 int idx; /* channel number on device, 0-3 */
266 struct s2255_dev {
267 struct s2255_channel channel[MAX_CHANNELS];
268 struct v4l2_device v4l2_dev;
269 atomic_t num_channels;
270 int frames;
271 struct mutex lock; /* channels[].vdev.lock */
272 struct mutex open_lock;
273 struct usb_device *udev;
274 struct usb_interface *interface;
275 u8 read_endpoint;
276 struct timer_list timer;
277 struct s2255_fw *fw_data;
278 struct s2255_pipeinfo pipe;
279 u32 cc; /* current channel */
280 int frame_ready;
281 int chn_ready;
282 spinlock_t slock;
283 /* dsp firmware version (f2255usb.bin) */
284 int dsp_fw_ver;
285 u16 pid; /* product id */
288 static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
290 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
293 struct s2255_fmt {
294 char *name;
295 u32 fourcc;
296 int depth;
299 /* buffer for one video frame */
300 struct s2255_buffer {
301 /* common v4l buffer stuff -- must be first */
302 struct videobuf_buffer vb;
303 const struct s2255_fmt *fmt;
306 struct s2255_fh {
307 struct s2255_dev *dev;
308 struct videobuf_queue vb_vidq;
309 enum v4l2_buf_type type;
310 struct s2255_channel *channel;
311 int resources;
314 /* current cypress EEPROM firmware version */
315 #define S2255_CUR_USB_FWVER ((3 << 8) | 11)
316 /* current DSP FW version */
317 #define S2255_CUR_DSP_FWVER 10102
318 /* Need DSP version 5+ for video status feature */
319 #define S2255_MIN_DSP_STATUS 5
320 #define S2255_MIN_DSP_COLORFILTER 8
321 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
323 /* private V4L2 controls */
326 * The following chart displays how COLORFILTER should be set
327 * =========================================================
328 * = fourcc = COLORFILTER =
329 * = ===============================
330 * = = 0 = 1 =
331 * =========================================================
332 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
333 * = = s-video or = composite =
334 * = = B/W camera = input =
335 * =========================================================
336 * = other = color, svideo = color, =
337 * = = = composite =
338 * =========================================================
340 * Notes:
341 * channels 0-3 on 2255 are composite
342 * channels 0-1 on 2257 are composite, 2-3 are s-video
343 * If COLORFILTER is 0 with a composite color camera connected,
344 * the output will appear monochrome but hatching
345 * will occur.
346 * COLORFILTER is different from "color killer" and "color effects"
347 * for reasons above.
349 #define S2255_V4L2_YC_ON 1
350 #define S2255_V4L2_YC_OFF 0
351 #define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
353 /* frame prefix size (sent once every frame) */
354 #define PREFIX_SIZE 512
356 /* Channels on box are in reverse order */
357 static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
359 static int debug;
360 static int *s2255_debug = &debug;
362 static int s2255_start_readpipe(struct s2255_dev *dev);
363 static void s2255_stop_readpipe(struct s2255_dev *dev);
364 static int s2255_start_acquire(struct s2255_channel *channel);
365 static int s2255_stop_acquire(struct s2255_channel *channel);
366 static void s2255_fillbuff(struct s2255_channel *chn, struct s2255_buffer *buf,
367 int jpgsize);
368 static int s2255_set_mode(struct s2255_channel *chan, struct s2255_mode *mode);
369 static int s2255_board_shutdown(struct s2255_dev *dev);
370 static void s2255_fwload_start(struct s2255_dev *dev, int reset);
371 static void s2255_destroy(struct s2255_dev *dev);
372 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
373 u16 index, u16 value, void *buf,
374 s32 buf_len, int bOut);
376 /* dev_err macro with driver name */
377 #define S2255_DRIVER_NAME "s2255"
378 #define s2255_dev_err(dev, fmt, arg...) \
379 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
381 #define dprintk(level, fmt, arg...) \
382 do { \
383 if (*s2255_debug >= (level)) { \
384 printk(KERN_DEBUG S2255_DRIVER_NAME \
385 ": " fmt, ##arg); \
387 } while (0)
389 static struct usb_driver s2255_driver;
391 /* Declare static vars that will be used as parameters */
392 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
394 /* start video number */
395 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
397 module_param(debug, int, 0644);
398 MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
399 module_param(vid_limit, int, 0644);
400 MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
401 module_param(video_nr, int, 0644);
402 MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
404 /* USB device table */
405 #define USB_SENSORAY_VID 0x1943
406 static struct usb_device_id s2255_table[] = {
407 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
408 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
409 { } /* Terminating entry */
411 MODULE_DEVICE_TABLE(usb, s2255_table);
413 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
415 /* image formats. */
416 static const struct s2255_fmt formats[] = {
418 .name = "4:2:2, planar, YUV422P",
419 .fourcc = V4L2_PIX_FMT_YUV422P,
420 .depth = 16
422 }, {
423 .name = "4:2:2, packed, YUYV",
424 .fourcc = V4L2_PIX_FMT_YUYV,
425 .depth = 16
427 }, {
428 .name = "4:2:2, packed, UYVY",
429 .fourcc = V4L2_PIX_FMT_UYVY,
430 .depth = 16
431 }, {
432 .name = "JPG",
433 .fourcc = V4L2_PIX_FMT_JPEG,
434 .depth = 24
435 }, {
436 .name = "MJPG",
437 .fourcc = V4L2_PIX_FMT_MJPEG,
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, 0x0b01, NULL, 0, 1);
500 msleep(10);
501 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
502 msleep(600);
503 s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1);
504 return;
507 /* kickstarts the firmware loading. from probe
509 static void s2255_timer(unsigned long user_data)
511 struct s2255_fw *data = (struct s2255_fw *)user_data;
512 dprintk(100, "%s\n", __func__);
513 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
514 printk(KERN_ERR "s2255: can't submit urb\n");
515 atomic_set(&data->fw_state, S2255_FW_FAILED);
516 /* wake up anything waiting for the firmware */
517 wake_up(&data->wait_fw);
518 return;
523 /* this loads the firmware asynchronously.
524 Originally this was done synchroously in probe.
525 But it is better to load it asynchronously here than block
526 inside the probe function. Blocking inside probe affects boot time.
527 FW loading is triggered by the timer in the probe function
529 static void s2255_fwchunk_complete(struct urb *urb)
531 struct s2255_fw *data = urb->context;
532 struct usb_device *udev = urb->dev;
533 int len;
534 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
535 if (urb->status) {
536 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
537 atomic_set(&data->fw_state, S2255_FW_FAILED);
538 /* wake up anything waiting for the firmware */
539 wake_up(&data->wait_fw);
540 return;
542 if (data->fw_urb == NULL) {
543 s2255_dev_err(&udev->dev, "disconnected\n");
544 atomic_set(&data->fw_state, S2255_FW_FAILED);
545 /* wake up anything waiting for the firmware */
546 wake_up(&data->wait_fw);
547 return;
549 #define CHUNK_SIZE 512
550 /* all USB transfers must be done with continuous kernel memory.
551 can't allocate more than 128k in current linux kernel, so
552 upload the firmware in chunks
554 if (data->fw_loaded < data->fw_size) {
555 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
556 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
558 if (len < CHUNK_SIZE)
559 memset(data->pfw_data, 0, CHUNK_SIZE);
561 dprintk(100, "completed len %d, loaded %d \n", len,
562 data->fw_loaded);
564 memcpy(data->pfw_data,
565 (char *) data->fw->data + data->fw_loaded, len);
567 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
568 data->pfw_data, CHUNK_SIZE,
569 s2255_fwchunk_complete, data);
570 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
571 dev_err(&udev->dev, "failed submit URB\n");
572 atomic_set(&data->fw_state, S2255_FW_FAILED);
573 /* wake up anything waiting for the firmware */
574 wake_up(&data->wait_fw);
575 return;
577 data->fw_loaded += len;
578 } else {
579 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
580 dprintk(100, "%s: firmware upload complete\n", __func__);
582 return;
586 static int s2255_got_frame(struct s2255_channel *channel, int jpgsize)
588 struct s2255_dmaqueue *dma_q = &channel->vidq;
589 struct s2255_buffer *buf;
590 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
591 unsigned long flags = 0;
592 int rc = 0;
593 spin_lock_irqsave(&dev->slock, flags);
594 if (list_empty(&dma_q->active)) {
595 dprintk(1, "No active queue to serve\n");
596 rc = -1;
597 goto unlock;
599 buf = list_entry(dma_q->active.next,
600 struct s2255_buffer, vb.queue);
601 list_del(&buf->vb.queue);
602 do_gettimeofday(&buf->vb.ts);
603 s2255_fillbuff(channel, buf, jpgsize);
604 wake_up(&buf->vb.done);
605 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
606 unlock:
607 spin_unlock_irqrestore(&dev->slock, flags);
608 return rc;
611 static const struct s2255_fmt *format_by_fourcc(int fourcc)
613 unsigned int i;
614 for (i = 0; i < ARRAY_SIZE(formats); i++) {
615 if (-1 == formats[i].fourcc)
616 continue;
617 if (formats[i].fourcc == fourcc)
618 return formats + i;
620 return NULL;
623 /* video buffer vmalloc implementation based partly on VIVI driver which is
624 * Copyright (c) 2006 by
625 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
626 * Ted Walther <ted--a.t--enumera.com>
627 * John Sokol <sokol--a.t--videotechnology.com>
628 * http://v4l.videotechnology.com/
631 static void s2255_fillbuff(struct s2255_channel *channel,
632 struct s2255_buffer *buf, int jpgsize)
634 int pos = 0;
635 struct timeval ts;
636 const char *tmpbuf;
637 char *vbuf = videobuf_to_vmalloc(&buf->vb);
638 unsigned long last_frame;
639 struct s2255_framei *frm;
641 if (!vbuf)
642 return;
643 last_frame = channel->last_frame;
644 if (last_frame != -1) {
645 frm = &channel->buffer.frame[last_frame];
646 tmpbuf =
647 (const char *)channel->buffer.frame[last_frame].lpvbits;
648 switch (buf->fmt->fourcc) {
649 case V4L2_PIX_FMT_YUYV:
650 case V4L2_PIX_FMT_UYVY:
651 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
652 vbuf, buf->vb.width,
653 buf->vb.height,
654 buf->fmt->fourcc);
655 break;
656 case V4L2_PIX_FMT_GREY:
657 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
658 break;
659 case V4L2_PIX_FMT_JPEG:
660 case V4L2_PIX_FMT_MJPEG:
661 buf->vb.size = jpgsize;
662 memcpy(vbuf, tmpbuf, buf->vb.size);
663 break;
664 case V4L2_PIX_FMT_YUV422P:
665 memcpy(vbuf, tmpbuf,
666 buf->vb.width * buf->vb.height * 2);
667 break;
668 default:
669 printk(KERN_DEBUG "s2255: unknown format?\n");
671 channel->last_frame = -1;
672 } else {
673 printk(KERN_ERR "s2255: =======no frame\n");
674 return;
677 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
678 (unsigned long)vbuf, pos);
679 /* tell v4l buffer was filled */
681 buf->vb.field_count = channel->frame_count * 2;
682 do_gettimeofday(&ts);
683 buf->vb.ts = ts;
684 buf->vb.state = VIDEOBUF_DONE;
688 /* ------------------------------------------------------------------
689 Videobuf operations
690 ------------------------------------------------------------------*/
692 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
693 unsigned int *size)
695 struct s2255_fh *fh = vq->priv_data;
696 struct s2255_channel *channel = fh->channel;
697 *size = channel->width * channel->height * (channel->fmt->depth >> 3);
699 if (0 == *count)
700 *count = S2255_DEF_BUFS;
702 if (*size * *count > vid_limit * 1024 * 1024)
703 *count = (vid_limit * 1024 * 1024) / *size;
705 return 0;
708 static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
710 dprintk(4, "%s\n", __func__);
712 videobuf_vmalloc_free(&buf->vb);
713 buf->vb.state = VIDEOBUF_NEEDS_INIT;
716 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
717 enum v4l2_field field)
719 struct s2255_fh *fh = vq->priv_data;
720 struct s2255_channel *channel = fh->channel;
721 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
722 int rc;
723 int w = channel->width;
724 int h = channel->height;
725 dprintk(4, "%s, field=%d\n", __func__, field);
726 if (channel->fmt == NULL)
727 return -EINVAL;
729 if ((w < norm_minw(&channel->vdev)) ||
730 (w > norm_maxw(&channel->vdev)) ||
731 (h < norm_minh(&channel->vdev)) ||
732 (h > norm_maxh(&channel->vdev))) {
733 dprintk(4, "invalid buffer prepare\n");
734 return -EINVAL;
736 buf->vb.size = w * h * (channel->fmt->depth >> 3);
737 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
738 dprintk(4, "invalid buffer prepare\n");
739 return -EINVAL;
742 buf->fmt = channel->fmt;
743 buf->vb.width = w;
744 buf->vb.height = h;
745 buf->vb.field = field;
747 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
748 rc = videobuf_iolock(vq, &buf->vb, NULL);
749 if (rc < 0)
750 goto fail;
753 buf->vb.state = VIDEOBUF_PREPARED;
754 return 0;
755 fail:
756 free_buffer(vq, buf);
757 return rc;
760 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
762 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
763 struct s2255_fh *fh = vq->priv_data;
764 struct s2255_channel *channel = fh->channel;
765 struct s2255_dmaqueue *vidq = &channel->vidq;
766 dprintk(1, "%s\n", __func__);
767 buf->vb.state = VIDEOBUF_QUEUED;
768 list_add_tail(&buf->vb.queue, &vidq->active);
771 static void buffer_release(struct videobuf_queue *vq,
772 struct videobuf_buffer *vb)
774 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
775 struct s2255_fh *fh = vq->priv_data;
776 dprintk(4, "%s %d\n", __func__, fh->channel->idx);
777 free_buffer(vq, buf);
780 static struct videobuf_queue_ops s2255_video_qops = {
781 .buf_setup = buffer_setup,
782 .buf_prepare = buffer_prepare,
783 .buf_queue = buffer_queue,
784 .buf_release = buffer_release,
788 static int res_get(struct s2255_fh *fh)
790 struct s2255_channel *channel = fh->channel;
791 /* is it free? */
792 if (channel->resources)
793 return 0; /* no, someone else uses it */
794 /* it's free, grab it */
795 channel->resources = 1;
796 fh->resources = 1;
797 dprintk(1, "s2255: res: get\n");
798 return 1;
801 static int res_locked(struct s2255_fh *fh)
803 return fh->channel->resources;
806 static int res_check(struct s2255_fh *fh)
808 return fh->resources;
812 static void res_free(struct s2255_fh *fh)
814 struct s2255_channel *channel = fh->channel;
815 channel->resources = 0;
816 fh->resources = 0;
817 dprintk(1, "res: put\n");
820 static int vidioc_querymenu(struct file *file, void *priv,
821 struct v4l2_querymenu *qmenu)
823 static const char *colorfilter[] = {
824 "Off",
825 "On",
826 NULL
828 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
829 int i;
830 const char **menu_items = colorfilter;
831 for (i = 0; i < qmenu->index && menu_items[i]; i++)
832 ; /* do nothing (from v4l2-common.c) */
833 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
834 return -EINVAL;
835 strlcpy(qmenu->name, menu_items[qmenu->index],
836 sizeof(qmenu->name));
837 return 0;
839 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
842 static int vidioc_querycap(struct file *file, void *priv,
843 struct v4l2_capability *cap)
845 struct s2255_fh *fh = file->private_data;
846 struct s2255_dev *dev = fh->dev;
847 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
848 strlcpy(cap->card, "s2255", sizeof(cap->card));
849 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
850 cap->version = S2255_VERSION;
851 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
852 return 0;
855 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
856 struct v4l2_fmtdesc *f)
858 int index = 0;
859 if (f)
860 index = f->index;
862 if (index >= ARRAY_SIZE(formats))
863 return -EINVAL;
865 dprintk(4, "name %s\n", formats[index].name);
866 strlcpy(f->description, formats[index].name, sizeof(f->description));
867 f->pixelformat = formats[index].fourcc;
868 return 0;
871 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
872 struct v4l2_format *f)
874 struct s2255_fh *fh = priv;
875 struct s2255_channel *channel = fh->channel;
877 f->fmt.pix.width = channel->width;
878 f->fmt.pix.height = channel->height;
879 f->fmt.pix.field = fh->vb_vidq.field;
880 f->fmt.pix.pixelformat = channel->fmt->fourcc;
881 f->fmt.pix.bytesperline = f->fmt.pix.width * (channel->fmt->depth >> 3);
882 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
883 return 0;
886 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
887 struct v4l2_format *f)
889 const struct s2255_fmt *fmt;
890 enum v4l2_field field;
891 int b_any_field = 0;
892 struct s2255_fh *fh = priv;
893 struct s2255_channel *channel = fh->channel;
894 int is_ntsc;
895 is_ntsc =
896 (channel->vdev.current_norm & V4L2_STD_NTSC) ? 1 : 0;
898 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
900 if (fmt == NULL)
901 return -EINVAL;
903 field = f->fmt.pix.field;
904 if (field == V4L2_FIELD_ANY)
905 b_any_field = 1;
907 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
908 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
909 if (is_ntsc) {
910 /* NTSC */
911 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
912 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
913 if (b_any_field) {
914 field = V4L2_FIELD_SEQ_TB;
915 } else if (!((field == V4L2_FIELD_INTERLACED) ||
916 (field == V4L2_FIELD_SEQ_TB) ||
917 (field == V4L2_FIELD_INTERLACED_TB))) {
918 dprintk(1, "unsupported field setting\n");
919 return -EINVAL;
921 } else {
922 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
923 if (b_any_field) {
924 field = V4L2_FIELD_TOP;
925 } else if (!((field == V4L2_FIELD_TOP) ||
926 (field == V4L2_FIELD_BOTTOM))) {
927 dprintk(1, "unsupported field setting\n");
928 return -EINVAL;
932 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
933 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
934 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
935 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
936 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
937 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
938 else
939 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
940 } else {
941 /* PAL */
942 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
943 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
944 if (b_any_field) {
945 field = V4L2_FIELD_SEQ_TB;
946 } else if (!((field == V4L2_FIELD_INTERLACED) ||
947 (field == V4L2_FIELD_SEQ_TB) ||
948 (field == V4L2_FIELD_INTERLACED_TB))) {
949 dprintk(1, "unsupported field setting\n");
950 return -EINVAL;
952 } else {
953 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
954 if (b_any_field) {
955 field = V4L2_FIELD_TOP;
956 } else if (!((field == V4L2_FIELD_TOP) ||
957 (field == V4L2_FIELD_BOTTOM))) {
958 dprintk(1, "unsupported field setting\n");
959 return -EINVAL;
962 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
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 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
967 field = V4L2_FIELD_TOP;
968 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
969 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
970 field = V4L2_FIELD_TOP;
971 } else {
972 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
973 field = V4L2_FIELD_TOP;
976 f->fmt.pix.field = field;
977 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
978 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
979 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
980 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
981 return 0;
984 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
985 struct v4l2_format *f)
987 struct s2255_fh *fh = priv;
988 struct s2255_channel *channel = fh->channel;
989 const struct s2255_fmt *fmt;
990 struct videobuf_queue *q = &fh->vb_vidq;
991 struct s2255_mode mode;
992 int ret;
993 int norm;
995 ret = vidioc_try_fmt_vid_cap(file, fh, f);
997 if (ret < 0)
998 return ret;
1000 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1002 if (fmt == NULL)
1003 return -EINVAL;
1005 mutex_lock(&q->vb_lock);
1007 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1008 dprintk(1, "queue busy\n");
1009 ret = -EBUSY;
1010 goto out_s_fmt;
1013 if (res_locked(fh)) {
1014 dprintk(1, "%s: channel busy\n", __func__);
1015 ret = -EBUSY;
1016 goto out_s_fmt;
1018 mode = channel->mode;
1019 channel->fmt = fmt;
1020 channel->width = f->fmt.pix.width;
1021 channel->height = f->fmt.pix.height;
1022 fh->vb_vidq.field = f->fmt.pix.field;
1023 fh->type = f->type;
1024 norm = norm_minw(&channel->vdev);
1025 if (channel->width > norm_minw(&channel->vdev)) {
1026 if (channel->height > norm_minh(&channel->vdev)) {
1027 if (channel->cap_parm.capturemode &
1028 V4L2_MODE_HIGHQUALITY)
1029 mode.scale = SCALE_4CIFSI;
1030 else
1031 mode.scale = SCALE_4CIFS;
1032 } else
1033 mode.scale = SCALE_2CIFS;
1035 } else {
1036 mode.scale = SCALE_1CIFS;
1038 /* color mode */
1039 switch (channel->fmt->fourcc) {
1040 case V4L2_PIX_FMT_GREY:
1041 mode.color &= ~MASK_COLOR;
1042 mode.color |= COLOR_Y8;
1043 break;
1044 case V4L2_PIX_FMT_JPEG:
1045 case V4L2_PIX_FMT_MJPEG:
1046 mode.color &= ~MASK_COLOR;
1047 mode.color |= COLOR_JPG;
1048 mode.color |= (channel->jc.quality << 8);
1049 break;
1050 case V4L2_PIX_FMT_YUV422P:
1051 mode.color &= ~MASK_COLOR;
1052 mode.color |= COLOR_YUVPL;
1053 break;
1054 case V4L2_PIX_FMT_YUYV:
1055 case V4L2_PIX_FMT_UYVY:
1056 default:
1057 mode.color &= ~MASK_COLOR;
1058 mode.color |= COLOR_YUVPK;
1059 break;
1061 if ((mode.color & MASK_COLOR) != (channel->mode.color & MASK_COLOR))
1062 mode.restart = 1;
1063 else if (mode.scale != channel->mode.scale)
1064 mode.restart = 1;
1065 else if (mode.format != channel->mode.format)
1066 mode.restart = 1;
1067 channel->mode = mode;
1068 (void) s2255_set_mode(channel, &mode);
1069 ret = 0;
1070 out_s_fmt:
1071 mutex_unlock(&q->vb_lock);
1072 return ret;
1075 static int vidioc_reqbufs(struct file *file, void *priv,
1076 struct v4l2_requestbuffers *p)
1078 int rc;
1079 struct s2255_fh *fh = priv;
1080 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1081 return rc;
1084 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1086 int rc;
1087 struct s2255_fh *fh = priv;
1088 rc = videobuf_querybuf(&fh->vb_vidq, p);
1089 return rc;
1092 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1094 int rc;
1095 struct s2255_fh *fh = priv;
1096 rc = videobuf_qbuf(&fh->vb_vidq, p);
1097 return rc;
1100 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1102 int rc;
1103 struct s2255_fh *fh = priv;
1104 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1105 return rc;
1108 /* write to the configuration pipe, synchronously */
1109 static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1110 int size)
1112 int pipe;
1113 int done;
1114 long retval = -1;
1115 if (udev) {
1116 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1117 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1119 return retval;
1122 static u32 get_transfer_size(struct s2255_mode *mode)
1124 int linesPerFrame = LINE_SZ_DEF;
1125 int pixelsPerLine = NUM_LINES_DEF;
1126 u32 outImageSize;
1127 u32 usbInSize;
1128 unsigned int mask_mult;
1130 if (mode == NULL)
1131 return 0;
1133 if (mode->format == FORMAT_NTSC) {
1134 switch (mode->scale) {
1135 case SCALE_4CIFS:
1136 case SCALE_4CIFSI:
1137 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1138 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1139 break;
1140 case SCALE_2CIFS:
1141 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1142 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1143 break;
1144 case SCALE_1CIFS:
1145 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1146 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1147 break;
1148 default:
1149 break;
1151 } else if (mode->format == FORMAT_PAL) {
1152 switch (mode->scale) {
1153 case SCALE_4CIFS:
1154 case SCALE_4CIFSI:
1155 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1156 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1157 break;
1158 case SCALE_2CIFS:
1159 linesPerFrame = NUM_LINES_2CIFS_PAL;
1160 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1161 break;
1162 case SCALE_1CIFS:
1163 linesPerFrame = NUM_LINES_1CIFS_PAL;
1164 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1165 break;
1166 default:
1167 break;
1170 outImageSize = linesPerFrame * pixelsPerLine;
1171 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1172 /* 2 bytes/pixel if not monochrome */
1173 outImageSize *= 2;
1176 /* total bytes to send including prefix and 4K padding;
1177 must be a multiple of USB_READ_SIZE */
1178 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1179 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1180 /* if size not a multiple of USB_READ_SIZE */
1181 if (usbInSize & ~mask_mult)
1182 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1183 return usbInSize;
1186 static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
1188 struct device *dev = &sdev->udev->dev;
1189 dev_info(dev, "------------------------------------------------\n");
1190 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1191 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
1192 dev_info(dev, "bright: 0x%x\n", mode->bright);
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
1200 * size.
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_channel *channel,
1205 struct s2255_mode *mode)
1207 int res;
1208 __le32 *buffer;
1209 unsigned long chn_rev;
1210 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1211 chn_rev = G_chnmap[channel->idx];
1212 dprintk(3, "%s channel: %d\n", __func__, channel->idx);
1213 /* if JPEG, set the quality */
1214 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1215 mode->color &= ~MASK_COLOR;
1216 mode->color |= COLOR_JPG;
1217 mode->color &= ~MASK_JPG_QUALITY;
1218 mode->color |= (channel->jc.quality << 8);
1220 /* save the mode */
1221 channel->mode = *mode;
1222 channel->req_image_size = get_transfer_size(mode);
1223 dprintk(1, "%s: reqsize %ld\n", __func__, channel->req_image_size);
1224 buffer = kzalloc(512, GFP_KERNEL);
1225 if (buffer == NULL) {
1226 dev_err(&dev->udev->dev, "out of mem\n");
1227 return -ENOMEM;
1229 /* set the mode */
1230 buffer[0] = IN_DATA_TOKEN;
1231 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1232 buffer[2] = CMD_SET_MODE;
1233 memcpy(&buffer[3], &channel->mode, sizeof(struct s2255_mode));
1234 channel->setmode_ready = 0;
1235 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1236 if (debug)
1237 s2255_print_cfg(dev, mode);
1238 kfree(buffer);
1239 /* wait at least 3 frames before continuing */
1240 if (mode->restart) {
1241 wait_event_timeout(channel->wait_setmode,
1242 (channel->setmode_ready != 0),
1243 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1244 if (channel->setmode_ready != 1) {
1245 printk(KERN_DEBUG "s2255: no set mode response\n");
1246 res = -EFAULT;
1249 /* clear the restart flag */
1250 channel->mode.restart = 0;
1251 dprintk(1, "%s chn %d, result: %d\n", __func__, channel->idx, res);
1252 return res;
1255 static int s2255_cmd_status(struct s2255_channel *channel, u32 *pstatus)
1257 int res;
1258 __le32 *buffer;
1259 u32 chn_rev;
1260 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1261 chn_rev = G_chnmap[channel->idx];
1262 dprintk(4, "%s chan %d\n", __func__, channel->idx);
1263 buffer = kzalloc(512, GFP_KERNEL);
1264 if (buffer == NULL) {
1265 dev_err(&dev->udev->dev, "out of mem\n");
1266 return -ENOMEM;
1268 /* form the get vid status command */
1269 buffer[0] = IN_DATA_TOKEN;
1270 buffer[1] = (__le32) cpu_to_le32(chn_rev);
1271 buffer[2] = CMD_STATUS;
1272 *pstatus = 0;
1273 channel->vidstatus_ready = 0;
1274 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1275 kfree(buffer);
1276 wait_event_timeout(channel->wait_vidstatus,
1277 (channel->vidstatus_ready != 0),
1278 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1279 if (channel->vidstatus_ready != 1) {
1280 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1281 res = -EFAULT;
1283 *pstatus = channel->vidstatus;
1284 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1285 return res;
1288 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1290 int res;
1291 struct s2255_fh *fh = priv;
1292 struct s2255_dev *dev = fh->dev;
1293 struct s2255_channel *channel = fh->channel;
1294 int j;
1295 dprintk(4, "%s\n", __func__);
1296 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1297 dev_err(&dev->udev->dev, "invalid fh type0\n");
1298 return -EINVAL;
1300 if (i != fh->type) {
1301 dev_err(&dev->udev->dev, "invalid fh type1\n");
1302 return -EINVAL;
1305 if (!res_get(fh)) {
1306 s2255_dev_err(&dev->udev->dev, "stream busy\n");
1307 return -EBUSY;
1309 channel->last_frame = -1;
1310 channel->bad_payload = 0;
1311 channel->cur_frame = 0;
1312 channel->frame_count = 0;
1313 for (j = 0; j < SYS_FRAMES; j++) {
1314 channel->buffer.frame[j].ulState = S2255_READ_IDLE;
1315 channel->buffer.frame[j].cur_size = 0;
1317 res = videobuf_streamon(&fh->vb_vidq);
1318 if (res == 0) {
1319 s2255_start_acquire(channel);
1320 channel->b_acquire = 1;
1321 } else
1322 res_free(fh);
1324 return res;
1327 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1329 struct s2255_fh *fh = priv;
1330 dprintk(4, "%s\n, channel: %d", __func__, fh->channel->idx);
1331 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1332 printk(KERN_ERR "invalid fh type0\n");
1333 return -EINVAL;
1335 if (i != fh->type) {
1336 printk(KERN_ERR "invalid type i\n");
1337 return -EINVAL;
1339 s2255_stop_acquire(fh->channel);
1340 videobuf_streamoff(&fh->vb_vidq);
1341 res_free(fh);
1342 return 0;
1345 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1347 struct s2255_fh *fh = priv;
1348 struct s2255_mode mode;
1349 struct videobuf_queue *q = &fh->vb_vidq;
1350 int ret = 0;
1351 mutex_lock(&q->vb_lock);
1352 if (videobuf_queue_is_busy(q)) {
1353 dprintk(1, "queue busy\n");
1354 ret = -EBUSY;
1355 goto out_s_std;
1357 if (res_locked(fh)) {
1358 dprintk(1, "can't change standard after started\n");
1359 ret = -EBUSY;
1360 goto out_s_std;
1362 mode = fh->channel->mode;
1363 if (*i & V4L2_STD_NTSC) {
1364 dprintk(4, "%s NTSC\n", __func__);
1365 /* if changing format, reset frame decimation/intervals */
1366 if (mode.format != FORMAT_NTSC) {
1367 mode.restart = 1;
1368 mode.format = FORMAT_NTSC;
1369 mode.fdec = FDEC_1;
1371 } else if (*i & V4L2_STD_PAL) {
1372 dprintk(4, "%s PAL\n", __func__);
1373 if (mode.format != FORMAT_PAL) {
1374 mode.restart = 1;
1375 mode.format = FORMAT_PAL;
1376 mode.fdec = FDEC_1;
1378 } else {
1379 ret = -EINVAL;
1381 if (mode.restart)
1382 s2255_set_mode(fh->channel, &mode);
1383 out_s_std:
1384 mutex_unlock(&q->vb_lock);
1385 return ret;
1388 /* Sensoray 2255 is a multiple channel capture device.
1389 It does not have a "crossbar" of inputs.
1390 We use one V4L device per channel. The user must
1391 be aware that certain combinations are not allowed.
1392 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1393 at once in color(you can do full fps on 4 channels with greyscale.
1395 static int vidioc_enum_input(struct file *file, void *priv,
1396 struct v4l2_input *inp)
1398 struct s2255_fh *fh = priv;
1399 struct s2255_dev *dev = fh->dev;
1400 struct s2255_channel *channel = fh->channel;
1401 u32 status = 0;
1402 if (inp->index != 0)
1403 return -EINVAL;
1404 inp->type = V4L2_INPUT_TYPE_CAMERA;
1405 inp->std = S2255_NORMS;
1406 inp->status = 0;
1407 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1408 int rc;
1409 rc = s2255_cmd_status(fh->channel, &status);
1410 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1411 if (rc == 0)
1412 inp->status = (status & 0x01) ? 0
1413 : V4L2_IN_ST_NO_SIGNAL;
1415 switch (dev->pid) {
1416 case 0x2255:
1417 default:
1418 strlcpy(inp->name, "Composite", sizeof(inp->name));
1419 break;
1420 case 0x2257:
1421 strlcpy(inp->name, (channel->idx < 2) ? "Composite" : "S-Video",
1422 sizeof(inp->name));
1423 break;
1425 return 0;
1428 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1430 *i = 0;
1431 return 0;
1433 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1435 if (i > 0)
1436 return -EINVAL;
1437 return 0;
1440 /* --- controls ---------------------------------------------- */
1441 static int vidioc_queryctrl(struct file *file, void *priv,
1442 struct v4l2_queryctrl *qc)
1444 struct s2255_fh *fh = priv;
1445 struct s2255_channel *channel = fh->channel;
1446 struct s2255_dev *dev = fh->dev;
1447 switch (qc->id) {
1448 case V4L2_CID_BRIGHTNESS:
1449 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1450 break;
1451 case V4L2_CID_CONTRAST:
1452 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1453 break;
1454 case V4L2_CID_SATURATION:
1455 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1456 break;
1457 case V4L2_CID_HUE:
1458 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1459 break;
1460 case V4L2_CID_PRIVATE_COLORFILTER:
1461 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1462 return -EINVAL;
1463 if ((dev->pid == 0x2257) && (channel->idx > 1))
1464 return -EINVAL;
1465 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1466 qc->type = V4L2_CTRL_TYPE_MENU;
1467 qc->minimum = 0;
1468 qc->maximum = 1;
1469 qc->step = 1;
1470 qc->default_value = 1;
1471 qc->flags = 0;
1472 break;
1473 default:
1474 return -EINVAL;
1476 dprintk(4, "%s, id %d\n", __func__, qc->id);
1477 return 0;
1480 static int vidioc_g_ctrl(struct file *file, void *priv,
1481 struct v4l2_control *ctrl)
1483 struct s2255_fh *fh = priv;
1484 struct s2255_dev *dev = fh->dev;
1485 struct s2255_channel *channel = fh->channel;
1486 switch (ctrl->id) {
1487 case V4L2_CID_BRIGHTNESS:
1488 ctrl->value = channel->mode.bright;
1489 break;
1490 case V4L2_CID_CONTRAST:
1491 ctrl->value = channel->mode.contrast;
1492 break;
1493 case V4L2_CID_SATURATION:
1494 ctrl->value = channel->mode.saturation;
1495 break;
1496 case V4L2_CID_HUE:
1497 ctrl->value = channel->mode.hue;
1498 break;
1499 case V4L2_CID_PRIVATE_COLORFILTER:
1500 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1501 return -EINVAL;
1502 if ((dev->pid == 0x2257) && (channel->idx > 1))
1503 return -EINVAL;
1504 ctrl->value = !((channel->mode.color & MASK_INPUT_TYPE) >> 16);
1505 break;
1506 default:
1507 return -EINVAL;
1509 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1510 return 0;
1513 static int vidioc_s_ctrl(struct file *file, void *priv,
1514 struct v4l2_control *ctrl)
1516 struct s2255_fh *fh = priv;
1517 struct s2255_channel *channel = fh->channel;
1518 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1519 struct s2255_mode mode;
1520 mode = channel->mode;
1521 dprintk(4, "%s\n", __func__);
1522 /* update the mode to the corresponding value */
1523 switch (ctrl->id) {
1524 case V4L2_CID_BRIGHTNESS:
1525 mode.bright = ctrl->value;
1526 break;
1527 case V4L2_CID_CONTRAST:
1528 mode.contrast = ctrl->value;
1529 break;
1530 case V4L2_CID_HUE:
1531 mode.hue = ctrl->value;
1532 break;
1533 case V4L2_CID_SATURATION:
1534 mode.saturation = ctrl->value;
1535 break;
1536 case V4L2_CID_PRIVATE_COLORFILTER:
1537 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1538 return -EINVAL;
1539 if ((dev->pid == 0x2257) && (channel->idx > 1))
1540 return -EINVAL;
1541 mode.color &= ~MASK_INPUT_TYPE;
1542 mode.color |= ((ctrl->value ? 0 : 1) << 16);
1543 break;
1544 default:
1545 return -EINVAL;
1547 mode.restart = 0;
1548 /* set mode here. Note: stream does not need restarted.
1549 some V4L programs restart stream unnecessarily
1550 after a s_crtl.
1552 s2255_set_mode(fh->channel, &mode);
1553 return 0;
1556 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1557 struct v4l2_jpegcompression *jc)
1559 struct s2255_fh *fh = priv;
1560 struct s2255_channel *channel = fh->channel;
1561 *jc = channel->jc;
1562 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1563 return 0;
1566 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1567 struct v4l2_jpegcompression *jc)
1569 struct s2255_fh *fh = priv;
1570 struct s2255_channel *channel = fh->channel;
1571 if (jc->quality < 0 || jc->quality > 100)
1572 return -EINVAL;
1573 channel->jc.quality = jc->quality;
1574 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
1575 return 0;
1578 static int vidioc_g_parm(struct file *file, void *priv,
1579 struct v4l2_streamparm *sp)
1581 struct s2255_fh *fh = priv;
1582 __u32 def_num, def_dem;
1583 struct s2255_channel *channel = fh->channel;
1584 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1585 return -EINVAL;
1586 memset(sp, 0, sizeof(struct v4l2_streamparm));
1587 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1588 sp->parm.capture.capturemode = channel->cap_parm.capturemode;
1589 def_num = (channel->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1590 def_dem = (channel->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1591 sp->parm.capture.timeperframe.denominator = def_dem;
1592 switch (channel->mode.fdec) {
1593 default:
1594 case FDEC_1:
1595 sp->parm.capture.timeperframe.numerator = def_num;
1596 break;
1597 case FDEC_2:
1598 sp->parm.capture.timeperframe.numerator = def_num * 2;
1599 break;
1600 case FDEC_3:
1601 sp->parm.capture.timeperframe.numerator = def_num * 3;
1602 break;
1603 case FDEC_5:
1604 sp->parm.capture.timeperframe.numerator = def_num * 5;
1605 break;
1607 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1608 sp->parm.capture.capturemode,
1609 sp->parm.capture.timeperframe.numerator,
1610 sp->parm.capture.timeperframe.denominator);
1611 return 0;
1614 static int vidioc_s_parm(struct file *file, void *priv,
1615 struct v4l2_streamparm *sp)
1617 struct s2255_fh *fh = priv;
1618 struct s2255_channel *channel = fh->channel;
1619 struct s2255_mode mode;
1620 int fdec = FDEC_1;
1621 __u32 def_num, def_dem;
1622 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1623 return -EINVAL;
1624 mode = channel->mode;
1625 /* high quality capture mode requires a stream restart */
1626 if (channel->cap_parm.capturemode
1627 != sp->parm.capture.capturemode && res_locked(fh))
1628 return -EBUSY;
1629 def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
1630 def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
1631 if (def_dem != sp->parm.capture.timeperframe.denominator)
1632 sp->parm.capture.timeperframe.numerator = def_num;
1633 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1634 sp->parm.capture.timeperframe.numerator = def_num;
1635 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1636 sp->parm.capture.timeperframe.numerator = def_num * 2;
1637 fdec = FDEC_2;
1638 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1639 sp->parm.capture.timeperframe.numerator = def_num * 3;
1640 fdec = FDEC_3;
1641 } else {
1642 sp->parm.capture.timeperframe.numerator = def_num * 5;
1643 fdec = FDEC_5;
1645 mode.fdec = fdec;
1646 sp->parm.capture.timeperframe.denominator = def_dem;
1647 s2255_set_mode(channel, &mode);
1648 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1649 __func__,
1650 sp->parm.capture.capturemode,
1651 sp->parm.capture.timeperframe.numerator,
1652 sp->parm.capture.timeperframe.denominator, fdec);
1653 return 0;
1656 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1657 struct v4l2_frmivalenum *fe)
1659 int is_ntsc = 0;
1660 #define NUM_FRAME_ENUMS 4
1661 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1662 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1663 return -EINVAL;
1664 switch (fe->width) {
1665 case 640:
1666 if (fe->height != 240 && fe->height != 480)
1667 return -EINVAL;
1668 is_ntsc = 1;
1669 break;
1670 case 320:
1671 if (fe->height != 240)
1672 return -EINVAL;
1673 is_ntsc = 1;
1674 break;
1675 case 704:
1676 if (fe->height != 288 && fe->height != 576)
1677 return -EINVAL;
1678 break;
1679 case 352:
1680 if (fe->height != 288)
1681 return -EINVAL;
1682 break;
1683 default:
1684 return -EINVAL;
1686 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1687 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1688 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1689 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1690 fe->discrete.denominator);
1691 return 0;
1694 static int s2255_open(struct file *file)
1696 struct video_device *vdev = video_devdata(file);
1697 struct s2255_channel *channel = video_drvdata(file);
1698 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1699 struct s2255_fh *fh;
1700 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1701 int state;
1702 dprintk(1, "s2255: open called (dev=%s)\n",
1703 video_device_node_name(vdev));
1705 * open lock necessary to prevent multiple instances
1706 * of v4l-conf (or other programs) from simultaneously
1707 * reloading firmware.
1709 mutex_lock(&dev->open_lock);
1710 state = atomic_read(&dev->fw_data->fw_state);
1711 switch (state) {
1712 case S2255_FW_DISCONNECTING:
1713 mutex_unlock(&dev->open_lock);
1714 return -ENODEV;
1715 case S2255_FW_FAILED:
1716 s2255_dev_err(&dev->udev->dev,
1717 "firmware load failed. retrying.\n");
1718 s2255_fwload_start(dev, 1);
1719 wait_event_timeout(dev->fw_data->wait_fw,
1720 ((atomic_read(&dev->fw_data->fw_state)
1721 == S2255_FW_SUCCESS) ||
1722 (atomic_read(&dev->fw_data->fw_state)
1723 == S2255_FW_DISCONNECTING)),
1724 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1725 /* state may have changed, re-read */
1726 state = atomic_read(&dev->fw_data->fw_state);
1727 break;
1728 case S2255_FW_NOTLOADED:
1729 case S2255_FW_LOADED_DSPWAIT:
1730 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1731 driver loaded and then device immediately opened */
1732 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1733 wait_event_timeout(dev->fw_data->wait_fw,
1734 ((atomic_read(&dev->fw_data->fw_state)
1735 == S2255_FW_SUCCESS) ||
1736 (atomic_read(&dev->fw_data->fw_state)
1737 == S2255_FW_DISCONNECTING)),
1738 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1739 /* state may have changed, re-read */
1740 state = atomic_read(&dev->fw_data->fw_state);
1741 break;
1742 case S2255_FW_SUCCESS:
1743 default:
1744 break;
1746 /* state may have changed in above switch statement */
1747 switch (state) {
1748 case S2255_FW_SUCCESS:
1749 break;
1750 case S2255_FW_FAILED:
1751 printk(KERN_INFO "2255 firmware load failed.\n");
1752 mutex_unlock(&dev->open_lock);
1753 return -ENODEV;
1754 case S2255_FW_DISCONNECTING:
1755 printk(KERN_INFO "%s: disconnecting\n", __func__);
1756 mutex_unlock(&dev->open_lock);
1757 return -ENODEV;
1758 case S2255_FW_LOADED_DSPWAIT:
1759 case S2255_FW_NOTLOADED:
1760 printk(KERN_INFO "%s: firmware not loaded yet"
1761 "please try again later\n",
1762 __func__);
1764 * Timeout on firmware load means device unusable.
1765 * Set firmware failure state.
1766 * On next s2255_open the firmware will be reloaded.
1768 atomic_set(&dev->fw_data->fw_state,
1769 S2255_FW_FAILED);
1770 mutex_unlock(&dev->open_lock);
1771 return -EAGAIN;
1772 default:
1773 printk(KERN_INFO "%s: unknown state\n", __func__);
1774 mutex_unlock(&dev->open_lock);
1775 return -EFAULT;
1777 mutex_unlock(&dev->open_lock);
1778 /* allocate + initialize per filehandle data */
1779 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1780 if (NULL == fh)
1781 return -ENOMEM;
1782 file->private_data = fh;
1783 fh->dev = dev;
1784 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1785 fh->channel = channel;
1786 if (!channel->configured) {
1787 /* configure channel to default state */
1788 channel->fmt = &formats[0];
1789 s2255_set_mode(channel, &channel->mode);
1790 channel->configured = 1;
1792 dprintk(1, "%s: dev=%s type=%s\n", __func__,
1793 video_device_node_name(vdev), v4l2_type_names[type]);
1794 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
1795 (unsigned long)fh, (unsigned long)dev,
1796 (unsigned long)&channel->vidq);
1797 dprintk(4, "%s: list_empty active=%d\n", __func__,
1798 list_empty(&channel->vidq.active));
1799 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1800 NULL, &dev->slock,
1801 fh->type,
1802 V4L2_FIELD_INTERLACED,
1803 sizeof(struct s2255_buffer),
1804 fh, vdev->lock);
1805 return 0;
1809 static unsigned int s2255_poll(struct file *file,
1810 struct poll_table_struct *wait)
1812 struct s2255_fh *fh = file->private_data;
1813 int rc;
1814 dprintk(100, "%s\n", __func__);
1815 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1816 return POLLERR;
1817 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1818 return rc;
1821 static void s2255_destroy(struct s2255_dev *dev)
1823 /* board shutdown stops the read pipe if it is running */
1824 s2255_board_shutdown(dev);
1825 /* make sure firmware still not trying to load */
1826 del_timer(&dev->timer); /* only started in .probe and .open */
1827 if (dev->fw_data->fw_urb) {
1828 usb_kill_urb(dev->fw_data->fw_urb);
1829 usb_free_urb(dev->fw_data->fw_urb);
1830 dev->fw_data->fw_urb = NULL;
1832 if (dev->fw_data->fw)
1833 release_firmware(dev->fw_data->fw);
1834 kfree(dev->fw_data->pfw_data);
1835 kfree(dev->fw_data);
1836 /* reset the DSP so firmware can be reloaded next time */
1837 s2255_reset_dsppower(dev);
1838 mutex_destroy(&dev->open_lock);
1839 mutex_destroy(&dev->lock);
1840 usb_put_dev(dev->udev);
1841 v4l2_device_unregister(&dev->v4l2_dev);
1842 dprintk(1, "%s", __func__);
1843 kfree(dev);
1846 static int s2255_release(struct file *file)
1848 struct s2255_fh *fh = file->private_data;
1849 struct s2255_dev *dev = fh->dev;
1850 struct video_device *vdev = video_devdata(file);
1851 struct s2255_channel *channel = fh->channel;
1852 if (!dev)
1853 return -ENODEV;
1854 /* turn off stream */
1855 if (res_check(fh)) {
1856 if (channel->b_acquire)
1857 s2255_stop_acquire(fh->channel);
1858 videobuf_streamoff(&fh->vb_vidq);
1859 res_free(fh);
1861 videobuf_mmap_free(&fh->vb_vidq);
1862 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
1863 kfree(fh);
1864 return 0;
1867 static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1869 struct s2255_fh *fh = file->private_data;
1870 int ret;
1872 if (!fh)
1873 return -ENODEV;
1874 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
1875 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1876 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
1877 (unsigned long)vma->vm_start,
1878 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1879 return ret;
1882 static const struct v4l2_file_operations s2255_fops_v4l = {
1883 .owner = THIS_MODULE,
1884 .open = s2255_open,
1885 .release = s2255_release,
1886 .poll = s2255_poll,
1887 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1888 .mmap = s2255_mmap_v4l,
1891 static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1892 .vidioc_querymenu = vidioc_querymenu,
1893 .vidioc_querycap = vidioc_querycap,
1894 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1895 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1896 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1897 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1898 .vidioc_reqbufs = vidioc_reqbufs,
1899 .vidioc_querybuf = vidioc_querybuf,
1900 .vidioc_qbuf = vidioc_qbuf,
1901 .vidioc_dqbuf = vidioc_dqbuf,
1902 .vidioc_s_std = vidioc_s_std,
1903 .vidioc_enum_input = vidioc_enum_input,
1904 .vidioc_g_input = vidioc_g_input,
1905 .vidioc_s_input = vidioc_s_input,
1906 .vidioc_queryctrl = vidioc_queryctrl,
1907 .vidioc_g_ctrl = vidioc_g_ctrl,
1908 .vidioc_s_ctrl = vidioc_s_ctrl,
1909 .vidioc_streamon = vidioc_streamon,
1910 .vidioc_streamoff = vidioc_streamoff,
1911 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1912 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1913 .vidioc_s_parm = vidioc_s_parm,
1914 .vidioc_g_parm = vidioc_g_parm,
1915 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1918 static void s2255_video_device_release(struct video_device *vdev)
1920 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1921 dprintk(4, "%s, chnls: %d \n", __func__,
1922 atomic_read(&dev->num_channels));
1923 if (atomic_dec_and_test(&dev->num_channels))
1924 s2255_destroy(dev);
1925 return;
1928 static struct video_device template = {
1929 .name = "s2255v",
1930 .fops = &s2255_fops_v4l,
1931 .ioctl_ops = &s2255_ioctl_ops,
1932 .release = s2255_video_device_release,
1933 .tvnorms = S2255_NORMS,
1934 .current_norm = V4L2_STD_NTSC_M,
1937 static int s2255_probe_v4l(struct s2255_dev *dev)
1939 int ret;
1940 int i;
1941 int cur_nr = video_nr;
1942 struct s2255_channel *channel;
1943 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1944 if (ret)
1945 return ret;
1946 /* initialize all video 4 linux */
1947 /* register 4 video devices */
1948 for (i = 0; i < MAX_CHANNELS; i++) {
1949 channel = &dev->channel[i];
1950 INIT_LIST_HEAD(&channel->vidq.active);
1951 channel->vidq.dev = dev;
1952 /* register 4 video devices */
1953 channel->vdev = template;
1954 channel->vdev.lock = &dev->lock;
1955 channel->vdev.v4l2_dev = &dev->v4l2_dev;
1956 video_set_drvdata(&channel->vdev, channel);
1957 if (video_nr == -1)
1958 ret = video_register_device(&channel->vdev,
1959 VFL_TYPE_GRABBER,
1960 video_nr);
1961 else
1962 ret = video_register_device(&channel->vdev,
1963 VFL_TYPE_GRABBER,
1964 cur_nr + i);
1966 if (ret) {
1967 dev_err(&dev->udev->dev,
1968 "failed to register video device!\n");
1969 break;
1971 atomic_inc(&dev->num_channels);
1972 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1973 video_device_node_name(&channel->vdev));
1976 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1977 S2255_MAJOR_VERSION,
1978 S2255_MINOR_VERSION);
1979 /* if no channels registered, return error and probe will fail*/
1980 if (atomic_read(&dev->num_channels) == 0) {
1981 v4l2_device_unregister(&dev->v4l2_dev);
1982 return ret;
1984 if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
1985 printk(KERN_WARNING "s2255: Not all channels available.\n");
1986 return 0;
1989 /* this function moves the usb stream read pipe data
1990 * into the system buffers.
1991 * returns 0 on success, EAGAIN if more data to process( call this
1992 * function again).
1994 * Received frame structure:
1995 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1996 * bytes 4-7: channel: 0-3
1997 * bytes 8-11: payload size: size of the frame
1998 * bytes 12-payloadsize+12: frame data
2000 static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2002 char *pdest;
2003 u32 offset = 0;
2004 int bframe = 0;
2005 char *psrc;
2006 unsigned long copy_size;
2007 unsigned long size;
2008 s32 idx = -1;
2009 struct s2255_framei *frm;
2010 unsigned char *pdata;
2011 struct s2255_channel *channel;
2012 dprintk(100, "buffer to user\n");
2013 channel = &dev->channel[dev->cc];
2014 idx = channel->cur_frame;
2015 frm = &channel->buffer.frame[idx];
2016 if (frm->ulState == S2255_READ_IDLE) {
2017 int jj;
2018 unsigned int cc;
2019 __le32 *pdword; /*data from dsp is little endian */
2020 int payload;
2021 /* search for marker codes */
2022 pdata = (unsigned char *)pipe_info->transfer_buffer;
2023 pdword = (__le32 *)pdata;
2024 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
2025 switch (*pdword) {
2026 case S2255_MARKER_FRAME:
2027 dprintk(4, "found frame marker at offset:"
2028 " %d [%x %x]\n", jj, pdata[0],
2029 pdata[1]);
2030 offset = jj + PREFIX_SIZE;
2031 bframe = 1;
2032 cc = pdword[1];
2033 if (cc >= MAX_CHANNELS) {
2034 printk(KERN_ERR
2035 "bad channel\n");
2036 return -EINVAL;
2038 /* reverse it */
2039 dev->cc = G_chnmap[cc];
2040 channel = &dev->channel[dev->cc];
2041 payload = pdword[3];
2042 if (payload > channel->req_image_size) {
2043 channel->bad_payload++;
2044 /* discard the bad frame */
2045 return -EINVAL;
2047 channel->pkt_size = payload;
2048 channel->jpg_size = pdword[4];
2049 break;
2050 case S2255_MARKER_RESPONSE:
2052 pdata += DEF_USB_BLOCK;
2053 jj += DEF_USB_BLOCK;
2054 if (pdword[1] >= MAX_CHANNELS)
2055 break;
2056 cc = G_chnmap[pdword[1]];
2057 if (cc >= MAX_CHANNELS)
2058 break;
2059 channel = &dev->channel[cc];
2060 switch (pdword[2]) {
2061 case S2255_RESPONSE_SETMODE:
2062 /* check if channel valid */
2063 /* set mode ready */
2064 channel->setmode_ready = 1;
2065 wake_up(&channel->wait_setmode);
2066 dprintk(5, "setmode ready %d\n", cc);
2067 break;
2068 case S2255_RESPONSE_FW:
2069 dev->chn_ready |= (1 << cc);
2070 if ((dev->chn_ready & 0x0f) != 0x0f)
2071 break;
2072 /* all channels ready */
2073 printk(KERN_INFO "s2255: fw loaded\n");
2074 atomic_set(&dev->fw_data->fw_state,
2075 S2255_FW_SUCCESS);
2076 wake_up(&dev->fw_data->wait_fw);
2077 break;
2078 case S2255_RESPONSE_STATUS:
2079 channel->vidstatus = pdword[3];
2080 channel->vidstatus_ready = 1;
2081 wake_up(&channel->wait_vidstatus);
2082 dprintk(5, "got vidstatus %x chan %d\n",
2083 pdword[3], cc);
2084 break;
2085 default:
2086 printk(KERN_INFO "s2255 unknown resp\n");
2088 default:
2089 pdata++;
2090 break;
2092 if (bframe)
2093 break;
2094 } /* for */
2095 if (!bframe)
2096 return -EINVAL;
2098 channel = &dev->channel[dev->cc];
2099 idx = channel->cur_frame;
2100 frm = &channel->buffer.frame[idx];
2101 /* search done. now find out if should be acquiring on this channel */
2102 if (!channel->b_acquire) {
2103 /* we found a frame, but this channel is turned off */
2104 frm->ulState = S2255_READ_IDLE;
2105 return -EINVAL;
2108 if (frm->ulState == S2255_READ_IDLE) {
2109 frm->ulState = S2255_READ_FRAME;
2110 frm->cur_size = 0;
2113 /* skip the marker 512 bytes (and offset if out of sync) */
2114 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2117 if (frm->lpvbits == NULL) {
2118 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2119 frm, dev, dev->cc, idx);
2120 return -ENOMEM;
2123 pdest = frm->lpvbits + frm->cur_size;
2125 copy_size = (pipe_info->cur_transfer_size - offset);
2127 size = channel->pkt_size - PREFIX_SIZE;
2129 /* sanity check on pdest */
2130 if ((copy_size + frm->cur_size) < channel->req_image_size)
2131 memcpy(pdest, psrc, copy_size);
2133 frm->cur_size += copy_size;
2134 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2136 if (frm->cur_size >= size) {
2137 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2138 dev->cc, idx);
2139 channel->last_frame = channel->cur_frame;
2140 channel->cur_frame++;
2141 /* end of system frame ring buffer, start at zero */
2142 if ((channel->cur_frame == SYS_FRAMES) ||
2143 (channel->cur_frame == channel->buffer.dwFrames))
2144 channel->cur_frame = 0;
2145 /* frame ready */
2146 if (channel->b_acquire)
2147 s2255_got_frame(channel, channel->jpg_size);
2148 channel->frame_count++;
2149 frm->ulState = S2255_READ_IDLE;
2150 frm->cur_size = 0;
2153 /* done successfully */
2154 return 0;
2157 static void s2255_read_video_callback(struct s2255_dev *dev,
2158 struct s2255_pipeinfo *pipe_info)
2160 int res;
2161 dprintk(50, "callback read video \n");
2163 if (dev->cc >= MAX_CHANNELS) {
2164 dev->cc = 0;
2165 dev_err(&dev->udev->dev, "invalid channel\n");
2166 return;
2168 /* otherwise copy to the system buffers */
2169 res = save_frame(dev, pipe_info);
2170 if (res != 0)
2171 dprintk(4, "s2255: read callback failed\n");
2173 dprintk(50, "callback read video done\n");
2174 return;
2177 static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2178 u16 Index, u16 Value, void *TransferBuffer,
2179 s32 TransferBufferLength, int bOut)
2181 int r;
2182 if (!bOut) {
2183 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2184 Request,
2185 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2186 USB_DIR_IN,
2187 Value, Index, TransferBuffer,
2188 TransferBufferLength, HZ * 5);
2189 } else {
2190 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2191 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2192 Value, Index, TransferBuffer,
2193 TransferBufferLength, HZ * 5);
2195 return r;
2199 * retrieve FX2 firmware version. future use.
2200 * @param dev pointer to device extension
2201 * @return -1 for fail, else returns firmware version as an int(16 bits)
2203 static int s2255_get_fx2fw(struct s2255_dev *dev)
2205 int fw;
2206 int ret;
2207 unsigned char transBuffer[64];
2208 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2209 S2255_VR_IN);
2210 if (ret < 0)
2211 dprintk(2, "get fw error: %x\n", ret);
2212 fw = transBuffer[0] + (transBuffer[1] << 8);
2213 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2214 return fw;
2218 * Create the system ring buffer to copy frames into from the
2219 * usb read pipe.
2221 static int s2255_create_sys_buffers(struct s2255_channel *channel)
2223 unsigned long i;
2224 unsigned long reqsize;
2225 dprintk(1, "create sys buffers\n");
2226 channel->buffer.dwFrames = SYS_FRAMES;
2227 /* always allocate maximum size(PAL) for system buffers */
2228 reqsize = SYS_FRAMES_MAXSIZE;
2230 if (reqsize > SYS_FRAMES_MAXSIZE)
2231 reqsize = SYS_FRAMES_MAXSIZE;
2233 for (i = 0; i < SYS_FRAMES; i++) {
2234 /* allocate the frames */
2235 channel->buffer.frame[i].lpvbits = vmalloc(reqsize);
2236 dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n",
2237 &channel->buffer.frame[i], channel->idx, i,
2238 channel->buffer.frame[i].lpvbits);
2239 channel->buffer.frame[i].size = reqsize;
2240 if (channel->buffer.frame[i].lpvbits == NULL) {
2241 printk(KERN_INFO "out of memory. using less frames\n");
2242 channel->buffer.dwFrames = i;
2243 break;
2247 /* make sure internal states are set */
2248 for (i = 0; i < SYS_FRAMES; i++) {
2249 channel->buffer.frame[i].ulState = 0;
2250 channel->buffer.frame[i].cur_size = 0;
2253 channel->cur_frame = 0;
2254 channel->last_frame = -1;
2255 return 0;
2258 static int s2255_release_sys_buffers(struct s2255_channel *channel)
2260 unsigned long i;
2261 dprintk(1, "release sys buffers\n");
2262 for (i = 0; i < SYS_FRAMES; i++) {
2263 if (channel->buffer.frame[i].lpvbits) {
2264 dprintk(1, "vfree %p\n",
2265 channel->buffer.frame[i].lpvbits);
2266 vfree(channel->buffer.frame[i].lpvbits);
2268 channel->buffer.frame[i].lpvbits = NULL;
2270 return 0;
2273 static int s2255_board_init(struct s2255_dev *dev)
2275 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2276 int fw_ver;
2277 int j;
2278 struct s2255_pipeinfo *pipe = &dev->pipe;
2279 dprintk(4, "board init: %p", dev);
2280 memset(pipe, 0, sizeof(*pipe));
2281 pipe->dev = dev;
2282 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2283 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2285 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2286 GFP_KERNEL);
2287 if (pipe->transfer_buffer == NULL) {
2288 dprintk(1, "out of memory!\n");
2289 return -ENOMEM;
2291 /* query the firmware */
2292 fw_ver = s2255_get_fx2fw(dev);
2294 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2295 (fw_ver >> 8) & 0xff,
2296 fw_ver & 0xff);
2298 if (fw_ver < S2255_CUR_USB_FWVER)
2299 dev_err(&dev->udev->dev,
2300 "usb firmware not up to date %d.%d\n",
2301 (fw_ver >> 8) & 0xff,
2302 fw_ver & 0xff);
2304 for (j = 0; j < MAX_CHANNELS; j++) {
2305 struct s2255_channel *channel = &dev->channel[j];
2306 channel->b_acquire = 0;
2307 channel->mode = mode_def;
2308 if (dev->pid == 0x2257 && j > 1)
2309 channel->mode.color |= (1 << 16);
2310 channel->jc.quality = S2255_DEF_JPEG_QUAL;
2311 channel->width = LINE_SZ_4CIFS_NTSC;
2312 channel->height = NUM_LINES_4CIFS_NTSC * 2;
2313 channel->fmt = &formats[0];
2314 channel->mode.restart = 1;
2315 channel->req_image_size = get_transfer_size(&mode_def);
2316 channel->frame_count = 0;
2317 /* create the system buffers */
2318 s2255_create_sys_buffers(channel);
2320 /* start read pipe */
2321 s2255_start_readpipe(dev);
2322 dprintk(1, "%s: success\n", __func__);
2323 return 0;
2326 static int s2255_board_shutdown(struct s2255_dev *dev)
2328 u32 i;
2329 dprintk(1, "%s: dev: %p", __func__, dev);
2331 for (i = 0; i < MAX_CHANNELS; i++) {
2332 if (dev->channel[i].b_acquire)
2333 s2255_stop_acquire(&dev->channel[i]);
2335 s2255_stop_readpipe(dev);
2336 for (i = 0; i < MAX_CHANNELS; i++)
2337 s2255_release_sys_buffers(&dev->channel[i]);
2338 /* release transfer buffer */
2339 kfree(dev->pipe.transfer_buffer);
2340 return 0;
2343 static void read_pipe_completion(struct urb *purb)
2345 struct s2255_pipeinfo *pipe_info;
2346 struct s2255_dev *dev;
2347 int status;
2348 int pipe;
2349 pipe_info = purb->context;
2350 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
2351 purb->status);
2352 if (pipe_info == NULL) {
2353 dev_err(&purb->dev->dev, "no context!\n");
2354 return;
2357 dev = pipe_info->dev;
2358 if (dev == NULL) {
2359 dev_err(&purb->dev->dev, "no context!\n");
2360 return;
2362 status = purb->status;
2363 /* if shutting down, do not resubmit, exit immediately */
2364 if (status == -ESHUTDOWN) {
2365 dprintk(2, "%s: err shutdown\n", __func__);
2366 pipe_info->err_count++;
2367 return;
2370 if (pipe_info->state == 0) {
2371 dprintk(2, "%s: exiting USB pipe", __func__);
2372 return;
2375 if (status == 0)
2376 s2255_read_video_callback(dev, pipe_info);
2377 else {
2378 pipe_info->err_count++;
2379 dprintk(1, "%s: failed URB %d\n", __func__, status);
2382 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2383 /* reuse urb */
2384 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2385 pipe,
2386 pipe_info->transfer_buffer,
2387 pipe_info->cur_transfer_size,
2388 read_pipe_completion, pipe_info);
2390 if (pipe_info->state != 0) {
2391 if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC)) {
2392 dev_err(&dev->udev->dev, "error submitting urb\n");
2394 } else {
2395 dprintk(2, "%s :complete state 0\n", __func__);
2397 return;
2400 static int s2255_start_readpipe(struct s2255_dev *dev)
2402 int pipe;
2403 int retval;
2404 struct s2255_pipeinfo *pipe_info = &dev->pipe;
2405 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2406 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
2407 pipe_info->state = 1;
2408 pipe_info->err_count = 0;
2409 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2410 if (!pipe_info->stream_urb) {
2411 dev_err(&dev->udev->dev,
2412 "ReadStream: Unable to alloc URB\n");
2413 return -ENOMEM;
2415 /* transfer buffer allocated in board_init */
2416 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2417 pipe,
2418 pipe_info->transfer_buffer,
2419 pipe_info->cur_transfer_size,
2420 read_pipe_completion, pipe_info);
2421 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2422 if (retval) {
2423 printk(KERN_ERR "s2255: start read pipe failed\n");
2424 return retval;
2426 return 0;
2429 /* starts acquisition process */
2430 static int s2255_start_acquire(struct s2255_channel *channel)
2432 unsigned char *buffer;
2433 int res;
2434 unsigned long chn_rev;
2435 int j;
2436 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2437 chn_rev = G_chnmap[channel->idx];
2438 buffer = kzalloc(512, GFP_KERNEL);
2439 if (buffer == NULL) {
2440 dev_err(&dev->udev->dev, "out of mem\n");
2441 return -ENOMEM;
2444 channel->last_frame = -1;
2445 channel->bad_payload = 0;
2446 channel->cur_frame = 0;
2447 for (j = 0; j < SYS_FRAMES; j++) {
2448 channel->buffer.frame[j].ulState = 0;
2449 channel->buffer.frame[j].cur_size = 0;
2452 /* send the start command */
2453 *(__le32 *) buffer = IN_DATA_TOKEN;
2454 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2455 *((__le32 *) buffer + 2) = CMD_START;
2456 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2457 if (res != 0)
2458 dev_err(&dev->udev->dev, "CMD_START error\n");
2460 dprintk(2, "start acquire exit[%d] %d \n", channel->idx, res);
2461 kfree(buffer);
2462 return 0;
2465 static int s2255_stop_acquire(struct s2255_channel *channel)
2467 unsigned char *buffer;
2468 int res;
2469 unsigned long chn_rev;
2470 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2471 chn_rev = G_chnmap[channel->idx];
2472 buffer = kzalloc(512, GFP_KERNEL);
2473 if (buffer == NULL) {
2474 dev_err(&dev->udev->dev, "out of mem\n");
2475 return -ENOMEM;
2477 /* send the stop command */
2478 *(__le32 *) buffer = IN_DATA_TOKEN;
2479 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2480 *((__le32 *) buffer + 2) = CMD_STOP;
2481 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2482 if (res != 0)
2483 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2484 kfree(buffer);
2485 channel->b_acquire = 0;
2486 dprintk(4, "%s: chn %d, res %d\n", __func__, channel->idx, res);
2487 return res;
2490 static void s2255_stop_readpipe(struct s2255_dev *dev)
2492 struct s2255_pipeinfo *pipe = &dev->pipe;
2494 pipe->state = 0;
2495 if (pipe->stream_urb) {
2496 /* cancel urb */
2497 usb_kill_urb(pipe->stream_urb);
2498 usb_free_urb(pipe->stream_urb);
2499 pipe->stream_urb = NULL;
2501 dprintk(4, "%s", __func__);
2502 return;
2505 static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2507 if (reset)
2508 s2255_reset_dsppower(dev);
2509 dev->fw_data->fw_size = dev->fw_data->fw->size;
2510 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2511 memcpy(dev->fw_data->pfw_data,
2512 dev->fw_data->fw->data, CHUNK_SIZE);
2513 dev->fw_data->fw_loaded = CHUNK_SIZE;
2514 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2515 usb_sndbulkpipe(dev->udev, 2),
2516 dev->fw_data->pfw_data,
2517 CHUNK_SIZE, s2255_fwchunk_complete,
2518 dev->fw_data);
2519 mod_timer(&dev->timer, jiffies + HZ);
2522 /* standard usb probe function */
2523 static int s2255_probe(struct usb_interface *interface,
2524 const struct usb_device_id *id)
2526 struct s2255_dev *dev = NULL;
2527 struct usb_host_interface *iface_desc;
2528 struct usb_endpoint_descriptor *endpoint;
2529 int i;
2530 int retval = -ENOMEM;
2531 __le32 *pdata;
2532 int fw_size;
2533 dprintk(2, "%s\n", __func__);
2534 /* allocate memory for our device state and initialize it to zero */
2535 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2536 if (dev == NULL) {
2537 s2255_dev_err(&interface->dev, "out of memory\n");
2538 return -ENOMEM;
2540 atomic_set(&dev->num_channels, 0);
2541 dev->pid = id->idProduct;
2542 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2543 if (!dev->fw_data)
2544 goto errorFWDATA1;
2545 mutex_init(&dev->lock);
2546 mutex_init(&dev->open_lock);
2547 /* grab usb_device and save it */
2548 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2549 if (dev->udev == NULL) {
2550 dev_err(&interface->dev, "null usb device\n");
2551 retval = -ENODEV;
2552 goto errorUDEV;
2554 dprintk(1, "dev: %p, udev %p interface %p\n", dev,
2555 dev->udev, interface);
2556 dev->interface = interface;
2557 /* set up the endpoint information */
2558 iface_desc = interface->cur_altsetting;
2559 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2560 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2561 endpoint = &iface_desc->endpoint[i].desc;
2562 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2563 /* we found the bulk in endpoint */
2564 dev->read_endpoint = endpoint->bEndpointAddress;
2568 if (!dev->read_endpoint) {
2569 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2570 goto errorEP;
2572 init_timer(&dev->timer);
2573 dev->timer.function = s2255_timer;
2574 dev->timer.data = (unsigned long)dev->fw_data;
2575 init_waitqueue_head(&dev->fw_data->wait_fw);
2576 for (i = 0; i < MAX_CHANNELS; i++) {
2577 struct s2255_channel *channel = &dev->channel[i];
2578 dev->channel[i].idx = i;
2579 init_waitqueue_head(&channel->wait_setmode);
2580 init_waitqueue_head(&channel->wait_vidstatus);
2583 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2584 if (!dev->fw_data->fw_urb) {
2585 dev_err(&interface->dev, "out of memory!\n");
2586 goto errorFWURB;
2589 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2590 if (!dev->fw_data->pfw_data) {
2591 dev_err(&interface->dev, "out of memory!\n");
2592 goto errorFWDATA2;
2594 /* load the first chunk */
2595 if (request_firmware(&dev->fw_data->fw,
2596 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2597 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2598 goto errorREQFW;
2600 /* check the firmware is valid */
2601 fw_size = dev->fw_data->fw->size;
2602 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2604 if (*pdata != S2255_FW_MARKER) {
2605 printk(KERN_INFO "Firmware invalid.\n");
2606 retval = -ENODEV;
2607 goto errorFWMARKER;
2608 } else {
2609 /* make sure firmware is the latest */
2610 __le32 *pRel;
2611 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2612 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2613 dev->dsp_fw_ver = *pRel;
2614 if (*pRel < S2255_CUR_DSP_FWVER)
2615 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
2616 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
2617 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2618 " or above.\n", S2255_MIN_DSP_COLORFILTER);
2620 usb_reset_device(dev->udev);
2621 /* load 2255 board specific */
2622 retval = s2255_board_init(dev);
2623 if (retval)
2624 goto errorBOARDINIT;
2625 spin_lock_init(&dev->slock);
2626 s2255_fwload_start(dev, 0);
2627 /* loads v4l specific */
2628 retval = s2255_probe_v4l(dev);
2629 if (retval)
2630 goto errorBOARDINIT;
2631 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2632 return 0;
2633 errorBOARDINIT:
2634 s2255_board_shutdown(dev);
2635 errorFWMARKER:
2636 release_firmware(dev->fw_data->fw);
2637 errorREQFW:
2638 kfree(dev->fw_data->pfw_data);
2639 errorFWDATA2:
2640 usb_free_urb(dev->fw_data->fw_urb);
2641 errorFWURB:
2642 del_timer(&dev->timer);
2643 errorEP:
2644 usb_put_dev(dev->udev);
2645 errorUDEV:
2646 kfree(dev->fw_data);
2647 mutex_destroy(&dev->open_lock);
2648 mutex_destroy(&dev->lock);
2649 errorFWDATA1:
2650 kfree(dev);
2651 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
2652 return retval;
2655 /* disconnect routine. when board is removed physically or with rmmod */
2656 static void s2255_disconnect(struct usb_interface *interface)
2658 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
2659 int i;
2660 int channels = atomic_read(&dev->num_channels);
2661 mutex_lock(&dev->lock);
2662 v4l2_device_disconnect(&dev->v4l2_dev);
2663 mutex_unlock(&dev->lock);
2664 /*see comments in the uvc_driver.c usb disconnect function */
2665 atomic_inc(&dev->num_channels);
2666 /* unregister each video device. */
2667 for (i = 0; i < channels; i++)
2668 video_unregister_device(&dev->channel[i].vdev);
2669 /* wake up any of our timers */
2670 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2671 wake_up(&dev->fw_data->wait_fw);
2672 for (i = 0; i < MAX_CHANNELS; i++) {
2673 dev->channel[i].setmode_ready = 1;
2674 wake_up(&dev->channel[i].wait_setmode);
2675 dev->channel[i].vidstatus_ready = 1;
2676 wake_up(&dev->channel[i].wait_vidstatus);
2678 if (atomic_dec_and_test(&dev->num_channels))
2679 s2255_destroy(dev);
2680 dev_info(&interface->dev, "%s\n", __func__);
2683 static struct usb_driver s2255_driver = {
2684 .name = S2255_DRIVER_NAME,
2685 .probe = s2255_probe,
2686 .disconnect = s2255_disconnect,
2687 .id_table = s2255_table,
2690 static int __init usb_s2255_init(void)
2692 int result;
2693 /* register this driver with the USB subsystem */
2694 result = usb_register(&s2255_driver);
2695 if (result)
2696 pr_err(KBUILD_MODNAME
2697 ": usb_register failed. Error number %d\n", result);
2698 dprintk(2, "%s\n", __func__);
2699 return result;
2702 static void __exit usb_s2255_exit(void)
2704 usb_deregister(&s2255_driver);
2707 module_init(usb_s2255_init);
2708 module_exit(usb_s2255_exit);
2710 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2711 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2712 MODULE_LICENSE("GPL");