blob: e1de2c4940b0f0016312a9f0374e5bb7b7635614 [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 Anderson3a67b5cc2010-04-08 23:52:20 -0300232 struct v4l2_device v4l2_dev[MAX_CHANNELS];
233 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};
281#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
282
283struct s2255_fmt {
284 char *name;
285 u32 fourcc;
286 int depth;
287};
288
289/* buffer for one video frame */
290struct s2255_buffer {
291 /* common v4l buffer stuff -- must be first */
292 struct videobuf_buffer vb;
293 const struct s2255_fmt *fmt;
294};
295
296struct s2255_fh {
297 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300298 const struct s2255_fmt *fmt;
299 unsigned int width;
300 unsigned int height;
301 struct videobuf_queue vb_vidq;
302 enum v4l2_buf_type type;
303 int channel;
304 /* mode below is the desired mode.
305 mode in s2255_dev is the current mode that was last set */
306 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300307 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300308};
309
Dean Andersonabce21f2009-04-23 16:04:41 -0300310/* current cypress EEPROM firmware version */
311#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson4de39f52010-03-03 19:39:19 -0300312/* current DSP FW version */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300313#define S2255_CUR_DSP_FWVER 8
Dean Anderson4de39f52010-03-03 19:39:19 -0300314/* Need DSP version 5+ for video status feature */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300315#define S2255_MIN_DSP_STATUS 5
316#define S2255_MIN_DSP_COLORFILTER 8
Dean Anderson38f993a2008-06-26 23:15:51 -0300317#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300318
319/* private V4L2 controls */
320
321/*
322 * The following chart displays how COLORFILTER should be set
323 * =========================================================
324 * = fourcc = COLORFILTER =
325 * = ===============================
326 * = = 0 = 1 =
327 * =========================================================
328 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
329 * = = s-video or = composite =
330 * = = B/W camera = input =
331 * =========================================================
332 * = other = color, svideo = color, =
333 * = = = composite =
334 * =========================================================
335 *
336 * Notes:
337 * channels 0-3 on 2255 are composite
338 * channels 0-1 on 2257 are composite, 2-3 are s-video
339 * If COLORFILTER is 0 with a composite color camera connected,
340 * the output will appear monochrome but hatching
341 * will occur.
342 * COLORFILTER is different from "color killer" and "color effects"
343 * for reasons above.
344 */
345#define S2255_V4L2_YC_ON 1
346#define S2255_V4L2_YC_OFF 0
347#define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
348
Dean Anderson38f993a2008-06-26 23:15:51 -0300349/* frame prefix size (sent once every frame) */
350#define PREFIX_SIZE 512
351
352/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300353static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300354
Dean Anderson38f993a2008-06-26 23:15:51 -0300355static int debug;
356static int *s2255_debug = &debug;
357
358static int s2255_start_readpipe(struct s2255_dev *dev);
359static void s2255_stop_readpipe(struct s2255_dev *dev);
360static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
361static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
362static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300363 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300364static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
365 struct s2255_mode *mode);
366static int s2255_board_shutdown(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300367static void s2255_fwload_start(struct s2255_dev *dev, int reset);
368static void s2255_destroy(struct kref *kref);
369static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
370 u16 index, u16 value, void *buf,
371 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300372
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300373/* dev_err macro with driver name */
374#define S2255_DRIVER_NAME "s2255"
375#define s2255_dev_err(dev, fmt, arg...) \
376 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
377
Dean Anderson38f993a2008-06-26 23:15:51 -0300378#define dprintk(level, fmt, arg...) \
379 do { \
380 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300381 printk(KERN_DEBUG S2255_DRIVER_NAME \
382 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300383 } \
384 } while (0)
385
Dean Anderson38f993a2008-06-26 23:15:51 -0300386static struct usb_driver s2255_driver;
387
Dean Anderson38f993a2008-06-26 23:15:51 -0300388/* Declare static vars that will be used as parameters */
389static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
390
391/* start video number */
392static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
393
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300394module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300395MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300396module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300397MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300398module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300399MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
400
401/* USB device table */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300402#define USB_SENSORAY_VID 0x1943
Dean Anderson38f993a2008-06-26 23:15:51 -0300403static struct usb_device_id s2255_table[] = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300404 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
405 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300406 { } /* Terminating entry */
407};
408MODULE_DEVICE_TABLE(usb, s2255_table);
409
Dean Anderson38f993a2008-06-26 23:15:51 -0300410#define BUFFER_TIMEOUT msecs_to_jiffies(400)
411
Dean Anderson38f993a2008-06-26 23:15:51 -0300412/* image formats. */
413static const struct s2255_fmt formats[] = {
414 {
415 .name = "4:2:2, planar, YUV422P",
416 .fourcc = V4L2_PIX_FMT_YUV422P,
417 .depth = 16
418
419 }, {
420 .name = "4:2:2, packed, YUYV",
421 .fourcc = V4L2_PIX_FMT_YUYV,
422 .depth = 16
423
424 }, {
425 .name = "4:2:2, packed, UYVY",
426 .fourcc = V4L2_PIX_FMT_UYVY,
427 .depth = 16
428 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300429 .name = "JPG",
430 .fourcc = V4L2_PIX_FMT_JPEG,
431 .depth = 24
432 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300433 .name = "8bpp GREY",
434 .fourcc = V4L2_PIX_FMT_GREY,
435 .depth = 8
436 }
437};
438
439static int norm_maxw(struct video_device *vdev)
440{
441 return (vdev->current_norm & V4L2_STD_NTSC) ?
442 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
443}
444
445static int norm_maxh(struct video_device *vdev)
446{
447 return (vdev->current_norm & V4L2_STD_NTSC) ?
448 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
449}
450
451static int norm_minw(struct video_device *vdev)
452{
453 return (vdev->current_norm & V4L2_STD_NTSC) ?
454 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
455}
456
457static int norm_minh(struct video_device *vdev)
458{
459 return (vdev->current_norm & V4L2_STD_NTSC) ?
460 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
461}
462
463
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300464/*
465 * TODO: fixme: move YUV reordering to hardware
466 * converts 2255 planar format to yuyv or uyvy
467 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300468static void planar422p_to_yuv_packed(const unsigned char *in,
469 unsigned char *out,
470 int width, int height,
471 int fmt)
472{
473 unsigned char *pY;
474 unsigned char *pCb;
475 unsigned char *pCr;
476 unsigned long size = height * width;
477 unsigned int i;
478 pY = (unsigned char *)in;
479 pCr = (unsigned char *)in + height * width;
480 pCb = (unsigned char *)in + height * width + (height * width / 2);
481 for (i = 0; i < size * 2; i += 4) {
482 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
483 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
484 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
485 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
486 }
487 return;
488}
489
Hans Verkuild45b9b82008-09-04 03:33:43 -0300490static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300491{
492 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
493 msleep(10);
494 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
495 return;
496}
Dean Anderson38f993a2008-06-26 23:15:51 -0300497
498/* kickstarts the firmware loading. from probe
499 */
500static void s2255_timer(unsigned long user_data)
501{
502 struct s2255_fw *data = (struct s2255_fw *)user_data;
Dean Anderson85b85482010-04-08 23:51:17 -0300503 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300504 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
505 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300506 atomic_set(&data->fw_state, S2255_FW_FAILED);
507 /* wake up anything waiting for the firmware */
508 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300509 return;
510 }
511}
512
Dean Anderson38f993a2008-06-26 23:15:51 -0300513
514/* this loads the firmware asynchronously.
515 Originally this was done synchroously in probe.
516 But it is better to load it asynchronously here than block
517 inside the probe function. Blocking inside probe affects boot time.
518 FW loading is triggered by the timer in the probe function
519*/
520static void s2255_fwchunk_complete(struct urb *urb)
521{
522 struct s2255_fw *data = urb->context;
523 struct usb_device *udev = urb->dev;
524 int len;
Dean Anderson85b85482010-04-08 23:51:17 -0300525 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300526 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300527 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300528 atomic_set(&data->fw_state, S2255_FW_FAILED);
529 /* wake up anything waiting for the firmware */
530 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300531 return;
532 }
533 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300534 s2255_dev_err(&udev->dev, "disconnected\n");
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#define CHUNK_SIZE 512
541 /* all USB transfers must be done with continuous kernel memory.
542 can't allocate more than 128k in current linux kernel, so
543 upload the firmware in chunks
544 */
545 if (data->fw_loaded < data->fw_size) {
546 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
547 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
548
549 if (len < CHUNK_SIZE)
550 memset(data->pfw_data, 0, CHUNK_SIZE);
551
552 dprintk(100, "completed len %d, loaded %d \n", len,
553 data->fw_loaded);
554
555 memcpy(data->pfw_data,
556 (char *) data->fw->data + data->fw_loaded, len);
557
558 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
559 data->pfw_data, CHUNK_SIZE,
560 s2255_fwchunk_complete, data);
561 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
562 dev_err(&udev->dev, "failed submit URB\n");
563 atomic_set(&data->fw_state, S2255_FW_FAILED);
564 /* wake up anything waiting for the firmware */
565 wake_up(&data->wait_fw);
566 return;
567 }
568 data->fw_loaded += len;
569 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300570 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson85b85482010-04-08 23:51:17 -0300571 dprintk(100, "%s: firmware upload complete\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300572 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300573 return;
574
575}
576
Dean Anderson14d96262008-08-25 13:58:55 -0300577static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300578{
579 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
580 struct s2255_buffer *buf;
581 unsigned long flags = 0;
582 int rc = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300583 spin_lock_irqsave(&dev->slock, flags);
Dean Anderson38f993a2008-06-26 23:15:51 -0300584 if (list_empty(&dma_q->active)) {
585 dprintk(1, "No active queue to serve\n");
586 rc = -1;
587 goto unlock;
588 }
589 buf = list_entry(dma_q->active.next,
590 struct s2255_buffer, vb.queue);
Dean Anderson38f993a2008-06-26 23:15:51 -0300591 list_del(&buf->vb.queue);
592 do_gettimeofday(&buf->vb.ts);
Dean Anderson14d96262008-08-25 13:58:55 -0300593 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300594 wake_up(&buf->vb.done);
Dean Anderson85b85482010-04-08 23:51:17 -0300595 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
Dean Anderson38f993a2008-06-26 23:15:51 -0300596unlock:
597 spin_unlock_irqrestore(&dev->slock, flags);
598 return 0;
599}
600
Dean Anderson38f993a2008-06-26 23:15:51 -0300601static const struct s2255_fmt *format_by_fourcc(int fourcc)
602{
603 unsigned int i;
Dean Anderson38f993a2008-06-26 23:15:51 -0300604 for (i = 0; i < ARRAY_SIZE(formats); i++) {
605 if (-1 == formats[i].fourcc)
606 continue;
607 if (formats[i].fourcc == fourcc)
608 return formats + i;
609 }
610 return NULL;
611}
612
Dean Anderson38f993a2008-06-26 23:15:51 -0300613/* video buffer vmalloc implementation based partly on VIVI driver which is
614 * Copyright (c) 2006 by
615 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
616 * Ted Walther <ted--a.t--enumera.com>
617 * John Sokol <sokol--a.t--videotechnology.com>
618 * http://v4l.videotechnology.com/
619 *
620 */
621static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300622 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300623{
624 int pos = 0;
625 struct timeval ts;
626 const char *tmpbuf;
627 char *vbuf = videobuf_to_vmalloc(&buf->vb);
628 unsigned long last_frame;
629 struct s2255_framei *frm;
630
631 if (!vbuf)
632 return;
633
634 last_frame = dev->last_frame[chn];
635 if (last_frame != -1) {
636 frm = &dev->buffer[chn].frame[last_frame];
637 tmpbuf =
638 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
639 switch (buf->fmt->fourcc) {
640 case V4L2_PIX_FMT_YUYV:
641 case V4L2_PIX_FMT_UYVY:
642 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
643 vbuf, buf->vb.width,
644 buf->vb.height,
645 buf->fmt->fourcc);
646 break;
647 case V4L2_PIX_FMT_GREY:
648 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
649 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300650 case V4L2_PIX_FMT_JPEG:
651 buf->vb.size = jpgsize;
652 memcpy(vbuf, tmpbuf, buf->vb.size);
653 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300654 case V4L2_PIX_FMT_YUV422P:
655 memcpy(vbuf, tmpbuf,
656 buf->vb.width * buf->vb.height * 2);
657 break;
658 default:
659 printk(KERN_DEBUG "s2255: unknown format?\n");
660 }
661 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300662 } else {
663 printk(KERN_ERR "s2255: =======no frame\n");
664 return;
665
666 }
667 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
668 (unsigned long)vbuf, pos);
669 /* tell v4l buffer was filled */
670
Dean Andersona1c45302008-09-09 12:29:56 -0300671 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300672 do_gettimeofday(&ts);
673 buf->vb.ts = ts;
674 buf->vb.state = VIDEOBUF_DONE;
675}
676
677
678/* ------------------------------------------------------------------
679 Videobuf operations
680 ------------------------------------------------------------------*/
681
682static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
683 unsigned int *size)
684{
685 struct s2255_fh *fh = vq->priv_data;
686
687 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
688
689 if (0 == *count)
690 *count = S2255_DEF_BUFS;
691
Andreas Bombedab7e312010-03-21 16:02:45 -0300692 if (*size * *count > vid_limit * 1024 * 1024)
693 *count = (vid_limit * 1024 * 1024) / *size;
Dean Anderson38f993a2008-06-26 23:15:51 -0300694
695 return 0;
696}
697
698static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
699{
700 dprintk(4, "%s\n", __func__);
701
Dean Anderson38f993a2008-06-26 23:15:51 -0300702 videobuf_vmalloc_free(&buf->vb);
703 buf->vb.state = VIDEOBUF_NEEDS_INIT;
704}
705
706static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
707 enum v4l2_field field)
708{
709 struct s2255_fh *fh = vq->priv_data;
710 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
711 int rc;
712 dprintk(4, "%s, field=%d\n", __func__, field);
713 if (fh->fmt == NULL)
714 return -EINVAL;
715
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300716 if ((fh->width < norm_minw(&fh->dev->vdev[fh->channel])) ||
717 (fh->width > norm_maxw(&fh->dev->vdev[fh->channel])) ||
718 (fh->height < norm_minh(&fh->dev->vdev[fh->channel])) ||
719 (fh->height > norm_maxh(&fh->dev->vdev[fh->channel]))) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300720 dprintk(4, "invalid buffer prepare\n");
721 return -EINVAL;
722 }
723
724 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
725
726 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
727 dprintk(4, "invalid buffer prepare\n");
728 return -EINVAL;
729 }
730
731 buf->fmt = fh->fmt;
732 buf->vb.width = fh->width;
733 buf->vb.height = fh->height;
734 buf->vb.field = field;
735
Dean Anderson38f993a2008-06-26 23:15:51 -0300736 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
737 rc = videobuf_iolock(vq, &buf->vb, NULL);
738 if (rc < 0)
739 goto fail;
740 }
741
742 buf->vb.state = VIDEOBUF_PREPARED;
743 return 0;
744fail:
745 free_buffer(vq, buf);
746 return rc;
747}
748
749static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
750{
751 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
752 struct s2255_fh *fh = vq->priv_data;
753 struct s2255_dev *dev = fh->dev;
754 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300755 dprintk(1, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300756 buf->vb.state = VIDEOBUF_QUEUED;
757 list_add_tail(&buf->vb.queue, &vidq->active);
758}
759
760static void buffer_release(struct videobuf_queue *vq,
761 struct videobuf_buffer *vb)
762{
763 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
764 struct s2255_fh *fh = vq->priv_data;
765 dprintk(4, "%s %d\n", __func__, fh->channel);
766 free_buffer(vq, buf);
767}
768
769static struct videobuf_queue_ops s2255_video_qops = {
770 .buf_setup = buffer_setup,
771 .buf_prepare = buffer_prepare,
772 .buf_queue = buffer_queue,
773 .buf_release = buffer_release,
774};
775
776
777static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
778{
779 /* is it free? */
780 mutex_lock(&dev->lock);
781 if (dev->resources[fh->channel]) {
782 /* no, someone else uses it */
783 mutex_unlock(&dev->lock);
784 return 0;
785 }
786 /* it's free, grab it */
787 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300788 fh->resources[fh->channel] = 1;
789 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300790 mutex_unlock(&dev->lock);
791 return 1;
792}
793
794static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
795{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300796 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300797}
798
Dean Andersonf78d92c2008-07-22 14:43:27 -0300799static int res_check(struct s2255_fh *fh)
800{
801 return fh->resources[fh->channel];
802}
803
804
Dean Anderson38f993a2008-06-26 23:15:51 -0300805static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
806{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300807 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300808 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300809 fh->resources[fh->channel] = 0;
810 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300811 dprintk(1, "res: put\n");
812}
813
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300814static int vidioc_querymenu(struct file *file, void *priv,
815 struct v4l2_querymenu *qmenu)
816{
817 static const char *colorfilter[] = {
818 "Off",
819 "On",
820 NULL
821 };
822 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
823 int i;
824 const char **menu_items = colorfilter;
825 for (i = 0; i < qmenu->index && menu_items[i]; i++)
826 ; /* do nothing (from v4l2-common.c) */
827 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
828 return -EINVAL;
829 strlcpy(qmenu->name, menu_items[qmenu->index],
830 sizeof(qmenu->name));
831 return 0;
832 }
833 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
834}
835
Dean Anderson38f993a2008-06-26 23:15:51 -0300836static int vidioc_querycap(struct file *file, void *priv,
837 struct v4l2_capability *cap)
838{
839 struct s2255_fh *fh = file->private_data;
840 struct s2255_dev *dev = fh->dev;
841 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
842 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300843 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300844 cap->version = S2255_VERSION;
845 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
846 return 0;
847}
848
849static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
850 struct v4l2_fmtdesc *f)
851{
852 int index = 0;
853 if (f)
854 index = f->index;
855
856 if (index >= ARRAY_SIZE(formats))
857 return -EINVAL;
858
859 dprintk(4, "name %s\n", formats[index].name);
860 strlcpy(f->description, formats[index].name, sizeof(f->description));
861 f->pixelformat = formats[index].fourcc;
862 return 0;
863}
864
865static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
866 struct v4l2_format *f)
867{
868 struct s2255_fh *fh = priv;
869
870 f->fmt.pix.width = fh->width;
871 f->fmt.pix.height = fh->height;
872 f->fmt.pix.field = fh->vb_vidq.field;
873 f->fmt.pix.pixelformat = fh->fmt->fourcc;
874 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
875 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300876 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300877}
878
879static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
880 struct v4l2_format *f)
881{
882 const struct s2255_fmt *fmt;
883 enum v4l2_field field;
884 int b_any_field = 0;
885 struct s2255_fh *fh = priv;
886 struct s2255_dev *dev = fh->dev;
887 int is_ntsc;
888
889 is_ntsc =
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300890 (dev->vdev[fh->channel].current_norm & V4L2_STD_NTSC) ? 1 : 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300891
892 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
893
894 if (fmt == NULL)
895 return -EINVAL;
896
897 field = f->fmt.pix.field;
898 if (field == V4L2_FIELD_ANY)
899 b_any_field = 1;
900
Dean Anderson85b85482010-04-08 23:51:17 -0300901 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
902 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
Dean Anderson38f993a2008-06-26 23:15:51 -0300903 if (is_ntsc) {
904 /* NTSC */
905 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
906 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
907 if (b_any_field) {
908 field = V4L2_FIELD_SEQ_TB;
909 } else if (!((field == V4L2_FIELD_INTERLACED) ||
910 (field == V4L2_FIELD_SEQ_TB) ||
911 (field == V4L2_FIELD_INTERLACED_TB))) {
912 dprintk(1, "unsupported field setting\n");
913 return -EINVAL;
914 }
915 } else {
916 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
917 if (b_any_field) {
918 field = V4L2_FIELD_TOP;
919 } else if (!((field == V4L2_FIELD_TOP) ||
920 (field == V4L2_FIELD_BOTTOM))) {
921 dprintk(1, "unsupported field setting\n");
922 return -EINVAL;
923 }
924
925 }
926 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
930 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
932 else
933 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
934 } else {
935 /* PAL */
936 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
937 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
938 if (b_any_field) {
939 field = V4L2_FIELD_SEQ_TB;
940 } else if (!((field == V4L2_FIELD_INTERLACED) ||
941 (field == V4L2_FIELD_SEQ_TB) ||
942 (field == V4L2_FIELD_INTERLACED_TB))) {
943 dprintk(1, "unsupported field setting\n");
944 return -EINVAL;
945 }
946 } else {
947 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
948 if (b_any_field) {
949 field = V4L2_FIELD_TOP;
950 } else if (!((field == V4L2_FIELD_TOP) ||
951 (field == V4L2_FIELD_BOTTOM))) {
952 dprintk(1, "unsupported field setting\n");
953 return -EINVAL;
954 }
955 }
956 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300957 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
958 field = V4L2_FIELD_SEQ_TB;
959 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300960 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
961 field = V4L2_FIELD_TOP;
962 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300963 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
964 field = V4L2_FIELD_TOP;
965 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300966 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
967 field = V4L2_FIELD_TOP;
968 }
969 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300970 f->fmt.pix.field = field;
971 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
972 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson85b85482010-04-08 23:51:17 -0300973 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
974 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
Dean Anderson38f993a2008-06-26 23:15:51 -0300975 return 0;
976}
977
978static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
979 struct v4l2_format *f)
980{
981 struct s2255_fh *fh = priv;
982 const struct s2255_fmt *fmt;
983 struct videobuf_queue *q = &fh->vb_vidq;
984 int ret;
985 int norm;
986
987 ret = vidioc_try_fmt_vid_cap(file, fh, f);
988
989 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300990 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300991
992 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
993
994 if (fmt == NULL)
995 return -EINVAL;
996
997 mutex_lock(&q->vb_lock);
998
999 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1000 dprintk(1, "queue busy\n");
1001 ret = -EBUSY;
1002 goto out_s_fmt;
1003 }
1004
1005 if (res_locked(fh->dev, fh)) {
Dean Anderson85b85482010-04-08 23:51:17 -03001006 dprintk(1, "%s: channel busy\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001007 ret = -EBUSY;
1008 goto out_s_fmt;
1009 }
1010
1011 fh->fmt = fmt;
1012 fh->width = f->fmt.pix.width;
1013 fh->height = f->fmt.pix.height;
1014 fh->vb_vidq.field = f->fmt.pix.field;
1015 fh->type = f->type;
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001016 norm = norm_minw(&fh->dev->vdev[fh->channel]);
1017 if (fh->width > norm_minw(&fh->dev->vdev[fh->channel])) {
1018 if (fh->height > norm_minh(&fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001019 if (fh->dev->cap_parm[fh->channel].capturemode &
Dean Anderson85b85482010-04-08 23:51:17 -03001020 V4L2_MODE_HIGHQUALITY)
Dean Anderson7d853532009-05-15 14:32:04 -03001021 fh->mode.scale = SCALE_4CIFSI;
Dean Anderson85b85482010-04-08 23:51:17 -03001022 else
Dean Anderson7d853532009-05-15 14:32:04 -03001023 fh->mode.scale = SCALE_4CIFS;
Dean Anderson7d853532009-05-15 14:32:04 -03001024 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001025 fh->mode.scale = SCALE_2CIFS;
1026
1027 } else {
1028 fh->mode.scale = SCALE_1CIFS;
1029 }
1030
1031 /* color mode */
1032 switch (fh->fmt->fourcc) {
1033 case V4L2_PIX_FMT_GREY:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001034 fh->mode.color &= ~MASK_COLOR;
1035 fh->mode.color |= COLOR_Y8;
Dean Anderson38f993a2008-06-26 23:15:51 -03001036 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001037 case V4L2_PIX_FMT_JPEG:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001038 fh->mode.color &= ~MASK_COLOR;
1039 fh->mode.color |= COLOR_JPG;
1040 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001041 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001042 case V4L2_PIX_FMT_YUV422P:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001043 fh->mode.color &= ~MASK_COLOR;
1044 fh->mode.color |= COLOR_YUVPL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001045 break;
1046 case V4L2_PIX_FMT_YUYV:
1047 case V4L2_PIX_FMT_UYVY:
1048 default:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001049 fh->mode.color &= ~MASK_COLOR;
1050 fh->mode.color |= COLOR_YUVPK;
Dean Anderson38f993a2008-06-26 23:15:51 -03001051 break;
1052 }
1053 ret = 0;
1054out_s_fmt:
1055 mutex_unlock(&q->vb_lock);
1056 return ret;
1057}
1058
1059static int vidioc_reqbufs(struct file *file, void *priv,
1060 struct v4l2_requestbuffers *p)
1061{
1062 int rc;
1063 struct s2255_fh *fh = priv;
1064 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1065 return rc;
1066}
1067
1068static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1069{
1070 int rc;
1071 struct s2255_fh *fh = priv;
1072 rc = videobuf_querybuf(&fh->vb_vidq, p);
1073 return rc;
1074}
1075
1076static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1077{
1078 int rc;
1079 struct s2255_fh *fh = priv;
1080 rc = videobuf_qbuf(&fh->vb_vidq, p);
1081 return rc;
1082}
1083
1084static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1085{
1086 int rc;
1087 struct s2255_fh *fh = priv;
1088 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1089 return rc;
1090}
1091
1092#ifdef CONFIG_VIDEO_V4L1_COMPAT
1093static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1094{
1095 struct s2255_fh *fh = priv;
1096
1097 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1098}
1099#endif
1100
1101/* write to the configuration pipe, synchronously */
1102static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1103 int size)
1104{
1105 int pipe;
1106 int done;
1107 long retval = -1;
1108 if (udev) {
1109 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1110 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1111 }
1112 return retval;
1113}
1114
1115static u32 get_transfer_size(struct s2255_mode *mode)
1116{
1117 int linesPerFrame = LINE_SZ_DEF;
1118 int pixelsPerLine = NUM_LINES_DEF;
1119 u32 outImageSize;
1120 u32 usbInSize;
1121 unsigned int mask_mult;
1122
1123 if (mode == NULL)
1124 return 0;
1125
1126 if (mode->format == FORMAT_NTSC) {
1127 switch (mode->scale) {
1128 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001129 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001130 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1131 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1132 break;
1133 case SCALE_2CIFS:
1134 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1135 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1136 break;
1137 case SCALE_1CIFS:
1138 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1139 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1140 break;
1141 default:
1142 break;
1143 }
1144 } else if (mode->format == FORMAT_PAL) {
1145 switch (mode->scale) {
1146 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001147 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001148 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1149 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1150 break;
1151 case SCALE_2CIFS:
1152 linesPerFrame = NUM_LINES_2CIFS_PAL;
1153 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1154 break;
1155 case SCALE_1CIFS:
1156 linesPerFrame = NUM_LINES_1CIFS_PAL;
1157 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1158 break;
1159 default:
1160 break;
1161 }
1162 }
1163 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001164 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001165 /* 2 bytes/pixel if not monochrome */
1166 outImageSize *= 2;
1167 }
1168
1169 /* total bytes to send including prefix and 4K padding;
1170 must be a multiple of USB_READ_SIZE */
1171 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1172 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1173 /* if size not a multiple of USB_READ_SIZE */
1174 if (usbInSize & ~mask_mult)
1175 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1176 return usbInSize;
1177}
1178
Dean Anderson85b85482010-04-08 23:51:17 -03001179static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
Dean Anderson38f993a2008-06-26 23:15:51 -03001180{
1181 struct device *dev = &sdev->udev->dev;
1182 dev_info(dev, "------------------------------------------------\n");
Dean Anderson85b85482010-04-08 23:51:17 -03001183 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1184 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
Dean Anderson38f993a2008-06-26 23:15:51 -03001185 dev_info(dev, "bright: 0x%x\n", mode->bright);
Dean Anderson38f993a2008-06-26 23:15:51 -03001186 dev_info(dev, "------------------------------------------------\n");
1187}
1188
1189/*
1190 * set mode is the function which controls the DSP.
1191 * the restart parameter in struct s2255_mode should be set whenever
1192 * the image size could change via color format, video system or image
1193 * size.
1194 * When the restart parameter is set, we sleep for ONE frame to allow the
1195 * DSP time to get the new frame
1196 */
1197static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1198 struct s2255_mode *mode)
1199{
1200 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001201 __le32 *buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -03001202 unsigned long chn_rev;
Dean Anderson14d96262008-08-25 13:58:55 -03001203 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001204 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001205 dprintk(3, "%s channel %lu\n", __func__, chn);
Dean Anderson22b88d42008-08-29 15:33:19 -03001206 /* if JPEG, set the quality */
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001207 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1208 mode->color &= ~MASK_COLOR;
1209 mode->color |= COLOR_JPG;
1210 mode->color &= ~MASK_JPG_QUALITY;
1211 mode->color |= (dev->jc[chn].quality << 8);
1212 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001213 /* save the mode */
1214 dev->mode[chn] = *mode;
1215 dev->req_image_size[chn] = get_transfer_size(mode);
Dean Anderson85b85482010-04-08 23:51:17 -03001216 dprintk(1, "%s: reqsize %ld\n", __func__, dev->req_image_size[chn]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001217 buffer = kzalloc(512, GFP_KERNEL);
1218 if (buffer == NULL) {
1219 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001220 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001221 return -ENOMEM;
1222 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001223 /* set the mode */
1224 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001225 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001226 buffer[2] = CMD_SET_MODE;
1227 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001228 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001229 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1230 if (debug)
Dean Anderson85b85482010-04-08 23:51:17 -03001231 s2255_print_cfg(dev, mode);
Dean Anderson38f993a2008-06-26 23:15:51 -03001232 kfree(buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03001233 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001234 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001235 wait_event_timeout(dev->wait_setmode[chn],
1236 (dev->setmode_ready[chn] != 0),
1237 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1238 if (dev->setmode_ready[chn] != 1) {
1239 printk(KERN_DEBUG "s2255: no set mode response\n");
1240 res = -EFAULT;
1241 }
1242 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001243 /* clear the restart flag */
1244 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001245 mutex_unlock(&dev->lock);
Dean Anderson85b85482010-04-08 23:51:17 -03001246 dprintk(1, "%s chn %lu, result: %d\n", __func__, chn, res);
Dean Anderson38f993a2008-06-26 23:15:51 -03001247 return res;
1248}
1249
Dean Anderson4de39f52010-03-03 19:39:19 -03001250static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1251 u32 *pstatus)
1252{
1253 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001254 __le32 *buffer;
Dean Anderson4de39f52010-03-03 19:39:19 -03001255 u32 chn_rev;
1256 mutex_lock(&dev->lock);
1257 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001258 dprintk(4, "%s chan %lu\n", __func__, chn);
Dean Anderson4de39f52010-03-03 19:39:19 -03001259 buffer = kzalloc(512, GFP_KERNEL);
1260 if (buffer == NULL) {
1261 dev_err(&dev->udev->dev, "out of mem\n");
1262 mutex_unlock(&dev->lock);
1263 return -ENOMEM;
1264 }
1265 /* form the get vid status command */
1266 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001267 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson4de39f52010-03-03 19:39:19 -03001268 buffer[2] = CMD_STATUS;
1269 *pstatus = 0;
1270 dev->vidstatus_ready[chn] = 0;
1271 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1272 kfree(buffer);
1273 wait_event_timeout(dev->wait_vidstatus[chn],
1274 (dev->vidstatus_ready[chn] != 0),
1275 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1276 if (dev->vidstatus_ready[chn] != 1) {
1277 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1278 res = -EFAULT;
1279 }
1280 *pstatus = dev->vidstatus[chn];
1281 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1282 mutex_unlock(&dev->lock);
1283 return res;
1284}
1285
Dean Anderson38f993a2008-06-26 23:15:51 -03001286static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1287{
1288 int res;
1289 struct s2255_fh *fh = priv;
1290 struct s2255_dev *dev = fh->dev;
1291 struct s2255_mode *new_mode;
1292 struct s2255_mode *old_mode;
1293 int chn;
1294 int j;
1295 dprintk(4, "%s\n", __func__);
1296 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1297 dev_err(&dev->udev->dev, "invalid fh type0\n");
1298 return -EINVAL;
1299 }
1300 if (i != fh->type) {
1301 dev_err(&dev->udev->dev, "invalid fh type1\n");
1302 return -EINVAL;
1303 }
1304
1305 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001306 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001307 return -EBUSY;
1308 }
1309
1310 /* send a set mode command everytime with restart.
1311 in case we switch resolutions or other parameters */
1312 chn = fh->channel;
1313 new_mode = &fh->mode;
1314 old_mode = &fh->dev->mode[chn];
1315
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001316 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
Dean Anderson38f993a2008-06-26 23:15:51 -03001317 new_mode->restart = 1;
1318 else if (new_mode->scale != old_mode->scale)
1319 new_mode->restart = 1;
1320 else if (new_mode->format != old_mode->format)
1321 new_mode->restart = 1;
1322
1323 s2255_set_mode(dev, chn, new_mode);
1324 new_mode->restart = 0;
1325 *old_mode = *new_mode;
1326 dev->cur_fmt[chn] = fh->fmt;
Dean Anderson38f993a2008-06-26 23:15:51 -03001327 dev->last_frame[chn] = -1;
1328 dev->bad_payload[chn] = 0;
1329 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001330 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001331 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001332 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001333 dev->buffer[chn].frame[j].cur_size = 0;
1334 }
1335 res = videobuf_streamon(&fh->vb_vidq);
1336 if (res == 0) {
1337 s2255_start_acquire(dev, chn);
1338 dev->b_acquire[chn] = 1;
1339 } else {
1340 res_free(dev, fh);
1341 }
1342 return res;
1343}
1344
1345static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1346{
Dean Anderson38f993a2008-06-26 23:15:51 -03001347 struct s2255_fh *fh = priv;
1348 struct s2255_dev *dev = fh->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001349 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1350 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1351 printk(KERN_ERR "invalid fh type0\n");
1352 return -EINVAL;
1353 }
1354 if (i != fh->type) {
1355 printk(KERN_ERR "invalid type i\n");
1356 return -EINVAL;
1357 }
1358 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001359 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001360 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001361 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001362}
1363
1364static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1365{
1366 struct s2255_fh *fh = priv;
1367 struct s2255_mode *mode;
1368 struct videobuf_queue *q = &fh->vb_vidq;
1369 int ret = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001370 mutex_lock(&q->vb_lock);
1371 if (videobuf_queue_is_busy(q)) {
1372 dprintk(1, "queue busy\n");
1373 ret = -EBUSY;
1374 goto out_s_std;
1375 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001376 if (res_locked(fh->dev, fh)) {
1377 dprintk(1, "can't change standard after started\n");
1378 ret = -EBUSY;
1379 goto out_s_std;
1380 }
1381 mode = &fh->mode;
Dean Anderson38f993a2008-06-26 23:15:51 -03001382 if (*i & V4L2_STD_NTSC) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001383 dprintk(4, "%s NTSC\n", __func__);
1384 /* if changing format, reset frame decimation/intervals */
1385 if (mode->format != FORMAT_NTSC) {
1386 mode->format = FORMAT_NTSC;
1387 mode->fdec = FDEC_1;
1388 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001389 } else if (*i & V4L2_STD_PAL) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001390 dprintk(4, "%s PAL\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001391 mode->format = FORMAT_PAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001392 if (mode->format != FORMAT_PAL) {
1393 mode->format = FORMAT_PAL;
1394 mode->fdec = FDEC_1;
1395 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001396 } else {
1397 ret = -EINVAL;
1398 }
1399out_s_std:
1400 mutex_unlock(&q->vb_lock);
1401 return ret;
1402}
1403
1404/* Sensoray 2255 is a multiple channel capture device.
1405 It does not have a "crossbar" of inputs.
1406 We use one V4L device per channel. The user must
1407 be aware that certain combinations are not allowed.
1408 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1409 at once in color(you can do full fps on 4 channels with greyscale.
1410*/
1411static int vidioc_enum_input(struct file *file, void *priv,
1412 struct v4l2_input *inp)
1413{
Dean Anderson4de39f52010-03-03 19:39:19 -03001414 struct s2255_fh *fh = priv;
1415 struct s2255_dev *dev = fh->dev;
1416 u32 status = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001417 if (inp->index != 0)
1418 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001419 inp->type = V4L2_INPUT_TYPE_CAMERA;
1420 inp->std = S2255_NORMS;
Dean Anderson4de39f52010-03-03 19:39:19 -03001421 inp->status = 0;
1422 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1423 int rc;
1424 rc = s2255_cmd_status(dev, fh->channel, &status);
1425 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1426 if (rc == 0)
1427 inp->status = (status & 0x01) ? 0
1428 : V4L2_IN_ST_NO_SIGNAL;
1429 }
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001430 switch (dev->pid) {
1431 case 0x2255:
1432 default:
1433 strlcpy(inp->name, "Composite", sizeof(inp->name));
1434 break;
1435 case 0x2257:
1436 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1437 sizeof(inp->name));
1438 break;
1439 }
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001440 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001441}
1442
1443static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1444{
1445 *i = 0;
1446 return 0;
1447}
1448static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1449{
1450 if (i > 0)
1451 return -EINVAL;
1452 return 0;
1453}
1454
1455/* --- controls ---------------------------------------------- */
1456static int vidioc_queryctrl(struct file *file, void *priv,
1457 struct v4l2_queryctrl *qc)
1458{
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001459 struct s2255_fh *fh = priv;
1460 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001461 switch (qc->id) {
1462 case V4L2_CID_BRIGHTNESS:
1463 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1464 break;
1465 case V4L2_CID_CONTRAST:
1466 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1467 break;
1468 case V4L2_CID_SATURATION:
1469 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1470 break;
1471 case V4L2_CID_HUE:
1472 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1473 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001474 case V4L2_CID_PRIVATE_COLORFILTER:
1475 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1476 return -EINVAL;
1477 if ((dev->pid == 0x2257) && (fh->channel > 1))
1478 return -EINVAL;
1479 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1480 qc->type = V4L2_CTRL_TYPE_MENU;
1481 qc->minimum = 0;
1482 qc->maximum = 1;
1483 qc->step = 1;
1484 qc->default_value = 1;
1485 qc->flags = 0;
1486 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001487 default:
1488 return -EINVAL;
1489 }
1490 dprintk(4, "%s, id %d\n", __func__, qc->id);
1491 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001492}
1493
1494static int vidioc_g_ctrl(struct file *file, void *priv,
1495 struct v4l2_control *ctrl)
1496{
Dean Anderson2e70db92010-03-05 14:29:09 -03001497 struct s2255_fh *fh = priv;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001498 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001499 switch (ctrl->id) {
1500 case V4L2_CID_BRIGHTNESS:
1501 ctrl->value = fh->mode.bright;
1502 break;
1503 case V4L2_CID_CONTRAST:
1504 ctrl->value = fh->mode.contrast;
1505 break;
1506 case V4L2_CID_SATURATION:
1507 ctrl->value = fh->mode.saturation;
1508 break;
1509 case V4L2_CID_HUE:
1510 ctrl->value = fh->mode.hue;
1511 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001512 case V4L2_CID_PRIVATE_COLORFILTER:
1513 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1514 return -EINVAL;
1515 if ((dev->pid == 0x2257) && (fh->channel > 1))
1516 return -EINVAL;
1517 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1518 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001519 default:
1520 return -EINVAL;
1521 }
1522 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1523 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001524}
1525
1526static int vidioc_s_ctrl(struct file *file, void *priv,
1527 struct v4l2_control *ctrl)
1528{
Dean Anderson38f993a2008-06-26 23:15:51 -03001529 struct s2255_fh *fh = priv;
1530 struct s2255_dev *dev = fh->dev;
1531 struct s2255_mode *mode;
1532 mode = &fh->mode;
Dean Anderson2e70db92010-03-05 14:29:09 -03001533 dprintk(4, "%s\n", __func__);
1534 /* update the mode to the corresponding value */
1535 switch (ctrl->id) {
1536 case V4L2_CID_BRIGHTNESS:
1537 mode->bright = ctrl->value;
1538 break;
1539 case V4L2_CID_CONTRAST:
1540 mode->contrast = ctrl->value;
1541 break;
1542 case V4L2_CID_HUE:
1543 mode->hue = ctrl->value;
1544 break;
1545 case V4L2_CID_SATURATION:
1546 mode->saturation = ctrl->value;
1547 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001548 case V4L2_CID_PRIVATE_COLORFILTER:
1549 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1550 return -EINVAL;
1551 if ((dev->pid == 0x2257) && (fh->channel > 1))
1552 return -EINVAL;
1553 mode->color &= ~MASK_INPUT_TYPE;
1554 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1555 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001556 default:
1557 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001558 }
Dean Anderson2e70db92010-03-05 14:29:09 -03001559 mode->restart = 0;
1560 /* set mode here. Note: stream does not need restarted.
1561 some V4L programs restart stream unnecessarily
1562 after a s_crtl.
1563 */
1564 s2255_set_mode(dev, fh->channel, mode);
1565 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001566}
1567
Dean Anderson22b88d42008-08-29 15:33:19 -03001568static int vidioc_g_jpegcomp(struct file *file, void *priv,
1569 struct v4l2_jpegcompression *jc)
1570{
1571 struct s2255_fh *fh = priv;
1572 struct s2255_dev *dev = fh->dev;
1573 *jc = dev->jc[fh->channel];
Dean Anderson85b85482010-04-08 23:51:17 -03001574 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001575 return 0;
1576}
1577
1578static int vidioc_s_jpegcomp(struct file *file, void *priv,
1579 struct v4l2_jpegcompression *jc)
1580{
1581 struct s2255_fh *fh = priv;
1582 struct s2255_dev *dev = fh->dev;
1583 if (jc->quality < 0 || jc->quality > 100)
1584 return -EINVAL;
1585 dev->jc[fh->channel].quality = jc->quality;
Dean Anderson85b85482010-04-08 23:51:17 -03001586 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001587 return 0;
1588}
Dean Anderson7d853532009-05-15 14:32:04 -03001589
1590static int vidioc_g_parm(struct file *file, void *priv,
1591 struct v4l2_streamparm *sp)
1592{
1593 struct s2255_fh *fh = priv;
1594 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001595 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001596 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1597 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001598 memset(sp, 0, sizeof(struct v4l2_streamparm));
1599 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
Dean Anderson7d853532009-05-15 14:32:04 -03001600 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001601 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1602 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1603 sp->parm.capture.timeperframe.denominator = def_dem;
1604 switch (fh->mode.fdec) {
1605 default:
1606 case FDEC_1:
1607 sp->parm.capture.timeperframe.numerator = def_num;
1608 break;
1609 case FDEC_2:
1610 sp->parm.capture.timeperframe.numerator = def_num * 2;
1611 break;
1612 case FDEC_3:
1613 sp->parm.capture.timeperframe.numerator = def_num * 3;
1614 break;
1615 case FDEC_5:
1616 sp->parm.capture.timeperframe.numerator = def_num * 5;
1617 break;
1618 }
1619 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1620 sp->parm.capture.capturemode,
1621 sp->parm.capture.timeperframe.numerator,
1622 sp->parm.capture.timeperframe.denominator);
Dean Anderson7d853532009-05-15 14:32:04 -03001623 return 0;
1624}
1625
1626static int vidioc_s_parm(struct file *file, void *priv,
1627 struct v4l2_streamparm *sp)
1628{
1629 struct s2255_fh *fh = priv;
1630 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001631 int fdec = FDEC_1;
1632 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001633 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1634 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001635 /* high quality capture mode requires a stream restart */
1636 if (dev->cap_parm[fh->channel].capturemode
1637 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1638 return -EBUSY;
1639 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1640 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1641 if (def_dem != sp->parm.capture.timeperframe.denominator)
1642 sp->parm.capture.timeperframe.numerator = def_num;
1643 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1644 sp->parm.capture.timeperframe.numerator = def_num;
1645 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1646 sp->parm.capture.timeperframe.numerator = def_num * 2;
1647 fdec = FDEC_2;
1648 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1649 sp->parm.capture.timeperframe.numerator = def_num * 3;
1650 fdec = FDEC_3;
1651 } else {
1652 sp->parm.capture.timeperframe.numerator = def_num * 5;
1653 fdec = FDEC_5;
1654 }
1655 fh->mode.fdec = fdec;
1656 sp->parm.capture.timeperframe.denominator = def_dem;
1657 s2255_set_mode(dev, fh->channel, &fh->mode);
1658 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1659 __func__,
1660 sp->parm.capture.capturemode,
1661 sp->parm.capture.timeperframe.numerator,
1662 sp->parm.capture.timeperframe.denominator, fdec);
Dean Anderson7d853532009-05-15 14:32:04 -03001663 return 0;
1664}
Dean Andersone6b44bc2010-03-08 20:04:48 -03001665
1666static int vidioc_enum_frameintervals(struct file *file, void *priv,
1667 struct v4l2_frmivalenum *fe)
1668{
1669 int is_ntsc = 0;
1670#define NUM_FRAME_ENUMS 4
1671 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1672 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1673 return -EINVAL;
1674 switch (fe->width) {
1675 case 640:
1676 if (fe->height != 240 && fe->height != 480)
1677 return -EINVAL;
1678 is_ntsc = 1;
1679 break;
1680 case 320:
1681 if (fe->height != 240)
1682 return -EINVAL;
1683 is_ntsc = 1;
1684 break;
1685 case 704:
1686 if (fe->height != 288 && fe->height != 576)
1687 return -EINVAL;
1688 break;
1689 case 352:
1690 if (fe->height != 288)
1691 return -EINVAL;
1692 break;
1693 default:
1694 return -EINVAL;
1695 }
1696 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1697 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1698 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1699 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1700 fe->discrete.denominator);
1701 return 0;
1702}
1703
Hans Verkuilbec43662008-12-30 06:58:20 -03001704static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001705{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001706 struct video_device *vdev = video_devdata(file);
1707 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001708 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001709 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001710 int i = 0;
1711 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001712 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001713 dprintk(1, "s2255: open called (dev=%s)\n",
1714 video_device_node_name(vdev));
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001715
1716 for (i = 0; i < dev->channels; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001717 if (&dev->vdev[i] == vdev) {
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001718 cur_channel = i;
1719 break;
1720 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001721 /*
1722 * open lock necessary to prevent multiple instances
1723 * of v4l-conf (or other programs) from simultaneously
1724 * reloading firmware.
1725 */
Dean Anderson38f993a2008-06-26 23:15:51 -03001726 mutex_lock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001727 state = atomic_read(&dev->fw_data->fw_state);
1728 switch (state) {
1729 case S2255_FW_DISCONNECTING:
1730 mutex_unlock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001731 return -ENODEV;
Dean Anderson14d96262008-08-25 13:58:55 -03001732 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001733 s2255_dev_err(&dev->udev->dev,
1734 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001735 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001736 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001737 ((atomic_read(&dev->fw_data->fw_state)
1738 == S2255_FW_SUCCESS) ||
1739 (atomic_read(&dev->fw_data->fw_state)
1740 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001741 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001742 /* state may have changed, re-read */
1743 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001744 break;
1745 case S2255_FW_NOTLOADED:
1746 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001747 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1748 driver loaded and then device immediately opened */
1749 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1750 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001751 ((atomic_read(&dev->fw_data->fw_state)
1752 == S2255_FW_SUCCESS) ||
1753 (atomic_read(&dev->fw_data->fw_state)
1754 == S2255_FW_DISCONNECTING)),
1755 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001756 /* state may have changed, re-read */
1757 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001758 break;
1759 case S2255_FW_SUCCESS:
1760 default:
1761 break;
1762 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001763 mutex_unlock(&dev->open_lock);
1764 /* state may have changed in above switch statement */
1765 switch (state) {
1766 case S2255_FW_SUCCESS:
1767 break;
1768 case S2255_FW_FAILED:
1769 printk(KERN_INFO "2255 firmware load failed.\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03001770 return -ENODEV;
1771 case S2255_FW_DISCONNECTING:
1772 printk(KERN_INFO "%s: disconnecting\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001773 return -ENODEV;
1774 case S2255_FW_LOADED_DSPWAIT:
1775 case S2255_FW_NOTLOADED:
1776 printk(KERN_INFO "%s: firmware not loaded yet"
1777 "please try again later\n",
1778 __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001779 return -EAGAIN;
1780 default:
1781 printk(KERN_INFO "%s: unknown state\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001782 return -EFAULT;
Dean Anderson38f993a2008-06-26 23:15:51 -03001783 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001784 /* allocate + initialize per filehandle data */
1785 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Dean Andersona5ef91c2010-04-08 23:46:08 -03001786 if (NULL == fh)
Dean Anderson38f993a2008-06-26 23:15:51 -03001787 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03001788 file->private_data = fh;
1789 fh->dev = dev;
1790 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1791 fh->mode = dev->mode[cur_channel];
1792 fh->fmt = dev->cur_fmt[cur_channel];
1793 /* default 4CIF NTSC */
1794 fh->width = LINE_SZ_4CIFS_NTSC;
1795 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1796 fh->channel = cur_channel;
Dean Anderson14d96262008-08-25 13:58:55 -03001797 /* configure channel to default state */
1798 if (!dev->chn_configured[cur_channel]) {
1799 s2255_set_mode(dev, cur_channel, &fh->mode);
1800 dev->chn_configured[cur_channel] = 1;
1801 }
Dean Anderson85b85482010-04-08 23:51:17 -03001802 dprintk(1, "%s: dev=%s type=%s\n", __func__,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001803 video_device_node_name(vdev), v4l2_type_names[type]);
Dean Anderson85b85482010-04-08 23:51:17 -03001804 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001805 (unsigned long)fh, (unsigned long)dev,
1806 (unsigned long)&dev->vidq[cur_channel]);
Dean Anderson85b85482010-04-08 23:51:17 -03001807 dprintk(4, "%s: list_empty active=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001808 list_empty(&dev->vidq[cur_channel].active));
Dean Anderson38f993a2008-06-26 23:15:51 -03001809 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1810 NULL, &dev->slock,
1811 fh->type,
1812 V4L2_FIELD_INTERLACED,
1813 sizeof(struct s2255_buffer), fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001814 return 0;
1815}
1816
1817
1818static unsigned int s2255_poll(struct file *file,
1819 struct poll_table_struct *wait)
1820{
1821 struct s2255_fh *fh = file->private_data;
1822 int rc;
1823 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001824 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1825 return POLLERR;
Dean Anderson38f993a2008-06-26 23:15:51 -03001826 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1827 return rc;
1828}
1829
1830static void s2255_destroy(struct kref *kref)
1831{
1832 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001833 /* board shutdown stops the read pipe if it is running */
1834 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001835 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001836 del_timer(&dev->timer); /* only started in .probe and .open */
Dean Anderson38f993a2008-06-26 23:15:51 -03001837 if (dev->fw_data->fw_urb) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001838 usb_kill_urb(dev->fw_data->fw_urb);
1839 usb_free_urb(dev->fw_data->fw_urb);
1840 dev->fw_data->fw_urb = NULL;
1841 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001842 if (dev->fw_data->fw)
1843 release_firmware(dev->fw_data->fw);
1844 kfree(dev->fw_data->pfw_data);
1845 kfree(dev->fw_data);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001846 /* reset the DSP so firmware can be reloaded next time */
1847 s2255_reset_dsppower(dev);
1848 mutex_destroy(&dev->open_lock);
1849 mutex_destroy(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001850 usb_put_dev(dev->udev);
1851 dprintk(1, "%s", __func__);
Dean Andersonb7732a32009-03-30 11:59:56 -03001852 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001853}
1854
Dean Andersonff7e22d2010-04-08 23:38:07 -03001855static int s2255_release(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001856{
1857 struct s2255_fh *fh = file->private_data;
1858 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001859 struct video_device *vdev = video_devdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001860 if (!dev)
1861 return -ENODEV;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001862 /* turn off stream */
1863 if (res_check(fh)) {
1864 if (dev->b_acquire[fh->channel])
1865 s2255_stop_acquire(dev, fh->channel);
1866 videobuf_streamoff(&fh->vb_vidq);
1867 res_free(dev, fh);
1868 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001869 videobuf_mmap_free(&fh->vb_vidq);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001870 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
Dean Andersonf78d92c2008-07-22 14:43:27 -03001871 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001872 return 0;
1873}
1874
1875static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1876{
1877 struct s2255_fh *fh = file->private_data;
1878 int ret;
1879
1880 if (!fh)
1881 return -ENODEV;
Dean Anderson85b85482010-04-08 23:51:17 -03001882 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
Dean Anderson38f993a2008-06-26 23:15:51 -03001883 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Dean Anderson85b85482010-04-08 23:51:17 -03001884 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001885 (unsigned long)vma->vm_start,
1886 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
Dean Anderson38f993a2008-06-26 23:15:51 -03001887 return ret;
1888}
1889
Hans Verkuilbec43662008-12-30 06:58:20 -03001890static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001891 .owner = THIS_MODULE,
1892 .open = s2255_open,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001893 .release = s2255_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001894 .poll = s2255_poll,
1895 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001896 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001897};
1898
Hans Verkuila3998102008-07-21 02:57:38 -03001899static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001900 .vidioc_querymenu = vidioc_querymenu,
Dean Anderson38f993a2008-06-26 23:15:51 -03001901 .vidioc_querycap = vidioc_querycap,
1902 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1903 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1904 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1905 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1906 .vidioc_reqbufs = vidioc_reqbufs,
1907 .vidioc_querybuf = vidioc_querybuf,
1908 .vidioc_qbuf = vidioc_qbuf,
1909 .vidioc_dqbuf = vidioc_dqbuf,
1910 .vidioc_s_std = vidioc_s_std,
1911 .vidioc_enum_input = vidioc_enum_input,
1912 .vidioc_g_input = vidioc_g_input,
1913 .vidioc_s_input = vidioc_s_input,
1914 .vidioc_queryctrl = vidioc_queryctrl,
1915 .vidioc_g_ctrl = vidioc_g_ctrl,
1916 .vidioc_s_ctrl = vidioc_s_ctrl,
1917 .vidioc_streamon = vidioc_streamon,
1918 .vidioc_streamoff = vidioc_streamoff,
1919#ifdef CONFIG_VIDEO_V4L1_COMPAT
1920 .vidiocgmbuf = vidioc_cgmbuf,
1921#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001922 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1923 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001924 .vidioc_s_parm = vidioc_s_parm,
1925 .vidioc_g_parm = vidioc_g_parm,
Dean Andersone6b44bc2010-03-08 20:04:48 -03001926 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Hans Verkuila3998102008-07-21 02:57:38 -03001927};
1928
Dean Andersonff7e22d2010-04-08 23:38:07 -03001929static void s2255_video_device_release(struct video_device *vdev)
1930{
1931 struct s2255_dev *dev = video_get_drvdata(vdev);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001932 kref_put(&dev->kref, s2255_destroy);
1933 return;
1934}
1935
Hans Verkuila3998102008-07-21 02:57:38 -03001936static struct video_device template = {
1937 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001938 .fops = &s2255_fops_v4l,
1939 .ioctl_ops = &s2255_ioctl_ops,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001940 .release = s2255_video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001941 .tvnorms = S2255_NORMS,
1942 .current_norm = V4L2_STD_NTSC_M,
1943};
1944
1945static int s2255_probe_v4l(struct s2255_dev *dev)
1946{
1947 int ret;
1948 int i;
1949 int cur_nr = video_nr;
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001950 for (i = 0; i < MAX_CHANNELS; i++) {
1951 ret = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev[i]);
1952 if (ret)
1953 goto unreg_v4l2;
1954 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001955 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03001956 /* register 4 video devices */
1957 for (i = 0; i < MAX_CHANNELS; i++) {
1958 INIT_LIST_HEAD(&dev->vidq[i].active);
1959 dev->vidq[i].dev = dev;
1960 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001961 /* register 4 video devices */
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001962 memcpy(&dev->vdev[i], &template, sizeof(struct video_device));
1963 dev->vdev[i].parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001964 if (video_nr == -1)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001965 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001966 VFL_TYPE_GRABBER,
1967 video_nr);
1968 else
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001969 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001970 VFL_TYPE_GRABBER,
1971 cur_nr + i);
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001972 video_set_drvdata(&dev->vdev[i], dev);
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001973 if (ret) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001974 dev_err(&dev->udev->dev,
1975 "failed to register video device!\n");
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001976 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001977 }
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001978 dev->channels++;
1979 v4l2_info(&dev->v4l2_dev[i], "V4L2 device registered as %s\n",
1980 video_device_node_name(&dev->vdev[i]));
1981
Dean Anderson38f993a2008-06-26 23:15:51 -03001982 }
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001983
Dean Andersonabce21f2009-04-23 16:04:41 -03001984 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1985 S2255_MAJOR_VERSION,
1986 S2255_MINOR_VERSION);
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03001987 /* if no channels registered, return error and probe will fail*/
1988 if (dev->channels == 0)
1989 return ret;
1990 if (dev->channels != MAX_CHANNELS)
1991 printk(KERN_WARNING "s2255: Not all channels available.\n");
1992 return 0;
1993unreg_v4l2:
1994 for (i-- ; i > 0; i--)
1995 v4l2_device_unregister(&dev->v4l2_dev[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001996 return ret;
1997}
1998
Dean Anderson38f993a2008-06-26 23:15:51 -03001999/* this function moves the usb stream read pipe data
2000 * into the system buffers.
2001 * returns 0 on success, EAGAIN if more data to process( call this
2002 * function again).
2003 *
2004 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03002005 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03002006 * bytes 4-7: channel: 0-3
2007 * bytes 8-11: payload size: size of the frame
2008 * bytes 12-payloadsize+12: frame data
2009 */
2010static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
2011{
Dean Anderson38f993a2008-06-26 23:15:51 -03002012 char *pdest;
2013 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002014 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002015 char *psrc;
2016 unsigned long copy_size;
2017 unsigned long size;
2018 s32 idx = -1;
2019 struct s2255_framei *frm;
2020 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002021
Dean Anderson38f993a2008-06-26 23:15:51 -03002022 dprintk(100, "buffer to user\n");
2023
2024 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03002025 frm = &dev->buffer[dev->cc].frame[idx];
2026
Dean Anderson14d96262008-08-25 13:58:55 -03002027 if (frm->ulState == S2255_READ_IDLE) {
2028 int jj;
2029 unsigned int cc;
Dean Anderson3fa00602010-03-04 20:47:33 -03002030 __le32 *pdword; /*data from dsp is little endian */
Dean Anderson14d96262008-08-25 13:58:55 -03002031 int payload;
2032 /* search for marker codes */
2033 pdata = (unsigned char *)pipe_info->transfer_buffer;
Dean Anderson3fa00602010-03-04 20:47:33 -03002034 pdword = (__le32 *)pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002035 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
Dean Anderson3fa00602010-03-04 20:47:33 -03002036 switch (*pdword) {
Dean Anderson14d96262008-08-25 13:58:55 -03002037 case S2255_MARKER_FRAME:
Dean Anderson14d96262008-08-25 13:58:55 -03002038 dprintk(4, "found frame marker at offset:"
2039 " %d [%x %x]\n", jj, pdata[0],
2040 pdata[1]);
2041 offset = jj + PREFIX_SIZE;
2042 bframe = 1;
2043 cc = pdword[1];
2044 if (cc >= MAX_CHANNELS) {
2045 printk(KERN_ERR
2046 "bad channel\n");
2047 return -EINVAL;
2048 }
2049 /* reverse it */
2050 dev->cc = G_chnmap[cc];
2051 payload = pdword[3];
2052 if (payload > dev->req_image_size[dev->cc]) {
2053 dev->bad_payload[dev->cc]++;
2054 /* discard the bad frame */
2055 return -EINVAL;
2056 }
2057 dev->pkt_size[dev->cc] = payload;
2058 dev->jpg_size[dev->cc] = pdword[4];
2059 break;
2060 case S2255_MARKER_RESPONSE:
Dean Anderson14d96262008-08-25 13:58:55 -03002061 pdata += DEF_USB_BLOCK;
2062 jj += DEF_USB_BLOCK;
2063 if (pdword[1] >= MAX_CHANNELS)
2064 break;
2065 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03002066 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03002067 break;
2068 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03002069 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03002070 /* check if channel valid */
2071 /* set mode ready */
2072 dev->setmode_ready[cc] = 1;
2073 wake_up(&dev->wait_setmode[cc]);
2074 dprintk(5, "setmode ready %d\n", cc);
2075 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03002076 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03002077
2078 dev->chn_ready |= (1 << cc);
2079 if ((dev->chn_ready & 0x0f) != 0x0f)
2080 break;
2081 /* all channels ready */
2082 printk(KERN_INFO "s2255: fw loaded\n");
2083 atomic_set(&dev->fw_data->fw_state,
2084 S2255_FW_SUCCESS);
2085 wake_up(&dev->fw_data->wait_fw);
2086 break;
Dean Anderson4de39f52010-03-03 19:39:19 -03002087 case S2255_RESPONSE_STATUS:
2088 dev->vidstatus[cc] = pdword[3];
2089 dev->vidstatus_ready[cc] = 1;
2090 wake_up(&dev->wait_vidstatus[cc]);
2091 dprintk(5, "got vidstatus %x chan %d\n",
2092 pdword[3], cc);
2093 break;
Dean Anderson14d96262008-08-25 13:58:55 -03002094 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002095 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002096 }
2097 default:
2098 pdata++;
2099 break;
2100 }
2101 if (bframe)
2102 break;
2103 } /* for */
2104 if (!bframe)
2105 return -EINVAL;
2106 }
2107
2108
2109 idx = dev->cur_frame[dev->cc];
2110 frm = &dev->buffer[dev->cc].frame[idx];
2111
2112 /* search done. now find out if should be acquiring on this channel */
2113 if (!dev->b_acquire[dev->cc]) {
2114 /* we found a frame, but this channel is turned off */
2115 frm->ulState = S2255_READ_IDLE;
2116 return -EINVAL;
2117 }
2118
2119 if (frm->ulState == S2255_READ_IDLE) {
2120 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002121 frm->cur_size = 0;
2122 }
2123
Dean Anderson14d96262008-08-25 13:58:55 -03002124 /* skip the marker 512 bytes (and offset if out of sync) */
2125 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2126
Dean Anderson38f993a2008-06-26 23:15:51 -03002127
2128 if (frm->lpvbits == NULL) {
2129 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2130 frm, dev, dev->cc, idx);
2131 return -ENOMEM;
2132 }
2133
2134 pdest = frm->lpvbits + frm->cur_size;
2135
Dean Anderson14d96262008-08-25 13:58:55 -03002136 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002137
Dean Anderson14d96262008-08-25 13:58:55 -03002138 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002139
Dean Anderson14d96262008-08-25 13:58:55 -03002140 /* sanity check on pdest */
2141 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2142 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002143
Dean Anderson38f993a2008-06-26 23:15:51 -03002144 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002145 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002146
Dean Anderson14d96262008-08-25 13:58:55 -03002147 if (frm->cur_size >= size) {
2148
Dean Anderson38f993a2008-06-26 23:15:51 -03002149 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002150 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2151 cc, idx);
2152 dev->last_frame[cc] = dev->cur_frame[cc];
2153 dev->cur_frame[cc]++;
2154 /* end of system frame ring buffer, start at zero */
2155 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2156 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2157 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002158 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002159 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002160 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002161 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002162 frm->ulState = S2255_READ_IDLE;
2163 frm->cur_size = 0;
2164
Dean Anderson38f993a2008-06-26 23:15:51 -03002165 }
2166 /* done successfully */
2167 return 0;
2168}
2169
2170static void s2255_read_video_callback(struct s2255_dev *dev,
2171 struct s2255_pipeinfo *pipe_info)
2172{
2173 int res;
2174 dprintk(50, "callback read video \n");
2175
2176 if (dev->cc >= MAX_CHANNELS) {
2177 dev->cc = 0;
2178 dev_err(&dev->udev->dev, "invalid channel\n");
2179 return;
2180 }
2181 /* otherwise copy to the system buffers */
2182 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002183 if (res != 0)
2184 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002185
2186 dprintk(50, "callback read video done\n");
2187 return;
2188}
2189
2190static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2191 u16 Index, u16 Value, void *TransferBuffer,
2192 s32 TransferBufferLength, int bOut)
2193{
2194 int r;
2195 if (!bOut) {
2196 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2197 Request,
2198 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2199 USB_DIR_IN,
2200 Value, Index, TransferBuffer,
2201 TransferBufferLength, HZ * 5);
2202 } else {
2203 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2204 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2205 Value, Index, TransferBuffer,
2206 TransferBufferLength, HZ * 5);
2207 }
2208 return r;
2209}
2210
2211/*
2212 * retrieve FX2 firmware version. future use.
2213 * @param dev pointer to device extension
2214 * @return -1 for fail, else returns firmware version as an int(16 bits)
2215 */
2216static int s2255_get_fx2fw(struct s2255_dev *dev)
2217{
2218 int fw;
2219 int ret;
2220 unsigned char transBuffer[64];
2221 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2222 S2255_VR_IN);
2223 if (ret < 0)
2224 dprintk(2, "get fw error: %x\n", ret);
2225 fw = transBuffer[0] + (transBuffer[1] << 8);
2226 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2227 return fw;
2228}
2229
2230/*
2231 * Create the system ring buffer to copy frames into from the
2232 * usb read pipe.
2233 */
2234static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2235{
2236 unsigned long i;
2237 unsigned long reqsize;
2238 dprintk(1, "create sys buffers\n");
2239 if (chn >= MAX_CHANNELS)
2240 return -1;
2241
2242 dev->buffer[chn].dwFrames = SYS_FRAMES;
2243
2244 /* always allocate maximum size(PAL) for system buffers */
2245 reqsize = SYS_FRAMES_MAXSIZE;
2246
2247 if (reqsize > SYS_FRAMES_MAXSIZE)
2248 reqsize = SYS_FRAMES_MAXSIZE;
2249
2250 for (i = 0; i < SYS_FRAMES; i++) {
2251 /* allocate the frames */
2252 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2253
2254 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2255 &dev->buffer[chn].frame[i], chn, i,
2256 dev->buffer[chn].frame[i].lpvbits);
2257 dev->buffer[chn].frame[i].size = reqsize;
2258 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2259 printk(KERN_INFO "out of memory. using less frames\n");
2260 dev->buffer[chn].dwFrames = i;
2261 break;
2262 }
2263 }
2264
2265 /* make sure internal states are set */
2266 for (i = 0; i < SYS_FRAMES; i++) {
2267 dev->buffer[chn].frame[i].ulState = 0;
2268 dev->buffer[chn].frame[i].cur_size = 0;
2269 }
2270
2271 dev->cur_frame[chn] = 0;
2272 dev->last_frame[chn] = -1;
2273 return 0;
2274}
2275
2276static int s2255_release_sys_buffers(struct s2255_dev *dev,
2277 unsigned long channel)
2278{
2279 unsigned long i;
2280 dprintk(1, "release sys buffers\n");
2281 for (i = 0; i < SYS_FRAMES; i++) {
2282 if (dev->buffer[channel].frame[i].lpvbits) {
2283 dprintk(1, "vfree %p\n",
2284 dev->buffer[channel].frame[i].lpvbits);
2285 vfree(dev->buffer[channel].frame[i].lpvbits);
2286 }
2287 dev->buffer[channel].frame[i].lpvbits = NULL;
2288 }
2289 return 0;
2290}
2291
2292static int s2255_board_init(struct s2255_dev *dev)
2293{
Dean Anderson38f993a2008-06-26 23:15:51 -03002294 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2295 int fw_ver;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002296 int j;
2297 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002298 dprintk(4, "board init: %p", dev);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002299 memset(pipe, 0, sizeof(*pipe));
2300 pipe->dev = dev;
2301 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2302 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002303
Dean Andersonab85c6a2010-04-08 23:39:12 -03002304 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2305 GFP_KERNEL);
2306 if (pipe->transfer_buffer == NULL) {
2307 dprintk(1, "out of memory!\n");
2308 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002309 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002310 /* query the firmware */
2311 fw_ver = s2255_get_fx2fw(dev);
2312
Dean Andersonabce21f2009-04-23 16:04:41 -03002313 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2314 (fw_ver >> 8) & 0xff,
2315 fw_ver & 0xff);
2316
2317 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002318 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002319 "usb firmware not up to date %d.%d\n",
2320 (fw_ver >> 8) & 0xff,
2321 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002322
2323 for (j = 0; j < MAX_CHANNELS; j++) {
2324 dev->b_acquire[j] = 0;
2325 dev->mode[j] = mode_def;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002326 if (dev->pid == 0x2257 && j > 1)
2327 dev->mode[j].color |= (1 << 16);
Dean Anderson22b88d42008-08-29 15:33:19 -03002328 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002329 dev->cur_fmt[j] = &formats[0];
2330 dev->mode[j].restart = 1;
2331 dev->req_image_size[j] = get_transfer_size(&mode_def);
2332 dev->frame_count[j] = 0;
2333 /* create the system buffers */
2334 s2255_create_sys_buffers(dev, j);
2335 }
2336 /* start read pipe */
2337 s2255_start_readpipe(dev);
Dean Anderson85b85482010-04-08 23:51:17 -03002338 dprintk(1, "%s: success\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002339 return 0;
2340}
2341
2342static int s2255_board_shutdown(struct s2255_dev *dev)
2343{
2344 u32 i;
Dean Anderson85b85482010-04-08 23:51:17 -03002345 dprintk(1, "%s: dev: %p", __func__, dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002346
2347 for (i = 0; i < MAX_CHANNELS; i++) {
2348 if (dev->b_acquire[i])
2349 s2255_stop_acquire(dev, i);
2350 }
2351
2352 s2255_stop_readpipe(dev);
2353
2354 for (i = 0; i < MAX_CHANNELS; i++)
2355 s2255_release_sys_buffers(dev, i);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002356 /* release transfer buffer */
2357 kfree(dev->pipe.transfer_buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03002358 return 0;
2359}
2360
2361static void read_pipe_completion(struct urb *purb)
2362{
2363 struct s2255_pipeinfo *pipe_info;
2364 struct s2255_dev *dev;
2365 int status;
2366 int pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002367 pipe_info = purb->context;
Dean Anderson85b85482010-04-08 23:51:17 -03002368 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
Dean Anderson38f993a2008-06-26 23:15:51 -03002369 purb->status);
2370 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002371 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002372 return;
2373 }
2374
2375 dev = pipe_info->dev;
2376 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002377 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002378 return;
2379 }
2380 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002381 /* if shutting down, do not resubmit, exit immediately */
2382 if (status == -ESHUTDOWN) {
Dean Anderson85b85482010-04-08 23:51:17 -03002383 dprintk(2, "%s: err shutdown\n", __func__);
Dean Andersonb02064c2009-04-30 12:29:38 -03002384 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002385 return;
2386 }
2387
2388 if (pipe_info->state == 0) {
Dean Anderson85b85482010-04-08 23:51:17 -03002389 dprintk(2, "%s: exiting USB pipe", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002390 return;
2391 }
2392
Dean Andersonb02064c2009-04-30 12:29:38 -03002393 if (status == 0)
2394 s2255_read_video_callback(dev, pipe_info);
2395 else {
2396 pipe_info->err_count++;
Dean Anderson85b85482010-04-08 23:51:17 -03002397 dprintk(1, "%s: failed URB %d\n", __func__, status);
Dean Andersonb02064c2009-04-30 12:29:38 -03002398 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002399
Dean Anderson38f993a2008-06-26 23:15:51 -03002400 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2401 /* reuse urb */
2402 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2403 pipe,
2404 pipe_info->transfer_buffer,
2405 pipe_info->cur_transfer_size,
2406 read_pipe_completion, pipe_info);
2407
2408 if (pipe_info->state != 0) {
2409 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2410 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002411 }
2412 } else {
Dean Anderson85b85482010-04-08 23:51:17 -03002413 dprintk(2, "%s :complete state 0\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002414 }
2415 return;
2416}
2417
2418static int s2255_start_readpipe(struct s2255_dev *dev)
2419{
2420 int pipe;
2421 int retval;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002422 struct s2255_pipeinfo *pipe_info = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002423 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
Dean Anderson85b85482010-04-08 23:51:17 -03002424 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002425 pipe_info->state = 1;
2426 pipe_info->err_count = 0;
2427 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2428 if (!pipe_info->stream_urb) {
2429 dev_err(&dev->udev->dev,
2430 "ReadStream: Unable to alloc URB\n");
2431 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002432 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002433 /* transfer buffer allocated in board_init */
2434 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2435 pipe,
2436 pipe_info->transfer_buffer,
2437 pipe_info->cur_transfer_size,
2438 read_pipe_completion, pipe_info);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002439 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2440 if (retval) {
2441 printk(KERN_ERR "s2255: start read pipe failed\n");
2442 return retval;
2443 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002444 return 0;
2445}
2446
2447/* starts acquisition process */
2448static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2449{
2450 unsigned char *buffer;
2451 int res;
2452 unsigned long chn_rev;
2453 int j;
2454 if (chn >= MAX_CHANNELS) {
2455 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2456 return -1;
2457 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002458 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002459 buffer = kzalloc(512, GFP_KERNEL);
2460 if (buffer == NULL) {
2461 dev_err(&dev->udev->dev, "out of mem\n");
2462 return -ENOMEM;
2463 }
2464
2465 dev->last_frame[chn] = -1;
2466 dev->bad_payload[chn] = 0;
2467 dev->cur_frame[chn] = 0;
2468 for (j = 0; j < SYS_FRAMES; j++) {
2469 dev->buffer[chn].frame[j].ulState = 0;
2470 dev->buffer[chn].frame[j].cur_size = 0;
2471 }
2472
2473 /* send the start command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002474 *(__le32 *) buffer = IN_DATA_TOKEN;
2475 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2476 *((__le32 *) buffer + 2) = CMD_START;
Dean Anderson38f993a2008-06-26 23:15:51 -03002477 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2478 if (res != 0)
2479 dev_err(&dev->udev->dev, "CMD_START error\n");
2480
2481 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2482 kfree(buffer);
2483 return 0;
2484}
2485
2486static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2487{
2488 unsigned char *buffer;
2489 int res;
2490 unsigned long chn_rev;
Dean Anderson38f993a2008-06-26 23:15:51 -03002491 if (chn >= MAX_CHANNELS) {
2492 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2493 return -1;
2494 }
2495 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002496 buffer = kzalloc(512, GFP_KERNEL);
2497 if (buffer == NULL) {
2498 dev_err(&dev->udev->dev, "out of mem\n");
2499 return -ENOMEM;
2500 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002501 /* send the stop command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002502 *(__le32 *) buffer = IN_DATA_TOKEN;
2503 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2504 *((__le32 *) buffer + 2) = CMD_STOP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002505 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
Dean Anderson38f993a2008-06-26 23:15:51 -03002506 if (res != 0)
2507 dev_err(&dev->udev->dev, "CMD_STOP error\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002508 kfree(buffer);
2509 dev->b_acquire[chn] = 0;
Dean Anderson85b85482010-04-08 23:51:17 -03002510 dprintk(4, "%s: chn %lu, res %d\n", __func__, chn, res);
Dean Anderson14d96262008-08-25 13:58:55 -03002511 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002512}
2513
2514static void s2255_stop_readpipe(struct s2255_dev *dev)
2515{
Dean Andersonab85c6a2010-04-08 23:39:12 -03002516 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002517 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002518 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002519 return;
2520 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002521 pipe->state = 0;
2522 if (pipe->stream_urb) {
2523 /* cancel urb */
2524 usb_kill_urb(pipe->stream_urb);
2525 usb_free_urb(pipe->stream_urb);
2526 pipe->stream_urb = NULL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002527 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002528 dprintk(4, "%s", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002529 return;
2530}
2531
Dean Anderson14d96262008-08-25 13:58:55 -03002532static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002533{
Dean Anderson14d96262008-08-25 13:58:55 -03002534 if (reset)
2535 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002536 dev->fw_data->fw_size = dev->fw_data->fw->size;
2537 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2538 memcpy(dev->fw_data->pfw_data,
2539 dev->fw_data->fw->data, CHUNK_SIZE);
2540 dev->fw_data->fw_loaded = CHUNK_SIZE;
2541 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2542 usb_sndbulkpipe(dev->udev, 2),
2543 dev->fw_data->pfw_data,
2544 CHUNK_SIZE, s2255_fwchunk_complete,
2545 dev->fw_data);
2546 mod_timer(&dev->timer, jiffies + HZ);
2547}
2548
2549/* standard usb probe function */
2550static int s2255_probe(struct usb_interface *interface,
2551 const struct usb_device_id *id)
2552{
2553 struct s2255_dev *dev = NULL;
2554 struct usb_host_interface *iface_desc;
2555 struct usb_endpoint_descriptor *endpoint;
2556 int i;
2557 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002558 __le32 *pdata;
2559 int fw_size;
Dean Anderson85b85482010-04-08 23:51:17 -03002560 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002561 /* allocate memory for our device state and initialize it to zero */
2562 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2563 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002564 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002565 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002566 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002567 kref_init(&dev->kref);
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002568 dev->pid = id->idProduct;
Dean Anderson38f993a2008-06-26 23:15:51 -03002569 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2570 if (!dev->fw_data)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002571 goto errorFWDATA1;
Dean Anderson38f993a2008-06-26 23:15:51 -03002572 mutex_init(&dev->lock);
2573 mutex_init(&dev->open_lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03002574 /* grab usb_device and save it */
2575 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2576 if (dev->udev == NULL) {
2577 dev_err(&interface->dev, "null usb device\n");
2578 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002579 goto errorUDEV;
Dean Anderson38f993a2008-06-26 23:15:51 -03002580 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002581 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2582 dev->udev, interface);
2583 dev->interface = interface;
2584 /* set up the endpoint information */
2585 iface_desc = interface->cur_altsetting;
2586 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2587 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2588 endpoint = &iface_desc->endpoint[i].desc;
2589 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2590 /* we found the bulk in endpoint */
2591 dev->read_endpoint = endpoint->bEndpointAddress;
2592 }
2593 }
2594
2595 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002596 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002597 goto errorEP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002598 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002599 /* set intfdata */
2600 usb_set_intfdata(interface, dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002601 init_timer(&dev->timer);
2602 dev->timer.function = s2255_timer;
2603 dev->timer.data = (unsigned long)dev->fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -03002604 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson4de39f52010-03-03 19:39:19 -03002605 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03002606 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002607 init_waitqueue_head(&dev->wait_vidstatus[i]);
2608 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002609
2610 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
Dean Anderson38f993a2008-06-26 23:15:51 -03002611 if (!dev->fw_data->fw_urb) {
2612 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002613 goto errorFWURB;
Dean Anderson38f993a2008-06-26 23:15:51 -03002614 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002615
Dean Anderson38f993a2008-06-26 23:15:51 -03002616 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2617 if (!dev->fw_data->pfw_data) {
2618 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002619 goto errorFWDATA2;
Dean Anderson38f993a2008-06-26 23:15:51 -03002620 }
2621 /* load the first chunk */
2622 if (request_firmware(&dev->fw_data->fw,
2623 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2624 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002625 goto errorREQFW;
Dean Anderson38f993a2008-06-26 23:15:51 -03002626 }
Dean Anderson14d96262008-08-25 13:58:55 -03002627 /* check the firmware is valid */
2628 fw_size = dev->fw_data->fw->size;
2629 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002630
Dean Anderson14d96262008-08-25 13:58:55 -03002631 if (*pdata != S2255_FW_MARKER) {
2632 printk(KERN_INFO "Firmware invalid.\n");
2633 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002634 goto errorFWMARKER;
Dean Anderson14d96262008-08-25 13:58:55 -03002635 } else {
2636 /* make sure firmware is the latest */
2637 __le32 *pRel;
2638 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2639 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
Dean Anderson4de39f52010-03-03 19:39:19 -03002640 dev->dsp_fw_ver = *pRel;
2641 if (*pRel < S2255_CUR_DSP_FWVER)
2642 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002643 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002644 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2645 "or above.\n", S2255_MIN_DSP_COLORFILTER);
Dean Anderson14d96262008-08-25 13:58:55 -03002646 }
Dean Anderson14d96262008-08-25 13:58:55 -03002647 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002648 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002649 retval = s2255_board_init(dev);
2650 if (retval)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002651 goto errorBOARDINIT;
Dean Anderson38f993a2008-06-26 23:15:51 -03002652 spin_lock_init(&dev->slock);
Dean Anderson14d96262008-08-25 13:58:55 -03002653 s2255_fwload_start(dev, 0);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002654 /* kref for each vdev. Released on video_device_release callback */
2655 for (i = 0; i < MAX_CHANNELS; i++)
2656 kref_get(&dev->kref);
2657 /* loads v4l specific */
2658 retval = s2255_probe_v4l(dev);
2659 if (retval)
Dean Anderson3a67b5cc2010-04-08 23:52:20 -03002660 goto errorBOARDINIT;
Dean Anderson38f993a2008-06-26 23:15:51 -03002661 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2662 return 0;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002663errorBOARDINIT:
2664 s2255_board_shutdown(dev);
2665errorFWMARKER:
2666 release_firmware(dev->fw_data->fw);
2667errorREQFW:
2668 kfree(dev->fw_data->pfw_data);
2669errorFWDATA2:
2670 usb_free_urb(dev->fw_data->fw_urb);
2671errorFWURB:
2672 del_timer(&dev->timer);
2673errorEP:
2674 usb_put_dev(dev->udev);
2675errorUDEV:
2676 kfree(dev->fw_data);
2677 mutex_destroy(&dev->open_lock);
2678 mutex_destroy(&dev->lock);
2679errorFWDATA1:
2680 kfree(dev);
2681 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
Dean Anderson38f993a2008-06-26 23:15:51 -03002682 return retval;
2683}
2684
Dean Andersonff7e22d2010-04-08 23:38:07 -03002685
Dean Anderson38f993a2008-06-26 23:15:51 -03002686/* disconnect routine. when board is removed physically or with rmmod */
2687static void s2255_disconnect(struct usb_interface *interface)
2688{
2689 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002690 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002691 dev = usb_get_intfdata(interface);
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 Anderson14d96262008-08-25 13:58:55 -03002705 usb_set_intfdata(interface, NULL);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002706 kref_put(&dev->kref, s2255_destroy);
2707 dev_info(&interface->dev, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002708}
2709
2710static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002711 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002712 .probe = s2255_probe,
2713 .disconnect = s2255_disconnect,
2714 .id_table = s2255_table,
2715};
2716
2717static int __init usb_s2255_init(void)
2718{
2719 int result;
Dean Anderson38f993a2008-06-26 23:15:51 -03002720 /* register this driver with the USB subsystem */
2721 result = usb_register(&s2255_driver);
Dean Anderson38f993a2008-06-26 23:15:51 -03002722 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002723 pr_err(KBUILD_MODNAME
Dean Andersonff7e22d2010-04-08 23:38:07 -03002724 ": usb_register failed. Error number %d\n", result);
2725 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002726 return result;
2727}
2728
2729static void __exit usb_s2255_exit(void)
2730{
2731 usb_deregister(&s2255_driver);
2732}
2733
2734module_init(usb_s2255_init);
2735module_exit(usb_s2255_exit);
2736
2737MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2738MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2739MODULE_LICENSE("GPL");