blob: 0092ff52bb2772d203188c7c2a8aa5559187bdf4 [file] [log] [blame]
Dean Anderson38f993a2008-06-26 23:15:51 -03001/*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
3 *
Dean Anderson4de39f52010-03-03 19:39:19 -03004 * Copyright (C) 2007-2010 by Sensoray Company Inc.
Dean Anderson38f993a2008-06-26 23:15:51 -03005 * Dean Anderson
6 *
7 * Some video buffer code based on vivi driver:
8 *
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
12 *
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
15 *
16 * Example maximum bandwidth utilization:
17 *
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
19 *
20 * -full or half size Grey scale: all 4 channels at once
21 *
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
23 *
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
25 * at once.
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
28 *
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.
33 *
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.
38 *
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.
42 */
43
44#include <linux/module.h>
45#include <linux/firmware.h>
46#include <linux/kernel.h>
47#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030049#include <linux/videodev2.h>
50#include <linux/version.h>
Hans Verkuilfeb75f02008-07-27 06:30:21 -030051#include <linux/mm.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040052#include <linux/smp_lock.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030053#include <media/videobuf-vmalloc.h>
54#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030055#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030056#include <linux/vmalloc.h>
57#include <linux/usb.h>
58
59#define FIRMWARE_FILE_NAME "f2255usb.bin"
60
61
62
Dean Anderson22b88d42008-08-29 15:33:19 -030063/* default JPEG quality */
64#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030065/* vendor request in */
66#define S2255_VR_IN 0
67/* vendor request out */
68#define S2255_VR_OUT 1
69/* firmware query */
70#define S2255_VR_FW 0x30
71/* USB endpoint number for configuring the device */
72#define S2255_CONFIG_EP 2
73/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030074#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030075/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030076#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030077#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030078#define S2255_SETMODE_TIMEOUT 500
Dean Anderson4de39f52010-03-03 19:39:19 -030079#define S2255_VIDSTATUS_TIMEOUT 350
Dean Anderson38f993a2008-06-26 23:15:51 -030080#define MAX_CHANNELS 4
Dean Anderson3fa00602010-03-04 20:47:33 -030081#define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
82#define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
83#define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
84#define S2255_RESPONSE_FW cpu_to_le32(0x10)
85#define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
Dean Anderson14d96262008-08-25 13:58:55 -030086#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030087#define MAX_CHANNELS 4
88#define MAX_PIPE_BUFFERS 1
89#define SYS_FRAMES 4
90/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030091#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
92#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030093#define LINE_SZ_4CIFS_NTSC 640
94#define LINE_SZ_2CIFS_NTSC 640
95#define LINE_SZ_1CIFS_NTSC 320
96#define LINE_SZ_4CIFS_PAL 704
97#define LINE_SZ_2CIFS_PAL 704
98#define LINE_SZ_1CIFS_PAL 352
99#define NUM_LINES_4CIFS_NTSC 240
100#define NUM_LINES_2CIFS_NTSC 240
101#define NUM_LINES_1CIFS_NTSC 240
102#define NUM_LINES_4CIFS_PAL 288
103#define NUM_LINES_2CIFS_PAL 288
104#define NUM_LINES_1CIFS_PAL 288
105#define LINE_SZ_DEF 640
106#define NUM_LINES_DEF 240
107
108
109/* predefined settings */
110#define FORMAT_NTSC 1
111#define FORMAT_PAL 2
112
113#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
114#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
115#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
Dean Anderson7d853532009-05-15 14:32:04 -0300116/* SCALE_4CIFSI is the 2 fields interpolated into one */
117#define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
Dean Anderson38f993a2008-06-26 23:15:51 -0300118
119#define COLOR_YUVPL 1 /* YUV planar */
120#define COLOR_YUVPK 2 /* YUV packed */
121#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300122#define COLOR_JPG 5 /* JPEG */
Dean Anderson38f993a2008-06-26 23:15:51 -0300123
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300124#define MASK_COLOR 0x000000ff
125#define MASK_JPG_QUALITY 0x0000ff00
126#define MASK_INPUT_TYPE 0x000f0000
Dean Anderson38f993a2008-06-26 23:15:51 -0300127/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
128#define FDEC_1 1 /* capture every frame. default */
129#define FDEC_2 2 /* capture every 2nd frame */
130#define FDEC_3 3 /* capture every 3rd frame */
131#define FDEC_5 5 /* capture every 5th frame */
132
133/*-------------------------------------------------------
134 * Default mode parameters.
135 *-------------------------------------------------------*/
136#define DEF_SCALE SCALE_4CIFS
137#define DEF_COLOR COLOR_YUVPL
138#define DEF_FDEC FDEC_1
139#define DEF_BRIGHT 0
140#define DEF_CONTRAST 0x5c
141#define DEF_SATURATION 0x80
142#define DEF_HUE 0
143
144/* usb config commands */
Dean Anderson3fa00602010-03-04 20:47:33 -0300145#define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
146#define CMD_2255 cpu_to_le32(0xc2255000)
147#define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
148#define CMD_START cpu_to_le32((CMD_2255 | 0x20))
149#define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
150#define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
Dean Anderson38f993a2008-06-26 23:15:51 -0300151
152struct s2255_mode {
153 u32 format; /* input video format (NTSC, PAL) */
154 u32 scale; /* output video scale */
155 u32 color; /* output video color format */
156 u32 fdec; /* frame decimation */
157 u32 bright; /* brightness */
158 u32 contrast; /* contrast */
159 u32 saturation; /* saturation */
160 u32 hue; /* hue (NTSC only)*/
161 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
162 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
163 u32 restart; /* if DSP requires restart */
164};
165
Dean Anderson14d96262008-08-25 13:58:55 -0300166
167#define S2255_READ_IDLE 0
168#define S2255_READ_FRAME 1
169
Dean Anderson38f993a2008-06-26 23:15:51 -0300170/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300171struct s2255_framei {
172 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300173 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300174 void *lpvbits; /* image data */
175 unsigned long cur_size; /* current data copied to it */
176};
177
178/* image buffer structure */
179struct s2255_bufferi {
180 unsigned long dwFrames; /* number of frames in buffer */
181 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
182};
183
184#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
185 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300186 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300187
188struct s2255_dmaqueue {
189 struct list_head active;
Dean Anderson38f993a2008-06-26 23:15:51 -0300190 struct s2255_dev *dev;
191 int channel;
192};
193
194/* for firmware loading, fw_state */
195#define S2255_FW_NOTLOADED 0
196#define S2255_FW_LOADED_DSPWAIT 1
197#define S2255_FW_SUCCESS 2
198#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300199#define S2255_FW_DISCONNECTING 4
Harvey Harrisondeaf53e2008-11-15 01:10:14 -0300200#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
Dean Anderson14d96262008-08-25 13:58:55 -0300201/* 2255 read states */
202#define S2255_READ_IDLE 0
203#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300204struct s2255_fw {
205 int fw_loaded;
206 int fw_size;
207 struct urb *fw_urb;
208 atomic_t fw_state;
209 void *pfw_data;
210 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300211 const struct firmware *fw;
212};
213
214struct s2255_pipeinfo {
215 u32 max_transfer_size;
216 u32 cur_transfer_size;
217 u8 *transfer_buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -0300218 u32 state;
Dean Anderson38f993a2008-06-26 23:15:51 -0300219 void *stream_urb;
220 void *dev; /* back pointer to s2255_dev struct*/
221 u32 err_count;
Dean Anderson38f993a2008-06-26 23:15:51 -0300222 u32 idx;
Dean Anderson38f993a2008-06-26 23:15:51 -0300223};
224
225struct s2255_fmt; /*forward declaration */
226
227struct s2255_dev {
228 int frames;
229 int users[MAX_CHANNELS];
230 struct mutex lock;
231 struct mutex open_lock;
232 int resources[MAX_CHANNELS];
233 struct usb_device *udev;
234 struct usb_interface *interface;
235 u8 read_endpoint;
236
237 struct s2255_dmaqueue vidq[MAX_CHANNELS];
238 struct video_device *vdev[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300239 struct timer_list timer;
240 struct s2255_fw *fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -0300241 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
242 struct s2255_bufferi buffer[MAX_CHANNELS];
243 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300244 /* jpeg compression */
245 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300246 /* capture parameters (for high quality mode full size) */
247 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300248 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
249 int cur_frame[MAX_CHANNELS];
250 int last_frame[MAX_CHANNELS];
251 u32 cc; /* current channel */
252 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300253 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300254 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300255 /* received packet size */
256 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300257 int bad_payload[MAX_CHANNELS];
258 unsigned long frame_count[MAX_CHANNELS];
259 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300260 /* if JPEG image */
261 int jpg_size[MAX_CHANNELS];
262 /* if channel configured to default state */
263 int chn_configured[MAX_CHANNELS];
264 wait_queue_head_t wait_setmode[MAX_CHANNELS];
265 int setmode_ready[MAX_CHANNELS];
Dean Anderson4de39f52010-03-03 19:39:19 -0300266 /* video status items */
267 int vidstatus[MAX_CHANNELS];
268 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
269 int vidstatus_ready[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300270 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300271 spinlock_t slock;
Dean Anderson4de39f52010-03-03 19:39:19 -0300272 /* dsp firmware version (f2255usb.bin) */
273 int dsp_fw_ver;
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300274 u16 pid; /* product id */
275 struct kref kref;
Dean Anderson38f993a2008-06-26 23:15:51 -0300276};
277#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
278
279struct s2255_fmt {
280 char *name;
281 u32 fourcc;
282 int depth;
283};
284
285/* buffer for one video frame */
286struct s2255_buffer {
287 /* common v4l buffer stuff -- must be first */
288 struct videobuf_buffer vb;
289 const struct s2255_fmt *fmt;
290};
291
292struct s2255_fh {
293 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300294 const struct s2255_fmt *fmt;
295 unsigned int width;
296 unsigned int height;
297 struct videobuf_queue vb_vidq;
298 enum v4l2_buf_type type;
299 int channel;
300 /* mode below is the desired mode.
301 mode in s2255_dev is the current mode that was last set */
302 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300303 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300304};
305
Dean Andersonabce21f2009-04-23 16:04:41 -0300306/* current cypress EEPROM firmware version */
307#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson4de39f52010-03-03 19:39:19 -0300308/* current DSP FW version */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300309#define S2255_CUR_DSP_FWVER 8
Dean Anderson4de39f52010-03-03 19:39:19 -0300310/* Need DSP version 5+ for video status feature */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300311#define S2255_MIN_DSP_STATUS 5
312#define S2255_MIN_DSP_COLORFILTER 8
Dean Anderson38f993a2008-06-26 23:15:51 -0300313#define S2255_MAJOR_VERSION 1
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300314#define S2255_MINOR_VERSION 18
Dean Anderson38f993a2008-06-26 23:15:51 -0300315#define S2255_RELEASE 0
316#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
317 S2255_MINOR_VERSION, \
318 S2255_RELEASE)
319
Dean Anderson38f993a2008-06-26 23:15:51 -0300320#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300321
322/* private V4L2 controls */
323
324/*
325 * The following chart displays how COLORFILTER should be set
326 * =========================================================
327 * = fourcc = COLORFILTER =
328 * = ===============================
329 * = = 0 = 1 =
330 * =========================================================
331 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
332 * = = s-video or = composite =
333 * = = B/W camera = input =
334 * =========================================================
335 * = other = color, svideo = color, =
336 * = = = composite =
337 * =========================================================
338 *
339 * Notes:
340 * channels 0-3 on 2255 are composite
341 * channels 0-1 on 2257 are composite, 2-3 are s-video
342 * If COLORFILTER is 0 with a composite color camera connected,
343 * the output will appear monochrome but hatching
344 * will occur.
345 * COLORFILTER is different from "color killer" and "color effects"
346 * for reasons above.
347 */
348#define S2255_V4L2_YC_ON 1
349#define S2255_V4L2_YC_OFF 0
350#define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
351
Dean Anderson38f993a2008-06-26 23:15:51 -0300352/* frame prefix size (sent once every frame) */
353#define PREFIX_SIZE 512
354
355/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300356static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300357
Dean Anderson38f993a2008-06-26 23:15:51 -0300358static int debug;
359static int *s2255_debug = &debug;
360
361static int s2255_start_readpipe(struct s2255_dev *dev);
362static void s2255_stop_readpipe(struct s2255_dev *dev);
363static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
364static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
365static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300366 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300367static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
368 struct s2255_mode *mode);
369static int s2255_board_shutdown(struct s2255_dev *dev);
370static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300371static void s2255_fwload_start(struct s2255_dev *dev, int reset);
372static void s2255_destroy(struct kref *kref);
373static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
374 u16 index, u16 value, void *buf,
375 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300376
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300377/* dev_err macro with driver name */
378#define S2255_DRIVER_NAME "s2255"
379#define s2255_dev_err(dev, fmt, arg...) \
380 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
381
Dean Anderson38f993a2008-06-26 23:15:51 -0300382#define dprintk(level, fmt, arg...) \
383 do { \
384 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300385 printk(KERN_DEBUG S2255_DRIVER_NAME \
386 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300387 } \
388 } while (0)
389
Dean Anderson38f993a2008-06-26 23:15:51 -0300390static struct usb_driver s2255_driver;
391
Dean Anderson38f993a2008-06-26 23:15:51 -0300392/* Declare static vars that will be used as parameters */
393static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
394
395/* start video number */
396static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
397
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300398module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300399MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300400module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300401MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300402module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300403MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
404
405/* USB device table */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300406#define USB_SENSORAY_VID 0x1943
Dean Anderson38f993a2008-06-26 23:15:51 -0300407static struct usb_device_id s2255_table[] = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300408 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
409 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300410 { } /* Terminating entry */
411};
412MODULE_DEVICE_TABLE(usb, s2255_table);
413
Dean Anderson38f993a2008-06-26 23:15:51 -0300414#define BUFFER_TIMEOUT msecs_to_jiffies(400)
415
Dean Anderson38f993a2008-06-26 23:15:51 -0300416/* image formats. */
417static const struct s2255_fmt formats[] = {
418 {
419 .name = "4:2:2, planar, YUV422P",
420 .fourcc = V4L2_PIX_FMT_YUV422P,
421 .depth = 16
422
423 }, {
424 .name = "4:2:2, packed, YUYV",
425 .fourcc = V4L2_PIX_FMT_YUYV,
426 .depth = 16
427
428 }, {
429 .name = "4:2:2, packed, UYVY",
430 .fourcc = V4L2_PIX_FMT_UYVY,
431 .depth = 16
432 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300433 .name = "JPG",
434 .fourcc = V4L2_PIX_FMT_JPEG,
435 .depth = 24
436 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300437 .name = "8bpp GREY",
438 .fourcc = V4L2_PIX_FMT_GREY,
439 .depth = 8
440 }
441};
442
443static int norm_maxw(struct video_device *vdev)
444{
445 return (vdev->current_norm & V4L2_STD_NTSC) ?
446 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
447}
448
449static int norm_maxh(struct video_device *vdev)
450{
451 return (vdev->current_norm & V4L2_STD_NTSC) ?
452 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
453}
454
455static int norm_minw(struct video_device *vdev)
456{
457 return (vdev->current_norm & V4L2_STD_NTSC) ?
458 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
459}
460
461static int norm_minh(struct video_device *vdev)
462{
463 return (vdev->current_norm & V4L2_STD_NTSC) ?
464 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
465}
466
467
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300468/*
469 * TODO: fixme: move YUV reordering to hardware
470 * converts 2255 planar format to yuyv or uyvy
471 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300472static void planar422p_to_yuv_packed(const unsigned char *in,
473 unsigned char *out,
474 int width, int height,
475 int fmt)
476{
477 unsigned char *pY;
478 unsigned char *pCb;
479 unsigned char *pCr;
480 unsigned long size = height * width;
481 unsigned int i;
482 pY = (unsigned char *)in;
483 pCr = (unsigned char *)in + height * width;
484 pCb = (unsigned char *)in + height * width + (height * width / 2);
485 for (i = 0; i < size * 2; i += 4) {
486 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
487 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
488 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
489 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
490 }
491 return;
492}
493
Hans Verkuild45b9b82008-09-04 03:33:43 -0300494static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300495{
496 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
497 msleep(10);
498 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
499 return;
500}
Dean Anderson38f993a2008-06-26 23:15:51 -0300501
502/* kickstarts the firmware loading. from probe
503 */
504static void s2255_timer(unsigned long user_data)
505{
506 struct s2255_fw *data = (struct s2255_fw *)user_data;
507 dprintk(100, "s2255 timer\n");
508 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
509 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300510 atomic_set(&data->fw_state, S2255_FW_FAILED);
511 /* wake up anything waiting for the firmware */
512 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300513 return;
514 }
515}
516
Dean Anderson38f993a2008-06-26 23:15:51 -0300517
518/* this loads the firmware asynchronously.
519 Originally this was done synchroously in probe.
520 But it is better to load it asynchronously here than block
521 inside the probe function. Blocking inside probe affects boot time.
522 FW loading is triggered by the timer in the probe function
523*/
524static void s2255_fwchunk_complete(struct urb *urb)
525{
526 struct s2255_fw *data = urb->context;
527 struct usb_device *udev = urb->dev;
528 int len;
529 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300530 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300531 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300532 atomic_set(&data->fw_state, S2255_FW_FAILED);
533 /* wake up anything waiting for the firmware */
534 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300535 return;
536 }
537 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300538 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300539 atomic_set(&data->fw_state, S2255_FW_FAILED);
540 /* wake up anything waiting for the firmware */
541 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300542 return;
543 }
544#define CHUNK_SIZE 512
545 /* all USB transfers must be done with continuous kernel memory.
546 can't allocate more than 128k in current linux kernel, so
547 upload the firmware in chunks
548 */
549 if (data->fw_loaded < data->fw_size) {
550 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
551 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
552
553 if (len < CHUNK_SIZE)
554 memset(data->pfw_data, 0, CHUNK_SIZE);
555
556 dprintk(100, "completed len %d, loaded %d \n", len,
557 data->fw_loaded);
558
559 memcpy(data->pfw_data,
560 (char *) data->fw->data + data->fw_loaded, len);
561
562 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
563 data->pfw_data, CHUNK_SIZE,
564 s2255_fwchunk_complete, data);
565 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
566 dev_err(&udev->dev, "failed submit URB\n");
567 atomic_set(&data->fw_state, S2255_FW_FAILED);
568 /* wake up anything waiting for the firmware */
569 wake_up(&data->wait_fw);
570 return;
571 }
572 data->fw_loaded += len;
573 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300574 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300575 }
576 dprintk(100, "2255 complete done\n");
577 return;
578
579}
580
Dean Anderson14d96262008-08-25 13:58:55 -0300581static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300582{
583 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
584 struct s2255_buffer *buf;
585 unsigned long flags = 0;
586 int rc = 0;
587 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
588 spin_lock_irqsave(&dev->slock, flags);
589
590 if (list_empty(&dma_q->active)) {
591 dprintk(1, "No active queue to serve\n");
592 rc = -1;
593 goto unlock;
594 }
595 buf = list_entry(dma_q->active.next,
596 struct s2255_buffer, vb.queue);
597
Dean Anderson38f993a2008-06-26 23:15:51 -0300598 list_del(&buf->vb.queue);
599 do_gettimeofday(&buf->vb.ts);
600 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300601 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300602 wake_up(&buf->vb.done);
603 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
604unlock:
605 spin_unlock_irqrestore(&dev->slock, flags);
606 return 0;
607}
608
609
610static const struct s2255_fmt *format_by_fourcc(int fourcc)
611{
612 unsigned int i;
613
614 for (i = 0; i < ARRAY_SIZE(formats); i++) {
615 if (-1 == formats[i].fourcc)
616 continue;
617 if (formats[i].fourcc == fourcc)
618 return formats + i;
619 }
620 return NULL;
621}
622
623
624
625
626/* video buffer vmalloc implementation based partly on VIVI driver which is
627 * Copyright (c) 2006 by
628 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
629 * Ted Walther <ted--a.t--enumera.com>
630 * John Sokol <sokol--a.t--videotechnology.com>
631 * http://v4l.videotechnology.com/
632 *
633 */
634static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300635 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300636{
637 int pos = 0;
638 struct timeval ts;
639 const char *tmpbuf;
640 char *vbuf = videobuf_to_vmalloc(&buf->vb);
641 unsigned long last_frame;
642 struct s2255_framei *frm;
643
644 if (!vbuf)
645 return;
646
647 last_frame = dev->last_frame[chn];
648 if (last_frame != -1) {
649 frm = &dev->buffer[chn].frame[last_frame];
650 tmpbuf =
651 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
652 switch (buf->fmt->fourcc) {
653 case V4L2_PIX_FMT_YUYV:
654 case V4L2_PIX_FMT_UYVY:
655 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
656 vbuf, buf->vb.width,
657 buf->vb.height,
658 buf->fmt->fourcc);
659 break;
660 case V4L2_PIX_FMT_GREY:
661 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
662 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300663 case V4L2_PIX_FMT_JPEG:
664 buf->vb.size = jpgsize;
665 memcpy(vbuf, tmpbuf, buf->vb.size);
666 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300667 case V4L2_PIX_FMT_YUV422P:
668 memcpy(vbuf, tmpbuf,
669 buf->vb.width * buf->vb.height * 2);
670 break;
671 default:
672 printk(KERN_DEBUG "s2255: unknown format?\n");
673 }
674 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300675 } else {
676 printk(KERN_ERR "s2255: =======no frame\n");
677 return;
678
679 }
680 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
681 (unsigned long)vbuf, pos);
682 /* tell v4l buffer was filled */
683
Dean Andersona1c45302008-09-09 12:29:56 -0300684 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300685 do_gettimeofday(&ts);
686 buf->vb.ts = ts;
687 buf->vb.state = VIDEOBUF_DONE;
688}
689
690
691/* ------------------------------------------------------------------
692 Videobuf operations
693 ------------------------------------------------------------------*/
694
695static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
696 unsigned int *size)
697{
698 struct s2255_fh *fh = vq->priv_data;
699
700 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
701
702 if (0 == *count)
703 *count = S2255_DEF_BUFS;
704
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300705 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300706 (*count)--;
707
708 return 0;
709}
710
711static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
712{
713 dprintk(4, "%s\n", __func__);
714
Dean Anderson38f993a2008-06-26 23:15:51 -0300715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
717}
718
719static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
720 enum v4l2_field field)
721{
722 struct s2255_fh *fh = vq->priv_data;
723 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
724 int rc;
725 dprintk(4, "%s, field=%d\n", __func__, field);
726 if (fh->fmt == NULL)
727 return -EINVAL;
728
729 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
730 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
731 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
732 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
733 dprintk(4, "invalid buffer prepare\n");
734 return -EINVAL;
735 }
736
737 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
738
739 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
740 dprintk(4, "invalid buffer prepare\n");
741 return -EINVAL;
742 }
743
744 buf->fmt = fh->fmt;
745 buf->vb.width = fh->width;
746 buf->vb.height = fh->height;
747 buf->vb.field = field;
748
749
750 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
751 rc = videobuf_iolock(vq, &buf->vb, NULL);
752 if (rc < 0)
753 goto fail;
754 }
755
756 buf->vb.state = VIDEOBUF_PREPARED;
757 return 0;
758fail:
759 free_buffer(vq, buf);
760 return rc;
761}
762
763static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
764{
765 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
766 struct s2255_fh *fh = vq->priv_data;
767 struct s2255_dev *dev = fh->dev;
768 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
769
770 dprintk(1, "%s\n", __func__);
771
772 buf->vb.state = VIDEOBUF_QUEUED;
773 list_add_tail(&buf->vb.queue, &vidq->active);
774}
775
776static void buffer_release(struct videobuf_queue *vq,
777 struct videobuf_buffer *vb)
778{
779 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
780 struct s2255_fh *fh = vq->priv_data;
781 dprintk(4, "%s %d\n", __func__, fh->channel);
782 free_buffer(vq, buf);
783}
784
785static struct videobuf_queue_ops s2255_video_qops = {
786 .buf_setup = buffer_setup,
787 .buf_prepare = buffer_prepare,
788 .buf_queue = buffer_queue,
789 .buf_release = buffer_release,
790};
791
792
793static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
794{
795 /* is it free? */
796 mutex_lock(&dev->lock);
797 if (dev->resources[fh->channel]) {
798 /* no, someone else uses it */
799 mutex_unlock(&dev->lock);
800 return 0;
801 }
802 /* it's free, grab it */
803 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300804 fh->resources[fh->channel] = 1;
805 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300806 mutex_unlock(&dev->lock);
807 return 1;
808}
809
810static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
811{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300812 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300813}
814
Dean Andersonf78d92c2008-07-22 14:43:27 -0300815static int res_check(struct s2255_fh *fh)
816{
817 return fh->resources[fh->channel];
818}
819
820
Dean Anderson38f993a2008-06-26 23:15:51 -0300821static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
822{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300823 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300824 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300825 fh->resources[fh->channel] = 0;
826 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300827 dprintk(1, "res: put\n");
828}
829
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300830static int vidioc_querymenu(struct file *file, void *priv,
831 struct v4l2_querymenu *qmenu)
832{
833 static const char *colorfilter[] = {
834 "Off",
835 "On",
836 NULL
837 };
838 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
839 int i;
840 const char **menu_items = colorfilter;
841 for (i = 0; i < qmenu->index && menu_items[i]; i++)
842 ; /* do nothing (from v4l2-common.c) */
843 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
844 return -EINVAL;
845 strlcpy(qmenu->name, menu_items[qmenu->index],
846 sizeof(qmenu->name));
847 return 0;
848 }
849 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
850}
851
Dean Anderson38f993a2008-06-26 23:15:51 -0300852
853static int vidioc_querycap(struct file *file, void *priv,
854 struct v4l2_capability *cap)
855{
856 struct s2255_fh *fh = file->private_data;
857 struct s2255_dev *dev = fh->dev;
858 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
859 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300860 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300861 cap->version = S2255_VERSION;
862 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
863 return 0;
864}
865
866static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
867 struct v4l2_fmtdesc *f)
868{
869 int index = 0;
870 if (f)
871 index = f->index;
872
873 if (index >= ARRAY_SIZE(formats))
874 return -EINVAL;
875
876 dprintk(4, "name %s\n", formats[index].name);
877 strlcpy(f->description, formats[index].name, sizeof(f->description));
878 f->pixelformat = formats[index].fourcc;
879 return 0;
880}
881
882static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
883 struct v4l2_format *f)
884{
885 struct s2255_fh *fh = priv;
886
887 f->fmt.pix.width = fh->width;
888 f->fmt.pix.height = fh->height;
889 f->fmt.pix.field = fh->vb_vidq.field;
890 f->fmt.pix.pixelformat = fh->fmt->fourcc;
891 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
892 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300893 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300894}
895
896static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
897 struct v4l2_format *f)
898{
899 const struct s2255_fmt *fmt;
900 enum v4l2_field field;
901 int b_any_field = 0;
902 struct s2255_fh *fh = priv;
903 struct s2255_dev *dev = fh->dev;
904 int is_ntsc;
905
906 is_ntsc =
907 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
908
909 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
910
911 if (fmt == NULL)
912 return -EINVAL;
913
914 field = f->fmt.pix.field;
915 if (field == V4L2_FIELD_ANY)
916 b_any_field = 1;
917
918 dprintk(4, "try format %d \n", is_ntsc);
919 /* supports 3 sizes. see s2255drv.h */
920 dprintk(50, "width test %d, height %d\n",
921 f->fmt.pix.width, f->fmt.pix.height);
922 if (is_ntsc) {
923 /* NTSC */
924 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
925 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
926 if (b_any_field) {
927 field = V4L2_FIELD_SEQ_TB;
928 } else if (!((field == V4L2_FIELD_INTERLACED) ||
929 (field == V4L2_FIELD_SEQ_TB) ||
930 (field == V4L2_FIELD_INTERLACED_TB))) {
931 dprintk(1, "unsupported field setting\n");
932 return -EINVAL;
933 }
934 } else {
935 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
936 if (b_any_field) {
937 field = V4L2_FIELD_TOP;
938 } else if (!((field == V4L2_FIELD_TOP) ||
939 (field == V4L2_FIELD_BOTTOM))) {
940 dprintk(1, "unsupported field setting\n");
941 return -EINVAL;
942 }
943
944 }
945 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
946 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
947 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
948 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
949 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
950 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
951 else
952 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
953 } else {
954 /* PAL */
955 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
956 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
957 if (b_any_field) {
958 field = V4L2_FIELD_SEQ_TB;
959 } else if (!((field == V4L2_FIELD_INTERLACED) ||
960 (field == V4L2_FIELD_SEQ_TB) ||
961 (field == V4L2_FIELD_INTERLACED_TB))) {
962 dprintk(1, "unsupported field setting\n");
963 return -EINVAL;
964 }
965 } else {
966 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
967 if (b_any_field) {
968 field = V4L2_FIELD_TOP;
969 } else if (!((field == V4L2_FIELD_TOP) ||
970 (field == V4L2_FIELD_BOTTOM))) {
971 dprintk(1, "unsupported field setting\n");
972 return -EINVAL;
973 }
974 }
975 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
976 dprintk(50, "pal 704\n");
977 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
978 field = V4L2_FIELD_SEQ_TB;
979 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
980 dprintk(50, "pal 352A\n");
981 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
982 field = V4L2_FIELD_TOP;
983 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
984 dprintk(50, "pal 352B\n");
985 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
986 field = V4L2_FIELD_TOP;
987 } else {
988 dprintk(50, "pal 352C\n");
989 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
990 field = V4L2_FIELD_TOP;
991 }
992 }
993
994 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
995 f->fmt.pix.height, f->fmt.pix.field);
996 f->fmt.pix.field = field;
997 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
998 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
999 return 0;
1000}
1001
1002static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1003 struct v4l2_format *f)
1004{
1005 struct s2255_fh *fh = priv;
1006 const struct s2255_fmt *fmt;
1007 struct videobuf_queue *q = &fh->vb_vidq;
1008 int ret;
1009 int norm;
1010
1011 ret = vidioc_try_fmt_vid_cap(file, fh, f);
1012
1013 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001014 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -03001015
1016 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1017
1018 if (fmt == NULL)
1019 return -EINVAL;
1020
1021 mutex_lock(&q->vb_lock);
1022
1023 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1024 dprintk(1, "queue busy\n");
1025 ret = -EBUSY;
1026 goto out_s_fmt;
1027 }
1028
1029 if (res_locked(fh->dev, fh)) {
1030 dprintk(1, "can't change format after started\n");
1031 ret = -EBUSY;
1032 goto out_s_fmt;
1033 }
1034
1035 fh->fmt = fmt;
1036 fh->width = f->fmt.pix.width;
1037 fh->height = f->fmt.pix.height;
1038 fh->vb_vidq.field = f->fmt.pix.field;
1039 fh->type = f->type;
1040 norm = norm_minw(fh->dev->vdev[fh->channel]);
1041 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001042 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1043 if (fh->dev->cap_parm[fh->channel].capturemode &
1044 V4L2_MODE_HIGHQUALITY) {
1045 fh->mode.scale = SCALE_4CIFSI;
1046 dprintk(2, "scale 4CIFSI\n");
1047 } else {
1048 fh->mode.scale = SCALE_4CIFS;
1049 dprintk(2, "scale 4CIFS\n");
1050 }
1051 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001052 fh->mode.scale = SCALE_2CIFS;
1053
1054 } else {
1055 fh->mode.scale = SCALE_1CIFS;
1056 }
1057
1058 /* color mode */
1059 switch (fh->fmt->fourcc) {
1060 case V4L2_PIX_FMT_GREY:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001061 fh->mode.color &= ~MASK_COLOR;
1062 fh->mode.color |= COLOR_Y8;
Dean Anderson38f993a2008-06-26 23:15:51 -03001063 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001064 case V4L2_PIX_FMT_JPEG:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001065 fh->mode.color &= ~MASK_COLOR;
1066 fh->mode.color |= COLOR_JPG;
1067 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001068 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001069 case V4L2_PIX_FMT_YUV422P:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001070 fh->mode.color &= ~MASK_COLOR;
1071 fh->mode.color |= COLOR_YUVPL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001072 break;
1073 case V4L2_PIX_FMT_YUYV:
1074 case V4L2_PIX_FMT_UYVY:
1075 default:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001076 fh->mode.color &= ~MASK_COLOR;
1077 fh->mode.color |= COLOR_YUVPK;
Dean Anderson38f993a2008-06-26 23:15:51 -03001078 break;
1079 }
1080 ret = 0;
1081out_s_fmt:
1082 mutex_unlock(&q->vb_lock);
1083 return ret;
1084}
1085
1086static int vidioc_reqbufs(struct file *file, void *priv,
1087 struct v4l2_requestbuffers *p)
1088{
1089 int rc;
1090 struct s2255_fh *fh = priv;
1091 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1092 return rc;
1093}
1094
1095static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1096{
1097 int rc;
1098 struct s2255_fh *fh = priv;
1099 rc = videobuf_querybuf(&fh->vb_vidq, p);
1100 return rc;
1101}
1102
1103static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1104{
1105 int rc;
1106 struct s2255_fh *fh = priv;
1107 rc = videobuf_qbuf(&fh->vb_vidq, p);
1108 return rc;
1109}
1110
1111static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1112{
1113 int rc;
1114 struct s2255_fh *fh = priv;
1115 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1116 return rc;
1117}
1118
1119#ifdef CONFIG_VIDEO_V4L1_COMPAT
1120static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1121{
1122 struct s2255_fh *fh = priv;
1123
1124 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1125}
1126#endif
1127
1128/* write to the configuration pipe, synchronously */
1129static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1130 int size)
1131{
1132 int pipe;
1133 int done;
1134 long retval = -1;
1135 if (udev) {
1136 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1137 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1138 }
1139 return retval;
1140}
1141
1142static u32 get_transfer_size(struct s2255_mode *mode)
1143{
1144 int linesPerFrame = LINE_SZ_DEF;
1145 int pixelsPerLine = NUM_LINES_DEF;
1146 u32 outImageSize;
1147 u32 usbInSize;
1148 unsigned int mask_mult;
1149
1150 if (mode == NULL)
1151 return 0;
1152
1153 if (mode->format == FORMAT_NTSC) {
1154 switch (mode->scale) {
1155 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001156 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001157 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1158 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1159 break;
1160 case SCALE_2CIFS:
1161 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1162 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1163 break;
1164 case SCALE_1CIFS:
1165 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1166 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1167 break;
1168 default:
1169 break;
1170 }
1171 } else if (mode->format == FORMAT_PAL) {
1172 switch (mode->scale) {
1173 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001174 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001175 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1176 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1177 break;
1178 case SCALE_2CIFS:
1179 linesPerFrame = NUM_LINES_2CIFS_PAL;
1180 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1181 break;
1182 case SCALE_1CIFS:
1183 linesPerFrame = NUM_LINES_1CIFS_PAL;
1184 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1185 break;
1186 default:
1187 break;
1188 }
1189 }
1190 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001191 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001192 /* 2 bytes/pixel if not monochrome */
1193 outImageSize *= 2;
1194 }
1195
1196 /* total bytes to send including prefix and 4K padding;
1197 must be a multiple of USB_READ_SIZE */
1198 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1199 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1200 /* if size not a multiple of USB_READ_SIZE */
1201 if (usbInSize & ~mask_mult)
1202 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1203 return usbInSize;
1204}
1205
1206static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1207{
1208 struct device *dev = &sdev->udev->dev;
1209 dev_info(dev, "------------------------------------------------\n");
1210 dev_info(dev, "verify mode\n");
1211 dev_info(dev, "format: %d\n", mode->format);
1212 dev_info(dev, "scale: %d\n", mode->scale);
1213 dev_info(dev, "fdec: %d\n", mode->fdec);
1214 dev_info(dev, "color: %d\n", mode->color);
1215 dev_info(dev, "bright: 0x%x\n", mode->bright);
1216 dev_info(dev, "restart: 0x%x\n", mode->restart);
1217 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1218 dev_info(dev, "single: 0x%x\n", mode->single);
1219 dev_info(dev, "------------------------------------------------\n");
1220}
1221
1222/*
1223 * set mode is the function which controls the DSP.
1224 * the restart parameter in struct s2255_mode should be set whenever
1225 * the image size could change via color format, video system or image
1226 * size.
1227 * When the restart parameter is set, we sleep for ONE frame to allow the
1228 * DSP time to get the new frame
1229 */
1230static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1231 struct s2255_mode *mode)
1232{
1233 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001234 __le32 *buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -03001235 unsigned long chn_rev;
Dean Anderson14d96262008-08-25 13:58:55 -03001236 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001237 chn_rev = G_chnmap[chn];
1238 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1239 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1240 dev->mode[chn].scale);
1241 dprintk(2, "mode contrast %x\n", mode->contrast);
1242
Dean Anderson22b88d42008-08-29 15:33:19 -03001243 /* if JPEG, set the quality */
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001244 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1245 mode->color &= ~MASK_COLOR;
1246 mode->color |= COLOR_JPG;
1247 mode->color &= ~MASK_JPG_QUALITY;
1248 mode->color |= (dev->jc[chn].quality << 8);
1249 }
Dean Anderson22b88d42008-08-29 15:33:19 -03001250
Dean Anderson38f993a2008-06-26 23:15:51 -03001251 /* save the mode */
1252 dev->mode[chn] = *mode;
1253 dev->req_image_size[chn] = get_transfer_size(mode);
1254 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1255
1256 buffer = kzalloc(512, GFP_KERNEL);
1257 if (buffer == NULL) {
1258 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001259 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001260 return -ENOMEM;
1261 }
1262
1263 /* set the mode */
1264 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001265 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001266 buffer[2] = CMD_SET_MODE;
1267 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001268 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001269 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1270 if (debug)
1271 dump_verify_mode(dev, mode);
1272 kfree(buffer);
1273 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1274
1275 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001276 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001277 wait_event_timeout(dev->wait_setmode[chn],
1278 (dev->setmode_ready[chn] != 0),
1279 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1280 if (dev->setmode_ready[chn] != 1) {
1281 printk(KERN_DEBUG "s2255: no set mode response\n");
1282 res = -EFAULT;
1283 }
1284 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001285
1286 /* clear the restart flag */
1287 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001288 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001289 return res;
1290}
1291
Dean Anderson4de39f52010-03-03 19:39:19 -03001292static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1293 u32 *pstatus)
1294{
1295 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001296 __le32 *buffer;
Dean Anderson4de39f52010-03-03 19:39:19 -03001297 u32 chn_rev;
1298 mutex_lock(&dev->lock);
1299 chn_rev = G_chnmap[chn];
1300 dprintk(4, "%s chan %d\n", __func__, chn_rev);
1301 buffer = kzalloc(512, GFP_KERNEL);
1302 if (buffer == NULL) {
1303 dev_err(&dev->udev->dev, "out of mem\n");
1304 mutex_unlock(&dev->lock);
1305 return -ENOMEM;
1306 }
1307 /* form the get vid status command */
1308 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001309 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson4de39f52010-03-03 19:39:19 -03001310 buffer[2] = CMD_STATUS;
1311 *pstatus = 0;
1312 dev->vidstatus_ready[chn] = 0;
1313 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1314 kfree(buffer);
1315 wait_event_timeout(dev->wait_vidstatus[chn],
1316 (dev->vidstatus_ready[chn] != 0),
1317 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1318 if (dev->vidstatus_ready[chn] != 1) {
1319 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1320 res = -EFAULT;
1321 }
1322 *pstatus = dev->vidstatus[chn];
1323 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1324 mutex_unlock(&dev->lock);
1325 return res;
1326}
1327
Dean Anderson38f993a2008-06-26 23:15:51 -03001328static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1329{
1330 int res;
1331 struct s2255_fh *fh = priv;
1332 struct s2255_dev *dev = fh->dev;
1333 struct s2255_mode *new_mode;
1334 struct s2255_mode *old_mode;
1335 int chn;
1336 int j;
1337 dprintk(4, "%s\n", __func__);
1338 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1339 dev_err(&dev->udev->dev, "invalid fh type0\n");
1340 return -EINVAL;
1341 }
1342 if (i != fh->type) {
1343 dev_err(&dev->udev->dev, "invalid fh type1\n");
1344 return -EINVAL;
1345 }
1346
1347 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001348 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001349 return -EBUSY;
1350 }
1351
1352 /* send a set mode command everytime with restart.
1353 in case we switch resolutions or other parameters */
1354 chn = fh->channel;
1355 new_mode = &fh->mode;
1356 old_mode = &fh->dev->mode[chn];
1357
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001358 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
Dean Anderson38f993a2008-06-26 23:15:51 -03001359 new_mode->restart = 1;
1360 else if (new_mode->scale != old_mode->scale)
1361 new_mode->restart = 1;
1362 else if (new_mode->format != old_mode->format)
1363 new_mode->restart = 1;
1364
1365 s2255_set_mode(dev, chn, new_mode);
1366 new_mode->restart = 0;
1367 *old_mode = *new_mode;
1368 dev->cur_fmt[chn] = fh->fmt;
1369 dprintk(1, "%s[%d]\n", __func__, chn);
1370 dev->last_frame[chn] = -1;
1371 dev->bad_payload[chn] = 0;
1372 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001373 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001374 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001375 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001376 dev->buffer[chn].frame[j].cur_size = 0;
1377 }
1378 res = videobuf_streamon(&fh->vb_vidq);
1379 if (res == 0) {
1380 s2255_start_acquire(dev, chn);
1381 dev->b_acquire[chn] = 1;
1382 } else {
1383 res_free(dev, fh);
1384 }
1385 return res;
1386}
1387
1388static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1389{
Dean Anderson38f993a2008-06-26 23:15:51 -03001390 struct s2255_fh *fh = priv;
1391 struct s2255_dev *dev = fh->dev;
1392
1393 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1394 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1395 printk(KERN_ERR "invalid fh type0\n");
1396 return -EINVAL;
1397 }
1398 if (i != fh->type) {
1399 printk(KERN_ERR "invalid type i\n");
1400 return -EINVAL;
1401 }
1402 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001403 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001404 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001405 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001406}
1407
1408static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1409{
1410 struct s2255_fh *fh = priv;
1411 struct s2255_mode *mode;
1412 struct videobuf_queue *q = &fh->vb_vidq;
1413 int ret = 0;
1414
1415 mutex_lock(&q->vb_lock);
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001416
Dean Anderson38f993a2008-06-26 23:15:51 -03001417 if (videobuf_queue_is_busy(q)) {
1418 dprintk(1, "queue busy\n");
1419 ret = -EBUSY;
1420 goto out_s_std;
1421 }
1422
1423 if (res_locked(fh->dev, fh)) {
1424 dprintk(1, "can't change standard after started\n");
1425 ret = -EBUSY;
1426 goto out_s_std;
1427 }
1428 mode = &fh->mode;
Dean Anderson38f993a2008-06-26 23:15:51 -03001429 if (*i & V4L2_STD_NTSC) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001430 dprintk(4, "%s NTSC\n", __func__);
1431 /* if changing format, reset frame decimation/intervals */
1432 if (mode->format != FORMAT_NTSC) {
1433 mode->format = FORMAT_NTSC;
1434 mode->fdec = FDEC_1;
1435 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001436 } else if (*i & V4L2_STD_PAL) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001437 dprintk(4, "%s PAL\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001438 mode->format = FORMAT_PAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001439 if (mode->format != FORMAT_PAL) {
1440 mode->format = FORMAT_PAL;
1441 mode->fdec = FDEC_1;
1442 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001443 } else {
1444 ret = -EINVAL;
1445 }
1446out_s_std:
1447 mutex_unlock(&q->vb_lock);
1448 return ret;
1449}
1450
1451/* Sensoray 2255 is a multiple channel capture device.
1452 It does not have a "crossbar" of inputs.
1453 We use one V4L device per channel. The user must
1454 be aware that certain combinations are not allowed.
1455 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1456 at once in color(you can do full fps on 4 channels with greyscale.
1457*/
1458static int vidioc_enum_input(struct file *file, void *priv,
1459 struct v4l2_input *inp)
1460{
Dean Anderson4de39f52010-03-03 19:39:19 -03001461 struct s2255_fh *fh = priv;
1462 struct s2255_dev *dev = fh->dev;
1463 u32 status = 0;
1464
Dean Anderson38f993a2008-06-26 23:15:51 -03001465 if (inp->index != 0)
1466 return -EINVAL;
1467
1468 inp->type = V4L2_INPUT_TYPE_CAMERA;
1469 inp->std = S2255_NORMS;
Dean Anderson4de39f52010-03-03 19:39:19 -03001470 inp->status = 0;
1471 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1472 int rc;
1473 rc = s2255_cmd_status(dev, fh->channel, &status);
1474 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1475 if (rc == 0)
1476 inp->status = (status & 0x01) ? 0
1477 : V4L2_IN_ST_NO_SIGNAL;
1478 }
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001479 switch (dev->pid) {
1480 case 0x2255:
1481 default:
1482 strlcpy(inp->name, "Composite", sizeof(inp->name));
1483 break;
1484 case 0x2257:
1485 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1486 sizeof(inp->name));
1487 break;
1488 }
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001489 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001490}
1491
1492static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1493{
1494 *i = 0;
1495 return 0;
1496}
1497static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1498{
1499 if (i > 0)
1500 return -EINVAL;
1501 return 0;
1502}
1503
1504/* --- controls ---------------------------------------------- */
1505static int vidioc_queryctrl(struct file *file, void *priv,
1506 struct v4l2_queryctrl *qc)
1507{
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001508 struct s2255_fh *fh = priv;
1509 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001510 switch (qc->id) {
1511 case V4L2_CID_BRIGHTNESS:
1512 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1513 break;
1514 case V4L2_CID_CONTRAST:
1515 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1516 break;
1517 case V4L2_CID_SATURATION:
1518 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1519 break;
1520 case V4L2_CID_HUE:
1521 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1522 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001523 case V4L2_CID_PRIVATE_COLORFILTER:
1524 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1525 return -EINVAL;
1526 if ((dev->pid == 0x2257) && (fh->channel > 1))
1527 return -EINVAL;
1528 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1529 qc->type = V4L2_CTRL_TYPE_MENU;
1530 qc->minimum = 0;
1531 qc->maximum = 1;
1532 qc->step = 1;
1533 qc->default_value = 1;
1534 qc->flags = 0;
1535 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001536 default:
1537 return -EINVAL;
1538 }
1539 dprintk(4, "%s, id %d\n", __func__, qc->id);
1540 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001541}
1542
1543static int vidioc_g_ctrl(struct file *file, void *priv,
1544 struct v4l2_control *ctrl)
1545{
Dean Anderson2e70db92010-03-05 14:29:09 -03001546 struct s2255_fh *fh = priv;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001547 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001548 switch (ctrl->id) {
1549 case V4L2_CID_BRIGHTNESS:
1550 ctrl->value = fh->mode.bright;
1551 break;
1552 case V4L2_CID_CONTRAST:
1553 ctrl->value = fh->mode.contrast;
1554 break;
1555 case V4L2_CID_SATURATION:
1556 ctrl->value = fh->mode.saturation;
1557 break;
1558 case V4L2_CID_HUE:
1559 ctrl->value = fh->mode.hue;
1560 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001561 case V4L2_CID_PRIVATE_COLORFILTER:
1562 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1563 return -EINVAL;
1564 if ((dev->pid == 0x2257) && (fh->channel > 1))
1565 return -EINVAL;
1566 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1567 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001568 default:
1569 return -EINVAL;
1570 }
1571 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1572 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001573}
1574
1575static int vidioc_s_ctrl(struct file *file, void *priv,
1576 struct v4l2_control *ctrl)
1577{
Dean Anderson38f993a2008-06-26 23:15:51 -03001578 struct s2255_fh *fh = priv;
1579 struct s2255_dev *dev = fh->dev;
1580 struct s2255_mode *mode;
1581 mode = &fh->mode;
Dean Anderson2e70db92010-03-05 14:29:09 -03001582 dprintk(4, "%s\n", __func__);
1583 /* update the mode to the corresponding value */
1584 switch (ctrl->id) {
1585 case V4L2_CID_BRIGHTNESS:
1586 mode->bright = ctrl->value;
1587 break;
1588 case V4L2_CID_CONTRAST:
1589 mode->contrast = ctrl->value;
1590 break;
1591 case V4L2_CID_HUE:
1592 mode->hue = ctrl->value;
1593 break;
1594 case V4L2_CID_SATURATION:
1595 mode->saturation = ctrl->value;
1596 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001597 case V4L2_CID_PRIVATE_COLORFILTER:
1598 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1599 return -EINVAL;
1600 if ((dev->pid == 0x2257) && (fh->channel > 1))
1601 return -EINVAL;
1602 mode->color &= ~MASK_INPUT_TYPE;
1603 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1604 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001605 default:
1606 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001607 }
Dean Anderson2e70db92010-03-05 14:29:09 -03001608 mode->restart = 0;
1609 /* set mode here. Note: stream does not need restarted.
1610 some V4L programs restart stream unnecessarily
1611 after a s_crtl.
1612 */
1613 s2255_set_mode(dev, fh->channel, mode);
1614 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001615}
1616
Dean Anderson22b88d42008-08-29 15:33:19 -03001617static int vidioc_g_jpegcomp(struct file *file, void *priv,
1618 struct v4l2_jpegcompression *jc)
1619{
1620 struct s2255_fh *fh = priv;
1621 struct s2255_dev *dev = fh->dev;
1622 *jc = dev->jc[fh->channel];
1623 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1624 return 0;
1625}
1626
1627static int vidioc_s_jpegcomp(struct file *file, void *priv,
1628 struct v4l2_jpegcompression *jc)
1629{
1630 struct s2255_fh *fh = priv;
1631 struct s2255_dev *dev = fh->dev;
1632 if (jc->quality < 0 || jc->quality > 100)
1633 return -EINVAL;
1634 dev->jc[fh->channel].quality = jc->quality;
1635 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1636 return 0;
1637}
Dean Anderson7d853532009-05-15 14:32:04 -03001638
1639static int vidioc_g_parm(struct file *file, void *priv,
1640 struct v4l2_streamparm *sp)
1641{
1642 struct s2255_fh *fh = priv;
1643 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001644 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001645 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1646 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001647 memset(sp, 0, sizeof(struct v4l2_streamparm));
1648 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
Dean Anderson7d853532009-05-15 14:32:04 -03001649 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001650 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1651 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1652 sp->parm.capture.timeperframe.denominator = def_dem;
1653 switch (fh->mode.fdec) {
1654 default:
1655 case FDEC_1:
1656 sp->parm.capture.timeperframe.numerator = def_num;
1657 break;
1658 case FDEC_2:
1659 sp->parm.capture.timeperframe.numerator = def_num * 2;
1660 break;
1661 case FDEC_3:
1662 sp->parm.capture.timeperframe.numerator = def_num * 3;
1663 break;
1664 case FDEC_5:
1665 sp->parm.capture.timeperframe.numerator = def_num * 5;
1666 break;
1667 }
1668 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1669 sp->parm.capture.capturemode,
1670 sp->parm.capture.timeperframe.numerator,
1671 sp->parm.capture.timeperframe.denominator);
Dean Anderson7d853532009-05-15 14:32:04 -03001672 return 0;
1673}
1674
1675static int vidioc_s_parm(struct file *file, void *priv,
1676 struct v4l2_streamparm *sp)
1677{
1678 struct s2255_fh *fh = priv;
1679 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001680 int fdec = FDEC_1;
1681 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001682 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1683 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001684 /* high quality capture mode requires a stream restart */
1685 if (dev->cap_parm[fh->channel].capturemode
1686 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1687 return -EBUSY;
1688 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1689 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1690 if (def_dem != sp->parm.capture.timeperframe.denominator)
1691 sp->parm.capture.timeperframe.numerator = def_num;
1692 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1693 sp->parm.capture.timeperframe.numerator = def_num;
1694 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1695 sp->parm.capture.timeperframe.numerator = def_num * 2;
1696 fdec = FDEC_2;
1697 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1698 sp->parm.capture.timeperframe.numerator = def_num * 3;
1699 fdec = FDEC_3;
1700 } else {
1701 sp->parm.capture.timeperframe.numerator = def_num * 5;
1702 fdec = FDEC_5;
1703 }
1704 fh->mode.fdec = fdec;
1705 sp->parm.capture.timeperframe.denominator = def_dem;
1706 s2255_set_mode(dev, fh->channel, &fh->mode);
1707 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1708 __func__,
1709 sp->parm.capture.capturemode,
1710 sp->parm.capture.timeperframe.numerator,
1711 sp->parm.capture.timeperframe.denominator, fdec);
Dean Anderson7d853532009-05-15 14:32:04 -03001712 return 0;
1713}
Dean Andersone6b44bc2010-03-08 20:04:48 -03001714
1715static int vidioc_enum_frameintervals(struct file *file, void *priv,
1716 struct v4l2_frmivalenum *fe)
1717{
1718 int is_ntsc = 0;
1719#define NUM_FRAME_ENUMS 4
1720 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1721 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1722 return -EINVAL;
1723 switch (fe->width) {
1724 case 640:
1725 if (fe->height != 240 && fe->height != 480)
1726 return -EINVAL;
1727 is_ntsc = 1;
1728 break;
1729 case 320:
1730 if (fe->height != 240)
1731 return -EINVAL;
1732 is_ntsc = 1;
1733 break;
1734 case 704:
1735 if (fe->height != 288 && fe->height != 576)
1736 return -EINVAL;
1737 break;
1738 case 352:
1739 if (fe->height != 288)
1740 return -EINVAL;
1741 break;
1742 default:
1743 return -EINVAL;
1744 }
1745 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1746 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1747 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1748 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1749 fe->discrete.denominator);
1750 return 0;
1751}
1752
Hans Verkuilbec43662008-12-30 06:58:20 -03001753static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001754{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001755 struct video_device *vdev = video_devdata(file);
1756 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001757 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001758 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001759 int i = 0;
1760 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001761 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001762
1763 dprintk(1, "s2255: open called (dev=%s)\n",
1764 video_device_node_name(vdev));
Dean Anderson38f993a2008-06-26 23:15:51 -03001765
Hans Verkuild56dc612008-07-30 08:43:36 -03001766 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001767
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001768 for (i = 0; i < MAX_CHANNELS; i++) {
1769 if (dev->vdev[i] == vdev) {
1770 cur_channel = i;
1771 break;
1772 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001773 }
1774
Dean Anderson14d96262008-08-25 13:58:55 -03001775 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1776 unlock_kernel();
1777 printk(KERN_INFO "disconnecting\n");
1778 return -ENODEV;
1779 }
1780 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001781 mutex_lock(&dev->open_lock);
1782
1783 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001784 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001785
Dean Anderson14d96262008-08-25 13:58:55 -03001786 switch (atomic_read(&dev->fw_data->fw_state)) {
1787 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001788 s2255_dev_err(&dev->udev->dev,
1789 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001790 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001791 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001792 ((atomic_read(&dev->fw_data->fw_state)
1793 == S2255_FW_SUCCESS) ||
1794 (atomic_read(&dev->fw_data->fw_state)
1795 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001796 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001797 break;
1798 case S2255_FW_NOTLOADED:
1799 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001800 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1801 driver loaded and then device immediately opened */
1802 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1803 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001804 ((atomic_read(&dev->fw_data->fw_state)
1805 == S2255_FW_SUCCESS) ||
1806 (atomic_read(&dev->fw_data->fw_state)
1807 == S2255_FW_DISCONNECTING)),
1808 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1809 break;
1810 case S2255_FW_SUCCESS:
1811 default:
1812 break;
1813 }
1814 state = atomic_read(&dev->fw_data->fw_state);
1815 if (state != S2255_FW_SUCCESS) {
1816 int rc;
1817 switch (state) {
1818 case S2255_FW_FAILED:
1819 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1820 rc = -ENODEV;
1821 break;
1822 case S2255_FW_DISCONNECTING:
1823 printk(KERN_INFO "%s: disconnecting\n", __func__);
1824 rc = -ENODEV;
1825 break;
1826 case S2255_FW_LOADED_DSPWAIT:
1827 case S2255_FW_NOTLOADED:
1828 printk(KERN_INFO "%s: firmware not loaded yet"
1829 "please try again later\n",
1830 __func__);
1831 rc = -EAGAIN;
1832 break;
1833 default:
1834 printk(KERN_INFO "%s: unknown state\n", __func__);
1835 rc = -EFAULT;
1836 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001837 }
Dean Anderson14d96262008-08-25 13:58:55 -03001838 dev->users[cur_channel]--;
1839 mutex_unlock(&dev->open_lock);
1840 kref_put(&dev->kref, s2255_destroy);
1841 unlock_kernel();
1842 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001843 }
1844
1845 /* allocate + initialize per filehandle data */
1846 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1847 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001848 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001849 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001850 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001851 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001852 return -ENOMEM;
1853 }
1854
1855 file->private_data = fh;
1856 fh->dev = dev;
1857 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1858 fh->mode = dev->mode[cur_channel];
1859 fh->fmt = dev->cur_fmt[cur_channel];
1860 /* default 4CIF NTSC */
1861 fh->width = LINE_SZ_4CIFS_NTSC;
1862 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1863 fh->channel = cur_channel;
Dean Anderson14d96262008-08-25 13:58:55 -03001864 /* configure channel to default state */
1865 if (!dev->chn_configured[cur_channel]) {
1866 s2255_set_mode(dev, cur_channel, &fh->mode);
1867 dev->chn_configured[cur_channel] = 1;
1868 }
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001869 dprintk(1, "s2255drv: open dev=%s type=%s users=%d\n",
1870 video_device_node_name(vdev), v4l2_type_names[type],
1871 dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001872 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1873 (unsigned long)fh, (unsigned long)dev,
1874 (unsigned long)&dev->vidq[cur_channel]);
1875 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1876 list_empty(&dev->vidq[cur_channel].active));
1877
1878 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1879 NULL, &dev->slock,
1880 fh->type,
1881 V4L2_FIELD_INTERLACED,
1882 sizeof(struct s2255_buffer), fh);
1883
Dean Anderson38f993a2008-06-26 23:15:51 -03001884 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001885 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001886 return 0;
1887}
1888
1889
1890static unsigned int s2255_poll(struct file *file,
1891 struct poll_table_struct *wait)
1892{
1893 struct s2255_fh *fh = file->private_data;
1894 int rc;
1895 dprintk(100, "%s\n", __func__);
1896
1897 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1898 return POLLERR;
1899
1900 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1901 return rc;
1902}
1903
1904static void s2255_destroy(struct kref *kref)
1905{
1906 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001907 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001908 if (!dev) {
1909 printk(KERN_ERR "s2255drv: kref problem\n");
1910 return;
1911 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001912 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1913 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001914 for (i = 0; i < MAX_CHANNELS; i++) {
1915 dev->setmode_ready[i] = 1;
1916 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03001917 dev->vidstatus_ready[i] = 1;
1918 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001919 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001920 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001921 /* reset the DSP so firmware can be reload next time */
1922 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001923 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001924 /* board shutdown stops the read pipe if it is running */
1925 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001926 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001927 del_timer(&dev->timer); /* only started in .probe and .open */
1928
Dean Anderson38f993a2008-06-26 23:15:51 -03001929 if (dev->fw_data->fw_urb) {
1930 dprintk(2, "kill fw_urb\n");
1931 usb_kill_urb(dev->fw_data->fw_urb);
1932 usb_free_urb(dev->fw_data->fw_urb);
1933 dev->fw_data->fw_urb = NULL;
1934 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001935 if (dev->fw_data->fw)
1936 release_firmware(dev->fw_data->fw);
1937 kfree(dev->fw_data->pfw_data);
1938 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001939 usb_put_dev(dev->udev);
1940 dprintk(1, "%s", __func__);
Dean Anderson14d96262008-08-25 13:58:55 -03001941
Dean Anderson14d96262008-08-25 13:58:55 -03001942 mutex_unlock(&dev->open_lock);
Dean Andersonb7732a32009-03-30 11:59:56 -03001943 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001944}
1945
Hans Verkuilbec43662008-12-30 06:58:20 -03001946static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001947{
1948 struct s2255_fh *fh = file->private_data;
1949 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001950 struct video_device *vdev = video_devdata(file);
1951
Dean Anderson38f993a2008-06-26 23:15:51 -03001952 if (!dev)
1953 return -ENODEV;
1954
1955 mutex_lock(&dev->open_lock);
1956
Dean Andersonf78d92c2008-07-22 14:43:27 -03001957 /* turn off stream */
1958 if (res_check(fh)) {
1959 if (dev->b_acquire[fh->channel])
1960 s2255_stop_acquire(dev, fh->channel);
1961 videobuf_streamoff(&fh->vb_vidq);
1962 res_free(dev, fh);
1963 }
1964
Dean Anderson38f993a2008-06-26 23:15:51 -03001965 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001966 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001967
Dean Anderson38f993a2008-06-26 23:15:51 -03001968 mutex_unlock(&dev->open_lock);
1969
1970 kref_put(&dev->kref, s2255_destroy);
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001971 dprintk(1, "s2255: close called (dev=%s, users=%d)\n",
1972 video_device_node_name(vdev), dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001973 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001974 return 0;
1975}
1976
1977static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1978{
1979 struct s2255_fh *fh = file->private_data;
1980 int ret;
1981
1982 if (!fh)
1983 return -ENODEV;
1984 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1985
1986 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1987
1988 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1989 (unsigned long)vma->vm_start,
1990 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1991
1992 return ret;
1993}
1994
Hans Verkuilbec43662008-12-30 06:58:20 -03001995static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001996 .owner = THIS_MODULE,
1997 .open = s2255_open,
1998 .release = s2255_close,
1999 .poll = s2255_poll,
2000 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03002001 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03002002};
2003
Hans Verkuila3998102008-07-21 02:57:38 -03002004static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002005 .vidioc_querymenu = vidioc_querymenu,
Dean Anderson38f993a2008-06-26 23:15:51 -03002006 .vidioc_querycap = vidioc_querycap,
2007 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2008 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2009 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2010 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2011 .vidioc_reqbufs = vidioc_reqbufs,
2012 .vidioc_querybuf = vidioc_querybuf,
2013 .vidioc_qbuf = vidioc_qbuf,
2014 .vidioc_dqbuf = vidioc_dqbuf,
2015 .vidioc_s_std = vidioc_s_std,
2016 .vidioc_enum_input = vidioc_enum_input,
2017 .vidioc_g_input = vidioc_g_input,
2018 .vidioc_s_input = vidioc_s_input,
2019 .vidioc_queryctrl = vidioc_queryctrl,
2020 .vidioc_g_ctrl = vidioc_g_ctrl,
2021 .vidioc_s_ctrl = vidioc_s_ctrl,
2022 .vidioc_streamon = vidioc_streamon,
2023 .vidioc_streamoff = vidioc_streamoff,
2024#ifdef CONFIG_VIDEO_V4L1_COMPAT
2025 .vidiocgmbuf = vidioc_cgmbuf,
2026#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03002027 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2028 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03002029 .vidioc_s_parm = vidioc_s_parm,
2030 .vidioc_g_parm = vidioc_g_parm,
Dean Andersone6b44bc2010-03-08 20:04:48 -03002031 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Hans Verkuila3998102008-07-21 02:57:38 -03002032};
2033
2034static struct video_device template = {
2035 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03002036 .fops = &s2255_fops_v4l,
2037 .ioctl_ops = &s2255_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002038 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03002039 .tvnorms = S2255_NORMS,
2040 .current_norm = V4L2_STD_NTSC_M,
2041};
2042
2043static int s2255_probe_v4l(struct s2255_dev *dev)
2044{
2045 int ret;
2046 int i;
2047 int cur_nr = video_nr;
2048
2049 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03002050 /* register 4 video devices */
2051 for (i = 0; i < MAX_CHANNELS; i++) {
2052 INIT_LIST_HEAD(&dev->vidq[i].active);
2053 dev->vidq[i].dev = dev;
2054 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002055 /* register 4 video devices */
2056 dev->vdev[i] = video_device_alloc();
2057 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03002058 dev->vdev[i]->parent = &dev->interface->dev;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002059 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002060 if (video_nr == -1)
2061 ret = video_register_device(dev->vdev[i],
2062 VFL_TYPE_GRABBER,
2063 video_nr);
2064 else
2065 ret = video_register_device(dev->vdev[i],
2066 VFL_TYPE_GRABBER,
2067 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03002068 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002069
2070 if (ret != 0) {
2071 dev_err(&dev->udev->dev,
2072 "failed to register video device!\n");
2073 return ret;
2074 }
2075 }
Dean Andersonabce21f2009-04-23 16:04:41 -03002076 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
2077 S2255_MAJOR_VERSION,
2078 S2255_MINOR_VERSION);
Dean Anderson38f993a2008-06-26 23:15:51 -03002079 return ret;
2080}
2081
2082static void s2255_exit_v4l(struct s2255_dev *dev)
2083{
Dean Anderson14d96262008-08-25 13:58:55 -03002084
Dean Anderson38f993a2008-06-26 23:15:51 -03002085 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002086 for (i = 0; i < MAX_CHANNELS; i++) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002087 if (video_is_registered(dev->vdev[i])) {
Dean Anderson38f993a2008-06-26 23:15:51 -03002088 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002089 printk(KERN_INFO "s2255 unregistered\n");
2090 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03002091 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002092 printk(KERN_INFO "s2255 released\n");
2093 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002094 }
2095}
2096
2097/* this function moves the usb stream read pipe data
2098 * into the system buffers.
2099 * returns 0 on success, EAGAIN if more data to process( call this
2100 * function again).
2101 *
2102 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03002103 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03002104 * bytes 4-7: channel: 0-3
2105 * bytes 8-11: payload size: size of the frame
2106 * bytes 12-payloadsize+12: frame data
2107 */
2108static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2109{
Dean Anderson38f993a2008-06-26 23:15:51 -03002110 char *pdest;
2111 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002112 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002113 char *psrc;
2114 unsigned long copy_size;
2115 unsigned long size;
2116 s32 idx = -1;
2117 struct s2255_framei *frm;
2118 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002119
Dean Anderson38f993a2008-06-26 23:15:51 -03002120 dprintk(100, "buffer to user\n");
2121
2122 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03002123 frm = &dev->buffer[dev->cc].frame[idx];
2124
Dean Anderson14d96262008-08-25 13:58:55 -03002125 if (frm->ulState == S2255_READ_IDLE) {
2126 int jj;
2127 unsigned int cc;
Dean Anderson3fa00602010-03-04 20:47:33 -03002128 __le32 *pdword; /*data from dsp is little endian */
Dean Anderson14d96262008-08-25 13:58:55 -03002129 int payload;
2130 /* search for marker codes */
2131 pdata = (unsigned char *)pipe_info->transfer_buffer;
Dean Anderson3fa00602010-03-04 20:47:33 -03002132 pdword = (__le32 *)pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002133 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
Dean Anderson3fa00602010-03-04 20:47:33 -03002134 switch (*pdword) {
Dean Anderson14d96262008-08-25 13:58:55 -03002135 case S2255_MARKER_FRAME:
Dean Anderson14d96262008-08-25 13:58:55 -03002136 dprintk(4, "found frame marker at offset:"
2137 " %d [%x %x]\n", jj, pdata[0],
2138 pdata[1]);
2139 offset = jj + PREFIX_SIZE;
2140 bframe = 1;
2141 cc = pdword[1];
2142 if (cc >= MAX_CHANNELS) {
2143 printk(KERN_ERR
2144 "bad channel\n");
2145 return -EINVAL;
2146 }
2147 /* reverse it */
2148 dev->cc = G_chnmap[cc];
2149 payload = pdword[3];
2150 if (payload > dev->req_image_size[dev->cc]) {
2151 dev->bad_payload[dev->cc]++;
2152 /* discard the bad frame */
2153 return -EINVAL;
2154 }
2155 dev->pkt_size[dev->cc] = payload;
2156 dev->jpg_size[dev->cc] = pdword[4];
2157 break;
2158 case S2255_MARKER_RESPONSE:
Dean Anderson14d96262008-08-25 13:58:55 -03002159 pdata += DEF_USB_BLOCK;
2160 jj += DEF_USB_BLOCK;
2161 if (pdword[1] >= MAX_CHANNELS)
2162 break;
2163 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03002164 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03002165 break;
2166 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03002167 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03002168 /* check if channel valid */
2169 /* set mode ready */
2170 dev->setmode_ready[cc] = 1;
2171 wake_up(&dev->wait_setmode[cc]);
2172 dprintk(5, "setmode ready %d\n", cc);
2173 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03002174 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03002175
2176 dev->chn_ready |= (1 << cc);
2177 if ((dev->chn_ready & 0x0f) != 0x0f)
2178 break;
2179 /* all channels ready */
2180 printk(KERN_INFO "s2255: fw loaded\n");
2181 atomic_set(&dev->fw_data->fw_state,
2182 S2255_FW_SUCCESS);
2183 wake_up(&dev->fw_data->wait_fw);
2184 break;
Dean Anderson4de39f52010-03-03 19:39:19 -03002185 case S2255_RESPONSE_STATUS:
2186 dev->vidstatus[cc] = pdword[3];
2187 dev->vidstatus_ready[cc] = 1;
2188 wake_up(&dev->wait_vidstatus[cc]);
2189 dprintk(5, "got vidstatus %x chan %d\n",
2190 pdword[3], cc);
2191 break;
Dean Anderson14d96262008-08-25 13:58:55 -03002192 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002193 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002194 }
2195 default:
2196 pdata++;
2197 break;
2198 }
2199 if (bframe)
2200 break;
2201 } /* for */
2202 if (!bframe)
2203 return -EINVAL;
2204 }
2205
2206
2207 idx = dev->cur_frame[dev->cc];
2208 frm = &dev->buffer[dev->cc].frame[idx];
2209
2210 /* search done. now find out if should be acquiring on this channel */
2211 if (!dev->b_acquire[dev->cc]) {
2212 /* we found a frame, but this channel is turned off */
2213 frm->ulState = S2255_READ_IDLE;
2214 return -EINVAL;
2215 }
2216
2217 if (frm->ulState == S2255_READ_IDLE) {
2218 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002219 frm->cur_size = 0;
2220 }
2221
Dean Anderson14d96262008-08-25 13:58:55 -03002222 /* skip the marker 512 bytes (and offset if out of sync) */
2223 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2224
Dean Anderson38f993a2008-06-26 23:15:51 -03002225
2226 if (frm->lpvbits == NULL) {
2227 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2228 frm, dev, dev->cc, idx);
2229 return -ENOMEM;
2230 }
2231
2232 pdest = frm->lpvbits + frm->cur_size;
2233
Dean Anderson14d96262008-08-25 13:58:55 -03002234 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002235
Dean Anderson14d96262008-08-25 13:58:55 -03002236 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002237
Dean Anderson14d96262008-08-25 13:58:55 -03002238 /* sanity check on pdest */
2239 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2240 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002241
Dean Anderson38f993a2008-06-26 23:15:51 -03002242 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002243 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002244
Dean Anderson14d96262008-08-25 13:58:55 -03002245 if (frm->cur_size >= size) {
2246
Dean Anderson38f993a2008-06-26 23:15:51 -03002247 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002248 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2249 cc, idx);
2250 dev->last_frame[cc] = dev->cur_frame[cc];
2251 dev->cur_frame[cc]++;
2252 /* end of system frame ring buffer, start at zero */
2253 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2254 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2255 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002256 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002257 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002258 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002259 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002260 frm->ulState = S2255_READ_IDLE;
2261 frm->cur_size = 0;
2262
Dean Anderson38f993a2008-06-26 23:15:51 -03002263 }
2264 /* done successfully */
2265 return 0;
2266}
2267
2268static void s2255_read_video_callback(struct s2255_dev *dev,
2269 struct s2255_pipeinfo *pipe_info)
2270{
2271 int res;
2272 dprintk(50, "callback read video \n");
2273
2274 if (dev->cc >= MAX_CHANNELS) {
2275 dev->cc = 0;
2276 dev_err(&dev->udev->dev, "invalid channel\n");
2277 return;
2278 }
2279 /* otherwise copy to the system buffers */
2280 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002281 if (res != 0)
2282 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002283
2284 dprintk(50, "callback read video done\n");
2285 return;
2286}
2287
2288static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2289 u16 Index, u16 Value, void *TransferBuffer,
2290 s32 TransferBufferLength, int bOut)
2291{
2292 int r;
2293 if (!bOut) {
2294 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2295 Request,
2296 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2297 USB_DIR_IN,
2298 Value, Index, TransferBuffer,
2299 TransferBufferLength, HZ * 5);
2300 } else {
2301 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2302 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2303 Value, Index, TransferBuffer,
2304 TransferBufferLength, HZ * 5);
2305 }
2306 return r;
2307}
2308
2309/*
2310 * retrieve FX2 firmware version. future use.
2311 * @param dev pointer to device extension
2312 * @return -1 for fail, else returns firmware version as an int(16 bits)
2313 */
2314static int s2255_get_fx2fw(struct s2255_dev *dev)
2315{
2316 int fw;
2317 int ret;
2318 unsigned char transBuffer[64];
2319 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2320 S2255_VR_IN);
2321 if (ret < 0)
2322 dprintk(2, "get fw error: %x\n", ret);
2323 fw = transBuffer[0] + (transBuffer[1] << 8);
2324 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2325 return fw;
2326}
2327
2328/*
2329 * Create the system ring buffer to copy frames into from the
2330 * usb read pipe.
2331 */
2332static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2333{
2334 unsigned long i;
2335 unsigned long reqsize;
2336 dprintk(1, "create sys buffers\n");
2337 if (chn >= MAX_CHANNELS)
2338 return -1;
2339
2340 dev->buffer[chn].dwFrames = SYS_FRAMES;
2341
2342 /* always allocate maximum size(PAL) for system buffers */
2343 reqsize = SYS_FRAMES_MAXSIZE;
2344
2345 if (reqsize > SYS_FRAMES_MAXSIZE)
2346 reqsize = SYS_FRAMES_MAXSIZE;
2347
2348 for (i = 0; i < SYS_FRAMES; i++) {
2349 /* allocate the frames */
2350 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2351
2352 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2353 &dev->buffer[chn].frame[i], chn, i,
2354 dev->buffer[chn].frame[i].lpvbits);
2355 dev->buffer[chn].frame[i].size = reqsize;
2356 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2357 printk(KERN_INFO "out of memory. using less frames\n");
2358 dev->buffer[chn].dwFrames = i;
2359 break;
2360 }
2361 }
2362
2363 /* make sure internal states are set */
2364 for (i = 0; i < SYS_FRAMES; i++) {
2365 dev->buffer[chn].frame[i].ulState = 0;
2366 dev->buffer[chn].frame[i].cur_size = 0;
2367 }
2368
2369 dev->cur_frame[chn] = 0;
2370 dev->last_frame[chn] = -1;
2371 return 0;
2372}
2373
2374static int s2255_release_sys_buffers(struct s2255_dev *dev,
2375 unsigned long channel)
2376{
2377 unsigned long i;
2378 dprintk(1, "release sys buffers\n");
2379 for (i = 0; i < SYS_FRAMES; i++) {
2380 if (dev->buffer[channel].frame[i].lpvbits) {
2381 dprintk(1, "vfree %p\n",
2382 dev->buffer[channel].frame[i].lpvbits);
2383 vfree(dev->buffer[channel].frame[i].lpvbits);
2384 }
2385 dev->buffer[channel].frame[i].lpvbits = NULL;
2386 }
2387 return 0;
2388}
2389
2390static int s2255_board_init(struct s2255_dev *dev)
2391{
2392 int j;
2393 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2394 int fw_ver;
2395 dprintk(4, "board init: %p", dev);
2396
2397 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2398 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2399
2400 memset(pipe, 0, sizeof(*pipe));
2401 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002402 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2403 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002404
Dean Anderson38f993a2008-06-26 23:15:51 -03002405 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2406 GFP_KERNEL);
2407 if (pipe->transfer_buffer == NULL) {
2408 dprintk(1, "out of memory!\n");
2409 return -ENOMEM;
2410 }
2411
2412 }
2413
2414 /* query the firmware */
2415 fw_ver = s2255_get_fx2fw(dev);
2416
Dean Andersonabce21f2009-04-23 16:04:41 -03002417 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2418 (fw_ver >> 8) & 0xff,
2419 fw_ver & 0xff);
2420
2421 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002422 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002423 "usb firmware not up to date %d.%d\n",
2424 (fw_ver >> 8) & 0xff,
2425 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002426
2427 for (j = 0; j < MAX_CHANNELS; j++) {
2428 dev->b_acquire[j] = 0;
2429 dev->mode[j] = mode_def;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002430 if (dev->pid == 0x2257 && j > 1)
2431 dev->mode[j].color |= (1 << 16);
Dean Anderson22b88d42008-08-29 15:33:19 -03002432 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002433 dev->cur_fmt[j] = &formats[0];
2434 dev->mode[j].restart = 1;
2435 dev->req_image_size[j] = get_transfer_size(&mode_def);
2436 dev->frame_count[j] = 0;
2437 /* create the system buffers */
2438 s2255_create_sys_buffers(dev, j);
2439 }
2440 /* start read pipe */
2441 s2255_start_readpipe(dev);
2442
2443 dprintk(1, "S2255: board initialized\n");
2444 return 0;
2445}
2446
2447static int s2255_board_shutdown(struct s2255_dev *dev)
2448{
2449 u32 i;
2450
2451 dprintk(1, "S2255: board shutdown: %p", dev);
2452
2453 for (i = 0; i < MAX_CHANNELS; i++) {
2454 if (dev->b_acquire[i])
2455 s2255_stop_acquire(dev, i);
2456 }
2457
2458 s2255_stop_readpipe(dev);
2459
2460 for (i = 0; i < MAX_CHANNELS; i++)
2461 s2255_release_sys_buffers(dev, i);
2462
2463 /* release transfer buffers */
2464 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2465 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2466 kfree(pipe->transfer_buffer);
2467 }
2468 return 0;
2469}
2470
2471static void read_pipe_completion(struct urb *purb)
2472{
2473 struct s2255_pipeinfo *pipe_info;
2474 struct s2255_dev *dev;
2475 int status;
2476 int pipe;
2477
2478 pipe_info = purb->context;
2479 dprintk(100, "read pipe completion %p, status %d\n", purb,
2480 purb->status);
2481 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002482 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002483 return;
2484 }
2485
2486 dev = pipe_info->dev;
2487 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002488 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002489 return;
2490 }
2491 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002492 /* if shutting down, do not resubmit, exit immediately */
2493 if (status == -ESHUTDOWN) {
2494 dprintk(2, "read_pipe_completion: err shutdown\n");
2495 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002496 return;
2497 }
2498
2499 if (pipe_info->state == 0) {
2500 dprintk(2, "exiting USB pipe");
2501 return;
2502 }
2503
Dean Andersonb02064c2009-04-30 12:29:38 -03002504 if (status == 0)
2505 s2255_read_video_callback(dev, pipe_info);
2506 else {
2507 pipe_info->err_count++;
2508 dprintk(1, "s2255drv: failed URB %d\n", status);
2509 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002510
Dean Anderson38f993a2008-06-26 23:15:51 -03002511 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2512 /* reuse urb */
2513 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2514 pipe,
2515 pipe_info->transfer_buffer,
2516 pipe_info->cur_transfer_size,
2517 read_pipe_completion, pipe_info);
2518
2519 if (pipe_info->state != 0) {
2520 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2521 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002522 }
2523 } else {
2524 dprintk(2, "read pipe complete state 0\n");
2525 }
2526 return;
2527}
2528
2529static int s2255_start_readpipe(struct s2255_dev *dev)
2530{
2531 int pipe;
2532 int retval;
2533 int i;
2534 struct s2255_pipeinfo *pipe_info = dev->pipes;
2535 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2536 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2537
2538 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2539 pipe_info->state = 1;
Dean Andersonabce21f2009-04-23 16:04:41 -03002540 pipe_info->err_count = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002541 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2542 if (!pipe_info->stream_urb) {
2543 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002544 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002545 return -ENOMEM;
2546 }
2547 /* transfer buffer allocated in board_init */
2548 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2549 pipe,
2550 pipe_info->transfer_buffer,
2551 pipe_info->cur_transfer_size,
2552 read_pipe_completion, pipe_info);
2553
Dean Anderson38f993a2008-06-26 23:15:51 -03002554 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2555 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2556 if (retval) {
2557 printk(KERN_ERR "s2255: start read pipe failed\n");
2558 return retval;
2559 }
2560 }
2561
2562 return 0;
2563}
2564
2565/* starts acquisition process */
2566static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2567{
2568 unsigned char *buffer;
2569 int res;
2570 unsigned long chn_rev;
2571 int j;
2572 if (chn >= MAX_CHANNELS) {
2573 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2574 return -1;
2575 }
2576
2577 chn_rev = G_chnmap[chn];
2578 dprintk(1, "S2255: start acquire %lu \n", chn);
2579
2580 buffer = kzalloc(512, GFP_KERNEL);
2581 if (buffer == NULL) {
2582 dev_err(&dev->udev->dev, "out of mem\n");
2583 return -ENOMEM;
2584 }
2585
2586 dev->last_frame[chn] = -1;
2587 dev->bad_payload[chn] = 0;
2588 dev->cur_frame[chn] = 0;
2589 for (j = 0; j < SYS_FRAMES; j++) {
2590 dev->buffer[chn].frame[j].ulState = 0;
2591 dev->buffer[chn].frame[j].cur_size = 0;
2592 }
2593
2594 /* send the start command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002595 *(__le32 *) buffer = IN_DATA_TOKEN;
2596 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2597 *((__le32 *) buffer + 2) = CMD_START;
Dean Anderson38f993a2008-06-26 23:15:51 -03002598 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2599 if (res != 0)
2600 dev_err(&dev->udev->dev, "CMD_START error\n");
2601
2602 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2603 kfree(buffer);
2604 return 0;
2605}
2606
2607static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2608{
2609 unsigned char *buffer;
2610 int res;
2611 unsigned long chn_rev;
Dean Anderson38f993a2008-06-26 23:15:51 -03002612 if (chn >= MAX_CHANNELS) {
2613 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2614 return -1;
2615 }
2616 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002617 buffer = kzalloc(512, GFP_KERNEL);
2618 if (buffer == NULL) {
2619 dev_err(&dev->udev->dev, "out of mem\n");
2620 return -ENOMEM;
2621 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002622 /* send the stop command */
2623 dprintk(4, "stop acquire %lu\n", chn);
Dean Anderson3fa00602010-03-04 20:47:33 -03002624 *(__le32 *) buffer = IN_DATA_TOKEN;
2625 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2626 *((__le32 *) buffer + 2) = CMD_STOP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002627 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2628
2629 if (res != 0)
2630 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2631
2632 dprintk(4, "stop acquire: releasing states \n");
2633
2634 kfree(buffer);
2635 dev->b_acquire[chn] = 0;
2636
Dean Anderson14d96262008-08-25 13:58:55 -03002637 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002638}
2639
2640static void s2255_stop_readpipe(struct s2255_dev *dev)
2641{
2642 int j;
2643
2644 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002645 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002646 return;
2647 }
2648 dprintk(4, "stop read pipe\n");
2649 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2650 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2651 if (pipe_info) {
2652 if (pipe_info->state == 0)
2653 continue;
2654 pipe_info->state = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002655 }
2656 }
2657
2658 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2659 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2660 if (pipe_info->stream_urb) {
2661 /* cancel urb */
2662 usb_kill_urb(pipe_info->stream_urb);
2663 usb_free_urb(pipe_info->stream_urb);
2664 pipe_info->stream_urb = NULL;
2665 }
2666 }
2667 dprintk(2, "s2255 stop read pipe: %d\n", j);
2668 return;
2669}
2670
Dean Anderson14d96262008-08-25 13:58:55 -03002671static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002672{
Dean Anderson14d96262008-08-25 13:58:55 -03002673 if (reset)
2674 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002675 dev->fw_data->fw_size = dev->fw_data->fw->size;
2676 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2677 memcpy(dev->fw_data->pfw_data,
2678 dev->fw_data->fw->data, CHUNK_SIZE);
2679 dev->fw_data->fw_loaded = CHUNK_SIZE;
2680 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2681 usb_sndbulkpipe(dev->udev, 2),
2682 dev->fw_data->pfw_data,
2683 CHUNK_SIZE, s2255_fwchunk_complete,
2684 dev->fw_data);
2685 mod_timer(&dev->timer, jiffies + HZ);
2686}
2687
2688/* standard usb probe function */
2689static int s2255_probe(struct usb_interface *interface,
2690 const struct usb_device_id *id)
2691{
2692 struct s2255_dev *dev = NULL;
2693 struct usb_host_interface *iface_desc;
2694 struct usb_endpoint_descriptor *endpoint;
2695 int i;
2696 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002697 __le32 *pdata;
2698 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002699
2700 dprintk(2, "s2255: probe\n");
2701
2702 /* allocate memory for our device state and initialize it to zero */
2703 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2704 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002705 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002706 goto error;
2707 }
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002708 dev->pid = id->idProduct;
Dean Anderson38f993a2008-06-26 23:15:51 -03002709 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2710 if (!dev->fw_data)
2711 goto error;
2712
2713 mutex_init(&dev->lock);
2714 mutex_init(&dev->open_lock);
2715
2716 /* grab usb_device and save it */
2717 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2718 if (dev->udev == NULL) {
2719 dev_err(&interface->dev, "null usb device\n");
2720 retval = -ENODEV;
2721 goto error;
2722 }
2723 kref_init(&dev->kref);
2724 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2725 dev->udev, interface);
2726 dev->interface = interface;
2727 /* set up the endpoint information */
2728 iface_desc = interface->cur_altsetting;
2729 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2730 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2731 endpoint = &iface_desc->endpoint[i].desc;
2732 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2733 /* we found the bulk in endpoint */
2734 dev->read_endpoint = endpoint->bEndpointAddress;
2735 }
2736 }
2737
2738 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002739 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002740 goto error;
2741 }
2742
2743 /* set intfdata */
2744 usb_set_intfdata(interface, dev);
2745
2746 dprintk(100, "after intfdata %p\n", dev);
2747
2748 init_timer(&dev->timer);
2749 dev->timer.function = s2255_timer;
2750 dev->timer.data = (unsigned long)dev->fw_data;
2751
2752 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson4de39f52010-03-03 19:39:19 -03002753 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03002754 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002755 init_waitqueue_head(&dev->wait_vidstatus[i]);
2756 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002757
2758 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2759
2760 if (!dev->fw_data->fw_urb) {
2761 dev_err(&interface->dev, "out of memory!\n");
2762 goto error;
2763 }
2764 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2765 if (!dev->fw_data->pfw_data) {
2766 dev_err(&interface->dev, "out of memory!\n");
2767 goto error;
2768 }
2769 /* load the first chunk */
2770 if (request_firmware(&dev->fw_data->fw,
2771 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2772 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2773 goto error;
2774 }
Dean Anderson14d96262008-08-25 13:58:55 -03002775 /* check the firmware is valid */
2776 fw_size = dev->fw_data->fw->size;
2777 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002778
Dean Anderson14d96262008-08-25 13:58:55 -03002779 if (*pdata != S2255_FW_MARKER) {
2780 printk(KERN_INFO "Firmware invalid.\n");
2781 retval = -ENODEV;
2782 goto error;
2783 } else {
2784 /* make sure firmware is the latest */
2785 __le32 *pRel;
2786 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2787 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
Dean Anderson4de39f52010-03-03 19:39:19 -03002788 dev->dsp_fw_ver = *pRel;
2789 if (*pRel < S2255_CUR_DSP_FWVER)
2790 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002791 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
2792 printk(KERN_WARNING "s2255: 2257 requires firmware 8 or above.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002793 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002794 /* loads v4l specific */
2795 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002796 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002797 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002798 retval = s2255_board_init(dev);
2799 if (retval)
2800 goto error;
Dean Anderson38f993a2008-06-26 23:15:51 -03002801
2802 dprintk(4, "before probe done %p\n", dev);
2803 spin_lock_init(&dev->slock);
2804
Dean Anderson14d96262008-08-25 13:58:55 -03002805 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002806 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2807 return 0;
2808error:
2809 return retval;
2810}
2811
2812/* disconnect routine. when board is removed physically or with rmmod */
2813static void s2255_disconnect(struct usb_interface *interface)
2814{
2815 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002816 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002817 dprintk(1, "s2255: disconnect interface %p\n", interface);
2818 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002819
2820 /*
2821 * wake up any of the timers to allow open_lock to be
2822 * acquired sooner
2823 */
2824 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2825 wake_up(&dev->fw_data->wait_fw);
2826 for (i = 0; i < MAX_CHANNELS; i++) {
2827 dev->setmode_ready[i] = 1;
2828 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002829 dev->vidstatus_ready[i] = 1;
2830 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002831 }
2832
2833 mutex_lock(&dev->open_lock);
2834 usb_set_intfdata(interface, NULL);
2835 mutex_unlock(&dev->open_lock);
2836
Dean Anderson38f993a2008-06-26 23:15:51 -03002837 if (dev) {
2838 kref_put(&dev->kref, s2255_destroy);
2839 dprintk(1, "s2255drv: disconnect\n");
2840 dev_info(&interface->dev, "s2255usb now disconnected\n");
2841 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002842}
2843
2844static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002845 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002846 .probe = s2255_probe,
2847 .disconnect = s2255_disconnect,
2848 .id_table = s2255_table,
2849};
2850
2851static int __init usb_s2255_init(void)
2852{
2853 int result;
2854
2855 /* register this driver with the USB subsystem */
2856 result = usb_register(&s2255_driver);
2857
2858 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002859 pr_err(KBUILD_MODNAME
2860 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002861
2862 dprintk(2, "s2255_init: done\n");
2863 return result;
2864}
2865
2866static void __exit usb_s2255_exit(void)
2867{
2868 usb_deregister(&s2255_driver);
2869}
2870
2871module_init(usb_s2255_init);
2872module_exit(usb_s2255_exit);
2873
2874MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2875MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2876MODULE_LICENSE("GPL");