blob: aba7a72f355f67f5bcfc4d213dad419fec93ed25 [file] [log] [blame]
Dean Anderson38f993a2008-06-26 23:15:51 -03001/*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
3 *
Dean Anderson4de39f52010-03-03 19:39:19 -03004 * Copyright (C) 2007-2010 by Sensoray Company Inc.
Dean Anderson38f993a2008-06-26 23:15:51 -03005 * Dean Anderson
6 *
7 * Some video buffer code based on vivi driver:
8 *
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
12 *
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
15 *
16 * Example maximum bandwidth utilization:
17 *
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
19 *
20 * -full or half size Grey scale: all 4 channels at once
21 *
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
23 *
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
25 * at once.
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#include <linux/module.h>
45#include <linux/firmware.h>
46#include <linux/kernel.h>
47#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030049#include <linux/videodev2.h>
50#include <linux/version.h>
Hans Verkuilfeb75f02008-07-27 06:30:21 -030051#include <linux/mm.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040052#include <linux/smp_lock.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030053#include <media/videobuf-vmalloc.h>
54#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030055#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030056#include <linux/vmalloc.h>
57#include <linux/usb.h>
58
Dean Anderson85b85482010-04-08 23:51:17 -030059#define S2255_MAJOR_VERSION 1
60#define S2255_MINOR_VERSION 19
61#define S2255_RELEASE 0
62#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
63 S2255_MINOR_VERSION, \
64 S2255_RELEASE)
Dean Anderson38f993a2008-06-26 23:15:51 -030065#define FIRMWARE_FILE_NAME "f2255usb.bin"
66
Dean Anderson22b88d42008-08-29 15:33:19 -030067/* default JPEG quality */
68#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030069/* vendor request in */
70#define S2255_VR_IN 0
71/* vendor request out */
72#define S2255_VR_OUT 1
73/* firmware query */
74#define S2255_VR_FW 0x30
75/* USB endpoint number for configuring the device */
76#define S2255_CONFIG_EP 2
77/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030078#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030079/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030080#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030081#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030082#define S2255_SETMODE_TIMEOUT 500
Dean Anderson4de39f52010-03-03 19:39:19 -030083#define S2255_VIDSTATUS_TIMEOUT 350
Dean Anderson38f993a2008-06-26 23:15:51 -030084#define MAX_CHANNELS 4
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 Anderson38f993a2008-06-26 23:15:51 -0300232 int frames;
Dean Anderson38f993a2008-06-26 23:15:51 -0300233 struct mutex lock;
234 struct mutex open_lock;
235 int resources[MAX_CHANNELS];
236 struct usb_device *udev;
237 struct usb_interface *interface;
238 u8 read_endpoint;
239
240 struct s2255_dmaqueue vidq[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300241 struct timer_list timer;
242 struct s2255_fw *fw_data;
Dean Andersonab85c6a2010-04-08 23:39:12 -0300243 struct s2255_pipeinfo pipe;
244 struct s2255_bufferi buffer[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300245 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300246 /* jpeg compression */
247 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300248 /* capture parameters (for high quality mode full size) */
249 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300250 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
251 int cur_frame[MAX_CHANNELS];
252 int last_frame[MAX_CHANNELS];
253 u32 cc; /* current channel */
254 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300255 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300256 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300257 /* received packet size */
258 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300259 int bad_payload[MAX_CHANNELS];
260 unsigned long frame_count[MAX_CHANNELS];
261 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300262 /* if JPEG image */
263 int jpg_size[MAX_CHANNELS];
264 /* if channel configured to default state */
265 int chn_configured[MAX_CHANNELS];
266 wait_queue_head_t wait_setmode[MAX_CHANNELS];
267 int setmode_ready[MAX_CHANNELS];
Dean Anderson4de39f52010-03-03 19:39:19 -0300268 /* video status items */
269 int vidstatus[MAX_CHANNELS];
270 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
271 int vidstatus_ready[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300272 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300273 spinlock_t slock;
Dean Anderson4de39f52010-03-03 19:39:19 -0300274 /* dsp firmware version (f2255usb.bin) */
275 int dsp_fw_ver;
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300276 u16 pid; /* product id */
277 struct kref kref;
Dean Anderson38f993a2008-06-26 23:15:51 -0300278};
279#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
280
281struct s2255_fmt {
282 char *name;
283 u32 fourcc;
284 int depth;
285};
286
287/* buffer for one video frame */
288struct s2255_buffer {
289 /* common v4l buffer stuff -- must be first */
290 struct videobuf_buffer vb;
291 const struct s2255_fmt *fmt;
292};
293
294struct s2255_fh {
295 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300296 const struct s2255_fmt *fmt;
297 unsigned int width;
298 unsigned int height;
299 struct videobuf_queue vb_vidq;
300 enum v4l2_buf_type type;
301 int channel;
302 /* mode below is the desired mode.
303 mode in s2255_dev is the current mode that was last set */
304 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300305 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300306};
307
Dean Andersonabce21f2009-04-23 16:04:41 -0300308/* current cypress EEPROM firmware version */
309#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson4de39f52010-03-03 19:39:19 -0300310/* current DSP FW version */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300311#define S2255_CUR_DSP_FWVER 8
Dean Anderson4de39f52010-03-03 19:39:19 -0300312/* Need DSP version 5+ for video status feature */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300313#define S2255_MIN_DSP_STATUS 5
314#define S2255_MIN_DSP_COLORFILTER 8
Dean Anderson38f993a2008-06-26 23:15:51 -0300315#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300316
317/* private V4L2 controls */
318
319/*
320 * The following chart displays how COLORFILTER should be set
321 * =========================================================
322 * = fourcc = COLORFILTER =
323 * = ===============================
324 * = = 0 = 1 =
325 * =========================================================
326 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
327 * = = s-video or = composite =
328 * = = B/W camera = input =
329 * =========================================================
330 * = other = color, svideo = color, =
331 * = = = composite =
332 * =========================================================
333 *
334 * Notes:
335 * channels 0-3 on 2255 are composite
336 * channels 0-1 on 2257 are composite, 2-3 are s-video
337 * If COLORFILTER is 0 with a composite color camera connected,
338 * the output will appear monochrome but hatching
339 * will occur.
340 * COLORFILTER is different from "color killer" and "color effects"
341 * for reasons above.
342 */
343#define S2255_V4L2_YC_ON 1
344#define S2255_V4L2_YC_OFF 0
345#define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
346
Dean Anderson38f993a2008-06-26 23:15:51 -0300347/* frame prefix size (sent once every frame) */
348#define PREFIX_SIZE 512
349
350/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300351static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300352
Dean Anderson38f993a2008-06-26 23:15:51 -0300353static int debug;
354static int *s2255_debug = &debug;
355
356static int s2255_start_readpipe(struct s2255_dev *dev);
357static void s2255_stop_readpipe(struct s2255_dev *dev);
358static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
359static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
360static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300361 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300362static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
363 struct s2255_mode *mode);
364static int s2255_board_shutdown(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300365static void s2255_fwload_start(struct s2255_dev *dev, int reset);
366static void s2255_destroy(struct kref *kref);
367static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
368 u16 index, u16 value, void *buf,
369 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300370
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300371/* dev_err macro with driver name */
372#define S2255_DRIVER_NAME "s2255"
373#define s2255_dev_err(dev, fmt, arg...) \
374 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
375
Dean Anderson38f993a2008-06-26 23:15:51 -0300376#define dprintk(level, fmt, arg...) \
377 do { \
378 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300379 printk(KERN_DEBUG S2255_DRIVER_NAME \
380 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300381 } \
382 } while (0)
383
Dean Anderson38f993a2008-06-26 23:15:51 -0300384static struct usb_driver s2255_driver;
385
Dean Anderson38f993a2008-06-26 23:15:51 -0300386/* Declare static vars that will be used as parameters */
387static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
388
389/* start video number */
390static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
391
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300392module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300393MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300394module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300395MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300396module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300397MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
398
399/* USB device table */
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300400#define USB_SENSORAY_VID 0x1943
Dean Anderson38f993a2008-06-26 23:15:51 -0300401static struct usb_device_id s2255_table[] = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300402 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
403 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300404 { } /* Terminating entry */
405};
406MODULE_DEVICE_TABLE(usb, s2255_table);
407
Dean Anderson38f993a2008-06-26 23:15:51 -0300408#define BUFFER_TIMEOUT msecs_to_jiffies(400)
409
Dean Anderson38f993a2008-06-26 23:15:51 -0300410/* image formats. */
411static const struct s2255_fmt formats[] = {
412 {
413 .name = "4:2:2, planar, YUV422P",
414 .fourcc = V4L2_PIX_FMT_YUV422P,
415 .depth = 16
416
417 }, {
418 .name = "4:2:2, packed, YUYV",
419 .fourcc = V4L2_PIX_FMT_YUYV,
420 .depth = 16
421
422 }, {
423 .name = "4:2:2, packed, UYVY",
424 .fourcc = V4L2_PIX_FMT_UYVY,
425 .depth = 16
426 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300427 .name = "JPG",
428 .fourcc = V4L2_PIX_FMT_JPEG,
429 .depth = 24
430 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300431 .name = "8bpp GREY",
432 .fourcc = V4L2_PIX_FMT_GREY,
433 .depth = 8
434 }
435};
436
437static int norm_maxw(struct video_device *vdev)
438{
439 return (vdev->current_norm & V4L2_STD_NTSC) ?
440 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
441}
442
443static int norm_maxh(struct video_device *vdev)
444{
445 return (vdev->current_norm & V4L2_STD_NTSC) ?
446 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
447}
448
449static int norm_minw(struct video_device *vdev)
450{
451 return (vdev->current_norm & V4L2_STD_NTSC) ?
452 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
453}
454
455static int norm_minh(struct video_device *vdev)
456{
457 return (vdev->current_norm & V4L2_STD_NTSC) ?
458 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
459}
460
461
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300462/*
463 * TODO: fixme: move YUV reordering to hardware
464 * converts 2255 planar format to yuyv or uyvy
465 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300466static void planar422p_to_yuv_packed(const unsigned char *in,
467 unsigned char *out,
468 int width, int height,
469 int fmt)
470{
471 unsigned char *pY;
472 unsigned char *pCb;
473 unsigned char *pCr;
474 unsigned long size = height * width;
475 unsigned int i;
476 pY = (unsigned char *)in;
477 pCr = (unsigned char *)in + height * width;
478 pCb = (unsigned char *)in + height * width + (height * width / 2);
479 for (i = 0; i < size * 2; i += 4) {
480 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
481 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
482 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
483 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
484 }
485 return;
486}
487
Hans Verkuild45b9b82008-09-04 03:33:43 -0300488static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300489{
490 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
491 msleep(10);
492 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
493 return;
494}
Dean Anderson38f993a2008-06-26 23:15:51 -0300495
496/* kickstarts the firmware loading. from probe
497 */
498static void s2255_timer(unsigned long user_data)
499{
500 struct s2255_fw *data = (struct s2255_fw *)user_data;
Dean Anderson85b85482010-04-08 23:51:17 -0300501 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300502 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
503 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300504 atomic_set(&data->fw_state, S2255_FW_FAILED);
505 /* wake up anything waiting for the firmware */
506 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300507 return;
508 }
509}
510
Dean Anderson38f993a2008-06-26 23:15:51 -0300511
512/* this loads the firmware asynchronously.
513 Originally this was done synchroously in probe.
514 But it is better to load it asynchronously here than block
515 inside the probe function. Blocking inside probe affects boot time.
516 FW loading is triggered by the timer in the probe function
517*/
518static void s2255_fwchunk_complete(struct urb *urb)
519{
520 struct s2255_fw *data = urb->context;
521 struct usb_device *udev = urb->dev;
522 int len;
Dean Anderson85b85482010-04-08 23:51:17 -0300523 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300524 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300525 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300526 atomic_set(&data->fw_state, S2255_FW_FAILED);
527 /* wake up anything waiting for the firmware */
528 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300529 return;
530 }
531 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300532 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300533 atomic_set(&data->fw_state, S2255_FW_FAILED);
534 /* wake up anything waiting for the firmware */
535 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300536 return;
537 }
538#define CHUNK_SIZE 512
539 /* all USB transfers must be done with continuous kernel memory.
540 can't allocate more than 128k in current linux kernel, so
541 upload the firmware in chunks
542 */
543 if (data->fw_loaded < data->fw_size) {
544 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
545 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
546
547 if (len < CHUNK_SIZE)
548 memset(data->pfw_data, 0, CHUNK_SIZE);
549
550 dprintk(100, "completed len %d, loaded %d \n", len,
551 data->fw_loaded);
552
553 memcpy(data->pfw_data,
554 (char *) data->fw->data + data->fw_loaded, len);
555
556 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
557 data->pfw_data, CHUNK_SIZE,
558 s2255_fwchunk_complete, data);
559 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
560 dev_err(&udev->dev, "failed submit URB\n");
561 atomic_set(&data->fw_state, S2255_FW_FAILED);
562 /* wake up anything waiting for the firmware */
563 wake_up(&data->wait_fw);
564 return;
565 }
566 data->fw_loaded += len;
567 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300568 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson85b85482010-04-08 23:51:17 -0300569 dprintk(100, "%s: firmware upload complete\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300570 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300571 return;
572
573}
574
Dean Anderson14d96262008-08-25 13:58:55 -0300575static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300576{
577 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
578 struct s2255_buffer *buf;
579 unsigned long flags = 0;
580 int rc = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300581 spin_lock_irqsave(&dev->slock, flags);
Dean Anderson38f993a2008-06-26 23:15:51 -0300582 if (list_empty(&dma_q->active)) {
583 dprintk(1, "No active queue to serve\n");
584 rc = -1;
585 goto unlock;
586 }
587 buf = list_entry(dma_q->active.next,
588 struct s2255_buffer, vb.queue);
Dean Anderson38f993a2008-06-26 23:15:51 -0300589 list_del(&buf->vb.queue);
590 do_gettimeofday(&buf->vb.ts);
Dean Anderson14d96262008-08-25 13:58:55 -0300591 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300592 wake_up(&buf->vb.done);
Dean Anderson85b85482010-04-08 23:51:17 -0300593 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
Dean Anderson38f993a2008-06-26 23:15:51 -0300594unlock:
595 spin_unlock_irqrestore(&dev->slock, flags);
596 return 0;
597}
598
Dean Anderson38f993a2008-06-26 23:15:51 -0300599static const struct s2255_fmt *format_by_fourcc(int fourcc)
600{
601 unsigned int i;
Dean Anderson38f993a2008-06-26 23:15:51 -0300602 for (i = 0; i < ARRAY_SIZE(formats); i++) {
603 if (-1 == formats[i].fourcc)
604 continue;
605 if (formats[i].fourcc == fourcc)
606 return formats + i;
607 }
608 return NULL;
609}
610
Dean Anderson38f993a2008-06-26 23:15:51 -0300611/* video buffer vmalloc implementation based partly on VIVI driver which is
612 * Copyright (c) 2006 by
613 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
614 * Ted Walther <ted--a.t--enumera.com>
615 * John Sokol <sokol--a.t--videotechnology.com>
616 * http://v4l.videotechnology.com/
617 *
618 */
619static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300620 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300621{
622 int pos = 0;
623 struct timeval ts;
624 const char *tmpbuf;
625 char *vbuf = videobuf_to_vmalloc(&buf->vb);
626 unsigned long last_frame;
627 struct s2255_framei *frm;
628
629 if (!vbuf)
630 return;
631
632 last_frame = dev->last_frame[chn];
633 if (last_frame != -1) {
634 frm = &dev->buffer[chn].frame[last_frame];
635 tmpbuf =
636 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
637 switch (buf->fmt->fourcc) {
638 case V4L2_PIX_FMT_YUYV:
639 case V4L2_PIX_FMT_UYVY:
640 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
641 vbuf, buf->vb.width,
642 buf->vb.height,
643 buf->fmt->fourcc);
644 break;
645 case V4L2_PIX_FMT_GREY:
646 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
647 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300648 case V4L2_PIX_FMT_JPEG:
649 buf->vb.size = jpgsize;
650 memcpy(vbuf, tmpbuf, buf->vb.size);
651 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300652 case V4L2_PIX_FMT_YUV422P:
653 memcpy(vbuf, tmpbuf,
654 buf->vb.width * buf->vb.height * 2);
655 break;
656 default:
657 printk(KERN_DEBUG "s2255: unknown format?\n");
658 }
659 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300660 } else {
661 printk(KERN_ERR "s2255: =======no frame\n");
662 return;
663
664 }
665 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
666 (unsigned long)vbuf, pos);
667 /* tell v4l buffer was filled */
668
Dean Andersona1c45302008-09-09 12:29:56 -0300669 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300670 do_gettimeofday(&ts);
671 buf->vb.ts = ts;
672 buf->vb.state = VIDEOBUF_DONE;
673}
674
675
676/* ------------------------------------------------------------------
677 Videobuf operations
678 ------------------------------------------------------------------*/
679
680static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
681 unsigned int *size)
682{
683 struct s2255_fh *fh = vq->priv_data;
684
685 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
686
687 if (0 == *count)
688 *count = S2255_DEF_BUFS;
689
Andreas Bombedab7e312010-03-21 16:02:45 -0300690 if (*size * *count > vid_limit * 1024 * 1024)
691 *count = (vid_limit * 1024 * 1024) / *size;
Dean Anderson38f993a2008-06-26 23:15:51 -0300692
693 return 0;
694}
695
696static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
697{
698 dprintk(4, "%s\n", __func__);
699
Dean Anderson38f993a2008-06-26 23:15:51 -0300700 videobuf_vmalloc_free(&buf->vb);
701 buf->vb.state = VIDEOBUF_NEEDS_INIT;
702}
703
704static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
705 enum v4l2_field field)
706{
707 struct s2255_fh *fh = vq->priv_data;
708 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
709 int rc;
710 dprintk(4, "%s, field=%d\n", __func__, field);
711 if (fh->fmt == NULL)
712 return -EINVAL;
713
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300714 if ((fh->width < norm_minw(&fh->dev->vdev[fh->channel])) ||
715 (fh->width > norm_maxw(&fh->dev->vdev[fh->channel])) ||
716 (fh->height < norm_minh(&fh->dev->vdev[fh->channel])) ||
717 (fh->height > norm_maxh(&fh->dev->vdev[fh->channel]))) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300718 dprintk(4, "invalid buffer prepare\n");
719 return -EINVAL;
720 }
721
722 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
723
724 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
725 dprintk(4, "invalid buffer prepare\n");
726 return -EINVAL;
727 }
728
729 buf->fmt = fh->fmt;
730 buf->vb.width = fh->width;
731 buf->vb.height = fh->height;
732 buf->vb.field = field;
733
Dean Anderson38f993a2008-06-26 23:15:51 -0300734 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
735 rc = videobuf_iolock(vq, &buf->vb, NULL);
736 if (rc < 0)
737 goto fail;
738 }
739
740 buf->vb.state = VIDEOBUF_PREPARED;
741 return 0;
742fail:
743 free_buffer(vq, buf);
744 return rc;
745}
746
747static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
748{
749 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
750 struct s2255_fh *fh = vq->priv_data;
751 struct s2255_dev *dev = fh->dev;
752 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300753 dprintk(1, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -0300754 buf->vb.state = VIDEOBUF_QUEUED;
755 list_add_tail(&buf->vb.queue, &vidq->active);
756}
757
758static void buffer_release(struct videobuf_queue *vq,
759 struct videobuf_buffer *vb)
760{
761 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
762 struct s2255_fh *fh = vq->priv_data;
763 dprintk(4, "%s %d\n", __func__, fh->channel);
764 free_buffer(vq, buf);
765}
766
767static struct videobuf_queue_ops s2255_video_qops = {
768 .buf_setup = buffer_setup,
769 .buf_prepare = buffer_prepare,
770 .buf_queue = buffer_queue,
771 .buf_release = buffer_release,
772};
773
774
775static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
776{
777 /* is it free? */
778 mutex_lock(&dev->lock);
779 if (dev->resources[fh->channel]) {
780 /* no, someone else uses it */
781 mutex_unlock(&dev->lock);
782 return 0;
783 }
784 /* it's free, grab it */
785 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300786 fh->resources[fh->channel] = 1;
787 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300788 mutex_unlock(&dev->lock);
789 return 1;
790}
791
792static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
793{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300794 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300795}
796
Dean Andersonf78d92c2008-07-22 14:43:27 -0300797static int res_check(struct s2255_fh *fh)
798{
799 return fh->resources[fh->channel];
800}
801
802
Dean Anderson38f993a2008-06-26 23:15:51 -0300803static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
804{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300805 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300806 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300807 fh->resources[fh->channel] = 0;
808 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300809 dprintk(1, "res: put\n");
810}
811
Dean Anderson5a34d9d2010-03-05 19:59:48 -0300812static int vidioc_querymenu(struct file *file, void *priv,
813 struct v4l2_querymenu *qmenu)
814{
815 static const char *colorfilter[] = {
816 "Off",
817 "On",
818 NULL
819 };
820 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
821 int i;
822 const char **menu_items = colorfilter;
823 for (i = 0; i < qmenu->index && menu_items[i]; i++)
824 ; /* do nothing (from v4l2-common.c) */
825 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
826 return -EINVAL;
827 strlcpy(qmenu->name, menu_items[qmenu->index],
828 sizeof(qmenu->name));
829 return 0;
830 }
831 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
832}
833
Dean Anderson38f993a2008-06-26 23:15:51 -0300834static int vidioc_querycap(struct file *file, void *priv,
835 struct v4l2_capability *cap)
836{
837 struct s2255_fh *fh = file->private_data;
838 struct s2255_dev *dev = fh->dev;
839 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
840 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300841 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300842 cap->version = S2255_VERSION;
843 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
844 return 0;
845}
846
847static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
848 struct v4l2_fmtdesc *f)
849{
850 int index = 0;
851 if (f)
852 index = f->index;
853
854 if (index >= ARRAY_SIZE(formats))
855 return -EINVAL;
856
857 dprintk(4, "name %s\n", formats[index].name);
858 strlcpy(f->description, formats[index].name, sizeof(f->description));
859 f->pixelformat = formats[index].fourcc;
860 return 0;
861}
862
863static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
864 struct v4l2_format *f)
865{
866 struct s2255_fh *fh = priv;
867
868 f->fmt.pix.width = fh->width;
869 f->fmt.pix.height = fh->height;
870 f->fmt.pix.field = fh->vb_vidq.field;
871 f->fmt.pix.pixelformat = fh->fmt->fourcc;
872 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
873 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300874 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300875}
876
877static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_format *f)
879{
880 const struct s2255_fmt *fmt;
881 enum v4l2_field field;
882 int b_any_field = 0;
883 struct s2255_fh *fh = priv;
884 struct s2255_dev *dev = fh->dev;
885 int is_ntsc;
886
887 is_ntsc =
Dean Andersonc0a2ec92010-04-08 23:40:31 -0300888 (dev->vdev[fh->channel].current_norm & V4L2_STD_NTSC) ? 1 : 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300889
890 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
891
892 if (fmt == NULL)
893 return -EINVAL;
894
895 field = f->fmt.pix.field;
896 if (field == V4L2_FIELD_ANY)
897 b_any_field = 1;
898
Dean Anderson85b85482010-04-08 23:51:17 -0300899 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
900 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
Dean Anderson38f993a2008-06-26 23:15:51 -0300901 if (is_ntsc) {
902 /* NTSC */
903 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
904 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
905 if (b_any_field) {
906 field = V4L2_FIELD_SEQ_TB;
907 } else if (!((field == V4L2_FIELD_INTERLACED) ||
908 (field == V4L2_FIELD_SEQ_TB) ||
909 (field == V4L2_FIELD_INTERLACED_TB))) {
910 dprintk(1, "unsupported field setting\n");
911 return -EINVAL;
912 }
913 } else {
914 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
915 if (b_any_field) {
916 field = V4L2_FIELD_TOP;
917 } else if (!((field == V4L2_FIELD_TOP) ||
918 (field == V4L2_FIELD_BOTTOM))) {
919 dprintk(1, "unsupported field setting\n");
920 return -EINVAL;
921 }
922
923 }
924 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
925 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
926 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
930 else
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
932 } else {
933 /* PAL */
934 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
935 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
936 if (b_any_field) {
937 field = V4L2_FIELD_SEQ_TB;
938 } else if (!((field == V4L2_FIELD_INTERLACED) ||
939 (field == V4L2_FIELD_SEQ_TB) ||
940 (field == V4L2_FIELD_INTERLACED_TB))) {
941 dprintk(1, "unsupported field setting\n");
942 return -EINVAL;
943 }
944 } else {
945 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
946 if (b_any_field) {
947 field = V4L2_FIELD_TOP;
948 } else if (!((field == V4L2_FIELD_TOP) ||
949 (field == V4L2_FIELD_BOTTOM))) {
950 dprintk(1, "unsupported field setting\n");
951 return -EINVAL;
952 }
953 }
954 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300955 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
956 field = V4L2_FIELD_SEQ_TB;
957 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300958 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
959 field = V4L2_FIELD_TOP;
960 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
Dean Anderson38f993a2008-06-26 23:15:51 -0300961 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
962 field = V4L2_FIELD_TOP;
963 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300964 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
965 field = V4L2_FIELD_TOP;
966 }
967 }
Dean Anderson38f993a2008-06-26 23:15:51 -0300968 f->fmt.pix.field = field;
969 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
970 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson85b85482010-04-08 23:51:17 -0300971 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
972 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
Dean Anderson38f993a2008-06-26 23:15:51 -0300973 return 0;
974}
975
976static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
977 struct v4l2_format *f)
978{
979 struct s2255_fh *fh = priv;
980 const struct s2255_fmt *fmt;
981 struct videobuf_queue *q = &fh->vb_vidq;
982 int ret;
983 int norm;
984
985 ret = vidioc_try_fmt_vid_cap(file, fh, f);
986
987 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300988 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300989
990 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
991
992 if (fmt == NULL)
993 return -EINVAL;
994
995 mutex_lock(&q->vb_lock);
996
997 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
998 dprintk(1, "queue busy\n");
999 ret = -EBUSY;
1000 goto out_s_fmt;
1001 }
1002
1003 if (res_locked(fh->dev, fh)) {
Dean Anderson85b85482010-04-08 23:51:17 -03001004 dprintk(1, "%s: channel busy\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001005 ret = -EBUSY;
1006 goto out_s_fmt;
1007 }
1008
1009 fh->fmt = fmt;
1010 fh->width = f->fmt.pix.width;
1011 fh->height = f->fmt.pix.height;
1012 fh->vb_vidq.field = f->fmt.pix.field;
1013 fh->type = f->type;
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001014 norm = norm_minw(&fh->dev->vdev[fh->channel]);
1015 if (fh->width > norm_minw(&fh->dev->vdev[fh->channel])) {
1016 if (fh->height > norm_minh(&fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001017 if (fh->dev->cap_parm[fh->channel].capturemode &
Dean Anderson85b85482010-04-08 23:51:17 -03001018 V4L2_MODE_HIGHQUALITY)
Dean Anderson7d853532009-05-15 14:32:04 -03001019 fh->mode.scale = SCALE_4CIFSI;
Dean Anderson85b85482010-04-08 23:51:17 -03001020 else
Dean Anderson7d853532009-05-15 14:32:04 -03001021 fh->mode.scale = SCALE_4CIFS;
Dean Anderson7d853532009-05-15 14:32:04 -03001022 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001023 fh->mode.scale = SCALE_2CIFS;
1024
1025 } else {
1026 fh->mode.scale = SCALE_1CIFS;
1027 }
1028
1029 /* color mode */
1030 switch (fh->fmt->fourcc) {
1031 case V4L2_PIX_FMT_GREY:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001032 fh->mode.color &= ~MASK_COLOR;
1033 fh->mode.color |= COLOR_Y8;
Dean Anderson38f993a2008-06-26 23:15:51 -03001034 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001035 case V4L2_PIX_FMT_JPEG:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001036 fh->mode.color &= ~MASK_COLOR;
1037 fh->mode.color |= COLOR_JPG;
1038 fh->mode.color |= (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001039 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001040 case V4L2_PIX_FMT_YUV422P:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001041 fh->mode.color &= ~MASK_COLOR;
1042 fh->mode.color |= COLOR_YUVPL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001043 break;
1044 case V4L2_PIX_FMT_YUYV:
1045 case V4L2_PIX_FMT_UYVY:
1046 default:
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001047 fh->mode.color &= ~MASK_COLOR;
1048 fh->mode.color |= COLOR_YUVPK;
Dean Anderson38f993a2008-06-26 23:15:51 -03001049 break;
1050 }
1051 ret = 0;
1052out_s_fmt:
1053 mutex_unlock(&q->vb_lock);
1054 return ret;
1055}
1056
1057static int vidioc_reqbufs(struct file *file, void *priv,
1058 struct v4l2_requestbuffers *p)
1059{
1060 int rc;
1061 struct s2255_fh *fh = priv;
1062 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1063 return rc;
1064}
1065
1066static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1067{
1068 int rc;
1069 struct s2255_fh *fh = priv;
1070 rc = videobuf_querybuf(&fh->vb_vidq, p);
1071 return rc;
1072}
1073
1074static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1075{
1076 int rc;
1077 struct s2255_fh *fh = priv;
1078 rc = videobuf_qbuf(&fh->vb_vidq, p);
1079 return rc;
1080}
1081
1082static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1083{
1084 int rc;
1085 struct s2255_fh *fh = priv;
1086 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1087 return rc;
1088}
1089
1090#ifdef CONFIG_VIDEO_V4L1_COMPAT
1091static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1092{
1093 struct s2255_fh *fh = priv;
1094
1095 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1096}
1097#endif
1098
1099/* write to the configuration pipe, synchronously */
1100static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1101 int size)
1102{
1103 int pipe;
1104 int done;
1105 long retval = -1;
1106 if (udev) {
1107 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1108 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1109 }
1110 return retval;
1111}
1112
1113static u32 get_transfer_size(struct s2255_mode *mode)
1114{
1115 int linesPerFrame = LINE_SZ_DEF;
1116 int pixelsPerLine = NUM_LINES_DEF;
1117 u32 outImageSize;
1118 u32 usbInSize;
1119 unsigned int mask_mult;
1120
1121 if (mode == NULL)
1122 return 0;
1123
1124 if (mode->format == FORMAT_NTSC) {
1125 switch (mode->scale) {
1126 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001127 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001128 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1129 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1130 break;
1131 case SCALE_2CIFS:
1132 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1133 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1134 break;
1135 case SCALE_1CIFS:
1136 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1137 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1138 break;
1139 default:
1140 break;
1141 }
1142 } else if (mode->format == FORMAT_PAL) {
1143 switch (mode->scale) {
1144 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001145 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001146 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1147 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1148 break;
1149 case SCALE_2CIFS:
1150 linesPerFrame = NUM_LINES_2CIFS_PAL;
1151 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1152 break;
1153 case SCALE_1CIFS:
1154 linesPerFrame = NUM_LINES_1CIFS_PAL;
1155 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1156 break;
1157 default:
1158 break;
1159 }
1160 }
1161 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001162 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001163 /* 2 bytes/pixel if not monochrome */
1164 outImageSize *= 2;
1165 }
1166
1167 /* total bytes to send including prefix and 4K padding;
1168 must be a multiple of USB_READ_SIZE */
1169 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1170 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1171 /* if size not a multiple of USB_READ_SIZE */
1172 if (usbInSize & ~mask_mult)
1173 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1174 return usbInSize;
1175}
1176
Dean Anderson85b85482010-04-08 23:51:17 -03001177static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
Dean Anderson38f993a2008-06-26 23:15:51 -03001178{
1179 struct device *dev = &sdev->udev->dev;
1180 dev_info(dev, "------------------------------------------------\n");
Dean Anderson85b85482010-04-08 23:51:17 -03001181 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1182 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
Dean Anderson38f993a2008-06-26 23:15:51 -03001183 dev_info(dev, "bright: 0x%x\n", mode->bright);
Dean Anderson38f993a2008-06-26 23:15:51 -03001184 dev_info(dev, "------------------------------------------------\n");
1185}
1186
1187/*
1188 * set mode is the function which controls the DSP.
1189 * the restart parameter in struct s2255_mode should be set whenever
1190 * the image size could change via color format, video system or image
1191 * size.
1192 * When the restart parameter is set, we sleep for ONE frame to allow the
1193 * DSP time to get the new frame
1194 */
1195static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1196 struct s2255_mode *mode)
1197{
1198 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001199 __le32 *buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -03001200 unsigned long chn_rev;
Dean Anderson14d96262008-08-25 13:58:55 -03001201 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001202 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001203 dprintk(3, "%s channel %lu\n", __func__, chn);
Dean Anderson22b88d42008-08-29 15:33:19 -03001204 /* if JPEG, set the quality */
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001205 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1206 mode->color &= ~MASK_COLOR;
1207 mode->color |= COLOR_JPG;
1208 mode->color &= ~MASK_JPG_QUALITY;
1209 mode->color |= (dev->jc[chn].quality << 8);
1210 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001211 /* save the mode */
1212 dev->mode[chn] = *mode;
1213 dev->req_image_size[chn] = get_transfer_size(mode);
Dean Anderson85b85482010-04-08 23:51:17 -03001214 dprintk(1, "%s: reqsize %ld\n", __func__, dev->req_image_size[chn]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001215 buffer = kzalloc(512, GFP_KERNEL);
1216 if (buffer == NULL) {
1217 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001218 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001219 return -ENOMEM;
1220 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001221 /* set the mode */
1222 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001223 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001224 buffer[2] = CMD_SET_MODE;
1225 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001226 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001227 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1228 if (debug)
Dean Anderson85b85482010-04-08 23:51:17 -03001229 s2255_print_cfg(dev, mode);
Dean Anderson38f993a2008-06-26 23:15:51 -03001230 kfree(buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03001231 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001232 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001233 wait_event_timeout(dev->wait_setmode[chn],
1234 (dev->setmode_ready[chn] != 0),
1235 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1236 if (dev->setmode_ready[chn] != 1) {
1237 printk(KERN_DEBUG "s2255: no set mode response\n");
1238 res = -EFAULT;
1239 }
1240 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001241 /* clear the restart flag */
1242 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001243 mutex_unlock(&dev->lock);
Dean Anderson85b85482010-04-08 23:51:17 -03001244 dprintk(1, "%s chn %lu, result: %d\n", __func__, chn, res);
Dean Anderson38f993a2008-06-26 23:15:51 -03001245 return res;
1246}
1247
Dean Anderson4de39f52010-03-03 19:39:19 -03001248static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1249 u32 *pstatus)
1250{
1251 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001252 __le32 *buffer;
Dean Anderson4de39f52010-03-03 19:39:19 -03001253 u32 chn_rev;
1254 mutex_lock(&dev->lock);
1255 chn_rev = G_chnmap[chn];
Dean Anderson85b85482010-04-08 23:51:17 -03001256 dprintk(4, "%s chan %lu\n", __func__, chn);
Dean Anderson4de39f52010-03-03 19:39:19 -03001257 buffer = kzalloc(512, GFP_KERNEL);
1258 if (buffer == NULL) {
1259 dev_err(&dev->udev->dev, "out of mem\n");
1260 mutex_unlock(&dev->lock);
1261 return -ENOMEM;
1262 }
1263 /* form the get vid status command */
1264 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001265 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson4de39f52010-03-03 19:39:19 -03001266 buffer[2] = CMD_STATUS;
1267 *pstatus = 0;
1268 dev->vidstatus_ready[chn] = 0;
1269 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1270 kfree(buffer);
1271 wait_event_timeout(dev->wait_vidstatus[chn],
1272 (dev->vidstatus_ready[chn] != 0),
1273 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1274 if (dev->vidstatus_ready[chn] != 1) {
1275 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1276 res = -EFAULT;
1277 }
1278 *pstatus = dev->vidstatus[chn];
1279 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1280 mutex_unlock(&dev->lock);
1281 return res;
1282}
1283
Dean Anderson38f993a2008-06-26 23:15:51 -03001284static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1285{
1286 int res;
1287 struct s2255_fh *fh = priv;
1288 struct s2255_dev *dev = fh->dev;
1289 struct s2255_mode *new_mode;
1290 struct s2255_mode *old_mode;
1291 int chn;
1292 int j;
1293 dprintk(4, "%s\n", __func__);
1294 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1295 dev_err(&dev->udev->dev, "invalid fh type0\n");
1296 return -EINVAL;
1297 }
1298 if (i != fh->type) {
1299 dev_err(&dev->udev->dev, "invalid fh type1\n");
1300 return -EINVAL;
1301 }
1302
1303 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001304 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001305 return -EBUSY;
1306 }
1307
1308 /* send a set mode command everytime with restart.
1309 in case we switch resolutions or other parameters */
1310 chn = fh->channel;
1311 new_mode = &fh->mode;
1312 old_mode = &fh->dev->mode[chn];
1313
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001314 if ((new_mode->color & MASK_COLOR) != (old_mode->color & MASK_COLOR))
Dean Anderson38f993a2008-06-26 23:15:51 -03001315 new_mode->restart = 1;
1316 else if (new_mode->scale != old_mode->scale)
1317 new_mode->restart = 1;
1318 else if (new_mode->format != old_mode->format)
1319 new_mode->restart = 1;
1320
1321 s2255_set_mode(dev, chn, new_mode);
1322 new_mode->restart = 0;
1323 *old_mode = *new_mode;
1324 dev->cur_fmt[chn] = fh->fmt;
Dean Anderson38f993a2008-06-26 23:15:51 -03001325 dev->last_frame[chn] = -1;
1326 dev->bad_payload[chn] = 0;
1327 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001328 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001329 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001330 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001331 dev->buffer[chn].frame[j].cur_size = 0;
1332 }
1333 res = videobuf_streamon(&fh->vb_vidq);
1334 if (res == 0) {
1335 s2255_start_acquire(dev, chn);
1336 dev->b_acquire[chn] = 1;
1337 } else {
1338 res_free(dev, fh);
1339 }
1340 return res;
1341}
1342
1343static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1344{
Dean Anderson38f993a2008-06-26 23:15:51 -03001345 struct s2255_fh *fh = priv;
1346 struct s2255_dev *dev = fh->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001347 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1348 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1349 printk(KERN_ERR "invalid fh type0\n");
1350 return -EINVAL;
1351 }
1352 if (i != fh->type) {
1353 printk(KERN_ERR "invalid type i\n");
1354 return -EINVAL;
1355 }
1356 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001357 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001358 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001359 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001360}
1361
1362static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1363{
1364 struct s2255_fh *fh = priv;
1365 struct s2255_mode *mode;
1366 struct videobuf_queue *q = &fh->vb_vidq;
1367 int ret = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001368 mutex_lock(&q->vb_lock);
1369 if (videobuf_queue_is_busy(q)) {
1370 dprintk(1, "queue busy\n");
1371 ret = -EBUSY;
1372 goto out_s_std;
1373 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001374 if (res_locked(fh->dev, fh)) {
1375 dprintk(1, "can't change standard after started\n");
1376 ret = -EBUSY;
1377 goto out_s_std;
1378 }
1379 mode = &fh->mode;
Dean Anderson38f993a2008-06-26 23:15:51 -03001380 if (*i & V4L2_STD_NTSC) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001381 dprintk(4, "%s NTSC\n", __func__);
1382 /* if changing format, reset frame decimation/intervals */
1383 if (mode->format != FORMAT_NTSC) {
1384 mode->format = FORMAT_NTSC;
1385 mode->fdec = FDEC_1;
1386 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001387 } else if (*i & V4L2_STD_PAL) {
Dean Andersone6b44bc2010-03-08 20:04:48 -03001388 dprintk(4, "%s PAL\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001389 mode->format = FORMAT_PAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001390 if (mode->format != FORMAT_PAL) {
1391 mode->format = FORMAT_PAL;
1392 mode->fdec = FDEC_1;
1393 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001394 } else {
1395 ret = -EINVAL;
1396 }
1397out_s_std:
1398 mutex_unlock(&q->vb_lock);
1399 return ret;
1400}
1401
1402/* Sensoray 2255 is a multiple channel capture device.
1403 It does not have a "crossbar" of inputs.
1404 We use one V4L device per channel. The user must
1405 be aware that certain combinations are not allowed.
1406 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1407 at once in color(you can do full fps on 4 channels with greyscale.
1408*/
1409static int vidioc_enum_input(struct file *file, void *priv,
1410 struct v4l2_input *inp)
1411{
Dean Anderson4de39f52010-03-03 19:39:19 -03001412 struct s2255_fh *fh = priv;
1413 struct s2255_dev *dev = fh->dev;
1414 u32 status = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001415 if (inp->index != 0)
1416 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001417 inp->type = V4L2_INPUT_TYPE_CAMERA;
1418 inp->std = S2255_NORMS;
Dean Anderson4de39f52010-03-03 19:39:19 -03001419 inp->status = 0;
1420 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1421 int rc;
1422 rc = s2255_cmd_status(dev, fh->channel, &status);
1423 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1424 if (rc == 0)
1425 inp->status = (status & 0x01) ? 0
1426 : V4L2_IN_ST_NO_SIGNAL;
1427 }
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001428 switch (dev->pid) {
1429 case 0x2255:
1430 default:
1431 strlcpy(inp->name, "Composite", sizeof(inp->name));
1432 break;
1433 case 0x2257:
1434 strlcpy(inp->name, (fh->channel < 2) ? "Composite" : "S-Video",
1435 sizeof(inp->name));
1436 break;
1437 }
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001438 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001439}
1440
1441static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1442{
1443 *i = 0;
1444 return 0;
1445}
1446static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1447{
1448 if (i > 0)
1449 return -EINVAL;
1450 return 0;
1451}
1452
1453/* --- controls ---------------------------------------------- */
1454static int vidioc_queryctrl(struct file *file, void *priv,
1455 struct v4l2_queryctrl *qc)
1456{
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001457 struct s2255_fh *fh = priv;
1458 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001459 switch (qc->id) {
1460 case V4L2_CID_BRIGHTNESS:
1461 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1462 break;
1463 case V4L2_CID_CONTRAST:
1464 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1465 break;
1466 case V4L2_CID_SATURATION:
1467 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1468 break;
1469 case V4L2_CID_HUE:
1470 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1471 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001472 case V4L2_CID_PRIVATE_COLORFILTER:
1473 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1474 return -EINVAL;
1475 if ((dev->pid == 0x2257) && (fh->channel > 1))
1476 return -EINVAL;
1477 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1478 qc->type = V4L2_CTRL_TYPE_MENU;
1479 qc->minimum = 0;
1480 qc->maximum = 1;
1481 qc->step = 1;
1482 qc->default_value = 1;
1483 qc->flags = 0;
1484 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001485 default:
1486 return -EINVAL;
1487 }
1488 dprintk(4, "%s, id %d\n", __func__, qc->id);
1489 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001490}
1491
1492static int vidioc_g_ctrl(struct file *file, void *priv,
1493 struct v4l2_control *ctrl)
1494{
Dean Anderson2e70db92010-03-05 14:29:09 -03001495 struct s2255_fh *fh = priv;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001496 struct s2255_dev *dev = fh->dev;
Dean Anderson2e70db92010-03-05 14:29:09 -03001497 switch (ctrl->id) {
1498 case V4L2_CID_BRIGHTNESS:
1499 ctrl->value = fh->mode.bright;
1500 break;
1501 case V4L2_CID_CONTRAST:
1502 ctrl->value = fh->mode.contrast;
1503 break;
1504 case V4L2_CID_SATURATION:
1505 ctrl->value = fh->mode.saturation;
1506 break;
1507 case V4L2_CID_HUE:
1508 ctrl->value = fh->mode.hue;
1509 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001510 case V4L2_CID_PRIVATE_COLORFILTER:
1511 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1512 return -EINVAL;
1513 if ((dev->pid == 0x2257) && (fh->channel > 1))
1514 return -EINVAL;
1515 ctrl->value = !((fh->mode.color & MASK_INPUT_TYPE) >> 16);
1516 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001517 default:
1518 return -EINVAL;
1519 }
1520 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1521 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001522}
1523
1524static int vidioc_s_ctrl(struct file *file, void *priv,
1525 struct v4l2_control *ctrl)
1526{
Dean Anderson38f993a2008-06-26 23:15:51 -03001527 struct s2255_fh *fh = priv;
1528 struct s2255_dev *dev = fh->dev;
1529 struct s2255_mode *mode;
1530 mode = &fh->mode;
Dean Anderson2e70db92010-03-05 14:29:09 -03001531 dprintk(4, "%s\n", __func__);
1532 /* update the mode to the corresponding value */
1533 switch (ctrl->id) {
1534 case V4L2_CID_BRIGHTNESS:
1535 mode->bright = ctrl->value;
1536 break;
1537 case V4L2_CID_CONTRAST:
1538 mode->contrast = ctrl->value;
1539 break;
1540 case V4L2_CID_HUE:
1541 mode->hue = ctrl->value;
1542 break;
1543 case V4L2_CID_SATURATION:
1544 mode->saturation = ctrl->value;
1545 break;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001546 case V4L2_CID_PRIVATE_COLORFILTER:
1547 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1548 return -EINVAL;
1549 if ((dev->pid == 0x2257) && (fh->channel > 1))
1550 return -EINVAL;
1551 mode->color &= ~MASK_INPUT_TYPE;
1552 mode->color |= ((ctrl->value ? 0 : 1) << 16);
1553 break;
Dean Anderson2e70db92010-03-05 14:29:09 -03001554 default:
1555 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001556 }
Dean Anderson2e70db92010-03-05 14:29:09 -03001557 mode->restart = 0;
1558 /* set mode here. Note: stream does not need restarted.
1559 some V4L programs restart stream unnecessarily
1560 after a s_crtl.
1561 */
1562 s2255_set_mode(dev, fh->channel, mode);
1563 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001564}
1565
Dean Anderson22b88d42008-08-29 15:33:19 -03001566static int vidioc_g_jpegcomp(struct file *file, void *priv,
1567 struct v4l2_jpegcompression *jc)
1568{
1569 struct s2255_fh *fh = priv;
1570 struct s2255_dev *dev = fh->dev;
1571 *jc = dev->jc[fh->channel];
Dean Anderson85b85482010-04-08 23:51:17 -03001572 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001573 return 0;
1574}
1575
1576static int vidioc_s_jpegcomp(struct file *file, void *priv,
1577 struct v4l2_jpegcompression *jc)
1578{
1579 struct s2255_fh *fh = priv;
1580 struct s2255_dev *dev = fh->dev;
1581 if (jc->quality < 0 || jc->quality > 100)
1582 return -EINVAL;
1583 dev->jc[fh->channel].quality = jc->quality;
Dean Anderson85b85482010-04-08 23:51:17 -03001584 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
Dean Anderson22b88d42008-08-29 15:33:19 -03001585 return 0;
1586}
Dean Anderson7d853532009-05-15 14:32:04 -03001587
1588static int vidioc_g_parm(struct file *file, void *priv,
1589 struct v4l2_streamparm *sp)
1590{
1591 struct s2255_fh *fh = priv;
1592 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001593 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001594 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1595 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001596 memset(sp, 0, sizeof(struct v4l2_streamparm));
1597 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
Dean Anderson7d853532009-05-15 14:32:04 -03001598 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001599 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1600 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1601 sp->parm.capture.timeperframe.denominator = def_dem;
1602 switch (fh->mode.fdec) {
1603 default:
1604 case FDEC_1:
1605 sp->parm.capture.timeperframe.numerator = def_num;
1606 break;
1607 case FDEC_2:
1608 sp->parm.capture.timeperframe.numerator = def_num * 2;
1609 break;
1610 case FDEC_3:
1611 sp->parm.capture.timeperframe.numerator = def_num * 3;
1612 break;
1613 case FDEC_5:
1614 sp->parm.capture.timeperframe.numerator = def_num * 5;
1615 break;
1616 }
1617 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1618 sp->parm.capture.capturemode,
1619 sp->parm.capture.timeperframe.numerator,
1620 sp->parm.capture.timeperframe.denominator);
Dean Anderson7d853532009-05-15 14:32:04 -03001621 return 0;
1622}
1623
1624static int vidioc_s_parm(struct file *file, void *priv,
1625 struct v4l2_streamparm *sp)
1626{
1627 struct s2255_fh *fh = priv;
1628 struct s2255_dev *dev = fh->dev;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001629 int fdec = FDEC_1;
1630 __u32 def_num, def_dem;
Dean Anderson7d853532009-05-15 14:32:04 -03001631 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1632 return -EINVAL;
Dean Andersone6b44bc2010-03-08 20:04:48 -03001633 /* high quality capture mode requires a stream restart */
1634 if (dev->cap_parm[fh->channel].capturemode
1635 != sp->parm.capture.capturemode && res_locked(fh->dev, fh))
1636 return -EBUSY;
1637 def_num = (fh->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1638 def_dem = (fh->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1639 if (def_dem != sp->parm.capture.timeperframe.denominator)
1640 sp->parm.capture.timeperframe.numerator = def_num;
1641 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1642 sp->parm.capture.timeperframe.numerator = def_num;
1643 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1644 sp->parm.capture.timeperframe.numerator = def_num * 2;
1645 fdec = FDEC_2;
1646 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1647 sp->parm.capture.timeperframe.numerator = def_num * 3;
1648 fdec = FDEC_3;
1649 } else {
1650 sp->parm.capture.timeperframe.numerator = def_num * 5;
1651 fdec = FDEC_5;
1652 }
1653 fh->mode.fdec = fdec;
1654 sp->parm.capture.timeperframe.denominator = def_dem;
1655 s2255_set_mode(dev, fh->channel, &fh->mode);
1656 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1657 __func__,
1658 sp->parm.capture.capturemode,
1659 sp->parm.capture.timeperframe.numerator,
1660 sp->parm.capture.timeperframe.denominator, fdec);
Dean Anderson7d853532009-05-15 14:32:04 -03001661 return 0;
1662}
Dean Andersone6b44bc2010-03-08 20:04:48 -03001663
1664static int vidioc_enum_frameintervals(struct file *file, void *priv,
1665 struct v4l2_frmivalenum *fe)
1666{
1667 int is_ntsc = 0;
1668#define NUM_FRAME_ENUMS 4
1669 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1670 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1671 return -EINVAL;
1672 switch (fe->width) {
1673 case 640:
1674 if (fe->height != 240 && fe->height != 480)
1675 return -EINVAL;
1676 is_ntsc = 1;
1677 break;
1678 case 320:
1679 if (fe->height != 240)
1680 return -EINVAL;
1681 is_ntsc = 1;
1682 break;
1683 case 704:
1684 if (fe->height != 288 && fe->height != 576)
1685 return -EINVAL;
1686 break;
1687 case 352:
1688 if (fe->height != 288)
1689 return -EINVAL;
1690 break;
1691 default:
1692 return -EINVAL;
1693 }
1694 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1695 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1696 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1697 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1698 fe->discrete.denominator);
1699 return 0;
1700}
1701
Hans Verkuilbec43662008-12-30 06:58:20 -03001702static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001703{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001704 struct video_device *vdev = video_devdata(file);
1705 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001706 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001707 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001708 int i = 0;
1709 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001710 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001711 dprintk(1, "s2255: open called (dev=%s)\n",
1712 video_device_node_name(vdev));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001713 for (i = 0; i < MAX_CHANNELS; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001714 if (&dev->vdev[i] == vdev) {
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001715 cur_channel = i;
1716 break;
1717 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001718 /*
1719 * open lock necessary to prevent multiple instances
1720 * of v4l-conf (or other programs) from simultaneously
1721 * reloading firmware.
1722 */
Dean Anderson38f993a2008-06-26 23:15:51 -03001723 mutex_lock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001724 state = atomic_read(&dev->fw_data->fw_state);
1725 switch (state) {
1726 case S2255_FW_DISCONNECTING:
1727 mutex_unlock(&dev->open_lock);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001728 return -ENODEV;
Dean Anderson14d96262008-08-25 13:58:55 -03001729 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001730 s2255_dev_err(&dev->udev->dev,
1731 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001732 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001733 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001734 ((atomic_read(&dev->fw_data->fw_state)
1735 == S2255_FW_SUCCESS) ||
1736 (atomic_read(&dev->fw_data->fw_state)
1737 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001738 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001739 /* state may have changed, re-read */
1740 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001741 break;
1742 case S2255_FW_NOTLOADED:
1743 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001744 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1745 driver loaded and then device immediately opened */
1746 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1747 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001748 ((atomic_read(&dev->fw_data->fw_state)
1749 == S2255_FW_SUCCESS) ||
1750 (atomic_read(&dev->fw_data->fw_state)
1751 == S2255_FW_DISCONNECTING)),
1752 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Andersonff7e22d2010-04-08 23:38:07 -03001753 /* state may have changed, re-read */
1754 state = atomic_read(&dev->fw_data->fw_state);
Dean Anderson14d96262008-08-25 13:58:55 -03001755 break;
1756 case S2255_FW_SUCCESS:
1757 default:
1758 break;
1759 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03001760 mutex_unlock(&dev->open_lock);
1761 /* state may have changed in above switch statement */
1762 switch (state) {
1763 case S2255_FW_SUCCESS:
1764 break;
1765 case S2255_FW_FAILED:
1766 printk(KERN_INFO "2255 firmware load failed.\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03001767 return -ENODEV;
1768 case S2255_FW_DISCONNECTING:
1769 printk(KERN_INFO "%s: disconnecting\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001770 return -ENODEV;
1771 case S2255_FW_LOADED_DSPWAIT:
1772 case S2255_FW_NOTLOADED:
1773 printk(KERN_INFO "%s: firmware not loaded yet"
1774 "please try again later\n",
1775 __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001776 return -EAGAIN;
1777 default:
1778 printk(KERN_INFO "%s: unknown state\n", __func__);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001779 return -EFAULT;
Dean Anderson38f993a2008-06-26 23:15:51 -03001780 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001781 /* allocate + initialize per filehandle data */
1782 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Dean Andersona5ef91c2010-04-08 23:46:08 -03001783 if (NULL == fh)
Dean Anderson38f993a2008-06-26 23:15:51 -03001784 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03001785 file->private_data = fh;
1786 fh->dev = dev;
1787 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1788 fh->mode = dev->mode[cur_channel];
1789 fh->fmt = dev->cur_fmt[cur_channel];
1790 /* default 4CIF NTSC */
1791 fh->width = LINE_SZ_4CIFS_NTSC;
1792 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1793 fh->channel = cur_channel;
Dean Anderson14d96262008-08-25 13:58:55 -03001794 /* configure channel to default state */
1795 if (!dev->chn_configured[cur_channel]) {
1796 s2255_set_mode(dev, cur_channel, &fh->mode);
1797 dev->chn_configured[cur_channel] = 1;
1798 }
Dean Anderson85b85482010-04-08 23:51:17 -03001799 dprintk(1, "%s: dev=%s type=%s\n", __func__,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001800 video_device_node_name(vdev), v4l2_type_names[type]);
Dean Anderson85b85482010-04-08 23:51:17 -03001801 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001802 (unsigned long)fh, (unsigned long)dev,
1803 (unsigned long)&dev->vidq[cur_channel]);
Dean Anderson85b85482010-04-08 23:51:17 -03001804 dprintk(4, "%s: list_empty active=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001805 list_empty(&dev->vidq[cur_channel].active));
Dean Anderson38f993a2008-06-26 23:15:51 -03001806 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1807 NULL, &dev->slock,
1808 fh->type,
1809 V4L2_FIELD_INTERLACED,
1810 sizeof(struct s2255_buffer), fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001811 return 0;
1812}
1813
1814
1815static unsigned int s2255_poll(struct file *file,
1816 struct poll_table_struct *wait)
1817{
1818 struct s2255_fh *fh = file->private_data;
1819 int rc;
1820 dprintk(100, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03001821 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1822 return POLLERR;
Dean Anderson38f993a2008-06-26 23:15:51 -03001823 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1824 return rc;
1825}
1826
1827static void s2255_destroy(struct kref *kref)
1828{
1829 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001830 /* board shutdown stops the read pipe if it is running */
1831 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001832 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001833 del_timer(&dev->timer); /* only started in .probe and .open */
Dean Anderson38f993a2008-06-26 23:15:51 -03001834 if (dev->fw_data->fw_urb) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001835 usb_kill_urb(dev->fw_data->fw_urb);
1836 usb_free_urb(dev->fw_data->fw_urb);
1837 dev->fw_data->fw_urb = NULL;
1838 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001839 if (dev->fw_data->fw)
1840 release_firmware(dev->fw_data->fw);
1841 kfree(dev->fw_data->pfw_data);
1842 kfree(dev->fw_data);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001843 /* reset the DSP so firmware can be reloaded next time */
1844 s2255_reset_dsppower(dev);
1845 mutex_destroy(&dev->open_lock);
1846 mutex_destroy(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001847 usb_put_dev(dev->udev);
1848 dprintk(1, "%s", __func__);
Dean Andersonb7732a32009-03-30 11:59:56 -03001849 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001850}
1851
Dean Andersonff7e22d2010-04-08 23:38:07 -03001852static int s2255_release(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001853{
1854 struct s2255_fh *fh = file->private_data;
1855 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001856 struct video_device *vdev = video_devdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001857 if (!dev)
1858 return -ENODEV;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001859 /* turn off stream */
1860 if (res_check(fh)) {
1861 if (dev->b_acquire[fh->channel])
1862 s2255_stop_acquire(dev, fh->channel);
1863 videobuf_streamoff(&fh->vb_vidq);
1864 res_free(dev, fh);
1865 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001866 videobuf_mmap_free(&fh->vb_vidq);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001867 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
Dean Andersonf78d92c2008-07-22 14:43:27 -03001868 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001869 return 0;
1870}
1871
1872static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1873{
1874 struct s2255_fh *fh = file->private_data;
1875 int ret;
1876
1877 if (!fh)
1878 return -ENODEV;
Dean Anderson85b85482010-04-08 23:51:17 -03001879 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
Dean Anderson38f993a2008-06-26 23:15:51 -03001880 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Dean Anderson85b85482010-04-08 23:51:17 -03001881 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
Dean Anderson38f993a2008-06-26 23:15:51 -03001882 (unsigned long)vma->vm_start,
1883 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
Dean Anderson38f993a2008-06-26 23:15:51 -03001884 return ret;
1885}
1886
Hans Verkuilbec43662008-12-30 06:58:20 -03001887static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001888 .owner = THIS_MODULE,
1889 .open = s2255_open,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001890 .release = s2255_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001891 .poll = s2255_poll,
1892 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001893 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001894};
1895
Hans Verkuila3998102008-07-21 02:57:38 -03001896static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson5a34d9d2010-03-05 19:59:48 -03001897 .vidioc_querymenu = vidioc_querymenu,
Dean Anderson38f993a2008-06-26 23:15:51 -03001898 .vidioc_querycap = vidioc_querycap,
1899 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1900 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1901 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1902 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1903 .vidioc_reqbufs = vidioc_reqbufs,
1904 .vidioc_querybuf = vidioc_querybuf,
1905 .vidioc_qbuf = vidioc_qbuf,
1906 .vidioc_dqbuf = vidioc_dqbuf,
1907 .vidioc_s_std = vidioc_s_std,
1908 .vidioc_enum_input = vidioc_enum_input,
1909 .vidioc_g_input = vidioc_g_input,
1910 .vidioc_s_input = vidioc_s_input,
1911 .vidioc_queryctrl = vidioc_queryctrl,
1912 .vidioc_g_ctrl = vidioc_g_ctrl,
1913 .vidioc_s_ctrl = vidioc_s_ctrl,
1914 .vidioc_streamon = vidioc_streamon,
1915 .vidioc_streamoff = vidioc_streamoff,
1916#ifdef CONFIG_VIDEO_V4L1_COMPAT
1917 .vidiocgmbuf = vidioc_cgmbuf,
1918#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001919 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1920 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001921 .vidioc_s_parm = vidioc_s_parm,
1922 .vidioc_g_parm = vidioc_g_parm,
Dean Andersone6b44bc2010-03-08 20:04:48 -03001923 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
Hans Verkuila3998102008-07-21 02:57:38 -03001924};
1925
Dean Andersonff7e22d2010-04-08 23:38:07 -03001926static void s2255_video_device_release(struct video_device *vdev)
1927{
1928 struct s2255_dev *dev = video_get_drvdata(vdev);
Dean Andersonff7e22d2010-04-08 23:38:07 -03001929 kref_put(&dev->kref, s2255_destroy);
1930 return;
1931}
1932
Hans Verkuila3998102008-07-21 02:57:38 -03001933static struct video_device template = {
1934 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001935 .fops = &s2255_fops_v4l,
1936 .ioctl_ops = &s2255_ioctl_ops,
Dean Andersonff7e22d2010-04-08 23:38:07 -03001937 .release = s2255_video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001938 .tvnorms = S2255_NORMS,
1939 .current_norm = V4L2_STD_NTSC_M,
1940};
1941
1942static int s2255_probe_v4l(struct s2255_dev *dev)
1943{
1944 int ret;
1945 int i;
1946 int cur_nr = video_nr;
1947
1948 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03001949 /* register 4 video devices */
1950 for (i = 0; i < MAX_CHANNELS; i++) {
1951 INIT_LIST_HEAD(&dev->vidq[i].active);
1952 dev->vidq[i].dev = dev;
1953 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001954 /* register 4 video devices */
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001955 memcpy(&dev->vdev[i], &template, sizeof(struct video_device));
1956 dev->vdev[i].parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001957 if (video_nr == -1)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001958 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001959 VFL_TYPE_GRABBER,
1960 video_nr);
1961 else
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001962 ret = video_register_device(&dev->vdev[i],
Dean Anderson38f993a2008-06-26 23:15:51 -03001963 VFL_TYPE_GRABBER,
1964 cur_nr + i);
Dean Andersonc0a2ec92010-04-08 23:40:31 -03001965 video_set_drvdata(&dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001966
1967 if (ret != 0) {
1968 dev_err(&dev->udev->dev,
1969 "failed to register video device!\n");
1970 return ret;
1971 }
1972 }
Dean Andersonabce21f2009-04-23 16:04:41 -03001973 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1974 S2255_MAJOR_VERSION,
1975 S2255_MINOR_VERSION);
Dean Anderson38f993a2008-06-26 23:15:51 -03001976 return ret;
1977}
1978
Dean Anderson38f993a2008-06-26 23:15:51 -03001979/* this function moves the usb stream read pipe data
1980 * into the system buffers.
1981 * returns 0 on success, EAGAIN if more data to process( call this
1982 * function again).
1983 *
1984 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001985 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001986 * bytes 4-7: channel: 0-3
1987 * bytes 8-11: payload size: size of the frame
1988 * bytes 12-payloadsize+12: frame data
1989 */
1990static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1991{
Dean Anderson38f993a2008-06-26 23:15:51 -03001992 char *pdest;
1993 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001994 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001995 char *psrc;
1996 unsigned long copy_size;
1997 unsigned long size;
1998 s32 idx = -1;
1999 struct s2255_framei *frm;
2000 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002001
Dean Anderson38f993a2008-06-26 23:15:51 -03002002 dprintk(100, "buffer to user\n");
2003
2004 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03002005 frm = &dev->buffer[dev->cc].frame[idx];
2006
Dean Anderson14d96262008-08-25 13:58:55 -03002007 if (frm->ulState == S2255_READ_IDLE) {
2008 int jj;
2009 unsigned int cc;
Dean Anderson3fa00602010-03-04 20:47:33 -03002010 __le32 *pdword; /*data from dsp is little endian */
Dean Anderson14d96262008-08-25 13:58:55 -03002011 int payload;
2012 /* search for marker codes */
2013 pdata = (unsigned char *)pipe_info->transfer_buffer;
Dean Anderson3fa00602010-03-04 20:47:33 -03002014 pdword = (__le32 *)pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03002015 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
Dean Anderson3fa00602010-03-04 20:47:33 -03002016 switch (*pdword) {
Dean Anderson14d96262008-08-25 13:58:55 -03002017 case S2255_MARKER_FRAME:
Dean Anderson14d96262008-08-25 13:58:55 -03002018 dprintk(4, "found frame marker at offset:"
2019 " %d [%x %x]\n", jj, pdata[0],
2020 pdata[1]);
2021 offset = jj + PREFIX_SIZE;
2022 bframe = 1;
2023 cc = pdword[1];
2024 if (cc >= MAX_CHANNELS) {
2025 printk(KERN_ERR
2026 "bad channel\n");
2027 return -EINVAL;
2028 }
2029 /* reverse it */
2030 dev->cc = G_chnmap[cc];
2031 payload = pdword[3];
2032 if (payload > dev->req_image_size[dev->cc]) {
2033 dev->bad_payload[dev->cc]++;
2034 /* discard the bad frame */
2035 return -EINVAL;
2036 }
2037 dev->pkt_size[dev->cc] = payload;
2038 dev->jpg_size[dev->cc] = pdword[4];
2039 break;
2040 case S2255_MARKER_RESPONSE:
Dean Anderson14d96262008-08-25 13:58:55 -03002041 pdata += DEF_USB_BLOCK;
2042 jj += DEF_USB_BLOCK;
2043 if (pdword[1] >= MAX_CHANNELS)
2044 break;
2045 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03002046 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03002047 break;
2048 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03002049 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03002050 /* check if channel valid */
2051 /* set mode ready */
2052 dev->setmode_ready[cc] = 1;
2053 wake_up(&dev->wait_setmode[cc]);
2054 dprintk(5, "setmode ready %d\n", cc);
2055 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03002056 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03002057
2058 dev->chn_ready |= (1 << cc);
2059 if ((dev->chn_ready & 0x0f) != 0x0f)
2060 break;
2061 /* all channels ready */
2062 printk(KERN_INFO "s2255: fw loaded\n");
2063 atomic_set(&dev->fw_data->fw_state,
2064 S2255_FW_SUCCESS);
2065 wake_up(&dev->fw_data->wait_fw);
2066 break;
Dean Anderson4de39f52010-03-03 19:39:19 -03002067 case S2255_RESPONSE_STATUS:
2068 dev->vidstatus[cc] = pdword[3];
2069 dev->vidstatus_ready[cc] = 1;
2070 wake_up(&dev->wait_vidstatus[cc]);
2071 dprintk(5, "got vidstatus %x chan %d\n",
2072 pdword[3], cc);
2073 break;
Dean Anderson14d96262008-08-25 13:58:55 -03002074 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002075 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002076 }
2077 default:
2078 pdata++;
2079 break;
2080 }
2081 if (bframe)
2082 break;
2083 } /* for */
2084 if (!bframe)
2085 return -EINVAL;
2086 }
2087
2088
2089 idx = dev->cur_frame[dev->cc];
2090 frm = &dev->buffer[dev->cc].frame[idx];
2091
2092 /* search done. now find out if should be acquiring on this channel */
2093 if (!dev->b_acquire[dev->cc]) {
2094 /* we found a frame, but this channel is turned off */
2095 frm->ulState = S2255_READ_IDLE;
2096 return -EINVAL;
2097 }
2098
2099 if (frm->ulState == S2255_READ_IDLE) {
2100 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002101 frm->cur_size = 0;
2102 }
2103
Dean Anderson14d96262008-08-25 13:58:55 -03002104 /* skip the marker 512 bytes (and offset if out of sync) */
2105 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2106
Dean Anderson38f993a2008-06-26 23:15:51 -03002107
2108 if (frm->lpvbits == NULL) {
2109 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2110 frm, dev, dev->cc, idx);
2111 return -ENOMEM;
2112 }
2113
2114 pdest = frm->lpvbits + frm->cur_size;
2115
Dean Anderson14d96262008-08-25 13:58:55 -03002116 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002117
Dean Anderson14d96262008-08-25 13:58:55 -03002118 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002119
Dean Anderson14d96262008-08-25 13:58:55 -03002120 /* sanity check on pdest */
2121 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2122 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002123
Dean Anderson38f993a2008-06-26 23:15:51 -03002124 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002125 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002126
Dean Anderson14d96262008-08-25 13:58:55 -03002127 if (frm->cur_size >= size) {
2128
Dean Anderson38f993a2008-06-26 23:15:51 -03002129 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002130 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2131 cc, idx);
2132 dev->last_frame[cc] = dev->cur_frame[cc];
2133 dev->cur_frame[cc]++;
2134 /* end of system frame ring buffer, start at zero */
2135 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2136 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2137 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002138 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002139 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002140 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002141 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002142 frm->ulState = S2255_READ_IDLE;
2143 frm->cur_size = 0;
2144
Dean Anderson38f993a2008-06-26 23:15:51 -03002145 }
2146 /* done successfully */
2147 return 0;
2148}
2149
2150static void s2255_read_video_callback(struct s2255_dev *dev,
2151 struct s2255_pipeinfo *pipe_info)
2152{
2153 int res;
2154 dprintk(50, "callback read video \n");
2155
2156 if (dev->cc >= MAX_CHANNELS) {
2157 dev->cc = 0;
2158 dev_err(&dev->udev->dev, "invalid channel\n");
2159 return;
2160 }
2161 /* otherwise copy to the system buffers */
2162 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002163 if (res != 0)
2164 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002165
2166 dprintk(50, "callback read video done\n");
2167 return;
2168}
2169
2170static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2171 u16 Index, u16 Value, void *TransferBuffer,
2172 s32 TransferBufferLength, int bOut)
2173{
2174 int r;
2175 if (!bOut) {
2176 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2177 Request,
2178 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2179 USB_DIR_IN,
2180 Value, Index, TransferBuffer,
2181 TransferBufferLength, HZ * 5);
2182 } else {
2183 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2184 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2185 Value, Index, TransferBuffer,
2186 TransferBufferLength, HZ * 5);
2187 }
2188 return r;
2189}
2190
2191/*
2192 * retrieve FX2 firmware version. future use.
2193 * @param dev pointer to device extension
2194 * @return -1 for fail, else returns firmware version as an int(16 bits)
2195 */
2196static int s2255_get_fx2fw(struct s2255_dev *dev)
2197{
2198 int fw;
2199 int ret;
2200 unsigned char transBuffer[64];
2201 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2202 S2255_VR_IN);
2203 if (ret < 0)
2204 dprintk(2, "get fw error: %x\n", ret);
2205 fw = transBuffer[0] + (transBuffer[1] << 8);
2206 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2207 return fw;
2208}
2209
2210/*
2211 * Create the system ring buffer to copy frames into from the
2212 * usb read pipe.
2213 */
2214static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2215{
2216 unsigned long i;
2217 unsigned long reqsize;
2218 dprintk(1, "create sys buffers\n");
2219 if (chn >= MAX_CHANNELS)
2220 return -1;
2221
2222 dev->buffer[chn].dwFrames = SYS_FRAMES;
2223
2224 /* always allocate maximum size(PAL) for system buffers */
2225 reqsize = SYS_FRAMES_MAXSIZE;
2226
2227 if (reqsize > SYS_FRAMES_MAXSIZE)
2228 reqsize = SYS_FRAMES_MAXSIZE;
2229
2230 for (i = 0; i < SYS_FRAMES; i++) {
2231 /* allocate the frames */
2232 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2233
2234 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2235 &dev->buffer[chn].frame[i], chn, i,
2236 dev->buffer[chn].frame[i].lpvbits);
2237 dev->buffer[chn].frame[i].size = reqsize;
2238 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2239 printk(KERN_INFO "out of memory. using less frames\n");
2240 dev->buffer[chn].dwFrames = i;
2241 break;
2242 }
2243 }
2244
2245 /* make sure internal states are set */
2246 for (i = 0; i < SYS_FRAMES; i++) {
2247 dev->buffer[chn].frame[i].ulState = 0;
2248 dev->buffer[chn].frame[i].cur_size = 0;
2249 }
2250
2251 dev->cur_frame[chn] = 0;
2252 dev->last_frame[chn] = -1;
2253 return 0;
2254}
2255
2256static int s2255_release_sys_buffers(struct s2255_dev *dev,
2257 unsigned long channel)
2258{
2259 unsigned long i;
2260 dprintk(1, "release sys buffers\n");
2261 for (i = 0; i < SYS_FRAMES; i++) {
2262 if (dev->buffer[channel].frame[i].lpvbits) {
2263 dprintk(1, "vfree %p\n",
2264 dev->buffer[channel].frame[i].lpvbits);
2265 vfree(dev->buffer[channel].frame[i].lpvbits);
2266 }
2267 dev->buffer[channel].frame[i].lpvbits = NULL;
2268 }
2269 return 0;
2270}
2271
2272static int s2255_board_init(struct s2255_dev *dev)
2273{
Dean Anderson38f993a2008-06-26 23:15:51 -03002274 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2275 int fw_ver;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002276 int j;
2277 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002278 dprintk(4, "board init: %p", dev);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002279 memset(pipe, 0, sizeof(*pipe));
2280 pipe->dev = dev;
2281 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2282 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002283
Dean Andersonab85c6a2010-04-08 23:39:12 -03002284 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2285 GFP_KERNEL);
2286 if (pipe->transfer_buffer == NULL) {
2287 dprintk(1, "out of memory!\n");
2288 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002289 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002290 /* query the firmware */
2291 fw_ver = s2255_get_fx2fw(dev);
2292
Dean Andersonabce21f2009-04-23 16:04:41 -03002293 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2294 (fw_ver >> 8) & 0xff,
2295 fw_ver & 0xff);
2296
2297 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002298 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002299 "usb firmware not up to date %d.%d\n",
2300 (fw_ver >> 8) & 0xff,
2301 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002302
2303 for (j = 0; j < MAX_CHANNELS; j++) {
2304 dev->b_acquire[j] = 0;
2305 dev->mode[j] = mode_def;
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002306 if (dev->pid == 0x2257 && j > 1)
2307 dev->mode[j].color |= (1 << 16);
Dean Anderson22b88d42008-08-29 15:33:19 -03002308 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002309 dev->cur_fmt[j] = &formats[0];
2310 dev->mode[j].restart = 1;
2311 dev->req_image_size[j] = get_transfer_size(&mode_def);
2312 dev->frame_count[j] = 0;
2313 /* create the system buffers */
2314 s2255_create_sys_buffers(dev, j);
2315 }
2316 /* start read pipe */
2317 s2255_start_readpipe(dev);
Dean Anderson85b85482010-04-08 23:51:17 -03002318 dprintk(1, "%s: success\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002319 return 0;
2320}
2321
2322static int s2255_board_shutdown(struct s2255_dev *dev)
2323{
2324 u32 i;
Dean Anderson85b85482010-04-08 23:51:17 -03002325 dprintk(1, "%s: dev: %p", __func__, dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002326
2327 for (i = 0; i < MAX_CHANNELS; i++) {
2328 if (dev->b_acquire[i])
2329 s2255_stop_acquire(dev, i);
2330 }
2331
2332 s2255_stop_readpipe(dev);
2333
2334 for (i = 0; i < MAX_CHANNELS; i++)
2335 s2255_release_sys_buffers(dev, i);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002336 /* release transfer buffer */
2337 kfree(dev->pipe.transfer_buffer);
Dean Anderson38f993a2008-06-26 23:15:51 -03002338 return 0;
2339}
2340
2341static void read_pipe_completion(struct urb *purb)
2342{
2343 struct s2255_pipeinfo *pipe_info;
2344 struct s2255_dev *dev;
2345 int status;
2346 int pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002347 pipe_info = purb->context;
Dean Anderson85b85482010-04-08 23:51:17 -03002348 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
Dean Anderson38f993a2008-06-26 23:15:51 -03002349 purb->status);
2350 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002351 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002352 return;
2353 }
2354
2355 dev = pipe_info->dev;
2356 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002357 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002358 return;
2359 }
2360 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002361 /* if shutting down, do not resubmit, exit immediately */
2362 if (status == -ESHUTDOWN) {
Dean Anderson85b85482010-04-08 23:51:17 -03002363 dprintk(2, "%s: err shutdown\n", __func__);
Dean Andersonb02064c2009-04-30 12:29:38 -03002364 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002365 return;
2366 }
2367
2368 if (pipe_info->state == 0) {
Dean Anderson85b85482010-04-08 23:51:17 -03002369 dprintk(2, "%s: exiting USB pipe", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002370 return;
2371 }
2372
Dean Andersonb02064c2009-04-30 12:29:38 -03002373 if (status == 0)
2374 s2255_read_video_callback(dev, pipe_info);
2375 else {
2376 pipe_info->err_count++;
Dean Anderson85b85482010-04-08 23:51:17 -03002377 dprintk(1, "%s: failed URB %d\n", __func__, status);
Dean Andersonb02064c2009-04-30 12:29:38 -03002378 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002379
Dean Anderson38f993a2008-06-26 23:15:51 -03002380 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2381 /* reuse urb */
2382 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2383 pipe,
2384 pipe_info->transfer_buffer,
2385 pipe_info->cur_transfer_size,
2386 read_pipe_completion, pipe_info);
2387
2388 if (pipe_info->state != 0) {
2389 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2390 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002391 }
2392 } else {
Dean Anderson85b85482010-04-08 23:51:17 -03002393 dprintk(2, "%s :complete state 0\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002394 }
2395 return;
2396}
2397
2398static int s2255_start_readpipe(struct s2255_dev *dev)
2399{
2400 int pipe;
2401 int retval;
Dean Andersonab85c6a2010-04-08 23:39:12 -03002402 struct s2255_pipeinfo *pipe_info = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002403 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
Dean Anderson85b85482010-04-08 23:51:17 -03002404 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002405 pipe_info->state = 1;
2406 pipe_info->err_count = 0;
2407 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2408 if (!pipe_info->stream_urb) {
2409 dev_err(&dev->udev->dev,
2410 "ReadStream: Unable to alloc URB\n");
2411 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002412 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002413 /* transfer buffer allocated in board_init */
2414 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2415 pipe,
2416 pipe_info->transfer_buffer,
2417 pipe_info->cur_transfer_size,
2418 read_pipe_completion, pipe_info);
Dean Andersonab85c6a2010-04-08 23:39:12 -03002419 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2420 if (retval) {
2421 printk(KERN_ERR "s2255: start read pipe failed\n");
2422 return retval;
2423 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002424 return 0;
2425}
2426
2427/* starts acquisition process */
2428static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2429{
2430 unsigned char *buffer;
2431 int res;
2432 unsigned long chn_rev;
2433 int j;
2434 if (chn >= MAX_CHANNELS) {
2435 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2436 return -1;
2437 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002438 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002439 buffer = kzalloc(512, GFP_KERNEL);
2440 if (buffer == NULL) {
2441 dev_err(&dev->udev->dev, "out of mem\n");
2442 return -ENOMEM;
2443 }
2444
2445 dev->last_frame[chn] = -1;
2446 dev->bad_payload[chn] = 0;
2447 dev->cur_frame[chn] = 0;
2448 for (j = 0; j < SYS_FRAMES; j++) {
2449 dev->buffer[chn].frame[j].ulState = 0;
2450 dev->buffer[chn].frame[j].cur_size = 0;
2451 }
2452
2453 /* send the start command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002454 *(__le32 *) buffer = IN_DATA_TOKEN;
2455 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2456 *((__le32 *) buffer + 2) = CMD_START;
Dean Anderson38f993a2008-06-26 23:15:51 -03002457 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2458 if (res != 0)
2459 dev_err(&dev->udev->dev, "CMD_START error\n");
2460
2461 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2462 kfree(buffer);
2463 return 0;
2464}
2465
2466static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2467{
2468 unsigned char *buffer;
2469 int res;
2470 unsigned long chn_rev;
Dean Anderson38f993a2008-06-26 23:15:51 -03002471 if (chn >= MAX_CHANNELS) {
2472 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2473 return -1;
2474 }
2475 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002476 buffer = kzalloc(512, GFP_KERNEL);
2477 if (buffer == NULL) {
2478 dev_err(&dev->udev->dev, "out of mem\n");
2479 return -ENOMEM;
2480 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002481 /* send the stop command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002482 *(__le32 *) buffer = IN_DATA_TOKEN;
2483 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2484 *((__le32 *) buffer + 2) = CMD_STOP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002485 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
Dean Anderson38f993a2008-06-26 23:15:51 -03002486 if (res != 0)
2487 dev_err(&dev->udev->dev, "CMD_STOP error\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002488 kfree(buffer);
2489 dev->b_acquire[chn] = 0;
Dean Anderson85b85482010-04-08 23:51:17 -03002490 dprintk(4, "%s: chn %lu, res %d\n", __func__, chn, res);
Dean Anderson14d96262008-08-25 13:58:55 -03002491 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002492}
2493
2494static void s2255_stop_readpipe(struct s2255_dev *dev)
2495{
Dean Andersonab85c6a2010-04-08 23:39:12 -03002496 struct s2255_pipeinfo *pipe = &dev->pipe;
Dean Anderson38f993a2008-06-26 23:15:51 -03002497 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002498 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002499 return;
2500 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002501 pipe->state = 0;
2502 if (pipe->stream_urb) {
2503 /* cancel urb */
2504 usb_kill_urb(pipe->stream_urb);
2505 usb_free_urb(pipe->stream_urb);
2506 pipe->stream_urb = NULL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002507 }
Dean Andersonab85c6a2010-04-08 23:39:12 -03002508 dprintk(4, "%s", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002509 return;
2510}
2511
Dean Anderson14d96262008-08-25 13:58:55 -03002512static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002513{
Dean Anderson14d96262008-08-25 13:58:55 -03002514 if (reset)
2515 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002516 dev->fw_data->fw_size = dev->fw_data->fw->size;
2517 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2518 memcpy(dev->fw_data->pfw_data,
2519 dev->fw_data->fw->data, CHUNK_SIZE);
2520 dev->fw_data->fw_loaded = CHUNK_SIZE;
2521 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2522 usb_sndbulkpipe(dev->udev, 2),
2523 dev->fw_data->pfw_data,
2524 CHUNK_SIZE, s2255_fwchunk_complete,
2525 dev->fw_data);
2526 mod_timer(&dev->timer, jiffies + HZ);
2527}
2528
2529/* standard usb probe function */
2530static int s2255_probe(struct usb_interface *interface,
2531 const struct usb_device_id *id)
2532{
2533 struct s2255_dev *dev = NULL;
2534 struct usb_host_interface *iface_desc;
2535 struct usb_endpoint_descriptor *endpoint;
2536 int i;
2537 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002538 __le32 *pdata;
2539 int fw_size;
Dean Anderson85b85482010-04-08 23:51:17 -03002540 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002541 /* allocate memory for our device state and initialize it to zero */
2542 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2543 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002544 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002545 return -ENOMEM;
Dean Anderson38f993a2008-06-26 23:15:51 -03002546 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002547 kref_init(&dev->kref);
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002548 dev->pid = id->idProduct;
Dean Anderson38f993a2008-06-26 23:15:51 -03002549 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2550 if (!dev->fw_data)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002551 goto errorFWDATA1;
Dean Anderson38f993a2008-06-26 23:15:51 -03002552 mutex_init(&dev->lock);
2553 mutex_init(&dev->open_lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03002554 /* grab usb_device and save it */
2555 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2556 if (dev->udev == NULL) {
2557 dev_err(&interface->dev, "null usb device\n");
2558 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002559 goto errorUDEV;
Dean Anderson38f993a2008-06-26 23:15:51 -03002560 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002561 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2562 dev->udev, interface);
2563 dev->interface = interface;
2564 /* set up the endpoint information */
2565 iface_desc = interface->cur_altsetting;
2566 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2567 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2568 endpoint = &iface_desc->endpoint[i].desc;
2569 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2570 /* we found the bulk in endpoint */
2571 dev->read_endpoint = endpoint->bEndpointAddress;
2572 }
2573 }
2574
2575 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002576 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002577 goto errorEP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002578 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002579 /* set intfdata */
2580 usb_set_intfdata(interface, dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002581 init_timer(&dev->timer);
2582 dev->timer.function = s2255_timer;
2583 dev->timer.data = (unsigned long)dev->fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -03002584 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson4de39f52010-03-03 19:39:19 -03002585 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03002586 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002587 init_waitqueue_head(&dev->wait_vidstatus[i]);
2588 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002589
2590 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
Dean Anderson38f993a2008-06-26 23:15:51 -03002591 if (!dev->fw_data->fw_urb) {
2592 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002593 goto errorFWURB;
Dean Anderson38f993a2008-06-26 23:15:51 -03002594 }
Dean Andersonff7e22d2010-04-08 23:38:07 -03002595
Dean Anderson38f993a2008-06-26 23:15:51 -03002596 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2597 if (!dev->fw_data->pfw_data) {
2598 dev_err(&interface->dev, "out of memory!\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002599 goto errorFWDATA2;
Dean Anderson38f993a2008-06-26 23:15:51 -03002600 }
2601 /* load the first chunk */
2602 if (request_firmware(&dev->fw_data->fw,
2603 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2604 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
Dean Andersonff7e22d2010-04-08 23:38:07 -03002605 goto errorREQFW;
Dean Anderson38f993a2008-06-26 23:15:51 -03002606 }
Dean Anderson14d96262008-08-25 13:58:55 -03002607 /* check the firmware is valid */
2608 fw_size = dev->fw_data->fw->size;
2609 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002610
Dean Anderson14d96262008-08-25 13:58:55 -03002611 if (*pdata != S2255_FW_MARKER) {
2612 printk(KERN_INFO "Firmware invalid.\n");
2613 retval = -ENODEV;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002614 goto errorFWMARKER;
Dean Anderson14d96262008-08-25 13:58:55 -03002615 } else {
2616 /* make sure firmware is the latest */
2617 __le32 *pRel;
2618 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2619 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
Dean Anderson4de39f52010-03-03 19:39:19 -03002620 dev->dsp_fw_ver = *pRel;
2621 if (*pRel < S2255_CUR_DSP_FWVER)
2622 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
Dean Anderson5a34d9d2010-03-05 19:59:48 -03002623 if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002624 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
2625 "or above.\n", S2255_MIN_DSP_COLORFILTER);
Dean Anderson14d96262008-08-25 13:58:55 -03002626 }
Dean Anderson14d96262008-08-25 13:58:55 -03002627 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002628 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002629 retval = s2255_board_init(dev);
2630 if (retval)
Dean Andersonff7e22d2010-04-08 23:38:07 -03002631 goto errorBOARDINIT;
Dean Anderson38f993a2008-06-26 23:15:51 -03002632 spin_lock_init(&dev->slock);
Dean Anderson14d96262008-08-25 13:58:55 -03002633 s2255_fwload_start(dev, 0);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002634 /* kref for each vdev. Released on video_device_release callback */
2635 for (i = 0; i < MAX_CHANNELS; i++)
2636 kref_get(&dev->kref);
2637 /* loads v4l specific */
2638 retval = s2255_probe_v4l(dev);
2639 if (retval)
2640 goto errorV4L;
Dean Anderson38f993a2008-06-26 23:15:51 -03002641 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2642 return 0;
Dean Andersonff7e22d2010-04-08 23:38:07 -03002643errorV4L:
2644 for (i = 0; i < MAX_CHANNELS; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03002645 if (video_is_registered(&dev->vdev[i]))
2646 video_unregister_device(&dev->vdev[i]);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002647errorBOARDINIT:
2648 s2255_board_shutdown(dev);
2649errorFWMARKER:
2650 release_firmware(dev->fw_data->fw);
2651errorREQFW:
2652 kfree(dev->fw_data->pfw_data);
2653errorFWDATA2:
2654 usb_free_urb(dev->fw_data->fw_urb);
2655errorFWURB:
2656 del_timer(&dev->timer);
2657errorEP:
2658 usb_put_dev(dev->udev);
2659errorUDEV:
2660 kfree(dev->fw_data);
2661 mutex_destroy(&dev->open_lock);
2662 mutex_destroy(&dev->lock);
2663errorFWDATA1:
2664 kfree(dev);
2665 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
Dean Anderson38f993a2008-06-26 23:15:51 -03002666 return retval;
2667}
2668
Dean Andersonff7e22d2010-04-08 23:38:07 -03002669
Dean Anderson38f993a2008-06-26 23:15:51 -03002670/* disconnect routine. when board is removed physically or with rmmod */
2671static void s2255_disconnect(struct usb_interface *interface)
2672{
2673 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002674 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002675 dev = usb_get_intfdata(interface);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002676 /* unregister each video device. */
2677 for (i = 0; i < MAX_CHANNELS; i++)
Dean Andersonc0a2ec92010-04-08 23:40:31 -03002678 if (video_is_registered(&dev->vdev[i]))
2679 video_unregister_device(&dev->vdev[i]);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002680 /* wake up any of our timers */
Dean Anderson14d96262008-08-25 13:58:55 -03002681 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2682 wake_up(&dev->fw_data->wait_fw);
2683 for (i = 0; i < MAX_CHANNELS; i++) {
2684 dev->setmode_ready[i] = 1;
2685 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002686 dev->vidstatus_ready[i] = 1;
2687 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002688 }
Dean Anderson14d96262008-08-25 13:58:55 -03002689 usb_set_intfdata(interface, NULL);
Dean Andersonff7e22d2010-04-08 23:38:07 -03002690 kref_put(&dev->kref, s2255_destroy);
2691 dev_info(&interface->dev, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002692}
2693
2694static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002695 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002696 .probe = s2255_probe,
2697 .disconnect = s2255_disconnect,
2698 .id_table = s2255_table,
2699};
2700
2701static int __init usb_s2255_init(void)
2702{
2703 int result;
Dean Anderson38f993a2008-06-26 23:15:51 -03002704 /* register this driver with the USB subsystem */
2705 result = usb_register(&s2255_driver);
Dean Anderson38f993a2008-06-26 23:15:51 -03002706 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002707 pr_err(KBUILD_MODNAME
Dean Andersonff7e22d2010-04-08 23:38:07 -03002708 ": usb_register failed. Error number %d\n", result);
2709 dprintk(2, "%s\n", __func__);
Dean Anderson38f993a2008-06-26 23:15:51 -03002710 return result;
2711}
2712
2713static void __exit usb_s2255_exit(void)
2714{
2715 usb_deregister(&s2255_driver);
2716}
2717
2718module_init(usb_s2255_init);
2719module_exit(usb_s2255_exit);
2720
2721MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2722MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2723MODULE_LICENSE("GPL");