2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2010 by Sensoray Company Inc.
7 * Some video buffer code based on vivi driver:
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
16 * Example maximum bandwidth utilization:
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
20 * -full or half size Grey scale: all 4 channels at once
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/module.h>
45 #include <linux/firmware.h>
46 #include <linux/kernel.h>
47 #include <linux/mutex.h>
48 #include <linux/slab.h>
49 #include <linux/videodev2.h>
50 #include <linux/version.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, \
65 #define FIRMWARE_FILE_NAME "f2255usb.bin"
67 /* default JPEG quality */
68 #define S2255_DEF_JPEG_QUAL 50
69 /* vendor request in */
71 /* vendor request out */
72 #define S2255_VR_OUT 1
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
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
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
142 #define DEF_CONTRAST 0x5c
143 #define DEF_SATURATION 0x80
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))
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
{
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
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
;
221 void *dev
; /* back pointer to s2255_dev struct*/
226 struct s2255_fmt
; /*forward declaration */
229 struct s2255_channel
{
230 struct video_device vdev
;
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
;
243 /* allocated image size */
244 unsigned long req_image_size
;
245 /* received packet size */
246 unsigned long pkt_size
;
248 unsigned long frame_count
;
251 /* if channel configured to default state */
253 wait_queue_head_t wait_setmode
;
255 /* video status items */
257 wait_queue_head_t wait_vidstatus
;
261 const struct s2255_fmt
*fmt
;
262 int idx
; /* channel number on device, 0-3 */
267 struct s2255_channel channel
[MAX_CHANNELS
];
268 struct v4l2_device v4l2_dev
;
269 atomic_t num_channels
;
271 struct mutex lock
; /* channels[].vdev.lock */
272 struct mutex open_lock
;
273 struct usb_device
*udev
;
274 struct usb_interface
*interface
;
276 struct timer_list timer
;
277 struct s2255_fw
*fw_data
;
278 struct s2255_pipeinfo pipe
;
279 u32 cc
; /* current channel */
283 /* dsp firmware version (f2255usb.bin) */
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
);
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
;
307 struct s2255_dev
*dev
;
308 struct videobuf_queue vb_vidq
;
309 enum v4l2_buf_type type
;
310 struct s2255_channel
*channel
;
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 * = ===============================
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, =
338 * =========================================================
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
346 * COLORFILTER is different from "color killer" and "color effects"
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};
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
,
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...) \
383 if (*s2255_debug >= (level)) { \
384 printk(KERN_DEBUG S2255_DRIVER_NAME \
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 /* Enable jpeg capture. */
398 static int jpeg_enable
= 1;
400 module_param(debug
, int, 0644);
401 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
402 module_param(vid_limit
, int, 0644);
403 MODULE_PARM_DESC(vid_limit
, "video memory limit(Mb)");
404 module_param(video_nr
, int, 0644);
405 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
406 module_param(jpeg_enable
, int, 0644);
407 MODULE_PARM_DESC(jpeg_enable
, "Jpeg enable(1-on 0-off) default 1");
409 /* USB device table */
410 #define USB_SENSORAY_VID 0x1943
411 static struct usb_device_id s2255_table
[] = {
412 {USB_DEVICE(USB_SENSORAY_VID
, 0x2255)},
413 {USB_DEVICE(USB_SENSORAY_VID
, 0x2257)}, /*same family as 2255*/
414 { } /* Terminating entry */
416 MODULE_DEVICE_TABLE(usb
, s2255_table
);
418 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
421 /* JPEG formats must be defined last to support jpeg_enable parameter */
422 static const struct s2255_fmt formats
[] = {
424 .name
= "4:2:2, planar, YUV422P",
425 .fourcc
= V4L2_PIX_FMT_YUV422P
,
429 .name
= "4:2:2, packed, YUYV",
430 .fourcc
= V4L2_PIX_FMT_YUYV
,
434 .name
= "4:2:2, packed, UYVY",
435 .fourcc
= V4L2_PIX_FMT_UYVY
,
439 .fourcc
= V4L2_PIX_FMT_GREY
,
443 .fourcc
= V4L2_PIX_FMT_JPEG
,
447 .fourcc
= V4L2_PIX_FMT_MJPEG
,
452 static int norm_maxw(struct video_device
*vdev
)
454 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
455 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
458 static int norm_maxh(struct video_device
*vdev
)
460 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
461 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
464 static int norm_minw(struct video_device
*vdev
)
466 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
467 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
470 static int norm_minh(struct video_device
*vdev
)
472 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
473 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
478 * TODO: fixme: move YUV reordering to hardware
479 * converts 2255 planar format to yuyv or uyvy
481 static void planar422p_to_yuv_packed(const unsigned char *in
,
483 int width
, int height
,
489 unsigned long size
= height
* width
;
491 pY
= (unsigned char *)in
;
492 pCr
= (unsigned char *)in
+ height
* width
;
493 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
494 for (i
= 0; i
< size
* 2; i
+= 4) {
495 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
496 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
497 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
498 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
503 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
505 s2255_vendor_req(dev
, 0x40, 0x0b0b, 0x0b01, NULL
, 0, 1);
507 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
509 s2255_vendor_req(dev
, 0x10, 0x0000, 0x0000, NULL
, 0, 1);
513 /* kickstarts the firmware loading. from probe
515 static void s2255_timer(unsigned long user_data
)
517 struct s2255_fw
*data
= (struct s2255_fw
*)user_data
;
518 dprintk(100, "%s\n", __func__
);
519 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
520 printk(KERN_ERR
"s2255: can't submit urb\n");
521 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
522 /* wake up anything waiting for the firmware */
523 wake_up(&data
->wait_fw
);
529 /* this loads the firmware asynchronously.
530 Originally this was done synchroously in probe.
531 But it is better to load it asynchronously here than block
532 inside the probe function. Blocking inside probe affects boot time.
533 FW loading is triggered by the timer in the probe function
535 static void s2255_fwchunk_complete(struct urb
*urb
)
537 struct s2255_fw
*data
= urb
->context
;
538 struct usb_device
*udev
= urb
->dev
;
540 dprintk(100, "%s: udev %p urb %p", __func__
, udev
, urb
);
542 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
543 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
544 /* wake up anything waiting for the firmware */
545 wake_up(&data
->wait_fw
);
548 if (data
->fw_urb
== NULL
) {
549 s2255_dev_err(&udev
->dev
, "disconnected\n");
550 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
551 /* wake up anything waiting for the firmware */
552 wake_up(&data
->wait_fw
);
555 #define CHUNK_SIZE 512
556 /* all USB transfers must be done with continuous kernel memory.
557 can't allocate more than 128k in current linux kernel, so
558 upload the firmware in chunks
560 if (data
->fw_loaded
< data
->fw_size
) {
561 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
562 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
564 if (len
< CHUNK_SIZE
)
565 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
567 dprintk(100, "completed len %d, loaded %d \n", len
,
570 memcpy(data
->pfw_data
,
571 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
573 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
574 data
->pfw_data
, CHUNK_SIZE
,
575 s2255_fwchunk_complete
, data
);
576 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
577 dev_err(&udev
->dev
, "failed submit URB\n");
578 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
579 /* wake up anything waiting for the firmware */
580 wake_up(&data
->wait_fw
);
583 data
->fw_loaded
+= len
;
585 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
586 dprintk(100, "%s: firmware upload complete\n", __func__
);
592 static int s2255_got_frame(struct s2255_channel
*channel
, int jpgsize
)
594 struct s2255_dmaqueue
*dma_q
= &channel
->vidq
;
595 struct s2255_buffer
*buf
;
596 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
597 unsigned long flags
= 0;
599 spin_lock_irqsave(&dev
->slock
, flags
);
600 if (list_empty(&dma_q
->active
)) {
601 dprintk(1, "No active queue to serve\n");
605 buf
= list_entry(dma_q
->active
.next
,
606 struct s2255_buffer
, vb
.queue
);
607 list_del(&buf
->vb
.queue
);
608 do_gettimeofday(&buf
->vb
.ts
);
609 s2255_fillbuff(channel
, buf
, jpgsize
);
610 wake_up(&buf
->vb
.done
);
611 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__
, buf
, buf
->vb
.i
);
613 spin_unlock_irqrestore(&dev
->slock
, flags
);
617 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
620 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
621 if (-1 == formats
[i
].fourcc
)
623 if (!jpeg_enable
&& ((formats
[i
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
624 (formats
[i
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
626 if (formats
[i
].fourcc
== fourcc
)
632 /* video buffer vmalloc implementation based partly on VIVI driver which is
633 * Copyright (c) 2006 by
634 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
635 * Ted Walther <ted--a.t--enumera.com>
636 * John Sokol <sokol--a.t--videotechnology.com>
637 * http://v4l.videotechnology.com/
640 static void s2255_fillbuff(struct s2255_channel
*channel
,
641 struct s2255_buffer
*buf
, int jpgsize
)
646 char *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
647 unsigned long last_frame
;
648 struct s2255_framei
*frm
;
652 last_frame
= channel
->last_frame
;
653 if (last_frame
!= -1) {
654 frm
= &channel
->buffer
.frame
[last_frame
];
656 (const char *)channel
->buffer
.frame
[last_frame
].lpvbits
;
657 switch (buf
->fmt
->fourcc
) {
658 case V4L2_PIX_FMT_YUYV
:
659 case V4L2_PIX_FMT_UYVY
:
660 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
665 case V4L2_PIX_FMT_GREY
:
666 memcpy(vbuf
, tmpbuf
, buf
->vb
.width
* buf
->vb
.height
);
668 case V4L2_PIX_FMT_JPEG
:
669 case V4L2_PIX_FMT_MJPEG
:
670 buf
->vb
.size
= jpgsize
;
671 memcpy(vbuf
, tmpbuf
, buf
->vb
.size
);
673 case V4L2_PIX_FMT_YUV422P
:
675 buf
->vb
.width
* buf
->vb
.height
* 2);
678 printk(KERN_DEBUG
"s2255: unknown format?\n");
680 channel
->last_frame
= -1;
682 printk(KERN_ERR
"s2255: =======no frame\n");
686 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
687 (unsigned long)vbuf
, pos
);
688 /* tell v4l buffer was filled */
690 buf
->vb
.field_count
= channel
->frame_count
* 2;
691 do_gettimeofday(&ts
);
693 buf
->vb
.state
= VIDEOBUF_DONE
;
697 /* ------------------------------------------------------------------
699 ------------------------------------------------------------------*/
701 static int buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
704 struct s2255_fh
*fh
= vq
->priv_data
;
705 struct s2255_channel
*channel
= fh
->channel
;
706 *size
= channel
->width
* channel
->height
* (channel
->fmt
->depth
>> 3);
709 *count
= S2255_DEF_BUFS
;
711 if (*size
* *count
> vid_limit
* 1024 * 1024)
712 *count
= (vid_limit
* 1024 * 1024) / *size
;
717 static void free_buffer(struct videobuf_queue
*vq
, struct s2255_buffer
*buf
)
719 dprintk(4, "%s\n", __func__
);
721 videobuf_vmalloc_free(&buf
->vb
);
722 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
725 static int buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
726 enum v4l2_field field
)
728 struct s2255_fh
*fh
= vq
->priv_data
;
729 struct s2255_channel
*channel
= fh
->channel
;
730 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
732 int w
= channel
->width
;
733 int h
= channel
->height
;
734 dprintk(4, "%s, field=%d\n", __func__
, field
);
735 if (channel
->fmt
== NULL
)
738 if ((w
< norm_minw(&channel
->vdev
)) ||
739 (w
> norm_maxw(&channel
->vdev
)) ||
740 (h
< norm_minh(&channel
->vdev
)) ||
741 (h
> norm_maxh(&channel
->vdev
))) {
742 dprintk(4, "invalid buffer prepare\n");
745 buf
->vb
.size
= w
* h
* (channel
->fmt
->depth
>> 3);
746 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
) {
747 dprintk(4, "invalid buffer prepare\n");
751 buf
->fmt
= channel
->fmt
;
754 buf
->vb
.field
= field
;
756 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
757 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
762 buf
->vb
.state
= VIDEOBUF_PREPARED
;
765 free_buffer(vq
, buf
);
769 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
771 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
772 struct s2255_fh
*fh
= vq
->priv_data
;
773 struct s2255_channel
*channel
= fh
->channel
;
774 struct s2255_dmaqueue
*vidq
= &channel
->vidq
;
775 dprintk(1, "%s\n", __func__
);
776 buf
->vb
.state
= VIDEOBUF_QUEUED
;
777 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
780 static void buffer_release(struct videobuf_queue
*vq
,
781 struct videobuf_buffer
*vb
)
783 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
784 struct s2255_fh
*fh
= vq
->priv_data
;
785 dprintk(4, "%s %d\n", __func__
, fh
->channel
->idx
);
786 free_buffer(vq
, buf
);
789 static struct videobuf_queue_ops s2255_video_qops
= {
790 .buf_setup
= buffer_setup
,
791 .buf_prepare
= buffer_prepare
,
792 .buf_queue
= buffer_queue
,
793 .buf_release
= buffer_release
,
797 static int res_get(struct s2255_fh
*fh
)
799 struct s2255_channel
*channel
= fh
->channel
;
801 if (channel
->resources
)
802 return 0; /* no, someone else uses it */
803 /* it's free, grab it */
804 channel
->resources
= 1;
806 dprintk(1, "s2255: res: get\n");
810 static int res_locked(struct s2255_fh
*fh
)
812 return fh
->channel
->resources
;
815 static int res_check(struct s2255_fh
*fh
)
817 return fh
->resources
;
821 static void res_free(struct s2255_fh
*fh
)
823 struct s2255_channel
*channel
= fh
->channel
;
824 channel
->resources
= 0;
826 dprintk(1, "res: put\n");
829 static int vidioc_querymenu(struct file
*file
, void *priv
,
830 struct v4l2_querymenu
*qmenu
)
832 static const char *colorfilter
[] = {
837 if (qmenu
->id
== V4L2_CID_PRIVATE_COLORFILTER
) {
839 const char **menu_items
= colorfilter
;
840 for (i
= 0; i
< qmenu
->index
&& menu_items
[i
]; i
++)
841 ; /* do nothing (from v4l2-common.c) */
842 if (menu_items
[i
] == NULL
|| menu_items
[i
][0] == '\0')
844 strlcpy(qmenu
->name
, menu_items
[qmenu
->index
],
845 sizeof(qmenu
->name
));
848 return v4l2_ctrl_query_menu(qmenu
, NULL
, NULL
);
851 static int vidioc_querycap(struct file
*file
, void *priv
,
852 struct v4l2_capability
*cap
)
854 struct s2255_fh
*fh
= file
->private_data
;
855 struct s2255_dev
*dev
= fh
->dev
;
856 strlcpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
857 strlcpy(cap
->card
, "s2255", sizeof(cap
->card
));
858 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
859 cap
->version
= S2255_VERSION
;
860 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
864 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
865 struct v4l2_fmtdesc
*f
)
871 if (index
>= ARRAY_SIZE(formats
))
873 if (!jpeg_enable
&& ((formats
[index
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
874 (formats
[index
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
876 dprintk(4, "name %s\n", formats
[index
].name
);
877 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
878 f
->pixelformat
= formats
[index
].fourcc
;
882 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
883 struct v4l2_format
*f
)
885 struct s2255_fh
*fh
= priv
;
886 struct s2255_channel
*channel
= fh
->channel
;
888 f
->fmt
.pix
.width
= channel
->width
;
889 f
->fmt
.pix
.height
= channel
->height
;
890 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
891 f
->fmt
.pix
.pixelformat
= channel
->fmt
->fourcc
;
892 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (channel
->fmt
->depth
>> 3);
893 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
897 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
898 struct v4l2_format
*f
)
900 const struct s2255_fmt
*fmt
;
901 enum v4l2_field field
;
903 struct s2255_fh
*fh
= priv
;
904 struct s2255_channel
*channel
= fh
->channel
;
907 (channel
->vdev
.current_norm
& V4L2_STD_NTSC
) ? 1 : 0;
909 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
914 field
= f
->fmt
.pix
.field
;
915 if (field
== V4L2_FIELD_ANY
)
918 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
919 __func__
, is_ntsc
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
922 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
923 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
925 field
= V4L2_FIELD_SEQ_TB
;
926 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
927 (field
== V4L2_FIELD_SEQ_TB
) ||
928 (field
== V4L2_FIELD_INTERLACED_TB
))) {
929 dprintk(1, "unsupported field setting\n");
933 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
935 field
= V4L2_FIELD_TOP
;
936 } else if (!((field
== V4L2_FIELD_TOP
) ||
937 (field
== V4L2_FIELD_BOTTOM
))) {
938 dprintk(1, "unsupported field setting\n");
943 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
944 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
945 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_NTSC
)
946 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_NTSC
;
947 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_NTSC
)
948 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
950 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
953 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
954 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
956 field
= V4L2_FIELD_SEQ_TB
;
957 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
958 (field
== V4L2_FIELD_SEQ_TB
) ||
959 (field
== V4L2_FIELD_INTERLACED_TB
))) {
960 dprintk(1, "unsupported field setting\n");
964 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
966 field
= V4L2_FIELD_TOP
;
967 } else if (!((field
== V4L2_FIELD_TOP
) ||
968 (field
== V4L2_FIELD_BOTTOM
))) {
969 dprintk(1, "unsupported field setting\n");
973 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
) {
974 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
975 field
= V4L2_FIELD_SEQ_TB
;
976 } else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_PAL
) {
977 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_PAL
;
978 field
= V4L2_FIELD_TOP
;
979 } else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_PAL
) {
980 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
981 field
= V4L2_FIELD_TOP
;
983 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
984 field
= V4L2_FIELD_TOP
;
987 f
->fmt
.pix
.field
= field
;
988 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
989 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
990 dprintk(50, "%s: set width %d height %d field %d\n", __func__
,
991 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
995 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
996 struct v4l2_format
*f
)
998 struct s2255_fh
*fh
= priv
;
999 struct s2255_channel
*channel
= fh
->channel
;
1000 const struct s2255_fmt
*fmt
;
1001 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1002 struct s2255_mode mode
;
1006 ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
1011 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1016 mutex_lock(&q
->vb_lock
);
1018 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1019 dprintk(1, "queue busy\n");
1024 if (res_locked(fh
)) {
1025 dprintk(1, "%s: channel busy\n", __func__
);
1029 mode
= channel
->mode
;
1031 channel
->width
= f
->fmt
.pix
.width
;
1032 channel
->height
= f
->fmt
.pix
.height
;
1033 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1035 norm
= norm_minw(&channel
->vdev
);
1036 if (channel
->width
> norm_minw(&channel
->vdev
)) {
1037 if (channel
->height
> norm_minh(&channel
->vdev
)) {
1038 if (channel
->cap_parm
.capturemode
&
1039 V4L2_MODE_HIGHQUALITY
)
1040 mode
.scale
= SCALE_4CIFSI
;
1042 mode
.scale
= SCALE_4CIFS
;
1044 mode
.scale
= SCALE_2CIFS
;
1047 mode
.scale
= SCALE_1CIFS
;
1050 switch (channel
->fmt
->fourcc
) {
1051 case V4L2_PIX_FMT_GREY
:
1052 mode
.color
&= ~MASK_COLOR
;
1053 mode
.color
|= COLOR_Y8
;
1055 case V4L2_PIX_FMT_JPEG
:
1056 case V4L2_PIX_FMT_MJPEG
:
1057 mode
.color
&= ~MASK_COLOR
;
1058 mode
.color
|= COLOR_JPG
;
1059 mode
.color
|= (channel
->jc
.quality
<< 8);
1061 case V4L2_PIX_FMT_YUV422P
:
1062 mode
.color
&= ~MASK_COLOR
;
1063 mode
.color
|= COLOR_YUVPL
;
1065 case V4L2_PIX_FMT_YUYV
:
1066 case V4L2_PIX_FMT_UYVY
:
1068 mode
.color
&= ~MASK_COLOR
;
1069 mode
.color
|= COLOR_YUVPK
;
1072 if ((mode
.color
& MASK_COLOR
) != (channel
->mode
.color
& MASK_COLOR
))
1074 else if (mode
.scale
!= channel
->mode
.scale
)
1076 else if (mode
.format
!= channel
->mode
.format
)
1078 channel
->mode
= mode
;
1079 (void) s2255_set_mode(channel
, &mode
);
1082 mutex_unlock(&q
->vb_lock
);
1086 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1087 struct v4l2_requestbuffers
*p
)
1090 struct s2255_fh
*fh
= priv
;
1091 rc
= videobuf_reqbufs(&fh
->vb_vidq
, p
);
1095 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1098 struct s2255_fh
*fh
= priv
;
1099 rc
= videobuf_querybuf(&fh
->vb_vidq
, p
);
1103 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1106 struct s2255_fh
*fh
= priv
;
1107 rc
= videobuf_qbuf(&fh
->vb_vidq
, p
);
1111 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1114 struct s2255_fh
*fh
= priv
;
1115 rc
= videobuf_dqbuf(&fh
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
1119 /* write to the configuration pipe, synchronously */
1120 static int s2255_write_config(struct usb_device
*udev
, unsigned char *pbuf
,
1127 pipe
= usb_sndbulkpipe(udev
, S2255_CONFIG_EP
);
1128 retval
= usb_bulk_msg(udev
, pipe
, pbuf
, size
, &done
, 500);
1133 static u32
get_transfer_size(struct s2255_mode
*mode
)
1135 int linesPerFrame
= LINE_SZ_DEF
;
1136 int pixelsPerLine
= NUM_LINES_DEF
;
1139 unsigned int mask_mult
;
1144 if (mode
->format
== FORMAT_NTSC
) {
1145 switch (mode
->scale
) {
1148 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
1149 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
1152 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
1153 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
1156 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
1157 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
1162 } else if (mode
->format
== FORMAT_PAL
) {
1163 switch (mode
->scale
) {
1166 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
1167 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
1170 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
1171 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
1174 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
1175 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
1181 outImageSize
= linesPerFrame
* pixelsPerLine
;
1182 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
1183 /* 2 bytes/pixel if not monochrome */
1187 /* total bytes to send including prefix and 4K padding;
1188 must be a multiple of USB_READ_SIZE */
1189 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
1190 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
1191 /* if size not a multiple of USB_READ_SIZE */
1192 if (usbInSize
& ~mask_mult
)
1193 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
1197 static void s2255_print_cfg(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
1199 struct device
*dev
= &sdev
->udev
->dev
;
1200 dev_info(dev
, "------------------------------------------------\n");
1201 dev_info(dev
, "format: %d\nscale %d\n", mode
->format
, mode
->scale
);
1202 dev_info(dev
, "fdec: %d\ncolor %d\n", mode
->fdec
, mode
->color
);
1203 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
1204 dev_info(dev
, "------------------------------------------------\n");
1208 * set mode is the function which controls the DSP.
1209 * the restart parameter in struct s2255_mode should be set whenever
1210 * the image size could change via color format, video system or image
1212 * When the restart parameter is set, we sleep for ONE frame to allow the
1213 * DSP time to get the new frame
1215 static int s2255_set_mode(struct s2255_channel
*channel
,
1216 struct s2255_mode
*mode
)
1220 unsigned long chn_rev
;
1221 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1222 chn_rev
= G_chnmap
[channel
->idx
];
1223 dprintk(3, "%s channel: %d\n", __func__
, channel
->idx
);
1224 /* if JPEG, set the quality */
1225 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
) {
1226 mode
->color
&= ~MASK_COLOR
;
1227 mode
->color
|= COLOR_JPG
;
1228 mode
->color
&= ~MASK_JPG_QUALITY
;
1229 mode
->color
|= (channel
->jc
.quality
<< 8);
1232 channel
->mode
= *mode
;
1233 channel
->req_image_size
= get_transfer_size(mode
);
1234 dprintk(1, "%s: reqsize %ld\n", __func__
, channel
->req_image_size
);
1235 buffer
= kzalloc(512, GFP_KERNEL
);
1236 if (buffer
== NULL
) {
1237 dev_err(&dev
->udev
->dev
, "out of mem\n");
1241 buffer
[0] = IN_DATA_TOKEN
;
1242 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1243 buffer
[2] = CMD_SET_MODE
;
1244 memcpy(&buffer
[3], &channel
->mode
, sizeof(struct s2255_mode
));
1245 channel
->setmode_ready
= 0;
1246 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1248 s2255_print_cfg(dev
, mode
);
1250 /* wait at least 3 frames before continuing */
1251 if (mode
->restart
) {
1252 wait_event_timeout(channel
->wait_setmode
,
1253 (channel
->setmode_ready
!= 0),
1254 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1255 if (channel
->setmode_ready
!= 1) {
1256 printk(KERN_DEBUG
"s2255: no set mode response\n");
1260 /* clear the restart flag */
1261 channel
->mode
.restart
= 0;
1262 dprintk(1, "%s chn %d, result: %d\n", __func__
, channel
->idx
, res
);
1266 static int s2255_cmd_status(struct s2255_channel
*channel
, u32
*pstatus
)
1271 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1272 chn_rev
= G_chnmap
[channel
->idx
];
1273 dprintk(4, "%s chan %d\n", __func__
, channel
->idx
);
1274 buffer
= kzalloc(512, GFP_KERNEL
);
1275 if (buffer
== NULL
) {
1276 dev_err(&dev
->udev
->dev
, "out of mem\n");
1279 /* form the get vid status command */
1280 buffer
[0] = IN_DATA_TOKEN
;
1281 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1282 buffer
[2] = CMD_STATUS
;
1284 channel
->vidstatus_ready
= 0;
1285 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1287 wait_event_timeout(channel
->wait_vidstatus
,
1288 (channel
->vidstatus_ready
!= 0),
1289 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT
));
1290 if (channel
->vidstatus_ready
!= 1) {
1291 printk(KERN_DEBUG
"s2255: no vidstatus response\n");
1294 *pstatus
= channel
->vidstatus
;
1295 dprintk(4, "%s, vid status %d\n", __func__
, *pstatus
);
1299 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1302 struct s2255_fh
*fh
= priv
;
1303 struct s2255_dev
*dev
= fh
->dev
;
1304 struct s2255_channel
*channel
= fh
->channel
;
1306 dprintk(4, "%s\n", __func__
);
1307 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1308 dev_err(&dev
->udev
->dev
, "invalid fh type0\n");
1311 if (i
!= fh
->type
) {
1312 dev_err(&dev
->udev
->dev
, "invalid fh type1\n");
1317 s2255_dev_err(&dev
->udev
->dev
, "stream busy\n");
1320 channel
->last_frame
= -1;
1321 channel
->bad_payload
= 0;
1322 channel
->cur_frame
= 0;
1323 channel
->frame_count
= 0;
1324 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1325 channel
->buffer
.frame
[j
].ulState
= S2255_READ_IDLE
;
1326 channel
->buffer
.frame
[j
].cur_size
= 0;
1328 res
= videobuf_streamon(&fh
->vb_vidq
);
1330 s2255_start_acquire(channel
);
1331 channel
->b_acquire
= 1;
1338 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1340 struct s2255_fh
*fh
= priv
;
1341 dprintk(4, "%s\n, channel: %d", __func__
, fh
->channel
->idx
);
1342 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1343 printk(KERN_ERR
"invalid fh type0\n");
1346 if (i
!= fh
->type
) {
1347 printk(KERN_ERR
"invalid type i\n");
1350 s2255_stop_acquire(fh
->channel
);
1351 videobuf_streamoff(&fh
->vb_vidq
);
1356 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1358 struct s2255_fh
*fh
= priv
;
1359 struct s2255_mode mode
;
1360 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1362 mutex_lock(&q
->vb_lock
);
1363 if (videobuf_queue_is_busy(q
)) {
1364 dprintk(1, "queue busy\n");
1368 if (res_locked(fh
)) {
1369 dprintk(1, "can't change standard after started\n");
1373 mode
= fh
->channel
->mode
;
1374 if (*i
& V4L2_STD_NTSC
) {
1375 dprintk(4, "%s NTSC\n", __func__
);
1376 /* if changing format, reset frame decimation/intervals */
1377 if (mode
.format
!= FORMAT_NTSC
) {
1379 mode
.format
= FORMAT_NTSC
;
1382 } else if (*i
& V4L2_STD_PAL
) {
1383 dprintk(4, "%s PAL\n", __func__
);
1384 if (mode
.format
!= FORMAT_PAL
) {
1386 mode
.format
= FORMAT_PAL
;
1393 s2255_set_mode(fh
->channel
, &mode
);
1395 mutex_unlock(&q
->vb_lock
);
1399 /* Sensoray 2255 is a multiple channel capture device.
1400 It does not have a "crossbar" of inputs.
1401 We use one V4L device per channel. The user must
1402 be aware that certain combinations are not allowed.
1403 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1404 at once in color(you can do full fps on 4 channels with greyscale.
1406 static int vidioc_enum_input(struct file
*file
, void *priv
,
1407 struct v4l2_input
*inp
)
1409 struct s2255_fh
*fh
= priv
;
1410 struct s2255_dev
*dev
= fh
->dev
;
1411 struct s2255_channel
*channel
= fh
->channel
;
1413 if (inp
->index
!= 0)
1415 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1416 inp
->std
= S2255_NORMS
;
1418 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_STATUS
) {
1420 rc
= s2255_cmd_status(fh
->channel
, &status
);
1421 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc
, status
);
1423 inp
->status
= (status
& 0x01) ? 0
1424 : V4L2_IN_ST_NO_SIGNAL
;
1429 strlcpy(inp
->name
, "Composite", sizeof(inp
->name
));
1432 strlcpy(inp
->name
, (channel
->idx
< 2) ? "Composite" : "S-Video",
1439 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1444 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1451 /* --- controls ---------------------------------------------- */
1452 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1453 struct v4l2_queryctrl
*qc
)
1455 struct s2255_fh
*fh
= priv
;
1456 struct s2255_channel
*channel
= fh
->channel
;
1457 struct s2255_dev
*dev
= fh
->dev
;
1459 case V4L2_CID_BRIGHTNESS
:
1460 v4l2_ctrl_query_fill(qc
, -127, 127, 1, DEF_BRIGHT
);
1462 case V4L2_CID_CONTRAST
:
1463 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_CONTRAST
);
1465 case V4L2_CID_SATURATION
:
1466 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_SATURATION
);
1469 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_HUE
);
1471 case V4L2_CID_PRIVATE_COLORFILTER
:
1472 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1474 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1476 strlcpy(qc
->name
, "Color Filter", sizeof(qc
->name
));
1477 qc
->type
= V4L2_CTRL_TYPE_MENU
;
1481 qc
->default_value
= 1;
1487 dprintk(4, "%s, id %d\n", __func__
, qc
->id
);
1491 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1492 struct v4l2_control
*ctrl
)
1494 struct s2255_fh
*fh
= priv
;
1495 struct s2255_dev
*dev
= fh
->dev
;
1496 struct s2255_channel
*channel
= fh
->channel
;
1498 case V4L2_CID_BRIGHTNESS
:
1499 ctrl
->value
= channel
->mode
.bright
;
1501 case V4L2_CID_CONTRAST
:
1502 ctrl
->value
= channel
->mode
.contrast
;
1504 case V4L2_CID_SATURATION
:
1505 ctrl
->value
= channel
->mode
.saturation
;
1508 ctrl
->value
= channel
->mode
.hue
;
1510 case V4L2_CID_PRIVATE_COLORFILTER
:
1511 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1513 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1515 ctrl
->value
= !((channel
->mode
.color
& MASK_INPUT_TYPE
) >> 16);
1520 dprintk(4, "%s, id %d val %d\n", __func__
, ctrl
->id
, ctrl
->value
);
1524 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1525 struct v4l2_control
*ctrl
)
1527 struct s2255_fh
*fh
= priv
;
1528 struct s2255_channel
*channel
= fh
->channel
;
1529 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1530 struct s2255_mode mode
;
1531 mode
= channel
->mode
;
1532 dprintk(4, "%s\n", __func__
);
1533 /* update the mode to the corresponding value */
1535 case V4L2_CID_BRIGHTNESS
:
1536 mode
.bright
= ctrl
->value
;
1538 case V4L2_CID_CONTRAST
:
1539 mode
.contrast
= ctrl
->value
;
1542 mode
.hue
= ctrl
->value
;
1544 case V4L2_CID_SATURATION
:
1545 mode
.saturation
= ctrl
->value
;
1547 case V4L2_CID_PRIVATE_COLORFILTER
:
1548 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1550 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1552 mode
.color
&= ~MASK_INPUT_TYPE
;
1553 mode
.color
|= ((ctrl
->value
? 0 : 1) << 16);
1559 /* set mode here. Note: stream does not need restarted.
1560 some V4L programs restart stream unnecessarily
1563 s2255_set_mode(fh
->channel
, &mode
);
1567 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1568 struct v4l2_jpegcompression
*jc
)
1570 struct s2255_fh
*fh
= priv
;
1571 struct s2255_channel
*channel
= fh
->channel
;
1573 dprintk(2, "%s: quality %d\n", __func__
, jc
->quality
);
1577 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1578 struct v4l2_jpegcompression
*jc
)
1580 struct s2255_fh
*fh
= priv
;
1581 struct s2255_channel
*channel
= fh
->channel
;
1582 if (jc
->quality
< 0 || jc
->quality
> 100)
1584 channel
->jc
.quality
= jc
->quality
;
1585 dprintk(2, "%s: quality %d\n", __func__
, jc
->quality
);
1589 static int vidioc_g_parm(struct file
*file
, void *priv
,
1590 struct v4l2_streamparm
*sp
)
1592 struct s2255_fh
*fh
= priv
;
1593 __u32 def_num
, def_dem
;
1594 struct s2255_channel
*channel
= fh
->channel
;
1595 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1597 memset(sp
, 0, sizeof(struct v4l2_streamparm
));
1598 sp
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1599 sp
->parm
.capture
.capturemode
= channel
->cap_parm
.capturemode
;
1600 def_num
= (channel
->mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1601 def_dem
= (channel
->mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1602 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1603 switch (channel
->mode
.fdec
) {
1606 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1609 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1612 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1615 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1618 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__
,
1619 sp
->parm
.capture
.capturemode
,
1620 sp
->parm
.capture
.timeperframe
.numerator
,
1621 sp
->parm
.capture
.timeperframe
.denominator
);
1625 static int vidioc_s_parm(struct file
*file
, void *priv
,
1626 struct v4l2_streamparm
*sp
)
1628 struct s2255_fh
*fh
= priv
;
1629 struct s2255_channel
*channel
= fh
->channel
;
1630 struct s2255_mode mode
;
1632 __u32 def_num
, def_dem
;
1633 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1635 mode
= channel
->mode
;
1636 /* high quality capture mode requires a stream restart */
1637 if (channel
->cap_parm
.capturemode
1638 != sp
->parm
.capture
.capturemode
&& res_locked(fh
))
1640 def_num
= (mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1641 def_dem
= (mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1642 if (def_dem
!= sp
->parm
.capture
.timeperframe
.denominator
)
1643 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1644 else if (sp
->parm
.capture
.timeperframe
.numerator
<= def_num
)
1645 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1646 else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 2)) {
1647 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1649 } else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 3)) {
1650 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1653 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1657 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1658 s2255_set_mode(channel
, &mode
);
1659 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1661 sp
->parm
.capture
.capturemode
,
1662 sp
->parm
.capture
.timeperframe
.numerator
,
1663 sp
->parm
.capture
.timeperframe
.denominator
, fdec
);
1667 static int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1668 struct v4l2_frmivalenum
*fe
)
1671 #define NUM_FRAME_ENUMS 4
1672 int frm_dec
[NUM_FRAME_ENUMS
] = {1, 2, 3, 5};
1673 if (fe
->index
< 0 || fe
->index
>= NUM_FRAME_ENUMS
)
1675 switch (fe
->width
) {
1677 if (fe
->height
!= 240 && fe
->height
!= 480)
1682 if (fe
->height
!= 240)
1687 if (fe
->height
!= 288 && fe
->height
!= 576)
1691 if (fe
->height
!= 288)
1697 fe
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1698 fe
->discrete
.denominator
= is_ntsc
? 30000 : 25000;
1699 fe
->discrete
.numerator
= (is_ntsc
? 1001 : 1000) * frm_dec
[fe
->index
];
1700 dprintk(4, "%s discrete %d/%d\n", __func__
, fe
->discrete
.numerator
,
1701 fe
->discrete
.denominator
);
1705 static int s2255_open(struct file
*file
)
1707 struct video_device
*vdev
= video_devdata(file
);
1708 struct s2255_channel
*channel
= video_drvdata(file
);
1709 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1710 struct s2255_fh
*fh
;
1711 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1713 dprintk(1, "s2255: open called (dev=%s)\n",
1714 video_device_node_name(vdev
));
1716 * open lock necessary to prevent multiple instances
1717 * of v4l-conf (or other programs) from simultaneously
1718 * reloading firmware.
1720 mutex_lock(&dev
->open_lock
);
1721 state
= atomic_read(&dev
->fw_data
->fw_state
);
1723 case S2255_FW_DISCONNECTING
:
1724 mutex_unlock(&dev
->open_lock
);
1726 case S2255_FW_FAILED
:
1727 s2255_dev_err(&dev
->udev
->dev
,
1728 "firmware load failed. retrying.\n");
1729 s2255_fwload_start(dev
, 1);
1730 wait_event_timeout(dev
->fw_data
->wait_fw
,
1731 ((atomic_read(&dev
->fw_data
->fw_state
)
1732 == S2255_FW_SUCCESS
) ||
1733 (atomic_read(&dev
->fw_data
->fw_state
)
1734 == S2255_FW_DISCONNECTING
)),
1735 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1736 /* state may have changed, re-read */
1737 state
= atomic_read(&dev
->fw_data
->fw_state
);
1739 case S2255_FW_NOTLOADED
:
1740 case S2255_FW_LOADED_DSPWAIT
:
1741 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1742 driver loaded and then device immediately opened */
1743 printk(KERN_INFO
"%s waiting for firmware load\n", __func__
);
1744 wait_event_timeout(dev
->fw_data
->wait_fw
,
1745 ((atomic_read(&dev
->fw_data
->fw_state
)
1746 == S2255_FW_SUCCESS
) ||
1747 (atomic_read(&dev
->fw_data
->fw_state
)
1748 == S2255_FW_DISCONNECTING
)),
1749 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1750 /* state may have changed, re-read */
1751 state
= atomic_read(&dev
->fw_data
->fw_state
);
1753 case S2255_FW_SUCCESS
:
1757 /* state may have changed in above switch statement */
1759 case S2255_FW_SUCCESS
:
1761 case S2255_FW_FAILED
:
1762 printk(KERN_INFO
"2255 firmware load failed.\n");
1763 mutex_unlock(&dev
->open_lock
);
1765 case S2255_FW_DISCONNECTING
:
1766 printk(KERN_INFO
"%s: disconnecting\n", __func__
);
1767 mutex_unlock(&dev
->open_lock
);
1769 case S2255_FW_LOADED_DSPWAIT
:
1770 case S2255_FW_NOTLOADED
:
1771 printk(KERN_INFO
"%s: firmware not loaded yet"
1772 "please try again later\n",
1775 * Timeout on firmware load means device unusable.
1776 * Set firmware failure state.
1777 * On next s2255_open the firmware will be reloaded.
1779 atomic_set(&dev
->fw_data
->fw_state
,
1781 mutex_unlock(&dev
->open_lock
);
1784 printk(KERN_INFO
"%s: unknown state\n", __func__
);
1785 mutex_unlock(&dev
->open_lock
);
1788 mutex_unlock(&dev
->open_lock
);
1789 /* allocate + initialize per filehandle data */
1790 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1793 file
->private_data
= fh
;
1795 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1796 fh
->channel
= channel
;
1797 if (!channel
->configured
) {
1798 /* configure channel to default state */
1799 channel
->fmt
= &formats
[0];
1800 s2255_set_mode(channel
, &channel
->mode
);
1801 channel
->configured
= 1;
1803 dprintk(1, "%s: dev=%s type=%s\n", __func__
,
1804 video_device_node_name(vdev
), v4l2_type_names
[type
]);
1805 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__
,
1806 (unsigned long)fh
, (unsigned long)dev
,
1807 (unsigned long)&channel
->vidq
);
1808 dprintk(4, "%s: list_empty active=%d\n", __func__
,
1809 list_empty(&channel
->vidq
.active
));
1810 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &s2255_video_qops
,
1813 V4L2_FIELD_INTERLACED
,
1814 sizeof(struct s2255_buffer
),
1820 static unsigned int s2255_poll(struct file
*file
,
1821 struct poll_table_struct
*wait
)
1823 struct s2255_fh
*fh
= file
->private_data
;
1825 dprintk(100, "%s\n", __func__
);
1826 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1828 rc
= videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1832 static void s2255_destroy(struct s2255_dev
*dev
)
1834 /* board shutdown stops the read pipe if it is running */
1835 s2255_board_shutdown(dev
);
1836 /* make sure firmware still not trying to load */
1837 del_timer(&dev
->timer
); /* only started in .probe and .open */
1838 if (dev
->fw_data
->fw_urb
) {
1839 usb_kill_urb(dev
->fw_data
->fw_urb
);
1840 usb_free_urb(dev
->fw_data
->fw_urb
);
1841 dev
->fw_data
->fw_urb
= NULL
;
1843 if (dev
->fw_data
->fw
)
1844 release_firmware(dev
->fw_data
->fw
);
1845 kfree(dev
->fw_data
->pfw_data
);
1846 kfree(dev
->fw_data
);
1847 /* reset the DSP so firmware can be reloaded next time */
1848 s2255_reset_dsppower(dev
);
1849 mutex_destroy(&dev
->open_lock
);
1850 mutex_destroy(&dev
->lock
);
1851 usb_put_dev(dev
->udev
);
1852 v4l2_device_unregister(&dev
->v4l2_dev
);
1853 dprintk(1, "%s", __func__
);
1857 static int s2255_release(struct file
*file
)
1859 struct s2255_fh
*fh
= file
->private_data
;
1860 struct s2255_dev
*dev
= fh
->dev
;
1861 struct video_device
*vdev
= video_devdata(file
);
1862 struct s2255_channel
*channel
= fh
->channel
;
1865 /* turn off stream */
1866 if (res_check(fh
)) {
1867 if (channel
->b_acquire
)
1868 s2255_stop_acquire(fh
->channel
);
1869 videobuf_streamoff(&fh
->vb_vidq
);
1872 videobuf_mmap_free(&fh
->vb_vidq
);
1873 dprintk(1, "%s (dev=%s)\n", __func__
, video_device_node_name(vdev
));
1878 static int s2255_mmap_v4l(struct file
*file
, struct vm_area_struct
*vma
)
1880 struct s2255_fh
*fh
= file
->private_data
;
1885 dprintk(4, "%s, vma=0x%08lx\n", __func__
, (unsigned long)vma
);
1886 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1887 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__
,
1888 (unsigned long)vma
->vm_start
,
1889 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
, ret
);
1893 static const struct v4l2_file_operations s2255_fops_v4l
= {
1894 .owner
= THIS_MODULE
,
1896 .release
= s2255_release
,
1898 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1899 .mmap
= s2255_mmap_v4l
,
1902 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1903 .vidioc_querymenu
= vidioc_querymenu
,
1904 .vidioc_querycap
= vidioc_querycap
,
1905 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1906 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1907 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1908 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1909 .vidioc_reqbufs
= vidioc_reqbufs
,
1910 .vidioc_querybuf
= vidioc_querybuf
,
1911 .vidioc_qbuf
= vidioc_qbuf
,
1912 .vidioc_dqbuf
= vidioc_dqbuf
,
1913 .vidioc_s_std
= vidioc_s_std
,
1914 .vidioc_enum_input
= vidioc_enum_input
,
1915 .vidioc_g_input
= vidioc_g_input
,
1916 .vidioc_s_input
= vidioc_s_input
,
1917 .vidioc_queryctrl
= vidioc_queryctrl
,
1918 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1919 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1920 .vidioc_streamon
= vidioc_streamon
,
1921 .vidioc_streamoff
= vidioc_streamoff
,
1922 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1923 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1924 .vidioc_s_parm
= vidioc_s_parm
,
1925 .vidioc_g_parm
= vidioc_g_parm
,
1926 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
1929 static void s2255_video_device_release(struct video_device
*vdev
)
1931 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1932 dprintk(4, "%s, chnls: %d \n", __func__
,
1933 atomic_read(&dev
->num_channels
));
1934 if (atomic_dec_and_test(&dev
->num_channels
))
1939 static struct video_device
template = {
1941 .fops
= &s2255_fops_v4l
,
1942 .ioctl_ops
= &s2255_ioctl_ops
,
1943 .release
= s2255_video_device_release
,
1944 .tvnorms
= S2255_NORMS
,
1945 .current_norm
= V4L2_STD_NTSC_M
,
1948 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1952 int cur_nr
= video_nr
;
1953 struct s2255_channel
*channel
;
1954 ret
= v4l2_device_register(&dev
->interface
->dev
, &dev
->v4l2_dev
);
1957 /* initialize all video 4 linux */
1958 /* register 4 video devices */
1959 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1960 channel
= &dev
->channel
[i
];
1961 INIT_LIST_HEAD(&channel
->vidq
.active
);
1962 channel
->vidq
.dev
= dev
;
1963 /* register 4 video devices */
1964 channel
->vdev
= template;
1965 channel
->vdev
.lock
= &dev
->lock
;
1966 channel
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1967 video_set_drvdata(&channel
->vdev
, channel
);
1969 ret
= video_register_device(&channel
->vdev
,
1973 ret
= video_register_device(&channel
->vdev
,
1978 dev_err(&dev
->udev
->dev
,
1979 "failed to register video device!\n");
1982 atomic_inc(&dev
->num_channels
);
1983 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1984 video_device_node_name(&channel
->vdev
));
1987 printk(KERN_INFO
"Sensoray 2255 V4L driver Revision: %d.%d\n",
1988 S2255_MAJOR_VERSION
,
1989 S2255_MINOR_VERSION
);
1990 /* if no channels registered, return error and probe will fail*/
1991 if (atomic_read(&dev
->num_channels
) == 0) {
1992 v4l2_device_unregister(&dev
->v4l2_dev
);
1995 if (atomic_read(&dev
->num_channels
) != MAX_CHANNELS
)
1996 printk(KERN_WARNING
"s2255: Not all channels available.\n");
2000 /* this function moves the usb stream read pipe data
2001 * into the system buffers.
2002 * returns 0 on success, EAGAIN if more data to process( call this
2005 * Received frame structure:
2006 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
2007 * bytes 4-7: channel: 0-3
2008 * bytes 8-11: payload size: size of the frame
2009 * bytes 12-payloadsize+12: frame data
2011 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
2017 unsigned long copy_size
;
2020 struct s2255_framei
*frm
;
2021 unsigned char *pdata
;
2022 struct s2255_channel
*channel
;
2023 dprintk(100, "buffer to user\n");
2024 channel
= &dev
->channel
[dev
->cc
];
2025 idx
= channel
->cur_frame
;
2026 frm
= &channel
->buffer
.frame
[idx
];
2027 if (frm
->ulState
== S2255_READ_IDLE
) {
2030 __le32
*pdword
; /*data from dsp is little endian */
2032 /* search for marker codes */
2033 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
2034 pdword
= (__le32
*)pdata
;
2035 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
2037 case S2255_MARKER_FRAME
:
2038 dprintk(4, "found frame marker at offset:"
2039 " %d [%x %x]\n", jj
, pdata
[0],
2041 offset
= jj
+ PREFIX_SIZE
;
2044 if (cc
>= MAX_CHANNELS
) {
2050 dev
->cc
= G_chnmap
[cc
];
2051 channel
= &dev
->channel
[dev
->cc
];
2052 payload
= pdword
[3];
2053 if (payload
> channel
->req_image_size
) {
2054 channel
->bad_payload
++;
2055 /* discard the bad frame */
2058 channel
->pkt_size
= payload
;
2059 channel
->jpg_size
= pdword
[4];
2061 case S2255_MARKER_RESPONSE
:
2063 pdata
+= DEF_USB_BLOCK
;
2064 jj
+= DEF_USB_BLOCK
;
2065 if (pdword
[1] >= MAX_CHANNELS
)
2067 cc
= G_chnmap
[pdword
[1]];
2068 if (cc
>= MAX_CHANNELS
)
2070 channel
= &dev
->channel
[cc
];
2071 switch (pdword
[2]) {
2072 case S2255_RESPONSE_SETMODE
:
2073 /* check if channel valid */
2074 /* set mode ready */
2075 channel
->setmode_ready
= 1;
2076 wake_up(&channel
->wait_setmode
);
2077 dprintk(5, "setmode ready %d\n", cc
);
2079 case S2255_RESPONSE_FW
:
2080 dev
->chn_ready
|= (1 << cc
);
2081 if ((dev
->chn_ready
& 0x0f) != 0x0f)
2083 /* all channels ready */
2084 printk(KERN_INFO
"s2255: fw loaded\n");
2085 atomic_set(&dev
->fw_data
->fw_state
,
2087 wake_up(&dev
->fw_data
->wait_fw
);
2089 case S2255_RESPONSE_STATUS
:
2090 channel
->vidstatus
= pdword
[3];
2091 channel
->vidstatus_ready
= 1;
2092 wake_up(&channel
->wait_vidstatus
);
2093 dprintk(5, "got vidstatus %x chan %d\n",
2097 printk(KERN_INFO
"s2255 unknown resp\n");
2109 channel
= &dev
->channel
[dev
->cc
];
2110 idx
= channel
->cur_frame
;
2111 frm
= &channel
->buffer
.frame
[idx
];
2112 /* search done. now find out if should be acquiring on this channel */
2113 if (!channel
->b_acquire
) {
2114 /* we found a frame, but this channel is turned off */
2115 frm
->ulState
= S2255_READ_IDLE
;
2119 if (frm
->ulState
== S2255_READ_IDLE
) {
2120 frm
->ulState
= S2255_READ_FRAME
;
2124 /* skip the marker 512 bytes (and offset if out of sync) */
2125 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
2128 if (frm
->lpvbits
== NULL
) {
2129 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2130 frm
, dev
, dev
->cc
, idx
);
2134 pdest
= frm
->lpvbits
+ frm
->cur_size
;
2136 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
2138 size
= channel
->pkt_size
- PREFIX_SIZE
;
2140 /* sanity check on pdest */
2141 if ((copy_size
+ frm
->cur_size
) < channel
->req_image_size
)
2142 memcpy(pdest
, psrc
, copy_size
);
2144 frm
->cur_size
+= copy_size
;
2145 dprintk(4, "cur_size size %lu size %lu \n", frm
->cur_size
, size
);
2147 if (frm
->cur_size
>= size
) {
2148 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2150 channel
->last_frame
= channel
->cur_frame
;
2151 channel
->cur_frame
++;
2152 /* end of system frame ring buffer, start at zero */
2153 if ((channel
->cur_frame
== SYS_FRAMES
) ||
2154 (channel
->cur_frame
== channel
->buffer
.dwFrames
))
2155 channel
->cur_frame
= 0;
2157 if (channel
->b_acquire
)
2158 s2255_got_frame(channel
, channel
->jpg_size
);
2159 channel
->frame_count
++;
2160 frm
->ulState
= S2255_READ_IDLE
;
2164 /* done successfully */
2168 static void s2255_read_video_callback(struct s2255_dev
*dev
,
2169 struct s2255_pipeinfo
*pipe_info
)
2172 dprintk(50, "callback read video \n");
2174 if (dev
->cc
>= MAX_CHANNELS
) {
2176 dev_err(&dev
->udev
->dev
, "invalid channel\n");
2179 /* otherwise copy to the system buffers */
2180 res
= save_frame(dev
, pipe_info
);
2182 dprintk(4, "s2255: read callback failed\n");
2184 dprintk(50, "callback read video done\n");
2188 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
2189 u16 Index
, u16 Value
, void *TransferBuffer
,
2190 s32 TransferBufferLength
, int bOut
)
2194 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
2196 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
2198 Value
, Index
, TransferBuffer
,
2199 TransferBufferLength
, HZ
* 5);
2201 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
2202 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2203 Value
, Index
, TransferBuffer
,
2204 TransferBufferLength
, HZ
* 5);
2210 * retrieve FX2 firmware version. future use.
2211 * @param dev pointer to device extension
2212 * @return -1 for fail, else returns firmware version as an int(16 bits)
2214 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
2218 unsigned char transBuffer
[64];
2219 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
2222 dprintk(2, "get fw error: %x\n", ret
);
2223 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
2224 dprintk(2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
2229 * Create the system ring buffer to copy frames into from the
2232 static int s2255_create_sys_buffers(struct s2255_channel
*channel
)
2235 unsigned long reqsize
;
2236 dprintk(1, "create sys buffers\n");
2237 channel
->buffer
.dwFrames
= SYS_FRAMES
;
2238 /* always allocate maximum size(PAL) for system buffers */
2239 reqsize
= SYS_FRAMES_MAXSIZE
;
2241 if (reqsize
> SYS_FRAMES_MAXSIZE
)
2242 reqsize
= SYS_FRAMES_MAXSIZE
;
2244 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2245 /* allocate the frames */
2246 channel
->buffer
.frame
[i
].lpvbits
= vmalloc(reqsize
);
2247 dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n",
2248 &channel
->buffer
.frame
[i
], channel
->idx
, i
,
2249 channel
->buffer
.frame
[i
].lpvbits
);
2250 channel
->buffer
.frame
[i
].size
= reqsize
;
2251 if (channel
->buffer
.frame
[i
].lpvbits
== NULL
) {
2252 printk(KERN_INFO
"out of memory. using less frames\n");
2253 channel
->buffer
.dwFrames
= i
;
2258 /* make sure internal states are set */
2259 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2260 channel
->buffer
.frame
[i
].ulState
= 0;
2261 channel
->buffer
.frame
[i
].cur_size
= 0;
2264 channel
->cur_frame
= 0;
2265 channel
->last_frame
= -1;
2269 static int s2255_release_sys_buffers(struct s2255_channel
*channel
)
2272 dprintk(1, "release sys buffers\n");
2273 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2274 if (channel
->buffer
.frame
[i
].lpvbits
) {
2275 dprintk(1, "vfree %p\n",
2276 channel
->buffer
.frame
[i
].lpvbits
);
2277 vfree(channel
->buffer
.frame
[i
].lpvbits
);
2279 channel
->buffer
.frame
[i
].lpvbits
= NULL
;
2284 static int s2255_board_init(struct s2255_dev
*dev
)
2286 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
2289 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2290 dprintk(4, "board init: %p", dev
);
2291 memset(pipe
, 0, sizeof(*pipe
));
2293 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
2294 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
2296 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
2298 if (pipe
->transfer_buffer
== NULL
) {
2299 dprintk(1, "out of memory!\n");
2302 /* query the firmware */
2303 fw_ver
= s2255_get_fx2fw(dev
);
2305 printk(KERN_INFO
"2255 usb firmware version %d.%d\n",
2306 (fw_ver
>> 8) & 0xff,
2309 if (fw_ver
< S2255_CUR_USB_FWVER
)
2310 dev_err(&dev
->udev
->dev
,
2311 "usb firmware not up to date %d.%d\n",
2312 (fw_ver
>> 8) & 0xff,
2315 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
2316 struct s2255_channel
*channel
= &dev
->channel
[j
];
2317 channel
->b_acquire
= 0;
2318 channel
->mode
= mode_def
;
2319 if (dev
->pid
== 0x2257 && j
> 1)
2320 channel
->mode
.color
|= (1 << 16);
2321 channel
->jc
.quality
= S2255_DEF_JPEG_QUAL
;
2322 channel
->width
= LINE_SZ_4CIFS_NTSC
;
2323 channel
->height
= NUM_LINES_4CIFS_NTSC
* 2;
2324 channel
->fmt
= &formats
[0];
2325 channel
->mode
.restart
= 1;
2326 channel
->req_image_size
= get_transfer_size(&mode_def
);
2327 channel
->frame_count
= 0;
2328 /* create the system buffers */
2329 s2255_create_sys_buffers(channel
);
2331 /* start read pipe */
2332 s2255_start_readpipe(dev
);
2333 dprintk(1, "%s: success\n", __func__
);
2337 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2340 dprintk(1, "%s: dev: %p", __func__
, dev
);
2342 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2343 if (dev
->channel
[i
].b_acquire
)
2344 s2255_stop_acquire(&dev
->channel
[i
]);
2346 s2255_stop_readpipe(dev
);
2347 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2348 s2255_release_sys_buffers(&dev
->channel
[i
]);
2349 /* release transfer buffer */
2350 kfree(dev
->pipe
.transfer_buffer
);
2354 static void read_pipe_completion(struct urb
*purb
)
2356 struct s2255_pipeinfo
*pipe_info
;
2357 struct s2255_dev
*dev
;
2360 pipe_info
= purb
->context
;
2361 dprintk(100, "%s: urb:%p, status %d\n", __func__
, purb
,
2363 if (pipe_info
== NULL
) {
2364 dev_err(&purb
->dev
->dev
, "no context!\n");
2368 dev
= pipe_info
->dev
;
2370 dev_err(&purb
->dev
->dev
, "no context!\n");
2373 status
= purb
->status
;
2374 /* if shutting down, do not resubmit, exit immediately */
2375 if (status
== -ESHUTDOWN
) {
2376 dprintk(2, "%s: err shutdown\n", __func__
);
2377 pipe_info
->err_count
++;
2381 if (pipe_info
->state
== 0) {
2382 dprintk(2, "%s: exiting USB pipe", __func__
);
2387 s2255_read_video_callback(dev
, pipe_info
);
2389 pipe_info
->err_count
++;
2390 dprintk(1, "%s: failed URB %d\n", __func__
, status
);
2393 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2395 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2397 pipe_info
->transfer_buffer
,
2398 pipe_info
->cur_transfer_size
,
2399 read_pipe_completion
, pipe_info
);
2401 if (pipe_info
->state
!= 0) {
2402 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_ATOMIC
)) {
2403 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2406 dprintk(2, "%s :complete state 0\n", __func__
);
2411 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2415 struct s2255_pipeinfo
*pipe_info
= &dev
->pipe
;
2416 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2417 dprintk(2, "%s: IN %d\n", __func__
, dev
->read_endpoint
);
2418 pipe_info
->state
= 1;
2419 pipe_info
->err_count
= 0;
2420 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2421 if (!pipe_info
->stream_urb
) {
2422 dev_err(&dev
->udev
->dev
,
2423 "ReadStream: Unable to alloc URB\n");
2426 /* transfer buffer allocated in board_init */
2427 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2429 pipe_info
->transfer_buffer
,
2430 pipe_info
->cur_transfer_size
,
2431 read_pipe_completion
, pipe_info
);
2432 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2434 printk(KERN_ERR
"s2255: start read pipe failed\n");
2440 /* starts acquisition process */
2441 static int s2255_start_acquire(struct s2255_channel
*channel
)
2443 unsigned char *buffer
;
2445 unsigned long chn_rev
;
2447 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
2448 chn_rev
= G_chnmap
[channel
->idx
];
2449 buffer
= kzalloc(512, GFP_KERNEL
);
2450 if (buffer
== NULL
) {
2451 dev_err(&dev
->udev
->dev
, "out of mem\n");
2455 channel
->last_frame
= -1;
2456 channel
->bad_payload
= 0;
2457 channel
->cur_frame
= 0;
2458 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2459 channel
->buffer
.frame
[j
].ulState
= 0;
2460 channel
->buffer
.frame
[j
].cur_size
= 0;
2463 /* send the start command */
2464 *(__le32
*) buffer
= IN_DATA_TOKEN
;
2465 *((__le32
*) buffer
+ 1) = (__le32
) cpu_to_le32(chn_rev
);
2466 *((__le32
*) buffer
+ 2) = CMD_START
;
2467 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2469 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2471 dprintk(2, "start acquire exit[%d] %d \n", channel
->idx
, res
);
2476 static int s2255_stop_acquire(struct s2255_channel
*channel
)
2478 unsigned char *buffer
;
2480 unsigned long chn_rev
;
2481 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
2482 chn_rev
= G_chnmap
[channel
->idx
];
2483 buffer
= kzalloc(512, GFP_KERNEL
);
2484 if (buffer
== NULL
) {
2485 dev_err(&dev
->udev
->dev
, "out of mem\n");
2488 /* send the stop command */
2489 *(__le32
*) buffer
= IN_DATA_TOKEN
;
2490 *((__le32
*) buffer
+ 1) = (__le32
) cpu_to_le32(chn_rev
);
2491 *((__le32
*) buffer
+ 2) = CMD_STOP
;
2492 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2494 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2496 channel
->b_acquire
= 0;
2497 dprintk(4, "%s: chn %d, res %d\n", __func__
, channel
->idx
, res
);
2501 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2503 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2506 if (pipe
->stream_urb
) {
2508 usb_kill_urb(pipe
->stream_urb
);
2509 usb_free_urb(pipe
->stream_urb
);
2510 pipe
->stream_urb
= NULL
;
2512 dprintk(4, "%s", __func__
);
2516 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
)
2519 s2255_reset_dsppower(dev
);
2520 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2521 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2522 memcpy(dev
->fw_data
->pfw_data
,
2523 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2524 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2525 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2526 usb_sndbulkpipe(dev
->udev
, 2),
2527 dev
->fw_data
->pfw_data
,
2528 CHUNK_SIZE
, s2255_fwchunk_complete
,
2530 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2533 /* standard usb probe function */
2534 static int s2255_probe(struct usb_interface
*interface
,
2535 const struct usb_device_id
*id
)
2537 struct s2255_dev
*dev
= NULL
;
2538 struct usb_host_interface
*iface_desc
;
2539 struct usb_endpoint_descriptor
*endpoint
;
2541 int retval
= -ENOMEM
;
2544 dprintk(2, "%s\n", __func__
);
2545 /* allocate memory for our device state and initialize it to zero */
2546 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2548 s2255_dev_err(&interface
->dev
, "out of memory\n");
2551 atomic_set(&dev
->num_channels
, 0);
2552 dev
->pid
= id
->idProduct
;
2553 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2556 mutex_init(&dev
->lock
);
2557 mutex_init(&dev
->open_lock
);
2558 /* grab usb_device and save it */
2559 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2560 if (dev
->udev
== NULL
) {
2561 dev_err(&interface
->dev
, "null usb device\n");
2565 dprintk(1, "dev: %p, udev %p interface %p\n", dev
,
2566 dev
->udev
, interface
);
2567 dev
->interface
= interface
;
2568 /* set up the endpoint information */
2569 iface_desc
= interface
->cur_altsetting
;
2570 dprintk(1, "num endpoints %d\n", iface_desc
->desc
.bNumEndpoints
);
2571 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2572 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2573 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2574 /* we found the bulk in endpoint */
2575 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2579 if (!dev
->read_endpoint
) {
2580 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2583 init_timer(&dev
->timer
);
2584 dev
->timer
.function
= s2255_timer
;
2585 dev
->timer
.data
= (unsigned long)dev
->fw_data
;
2586 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2587 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2588 struct s2255_channel
*channel
= &dev
->channel
[i
];
2589 dev
->channel
[i
].idx
= i
;
2590 init_waitqueue_head(&channel
->wait_setmode
);
2591 init_waitqueue_head(&channel
->wait_vidstatus
);
2594 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2595 if (!dev
->fw_data
->fw_urb
) {
2596 dev_err(&interface
->dev
, "out of memory!\n");
2600 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2601 if (!dev
->fw_data
->pfw_data
) {
2602 dev_err(&interface
->dev
, "out of memory!\n");
2605 /* load the first chunk */
2606 if (request_firmware(&dev
->fw_data
->fw
,
2607 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2608 printk(KERN_ERR
"sensoray 2255 failed to get firmware\n");
2611 /* check the firmware is valid */
2612 fw_size
= dev
->fw_data
->fw
->size
;
2613 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2615 if (*pdata
!= S2255_FW_MARKER
) {
2616 printk(KERN_INFO
"Firmware invalid.\n");
2620 /* make sure firmware is the latest */
2622 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2623 printk(KERN_INFO
"s2255 dsp fw version %x\n", *pRel
);
2624 dev
->dsp_fw_ver
= *pRel
;
2625 if (*pRel
< S2255_CUR_DSP_FWVER
)
2626 printk(KERN_INFO
"s2255: f2255usb.bin out of date.\n");
2627 if (dev
->pid
== 0x2257 && *pRel
< S2255_MIN_DSP_COLORFILTER
)
2628 printk(KERN_WARNING
"s2255: 2257 requires firmware %d"
2629 " or above.\n", S2255_MIN_DSP_COLORFILTER
);
2631 usb_reset_device(dev
->udev
);
2632 /* load 2255 board specific */
2633 retval
= s2255_board_init(dev
);
2635 goto errorBOARDINIT
;
2636 spin_lock_init(&dev
->slock
);
2637 s2255_fwload_start(dev
, 0);
2638 /* loads v4l specific */
2639 retval
= s2255_probe_v4l(dev
);
2641 goto errorBOARDINIT
;
2642 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2645 s2255_board_shutdown(dev
);
2647 release_firmware(dev
->fw_data
->fw
);
2649 kfree(dev
->fw_data
->pfw_data
);
2651 usb_free_urb(dev
->fw_data
->fw_urb
);
2653 del_timer(&dev
->timer
);
2655 usb_put_dev(dev
->udev
);
2657 kfree(dev
->fw_data
);
2658 mutex_destroy(&dev
->open_lock
);
2659 mutex_destroy(&dev
->lock
);
2662 printk(KERN_WARNING
"Sensoray 2255 driver load failed: 0x%x\n", retval
);
2666 /* disconnect routine. when board is removed physically or with rmmod */
2667 static void s2255_disconnect(struct usb_interface
*interface
)
2669 struct s2255_dev
*dev
= to_s2255_dev(usb_get_intfdata(interface
));
2671 int channels
= atomic_read(&dev
->num_channels
);
2672 mutex_lock(&dev
->lock
);
2673 v4l2_device_disconnect(&dev
->v4l2_dev
);
2674 mutex_unlock(&dev
->lock
);
2675 /*see comments in the uvc_driver.c usb disconnect function */
2676 atomic_inc(&dev
->num_channels
);
2677 /* unregister each video device. */
2678 for (i
= 0; i
< channels
; i
++)
2679 video_unregister_device(&dev
->channel
[i
].vdev
);
2680 /* wake up any of our timers */
2681 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2682 wake_up(&dev
->fw_data
->wait_fw
);
2683 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2684 dev
->channel
[i
].setmode_ready
= 1;
2685 wake_up(&dev
->channel
[i
].wait_setmode
);
2686 dev
->channel
[i
].vidstatus_ready
= 1;
2687 wake_up(&dev
->channel
[i
].wait_vidstatus
);
2689 if (atomic_dec_and_test(&dev
->num_channels
))
2691 dev_info(&interface
->dev
, "%s\n", __func__
);
2694 static struct usb_driver s2255_driver
= {
2695 .name
= S2255_DRIVER_NAME
,
2696 .probe
= s2255_probe
,
2697 .disconnect
= s2255_disconnect
,
2698 .id_table
= s2255_table
,
2701 static int __init
usb_s2255_init(void)
2704 /* register this driver with the USB subsystem */
2705 result
= usb_register(&s2255_driver
);
2707 pr_err(KBUILD_MODNAME
2708 ": usb_register failed. Error number %d\n", result
);
2709 dprintk(2, "%s\n", __func__
);
2713 static void __exit
usb_s2255_exit(void)
2715 usb_deregister(&s2255_driver
);
2718 module_init(usb_s2255_init
);
2719 module_exit(usb_s2255_exit
);
2721 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2722 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2723 MODULE_LICENSE("GPL");