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 <media/videobuf-vmalloc.h>
52 #include <media/v4l2-common.h>
53 #include <media/v4l2-ioctl.h>
54 #include <linux/vmalloc.h>
55 #include <linux/usb.h>
57 #define FIRMWARE_FILE_NAME "f2255usb.bin"
61 /* default JPEG quality */
62 #define S2255_DEF_JPEG_QUAL 50
63 /* vendor request in */
65 /* vendor request out */
66 #define S2255_VR_OUT 1
68 #define S2255_VR_FW 0x30
69 /* USB endpoint number for configuring the device */
70 #define S2255_CONFIG_EP 2
71 /* maximum time for DSP to start responding after last FW word loaded(ms) */
72 #define S2255_DSP_BOOTTIME 800
73 /* maximum time to wait for firmware to load (ms) */
74 #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
75 #define S2255_DEF_BUFS 16
76 #define S2255_SETMODE_TIMEOUT 500
77 #define MAX_CHANNELS 4
78 #define S2255_MARKER_FRAME 0x2255DA4AL
79 #define S2255_MARKER_RESPONSE 0x2255ACACL
80 #define S2255_USB_XFER_SIZE (16 * 1024)
81 #define MAX_CHANNELS 4
82 #define MAX_PIPE_BUFFERS 1
84 /* maximum size is PAL full size plus room for the marker header(s) */
85 #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
86 #define DEF_USB_BLOCK S2255_USB_XFER_SIZE
87 #define LINE_SZ_4CIFS_NTSC 640
88 #define LINE_SZ_2CIFS_NTSC 640
89 #define LINE_SZ_1CIFS_NTSC 320
90 #define LINE_SZ_4CIFS_PAL 704
91 #define LINE_SZ_2CIFS_PAL 704
92 #define LINE_SZ_1CIFS_PAL 352
93 #define NUM_LINES_4CIFS_NTSC 240
94 #define NUM_LINES_2CIFS_NTSC 240
95 #define NUM_LINES_1CIFS_NTSC 240
96 #define NUM_LINES_4CIFS_PAL 288
97 #define NUM_LINES_2CIFS_PAL 288
98 #define NUM_LINES_1CIFS_PAL 288
99 #define LINE_SZ_DEF 640
100 #define NUM_LINES_DEF 240
103 /* predefined settings */
104 #define FORMAT_NTSC 1
107 #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
108 #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
109 #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
111 #define COLOR_YUVPL 1 /* YUV planar */
112 #define COLOR_YUVPK 2 /* YUV packed */
113 #define COLOR_Y8 4 /* monochrome */
114 #define COLOR_JPG 5 /* JPEG */
115 #define MASK_COLOR 0xff
116 #define MASK_JPG_QUALITY 0xff00
118 /* frame decimation. Not implemented by V4L yet(experimental in V4L) */
119 #define FDEC_1 1 /* capture every frame. default */
120 #define FDEC_2 2 /* capture every 2nd frame */
121 #define FDEC_3 3 /* capture every 3rd frame */
122 #define FDEC_5 5 /* capture every 5th frame */
124 /*-------------------------------------------------------
125 * Default mode parameters.
126 *-------------------------------------------------------*/
127 #define DEF_SCALE SCALE_4CIFS
128 #define DEF_COLOR COLOR_YUVPL
129 #define DEF_FDEC FDEC_1
131 #define DEF_CONTRAST 0x5c
132 #define DEF_SATURATION 0x80
135 /* usb config commands */
136 #define IN_DATA_TOKEN 0x2255c0de
137 #define CMD_2255 0xc2255000
138 #define CMD_SET_MODE (CMD_2255 | 0x10)
139 #define CMD_START (CMD_2255 | 0x20)
140 #define CMD_STOP (CMD_2255 | 0x30)
141 #define CMD_STATUS (CMD_2255 | 0x40)
144 u32 format
; /* input video format (NTSC, PAL) */
145 u32 scale
; /* output video scale */
146 u32 color
; /* output video color format */
147 u32 fdec
; /* frame decimation */
148 u32 bright
; /* brightness */
149 u32 contrast
; /* contrast */
150 u32 saturation
; /* saturation */
151 u32 hue
; /* hue (NTSC only)*/
152 u32 single
; /* capture 1 frame at a time (!=0), continuously (==0)*/
153 u32 usb_block
; /* block size. should be 4096 of DEF_USB_BLOCK */
154 u32 restart
; /* if DSP requires restart */
158 #define S2255_READ_IDLE 0
159 #define S2255_READ_FRAME 1
161 /* frame structure */
162 struct s2255_framei
{
164 unsigned long ulState
; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
165 void *lpvbits
; /* image data */
166 unsigned long cur_size
; /* current data copied to it */
169 /* image buffer structure */
170 struct s2255_bufferi
{
171 unsigned long dwFrames
; /* number of frames in buffer */
172 struct s2255_framei frame
[SYS_FRAMES
]; /* array of FRAME structures */
175 #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
176 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
177 DEF_HUE, 0, DEF_USB_BLOCK, 0}
179 struct s2255_dmaqueue
{
180 struct list_head active
;
181 /* thread for acquisition */
182 struct task_struct
*kthread
;
184 struct s2255_dev
*dev
;
188 /* for firmware loading, fw_state */
189 #define S2255_FW_NOTLOADED 0
190 #define S2255_FW_LOADED_DSPWAIT 1
191 #define S2255_FW_SUCCESS 2
192 #define S2255_FW_FAILED 3
193 #define S2255_FW_DISCONNECTING 4
195 #define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
196 /* 2255 read states */
197 #define S2255_READ_IDLE 0
198 #define S2255_READ_FRAME 1
205 wait_queue_head_t wait_fw
;
206 const struct firmware
*fw
;
209 struct s2255_pipeinfo
{
210 u32 max_transfer_size
;
211 u32 cur_transfer_size
;
218 void *dev
; /* back pointer to s2255_dev struct*/
225 struct s2255_fmt
; /*forward declaration */
229 int users
[MAX_CHANNELS
];
231 struct mutex open_lock
;
232 int resources
[MAX_CHANNELS
];
233 struct usb_device
*udev
;
234 struct usb_interface
*interface
;
237 struct s2255_dmaqueue vidq
[MAX_CHANNELS
];
238 struct video_device
*vdev
[MAX_CHANNELS
];
239 struct list_head s2255_devlist
;
240 struct timer_list timer
;
241 struct s2255_fw
*fw_data
;
244 struct s2255_pipeinfo pipes
[MAX_PIPE_BUFFERS
];
245 struct s2255_bufferi buffer
[MAX_CHANNELS
];
246 struct s2255_mode mode
[MAX_CHANNELS
];
247 /* jpeg compression */
248 struct v4l2_jpegcompression jc
[MAX_CHANNELS
];
249 const struct s2255_fmt
*cur_fmt
[MAX_CHANNELS
];
250 int cur_frame
[MAX_CHANNELS
];
251 int last_frame
[MAX_CHANNELS
];
252 u32 cc
; /* current channel */
253 int b_acquire
[MAX_CHANNELS
];
254 /* allocated image size */
255 unsigned long req_image_size
[MAX_CHANNELS
];
256 /* received packet size */
257 unsigned long pkt_size
[MAX_CHANNELS
];
258 int bad_payload
[MAX_CHANNELS
];
259 unsigned long frame_count
[MAX_CHANNELS
];
262 int jpg_size
[MAX_CHANNELS
];
263 /* if channel configured to default state */
264 int chn_configured
[MAX_CHANNELS
];
265 wait_queue_head_t wait_setmode
[MAX_CHANNELS
];
266 int setmode_ready
[MAX_CHANNELS
];
271 #define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
279 /* buffer for one video frame */
280 struct s2255_buffer
{
281 /* common v4l buffer stuff -- must be first */
282 struct videobuf_buffer vb
;
283 const struct s2255_fmt
*fmt
;
287 struct s2255_dev
*dev
;
288 const struct s2255_fmt
*fmt
;
291 struct videobuf_queue vb_vidq
;
292 enum v4l2_buf_type type
;
294 /* mode below is the desired mode.
295 mode in s2255_dev is the current mode that was last set */
296 struct s2255_mode mode
;
297 int resources
[MAX_CHANNELS
];
300 #define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
301 #define S2255_MAJOR_VERSION 1
302 #define S2255_MINOR_VERSION 13
303 #define S2255_RELEASE 0
304 #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
305 S2255_MINOR_VERSION, \
309 #define USB_S2255_VENDOR_ID 0x1943
310 #define USB_S2255_PRODUCT_ID 0x2255
311 #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
312 /* frame prefix size (sent once every frame) */
313 #define PREFIX_SIZE 512
315 /* Channels on box are in reverse order */
316 static unsigned long G_chnmap
[MAX_CHANNELS
] = {3, 2, 1, 0};
318 static LIST_HEAD(s2255_devlist
);
321 static int *s2255_debug
= &debug
;
323 static int s2255_start_readpipe(struct s2255_dev
*dev
);
324 static void s2255_stop_readpipe(struct s2255_dev
*dev
);
325 static int s2255_start_acquire(struct s2255_dev
*dev
, unsigned long chn
);
326 static int s2255_stop_acquire(struct s2255_dev
*dev
, unsigned long chn
);
327 static void s2255_fillbuff(struct s2255_dev
*dev
, struct s2255_buffer
*buf
,
328 int chn
, int jpgsize
);
329 static int s2255_set_mode(struct s2255_dev
*dev
, unsigned long chn
,
330 struct s2255_mode
*mode
);
331 static int s2255_board_shutdown(struct s2255_dev
*dev
);
332 static void s2255_exit_v4l(struct s2255_dev
*dev
);
333 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
);
334 static void s2255_destroy(struct kref
*kref
);
335 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char req
,
336 u16 index
, u16 value
, void *buf
,
337 s32 buf_len
, int bOut
);
339 /* dev_err macro with driver name */
340 #define S2255_DRIVER_NAME "s2255"
341 #define s2255_dev_err(dev, fmt, arg...) \
342 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
344 #define dprintk(level, fmt, arg...) \
346 if (*s2255_debug >= (level)) { \
347 printk(KERN_DEBUG S2255_DRIVER_NAME \
352 static struct usb_driver s2255_driver
;
355 /* Declare static vars that will be used as parameters */
356 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
358 /* start video number */
359 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
361 module_param(debug
, int, 0644);
362 MODULE_PARM_DESC(debug
, "Debug level(0-100) default 0");
363 module_param(vid_limit
, int, 0644);
364 MODULE_PARM_DESC(vid_limit
, "video memory limit(Mb)");
365 module_param(video_nr
, int, 0644);
366 MODULE_PARM_DESC(video_nr
, "start video minor(-1 default autodetect)");
368 /* USB device table */
369 static struct usb_device_id s2255_table
[] = {
370 {USB_DEVICE(USB_S2255_VENDOR_ID
, USB_S2255_PRODUCT_ID
)},
371 { } /* Terminating entry */
373 MODULE_DEVICE_TABLE(usb
, s2255_table
);
376 #define BUFFER_TIMEOUT msecs_to_jiffies(400)
378 /* supported controls */
379 static struct v4l2_queryctrl s2255_qctrl
[] = {
381 .id
= V4L2_CID_BRIGHTNESS
,
382 .type
= V4L2_CTRL_TYPE_INTEGER
,
383 .name
= "Brightness",
390 .id
= V4L2_CID_CONTRAST
,
391 .type
= V4L2_CTRL_TYPE_INTEGER
,
396 .default_value
= DEF_CONTRAST
,
399 .id
= V4L2_CID_SATURATION
,
400 .type
= V4L2_CTRL_TYPE_INTEGER
,
401 .name
= "Saturation",
405 .default_value
= DEF_SATURATION
,
409 .type
= V4L2_CTRL_TYPE_INTEGER
,
414 .default_value
= DEF_HUE
,
419 static int qctl_regs
[ARRAY_SIZE(s2255_qctrl
)];
422 static const struct s2255_fmt formats
[] = {
424 .name
= "4:2:2, planar, YUV422P",
425 .fourcc
= V4L2_PIX_FMT_YUV422P
,
429 .name
= "4:2:2, packed, YUYV",
430 .fourcc
= V4L2_PIX_FMT_YUYV
,
434 .name
= "4:2:2, packed, UYVY",
435 .fourcc
= V4L2_PIX_FMT_UYVY
,
439 .fourcc
= V4L2_PIX_FMT_JPEG
,
443 .fourcc
= V4L2_PIX_FMT_GREY
,
448 static int norm_maxw(struct video_device
*vdev
)
450 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
451 LINE_SZ_4CIFS_NTSC
: LINE_SZ_4CIFS_PAL
;
454 static int norm_maxh(struct video_device
*vdev
)
456 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
457 (NUM_LINES_1CIFS_NTSC
* 2) : (NUM_LINES_1CIFS_PAL
* 2);
460 static int norm_minw(struct video_device
*vdev
)
462 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
463 LINE_SZ_1CIFS_NTSC
: LINE_SZ_1CIFS_PAL
;
466 static int norm_minh(struct video_device
*vdev
)
468 return (vdev
->current_norm
& V4L2_STD_NTSC
) ?
469 (NUM_LINES_1CIFS_NTSC
) : (NUM_LINES_1CIFS_PAL
);
474 * TODO: fixme: move YUV reordering to hardware
475 * converts 2255 planar format to yuyv or uyvy
477 static void planar422p_to_yuv_packed(const unsigned char *in
,
479 int width
, int height
,
485 unsigned long size
= height
* width
;
487 pY
= (unsigned char *)in
;
488 pCr
= (unsigned char *)in
+ height
* width
;
489 pCb
= (unsigned char *)in
+ height
* width
+ (height
* width
/ 2);
490 for (i
= 0; i
< size
* 2; i
+= 4) {
491 out
[i
] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCr
++;
492 out
[i
+ 1] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCr
++ : *pY
++;
493 out
[i
+ 2] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pY
++ : *pCb
++;
494 out
[i
+ 3] = (fmt
== V4L2_PIX_FMT_YUYV
) ? *pCb
++ : *pY
++;
499 static void s2255_reset_dsppower(struct s2255_dev
*dev
)
501 s2255_vendor_req(dev
, 0x40, 0x0b0b, 0x0b0b, NULL
, 0, 1);
503 s2255_vendor_req(dev
, 0x50, 0x0000, 0x0000, NULL
, 0, 1);
507 /* kickstarts the firmware loading. from probe
509 static void s2255_timer(unsigned long user_data
)
511 struct s2255_fw
*data
= (struct s2255_fw
*)user_data
;
512 dprintk(100, "s2255 timer\n");
513 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
514 printk(KERN_ERR
"s2255: can't submit urb\n");
515 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
516 /* wake up anything waiting for the firmware */
517 wake_up(&data
->wait_fw
);
523 /* this loads the firmware asynchronously.
524 Originally this was done synchroously in probe.
525 But it is better to load it asynchronously here than block
526 inside the probe function. Blocking inside probe affects boot time.
527 FW loading is triggered by the timer in the probe function
529 static void s2255_fwchunk_complete(struct urb
*urb
)
531 struct s2255_fw
*data
= urb
->context
;
532 struct usb_device
*udev
= urb
->dev
;
534 dprintk(100, "udev %p urb %p", udev
, urb
);
536 dev_err(&udev
->dev
, "URB failed with status %d\n", urb
->status
);
537 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
538 /* wake up anything waiting for the firmware */
539 wake_up(&data
->wait_fw
);
542 if (data
->fw_urb
== NULL
) {
543 s2255_dev_err(&udev
->dev
, "disconnected\n");
544 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
545 /* wake up anything waiting for the firmware */
546 wake_up(&data
->wait_fw
);
549 #define CHUNK_SIZE 512
550 /* all USB transfers must be done with continuous kernel memory.
551 can't allocate more than 128k in current linux kernel, so
552 upload the firmware in chunks
554 if (data
->fw_loaded
< data
->fw_size
) {
555 len
= (data
->fw_loaded
+ CHUNK_SIZE
) > data
->fw_size
?
556 data
->fw_size
% CHUNK_SIZE
: CHUNK_SIZE
;
558 if (len
< CHUNK_SIZE
)
559 memset(data
->pfw_data
, 0, CHUNK_SIZE
);
561 dprintk(100, "completed len %d, loaded %d \n", len
,
564 memcpy(data
->pfw_data
,
565 (char *) data
->fw
->data
+ data
->fw_loaded
, len
);
567 usb_fill_bulk_urb(data
->fw_urb
, udev
, usb_sndbulkpipe(udev
, 2),
568 data
->pfw_data
, CHUNK_SIZE
,
569 s2255_fwchunk_complete
, data
);
570 if (usb_submit_urb(data
->fw_urb
, GFP_ATOMIC
) < 0) {
571 dev_err(&udev
->dev
, "failed submit URB\n");
572 atomic_set(&data
->fw_state
, S2255_FW_FAILED
);
573 /* wake up anything waiting for the firmware */
574 wake_up(&data
->wait_fw
);
577 data
->fw_loaded
+= len
;
579 atomic_set(&data
->fw_state
, S2255_FW_LOADED_DSPWAIT
);
581 dprintk(100, "2255 complete done\n");
586 static int s2255_got_frame(struct s2255_dev
*dev
, int chn
, int jpgsize
)
588 struct s2255_dmaqueue
*dma_q
= &dev
->vidq
[chn
];
589 struct s2255_buffer
*buf
;
590 unsigned long flags
= 0;
592 dprintk(2, "wakeup: %p channel: %d\n", &dma_q
, chn
);
593 spin_lock_irqsave(&dev
->slock
, flags
);
595 if (list_empty(&dma_q
->active
)) {
596 dprintk(1, "No active queue to serve\n");
600 buf
= list_entry(dma_q
->active
.next
,
601 struct s2255_buffer
, vb
.queue
);
603 if (!waitqueue_active(&buf
->vb
.done
)) {
608 list_del(&buf
->vb
.queue
);
609 do_gettimeofday(&buf
->vb
.ts
);
610 dprintk(100, "[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
611 s2255_fillbuff(dev
, buf
, dma_q
->channel
, jpgsize
);
612 wake_up(&buf
->vb
.done
);
613 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf
, buf
->vb
.i
);
615 spin_unlock_irqrestore(&dev
->slock
, flags
);
620 static const struct s2255_fmt
*format_by_fourcc(int fourcc
)
624 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++) {
625 if (-1 == formats
[i
].fourcc
)
627 if (formats
[i
].fourcc
== fourcc
)
636 /* video buffer vmalloc implementation based partly on VIVI driver which is
637 * Copyright (c) 2006 by
638 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
639 * Ted Walther <ted--a.t--enumera.com>
640 * John Sokol <sokol--a.t--videotechnology.com>
641 * http://v4l.videotechnology.com/
644 static void s2255_fillbuff(struct s2255_dev
*dev
, struct s2255_buffer
*buf
,
645 int chn
, int jpgsize
)
650 char *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
651 unsigned long last_frame
;
652 struct s2255_framei
*frm
;
657 last_frame
= dev
->last_frame
[chn
];
658 if (last_frame
!= -1) {
659 frm
= &dev
->buffer
[chn
].frame
[last_frame
];
661 (const char *)dev
->buffer
[chn
].frame
[last_frame
].lpvbits
;
662 switch (buf
->fmt
->fourcc
) {
663 case V4L2_PIX_FMT_YUYV
:
664 case V4L2_PIX_FMT_UYVY
:
665 planar422p_to_yuv_packed((const unsigned char *)tmpbuf
,
670 case V4L2_PIX_FMT_GREY
:
671 memcpy(vbuf
, tmpbuf
, buf
->vb
.width
* buf
->vb
.height
);
673 case V4L2_PIX_FMT_JPEG
:
674 buf
->vb
.size
= jpgsize
;
675 memcpy(vbuf
, tmpbuf
, buf
->vb
.size
);
677 case V4L2_PIX_FMT_YUV422P
:
679 buf
->vb
.width
* buf
->vb
.height
* 2);
682 printk(KERN_DEBUG
"s2255: unknown format?\n");
684 dev
->last_frame
[chn
] = -1;
686 printk(KERN_ERR
"s2255: =======no frame\n");
690 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
691 (unsigned long)vbuf
, pos
);
692 /* tell v4l buffer was filled */
694 buf
->vb
.field_count
= dev
->frame_count
[chn
] * 2;
695 do_gettimeofday(&ts
);
697 buf
->vb
.state
= VIDEOBUF_DONE
;
701 /* ------------------------------------------------------------------
703 ------------------------------------------------------------------*/
705 static int buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
708 struct s2255_fh
*fh
= vq
->priv_data
;
710 *size
= fh
->width
* fh
->height
* (fh
->fmt
->depth
>> 3);
713 *count
= S2255_DEF_BUFS
;
715 while (*size
* (*count
) > vid_limit
* 1024 * 1024)
721 static void free_buffer(struct videobuf_queue
*vq
, struct s2255_buffer
*buf
)
723 dprintk(4, "%s\n", __func__
);
725 videobuf_vmalloc_free(&buf
->vb
);
726 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
729 static int buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
730 enum v4l2_field field
)
732 struct s2255_fh
*fh
= vq
->priv_data
;
733 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
735 dprintk(4, "%s, field=%d\n", __func__
, field
);
739 if ((fh
->width
< norm_minw(fh
->dev
->vdev
[fh
->channel
])) ||
740 (fh
->width
> norm_maxw(fh
->dev
->vdev
[fh
->channel
])) ||
741 (fh
->height
< norm_minh(fh
->dev
->vdev
[fh
->channel
])) ||
742 (fh
->height
> norm_maxh(fh
->dev
->vdev
[fh
->channel
]))) {
743 dprintk(4, "invalid buffer prepare\n");
747 buf
->vb
.size
= fh
->width
* fh
->height
* (fh
->fmt
->depth
>> 3);
749 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
) {
750 dprintk(4, "invalid buffer prepare\n");
755 buf
->vb
.width
= fh
->width
;
756 buf
->vb
.height
= fh
->height
;
757 buf
->vb
.field
= field
;
760 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
761 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
766 buf
->vb
.state
= VIDEOBUF_PREPARED
;
769 free_buffer(vq
, buf
);
773 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
775 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
776 struct s2255_fh
*fh
= vq
->priv_data
;
777 struct s2255_dev
*dev
= fh
->dev
;
778 struct s2255_dmaqueue
*vidq
= &dev
->vidq
[fh
->channel
];
780 dprintk(1, "%s\n", __func__
);
782 buf
->vb
.state
= VIDEOBUF_QUEUED
;
783 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
786 static void buffer_release(struct videobuf_queue
*vq
,
787 struct videobuf_buffer
*vb
)
789 struct s2255_buffer
*buf
= container_of(vb
, struct s2255_buffer
, vb
);
790 struct s2255_fh
*fh
= vq
->priv_data
;
791 dprintk(4, "%s %d\n", __func__
, fh
->channel
);
792 free_buffer(vq
, buf
);
795 static struct videobuf_queue_ops s2255_video_qops
= {
796 .buf_setup
= buffer_setup
,
797 .buf_prepare
= buffer_prepare
,
798 .buf_queue
= buffer_queue
,
799 .buf_release
= buffer_release
,
803 static int res_get(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
806 mutex_lock(&dev
->lock
);
807 if (dev
->resources
[fh
->channel
]) {
808 /* no, someone else uses it */
809 mutex_unlock(&dev
->lock
);
812 /* it's free, grab it */
813 dev
->resources
[fh
->channel
] = 1;
814 fh
->resources
[fh
->channel
] = 1;
815 dprintk(1, "s2255: res: get\n");
816 mutex_unlock(&dev
->lock
);
820 static int res_locked(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
822 return dev
->resources
[fh
->channel
];
825 static int res_check(struct s2255_fh
*fh
)
827 return fh
->resources
[fh
->channel
];
831 static void res_free(struct s2255_dev
*dev
, struct s2255_fh
*fh
)
833 mutex_lock(&dev
->lock
);
834 dev
->resources
[fh
->channel
] = 0;
835 fh
->resources
[fh
->channel
] = 0;
836 mutex_unlock(&dev
->lock
);
837 dprintk(1, "res: put\n");
841 static int vidioc_querycap(struct file
*file
, void *priv
,
842 struct v4l2_capability
*cap
)
844 struct s2255_fh
*fh
= file
->private_data
;
845 struct s2255_dev
*dev
= fh
->dev
;
846 strlcpy(cap
->driver
, "s2255", sizeof(cap
->driver
));
847 strlcpy(cap
->card
, "s2255", sizeof(cap
->card
));
848 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
849 cap
->version
= S2255_VERSION
;
850 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
854 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
855 struct v4l2_fmtdesc
*f
)
861 if (index
>= ARRAY_SIZE(formats
))
864 dprintk(4, "name %s\n", formats
[index
].name
);
865 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
866 f
->pixelformat
= formats
[index
].fourcc
;
870 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
871 struct v4l2_format
*f
)
873 struct s2255_fh
*fh
= priv
;
875 f
->fmt
.pix
.width
= fh
->width
;
876 f
->fmt
.pix
.height
= fh
->height
;
877 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
878 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
879 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* (fh
->fmt
->depth
>> 3);
880 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
884 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
885 struct v4l2_format
*f
)
887 const struct s2255_fmt
*fmt
;
888 enum v4l2_field field
;
890 struct s2255_fh
*fh
= priv
;
891 struct s2255_dev
*dev
= fh
->dev
;
895 (dev
->vdev
[fh
->channel
]->current_norm
& V4L2_STD_NTSC
) ? 1 : 0;
897 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
902 field
= f
->fmt
.pix
.field
;
903 if (field
== V4L2_FIELD_ANY
)
906 dprintk(4, "try format %d \n", is_ntsc
);
907 /* supports 3 sizes. see s2255drv.h */
908 dprintk(50, "width test %d, height %d\n",
909 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
912 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_NTSC
* 2) {
913 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
* 2;
915 field
= V4L2_FIELD_SEQ_TB
;
916 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
917 (field
== V4L2_FIELD_SEQ_TB
) ||
918 (field
== V4L2_FIELD_INTERLACED_TB
))) {
919 dprintk(1, "unsupported field setting\n");
923 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_NTSC
;
925 field
= V4L2_FIELD_TOP
;
926 } else if (!((field
== V4L2_FIELD_TOP
) ||
927 (field
== V4L2_FIELD_BOTTOM
))) {
928 dprintk(1, "unsupported field setting\n");
933 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_NTSC
)
934 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_NTSC
;
935 else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_NTSC
)
936 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_NTSC
;
937 else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_NTSC
)
938 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
940 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_NTSC
;
943 if (f
->fmt
.pix
.height
>= NUM_LINES_1CIFS_PAL
* 2) {
944 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
* 2;
946 field
= V4L2_FIELD_SEQ_TB
;
947 } else if (!((field
== V4L2_FIELD_INTERLACED
) ||
948 (field
== V4L2_FIELD_SEQ_TB
) ||
949 (field
== V4L2_FIELD_INTERLACED_TB
))) {
950 dprintk(1, "unsupported field setting\n");
954 f
->fmt
.pix
.height
= NUM_LINES_1CIFS_PAL
;
956 field
= V4L2_FIELD_TOP
;
957 } else if (!((field
== V4L2_FIELD_TOP
) ||
958 (field
== V4L2_FIELD_BOTTOM
))) {
959 dprintk(1, "unsupported field setting\n");
963 if (f
->fmt
.pix
.width
>= LINE_SZ_4CIFS_PAL
) {
964 dprintk(50, "pal 704\n");
965 f
->fmt
.pix
.width
= LINE_SZ_4CIFS_PAL
;
966 field
= V4L2_FIELD_SEQ_TB
;
967 } else if (f
->fmt
.pix
.width
>= LINE_SZ_2CIFS_PAL
) {
968 dprintk(50, "pal 352A\n");
969 f
->fmt
.pix
.width
= LINE_SZ_2CIFS_PAL
;
970 field
= V4L2_FIELD_TOP
;
971 } else if (f
->fmt
.pix
.width
>= LINE_SZ_1CIFS_PAL
) {
972 dprintk(50, "pal 352B\n");
973 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
974 field
= V4L2_FIELD_TOP
;
976 dprintk(50, "pal 352C\n");
977 f
->fmt
.pix
.width
= LINE_SZ_1CIFS_PAL
;
978 field
= V4L2_FIELD_TOP
;
982 dprintk(50, "width %d height %d field %d \n", f
->fmt
.pix
.width
,
983 f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
984 f
->fmt
.pix
.field
= field
;
985 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
986 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
990 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
991 struct v4l2_format
*f
)
993 struct s2255_fh
*fh
= priv
;
994 const struct s2255_fmt
*fmt
;
995 struct videobuf_queue
*q
= &fh
->vb_vidq
;
999 ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
1004 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1009 mutex_lock(&q
->vb_lock
);
1011 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1012 dprintk(1, "queue busy\n");
1017 if (res_locked(fh
->dev
, fh
)) {
1018 dprintk(1, "can't change format after started\n");
1024 fh
->width
= f
->fmt
.pix
.width
;
1025 fh
->height
= f
->fmt
.pix
.height
;
1026 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1028 norm
= norm_minw(fh
->dev
->vdev
[fh
->channel
]);
1029 if (fh
->width
> norm_minw(fh
->dev
->vdev
[fh
->channel
])) {
1030 if (fh
->height
> norm_minh(fh
->dev
->vdev
[fh
->channel
]))
1031 fh
->mode
.scale
= SCALE_4CIFS
;
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
) {
1133 linesPerFrame
= NUM_LINES_4CIFS_NTSC
* 2;
1134 pixelsPerLine
= LINE_SZ_4CIFS_NTSC
;
1137 linesPerFrame
= NUM_LINES_2CIFS_NTSC
;
1138 pixelsPerLine
= LINE_SZ_2CIFS_NTSC
;
1141 linesPerFrame
= NUM_LINES_1CIFS_NTSC
;
1142 pixelsPerLine
= LINE_SZ_1CIFS_NTSC
;
1147 } else if (mode
->format
== FORMAT_PAL
) {
1148 switch (mode
->scale
) {
1150 linesPerFrame
= NUM_LINES_4CIFS_PAL
* 2;
1151 pixelsPerLine
= LINE_SZ_4CIFS_PAL
;
1154 linesPerFrame
= NUM_LINES_2CIFS_PAL
;
1155 pixelsPerLine
= LINE_SZ_2CIFS_PAL
;
1158 linesPerFrame
= NUM_LINES_1CIFS_PAL
;
1159 pixelsPerLine
= LINE_SZ_1CIFS_PAL
;
1165 outImageSize
= linesPerFrame
* pixelsPerLine
;
1166 if ((mode
->color
& MASK_COLOR
) != COLOR_Y8
) {
1167 /* 2 bytes/pixel if not monochrome */
1171 /* total bytes to send including prefix and 4K padding;
1172 must be a multiple of USB_READ_SIZE */
1173 usbInSize
= outImageSize
+ PREFIX_SIZE
; /* always send prefix */
1174 mask_mult
= 0xffffffffUL
- DEF_USB_BLOCK
+ 1;
1175 /* if size not a multiple of USB_READ_SIZE */
1176 if (usbInSize
& ~mask_mult
)
1177 usbInSize
= (usbInSize
& mask_mult
) + (DEF_USB_BLOCK
);
1181 static void dump_verify_mode(struct s2255_dev
*sdev
, struct s2255_mode
*mode
)
1183 struct device
*dev
= &sdev
->udev
->dev
;
1184 dev_info(dev
, "------------------------------------------------\n");
1185 dev_info(dev
, "verify mode\n");
1186 dev_info(dev
, "format: %d\n", mode
->format
);
1187 dev_info(dev
, "scale: %d\n", mode
->scale
);
1188 dev_info(dev
, "fdec: %d\n", mode
->fdec
);
1189 dev_info(dev
, "color: %d\n", mode
->color
);
1190 dev_info(dev
, "bright: 0x%x\n", mode
->bright
);
1191 dev_info(dev
, "restart: 0x%x\n", mode
->restart
);
1192 dev_info(dev
, "usb_block: 0x%x\n", mode
->usb_block
);
1193 dev_info(dev
, "single: 0x%x\n", mode
->single
);
1194 dev_info(dev
, "------------------------------------------------\n");
1198 * set mode is the function which controls the DSP.
1199 * the restart parameter in struct s2255_mode should be set whenever
1200 * the image size could change via color format, video system or image
1202 * When the restart parameter is set, we sleep for ONE frame to allow the
1203 * DSP time to get the new frame
1205 static int s2255_set_mode(struct s2255_dev
*dev
, unsigned long chn
,
1206 struct s2255_mode
*mode
)
1210 unsigned long chn_rev
;
1212 mutex_lock(&dev
->lock
);
1213 chn_rev
= G_chnmap
[chn
];
1214 dprintk(3, "mode scale [%ld] %p %d\n", chn
, mode
, mode
->scale
);
1215 dprintk(3, "mode scale [%ld] %p %d\n", chn
, &dev
->mode
[chn
],
1216 dev
->mode
[chn
].scale
);
1217 dprintk(2, "mode contrast %x\n", mode
->contrast
);
1219 /* if JPEG, set the quality */
1220 if ((mode
->color
& MASK_COLOR
) == COLOR_JPG
)
1221 mode
->color
= (dev
->jc
[chn
].quality
<< 8) | COLOR_JPG
;
1224 dev
->mode
[chn
] = *mode
;
1225 dev
->req_image_size
[chn
] = get_transfer_size(mode
);
1226 dprintk(1, "transfer size %ld\n", dev
->req_image_size
[chn
]);
1228 buffer
= kzalloc(512, GFP_KERNEL
);
1229 if (buffer
== NULL
) {
1230 dev_err(&dev
->udev
->dev
, "out of mem\n");
1231 mutex_unlock(&dev
->lock
);
1236 buffer
[0] = IN_DATA_TOKEN
;
1237 buffer
[1] = (u32
) chn_rev
;
1238 buffer
[2] = CMD_SET_MODE
;
1239 memcpy(&buffer
[3], &dev
->mode
[chn
], sizeof(struct s2255_mode
));
1240 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
1242 dump_verify_mode(dev
, mode
);
1244 dprintk(1, "set mode done chn %lu, %d\n", chn
, res
);
1246 /* wait at least 3 frames before continuing */
1247 if (mode
->restart
) {
1248 dev
->setmode_ready
[chn
] = 0;
1249 wait_event_timeout(dev
->wait_setmode
[chn
],
1250 (dev
->setmode_ready
[chn
] != 0),
1251 msecs_to_jiffies(S2255_SETMODE_TIMEOUT
));
1252 if (dev
->setmode_ready
[chn
] != 1) {
1253 printk(KERN_DEBUG
"s2255: no set mode response\n");
1258 /* clear the restart flag */
1259 dev
->mode
[chn
].restart
= 0;
1260 mutex_unlock(&dev
->lock
);
1264 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1267 struct s2255_fh
*fh
= priv
;
1268 struct s2255_dev
*dev
= fh
->dev
;
1269 struct s2255_mode
*new_mode
;
1270 struct s2255_mode
*old_mode
;
1273 dprintk(4, "%s\n", __func__
);
1274 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1275 dev_err(&dev
->udev
->dev
, "invalid fh type0\n");
1278 if (i
!= fh
->type
) {
1279 dev_err(&dev
->udev
->dev
, "invalid fh type1\n");
1283 if (!res_get(dev
, fh
)) {
1284 s2255_dev_err(&dev
->udev
->dev
, "stream busy\n");
1288 /* send a set mode command everytime with restart.
1289 in case we switch resolutions or other parameters */
1291 new_mode
= &fh
->mode
;
1292 old_mode
= &fh
->dev
->mode
[chn
];
1294 if (new_mode
->color
!= old_mode
->color
)
1295 new_mode
->restart
= 1;
1296 else if (new_mode
->scale
!= old_mode
->scale
)
1297 new_mode
->restart
= 1;
1298 else if (new_mode
->format
!= old_mode
->format
)
1299 new_mode
->restart
= 1;
1301 s2255_set_mode(dev
, chn
, new_mode
);
1302 new_mode
->restart
= 0;
1303 *old_mode
= *new_mode
;
1304 dev
->cur_fmt
[chn
] = fh
->fmt
;
1305 dprintk(1, "%s[%d]\n", __func__
, chn
);
1306 dev
->last_frame
[chn
] = -1;
1307 dev
->bad_payload
[chn
] = 0;
1308 dev
->cur_frame
[chn
] = 0;
1309 dev
->frame_count
[chn
] = 0;
1310 for (j
= 0; j
< SYS_FRAMES
; j
++) {
1311 dev
->buffer
[chn
].frame
[j
].ulState
= S2255_READ_IDLE
;
1312 dev
->buffer
[chn
].frame
[j
].cur_size
= 0;
1314 res
= videobuf_streamon(&fh
->vb_vidq
);
1316 s2255_start_acquire(dev
, chn
);
1317 dev
->b_acquire
[chn
] = 1;
1324 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1326 struct s2255_fh
*fh
= priv
;
1327 struct s2255_dev
*dev
= fh
->dev
;
1329 dprintk(4, "%s\n, channel: %d", __func__
, fh
->channel
);
1330 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1331 printk(KERN_ERR
"invalid fh type0\n");
1334 if (i
!= fh
->type
) {
1335 printk(KERN_ERR
"invalid type i\n");
1338 s2255_stop_acquire(dev
, fh
->channel
);
1339 videobuf_streamoff(&fh
->vb_vidq
);
1344 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1346 struct s2255_fh
*fh
= priv
;
1347 struct s2255_mode
*mode
;
1348 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1351 mutex_lock(&q
->vb_lock
);
1352 if (videobuf_queue_is_busy(q
)) {
1353 dprintk(1, "queue busy\n");
1358 if (res_locked(fh
->dev
, fh
)) {
1359 dprintk(1, "can't change standard after started\n");
1365 if (*i
& V4L2_STD_NTSC
) {
1366 dprintk(4, "vidioc_s_std NTSC\n");
1367 mode
->format
= FORMAT_NTSC
;
1368 } else if (*i
& V4L2_STD_PAL
) {
1369 dprintk(4, "vidioc_s_std PAL\n");
1370 mode
->format
= FORMAT_PAL
;
1375 mutex_unlock(&q
->vb_lock
);
1379 /* Sensoray 2255 is a multiple channel capture device.
1380 It does not have a "crossbar" of inputs.
1381 We use one V4L device per channel. The user must
1382 be aware that certain combinations are not allowed.
1383 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1384 at once in color(you can do full fps on 4 channels with greyscale.
1386 static int vidioc_enum_input(struct file
*file
, void *priv
,
1387 struct v4l2_input
*inp
)
1389 if (inp
->index
!= 0)
1392 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1393 inp
->std
= S2255_NORMS
;
1394 strlcpy(inp
->name
, "Camera", sizeof(inp
->name
));
1398 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1403 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1410 /* --- controls ---------------------------------------------- */
1411 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1412 struct v4l2_queryctrl
*qc
)
1416 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1417 if (qc
->id
&& qc
->id
== s2255_qctrl
[i
].id
) {
1418 memcpy(qc
, &(s2255_qctrl
[i
]), sizeof(*qc
));
1422 dprintk(4, "query_ctrl -EINVAL %d\n", qc
->id
);
1426 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1427 struct v4l2_control
*ctrl
)
1431 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1432 if (ctrl
->id
== s2255_qctrl
[i
].id
) {
1433 ctrl
->value
= qctl_regs
[i
];
1436 dprintk(4, "g_ctrl -EINVAL\n");
1441 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1442 struct v4l2_control
*ctrl
)
1445 struct s2255_fh
*fh
= priv
;
1446 struct s2255_dev
*dev
= fh
->dev
;
1447 struct s2255_mode
*mode
;
1449 dprintk(4, "vidioc_s_ctrl\n");
1450 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++) {
1451 if (ctrl
->id
== s2255_qctrl
[i
].id
) {
1452 if (ctrl
->value
< s2255_qctrl
[i
].minimum
||
1453 ctrl
->value
> s2255_qctrl
[i
].maximum
)
1456 qctl_regs
[i
] = ctrl
->value
;
1457 /* update the mode to the corresponding value */
1459 case V4L2_CID_BRIGHTNESS
:
1460 mode
->bright
= ctrl
->value
;
1462 case V4L2_CID_CONTRAST
:
1463 mode
->contrast
= ctrl
->value
;
1466 mode
->hue
= ctrl
->value
;
1468 case V4L2_CID_SATURATION
:
1469 mode
->saturation
= ctrl
->value
;
1473 /* set mode here. Note: stream does not need restarted.
1474 some V4L programs restart stream unnecessarily
1477 s2255_set_mode(dev
, fh
->channel
, mode
);
1484 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1485 struct v4l2_jpegcompression
*jc
)
1487 struct s2255_fh
*fh
= priv
;
1488 struct s2255_dev
*dev
= fh
->dev
;
1489 *jc
= dev
->jc
[fh
->channel
];
1490 dprintk(2, "getting jpegcompression, quality %d\n", jc
->quality
);
1494 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1495 struct v4l2_jpegcompression
*jc
)
1497 struct s2255_fh
*fh
= priv
;
1498 struct s2255_dev
*dev
= fh
->dev
;
1499 if (jc
->quality
< 0 || jc
->quality
> 100)
1501 dev
->jc
[fh
->channel
].quality
= jc
->quality
;
1502 dprintk(2, "setting jpeg quality %d\n", jc
->quality
);
1505 static int s2255_open(struct file
*file
)
1507 int minor
= video_devdata(file
)->minor
;
1508 struct s2255_dev
*h
, *dev
= NULL
;
1509 struct s2255_fh
*fh
;
1510 struct list_head
*list
;
1511 enum v4l2_buf_type type
= 0;
1513 int cur_channel
= -1;
1515 dprintk(1, "s2255: open called (minor=%d)\n", minor
);
1518 list_for_each(list
, &s2255_devlist
) {
1519 h
= list_entry(list
, struct s2255_dev
, s2255_devlist
);
1520 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1521 if (h
->vdev
[i
]->minor
== minor
) {
1524 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1529 if ((NULL
== dev
) || (cur_channel
== -1)) {
1531 printk(KERN_INFO
"s2255: openv4l no dev\n");
1535 if (atomic_read(&dev
->fw_data
->fw_state
) == S2255_FW_DISCONNECTING
) {
1537 printk(KERN_INFO
"disconnecting\n");
1540 kref_get(&dev
->kref
);
1541 mutex_lock(&dev
->open_lock
);
1543 dev
->users
[cur_channel
]++;
1544 dprintk(4, "s2255: open_handles %d\n", dev
->users
[cur_channel
]);
1546 switch (atomic_read(&dev
->fw_data
->fw_state
)) {
1547 case S2255_FW_FAILED
:
1548 s2255_dev_err(&dev
->udev
->dev
,
1549 "firmware load failed. retrying.\n");
1550 s2255_fwload_start(dev
, 1);
1551 wait_event_timeout(dev
->fw_data
->wait_fw
,
1552 ((atomic_read(&dev
->fw_data
->fw_state
)
1553 == S2255_FW_SUCCESS
) ||
1554 (atomic_read(&dev
->fw_data
->fw_state
)
1555 == S2255_FW_DISCONNECTING
)),
1556 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1558 case S2255_FW_NOTLOADED
:
1559 case S2255_FW_LOADED_DSPWAIT
:
1560 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1561 driver loaded and then device immediately opened */
1562 printk(KERN_INFO
"%s waiting for firmware load\n", __func__
);
1563 wait_event_timeout(dev
->fw_data
->wait_fw
,
1564 ((atomic_read(&dev
->fw_data
->fw_state
)
1565 == S2255_FW_SUCCESS
) ||
1566 (atomic_read(&dev
->fw_data
->fw_state
)
1567 == S2255_FW_DISCONNECTING
)),
1568 msecs_to_jiffies(S2255_LOAD_TIMEOUT
));
1570 case S2255_FW_SUCCESS
:
1574 state
= atomic_read(&dev
->fw_data
->fw_state
);
1575 if (state
!= S2255_FW_SUCCESS
) {
1578 case S2255_FW_FAILED
:
1579 printk(KERN_INFO
"2255 FW load failed. %d\n", state
);
1582 case S2255_FW_DISCONNECTING
:
1583 printk(KERN_INFO
"%s: disconnecting\n", __func__
);
1586 case S2255_FW_LOADED_DSPWAIT
:
1587 case S2255_FW_NOTLOADED
:
1588 printk(KERN_INFO
"%s: firmware not loaded yet"
1589 "please try again later\n",
1594 printk(KERN_INFO
"%s: unknown state\n", __func__
);
1598 dev
->users
[cur_channel
]--;
1599 mutex_unlock(&dev
->open_lock
);
1600 kref_put(&dev
->kref
, s2255_destroy
);
1605 /* allocate + initialize per filehandle data */
1606 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1608 dev
->users
[cur_channel
]--;
1609 mutex_unlock(&dev
->open_lock
);
1610 kref_put(&dev
->kref
, s2255_destroy
);
1615 file
->private_data
= fh
;
1617 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1618 fh
->mode
= dev
->mode
[cur_channel
];
1619 fh
->fmt
= dev
->cur_fmt
[cur_channel
];
1620 /* default 4CIF NTSC */
1621 fh
->width
= LINE_SZ_4CIFS_NTSC
;
1622 fh
->height
= NUM_LINES_4CIFS_NTSC
* 2;
1623 fh
->channel
= cur_channel
;
1625 /* configure channel to default state */
1626 if (!dev
->chn_configured
[cur_channel
]) {
1627 s2255_set_mode(dev
, cur_channel
, &fh
->mode
);
1628 dev
->chn_configured
[cur_channel
] = 1;
1632 /* Put all controls at a sane state */
1633 for (i
= 0; i
< ARRAY_SIZE(s2255_qctrl
); i
++)
1634 qctl_regs
[i
] = s2255_qctrl
[i
].default_value
;
1636 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1637 minor
, v4l2_type_names
[type
], dev
->users
[cur_channel
]);
1638 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1639 (unsigned long)fh
, (unsigned long)dev
,
1640 (unsigned long)&dev
->vidq
[cur_channel
]);
1641 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1642 list_empty(&dev
->vidq
[cur_channel
].active
));
1644 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &s2255_video_qops
,
1647 V4L2_FIELD_INTERLACED
,
1648 sizeof(struct s2255_buffer
), fh
);
1650 mutex_unlock(&dev
->open_lock
);
1656 static unsigned int s2255_poll(struct file
*file
,
1657 struct poll_table_struct
*wait
)
1659 struct s2255_fh
*fh
= file
->private_data
;
1661 dprintk(100, "%s\n", __func__
);
1663 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1666 rc
= videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1670 static void s2255_destroy(struct kref
*kref
)
1672 struct s2255_dev
*dev
= to_s2255_dev(kref
);
1673 struct list_head
*list
;
1676 printk(KERN_ERR
"s2255drv: kref problem\n");
1679 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
1680 wake_up(&dev
->fw_data
->wait_fw
);
1681 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1682 dev
->setmode_ready
[i
] = 1;
1683 wake_up(&dev
->wait_setmode
[i
]);
1685 mutex_lock(&dev
->open_lock
);
1686 /* reset the DSP so firmware can be reload next time */
1687 s2255_reset_dsppower(dev
);
1688 s2255_exit_v4l(dev
);
1689 /* board shutdown stops the read pipe if it is running */
1690 s2255_board_shutdown(dev
);
1691 /* make sure firmware still not trying to load */
1692 del_timer(&dev
->timer
); /* only started in .probe and .open */
1694 if (dev
->fw_data
->fw_urb
) {
1695 dprintk(2, "kill fw_urb\n");
1696 usb_kill_urb(dev
->fw_data
->fw_urb
);
1697 usb_free_urb(dev
->fw_data
->fw_urb
);
1698 dev
->fw_data
->fw_urb
= NULL
;
1700 if (dev
->fw_data
->fw
)
1701 release_firmware(dev
->fw_data
->fw
);
1702 kfree(dev
->fw_data
->pfw_data
);
1703 kfree(dev
->fw_data
);
1704 usb_put_dev(dev
->udev
);
1705 dprintk(1, "%s", __func__
);
1707 while (!list_empty(&s2255_devlist
)) {
1708 list
= s2255_devlist
.next
;
1711 mutex_unlock(&dev
->open_lock
);
1715 static int s2255_close(struct file
*file
)
1717 struct s2255_fh
*fh
= file
->private_data
;
1718 struct s2255_dev
*dev
= fh
->dev
;
1719 int minor
= video_devdata(file
)->minor
;
1723 mutex_lock(&dev
->open_lock
);
1725 /* turn off stream */
1726 if (res_check(fh
)) {
1727 if (dev
->b_acquire
[fh
->channel
])
1728 s2255_stop_acquire(dev
, fh
->channel
);
1729 videobuf_streamoff(&fh
->vb_vidq
);
1733 videobuf_mmap_free(&fh
->vb_vidq
);
1734 dev
->users
[fh
->channel
]--;
1736 mutex_unlock(&dev
->open_lock
);
1738 kref_put(&dev
->kref
, s2255_destroy
);
1739 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1740 minor
, dev
->users
[fh
->channel
]);
1745 static int s2255_mmap_v4l(struct file
*file
, struct vm_area_struct
*vma
)
1747 struct s2255_fh
*fh
= file
->private_data
;
1752 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1754 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1756 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1757 (unsigned long)vma
->vm_start
,
1758 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
, ret
);
1763 static const struct v4l2_file_operations s2255_fops_v4l
= {
1764 .owner
= THIS_MODULE
,
1766 .release
= s2255_close
,
1768 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1769 .mmap
= s2255_mmap_v4l
,
1772 static const struct v4l2_ioctl_ops s2255_ioctl_ops
= {
1773 .vidioc_querycap
= vidioc_querycap
,
1774 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1775 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1776 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1777 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1778 .vidioc_reqbufs
= vidioc_reqbufs
,
1779 .vidioc_querybuf
= vidioc_querybuf
,
1780 .vidioc_qbuf
= vidioc_qbuf
,
1781 .vidioc_dqbuf
= vidioc_dqbuf
,
1782 .vidioc_s_std
= vidioc_s_std
,
1783 .vidioc_enum_input
= vidioc_enum_input
,
1784 .vidioc_g_input
= vidioc_g_input
,
1785 .vidioc_s_input
= vidioc_s_input
,
1786 .vidioc_queryctrl
= vidioc_queryctrl
,
1787 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1788 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1789 .vidioc_streamon
= vidioc_streamon
,
1790 .vidioc_streamoff
= vidioc_streamoff
,
1791 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1792 .vidiocgmbuf
= vidioc_cgmbuf
,
1794 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1795 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1798 static struct video_device
template = {
1800 .fops
= &s2255_fops_v4l
,
1801 .ioctl_ops
= &s2255_ioctl_ops
,
1803 .release
= video_device_release
,
1804 .tvnorms
= S2255_NORMS
,
1805 .current_norm
= V4L2_STD_NTSC_M
,
1808 static int s2255_probe_v4l(struct s2255_dev
*dev
)
1812 int cur_nr
= video_nr
;
1814 /* initialize all video 4 linux */
1815 list_add_tail(&dev
->s2255_devlist
, &s2255_devlist
);
1816 /* register 4 video devices */
1817 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1818 INIT_LIST_HEAD(&dev
->vidq
[i
].active
);
1819 dev
->vidq
[i
].dev
= dev
;
1820 dev
->vidq
[i
].channel
= i
;
1821 dev
->vidq
[i
].kthread
= NULL
;
1822 /* register 4 video devices */
1823 dev
->vdev
[i
] = video_device_alloc();
1824 memcpy(dev
->vdev
[i
], &template, sizeof(struct video_device
));
1825 dev
->vdev
[i
]->parent
= &dev
->interface
->dev
;
1827 ret
= video_register_device(dev
->vdev
[i
],
1831 ret
= video_register_device(dev
->vdev
[i
],
1834 video_set_drvdata(dev
->vdev
[i
], dev
);
1837 dev_err(&dev
->udev
->dev
,
1838 "failed to register video device!\n");
1842 printk(KERN_INFO
"Sensoray 2255 V4L driver\n");
1846 static void s2255_exit_v4l(struct s2255_dev
*dev
)
1850 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
1851 if (-1 != dev
->vdev
[i
]->minor
) {
1852 video_unregister_device(dev
->vdev
[i
]);
1853 printk(KERN_INFO
"s2255 unregistered\n");
1855 video_device_release(dev
->vdev
[i
]);
1856 printk(KERN_INFO
"s2255 released\n");
1861 /* this function moves the usb stream read pipe data
1862 * into the system buffers.
1863 * returns 0 on success, EAGAIN if more data to process( call this
1866 * Received frame structure:
1867 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1868 * bytes 4-7: channel: 0-3
1869 * bytes 8-11: payload size: size of the frame
1870 * bytes 12-payloadsize+12: frame data
1872 static int save_frame(struct s2255_dev
*dev
, struct s2255_pipeinfo
*pipe_info
)
1878 unsigned long copy_size
;
1881 struct s2255_framei
*frm
;
1882 unsigned char *pdata
;
1884 dprintk(100, "buffer to user\n");
1886 idx
= dev
->cur_frame
[dev
->cc
];
1887 frm
= &dev
->buffer
[dev
->cc
].frame
[idx
];
1889 if (frm
->ulState
== S2255_READ_IDLE
) {
1894 /* search for marker codes */
1895 pdata
= (unsigned char *)pipe_info
->transfer_buffer
;
1896 for (jj
= 0; jj
< (pipe_info
->cur_transfer_size
- 12); jj
++) {
1897 switch (*(s32
*) pdata
) {
1898 case S2255_MARKER_FRAME
:
1899 pdword
= (s32
*)pdata
;
1900 dprintk(4, "found frame marker at offset:"
1901 " %d [%x %x]\n", jj
, pdata
[0],
1903 offset
= jj
+ PREFIX_SIZE
;
1906 if (cc
>= MAX_CHANNELS
) {
1912 dev
->cc
= G_chnmap
[cc
];
1913 payload
= pdword
[3];
1914 if (payload
> dev
->req_image_size
[dev
->cc
]) {
1915 dev
->bad_payload
[dev
->cc
]++;
1916 /* discard the bad frame */
1919 dev
->pkt_size
[dev
->cc
] = payload
;
1920 dev
->jpg_size
[dev
->cc
] = pdword
[4];
1922 case S2255_MARKER_RESPONSE
:
1923 pdword
= (s32
*)pdata
;
1924 pdata
+= DEF_USB_BLOCK
;
1925 jj
+= DEF_USB_BLOCK
;
1926 if (pdword
[1] >= MAX_CHANNELS
)
1928 cc
= G_chnmap
[pdword
[1]];
1929 if (!(cc
>= 0 && cc
< MAX_CHANNELS
))
1931 switch (pdword
[2]) {
1933 /* check if channel valid */
1934 /* set mode ready */
1935 dev
->setmode_ready
[cc
] = 1;
1936 wake_up(&dev
->wait_setmode
[cc
]);
1937 dprintk(5, "setmode ready %d\n", cc
);
1941 dev
->chn_ready
|= (1 << cc
);
1942 if ((dev
->chn_ready
& 0x0f) != 0x0f)
1944 /* all channels ready */
1945 printk(KERN_INFO
"s2255: fw loaded\n");
1946 atomic_set(&dev
->fw_data
->fw_state
,
1948 wake_up(&dev
->fw_data
->wait_fw
);
1951 printk(KERN_INFO
"s2255 unknwn resp\n");
1965 idx
= dev
->cur_frame
[dev
->cc
];
1966 frm
= &dev
->buffer
[dev
->cc
].frame
[idx
];
1968 /* search done. now find out if should be acquiring on this channel */
1969 if (!dev
->b_acquire
[dev
->cc
]) {
1970 /* we found a frame, but this channel is turned off */
1971 frm
->ulState
= S2255_READ_IDLE
;
1975 if (frm
->ulState
== S2255_READ_IDLE
) {
1976 frm
->ulState
= S2255_READ_FRAME
;
1980 /* skip the marker 512 bytes (and offset if out of sync) */
1981 psrc
= (u8
*)pipe_info
->transfer_buffer
+ offset
;
1984 if (frm
->lpvbits
== NULL
) {
1985 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1986 frm
, dev
, dev
->cc
, idx
);
1990 pdest
= frm
->lpvbits
+ frm
->cur_size
;
1992 copy_size
= (pipe_info
->cur_transfer_size
- offset
);
1994 size
= dev
->pkt_size
[dev
->cc
] - PREFIX_SIZE
;
1996 /* sanity check on pdest */
1997 if ((copy_size
+ frm
->cur_size
) < dev
->req_image_size
[dev
->cc
])
1998 memcpy(pdest
, psrc
, copy_size
);
2000 frm
->cur_size
+= copy_size
;
2001 dprintk(4, "cur_size size %lu size %lu \n", frm
->cur_size
, size
);
2003 if (frm
->cur_size
>= size
) {
2006 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2008 dev
->last_frame
[cc
] = dev
->cur_frame
[cc
];
2009 dev
->cur_frame
[cc
]++;
2010 /* end of system frame ring buffer, start at zero */
2011 if ((dev
->cur_frame
[cc
] == SYS_FRAMES
) ||
2012 (dev
->cur_frame
[cc
] == dev
->buffer
[cc
].dwFrames
))
2013 dev
->cur_frame
[cc
] = 0;
2015 if (dev
->b_acquire
[cc
])
2016 s2255_got_frame(dev
, cc
, dev
->jpg_size
[cc
]);
2017 dev
->frame_count
[cc
]++;
2018 frm
->ulState
= S2255_READ_IDLE
;
2022 /* done successfully */
2026 static void s2255_read_video_callback(struct s2255_dev
*dev
,
2027 struct s2255_pipeinfo
*pipe_info
)
2030 dprintk(50, "callback read video \n");
2032 if (dev
->cc
>= MAX_CHANNELS
) {
2034 dev_err(&dev
->udev
->dev
, "invalid channel\n");
2037 /* otherwise copy to the system buffers */
2038 res
= save_frame(dev
, pipe_info
);
2040 dprintk(4, "s2255: read callback failed\n");
2042 dprintk(50, "callback read video done\n");
2046 static long s2255_vendor_req(struct s2255_dev
*dev
, unsigned char Request
,
2047 u16 Index
, u16 Value
, void *TransferBuffer
,
2048 s32 TransferBufferLength
, int bOut
)
2052 r
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
2054 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
2056 Value
, Index
, TransferBuffer
,
2057 TransferBufferLength
, HZ
* 5);
2059 r
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
2060 Request
, USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
2061 Value
, Index
, TransferBuffer
,
2062 TransferBufferLength
, HZ
* 5);
2068 * retrieve FX2 firmware version. future use.
2069 * @param dev pointer to device extension
2070 * @return -1 for fail, else returns firmware version as an int(16 bits)
2072 static int s2255_get_fx2fw(struct s2255_dev
*dev
)
2076 unsigned char transBuffer
[64];
2077 ret
= s2255_vendor_req(dev
, S2255_VR_FW
, 0, 0, transBuffer
, 2,
2080 dprintk(2, "get fw error: %x\n", ret
);
2081 fw
= transBuffer
[0] + (transBuffer
[1] << 8);
2082 dprintk(2, "Get FW %x %x\n", transBuffer
[0], transBuffer
[1]);
2087 * Create the system ring buffer to copy frames into from the
2090 static int s2255_create_sys_buffers(struct s2255_dev
*dev
, unsigned long chn
)
2093 unsigned long reqsize
;
2094 dprintk(1, "create sys buffers\n");
2095 if (chn
>= MAX_CHANNELS
)
2098 dev
->buffer
[chn
].dwFrames
= SYS_FRAMES
;
2100 /* always allocate maximum size(PAL) for system buffers */
2101 reqsize
= SYS_FRAMES_MAXSIZE
;
2103 if (reqsize
> SYS_FRAMES_MAXSIZE
)
2104 reqsize
= SYS_FRAMES_MAXSIZE
;
2106 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2107 /* allocate the frames */
2108 dev
->buffer
[chn
].frame
[i
].lpvbits
= vmalloc(reqsize
);
2110 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2111 &dev
->buffer
[chn
].frame
[i
], chn
, i
,
2112 dev
->buffer
[chn
].frame
[i
].lpvbits
);
2113 dev
->buffer
[chn
].frame
[i
].size
= reqsize
;
2114 if (dev
->buffer
[chn
].frame
[i
].lpvbits
== NULL
) {
2115 printk(KERN_INFO
"out of memory. using less frames\n");
2116 dev
->buffer
[chn
].dwFrames
= i
;
2121 /* make sure internal states are set */
2122 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2123 dev
->buffer
[chn
].frame
[i
].ulState
= 0;
2124 dev
->buffer
[chn
].frame
[i
].cur_size
= 0;
2127 dev
->cur_frame
[chn
] = 0;
2128 dev
->last_frame
[chn
] = -1;
2132 static int s2255_release_sys_buffers(struct s2255_dev
*dev
,
2133 unsigned long channel
)
2136 dprintk(1, "release sys buffers\n");
2137 for (i
= 0; i
< SYS_FRAMES
; i
++) {
2138 if (dev
->buffer
[channel
].frame
[i
].lpvbits
) {
2139 dprintk(1, "vfree %p\n",
2140 dev
->buffer
[channel
].frame
[i
].lpvbits
);
2141 vfree(dev
->buffer
[channel
].frame
[i
].lpvbits
);
2143 dev
->buffer
[channel
].frame
[i
].lpvbits
= NULL
;
2148 static int s2255_board_init(struct s2255_dev
*dev
)
2151 struct s2255_mode mode_def
= DEF_MODEI_NTSC_CONT
;
2153 dprintk(4, "board init: %p", dev
);
2155 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2156 struct s2255_pipeinfo
*pipe
= &dev
->pipes
[j
];
2158 memset(pipe
, 0, sizeof(*pipe
));
2160 pipe
->cur_transfer_size
= S2255_USB_XFER_SIZE
;
2161 pipe
->max_transfer_size
= S2255_USB_XFER_SIZE
;
2163 pipe
->transfer_buffer
= kzalloc(pipe
->max_transfer_size
,
2165 if (pipe
->transfer_buffer
== NULL
) {
2166 dprintk(1, "out of memory!\n");
2172 /* query the firmware */
2173 fw_ver
= s2255_get_fx2fw(dev
);
2175 printk(KERN_INFO
"2255 usb firmware version %d \n", fw_ver
);
2176 if (fw_ver
< CUR_USB_FWVER
)
2177 dev_err(&dev
->udev
->dev
,
2178 "usb firmware not up to date %d\n", fw_ver
);
2180 for (j
= 0; j
< MAX_CHANNELS
; j
++) {
2181 dev
->b_acquire
[j
] = 0;
2182 dev
->mode
[j
] = mode_def
;
2183 dev
->jc
[j
].quality
= S2255_DEF_JPEG_QUAL
;
2184 dev
->cur_fmt
[j
] = &formats
[0];
2185 dev
->mode
[j
].restart
= 1;
2186 dev
->req_image_size
[j
] = get_transfer_size(&mode_def
);
2187 dev
->frame_count
[j
] = 0;
2188 /* create the system buffers */
2189 s2255_create_sys_buffers(dev
, j
);
2191 /* start read pipe */
2192 s2255_start_readpipe(dev
);
2194 dprintk(1, "S2255: board initialized\n");
2198 static int s2255_board_shutdown(struct s2255_dev
*dev
)
2202 dprintk(1, "S2255: board shutdown: %p", dev
);
2204 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2205 if (dev
->b_acquire
[i
])
2206 s2255_stop_acquire(dev
, i
);
2209 s2255_stop_readpipe(dev
);
2211 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2212 s2255_release_sys_buffers(dev
, i
);
2214 /* release transfer buffers */
2215 for (i
= 0; i
< MAX_PIPE_BUFFERS
; i
++) {
2216 struct s2255_pipeinfo
*pipe
= &dev
->pipes
[i
];
2217 kfree(pipe
->transfer_buffer
);
2222 static void read_pipe_completion(struct urb
*purb
)
2224 struct s2255_pipeinfo
*pipe_info
;
2225 struct s2255_dev
*dev
;
2229 pipe_info
= purb
->context
;
2230 dprintk(100, "read pipe completion %p, status %d\n", purb
,
2232 if (pipe_info
== NULL
) {
2233 dev_err(&purb
->dev
->dev
, "no context!\n");
2237 dev
= pipe_info
->dev
;
2239 dev_err(&purb
->dev
->dev
, "no context!\n");
2242 status
= purb
->status
;
2244 dprintk(2, "read_pipe_completion: err\n");
2248 if (pipe_info
->state
== 0) {
2249 dprintk(2, "exiting USB pipe");
2253 s2255_read_video_callback(dev
, pipe_info
);
2255 pipe_info
->err_count
= 0;
2256 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2258 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2260 pipe_info
->transfer_buffer
,
2261 pipe_info
->cur_transfer_size
,
2262 read_pipe_completion
, pipe_info
);
2264 if (pipe_info
->state
!= 0) {
2265 if (usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
)) {
2266 dev_err(&dev
->udev
->dev
, "error submitting urb\n");
2267 usb_free_urb(pipe_info
->stream_urb
);
2270 dprintk(2, "read pipe complete state 0\n");
2275 static int s2255_start_readpipe(struct s2255_dev
*dev
)
2280 struct s2255_pipeinfo
*pipe_info
= dev
->pipes
;
2281 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->read_endpoint
);
2282 dprintk(2, "start pipe IN %d\n", dev
->read_endpoint
);
2284 for (i
= 0; i
< MAX_PIPE_BUFFERS
; i
++) {
2285 pipe_info
->state
= 1;
2286 pipe_info
->buf_index
= (u32
) i
;
2287 pipe_info
->priority_set
= 0;
2288 pipe_info
->stream_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2289 if (!pipe_info
->stream_urb
) {
2290 dev_err(&dev
->udev
->dev
,
2291 "ReadStream: Unable to alloc URB\n");
2294 /* transfer buffer allocated in board_init */
2295 usb_fill_bulk_urb(pipe_info
->stream_urb
, dev
->udev
,
2297 pipe_info
->transfer_buffer
,
2298 pipe_info
->cur_transfer_size
,
2299 read_pipe_completion
, pipe_info
);
2301 pipe_info
->urb_size
= sizeof(pipe_info
->stream_urb
);
2302 dprintk(4, "submitting URB %p\n", pipe_info
->stream_urb
);
2303 retval
= usb_submit_urb(pipe_info
->stream_urb
, GFP_KERNEL
);
2305 printk(KERN_ERR
"s2255: start read pipe failed\n");
2313 /* starts acquisition process */
2314 static int s2255_start_acquire(struct s2255_dev
*dev
, unsigned long chn
)
2316 unsigned char *buffer
;
2318 unsigned long chn_rev
;
2320 if (chn
>= MAX_CHANNELS
) {
2321 dprintk(2, "start acquire failed, bad channel %lu\n", chn
);
2325 chn_rev
= G_chnmap
[chn
];
2326 dprintk(1, "S2255: start acquire %lu \n", chn
);
2328 buffer
= kzalloc(512, GFP_KERNEL
);
2329 if (buffer
== NULL
) {
2330 dev_err(&dev
->udev
->dev
, "out of mem\n");
2334 dev
->last_frame
[chn
] = -1;
2335 dev
->bad_payload
[chn
] = 0;
2336 dev
->cur_frame
[chn
] = 0;
2337 for (j
= 0; j
< SYS_FRAMES
; j
++) {
2338 dev
->buffer
[chn
].frame
[j
].ulState
= 0;
2339 dev
->buffer
[chn
].frame
[j
].cur_size
= 0;
2342 /* send the start command */
2343 *(u32
*) buffer
= IN_DATA_TOKEN
;
2344 *((u32
*) buffer
+ 1) = (u32
) chn_rev
;
2345 *((u32
*) buffer
+ 2) = (u32
) CMD_START
;
2346 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2348 dev_err(&dev
->udev
->dev
, "CMD_START error\n");
2350 dprintk(2, "start acquire exit[%lu] %d \n", chn
, res
);
2355 static int s2255_stop_acquire(struct s2255_dev
*dev
, unsigned long chn
)
2357 unsigned char *buffer
;
2359 unsigned long chn_rev
;
2361 if (chn
>= MAX_CHANNELS
) {
2362 dprintk(2, "stop acquire failed, bad channel %lu\n", chn
);
2365 chn_rev
= G_chnmap
[chn
];
2367 buffer
= kzalloc(512, GFP_KERNEL
);
2368 if (buffer
== NULL
) {
2369 dev_err(&dev
->udev
->dev
, "out of mem\n");
2373 /* send the stop command */
2374 dprintk(4, "stop acquire %lu\n", chn
);
2375 *(u32
*) buffer
= IN_DATA_TOKEN
;
2376 *((u32
*) buffer
+ 1) = (u32
) chn_rev
;
2377 *((u32
*) buffer
+ 2) = CMD_STOP
;
2378 res
= s2255_write_config(dev
->udev
, (unsigned char *)buffer
, 512);
2381 dev_err(&dev
->udev
->dev
, "CMD_STOP error\n");
2383 dprintk(4, "stop acquire: releasing states \n");
2386 dev
->b_acquire
[chn
] = 0;
2391 static void s2255_stop_readpipe(struct s2255_dev
*dev
)
2396 s2255_dev_err(&dev
->udev
->dev
, "invalid device\n");
2399 dprintk(4, "stop read pipe\n");
2400 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2401 struct s2255_pipeinfo
*pipe_info
= &dev
->pipes
[j
];
2403 if (pipe_info
->state
== 0)
2405 pipe_info
->state
= 0;
2406 pipe_info
->prev_state
= 1;
2411 for (j
= 0; j
< MAX_PIPE_BUFFERS
; j
++) {
2412 struct s2255_pipeinfo
*pipe_info
= &dev
->pipes
[j
];
2413 if (pipe_info
->stream_urb
) {
2415 usb_kill_urb(pipe_info
->stream_urb
);
2416 usb_free_urb(pipe_info
->stream_urb
);
2417 pipe_info
->stream_urb
= NULL
;
2420 dprintk(2, "s2255 stop read pipe: %d\n", j
);
2424 static void s2255_fwload_start(struct s2255_dev
*dev
, int reset
)
2427 s2255_reset_dsppower(dev
);
2428 dev
->fw_data
->fw_size
= dev
->fw_data
->fw
->size
;
2429 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_NOTLOADED
);
2430 memcpy(dev
->fw_data
->pfw_data
,
2431 dev
->fw_data
->fw
->data
, CHUNK_SIZE
);
2432 dev
->fw_data
->fw_loaded
= CHUNK_SIZE
;
2433 usb_fill_bulk_urb(dev
->fw_data
->fw_urb
, dev
->udev
,
2434 usb_sndbulkpipe(dev
->udev
, 2),
2435 dev
->fw_data
->pfw_data
,
2436 CHUNK_SIZE
, s2255_fwchunk_complete
,
2438 mod_timer(&dev
->timer
, jiffies
+ HZ
);
2441 /* standard usb probe function */
2442 static int s2255_probe(struct usb_interface
*interface
,
2443 const struct usb_device_id
*id
)
2445 struct s2255_dev
*dev
= NULL
;
2446 struct usb_host_interface
*iface_desc
;
2447 struct usb_endpoint_descriptor
*endpoint
;
2449 int retval
= -ENOMEM
;
2453 dprintk(2, "s2255: probe\n");
2455 /* allocate memory for our device state and initialize it to zero */
2456 dev
= kzalloc(sizeof(struct s2255_dev
), GFP_KERNEL
);
2458 s2255_dev_err(&interface
->dev
, "out of memory\n");
2462 dev
->fw_data
= kzalloc(sizeof(struct s2255_fw
), GFP_KERNEL
);
2466 mutex_init(&dev
->lock
);
2467 mutex_init(&dev
->open_lock
);
2469 /* grab usb_device and save it */
2470 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2471 if (dev
->udev
== NULL
) {
2472 dev_err(&interface
->dev
, "null usb device\n");
2476 kref_init(&dev
->kref
);
2477 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev
, &dev
->kref
,
2478 dev
->udev
, interface
);
2479 dev
->interface
= interface
;
2480 /* set up the endpoint information */
2481 iface_desc
= interface
->cur_altsetting
;
2482 dprintk(1, "num endpoints %d\n", iface_desc
->desc
.bNumEndpoints
);
2483 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2484 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2485 if (!dev
->read_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2486 /* we found the bulk in endpoint */
2487 dev
->read_endpoint
= endpoint
->bEndpointAddress
;
2491 if (!dev
->read_endpoint
) {
2492 dev_err(&interface
->dev
, "Could not find bulk-in endpoint\n");
2497 usb_set_intfdata(interface
, dev
);
2499 dprintk(100, "after intfdata %p\n", dev
);
2501 init_timer(&dev
->timer
);
2502 dev
->timer
.function
= s2255_timer
;
2503 dev
->timer
.data
= (unsigned long)dev
->fw_data
;
2505 init_waitqueue_head(&dev
->fw_data
->wait_fw
);
2506 for (i
= 0; i
< MAX_CHANNELS
; i
++)
2507 init_waitqueue_head(&dev
->wait_setmode
[i
]);
2510 dev
->fw_data
->fw_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2512 if (!dev
->fw_data
->fw_urb
) {
2513 dev_err(&interface
->dev
, "out of memory!\n");
2516 dev
->fw_data
->pfw_data
= kzalloc(CHUNK_SIZE
, GFP_KERNEL
);
2517 if (!dev
->fw_data
->pfw_data
) {
2518 dev_err(&interface
->dev
, "out of memory!\n");
2521 /* load the first chunk */
2522 if (request_firmware(&dev
->fw_data
->fw
,
2523 FIRMWARE_FILE_NAME
, &dev
->udev
->dev
)) {
2524 printk(KERN_ERR
"sensoray 2255 failed to get firmware\n");
2527 /* check the firmware is valid */
2528 fw_size
= dev
->fw_data
->fw
->size
;
2529 pdata
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 8];
2531 if (*pdata
!= S2255_FW_MARKER
) {
2532 printk(KERN_INFO
"Firmware invalid.\n");
2536 /* make sure firmware is the latest */
2538 pRel
= (__le32
*) &dev
->fw_data
->fw
->data
[fw_size
- 4];
2539 printk(KERN_INFO
"s2255 dsp fw version %x\n", *pRel
);
2541 /* loads v4l specific */
2542 s2255_probe_v4l(dev
);
2543 usb_reset_device(dev
->udev
);
2544 /* load 2255 board specific */
2545 s2255_board_init(dev
);
2547 dprintk(4, "before probe done %p\n", dev
);
2548 spin_lock_init(&dev
->slock
);
2550 s2255_fwload_start(dev
, 0);
2551 dev_info(&interface
->dev
, "Sensoray 2255 detected\n");
2557 /* disconnect routine. when board is removed physically or with rmmod */
2558 static void s2255_disconnect(struct usb_interface
*interface
)
2560 struct s2255_dev
*dev
= NULL
;
2562 dprintk(1, "s2255: disconnect interface %p\n", interface
);
2563 dev
= usb_get_intfdata(interface
);
2566 * wake up any of the timers to allow open_lock to be
2569 atomic_set(&dev
->fw_data
->fw_state
, S2255_FW_DISCONNECTING
);
2570 wake_up(&dev
->fw_data
->wait_fw
);
2571 for (i
= 0; i
< MAX_CHANNELS
; i
++) {
2572 dev
->setmode_ready
[i
] = 1;
2573 wake_up(&dev
->wait_setmode
[i
]);
2576 mutex_lock(&dev
->open_lock
);
2577 usb_set_intfdata(interface
, NULL
);
2578 mutex_unlock(&dev
->open_lock
);
2581 kref_put(&dev
->kref
, s2255_destroy
);
2582 dprintk(1, "s2255drv: disconnect\n");
2583 dev_info(&interface
->dev
, "s2255usb now disconnected\n");
2587 static struct usb_driver s2255_driver
= {
2588 .name
= S2255_DRIVER_NAME
,
2589 .probe
= s2255_probe
,
2590 .disconnect
= s2255_disconnect
,
2591 .id_table
= s2255_table
,
2594 static int __init
usb_s2255_init(void)
2598 /* register this driver with the USB subsystem */
2599 result
= usb_register(&s2255_driver
);
2602 pr_err(KBUILD_MODNAME
2603 ": usb_register failed. Error number %d\n", result
);
2605 dprintk(2, "s2255_init: done\n");
2609 static void __exit
usb_s2255_exit(void)
2611 usb_deregister(&s2255_driver
);
2614 module_init(usb_s2255_init
);
2615 module_exit(usb_s2255_exit
);
2617 MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2618 MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2619 MODULE_LICENSE("GPL");