2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4 * Copyright (C) 2007-2008 by Sensoray Company Inc.
7 * Some video buffer code based on vivi driver:
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
16 * Example maximum bandwidth utilization:
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
20 * -full or half size Grey scale: all 4 channels at once
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #include <linux/module.h>
45 #include <linux/firmware.h>
46 #include <linux/kernel.h>
47 #include <linux/mutex.h>
48 #include <linux/videodev2.h>
49 #include <linux/version.h>
51 #include <linux/smp_lock.h>
52 #include <media/videobuf-vmalloc.h>
53 #include <media/v4l2-common.h>
54 #include <media/v4l2-ioctl.h>
55 #include <linux/vmalloc.h>
56 #include <linux/usb.h>
58 #define FIRMWARE_FILE_NAME "f2255usb.bin"
62 /* default JPEG quality */
63 #define S2255_DEF_JPEG_QUAL 50
64 /* vendor request in */
66 /* vendor request out */
67 #define S2255_VR_OUT 1
69 #define S2255_VR_FW 0x30
70 /* USB endpoint number for configuring the device */
71 #define S2255_CONFIG_EP 2
72 /* maximum time for DSP to start responding after last FW word loaded(ms) */
73 #define S2255_DSP_BOOTTIME 800
74 /* maximum time to wait for firmware to load (ms) */
75 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
76 #define S2255_DEF_BUFS 16
77 #define S2255_SETMODE_TIMEOUT 500
78 #define MAX_CHANNELS 4
79 #define S2255_MARKER_FRAME 0x2255DA4AL
80 #define S2255_MARKER_RESPONSE 0x2255ACACL
81 #define S2255_RESPONSE_SETMODE 0x01
82 #define S2255_RESPONSE_FW 0x10
83 #define S2255_USB_XFER_SIZE (16 * 1024)
84 #define MAX_CHANNELS 4
85 #define MAX_PIPE_BUFFERS 1
87 /* maximum size is PAL full size plus room for the marker header(s) */
88 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
89 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
90 #define LINE_SZ_4CIFS_NTSC 640
91 #define LINE_SZ_2CIFS_NTSC 640
92 #define LINE_SZ_1CIFS_NTSC 320
93 #define LINE_SZ_4CIFS_PAL 704
94 #define LINE_SZ_2CIFS_PAL 704
95 #define LINE_SZ_1CIFS_PAL 352
96 #define NUM_LINES_4CIFS_NTSC 240
97 #define NUM_LINES_2CIFS_NTSC 240
98 #define NUM_LINES_1CIFS_NTSC 240
99 #define NUM_LINES_4CIFS_PAL 288
100 #define NUM_LINES_2CIFS_PAL 288
101 #define NUM_LINES_1CIFS_PAL 288
102 #define LINE_SZ_DEF 640
103 #define NUM_LINES_DEF 240
106 /* predefined settings */
107 #define FORMAT_NTSC 1
110 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
111 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
112 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
113 /* SCALE_4CIFSI is the 2 fields interpolated into one */
114 #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
116 #define COLOR_YUVPL 1 /* YUV planar */
117 #define COLOR_YUVPK 2 /* YUV packed */
118 #define COLOR_Y8 4 /* monochrome */
119 #define COLOR_JPG 5 /* JPEG */
120 #define MASK_COLOR 0xff
121 #define MASK_JPG_QUALITY 0xff00
123 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
124 #define FDEC_1 1 /* capture every frame. default */
125 #define FDEC_2 2 /* capture every 2nd frame */
126 #define FDEC_3 3 /* capture every 3rd frame */
127 #define FDEC_5 5 /* capture every 5th frame */
129 /*-------------------------------------------------------
130 * Default mode parameters.
131 *-------------------------------------------------------*/
132 #define DEF_SCALE SCALE_4CIFS
133 #define DEF_COLOR COLOR_YUVPL
134 #define DEF_FDEC FDEC_1
136 #define DEF_CONTRAST 0x5c
137 #define DEF_SATURATION 0x80
140 /* usb config commands */
141 #define IN_DATA_TOKEN 0x2255c0de
142 #define CMD_2255 0xc2255000
143 #define CMD_SET_MODE (CMD_2255 | 0x10)
144 #define CMD_START (CMD_2255 | 0x20)
145 #define CMD_STOP (CMD_2255 | 0x30)
146 #define CMD_STATUS (CMD_2255 | 0x40)
149 u32 format
; /* input video format (NTSC, PAL) */
150 u32 scale
; /* output video scale */
151 u32 color
; /* output video color format */
152 u32 fdec
; /* frame decimation */
153 u32 bright
; /* brightness */
154 u32 contrast
; /* contrast */
155 u32 saturation
; /* saturation */
156 u32 hue
; /* hue (NTSC only)*/
157 u32 single
; /* capture 1 frame at a time (!=0), continuously (==0)*/
158 u32 usb_block
; /* block size. should be 4096 of DEF_USB_BLOCK */
159 u32 restart
; /* if DSP requires restart */
163 #define S2255_READ_IDLE 0
164 #define S2255_READ_FRAME 1
166 /* frame structure */
167 struct s2255_framei
{
169 unsigned long ulState
; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
170 void *lpvbits
; /* image data */
171 unsigned long cur_size
; /* current data copied to it */
174 /* image buffer structure */
175 struct s2255_bufferi
{
176 unsigned long dwFrames
; /* number of frames in buffer */
177 struct s2255_framei frame
[SYS_FRAMES
]; /* array of FRAME structures */
180 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
181 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
182 DEF_HUE, 0, DEF_USB_BLOCK, 0}
184 struct s2255_dmaqueue
{
185 struct list_head active
;
186 struct s2255_dev
*dev
;
190 /* for firmware loading, fw_state */
191 #define S2255_FW_NOTLOADED 0
192 #define S2255_FW_LOADED_DSPWAIT 1
193 #define S2255_FW_SUCCESS 2
194 #define S2255_FW_FAILED 3
195 #define S2255_FW_DISCONNECTING 4
197 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
198 /* 2255 read states */
199 #define S2255_READ_IDLE 0
200 #define S2255_READ_FRAME 1
207 wait_queue_head_t wait_fw
;
208 const struct firmware
*fw
;
211 struct s2255_pipeinfo
{
212 u32 max_transfer_size
;
213 u32 cur_transfer_size
;
217 void *dev
; /* back pointer to s2255_dev struct*/
222 struct s2255_fmt
; /*forward declaration */
226 int users
[MAX_CHANNELS
];
228 struct mutex open_lock
;
229 int resources
[MAX_CHANNELS
];
230 struct usb_device
*udev
;
231 struct usb_interface
*interface
;
234 struct s2255_dmaqueue vidq
[MAX_CHANNELS
];
235 struct video_device
*vdev
[MAX_CHANNELS
];
236 struct list_head s2255_devlist
;
237 struct timer_list timer
;
238 struct s2255_fw
*fw_data
;
239 struct s2255_pipeinfo pipes
[MAX_PIPE_BUFFERS
];
240 struct s2255_bufferi buffer
[MAX_CHANNELS
];
241 struct s2255_mode mode
[MAX_CHANNELS
];
242 /* jpeg compression */
243 struct v4l2_jpegcompression jc
[MAX_CHANNELS
];
244 /* capture parameters (for high quality mode full size) */
245 struct v4l2_captureparm cap_parm
[MAX_CHANNELS
];
246 const struct s2255_fmt
*cur_fmt
[MAX_CHANNELS
];
247 int cur_frame
[MAX_CHANNELS
];
248 int last_frame
[MAX_CHANNELS
];
249 u32 cc
; /* current channel */
250 int b_acquire
[MAX_CHANNELS
];
251 /* allocated image size */
252 unsigned long req_image_size
[MAX_CHANNELS
];
253 /* received packet size */
254 unsigned long pkt_size
[MAX_CHANNELS
];
255 int bad_payload
[MAX_CHANNELS
];
256 unsigned long frame_count
[MAX_CHANNELS
];
259 int jpg_size
[MAX_CHANNELS
];
260 /* if channel configured to default state */
261 int chn_configured
[MAX_CHANNELS
];
262 wait_queue_head_t wait_setmode
[MAX_CHANNELS
];
263 int setmode_ready
[MAX_CHANNELS
];
268 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
276 /* buffer for one video frame */
277 struct s2255_buffer
{
278 /* common v4l buffer stuff -- must be first */
279 struct videobuf_buffer vb
;
280 const struct s2255_fmt
*fmt
;
284 struct s2255_dev
*dev
;
285 const struct s2255_fmt
*fmt
;
288 struct videobuf_queue vb_vidq
;
289 enum v4l2_buf_type type
;
291 /* mode below is the desired mode.
292 mode in s2255_dev is the current mode that was last set */
293 struct s2255_mode mode
;
294 int resources
[MAX_CHANNELS
];
297 /* current cypress EEPROM firmware version */
298 #define S2255_CUR_USB_FWVER ((3 << 8) | 6)
299 #define S2255_MAJOR_VERSION 1
300 #define S2255_MINOR_VERSION 14
301 #define S2255_RELEASE 0
302 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
303 S2255_MINOR_VERSION, \
307 #define USB_S2255_VENDOR_ID 0x1943
308 #define USB_S2255_PRODUCT_ID 0x2255
309 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
310 /* frame prefix size (sent once every frame) */
311 #define PREFIX_SIZE 512
313 /* Channels on box are in reverse order */
314 static unsigned long G_chnmap
[MAX_CHANNELS
] = {3, 2, 1, 0};
316 static LIST_HEAD(s2255_devlist
);
319 static int *s2255_debug
= &debug
;
321 static int s2255_start_readpipe(struct s2255_dev
*dev
);
322 static void s2255_stop_readpipe(struct s2255_dev
*dev
);
323 static int s2255_start_acquire(struct s2255_dev
*dev
, unsigned long chn
);
324 static int s2255_stop_acquire(struct s2255_dev
*dev
, unsigned long chn
);
325 static void s2255_fillbuff(struct s2255_dev
*dev
, struct s2255_buffer
*buf
,
326 int chn
, int jpgsize
);
327 static int s2255_set_mode(struct s2255_dev
*dev
, unsigned long chn
,
328 struct s2255_mode
*mode
);
329 static int s2255_board_shutdown(struct s2255_dev
*dev
);
330 static void s2255_exit_v4l(struct s2255_dev
*dev
);
331 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
);
332 static void s2255_destroy(struct kref
*kref
);
333 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char req
,
334 u16 index
, u16 value
, void *buf
,
335 s32 buf_len
, int bOut
);
337 /* dev_err macro with driver name */
338 #define S2255_DRIVER_NAME "s2255"
339 #define s2255_dev_err(dev, fmt, arg...) \
340 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
342 #define dprintk(level, fmt, arg...) \
344 if (*s2255_debug >= (level)) { \
345 printk(KERN_DEBUG S2255_DRIVER_NAME \
350 static struct usb_driver s2255_driver
;
353 /* Declare static vars that will be used as parameters */
354 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
356 /* start video number */
357 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
359 module_param(debug
, int, 0644);
360 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
361 module_param(vid_limit
, int, 0644);
362 MODULE_PARM_DESC(vid_limit
, "video memory limit(Mb)");
363 module_param(video_nr
, int, 0644);
364 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
366 /* USB device table */
367 static struct usb_device_id s2255_table
[] = {
368 {USB_DEVICE(USB_S2255_VENDOR_ID
, USB_S2255_PRODUCT_ID
)},
369 { } /* Terminating entry */
371 MODULE_DEVICE_TABLE(usb
, s2255_table
);
374 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
376 /* supported controls */
377 static struct v4l2_queryctrl s2255_qctrl
[] = {
379 .id
= V4L2_CID_BRIGHTNESS
,
380 .type
= V4L2_CTRL_TYPE_INTEGER
,
381 .name
= "Brightness",
388 .id
= V4L2_CID_CONTRAST
,
389 .type
= V4L2_CTRL_TYPE_INTEGER
,
394 .default_value
= DEF_CONTRAST
,
397 .id
= V4L2_CID_SATURATION
,
398 .type
= V4L2_CTRL_TYPE_INTEGER
,
399 .name
= "Saturation",
403 .default_value
= DEF_SATURATION
,
407 .type
= V4L2_CTRL_TYPE_INTEGER
,
412 .default_value
= DEF_HUE
,
417 static int qctl_regs
[ARRAY_SIZE(s2255_qctrl
)];
420 static const struct s2255_fmt formats
[] = {
422 .name
= "4:2:2, planar, YUV422P",
423 .fourcc
= V4L2_PIX_FMT_YUV422P
,
427 .name
= "4:2:2, packed, YUYV",
428 .fourcc
= V4L2_PIX_FMT_YUYV
,
432 .name
= "4:2:2, packed, UYVY",
433 .fourcc
= V4L2_PIX_FMT_UYVY
,
437 .fourcc
= V4L2_PIX_FMT_JPEG
,
441 .fourcc
= V4L2_PIX_FMT_GREY
,
446 static int norm_maxw(struct video_device
*vdev
)
448 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
449 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
452 static int norm_maxh(struct video_device
*vdev
)
454 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
455 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
458 static int norm_minw(struct video_device
*vdev
)
460 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
461 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
464 static int norm_minh(struct video_device
*vdev
)
466 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
467 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
472 * TODO: fixme: move YUV reordering to hardware
473 * converts 2255 planar format to yuyv or uyvy
475 static void planar422p_to_yuv_packed(const unsigned char *in
,
477 int width
, int height
,
483 unsigned long size
= height
* width
;
485 pY
= (unsigned char *)in
;
486 pCr
= (unsigned char *)in
+ height
* width
;
487 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
488 for (i
= 0; i
< size
* 2; i
+= 4) {
489 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
490 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
491 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
492 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
497 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
499 s2255_vendor_req(dev
, 0x40, 0x0b0b, 0x0b0b, NULL
, 0, 1);
501 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
505 /* kickstarts the firmware loading. from probe
507 static void s2255_timer(unsigned long user_data
)
509 struct s2255_fw
*data
= (struct s2255_fw
*)user_data
;
510 dprintk(100, "s2255 timer\n");
511 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
512 printk(KERN_ERR
"s2255: can't submit urb\n");
513 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
514 /* wake up anything waiting for the firmware */
515 wake_up(&data
->wait_fw
);
521 /* this loads the firmware asynchronously.
522 Originally this was done synchroously in probe.
523 But it is better to load it asynchronously here than block
524 inside the probe function. Blocking inside probe affects boot time.
525 FW loading is triggered by the timer in the probe function
527 static void s2255_fwchunk_complete(struct urb
*urb
)
529 struct s2255_fw
*data
= urb
->context
;
530 struct usb_device
*udev
= urb
->dev
;
532 dprintk(100, "udev %p urb %p", udev
, urb
);
534 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
535 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data
->wait_fw
);
540 if (data
->fw_urb
== NULL
) {
541 s2255_dev_err(&udev
->dev
, "disconnected\n");
542 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
543 /* wake up anything waiting for the firmware */
544 wake_up(&data
->wait_fw
);
547 #define CHUNK_SIZE 512
548 /* all USB transfers must be done with continuous kernel memory.
549 can't allocate more than 128k in current linux kernel, so
550 upload the firmware in chunks
552 if (data
->fw_loaded
< data
->fw_size
) {
553 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
554 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
556 if (len
< CHUNK_SIZE
)
557 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
559 dprintk(100, "completed len %d, loaded %d \n", len
,
562 memcpy(data
->pfw_data
,
563 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
565 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
566 data
->pfw_data
, CHUNK_SIZE
,
567 s2255_fwchunk_complete
, data
);
568 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
569 dev_err(&udev
->dev
, "failed submit URB\n");
570 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
571 /* wake up anything waiting for the firmware */
572 wake_up(&data
->wait_fw
);
575 data
->fw_loaded
+= len
;
577 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
579 dprintk(100, "2255 complete done\n");
584 static int s2255_got_frame(struct s2255_dev
*dev
, int chn
, int jpgsize
)
586 struct s2255_dmaqueue
*dma_q
= &dev
->vidq
[chn
];
587 struct s2255_buffer
*buf
;
588 unsigned long flags
= 0;
590 dprintk(2, "wakeup: %p channel: %d\n", &dma_q
, chn
);
591 spin_lock_irqsave(&dev
->slock
, flags
);
593 if (list_empty(&dma_q
->active
)) {
594 dprintk(1, "No active queue to serve\n");
598 buf
= list_entry(dma_q
->active
.next
,
599 struct s2255_buffer
, vb
.queue
);
601 list_del(&buf
->vb
.queue
);
602 do_gettimeofday(&buf
->vb
.ts
);
603 dprintk(100, "[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
604 s2255_fillbuff(dev
, buf
, dma_q
->channel
, jpgsize
);
605 wake_up(&buf
->vb
.done
);
606 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf
, buf
->vb
.i
);
608 spin_unlock_irqrestore(&dev
->slock
, flags
);
613 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
617 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
618 if (-1 == formats
[i
].fourcc
)
620 if (formats
[i
].fourcc
== fourcc
)
629 /* video buffer vmalloc implementation based partly on VIVI driver which is
630 * Copyright (c) 2006 by
631 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
632 * Ted Walther <ted--a.t--enumera.com>
633 * John Sokol <sokol--a.t--videotechnology.com>
634 * http://v4l.videotechnology.com/
637 static void s2255_fillbuff(struct s2255_dev
*dev
, struct s2255_buffer
*buf
,
638 int chn
, int jpgsize
)
643 char *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
644 unsigned long last_frame
;
645 struct s2255_framei
*frm
;
650 last_frame
= dev
->last_frame
[chn
];
651 if (last_frame
!= -1) {
652 frm
= &dev
->buffer
[chn
].frame
[last_frame
];
654 (const char *)dev
->buffer
[chn
].frame
[last_frame
].lpvbits
;
655 switch (buf
->fmt
->fourcc
) {
656 case V4L2_PIX_FMT_YUYV
:
657 case V4L2_PIX_FMT_UYVY
:
658 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
663 case V4L2_PIX_FMT_GREY
:
664 memcpy(vbuf
, tmpbuf
, buf
->vb
.width
* buf
->vb
.height
);
666 case V4L2_PIX_FMT_JPEG
:
667 buf
->vb
.size
= jpgsize
;
668 memcpy(vbuf
, tmpbuf
, buf
->vb
.size
);
670 case V4L2_PIX_FMT_YUV422P
:
672 buf
->vb
.width
* buf
->vb
.height
* 2);
675 printk(KERN_DEBUG
"s2255: unknown format?\n");
677 dev
->last_frame
[chn
] = -1;
679 printk(KERN_ERR
"s2255: =======no frame\n");
683 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
684 (unsigned long)vbuf
, pos
);
685 /* tell v4l buffer was filled */
687 buf
->vb
.field_count
= dev
->frame_count
[chn
] * 2;
688 do_gettimeofday(&ts
);
690 buf
->vb
.state
= VIDEOBUF_DONE
;
694 /* ------------------------------------------------------------------
696 ------------------------------------------------------------------*/
698 static int buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
701 struct s2255_fh
*fh
= vq
->priv_data
;
703 *size
= fh
->width
* fh
->height
* (fh
->fmt
->depth
>> 3);
706 *count
= S2255_DEF_BUFS
;
708 while (*size
* (*count
) > vid_limit
* 1024 * 1024)
714 static void free_buffer(struct videobuf_queue
*vq
, struct s2255_buffer
*buf
)
716 dprintk(4, "%s\n", __func__
);
718 videobuf_vmalloc_free(&buf
->vb
);
719 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
722 static int buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
723 enum v4l2_field field
)
725 struct s2255_fh
*fh
= vq
->priv_data
;
726 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
728 dprintk(4, "%s, field=%d\n", __func__
, field
);
732 if ((fh
->width
< norm_minw(fh
->dev
->vdev
[fh
->channel
])) ||
733 (fh
->width
> norm_maxw(fh
->dev
->vdev
[fh
->channel
])) ||
734 (fh
->height
< norm_minh(fh
->dev
->vdev
[fh
->channel
])) ||
735 (fh
->height
> norm_maxh(fh
->dev
->vdev
[fh
->channel
]))) {
736 dprintk(4, "invalid buffer prepare\n");
740 buf
->vb
.size
= fh
->width
* fh
->height
* (fh
->fmt
->depth
>> 3);
742 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
) {
743 dprintk(4, "invalid buffer prepare\n");
748 buf
->vb
.width
= fh
->width
;
749 buf
->vb
.height
= fh
->height
;
750 buf
->vb
.field
= field
;
753 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
754 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
759 buf
->vb
.state
= VIDEOBUF_PREPARED
;
762 free_buffer(vq
, buf
);
766 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
768 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
769 struct s2255_fh
*fh
= vq
->priv_data
;
770 struct s2255_dev
*dev
= fh
->dev
;
771 struct s2255_dmaqueue
*vidq
= &dev
->vidq
[fh
->channel
];
773 dprintk(1, "%s\n", __func__
);
775 buf
->vb
.state
= VIDEOBUF_QUEUED
;
776 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
779 static void buffer_release(struct videobuf_queue
*vq
,
780 struct videobuf_buffer
*vb
)
782 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
783 struct s2255_fh
*fh
= vq
->priv_data
;
784 dprintk(4, "%s %d\n", __func__
, fh
->channel
);
785 free_buffer(vq
, buf
);
788 static struct videobuf_queue_ops s2255_video_qops
= {
789 .buf_setup
= buffer_setup
,
790 .buf_prepare
= buffer_prepare
,
791 .buf_queue
= buffer_queue
,
792 .buf_release
= buffer_release
,
796 static int res_get(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
799 mutex_lock(&dev
->lock
);
800 if (dev
->resources
[fh
->channel
]) {
801 /* no, someone else uses it */
802 mutex_unlock(&dev
->lock
);
805 /* it's free, grab it */
806 dev
->resources
[fh
->channel
] = 1;
807 fh
->resources
[fh
->channel
] = 1;
808 dprintk(1, "s2255: res: get\n");
809 mutex_unlock(&dev
->lock
);
813 static int res_locked(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
815 return dev
->resources
[fh
->channel
];
818 static int res_check(struct s2255_fh
*fh
)
820 return fh
->resources
[fh
->channel
];
824 static void res_free(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
826 mutex_lock(&dev
->lock
);
827 dev
->resources
[fh
->channel
] = 0;
828 fh
->resources
[fh
->channel
] = 0;
829 mutex_unlock(&dev
->lock
);
830 dprintk(1, "res: put\n");
834 static int vidioc_querycap(struct file
*file
, void *priv
,
835 struct v4l2_capability
*cap
)
837 struct s2255_fh
*fh
= file
->private_data
;
838 struct s2255_dev
*dev
= fh
->dev
;
839 strlcpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
840 strlcpy(cap
->card
, "s2255", sizeof(cap
->card
));
841 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
842 cap
->version
= S2255_VERSION
;
843 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
847 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
848 struct v4l2_fmtdesc
*f
)
854 if (index
>= ARRAY_SIZE(formats
))
857 dprintk(4, "name %s\n", formats
[index
].name
);
858 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
859 f
->pixelformat
= formats
[index
].fourcc
;
863 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
864 struct v4l2_format
*f
)
866 struct s2255_fh
*fh
= priv
;
868 f
->fmt
.pix
.width
= fh
->width
;
869 f
->fmt
.pix
.height
= fh
->height
;
870 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
871 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
872 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (fh
->fmt
->depth
>> 3);
873 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
877 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
878 struct v4l2_format
*f
)
880 const struct s2255_fmt
*fmt
;
881 enum v4l2_field field
;
883 struct s2255_fh
*fh
= priv
;
884 struct s2255_dev
*dev
= fh
->dev
;
888 (dev
->vdev
[fh
->channel
]->current_norm
& V4L2_STD_NTSC
) ? 1 : 0;
890 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
895 field
= f
->fmt
.pix
.field
;
896 if (field
== V4L2_FIELD_ANY
)
899 dprintk(4, "try format %d \n", is_ntsc
);
900 /* supports 3 sizes. see s2255drv.h */
901 dprintk(50, "width test %d, height %d\n",
902 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
905 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
906 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
908 field
= V4L2_FIELD_SEQ_TB
;
909 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
910 (field
== V4L2_FIELD_SEQ_TB
) ||
911 (field
== V4L2_FIELD_INTERLACED_TB
))) {
912 dprintk(1, "unsupported field setting\n");
916 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
918 field
= V4L2_FIELD_TOP
;
919 } else if (!((field
== V4L2_FIELD_TOP
) ||
920 (field
== V4L2_FIELD_BOTTOM
))) {
921 dprintk(1, "unsupported field setting\n");
926 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
927 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
928 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_NTSC
)
929 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_NTSC
;
930 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_NTSC
)
931 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
933 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
936 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
937 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
939 field
= V4L2_FIELD_SEQ_TB
;
940 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
941 (field
== V4L2_FIELD_SEQ_TB
) ||
942 (field
== V4L2_FIELD_INTERLACED_TB
))) {
943 dprintk(1, "unsupported field setting\n");
947 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
949 field
= V4L2_FIELD_TOP
;
950 } else if (!((field
== V4L2_FIELD_TOP
) ||
951 (field
== V4L2_FIELD_BOTTOM
))) {
952 dprintk(1, "unsupported field setting\n");
956 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
) {
957 dprintk(50, "pal 704\n");
958 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
959 field
= V4L2_FIELD_SEQ_TB
;
960 } else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_PAL
) {
961 dprintk(50, "pal 352A\n");
962 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_PAL
;
963 field
= V4L2_FIELD_TOP
;
964 } else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_PAL
) {
965 dprintk(50, "pal 352B\n");
966 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
967 field
= V4L2_FIELD_TOP
;
969 dprintk(50, "pal 352C\n");
970 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
971 field
= V4L2_FIELD_TOP
;
975 dprintk(50, "width %d height %d field %d \n", f
->fmt
.pix
.width
,
976 f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
977 f
->fmt
.pix
.field
= field
;
978 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
979 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
983 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
984 struct v4l2_format
*f
)
986 struct s2255_fh
*fh
= priv
;
987 const struct s2255_fmt
*fmt
;
988 struct videobuf_queue
*q
= &fh
->vb_vidq
;
992 ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
997 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1002 mutex_lock(&q
->vb_lock
);
1004 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1005 dprintk(1, "queue busy\n");
1010 if (res_locked(fh
->dev
, fh
)) {
1011 dprintk(1, "can't change format after started\n");
1017 fh
->width
= f
->fmt
.pix
.width
;
1018 fh
->height
= f
->fmt
.pix
.height
;
1019 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1021 norm
= norm_minw(fh
->dev
->vdev
[fh
->channel
]);
1022 if (fh
->width
> norm_minw(fh
->dev
->vdev
[fh
->channel
])) {
1023 if (fh
->height
> norm_minh(fh
->dev
->vdev
[fh
->channel
])) {
1024 if (fh
->dev
->cap_parm
[fh
->channel
].capturemode
&
1025 V4L2_MODE_HIGHQUALITY
) {
1026 fh
->mode
.scale
= SCALE_4CIFSI
;
1027 dprintk(2, "scale 4CIFSI\n");
1029 fh
->mode
.scale
= SCALE_4CIFS
;
1030 dprintk(2, "scale 4CIFS\n");
1033 fh
->mode
.scale
= SCALE_2CIFS
;
1036 fh
->mode
.scale
= SCALE_1CIFS
;
1040 switch (fh
->fmt
->fourcc
) {
1041 case V4L2_PIX_FMT_GREY
:
1042 fh
->mode
.color
= COLOR_Y8
;
1044 case V4L2_PIX_FMT_JPEG
:
1045 fh
->mode
.color
= COLOR_JPG
|
1046 (fh
->dev
->jc
[fh
->channel
].quality
<< 8);
1048 case V4L2_PIX_FMT_YUV422P
:
1049 fh
->mode
.color
= COLOR_YUVPL
;
1051 case V4L2_PIX_FMT_YUYV
:
1052 case V4L2_PIX_FMT_UYVY
:
1054 fh
->mode
.color
= COLOR_YUVPK
;
1059 mutex_unlock(&q
->vb_lock
);
1063 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1064 struct v4l2_requestbuffers
*p
)
1067 struct s2255_fh
*fh
= priv
;
1068 rc
= videobuf_reqbufs(&fh
->vb_vidq
, p
);
1072 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1075 struct s2255_fh
*fh
= priv
;
1076 rc
= videobuf_querybuf(&fh
->vb_vidq
, p
);
1080 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1083 struct s2255_fh
*fh
= priv
;
1084 rc
= videobuf_qbuf(&fh
->vb_vidq
, p
);
1088 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1091 struct s2255_fh
*fh
= priv
;
1092 rc
= videobuf_dqbuf(&fh
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
1096 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1097 static int vidioc_cgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1099 struct s2255_fh
*fh
= priv
;
1101 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1105 /* write to the configuration pipe, synchronously */
1106 static int s2255_write_config(struct usb_device
*udev
, unsigned char *pbuf
,
1113 pipe
= usb_sndbulkpipe(udev
, S2255_CONFIG_EP
);
1114 retval
= usb_bulk_msg(udev
, pipe
, pbuf
, size
, &done
, 500);
1119 static u32
get_transfer_size(struct s2255_mode
*mode
)
1121 int linesPerFrame
= LINE_SZ_DEF
;
1122 int pixelsPerLine
= NUM_LINES_DEF
;
1125 unsigned int mask_mult
;
1130 if (mode
->format
== FORMAT_NTSC
) {
1131 switch (mode
->scale
) {
1134 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
1135 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
1138 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
1139 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
1142 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
1143 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
1148 } else if (mode
->format
== FORMAT_PAL
) {
1149 switch (mode
->scale
) {
1152 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
1153 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
1156 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
1157 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
1160 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
1161 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
1167 outImageSize
= linesPerFrame
* pixelsPerLine
;
1168 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
1169 /* 2 bytes/pixel if not monochrome */
1173 /* total bytes to send including prefix and 4K padding;
1174 must be a multiple of USB_READ_SIZE */
1175 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
1176 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
1177 /* if size not a multiple of USB_READ_SIZE */
1178 if (usbInSize
& ~mask_mult
)
1179 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
1183 static void dump_verify_mode(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
1185 struct device
*dev
= &sdev
->udev
->dev
;
1186 dev_info(dev
, "------------------------------------------------\n");
1187 dev_info(dev
, "verify mode\n");
1188 dev_info(dev
, "format: %d\n", mode
->format
);
1189 dev_info(dev
, "scale: %d\n", mode
->scale
);
1190 dev_info(dev
, "fdec: %d\n", mode
->fdec
);
1191 dev_info(dev
, "color: %d\n", mode
->color
);
1192 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
1193 dev_info(dev
, "restart: 0x%x\n", mode
->restart
);
1194 dev_info(dev
, "usb_block: 0x%x\n", mode
->usb_block
);
1195 dev_info(dev
, "single: 0x%x\n", mode
->single
);
1196 dev_info(dev
, "------------------------------------------------\n");
1200 * set mode is the function which controls the DSP.
1201 * the restart parameter in struct s2255_mode should be set whenever
1202 * the image size could change via color format, video system or image
1204 * When the restart parameter is set, we sleep for ONE frame to allow the
1205 * DSP time to get the new frame
1207 static int s2255_set_mode(struct s2255_dev
*dev
, unsigned long chn
,
1208 struct s2255_mode
*mode
)
1212 unsigned long chn_rev
;
1214 mutex_lock(&dev
->lock
);
1215 chn_rev
= G_chnmap
[chn
];
1216 dprintk(3, "mode scale [%ld] %p %d\n", chn
, mode
, mode
->scale
);
1217 dprintk(3, "mode scale [%ld] %p %d\n", chn
, &dev
->mode
[chn
],
1218 dev
->mode
[chn
].scale
);
1219 dprintk(2, "mode contrast %x\n", mode
->contrast
);
1221 /* if JPEG, set the quality */
1222 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
)
1223 mode
->color
= (dev
->jc
[chn
].quality
<< 8) | COLOR_JPG
;
1226 dev
->mode
[chn
] = *mode
;
1227 dev
->req_image_size
[chn
] = get_transfer_size(mode
);
1228 dprintk(1, "transfer size %ld\n", dev
->req_image_size
[chn
]);
1230 buffer
= kzalloc(512, GFP_KERNEL
);
1231 if (buffer
== NULL
) {
1232 dev_err(&dev
->udev
->dev
, "out of mem\n");
1233 mutex_unlock(&dev
->lock
);
1238 buffer
[0] = IN_DATA_TOKEN
;
1239 buffer
[1] = (u32
) chn_rev
;
1240 buffer
[2] = CMD_SET_MODE
;
1241 memcpy(&buffer
[3], &dev
->mode
[chn
], sizeof(struct s2255_mode
));
1242 dev
->setmode_ready
[chn
] = 0;
1243 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1245 dump_verify_mode(dev
, mode
);
1247 dprintk(1, "set mode done chn %lu, %d\n", chn
, res
);
1249 /* wait at least 3 frames before continuing */
1250 if (mode
->restart
) {
1251 wait_event_timeout(dev
->wait_setmode
[chn
],
1252 (dev
->setmode_ready
[chn
] != 0),
1253 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1254 if (dev
->setmode_ready
[chn
] != 1) {
1255 printk(KERN_DEBUG
"s2255: no set mode response\n");
1260 /* clear the restart flag */
1261 dev
->mode
[chn
].restart
= 0;
1262 mutex_unlock(&dev
->lock
);
1266 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1269 struct s2255_fh
*fh
= priv
;
1270 struct s2255_dev
*dev
= fh
->dev
;
1271 struct s2255_mode
*new_mode
;
1272 struct s2255_mode
*old_mode
;
1275 dprintk(4, "%s\n", __func__
);
1276 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1277 dev_err(&dev
->udev
->dev
, "invalid fh type0\n");
1280 if (i
!= fh
->type
) {
1281 dev_err(&dev
->udev
->dev
, "invalid fh type1\n");
1285 if (!res_get(dev
, fh
)) {
1286 s2255_dev_err(&dev
->udev
->dev
, "stream busy\n");
1290 /* send a set mode command everytime with restart.
1291 in case we switch resolutions or other parameters */
1293 new_mode
= &fh
->mode
;
1294 old_mode
= &fh
->dev
->mode
[chn
];
1296 if (new_mode
->color
!= old_mode
->color
)
1297 new_mode
->restart
= 1;
1298 else if (new_mode
->scale
!= old_mode
->scale
)
1299 new_mode
->restart
= 1;
1300 else if (new_mode
->format
!= old_mode
->format
)
1301 new_mode
->restart
= 1;
1303 s2255_set_mode(dev
, chn
, new_mode
);
1304 new_mode
->restart
= 0;
1305 *old_mode
= *new_mode
;
1306 dev
->cur_fmt
[chn
] = fh
->fmt
;
1307 dprintk(1, "%s[%d]\n", __func__
, chn
);
1308 dev
->last_frame
[chn
] = -1;
1309 dev
->bad_payload
[chn
] = 0;
1310 dev
->cur_frame
[chn
] = 0;
1311 dev
->frame_count
[chn
] = 0;
1312 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1313 dev
->buffer
[chn
].frame
[j
].ulState
= S2255_READ_IDLE
;
1314 dev
->buffer
[chn
].frame
[j
].cur_size
= 0;
1316 res
= videobuf_streamon(&fh
->vb_vidq
);
1318 s2255_start_acquire(dev
, chn
);
1319 dev
->b_acquire
[chn
] = 1;
1326 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1328 struct s2255_fh
*fh
= priv
;
1329 struct s2255_dev
*dev
= fh
->dev
;
1331 dprintk(4, "%s\n, channel: %d", __func__
, fh
->channel
);
1332 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1333 printk(KERN_ERR
"invalid fh type0\n");
1336 if (i
!= fh
->type
) {
1337 printk(KERN_ERR
"invalid type i\n");
1340 s2255_stop_acquire(dev
, fh
->channel
);
1341 videobuf_streamoff(&fh
->vb_vidq
);
1346 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1348 struct s2255_fh
*fh
= priv
;
1349 struct s2255_mode
*mode
;
1350 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1353 mutex_lock(&q
->vb_lock
);
1354 if (videobuf_queue_is_busy(q
)) {
1355 dprintk(1, "queue busy\n");
1360 if (res_locked(fh
->dev
, fh
)) {
1361 dprintk(1, "can't change standard after started\n");
1367 if (*i
& V4L2_STD_NTSC
) {
1368 dprintk(4, "vidioc_s_std NTSC\n");
1369 mode
->format
= FORMAT_NTSC
;
1370 } else if (*i
& V4L2_STD_PAL
) {
1371 dprintk(4, "vidioc_s_std PAL\n");
1372 mode
->format
= FORMAT_PAL
;
1377 mutex_unlock(&q
->vb_lock
);
1381 /* Sensoray 2255 is a multiple channel capture device.
1382 It does not have a "crossbar" of inputs.
1383 We use one V4L device per channel. The user must
1384 be aware that certain combinations are not allowed.
1385 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1386 at once in color(you can do full fps on 4 channels with greyscale.
1388 static int vidioc_enum_input(struct file
*file
, void *priv
,
1389 struct v4l2_input
*inp
)
1391 if (inp
->index
!= 0)
1394 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1395 inp
->std
= S2255_NORMS
;
1396 strlcpy(inp
->name
, "Camera", sizeof(inp
->name
));
1400 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1405 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1412 /* --- controls ---------------------------------------------- */
1413 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1414 struct v4l2_queryctrl
*qc
)
1418 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1419 if (qc
->id
&& qc
->id
== s2255_qctrl
[i
].id
) {
1420 memcpy(qc
, &(s2255_qctrl
[i
]), sizeof(*qc
));
1424 dprintk(4, "query_ctrl -EINVAL %d\n", qc
->id
);
1428 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1429 struct v4l2_control
*ctrl
)
1433 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1434 if (ctrl
->id
== s2255_qctrl
[i
].id
) {
1435 ctrl
->value
= qctl_regs
[i
];
1438 dprintk(4, "g_ctrl -EINVAL\n");
1443 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1444 struct v4l2_control
*ctrl
)
1447 struct s2255_fh
*fh
= priv
;
1448 struct s2255_dev
*dev
= fh
->dev
;
1449 struct s2255_mode
*mode
;
1451 dprintk(4, "vidioc_s_ctrl\n");
1452 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++) {
1453 if (ctrl
->id
== s2255_qctrl
[i
].id
) {
1454 if (ctrl
->value
< s2255_qctrl
[i
].minimum
||
1455 ctrl
->value
> s2255_qctrl
[i
].maximum
)
1458 qctl_regs
[i
] = ctrl
->value
;
1459 /* update the mode to the corresponding value */
1461 case V4L2_CID_BRIGHTNESS
:
1462 mode
->bright
= ctrl
->value
;
1464 case V4L2_CID_CONTRAST
:
1465 mode
->contrast
= ctrl
->value
;
1468 mode
->hue
= ctrl
->value
;
1470 case V4L2_CID_SATURATION
:
1471 mode
->saturation
= ctrl
->value
;
1475 /* set mode here. Note: stream does not need restarted.
1476 some V4L programs restart stream unnecessarily
1479 s2255_set_mode(dev
, fh
->channel
, mode
);
1486 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1487 struct v4l2_jpegcompression
*jc
)
1489 struct s2255_fh
*fh
= priv
;
1490 struct s2255_dev
*dev
= fh
->dev
;
1491 *jc
= dev
->jc
[fh
->channel
];
1492 dprintk(2, "getting jpegcompression, quality %d\n", jc
->quality
);
1496 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1497 struct v4l2_jpegcompression
*jc
)
1499 struct s2255_fh
*fh
= priv
;
1500 struct s2255_dev
*dev
= fh
->dev
;
1501 if (jc
->quality
< 0 || jc
->quality
> 100)
1503 dev
->jc
[fh
->channel
].quality
= jc
->quality
;
1504 dprintk(2, "setting jpeg quality %d\n", jc
->quality
);
1508 static int vidioc_g_parm(struct file
*file
, void *priv
,
1509 struct v4l2_streamparm
*sp
)
1511 struct s2255_fh
*fh
= priv
;
1512 struct s2255_dev
*dev
= fh
->dev
;
1513 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1515 sp
->parm
.capture
.capturemode
= dev
->cap_parm
[fh
->channel
].capturemode
;
1516 dprintk(2, "getting parm %d\n", sp
->parm
.capture
.capturemode
);
1520 static int vidioc_s_parm(struct file
*file
, void *priv
,
1521 struct v4l2_streamparm
*sp
)
1523 struct s2255_fh
*fh
= priv
;
1524 struct s2255_dev
*dev
= fh
->dev
;
1526 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1529 dev
->cap_parm
[fh
->channel
].capturemode
= sp
->parm
.capture
.capturemode
;
1530 dprintk(2, "setting param capture mode %d\n",
1531 sp
->parm
.capture
.capturemode
);
1534 static int s2255_open(struct file
*file
)
1536 int minor
= video_devdata(file
)->minor
;
1537 struct s2255_dev
*h
, *dev
= NULL
;
1538 struct s2255_fh
*fh
;
1539 struct list_head
*list
;
1540 enum v4l2_buf_type type
= 0;
1542 int cur_channel
= -1;
1544 dprintk(1, "s2255: open called (minor=%d)\n", minor
);
1547 list_for_each(list
, &s2255_devlist
) {
1548 h
= list_entry(list
, struct s2255_dev
, s2255_devlist
);
1549 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1550 if (h
->vdev
[i
]->minor
== minor
) {
1553 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1558 if ((NULL
== dev
) || (cur_channel
== -1)) {
1560 printk(KERN_INFO
"s2255: openv4l no dev\n");
1564 if (atomic_read(&dev
->fw_data
->fw_state
) == S2255_FW_DISCONNECTING
) {
1566 printk(KERN_INFO
"disconnecting\n");
1569 kref_get(&dev
->kref
);
1570 mutex_lock(&dev
->open_lock
);
1572 dev
->users
[cur_channel
]++;
1573 dprintk(4, "s2255: open_handles %d\n", dev
->users
[cur_channel
]);
1575 switch (atomic_read(&dev
->fw_data
->fw_state
)) {
1576 case S2255_FW_FAILED
:
1577 s2255_dev_err(&dev
->udev
->dev
,
1578 "firmware load failed. retrying.\n");
1579 s2255_fwload_start(dev
, 1);
1580 wait_event_timeout(dev
->fw_data
->wait_fw
,
1581 ((atomic_read(&dev
->fw_data
->fw_state
)
1582 == S2255_FW_SUCCESS
) ||
1583 (atomic_read(&dev
->fw_data
->fw_state
)
1584 == S2255_FW_DISCONNECTING
)),
1585 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1587 case S2255_FW_NOTLOADED
:
1588 case S2255_FW_LOADED_DSPWAIT
:
1589 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1590 driver loaded and then device immediately opened */
1591 printk(KERN_INFO
"%s waiting for firmware load\n", __func__
);
1592 wait_event_timeout(dev
->fw_data
->wait_fw
,
1593 ((atomic_read(&dev
->fw_data
->fw_state
)
1594 == S2255_FW_SUCCESS
) ||
1595 (atomic_read(&dev
->fw_data
->fw_state
)
1596 == S2255_FW_DISCONNECTING
)),
1597 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1599 case S2255_FW_SUCCESS
:
1603 state
= atomic_read(&dev
->fw_data
->fw_state
);
1604 if (state
!= S2255_FW_SUCCESS
) {
1607 case S2255_FW_FAILED
:
1608 printk(KERN_INFO
"2255 FW load failed. %d\n", state
);
1611 case S2255_FW_DISCONNECTING
:
1612 printk(KERN_INFO
"%s: disconnecting\n", __func__
);
1615 case S2255_FW_LOADED_DSPWAIT
:
1616 case S2255_FW_NOTLOADED
:
1617 printk(KERN_INFO
"%s: firmware not loaded yet"
1618 "please try again later\n",
1623 printk(KERN_INFO
"%s: unknown state\n", __func__
);
1627 dev
->users
[cur_channel
]--;
1628 mutex_unlock(&dev
->open_lock
);
1629 kref_put(&dev
->kref
, s2255_destroy
);
1634 /* allocate + initialize per filehandle data */
1635 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1637 dev
->users
[cur_channel
]--;
1638 mutex_unlock(&dev
->open_lock
);
1639 kref_put(&dev
->kref
, s2255_destroy
);
1644 file
->private_data
= fh
;
1646 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1647 fh
->mode
= dev
->mode
[cur_channel
];
1648 fh
->fmt
= dev
->cur_fmt
[cur_channel
];
1649 /* default 4CIF NTSC */
1650 fh
->width
= LINE_SZ_4CIFS_NTSC
;
1651 fh
->height
= NUM_LINES_4CIFS_NTSC
* 2;
1652 fh
->channel
= cur_channel
;
1654 /* configure channel to default state */
1655 if (!dev
->chn_configured
[cur_channel
]) {
1656 s2255_set_mode(dev
, cur_channel
, &fh
->mode
);
1657 dev
->chn_configured
[cur_channel
] = 1;
1661 /* Put all controls at a sane state */
1662 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1663 qctl_regs
[i
] = s2255_qctrl
[i
].default_value
;
1665 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1666 minor
, v4l2_type_names
[type
], dev
->users
[cur_channel
]);
1667 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1668 (unsigned long)fh
, (unsigned long)dev
,
1669 (unsigned long)&dev
->vidq
[cur_channel
]);
1670 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1671 list_empty(&dev
->vidq
[cur_channel
].active
));
1673 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &s2255_video_qops
,
1676 V4L2_FIELD_INTERLACED
,
1677 sizeof(struct s2255_buffer
), fh
);
1679 mutex_unlock(&dev
->open_lock
);
1685 static unsigned int s2255_poll(struct file
*file
,
1686 struct poll_table_struct
*wait
)
1688 struct s2255_fh
*fh
= file
->private_data
;
1690 dprintk(100, "%s\n", __func__
);
1692 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1695 rc
= videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1699 static void s2255_destroy(struct kref
*kref
)
1701 struct s2255_dev
*dev
= to_s2255_dev(kref
);
1702 struct list_head
*list
;
1705 printk(KERN_ERR
"s2255drv: kref problem\n");
1708 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
1709 wake_up(&dev
->fw_data
->wait_fw
);
1710 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1711 dev
->setmode_ready
[i
] = 1;
1712 wake_up(&dev
->wait_setmode
[i
]);
1714 mutex_lock(&dev
->open_lock
);
1715 /* reset the DSP so firmware can be reload next time */
1716 s2255_reset_dsppower(dev
);
1717 s2255_exit_v4l(dev
);
1718 /* board shutdown stops the read pipe if it is running */
1719 s2255_board_shutdown(dev
);
1720 /* make sure firmware still not trying to load */
1721 del_timer(&dev
->timer
); /* only started in .probe and .open */
1723 if (dev
->fw_data
->fw_urb
) {
1724 dprintk(2, "kill fw_urb\n");
1725 usb_kill_urb(dev
->fw_data
->fw_urb
);
1726 usb_free_urb(dev
->fw_data
->fw_urb
);
1727 dev
->fw_data
->fw_urb
= NULL
;
1729 if (dev
->fw_data
->fw
)
1730 release_firmware(dev
->fw_data
->fw
);
1731 kfree(dev
->fw_data
->pfw_data
);
1732 kfree(dev
->fw_data
);
1733 usb_put_dev(dev
->udev
);
1734 dprintk(1, "%s", __func__
);
1736 while (!list_empty(&s2255_devlist
)) {
1737 list
= s2255_devlist
.next
;
1740 mutex_unlock(&dev
->open_lock
);
1744 static int s2255_close(struct file
*file
)
1746 struct s2255_fh
*fh
= file
->private_data
;
1747 struct s2255_dev
*dev
= fh
->dev
;
1748 int minor
= video_devdata(file
)->minor
;
1752 mutex_lock(&dev
->open_lock
);
1754 /* turn off stream */
1755 if (res_check(fh
)) {
1756 if (dev
->b_acquire
[fh
->channel
])
1757 s2255_stop_acquire(dev
, fh
->channel
);
1758 videobuf_streamoff(&fh
->vb_vidq
);
1762 videobuf_mmap_free(&fh
->vb_vidq
);
1763 dev
->users
[fh
->channel
]--;
1765 mutex_unlock(&dev
->open_lock
);
1767 kref_put(&dev
->kref
, s2255_destroy
);
1768 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1769 minor
, dev
->users
[fh
->channel
]);
1774 static int s2255_mmap_v4l(struct file
*file
, struct vm_area_struct
*vma
)
1776 struct s2255_fh
*fh
= file
->private_data
;
1781 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1783 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1785 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1786 (unsigned long)vma
->vm_start
,
1787 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
, ret
);
1792 static const struct v4l2_file_operations s2255_fops_v4l
= {
1793 .owner
= THIS_MODULE
,
1795 .release
= s2255_close
,
1797 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1798 .mmap
= s2255_mmap_v4l
,
1801 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1802 .vidioc_querycap
= vidioc_querycap
,
1803 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1804 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1805 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1806 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1807 .vidioc_reqbufs
= vidioc_reqbufs
,
1808 .vidioc_querybuf
= vidioc_querybuf
,
1809 .vidioc_qbuf
= vidioc_qbuf
,
1810 .vidioc_dqbuf
= vidioc_dqbuf
,
1811 .vidioc_s_std
= vidioc_s_std
,
1812 .vidioc_enum_input
= vidioc_enum_input
,
1813 .vidioc_g_input
= vidioc_g_input
,
1814 .vidioc_s_input
= vidioc_s_input
,
1815 .vidioc_queryctrl
= vidioc_queryctrl
,
1816 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1817 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1818 .vidioc_streamon
= vidioc_streamon
,
1819 .vidioc_streamoff
= vidioc_streamoff
,
1820 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1821 .vidiocgmbuf
= vidioc_cgmbuf
,
1823 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1824 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1825 .vidioc_s_parm
= vidioc_s_parm
,
1826 .vidioc_g_parm
= vidioc_g_parm
,
1829 static struct video_device
template = {
1831 .fops
= &s2255_fops_v4l
,
1832 .ioctl_ops
= &s2255_ioctl_ops
,
1834 .release
= video_device_release
,
1835 .tvnorms
= S2255_NORMS
,
1836 .current_norm
= V4L2_STD_NTSC_M
,
1839 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1843 int cur_nr
= video_nr
;
1845 /* initialize all video 4 linux */
1846 list_add_tail(&dev
->s2255_devlist
, &s2255_devlist
);
1847 /* register 4 video devices */
1848 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1849 INIT_LIST_HEAD(&dev
->vidq
[i
].active
);
1850 dev
->vidq
[i
].dev
= dev
;
1851 dev
->vidq
[i
].channel
= i
;
1852 /* register 4 video devices */
1853 dev
->vdev
[i
] = video_device_alloc();
1854 memcpy(dev
->vdev
[i
], &template, sizeof(struct video_device
));
1855 dev
->vdev
[i
]->parent
= &dev
->interface
->dev
;
1857 ret
= video_register_device(dev
->vdev
[i
],
1861 ret
= video_register_device(dev
->vdev
[i
],
1864 video_set_drvdata(dev
->vdev
[i
], dev
);
1867 dev_err(&dev
->udev
->dev
,
1868 "failed to register video device!\n");
1872 printk(KERN_INFO
"Sensoray 2255 V4L driver Revision: %d.%d\n",
1873 S2255_MAJOR_VERSION
,
1874 S2255_MINOR_VERSION
);
1878 static void s2255_exit_v4l(struct s2255_dev
*dev
)
1882 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1883 if (-1 != dev
->vdev
[i
]->minor
) {
1884 video_unregister_device(dev
->vdev
[i
]);
1885 printk(KERN_INFO
"s2255 unregistered\n");
1887 video_device_release(dev
->vdev
[i
]);
1888 printk(KERN_INFO
"s2255 released\n");
1893 /* this function moves the usb stream read pipe data
1894 * into the system buffers.
1895 * returns 0 on success, EAGAIN if more data to process( call this
1898 * Received frame structure:
1899 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1900 * bytes 4-7: channel: 0-3
1901 * bytes 8-11: payload size: size of the frame
1902 * bytes 12-payloadsize+12: frame data
1904 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
1910 unsigned long copy_size
;
1913 struct s2255_framei
*frm
;
1914 unsigned char *pdata
;
1916 dprintk(100, "buffer to user\n");
1918 idx
= dev
->cur_frame
[dev
->cc
];
1919 frm
= &dev
->buffer
[dev
->cc
].frame
[idx
];
1921 if (frm
->ulState
== S2255_READ_IDLE
) {
1926 /* search for marker codes */
1927 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
1928 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
1929 switch (*(s32
*) pdata
) {
1930 case S2255_MARKER_FRAME
:
1931 pdword
= (s32
*)pdata
;
1932 dprintk(4, "found frame marker at offset:"
1933 " %d [%x %x]\n", jj
, pdata
[0],
1935 offset
= jj
+ PREFIX_SIZE
;
1938 if (cc
>= MAX_CHANNELS
) {
1944 dev
->cc
= G_chnmap
[cc
];
1945 payload
= pdword
[3];
1946 if (payload
> dev
->req_image_size
[dev
->cc
]) {
1947 dev
->bad_payload
[dev
->cc
]++;
1948 /* discard the bad frame */
1951 dev
->pkt_size
[dev
->cc
] = payload
;
1952 dev
->jpg_size
[dev
->cc
] = pdword
[4];
1954 case S2255_MARKER_RESPONSE
:
1955 pdword
= (s32
*)pdata
;
1956 pdata
+= DEF_USB_BLOCK
;
1957 jj
+= DEF_USB_BLOCK
;
1958 if (pdword
[1] >= MAX_CHANNELS
)
1960 cc
= G_chnmap
[pdword
[1]];
1961 if (!(cc
>= 0 && cc
< MAX_CHANNELS
))
1963 switch (pdword
[2]) {
1964 case S2255_RESPONSE_SETMODE
:
1965 /* check if channel valid */
1966 /* set mode ready */
1967 dev
->setmode_ready
[cc
] = 1;
1968 wake_up(&dev
->wait_setmode
[cc
]);
1969 dprintk(5, "setmode ready %d\n", cc
);
1971 case S2255_RESPONSE_FW
:
1973 dev
->chn_ready
|= (1 << cc
);
1974 if ((dev
->chn_ready
& 0x0f) != 0x0f)
1976 /* all channels ready */
1977 printk(KERN_INFO
"s2255: fw loaded\n");
1978 atomic_set(&dev
->fw_data
->fw_state
,
1980 wake_up(&dev
->fw_data
->wait_fw
);
1983 printk(KERN_INFO
"s2255 unknwn resp\n");
1997 idx
= dev
->cur_frame
[dev
->cc
];
1998 frm
= &dev
->buffer
[dev
->cc
].frame
[idx
];
2000 /* search done. now find out if should be acquiring on this channel */
2001 if (!dev
->b_acquire
[dev
->cc
]) {
2002 /* we found a frame, but this channel is turned off */
2003 frm
->ulState
= S2255_READ_IDLE
;
2007 if (frm
->ulState
== S2255_READ_IDLE
) {
2008 frm
->ulState
= S2255_READ_FRAME
;
2012 /* skip the marker 512 bytes (and offset if out of sync) */
2013 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
2016 if (frm
->lpvbits
== NULL
) {
2017 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2018 frm
, dev
, dev
->cc
, idx
);
2022 pdest
= frm
->lpvbits
+ frm
->cur_size
;
2024 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
2026 size
= dev
->pkt_size
[dev
->cc
] - PREFIX_SIZE
;
2028 /* sanity check on pdest */
2029 if ((copy_size
+ frm
->cur_size
) < dev
->req_image_size
[dev
->cc
])
2030 memcpy(pdest
, psrc
, copy_size
);
2032 frm
->cur_size
+= copy_size
;
2033 dprintk(4, "cur_size size %lu size %lu \n", frm
->cur_size
, size
);
2035 if (frm
->cur_size
>= size
) {
2038 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2040 dev
->last_frame
[cc
] = dev
->cur_frame
[cc
];
2041 dev
->cur_frame
[cc
]++;
2042 /* end of system frame ring buffer, start at zero */
2043 if ((dev
->cur_frame
[cc
] == SYS_FRAMES
) ||
2044 (dev
->cur_frame
[cc
] == dev
->buffer
[cc
].dwFrames
))
2045 dev
->cur_frame
[cc
] = 0;
2047 if (dev
->b_acquire
[cc
])
2048 s2255_got_frame(dev
, cc
, dev
->jpg_size
[cc
]);
2049 dev
->frame_count
[cc
]++;
2050 frm
->ulState
= S2255_READ_IDLE
;
2054 /* done successfully */
2058 static void s2255_read_video_callback(struct s2255_dev
*dev
,
2059 struct s2255_pipeinfo
*pipe_info
)
2062 dprintk(50, "callback read video \n");
2064 if (dev
->cc
>= MAX_CHANNELS
) {
2066 dev_err(&dev
->udev
->dev
, "invalid channel\n");
2069 /* otherwise copy to the system buffers */
2070 res
= save_frame(dev
, pipe_info
);
2072 dprintk(4, "s2255: read callback failed\n");
2074 dprintk(50, "callback read video done\n");
2078 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
2079 u16 Index
, u16 Value
, void *TransferBuffer
,
2080 s32 TransferBufferLength
, int bOut
)
2084 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
2086 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
2088 Value
, Index
, TransferBuffer
,
2089 TransferBufferLength
, HZ
* 5);
2091 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
2092 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2093 Value
, Index
, TransferBuffer
,
2094 TransferBufferLength
, HZ
* 5);
2100 * retrieve FX2 firmware version. future use.
2101 * @param dev pointer to device extension
2102 * @return -1 for fail, else returns firmware version as an int(16 bits)
2104 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
2108 unsigned char transBuffer
[64];
2109 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
2112 dprintk(2, "get fw error: %x\n", ret
);
2113 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
2114 dprintk(2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
2119 * Create the system ring buffer to copy frames into from the
2122 static int s2255_create_sys_buffers(struct s2255_dev
*dev
, unsigned long chn
)
2125 unsigned long reqsize
;
2126 dprintk(1, "create sys buffers\n");
2127 if (chn
>= MAX_CHANNELS
)
2130 dev
->buffer
[chn
].dwFrames
= SYS_FRAMES
;
2132 /* always allocate maximum size(PAL) for system buffers */
2133 reqsize
= SYS_FRAMES_MAXSIZE
;
2135 if (reqsize
> SYS_FRAMES_MAXSIZE
)
2136 reqsize
= SYS_FRAMES_MAXSIZE
;
2138 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2139 /* allocate the frames */
2140 dev
->buffer
[chn
].frame
[i
].lpvbits
= vmalloc(reqsize
);
2142 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2143 &dev
->buffer
[chn
].frame
[i
], chn
, i
,
2144 dev
->buffer
[chn
].frame
[i
].lpvbits
);
2145 dev
->buffer
[chn
].frame
[i
].size
= reqsize
;
2146 if (dev
->buffer
[chn
].frame
[i
].lpvbits
== NULL
) {
2147 printk(KERN_INFO
"out of memory. using less frames\n");
2148 dev
->buffer
[chn
].dwFrames
= i
;
2153 /* make sure internal states are set */
2154 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2155 dev
->buffer
[chn
].frame
[i
].ulState
= 0;
2156 dev
->buffer
[chn
].frame
[i
].cur_size
= 0;
2159 dev
->cur_frame
[chn
] = 0;
2160 dev
->last_frame
[chn
] = -1;
2164 static int s2255_release_sys_buffers(struct s2255_dev
*dev
,
2165 unsigned long channel
)
2168 dprintk(1, "release sys buffers\n");
2169 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2170 if (dev
->buffer
[channel
].frame
[i
].lpvbits
) {
2171 dprintk(1, "vfree %p\n",
2172 dev
->buffer
[channel
].frame
[i
].lpvbits
);
2173 vfree(dev
->buffer
[channel
].frame
[i
].lpvbits
);
2175 dev
->buffer
[channel
].frame
[i
].lpvbits
= NULL
;
2180 static int s2255_board_init(struct s2255_dev
*dev
)
2183 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
2185 dprintk(4, "board init: %p", dev
);
2187 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2188 struct s2255_pipeinfo
*pipe
= &dev
->pipes
[j
];
2190 memset(pipe
, 0, sizeof(*pipe
));
2192 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
2193 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
2195 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
2197 if (pipe
->transfer_buffer
== NULL
) {
2198 dprintk(1, "out of memory!\n");
2204 /* query the firmware */
2205 fw_ver
= s2255_get_fx2fw(dev
);
2207 printk(KERN_INFO
"2255 usb firmware version %d.%d\n",
2208 (fw_ver
>> 8) & 0xff,
2211 if (fw_ver
< S2255_CUR_USB_FWVER
)
2212 dev_err(&dev
->udev
->dev
,
2213 "usb firmware not up to date %d.%d\n",
2214 (fw_ver
>> 8) & 0xff,
2217 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
2218 dev
->b_acquire
[j
] = 0;
2219 dev
->mode
[j
] = mode_def
;
2220 dev
->jc
[j
].quality
= S2255_DEF_JPEG_QUAL
;
2221 dev
->cur_fmt
[j
] = &formats
[0];
2222 dev
->mode
[j
].restart
= 1;
2223 dev
->req_image_size
[j
] = get_transfer_size(&mode_def
);
2224 dev
->frame_count
[j
] = 0;
2225 /* create the system buffers */
2226 s2255_create_sys_buffers(dev
, j
);
2228 /* start read pipe */
2229 s2255_start_readpipe(dev
);
2231 dprintk(1, "S2255: board initialized\n");
2235 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2239 dprintk(1, "S2255: board shutdown: %p", dev
);
2241 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2242 if (dev
->b_acquire
[i
])
2243 s2255_stop_acquire(dev
, i
);
2246 s2255_stop_readpipe(dev
);
2248 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2249 s2255_release_sys_buffers(dev
, i
);
2251 /* release transfer buffers */
2252 for (i
= 0; i
< MAX_PIPE_BUFFERS
; i
++) {
2253 struct s2255_pipeinfo
*pipe
= &dev
->pipes
[i
];
2254 kfree(pipe
->transfer_buffer
);
2259 static void read_pipe_completion(struct urb
*purb
)
2261 struct s2255_pipeinfo
*pipe_info
;
2262 struct s2255_dev
*dev
;
2266 pipe_info
= purb
->context
;
2267 dprintk(100, "read pipe completion %p, status %d\n", purb
,
2269 if (pipe_info
== NULL
) {
2270 dev_err(&purb
->dev
->dev
, "no context!\n");
2274 dev
= pipe_info
->dev
;
2276 dev_err(&purb
->dev
->dev
, "no context!\n");
2279 status
= purb
->status
;
2280 /* if shutting down, do not resubmit, exit immediately */
2281 if (status
== -ESHUTDOWN
) {
2282 dprintk(2, "read_pipe_completion: err shutdown\n");
2283 pipe_info
->err_count
++;
2287 if (pipe_info
->state
== 0) {
2288 dprintk(2, "exiting USB pipe");
2293 s2255_read_video_callback(dev
, pipe_info
);
2295 pipe_info
->err_count
++;
2296 dprintk(1, "s2255drv: failed URB %d\n", status
);
2299 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2301 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2303 pipe_info
->transfer_buffer
,
2304 pipe_info
->cur_transfer_size
,
2305 read_pipe_completion
, pipe_info
);
2307 if (pipe_info
->state
!= 0) {
2308 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
)) {
2309 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2312 dprintk(2, "read pipe complete state 0\n");
2317 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2322 struct s2255_pipeinfo
*pipe_info
= dev
->pipes
;
2323 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2324 dprintk(2, "start pipe IN %d\n", dev
->read_endpoint
);
2326 for (i
= 0; i
< MAX_PIPE_BUFFERS
; i
++) {
2327 pipe_info
->state
= 1;
2328 pipe_info
->err_count
= 0;
2329 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2330 if (!pipe_info
->stream_urb
) {
2331 dev_err(&dev
->udev
->dev
,
2332 "ReadStream: Unable to alloc URB\n");
2335 /* transfer buffer allocated in board_init */
2336 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2338 pipe_info
->transfer_buffer
,
2339 pipe_info
->cur_transfer_size
,
2340 read_pipe_completion
, pipe_info
);
2342 dprintk(4, "submitting URB %p\n", pipe_info
->stream_urb
);
2343 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2345 printk(KERN_ERR
"s2255: start read pipe failed\n");
2353 /* starts acquisition process */
2354 static int s2255_start_acquire(struct s2255_dev
*dev
, unsigned long chn
)
2356 unsigned char *buffer
;
2358 unsigned long chn_rev
;
2360 if (chn
>= MAX_CHANNELS
) {
2361 dprintk(2, "start acquire failed, bad channel %lu\n", chn
);
2365 chn_rev
= G_chnmap
[chn
];
2366 dprintk(1, "S2255: start acquire %lu \n", chn
);
2368 buffer
= kzalloc(512, GFP_KERNEL
);
2369 if (buffer
== NULL
) {
2370 dev_err(&dev
->udev
->dev
, "out of mem\n");
2374 dev
->last_frame
[chn
] = -1;
2375 dev
->bad_payload
[chn
] = 0;
2376 dev
->cur_frame
[chn
] = 0;
2377 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2378 dev
->buffer
[chn
].frame
[j
].ulState
= 0;
2379 dev
->buffer
[chn
].frame
[j
].cur_size
= 0;
2382 /* send the start command */
2383 *(u32
*) buffer
= IN_DATA_TOKEN
;
2384 *((u32
*) buffer
+ 1) = (u32
) chn_rev
;
2385 *((u32
*) buffer
+ 2) = (u32
) CMD_START
;
2386 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2388 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2390 dprintk(2, "start acquire exit[%lu] %d \n", chn
, res
);
2395 static int s2255_stop_acquire(struct s2255_dev
*dev
, unsigned long chn
)
2397 unsigned char *buffer
;
2399 unsigned long chn_rev
;
2401 if (chn
>= MAX_CHANNELS
) {
2402 dprintk(2, "stop acquire failed, bad channel %lu\n", chn
);
2405 chn_rev
= G_chnmap
[chn
];
2407 buffer
= kzalloc(512, GFP_KERNEL
);
2408 if (buffer
== NULL
) {
2409 dev_err(&dev
->udev
->dev
, "out of mem\n");
2413 /* send the stop command */
2414 dprintk(4, "stop acquire %lu\n", chn
);
2415 *(u32
*) buffer
= IN_DATA_TOKEN
;
2416 *((u32
*) buffer
+ 1) = (u32
) chn_rev
;
2417 *((u32
*) buffer
+ 2) = CMD_STOP
;
2418 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2421 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2423 dprintk(4, "stop acquire: releasing states \n");
2426 dev
->b_acquire
[chn
] = 0;
2431 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2436 s2255_dev_err(&dev
->udev
->dev
, "invalid device\n");
2439 dprintk(4, "stop read pipe\n");
2440 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2441 struct s2255_pipeinfo
*pipe_info
= &dev
->pipes
[j
];
2443 if (pipe_info
->state
== 0)
2445 pipe_info
->state
= 0;
2449 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2450 struct s2255_pipeinfo
*pipe_info
= &dev
->pipes
[j
];
2451 if (pipe_info
->stream_urb
) {
2453 usb_kill_urb(pipe_info
->stream_urb
);
2454 usb_free_urb(pipe_info
->stream_urb
);
2455 pipe_info
->stream_urb
= NULL
;
2458 dprintk(2, "s2255 stop read pipe: %d\n", j
);
2462 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
)
2465 s2255_reset_dsppower(dev
);
2466 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2467 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2468 memcpy(dev
->fw_data
->pfw_data
,
2469 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2470 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2471 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2472 usb_sndbulkpipe(dev
->udev
, 2),
2473 dev
->fw_data
->pfw_data
,
2474 CHUNK_SIZE
, s2255_fwchunk_complete
,
2476 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2479 /* standard usb probe function */
2480 static int s2255_probe(struct usb_interface
*interface
,
2481 const struct usb_device_id
*id
)
2483 struct s2255_dev
*dev
= NULL
;
2484 struct usb_host_interface
*iface_desc
;
2485 struct usb_endpoint_descriptor
*endpoint
;
2487 int retval
= -ENOMEM
;
2491 dprintk(2, "s2255: probe\n");
2493 /* allocate memory for our device state and initialize it to zero */
2494 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2496 s2255_dev_err(&interface
->dev
, "out of memory\n");
2500 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2504 mutex_init(&dev
->lock
);
2505 mutex_init(&dev
->open_lock
);
2507 /* grab usb_device and save it */
2508 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2509 if (dev
->udev
== NULL
) {
2510 dev_err(&interface
->dev
, "null usb device\n");
2514 kref_init(&dev
->kref
);
2515 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev
, &dev
->kref
,
2516 dev
->udev
, interface
);
2517 dev
->interface
= interface
;
2518 /* set up the endpoint information */
2519 iface_desc
= interface
->cur_altsetting
;
2520 dprintk(1, "num endpoints %d\n", iface_desc
->desc
.bNumEndpoints
);
2521 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2522 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2523 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2524 /* we found the bulk in endpoint */
2525 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2529 if (!dev
->read_endpoint
) {
2530 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2535 usb_set_intfdata(interface
, dev
);
2537 dprintk(100, "after intfdata %p\n", dev
);
2539 init_timer(&dev
->timer
);
2540 dev
->timer
.function
= s2255_timer
;
2541 dev
->timer
.data
= (unsigned long)dev
->fw_data
;
2543 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2544 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2545 init_waitqueue_head(&dev
->wait_setmode
[i
]);
2548 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2550 if (!dev
->fw_data
->fw_urb
) {
2551 dev_err(&interface
->dev
, "out of memory!\n");
2554 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2555 if (!dev
->fw_data
->pfw_data
) {
2556 dev_err(&interface
->dev
, "out of memory!\n");
2559 /* load the first chunk */
2560 if (request_firmware(&dev
->fw_data
->fw
,
2561 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2562 printk(KERN_ERR
"sensoray 2255 failed to get firmware\n");
2565 /* check the firmware is valid */
2566 fw_size
= dev
->fw_data
->fw
->size
;
2567 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2569 if (*pdata
!= S2255_FW_MARKER
) {
2570 printk(KERN_INFO
"Firmware invalid.\n");
2574 /* make sure firmware is the latest */
2576 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2577 printk(KERN_INFO
"s2255 dsp fw version %x\n", *pRel
);
2579 /* loads v4l specific */
2580 s2255_probe_v4l(dev
);
2581 usb_reset_device(dev
->udev
);
2582 /* load 2255 board specific */
2583 retval
= s2255_board_init(dev
);
2587 dprintk(4, "before probe done %p\n", dev
);
2588 spin_lock_init(&dev
->slock
);
2590 s2255_fwload_start(dev
, 0);
2591 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2597 /* disconnect routine. when board is removed physically or with rmmod */
2598 static void s2255_disconnect(struct usb_interface
*interface
)
2600 struct s2255_dev
*dev
= NULL
;
2602 dprintk(1, "s2255: disconnect interface %p\n", interface
);
2603 dev
= usb_get_intfdata(interface
);
2606 * wake up any of the timers to allow open_lock to be
2609 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2610 wake_up(&dev
->fw_data
->wait_fw
);
2611 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2612 dev
->setmode_ready
[i
] = 1;
2613 wake_up(&dev
->wait_setmode
[i
]);
2616 mutex_lock(&dev
->open_lock
);
2617 usb_set_intfdata(interface
, NULL
);
2618 mutex_unlock(&dev
->open_lock
);
2621 kref_put(&dev
->kref
, s2255_destroy
);
2622 dprintk(1, "s2255drv: disconnect\n");
2623 dev_info(&interface
->dev
, "s2255usb now disconnected\n");
2627 static struct usb_driver s2255_driver
= {
2628 .name
= S2255_DRIVER_NAME
,
2629 .probe
= s2255_probe
,
2630 .disconnect
= s2255_disconnect
,
2631 .id_table
= s2255_table
,
2634 static int __init
usb_s2255_init(void)
2638 /* register this driver with the USB subsystem */
2639 result
= usb_register(&s2255_driver
);
2642 pr_err(KBUILD_MODNAME
2643 ": usb_register failed. Error number %d\n", result
);
2645 dprintk(2, "s2255_init: done\n");
2649 static void __exit
usb_s2255_exit(void)
2651 usb_deregister(&s2255_driver
);
2654 module_init(usb_s2255_init
);
2655 module_exit(usb_s2255_exit
);
2657 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2658 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2659 MODULE_LICENSE("GPL");