2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2014 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>
46 #include <linux/vmalloc.h>
47 #include <linux/usb.h>
48 #include <media/videobuf2-vmalloc.h>
49 #include <media/v4l2-common.h>
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/v4l2-ctrls.h>
53 #include <media/v4l2-event.h>
55 #define S2255_VERSION "1.25.1"
56 #define FIRMWARE_FILE_NAME "f2255usb.bin"
58 /* default JPEG quality */
59 #define S2255_DEF_JPEG_QUAL 50
60 /* vendor request in */
62 /* vendor request out */
63 #define S2255_VR_OUT 1
65 #define S2255_VR_FW 0x30
66 /* USB endpoint number for configuring the device */
67 #define S2255_CONFIG_EP 2
68 /* maximum time for DSP to start responding after last FW word loaded(ms) */
69 #define S2255_DSP_BOOTTIME 800
70 /* maximum time to wait for firmware to load (ms) */
71 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
72 #define S2255_MIN_BUFS 2
73 #define S2255_SETMODE_TIMEOUT 500
74 #define S2255_VIDSTATUS_TIMEOUT 350
75 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
76 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
77 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
78 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
79 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
80 #define S2255_USB_XFER_SIZE (16 * 1024)
81 #define MAX_CHANNELS 4
83 /* maximum size is PAL full size plus room for the marker header(s) */
84 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
85 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
86 #define LINE_SZ_4CIFS_NTSC 640
87 #define LINE_SZ_2CIFS_NTSC 640
88 #define LINE_SZ_1CIFS_NTSC 320
89 #define LINE_SZ_4CIFS_PAL 704
90 #define LINE_SZ_2CIFS_PAL 704
91 #define LINE_SZ_1CIFS_PAL 352
92 #define NUM_LINES_4CIFS_NTSC 240
93 #define NUM_LINES_2CIFS_NTSC 240
94 #define NUM_LINES_1CIFS_NTSC 240
95 #define NUM_LINES_4CIFS_PAL 288
96 #define NUM_LINES_2CIFS_PAL 288
97 #define NUM_LINES_1CIFS_PAL 288
98 #define LINE_SZ_DEF 640
99 #define NUM_LINES_DEF 240
102 /* predefined settings */
103 #define FORMAT_NTSC 1
106 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
107 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
108 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
109 /* SCALE_4CIFSI is the 2 fields interpolated into one */
110 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
112 #define COLOR_YUVPL 1 /* YUV planar */
113 #define COLOR_YUVPK 2 /* YUV packed */
114 #define COLOR_Y8 4 /* monochrome */
115 #define COLOR_JPG 5 /* JPEG */
117 #define MASK_COLOR 0x000000ff
118 #define MASK_JPG_QUALITY 0x0000ff00
119 #define MASK_INPUT_TYPE 0x000f0000
120 /* frame decimation. */
121 #define FDEC_1 1 /* capture every frame. default */
122 #define FDEC_2 2 /* capture every 2nd frame */
123 #define FDEC_3 3 /* capture every 3rd frame */
124 #define FDEC_5 5 /* capture every 5th frame */
126 /*-------------------------------------------------------
127 * Default mode parameters.
128 *-------------------------------------------------------*/
129 #define DEF_SCALE SCALE_4CIFS
130 #define DEF_COLOR COLOR_YUVPL
131 #define DEF_FDEC FDEC_1
133 #define DEF_CONTRAST 0x5c
134 #define DEF_SATURATION 0x80
137 /* usb config commands */
138 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
139 #define CMD_2255 0xc2255000
140 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
141 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
142 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
143 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
146 u32 format
; /* input video format (NTSC, PAL) */
147 u32 scale
; /* output video scale */
148 u32 color
; /* output video color format */
149 u32 fdec
; /* frame decimation */
150 u32 bright
; /* brightness */
151 u32 contrast
; /* contrast */
152 u32 saturation
; /* saturation */
153 u32 hue
; /* hue (NTSC only)*/
154 u32 single
; /* capture 1 frame at a time (!=0), continuously (==0)*/
155 u32 usb_block
; /* block size. should be 4096 of DEF_USB_BLOCK */
156 u32 restart
; /* if DSP requires restart */
160 #define S2255_READ_IDLE 0
161 #define S2255_READ_FRAME 1
163 /* frame structure */
164 struct s2255_framei
{
166 unsigned long ulState
; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
167 void *lpvbits
; /* image data */
168 unsigned long cur_size
; /* current data copied to it */
171 /* image buffer structure */
172 struct s2255_bufferi
{
173 unsigned long dwFrames
; /* number of frames in buffer */
174 struct s2255_framei frame
[SYS_FRAMES
]; /* array of FRAME structures */
177 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
178 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
179 DEF_HUE, 0, DEF_USB_BLOCK, 0}
181 /* for firmware loading, fw_state */
182 #define S2255_FW_NOTLOADED 0
183 #define S2255_FW_LOADED_DSPWAIT 1
184 #define S2255_FW_SUCCESS 2
185 #define S2255_FW_FAILED 3
186 #define S2255_FW_DISCONNECTING 4
187 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
188 /* 2255 read states */
189 #define S2255_READ_IDLE 0
190 #define S2255_READ_FRAME 1
197 wait_queue_head_t wait_fw
;
198 const struct firmware
*fw
;
201 struct s2255_pipeinfo
{
202 u32 max_transfer_size
;
203 u32 cur_transfer_size
;
207 void *dev
; /* back pointer to s2255_dev struct*/
212 struct s2255_fmt
; /*forward declaration */
215 /* 2255 video channel */
217 struct s2255_dev
*dev
;
218 struct video_device vdev
;
219 struct v4l2_ctrl_handler hdl
;
220 struct v4l2_ctrl
*jpegqual_ctrl
;
222 struct list_head buf_list
;
223 struct s2255_bufferi buffer
;
224 struct s2255_mode mode
;
226 /* jpeg compression */
228 /* capture parameters (for high quality mode full size) */
229 struct v4l2_captureparm cap_parm
;
232 /* allocated image size */
233 unsigned long req_image_size
;
234 /* received packet size */
235 unsigned long pkt_size
;
237 unsigned long frame_count
;
240 /* if channel configured to default state */
242 wait_queue_head_t wait_setmode
;
244 /* video status items */
246 wait_queue_head_t wait_vidstatus
;
250 enum v4l2_field field
;
251 const struct s2255_fmt
*fmt
;
252 int idx
; /* channel number on device, 0-3 */
253 struct vb2_queue vb_vidq
;
254 struct mutex vb_lock
; /* streaming lock */
260 struct s2255_vc vc
[MAX_CHANNELS
];
261 struct v4l2_device v4l2_dev
;
262 atomic_t num_channels
;
264 struct mutex lock
; /* channels[].vdev.lock */
265 struct mutex cmdlock
; /* protects cmdbuf */
266 struct usb_device
*udev
;
267 struct usb_interface
*interface
;
269 struct timer_list timer
;
270 struct s2255_fw
*fw_data
;
271 struct s2255_pipeinfo pipe
;
272 u32 cc
; /* current channel */
275 /* dsp firmware version (f2255usb.bin) */
277 u16 pid
; /* product id */
278 #define S2255_CMDBUF_SIZE 512
282 static inline struct s2255_dev
*to_s2255_dev(struct v4l2_device
*v4l2_dev
)
284 return container_of(v4l2_dev
, struct s2255_dev
, v4l2_dev
);
293 /* buffer for one video frame */
294 struct s2255_buffer
{
295 /* common v4l buffer stuff -- must be first */
296 struct vb2_buffer vb
;
297 struct list_head list
;
301 /* current cypress EEPROM firmware version */
302 #define S2255_CUR_USB_FWVER ((3 << 8) | 12)
303 /* current DSP FW version */
304 #define S2255_CUR_DSP_FWVER 10104
305 /* Need DSP version 5+ for video status feature */
306 #define S2255_MIN_DSP_STATUS 5
307 #define S2255_MIN_DSP_COLORFILTER 8
308 #define S2255_NORMS (V4L2_STD_ALL)
310 /* private V4L2 controls */
313 * The following chart displays how COLORFILTER should be set
314 * =========================================================
315 * = fourcc = COLORFILTER =
316 * = ===============================
318 * =========================================================
319 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
320 * = = s-video or = composite =
321 * = = B/W camera = input =
322 * =========================================================
323 * = other = color, svideo = color, =
325 * =========================================================
328 * channels 0-3 on 2255 are composite
329 * channels 0-1 on 2257 are composite, 2-3 are s-video
330 * If COLORFILTER is 0 with a composite color camera connected,
331 * the output will appear monochrome but hatching
333 * COLORFILTER is different from "color killer" and "color effects"
336 #define S2255_V4L2_YC_ON 1
337 #define S2255_V4L2_YC_OFF 0
338 #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0)
340 /* frame prefix size (sent once every frame) */
341 #define PREFIX_SIZE 512
343 /* Channels on box are in reverse order */
344 static unsigned long G_chnmap
[MAX_CHANNELS
] = {3, 2, 1, 0};
348 static int s2255_start_readpipe(struct s2255_dev
*dev
);
349 static void s2255_stop_readpipe(struct s2255_dev
*dev
);
350 static int s2255_start_acquire(struct s2255_vc
*vc
);
351 static int s2255_stop_acquire(struct s2255_vc
*vc
);
352 static void s2255_fillbuff(struct s2255_vc
*vc
, struct s2255_buffer
*buf
,
354 static int s2255_set_mode(struct s2255_vc
*vc
, struct s2255_mode
*mode
);
355 static int s2255_board_shutdown(struct s2255_dev
*dev
);
356 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
);
357 static void s2255_destroy(struct s2255_dev
*dev
);
358 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char req
,
359 u16 index
, u16 value
, void *buf
,
360 s32 buf_len
, int bOut
);
362 /* dev_err macro with driver name */
363 #define S2255_DRIVER_NAME "s2255"
364 #define s2255_dev_err(dev, fmt, arg...) \
365 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
367 #define dprintk(dev, level, fmt, arg...) \
368 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
370 static struct usb_driver s2255_driver
;
372 /* start video number */
373 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
375 /* Enable jpeg capture. */
376 static int jpeg_enable
= 1;
378 module_param(debug
, int, 0644);
379 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
380 module_param(video_nr
, int, 0644);
381 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
382 module_param(jpeg_enable
, int, 0644);
383 MODULE_PARM_DESC(jpeg_enable
, "Jpeg enable(1-on 0-off) default 1");
385 /* USB device table */
386 #define USB_SENSORAY_VID 0x1943
387 static struct usb_device_id s2255_table
[] = {
388 {USB_DEVICE(USB_SENSORAY_VID
, 0x2255)},
389 {USB_DEVICE(USB_SENSORAY_VID
, 0x2257)}, /*same family as 2255*/
390 { } /* Terminating entry */
392 MODULE_DEVICE_TABLE(usb
, s2255_table
);
394 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
397 /* JPEG formats must be defined last to support jpeg_enable parameter */
398 static const struct s2255_fmt formats
[] = {
400 .name
= "4:2:2, packed, YUYV",
401 .fourcc
= V4L2_PIX_FMT_YUYV
,
405 .name
= "4:2:2, packed, UYVY",
406 .fourcc
= V4L2_PIX_FMT_UYVY
,
409 .name
= "4:2:2, planar, YUV422P",
410 .fourcc
= V4L2_PIX_FMT_YUV422P
,
415 .fourcc
= V4L2_PIX_FMT_GREY
,
419 .fourcc
= V4L2_PIX_FMT_JPEG
,
423 .fourcc
= V4L2_PIX_FMT_MJPEG
,
428 static int norm_maxw(struct s2255_vc
*vc
)
430 return (vc
->std
& V4L2_STD_525_60
) ?
431 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
434 static int norm_maxh(struct s2255_vc
*vc
)
436 return (vc
->std
& V4L2_STD_525_60
) ?
437 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
440 static int norm_minw(struct s2255_vc
*vc
)
442 return (vc
->std
& V4L2_STD_525_60
) ?
443 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
446 static int norm_minh(struct s2255_vc
*vc
)
448 return (vc
->std
& V4L2_STD_525_60
) ?
449 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
454 * TODO: fixme: move YUV reordering to hardware
455 * converts 2255 planar format to yuyv or uyvy
457 static void planar422p_to_yuv_packed(const unsigned char *in
,
459 int width
, int height
,
465 unsigned long size
= height
* width
;
467 pY
= (unsigned char *)in
;
468 pCr
= (unsigned char *)in
+ height
* width
;
469 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
470 for (i
= 0; i
< size
* 2; i
+= 4) {
471 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
472 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
473 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
474 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
479 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
481 s2255_vendor_req(dev
, 0x40, 0x0000, 0x0001, NULL
, 0, 1);
483 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
485 s2255_vendor_req(dev
, 0x10, 0x0000, 0x0000, NULL
, 0, 1);
489 /* kickstarts the firmware loading. from probe
491 static void s2255_timer(unsigned long user_data
)
493 struct s2255_fw
*data
= (struct s2255_fw
*)user_data
;
494 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
495 pr_err("s2255: can't submit urb\n");
496 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
497 /* wake up anything waiting for the firmware */
498 wake_up(&data
->wait_fw
);
504 /* this loads the firmware asynchronously.
505 Originally this was done synchronously in probe.
506 But it is better to load it asynchronously here than block
507 inside the probe function. Blocking inside probe affects boot time.
508 FW loading is triggered by the timer in the probe function
510 static void s2255_fwchunk_complete(struct urb
*urb
)
512 struct s2255_fw
*data
= urb
->context
;
513 struct usb_device
*udev
= urb
->dev
;
516 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
517 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
518 /* wake up anything waiting for the firmware */
519 wake_up(&data
->wait_fw
);
522 if (data
->fw_urb
== NULL
) {
523 s2255_dev_err(&udev
->dev
, "disconnected\n");
524 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
525 /* wake up anything waiting for the firmware */
526 wake_up(&data
->wait_fw
);
529 #define CHUNK_SIZE 512
530 /* all USB transfers must be done with continuous kernel memory.
531 can't allocate more than 128k in current linux kernel, so
532 upload the firmware in chunks
534 if (data
->fw_loaded
< data
->fw_size
) {
535 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
536 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
538 if (len
< CHUNK_SIZE
)
539 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
541 memcpy(data
->pfw_data
,
542 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
544 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
545 data
->pfw_data
, CHUNK_SIZE
,
546 s2255_fwchunk_complete
, data
);
547 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
548 dev_err(&udev
->dev
, "failed submit URB\n");
549 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
550 /* wake up anything waiting for the firmware */
551 wake_up(&data
->wait_fw
);
554 data
->fw_loaded
+= len
;
556 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
561 static int s2255_got_frame(struct s2255_vc
*vc
, int jpgsize
)
563 struct s2255_buffer
*buf
;
564 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
565 unsigned long flags
= 0;
567 spin_lock_irqsave(&vc
->qlock
, flags
);
568 if (list_empty(&vc
->buf_list
)) {
569 dprintk(dev
, 1, "No active queue to serve\n");
573 buf
= list_entry(vc
->buf_list
.next
,
574 struct s2255_buffer
, list
);
575 list_del(&buf
->list
);
576 v4l2_get_timestamp(&buf
->vb
.v4l2_buf
.timestamp
);
577 s2255_fillbuff(vc
, buf
, jpgsize
);
578 dprintk(dev
, 2, "%s: [buf] [%p]\n", __func__
, buf
);
580 spin_unlock_irqrestore(&vc
->qlock
, flags
);
584 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
587 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
588 if (-1 == formats
[i
].fourcc
)
590 if (!jpeg_enable
&& ((formats
[i
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
591 (formats
[i
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
593 if (formats
[i
].fourcc
== fourcc
)
599 /* video buffer vmalloc implementation based partly on VIVI driver which is
600 * Copyright (c) 2006 by
601 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
602 * Ted Walther <ted--a.t--enumera.com>
603 * John Sokol <sokol--a.t--videotechnology.com>
604 * http://v4l.videotechnology.com/
607 static void s2255_fillbuff(struct s2255_vc
*vc
,
608 struct s2255_buffer
*buf
, int jpgsize
)
612 char *vbuf
= vb2_plane_vaddr(&buf
->vb
, 0);
613 unsigned long last_frame
;
614 struct s2255_dev
*dev
= vc
->dev
;
618 last_frame
= vc
->last_frame
;
619 if (last_frame
!= -1) {
621 (const char *)vc
->buffer
.frame
[last_frame
].lpvbits
;
622 switch (vc
->fmt
->fourcc
) {
623 case V4L2_PIX_FMT_YUYV
:
624 case V4L2_PIX_FMT_UYVY
:
625 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
630 case V4L2_PIX_FMT_GREY
:
631 memcpy(vbuf
, tmpbuf
, vc
->width
* vc
->height
);
633 case V4L2_PIX_FMT_JPEG
:
634 case V4L2_PIX_FMT_MJPEG
:
635 buf
->vb
.v4l2_buf
.length
= jpgsize
;
636 memcpy(vbuf
, tmpbuf
, jpgsize
);
638 case V4L2_PIX_FMT_YUV422P
:
640 vc
->width
* vc
->height
* 2);
643 pr_info("s2255: unknown format?\n");
647 pr_err("s2255: =======no frame\n");
650 dprintk(dev
, 2, "s2255fill at : Buffer 0x%08lx size= %d\n",
651 (unsigned long)vbuf
, pos
);
652 /* tell v4l buffer was filled */
653 buf
->vb
.v4l2_buf
.field
= vc
->field
;
654 buf
->vb
.v4l2_buf
.sequence
= vc
->frame_count
;
655 v4l2_get_timestamp(&buf
->vb
.v4l2_buf
.timestamp
);
656 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_DONE
);
660 /* ------------------------------------------------------------------
662 ------------------------------------------------------------------*/
664 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
665 unsigned int *nbuffers
, unsigned int *nplanes
,
666 unsigned int sizes
[], void *alloc_ctxs
[])
668 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
669 if (*nbuffers
< S2255_MIN_BUFS
)
670 *nbuffers
= S2255_MIN_BUFS
;
672 sizes
[0] = vc
->width
* vc
->height
* (vc
->fmt
->depth
>> 3);
676 static int buffer_prepare(struct vb2_buffer
*vb
)
678 struct s2255_vc
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
679 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
684 dprintk(vc
->dev
, 4, "%s\n", __func__
);
688 if ((w
< norm_minw(vc
)) ||
689 (w
> norm_maxw(vc
)) ||
690 (h
< norm_minh(vc
)) ||
691 (h
> norm_maxh(vc
))) {
692 dprintk(vc
->dev
, 4, "invalid buffer prepare\n");
695 size
= w
* h
* (vc
->fmt
->depth
>> 3);
696 if (vb2_plane_size(vb
, 0) < size
) {
697 dprintk(vc
->dev
, 4, "invalid buffer prepare\n");
701 vb2_set_plane_payload(&buf
->vb
, 0, size
);
705 static void buffer_queue(struct vb2_buffer
*vb
)
707 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
708 struct s2255_vc
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
709 unsigned long flags
= 0;
710 dprintk(vc
->dev
, 1, "%s\n", __func__
);
711 spin_lock_irqsave(&vc
->qlock
, flags
);
712 list_add_tail(&buf
->list
, &vc
->buf_list
);
713 spin_unlock_irqrestore(&vc
->qlock
, flags
);
716 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
);
717 static int stop_streaming(struct vb2_queue
*vq
);
719 static struct vb2_ops s2255_video_qops
= {
720 .queue_setup
= queue_setup
,
721 .buf_prepare
= buffer_prepare
,
722 .buf_queue
= buffer_queue
,
723 .start_streaming
= start_streaming
,
724 .stop_streaming
= stop_streaming
,
725 .wait_prepare
= vb2_ops_wait_prepare
,
726 .wait_finish
= vb2_ops_wait_finish
,
729 static int vidioc_querycap(struct file
*file
, void *priv
,
730 struct v4l2_capability
*cap
)
732 struct s2255_vc
*vc
= video_drvdata(file
);
733 struct s2255_dev
*dev
= vc
->dev
;
735 strlcpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
736 strlcpy(cap
->card
, "s2255", sizeof(cap
->card
));
737 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
738 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
740 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
744 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
745 struct v4l2_fmtdesc
*f
)
747 int index
= f
->index
;
749 if (index
>= ARRAY_SIZE(formats
))
751 if (!jpeg_enable
&& ((formats
[index
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
752 (formats
[index
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
754 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
755 f
->pixelformat
= formats
[index
].fourcc
;
759 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
760 struct v4l2_format
*f
)
762 struct s2255_vc
*vc
= video_drvdata(file
);
763 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
765 f
->fmt
.pix
.width
= vc
->width
;
766 f
->fmt
.pix
.height
= vc
->height
;
767 if (f
->fmt
.pix
.height
>=
768 (is_ntsc
? NUM_LINES_1CIFS_NTSC
: NUM_LINES_1CIFS_PAL
) * 2)
769 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
771 f
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
772 f
->fmt
.pix
.pixelformat
= vc
->fmt
->fourcc
;
773 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (vc
->fmt
->depth
>> 3);
774 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
775 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
780 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
781 struct v4l2_format
*f
)
783 const struct s2255_fmt
*fmt
;
784 enum v4l2_field field
;
785 struct s2255_vc
*vc
= video_drvdata(file
);
786 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
788 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
793 field
= f
->fmt
.pix
.field
;
795 dprintk(vc
->dev
, 50, "%s NTSC: %d suggested width: %d, height: %d\n",
796 __func__
, is_ntsc
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
799 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
800 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
801 field
= V4L2_FIELD_INTERLACED
;
803 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
804 field
= V4L2_FIELD_TOP
;
806 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
807 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
808 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_NTSC
)
809 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_NTSC
;
810 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_NTSC
)
811 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
813 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
816 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
817 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
818 field
= V4L2_FIELD_INTERLACED
;
820 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
821 field
= V4L2_FIELD_TOP
;
823 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
)
824 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
825 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_PAL
)
826 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_PAL
;
827 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_PAL
)
828 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
830 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
832 f
->fmt
.pix
.field
= field
;
833 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
834 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
835 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
837 dprintk(vc
->dev
, 50, "%s: set width %d height %d field %d\n", __func__
,
838 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
842 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
843 struct v4l2_format
*f
)
845 struct s2255_vc
*vc
= video_drvdata(file
);
846 const struct s2255_fmt
*fmt
;
847 struct vb2_queue
*q
= &vc
->vb_vidq
;
848 struct s2255_mode mode
;
851 ret
= vidioc_try_fmt_vid_cap(file
, vc
, f
);
856 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
861 if (vb2_is_busy(q
)) {
862 dprintk(vc
->dev
, 1, "queue busy\n");
868 vc
->width
= f
->fmt
.pix
.width
;
869 vc
->height
= f
->fmt
.pix
.height
;
870 vc
->field
= f
->fmt
.pix
.field
;
871 if (vc
->width
> norm_minw(vc
)) {
872 if (vc
->height
> norm_minh(vc
)) {
873 if (vc
->cap_parm
.capturemode
&
874 V4L2_MODE_HIGHQUALITY
)
875 mode
.scale
= SCALE_4CIFSI
;
877 mode
.scale
= SCALE_4CIFS
;
879 mode
.scale
= SCALE_2CIFS
;
882 mode
.scale
= SCALE_1CIFS
;
885 switch (vc
->fmt
->fourcc
) {
886 case V4L2_PIX_FMT_GREY
:
887 mode
.color
&= ~MASK_COLOR
;
888 mode
.color
|= COLOR_Y8
;
890 case V4L2_PIX_FMT_JPEG
:
891 case V4L2_PIX_FMT_MJPEG
:
892 mode
.color
&= ~MASK_COLOR
;
893 mode
.color
|= COLOR_JPG
;
894 mode
.color
|= (vc
->jpegqual
<< 8);
896 case V4L2_PIX_FMT_YUV422P
:
897 mode
.color
&= ~MASK_COLOR
;
898 mode
.color
|= COLOR_YUVPL
;
900 case V4L2_PIX_FMT_YUYV
:
901 case V4L2_PIX_FMT_UYVY
:
903 mode
.color
&= ~MASK_COLOR
;
904 mode
.color
|= COLOR_YUVPK
;
907 if ((mode
.color
& MASK_COLOR
) != (vc
->mode
.color
& MASK_COLOR
))
909 else if (mode
.scale
!= vc
->mode
.scale
)
911 else if (mode
.format
!= vc
->mode
.format
)
914 (void) s2255_set_mode(vc
, &mode
);
919 /* write to the configuration pipe, synchronously */
920 static int s2255_write_config(struct usb_device
*udev
, unsigned char *pbuf
,
927 pipe
= usb_sndbulkpipe(udev
, S2255_CONFIG_EP
);
928 retval
= usb_bulk_msg(udev
, pipe
, pbuf
, size
, &done
, 500);
933 static u32
get_transfer_size(struct s2255_mode
*mode
)
935 int linesPerFrame
= LINE_SZ_DEF
;
936 int pixelsPerLine
= NUM_LINES_DEF
;
939 unsigned int mask_mult
;
944 if (mode
->format
== FORMAT_NTSC
) {
945 switch (mode
->scale
) {
948 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
949 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
952 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
953 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
956 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
957 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
962 } else if (mode
->format
== FORMAT_PAL
) {
963 switch (mode
->scale
) {
966 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
967 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
970 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
971 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
974 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
975 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
981 outImageSize
= linesPerFrame
* pixelsPerLine
;
982 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
983 /* 2 bytes/pixel if not monochrome */
987 /* total bytes to send including prefix and 4K padding;
988 must be a multiple of USB_READ_SIZE */
989 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
990 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
991 /* if size not a multiple of USB_READ_SIZE */
992 if (usbInSize
& ~mask_mult
)
993 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
997 static void s2255_print_cfg(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
999 struct device
*dev
= &sdev
->udev
->dev
;
1000 dev_info(dev
, "------------------------------------------------\n");
1001 dev_info(dev
, "format: %d\nscale %d\n", mode
->format
, mode
->scale
);
1002 dev_info(dev
, "fdec: %d\ncolor %d\n", mode
->fdec
, mode
->color
);
1003 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
1004 dev_info(dev
, "------------------------------------------------\n");
1008 * set mode is the function which controls the DSP.
1009 * the restart parameter in struct s2255_mode should be set whenever
1010 * the image size could change via color format, video system or image
1012 * When the restart parameter is set, we sleep for ONE frame to allow the
1013 * DSP time to get the new frame
1015 static int s2255_set_mode(struct s2255_vc
*vc
,
1016 struct s2255_mode
*mode
)
1019 unsigned long chn_rev
;
1020 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
1022 __le32
*buffer
= dev
->cmdbuf
;
1024 mutex_lock(&dev
->cmdlock
);
1025 chn_rev
= G_chnmap
[vc
->idx
];
1026 dprintk(dev
, 3, "%s channel: %d\n", __func__
, vc
->idx
);
1027 /* if JPEG, set the quality */
1028 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
) {
1029 mode
->color
&= ~MASK_COLOR
;
1030 mode
->color
|= COLOR_JPG
;
1031 mode
->color
&= ~MASK_JPG_QUALITY
;
1032 mode
->color
|= (vc
->jpegqual
<< 8);
1036 vc
->req_image_size
= get_transfer_size(mode
);
1037 dprintk(dev
, 1, "%s: reqsize %ld\n", __func__
, vc
->req_image_size
);
1039 buffer
[0] = IN_DATA_TOKEN
;
1040 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1041 buffer
[2] = CMD_SET_MODE
;
1042 for (i
= 0; i
< sizeof(struct s2255_mode
) / sizeof(u32
); i
++)
1043 buffer
[3 + i
] = cpu_to_le32(((u32
*)&vc
->mode
)[i
]);
1044 vc
->setmode_ready
= 0;
1045 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1047 s2255_print_cfg(dev
, mode
);
1048 /* wait at least 3 frames before continuing */
1049 if (mode
->restart
) {
1050 wait_event_timeout(vc
->wait_setmode
,
1051 (vc
->setmode_ready
!= 0),
1052 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1053 if (vc
->setmode_ready
!= 1) {
1054 dprintk(dev
, 0, "s2255: no set mode response\n");
1058 /* clear the restart flag */
1059 vc
->mode
.restart
= 0;
1060 dprintk(dev
, 1, "%s chn %d, result: %d\n", __func__
, vc
->idx
, res
);
1061 mutex_unlock(&dev
->cmdlock
);
1065 static int s2255_cmd_status(struct s2255_vc
*vc
, u32
*pstatus
)
1069 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
1070 __le32
*buffer
= dev
->cmdbuf
;
1072 mutex_lock(&dev
->cmdlock
);
1073 chn_rev
= G_chnmap
[vc
->idx
];
1074 dprintk(dev
, 4, "%s chan %d\n", __func__
, vc
->idx
);
1075 /* form the get vid status command */
1076 buffer
[0] = IN_DATA_TOKEN
;
1077 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1078 buffer
[2] = CMD_STATUS
;
1080 vc
->vidstatus_ready
= 0;
1081 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1082 wait_event_timeout(vc
->wait_vidstatus
,
1083 (vc
->vidstatus_ready
!= 0),
1084 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT
));
1085 if (vc
->vidstatus_ready
!= 1) {
1086 dprintk(dev
, 0, "s2255: no vidstatus response\n");
1089 *pstatus
= vc
->vidstatus
;
1090 dprintk(dev
, 4, "%s, vid status %d\n", __func__
, *pstatus
);
1091 mutex_unlock(&dev
->cmdlock
);
1095 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1097 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
1100 vc
->last_frame
= -1;
1101 vc
->bad_payload
= 0;
1103 vc
->frame_count
= 0;
1104 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1105 vc
->buffer
.frame
[j
].ulState
= S2255_READ_IDLE
;
1106 vc
->buffer
.frame
[j
].cur_size
= 0;
1108 return s2255_start_acquire(vc
);
1111 /* abort streaming and wait for last buffer */
1112 static int stop_streaming(struct vb2_queue
*vq
)
1114 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
1115 struct s2255_buffer
*buf
, *node
;
1116 unsigned long flags
;
1117 (void) s2255_stop_acquire(vc
);
1118 spin_lock_irqsave(&vc
->qlock
, flags
);
1119 list_for_each_entry_safe(buf
, node
, &vc
->buf_list
, list
) {
1120 list_del(&buf
->list
);
1121 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
1122 dprintk(vc
->dev
, 2, "[%p/%d] done\n",
1123 buf
, buf
->vb
.v4l2_buf
.index
);
1125 spin_unlock_irqrestore(&vc
->qlock
, flags
);
1129 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id i
)
1131 struct s2255_vc
*vc
= video_drvdata(file
);
1132 struct s2255_mode mode
;
1133 struct vb2_queue
*q
= &vc
->vb_vidq
;
1136 * Changing the standard implies a format change, which is not allowed
1137 * while buffers for use with streaming have already been allocated.
1143 if (i
& V4L2_STD_525_60
) {
1144 dprintk(vc
->dev
, 4, "%s 60 Hz\n", __func__
);
1145 /* if changing format, reset frame decimation/intervals */
1146 if (mode
.format
!= FORMAT_NTSC
) {
1148 mode
.format
= FORMAT_NTSC
;
1150 vc
->width
= LINE_SZ_4CIFS_NTSC
;
1151 vc
->height
= NUM_LINES_4CIFS_NTSC
* 2;
1153 } else if (i
& V4L2_STD_625_50
) {
1154 dprintk(vc
->dev
, 4, "%s 50 Hz\n", __func__
);
1155 if (mode
.format
!= FORMAT_PAL
) {
1157 mode
.format
= FORMAT_PAL
;
1159 vc
->width
= LINE_SZ_4CIFS_PAL
;
1160 vc
->height
= NUM_LINES_4CIFS_PAL
* 2;
1166 s2255_set_mode(vc
, &mode
);
1170 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1172 struct s2255_vc
*vc
= video_drvdata(file
);
1178 /* Sensoray 2255 is a multiple channel capture device.
1179 It does not have a "crossbar" of inputs.
1180 We use one V4L device per channel. The user must
1181 be aware that certain combinations are not allowed.
1182 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1183 at once in color(you can do full fps on 4 channels with greyscale.
1185 static int vidioc_enum_input(struct file
*file
, void *priv
,
1186 struct v4l2_input
*inp
)
1188 struct s2255_vc
*vc
= video_drvdata(file
);
1189 struct s2255_dev
*dev
= vc
->dev
;
1192 if (inp
->index
!= 0)
1194 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1195 inp
->std
= S2255_NORMS
;
1197 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_STATUS
) {
1199 rc
= s2255_cmd_status(vc
, &status
);
1200 dprintk(dev
, 4, "s2255_cmd_status rc: %d status %x\n",
1203 inp
->status
= (status
& 0x01) ? 0
1204 : V4L2_IN_ST_NO_SIGNAL
;
1209 strlcpy(inp
->name
, "Composite", sizeof(inp
->name
));
1212 strlcpy(inp
->name
, (vc
->idx
< 2) ? "Composite" : "S-Video",
1219 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1224 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1231 static int s2255_s_ctrl(struct v4l2_ctrl
*ctrl
)
1233 struct s2255_vc
*vc
=
1234 container_of(ctrl
->handler
, struct s2255_vc
, hdl
);
1235 struct s2255_mode mode
;
1237 /* update the mode to the corresponding value */
1239 case V4L2_CID_BRIGHTNESS
:
1240 mode
.bright
= ctrl
->val
;
1242 case V4L2_CID_CONTRAST
:
1243 mode
.contrast
= ctrl
->val
;
1246 mode
.hue
= ctrl
->val
;
1248 case V4L2_CID_SATURATION
:
1249 mode
.saturation
= ctrl
->val
;
1251 case V4L2_CID_S2255_COLORFILTER
:
1252 mode
.color
&= ~MASK_INPUT_TYPE
;
1253 mode
.color
|= !ctrl
->val
<< 16;
1255 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1256 vc
->jpegqual
= ctrl
->val
;
1262 /* set mode here. Note: stream does not need restarted.
1263 some V4L programs restart stream unnecessarily
1266 s2255_set_mode(vc
, &mode
);
1270 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1271 struct v4l2_jpegcompression
*jc
)
1273 struct s2255_vc
*vc
= video_drvdata(file
);
1275 memset(jc
, 0, sizeof(*jc
));
1276 jc
->quality
= vc
->jpegqual
;
1277 dprintk(vc
->dev
, 2, "%s: quality %d\n", __func__
, jc
->quality
);
1281 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1282 const struct v4l2_jpegcompression
*jc
)
1284 struct s2255_vc
*vc
= video_drvdata(file
);
1286 if (jc
->quality
< 0 || jc
->quality
> 100)
1288 v4l2_ctrl_s_ctrl(vc
->jpegqual_ctrl
, jc
->quality
);
1289 dprintk(vc
->dev
, 2, "%s: quality %d\n", __func__
, jc
->quality
);
1293 static int vidioc_g_parm(struct file
*file
, void *priv
,
1294 struct v4l2_streamparm
*sp
)
1296 __u32 def_num
, def_dem
;
1297 struct s2255_vc
*vc
= video_drvdata(file
);
1299 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1301 sp
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1302 sp
->parm
.capture
.capturemode
= vc
->cap_parm
.capturemode
;
1303 sp
->parm
.capture
.readbuffers
= S2255_MIN_BUFS
;
1304 def_num
= (vc
->mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1305 def_dem
= (vc
->mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1306 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1307 switch (vc
->mode
.fdec
) {
1310 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1313 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1316 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1319 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1322 dprintk(vc
->dev
, 4, "%s capture mode, %d timeperframe %d/%d\n",
1324 sp
->parm
.capture
.capturemode
,
1325 sp
->parm
.capture
.timeperframe
.numerator
,
1326 sp
->parm
.capture
.timeperframe
.denominator
);
1330 static int vidioc_s_parm(struct file
*file
, void *priv
,
1331 struct v4l2_streamparm
*sp
)
1333 struct s2255_vc
*vc
= video_drvdata(file
);
1334 struct s2255_mode mode
;
1336 __u32 def_num
, def_dem
;
1337 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1340 /* high quality capture mode requires a stream restart */
1341 if ((vc
->cap_parm
.capturemode
!= sp
->parm
.capture
.capturemode
)
1342 && vb2_is_streaming(&vc
->vb_vidq
))
1344 def_num
= (mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1345 def_dem
= (mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1346 if (def_dem
!= sp
->parm
.capture
.timeperframe
.denominator
)
1347 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1348 else if (sp
->parm
.capture
.timeperframe
.numerator
<= def_num
)
1349 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1350 else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 2)) {
1351 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1353 } else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 3)) {
1354 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1357 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1361 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1362 sp
->parm
.capture
.readbuffers
= S2255_MIN_BUFS
;
1363 s2255_set_mode(vc
, &mode
);
1364 dprintk(vc
->dev
, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1366 sp
->parm
.capture
.capturemode
,
1367 sp
->parm
.capture
.timeperframe
.numerator
,
1368 sp
->parm
.capture
.timeperframe
.denominator
, fdec
);
1372 #define NUM_SIZE_ENUMS 3
1373 static const struct v4l2_frmsize_discrete ntsc_sizes
[] = {
1378 static const struct v4l2_frmsize_discrete pal_sizes
[] = {
1384 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1385 struct v4l2_frmsizeenum
*fe
)
1387 struct s2255_vc
*vc
= video_drvdata(file
);
1388 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
1389 const struct s2255_fmt
*fmt
;
1391 if (fe
->index
>= NUM_SIZE_ENUMS
)
1394 fmt
= format_by_fourcc(fe
->pixel_format
);
1397 fe
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1398 fe
->discrete
= is_ntsc
? ntsc_sizes
[fe
->index
] : pal_sizes
[fe
->index
];
1402 static int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1403 struct v4l2_frmivalenum
*fe
)
1405 struct s2255_vc
*vc
= video_drvdata(file
);
1406 const struct s2255_fmt
*fmt
;
1407 const struct v4l2_frmsize_discrete
*sizes
;
1408 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
1409 #define NUM_FRAME_ENUMS 4
1410 int frm_dec
[NUM_FRAME_ENUMS
] = {1, 2, 3, 5};
1413 if (fe
->index
>= NUM_FRAME_ENUMS
)
1416 fmt
= format_by_fourcc(fe
->pixel_format
);
1420 sizes
= is_ntsc
? ntsc_sizes
: pal_sizes
;
1421 for (i
= 0; i
< NUM_SIZE_ENUMS
; i
++, sizes
++)
1422 if (fe
->width
== sizes
->width
&&
1423 fe
->height
== sizes
->height
)
1425 if (i
== NUM_SIZE_ENUMS
)
1428 fe
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1429 fe
->discrete
.denominator
= is_ntsc
? 30000 : 25000;
1430 fe
->discrete
.numerator
= (is_ntsc
? 1001 : 1000) * frm_dec
[fe
->index
];
1431 dprintk(vc
->dev
, 4, "%s discrete %d/%d\n", __func__
,
1432 fe
->discrete
.numerator
,
1433 fe
->discrete
.denominator
);
1437 static int s2255_open(struct file
*file
)
1439 struct s2255_vc
*vc
= video_drvdata(file
);
1440 struct s2255_dev
*dev
= vc
->dev
;
1444 rc
= v4l2_fh_open(file
);
1448 dprintk(dev
, 1, "s2255: %s\n", __func__
);
1449 state
= atomic_read(&dev
->fw_data
->fw_state
);
1451 case S2255_FW_DISCONNECTING
:
1453 case S2255_FW_FAILED
:
1454 s2255_dev_err(&dev
->udev
->dev
,
1455 "firmware load failed. retrying.\n");
1456 s2255_fwload_start(dev
, 1);
1457 wait_event_timeout(dev
->fw_data
->wait_fw
,
1458 ((atomic_read(&dev
->fw_data
->fw_state
)
1459 == S2255_FW_SUCCESS
) ||
1460 (atomic_read(&dev
->fw_data
->fw_state
)
1461 == S2255_FW_DISCONNECTING
)),
1462 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1463 /* state may have changed, re-read */
1464 state
= atomic_read(&dev
->fw_data
->fw_state
);
1466 case S2255_FW_NOTLOADED
:
1467 case S2255_FW_LOADED_DSPWAIT
:
1468 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1469 driver loaded and then device immediately opened */
1470 pr_info("%s waiting for firmware load\n", __func__
);
1471 wait_event_timeout(dev
->fw_data
->wait_fw
,
1472 ((atomic_read(&dev
->fw_data
->fw_state
)
1473 == S2255_FW_SUCCESS
) ||
1474 (atomic_read(&dev
->fw_data
->fw_state
)
1475 == S2255_FW_DISCONNECTING
)),
1476 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1477 /* state may have changed, re-read */
1478 state
= atomic_read(&dev
->fw_data
->fw_state
);
1480 case S2255_FW_SUCCESS
:
1484 /* state may have changed in above switch statement */
1486 case S2255_FW_SUCCESS
:
1488 case S2255_FW_FAILED
:
1489 pr_info("2255 firmware load failed.\n");
1491 case S2255_FW_DISCONNECTING
:
1492 pr_info("%s: disconnecting\n", __func__
);
1494 case S2255_FW_LOADED_DSPWAIT
:
1495 case S2255_FW_NOTLOADED
:
1496 pr_info("%s: firmware not loaded, please retry\n",
1499 * Timeout on firmware load means device unusable.
1500 * Set firmware failure state.
1501 * On next s2255_open the firmware will be reloaded.
1503 atomic_set(&dev
->fw_data
->fw_state
,
1507 pr_info("%s: unknown state\n", __func__
);
1510 if (!vc
->configured
) {
1511 /* configure channel to default state */
1512 vc
->fmt
= &formats
[0];
1513 s2255_set_mode(vc
, &vc
->mode
);
1519 static void s2255_destroy(struct s2255_dev
*dev
)
1521 dprintk(dev
, 1, "%s", __func__
);
1522 /* board shutdown stops the read pipe if it is running */
1523 s2255_board_shutdown(dev
);
1524 /* make sure firmware still not trying to load */
1525 del_timer(&dev
->timer
); /* only started in .probe and .open */
1526 if (dev
->fw_data
->fw_urb
) {
1527 usb_kill_urb(dev
->fw_data
->fw_urb
);
1528 usb_free_urb(dev
->fw_data
->fw_urb
);
1529 dev
->fw_data
->fw_urb
= NULL
;
1531 release_firmware(dev
->fw_data
->fw
);
1532 kfree(dev
->fw_data
->pfw_data
);
1533 kfree(dev
->fw_data
);
1534 /* reset the DSP so firmware can be reloaded next time */
1535 s2255_reset_dsppower(dev
);
1536 mutex_destroy(&dev
->lock
);
1537 usb_put_dev(dev
->udev
);
1538 v4l2_device_unregister(&dev
->v4l2_dev
);
1543 static const struct v4l2_file_operations s2255_fops_v4l
= {
1544 .owner
= THIS_MODULE
,
1546 .release
= vb2_fop_release
,
1547 .poll
= vb2_fop_poll
,
1548 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1549 .mmap
= vb2_fop_mmap
,
1550 .read
= vb2_fop_read
,
1553 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1554 .vidioc_querycap
= vidioc_querycap
,
1555 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1556 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1557 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1558 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1559 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1560 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1561 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1562 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1563 .vidioc_s_std
= vidioc_s_std
,
1564 .vidioc_g_std
= vidioc_g_std
,
1565 .vidioc_enum_input
= vidioc_enum_input
,
1566 .vidioc_g_input
= vidioc_g_input
,
1567 .vidioc_s_input
= vidioc_s_input
,
1568 .vidioc_streamon
= vb2_ioctl_streamon
,
1569 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1570 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1571 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1572 .vidioc_s_parm
= vidioc_s_parm
,
1573 .vidioc_g_parm
= vidioc_g_parm
,
1574 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1575 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
1576 .vidioc_log_status
= v4l2_ctrl_log_status
,
1577 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1578 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1581 static void s2255_video_device_release(struct video_device
*vdev
)
1583 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1584 struct s2255_vc
*vc
=
1585 container_of(vdev
, struct s2255_vc
, vdev
);
1587 dprintk(dev
, 4, "%s, chnls: %d\n", __func__
,
1588 atomic_read(&dev
->num_channels
));
1590 v4l2_ctrl_handler_free(&vc
->hdl
);
1592 if (atomic_dec_and_test(&dev
->num_channels
))
1597 static struct video_device
template = {
1599 .fops
= &s2255_fops_v4l
,
1600 .ioctl_ops
= &s2255_ioctl_ops
,
1601 .release
= s2255_video_device_release
,
1602 .tvnorms
= S2255_NORMS
,
1605 static const struct v4l2_ctrl_ops s2255_ctrl_ops
= {
1606 .s_ctrl
= s2255_s_ctrl
,
1609 static const struct v4l2_ctrl_config color_filter_ctrl
= {
1610 .ops
= &s2255_ctrl_ops
,
1611 .name
= "Color Filter",
1612 .id
= V4L2_CID_S2255_COLORFILTER
,
1613 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1619 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1623 int cur_nr
= video_nr
;
1624 struct s2255_vc
*vc
;
1625 struct vb2_queue
*q
;
1627 ret
= v4l2_device_register(&dev
->interface
->dev
, &dev
->v4l2_dev
);
1630 /* initialize all video 4 linux */
1631 /* register 4 video devices */
1632 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1634 INIT_LIST_HEAD(&vc
->buf_list
);
1636 v4l2_ctrl_handler_init(&vc
->hdl
, 6);
1637 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1638 V4L2_CID_BRIGHTNESS
, -127, 127, 1, DEF_BRIGHT
);
1639 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1640 V4L2_CID_CONTRAST
, 0, 255, 1, DEF_CONTRAST
);
1641 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1642 V4L2_CID_SATURATION
, 0, 255, 1, DEF_SATURATION
);
1643 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1644 V4L2_CID_HUE
, 0, 255, 1, DEF_HUE
);
1645 vc
->jpegqual_ctrl
= v4l2_ctrl_new_std(&vc
->hdl
,
1647 V4L2_CID_JPEG_COMPRESSION_QUALITY
,
1648 0, 100, 1, S2255_DEF_JPEG_QUAL
);
1649 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_COLORFILTER
&&
1650 (dev
->pid
!= 0x2257 || vc
->idx
<= 1))
1651 v4l2_ctrl_new_custom(&vc
->hdl
, &color_filter_ctrl
,
1653 if (vc
->hdl
.error
) {
1654 ret
= vc
->hdl
.error
;
1655 v4l2_ctrl_handler_free(&vc
->hdl
);
1656 dev_err(&dev
->udev
->dev
, "couldn't register control\n");
1660 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1661 q
->io_modes
= VB2_MMAP
| VB2_READ
| VB2_USERPTR
;
1663 q
->lock
= &vc
->vb_lock
;
1664 q
->buf_struct_size
= sizeof(struct s2255_buffer
);
1665 q
->mem_ops
= &vb2_vmalloc_memops
;
1666 q
->ops
= &s2255_video_qops
;
1667 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1668 ret
= vb2_queue_init(q
);
1670 dev_err(&dev
->udev
->dev
,
1671 "%s vb2_queue_init 0x%x\n", __func__
, ret
);
1674 /* register video devices */
1675 vc
->vdev
= template;
1677 vc
->vdev
.ctrl_handler
= &vc
->hdl
;
1678 vc
->vdev
.lock
= &dev
->lock
;
1679 vc
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1680 set_bit(V4L2_FL_USE_FH_PRIO
, &vc
->vdev
.flags
);
1681 video_set_drvdata(&vc
->vdev
, vc
);
1683 ret
= video_register_device(&vc
->vdev
,
1687 ret
= video_register_device(&vc
->vdev
,
1692 dev_err(&dev
->udev
->dev
,
1693 "failed to register video device!\n");
1696 atomic_inc(&dev
->num_channels
);
1697 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1698 video_device_node_name(&vc
->vdev
));
1701 pr_info("Sensoray 2255 V4L driver Revision: %s\n",
1703 /* if no channels registered, return error and probe will fail*/
1704 if (atomic_read(&dev
->num_channels
) == 0) {
1705 v4l2_device_unregister(&dev
->v4l2_dev
);
1708 if (atomic_read(&dev
->num_channels
) != MAX_CHANNELS
)
1709 pr_warn("s2255: Not all channels available.\n");
1713 /* this function moves the usb stream read pipe data
1714 * into the system buffers.
1715 * returns 0 on success, EAGAIN if more data to process( call this
1718 * Received frame structure:
1719 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1720 * bytes 4-7: channel: 0-3
1721 * bytes 8-11: payload size: size of the frame
1722 * bytes 12-payloadsize+12: frame data
1724 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
1730 unsigned long copy_size
;
1733 struct s2255_framei
*frm
;
1734 unsigned char *pdata
;
1735 struct s2255_vc
*vc
;
1736 dprintk(dev
, 100, "buffer to user\n");
1737 vc
= &dev
->vc
[dev
->cc
];
1738 idx
= vc
->cur_frame
;
1739 frm
= &vc
->buffer
.frame
[idx
];
1740 if (frm
->ulState
== S2255_READ_IDLE
) {
1743 __le32
*pdword
; /*data from dsp is little endian */
1745 /* search for marker codes */
1746 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
1747 pdword
= (__le32
*)pdata
;
1748 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
1750 case S2255_MARKER_FRAME
:
1751 dprintk(dev
, 4, "marker @ offset: %d [%x %x]\n",
1752 jj
, pdata
[0], pdata
[1]);
1753 offset
= jj
+ PREFIX_SIZE
;
1755 cc
= le32_to_cpu(pdword
[1]);
1756 if (cc
>= MAX_CHANNELS
) {
1762 dev
->cc
= G_chnmap
[cc
];
1763 vc
= &dev
->vc
[dev
->cc
];
1764 payload
= le32_to_cpu(pdword
[3]);
1765 if (payload
> vc
->req_image_size
) {
1767 /* discard the bad frame */
1770 vc
->pkt_size
= payload
;
1771 vc
->jpg_size
= le32_to_cpu(pdword
[4]);
1773 case S2255_MARKER_RESPONSE
:
1775 pdata
+= DEF_USB_BLOCK
;
1776 jj
+= DEF_USB_BLOCK
;
1777 if (le32_to_cpu(pdword
[1]) >= MAX_CHANNELS
)
1779 cc
= G_chnmap
[le32_to_cpu(pdword
[1])];
1780 if (cc
>= MAX_CHANNELS
)
1783 switch (pdword
[2]) {
1784 case S2255_RESPONSE_SETMODE
:
1785 /* check if channel valid */
1786 /* set mode ready */
1787 vc
->setmode_ready
= 1;
1788 wake_up(&vc
->wait_setmode
);
1789 dprintk(dev
, 5, "setmode rdy %d\n", cc
);
1791 case S2255_RESPONSE_FW
:
1792 dev
->chn_ready
|= (1 << cc
);
1793 if ((dev
->chn_ready
& 0x0f) != 0x0f)
1795 /* all channels ready */
1796 pr_info("s2255: fw loaded\n");
1797 atomic_set(&dev
->fw_data
->fw_state
,
1799 wake_up(&dev
->fw_data
->wait_fw
);
1801 case S2255_RESPONSE_STATUS
:
1802 vc
->vidstatus
= le32_to_cpu(pdword
[3]);
1803 vc
->vidstatus_ready
= 1;
1804 wake_up(&vc
->wait_vidstatus
);
1805 dprintk(dev
, 5, "vstat %x chan %d\n",
1806 le32_to_cpu(pdword
[3]), cc
);
1809 pr_info("s2255 unknown resp\n");
1821 vc
= &dev
->vc
[dev
->cc
];
1822 idx
= vc
->cur_frame
;
1823 frm
= &vc
->buffer
.frame
[idx
];
1824 /* search done. now find out if should be acquiring on this channel */
1825 if (!vb2_is_streaming(&vc
->vb_vidq
)) {
1826 /* we found a frame, but this channel is turned off */
1827 frm
->ulState
= S2255_READ_IDLE
;
1831 if (frm
->ulState
== S2255_READ_IDLE
) {
1832 frm
->ulState
= S2255_READ_FRAME
;
1836 /* skip the marker 512 bytes (and offset if out of sync) */
1837 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
1840 if (frm
->lpvbits
== NULL
) {
1841 dprintk(dev
, 1, "s2255 frame buffer == NULL.%p %p %d %d",
1842 frm
, dev
, dev
->cc
, idx
);
1846 pdest
= frm
->lpvbits
+ frm
->cur_size
;
1848 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
1850 size
= vc
->pkt_size
- PREFIX_SIZE
;
1852 /* sanity check on pdest */
1853 if ((copy_size
+ frm
->cur_size
) < vc
->req_image_size
)
1854 memcpy(pdest
, psrc
, copy_size
);
1856 frm
->cur_size
+= copy_size
;
1857 dprintk(dev
, 4, "cur_size: %lu, size: %lu\n", frm
->cur_size
, size
);
1859 if (frm
->cur_size
>= size
) {
1860 dprintk(dev
, 2, "******[%d]Buffer[%d]full*******\n",
1862 vc
->last_frame
= vc
->cur_frame
;
1864 /* end of system frame ring buffer, start at zero */
1865 if ((vc
->cur_frame
== SYS_FRAMES
) ||
1866 (vc
->cur_frame
== vc
->buffer
.dwFrames
))
1869 if (vb2_is_streaming(&vc
->vb_vidq
))
1870 s2255_got_frame(vc
, vc
->jpg_size
);
1872 frm
->ulState
= S2255_READ_IDLE
;
1876 /* done successfully */
1880 static void s2255_read_video_callback(struct s2255_dev
*dev
,
1881 struct s2255_pipeinfo
*pipe_info
)
1884 dprintk(dev
, 50, "callback read video\n");
1886 if (dev
->cc
>= MAX_CHANNELS
) {
1888 dev_err(&dev
->udev
->dev
, "invalid channel\n");
1891 /* otherwise copy to the system buffers */
1892 res
= save_frame(dev
, pipe_info
);
1894 dprintk(dev
, 4, "s2255: read callback failed\n");
1896 dprintk(dev
, 50, "callback read video done\n");
1900 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
1901 u16 Index
, u16 Value
, void *TransferBuffer
,
1902 s32 TransferBufferLength
, int bOut
)
1906 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
1908 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
1910 Value
, Index
, TransferBuffer
,
1911 TransferBufferLength
, HZ
* 5);
1913 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1914 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
1915 Value
, Index
, TransferBuffer
,
1916 TransferBufferLength
, HZ
* 5);
1922 * retrieve FX2 firmware version. future use.
1923 * @param dev pointer to device extension
1924 * @return -1 for fail, else returns firmware version as an int(16 bits)
1926 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
1930 unsigned char transBuffer
[64];
1931 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
1934 dprintk(dev
, 2, "get fw error: %x\n", ret
);
1935 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
1936 dprintk(dev
, 2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
1941 * Create the system ring buffer to copy frames into from the
1944 static int s2255_create_sys_buffers(struct s2255_vc
*vc
)
1947 unsigned long reqsize
;
1948 vc
->buffer
.dwFrames
= SYS_FRAMES
;
1949 /* always allocate maximum size(PAL) for system buffers */
1950 reqsize
= SYS_FRAMES_MAXSIZE
;
1952 if (reqsize
> SYS_FRAMES_MAXSIZE
)
1953 reqsize
= SYS_FRAMES_MAXSIZE
;
1955 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1956 /* allocate the frames */
1957 vc
->buffer
.frame
[i
].lpvbits
= vmalloc(reqsize
);
1958 vc
->buffer
.frame
[i
].size
= reqsize
;
1959 if (vc
->buffer
.frame
[i
].lpvbits
== NULL
) {
1960 pr_info("out of memory. using less frames\n");
1961 vc
->buffer
.dwFrames
= i
;
1966 /* make sure internal states are set */
1967 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1968 vc
->buffer
.frame
[i
].ulState
= 0;
1969 vc
->buffer
.frame
[i
].cur_size
= 0;
1973 vc
->last_frame
= -1;
1977 static int s2255_release_sys_buffers(struct s2255_vc
*vc
)
1980 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1981 if (vc
->buffer
.frame
[i
].lpvbits
)
1982 vfree(vc
->buffer
.frame
[i
].lpvbits
);
1983 vc
->buffer
.frame
[i
].lpvbits
= NULL
;
1988 static int s2255_board_init(struct s2255_dev
*dev
)
1990 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
1993 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
1994 dprintk(dev
, 4, "board init: %p", dev
);
1995 memset(pipe
, 0, sizeof(*pipe
));
1997 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
1998 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
2000 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
2002 if (pipe
->transfer_buffer
== NULL
) {
2003 dprintk(dev
, 1, "out of memory!\n");
2006 /* query the firmware */
2007 fw_ver
= s2255_get_fx2fw(dev
);
2009 pr_info("s2255: usb firmware version %d.%d\n",
2010 (fw_ver
>> 8) & 0xff,
2013 if (fw_ver
< S2255_CUR_USB_FWVER
)
2014 pr_info("s2255: newer USB firmware available\n");
2016 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
2017 struct s2255_vc
*vc
= &dev
->vc
[j
];
2018 vc
->mode
= mode_def
;
2019 if (dev
->pid
== 0x2257 && j
> 1)
2020 vc
->mode
.color
|= (1 << 16);
2021 vc
->jpegqual
= S2255_DEF_JPEG_QUAL
;
2022 vc
->width
= LINE_SZ_4CIFS_NTSC
;
2023 vc
->height
= NUM_LINES_4CIFS_NTSC
* 2;
2024 vc
->std
= V4L2_STD_NTSC_M
;
2025 vc
->fmt
= &formats
[0];
2026 vc
->mode
.restart
= 1;
2027 vc
->req_image_size
= get_transfer_size(&mode_def
);
2028 vc
->frame_count
= 0;
2029 /* create the system buffers */
2030 s2255_create_sys_buffers(vc
);
2032 /* start read pipe */
2033 s2255_start_readpipe(dev
);
2034 dprintk(dev
, 1, "%s: success\n", __func__
);
2038 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2041 dprintk(dev
, 1, "%s: dev: %p", __func__
, dev
);
2043 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2044 if (vb2_is_streaming(&dev
->vc
[i
].vb_vidq
))
2045 s2255_stop_acquire(&dev
->vc
[i
]);
2047 s2255_stop_readpipe(dev
);
2048 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2049 s2255_release_sys_buffers(&dev
->vc
[i
]);
2050 /* release transfer buffer */
2051 kfree(dev
->pipe
.transfer_buffer
);
2055 static void read_pipe_completion(struct urb
*purb
)
2057 struct s2255_pipeinfo
*pipe_info
;
2058 struct s2255_dev
*dev
;
2061 pipe_info
= purb
->context
;
2062 if (pipe_info
== NULL
) {
2063 dev_err(&purb
->dev
->dev
, "no context!\n");
2066 dev
= pipe_info
->dev
;
2068 dev_err(&purb
->dev
->dev
, "no context!\n");
2071 status
= purb
->status
;
2072 /* if shutting down, do not resubmit, exit immediately */
2073 if (status
== -ESHUTDOWN
) {
2074 dprintk(dev
, 2, "%s: err shutdown\n", __func__
);
2075 pipe_info
->err_count
++;
2079 if (pipe_info
->state
== 0) {
2080 dprintk(dev
, 2, "%s: exiting USB pipe", __func__
);
2085 s2255_read_video_callback(dev
, pipe_info
);
2087 pipe_info
->err_count
++;
2088 dprintk(dev
, 1, "%s: failed URB %d\n", __func__
, status
);
2091 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2093 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2095 pipe_info
->transfer_buffer
,
2096 pipe_info
->cur_transfer_size
,
2097 read_pipe_completion
, pipe_info
);
2099 if (pipe_info
->state
!= 0) {
2100 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_ATOMIC
))
2101 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2103 dprintk(dev
, 2, "%s :complete state 0\n", __func__
);
2108 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2112 struct s2255_pipeinfo
*pipe_info
= &dev
->pipe
;
2113 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2114 dprintk(dev
, 2, "%s: IN %d\n", __func__
, dev
->read_endpoint
);
2115 pipe_info
->state
= 1;
2116 pipe_info
->err_count
= 0;
2117 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2118 if (!pipe_info
->stream_urb
) {
2119 dev_err(&dev
->udev
->dev
,
2120 "ReadStream: Unable to alloc URB\n");
2123 /* transfer buffer allocated in board_init */
2124 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2126 pipe_info
->transfer_buffer
,
2127 pipe_info
->cur_transfer_size
,
2128 read_pipe_completion
, pipe_info
);
2129 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2131 pr_err("s2255: start read pipe failed\n");
2137 /* starts acquisition process */
2138 static int s2255_start_acquire(struct s2255_vc
*vc
)
2141 unsigned long chn_rev
;
2143 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
2144 __le32
*buffer
= dev
->cmdbuf
;
2146 mutex_lock(&dev
->cmdlock
);
2147 chn_rev
= G_chnmap
[vc
->idx
];
2148 vc
->last_frame
= -1;
2149 vc
->bad_payload
= 0;
2151 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2152 vc
->buffer
.frame
[j
].ulState
= 0;
2153 vc
->buffer
.frame
[j
].cur_size
= 0;
2156 /* send the start command */
2157 buffer
[0] = IN_DATA_TOKEN
;
2158 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
2159 buffer
[2] = CMD_START
;
2160 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2162 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2164 dprintk(dev
, 2, "start acquire exit[%d] %d\n", vc
->idx
, res
);
2165 mutex_unlock(&dev
->cmdlock
);
2169 static int s2255_stop_acquire(struct s2255_vc
*vc
)
2172 unsigned long chn_rev
;
2173 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
2174 __le32
*buffer
= dev
->cmdbuf
;
2176 mutex_lock(&dev
->cmdlock
);
2177 chn_rev
= G_chnmap
[vc
->idx
];
2178 /* send the stop command */
2179 buffer
[0] = IN_DATA_TOKEN
;
2180 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
2181 buffer
[2] = CMD_STOP
;
2183 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2185 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2187 dprintk(dev
, 4, "%s: chn %d, res %d\n", __func__
, vc
->idx
, res
);
2188 mutex_unlock(&dev
->cmdlock
);
2192 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2194 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2197 if (pipe
->stream_urb
) {
2199 usb_kill_urb(pipe
->stream_urb
);
2200 usb_free_urb(pipe
->stream_urb
);
2201 pipe
->stream_urb
= NULL
;
2203 dprintk(dev
, 4, "%s", __func__
);
2207 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
)
2210 s2255_reset_dsppower(dev
);
2211 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2212 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2213 memcpy(dev
->fw_data
->pfw_data
,
2214 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2215 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2216 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2217 usb_sndbulkpipe(dev
->udev
, 2),
2218 dev
->fw_data
->pfw_data
,
2219 CHUNK_SIZE
, s2255_fwchunk_complete
,
2221 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2224 /* standard usb probe function */
2225 static int s2255_probe(struct usb_interface
*interface
,
2226 const struct usb_device_id
*id
)
2228 struct s2255_dev
*dev
= NULL
;
2229 struct usb_host_interface
*iface_desc
;
2230 struct usb_endpoint_descriptor
*endpoint
;
2232 int retval
= -ENOMEM
;
2236 /* allocate memory for our device state and initialize it to zero */
2237 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2239 s2255_dev_err(&interface
->dev
, "out of memory\n");
2243 dev
->cmdbuf
= kzalloc(S2255_CMDBUF_SIZE
, GFP_KERNEL
);
2244 if (dev
->cmdbuf
== NULL
) {
2245 s2255_dev_err(&interface
->dev
, "out of memory\n");
2249 atomic_set(&dev
->num_channels
, 0);
2250 dev
->pid
= le16_to_cpu(id
->idProduct
);
2251 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2254 mutex_init(&dev
->lock
);
2255 mutex_init(&dev
->cmdlock
);
2256 /* grab usb_device and save it */
2257 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2258 if (dev
->udev
== NULL
) {
2259 dev_err(&interface
->dev
, "null usb device\n");
2263 dev_dbg(&interface
->dev
, "dev: %p, udev %p interface %p\n",
2264 dev
, dev
->udev
, interface
);
2265 dev
->interface
= interface
;
2266 /* set up the endpoint information */
2267 iface_desc
= interface
->cur_altsetting
;
2268 dev_dbg(&interface
->dev
, "num EP: %d\n",
2269 iface_desc
->desc
.bNumEndpoints
);
2270 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2271 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2272 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2273 /* we found the bulk in endpoint */
2274 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2278 if (!dev
->read_endpoint
) {
2279 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2282 init_timer(&dev
->timer
);
2283 dev
->timer
.function
= s2255_timer
;
2284 dev
->timer
.data
= (unsigned long)dev
->fw_data
;
2285 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2286 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2287 struct s2255_vc
*vc
= &dev
->vc
[i
];
2290 init_waitqueue_head(&vc
->wait_setmode
);
2291 init_waitqueue_head(&vc
->wait_vidstatus
);
2292 spin_lock_init(&vc
->qlock
);
2293 mutex_init(&vc
->vb_lock
);
2296 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2297 if (!dev
->fw_data
->fw_urb
) {
2298 dev_err(&interface
->dev
, "out of memory!\n");
2302 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2303 if (!dev
->fw_data
->pfw_data
) {
2304 dev_err(&interface
->dev
, "out of memory!\n");
2307 /* load the first chunk */
2308 if (request_firmware(&dev
->fw_data
->fw
,
2309 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2310 dev_err(&interface
->dev
, "sensoray 2255 failed to get firmware\n");
2313 /* check the firmware is valid */
2314 fw_size
= dev
->fw_data
->fw
->size
;
2315 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2317 if (*pdata
!= S2255_FW_MARKER
) {
2318 dev_err(&interface
->dev
, "Firmware invalid.\n");
2322 /* make sure firmware is the latest */
2324 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2325 pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel
));
2326 dev
->dsp_fw_ver
= le32_to_cpu(*pRel
);
2327 if (dev
->dsp_fw_ver
< S2255_CUR_DSP_FWVER
)
2328 pr_info("s2255: f2255usb.bin out of date.\n");
2329 if (dev
->pid
== 0x2257 &&
2330 dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
2331 pr_warn("2257 needs firmware %d or above.\n",
2332 S2255_MIN_DSP_COLORFILTER
);
2334 usb_reset_device(dev
->udev
);
2335 /* load 2255 board specific */
2336 retval
= s2255_board_init(dev
);
2338 goto errorBOARDINIT
;
2339 s2255_fwload_start(dev
, 0);
2340 /* loads v4l specific */
2341 retval
= s2255_probe_v4l(dev
);
2343 goto errorBOARDINIT
;
2344 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2347 s2255_board_shutdown(dev
);
2349 release_firmware(dev
->fw_data
->fw
);
2351 kfree(dev
->fw_data
->pfw_data
);
2353 usb_free_urb(dev
->fw_data
->fw_urb
);
2355 del_timer(&dev
->timer
);
2357 usb_put_dev(dev
->udev
);
2359 kfree(dev
->fw_data
);
2360 mutex_destroy(&dev
->lock
);
2364 pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval
);
2368 /* disconnect routine. when board is removed physically or with rmmod */
2369 static void s2255_disconnect(struct usb_interface
*interface
)
2371 struct s2255_dev
*dev
= to_s2255_dev(usb_get_intfdata(interface
));
2373 int channels
= atomic_read(&dev
->num_channels
);
2374 mutex_lock(&dev
->lock
);
2375 v4l2_device_disconnect(&dev
->v4l2_dev
);
2376 mutex_unlock(&dev
->lock
);
2377 /*see comments in the uvc_driver.c usb disconnect function */
2378 atomic_inc(&dev
->num_channels
);
2379 /* unregister each video device. */
2380 for (i
= 0; i
< channels
; i
++)
2381 video_unregister_device(&dev
->vc
[i
].vdev
);
2382 /* wake up any of our timers */
2383 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2384 wake_up(&dev
->fw_data
->wait_fw
);
2385 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2386 dev
->vc
[i
].setmode_ready
= 1;
2387 wake_up(&dev
->vc
[i
].wait_setmode
);
2388 dev
->vc
[i
].vidstatus_ready
= 1;
2389 wake_up(&dev
->vc
[i
].wait_vidstatus
);
2391 if (atomic_dec_and_test(&dev
->num_channels
))
2393 dev_info(&interface
->dev
, "%s\n", __func__
);
2396 static struct usb_driver s2255_driver
= {
2397 .name
= S2255_DRIVER_NAME
,
2398 .probe
= s2255_probe
,
2399 .disconnect
= s2255_disconnect
,
2400 .id_table
= s2255_table
,
2403 module_usb_driver(s2255_driver
);
2405 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2406 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2407 MODULE_LICENSE("GPL");
2408 MODULE_VERSION(S2255_VERSION
);
2409 MODULE_FIRMWARE(FIRMWARE_FILE_NAME
);