1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
5 * Copyright (C) 2007-2014 by Sensoray Company Inc.
8 * Some video buffer code based on vivi driver:
10 * Sensoray 2255 device supports 4 simultaneous channels.
11 * The channels are not "crossbar" inputs, they are physically
12 * attached to separate video decoders.
14 * Because of USB2.0 bandwidth limitations. There is only a
15 * certain amount of data which may be transferred at one time.
17 * Example maximum bandwidth utilization:
19 * -full size, color mode YUYV or YUV422P: 2 channels at once
20 * -full or half size Grey scale: all 4 channels at once
21 * -half size, color mode YUYV or YUV422P: all 4 channels at once
22 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
26 #include <linux/module.h>
27 #include <linux/firmware.h>
28 #include <linux/kernel.h>
29 #include <linux/mutex.h>
30 #include <linux/slab.h>
31 #include <linux/videodev2.h>
33 #include <linux/vmalloc.h>
34 #include <linux/usb.h>
35 #include <media/videobuf2-v4l2.h>
36 #include <media/videobuf2-vmalloc.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-ctrls.h>
41 #include <media/v4l2-event.h>
43 #define S2255_VERSION "1.25.1"
44 #define FIRMWARE_FILE_NAME "f2255usb.bin"
46 /* default JPEG quality */
47 #define S2255_DEF_JPEG_QUAL 50
48 /* vendor request in */
50 /* vendor request out */
51 #define S2255_VR_OUT 1
53 #define S2255_VR_FW 0x30
54 /* USB endpoint number for configuring the device */
55 #define S2255_CONFIG_EP 2
56 /* maximum time for DSP to start responding after last FW word loaded(ms) */
57 #define S2255_DSP_BOOTTIME 800
58 /* maximum time to wait for firmware to load (ms) */
59 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
60 #define S2255_MIN_BUFS 2
61 #define S2255_SETMODE_TIMEOUT 500
62 #define S2255_VIDSTATUS_TIMEOUT 350
63 #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
64 #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
65 #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
66 #define S2255_RESPONSE_FW cpu_to_le32(0x10)
67 #define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
68 #define S2255_USB_XFER_SIZE (16 * 1024)
69 #define MAX_CHANNELS 4
71 /* maximum size is PAL full size plus room for the marker header(s) */
72 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
73 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
74 #define LINE_SZ_4CIFS_NTSC 640
75 #define LINE_SZ_2CIFS_NTSC 640
76 #define LINE_SZ_1CIFS_NTSC 320
77 #define LINE_SZ_4CIFS_PAL 704
78 #define LINE_SZ_2CIFS_PAL 704
79 #define LINE_SZ_1CIFS_PAL 352
80 #define NUM_LINES_4CIFS_NTSC 240
81 #define NUM_LINES_2CIFS_NTSC 240
82 #define NUM_LINES_1CIFS_NTSC 240
83 #define NUM_LINES_4CIFS_PAL 288
84 #define NUM_LINES_2CIFS_PAL 288
85 #define NUM_LINES_1CIFS_PAL 288
86 #define LINE_SZ_DEF 640
87 #define NUM_LINES_DEF 240
90 /* predefined settings */
94 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
95 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
96 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
97 /* SCALE_4CIFSI is the 2 fields interpolated into one */
98 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
100 #define COLOR_YUVPL 1 /* YUV planar */
101 #define COLOR_YUVPK 2 /* YUV packed */
102 #define COLOR_Y8 4 /* monochrome */
103 #define COLOR_JPG 5 /* JPEG */
105 #define MASK_COLOR 0x000000ff
106 #define MASK_JPG_QUALITY 0x0000ff00
107 #define MASK_INPUT_TYPE 0x000f0000
108 /* frame decimation. */
109 #define FDEC_1 1 /* capture every frame. default */
110 #define FDEC_2 2 /* capture every 2nd frame */
111 #define FDEC_3 3 /* capture every 3rd frame */
112 #define FDEC_5 5 /* capture every 5th frame */
114 /*-------------------------------------------------------
115 * Default mode parameters.
116 *-------------------------------------------------------*/
117 #define DEF_SCALE SCALE_4CIFS
118 #define DEF_COLOR COLOR_YUVPL
119 #define DEF_FDEC FDEC_1
121 #define DEF_CONTRAST 0x5c
122 #define DEF_SATURATION 0x80
125 /* usb config commands */
126 #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
127 #define CMD_2255 0xc2255000
128 #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
129 #define CMD_START cpu_to_le32((CMD_2255 | 0x20))
130 #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
131 #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
134 u32 format
; /* input video format (NTSC, PAL) */
135 u32 scale
; /* output video scale */
136 u32 color
; /* output video color format */
137 u32 fdec
; /* frame decimation */
138 u32 bright
; /* brightness */
139 u32 contrast
; /* contrast */
140 u32 saturation
; /* saturation */
141 u32 hue
; /* hue (NTSC only)*/
142 u32 single
; /* capture 1 frame at a time (!=0), continuously (==0)*/
143 u32 usb_block
; /* block size. should be 4096 of DEF_USB_BLOCK */
144 u32 restart
; /* if DSP requires restart */
148 #define S2255_READ_IDLE 0
149 #define S2255_READ_FRAME 1
151 /* frame structure */
152 struct s2255_framei
{
154 unsigned long ulState
; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
155 void *lpvbits
; /* image data */
156 unsigned long cur_size
; /* current data copied to it */
159 /* image buffer structure */
160 struct s2255_bufferi
{
161 unsigned long dwFrames
; /* number of frames in buffer */
162 struct s2255_framei frame
[SYS_FRAMES
]; /* array of FRAME structures */
165 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
166 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
167 DEF_HUE, 0, DEF_USB_BLOCK, 0}
169 /* for firmware loading, fw_state */
170 #define S2255_FW_NOTLOADED 0
171 #define S2255_FW_LOADED_DSPWAIT 1
172 #define S2255_FW_SUCCESS 2
173 #define S2255_FW_FAILED 3
174 #define S2255_FW_DISCONNECTING 4
175 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
176 /* 2255 read states */
177 #define S2255_READ_IDLE 0
178 #define S2255_READ_FRAME 1
185 wait_queue_head_t wait_fw
;
186 const struct firmware
*fw
;
189 struct s2255_pipeinfo
{
190 u32 max_transfer_size
;
191 u32 cur_transfer_size
;
195 void *dev
; /* back pointer to s2255_dev struct*/
200 struct s2255_fmt
; /*forward declaration */
203 /* 2255 video channel */
205 struct s2255_dev
*dev
;
206 struct video_device vdev
;
207 struct v4l2_ctrl_handler hdl
;
208 struct v4l2_ctrl
*jpegqual_ctrl
;
210 struct list_head buf_list
;
211 struct s2255_bufferi buffer
;
212 struct s2255_mode mode
;
214 /* jpeg compression */
216 /* capture parameters (for high quality mode full size) */
217 struct v4l2_captureparm cap_parm
;
220 /* allocated image size */
221 unsigned long req_image_size
;
222 /* received packet size */
223 unsigned long pkt_size
;
225 unsigned long frame_count
;
228 /* if channel configured to default state */
230 wait_queue_head_t wait_setmode
;
232 /* video status items */
234 wait_queue_head_t wait_vidstatus
;
238 enum v4l2_field field
;
239 const struct s2255_fmt
*fmt
;
240 int idx
; /* channel number on device, 0-3 */
241 struct vb2_queue vb_vidq
;
242 struct mutex vb_lock
; /* streaming lock */
248 struct s2255_vc vc
[MAX_CHANNELS
];
249 struct v4l2_device v4l2_dev
;
250 atomic_t num_channels
;
252 struct mutex lock
; /* channels[].vdev.lock */
253 struct mutex cmdlock
; /* protects cmdbuf */
254 struct usb_device
*udev
;
255 struct usb_interface
*interface
;
257 struct timer_list timer
;
258 struct s2255_fw
*fw_data
;
259 struct s2255_pipeinfo pipe
;
260 u32 cc
; /* current channel */
263 /* dsp firmware version (f2255usb.bin) */
265 u16 pid
; /* product id */
266 #define S2255_CMDBUF_SIZE 512
270 static inline struct s2255_dev
*to_s2255_dev(struct v4l2_device
*v4l2_dev
)
272 return container_of(v4l2_dev
, struct s2255_dev
, v4l2_dev
);
280 /* buffer for one video frame */
281 struct s2255_buffer
{
282 /* common v4l buffer stuff -- must be first */
283 struct vb2_v4l2_buffer vb
;
284 struct list_head list
;
288 /* current cypress EEPROM firmware version */
289 #define S2255_CUR_USB_FWVER ((3 << 8) | 12)
290 /* current DSP FW version */
291 #define S2255_CUR_DSP_FWVER 10104
292 /* Need DSP version 5+ for video status feature */
293 #define S2255_MIN_DSP_STATUS 5
294 #define S2255_MIN_DSP_COLORFILTER 8
295 #define S2255_NORMS (V4L2_STD_ALL)
297 /* private V4L2 controls */
300 * The following chart displays how COLORFILTER should be set
301 * =========================================================
302 * = fourcc = COLORFILTER =
303 * = ===============================
305 * =========================================================
306 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
307 * = = s-video or = composite =
308 * = = B/W camera = input =
309 * =========================================================
310 * = other = color, svideo = color, =
312 * =========================================================
315 * channels 0-3 on 2255 are composite
316 * channels 0-1 on 2257 are composite, 2-3 are s-video
317 * If COLORFILTER is 0 with a composite color camera connected,
318 * the output will appear monochrome but hatching
320 * COLORFILTER is different from "color killer" and "color effects"
323 #define S2255_V4L2_YC_ON 1
324 #define S2255_V4L2_YC_OFF 0
325 #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0)
327 /* frame prefix size (sent once every frame) */
328 #define PREFIX_SIZE 512
330 /* Channels on box are in reverse order */
331 static unsigned long G_chnmap
[MAX_CHANNELS
] = {3, 2, 1, 0};
335 static int s2255_start_readpipe(struct s2255_dev
*dev
);
336 static void s2255_stop_readpipe(struct s2255_dev
*dev
);
337 static int s2255_start_acquire(struct s2255_vc
*vc
);
338 static int s2255_stop_acquire(struct s2255_vc
*vc
);
339 static void s2255_fillbuff(struct s2255_vc
*vc
, struct s2255_buffer
*buf
,
341 static int s2255_set_mode(struct s2255_vc
*vc
, struct s2255_mode
*mode
);
342 static int s2255_board_shutdown(struct s2255_dev
*dev
);
343 static void s2255_fwload_start(struct s2255_dev
*dev
);
344 static void s2255_destroy(struct s2255_dev
*dev
);
345 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char req
,
346 u16 index
, u16 value
, void *buf
,
347 s32 buf_len
, int bOut
);
349 /* dev_err macro with driver name */
350 #define S2255_DRIVER_NAME "s2255"
351 #define s2255_dev_err(dev, fmt, arg...) \
352 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
354 #define dprintk(dev, level, fmt, arg...) \
355 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
357 static struct usb_driver s2255_driver
;
359 /* start video number */
360 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
362 /* Enable jpeg capture. */
363 static int jpeg_enable
= 1;
365 module_param(debug
, int, 0644);
366 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
367 module_param(video_nr
, int, 0644);
368 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
369 module_param(jpeg_enable
, int, 0644);
370 MODULE_PARM_DESC(jpeg_enable
, "Jpeg enable(1-on 0-off) default 1");
372 /* USB device table */
373 #define USB_SENSORAY_VID 0x1943
374 static const struct usb_device_id s2255_table
[] = {
375 {USB_DEVICE(USB_SENSORAY_VID
, 0x2255)},
376 {USB_DEVICE(USB_SENSORAY_VID
, 0x2257)}, /*same family as 2255*/
377 { } /* Terminating entry */
379 MODULE_DEVICE_TABLE(usb
, s2255_table
);
381 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
384 /* JPEG formats must be defined last to support jpeg_enable parameter */
385 static const struct s2255_fmt formats
[] = {
387 .fourcc
= V4L2_PIX_FMT_YUYV
,
391 .fourcc
= V4L2_PIX_FMT_UYVY
,
394 .fourcc
= V4L2_PIX_FMT_YUV422P
,
398 .fourcc
= V4L2_PIX_FMT_GREY
,
401 .fourcc
= V4L2_PIX_FMT_JPEG
,
404 .fourcc
= V4L2_PIX_FMT_MJPEG
,
409 static int norm_maxw(struct s2255_vc
*vc
)
411 return (vc
->std
& V4L2_STD_525_60
) ?
412 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
415 static int norm_maxh(struct s2255_vc
*vc
)
417 return (vc
->std
& V4L2_STD_525_60
) ?
418 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
421 static int norm_minw(struct s2255_vc
*vc
)
423 return (vc
->std
& V4L2_STD_525_60
) ?
424 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
427 static int norm_minh(struct s2255_vc
*vc
)
429 return (vc
->std
& V4L2_STD_525_60
) ?
430 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
435 * TODO: fixme: move YUV reordering to hardware
436 * converts 2255 planar format to yuyv or uyvy
438 static void planar422p_to_yuv_packed(const unsigned char *in
,
440 int width
, int height
,
446 unsigned long size
= height
* width
;
448 pY
= (unsigned char *)in
;
449 pCr
= (unsigned char *)in
+ height
* width
;
450 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
451 for (i
= 0; i
< size
* 2; i
+= 4) {
452 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
453 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
454 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
455 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
460 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
462 s2255_vendor_req(dev
, 0x40, 0x0000, 0x0001, NULL
, 0, 1);
464 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
466 s2255_vendor_req(dev
, 0x10, 0x0000, 0x0000, NULL
, 0, 1);
470 /* kickstarts the firmware loading. from probe
472 static void s2255_timer(struct timer_list
*t
)
474 struct s2255_dev
*dev
= from_timer(dev
, t
, timer
);
475 struct s2255_fw
*data
= dev
->fw_data
;
476 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
477 pr_err("s2255: can't submit urb\n");
478 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
479 /* wake up anything waiting for the firmware */
480 wake_up(&data
->wait_fw
);
486 /* this loads the firmware asynchronously.
487 Originally this was done synchronously in probe.
488 But it is better to load it asynchronously here than block
489 inside the probe function. Blocking inside probe affects boot time.
490 FW loading is triggered by the timer in the probe function
492 static void s2255_fwchunk_complete(struct urb
*urb
)
494 struct s2255_fw
*data
= urb
->context
;
495 struct usb_device
*udev
= urb
->dev
;
498 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
499 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
500 /* wake up anything waiting for the firmware */
501 wake_up(&data
->wait_fw
);
504 if (data
->fw_urb
== NULL
) {
505 s2255_dev_err(&udev
->dev
, "disconnected\n");
506 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
507 /* wake up anything waiting for the firmware */
508 wake_up(&data
->wait_fw
);
511 #define CHUNK_SIZE 512
512 /* all USB transfers must be done with continuous kernel memory.
513 can't allocate more than 128k in current linux kernel, so
514 upload the firmware in chunks
516 if (data
->fw_loaded
< data
->fw_size
) {
517 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
518 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
520 if (len
< CHUNK_SIZE
)
521 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
523 memcpy(data
->pfw_data
,
524 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
526 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
527 data
->pfw_data
, CHUNK_SIZE
,
528 s2255_fwchunk_complete
, data
);
529 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
530 dev_err(&udev
->dev
, "failed submit URB\n");
531 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
532 /* wake up anything waiting for the firmware */
533 wake_up(&data
->wait_fw
);
536 data
->fw_loaded
+= len
;
538 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
543 static void s2255_got_frame(struct s2255_vc
*vc
, int jpgsize
)
545 struct s2255_buffer
*buf
;
546 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
547 unsigned long flags
= 0;
549 spin_lock_irqsave(&vc
->qlock
, flags
);
550 if (list_empty(&vc
->buf_list
)) {
551 dprintk(dev
, 1, "No active queue to serve\n");
552 spin_unlock_irqrestore(&vc
->qlock
, flags
);
555 buf
= list_entry(vc
->buf_list
.next
,
556 struct s2255_buffer
, list
);
557 list_del(&buf
->list
);
558 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
559 buf
->vb
.field
= vc
->field
;
560 buf
->vb
.sequence
= vc
->frame_count
;
561 spin_unlock_irqrestore(&vc
->qlock
, flags
);
563 s2255_fillbuff(vc
, buf
, jpgsize
);
564 /* tell v4l buffer was filled */
565 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
566 dprintk(dev
, 2, "%s: [buf] [%p]\n", __func__
, buf
);
569 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
572 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
573 if (-1 == formats
[i
].fourcc
)
575 if (!jpeg_enable
&& ((formats
[i
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
576 (formats
[i
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
578 if (formats
[i
].fourcc
== fourcc
)
584 /* video buffer vmalloc implementation based partly on VIVI driver which is
585 * Copyright (c) 2006 by
586 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
587 * Ted Walther <ted--a.t--enumera.com>
588 * John Sokol <sokol--a.t--videotechnology.com>
589 * http://v4l.videotechnology.com/
592 static void s2255_fillbuff(struct s2255_vc
*vc
,
593 struct s2255_buffer
*buf
, int jpgsize
)
597 char *vbuf
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
598 unsigned long last_frame
;
599 struct s2255_dev
*dev
= vc
->dev
;
603 last_frame
= vc
->last_frame
;
604 if (last_frame
!= -1) {
606 (const char *)vc
->buffer
.frame
[last_frame
].lpvbits
;
607 switch (vc
->fmt
->fourcc
) {
608 case V4L2_PIX_FMT_YUYV
:
609 case V4L2_PIX_FMT_UYVY
:
610 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
615 case V4L2_PIX_FMT_GREY
:
616 memcpy(vbuf
, tmpbuf
, vc
->width
* vc
->height
);
618 case V4L2_PIX_FMT_JPEG
:
619 case V4L2_PIX_FMT_MJPEG
:
620 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, jpgsize
);
621 memcpy(vbuf
, tmpbuf
, jpgsize
);
623 case V4L2_PIX_FMT_YUV422P
:
625 vc
->width
* vc
->height
* 2);
628 pr_info("s2255: unknown format?\n");
632 pr_err("s2255: =======no frame\n");
635 dprintk(dev
, 2, "s2255fill at : Buffer %p size= %d\n",
640 /* ------------------------------------------------------------------
642 ------------------------------------------------------------------*/
644 static int queue_setup(struct vb2_queue
*vq
,
645 unsigned int *nbuffers
, unsigned int *nplanes
,
646 unsigned int sizes
[], struct device
*alloc_devs
[])
648 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
649 if (*nbuffers
< S2255_MIN_BUFS
)
650 *nbuffers
= S2255_MIN_BUFS
;
652 sizes
[0] = vc
->width
* vc
->height
* (vc
->fmt
->depth
>> 3);
656 static int buffer_prepare(struct vb2_buffer
*vb
)
658 struct s2255_vc
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
659 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
660 struct s2255_buffer
*buf
= container_of(vbuf
, struct s2255_buffer
, vb
);
665 dprintk(vc
->dev
, 4, "%s\n", __func__
);
669 if ((w
< norm_minw(vc
)) ||
670 (w
> norm_maxw(vc
)) ||
671 (h
< norm_minh(vc
)) ||
672 (h
> norm_maxh(vc
))) {
673 dprintk(vc
->dev
, 4, "invalid buffer prepare\n");
676 size
= w
* h
* (vc
->fmt
->depth
>> 3);
677 if (vb2_plane_size(vb
, 0) < size
) {
678 dprintk(vc
->dev
, 4, "invalid buffer prepare\n");
682 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, size
);
686 static void buffer_queue(struct vb2_buffer
*vb
)
688 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
689 struct s2255_buffer
*buf
= container_of(vbuf
, struct s2255_buffer
, vb
);
690 struct s2255_vc
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
691 unsigned long flags
= 0;
692 dprintk(vc
->dev
, 1, "%s\n", __func__
);
693 spin_lock_irqsave(&vc
->qlock
, flags
);
694 list_add_tail(&buf
->list
, &vc
->buf_list
);
695 spin_unlock_irqrestore(&vc
->qlock
, flags
);
698 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
);
699 static void stop_streaming(struct vb2_queue
*vq
);
701 static const struct vb2_ops s2255_video_qops
= {
702 .queue_setup
= queue_setup
,
703 .buf_prepare
= buffer_prepare
,
704 .buf_queue
= buffer_queue
,
705 .start_streaming
= start_streaming
,
706 .stop_streaming
= stop_streaming
,
707 .wait_prepare
= vb2_ops_wait_prepare
,
708 .wait_finish
= vb2_ops_wait_finish
,
711 static int vidioc_querycap(struct file
*file
, void *priv
,
712 struct v4l2_capability
*cap
)
714 struct s2255_vc
*vc
= video_drvdata(file
);
715 struct s2255_dev
*dev
= vc
->dev
;
717 strscpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
718 strscpy(cap
->card
, "s2255", sizeof(cap
->card
));
719 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
723 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
724 struct v4l2_fmtdesc
*f
)
726 int index
= f
->index
;
728 if (index
>= ARRAY_SIZE(formats
))
730 if (!jpeg_enable
&& ((formats
[index
].fourcc
== V4L2_PIX_FMT_JPEG
) ||
731 (formats
[index
].fourcc
== V4L2_PIX_FMT_MJPEG
)))
733 f
->pixelformat
= formats
[index
].fourcc
;
737 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
738 struct v4l2_format
*f
)
740 struct s2255_vc
*vc
= video_drvdata(file
);
741 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
743 f
->fmt
.pix
.width
= vc
->width
;
744 f
->fmt
.pix
.height
= vc
->height
;
745 if (f
->fmt
.pix
.height
>=
746 (is_ntsc
? NUM_LINES_1CIFS_NTSC
: NUM_LINES_1CIFS_PAL
) * 2)
747 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
749 f
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
750 f
->fmt
.pix
.pixelformat
= vc
->fmt
->fourcc
;
751 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (vc
->fmt
->depth
>> 3);
752 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
753 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
757 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
758 struct v4l2_format
*f
)
760 const struct s2255_fmt
*fmt
;
761 enum v4l2_field field
;
762 struct s2255_vc
*vc
= video_drvdata(file
);
763 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
765 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
770 field
= f
->fmt
.pix
.field
;
772 dprintk(vc
->dev
, 50, "%s NTSC: %d suggested width: %d, height: %d\n",
773 __func__
, is_ntsc
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
776 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
777 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
778 field
= V4L2_FIELD_INTERLACED
;
780 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
781 field
= V4L2_FIELD_TOP
;
783 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
784 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
786 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
789 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
790 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
791 field
= V4L2_FIELD_INTERLACED
;
793 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
794 field
= V4L2_FIELD_TOP
;
796 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
)
797 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
799 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
801 f
->fmt
.pix
.field
= field
;
802 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
803 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
804 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
805 dprintk(vc
->dev
, 50, "%s: set width %d height %d field %d\n", __func__
,
806 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
810 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
811 struct v4l2_format
*f
)
813 struct s2255_vc
*vc
= video_drvdata(file
);
814 const struct s2255_fmt
*fmt
;
815 struct vb2_queue
*q
= &vc
->vb_vidq
;
816 struct s2255_mode mode
;
819 ret
= vidioc_try_fmt_vid_cap(file
, vc
, f
);
824 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
829 if (vb2_is_busy(q
)) {
830 dprintk(vc
->dev
, 1, "queue busy\n");
836 vc
->width
= f
->fmt
.pix
.width
;
837 vc
->height
= f
->fmt
.pix
.height
;
838 vc
->field
= f
->fmt
.pix
.field
;
839 if (vc
->width
> norm_minw(vc
)) {
840 if (vc
->height
> norm_minh(vc
)) {
841 if (vc
->cap_parm
.capturemode
&
842 V4L2_MODE_HIGHQUALITY
)
843 mode
.scale
= SCALE_4CIFSI
;
845 mode
.scale
= SCALE_4CIFS
;
847 mode
.scale
= SCALE_2CIFS
;
850 mode
.scale
= SCALE_1CIFS
;
853 switch (vc
->fmt
->fourcc
) {
854 case V4L2_PIX_FMT_GREY
:
855 mode
.color
&= ~MASK_COLOR
;
856 mode
.color
|= COLOR_Y8
;
858 case V4L2_PIX_FMT_JPEG
:
859 case V4L2_PIX_FMT_MJPEG
:
860 mode
.color
&= ~MASK_COLOR
;
861 mode
.color
|= COLOR_JPG
;
862 mode
.color
|= (vc
->jpegqual
<< 8);
864 case V4L2_PIX_FMT_YUV422P
:
865 mode
.color
&= ~MASK_COLOR
;
866 mode
.color
|= COLOR_YUVPL
;
868 case V4L2_PIX_FMT_YUYV
:
869 case V4L2_PIX_FMT_UYVY
:
871 mode
.color
&= ~MASK_COLOR
;
872 mode
.color
|= COLOR_YUVPK
;
875 if ((mode
.color
& MASK_COLOR
) != (vc
->mode
.color
& MASK_COLOR
))
877 else if (mode
.scale
!= vc
->mode
.scale
)
879 else if (mode
.format
!= vc
->mode
.format
)
882 (void) s2255_set_mode(vc
, &mode
);
887 /* write to the configuration pipe, synchronously */
888 static int s2255_write_config(struct usb_device
*udev
, unsigned char *pbuf
,
895 pipe
= usb_sndbulkpipe(udev
, S2255_CONFIG_EP
);
896 retval
= usb_bulk_msg(udev
, pipe
, pbuf
, size
, &done
, 500);
901 static u32
get_transfer_size(struct s2255_mode
*mode
)
903 int linesPerFrame
= LINE_SZ_DEF
;
904 int pixelsPerLine
= NUM_LINES_DEF
;
907 unsigned int mask_mult
;
912 if (mode
->format
== FORMAT_NTSC
) {
913 switch (mode
->scale
) {
916 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
917 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
920 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
921 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
924 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
925 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
930 } else if (mode
->format
== FORMAT_PAL
) {
931 switch (mode
->scale
) {
934 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
935 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
938 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
939 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
942 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
943 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
949 outImageSize
= linesPerFrame
* pixelsPerLine
;
950 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
951 /* 2 bytes/pixel if not monochrome */
955 /* total bytes to send including prefix and 4K padding;
956 must be a multiple of USB_READ_SIZE */
957 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
958 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
959 /* if size not a multiple of USB_READ_SIZE */
960 if (usbInSize
& ~mask_mult
)
961 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
965 static void s2255_print_cfg(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
967 struct device
*dev
= &sdev
->udev
->dev
;
968 dev_info(dev
, "------------------------------------------------\n");
969 dev_info(dev
, "format: %d\nscale %d\n", mode
->format
, mode
->scale
);
970 dev_info(dev
, "fdec: %d\ncolor %d\n", mode
->fdec
, mode
->color
);
971 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
972 dev_info(dev
, "------------------------------------------------\n");
976 * set mode is the function which controls the DSP.
977 * the restart parameter in struct s2255_mode should be set whenever
978 * the image size could change via color format, video system or image
980 * When the restart parameter is set, we sleep for ONE frame to allow the
981 * DSP time to get the new frame
983 static int s2255_set_mode(struct s2255_vc
*vc
,
984 struct s2255_mode
*mode
)
987 unsigned long chn_rev
;
988 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
990 __le32
*buffer
= dev
->cmdbuf
;
992 mutex_lock(&dev
->cmdlock
);
993 chn_rev
= G_chnmap
[vc
->idx
];
994 dprintk(dev
, 3, "%s channel: %d\n", __func__
, vc
->idx
);
995 /* if JPEG, set the quality */
996 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
) {
997 mode
->color
&= ~MASK_COLOR
;
998 mode
->color
|= COLOR_JPG
;
999 mode
->color
&= ~MASK_JPG_QUALITY
;
1000 mode
->color
|= (vc
->jpegqual
<< 8);
1004 vc
->req_image_size
= get_transfer_size(mode
);
1005 dprintk(dev
, 1, "%s: reqsize %ld\n", __func__
, vc
->req_image_size
);
1007 buffer
[0] = IN_DATA_TOKEN
;
1008 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1009 buffer
[2] = CMD_SET_MODE
;
1010 for (i
= 0; i
< sizeof(struct s2255_mode
) / sizeof(u32
); i
++)
1011 buffer
[3 + i
] = cpu_to_le32(((u32
*)&vc
->mode
)[i
]);
1012 vc
->setmode_ready
= 0;
1013 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1015 s2255_print_cfg(dev
, mode
);
1016 /* wait at least 3 frames before continuing */
1017 if (mode
->restart
) {
1018 wait_event_timeout(vc
->wait_setmode
,
1019 (vc
->setmode_ready
!= 0),
1020 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1021 if (vc
->setmode_ready
!= 1) {
1022 dprintk(dev
, 0, "s2255: no set mode response\n");
1026 /* clear the restart flag */
1027 vc
->mode
.restart
= 0;
1028 dprintk(dev
, 1, "%s chn %d, result: %d\n", __func__
, vc
->idx
, res
);
1029 mutex_unlock(&dev
->cmdlock
);
1033 static int s2255_cmd_status(struct s2255_vc
*vc
, u32
*pstatus
)
1037 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
1038 __le32
*buffer
= dev
->cmdbuf
;
1040 mutex_lock(&dev
->cmdlock
);
1041 chn_rev
= G_chnmap
[vc
->idx
];
1042 dprintk(dev
, 4, "%s chan %d\n", __func__
, vc
->idx
);
1043 /* form the get vid status command */
1044 buffer
[0] = IN_DATA_TOKEN
;
1045 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
1046 buffer
[2] = CMD_STATUS
;
1048 vc
->vidstatus_ready
= 0;
1049 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1050 wait_event_timeout(vc
->wait_vidstatus
,
1051 (vc
->vidstatus_ready
!= 0),
1052 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT
));
1053 if (vc
->vidstatus_ready
!= 1) {
1054 dprintk(dev
, 0, "s2255: no vidstatus response\n");
1057 *pstatus
= vc
->vidstatus
;
1058 dprintk(dev
, 4, "%s, vid status %d\n", __func__
, *pstatus
);
1059 mutex_unlock(&dev
->cmdlock
);
1063 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1065 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
1068 vc
->last_frame
= -1;
1069 vc
->bad_payload
= 0;
1071 vc
->frame_count
= 0;
1072 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1073 vc
->buffer
.frame
[j
].ulState
= S2255_READ_IDLE
;
1074 vc
->buffer
.frame
[j
].cur_size
= 0;
1076 return s2255_start_acquire(vc
);
1079 /* abort streaming and wait for last buffer */
1080 static void stop_streaming(struct vb2_queue
*vq
)
1082 struct s2255_vc
*vc
= vb2_get_drv_priv(vq
);
1083 struct s2255_buffer
*buf
, *node
;
1084 unsigned long flags
;
1085 (void) s2255_stop_acquire(vc
);
1086 spin_lock_irqsave(&vc
->qlock
, flags
);
1087 list_for_each_entry_safe(buf
, node
, &vc
->buf_list
, list
) {
1088 list_del(&buf
->list
);
1089 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
1090 dprintk(vc
->dev
, 2, "[%p/%d] done\n",
1091 buf
, buf
->vb
.vb2_buf
.index
);
1093 spin_unlock_irqrestore(&vc
->qlock
, flags
);
1096 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id i
)
1098 struct s2255_vc
*vc
= video_drvdata(file
);
1099 struct s2255_mode mode
;
1100 struct vb2_queue
*q
= &vc
->vb_vidq
;
1103 * Changing the standard implies a format change, which is not allowed
1104 * while buffers for use with streaming have already been allocated.
1110 if (i
& V4L2_STD_525_60
) {
1111 dprintk(vc
->dev
, 4, "%s 60 Hz\n", __func__
);
1112 /* if changing format, reset frame decimation/intervals */
1113 if (mode
.format
!= FORMAT_NTSC
) {
1115 mode
.format
= FORMAT_NTSC
;
1117 vc
->width
= LINE_SZ_4CIFS_NTSC
;
1118 vc
->height
= NUM_LINES_4CIFS_NTSC
* 2;
1120 } else if (i
& V4L2_STD_625_50
) {
1121 dprintk(vc
->dev
, 4, "%s 50 Hz\n", __func__
);
1122 if (mode
.format
!= FORMAT_PAL
) {
1124 mode
.format
= FORMAT_PAL
;
1126 vc
->width
= LINE_SZ_4CIFS_PAL
;
1127 vc
->height
= NUM_LINES_4CIFS_PAL
* 2;
1133 s2255_set_mode(vc
, &mode
);
1137 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1139 struct s2255_vc
*vc
= video_drvdata(file
);
1145 /* Sensoray 2255 is a multiple channel capture device.
1146 It does not have a "crossbar" of inputs.
1147 We use one V4L device per channel. The user must
1148 be aware that certain combinations are not allowed.
1149 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1150 at once in color(you can do full fps on 4 channels with greyscale.
1152 static int vidioc_enum_input(struct file
*file
, void *priv
,
1153 struct v4l2_input
*inp
)
1155 struct s2255_vc
*vc
= video_drvdata(file
);
1156 struct s2255_dev
*dev
= vc
->dev
;
1159 if (inp
->index
!= 0)
1161 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1162 inp
->std
= S2255_NORMS
;
1164 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_STATUS
) {
1166 rc
= s2255_cmd_status(vc
, &status
);
1167 dprintk(dev
, 4, "s2255_cmd_status rc: %d status %x\n",
1170 inp
->status
= (status
& 0x01) ? 0
1171 : V4L2_IN_ST_NO_SIGNAL
;
1176 strscpy(inp
->name
, "Composite", sizeof(inp
->name
));
1179 strscpy(inp
->name
, (vc
->idx
< 2) ? "Composite" : "S-Video",
1186 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1191 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1198 static int s2255_s_ctrl(struct v4l2_ctrl
*ctrl
)
1200 struct s2255_vc
*vc
=
1201 container_of(ctrl
->handler
, struct s2255_vc
, hdl
);
1202 struct s2255_mode mode
;
1204 /* update the mode to the corresponding value */
1206 case V4L2_CID_BRIGHTNESS
:
1207 mode
.bright
= ctrl
->val
;
1209 case V4L2_CID_CONTRAST
:
1210 mode
.contrast
= ctrl
->val
;
1213 mode
.hue
= ctrl
->val
;
1215 case V4L2_CID_SATURATION
:
1216 mode
.saturation
= ctrl
->val
;
1218 case V4L2_CID_S2255_COLORFILTER
:
1219 mode
.color
&= ~MASK_INPUT_TYPE
;
1220 mode
.color
|= !ctrl
->val
<< 16;
1222 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
1223 vc
->jpegqual
= ctrl
->val
;
1229 /* set mode here. Note: stream does not need restarted.
1230 some V4L programs restart stream unnecessarily
1233 s2255_set_mode(vc
, &mode
);
1237 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1238 struct v4l2_jpegcompression
*jc
)
1240 struct s2255_vc
*vc
= video_drvdata(file
);
1242 memset(jc
, 0, sizeof(*jc
));
1243 jc
->quality
= vc
->jpegqual
;
1244 dprintk(vc
->dev
, 2, "%s: quality %d\n", __func__
, jc
->quality
);
1248 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1249 const struct v4l2_jpegcompression
*jc
)
1251 struct s2255_vc
*vc
= video_drvdata(file
);
1253 if (jc
->quality
< 0 || jc
->quality
> 100)
1255 v4l2_ctrl_s_ctrl(vc
->jpegqual_ctrl
, jc
->quality
);
1256 dprintk(vc
->dev
, 2, "%s: quality %d\n", __func__
, jc
->quality
);
1260 static int vidioc_g_parm(struct file
*file
, void *priv
,
1261 struct v4l2_streamparm
*sp
)
1263 __u32 def_num
, def_dem
;
1264 struct s2255_vc
*vc
= video_drvdata(file
);
1266 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1268 sp
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1269 sp
->parm
.capture
.capturemode
= vc
->cap_parm
.capturemode
;
1270 sp
->parm
.capture
.readbuffers
= S2255_MIN_BUFS
;
1271 def_num
= (vc
->mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1272 def_dem
= (vc
->mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1273 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1274 switch (vc
->mode
.fdec
) {
1277 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1280 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1283 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1286 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1289 dprintk(vc
->dev
, 4, "%s capture mode, %d timeperframe %d/%d\n",
1291 sp
->parm
.capture
.capturemode
,
1292 sp
->parm
.capture
.timeperframe
.numerator
,
1293 sp
->parm
.capture
.timeperframe
.denominator
);
1297 static int vidioc_s_parm(struct file
*file
, void *priv
,
1298 struct v4l2_streamparm
*sp
)
1300 struct s2255_vc
*vc
= video_drvdata(file
);
1301 struct s2255_mode mode
;
1303 __u32 def_num
, def_dem
;
1304 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1307 /* high quality capture mode requires a stream restart */
1308 if ((vc
->cap_parm
.capturemode
!= sp
->parm
.capture
.capturemode
)
1309 && vb2_is_streaming(&vc
->vb_vidq
))
1311 def_num
= (mode
.format
== FORMAT_NTSC
) ? 1001 : 1000;
1312 def_dem
= (mode
.format
== FORMAT_NTSC
) ? 30000 : 25000;
1313 if (def_dem
!= sp
->parm
.capture
.timeperframe
.denominator
)
1314 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1315 else if (sp
->parm
.capture
.timeperframe
.numerator
<= def_num
)
1316 sp
->parm
.capture
.timeperframe
.numerator
= def_num
;
1317 else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 2)) {
1318 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 2;
1320 } else if (sp
->parm
.capture
.timeperframe
.numerator
<= (def_num
* 3)) {
1321 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 3;
1324 sp
->parm
.capture
.timeperframe
.numerator
= def_num
* 5;
1328 sp
->parm
.capture
.timeperframe
.denominator
= def_dem
;
1329 sp
->parm
.capture
.readbuffers
= S2255_MIN_BUFS
;
1330 s2255_set_mode(vc
, &mode
);
1331 dprintk(vc
->dev
, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1333 sp
->parm
.capture
.capturemode
,
1334 sp
->parm
.capture
.timeperframe
.numerator
,
1335 sp
->parm
.capture
.timeperframe
.denominator
, fdec
);
1339 #define NUM_SIZE_ENUMS 3
1340 static const struct v4l2_frmsize_discrete ntsc_sizes
[] = {
1345 static const struct v4l2_frmsize_discrete pal_sizes
[] = {
1351 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1352 struct v4l2_frmsizeenum
*fe
)
1354 struct s2255_vc
*vc
= video_drvdata(file
);
1355 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
1356 const struct s2255_fmt
*fmt
;
1358 if (fe
->index
>= NUM_SIZE_ENUMS
)
1361 fmt
= format_by_fourcc(fe
->pixel_format
);
1364 fe
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1365 fe
->discrete
= is_ntsc
? ntsc_sizes
[fe
->index
] : pal_sizes
[fe
->index
];
1369 static int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1370 struct v4l2_frmivalenum
*fe
)
1372 struct s2255_vc
*vc
= video_drvdata(file
);
1373 const struct s2255_fmt
*fmt
;
1374 const struct v4l2_frmsize_discrete
*sizes
;
1375 int is_ntsc
= vc
->std
& V4L2_STD_525_60
;
1376 #define NUM_FRAME_ENUMS 4
1377 int frm_dec
[NUM_FRAME_ENUMS
] = {1, 2, 3, 5};
1380 if (fe
->index
>= NUM_FRAME_ENUMS
)
1383 fmt
= format_by_fourcc(fe
->pixel_format
);
1387 sizes
= is_ntsc
? ntsc_sizes
: pal_sizes
;
1388 for (i
= 0; i
< NUM_SIZE_ENUMS
; i
++, sizes
++)
1389 if (fe
->width
== sizes
->width
&&
1390 fe
->height
== sizes
->height
)
1392 if (i
== NUM_SIZE_ENUMS
)
1395 fe
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1396 fe
->discrete
.denominator
= is_ntsc
? 30000 : 25000;
1397 fe
->discrete
.numerator
= (is_ntsc
? 1001 : 1000) * frm_dec
[fe
->index
];
1398 dprintk(vc
->dev
, 4, "%s discrete %d/%d\n", __func__
,
1399 fe
->discrete
.numerator
,
1400 fe
->discrete
.denominator
);
1404 static int s2255_open(struct file
*file
)
1406 struct s2255_vc
*vc
= video_drvdata(file
);
1407 struct s2255_dev
*dev
= vc
->dev
;
1411 rc
= v4l2_fh_open(file
);
1415 dprintk(dev
, 1, "s2255: %s\n", __func__
);
1416 state
= atomic_read(&dev
->fw_data
->fw_state
);
1418 case S2255_FW_DISCONNECTING
:
1420 case S2255_FW_FAILED
:
1421 s2255_dev_err(&dev
->udev
->dev
,
1422 "firmware load failed. retrying.\n");
1423 s2255_fwload_start(dev
);
1424 wait_event_timeout(dev
->fw_data
->wait_fw
,
1425 ((atomic_read(&dev
->fw_data
->fw_state
)
1426 == S2255_FW_SUCCESS
) ||
1427 (atomic_read(&dev
->fw_data
->fw_state
)
1428 == S2255_FW_DISCONNECTING
)),
1429 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1430 /* state may have changed, re-read */
1431 state
= atomic_read(&dev
->fw_data
->fw_state
);
1433 case S2255_FW_NOTLOADED
:
1434 case S2255_FW_LOADED_DSPWAIT
:
1435 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1436 driver loaded and then device immediately opened */
1437 pr_info("%s waiting for firmware load\n", __func__
);
1438 wait_event_timeout(dev
->fw_data
->wait_fw
,
1439 ((atomic_read(&dev
->fw_data
->fw_state
)
1440 == S2255_FW_SUCCESS
) ||
1441 (atomic_read(&dev
->fw_data
->fw_state
)
1442 == S2255_FW_DISCONNECTING
)),
1443 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1444 /* state may have changed, re-read */
1445 state
= atomic_read(&dev
->fw_data
->fw_state
);
1447 case S2255_FW_SUCCESS
:
1451 /* state may have changed in above switch statement */
1453 case S2255_FW_SUCCESS
:
1455 case S2255_FW_FAILED
:
1456 pr_info("2255 firmware load failed.\n");
1458 case S2255_FW_DISCONNECTING
:
1459 pr_info("%s: disconnecting\n", __func__
);
1461 case S2255_FW_LOADED_DSPWAIT
:
1462 case S2255_FW_NOTLOADED
:
1463 pr_info("%s: firmware not loaded, please retry\n",
1466 * Timeout on firmware load means device unusable.
1467 * Set firmware failure state.
1468 * On next s2255_open the firmware will be reloaded.
1470 atomic_set(&dev
->fw_data
->fw_state
,
1474 pr_info("%s: unknown state\n", __func__
);
1477 if (!vc
->configured
) {
1478 /* configure channel to default state */
1479 vc
->fmt
= &formats
[0];
1480 s2255_set_mode(vc
, &vc
->mode
);
1486 static void s2255_destroy(struct s2255_dev
*dev
)
1488 dprintk(dev
, 1, "%s", __func__
);
1489 /* board shutdown stops the read pipe if it is running */
1490 s2255_board_shutdown(dev
);
1491 /* make sure firmware still not trying to load */
1492 del_timer_sync(&dev
->timer
); /* only started in .probe and .open */
1493 if (dev
->fw_data
->fw_urb
) {
1494 usb_kill_urb(dev
->fw_data
->fw_urb
);
1495 usb_free_urb(dev
->fw_data
->fw_urb
);
1496 dev
->fw_data
->fw_urb
= NULL
;
1498 release_firmware(dev
->fw_data
->fw
);
1499 kfree(dev
->fw_data
->pfw_data
);
1500 kfree(dev
->fw_data
);
1501 /* reset the DSP so firmware can be reloaded next time */
1502 s2255_reset_dsppower(dev
);
1503 mutex_destroy(&dev
->lock
);
1504 usb_put_dev(dev
->udev
);
1505 v4l2_device_unregister(&dev
->v4l2_dev
);
1510 static const struct v4l2_file_operations s2255_fops_v4l
= {
1511 .owner
= THIS_MODULE
,
1513 .release
= vb2_fop_release
,
1514 .poll
= vb2_fop_poll
,
1515 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1516 .mmap
= vb2_fop_mmap
,
1517 .read
= vb2_fop_read
,
1520 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1521 .vidioc_querycap
= vidioc_querycap
,
1522 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1523 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1524 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1525 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1526 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1527 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1528 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1529 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1530 .vidioc_s_std
= vidioc_s_std
,
1531 .vidioc_g_std
= vidioc_g_std
,
1532 .vidioc_enum_input
= vidioc_enum_input
,
1533 .vidioc_g_input
= vidioc_g_input
,
1534 .vidioc_s_input
= vidioc_s_input
,
1535 .vidioc_streamon
= vb2_ioctl_streamon
,
1536 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1537 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1538 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1539 .vidioc_s_parm
= vidioc_s_parm
,
1540 .vidioc_g_parm
= vidioc_g_parm
,
1541 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1542 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
1543 .vidioc_log_status
= v4l2_ctrl_log_status
,
1544 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1545 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1548 static void s2255_video_device_release(struct video_device
*vdev
)
1550 struct s2255_dev
*dev
= to_s2255_dev(vdev
->v4l2_dev
);
1551 struct s2255_vc
*vc
=
1552 container_of(vdev
, struct s2255_vc
, vdev
);
1554 dprintk(dev
, 4, "%s, chnls: %d\n", __func__
,
1555 atomic_read(&dev
->num_channels
));
1557 v4l2_ctrl_handler_free(&vc
->hdl
);
1559 if (atomic_dec_and_test(&dev
->num_channels
))
1564 static const struct video_device
template = {
1566 .fops
= &s2255_fops_v4l
,
1567 .ioctl_ops
= &s2255_ioctl_ops
,
1568 .release
= s2255_video_device_release
,
1569 .tvnorms
= S2255_NORMS
,
1572 static const struct v4l2_ctrl_ops s2255_ctrl_ops
= {
1573 .s_ctrl
= s2255_s_ctrl
,
1576 static const struct v4l2_ctrl_config color_filter_ctrl
= {
1577 .ops
= &s2255_ctrl_ops
,
1578 .name
= "Color Filter",
1579 .id
= V4L2_CID_S2255_COLORFILTER
,
1580 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1586 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1590 int cur_nr
= video_nr
;
1591 struct s2255_vc
*vc
;
1592 struct vb2_queue
*q
;
1594 ret
= v4l2_device_register(&dev
->interface
->dev
, &dev
->v4l2_dev
);
1597 /* initialize all video 4 linux */
1598 /* register 4 video devices */
1599 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1601 INIT_LIST_HEAD(&vc
->buf_list
);
1603 v4l2_ctrl_handler_init(&vc
->hdl
, 6);
1604 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1605 V4L2_CID_BRIGHTNESS
, -127, 127, 1, DEF_BRIGHT
);
1606 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1607 V4L2_CID_CONTRAST
, 0, 255, 1, DEF_CONTRAST
);
1608 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1609 V4L2_CID_SATURATION
, 0, 255, 1, DEF_SATURATION
);
1610 v4l2_ctrl_new_std(&vc
->hdl
, &s2255_ctrl_ops
,
1611 V4L2_CID_HUE
, 0, 255, 1, DEF_HUE
);
1612 vc
->jpegqual_ctrl
= v4l2_ctrl_new_std(&vc
->hdl
,
1614 V4L2_CID_JPEG_COMPRESSION_QUALITY
,
1615 0, 100, 1, S2255_DEF_JPEG_QUAL
);
1616 if (dev
->dsp_fw_ver
>= S2255_MIN_DSP_COLORFILTER
&&
1617 (dev
->pid
!= 0x2257 || vc
->idx
<= 1))
1618 v4l2_ctrl_new_custom(&vc
->hdl
, &color_filter_ctrl
,
1620 if (vc
->hdl
.error
) {
1621 ret
= vc
->hdl
.error
;
1622 v4l2_ctrl_handler_free(&vc
->hdl
);
1623 dev_err(&dev
->udev
->dev
, "couldn't register control\n");
1627 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1628 q
->io_modes
= VB2_MMAP
| VB2_READ
| VB2_USERPTR
;
1630 q
->lock
= &vc
->vb_lock
;
1631 q
->buf_struct_size
= sizeof(struct s2255_buffer
);
1632 q
->mem_ops
= &vb2_vmalloc_memops
;
1633 q
->ops
= &s2255_video_qops
;
1634 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1635 ret
= vb2_queue_init(q
);
1637 dev_err(&dev
->udev
->dev
,
1638 "%s vb2_queue_init 0x%x\n", __func__
, ret
);
1641 /* register video devices */
1642 vc
->vdev
= template;
1644 vc
->vdev
.ctrl_handler
= &vc
->hdl
;
1645 vc
->vdev
.lock
= &dev
->lock
;
1646 vc
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1647 vc
->vdev
.device_caps
= V4L2_CAP_VIDEO_CAPTURE
|
1648 V4L2_CAP_STREAMING
| V4L2_CAP_READWRITE
;
1649 video_set_drvdata(&vc
->vdev
, vc
);
1651 ret
= video_register_device(&vc
->vdev
,
1655 ret
= video_register_device(&vc
->vdev
,
1660 dev_err(&dev
->udev
->dev
,
1661 "failed to register video device!\n");
1664 atomic_inc(&dev
->num_channels
);
1665 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1666 video_device_node_name(&vc
->vdev
));
1669 pr_info("Sensoray 2255 V4L driver Revision: %s\n",
1671 /* if no channels registered, return error and probe will fail*/
1672 if (atomic_read(&dev
->num_channels
) == 0) {
1673 v4l2_device_unregister(&dev
->v4l2_dev
);
1676 if (atomic_read(&dev
->num_channels
) != MAX_CHANNELS
)
1677 pr_warn("s2255: Not all channels available.\n");
1681 /* this function moves the usb stream read pipe data
1682 * into the system buffers.
1683 * returns 0 on success, EAGAIN if more data to process( call this
1686 * Received frame structure:
1687 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1688 * bytes 4-7: channel: 0-3
1689 * bytes 8-11: payload size: size of the frame
1690 * bytes 12-payloadsize+12: frame data
1692 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
1698 unsigned long copy_size
;
1701 struct s2255_framei
*frm
;
1702 unsigned char *pdata
;
1703 struct s2255_vc
*vc
;
1704 dprintk(dev
, 100, "buffer to user\n");
1705 vc
= &dev
->vc
[dev
->cc
];
1706 idx
= vc
->cur_frame
;
1707 frm
= &vc
->buffer
.frame
[idx
];
1708 if (frm
->ulState
== S2255_READ_IDLE
) {
1711 __le32
*pdword
; /*data from dsp is little endian */
1713 /* search for marker codes */
1714 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
1715 pdword
= (__le32
*)pdata
;
1716 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
1718 case S2255_MARKER_FRAME
:
1719 dprintk(dev
, 4, "marker @ offset: %d [%x %x]\n",
1720 jj
, pdata
[0], pdata
[1]);
1721 offset
= jj
+ PREFIX_SIZE
;
1723 cc
= le32_to_cpu(pdword
[1]);
1724 if (cc
>= MAX_CHANNELS
) {
1730 dev
->cc
= G_chnmap
[cc
];
1731 vc
= &dev
->vc
[dev
->cc
];
1732 payload
= le32_to_cpu(pdword
[3]);
1733 if (payload
> vc
->req_image_size
) {
1735 /* discard the bad frame */
1738 vc
->pkt_size
= payload
;
1739 vc
->jpg_size
= le32_to_cpu(pdword
[4]);
1741 case S2255_MARKER_RESPONSE
:
1743 pdata
+= DEF_USB_BLOCK
;
1744 jj
+= DEF_USB_BLOCK
;
1745 if (le32_to_cpu(pdword
[1]) >= MAX_CHANNELS
)
1747 cc
= G_chnmap
[le32_to_cpu(pdword
[1])];
1748 if (cc
>= MAX_CHANNELS
)
1751 switch (pdword
[2]) {
1752 case S2255_RESPONSE_SETMODE
:
1753 /* check if channel valid */
1754 /* set mode ready */
1755 vc
->setmode_ready
= 1;
1756 wake_up(&vc
->wait_setmode
);
1757 dprintk(dev
, 5, "setmode rdy %d\n", cc
);
1759 case S2255_RESPONSE_FW
:
1760 dev
->chn_ready
|= (1 << cc
);
1761 if ((dev
->chn_ready
& 0x0f) != 0x0f)
1763 /* all channels ready */
1764 pr_info("s2255: fw loaded\n");
1765 atomic_set(&dev
->fw_data
->fw_state
,
1767 wake_up(&dev
->fw_data
->wait_fw
);
1769 case S2255_RESPONSE_STATUS
:
1770 vc
->vidstatus
= le32_to_cpu(pdword
[3]);
1771 vc
->vidstatus_ready
= 1;
1772 wake_up(&vc
->wait_vidstatus
);
1773 dprintk(dev
, 5, "vstat %x chan %d\n",
1774 le32_to_cpu(pdword
[3]), cc
);
1777 pr_info("s2255 unknown resp\n");
1791 vc
= &dev
->vc
[dev
->cc
];
1792 idx
= vc
->cur_frame
;
1793 frm
= &vc
->buffer
.frame
[idx
];
1794 /* search done. now find out if should be acquiring on this channel */
1795 if (!vb2_is_streaming(&vc
->vb_vidq
)) {
1796 /* we found a frame, but this channel is turned off */
1797 frm
->ulState
= S2255_READ_IDLE
;
1801 if (frm
->ulState
== S2255_READ_IDLE
) {
1802 frm
->ulState
= S2255_READ_FRAME
;
1806 /* skip the marker 512 bytes (and offset if out of sync) */
1807 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
1810 if (frm
->lpvbits
== NULL
) {
1811 dprintk(dev
, 1, "s2255 frame buffer == NULL.%p %p %d %d",
1812 frm
, dev
, dev
->cc
, idx
);
1816 pdest
= frm
->lpvbits
+ frm
->cur_size
;
1818 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
1820 size
= vc
->pkt_size
- PREFIX_SIZE
;
1822 /* sanity check on pdest */
1823 if ((copy_size
+ frm
->cur_size
) < vc
->req_image_size
)
1824 memcpy(pdest
, psrc
, copy_size
);
1826 frm
->cur_size
+= copy_size
;
1827 dprintk(dev
, 4, "cur_size: %lu, size: %lu\n", frm
->cur_size
, size
);
1829 if (frm
->cur_size
>= size
) {
1830 dprintk(dev
, 2, "******[%d]Buffer[%d]full*******\n",
1832 vc
->last_frame
= vc
->cur_frame
;
1834 /* end of system frame ring buffer, start at zero */
1835 if ((vc
->cur_frame
== SYS_FRAMES
) ||
1836 (vc
->cur_frame
== vc
->buffer
.dwFrames
))
1839 if (vb2_is_streaming(&vc
->vb_vidq
))
1840 s2255_got_frame(vc
, vc
->jpg_size
);
1842 frm
->ulState
= S2255_READ_IDLE
;
1846 /* done successfully */
1850 static void s2255_read_video_callback(struct s2255_dev
*dev
,
1851 struct s2255_pipeinfo
*pipe_info
)
1854 dprintk(dev
, 50, "callback read video\n");
1856 if (dev
->cc
>= MAX_CHANNELS
) {
1858 dev_err(&dev
->udev
->dev
, "invalid channel\n");
1861 /* otherwise copy to the system buffers */
1862 res
= save_frame(dev
, pipe_info
);
1864 dprintk(dev
, 4, "s2255: read callback failed\n");
1866 dprintk(dev
, 50, "callback read video done\n");
1870 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
1871 u16 Index
, u16 Value
, void *TransferBuffer
,
1872 s32 TransferBufferLength
, int bOut
)
1877 buf
= kmalloc(TransferBufferLength
, GFP_KERNEL
);
1882 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
1884 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
1887 TransferBufferLength
, HZ
* 5);
1890 memcpy(TransferBuffer
, buf
, TransferBufferLength
);
1892 memcpy(buf
, TransferBuffer
, TransferBufferLength
);
1893 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1894 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
1896 TransferBufferLength
, HZ
* 5);
1903 * retrieve FX2 firmware version. future use.
1904 * @param dev pointer to device extension
1905 * @return -1 for fail, else returns firmware version as an int(16 bits)
1907 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
1911 unsigned char transBuffer
[64];
1912 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
1915 dprintk(dev
, 2, "get fw error: %x\n", ret
);
1916 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
1917 dprintk(dev
, 2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
1922 * Create the system ring buffer to copy frames into from the
1925 static int s2255_create_sys_buffers(struct s2255_vc
*vc
)
1928 unsigned long reqsize
;
1929 vc
->buffer
.dwFrames
= SYS_FRAMES
;
1930 /* always allocate maximum size(PAL) for system buffers */
1931 reqsize
= SYS_FRAMES_MAXSIZE
;
1933 if (reqsize
> SYS_FRAMES_MAXSIZE
)
1934 reqsize
= SYS_FRAMES_MAXSIZE
;
1936 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1937 /* allocate the frames */
1938 vc
->buffer
.frame
[i
].lpvbits
= vmalloc(reqsize
);
1939 vc
->buffer
.frame
[i
].size
= reqsize
;
1940 if (vc
->buffer
.frame
[i
].lpvbits
== NULL
) {
1941 pr_info("out of memory. using less frames\n");
1942 vc
->buffer
.dwFrames
= i
;
1947 /* make sure internal states are set */
1948 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1949 vc
->buffer
.frame
[i
].ulState
= 0;
1950 vc
->buffer
.frame
[i
].cur_size
= 0;
1954 vc
->last_frame
= -1;
1958 static int s2255_release_sys_buffers(struct s2255_vc
*vc
)
1961 for (i
= 0; i
< SYS_FRAMES
; i
++) {
1962 vfree(vc
->buffer
.frame
[i
].lpvbits
);
1963 vc
->buffer
.frame
[i
].lpvbits
= NULL
;
1968 static int s2255_board_init(struct s2255_dev
*dev
)
1970 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
1973 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
1974 dprintk(dev
, 4, "board init: %p", dev
);
1975 memset(pipe
, 0, sizeof(*pipe
));
1977 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
1978 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
1980 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
1982 if (pipe
->transfer_buffer
== NULL
) {
1983 dprintk(dev
, 1, "out of memory!\n");
1986 /* query the firmware */
1987 fw_ver
= s2255_get_fx2fw(dev
);
1989 pr_info("s2255: usb firmware version %d.%d\n",
1990 (fw_ver
>> 8) & 0xff,
1993 if (fw_ver
< S2255_CUR_USB_FWVER
)
1994 pr_info("s2255: newer USB firmware available\n");
1996 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
1997 struct s2255_vc
*vc
= &dev
->vc
[j
];
1998 vc
->mode
= mode_def
;
1999 if (dev
->pid
== 0x2257 && j
> 1)
2000 vc
->mode
.color
|= (1 << 16);
2001 vc
->jpegqual
= S2255_DEF_JPEG_QUAL
;
2002 vc
->width
= LINE_SZ_4CIFS_NTSC
;
2003 vc
->height
= NUM_LINES_4CIFS_NTSC
* 2;
2004 vc
->std
= V4L2_STD_NTSC_M
;
2005 vc
->fmt
= &formats
[0];
2006 vc
->mode
.restart
= 1;
2007 vc
->req_image_size
= get_transfer_size(&mode_def
);
2008 vc
->frame_count
= 0;
2009 /* create the system buffers */
2010 s2255_create_sys_buffers(vc
);
2012 /* start read pipe */
2013 s2255_start_readpipe(dev
);
2014 dprintk(dev
, 1, "%s: success\n", __func__
);
2018 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2021 dprintk(dev
, 1, "%s: dev: %p", __func__
, dev
);
2023 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2024 if (vb2_is_streaming(&dev
->vc
[i
].vb_vidq
))
2025 s2255_stop_acquire(&dev
->vc
[i
]);
2027 s2255_stop_readpipe(dev
);
2028 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2029 s2255_release_sys_buffers(&dev
->vc
[i
]);
2030 /* release transfer buffer */
2031 kfree(dev
->pipe
.transfer_buffer
);
2035 static void read_pipe_completion(struct urb
*purb
)
2037 struct s2255_pipeinfo
*pipe_info
;
2038 struct s2255_dev
*dev
;
2041 pipe_info
= purb
->context
;
2042 if (pipe_info
== NULL
) {
2043 dev_err(&purb
->dev
->dev
, "no context!\n");
2046 dev
= pipe_info
->dev
;
2048 dev_err(&purb
->dev
->dev
, "no context!\n");
2051 status
= purb
->status
;
2052 /* if shutting down, do not resubmit, exit immediately */
2053 if (status
== -ESHUTDOWN
) {
2054 dprintk(dev
, 2, "%s: err shutdown\n", __func__
);
2055 pipe_info
->err_count
++;
2059 if (pipe_info
->state
== 0) {
2060 dprintk(dev
, 2, "%s: exiting USB pipe", __func__
);
2065 s2255_read_video_callback(dev
, pipe_info
);
2067 pipe_info
->err_count
++;
2068 dprintk(dev
, 1, "%s: failed URB %d\n", __func__
, status
);
2071 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2073 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2075 pipe_info
->transfer_buffer
,
2076 pipe_info
->cur_transfer_size
,
2077 read_pipe_completion
, pipe_info
);
2079 if (pipe_info
->state
!= 0) {
2080 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_ATOMIC
))
2081 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2083 dprintk(dev
, 2, "%s :complete state 0\n", __func__
);
2088 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2092 struct s2255_pipeinfo
*pipe_info
= &dev
->pipe
;
2093 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2094 dprintk(dev
, 2, "%s: IN %d\n", __func__
, dev
->read_endpoint
);
2095 pipe_info
->state
= 1;
2096 pipe_info
->err_count
= 0;
2097 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2098 if (!pipe_info
->stream_urb
)
2100 /* transfer buffer allocated in board_init */
2101 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2103 pipe_info
->transfer_buffer
,
2104 pipe_info
->cur_transfer_size
,
2105 read_pipe_completion
, pipe_info
);
2106 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2108 pr_err("s2255: start read pipe failed\n");
2114 /* starts acquisition process */
2115 static int s2255_start_acquire(struct s2255_vc
*vc
)
2118 unsigned long chn_rev
;
2120 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
2121 __le32
*buffer
= dev
->cmdbuf
;
2123 mutex_lock(&dev
->cmdlock
);
2124 chn_rev
= G_chnmap
[vc
->idx
];
2125 vc
->last_frame
= -1;
2126 vc
->bad_payload
= 0;
2128 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2129 vc
->buffer
.frame
[j
].ulState
= 0;
2130 vc
->buffer
.frame
[j
].cur_size
= 0;
2133 /* send the start command */
2134 buffer
[0] = IN_DATA_TOKEN
;
2135 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
2136 buffer
[2] = CMD_START
;
2137 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2139 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2141 dprintk(dev
, 2, "start acquire exit[%d] %d\n", vc
->idx
, res
);
2142 mutex_unlock(&dev
->cmdlock
);
2146 static int s2255_stop_acquire(struct s2255_vc
*vc
)
2149 unsigned long chn_rev
;
2150 struct s2255_dev
*dev
= to_s2255_dev(vc
->vdev
.v4l2_dev
);
2151 __le32
*buffer
= dev
->cmdbuf
;
2153 mutex_lock(&dev
->cmdlock
);
2154 chn_rev
= G_chnmap
[vc
->idx
];
2155 /* send the stop command */
2156 buffer
[0] = IN_DATA_TOKEN
;
2157 buffer
[1] = (__le32
) cpu_to_le32(chn_rev
);
2158 buffer
[2] = CMD_STOP
;
2160 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2162 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2164 dprintk(dev
, 4, "%s: chn %d, res %d\n", __func__
, vc
->idx
, res
);
2165 mutex_unlock(&dev
->cmdlock
);
2169 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2171 struct s2255_pipeinfo
*pipe
= &dev
->pipe
;
2174 if (pipe
->stream_urb
) {
2176 usb_kill_urb(pipe
->stream_urb
);
2177 usb_free_urb(pipe
->stream_urb
);
2178 pipe
->stream_urb
= NULL
;
2180 dprintk(dev
, 4, "%s", __func__
);
2184 static void s2255_fwload_start(struct s2255_dev
*dev
)
2186 s2255_reset_dsppower(dev
);
2187 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2188 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2189 memcpy(dev
->fw_data
->pfw_data
,
2190 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2191 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2192 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2193 usb_sndbulkpipe(dev
->udev
, 2),
2194 dev
->fw_data
->pfw_data
,
2195 CHUNK_SIZE
, s2255_fwchunk_complete
,
2197 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2200 /* standard usb probe function */
2201 static int s2255_probe(struct usb_interface
*interface
,
2202 const struct usb_device_id
*id
)
2204 struct s2255_dev
*dev
= NULL
;
2205 struct usb_host_interface
*iface_desc
;
2206 struct usb_endpoint_descriptor
*endpoint
;
2208 int retval
= -ENOMEM
;
2212 /* allocate memory for our device state and initialize it to zero */
2213 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2215 s2255_dev_err(&interface
->dev
, "out of memory\n");
2219 dev
->cmdbuf
= kzalloc(S2255_CMDBUF_SIZE
, GFP_KERNEL
);
2220 if (dev
->cmdbuf
== NULL
) {
2221 s2255_dev_err(&interface
->dev
, "out of memory\n");
2225 atomic_set(&dev
->num_channels
, 0);
2226 dev
->pid
= id
->idProduct
;
2227 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2230 mutex_init(&dev
->lock
);
2231 mutex_init(&dev
->cmdlock
);
2232 /* grab usb_device and save it */
2233 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2234 if (dev
->udev
== NULL
) {
2235 dev_err(&interface
->dev
, "null usb device\n");
2239 dev_dbg(&interface
->dev
, "dev: %p, udev %p interface %p\n",
2240 dev
, dev
->udev
, interface
);
2241 dev
->interface
= interface
;
2242 /* set up the endpoint information */
2243 iface_desc
= interface
->cur_altsetting
;
2244 dev_dbg(&interface
->dev
, "num EP: %d\n",
2245 iface_desc
->desc
.bNumEndpoints
);
2246 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2247 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2248 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2249 /* we found the bulk in endpoint */
2250 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2254 if (!dev
->read_endpoint
) {
2255 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2258 timer_setup(&dev
->timer
, s2255_timer
, 0);
2259 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2260 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2261 struct s2255_vc
*vc
= &dev
->vc
[i
];
2264 init_waitqueue_head(&vc
->wait_setmode
);
2265 init_waitqueue_head(&vc
->wait_vidstatus
);
2266 spin_lock_init(&vc
->qlock
);
2267 mutex_init(&vc
->vb_lock
);
2270 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2271 if (!dev
->fw_data
->fw_urb
)
2274 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2275 if (!dev
->fw_data
->pfw_data
) {
2276 dev_err(&interface
->dev
, "out of memory!\n");
2279 /* load the first chunk */
2280 if (request_firmware(&dev
->fw_data
->fw
,
2281 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2282 dev_err(&interface
->dev
, "sensoray 2255 failed to get firmware\n");
2285 /* check the firmware is valid */
2286 fw_size
= dev
->fw_data
->fw
->size
;
2287 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2289 if (*pdata
!= S2255_FW_MARKER
) {
2290 dev_err(&interface
->dev
, "Firmware invalid.\n");
2294 /* make sure firmware is the latest */
2296 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2297 pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel
));
2298 dev
->dsp_fw_ver
= le32_to_cpu(*pRel
);
2299 if (dev
->dsp_fw_ver
< S2255_CUR_DSP_FWVER
)
2300 pr_info("s2255: f2255usb.bin out of date.\n");
2301 if (dev
->pid
== 0x2257 &&
2302 dev
->dsp_fw_ver
< S2255_MIN_DSP_COLORFILTER
)
2303 pr_warn("2257 needs firmware %d or above.\n",
2304 S2255_MIN_DSP_COLORFILTER
);
2306 usb_reset_device(dev
->udev
);
2307 /* load 2255 board specific */
2308 retval
= s2255_board_init(dev
);
2310 goto errorBOARDINIT
;
2311 s2255_fwload_start(dev
);
2312 /* loads v4l specific */
2313 retval
= s2255_probe_v4l(dev
);
2315 goto errorBOARDINIT
;
2316 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2319 s2255_board_shutdown(dev
);
2321 release_firmware(dev
->fw_data
->fw
);
2323 kfree(dev
->fw_data
->pfw_data
);
2325 usb_free_urb(dev
->fw_data
->fw_urb
);
2327 del_timer_sync(&dev
->timer
);
2329 usb_put_dev(dev
->udev
);
2331 kfree(dev
->fw_data
);
2332 mutex_destroy(&dev
->lock
);
2336 pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval
);
2340 /* disconnect routine. when board is removed physically or with rmmod */
2341 static void s2255_disconnect(struct usb_interface
*interface
)
2343 struct s2255_dev
*dev
= to_s2255_dev(usb_get_intfdata(interface
));
2345 int channels
= atomic_read(&dev
->num_channels
);
2346 mutex_lock(&dev
->lock
);
2347 v4l2_device_disconnect(&dev
->v4l2_dev
);
2348 mutex_unlock(&dev
->lock
);
2349 /*see comments in the uvc_driver.c usb disconnect function */
2350 atomic_inc(&dev
->num_channels
);
2351 /* unregister each video device. */
2352 for (i
= 0; i
< channels
; i
++)
2353 video_unregister_device(&dev
->vc
[i
].vdev
);
2354 /* wake up any of our timers */
2355 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2356 wake_up(&dev
->fw_data
->wait_fw
);
2357 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2358 dev
->vc
[i
].setmode_ready
= 1;
2359 wake_up(&dev
->vc
[i
].wait_setmode
);
2360 dev
->vc
[i
].vidstatus_ready
= 1;
2361 wake_up(&dev
->vc
[i
].wait_vidstatus
);
2363 if (atomic_dec_and_test(&dev
->num_channels
))
2365 dev_info(&interface
->dev
, "%s\n", __func__
);
2368 static struct usb_driver s2255_driver
= {
2369 .name
= S2255_DRIVER_NAME
,
2370 .probe
= s2255_probe
,
2371 .disconnect
= s2255_disconnect
,
2372 .id_table
= s2255_table
,
2375 module_usb_driver(s2255_driver
);
2377 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2378 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2379 MODULE_LICENSE("GPL");
2380 MODULE_VERSION(S2255_VERSION
);
2381 MODULE_FIRMWARE(FIRMWARE_FILE_NAME
);