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
19 * -full or half size Grey scale: all 4 channels at once
20 * -half size, color mode YUYV or YUV422P: all 4 channels at once
21 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <linux/module.h>
40 #include <linux/firmware.h>
41 #include <linux/kernel.h>
42 #include <linux/mutex.h>
43 #include <linux/slab.h>
44 #include <linux/videodev2.h>
45 #include <linux/version.h>
47 #include <media/videobuf-vmalloc.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-device.h>
50 #include <media/v4l2-ioctl.h>
51 #include <linux/vmalloc.h>
52 #include <linux/usb.h>
54 #define S2255_MAJOR_VERSION 1
55 #define S2255_MINOR_VERSION 22
56 #define S2255_RELEASE 0
57 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
58 S2255_MINOR_VERSION, \
60 #define FIRMWARE_FILE_NAME "f2255usb.bin"
62 /* default JPEG quality */
63 #define S2255_DEF_JPEG_QUAL 50
64 /* vendor request in */
66 /* vendor request out */
67 #define S2255_VR_OUT 1
69 #define S2255_VR_FW 0x30
70 /* USB endpoint number for configuring the device */
71 #define S2255_CONFIG_EP 2
72 /* maximum time for DSP to start responding after last FW word loaded(ms) */
73 #define S2255_DSP_BOOTTIME 800
74 /* maximum time to wait for firmware to load (ms) */
75 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
76 #define S2255_DEF_BUFS 16
77 #define S2255_SETMODE_TIMEOUT 500
78 #define S2255_VIDSTATUS_TIMEOUT 350
79 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
80 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
81 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
82 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
83 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
84 #define S2255_USB_XFER_SIZE (16 * 1024)
85 #define MAX_CHANNELS 4
87 /* maximum size is PAL full size plus room for the marker header(s) */
88 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
89 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
90 #define LINE_SZ_4CIFS_NTSC 640
91 #define LINE_SZ_2CIFS_NTSC 640
92 #define LINE_SZ_1CIFS_NTSC 320
93 #define LINE_SZ_4CIFS_PAL 704
94 #define LINE_SZ_2CIFS_PAL 704
95 #define LINE_SZ_1CIFS_PAL 352
96 #define NUM_LINES_4CIFS_NTSC 240
97 #define NUM_LINES_2CIFS_NTSC 240
98 #define NUM_LINES_1CIFS_NTSC 240
99 #define NUM_LINES_4CIFS_PAL 288
100 #define NUM_LINES_2CIFS_PAL 288
101 #define NUM_LINES_1CIFS_PAL 288
102 #define LINE_SZ_DEF 640
103 #define NUM_LINES_DEF 240
106 /* predefined settings */
107 #define FORMAT_NTSC 1
110 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
111 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
112 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
113 /* SCALE_4CIFSI is the 2 fields interpolated into one */
114 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
116 #define COLOR_YUVPL 1 /* YUV planar */
117 #define COLOR_YUVPK 2 /* YUV packed */
118 #define COLOR_Y8 4 /* monochrome */
119 #define COLOR_JPG 5 /* JPEG */
121 #define MASK_COLOR 0x000000ff
122 #define MASK_JPG_QUALITY 0x0000ff00
123 #define MASK_INPUT_TYPE 0x000f0000
124 /* frame decimation. */
125 #define FDEC_1 1 /* capture every frame. default */
126 #define FDEC_2 2 /* capture every 2nd frame */
127 #define FDEC_3 3 /* capture every 3rd frame */
128 #define FDEC_5 5 /* capture every 5th frame */
130 /*-------------------------------------------------------
131 * Default mode parameters.
132 *-------------------------------------------------------*/
133 #define DEF_SCALE SCALE_4CIFS
134 #define DEF_COLOR COLOR_YUVPL
135 #define DEF_FDEC FDEC_1
137 #define DEF_CONTRAST 0x5c
138 #define DEF_SATURATION 0x80
141 /* usb config commands */
142 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
143 #define CMD_2255 cpu_to_le32(0xc2255000)
144 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
145 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
146 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
147 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
150 u32 format
; /* input video format (NTSC, PAL) */
151 u32 scale
; /* output video scale */
152 u32 color
; /* output video color format */
153 u32 fdec
; /* frame decimation */
154 u32 bright
; /* brightness */
155 u32 contrast
; /* contrast */
156 u32 saturation
; /* saturation */
157 u32 hue
; /* hue (NTSC only)*/
158 u32 single
; /* capture 1 frame at a time (!=0), continuously (==0)*/
159 u32 usb_block
; /* block size. should be 4096 of DEF_USB_BLOCK */
160 u32 restart
; /* if DSP requires restart */
164 #define S2255_READ_IDLE 0
165 #define S2255_READ_FRAME 1
167 /* frame structure */
168 struct s2255_framei
{
170 unsigned long ulState
; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
171 void *lpvbits
; /* image data */
172 unsigned long cur_size
; /* current data copied to it */
175 /* image buffer structure */
176 struct s2255_bufferi
{
177 unsigned long dwFrames
; /* number of frames in buffer */
178 struct s2255_framei frame
[SYS_FRAMES
]; /* array of FRAME structures */
181 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
182 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
183 DEF_HUE, 0, DEF_USB_BLOCK, 0}
185 struct s2255_dmaqueue
{
186 struct list_head active
;
187 struct s2255_dev
*dev
;
190 /* for firmware loading, fw_state */
191 #define S2255_FW_NOTLOADED 0
192 #define S2255_FW_LOADED_DSPWAIT 1
193 #define S2255_FW_SUCCESS 2
194 #define S2255_FW_FAILED 3
195 #define S2255_FW_DISCONNECTING 4
196 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
197 /* 2255 read states */
198 #define S2255_READ_IDLE 0
199 #define S2255_READ_FRAME 1
206 wait_queue_head_t wait_fw
;
207 const struct firmware
*fw
;
210 struct s2255_pipeinfo
{
211 u32 max_transfer_size
;
212 u32 cur_transfer_size
;
216 void *dev
; /* back pointer to s2255_dev struct*/
221 struct s2255_fmt
; /*forward declaration */
224 struct s2255_channel
{
225 struct video_device vdev
;
227 struct s2255_dmaqueue vidq
;
228 struct s2255_bufferi buffer
;
229 struct s2255_mode mode
;
230 /* jpeg compression */
231 struct v4l2_jpegcompression jc
;
232 /* capture parameters (for high quality mode full size) */
233 struct v4l2_captureparm cap_parm
;
238 /* allocated image size */
239 unsigned long req_image_size
;
240 /* received packet size */
241 unsigned long pkt_size
;
243 unsigned long frame_count
;
246 /* if channel configured to default state */
248 wait_queue_head_t wait_setmode
;
250 /* video status items */
252 wait_queue_head_t wait_vidstatus
;
256 const struct s2255_fmt
*fmt
;
257 int idx
; /* channel number on device, 0-3 */
262 struct s2255_channel channel
[MAX_CHANNELS
];
263 struct v4l2_device v4l2_dev
;
264 atomic_t num_channels
;
266 struct mutex lock
; /* channels[].vdev.lock */
267 struct mutex open_lock
;
268 struct usb_device
*udev
;
269 struct usb_interface
*interface
;
271 struct timer_list timer
;
272 struct s2255_fw
*fw_data
;
273 struct s2255_pipeinfo pipe
;
274 u32 cc
; /* current channel */
278 /* dsp firmware version (f2255usb.bin) */
280 u16 pid
; /* product id */
283 static inline struct s2255_dev
*to_s2255_dev(struct v4l2_device
*v4l2_dev
)
285 return container_of(v4l2_dev
, struct s2255_dev
, v4l2_dev
);
294 /* buffer for one video frame */
295 struct s2255_buffer
{
296 /* common v4l buffer stuff -- must be first */
297 struct videobuf_buffer vb
;
298 const struct s2255_fmt
*fmt
;
302 struct s2255_dev
*dev
;
303 struct videobuf_queue vb_vidq
;
304 enum v4l2_buf_type type
;
305 struct s2255_channel
*channel
;
309 /* current cypress EEPROM firmware version */
310 #define S2255_CUR_USB_FWVER ((3 << 8) | 12)
311 /* current DSP FW version */
312 #define S2255_CUR_DSP_FWVER 10104
313 /* Need DSP version 5+ for video status feature */
314 #define S2255_MIN_DSP_STATUS 5
315 #define S2255_MIN_DSP_COLORFILTER 8
316 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
318 /* private V4L2 controls */
321 * The following chart displays how COLORFILTER should be set
322 * =========================================================
323 * = fourcc = COLORFILTER =
324 * = ===============================
326 * =========================================================
327 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
328 * = = s-video or = composite =
329 * = = B/W camera = input =
330 * =========================================================
331 * = other = color, svideo = color, =
333 * =========================================================
336 * channels 0-3 on 2255 are composite
337 * channels 0-1 on 2257 are composite, 2-3 are s-video
338 * If COLORFILTER is 0 with a composite color camera connected,
339 * the output will appear monochrome but hatching
341 * COLORFILTER is different from "color killer" and "color effects"
344 #define S2255_V4L2_YC_ON 1
345 #define S2255_V4L2_YC_OFF 0
346 #define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
348 /* frame prefix size (sent once every frame) */
349 #define PREFIX_SIZE 512
351 /* Channels on box are in reverse order */
352 static unsigned long G_chnmap
[MAX_CHANNELS
] = {3, 2, 1, 0};
355 static int *s2255_debug
= &debug
;
357 static int s2255_start_readpipe(struct s2255_dev
*dev
);
358 static void s2255_stop_readpipe(struct s2255_dev
*dev
);
359 static int s2255_start_acquire(struct s2255_channel
*channel
);
360 static int s2255_stop_acquire(struct s2255_channel
*channel
);
361 static void s2255_fillbuff(struct s2255_channel
*chn
, struct s2255_buffer
*buf
,
363 static int s2255_set_mode(struct s2255_channel
*chan
, struct s2255_mode
*mode
);
364 static int s2255_board_shutdown(struct s2255_dev
*dev
);
365 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
);
366 static void s2255_destroy(struct s2255_dev
*dev
);
367 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char req
,
368 u16 index
, u16 value
, void *buf
,
369 s32 buf_len
, int bOut
);
371 /* dev_err macro with driver name */
372 #define S2255_DRIVER_NAME "s2255"
373 #define s2255_dev_err(dev, fmt, arg...) \
374 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
376 #define dprintk(level, fmt, arg...) \
378 if (*s2255_debug >= (level)) { \
379 printk(KERN_DEBUG S2255_DRIVER_NAME \
384 static struct usb_driver s2255_driver
;
386 /* Declare static vars that will be used as parameters */
387 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
389 /* start video number */
390 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
392 /* Enable jpeg capture. */
393 static int jpeg_enable
= 1;
395 module_param(debug
, int, 0644);
396 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
397 module_param(vid_limit
, int, 0644);
398 MODULE_PARM_DESC(vid_limit
, "video memory limit(Mb)");
399 module_param(video_nr
, int, 0644);
400 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
401 module_param(jpeg_enable
, int, 0644);
402 MODULE_PARM_DESC(jpeg_enable
, "Jpeg enable(1-on 0-off) default 1");
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)
416 /* JPEG formats must be defined last to support jpeg_enable parameter */
417 static const struct s2255_fmt formats
[] = {
419 .name
= "4:2:2, planar, YUV422P",
420 .fourcc
= V4L2_PIX_FMT_YUV422P
,
424 .name
= "4:2:2, packed, YUYV",
425 .fourcc
= V4L2_PIX_FMT_YUYV
,
429 .name
= "4:2:2, packed, UYVY",
430 .fourcc
= V4L2_PIX_FMT_UYVY
,
434 .fourcc
= V4L2_PIX_FMT_GREY
,
438 .fourcc
= V4L2_PIX_FMT_JPEG
,
442 .fourcc
= V4L2_PIX_FMT_MJPEG
,
447 static int norm_maxw(struct video_device
*vdev
)
449 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
450 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
453 static int norm_maxh(struct video_device
*vdev
)
455 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
456 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
459 static int norm_minw(struct video_device
*vdev
)
461 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
462 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
465 static int norm_minh(struct video_device
*vdev
)
467 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
468 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
473 * TODO: fixme: move YUV reordering to hardware
474 * converts 2255 planar format to yuyv or uyvy
476 static void planar422p_to_yuv_packed(const unsigned char *in
,
478 int width
, int height
,
484 unsigned long size
= height
* width
;
486 pY
= (unsigned char *)in
;
487 pCr
= (unsigned char *)in
+ height
* width
;
488 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
489 for (i
= 0; i
< size
* 2; i
+= 4) {
490 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
491 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
492 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
493 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
498 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
500 s2255_vendor_req(dev
, 0x40, 0x0000, 0x0001, NULL
, 0, 1);
502 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
504 s2255_vendor_req(dev
, 0x10, 0x0000, 0x0000, NULL
, 0, 1);
508 /* kickstarts the firmware loading. from probe
510 static void s2255_timer(unsigned long user_data
)
512 struct s2255_fw
*data
= (struct s2255_fw
*)user_data
;
513 dprintk(100, "%s\n", __func__
);
514 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
515 printk(KERN_ERR
"s2255: can't submit urb\n");
516 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
517 /* wake up anything waiting for the firmware */
518 wake_up(&data
->wait_fw
);
524 /* this loads the firmware asynchronously.
525 Originally this was done synchroously in probe.
526 But it is better to load it asynchronously here than block
527 inside the probe function. Blocking inside probe affects boot time.
528 FW loading is triggered by the timer in the probe function
530 static void s2255_fwchunk_complete(struct urb
*urb
)
532 struct s2255_fw
*data
= urb
->context
;
533 struct usb_device
*udev
= urb
->dev
;
535 dprintk(100, "%s: udev %p urb %p", __func__
, udev
, urb
);
537 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
538 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
539 /* wake up anything waiting for the firmware */
540 wake_up(&data
->wait_fw
);
543 if (data
->fw_urb
== NULL
) {
544 s2255_dev_err(&udev
->dev
, "disconnected\n");
545 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
546 /* wake up anything waiting for the firmware */
547 wake_up(&data
->wait_fw
);
550 #define CHUNK_SIZE 512
551 /* all USB transfers must be done with continuous kernel memory.
552 can't allocate more than 128k in current linux kernel, so
553 upload the firmware in chunks
555 if (data
->fw_loaded
< data
->fw_size
) {
556 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
557 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
559 if (len
< CHUNK_SIZE
)
560 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
562 dprintk(100, "completed len %d, loaded %d \n", len
,
565 memcpy(data
->pfw_data
,
566 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
568 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
569 data
->pfw_data
, CHUNK_SIZE
,
570 s2255_fwchunk_complete
, data
);
571 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
572 dev_err(&udev
->dev
, "failed submit URB\n");
573 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
574 /* wake up anything waiting for the firmware */
575 wake_up(&data
->wait_fw
);
578 data
->fw_loaded
+= len
;
580 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
581 dprintk(100, "%s: firmware upload complete\n", __func__
);
587 static int s2255_got_frame(struct s2255_channel
*channel
, int jpgsize
)
589 struct s2255_dmaqueue
*dma_q
= &channel
->vidq
;
590 struct s2255_buffer
*buf
;
591 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
592 unsigned long flags
= 0;
594 spin_lock_irqsave(&dev
->slock
, flags
);
595 if (list_empty(&dma_q
->active
)) {
596 dprintk(1, "No active queue to serve\n");
600 buf
= list_entry(dma_q
->active
.next
,
601 struct s2255_buffer
, vb
.queue
);
602 list_del(&buf
->vb
.queue
);
603 do_gettimeofday(&buf
->vb
.ts
);
604 s2255_fillbuff(channel
, buf
, jpgsize
);
605 wake_up(&buf
->vb
.done
);
606 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__
, buf
, buf
->vb
.i
);
608 spin_unlock_irqrestore(&dev
->slock
, flags
);
612 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
615 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
616 if (-1 == formats
[i
].fourcc
)
618 if (!jpeg_enable
&& ((formats
[i
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
619 (formats
[i
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
621 if (formats
[i
].fourcc
== fourcc
)
627 /* video buffer vmalloc implementation based partly on VIVI driver which is
628 * Copyright (c) 2006 by
629 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
630 * Ted Walther <ted--a.t--enumera.com>
631 * John Sokol <sokol--a.t--videotechnology.com>
632 * http://v4l.videotechnology.com/
635 static void s2255_fillbuff(struct s2255_channel
*channel
,
636 struct s2255_buffer
*buf
, int jpgsize
)
641 char *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
642 unsigned long last_frame
;
643 struct s2255_framei
*frm
;
647 last_frame
= channel
->last_frame
;
648 if (last_frame
!= -1) {
649 frm
= &channel
->buffer
.frame
[last_frame
];
651 (const char *)channel
->buffer
.frame
[last_frame
].lpvbits
;
652 switch (buf
->fmt
->fourcc
) {
653 case V4L2_PIX_FMT_YUYV
:
654 case V4L2_PIX_FMT_UYVY
:
655 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
660 case V4L2_PIX_FMT_GREY
:
661 memcpy(vbuf
, tmpbuf
, buf
->vb
.width
* buf
->vb
.height
);
663 case V4L2_PIX_FMT_JPEG
:
664 case V4L2_PIX_FMT_MJPEG
:
665 buf
->vb
.size
= jpgsize
;
666 memcpy(vbuf
, tmpbuf
, buf
->vb
.size
);
668 case V4L2_PIX_FMT_YUV422P
:
670 buf
->vb
.width
* buf
->vb
.height
* 2);
673 printk(KERN_DEBUG
"s2255: unknown format?\n");
675 channel
->last_frame
= -1;
677 printk(KERN_ERR
"s2255: =======no frame\n");
681 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
682 (unsigned long)vbuf
, pos
);
683 /* tell v4l buffer was filled */
685 buf
->vb
.field_count
= channel
->frame_count
* 2;
686 do_gettimeofday(&ts
);
688 buf
->vb
.state
= VIDEOBUF_DONE
;
692 /* ------------------------------------------------------------------
694 ------------------------------------------------------------------*/
696 static int buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
699 struct s2255_fh
*fh
= vq
->priv_data
;
700 struct s2255_channel
*channel
= fh
->channel
;
701 *size
= channel
->width
* channel
->height
* (channel
->fmt
->depth
>> 3);
704 *count
= S2255_DEF_BUFS
;
706 if (*size
* *count
> vid_limit
* 1024 * 1024)
707 *count
= (vid_limit
* 1024 * 1024) / *size
;
712 static void free_buffer(struct videobuf_queue
*vq
, struct s2255_buffer
*buf
)
714 dprintk(4, "%s\n", __func__
);
716 videobuf_vmalloc_free(&buf
->vb
);
717 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
720 static int buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
721 enum v4l2_field field
)
723 struct s2255_fh
*fh
= vq
->priv_data
;
724 struct s2255_channel
*channel
= fh
->channel
;
725 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
727 int w
= channel
->width
;
728 int h
= channel
->height
;
729 dprintk(4, "%s, field=%d\n", __func__
, field
);
730 if (channel
->fmt
== NULL
)
733 if ((w
< norm_minw(&channel
->vdev
)) ||
734 (w
> norm_maxw(&channel
->vdev
)) ||
735 (h
< norm_minh(&channel
->vdev
)) ||
736 (h
> norm_maxh(&channel
->vdev
))) {
737 dprintk(4, "invalid buffer prepare\n");
740 buf
->vb
.size
= w
* h
* (channel
->fmt
->depth
>> 3);
741 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
) {
742 dprintk(4, "invalid buffer prepare\n");
746 buf
->fmt
= channel
->fmt
;
749 buf
->vb
.field
= field
;
751 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
752 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
757 buf
->vb
.state
= VIDEOBUF_PREPARED
;
760 free_buffer(vq
, buf
);
764 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
766 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
767 struct s2255_fh
*fh
= vq
->priv_data
;
768 struct s2255_channel
*channel
= fh
->channel
;
769 struct s2255_dmaqueue
*vidq
= &channel
->vidq
;
770 dprintk(1, "%s\n", __func__
);
771 buf
->vb
.state
= VIDEOBUF_QUEUED
;
772 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
775 static void buffer_release(struct videobuf_queue
*vq
,
776 struct videobuf_buffer
*vb
)
778 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
779 struct s2255_fh
*fh
= vq
->priv_data
;
780 dprintk(4, "%s %d\n", __func__
, fh
->channel
->idx
);
781 free_buffer(vq
, buf
);
784 static struct videobuf_queue_ops s2255_video_qops
= {
785 .buf_setup
= buffer_setup
,
786 .buf_prepare
= buffer_prepare
,
787 .buf_queue
= buffer_queue
,
788 .buf_release
= buffer_release
,
792 static int res_get(struct s2255_fh
*fh
)
794 struct s2255_channel
*channel
= fh
->channel
;
796 if (channel
->resources
)
797 return 0; /* no, someone else uses it */
798 /* it's free, grab it */
799 channel
->resources
= 1;
801 dprintk(1, "s2255: res: get\n");
805 static int res_locked(struct s2255_fh
*fh
)
807 return fh
->channel
->resources
;
810 static int res_check(struct s2255_fh
*fh
)
812 return fh
->resources
;
816 static void res_free(struct s2255_fh
*fh
)
818 struct s2255_channel
*channel
= fh
->channel
;
819 channel
->resources
= 0;
821 dprintk(1, "res: put\n");
824 static int vidioc_querymenu(struct file
*file
, void *priv
,
825 struct v4l2_querymenu
*qmenu
)
827 static const char *colorfilter
[] = {
832 if (qmenu
->id
== V4L2_CID_PRIVATE_COLORFILTER
) {
834 const char **menu_items
= colorfilter
;
835 for (i
= 0; i
< qmenu
->index
&& menu_items
[i
]; i
++)
836 ; /* do nothing (from v4l2-common.c) */
837 if (menu_items
[i
] == NULL
|| menu_items
[i
][0] == '\0')
839 strlcpy(qmenu
->name
, menu_items
[qmenu
->index
],
840 sizeof(qmenu
->name
));
843 return v4l2_ctrl_query_menu(qmenu
, NULL
, NULL
);
846 static int vidioc_querycap(struct file
*file
, void *priv
,
847 struct v4l2_capability
*cap
)
849 struct s2255_fh
*fh
= file
->private_data
;
850 struct s2255_dev
*dev
= fh
->dev
;
851 strlcpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
852 strlcpy(cap
->card
, "s2255", sizeof(cap
->card
));
853 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
854 cap
->version
= S2255_VERSION
;
855 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
859 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
860 struct v4l2_fmtdesc
*f
)
866 if (index
>= ARRAY_SIZE(formats
))
868 if (!jpeg_enable
&& ((formats
[index
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
869 (formats
[index
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
871 dprintk(4, "name %s\n", formats
[index
].name
);
872 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
873 f
->pixelformat
= formats
[index
].fourcc
;
877 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
878 struct v4l2_format
*f
)
880 struct s2255_fh
*fh
= priv
;
881 struct s2255_channel
*channel
= fh
->channel
;
883 f
->fmt
.pix
.width
= channel
->width
;
884 f
->fmt
.pix
.height
= channel
->height
;
885 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
886 f
->fmt
.pix
.pixelformat
= channel
->fmt
->fourcc
;
887 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (channel
->fmt
->depth
>> 3);
888 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
892 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
893 struct v4l2_format
*f
)
895 const struct s2255_fmt
*fmt
;
896 enum v4l2_field field
;
898 struct s2255_fh
*fh
= priv
;
899 struct s2255_channel
*channel
= fh
->channel
;
902 (channel
->vdev
.current_norm
& V4L2_STD_NTSC
) ? 1 : 0;
904 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
909 field
= f
->fmt
.pix
.field
;
910 if (field
== V4L2_FIELD_ANY
)
913 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
914 __func__
, is_ntsc
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
917 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
918 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
920 field
= V4L2_FIELD_SEQ_TB
;
921 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
922 (field
== V4L2_FIELD_SEQ_TB
) ||
923 (field
== V4L2_FIELD_INTERLACED_TB
))) {
924 dprintk(1, "unsupported field setting\n");
928 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
930 field
= V4L2_FIELD_TOP
;
931 } else if (!((field
== V4L2_FIELD_TOP
) ||
932 (field
== V4L2_FIELD_BOTTOM
))) {
933 dprintk(1, "unsupported field setting\n");
938 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
939 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
940 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_NTSC
)
941 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_NTSC
;
942 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_NTSC
)
943 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
945 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
948 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
949 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
951 field
= V4L2_FIELD_SEQ_TB
;
952 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
953 (field
== V4L2_FIELD_SEQ_TB
) ||
954 (field
== V4L2_FIELD_INTERLACED_TB
))) {
955 dprintk(1, "unsupported field setting\n");
959 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
961 field
= V4L2_FIELD_TOP
;
962 } else if (!((field
== V4L2_FIELD_TOP
) ||
963 (field
== V4L2_FIELD_BOTTOM
))) {
964 dprintk(1, "unsupported field setting\n");
968 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
) {
969 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
970 field
= V4L2_FIELD_SEQ_TB
;
971 } else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_PAL
) {
972 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_PAL
;
973 field
= V4L2_FIELD_TOP
;
974 } else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_PAL
) {
975 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
976 field
= V4L2_FIELD_TOP
;
978 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
979 field
= V4L2_FIELD_TOP
;
982 f
->fmt
.pix
.field
= field
;
983 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
984 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
985 dprintk(50, "%s: set width %d height %d field %d\n", __func__
,
986 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
990 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
991 struct v4l2_format
*f
)
993 struct s2255_fh
*fh
= priv
;
994 struct s2255_channel
*channel
= fh
->channel
;
995 const struct s2255_fmt
*fmt
;
996 struct videobuf_queue
*q
= &fh
->vb_vidq
;
997 struct s2255_mode mode
;
1001 ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
1006 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1011 mutex_lock(&q
->vb_lock
);
1013 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1014 dprintk(1, "queue busy\n");
1019 if (res_locked(fh
)) {
1020 dprintk(1, "%s: channel busy\n", __func__
);
1024 mode
= channel
->mode
;
1026 channel
->width
= f
->fmt
.pix
.width
;
1027 channel
->height
= f
->fmt
.pix
.height
;
1028 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1030 norm
= norm_minw(&channel
->vdev
);
1031 if (channel
->width
> norm_minw(&channel
->vdev
)) {
1032 if (channel
->height
> norm_minh(&channel
->vdev
)) {
1033 if (channel
->cap_parm
.capturemode
&
1034 V4L2_MODE_HIGHQUALITY
)
1035 mode
.scale
= SCALE_4CIFSI
;
1037 mode
.scale
= SCALE_4CIFS
;
1039 mode
.scale
= SCALE_2CIFS
;
1042 mode
.scale
= SCALE_1CIFS
;
1045 switch (channel
->fmt
->fourcc
) {
1046 case V4L2_PIX_FMT_GREY
:
1047 mode
.color
&= ~MASK_COLOR
;
1048 mode
.color
|= COLOR_Y8
;
1050 case V4L2_PIX_FMT_JPEG
:
1051 case V4L2_PIX_FMT_MJPEG
:
1052 mode
.color
&= ~MASK_COLOR
;
1053 mode
.color
|= COLOR_JPG
;
1054 mode
.color
|= (channel
->jc
.quality
<< 8);
1056 case V4L2_PIX_FMT_YUV422P
:
1057 mode
.color
&= ~MASK_COLOR
;
1058 mode
.color
|= COLOR_YUVPL
;
1060 case V4L2_PIX_FMT_YUYV
:
1061 case V4L2_PIX_FMT_UYVY
:
1063 mode
.color
&= ~MASK_COLOR
;
1064 mode
.color
|= COLOR_YUVPK
;
1067 if ((mode
.color
& MASK_COLOR
) != (channel
->mode
.color
& MASK_COLOR
))
1069 else if (mode
.scale
!= channel
->mode
.scale
)
1071 else if (mode
.format
!= channel
->mode
.format
)
1073 channel
->mode
= mode
;
1074 (void) s2255_set_mode(channel
, &mode
);
1077 mutex_unlock(&q
->vb_lock
);
1081 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1082 struct v4l2_requestbuffers
*p
)
1085 struct s2255_fh
*fh
= priv
;
1086 rc
= videobuf_reqbufs(&fh
->vb_vidq
, p
);
1090 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1093 struct s2255_fh
*fh
= priv
;
1094 rc
= videobuf_querybuf(&fh
->vb_vidq
, p
);
1098 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1101 struct s2255_fh
*fh
= priv
;
1102 rc
= videobuf_qbuf(&fh
->vb_vidq
, p
);
1106 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1109 struct s2255_fh
*fh
= priv
;
1110 rc
= videobuf_dqbuf(&fh
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
1114 /* write to the configuration pipe, synchronously */
1115 static int s2255_write_config(struct usb_device
*udev
, unsigned char *pbuf
,
1122 pipe
= usb_sndbulkpipe(udev
, S2255_CONFIG_EP
);
1123 retval
= usb_bulk_msg(udev
, pipe
, pbuf
, size
, &done
, 500);
1128 static u32
get_transfer_size(struct s2255_mode
*mode
)
1130 int linesPerFrame
= LINE_SZ_DEF
;
1131 int pixelsPerLine
= NUM_LINES_DEF
;
1134 unsigned int mask_mult
;
1139 if (mode
->format
== FORMAT_NTSC
) {
1140 switch (mode
->scale
) {
1143 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
1144 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
1147 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
1148 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
1151 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
1152 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
1157 } else if (mode
->format
== FORMAT_PAL
) {
1158 switch (mode
->scale
) {
1161 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
1162 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
1165 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
1166 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
1169 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
1170 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
1176 outImageSize
= linesPerFrame
* pixelsPerLine
;
1177 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
1178 /* 2 bytes/pixel if not monochrome */
1182 /* total bytes to send including prefix and 4K padding;
1183 must be a multiple of USB_READ_SIZE */
1184 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
1185 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
1186 /* if size not a multiple of USB_READ_SIZE */
1187 if (usbInSize
& ~mask_mult
)
1188 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
1192 static void s2255_print_cfg(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
1194 struct device
*dev
= &sdev
->udev
->dev
;
1195 dev_info(dev
, "------------------------------------------------\n");
1196 dev_info(dev
, "format: %d\nscale %d\n", mode
->format
, mode
->scale
);
1197 dev_info(dev
, "fdec: %d\ncolor %d\n", mode
->fdec
, mode
->color
);
1198 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
1199 dev_info(dev
, "------------------------------------------------\n");
1203 * set mode is the function which controls the DSP.
1204 * the restart parameter in struct s2255_mode should be set whenever
1205 * the image size could change via color format, video system or image
1207 * When the restart parameter is set, we sleep for ONE frame to allow the
1208 * DSP time to get the new frame
1210 static int s2255_set_mode(struct s2255_channel
*channel
,
1211 struct s2255_mode
*mode
)
1215 unsigned long chn_rev
;
1216 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1217 chn_rev
= G_chnmap
[channel
->idx
];
1218 dprintk(3, "%s channel: %d\n", __func__
, channel
->idx
);
1219 /* if JPEG, set the quality */
1220 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
) {
1221 mode
->color
&= ~MASK_COLOR
;
1222 mode
->color
|= COLOR_JPG
;
1223 mode
->color
&= ~MASK_JPG_QUALITY
;
1224 mode
->color
|= (channel
->jc
.quality
<< 8);
1227 channel
->mode
= *mode
;
1228 channel
->req_image_size
= get_transfer_size(mode
);
1229 dprintk(1, "%s: reqsize %ld\n", __func__
, channel
->req_image_size
);
1230 buffer
= kzalloc(512, GFP_KERNEL
);
1231 if (buffer
== NULL
) {
1232 dev_err(&dev
->udev
->dev
, "out of mem\n");
1236 buffer
[0] = IN_DATA_TOKEN
;
1237 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1238 buffer
[2] = CMD_SET_MODE
;
1239 memcpy(&buffer
[3], &channel
->mode
, sizeof(struct s2255_mode
));
1240 channel
->setmode_ready
= 0;
1241 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1243 s2255_print_cfg(dev
, mode
);
1245 /* wait at least 3 frames before continuing */
1246 if (mode
->restart
) {
1247 wait_event_timeout(channel
->wait_setmode
,
1248 (channel
->setmode_ready
!= 0),
1249 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1250 if (channel
->setmode_ready
!= 1) {
1251 printk(KERN_DEBUG
"s2255: no set mode response\n");
1255 /* clear the restart flag */
1256 channel
->mode
.restart
= 0;
1257 dprintk(1, "%s chn %d, result: %d\n", __func__
, channel
->idx
, res
);
1261 static int s2255_cmd_status(struct s2255_channel
*channel
, u32
*pstatus
)
1266 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1267 chn_rev
= G_chnmap
[channel
->idx
];
1268 dprintk(4, "%s chan %d\n", __func__
, channel
->idx
);
1269 buffer
= kzalloc(512, GFP_KERNEL
);
1270 if (buffer
== NULL
) {
1271 dev_err(&dev
->udev
->dev
, "out of mem\n");
1274 /* form the get vid status command */
1275 buffer
[0] = IN_DATA_TOKEN
;
1276 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1277 buffer
[2] = CMD_STATUS
;
1279 channel
->vidstatus_ready
= 0;
1280 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1282 wait_event_timeout(channel
->wait_vidstatus
,
1283 (channel
->vidstatus_ready
!= 0),
1284 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT
));
1285 if (channel
->vidstatus_ready
!= 1) {
1286 printk(KERN_DEBUG
"s2255: no vidstatus response\n");
1289 *pstatus
= channel
->vidstatus
;
1290 dprintk(4, "%s, vid status %d\n", __func__
, *pstatus
);
1294 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1297 struct s2255_fh
*fh
= priv
;
1298 struct s2255_dev
*dev
= fh
->dev
;
1299 struct s2255_channel
*channel
= fh
->channel
;
1301 dprintk(4, "%s\n", __func__
);
1302 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1303 dev_err(&dev
->udev
->dev
, "invalid fh type0\n");
1306 if (i
!= fh
->type
) {
1307 dev_err(&dev
->udev
->dev
, "invalid fh type1\n");
1312 s2255_dev_err(&dev
->udev
->dev
, "stream busy\n");
1315 channel
->last_frame
= -1;
1316 channel
->bad_payload
= 0;
1317 channel
->cur_frame
= 0;
1318 channel
->frame_count
= 0;
1319 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1320 channel
->buffer
.frame
[j
].ulState
= S2255_READ_IDLE
;
1321 channel
->buffer
.frame
[j
].cur_size
= 0;
1323 res
= videobuf_streamon(&fh
->vb_vidq
);
1325 s2255_start_acquire(channel
);
1326 channel
->b_acquire
= 1;
1333 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1335 struct s2255_fh
*fh
= priv
;
1336 dprintk(4, "%s\n, channel: %d", __func__
, fh
->channel
->idx
);
1337 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1338 printk(KERN_ERR
"invalid fh type0\n");
1341 if (i
!= fh
->type
) {
1342 printk(KERN_ERR
"invalid type i\n");
1345 s2255_stop_acquire(fh
->channel
);
1346 videobuf_streamoff(&fh
->vb_vidq
);
1351 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1353 struct s2255_fh
*fh
= priv
;
1354 struct s2255_mode mode
;
1355 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1357 mutex_lock(&q
->vb_lock
);
1358 if (videobuf_queue_is_busy(q
)) {
1359 dprintk(1, "queue busy\n");
1363 if (res_locked(fh
)) {
1364 dprintk(1, "can't change standard after started\n");
1368 mode
= fh
->channel
->mode
;
1369 if (*i
& V4L2_STD_NTSC
) {
1370 dprintk(4, "%s NTSC\n", __func__
);
1371 /* if changing format, reset frame decimation/intervals */
1372 if (mode
.format
!= FORMAT_NTSC
) {
1374 mode
.format
= FORMAT_NTSC
;
1377 } else if (*i
& V4L2_STD_PAL
) {
1378 dprintk(4, "%s PAL\n", __func__
);
1379 if (mode
.format
!= FORMAT_PAL
) {
1381 mode
.format
= FORMAT_PAL
;
1388 s2255_set_mode(fh
->channel
, &mode
);
1390 mutex_unlock(&q
->vb_lock
);
1394 /* Sensoray 2255 is a multiple channel capture device.
1395 It does not have a "crossbar" of inputs.
1396 We use one V4L device per channel. The user must
1397 be aware that certain combinations are not allowed.
1398 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1399 at once in color(you can do full fps on 4 channels with greyscale.
1401 static int vidioc_enum_input(struct file
*file
, void *priv
,
1402 struct v4l2_input
*inp
)
1404 struct s2255_fh
*fh
= priv
;
1405 struct s2255_dev
*dev
= fh
->dev
;
1406 struct s2255_channel
*channel
= fh
->channel
;
1408 if (inp
->index
!= 0)
1410 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1411 inp
->std
= S2255_NORMS
;
1413 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_STATUS
) {
1415 rc
= s2255_cmd_status(fh
->channel
, &status
);
1416 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc
, status
);
1418 inp
->status
= (status
& 0x01) ? 0
1419 : V4L2_IN_ST_NO_SIGNAL
;
1424 strlcpy(inp
->name
, "Composite", sizeof(inp
->name
));
1427 strlcpy(inp
->name
, (channel
->idx
< 2) ? "Composite" : "S-Video",
1434 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1439 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1446 /* --- controls ---------------------------------------------- */
1447 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1448 struct v4l2_queryctrl
*qc
)
1450 struct s2255_fh
*fh
= priv
;
1451 struct s2255_channel
*channel
= fh
->channel
;
1452 struct s2255_dev
*dev
= fh
->dev
;
1454 case V4L2_CID_BRIGHTNESS
:
1455 v4l2_ctrl_query_fill(qc
, -127, 127, 1, DEF_BRIGHT
);
1457 case V4L2_CID_CONTRAST
:
1458 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_CONTRAST
);
1460 case V4L2_CID_SATURATION
:
1461 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_SATURATION
);
1464 v4l2_ctrl_query_fill(qc
, 0, 255, 1, DEF_HUE
);
1466 case V4L2_CID_PRIVATE_COLORFILTER
:
1467 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1469 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1471 strlcpy(qc
->name
, "Color Filter", sizeof(qc
->name
));
1472 qc
->type
= V4L2_CTRL_TYPE_MENU
;
1476 qc
->default_value
= 1;
1482 dprintk(4, "%s, id %d\n", __func__
, qc
->id
);
1486 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1487 struct v4l2_control
*ctrl
)
1489 struct s2255_fh
*fh
= priv
;
1490 struct s2255_dev
*dev
= fh
->dev
;
1491 struct s2255_channel
*channel
= fh
->channel
;
1493 case V4L2_CID_BRIGHTNESS
:
1494 ctrl
->value
= channel
->mode
.bright
;
1496 case V4L2_CID_CONTRAST
:
1497 ctrl
->value
= channel
->mode
.contrast
;
1499 case V4L2_CID_SATURATION
:
1500 ctrl
->value
= channel
->mode
.saturation
;
1503 ctrl
->value
= channel
->mode
.hue
;
1505 case V4L2_CID_PRIVATE_COLORFILTER
:
1506 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1508 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1510 ctrl
->value
= !((channel
->mode
.color
& MASK_INPUT_TYPE
) >> 16);
1515 dprintk(4, "%s, id %d val %d\n", __func__
, ctrl
->id
, ctrl
->value
);
1519 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1520 struct v4l2_control
*ctrl
)
1522 struct s2255_fh
*fh
= priv
;
1523 struct s2255_channel
*channel
= fh
->channel
;
1524 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
1525 struct s2255_mode mode
;
1526 mode
= channel
->mode
;
1527 dprintk(4, "%s\n", __func__
);
1528 /* update the mode to the corresponding value */
1530 case V4L2_CID_BRIGHTNESS
:
1531 mode
.bright
= ctrl
->value
;
1533 case V4L2_CID_CONTRAST
:
1534 mode
.contrast
= ctrl
->value
;
1537 mode
.hue
= ctrl
->value
;
1539 case V4L2_CID_SATURATION
:
1540 mode
.saturation
= ctrl
->value
;
1542 case V4L2_CID_PRIVATE_COLORFILTER
:
1543 if (dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
1545 if ((dev
->pid
== 0x2257) && (channel
->idx
> 1))
1547 mode
.color
&= ~MASK_INPUT_TYPE
;
1548 mode
.color
|= ((ctrl
->value
? 0 : 1) << 16);
1554 /* set mode here. Note: stream does not need restarted.
1555 some V4L programs restart stream unnecessarily
1558 s2255_set_mode(fh
->channel
, &mode
);
1562 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1563 struct v4l2_jpegcompression
*jc
)
1565 struct s2255_fh
*fh
= priv
;
1566 struct s2255_channel
*channel
= fh
->channel
;
1568 dprintk(2, "%s: quality %d\n", __func__
, jc
->quality
);
1572 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1573 struct v4l2_jpegcompression
*jc
)
1575 struct s2255_fh
*fh
= priv
;
1576 struct s2255_channel
*channel
= fh
->channel
;
1577 if (jc
->quality
< 0 || jc
->quality
> 100)
1579 channel
->jc
.quality
= jc
->quality
;
1580 dprintk(2, "%s: quality %d\n", __func__
, jc
->quality
);
1584 static int vidioc_g_parm(struct file
*file
, void *priv
,
1585 struct v4l2_streamparm
*sp
)
1587 struct s2255_fh
*fh
= priv
;
1588 __u32 def_num
, def_dem
;
1589 struct s2255_channel
*channel
= fh
->channel
;
1590 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1592 memset(sp
, 0, sizeof(struct v4l2_streamparm
));
1593 sp
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1594 sp
->parm
.capture
.capturemode
= channel
->cap_parm
.capturemode
;
1595 def_num
= (channel
->mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1596 def_dem
= (channel
->mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1597 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1598 switch (channel
->mode
.fdec
) {
1601 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1604 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1607 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1610 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1613 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__
,
1614 sp
->parm
.capture
.capturemode
,
1615 sp
->parm
.capture
.timeperframe
.numerator
,
1616 sp
->parm
.capture
.timeperframe
.denominator
);
1620 static int vidioc_s_parm(struct file
*file
, void *priv
,
1621 struct v4l2_streamparm
*sp
)
1623 struct s2255_fh
*fh
= priv
;
1624 struct s2255_channel
*channel
= fh
->channel
;
1625 struct s2255_mode mode
;
1627 __u32 def_num
, def_dem
;
1628 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1630 mode
= channel
->mode
;
1631 /* high quality capture mode requires a stream restart */
1632 if (channel
->cap_parm
.capturemode
1633 != sp
->parm
.capture
.capturemode
&& res_locked(fh
))
1635 def_num
= (mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1636 def_dem
= (mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1637 if (def_dem
!= sp
->parm
.capture
.timeperframe
.denominator
)
1638 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1639 else if (sp
->parm
.capture
.timeperframe
.numerator
<= def_num
)
1640 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1641 else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 2)) {
1642 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1644 } else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 3)) {
1645 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1648 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1652 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1653 s2255_set_mode(channel
, &mode
);
1654 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1656 sp
->parm
.capture
.capturemode
,
1657 sp
->parm
.capture
.timeperframe
.numerator
,
1658 sp
->parm
.capture
.timeperframe
.denominator
, fdec
);
1662 static int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1663 struct v4l2_frmivalenum
*fe
)
1666 #define NUM_FRAME_ENUMS 4
1667 int frm_dec
[NUM_FRAME_ENUMS
] = {1, 2, 3, 5};
1668 if (fe
->index
< 0 || fe
->index
>= NUM_FRAME_ENUMS
)
1670 switch (fe
->width
) {
1672 if (fe
->height
!= 240 && fe
->height
!= 480)
1677 if (fe
->height
!= 240)
1682 if (fe
->height
!= 288 && fe
->height
!= 576)
1686 if (fe
->height
!= 288)
1692 fe
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1693 fe
->discrete
.denominator
= is_ntsc
? 30000 : 25000;
1694 fe
->discrete
.numerator
= (is_ntsc
? 1001 : 1000) * frm_dec
[fe
->index
];
1695 dprintk(4, "%s discrete %d/%d\n", __func__
, fe
->discrete
.numerator
,
1696 fe
->discrete
.denominator
);
1700 static int s2255_open(struct file
*file
)
1702 struct video_device
*vdev
= video_devdata(file
);
1703 struct s2255_channel
*channel
= video_drvdata(file
);
1704 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1705 struct s2255_fh
*fh
;
1706 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1708 dprintk(1, "s2255: open called (dev=%s)\n",
1709 video_device_node_name(vdev
));
1711 * open lock necessary to prevent multiple instances
1712 * of v4l-conf (or other programs) from simultaneously
1713 * reloading firmware.
1715 mutex_lock(&dev
->open_lock
);
1716 state
= atomic_read(&dev
->fw_data
->fw_state
);
1718 case S2255_FW_DISCONNECTING
:
1719 mutex_unlock(&dev
->open_lock
);
1721 case S2255_FW_FAILED
:
1722 s2255_dev_err(&dev
->udev
->dev
,
1723 "firmware load failed. retrying.\n");
1724 s2255_fwload_start(dev
, 1);
1725 wait_event_timeout(dev
->fw_data
->wait_fw
,
1726 ((atomic_read(&dev
->fw_data
->fw_state
)
1727 == S2255_FW_SUCCESS
) ||
1728 (atomic_read(&dev
->fw_data
->fw_state
)
1729 == S2255_FW_DISCONNECTING
)),
1730 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1731 /* state may have changed, re-read */
1732 state
= atomic_read(&dev
->fw_data
->fw_state
);
1734 case S2255_FW_NOTLOADED
:
1735 case S2255_FW_LOADED_DSPWAIT
:
1736 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1737 driver loaded and then device immediately opened */
1738 printk(KERN_INFO
"%s waiting for firmware load\n", __func__
);
1739 wait_event_timeout(dev
->fw_data
->wait_fw
,
1740 ((atomic_read(&dev
->fw_data
->fw_state
)
1741 == S2255_FW_SUCCESS
) ||
1742 (atomic_read(&dev
->fw_data
->fw_state
)
1743 == S2255_FW_DISCONNECTING
)),
1744 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1745 /* state may have changed, re-read */
1746 state
= atomic_read(&dev
->fw_data
->fw_state
);
1748 case S2255_FW_SUCCESS
:
1752 /* state may have changed in above switch statement */
1754 case S2255_FW_SUCCESS
:
1756 case S2255_FW_FAILED
:
1757 printk(KERN_INFO
"2255 firmware load failed.\n");
1758 mutex_unlock(&dev
->open_lock
);
1760 case S2255_FW_DISCONNECTING
:
1761 printk(KERN_INFO
"%s: disconnecting\n", __func__
);
1762 mutex_unlock(&dev
->open_lock
);
1764 case S2255_FW_LOADED_DSPWAIT
:
1765 case S2255_FW_NOTLOADED
:
1766 printk(KERN_INFO
"%s: firmware not loaded yet"
1767 "please try again later\n",
1770 * Timeout on firmware load means device unusable.
1771 * Set firmware failure state.
1772 * On next s2255_open the firmware will be reloaded.
1774 atomic_set(&dev
->fw_data
->fw_state
,
1776 mutex_unlock(&dev
->open_lock
);
1779 printk(KERN_INFO
"%s: unknown state\n", __func__
);
1780 mutex_unlock(&dev
->open_lock
);
1783 mutex_unlock(&dev
->open_lock
);
1784 /* allocate + initialize per filehandle data */
1785 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1788 file
->private_data
= fh
;
1790 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1791 fh
->channel
= channel
;
1792 if (!channel
->configured
) {
1793 /* configure channel to default state */
1794 channel
->fmt
= &formats
[0];
1795 s2255_set_mode(channel
, &channel
->mode
);
1796 channel
->configured
= 1;
1798 dprintk(1, "%s: dev=%s type=%s\n", __func__
,
1799 video_device_node_name(vdev
), v4l2_type_names
[type
]);
1800 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__
,
1801 (unsigned long)fh
, (unsigned long)dev
,
1802 (unsigned long)&channel
->vidq
);
1803 dprintk(4, "%s: list_empty active=%d\n", __func__
,
1804 list_empty(&channel
->vidq
.active
));
1805 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &s2255_video_qops
,
1808 V4L2_FIELD_INTERLACED
,
1809 sizeof(struct s2255_buffer
),
1815 static unsigned int s2255_poll(struct file
*file
,
1816 struct poll_table_struct
*wait
)
1818 struct s2255_fh
*fh
= file
->private_data
;
1820 dprintk(100, "%s\n", __func__
);
1821 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1823 rc
= videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1827 static void s2255_destroy(struct s2255_dev
*dev
)
1829 /* board shutdown stops the read pipe if it is running */
1830 s2255_board_shutdown(dev
);
1831 /* make sure firmware still not trying to load */
1832 del_timer(&dev
->timer
); /* only started in .probe and .open */
1833 if (dev
->fw_data
->fw_urb
) {
1834 usb_kill_urb(dev
->fw_data
->fw_urb
);
1835 usb_free_urb(dev
->fw_data
->fw_urb
);
1836 dev
->fw_data
->fw_urb
= NULL
;
1838 if (dev
->fw_data
->fw
)
1839 release_firmware(dev
->fw_data
->fw
);
1840 kfree(dev
->fw_data
->pfw_data
);
1841 kfree(dev
->fw_data
);
1842 /* reset the DSP so firmware can be reloaded next time */
1843 s2255_reset_dsppower(dev
);
1844 mutex_destroy(&dev
->open_lock
);
1845 mutex_destroy(&dev
->lock
);
1846 usb_put_dev(dev
->udev
);
1847 v4l2_device_unregister(&dev
->v4l2_dev
);
1848 dprintk(1, "%s", __func__
);
1852 static int s2255_release(struct file
*file
)
1854 struct s2255_fh
*fh
= file
->private_data
;
1855 struct s2255_dev
*dev
= fh
->dev
;
1856 struct video_device
*vdev
= video_devdata(file
);
1857 struct s2255_channel
*channel
= fh
->channel
;
1860 /* turn off stream */
1861 if (res_check(fh
)) {
1862 if (channel
->b_acquire
)
1863 s2255_stop_acquire(fh
->channel
);
1864 videobuf_streamoff(&fh
->vb_vidq
);
1867 videobuf_mmap_free(&fh
->vb_vidq
);
1868 dprintk(1, "%s (dev=%s)\n", __func__
, video_device_node_name(vdev
));
1873 static int s2255_mmap_v4l(struct file
*file
, struct vm_area_struct
*vma
)
1875 struct s2255_fh
*fh
= file
->private_data
;
1880 dprintk(4, "%s, vma=0x%08lx\n", __func__
, (unsigned long)vma
);
1881 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1882 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__
,
1883 (unsigned long)vma
->vm_start
,
1884 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
, ret
);
1888 static const struct v4l2_file_operations s2255_fops_v4l
= {
1889 .owner
= THIS_MODULE
,
1891 .release
= s2255_release
,
1893 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1894 .mmap
= s2255_mmap_v4l
,
1897 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1898 .vidioc_querymenu
= vidioc_querymenu
,
1899 .vidioc_querycap
= vidioc_querycap
,
1900 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1901 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1902 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1903 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1904 .vidioc_reqbufs
= vidioc_reqbufs
,
1905 .vidioc_querybuf
= vidioc_querybuf
,
1906 .vidioc_qbuf
= vidioc_qbuf
,
1907 .vidioc_dqbuf
= vidioc_dqbuf
,
1908 .vidioc_s_std
= vidioc_s_std
,
1909 .vidioc_enum_input
= vidioc_enum_input
,
1910 .vidioc_g_input
= vidioc_g_input
,
1911 .vidioc_s_input
= vidioc_s_input
,
1912 .vidioc_queryctrl
= vidioc_queryctrl
,
1913 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1914 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1915 .vidioc_streamon
= vidioc_streamon
,
1916 .vidioc_streamoff
= vidioc_streamoff
,
1917 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1918 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1919 .vidioc_s_parm
= vidioc_s_parm
,
1920 .vidioc_g_parm
= vidioc_g_parm
,
1921 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
1924 static void s2255_video_device_release(struct video_device
*vdev
)
1926 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1927 dprintk(4, "%s, chnls: %d \n", __func__
,
1928 atomic_read(&dev
->num_channels
));
1929 if (atomic_dec_and_test(&dev
->num_channels
))
1934 static struct video_device
template = {
1936 .fops
= &s2255_fops_v4l
,
1937 .ioctl_ops
= &s2255_ioctl_ops
,
1938 .release
= s2255_video_device_release
,
1939 .tvnorms
= S2255_NORMS
,
1940 .current_norm
= V4L2_STD_NTSC_M
,
1943 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1947 int cur_nr
= video_nr
;
1948 struct s2255_channel
*channel
;
1949 ret
= v4l2_device_register(&dev
->interface
->dev
, &dev
->v4l2_dev
);
1952 /* initialize all video 4 linux */
1953 /* register 4 video devices */
1954 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1955 channel
= &dev
->channel
[i
];
1956 INIT_LIST_HEAD(&channel
->vidq
.active
);
1957 channel
->vidq
.dev
= dev
;
1958 /* register 4 video devices */
1959 channel
->vdev
= template;
1960 channel
->vdev
.lock
= &dev
->lock
;
1961 channel
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1962 video_set_drvdata(&channel
->vdev
, channel
);
1964 ret
= video_register_device(&channel
->vdev
,
1968 ret
= video_register_device(&channel
->vdev
,
1973 dev_err(&dev
->udev
->dev
,
1974 "failed to register video device!\n");
1977 atomic_inc(&dev
->num_channels
);
1978 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1979 video_device_node_name(&channel
->vdev
));
1982 printk(KERN_INFO
"Sensoray 2255 V4L driver Revision: %d.%d\n",
1983 S2255_MAJOR_VERSION
,
1984 S2255_MINOR_VERSION
);
1985 /* if no channels registered, return error and probe will fail*/
1986 if (atomic_read(&dev
->num_channels
) == 0) {
1987 v4l2_device_unregister(&dev
->v4l2_dev
);
1990 if (atomic_read(&dev
->num_channels
) != MAX_CHANNELS
)
1991 printk(KERN_WARNING
"s2255: Not all channels available.\n");
1995 /* this function moves the usb stream read pipe data
1996 * into the system buffers.
1997 * returns 0 on success, EAGAIN if more data to process( call this
2000 * Received frame structure:
2001 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
2002 * bytes 4-7: channel: 0-3
2003 * bytes 8-11: payload size: size of the frame
2004 * bytes 12-payloadsize+12: frame data
2006 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
2012 unsigned long copy_size
;
2015 struct s2255_framei
*frm
;
2016 unsigned char *pdata
;
2017 struct s2255_channel
*channel
;
2018 dprintk(100, "buffer to user\n");
2019 channel
= &dev
->channel
[dev
->cc
];
2020 idx
= channel
->cur_frame
;
2021 frm
= &channel
->buffer
.frame
[idx
];
2022 if (frm
->ulState
== S2255_READ_IDLE
) {
2025 __le32
*pdword
; /*data from dsp is little endian */
2027 /* search for marker codes */
2028 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
2029 pdword
= (__le32
*)pdata
;
2030 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
2032 case S2255_MARKER_FRAME
:
2033 dprintk(4, "found frame marker at offset:"
2034 " %d [%x %x]\n", jj
, pdata
[0],
2036 offset
= jj
+ PREFIX_SIZE
;
2039 if (cc
>= MAX_CHANNELS
) {
2045 dev
->cc
= G_chnmap
[cc
];
2046 channel
= &dev
->channel
[dev
->cc
];
2047 payload
= pdword
[3];
2048 if (payload
> channel
->req_image_size
) {
2049 channel
->bad_payload
++;
2050 /* discard the bad frame */
2053 channel
->pkt_size
= payload
;
2054 channel
->jpg_size
= pdword
[4];
2056 case S2255_MARKER_RESPONSE
:
2058 pdata
+= DEF_USB_BLOCK
;
2059 jj
+= DEF_USB_BLOCK
;
2060 if (pdword
[1] >= MAX_CHANNELS
)
2062 cc
= G_chnmap
[pdword
[1]];
2063 if (cc
>= MAX_CHANNELS
)
2065 channel
= &dev
->channel
[cc
];
2066 switch (pdword
[2]) {
2067 case S2255_RESPONSE_SETMODE
:
2068 /* check if channel valid */
2069 /* set mode ready */
2070 channel
->setmode_ready
= 1;
2071 wake_up(&channel
->wait_setmode
);
2072 dprintk(5, "setmode ready %d\n", cc
);
2074 case S2255_RESPONSE_FW
:
2075 dev
->chn_ready
|= (1 << cc
);
2076 if ((dev
->chn_ready
& 0x0f) != 0x0f)
2078 /* all channels ready */
2079 printk(KERN_INFO
"s2255: fw loaded\n");
2080 atomic_set(&dev
->fw_data
->fw_state
,
2082 wake_up(&dev
->fw_data
->wait_fw
);
2084 case S2255_RESPONSE_STATUS
:
2085 channel
->vidstatus
= pdword
[3];
2086 channel
->vidstatus_ready
= 1;
2087 wake_up(&channel
->wait_vidstatus
);
2088 dprintk(5, "got vidstatus %x chan %d\n",
2092 printk(KERN_INFO
"s2255 unknown resp\n");
2104 channel
= &dev
->channel
[dev
->cc
];
2105 idx
= channel
->cur_frame
;
2106 frm
= &channel
->buffer
.frame
[idx
];
2107 /* search done. now find out if should be acquiring on this channel */
2108 if (!channel
->b_acquire
) {
2109 /* we found a frame, but this channel is turned off */
2110 frm
->ulState
= S2255_READ_IDLE
;
2114 if (frm
->ulState
== S2255_READ_IDLE
) {
2115 frm
->ulState
= S2255_READ_FRAME
;
2119 /* skip the marker 512 bytes (and offset if out of sync) */
2120 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
2123 if (frm
->lpvbits
== NULL
) {
2124 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2125 frm
, dev
, dev
->cc
, idx
);
2129 pdest
= frm
->lpvbits
+ frm
->cur_size
;
2131 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
2133 size
= channel
->pkt_size
- PREFIX_SIZE
;
2135 /* sanity check on pdest */
2136 if ((copy_size
+ frm
->cur_size
) < channel
->req_image_size
)
2137 memcpy(pdest
, psrc
, copy_size
);
2139 frm
->cur_size
+= copy_size
;
2140 dprintk(4, "cur_size size %lu size %lu \n", frm
->cur_size
, size
);
2142 if (frm
->cur_size
>= size
) {
2143 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2145 channel
->last_frame
= channel
->cur_frame
;
2146 channel
->cur_frame
++;
2147 /* end of system frame ring buffer, start at zero */
2148 if ((channel
->cur_frame
== SYS_FRAMES
) ||
2149 (channel
->cur_frame
== channel
->buffer
.dwFrames
))
2150 channel
->cur_frame
= 0;
2152 if (channel
->b_acquire
)
2153 s2255_got_frame(channel
, channel
->jpg_size
);
2154 channel
->frame_count
++;
2155 frm
->ulState
= S2255_READ_IDLE
;
2159 /* done successfully */
2163 static void s2255_read_video_callback(struct s2255_dev
*dev
,
2164 struct s2255_pipeinfo
*pipe_info
)
2167 dprintk(50, "callback read video \n");
2169 if (dev
->cc
>= MAX_CHANNELS
) {
2171 dev_err(&dev
->udev
->dev
, "invalid channel\n");
2174 /* otherwise copy to the system buffers */
2175 res
= save_frame(dev
, pipe_info
);
2177 dprintk(4, "s2255: read callback failed\n");
2179 dprintk(50, "callback read video done\n");
2183 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
2184 u16 Index
, u16 Value
, void *TransferBuffer
,
2185 s32 TransferBufferLength
, int bOut
)
2189 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
2191 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
2193 Value
, Index
, TransferBuffer
,
2194 TransferBufferLength
, HZ
* 5);
2196 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
2197 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2198 Value
, Index
, TransferBuffer
,
2199 TransferBufferLength
, HZ
* 5);
2205 * retrieve FX2 firmware version. future use.
2206 * @param dev pointer to device extension
2207 * @return -1 for fail, else returns firmware version as an int(16 bits)
2209 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
2213 unsigned char transBuffer
[64];
2214 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
2217 dprintk(2, "get fw error: %x\n", ret
);
2218 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
2219 dprintk(2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
2224 * Create the system ring buffer to copy frames into from the
2227 static int s2255_create_sys_buffers(struct s2255_channel
*channel
)
2230 unsigned long reqsize
;
2231 dprintk(1, "create sys buffers\n");
2232 channel
->buffer
.dwFrames
= SYS_FRAMES
;
2233 /* always allocate maximum size(PAL) for system buffers */
2234 reqsize
= SYS_FRAMES_MAXSIZE
;
2236 if (reqsize
> SYS_FRAMES_MAXSIZE
)
2237 reqsize
= SYS_FRAMES_MAXSIZE
;
2239 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2240 /* allocate the frames */
2241 channel
->buffer
.frame
[i
].lpvbits
= vmalloc(reqsize
);
2242 dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n",
2243 &channel
->buffer
.frame
[i
], channel
->idx
, i
,
2244 channel
->buffer
.frame
[i
].lpvbits
);
2245 channel
->buffer
.frame
[i
].size
= reqsize
;
2246 if (channel
->buffer
.frame
[i
].lpvbits
== NULL
) {
2247 printk(KERN_INFO
"out of memory. using less frames\n");
2248 channel
->buffer
.dwFrames
= i
;
2253 /* make sure internal states are set */
2254 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2255 channel
->buffer
.frame
[i
].ulState
= 0;
2256 channel
->buffer
.frame
[i
].cur_size
= 0;
2259 channel
->cur_frame
= 0;
2260 channel
->last_frame
= -1;
2264 static int s2255_release_sys_buffers(struct s2255_channel
*channel
)
2267 dprintk(1, "release sys buffers\n");
2268 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2269 if (channel
->buffer
.frame
[i
].lpvbits
) {
2270 dprintk(1, "vfree %p\n",
2271 channel
->buffer
.frame
[i
].lpvbits
);
2272 vfree(channel
->buffer
.frame
[i
].lpvbits
);
2274 channel
->buffer
.frame
[i
].lpvbits
= NULL
;
2279 static int s2255_board_init(struct s2255_dev
*dev
)
2281 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
2284 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2285 dprintk(4, "board init: %p", dev
);
2286 memset(pipe
, 0, sizeof(*pipe
));
2288 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
2289 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
2291 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
2293 if (pipe
->transfer_buffer
== NULL
) {
2294 dprintk(1, "out of memory!\n");
2297 /* query the firmware */
2298 fw_ver
= s2255_get_fx2fw(dev
);
2300 printk(KERN_INFO
"s2255: usb firmware version %d.%d\n",
2301 (fw_ver
>> 8) & 0xff,
2304 if (fw_ver
< S2255_CUR_USB_FWVER
)
2305 printk(KERN_INFO
"s2255: newer USB firmware available\n");
2307 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
2308 struct s2255_channel
*channel
= &dev
->channel
[j
];
2309 channel
->b_acquire
= 0;
2310 channel
->mode
= mode_def
;
2311 if (dev
->pid
== 0x2257 && j
> 1)
2312 channel
->mode
.color
|= (1 << 16);
2313 channel
->jc
.quality
= S2255_DEF_JPEG_QUAL
;
2314 channel
->width
= LINE_SZ_4CIFS_NTSC
;
2315 channel
->height
= NUM_LINES_4CIFS_NTSC
* 2;
2316 channel
->fmt
= &formats
[0];
2317 channel
->mode
.restart
= 1;
2318 channel
->req_image_size
= get_transfer_size(&mode_def
);
2319 channel
->frame_count
= 0;
2320 /* create the system buffers */
2321 s2255_create_sys_buffers(channel
);
2323 /* start read pipe */
2324 s2255_start_readpipe(dev
);
2325 dprintk(1, "%s: success\n", __func__
);
2329 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2332 dprintk(1, "%s: dev: %p", __func__
, dev
);
2334 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2335 if (dev
->channel
[i
].b_acquire
)
2336 s2255_stop_acquire(&dev
->channel
[i
]);
2338 s2255_stop_readpipe(dev
);
2339 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2340 s2255_release_sys_buffers(&dev
->channel
[i
]);
2341 /* release transfer buffer */
2342 kfree(dev
->pipe
.transfer_buffer
);
2346 static void read_pipe_completion(struct urb
*purb
)
2348 struct s2255_pipeinfo
*pipe_info
;
2349 struct s2255_dev
*dev
;
2352 pipe_info
= purb
->context
;
2353 dprintk(100, "%s: urb:%p, status %d\n", __func__
, purb
,
2355 if (pipe_info
== NULL
) {
2356 dev_err(&purb
->dev
->dev
, "no context!\n");
2360 dev
= pipe_info
->dev
;
2362 dev_err(&purb
->dev
->dev
, "no context!\n");
2365 status
= purb
->status
;
2366 /* if shutting down, do not resubmit, exit immediately */
2367 if (status
== -ESHUTDOWN
) {
2368 dprintk(2, "%s: err shutdown\n", __func__
);
2369 pipe_info
->err_count
++;
2373 if (pipe_info
->state
== 0) {
2374 dprintk(2, "%s: exiting USB pipe", __func__
);
2379 s2255_read_video_callback(dev
, pipe_info
);
2381 pipe_info
->err_count
++;
2382 dprintk(1, "%s: failed URB %d\n", __func__
, status
);
2385 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2387 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2389 pipe_info
->transfer_buffer
,
2390 pipe_info
->cur_transfer_size
,
2391 read_pipe_completion
, pipe_info
);
2393 if (pipe_info
->state
!= 0) {
2394 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_ATOMIC
)) {
2395 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2398 dprintk(2, "%s :complete state 0\n", __func__
);
2403 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2407 struct s2255_pipeinfo
*pipe_info
= &dev
->pipe
;
2408 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2409 dprintk(2, "%s: IN %d\n", __func__
, dev
->read_endpoint
);
2410 pipe_info
->state
= 1;
2411 pipe_info
->err_count
= 0;
2412 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2413 if (!pipe_info
->stream_urb
) {
2414 dev_err(&dev
->udev
->dev
,
2415 "ReadStream: Unable to alloc URB\n");
2418 /* transfer buffer allocated in board_init */
2419 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2421 pipe_info
->transfer_buffer
,
2422 pipe_info
->cur_transfer_size
,
2423 read_pipe_completion
, pipe_info
);
2424 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2426 printk(KERN_ERR
"s2255: start read pipe failed\n");
2432 /* starts acquisition process */
2433 static int s2255_start_acquire(struct s2255_channel
*channel
)
2435 unsigned char *buffer
;
2437 unsigned long chn_rev
;
2439 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
2440 chn_rev
= G_chnmap
[channel
->idx
];
2441 buffer
= kzalloc(512, GFP_KERNEL
);
2442 if (buffer
== NULL
) {
2443 dev_err(&dev
->udev
->dev
, "out of mem\n");
2447 channel
->last_frame
= -1;
2448 channel
->bad_payload
= 0;
2449 channel
->cur_frame
= 0;
2450 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2451 channel
->buffer
.frame
[j
].ulState
= 0;
2452 channel
->buffer
.frame
[j
].cur_size
= 0;
2455 /* send the start command */
2456 *(__le32
*) buffer
= IN_DATA_TOKEN
;
2457 *((__le32
*) buffer
+ 1) = (__le32
) cpu_to_le32(chn_rev
);
2458 *((__le32
*) buffer
+ 2) = CMD_START
;
2459 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2461 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2463 dprintk(2, "start acquire exit[%d] %d \n", channel
->idx
, res
);
2468 static int s2255_stop_acquire(struct s2255_channel
*channel
)
2470 unsigned char *buffer
;
2472 unsigned long chn_rev
;
2473 struct s2255_dev
*dev
= to_s2255_dev(channel
->vdev
.v4l2_dev
);
2474 chn_rev
= G_chnmap
[channel
->idx
];
2475 buffer
= kzalloc(512, GFP_KERNEL
);
2476 if (buffer
== NULL
) {
2477 dev_err(&dev
->udev
->dev
, "out of mem\n");
2480 /* send the stop command */
2481 *(__le32
*) buffer
= IN_DATA_TOKEN
;
2482 *((__le32
*) buffer
+ 1) = (__le32
) cpu_to_le32(chn_rev
);
2483 *((__le32
*) buffer
+ 2) = CMD_STOP
;
2484 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2486 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2488 channel
->b_acquire
= 0;
2489 dprintk(4, "%s: chn %d, res %d\n", __func__
, channel
->idx
, res
);
2493 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2495 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2498 if (pipe
->stream_urb
) {
2500 usb_kill_urb(pipe
->stream_urb
);
2501 usb_free_urb(pipe
->stream_urb
);
2502 pipe
->stream_urb
= NULL
;
2504 dprintk(4, "%s", __func__
);
2508 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
)
2511 s2255_reset_dsppower(dev
);
2512 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2513 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2514 memcpy(dev
->fw_data
->pfw_data
,
2515 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2516 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2517 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2518 usb_sndbulkpipe(dev
->udev
, 2),
2519 dev
->fw_data
->pfw_data
,
2520 CHUNK_SIZE
, s2255_fwchunk_complete
,
2522 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2525 /* standard usb probe function */
2526 static int s2255_probe(struct usb_interface
*interface
,
2527 const struct usb_device_id
*id
)
2529 struct s2255_dev
*dev
= NULL
;
2530 struct usb_host_interface
*iface_desc
;
2531 struct usb_endpoint_descriptor
*endpoint
;
2533 int retval
= -ENOMEM
;
2536 dprintk(2, "%s\n", __func__
);
2537 /* allocate memory for our device state and initialize it to zero */
2538 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2540 s2255_dev_err(&interface
->dev
, "out of memory\n");
2543 atomic_set(&dev
->num_channels
, 0);
2544 dev
->pid
= id
->idProduct
;
2545 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2548 mutex_init(&dev
->lock
);
2549 mutex_init(&dev
->open_lock
);
2550 /* grab usb_device and save it */
2551 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2552 if (dev
->udev
== NULL
) {
2553 dev_err(&interface
->dev
, "null usb device\n");
2557 dprintk(1, "dev: %p, udev %p interface %p\n", dev
,
2558 dev
->udev
, interface
);
2559 dev
->interface
= interface
;
2560 /* set up the endpoint information */
2561 iface_desc
= interface
->cur_altsetting
;
2562 dprintk(1, "num endpoints %d\n", iface_desc
->desc
.bNumEndpoints
);
2563 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2564 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2565 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2566 /* we found the bulk in endpoint */
2567 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2571 if (!dev
->read_endpoint
) {
2572 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2575 init_timer(&dev
->timer
);
2576 dev
->timer
.function
= s2255_timer
;
2577 dev
->timer
.data
= (unsigned long)dev
->fw_data
;
2578 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2579 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2580 struct s2255_channel
*channel
= &dev
->channel
[i
];
2581 dev
->channel
[i
].idx
= i
;
2582 init_waitqueue_head(&channel
->wait_setmode
);
2583 init_waitqueue_head(&channel
->wait_vidstatus
);
2586 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2587 if (!dev
->fw_data
->fw_urb
) {
2588 dev_err(&interface
->dev
, "out of memory!\n");
2592 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2593 if (!dev
->fw_data
->pfw_data
) {
2594 dev_err(&interface
->dev
, "out of memory!\n");
2597 /* load the first chunk */
2598 if (request_firmware(&dev
->fw_data
->fw
,
2599 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2600 printk(KERN_ERR
"sensoray 2255 failed to get firmware\n");
2603 /* check the firmware is valid */
2604 fw_size
= dev
->fw_data
->fw
->size
;
2605 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2607 if (*pdata
!= S2255_FW_MARKER
) {
2608 printk(KERN_INFO
"Firmware invalid.\n");
2612 /* make sure firmware is the latest */
2614 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2615 printk(KERN_INFO
"s2255 dsp fw version %x\n", *pRel
);
2616 dev
->dsp_fw_ver
= *pRel
;
2617 if (*pRel
< S2255_CUR_DSP_FWVER
)
2618 printk(KERN_INFO
"s2255: f2255usb.bin out of date.\n");
2619 if (dev
->pid
== 0x2257 && *pRel
< S2255_MIN_DSP_COLORFILTER
)
2620 printk(KERN_WARNING
"s2255: 2257 requires firmware %d"
2621 " or above.\n", S2255_MIN_DSP_COLORFILTER
);
2623 usb_reset_device(dev
->udev
);
2624 /* load 2255 board specific */
2625 retval
= s2255_board_init(dev
);
2627 goto errorBOARDINIT
;
2628 spin_lock_init(&dev
->slock
);
2629 s2255_fwload_start(dev
, 0);
2630 /* loads v4l specific */
2631 retval
= s2255_probe_v4l(dev
);
2633 goto errorBOARDINIT
;
2634 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2637 s2255_board_shutdown(dev
);
2639 release_firmware(dev
->fw_data
->fw
);
2641 kfree(dev
->fw_data
->pfw_data
);
2643 usb_free_urb(dev
->fw_data
->fw_urb
);
2645 del_timer(&dev
->timer
);
2647 usb_put_dev(dev
->udev
);
2649 kfree(dev
->fw_data
);
2650 mutex_destroy(&dev
->open_lock
);
2651 mutex_destroy(&dev
->lock
);
2654 printk(KERN_WARNING
"Sensoray 2255 driver load failed: 0x%x\n", retval
);
2658 /* disconnect routine. when board is removed physically or with rmmod */
2659 static void s2255_disconnect(struct usb_interface
*interface
)
2661 struct s2255_dev
*dev
= to_s2255_dev(usb_get_intfdata(interface
));
2663 int channels
= atomic_read(&dev
->num_channels
);
2664 mutex_lock(&dev
->lock
);
2665 v4l2_device_disconnect(&dev
->v4l2_dev
);
2666 mutex_unlock(&dev
->lock
);
2667 /*see comments in the uvc_driver.c usb disconnect function */
2668 atomic_inc(&dev
->num_channels
);
2669 /* unregister each video device. */
2670 for (i
= 0; i
< channels
; i
++)
2671 video_unregister_device(&dev
->channel
[i
].vdev
);
2672 /* wake up any of our timers */
2673 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2674 wake_up(&dev
->fw_data
->wait_fw
);
2675 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2676 dev
->channel
[i
].setmode_ready
= 1;
2677 wake_up(&dev
->channel
[i
].wait_setmode
);
2678 dev
->channel
[i
].vidstatus_ready
= 1;
2679 wake_up(&dev
->channel
[i
].wait_vidstatus
);
2681 if (atomic_dec_and_test(&dev
->num_channels
))
2683 dev_info(&interface
->dev
, "%s\n", __func__
);
2686 static struct usb_driver s2255_driver
= {
2687 .name
= S2255_DRIVER_NAME
,
2688 .probe
= s2255_probe
,
2689 .disconnect
= s2255_disconnect
,
2690 .id_table
= s2255_table
,
2693 static int __init
usb_s2255_init(void)
2696 /* register this driver with the USB subsystem */
2697 result
= usb_register(&s2255_driver
);
2699 pr_err(KBUILD_MODNAME
2700 ": usb_register failed. Error number %d\n", result
);
2701 dprintk(2, "%s\n", __func__
);
2705 static void __exit
usb_s2255_exit(void)
2707 usb_deregister(&s2255_driver
);
2710 module_init(usb_s2255_init
);
2711 module_exit(usb_s2255_exit
);
2713 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2714 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2715 MODULE_LICENSE("GPL");