blob: e6dc7fac5b01ee7be6b70d8235043e1d6e2f618a [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>
Dean Anderson3a67b5cc2010-04-08 23:52:20 -030055#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030056#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030057#include <linux/vmalloc.h>
58#include <linux/usb.h>
59
Dean Anderson85b85482010-04-08 23:51:17 -030060#define S2255_MAJOR_VERSION 1
61#define S2255_MINOR_VERSION 19
62#define S2255_RELEASE 0
63#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
64 S2255_MINOR_VERSION, \
65 S2255_RELEASE)
Dean Anderson38f993a2008-06-26 23:15:51 -030066#define FIRMWARE_FILE_NAME "f2255usb.bin"
67
Dean Anderson22b88d42008-08-29 15:33:19 -030068/* default JPEG quality */
69#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030070/* vendor request in */
71#define S2255_VR_IN 0
72/* vendor request out */
73#define S2255_VR_OUT 1
74/* firmware query */
75#define S2255_VR_FW 0x30
76/* USB endpoint number for configuring the device */
77#define S2255_CONFIG_EP 2
78/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030079#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030080/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030081#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030082#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030083#define S2255_SETMODE_TIMEOUT 500
Dean Anderson4de39f52010-03-03 19:39:19 -030084#define S2255_VIDSTATUS_TIMEOUT 350
Dean Anderson3fa00602010-03-04 20:47:33 -030085#define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
86#define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
87#define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
88#define S2255_RESPONSE_FW cpu_to_le32(0x10)
89#define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
Dean Anderson14d96262008-08-25 13:58:55 -030090#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030091#define MAX_CHANNELS 4
Dean Anderson38f993a2008-06-26 23:15:51 -030092#define SYS_FRAMES 4
93/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030094#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
95#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030096#define LINE_SZ_4CIFS_NTSC 640
97#define LINE_SZ_2CIFS_NTSC 640
98#define LINE_SZ_1CIFS_NTSC 320
99#define LINE_SZ_4CIFS_PAL 704
100#define LINE_SZ_2CIFS_PAL 704
101#define LINE_SZ_1CIFS_PAL 352
102#define NUM_LINES_4CIFS_NTSC 240
103#define NUM_LINES_2CIFS_NTSC 240
104#define NUM_LINES_1CIFS_NTSC 240
105#define NUM_LINES_4CIFS_PAL 288
106#define NUM_LINES_2CIFS_PAL 288
107#define NUM_LINES_1CIFS_PAL 288
108#define LINE_SZ_DEF 640
109#define NUM_LINES_DEF 240
110
111
112/* predefined settings */
113#define FORMAT_NTSC 1
114#define FORMAT_PAL 2
115
116#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
117#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
118#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
Dean Anderson7d853532009-05-15 14:32:04 -0300119/* SCALE_4CIFSI is the 2 fields interpolated into one */
120#define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
Dean Anderson38f993a2008-06-26 23:15:51 -0300121
122#define COLOR_YUVPL 1 /* YUV planar */
123#define COLOR_YUVPK 2 /* YUV packed */
124#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300125#define COLOR_JPG 5 /* JPEG */
Dean Anderson38f993a2008-06-26 23:15:51 -0300126
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300127#define MASK_COLOR 0x000000ff
128#define MASK_JPG_QUALITY 0x0000ff00
129#define MASK_INPUT_TYPE 0x000f0000
Dean Anderson38f993a2008-06-26 23:15:51 -0300130/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
131#define FDEC_1 1 /* capture every frame. default */
132#define FDEC_2 2 /* capture every 2nd frame */
133#define FDEC_3 3 /* capture every 3rd frame */
134#define FDEC_5 5 /* capture every 5th frame */
135
136/*-------------------------------------------------------
137 * Default mode parameters.
138 *-------------------------------------------------------*/
139#define DEF_SCALE SCALE_4CIFS
140#define DEF_COLOR COLOR_YUVPL
141#define DEF_FDEC FDEC_1
142#define DEF_BRIGHT 0
143#define DEF_CONTRAST 0x5c
144#define DEF_SATURATION 0x80
145#define DEF_HUE 0
146
147/* usb config commands */
Dean Anderson3fa00602010-03-04 20:47:33 -0300148#define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
149#define CMD_2255 cpu_to_le32(0xc2255000)
150#define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
151#define CMD_START cpu_to_le32((CMD_2255 | 0x20))
152#define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
153#define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
Dean Anderson38f993a2008-06-26 23:15:51 -0300154
155struct s2255_mode {
156 u32 format; /* input video format (NTSC, PAL) */
157 u32 scale; /* output video scale */
158 u32 color; /* output video color format */
159 u32 fdec; /* frame decimation */
160 u32 bright; /* brightness */
161 u32 contrast; /* contrast */
162 u32 saturation; /* saturation */
163 u32 hue; /* hue (NTSC only)*/
164 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
165 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
166 u32 restart; /* if DSP requires restart */
167};
168
Dean Anderson14d96262008-08-25 13:58:55 -0300169
170#define S2255_READ_IDLE 0
171#define S2255_READ_FRAME 1
172
Dean Anderson38f993a2008-06-26 23:15:51 -0300173/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300174struct s2255_framei {
175 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300176 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300177 void *lpvbits; /* image data */
178 unsigned long cur_size; /* current data copied to it */
179};
180
181/* image buffer structure */
182struct s2255_bufferi {
183 unsigned long dwFrames; /* number of frames in buffer */
184 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
185};
186
187#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
188 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300189 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300190
191struct s2255_dmaqueue {
192 struct list_head active;
Dean Anderson38f993a2008-06-26 23:15:51 -0300193 struct s2255_dev *dev;
194 int channel;
195};
196
197/* for firmware loading, fw_state */
198#define S2255_FW_NOTLOADED 0
199#define S2255_FW_LOADED_DSPWAIT 1
200#define S2255_FW_SUCCESS 2
201#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300202#define S2255_FW_DISCONNECTING 4
Harvey Harrisondeaf53e2008-11-15 01:10:14 -0300203#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
Dean Anderson14d96262008-08-25 13:58:55 -0300204/* 2255 read states */
205#define S2255_READ_IDLE 0
206#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300207struct s2255_fw {
208 int fw_loaded;
209 int fw_size;
210 struct urb *fw_urb;
211 atomic_t fw_state;
212 void *pfw_data;
213 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300214 const struct firmware *fw;
215};
216
217struct s2255_pipeinfo {
218 u32 max_transfer_size;
219 u32 cur_transfer_size;
220 u8 *transfer_buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -0300221 u32 state;
Dean Anderson38f993a2008-06-26 23:15:51 -0300222 void *stream_urb;
223 void *dev; /* back pointer to s2255_dev struct*/
224 u32 err_count;
Dean Anderson38f993a2008-06-26 23:15:51 -0300225 u32 idx;
Dean Anderson38f993a2008-06-26 23:15:51 -0300226};
227
228struct s2255_fmt; /*forward declaration */
229
230struct s2255_dev {
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300231 struct video_device vdev[MAX_CHANNELS];
Dean Anderson65c6edb2010-04-20 17:21:32 -0300232 struct v4l2_device v4l2_dev;
Dean Anderson3a67b5cc2010-04-08 23:52:20 -0300233 int channels; /* number of channels registered */
Dean Anderson38f993a2008-06-26 23:15:51 -0300234 int frames;
Dean Anderson38f993a2008-06-26 23:15:51 -0300235 struct mutex lock;
236 struct mutex open_lock;
237 int resources[MAX_CHANNELS];
238 struct usb_device *udev;
239 struct usb_interface *interface;
240 u8 read_endpoint;
241
242 struct s2255_dmaqueue vidq[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300243 struct timer_list timer;
244 struct s2255_fw *fw_data;
Dean Andersonab85c6a2010-04-08 23:39:12 -0300245 struct s2255_pipeinfo pipe;
246 struct s2255_bufferi buffer[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300247 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300248 /* jpeg compression */
249 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300250 /* capture parameters (for high quality mode full size) */
251 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300252 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
253 int cur_frame[MAX_CHANNELS];
254 int last_frame[MAX_CHANNELS];
255 u32 cc; /* current channel */
256 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300257 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300258 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300259 /* received packet size */
260 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300261 int bad_payload[MAX_CHANNELS];
262 unsigned long frame_count[MAX_CHANNELS];
263 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300264 /* if JPEG image */
265 int jpg_size[MAX_CHANNELS];
266 /* if channel configured to default state */
267 int chn_configured[MAX_CHANNELS];
268 wait_queue_head_t wait_setmode[MAX_CHANNELS];
269 int setmode_ready[MAX_CHANNELS];
Dean Anderson4de39f52010-03-03 19:39:19 -0300270 /* video status items */
271 int vidstatus[MAX_CHANNELS];
272 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
273 int vidstatus_ready[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300274 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300275 spinlock_t slock;
Dean Anderson4de39f52010-03-03 19:39:19 -0300276 /* dsp firmware version (f2255usb.bin) */
277 int dsp_fw_ver;
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300278 u16 pid; /* product id */
279 struct kref kref;
Dean Anderson38f993a2008-06-26 23:15:51 -0300280};
Dean Anderson65c6edb2010-04-20 17:21:32 -0300281
282/* kref will be removed soon */
283#define to_s2255_dev_from_kref(d) container_of(d, struct s2255_dev, kref)
284
285static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
286{
287 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
288}
Dean Anderson38f993a2008-06-26 23:15:51 -0300289
290struct s2255_fmt {
291 char *name;
292 u32 fourcc;
293 int depth;
294};
295
296/* buffer for one video frame */
297struct s2255_buffer {
298 /* common v4l buffer stuff -- must be first */
299 struct videobuf_buffer vb;
300 const struct s2255_fmt *fmt;
301};
302
303struct s2255_fh {
304 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300305 const struct s2255_fmt *fmt;
306 unsigned int width;
307 unsigned int height;
308 struct videobuf_queue vb_vidq;
309 enum v4l2_buf_type type;
310 int channel;
311 /* mode below is the desired mode.
312 mode in s2255_dev is the current mode that was last set */
313 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300314 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300315};
316
Dean Andersonabce21f2009-04-23 16:04:41 -0300317/* current cypress EEPROM firmware version */
318#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson4de39f52010-03-03 19:39:19 -0300319/* current DSP FW version */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300320#define S2255_CUR_DSP_FWVER 8
Dean Anderson4de39f52010-03-03 19:39:19 -0300321/* Need DSP version 5+ for video status feature */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300322#define S2255_MIN_DSP_STATUS 5
323#define S2255_MIN_DSP_COLORFILTER 8
Dean Anderson38f993a2008-06-26 23:15:51 -0300324#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300325
326/* private V4L2 controls */
327
328/*
329 * The following chart displays how COLORFILTER should be set
330 * =========================================================
331 * = fourcc = COLORFILTER =
332 * = ===============================
333 * = = 0 = 1 =
334 * =========================================================
335 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
336 * = = s-video or = composite =
337 * = = B/W camera = input =
338 * =========================================================
339 * = other = color, svideo = color, =
340 * = = = composite =
341 * =========================================================
342 *
343 * Notes:
344 * channels 0-3 on 2255 are composite
345 * channels 0-1 on 2257 are composite, 2-3 are s-video
346 * If COLORFILTER is 0 with a composite color camera connected,
347 * the output will appear monochrome but hatching
348 * will occur.
349 * COLORFILTER is different from "color killer" and "color effects"
350 * for reasons above.
351 */
352#define S2255_V4L2_YC_ON 1
353#define S2255_V4L2_YC_OFF 0
354#define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
355
Dean Anderson38f993a2008-06-26 23:15:51 -0300356/* frame prefix size (sent once every frame) */
357#define PREFIX_SIZE 512
358
359/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300360static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300361
Dean Anderson38f993a2008-06-26 23:15:51 -0300362static int debug;
363static int *s2255_debug = &debug;
364
365static int s2255_start_readpipe(struct s2255_dev *dev);
366static void s2255_stop_readpipe(struct s2255_dev *dev);
367static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
368static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
369static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300370 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300371static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
372 struct s2255_mode *mode);
373static int s2255_board_shutdown(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300374static void s2255_fwload_start(struct s2255_dev *dev, int reset);
375static void s2255_destroy(struct kref *kref);
376static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
377 u16 index, u16 value, void *buf,
378 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300379
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300380/* dev_err macro with driver name */
381#define S2255_DRIVER_NAME "s2255"
382#define s2255_dev_err(dev, fmt, arg...) \
383 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
384
Dean Anderson38f993a2008-06-26 23:15:51 -0300385#define dprintk(level, fmt, arg...) \
386 do { \
387 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300388 printk(KERN_DEBUG S2255_DRIVER_NAME \
389 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300390 } \
391 } while (0)
392
Dean Anderson38f993a2008-06-26 23:15:51 -0300393static struct usb_driver s2255_driver;
394
Dean Anderson38f993a2008-06-26 23:15:51 -0300395/* Declare static vars that will be used as parameters */
396static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
397
398/* start video number */
399static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
400
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300401module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300402MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300403module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300404MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300405module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300406MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
407
408/* USB device table */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300409#define USB_SENSORAY_VID 0x1943
Dean Anderson38f993a2008-06-26 23:15:51 -0300410static struct usb_device_id s2255_table[] = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300411 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
412 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300413 { } /* Terminating entry */
414};
415MODULE_DEVICE_TABLE(usb, s2255_table);
416
Dean Anderson38f993a2008-06-26 23:15:51 -0300417#define BUFFER_TIMEOUT msecs_to_jiffies(400)
418
Dean Anderson38f993a2008-06-26 23:15:51 -0300419/* image formats. */
420static const struct s2255_fmt formats[] = {
421 {
422 .name = "4:2:2, planar, YUV422P",
423 .fourcc = V4L2_PIX_FMT_YUV422P,
424 .depth = 16
425
426 }, {
427 .name = "4:2:2, packed, YUYV",
428 .fourcc = V4L2_PIX_FMT_YUYV,
429 .depth = 16
430
431 }, {
432 .name = "4:2:2, packed, UYVY",
433 .fourcc = V4L2_PIX_FMT_UYVY,
434 .depth = 16
435 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300436 .name = "JPG",
437 .fourcc = V4L2_PIX_FMT_JPEG,
438 .depth = 24
439 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300440 .name = "8bpp GREY",
441 .fourcc = V4L2_PIX_FMT_GREY,
442 .depth = 8
443 }
444};
445
446static int norm_maxw(struct video_device *vdev)
447{
448 return (vdev->current_norm & V4L2_STD_NTSC) ?
449 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
450}
451
452static int norm_maxh(struct video_device *vdev)
453{
454 return (vdev->current_norm & V4L2_STD_NTSC) ?
455 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
456}
457
458static int norm_minw(struct video_device *vdev)
459{
460 return (vdev->current_norm & V4L2_STD_NTSC) ?
461 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
462}
463
464static int norm_minh(struct video_device *vdev)
465{
466 return (vdev->current_norm & V4L2_STD_NTSC) ?
467 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
468}
469
470
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300471/*
472 * TODO: fixme: move YUV reordering to hardware
473 * converts 2255 planar format to yuyv or uyvy
474 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300475static void planar422p_to_yuv_packed(const unsigned char *in,
476 unsigned char *out,
477 int width, int height,
478 int fmt)
479{
480 unsigned char *pY;
481 unsigned char *pCb;
482 unsigned char *pCr;
483 unsigned long size = height * width;
484 unsigned int i;
485 pY = (unsigned char *)in;
486 pCr = (unsigned char *)in + height * width;
487 pCb = (unsigned char *)in + height * width + (height * width / 2);
488 for (i = 0; i < size * 2; i += 4) {
489 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
490 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
491 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
492 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
493 }
494 return;
495}
496
Hans Verkuild45b9b82008-09-04 03:33:43 -0300497static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300498{
499 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
500 msleep(10);
501 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
502 return;
503}
Dean Anderson38f993a2008-06-26 23:15:51 -0300504
505/* kickstarts the firmware loading. from probe
506 */
507static void s2255_timer(unsigned long user_data)
508{
509 struct s2255_fw *data = (struct s2255_fw *)user_data;
Dean Anderson85b85482010-04-08 23:51:17 -0300510 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300511 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
512 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300513 atomic_set(&data->fw_state, S2255_FW_FAILED);
514 /* wake up anything waiting for the firmware */
515 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300516 return;
517 }
518}
519
Dean Anderson38f993a2008-06-26 23:15:51 -0300520
521/* this loads the firmware asynchronously.
522 Originally this was done synchroously in probe.
523 But it is better to load it asynchronously here than block
524 inside the probe function. Blocking inside probe affects boot time.
525 FW loading is triggered by the timer in the probe function
526*/
527static void s2255_fwchunk_complete(struct urb *urb)
528{
529 struct s2255_fw *data = urb->context;
530 struct usb_device *udev = urb->dev;
531 int len;
Dean Anderson85b85482010-04-08 23:51:17 -0300532 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300533 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300534 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300535 atomic_set(&data->fw_state, S2255_FW_FAILED);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300538 return;
539 }
540 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300541 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300542 atomic_set(&data->fw_state, S2255_FW_FAILED);
543 /* wake up anything waiting for the firmware */
544 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300545 return;
546 }
547#define CHUNK_SIZE 512
548 /* all USB transfers must be done with continuous kernel memory.
549 can't allocate more than 128k in current linux kernel, so
550 upload the firmware in chunks
551 */
552 if (data->fw_loaded < data->fw_size) {
553 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
554 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
555
556 if (len < CHUNK_SIZE)
557 memset(data->pfw_data, 0, CHUNK_SIZE);
558
559 dprintk(100, "completed len %d, loaded %d \n", len,
560 data->fw_loaded);
561
562 memcpy(data->pfw_data,
563 (char *) data->fw->data + data->fw_loaded, len);
564
565 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
566 data->pfw_data, CHUNK_SIZE,
567 s2255_fwchunk_complete, data);
568 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
569 dev_err(&udev->dev, "failed submit URB\n");
570 atomic_set(&data->fw_state, S2255_FW_FAILED);
571 /* wake up anything waiting for the firmware */
572 wake_up(&data->wait_fw);
573 return;
574 }
575 data->fw_loaded += len;
576 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300577 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson85b85482010-04-08 23:51:17 -0300578 dprintk(100, "%s: firmware upload complete\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300579 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300580 return;
581
582}
583
Dean Anderson14d96262008-08-25 13:58:55 -0300584static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300585{
586 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
587 struct s2255_buffer *buf;
588 unsigned long flags = 0;
589 int rc = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300590 spin_lock_irqsave(&dev->slock, flags);
Dean Anderson38f993a2008-06-26 23:15:51 -0300591 if (list_empty(&dma_q->active)) {
592 dprintk(1, "No active queue to serve\n");
593 rc = -1;
594 goto unlock;
595 }
596 buf = list_entry(dma_q->active.next,
597 struct s2255_buffer, vb.queue);
Dean Anderson38f993a2008-06-26 23:15:51 -0300598 list_del(&buf->vb.queue);
599 do_gettimeofday(&buf->vb.ts);
Dean Anderson14d96262008-08-25 13:58:55 -0300600 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300601 wake_up(&buf->vb.done);
Dean Anderson85b85482010-04-08 23:51:17 -0300602 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
Dean Anderson38f993a2008-06-26 23:15:51 -0300603unlock:
604 spin_unlock_irqrestore(&dev->slock, flags);
605 return 0;
606}
607
Dean Anderson38f993a2008-06-26 23:15:51 -0300608static const struct s2255_fmt *format_by_fourcc(int fourcc)
609{
610 unsigned int i;
Dean Anderson38f993a2008-06-26 23:15:51 -0300611 for (i = 0; i < ARRAY_SIZE(formats); i++) {
612 if (-1 == formats[i].fourcc)
613 continue;
614 if (formats[i].fourcc == fourcc)
615 return formats + i;
616 }
617 return NULL;
618}
619
Dean Anderson38f993a2008-06-26 23:15:51 -0300620/* video buffer vmalloc implementation based partly on VIVI driver which is
621 * Copyright (c) 2006 by
622 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
623 * Ted Walther <ted--a.t--enumera.com>
624 * John Sokol <sokol--a.t--videotechnology.com>
625 * http://v4l.videotechnology.com/
626 *
627 */
628static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300629 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300630{
631 int pos = 0;
632 struct timeval ts;
633 const char *tmpbuf;
634 char *vbuf = videobuf_to_vmalloc(&buf->vb);
635 unsigned long last_frame;
636 struct s2255_framei *frm;
637
638 if (!vbuf)
639 return;
640
641 last_frame = dev->last_frame[chn];
642 if (last_frame != -1) {
643 frm = &dev->buffer[chn].frame[last_frame];
644 tmpbuf =
645 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
646 switch (buf->fmt->fourcc) {
647 case V4L2_PIX_FMT_YUYV:
648 case V4L2_PIX_FMT_UYVY:
649 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
650 vbuf, buf->vb.width,
651 buf->vb.height,
652 buf->fmt->fourcc);
653 break;
654 case V4L2_PIX_FMT_GREY:
655 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
656 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300657 case V4L2_PIX_FMT_JPEG:
658 buf->vb.size = jpgsize;
659 memcpy(vbuf, tmpbuf, buf->vb.size);
660 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300661 case V4L2_PIX_FMT_YUV422P:
662 memcpy(vbuf, tmpbuf,
663 buf->vb.width * buf->vb.height * 2);
664 break;
665 default:
666 printk(KERN_DEBUG "s2255: unknown format?\n");
667 }
668 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300669 } else {
670 printk(KERN_ERR "s2255: =======no frame\n");
671 return;
672
673 }
674 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
675 (unsigned long)vbuf, pos);
676 /* tell v4l buffer was filled */
677
Dean Andersona1c45302008-09-09 12:29:56 -0300678 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300679 do_gettimeofday(&ts);
680 buf->vb.ts = ts;
681 buf->vb.state = VIDEOBUF_DONE;
682}
683
684
685/* ------------------------------------------------------------------
686 Videobuf operations
687 ------------------------------------------------------------------*/
688
689static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
690 unsigned int *size)
691{
692 struct s2255_fh *fh = vq->priv_data;
693
694 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
695
696 if (0 == *count)
697 *count = S2255_DEF_BUFS;
698
Andreas Bombedab7e312010-03-21 16:02:45 -0300699 if (*size * *count > vid_limit * 1024 * 1024)
700 *count = (vid_limit * 1024 * 1024) / *size;
Dean Anderson38f993a2008-06-26 23:15:51 -0300701
702 return 0;
703}
704
705static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
706{
707 dprintk(4, "%s\n", __func__);
708
Dean Anderson38f993a2008-06-26 23:15:51 -0300709 videobuf_vmalloc_free(&buf->vb);
710 buf->vb.state = VIDEOBUF_NEEDS_INIT;
711}
712
713static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
714 enum v4l2_field field)
715{
716 struct s2255_fh *fh = vq->priv_data;
717 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
718 int rc;
719 dprintk(4, "%s, field=%d\n", __func__, field);
720 if (fh->fmt == NULL)
721 return -EINVAL;
722
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300723 if ((fh->width < norm_minw(&fh->dev->vdev[fh->channel])) ||
724 (fh->width > norm_maxw(&fh->dev->vdev[fh->channel])) ||
725 (fh->height < norm_minh(&fh->dev->vdev[fh->channel])) ||
726 (fh->height > norm_maxh(&fh->dev->vdev[fh->channel]))) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300727 dprintk(4, "invalid buffer prepare\n");
728 return -EINVAL;
729 }
730
731 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
732
733 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
734 dprintk(4, "invalid buffer prepare\n");
735 return -EINVAL;
736 }
737
738 buf->fmt = fh->fmt;
739 buf->vb.width = fh->width;
740 buf->vb.height = fh->height;
741 buf->vb.field = field;
742
Dean Anderson38f993a2008-06-26 23:15:51 -0300743 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
744 rc = videobuf_iolock(vq, &buf->vb, NULL);
745 if (rc < 0)
746 goto fail;
747 }
748
749 buf->vb.state = VIDEOBUF_PREPARED;
750 return 0;
751fail:
752 free_buffer(vq, buf);
753 return rc;
754}
755
756static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
757{
758 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
759 struct s2255_fh *fh = vq->priv_data;
760 struct s2255_dev *dev = fh->dev;
761 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300762 dprintk(1, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300763 buf->vb.state = VIDEOBUF_QUEUED;
764 list_add_tail(&buf->vb.queue, &vidq->active);
765}
766
767static void buffer_release(struct videobuf_queue *vq,
768 struct videobuf_buffer *vb)
769{
770 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
771 struct s2255_fh *fh = vq->priv_data;
772 dprintk(4, "%s %d\n", __func__, fh->channel);
773 free_buffer(vq, buf);
774}
775
776static struct videobuf_queue_ops s2255_video_qops = {
777 .buf_setup = buffer_setup,
778 .buf_prepare = buffer_prepare,
779 .buf_queue = buffer_queue,
780 .buf_release = buffer_release,
781};
782
783
784static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
785{
786 /* is it free? */
787 mutex_lock(&dev->lock);
788 if (dev->resources[fh->channel]) {
789 /* no, someone else uses it */
790 mutex_unlock(&dev->lock);
791 return 0;
792 }
793 /* it's free, grab it */
794 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300795 fh->resources[fh->channel] = 1;
796 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300797 mutex_unlock(&dev->lock);
798 return 1;
799}
800
801static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
802{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300803 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300804}
805
Dean Andersonf78d92c2008-07-22 14:43:27 -0300806static int res_check(struct s2255_fh *fh)
807{
808 return fh->resources[fh->channel];
809}
810
811
Dean Anderson38f993a2008-06-26 23:15:51 -0300812static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
813{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300814 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300815 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300816 fh->resources[fh->channel] = 0;
817 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300818 dprintk(1, "res: put\n");
819}
820
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300821static int vidioc_querymenu(struct file *file, void *priv,
822 struct v4l2_querymenu *qmenu)
823{
824 static const char *colorfilter[] = {
825 "Off",
826 "On",
827 NULL
828 };
829 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
830 int i;
831 const char **menu_items = colorfilter;
832 for (i = 0; i < qmenu->index && menu_items[i]; i++)
833 ; /* do nothing (from v4l2-common.c) */
834 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
835 return -EINVAL;
836 strlcpy(qmenu->name, menu_items[qmenu->index],
837 sizeof(qmenu->name));
838 return 0;
839 }
840 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
841}
842
Dean Anderson38f993a2008-06-26 23:15:51 -0300843static int vidioc_querycap(struct file *file, void *priv,
844 struct v4l2_capability *cap)
845{
846 struct s2255_fh *fh = file->private_data;
847 struct s2255_dev *dev = fh->dev;
848 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
849 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300850 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300851 cap->version = S2255_VERSION;
852 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
853 return 0;
854}
855
856static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
857 struct v4l2_fmtdesc *f)
858{
859 int index = 0;
860 if (f)
861 index = f->index;
862
863 if (index >= ARRAY_SIZE(formats))
864 return -EINVAL;
865
866 dprintk(4, "name %s\n", formats[index].name);
867 strlcpy(f->description, formats[index].name, sizeof(f->description));
868 f->pixelformat = formats[index].fourcc;
869 return 0;
870}
871
872static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
873 struct v4l2_format *f)
874{
875 struct s2255_fh *fh = priv;
876
877 f->fmt.pix.width = fh->width;
878 f->fmt.pix.height = fh->height;
879 f->fmt.pix.field = fh->vb_vidq.field;
880 f->fmt.pix.pixelformat = fh->fmt->fourcc;
881 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
882 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300883 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300884}
885
886static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
887 struct v4l2_format *f)
888{
889 const struct s2255_fmt *fmt;
890 enum v4l2_field field;
891 int b_any_field = 0;
892 struct s2255_fh *fh = priv;
893 struct s2255_dev *dev = fh->dev;
894 int is_ntsc;
895
896 is_ntsc =
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300897 (dev->vdev[fh->channel].current_norm & V4L2_STD_NTSC) ? 1 : 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300898
899 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
900
901 if (fmt == NULL)
902 return -EINVAL;
903
904 field = f->fmt.pix.field;
905 if (field == V4L2_FIELD_ANY)
906 b_any_field = 1;
907
Dean Anderson85b85482010-04-08 23:51:17 -0300908 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
909 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
Dean Anderson38f993a2008-06-26 23:15:51 -0300910 if (is_ntsc) {
911 /* NTSC */
912 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
913 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
914 if (b_any_field) {
915 field = V4L2_FIELD_SEQ_TB;
916 } else if (!((field == V4L2_FIELD_INTERLACED) ||
917 (field == V4L2_FIELD_SEQ_TB) ||
918 (field == V4L2_FIELD_INTERLACED_TB))) {
919 dprintk(1, "unsupported field setting\n");
920 return -EINVAL;
921 }
922 } else {
923 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
924 if (b_any_field) {
925 field = V4L2_FIELD_TOP;
926 } else if (!((field == V4L2_FIELD_TOP) ||
927 (field == V4L2_FIELD_BOTTOM))) {
928 dprintk(1, "unsupported field setting\n");
929 return -EINVAL;
930 }
931
932 }
933 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
934 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
935 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
936 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
937 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
938 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
939 else
940 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
941 } else {
942 /* PAL */
943 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
944 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
945 if (b_any_field) {
946 field = V4L2_FIELD_SEQ_TB;
947 } else if (!((field == V4L2_FIELD_INTERLACED) ||
948 (field == V4L2_FIELD_SEQ_TB) ||
949 (field == V4L2_FIELD_INTERLACED_TB))) {
950 dprintk(1, "unsupported field setting\n");
951 return -EINVAL;
952 }
953 } else {
954 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
955 if (b_any_field) {
956 field = V4L2_FIELD_TOP;
957 } else if (!((field == V4L2_FIELD_TOP) ||
958 (field == V4L2_FIELD_BOTTOM))) {
959 dprintk(1, "unsupported field setting\n");
960 return -EINVAL;
961 }
962 }
963 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300964 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
965 field = V4L2_FIELD_SEQ_TB;
966 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300967 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
968 field = V4L2_FIELD_TOP;
969 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300970 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
971 field = V4L2_FIELD_TOP;
972 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300973 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
974 field = V4L2_FIELD_TOP;
975 }
976 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300977 f->fmt.pix.field = field;
978 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
979 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson85b85482010-04-08 23:51:17 -0300980 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
981 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
Dean Anderson38f993a2008-06-26 23:15:51 -0300982 return 0;
983}
984
985static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
986 struct v4l2_format *f)
987{
988 struct s2255_fh *fh = priv;
989 const struct s2255_fmt *fmt;
990 struct videobuf_queue *q = &fh->vb_vidq;
991 int ret;
992 int norm;
993
994 ret = vidioc_try_fmt_vid_cap(file, fh, f);
995
996 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300997 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300998
999 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1000
1001 if (fmt == NULL)
1002 return -EINVAL;
1003
1004 mutex_lock(&q->vb_lock);
1005
1006 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1007 dprintk(1, "queue busy\n");
1008 ret = -EBUSY;
1009 goto out_s_fmt;
1010 }
1011
1012 if (res_locked(fh->dev, fh)) {
Dean Anderson85b85482010-04-08 23:51:17 -03001013 dprintk(1, "%s: channel busy\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001014 ret = -EBUSY;
1015 goto out_s_fmt;
1016 }
1017
1018 fh->fmt = fmt;
1019 fh->width = f->fmt.pix.width;
1020 fh->height = f->fmt.pix.height;
1021 fh->vb_vidq.field = f->fmt.pix.field;
1022 fh->type = f->type;
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001023 norm = norm_minw(&fh->dev->vdev[fh->channel]);
1024 if (fh->width > norm_minw(&fh->dev->vdev[fh->channel])) {
1025 if (fh->height > norm_minh(&fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001026 if (fh->dev->cap_parm[fh->channel].capturemode &
Dean Anderson85b85482010-04-08 23:51:17 -03001027 V4L2_MODE_HIGHQUALITY)
Dean Anderson7d853532009-05-15 14:32:04 -03001028 fh->mode.scale = SCALE_4CIFSI;
Dean Anderson85b85482010-04-08 23:51:17 -03001029 else
Dean Anderson7d853532009-05-15 14:32:04 -03001030 fh->mode.scale = SCALE_4CIFS;
Dean Anderson7d853532009-05-15 14:32:04 -03001031 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001032 fh->mode.scale = SCALE_2CIFS;
1033
1034 } else {
1035 fh->mode.scale = SCALE_1CIFS;
1036 }
1037
1038 /* color mode */
1039 switch (fh->fmt->fourcc) {
1040 case V4L2_PIX_FMT_GREY:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001041 fh->mode.color &= ~MASK_COLOR;
1042 fh->mode.color |= COLOR_Y8;
Dean Anderson38f993a2008-06-26 23:15:51 -03001043 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001044 case V4L2_PIX_FMT_JPEG:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001045 fh->mode.color &= ~MASK_COLOR;
1046 fh->mode.color |= COLOR_JPG;
1047 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001048 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001049 case V4L2_PIX_FMT_YUV422P:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001050 fh->mode.color &= ~MASK_COLOR;
1051 fh->mode.color |= COLOR_YUVPL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001052 break;
1053 case V4L2_PIX_FMT_YUYV:
1054 case V4L2_PIX_FMT_UYVY:
1055 default:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001056 fh->mode.color &= ~MASK_COLOR;
1057 fh->mode.color |= COLOR_YUVPK;
Dean Anderson38f993a2008-06-26 23:15:51 -03001058 break;
1059 }
1060 ret = 0;
1061out_s_fmt:
1062 mutex_unlock(&q->vb_lock);
1063 return ret;
1064}
1065
1066static int vidioc_reqbufs(struct file *file, void *priv,
1067 struct v4l2_requestbuffers *p)
1068{
1069 int rc;
1070 struct s2255_fh *fh = priv;
1071 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1072 return rc;
1073}
1074
1075static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1076{
1077 int rc;
1078 struct s2255_fh *fh = priv;
1079 rc = videobuf_querybuf(&fh->vb_vidq, p);
1080 return rc;
1081}
1082
1083static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1084{
1085 int rc;
1086 struct s2255_fh *fh = priv;
1087 rc = videobuf_qbuf(&fh->vb_vidq, p);
1088 return rc;
1089}
1090
1091static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1092{
1093 int rc;
1094 struct s2255_fh *fh = priv;
1095 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1096 return rc;
1097}
1098
1099#ifdef CONFIG_VIDEO_V4L1_COMPAT
1100static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1101{
1102 struct s2255_fh *fh = priv;
1103
1104 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1105}
1106#endif
1107
1108/* write to the configuration pipe, synchronously */
1109static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1110 int size)
1111{
1112 int pipe;
1113 int done;
1114 long retval = -1;
1115 if (udev) {
1116 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1117 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1118 }
1119 return retval;
1120}
1121
1122static u32 get_transfer_size(struct s2255_mode *mode)
1123{
1124 int linesPerFrame = LINE_SZ_DEF;
1125 int pixelsPerLine = NUM_LINES_DEF;
1126 u32 outImageSize;
1127 u32 usbInSize;
1128 unsigned int mask_mult;
1129
1130 if (mode == NULL)
1131 return 0;
1132
1133 if (mode->format == FORMAT_NTSC) {
1134 switch (mode->scale) {
1135 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001136 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001137 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1138 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1139 break;
1140 case SCALE_2CIFS:
1141 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1142 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1143 break;
1144 case SCALE_1CIFS:
1145 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1146 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1147 break;
1148 default:
1149 break;
1150 }
1151 } else if (mode->format == FORMAT_PAL) {
1152 switch (mode->scale) {
1153 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001154 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001155 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1156 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1157 break;
1158 case SCALE_2CIFS:
1159 linesPerFrame = NUM_LINES_2CIFS_PAL;
1160 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1161 break;
1162 case SCALE_1CIFS:
1163 linesPerFrame = NUM_LINES_1CIFS_PAL;
1164 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1165 break;
1166 default:
1167 break;
1168 }
1169 }
1170 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001171 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001172 /* 2 bytes/pixel if not monochrome */
1173 outImageSize *= 2;
1174 }
1175
1176 /* total bytes to send including prefix and 4K padding;
1177 must be a multiple of USB_READ_SIZE */
1178 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1179 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1180 /* if size not a multiple of USB_READ_SIZE */
1181 if (usbInSize & ~mask_mult)
1182 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1183 return usbInSize;
1184}
1185
Dean Anderson85b85482010-04-08 23:51:17 -03001186static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
Dean Anderson38f993a2008-06-26 23:15:51 -03001187{
1188 struct device *dev = &sdev->udev->dev;
1189 dev_info(dev, "------------------------------------------------\n");
Dean Anderson85b85482010-04-08 23:51:17 -03001190 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1191 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
Dean Anderson38f993a2008-06-26 23:15:51 -03001192 dev_info(dev, "bright: 0x%x\n", mode->bright);
Dean Anderson38f993a2008-06-26 23:15:51 -03001193 dev_info(dev, "------------------------------------------------\n");
1194}
1195
1196/*
1197 * set mode is the function which controls the DSP.
1198 * the restart parameter in struct s2255_mode should be set whenever
1199 * the image size could change via color format, video system or image
1200 * size.
1201 * When the restart parameter is set, we sleep for ONE frame to allow the
1202 * DSP time to get the new frame
1203 */
1204static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1205 struct s2255_mode *mode)
1206{
1207 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001208 __le32 *buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -03001209 unsigned long chn_rev;
Dean Anderson14d96262008-08-25 13:58:55 -03001210 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001211 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001212 dprintk(3, "%s channel %lu\n", __func__, chn);
Dean Anderson22b88d42008-08-29 15:33:19 -03001213 /* if JPEG, set the quality */
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001214 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1215 mode->color &= ~MASK_COLOR;
1216 mode->color |= COLOR_JPG;
1217 mode->color &= ~MASK_JPG_QUALITY;
1218 mode->color |= (dev->jc[chn].quality << 8);
1219 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001220 /* save the mode */
1221 dev->mode[chn] = *mode;
1222 dev->req_image_size[chn] = get_transfer_size(mode);
Dean Anderson85b85482010-04-08 23:51:17 -03001223 dprintk(1, "%s: reqsize %ld\n", __func__, dev->req_image_size[chn]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001224 buffer = kzalloc(512, GFP_KERNEL);
1225 if (buffer == NULL) {
1226 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001227 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001228 return -ENOMEM;
1229 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001230 /* set the mode */
1231 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001232 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001233 buffer[2] = CMD_SET_MODE;
1234 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001235 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001236 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1237 if (debug)
Dean Anderson85b85482010-04-08 23:51:17 -03001238 s2255_print_cfg(dev, mode);
Dean Anderson38f993a2008-06-26 23:15:51 -03001239 kfree(buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03001240 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001241 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001242 wait_event_timeout(dev->wait_setmode[chn],
1243 (dev->setmode_ready[chn] != 0),
1244 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1245 if (dev->setmode_ready[chn] != 1) {
1246 printk(KERN_DEBUG "s2255: no set mode response\n");
1247 res = -EFAULT;
1248 }
1249 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001250 /* clear the restart flag */
1251 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001252 mutex_unlock(&dev->lock);
Dean Anderson85b85482010-04-08 23:51:17 -03001253 dprintk(1, "%s chn %lu, result: %d\n", __func__, chn, res);
Dean Anderson38f993a2008-06-26 23:15:51 -03001254 return res;
1255}
1256
Dean Anderson4de39f52010-03-03 19:39:19 -03001257static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1258 u32 *pstatus)
1259{
1260 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001261 __le32 *buffer;
Dean Anderson4de39f52010-03-03 19:39:19 -03001262 u32 chn_rev;
1263 mutex_lock(&dev->lock);
1264 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001265 dprintk(4, "%s chan %lu\n", __func__, chn);
Dean Anderson4de39f52010-03-03 19:39:19 -03001266 buffer = kzalloc(512, GFP_KERNEL);
1267 if (buffer == NULL) {
1268 dev_err(&dev->udev->dev, "out of mem\n");
1269 mutex_unlock(&dev->lock);
1270 return -ENOMEM;
1271 }
1272 /* form the get vid status command */
1273 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001274 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson4de39f52010-03-03 19:39:19 -03001275 buffer[2] = CMD_STATUS;
1276 *pstatus = 0;
1277 dev->vidstatus_ready[chn] = 0;
1278 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1279 kfree(buffer);
1280 wait_event_timeout(dev->wait_vidstatus[chn],
1281 (dev->vidstatus_ready[chn] != 0),
1282 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1283 if (dev->vidstatus_ready[chn] != 1) {
1284 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1285 res = -EFAULT;
1286 }
1287 *pstatus = dev->vidstatus[chn];
1288 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1289 mutex_unlock(&dev->lock);
1290 return res;
1291}
1292
Dean Anderson38f993a2008-06-26 23:15:51 -03001293static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1294{
1295 int res;
1296 struct s2255_fh *fh = priv;
1297 struct s2255_dev *dev = fh->dev;
1298 struct s2255_mode *new_mode;
1299 struct s2255_mode *old_mode;
1300 int chn;
1301 int j;
1302 dprintk(4, "%s\n", __func__);
1303 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1304 dev_err(&dev->udev->dev, "invalid fh type0\n");
1305 return -EINVAL;
1306 }
1307 if (i != fh->type) {
1308 dev_err(&dev->udev->dev, "invalid fh type1\n");
1309 return -EINVAL;
1310 }
1311
1312 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001313 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001314 return -EBUSY;
1315 }
1316
1317 /* send a set mode command everytime with restart.
1318 in case we switch resolutions or other parameters */
1319 chn = fh->channel;
1320 new_mode = &fh->mode;
1321 old_mode = &fh->dev->mode[chn];
1322
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001323 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
Dean Anderson38f993a2008-06-26 23:15:51 -03001324 new_mode->restart = 1;
1325 else if (new_mode->scale != old_mode->scale)
1326 new_mode->restart = 1;
1327 else if (new_mode->format != old_mode->format)
1328 new_mode->restart = 1;
1329
1330 s2255_set_mode(dev, chn, new_mode);
1331 new_mode->restart = 0;
1332 *old_mode = *new_mode;
1333 dev->cur_fmt[chn] = fh->fmt;
Dean Anderson38f993a2008-06-26 23:15:51 -03001334 dev->last_frame[chn] = -1;
1335 dev->bad_payload[chn] = 0;
1336 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001337 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001338 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001339 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001340 dev->buffer[chn].frame[j].cur_size = 0;
1341 }
1342 res = videobuf_streamon(&fh->vb_vidq);
1343 if (res == 0) {
1344 s2255_start_acquire(dev, chn);
1345 dev->b_acquire[chn] = 1;
1346 } else {
1347 res_free(dev, fh);
1348 }
1349 return res;
1350}
1351
1352static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1353{
Dean Anderson38f993a2008-06-26 23:15:51 -03001354 struct s2255_fh *fh = priv;
1355 struct s2255_dev *dev = fh->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001356 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1357 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1358 printk(KERN_ERR "invalid fh type0\n");
1359 return -EINVAL;
1360 }
1361 if (i != fh->type) {
1362 printk(KERN_ERR "invalid type i\n");
1363 return -EINVAL;
1364 }
1365 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001366 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001367 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001368 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001369}
1370
1371static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1372{
1373 struct s2255_fh *fh = priv;
1374 struct s2255_mode *mode;
1375 struct videobuf_queue *q = &fh->vb_vidq;
1376 int ret = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001377 mutex_lock(&q->vb_lock);
1378 if (videobuf_queue_is_busy(q)) {
1379 dprintk(1, "queue busy\n");
1380 ret = -EBUSY;
1381 goto out_s_std;
1382 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001383 if (res_locked(fh->dev, fh)) {
1384 dprintk(1, "can't change standard after started\n");
1385 ret = -EBUSY;
1386 goto out_s_std;
1387 }
1388 mode = &fh->mode;
Dean Anderson38f993a2008-06-26 23:15:51 -03001389 if (*i & V4L2_STD_NTSC) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001390 dprintk(4, "%s NTSC\n", __func__);
1391 /* if changing format, reset frame decimation/intervals */
1392 if (mode->format != FORMAT_NTSC) {
1393 mode->format = FORMAT_NTSC;
1394 mode->fdec = FDEC_1;
1395 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001396 } else if (*i & V4L2_STD_PAL) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001397 dprintk(4, "%s PAL\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001398 mode->format = FORMAT_PAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001399 if (mode->format != FORMAT_PAL) {
1400 mode->format = FORMAT_PAL;
1401 mode->fdec = FDEC_1;
1402 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001403 } else {
1404 ret = -EINVAL;
1405 }
1406out_s_std:
1407 mutex_unlock(&q->vb_lock);
1408 return ret;
1409}
1410
1411/* Sensoray 2255 is a multiple channel capture device.
1412 It does not have a "crossbar" of inputs.
1413 We use one V4L device per channel. The user must
1414 be aware that certain combinations are not allowed.
1415 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1416 at once in color(you can do full fps on 4 channels with greyscale.
1417*/
1418static int vidioc_enum_input(struct file *file, void *priv,
1419 struct v4l2_input *inp)
1420{
Dean Anderson4de39f52010-03-03 19:39:19 -03001421 struct s2255_fh *fh = priv;
1422 struct s2255_dev *dev = fh->dev;
1423 u32 status = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001424 if (inp->index != 0)
1425 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001426 inp->type = V4L2_INPUT_TYPE_CAMERA;
1427 inp->std = S2255_NORMS;
Dean Anderson4de39f52010-03-03 19:39:19 -03001428 inp->status = 0;
1429 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1430 int rc;
1431 rc = s2255_cmd_status(dev, fh->channel, &status);
1432 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1433 if (rc == 0)
1434 inp->status = (status & 0x01) ? 0
1435 : V4L2_IN_ST_NO_SIGNAL;
1436 }
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001437 switch (dev->pid) {
1438 case 0x2255:
1439 default:
1440 strlcpy(inp->name, "Composite", sizeof(inp->name));
1441 break;
1442 case 0x2257:
1443 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1444 sizeof(inp->name));
1445 break;
1446 }
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001447 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001448}
1449
1450static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1451{
1452 *i = 0;
1453 return 0;
1454}
1455static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1456{
1457 if (i > 0)
1458 return -EINVAL;
1459 return 0;
1460}
1461
1462/* --- controls ---------------------------------------------- */
1463static int vidioc_queryctrl(struct file *file, void *priv,
1464 struct v4l2_queryctrl *qc)
1465{
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001466 struct s2255_fh *fh = priv;
1467 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001468 switch (qc->id) {
1469 case V4L2_CID_BRIGHTNESS:
1470 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1471 break;
1472 case V4L2_CID_CONTRAST:
1473 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1474 break;
1475 case V4L2_CID_SATURATION:
1476 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1477 break;
1478 case V4L2_CID_HUE:
1479 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1480 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001481 case V4L2_CID_PRIVATE_COLORFILTER:
1482 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1483 return -EINVAL;
1484 if ((dev->pid == 0x2257) && (fh->channel > 1))
1485 return -EINVAL;
1486 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1487 qc->type = V4L2_CTRL_TYPE_MENU;
1488 qc->minimum = 0;
1489 qc->maximum = 1;
1490 qc->step = 1;
1491 qc->default_value = 1;
1492 qc->flags = 0;
1493 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001494 default:
1495 return -EINVAL;
1496 }
1497 dprintk(4, "%s, id %d\n", __func__, qc->id);
1498 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001499}
1500
1501static int vidioc_g_ctrl(struct file *file, void *priv,
1502 struct v4l2_control *ctrl)
1503{
Dean Anderson2e70db92010-03-05 14:29:09 -03001504 struct s2255_fh *fh = priv;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001505 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001506 switch (ctrl->id) {
1507 case V4L2_CID_BRIGHTNESS:
1508 ctrl->value = fh->mode.bright;
1509 break;
1510 case V4L2_CID_CONTRAST:
1511 ctrl->value = fh->mode.contrast;
1512 break;
1513 case V4L2_CID_SATURATION:
1514 ctrl->value = fh->mode.saturation;
1515 break;
1516 case V4L2_CID_HUE:
1517 ctrl->value = fh->mode.hue;
1518 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001519 case V4L2_CID_PRIVATE_COLORFILTER:
1520 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1521 return -EINVAL;
1522 if ((dev->pid == 0x2257) && (fh->channel > 1))
1523 return -EINVAL;
1524 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1525 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001526 default:
1527 return -EINVAL;
1528 }
1529 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1530 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001531}
1532
1533static int vidioc_s_ctrl(struct file *file, void *priv,
1534 struct v4l2_control *ctrl)
1535{
Dean Anderson38f993a2008-06-26 23:15:51 -03001536 struct s2255_fh *fh = priv;
1537 struct s2255_dev *dev = fh->dev;
1538 struct s2255_mode *mode;
1539 mode = &fh->mode;
Dean Anderson2e70db92010-03-05 14:29:09 -03001540 dprintk(4, "%s\n", __func__);
1541 /* update the mode to the corresponding value */
1542 switch (ctrl->id) {
1543 case V4L2_CID_BRIGHTNESS:
1544 mode->bright = ctrl->value;
1545 break;
1546 case V4L2_CID_CONTRAST:
1547 mode->contrast = ctrl->value;
1548 break;
1549 case V4L2_CID_HUE:
1550 mode->hue = ctrl->value;
1551 break;
1552 case V4L2_CID_SATURATION:
1553 mode->saturation = ctrl->value;
1554 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001555 case V4L2_CID_PRIVATE_COLORFILTER:
1556 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1557 return -EINVAL;
1558 if ((dev->pid == 0x2257) && (fh->channel > 1))
1559 return -EINVAL;
1560 mode->color &= ~MASK_INPUT_TYPE;
1561 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1562 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001563 default:
1564 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001565 }
Dean Anderson2e70db92010-03-05 14:29:09 -03001566 mode->restart = 0;
1567 /* set mode here. Note: stream does not need restarted.
1568 some V4L programs restart stream unnecessarily
1569 after a s_crtl.
1570 */
1571 s2255_set_mode(dev, fh->channel, mode);
1572 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001573}
1574
Dean Anderson22b88d42008-08-29 15:33:19 -03001575static int vidioc_g_jpegcomp(struct file *file, void *priv,
1576 struct v4l2_jpegcompression *jc)
1577{
1578 struct s2255_fh *fh = priv;
1579 struct s2255_dev *dev = fh->dev;
1580 *jc = dev->jc[fh->channel];
Dean Anderson85b85482010-04-08 23:51:17 -03001581 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001582 return 0;
1583}
1584
1585static int vidioc_s_jpegcomp(struct file *file, void *priv,
1586 struct v4l2_jpegcompression *jc)
1587{
1588 struct s2255_fh *fh = priv;
1589 struct s2255_dev *dev = fh->dev;
1590 if (jc->quality < 0 || jc->quality > 100)
1591 return -EINVAL;
1592 dev->jc[fh->channel].quality = jc->quality;
Dean Anderson85b85482010-04-08 23:51:17 -03001593 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001594 return 0;
1595}
Dean Anderson7d853532009-05-15 14:32:04 -03001596
1597static int vidioc_g_parm(struct file *file, void *priv,
1598 struct v4l2_streamparm *sp)
1599{
1600 struct s2255_fh *fh = priv;
1601 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001602 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001603 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1604 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001605 memset(sp, 0, sizeof(struct v4l2_streamparm));
1606 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
Dean Anderson7d853532009-05-15 14:32:04 -03001607 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001608 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1609 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1610 sp->parm.capture.timeperframe.denominator = def_dem;
1611 switch (fh->mode.fdec) {
1612 default:
1613 case FDEC_1:
1614 sp->parm.capture.timeperframe.numerator = def_num;
1615 break;
1616 case FDEC_2:
1617 sp->parm.capture.timeperframe.numerator = def_num * 2;
1618 break;
1619 case FDEC_3:
1620 sp->parm.capture.timeperframe.numerator = def_num * 3;
1621 break;
1622 case FDEC_5:
1623 sp->parm.capture.timeperframe.numerator = def_num * 5;
1624 break;
1625 }
1626 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1627 sp->parm.capture.capturemode,
1628 sp->parm.capture.timeperframe.numerator,
1629 sp->parm.capture.timeperframe.denominator);
Dean Anderson7d853532009-05-15 14:32:04 -03001630 return 0;
1631}
1632
1633static int vidioc_s_parm(struct file *file, void *priv,
1634 struct v4l2_streamparm *sp)
1635{
1636 struct s2255_fh *fh = priv;
1637 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001638 int fdec = FDEC_1;
1639 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001640 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1641 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001642 /* high quality capture mode requires a stream restart */
1643 if (dev->cap_parm[fh->channel].capturemode
1644 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1645 return -EBUSY;
1646 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1647 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1648 if (def_dem != sp->parm.capture.timeperframe.denominator)
1649 sp->parm.capture.timeperframe.numerator = def_num;
1650 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1651 sp->parm.capture.timeperframe.numerator = def_num;
1652 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1653 sp->parm.capture.timeperframe.numerator = def_num * 2;
1654 fdec = FDEC_2;
1655 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1656 sp->parm.capture.timeperframe.numerator = def_num * 3;
1657 fdec = FDEC_3;
1658 } else {
1659 sp->parm.capture.timeperframe.numerator = def_num * 5;
1660 fdec = FDEC_5;
1661 }
1662 fh->mode.fdec = fdec;
1663 sp->parm.capture.timeperframe.denominator = def_dem;
1664 s2255_set_mode(dev, fh->channel, &fh->mode);
1665 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1666 __func__,
1667 sp->parm.capture.capturemode,
1668 sp->parm.capture.timeperframe.numerator,
1669 sp->parm.capture.timeperframe.denominator, fdec);
Dean Anderson7d853532009-05-15 14:32:04 -03001670 return 0;
1671}
Dean Andersone6b44bc2010-03-08 20:04:48 -03001672
1673static int vidioc_enum_frameintervals(struct file *file, void *priv,
1674 struct v4l2_frmivalenum *fe)
1675{
1676 int is_ntsc = 0;
1677#define NUM_FRAME_ENUMS 4
1678 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1679 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1680 return -EINVAL;
1681 switch (fe->width) {
1682 case 640:
1683 if (fe->height != 240 && fe->height != 480)
1684 return -EINVAL;
1685 is_ntsc = 1;
1686 break;
1687 case 320:
1688 if (fe->height != 240)
1689 return -EINVAL;
1690 is_ntsc = 1;
1691 break;
1692 case 704:
1693 if (fe->height != 288 && fe->height != 576)
1694 return -EINVAL;
1695 break;
1696 case 352:
1697 if (fe->height != 288)
1698 return -EINVAL;
1699 break;
1700 default:
1701 return -EINVAL;
1702 }
1703 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1704 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1705 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1706 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1707 fe->discrete.denominator);
1708 return 0;
1709}
1710
Hans Verkuilbec43662008-12-30 06:58:20 -03001711static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001712{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001713 struct video_device *vdev = video_devdata(file);
1714 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001715 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001716 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001717 int i = 0;
1718 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001719 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001720 dprintk(1, "s2255: open called (dev=%s)\n",
1721 video_device_node_name(vdev));
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001722
1723 for (i = 0; i < dev->channels; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001724 if (&dev->vdev[i] == vdev) {
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001725 cur_channel = i;
1726 break;
1727 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001728 /*
1729 * open lock necessary to prevent multiple instances
1730 * of v4l-conf (or other programs) from simultaneously
1731 * reloading firmware.
1732 */
Dean Anderson38f993a2008-06-26 23:15:51 -03001733 mutex_lock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001734 state = atomic_read(&dev->fw_data->fw_state);
1735 switch (state) {
1736 case S2255_FW_DISCONNECTING:
1737 mutex_unlock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001738 return -ENODEV;
Dean Anderson14d96262008-08-25 13:58:55 -03001739 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001740 s2255_dev_err(&dev->udev->dev,
1741 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001742 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001743 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001744 ((atomic_read(&dev->fw_data->fw_state)
1745 == S2255_FW_SUCCESS) ||
1746 (atomic_read(&dev->fw_data->fw_state)
1747 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001748 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001749 /* state may have changed, re-read */
1750 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001751 break;
1752 case S2255_FW_NOTLOADED:
1753 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001754 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1755 driver loaded and then device immediately opened */
1756 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1757 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001758 ((atomic_read(&dev->fw_data->fw_state)
1759 == S2255_FW_SUCCESS) ||
1760 (atomic_read(&dev->fw_data->fw_state)
1761 == S2255_FW_DISCONNECTING)),
1762 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001763 /* state may have changed, re-read */
1764 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001765 break;
1766 case S2255_FW_SUCCESS:
1767 default:
1768 break;
1769 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001770 mutex_unlock(&dev->open_lock);
1771 /* state may have changed in above switch statement */
1772 switch (state) {
1773 case S2255_FW_SUCCESS:
1774 break;
1775 case S2255_FW_FAILED:
1776 printk(KERN_INFO "2255 firmware load failed.\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03001777 return -ENODEV;
1778 case S2255_FW_DISCONNECTING:
1779 printk(KERN_INFO "%s: disconnecting\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001780 return -ENODEV;
1781 case S2255_FW_LOADED_DSPWAIT:
1782 case S2255_FW_NOTLOADED:
1783 printk(KERN_INFO "%s: firmware not loaded yet"
1784 "please try again later\n",
1785 __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001786 return -EAGAIN;
1787 default:
1788 printk(KERN_INFO "%s: unknown state\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001789 return -EFAULT;
Dean Anderson38f993a2008-06-26 23:15:51 -03001790 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001791 /* allocate + initialize per filehandle data */
1792 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Dean Andersona5ef91c2010-04-08 23:46:08 -03001793 if (NULL == fh)
Dean Anderson38f993a2008-06-26 23:15:51 -03001794 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03001795 file->private_data = fh;
1796 fh->dev = dev;
1797 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1798 fh->mode = dev->mode[cur_channel];
1799 fh->fmt = dev->cur_fmt[cur_channel];
1800 /* default 4CIF NTSC */
1801 fh->width = LINE_SZ_4CIFS_NTSC;
1802 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1803 fh->channel = cur_channel;
Dean Anderson14d96262008-08-25 13:58:55 -03001804 /* configure channel to default state */
1805 if (!dev->chn_configured[cur_channel]) {
1806 s2255_set_mode(dev, cur_channel, &fh->mode);
1807 dev->chn_configured[cur_channel] = 1;
1808 }
Dean Anderson85b85482010-04-08 23:51:17 -03001809 dprintk(1, "%s: dev=%s type=%s\n", __func__,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001810 video_device_node_name(vdev), v4l2_type_names[type]);
Dean Anderson85b85482010-04-08 23:51:17 -03001811 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001812 (unsigned long)fh, (unsigned long)dev,
1813 (unsigned long)&dev->vidq[cur_channel]);
Dean Anderson85b85482010-04-08 23:51:17 -03001814 dprintk(4, "%s: list_empty active=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001815 list_empty(&dev->vidq[cur_channel].active));
Dean Anderson38f993a2008-06-26 23:15:51 -03001816 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1817 NULL, &dev->slock,
1818 fh->type,
1819 V4L2_FIELD_INTERLACED,
1820 sizeof(struct s2255_buffer), fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001821 return 0;
1822}
1823
1824
1825static unsigned int s2255_poll(struct file *file,
1826 struct poll_table_struct *wait)
1827{
1828 struct s2255_fh *fh = file->private_data;
1829 int rc;
1830 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001831 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1832 return POLLERR;
Dean Anderson38f993a2008-06-26 23:15:51 -03001833 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1834 return rc;
1835}
1836
1837static void s2255_destroy(struct kref *kref)
1838{
Dean Anderson65c6edb2010-04-20 17:21:32 -03001839 struct s2255_dev *dev = to_s2255_dev_from_kref(kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001840 /* board shutdown stops the read pipe if it is running */
1841 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001842 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001843 del_timer(&dev->timer); /* only started in .probe and .open */
Dean Anderson38f993a2008-06-26 23:15:51 -03001844 if (dev->fw_data->fw_urb) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001845 usb_kill_urb(dev->fw_data->fw_urb);
1846 usb_free_urb(dev->fw_data->fw_urb);
1847 dev->fw_data->fw_urb = NULL;
1848 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001849 if (dev->fw_data->fw)
1850 release_firmware(dev->fw_data->fw);
1851 kfree(dev->fw_data->pfw_data);
1852 kfree(dev->fw_data);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001853 /* reset the DSP so firmware can be reloaded next time */
1854 s2255_reset_dsppower(dev);
1855 mutex_destroy(&dev->open_lock);
1856 mutex_destroy(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001857 usb_put_dev(dev->udev);
1858 dprintk(1, "%s", __func__);
Dean Andersonb7732a32009-03-30 11:59:56 -03001859 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001860}
1861
Dean Andersonff7e22d2010-04-08 23:38:07 -03001862static int s2255_release(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001863{
1864 struct s2255_fh *fh = file->private_data;
1865 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001866 struct video_device *vdev = video_devdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001867 if (!dev)
1868 return -ENODEV;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001869 /* turn off stream */
1870 if (res_check(fh)) {
1871 if (dev->b_acquire[fh->channel])
1872 s2255_stop_acquire(dev, fh->channel);
1873 videobuf_streamoff(&fh->vb_vidq);
1874 res_free(dev, fh);
1875 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001876 videobuf_mmap_free(&fh->vb_vidq);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001877 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
Dean Andersonf78d92c2008-07-22 14:43:27 -03001878 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001879 return 0;
1880}
1881
1882static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1883{
1884 struct s2255_fh *fh = file->private_data;
1885 int ret;
1886
1887 if (!fh)
1888 return -ENODEV;
Dean Anderson85b85482010-04-08 23:51:17 -03001889 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
Dean Anderson38f993a2008-06-26 23:15:51 -03001890 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Dean Anderson85b85482010-04-08 23:51:17 -03001891 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001892 (unsigned long)vma->vm_start,
1893 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
Dean Anderson38f993a2008-06-26 23:15:51 -03001894 return ret;
1895}
1896
Hans Verkuilbec43662008-12-30 06:58:20 -03001897static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001898 .owner = THIS_MODULE,
1899 .open = s2255_open,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001900 .release = s2255_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001901 .poll = s2255_poll,
1902 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001903 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001904};
1905
Hans Verkuila3998102008-07-21 02:57:38 -03001906static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001907 .vidioc_querymenu = vidioc_querymenu,
Dean Anderson38f993a2008-06-26 23:15:51 -03001908 .vidioc_querycap = vidioc_querycap,
1909 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1910 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1911 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1912 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1913 .vidioc_reqbufs = vidioc_reqbufs,
1914 .vidioc_querybuf = vidioc_querybuf,
1915 .vidioc_qbuf = vidioc_qbuf,
1916 .vidioc_dqbuf = vidioc_dqbuf,
1917 .vidioc_s_std = vidioc_s_std,
1918 .vidioc_enum_input = vidioc_enum_input,
1919 .vidioc_g_input = vidioc_g_input,
1920 .vidioc_s_input = vidioc_s_input,
1921 .vidioc_queryctrl = vidioc_queryctrl,
1922 .vidioc_g_ctrl = vidioc_g_ctrl,
1923 .vidioc_s_ctrl = vidioc_s_ctrl,
1924 .vidioc_streamon = vidioc_streamon,
1925 .vidioc_streamoff = vidioc_streamoff,
1926#ifdef CONFIG_VIDEO_V4L1_COMPAT
1927 .vidiocgmbuf = vidioc_cgmbuf,
1928#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001929 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1930 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001931 .vidioc_s_parm = vidioc_s_parm,
1932 .vidioc_g_parm = vidioc_g_parm,
Dean Andersone6b44bc2010-03-08 20:04:48 -03001933 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Hans Verkuila3998102008-07-21 02:57:38 -03001934};
1935
Dean Andersonff7e22d2010-04-08 23:38:07 -03001936static void s2255_video_device_release(struct video_device *vdev)
1937{
1938 struct s2255_dev *dev = video_get_drvdata(vdev);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001939 kref_put(&dev->kref, s2255_destroy);
1940 return;
1941}
1942
Hans Verkuila3998102008-07-21 02:57:38 -03001943static struct video_device template = {
1944 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001945 .fops = &s2255_fops_v4l,
1946 .ioctl_ops = &s2255_ioctl_ops,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001947 .release = s2255_video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001948 .tvnorms = S2255_NORMS,
1949 .current_norm = V4L2_STD_NTSC_M,
1950};
1951
1952static int s2255_probe_v4l(struct s2255_dev *dev)
1953{
1954 int ret;
1955 int i;
1956 int cur_nr = video_nr;
Dean Anderson65c6edb2010-04-20 17:21:32 -03001957 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1958 if (ret)
1959 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -03001960 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03001961 /* register 4 video devices */
1962 for (i = 0; i < MAX_CHANNELS; i++) {
1963 INIT_LIST_HEAD(&dev->vidq[i].active);
1964 dev->vidq[i].dev = dev;
1965 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001966 /* register 4 video devices */
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001967 memcpy(&dev->vdev[i], &template, sizeof(struct video_device));
1968 dev->vdev[i].parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001969 if (video_nr == -1)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001970 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001971 VFL_TYPE_GRABBER,
1972 video_nr);
1973 else
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001974 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001975 VFL_TYPE_GRABBER,
1976 cur_nr + i);
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001977 video_set_drvdata(&dev->vdev[i], dev);
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001978 if (ret) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001979 dev_err(&dev->udev->dev,
1980 "failed to register video device!\n");
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001981 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001982 }
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001983 dev->channels++;
Dean Anderson65c6edb2010-04-20 17:21:32 -03001984 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001985 video_device_node_name(&dev->vdev[i]));
1986
Dean Anderson38f993a2008-06-26 23:15:51 -03001987 }
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001988
Dean Andersonabce21f2009-04-23 16:04:41 -03001989 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1990 S2255_MAJOR_VERSION,
1991 S2255_MINOR_VERSION);
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001992 /* if no channels registered, return error and probe will fail*/
Dean Anderson65c6edb2010-04-20 17:21:32 -03001993 if (dev->channels == 0) {
1994 v4l2_device_unregister(&dev->v4l2_dev);
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001995 return ret;
Dean Anderson65c6edb2010-04-20 17:21:32 -03001996 }
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001997 if (dev->channels != MAX_CHANNELS)
1998 printk(KERN_WARNING "s2255: Not all channels available.\n");
1999 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002000}
2001
Dean Anderson38f993a2008-06-26 23:15:51 -03002002/* this function moves the usb stream read pipe data
2003 * into the system buffers.
2004 * returns 0 on success, EAGAIN if more data to process( call this
2005 * function again).
2006 *
2007 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03002008 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03002009 * bytes 4-7: channel: 0-3
2010 * bytes 8-11: payload size: size of the frame
2011 * bytes 12-payloadsize+12: frame data
2012 */
2013static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2014{
Dean Anderson38f993a2008-06-26 23:15:51 -03002015 char *pdest;
2016 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002017 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002018 char *psrc;
2019 unsigned long copy_size;
2020 unsigned long size;
2021 s32 idx = -1;
2022 struct s2255_framei *frm;
2023 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002024
Dean Anderson38f993a2008-06-26 23:15:51 -03002025 dprintk(100, "buffer to user\n");
2026
2027 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03002028 frm = &dev->buffer[dev->cc].frame[idx];
2029
Dean Anderson14d96262008-08-25 13:58:55 -03002030 if (frm->ulState == S2255_READ_IDLE) {
2031 int jj;
2032 unsigned int cc;
Dean Anderson3fa00602010-03-04 20:47:33 -03002033 __le32 *pdword; /*data from dsp is little endian */
Dean Anderson14d96262008-08-25 13:58:55 -03002034 int payload;
2035 /* search for marker codes */
2036 pdata = (unsigned char *)pipe_info->transfer_buffer;
Dean Anderson3fa00602010-03-04 20:47:33 -03002037 pdword = (__le32 *)pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002038 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
Dean Anderson3fa00602010-03-04 20:47:33 -03002039 switch (*pdword) {
Dean Anderson14d96262008-08-25 13:58:55 -03002040 case S2255_MARKER_FRAME:
Dean Anderson14d96262008-08-25 13:58:55 -03002041 dprintk(4, "found frame marker at offset:"
2042 " %d [%x %x]\n", jj, pdata[0],
2043 pdata[1]);
2044 offset = jj + PREFIX_SIZE;
2045 bframe = 1;
2046 cc = pdword[1];
2047 if (cc >= MAX_CHANNELS) {
2048 printk(KERN_ERR
2049 "bad channel\n");
2050 return -EINVAL;
2051 }
2052 /* reverse it */
2053 dev->cc = G_chnmap[cc];
2054 payload = pdword[3];
2055 if (payload > dev->req_image_size[dev->cc]) {
2056 dev->bad_payload[dev->cc]++;
2057 /* discard the bad frame */
2058 return -EINVAL;
2059 }
2060 dev->pkt_size[dev->cc] = payload;
2061 dev->jpg_size[dev->cc] = pdword[4];
2062 break;
2063 case S2255_MARKER_RESPONSE:
Dean Anderson14d96262008-08-25 13:58:55 -03002064 pdata += DEF_USB_BLOCK;
2065 jj += DEF_USB_BLOCK;
2066 if (pdword[1] >= MAX_CHANNELS)
2067 break;
2068 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03002069 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03002070 break;
2071 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03002072 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03002073 /* check if channel valid */
2074 /* set mode ready */
2075 dev->setmode_ready[cc] = 1;
2076 wake_up(&dev->wait_setmode[cc]);
2077 dprintk(5, "setmode ready %d\n", cc);
2078 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03002079 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03002080
2081 dev->chn_ready |= (1 << cc);
2082 if ((dev->chn_ready & 0x0f) != 0x0f)
2083 break;
2084 /* all channels ready */
2085 printk(KERN_INFO "s2255: fw loaded\n");
2086 atomic_set(&dev->fw_data->fw_state,
2087 S2255_FW_SUCCESS);
2088 wake_up(&dev->fw_data->wait_fw);
2089 break;
Dean Anderson4de39f52010-03-03 19:39:19 -03002090 case S2255_RESPONSE_STATUS:
2091 dev->vidstatus[cc] = pdword[3];
2092 dev->vidstatus_ready[cc] = 1;
2093 wake_up(&dev->wait_vidstatus[cc]);
2094 dprintk(5, "got vidstatus %x chan %d\n",
2095 pdword[3], cc);
2096 break;
Dean Anderson14d96262008-08-25 13:58:55 -03002097 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002098 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002099 }
2100 default:
2101 pdata++;
2102 break;
2103 }
2104 if (bframe)
2105 break;
2106 } /* for */
2107 if (!bframe)
2108 return -EINVAL;
2109 }
2110
2111
2112 idx = dev->cur_frame[dev->cc];
2113 frm = &dev->buffer[dev->cc].frame[idx];
2114
2115 /* search done. now find out if should be acquiring on this channel */
2116 if (!dev->b_acquire[dev->cc]) {
2117 /* we found a frame, but this channel is turned off */
2118 frm->ulState = S2255_READ_IDLE;
2119 return -EINVAL;
2120 }
2121
2122 if (frm->ulState == S2255_READ_IDLE) {
2123 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002124 frm->cur_size = 0;
2125 }
2126
Dean Anderson14d96262008-08-25 13:58:55 -03002127 /* skip the marker 512 bytes (and offset if out of sync) */
2128 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2129
Dean Anderson38f993a2008-06-26 23:15:51 -03002130
2131 if (frm->lpvbits == NULL) {
2132 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2133 frm, dev, dev->cc, idx);
2134 return -ENOMEM;
2135 }
2136
2137 pdest = frm->lpvbits + frm->cur_size;
2138
Dean Anderson14d96262008-08-25 13:58:55 -03002139 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002140
Dean Anderson14d96262008-08-25 13:58:55 -03002141 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002142
Dean Anderson14d96262008-08-25 13:58:55 -03002143 /* sanity check on pdest */
2144 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2145 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002146
Dean Anderson38f993a2008-06-26 23:15:51 -03002147 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002148 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002149
Dean Anderson14d96262008-08-25 13:58:55 -03002150 if (frm->cur_size >= size) {
2151
Dean Anderson38f993a2008-06-26 23:15:51 -03002152 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002153 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2154 cc, idx);
2155 dev->last_frame[cc] = dev->cur_frame[cc];
2156 dev->cur_frame[cc]++;
2157 /* end of system frame ring buffer, start at zero */
2158 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2159 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2160 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002161 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002162 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002163 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002164 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002165 frm->ulState = S2255_READ_IDLE;
2166 frm->cur_size = 0;
2167
Dean Anderson38f993a2008-06-26 23:15:51 -03002168 }
2169 /* done successfully */
2170 return 0;
2171}
2172
2173static void s2255_read_video_callback(struct s2255_dev *dev,
2174 struct s2255_pipeinfo *pipe_info)
2175{
2176 int res;
2177 dprintk(50, "callback read video \n");
2178
2179 if (dev->cc >= MAX_CHANNELS) {
2180 dev->cc = 0;
2181 dev_err(&dev->udev->dev, "invalid channel\n");
2182 return;
2183 }
2184 /* otherwise copy to the system buffers */
2185 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002186 if (res != 0)
2187 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002188
2189 dprintk(50, "callback read video done\n");
2190 return;
2191}
2192
2193static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2194 u16 Index, u16 Value, void *TransferBuffer,
2195 s32 TransferBufferLength, int bOut)
2196{
2197 int r;
2198 if (!bOut) {
2199 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2200 Request,
2201 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2202 USB_DIR_IN,
2203 Value, Index, TransferBuffer,
2204 TransferBufferLength, HZ * 5);
2205 } else {
2206 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2207 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2208 Value, Index, TransferBuffer,
2209 TransferBufferLength, HZ * 5);
2210 }
2211 return r;
2212}
2213
2214/*
2215 * retrieve FX2 firmware version. future use.
2216 * @param dev pointer to device extension
2217 * @return -1 for fail, else returns firmware version as an int(16 bits)
2218 */
2219static int s2255_get_fx2fw(struct s2255_dev *dev)
2220{
2221 int fw;
2222 int ret;
2223 unsigned char transBuffer[64];
2224 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2225 S2255_VR_IN);
2226 if (ret < 0)
2227 dprintk(2, "get fw error: %x\n", ret);
2228 fw = transBuffer[0] + (transBuffer[1] << 8);
2229 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2230 return fw;
2231}
2232
2233/*
2234 * Create the system ring buffer to copy frames into from the
2235 * usb read pipe.
2236 */
2237static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2238{
2239 unsigned long i;
2240 unsigned long reqsize;
2241 dprintk(1, "create sys buffers\n");
2242 if (chn >= MAX_CHANNELS)
2243 return -1;
2244
2245 dev->buffer[chn].dwFrames = SYS_FRAMES;
2246
2247 /* always allocate maximum size(PAL) for system buffers */
2248 reqsize = SYS_FRAMES_MAXSIZE;
2249
2250 if (reqsize > SYS_FRAMES_MAXSIZE)
2251 reqsize = SYS_FRAMES_MAXSIZE;
2252
2253 for (i = 0; i < SYS_FRAMES; i++) {
2254 /* allocate the frames */
2255 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2256
2257 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2258 &dev->buffer[chn].frame[i], chn, i,
2259 dev->buffer[chn].frame[i].lpvbits);
2260 dev->buffer[chn].frame[i].size = reqsize;
2261 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2262 printk(KERN_INFO "out of memory. using less frames\n");
2263 dev->buffer[chn].dwFrames = i;
2264 break;
2265 }
2266 }
2267
2268 /* make sure internal states are set */
2269 for (i = 0; i < SYS_FRAMES; i++) {
2270 dev->buffer[chn].frame[i].ulState = 0;
2271 dev->buffer[chn].frame[i].cur_size = 0;
2272 }
2273
2274 dev->cur_frame[chn] = 0;
2275 dev->last_frame[chn] = -1;
2276 return 0;
2277}
2278
2279static int s2255_release_sys_buffers(struct s2255_dev *dev,
2280 unsigned long channel)
2281{
2282 unsigned long i;
2283 dprintk(1, "release sys buffers\n");
2284 for (i = 0; i < SYS_FRAMES; i++) {
2285 if (dev->buffer[channel].frame[i].lpvbits) {
2286 dprintk(1, "vfree %p\n",
2287 dev->buffer[channel].frame[i].lpvbits);
2288 vfree(dev->buffer[channel].frame[i].lpvbits);
2289 }
2290 dev->buffer[channel].frame[i].lpvbits = NULL;
2291 }
2292 return 0;
2293}
2294
2295static int s2255_board_init(struct s2255_dev *dev)
2296{
Dean Anderson38f993a2008-06-26 23:15:51 -03002297 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2298 int fw_ver;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002299 int j;
2300 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002301 dprintk(4, "board init: %p", dev);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002302 memset(pipe, 0, sizeof(*pipe));
2303 pipe->dev = dev;
2304 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2305 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002306
Dean Andersonab85c6a2010-04-08 23:39:12 -03002307 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2308 GFP_KERNEL);
2309 if (pipe->transfer_buffer == NULL) {
2310 dprintk(1, "out of memory!\n");
2311 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002312 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002313 /* query the firmware */
2314 fw_ver = s2255_get_fx2fw(dev);
2315
Dean Andersonabce21f2009-04-23 16:04:41 -03002316 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2317 (fw_ver >> 8) & 0xff,
2318 fw_ver & 0xff);
2319
2320 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002321 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002322 "usb firmware not up to date %d.%d\n",
2323 (fw_ver >> 8) & 0xff,
2324 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002325
2326 for (j = 0; j < MAX_CHANNELS; j++) {
2327 dev->b_acquire[j] = 0;
2328 dev->mode[j] = mode_def;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002329 if (dev->pid == 0x2257 && j > 1)
2330 dev->mode[j].color |= (1 << 16);
Dean Anderson22b88d42008-08-29 15:33:19 -03002331 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002332 dev->cur_fmt[j] = &formats[0];
2333 dev->mode[j].restart = 1;
2334 dev->req_image_size[j] = get_transfer_size(&mode_def);
2335 dev->frame_count[j] = 0;
2336 /* create the system buffers */
2337 s2255_create_sys_buffers(dev, j);
2338 }
2339 /* start read pipe */
2340 s2255_start_readpipe(dev);
Dean Anderson85b85482010-04-08 23:51:17 -03002341 dprintk(1, "%s: success\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002342 return 0;
2343}
2344
2345static int s2255_board_shutdown(struct s2255_dev *dev)
2346{
2347 u32 i;
Dean Anderson85b85482010-04-08 23:51:17 -03002348 dprintk(1, "%s: dev: %p", __func__, dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002349
2350 for (i = 0; i < MAX_CHANNELS; i++) {
2351 if (dev->b_acquire[i])
2352 s2255_stop_acquire(dev, i);
2353 }
2354
2355 s2255_stop_readpipe(dev);
2356
2357 for (i = 0; i < MAX_CHANNELS; i++)
2358 s2255_release_sys_buffers(dev, i);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002359 /* release transfer buffer */
2360 kfree(dev->pipe.transfer_buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03002361 return 0;
2362}
2363
2364static void read_pipe_completion(struct urb *purb)
2365{
2366 struct s2255_pipeinfo *pipe_info;
2367 struct s2255_dev *dev;
2368 int status;
2369 int pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002370 pipe_info = purb->context;
Dean Anderson85b85482010-04-08 23:51:17 -03002371 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
Dean Anderson38f993a2008-06-26 23:15:51 -03002372 purb->status);
2373 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002374 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002375 return;
2376 }
2377
2378 dev = pipe_info->dev;
2379 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002380 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002381 return;
2382 }
2383 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002384 /* if shutting down, do not resubmit, exit immediately */
2385 if (status == -ESHUTDOWN) {
Dean Anderson85b85482010-04-08 23:51:17 -03002386 dprintk(2, "%s: err shutdown\n", __func__);
Dean Andersonb02064c2009-04-30 12:29:38 -03002387 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002388 return;
2389 }
2390
2391 if (pipe_info->state == 0) {
Dean Anderson85b85482010-04-08 23:51:17 -03002392 dprintk(2, "%s: exiting USB pipe", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002393 return;
2394 }
2395
Dean Andersonb02064c2009-04-30 12:29:38 -03002396 if (status == 0)
2397 s2255_read_video_callback(dev, pipe_info);
2398 else {
2399 pipe_info->err_count++;
Dean Anderson85b85482010-04-08 23:51:17 -03002400 dprintk(1, "%s: failed URB %d\n", __func__, status);
Dean Andersonb02064c2009-04-30 12:29:38 -03002401 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002402
Dean Anderson38f993a2008-06-26 23:15:51 -03002403 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2404 /* reuse urb */
2405 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2406 pipe,
2407 pipe_info->transfer_buffer,
2408 pipe_info->cur_transfer_size,
2409 read_pipe_completion, pipe_info);
2410
2411 if (pipe_info->state != 0) {
2412 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2413 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002414 }
2415 } else {
Dean Anderson85b85482010-04-08 23:51:17 -03002416 dprintk(2, "%s :complete state 0\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002417 }
2418 return;
2419}
2420
2421static int s2255_start_readpipe(struct s2255_dev *dev)
2422{
2423 int pipe;
2424 int retval;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002425 struct s2255_pipeinfo *pipe_info = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002426 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
Dean Anderson85b85482010-04-08 23:51:17 -03002427 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002428 pipe_info->state = 1;
2429 pipe_info->err_count = 0;
2430 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2431 if (!pipe_info->stream_urb) {
2432 dev_err(&dev->udev->dev,
2433 "ReadStream: Unable to alloc URB\n");
2434 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002435 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002436 /* transfer buffer allocated in board_init */
2437 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2438 pipe,
2439 pipe_info->transfer_buffer,
2440 pipe_info->cur_transfer_size,
2441 read_pipe_completion, pipe_info);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002442 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2443 if (retval) {
2444 printk(KERN_ERR "s2255: start read pipe failed\n");
2445 return retval;
2446 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002447 return 0;
2448}
2449
2450/* starts acquisition process */
2451static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2452{
2453 unsigned char *buffer;
2454 int res;
2455 unsigned long chn_rev;
2456 int j;
2457 if (chn >= MAX_CHANNELS) {
2458 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2459 return -1;
2460 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002461 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002462 buffer = kzalloc(512, GFP_KERNEL);
2463 if (buffer == NULL) {
2464 dev_err(&dev->udev->dev, "out of mem\n");
2465 return -ENOMEM;
2466 }
2467
2468 dev->last_frame[chn] = -1;
2469 dev->bad_payload[chn] = 0;
2470 dev->cur_frame[chn] = 0;
2471 for (j = 0; j < SYS_FRAMES; j++) {
2472 dev->buffer[chn].frame[j].ulState = 0;
2473 dev->buffer[chn].frame[j].cur_size = 0;
2474 }
2475
2476 /* send the start command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002477 *(__le32 *) buffer = IN_DATA_TOKEN;
2478 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2479 *((__le32 *) buffer + 2) = CMD_START;
Dean Anderson38f993a2008-06-26 23:15:51 -03002480 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2481 if (res != 0)
2482 dev_err(&dev->udev->dev, "CMD_START error\n");
2483
2484 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2485 kfree(buffer);
2486 return 0;
2487}
2488
2489static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2490{
2491 unsigned char *buffer;
2492 int res;
2493 unsigned long chn_rev;
Dean Anderson38f993a2008-06-26 23:15:51 -03002494 if (chn >= MAX_CHANNELS) {
2495 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2496 return -1;
2497 }
2498 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002499 buffer = kzalloc(512, GFP_KERNEL);
2500 if (buffer == NULL) {
2501 dev_err(&dev->udev->dev, "out of mem\n");
2502 return -ENOMEM;
2503 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002504 /* send the stop command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002505 *(__le32 *) buffer = IN_DATA_TOKEN;
2506 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2507 *((__le32 *) buffer + 2) = CMD_STOP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002508 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
Dean Anderson38f993a2008-06-26 23:15:51 -03002509 if (res != 0)
2510 dev_err(&dev->udev->dev, "CMD_STOP error\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002511 kfree(buffer);
2512 dev->b_acquire[chn] = 0;
Dean Anderson85b85482010-04-08 23:51:17 -03002513 dprintk(4, "%s: chn %lu, res %d\n", __func__, chn, res);
Dean Anderson14d96262008-08-25 13:58:55 -03002514 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002515}
2516
2517static void s2255_stop_readpipe(struct s2255_dev *dev)
2518{
Dean Andersonab85c6a2010-04-08 23:39:12 -03002519 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002520 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002521 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002522 return;
2523 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002524 pipe->state = 0;
2525 if (pipe->stream_urb) {
2526 /* cancel urb */
2527 usb_kill_urb(pipe->stream_urb);
2528 usb_free_urb(pipe->stream_urb);
2529 pipe->stream_urb = NULL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002530 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002531 dprintk(4, "%s", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002532 return;
2533}
2534
Dean Anderson14d96262008-08-25 13:58:55 -03002535static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002536{
Dean Anderson14d96262008-08-25 13:58:55 -03002537 if (reset)
2538 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002539 dev->fw_data->fw_size = dev->fw_data->fw->size;
2540 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2541 memcpy(dev->fw_data->pfw_data,
2542 dev->fw_data->fw->data, CHUNK_SIZE);
2543 dev->fw_data->fw_loaded = CHUNK_SIZE;
2544 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2545 usb_sndbulkpipe(dev->udev, 2),
2546 dev->fw_data->pfw_data,
2547 CHUNK_SIZE, s2255_fwchunk_complete,
2548 dev->fw_data);
2549 mod_timer(&dev->timer, jiffies + HZ);
2550}
2551
2552/* standard usb probe function */
2553static int s2255_probe(struct usb_interface *interface,
2554 const struct usb_device_id *id)
2555{
2556 struct s2255_dev *dev = NULL;
2557 struct usb_host_interface *iface_desc;
2558 struct usb_endpoint_descriptor *endpoint;
2559 int i;
2560 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002561 __le32 *pdata;
2562 int fw_size;
Dean Anderson85b85482010-04-08 23:51:17 -03002563 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002564 /* allocate memory for our device state and initialize it to zero */
2565 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2566 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002567 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002568 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002569 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002570 kref_init(&dev->kref);
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002571 dev->pid = id->idProduct;
Dean Anderson38f993a2008-06-26 23:15:51 -03002572 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2573 if (!dev->fw_data)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002574 goto errorFWDATA1;
Dean Anderson38f993a2008-06-26 23:15:51 -03002575 mutex_init(&dev->lock);
2576 mutex_init(&dev->open_lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03002577 /* grab usb_device and save it */
2578 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2579 if (dev->udev == NULL) {
2580 dev_err(&interface->dev, "null usb device\n");
2581 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002582 goto errorUDEV;
Dean Anderson38f993a2008-06-26 23:15:51 -03002583 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002584 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2585 dev->udev, interface);
2586 dev->interface = interface;
2587 /* set up the endpoint information */
2588 iface_desc = interface->cur_altsetting;
2589 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2590 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2591 endpoint = &iface_desc->endpoint[i].desc;
2592 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2593 /* we found the bulk in endpoint */
2594 dev->read_endpoint = endpoint->bEndpointAddress;
2595 }
2596 }
2597
2598 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002599 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002600 goto errorEP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002601 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002602 init_timer(&dev->timer);
2603 dev->timer.function = s2255_timer;
2604 dev->timer.data = (unsigned long)dev->fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -03002605 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson4de39f52010-03-03 19:39:19 -03002606 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03002607 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002608 init_waitqueue_head(&dev->wait_vidstatus[i]);
2609 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002610
2611 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
Dean Anderson38f993a2008-06-26 23:15:51 -03002612 if (!dev->fw_data->fw_urb) {
2613 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002614 goto errorFWURB;
Dean Anderson38f993a2008-06-26 23:15:51 -03002615 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002616
Dean Anderson38f993a2008-06-26 23:15:51 -03002617 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2618 if (!dev->fw_data->pfw_data) {
2619 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002620 goto errorFWDATA2;
Dean Anderson38f993a2008-06-26 23:15:51 -03002621 }
2622 /* load the first chunk */
2623 if (request_firmware(&dev->fw_data->fw,
2624 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2625 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002626 goto errorREQFW;
Dean Anderson38f993a2008-06-26 23:15:51 -03002627 }
Dean Anderson14d96262008-08-25 13:58:55 -03002628 /* check the firmware is valid */
2629 fw_size = dev->fw_data->fw->size;
2630 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002631
Dean Anderson14d96262008-08-25 13:58:55 -03002632 if (*pdata != S2255_FW_MARKER) {
2633 printk(KERN_INFO "Firmware invalid.\n");
2634 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002635 goto errorFWMARKER;
Dean Anderson14d96262008-08-25 13:58:55 -03002636 } else {
2637 /* make sure firmware is the latest */
2638 __le32 *pRel;
2639 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2640 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
Dean Anderson4de39f52010-03-03 19:39:19 -03002641 dev->dsp_fw_ver = *pRel;
2642 if (*pRel < S2255_CUR_DSP_FWVER)
2643 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002644 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002645 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2646 "or above.\n", S2255_MIN_DSP_COLORFILTER);
Dean Anderson14d96262008-08-25 13:58:55 -03002647 }
Dean Anderson14d96262008-08-25 13:58:55 -03002648 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002649 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002650 retval = s2255_board_init(dev);
2651 if (retval)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002652 goto errorBOARDINIT;
Dean Anderson38f993a2008-06-26 23:15:51 -03002653 spin_lock_init(&dev->slock);
Dean Anderson14d96262008-08-25 13:58:55 -03002654 s2255_fwload_start(dev, 0);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002655 /* kref for each vdev. Released on video_device_release callback */
2656 for (i = 0; i < MAX_CHANNELS; i++)
2657 kref_get(&dev->kref);
2658 /* loads v4l specific */
2659 retval = s2255_probe_v4l(dev);
2660 if (retval)
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03002661 goto errorBOARDINIT;
Dean Anderson38f993a2008-06-26 23:15:51 -03002662 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2663 return 0;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002664errorBOARDINIT:
2665 s2255_board_shutdown(dev);
2666errorFWMARKER:
2667 release_firmware(dev->fw_data->fw);
2668errorREQFW:
2669 kfree(dev->fw_data->pfw_data);
2670errorFWDATA2:
2671 usb_free_urb(dev->fw_data->fw_urb);
2672errorFWURB:
2673 del_timer(&dev->timer);
2674errorEP:
2675 usb_put_dev(dev->udev);
2676errorUDEV:
2677 kfree(dev->fw_data);
2678 mutex_destroy(&dev->open_lock);
2679 mutex_destroy(&dev->lock);
2680errorFWDATA1:
2681 kfree(dev);
2682 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
Dean Anderson38f993a2008-06-26 23:15:51 -03002683 return retval;
2684}
2685
2686/* disconnect routine. when board is removed physically or with rmmod */
2687static void s2255_disconnect(struct usb_interface *interface)
2688{
Dean Anderson65c6edb2010-04-20 17:21:32 -03002689 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
Dean Anderson14d96262008-08-25 13:58:55 -03002690 int i;
Dean Anderson65c6edb2010-04-20 17:21:32 -03002691 v4l2_device_unregister(&dev->v4l2_dev);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002692 /* unregister each video device. */
2693 for (i = 0; i < MAX_CHANNELS; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03002694 if (video_is_registered(&dev->vdev[i]))
2695 video_unregister_device(&dev->vdev[i]);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002696 /* wake up any of our timers */
Dean Anderson14d96262008-08-25 13:58:55 -03002697 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2698 wake_up(&dev->fw_data->wait_fw);
2699 for (i = 0; i < MAX_CHANNELS; i++) {
2700 dev->setmode_ready[i] = 1;
2701 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002702 dev->vidstatus_ready[i] = 1;
2703 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002704 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002705 kref_put(&dev->kref, s2255_destroy);
2706 dev_info(&interface->dev, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002707}
2708
2709static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002710 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002711 .probe = s2255_probe,
2712 .disconnect = s2255_disconnect,
2713 .id_table = s2255_table,
2714};
2715
2716static int __init usb_s2255_init(void)
2717{
2718 int result;
Dean Anderson38f993a2008-06-26 23:15:51 -03002719 /* register this driver with the USB subsystem */
2720 result = usb_register(&s2255_driver);
Dean Anderson38f993a2008-06-26 23:15:51 -03002721 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002722 pr_err(KBUILD_MODNAME
Dean Andersonff7e22d2010-04-08 23:38:07 -03002723 ": usb_register failed. Error number %d\n", result);
2724 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002725 return result;
2726}
2727
2728static void __exit usb_s2255_exit(void)
2729{
2730 usb_deregister(&s2255_driver);
2731}
2732
2733module_init(usb_s2255_init);
2734module_exit(usb_s2255_exit);
2735
2736MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2737MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2738MODULE_LICENSE("GPL");