blob: 631a84eea1ae32f17985fec592ded6257a79c56d [file] [log] [blame]
Dean Anderson38f993a2008-06-26 23:15:51 -03001/*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
3 *
Dean Anderson4de39f52010-03-03 19:39:19 -03004 * Copyright (C) 2007-2010 by Sensoray Company Inc.
Dean Anderson38f993a2008-06-26 23:15:51 -03005 * Dean Anderson
6 *
7 * Some video buffer code based on vivi driver:
8 *
9 * Sensoray 2255 device supports 4 simultaneous channels.
10 * The channels are not "crossbar" inputs, they are physically
11 * attached to separate video decoders.
12 *
13 * Because of USB2.0 bandwidth limitations. There is only a
14 * certain amount of data which may be transferred at one time.
15 *
16 * Example maximum bandwidth utilization:
17 *
18 * -full size, color mode YUYV or YUV422P: 2 channels at once
19 *
20 * -full or half size Grey scale: all 4 channels at once
21 *
22 * -half size, color mode YUYV or YUV422P: all 4 channels at once
23 *
24 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
25 * at once.
26 * (TODO: Incorporate videodev2 frame rate(FR) enumeration,
27 * which is currently experimental.)
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#include <linux/module.h>
45#include <linux/firmware.h>
46#include <linux/kernel.h>
47#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030049#include <linux/videodev2.h>
50#include <linux/version.h>
Hans Verkuilfeb75f02008-07-27 06:30:21 -030051#include <linux/mm.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040052#include <linux/smp_lock.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030053#include <media/videobuf-vmalloc.h>
54#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030055#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030056#include <linux/vmalloc.h>
57#include <linux/usb.h>
58
59#define FIRMWARE_FILE_NAME "f2255usb.bin"
60
61
62
Dean Anderson22b88d42008-08-29 15:33:19 -030063/* default JPEG quality */
64#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030065/* vendor request in */
66#define S2255_VR_IN 0
67/* vendor request out */
68#define S2255_VR_OUT 1
69/* firmware query */
70#define S2255_VR_FW 0x30
71/* USB endpoint number for configuring the device */
72#define S2255_CONFIG_EP 2
73/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030074#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030075/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030076#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030077#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030078#define S2255_SETMODE_TIMEOUT 500
Dean Anderson4de39f52010-03-03 19:39:19 -030079#define S2255_VIDSTATUS_TIMEOUT 350
Dean Anderson38f993a2008-06-26 23:15:51 -030080#define MAX_CHANNELS 4
Dean Anderson3fa00602010-03-04 20:47:33 -030081#define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
82#define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
83#define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
84#define S2255_RESPONSE_FW cpu_to_le32(0x10)
85#define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
Dean Anderson14d96262008-08-25 13:58:55 -030086#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030087#define MAX_CHANNELS 4
88#define MAX_PIPE_BUFFERS 1
89#define SYS_FRAMES 4
90/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030091#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
92#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030093#define LINE_SZ_4CIFS_NTSC 640
94#define LINE_SZ_2CIFS_NTSC 640
95#define LINE_SZ_1CIFS_NTSC 320
96#define LINE_SZ_4CIFS_PAL 704
97#define LINE_SZ_2CIFS_PAL 704
98#define LINE_SZ_1CIFS_PAL 352
99#define NUM_LINES_4CIFS_NTSC 240
100#define NUM_LINES_2CIFS_NTSC 240
101#define NUM_LINES_1CIFS_NTSC 240
102#define NUM_LINES_4CIFS_PAL 288
103#define NUM_LINES_2CIFS_PAL 288
104#define NUM_LINES_1CIFS_PAL 288
105#define LINE_SZ_DEF 640
106#define NUM_LINES_DEF 240
107
108
109/* predefined settings */
110#define FORMAT_NTSC 1
111#define FORMAT_PAL 2
112
113#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
114#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
115#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
Dean Anderson7d853532009-05-15 14:32:04 -0300116/* SCALE_4CIFSI is the 2 fields interpolated into one */
117#define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
Dean Anderson38f993a2008-06-26 23:15:51 -0300118
119#define COLOR_YUVPL 1 /* YUV planar */
120#define COLOR_YUVPK 2 /* YUV packed */
121#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300122#define COLOR_JPG 5 /* JPEG */
123#define MASK_COLOR 0xff
124#define MASK_JPG_QUALITY 0xff00
Dean Anderson38f993a2008-06-26 23:15:51 -0300125
126/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
127#define FDEC_1 1 /* capture every frame. default */
128#define FDEC_2 2 /* capture every 2nd frame */
129#define FDEC_3 3 /* capture every 3rd frame */
130#define FDEC_5 5 /* capture every 5th frame */
131
132/*-------------------------------------------------------
133 * Default mode parameters.
134 *-------------------------------------------------------*/
135#define DEF_SCALE SCALE_4CIFS
136#define DEF_COLOR COLOR_YUVPL
137#define DEF_FDEC FDEC_1
138#define DEF_BRIGHT 0
139#define DEF_CONTRAST 0x5c
140#define DEF_SATURATION 0x80
141#define DEF_HUE 0
142
143/* usb config commands */
Dean Anderson3fa00602010-03-04 20:47:33 -0300144#define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
145#define CMD_2255 cpu_to_le32(0xc2255000)
146#define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
147#define CMD_START cpu_to_le32((CMD_2255 | 0x20))
148#define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
149#define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
Dean Anderson38f993a2008-06-26 23:15:51 -0300150
151struct s2255_mode {
152 u32 format; /* input video format (NTSC, PAL) */
153 u32 scale; /* output video scale */
154 u32 color; /* output video color format */
155 u32 fdec; /* frame decimation */
156 u32 bright; /* brightness */
157 u32 contrast; /* contrast */
158 u32 saturation; /* saturation */
159 u32 hue; /* hue (NTSC only)*/
160 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
161 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
162 u32 restart; /* if DSP requires restart */
163};
164
Dean Anderson14d96262008-08-25 13:58:55 -0300165
166#define S2255_READ_IDLE 0
167#define S2255_READ_FRAME 1
168
Dean Anderson38f993a2008-06-26 23:15:51 -0300169/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300170struct s2255_framei {
171 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300172 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300173 void *lpvbits; /* image data */
174 unsigned long cur_size; /* current data copied to it */
175};
176
177/* image buffer structure */
178struct s2255_bufferi {
179 unsigned long dwFrames; /* number of frames in buffer */
180 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
181};
182
183#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
184 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300185 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300186
187struct s2255_dmaqueue {
188 struct list_head active;
Dean Anderson38f993a2008-06-26 23:15:51 -0300189 struct s2255_dev *dev;
190 int channel;
191};
192
193/* for firmware loading, fw_state */
194#define S2255_FW_NOTLOADED 0
195#define S2255_FW_LOADED_DSPWAIT 1
196#define S2255_FW_SUCCESS 2
197#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300198#define S2255_FW_DISCONNECTING 4
Dean Anderson38f993a2008-06-26 23:15:51 -0300199
Harvey Harrisondeaf53e2008-11-15 01:10:14 -0300200#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
Dean Anderson14d96262008-08-25 13:58:55 -0300201/* 2255 read states */
202#define S2255_READ_IDLE 0
203#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300204struct s2255_fw {
205 int fw_loaded;
206 int fw_size;
207 struct urb *fw_urb;
208 atomic_t fw_state;
209 void *pfw_data;
210 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300211 const struct firmware *fw;
212};
213
214struct s2255_pipeinfo {
215 u32 max_transfer_size;
216 u32 cur_transfer_size;
217 u8 *transfer_buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -0300218 u32 state;
Dean Anderson38f993a2008-06-26 23:15:51 -0300219 void *stream_urb;
220 void *dev; /* back pointer to s2255_dev struct*/
221 u32 err_count;
Dean Anderson38f993a2008-06-26 23:15:51 -0300222 u32 idx;
Dean Anderson38f993a2008-06-26 23:15:51 -0300223};
224
225struct s2255_fmt; /*forward declaration */
226
227struct s2255_dev {
228 int frames;
229 int users[MAX_CHANNELS];
230 struct mutex lock;
231 struct mutex open_lock;
232 int resources[MAX_CHANNELS];
233 struct usb_device *udev;
234 struct usb_interface *interface;
235 u8 read_endpoint;
236
237 struct s2255_dmaqueue vidq[MAX_CHANNELS];
238 struct video_device *vdev[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300239 struct timer_list timer;
240 struct s2255_fw *fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -0300241 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
242 struct s2255_bufferi buffer[MAX_CHANNELS];
243 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300244 /* jpeg compression */
245 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300246 /* capture parameters (for high quality mode full size) */
247 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300248 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
249 int cur_frame[MAX_CHANNELS];
250 int last_frame[MAX_CHANNELS];
251 u32 cc; /* current channel */
252 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300253 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300254 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300255 /* received packet size */
256 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300257 int bad_payload[MAX_CHANNELS];
258 unsigned long frame_count[MAX_CHANNELS];
259 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300260 /* if JPEG image */
261 int jpg_size[MAX_CHANNELS];
262 /* if channel configured to default state */
263 int chn_configured[MAX_CHANNELS];
264 wait_queue_head_t wait_setmode[MAX_CHANNELS];
265 int setmode_ready[MAX_CHANNELS];
Dean Anderson4de39f52010-03-03 19:39:19 -0300266 /* video status items */
267 int vidstatus[MAX_CHANNELS];
268 wait_queue_head_t wait_vidstatus[MAX_CHANNELS];
269 int vidstatus_ready[MAX_CHANNELS];
270
Dean Anderson14d96262008-08-25 13:58:55 -0300271 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300272 struct kref kref;
273 spinlock_t slock;
Dean Anderson4de39f52010-03-03 19:39:19 -0300274 /* dsp firmware version (f2255usb.bin) */
275 int dsp_fw_ver;
Dean Anderson38f993a2008-06-26 23:15:51 -0300276};
277#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
278
279struct s2255_fmt {
280 char *name;
281 u32 fourcc;
282 int depth;
283};
284
285/* buffer for one video frame */
286struct s2255_buffer {
287 /* common v4l buffer stuff -- must be first */
288 struct videobuf_buffer vb;
289 const struct s2255_fmt *fmt;
290};
291
292struct s2255_fh {
293 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300294 const struct s2255_fmt *fmt;
295 unsigned int width;
296 unsigned int height;
297 struct videobuf_queue vb_vidq;
298 enum v4l2_buf_type type;
299 int channel;
300 /* mode below is the desired mode.
301 mode in s2255_dev is the current mode that was last set */
302 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300303 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300304};
305
Dean Andersonabce21f2009-04-23 16:04:41 -0300306/* current cypress EEPROM firmware version */
307#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson4de39f52010-03-03 19:39:19 -0300308/* current DSP FW version */
309#define S2255_CUR_DSP_FWVER 5
310/* Need DSP version 5+ for video status feature */
311#define S2255_MIN_DSP_STATUS 5
Dean Anderson38f993a2008-06-26 23:15:51 -0300312#define S2255_MAJOR_VERSION 1
Dean Anderson2e70db92010-03-05 14:29:09 -0300313#define S2255_MINOR_VERSION 17
Dean Anderson38f993a2008-06-26 23:15:51 -0300314#define S2255_RELEASE 0
315#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
316 S2255_MINOR_VERSION, \
317 S2255_RELEASE)
318
319/* vendor ids */
320#define USB_S2255_VENDOR_ID 0x1943
321#define USB_S2255_PRODUCT_ID 0x2255
322#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
323/* frame prefix size (sent once every frame) */
324#define PREFIX_SIZE 512
325
326/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300327static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300328
Dean Anderson38f993a2008-06-26 23:15:51 -0300329static int debug;
330static int *s2255_debug = &debug;
331
332static int s2255_start_readpipe(struct s2255_dev *dev);
333static void s2255_stop_readpipe(struct s2255_dev *dev);
334static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
335static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
336static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300337 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300338static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
339 struct s2255_mode *mode);
340static int s2255_board_shutdown(struct s2255_dev *dev);
341static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300342static void s2255_fwload_start(struct s2255_dev *dev, int reset);
343static void s2255_destroy(struct kref *kref);
344static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
345 u16 index, u16 value, void *buf,
346 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300347
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300348/* dev_err macro with driver name */
349#define S2255_DRIVER_NAME "s2255"
350#define s2255_dev_err(dev, fmt, arg...) \
351 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
352
Dean Anderson38f993a2008-06-26 23:15:51 -0300353#define dprintk(level, fmt, arg...) \
354 do { \
355 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300356 printk(KERN_DEBUG S2255_DRIVER_NAME \
357 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300358 } \
359 } while (0)
360
Dean Anderson38f993a2008-06-26 23:15:51 -0300361static struct usb_driver s2255_driver;
362
363
364/* Declare static vars that will be used as parameters */
365static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
366
367/* start video number */
368static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
369
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300370module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300371MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300372module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300373MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300374module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300375MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
376
377/* USB device table */
378static struct usb_device_id s2255_table[] = {
379 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
380 { } /* Terminating entry */
381};
382MODULE_DEVICE_TABLE(usb, s2255_table);
383
384
385#define BUFFER_TIMEOUT msecs_to_jiffies(400)
386
Dean Anderson38f993a2008-06-26 23:15:51 -0300387/* image formats. */
388static const struct s2255_fmt formats[] = {
389 {
390 .name = "4:2:2, planar, YUV422P",
391 .fourcc = V4L2_PIX_FMT_YUV422P,
392 .depth = 16
393
394 }, {
395 .name = "4:2:2, packed, YUYV",
396 .fourcc = V4L2_PIX_FMT_YUYV,
397 .depth = 16
398
399 }, {
400 .name = "4:2:2, packed, UYVY",
401 .fourcc = V4L2_PIX_FMT_UYVY,
402 .depth = 16
403 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300404 .name = "JPG",
405 .fourcc = V4L2_PIX_FMT_JPEG,
406 .depth = 24
407 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300408 .name = "8bpp GREY",
409 .fourcc = V4L2_PIX_FMT_GREY,
410 .depth = 8
411 }
412};
413
414static int norm_maxw(struct video_device *vdev)
415{
416 return (vdev->current_norm & V4L2_STD_NTSC) ?
417 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
418}
419
420static int norm_maxh(struct video_device *vdev)
421{
422 return (vdev->current_norm & V4L2_STD_NTSC) ?
423 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
424}
425
426static int norm_minw(struct video_device *vdev)
427{
428 return (vdev->current_norm & V4L2_STD_NTSC) ?
429 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
430}
431
432static int norm_minh(struct video_device *vdev)
433{
434 return (vdev->current_norm & V4L2_STD_NTSC) ?
435 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
436}
437
438
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300439/*
440 * TODO: fixme: move YUV reordering to hardware
441 * converts 2255 planar format to yuyv or uyvy
442 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300443static void planar422p_to_yuv_packed(const unsigned char *in,
444 unsigned char *out,
445 int width, int height,
446 int fmt)
447{
448 unsigned char *pY;
449 unsigned char *pCb;
450 unsigned char *pCr;
451 unsigned long size = height * width;
452 unsigned int i;
453 pY = (unsigned char *)in;
454 pCr = (unsigned char *)in + height * width;
455 pCb = (unsigned char *)in + height * width + (height * width / 2);
456 for (i = 0; i < size * 2; i += 4) {
457 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
458 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
459 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
460 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
461 }
462 return;
463}
464
Hans Verkuild45b9b82008-09-04 03:33:43 -0300465static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300466{
467 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
468 msleep(10);
469 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
470 return;
471}
Dean Anderson38f993a2008-06-26 23:15:51 -0300472
473/* kickstarts the firmware loading. from probe
474 */
475static void s2255_timer(unsigned long user_data)
476{
477 struct s2255_fw *data = (struct s2255_fw *)user_data;
478 dprintk(100, "s2255 timer\n");
479 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
480 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300481 atomic_set(&data->fw_state, S2255_FW_FAILED);
482 /* wake up anything waiting for the firmware */
483 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300484 return;
485 }
486}
487
Dean Anderson38f993a2008-06-26 23:15:51 -0300488
489/* this loads the firmware asynchronously.
490 Originally this was done synchroously in probe.
491 But it is better to load it asynchronously here than block
492 inside the probe function. Blocking inside probe affects boot time.
493 FW loading is triggered by the timer in the probe function
494*/
495static void s2255_fwchunk_complete(struct urb *urb)
496{
497 struct s2255_fw *data = urb->context;
498 struct usb_device *udev = urb->dev;
499 int len;
500 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300501 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300502 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300503 atomic_set(&data->fw_state, S2255_FW_FAILED);
504 /* wake up anything waiting for the firmware */
505 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300506 return;
507 }
508 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300509 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300510 atomic_set(&data->fw_state, S2255_FW_FAILED);
511 /* wake up anything waiting for the firmware */
512 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300513 return;
514 }
515#define CHUNK_SIZE 512
516 /* all USB transfers must be done with continuous kernel memory.
517 can't allocate more than 128k in current linux kernel, so
518 upload the firmware in chunks
519 */
520 if (data->fw_loaded < data->fw_size) {
521 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
522 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
523
524 if (len < CHUNK_SIZE)
525 memset(data->pfw_data, 0, CHUNK_SIZE);
526
527 dprintk(100, "completed len %d, loaded %d \n", len,
528 data->fw_loaded);
529
530 memcpy(data->pfw_data,
531 (char *) data->fw->data + data->fw_loaded, len);
532
533 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
534 data->pfw_data, CHUNK_SIZE,
535 s2255_fwchunk_complete, data);
536 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
537 dev_err(&udev->dev, "failed submit URB\n");
538 atomic_set(&data->fw_state, S2255_FW_FAILED);
539 /* wake up anything waiting for the firmware */
540 wake_up(&data->wait_fw);
541 return;
542 }
543 data->fw_loaded += len;
544 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300545 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300546 }
547 dprintk(100, "2255 complete done\n");
548 return;
549
550}
551
Dean Anderson14d96262008-08-25 13:58:55 -0300552static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300553{
554 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
555 struct s2255_buffer *buf;
556 unsigned long flags = 0;
557 int rc = 0;
558 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
559 spin_lock_irqsave(&dev->slock, flags);
560
561 if (list_empty(&dma_q->active)) {
562 dprintk(1, "No active queue to serve\n");
563 rc = -1;
564 goto unlock;
565 }
566 buf = list_entry(dma_q->active.next,
567 struct s2255_buffer, vb.queue);
568
Dean Anderson38f993a2008-06-26 23:15:51 -0300569 list_del(&buf->vb.queue);
570 do_gettimeofday(&buf->vb.ts);
571 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300572 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300573 wake_up(&buf->vb.done);
574 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
575unlock:
576 spin_unlock_irqrestore(&dev->slock, flags);
577 return 0;
578}
579
580
581static const struct s2255_fmt *format_by_fourcc(int fourcc)
582{
583 unsigned int i;
584
585 for (i = 0; i < ARRAY_SIZE(formats); i++) {
586 if (-1 == formats[i].fourcc)
587 continue;
588 if (formats[i].fourcc == fourcc)
589 return formats + i;
590 }
591 return NULL;
592}
593
594
595
596
597/* video buffer vmalloc implementation based partly on VIVI driver which is
598 * Copyright (c) 2006 by
599 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
600 * Ted Walther <ted--a.t--enumera.com>
601 * John Sokol <sokol--a.t--videotechnology.com>
602 * http://v4l.videotechnology.com/
603 *
604 */
605static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300606 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300607{
608 int pos = 0;
609 struct timeval ts;
610 const char *tmpbuf;
611 char *vbuf = videobuf_to_vmalloc(&buf->vb);
612 unsigned long last_frame;
613 struct s2255_framei *frm;
614
615 if (!vbuf)
616 return;
617
618 last_frame = dev->last_frame[chn];
619 if (last_frame != -1) {
620 frm = &dev->buffer[chn].frame[last_frame];
621 tmpbuf =
622 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
623 switch (buf->fmt->fourcc) {
624 case V4L2_PIX_FMT_YUYV:
625 case V4L2_PIX_FMT_UYVY:
626 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
627 vbuf, buf->vb.width,
628 buf->vb.height,
629 buf->fmt->fourcc);
630 break;
631 case V4L2_PIX_FMT_GREY:
632 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
633 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300634 case V4L2_PIX_FMT_JPEG:
635 buf->vb.size = jpgsize;
636 memcpy(vbuf, tmpbuf, buf->vb.size);
637 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300638 case V4L2_PIX_FMT_YUV422P:
639 memcpy(vbuf, tmpbuf,
640 buf->vb.width * buf->vb.height * 2);
641 break;
642 default:
643 printk(KERN_DEBUG "s2255: unknown format?\n");
644 }
645 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300646 } else {
647 printk(KERN_ERR "s2255: =======no frame\n");
648 return;
649
650 }
651 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
652 (unsigned long)vbuf, pos);
653 /* tell v4l buffer was filled */
654
Dean Andersona1c45302008-09-09 12:29:56 -0300655 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300656 do_gettimeofday(&ts);
657 buf->vb.ts = ts;
658 buf->vb.state = VIDEOBUF_DONE;
659}
660
661
662/* ------------------------------------------------------------------
663 Videobuf operations
664 ------------------------------------------------------------------*/
665
666static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
667 unsigned int *size)
668{
669 struct s2255_fh *fh = vq->priv_data;
670
671 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
672
673 if (0 == *count)
674 *count = S2255_DEF_BUFS;
675
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300676 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300677 (*count)--;
678
679 return 0;
680}
681
682static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
683{
684 dprintk(4, "%s\n", __func__);
685
Dean Anderson38f993a2008-06-26 23:15:51 -0300686 videobuf_vmalloc_free(&buf->vb);
687 buf->vb.state = VIDEOBUF_NEEDS_INIT;
688}
689
690static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
691 enum v4l2_field field)
692{
693 struct s2255_fh *fh = vq->priv_data;
694 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
695 int rc;
696 dprintk(4, "%s, field=%d\n", __func__, field);
697 if (fh->fmt == NULL)
698 return -EINVAL;
699
700 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
701 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
702 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
703 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
704 dprintk(4, "invalid buffer prepare\n");
705 return -EINVAL;
706 }
707
708 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
709
710 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
711 dprintk(4, "invalid buffer prepare\n");
712 return -EINVAL;
713 }
714
715 buf->fmt = fh->fmt;
716 buf->vb.width = fh->width;
717 buf->vb.height = fh->height;
718 buf->vb.field = field;
719
720
721 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
722 rc = videobuf_iolock(vq, &buf->vb, NULL);
723 if (rc < 0)
724 goto fail;
725 }
726
727 buf->vb.state = VIDEOBUF_PREPARED;
728 return 0;
729fail:
730 free_buffer(vq, buf);
731 return rc;
732}
733
734static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
735{
736 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
737 struct s2255_fh *fh = vq->priv_data;
738 struct s2255_dev *dev = fh->dev;
739 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
740
741 dprintk(1, "%s\n", __func__);
742
743 buf->vb.state = VIDEOBUF_QUEUED;
744 list_add_tail(&buf->vb.queue, &vidq->active);
745}
746
747static void buffer_release(struct videobuf_queue *vq,
748 struct videobuf_buffer *vb)
749{
750 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
751 struct s2255_fh *fh = vq->priv_data;
752 dprintk(4, "%s %d\n", __func__, fh->channel);
753 free_buffer(vq, buf);
754}
755
756static struct videobuf_queue_ops s2255_video_qops = {
757 .buf_setup = buffer_setup,
758 .buf_prepare = buffer_prepare,
759 .buf_queue = buffer_queue,
760 .buf_release = buffer_release,
761};
762
763
764static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
765{
766 /* is it free? */
767 mutex_lock(&dev->lock);
768 if (dev->resources[fh->channel]) {
769 /* no, someone else uses it */
770 mutex_unlock(&dev->lock);
771 return 0;
772 }
773 /* it's free, grab it */
774 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300775 fh->resources[fh->channel] = 1;
776 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300777 mutex_unlock(&dev->lock);
778 return 1;
779}
780
781static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
782{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300783 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300784}
785
Dean Andersonf78d92c2008-07-22 14:43:27 -0300786static int res_check(struct s2255_fh *fh)
787{
788 return fh->resources[fh->channel];
789}
790
791
Dean Anderson38f993a2008-06-26 23:15:51 -0300792static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
793{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300794 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300795 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300796 fh->resources[fh->channel] = 0;
797 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300798 dprintk(1, "res: put\n");
799}
800
801
802static int vidioc_querycap(struct file *file, void *priv,
803 struct v4l2_capability *cap)
804{
805 struct s2255_fh *fh = file->private_data;
806 struct s2255_dev *dev = fh->dev;
807 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
808 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300809 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300810 cap->version = S2255_VERSION;
811 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
812 return 0;
813}
814
815static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
816 struct v4l2_fmtdesc *f)
817{
818 int index = 0;
819 if (f)
820 index = f->index;
821
822 if (index >= ARRAY_SIZE(formats))
823 return -EINVAL;
824
825 dprintk(4, "name %s\n", formats[index].name);
826 strlcpy(f->description, formats[index].name, sizeof(f->description));
827 f->pixelformat = formats[index].fourcc;
828 return 0;
829}
830
831static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
832 struct v4l2_format *f)
833{
834 struct s2255_fh *fh = priv;
835
836 f->fmt.pix.width = fh->width;
837 f->fmt.pix.height = fh->height;
838 f->fmt.pix.field = fh->vb_vidq.field;
839 f->fmt.pix.pixelformat = fh->fmt->fourcc;
840 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
841 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300842 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300843}
844
845static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
846 struct v4l2_format *f)
847{
848 const struct s2255_fmt *fmt;
849 enum v4l2_field field;
850 int b_any_field = 0;
851 struct s2255_fh *fh = priv;
852 struct s2255_dev *dev = fh->dev;
853 int is_ntsc;
854
855 is_ntsc =
856 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
857
858 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
859
860 if (fmt == NULL)
861 return -EINVAL;
862
863 field = f->fmt.pix.field;
864 if (field == V4L2_FIELD_ANY)
865 b_any_field = 1;
866
867 dprintk(4, "try format %d \n", is_ntsc);
868 /* supports 3 sizes. see s2255drv.h */
869 dprintk(50, "width test %d, height %d\n",
870 f->fmt.pix.width, f->fmt.pix.height);
871 if (is_ntsc) {
872 /* NTSC */
873 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
874 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
875 if (b_any_field) {
876 field = V4L2_FIELD_SEQ_TB;
877 } else if (!((field == V4L2_FIELD_INTERLACED) ||
878 (field == V4L2_FIELD_SEQ_TB) ||
879 (field == V4L2_FIELD_INTERLACED_TB))) {
880 dprintk(1, "unsupported field setting\n");
881 return -EINVAL;
882 }
883 } else {
884 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
885 if (b_any_field) {
886 field = V4L2_FIELD_TOP;
887 } else if (!((field == V4L2_FIELD_TOP) ||
888 (field == V4L2_FIELD_BOTTOM))) {
889 dprintk(1, "unsupported field setting\n");
890 return -EINVAL;
891 }
892
893 }
894 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
895 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
896 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
897 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
898 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
899 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
900 else
901 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
902 } else {
903 /* PAL */
904 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
905 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
906 if (b_any_field) {
907 field = V4L2_FIELD_SEQ_TB;
908 } else if (!((field == V4L2_FIELD_INTERLACED) ||
909 (field == V4L2_FIELD_SEQ_TB) ||
910 (field == V4L2_FIELD_INTERLACED_TB))) {
911 dprintk(1, "unsupported field setting\n");
912 return -EINVAL;
913 }
914 } else {
915 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
916 if (b_any_field) {
917 field = V4L2_FIELD_TOP;
918 } else if (!((field == V4L2_FIELD_TOP) ||
919 (field == V4L2_FIELD_BOTTOM))) {
920 dprintk(1, "unsupported field setting\n");
921 return -EINVAL;
922 }
923 }
924 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
925 dprintk(50, "pal 704\n");
926 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
927 field = V4L2_FIELD_SEQ_TB;
928 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
929 dprintk(50, "pal 352A\n");
930 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
931 field = V4L2_FIELD_TOP;
932 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
933 dprintk(50, "pal 352B\n");
934 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
935 field = V4L2_FIELD_TOP;
936 } else {
937 dprintk(50, "pal 352C\n");
938 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
939 field = V4L2_FIELD_TOP;
940 }
941 }
942
943 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
944 f->fmt.pix.height, f->fmt.pix.field);
945 f->fmt.pix.field = field;
946 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
947 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
948 return 0;
949}
950
951static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
952 struct v4l2_format *f)
953{
954 struct s2255_fh *fh = priv;
955 const struct s2255_fmt *fmt;
956 struct videobuf_queue *q = &fh->vb_vidq;
957 int ret;
958 int norm;
959
960 ret = vidioc_try_fmt_vid_cap(file, fh, f);
961
962 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300963 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300964
965 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
966
967 if (fmt == NULL)
968 return -EINVAL;
969
970 mutex_lock(&q->vb_lock);
971
972 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
973 dprintk(1, "queue busy\n");
974 ret = -EBUSY;
975 goto out_s_fmt;
976 }
977
978 if (res_locked(fh->dev, fh)) {
979 dprintk(1, "can't change format after started\n");
980 ret = -EBUSY;
981 goto out_s_fmt;
982 }
983
984 fh->fmt = fmt;
985 fh->width = f->fmt.pix.width;
986 fh->height = f->fmt.pix.height;
987 fh->vb_vidq.field = f->fmt.pix.field;
988 fh->type = f->type;
989 norm = norm_minw(fh->dev->vdev[fh->channel]);
990 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -0300991 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
992 if (fh->dev->cap_parm[fh->channel].capturemode &
993 V4L2_MODE_HIGHQUALITY) {
994 fh->mode.scale = SCALE_4CIFSI;
995 dprintk(2, "scale 4CIFSI\n");
996 } else {
997 fh->mode.scale = SCALE_4CIFS;
998 dprintk(2, "scale 4CIFS\n");
999 }
1000 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001001 fh->mode.scale = SCALE_2CIFS;
1002
1003 } else {
1004 fh->mode.scale = SCALE_1CIFS;
1005 }
1006
1007 /* color mode */
1008 switch (fh->fmt->fourcc) {
1009 case V4L2_PIX_FMT_GREY:
1010 fh->mode.color = COLOR_Y8;
1011 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001012 case V4L2_PIX_FMT_JPEG:
Dean Anderson22b88d42008-08-29 15:33:19 -03001013 fh->mode.color = COLOR_JPG |
1014 (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001015 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001016 case V4L2_PIX_FMT_YUV422P:
1017 fh->mode.color = COLOR_YUVPL;
1018 break;
1019 case V4L2_PIX_FMT_YUYV:
1020 case V4L2_PIX_FMT_UYVY:
1021 default:
1022 fh->mode.color = COLOR_YUVPK;
1023 break;
1024 }
1025 ret = 0;
1026out_s_fmt:
1027 mutex_unlock(&q->vb_lock);
1028 return ret;
1029}
1030
1031static int vidioc_reqbufs(struct file *file, void *priv,
1032 struct v4l2_requestbuffers *p)
1033{
1034 int rc;
1035 struct s2255_fh *fh = priv;
1036 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1037 return rc;
1038}
1039
1040static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1041{
1042 int rc;
1043 struct s2255_fh *fh = priv;
1044 rc = videobuf_querybuf(&fh->vb_vidq, p);
1045 return rc;
1046}
1047
1048static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1049{
1050 int rc;
1051 struct s2255_fh *fh = priv;
1052 rc = videobuf_qbuf(&fh->vb_vidq, p);
1053 return rc;
1054}
1055
1056static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1057{
1058 int rc;
1059 struct s2255_fh *fh = priv;
1060 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1061 return rc;
1062}
1063
1064#ifdef CONFIG_VIDEO_V4L1_COMPAT
1065static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1066{
1067 struct s2255_fh *fh = priv;
1068
1069 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1070}
1071#endif
1072
1073/* write to the configuration pipe, synchronously */
1074static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1075 int size)
1076{
1077 int pipe;
1078 int done;
1079 long retval = -1;
1080 if (udev) {
1081 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1082 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1083 }
1084 return retval;
1085}
1086
1087static u32 get_transfer_size(struct s2255_mode *mode)
1088{
1089 int linesPerFrame = LINE_SZ_DEF;
1090 int pixelsPerLine = NUM_LINES_DEF;
1091 u32 outImageSize;
1092 u32 usbInSize;
1093 unsigned int mask_mult;
1094
1095 if (mode == NULL)
1096 return 0;
1097
1098 if (mode->format == FORMAT_NTSC) {
1099 switch (mode->scale) {
1100 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001101 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001102 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1103 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1104 break;
1105 case SCALE_2CIFS:
1106 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1107 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1108 break;
1109 case SCALE_1CIFS:
1110 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1111 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1112 break;
1113 default:
1114 break;
1115 }
1116 } else if (mode->format == FORMAT_PAL) {
1117 switch (mode->scale) {
1118 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001119 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001120 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1121 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1122 break;
1123 case SCALE_2CIFS:
1124 linesPerFrame = NUM_LINES_2CIFS_PAL;
1125 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1126 break;
1127 case SCALE_1CIFS:
1128 linesPerFrame = NUM_LINES_1CIFS_PAL;
1129 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1130 break;
1131 default:
1132 break;
1133 }
1134 }
1135 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001136 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001137 /* 2 bytes/pixel if not monochrome */
1138 outImageSize *= 2;
1139 }
1140
1141 /* total bytes to send including prefix and 4K padding;
1142 must be a multiple of USB_READ_SIZE */
1143 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1144 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1145 /* if size not a multiple of USB_READ_SIZE */
1146 if (usbInSize & ~mask_mult)
1147 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1148 return usbInSize;
1149}
1150
1151static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1152{
1153 struct device *dev = &sdev->udev->dev;
1154 dev_info(dev, "------------------------------------------------\n");
1155 dev_info(dev, "verify mode\n");
1156 dev_info(dev, "format: %d\n", mode->format);
1157 dev_info(dev, "scale: %d\n", mode->scale);
1158 dev_info(dev, "fdec: %d\n", mode->fdec);
1159 dev_info(dev, "color: %d\n", mode->color);
1160 dev_info(dev, "bright: 0x%x\n", mode->bright);
1161 dev_info(dev, "restart: 0x%x\n", mode->restart);
1162 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1163 dev_info(dev, "single: 0x%x\n", mode->single);
1164 dev_info(dev, "------------------------------------------------\n");
1165}
1166
1167/*
1168 * set mode is the function which controls the DSP.
1169 * the restart parameter in struct s2255_mode should be set whenever
1170 * the image size could change via color format, video system or image
1171 * size.
1172 * When the restart parameter is set, we sleep for ONE frame to allow the
1173 * DSP time to get the new frame
1174 */
1175static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1176 struct s2255_mode *mode)
1177{
1178 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001179 __le32 *buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -03001180 unsigned long chn_rev;
Dean Anderson14d96262008-08-25 13:58:55 -03001181 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001182 chn_rev = G_chnmap[chn];
1183 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1184 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1185 dev->mode[chn].scale);
1186 dprintk(2, "mode contrast %x\n", mode->contrast);
1187
Dean Anderson22b88d42008-08-29 15:33:19 -03001188 /* if JPEG, set the quality */
1189 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1190 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1191
Dean Anderson38f993a2008-06-26 23:15:51 -03001192 /* save the mode */
1193 dev->mode[chn] = *mode;
1194 dev->req_image_size[chn] = get_transfer_size(mode);
1195 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1196
1197 buffer = kzalloc(512, GFP_KERNEL);
1198 if (buffer == NULL) {
1199 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001200 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001201 return -ENOMEM;
1202 }
1203
1204 /* set the mode */
1205 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001206 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001207 buffer[2] = CMD_SET_MODE;
1208 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001209 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001210 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1211 if (debug)
1212 dump_verify_mode(dev, mode);
1213 kfree(buffer);
1214 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1215
1216 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001217 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001218 wait_event_timeout(dev->wait_setmode[chn],
1219 (dev->setmode_ready[chn] != 0),
1220 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1221 if (dev->setmode_ready[chn] != 1) {
1222 printk(KERN_DEBUG "s2255: no set mode response\n");
1223 res = -EFAULT;
1224 }
1225 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001226
1227 /* clear the restart flag */
1228 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001229 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001230 return res;
1231}
1232
Dean Anderson4de39f52010-03-03 19:39:19 -03001233static int s2255_cmd_status(struct s2255_dev *dev, unsigned long chn,
1234 u32 *pstatus)
1235{
1236 int res;
Dean Anderson3fa00602010-03-04 20:47:33 -03001237 __le32 *buffer;
Dean Anderson4de39f52010-03-03 19:39:19 -03001238 u32 chn_rev;
1239 mutex_lock(&dev->lock);
1240 chn_rev = G_chnmap[chn];
1241 dprintk(4, "%s chan %d\n", __func__, chn_rev);
1242 buffer = kzalloc(512, GFP_KERNEL);
1243 if (buffer == NULL) {
1244 dev_err(&dev->udev->dev, "out of mem\n");
1245 mutex_unlock(&dev->lock);
1246 return -ENOMEM;
1247 }
1248 /* form the get vid status command */
1249 buffer[0] = IN_DATA_TOKEN;
Dean Anderson3fa00602010-03-04 20:47:33 -03001250 buffer[1] = (__le32) cpu_to_le32(chn_rev);
Dean Anderson4de39f52010-03-03 19:39:19 -03001251 buffer[2] = CMD_STATUS;
1252 *pstatus = 0;
1253 dev->vidstatus_ready[chn] = 0;
1254 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1255 kfree(buffer);
1256 wait_event_timeout(dev->wait_vidstatus[chn],
1257 (dev->vidstatus_ready[chn] != 0),
1258 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1259 if (dev->vidstatus_ready[chn] != 1) {
1260 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1261 res = -EFAULT;
1262 }
1263 *pstatus = dev->vidstatus[chn];
1264 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
1265 mutex_unlock(&dev->lock);
1266 return res;
1267}
1268
Dean Anderson38f993a2008-06-26 23:15:51 -03001269static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1270{
1271 int res;
1272 struct s2255_fh *fh = priv;
1273 struct s2255_dev *dev = fh->dev;
1274 struct s2255_mode *new_mode;
1275 struct s2255_mode *old_mode;
1276 int chn;
1277 int j;
1278 dprintk(4, "%s\n", __func__);
1279 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1280 dev_err(&dev->udev->dev, "invalid fh type0\n");
1281 return -EINVAL;
1282 }
1283 if (i != fh->type) {
1284 dev_err(&dev->udev->dev, "invalid fh type1\n");
1285 return -EINVAL;
1286 }
1287
1288 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001289 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001290 return -EBUSY;
1291 }
1292
1293 /* send a set mode command everytime with restart.
1294 in case we switch resolutions or other parameters */
1295 chn = fh->channel;
1296 new_mode = &fh->mode;
1297 old_mode = &fh->dev->mode[chn];
1298
1299 if (new_mode->color != old_mode->color)
1300 new_mode->restart = 1;
1301 else if (new_mode->scale != old_mode->scale)
1302 new_mode->restart = 1;
1303 else if (new_mode->format != old_mode->format)
1304 new_mode->restart = 1;
1305
1306 s2255_set_mode(dev, chn, new_mode);
1307 new_mode->restart = 0;
1308 *old_mode = *new_mode;
1309 dev->cur_fmt[chn] = fh->fmt;
1310 dprintk(1, "%s[%d]\n", __func__, chn);
1311 dev->last_frame[chn] = -1;
1312 dev->bad_payload[chn] = 0;
1313 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001314 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001315 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001316 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001317 dev->buffer[chn].frame[j].cur_size = 0;
1318 }
1319 res = videobuf_streamon(&fh->vb_vidq);
1320 if (res == 0) {
1321 s2255_start_acquire(dev, chn);
1322 dev->b_acquire[chn] = 1;
1323 } else {
1324 res_free(dev, fh);
1325 }
1326 return res;
1327}
1328
1329static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1330{
Dean Anderson38f993a2008-06-26 23:15:51 -03001331 struct s2255_fh *fh = priv;
1332 struct s2255_dev *dev = fh->dev;
1333
1334 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1335 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1336 printk(KERN_ERR "invalid fh type0\n");
1337 return -EINVAL;
1338 }
1339 if (i != fh->type) {
1340 printk(KERN_ERR "invalid type i\n");
1341 return -EINVAL;
1342 }
1343 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001344 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001345 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001346 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001347}
1348
1349static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1350{
1351 struct s2255_fh *fh = priv;
1352 struct s2255_mode *mode;
1353 struct videobuf_queue *q = &fh->vb_vidq;
1354 int ret = 0;
1355
1356 mutex_lock(&q->vb_lock);
1357 if (videobuf_queue_is_busy(q)) {
1358 dprintk(1, "queue busy\n");
1359 ret = -EBUSY;
1360 goto out_s_std;
1361 }
1362
1363 if (res_locked(fh->dev, fh)) {
1364 dprintk(1, "can't change standard after started\n");
1365 ret = -EBUSY;
1366 goto out_s_std;
1367 }
1368 mode = &fh->mode;
1369
1370 if (*i & V4L2_STD_NTSC) {
1371 dprintk(4, "vidioc_s_std NTSC\n");
1372 mode->format = FORMAT_NTSC;
1373 } else if (*i & V4L2_STD_PAL) {
1374 dprintk(4, "vidioc_s_std PAL\n");
1375 mode->format = FORMAT_PAL;
1376 } else {
1377 ret = -EINVAL;
1378 }
1379out_s_std:
1380 mutex_unlock(&q->vb_lock);
1381 return ret;
1382}
1383
1384/* Sensoray 2255 is a multiple channel capture device.
1385 It does not have a "crossbar" of inputs.
1386 We use one V4L device per channel. The user must
1387 be aware that certain combinations are not allowed.
1388 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1389 at once in color(you can do full fps on 4 channels with greyscale.
1390*/
1391static int vidioc_enum_input(struct file *file, void *priv,
1392 struct v4l2_input *inp)
1393{
Dean Anderson4de39f52010-03-03 19:39:19 -03001394 struct s2255_fh *fh = priv;
1395 struct s2255_dev *dev = fh->dev;
1396 u32 status = 0;
1397
Dean Anderson38f993a2008-06-26 23:15:51 -03001398 if (inp->index != 0)
1399 return -EINVAL;
1400
1401 inp->type = V4L2_INPUT_TYPE_CAMERA;
1402 inp->std = S2255_NORMS;
Dean Anderson4de39f52010-03-03 19:39:19 -03001403 inp->status = 0;
1404 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1405 int rc;
1406 rc = s2255_cmd_status(dev, fh->channel, &status);
1407 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1408 if (rc == 0)
1409 inp->status = (status & 0x01) ? 0
1410 : V4L2_IN_ST_NO_SIGNAL;
1411 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001412 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001413 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001414}
1415
1416static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1417{
1418 *i = 0;
1419 return 0;
1420}
1421static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1422{
1423 if (i > 0)
1424 return -EINVAL;
1425 return 0;
1426}
1427
1428/* --- controls ---------------------------------------------- */
1429static int vidioc_queryctrl(struct file *file, void *priv,
1430 struct v4l2_queryctrl *qc)
1431{
Dean Anderson2e70db92010-03-05 14:29:09 -03001432 switch (qc->id) {
1433 case V4L2_CID_BRIGHTNESS:
1434 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1435 break;
1436 case V4L2_CID_CONTRAST:
1437 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1438 break;
1439 case V4L2_CID_SATURATION:
1440 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1441 break;
1442 case V4L2_CID_HUE:
1443 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1444 break;
1445 default:
1446 return -EINVAL;
1447 }
1448 dprintk(4, "%s, id %d\n", __func__, qc->id);
1449 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001450}
1451
1452static int vidioc_g_ctrl(struct file *file, void *priv,
1453 struct v4l2_control *ctrl)
1454{
Dean Anderson2e70db92010-03-05 14:29:09 -03001455 struct s2255_fh *fh = priv;
1456 switch (ctrl->id) {
1457 case V4L2_CID_BRIGHTNESS:
1458 ctrl->value = fh->mode.bright;
1459 break;
1460 case V4L2_CID_CONTRAST:
1461 ctrl->value = fh->mode.contrast;
1462 break;
1463 case V4L2_CID_SATURATION:
1464 ctrl->value = fh->mode.saturation;
1465 break;
1466 case V4L2_CID_HUE:
1467 ctrl->value = fh->mode.hue;
1468 break;
1469 default:
1470 return -EINVAL;
1471 }
1472 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1473 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001474}
1475
1476static int vidioc_s_ctrl(struct file *file, void *priv,
1477 struct v4l2_control *ctrl)
1478{
Dean Anderson38f993a2008-06-26 23:15:51 -03001479 struct s2255_fh *fh = priv;
1480 struct s2255_dev *dev = fh->dev;
1481 struct s2255_mode *mode;
1482 mode = &fh->mode;
Dean Anderson2e70db92010-03-05 14:29:09 -03001483 dprintk(4, "%s\n", __func__);
1484 /* update the mode to the corresponding value */
1485 switch (ctrl->id) {
1486 case V4L2_CID_BRIGHTNESS:
1487 mode->bright = ctrl->value;
1488 break;
1489 case V4L2_CID_CONTRAST:
1490 mode->contrast = ctrl->value;
1491 break;
1492 case V4L2_CID_HUE:
1493 mode->hue = ctrl->value;
1494 break;
1495 case V4L2_CID_SATURATION:
1496 mode->saturation = ctrl->value;
1497 break;
1498 default:
1499 return -EINVAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03001500 }
Dean Anderson2e70db92010-03-05 14:29:09 -03001501 mode->restart = 0;
1502 /* set mode here. Note: stream does not need restarted.
1503 some V4L programs restart stream unnecessarily
1504 after a s_crtl.
1505 */
1506 s2255_set_mode(dev, fh->channel, mode);
1507 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001508}
1509
Dean Anderson22b88d42008-08-29 15:33:19 -03001510static int vidioc_g_jpegcomp(struct file *file, void *priv,
1511 struct v4l2_jpegcompression *jc)
1512{
1513 struct s2255_fh *fh = priv;
1514 struct s2255_dev *dev = fh->dev;
1515 *jc = dev->jc[fh->channel];
1516 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1517 return 0;
1518}
1519
1520static int vidioc_s_jpegcomp(struct file *file, void *priv,
1521 struct v4l2_jpegcompression *jc)
1522{
1523 struct s2255_fh *fh = priv;
1524 struct s2255_dev *dev = fh->dev;
1525 if (jc->quality < 0 || jc->quality > 100)
1526 return -EINVAL;
1527 dev->jc[fh->channel].quality = jc->quality;
1528 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1529 return 0;
1530}
Dean Anderson7d853532009-05-15 14:32:04 -03001531
1532static int vidioc_g_parm(struct file *file, void *priv,
1533 struct v4l2_streamparm *sp)
1534{
1535 struct s2255_fh *fh = priv;
1536 struct s2255_dev *dev = fh->dev;
1537 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1538 return -EINVAL;
1539 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1540 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1541 return 0;
1542}
1543
1544static int vidioc_s_parm(struct file *file, void *priv,
1545 struct v4l2_streamparm *sp)
1546{
1547 struct s2255_fh *fh = priv;
1548 struct s2255_dev *dev = fh->dev;
1549
1550 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1551 return -EINVAL;
1552
1553 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1554 dprintk(2, "setting param capture mode %d\n",
1555 sp->parm.capture.capturemode);
1556 return 0;
1557}
Hans Verkuilbec43662008-12-30 06:58:20 -03001558static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001559{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001560 struct video_device *vdev = video_devdata(file);
1561 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001562 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001563 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001564 int i = 0;
1565 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001566 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001567
1568 dprintk(1, "s2255: open called (dev=%s)\n",
1569 video_device_node_name(vdev));
Dean Anderson38f993a2008-06-26 23:15:51 -03001570
Hans Verkuild56dc612008-07-30 08:43:36 -03001571 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001572
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001573 for (i = 0; i < MAX_CHANNELS; i++) {
1574 if (dev->vdev[i] == vdev) {
1575 cur_channel = i;
1576 break;
1577 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001578 }
1579
Dean Anderson14d96262008-08-25 13:58:55 -03001580 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1581 unlock_kernel();
1582 printk(KERN_INFO "disconnecting\n");
1583 return -ENODEV;
1584 }
1585 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001586 mutex_lock(&dev->open_lock);
1587
1588 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001589 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001590
Dean Anderson14d96262008-08-25 13:58:55 -03001591 switch (atomic_read(&dev->fw_data->fw_state)) {
1592 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001593 s2255_dev_err(&dev->udev->dev,
1594 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001595 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001596 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001597 ((atomic_read(&dev->fw_data->fw_state)
1598 == S2255_FW_SUCCESS) ||
1599 (atomic_read(&dev->fw_data->fw_state)
1600 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001601 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001602 break;
1603 case S2255_FW_NOTLOADED:
1604 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001605 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1606 driver loaded and then device immediately opened */
1607 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1608 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001609 ((atomic_read(&dev->fw_data->fw_state)
1610 == S2255_FW_SUCCESS) ||
1611 (atomic_read(&dev->fw_data->fw_state)
1612 == S2255_FW_DISCONNECTING)),
1613 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1614 break;
1615 case S2255_FW_SUCCESS:
1616 default:
1617 break;
1618 }
1619 state = atomic_read(&dev->fw_data->fw_state);
1620 if (state != S2255_FW_SUCCESS) {
1621 int rc;
1622 switch (state) {
1623 case S2255_FW_FAILED:
1624 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1625 rc = -ENODEV;
1626 break;
1627 case S2255_FW_DISCONNECTING:
1628 printk(KERN_INFO "%s: disconnecting\n", __func__);
1629 rc = -ENODEV;
1630 break;
1631 case S2255_FW_LOADED_DSPWAIT:
1632 case S2255_FW_NOTLOADED:
1633 printk(KERN_INFO "%s: firmware not loaded yet"
1634 "please try again later\n",
1635 __func__);
1636 rc = -EAGAIN;
1637 break;
1638 default:
1639 printk(KERN_INFO "%s: unknown state\n", __func__);
1640 rc = -EFAULT;
1641 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001642 }
Dean Anderson14d96262008-08-25 13:58:55 -03001643 dev->users[cur_channel]--;
1644 mutex_unlock(&dev->open_lock);
1645 kref_put(&dev->kref, s2255_destroy);
1646 unlock_kernel();
1647 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001648 }
1649
1650 /* allocate + initialize per filehandle data */
1651 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1652 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001653 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001654 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001655 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001656 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001657 return -ENOMEM;
1658 }
1659
1660 file->private_data = fh;
1661 fh->dev = dev;
1662 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1663 fh->mode = dev->mode[cur_channel];
1664 fh->fmt = dev->cur_fmt[cur_channel];
1665 /* default 4CIF NTSC */
1666 fh->width = LINE_SZ_4CIFS_NTSC;
1667 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1668 fh->channel = cur_channel;
Dean Anderson14d96262008-08-25 13:58:55 -03001669 /* configure channel to default state */
1670 if (!dev->chn_configured[cur_channel]) {
1671 s2255_set_mode(dev, cur_channel, &fh->mode);
1672 dev->chn_configured[cur_channel] = 1;
1673 }
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001674 dprintk(1, "s2255drv: open dev=%s type=%s users=%d\n",
1675 video_device_node_name(vdev), v4l2_type_names[type],
1676 dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001677 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1678 (unsigned long)fh, (unsigned long)dev,
1679 (unsigned long)&dev->vidq[cur_channel]);
1680 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1681 list_empty(&dev->vidq[cur_channel].active));
1682
1683 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1684 NULL, &dev->slock,
1685 fh->type,
1686 V4L2_FIELD_INTERLACED,
1687 sizeof(struct s2255_buffer), fh);
1688
Dean Anderson38f993a2008-06-26 23:15:51 -03001689 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001690 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001691 return 0;
1692}
1693
1694
1695static unsigned int s2255_poll(struct file *file,
1696 struct poll_table_struct *wait)
1697{
1698 struct s2255_fh *fh = file->private_data;
1699 int rc;
1700 dprintk(100, "%s\n", __func__);
1701
1702 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1703 return POLLERR;
1704
1705 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1706 return rc;
1707}
1708
1709static void s2255_destroy(struct kref *kref)
1710{
1711 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001712 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001713 if (!dev) {
1714 printk(KERN_ERR "s2255drv: kref problem\n");
1715 return;
1716 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001717 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1718 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001719 for (i = 0; i < MAX_CHANNELS; i++) {
1720 dev->setmode_ready[i] = 1;
1721 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03001722 dev->vidstatus_ready[i] = 1;
1723 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001724 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001725 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001726 /* reset the DSP so firmware can be reload next time */
1727 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001728 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001729 /* board shutdown stops the read pipe if it is running */
1730 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001731 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001732 del_timer(&dev->timer); /* only started in .probe and .open */
1733
Dean Anderson38f993a2008-06-26 23:15:51 -03001734 if (dev->fw_data->fw_urb) {
1735 dprintk(2, "kill fw_urb\n");
1736 usb_kill_urb(dev->fw_data->fw_urb);
1737 usb_free_urb(dev->fw_data->fw_urb);
1738 dev->fw_data->fw_urb = NULL;
1739 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001740 if (dev->fw_data->fw)
1741 release_firmware(dev->fw_data->fw);
1742 kfree(dev->fw_data->pfw_data);
1743 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001744 usb_put_dev(dev->udev);
1745 dprintk(1, "%s", __func__);
Dean Anderson14d96262008-08-25 13:58:55 -03001746
Dean Anderson14d96262008-08-25 13:58:55 -03001747 mutex_unlock(&dev->open_lock);
Dean Andersonb7732a32009-03-30 11:59:56 -03001748 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001749}
1750
Hans Verkuilbec43662008-12-30 06:58:20 -03001751static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001752{
1753 struct s2255_fh *fh = file->private_data;
1754 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001755 struct video_device *vdev = video_devdata(file);
1756
Dean Anderson38f993a2008-06-26 23:15:51 -03001757 if (!dev)
1758 return -ENODEV;
1759
1760 mutex_lock(&dev->open_lock);
1761
Dean Andersonf78d92c2008-07-22 14:43:27 -03001762 /* turn off stream */
1763 if (res_check(fh)) {
1764 if (dev->b_acquire[fh->channel])
1765 s2255_stop_acquire(dev, fh->channel);
1766 videobuf_streamoff(&fh->vb_vidq);
1767 res_free(dev, fh);
1768 }
1769
Dean Anderson38f993a2008-06-26 23:15:51 -03001770 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001771 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001772
Dean Anderson38f993a2008-06-26 23:15:51 -03001773 mutex_unlock(&dev->open_lock);
1774
1775 kref_put(&dev->kref, s2255_destroy);
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001776 dprintk(1, "s2255: close called (dev=%s, users=%d)\n",
1777 video_device_node_name(vdev), dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001778 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001779 return 0;
1780}
1781
1782static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1783{
1784 struct s2255_fh *fh = file->private_data;
1785 int ret;
1786
1787 if (!fh)
1788 return -ENODEV;
1789 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1790
1791 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1792
1793 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1794 (unsigned long)vma->vm_start,
1795 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1796
1797 return ret;
1798}
1799
Hans Verkuilbec43662008-12-30 06:58:20 -03001800static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001801 .owner = THIS_MODULE,
1802 .open = s2255_open,
1803 .release = s2255_close,
1804 .poll = s2255_poll,
1805 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001806 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001807};
1808
Hans Verkuila3998102008-07-21 02:57:38 -03001809static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001810 .vidioc_querycap = vidioc_querycap,
1811 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1812 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1813 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1814 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1815 .vidioc_reqbufs = vidioc_reqbufs,
1816 .vidioc_querybuf = vidioc_querybuf,
1817 .vidioc_qbuf = vidioc_qbuf,
1818 .vidioc_dqbuf = vidioc_dqbuf,
1819 .vidioc_s_std = vidioc_s_std,
1820 .vidioc_enum_input = vidioc_enum_input,
1821 .vidioc_g_input = vidioc_g_input,
1822 .vidioc_s_input = vidioc_s_input,
1823 .vidioc_queryctrl = vidioc_queryctrl,
1824 .vidioc_g_ctrl = vidioc_g_ctrl,
1825 .vidioc_s_ctrl = vidioc_s_ctrl,
1826 .vidioc_streamon = vidioc_streamon,
1827 .vidioc_streamoff = vidioc_streamoff,
1828#ifdef CONFIG_VIDEO_V4L1_COMPAT
1829 .vidiocgmbuf = vidioc_cgmbuf,
1830#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001831 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1832 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001833 .vidioc_s_parm = vidioc_s_parm,
1834 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001835};
1836
1837static struct video_device template = {
1838 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001839 .fops = &s2255_fops_v4l,
1840 .ioctl_ops = &s2255_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001841 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001842 .tvnorms = S2255_NORMS,
1843 .current_norm = V4L2_STD_NTSC_M,
1844};
1845
1846static int s2255_probe_v4l(struct s2255_dev *dev)
1847{
1848 int ret;
1849 int i;
1850 int cur_nr = video_nr;
1851
1852 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03001853 /* register 4 video devices */
1854 for (i = 0; i < MAX_CHANNELS; i++) {
1855 INIT_LIST_HEAD(&dev->vidq[i].active);
1856 dev->vidq[i].dev = dev;
1857 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001858 /* register 4 video devices */
1859 dev->vdev[i] = video_device_alloc();
1860 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001861 dev->vdev[i]->parent = &dev->interface->dev;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001862 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001863 if (video_nr == -1)
1864 ret = video_register_device(dev->vdev[i],
1865 VFL_TYPE_GRABBER,
1866 video_nr);
1867 else
1868 ret = video_register_device(dev->vdev[i],
1869 VFL_TYPE_GRABBER,
1870 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001871 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001872
1873 if (ret != 0) {
1874 dev_err(&dev->udev->dev,
1875 "failed to register video device!\n");
1876 return ret;
1877 }
1878 }
Dean Andersonabce21f2009-04-23 16:04:41 -03001879 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1880 S2255_MAJOR_VERSION,
1881 S2255_MINOR_VERSION);
Dean Anderson38f993a2008-06-26 23:15:51 -03001882 return ret;
1883}
1884
1885static void s2255_exit_v4l(struct s2255_dev *dev)
1886{
Dean Anderson14d96262008-08-25 13:58:55 -03001887
Dean Anderson38f993a2008-06-26 23:15:51 -03001888 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001889 for (i = 0; i < MAX_CHANNELS; i++) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001890 if (video_is_registered(dev->vdev[i])) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001891 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001892 printk(KERN_INFO "s2255 unregistered\n");
1893 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001894 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001895 printk(KERN_INFO "s2255 released\n");
1896 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001897 }
1898}
1899
1900/* this function moves the usb stream read pipe data
1901 * into the system buffers.
1902 * returns 0 on success, EAGAIN if more data to process( call this
1903 * function again).
1904 *
1905 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001906 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001907 * bytes 4-7: channel: 0-3
1908 * bytes 8-11: payload size: size of the frame
1909 * bytes 12-payloadsize+12: frame data
1910 */
1911static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1912{
Dean Anderson38f993a2008-06-26 23:15:51 -03001913 char *pdest;
1914 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001915 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001916 char *psrc;
1917 unsigned long copy_size;
1918 unsigned long size;
1919 s32 idx = -1;
1920 struct s2255_framei *frm;
1921 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001922
Dean Anderson38f993a2008-06-26 23:15:51 -03001923 dprintk(100, "buffer to user\n");
1924
1925 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001926 frm = &dev->buffer[dev->cc].frame[idx];
1927
Dean Anderson14d96262008-08-25 13:58:55 -03001928 if (frm->ulState == S2255_READ_IDLE) {
1929 int jj;
1930 unsigned int cc;
Dean Anderson3fa00602010-03-04 20:47:33 -03001931 __le32 *pdword; /*data from dsp is little endian */
Dean Anderson14d96262008-08-25 13:58:55 -03001932 int payload;
1933 /* search for marker codes */
1934 pdata = (unsigned char *)pipe_info->transfer_buffer;
Dean Anderson3fa00602010-03-04 20:47:33 -03001935 pdword = (__le32 *)pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001936 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
Dean Anderson3fa00602010-03-04 20:47:33 -03001937 switch (*pdword) {
Dean Anderson14d96262008-08-25 13:58:55 -03001938 case S2255_MARKER_FRAME:
Dean Anderson14d96262008-08-25 13:58:55 -03001939 dprintk(4, "found frame marker at offset:"
1940 " %d [%x %x]\n", jj, pdata[0],
1941 pdata[1]);
1942 offset = jj + PREFIX_SIZE;
1943 bframe = 1;
1944 cc = pdword[1];
1945 if (cc >= MAX_CHANNELS) {
1946 printk(KERN_ERR
1947 "bad channel\n");
1948 return -EINVAL;
1949 }
1950 /* reverse it */
1951 dev->cc = G_chnmap[cc];
1952 payload = pdword[3];
1953 if (payload > dev->req_image_size[dev->cc]) {
1954 dev->bad_payload[dev->cc]++;
1955 /* discard the bad frame */
1956 return -EINVAL;
1957 }
1958 dev->pkt_size[dev->cc] = payload;
1959 dev->jpg_size[dev->cc] = pdword[4];
1960 break;
1961 case S2255_MARKER_RESPONSE:
Dean Anderson14d96262008-08-25 13:58:55 -03001962 pdata += DEF_USB_BLOCK;
1963 jj += DEF_USB_BLOCK;
1964 if (pdword[1] >= MAX_CHANNELS)
1965 break;
1966 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03001967 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03001968 break;
1969 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03001970 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03001971 /* check if channel valid */
1972 /* set mode ready */
1973 dev->setmode_ready[cc] = 1;
1974 wake_up(&dev->wait_setmode[cc]);
1975 dprintk(5, "setmode ready %d\n", cc);
1976 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03001977 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03001978
1979 dev->chn_ready |= (1 << cc);
1980 if ((dev->chn_ready & 0x0f) != 0x0f)
1981 break;
1982 /* all channels ready */
1983 printk(KERN_INFO "s2255: fw loaded\n");
1984 atomic_set(&dev->fw_data->fw_state,
1985 S2255_FW_SUCCESS);
1986 wake_up(&dev->fw_data->wait_fw);
1987 break;
Dean Anderson4de39f52010-03-03 19:39:19 -03001988 case S2255_RESPONSE_STATUS:
1989 dev->vidstatus[cc] = pdword[3];
1990 dev->vidstatus_ready[cc] = 1;
1991 wake_up(&dev->wait_vidstatus[cc]);
1992 dprintk(5, "got vidstatus %x chan %d\n",
1993 pdword[3], cc);
1994 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001995 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001996 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001997 }
1998 default:
1999 pdata++;
2000 break;
2001 }
2002 if (bframe)
2003 break;
2004 } /* for */
2005 if (!bframe)
2006 return -EINVAL;
2007 }
2008
2009
2010 idx = dev->cur_frame[dev->cc];
2011 frm = &dev->buffer[dev->cc].frame[idx];
2012
2013 /* search done. now find out if should be acquiring on this channel */
2014 if (!dev->b_acquire[dev->cc]) {
2015 /* we found a frame, but this channel is turned off */
2016 frm->ulState = S2255_READ_IDLE;
2017 return -EINVAL;
2018 }
2019
2020 if (frm->ulState == S2255_READ_IDLE) {
2021 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002022 frm->cur_size = 0;
2023 }
2024
Dean Anderson14d96262008-08-25 13:58:55 -03002025 /* skip the marker 512 bytes (and offset if out of sync) */
2026 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2027
Dean Anderson38f993a2008-06-26 23:15:51 -03002028
2029 if (frm->lpvbits == NULL) {
2030 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2031 frm, dev, dev->cc, idx);
2032 return -ENOMEM;
2033 }
2034
2035 pdest = frm->lpvbits + frm->cur_size;
2036
Dean Anderson14d96262008-08-25 13:58:55 -03002037 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002038
Dean Anderson14d96262008-08-25 13:58:55 -03002039 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002040
Dean Anderson14d96262008-08-25 13:58:55 -03002041 /* sanity check on pdest */
2042 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2043 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002044
Dean Anderson38f993a2008-06-26 23:15:51 -03002045 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002046 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002047
Dean Anderson14d96262008-08-25 13:58:55 -03002048 if (frm->cur_size >= size) {
2049
Dean Anderson38f993a2008-06-26 23:15:51 -03002050 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002051 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2052 cc, idx);
2053 dev->last_frame[cc] = dev->cur_frame[cc];
2054 dev->cur_frame[cc]++;
2055 /* end of system frame ring buffer, start at zero */
2056 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2057 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2058 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002059 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002060 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002061 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002062 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002063 frm->ulState = S2255_READ_IDLE;
2064 frm->cur_size = 0;
2065
Dean Anderson38f993a2008-06-26 23:15:51 -03002066 }
2067 /* done successfully */
2068 return 0;
2069}
2070
2071static void s2255_read_video_callback(struct s2255_dev *dev,
2072 struct s2255_pipeinfo *pipe_info)
2073{
2074 int res;
2075 dprintk(50, "callback read video \n");
2076
2077 if (dev->cc >= MAX_CHANNELS) {
2078 dev->cc = 0;
2079 dev_err(&dev->udev->dev, "invalid channel\n");
2080 return;
2081 }
2082 /* otherwise copy to the system buffers */
2083 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002084 if (res != 0)
2085 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002086
2087 dprintk(50, "callback read video done\n");
2088 return;
2089}
2090
2091static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2092 u16 Index, u16 Value, void *TransferBuffer,
2093 s32 TransferBufferLength, int bOut)
2094{
2095 int r;
2096 if (!bOut) {
2097 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2098 Request,
2099 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2100 USB_DIR_IN,
2101 Value, Index, TransferBuffer,
2102 TransferBufferLength, HZ * 5);
2103 } else {
2104 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2105 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2106 Value, Index, TransferBuffer,
2107 TransferBufferLength, HZ * 5);
2108 }
2109 return r;
2110}
2111
2112/*
2113 * retrieve FX2 firmware version. future use.
2114 * @param dev pointer to device extension
2115 * @return -1 for fail, else returns firmware version as an int(16 bits)
2116 */
2117static int s2255_get_fx2fw(struct s2255_dev *dev)
2118{
2119 int fw;
2120 int ret;
2121 unsigned char transBuffer[64];
2122 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2123 S2255_VR_IN);
2124 if (ret < 0)
2125 dprintk(2, "get fw error: %x\n", ret);
2126 fw = transBuffer[0] + (transBuffer[1] << 8);
2127 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2128 return fw;
2129}
2130
2131/*
2132 * Create the system ring buffer to copy frames into from the
2133 * usb read pipe.
2134 */
2135static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2136{
2137 unsigned long i;
2138 unsigned long reqsize;
2139 dprintk(1, "create sys buffers\n");
2140 if (chn >= MAX_CHANNELS)
2141 return -1;
2142
2143 dev->buffer[chn].dwFrames = SYS_FRAMES;
2144
2145 /* always allocate maximum size(PAL) for system buffers */
2146 reqsize = SYS_FRAMES_MAXSIZE;
2147
2148 if (reqsize > SYS_FRAMES_MAXSIZE)
2149 reqsize = SYS_FRAMES_MAXSIZE;
2150
2151 for (i = 0; i < SYS_FRAMES; i++) {
2152 /* allocate the frames */
2153 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2154
2155 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2156 &dev->buffer[chn].frame[i], chn, i,
2157 dev->buffer[chn].frame[i].lpvbits);
2158 dev->buffer[chn].frame[i].size = reqsize;
2159 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2160 printk(KERN_INFO "out of memory. using less frames\n");
2161 dev->buffer[chn].dwFrames = i;
2162 break;
2163 }
2164 }
2165
2166 /* make sure internal states are set */
2167 for (i = 0; i < SYS_FRAMES; i++) {
2168 dev->buffer[chn].frame[i].ulState = 0;
2169 dev->buffer[chn].frame[i].cur_size = 0;
2170 }
2171
2172 dev->cur_frame[chn] = 0;
2173 dev->last_frame[chn] = -1;
2174 return 0;
2175}
2176
2177static int s2255_release_sys_buffers(struct s2255_dev *dev,
2178 unsigned long channel)
2179{
2180 unsigned long i;
2181 dprintk(1, "release sys buffers\n");
2182 for (i = 0; i < SYS_FRAMES; i++) {
2183 if (dev->buffer[channel].frame[i].lpvbits) {
2184 dprintk(1, "vfree %p\n",
2185 dev->buffer[channel].frame[i].lpvbits);
2186 vfree(dev->buffer[channel].frame[i].lpvbits);
2187 }
2188 dev->buffer[channel].frame[i].lpvbits = NULL;
2189 }
2190 return 0;
2191}
2192
2193static int s2255_board_init(struct s2255_dev *dev)
2194{
2195 int j;
2196 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2197 int fw_ver;
2198 dprintk(4, "board init: %p", dev);
2199
2200 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2201 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2202
2203 memset(pipe, 0, sizeof(*pipe));
2204 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002205 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2206 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002207
Dean Anderson38f993a2008-06-26 23:15:51 -03002208 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2209 GFP_KERNEL);
2210 if (pipe->transfer_buffer == NULL) {
2211 dprintk(1, "out of memory!\n");
2212 return -ENOMEM;
2213 }
2214
2215 }
2216
2217 /* query the firmware */
2218 fw_ver = s2255_get_fx2fw(dev);
2219
Dean Andersonabce21f2009-04-23 16:04:41 -03002220 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2221 (fw_ver >> 8) & 0xff,
2222 fw_ver & 0xff);
2223
2224 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002225 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002226 "usb firmware not up to date %d.%d\n",
2227 (fw_ver >> 8) & 0xff,
2228 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002229
2230 for (j = 0; j < MAX_CHANNELS; j++) {
2231 dev->b_acquire[j] = 0;
2232 dev->mode[j] = mode_def;
Dean Anderson22b88d42008-08-29 15:33:19 -03002233 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002234 dev->cur_fmt[j] = &formats[0];
2235 dev->mode[j].restart = 1;
2236 dev->req_image_size[j] = get_transfer_size(&mode_def);
2237 dev->frame_count[j] = 0;
2238 /* create the system buffers */
2239 s2255_create_sys_buffers(dev, j);
2240 }
2241 /* start read pipe */
2242 s2255_start_readpipe(dev);
2243
2244 dprintk(1, "S2255: board initialized\n");
2245 return 0;
2246}
2247
2248static int s2255_board_shutdown(struct s2255_dev *dev)
2249{
2250 u32 i;
2251
2252 dprintk(1, "S2255: board shutdown: %p", dev);
2253
2254 for (i = 0; i < MAX_CHANNELS; i++) {
2255 if (dev->b_acquire[i])
2256 s2255_stop_acquire(dev, i);
2257 }
2258
2259 s2255_stop_readpipe(dev);
2260
2261 for (i = 0; i < MAX_CHANNELS; i++)
2262 s2255_release_sys_buffers(dev, i);
2263
2264 /* release transfer buffers */
2265 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2266 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2267 kfree(pipe->transfer_buffer);
2268 }
2269 return 0;
2270}
2271
2272static void read_pipe_completion(struct urb *purb)
2273{
2274 struct s2255_pipeinfo *pipe_info;
2275 struct s2255_dev *dev;
2276 int status;
2277 int pipe;
2278
2279 pipe_info = purb->context;
2280 dprintk(100, "read pipe completion %p, status %d\n", purb,
2281 purb->status);
2282 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002283 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002284 return;
2285 }
2286
2287 dev = pipe_info->dev;
2288 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002289 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002290 return;
2291 }
2292 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002293 /* if shutting down, do not resubmit, exit immediately */
2294 if (status == -ESHUTDOWN) {
2295 dprintk(2, "read_pipe_completion: err shutdown\n");
2296 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002297 return;
2298 }
2299
2300 if (pipe_info->state == 0) {
2301 dprintk(2, "exiting USB pipe");
2302 return;
2303 }
2304
Dean Andersonb02064c2009-04-30 12:29:38 -03002305 if (status == 0)
2306 s2255_read_video_callback(dev, pipe_info);
2307 else {
2308 pipe_info->err_count++;
2309 dprintk(1, "s2255drv: failed URB %d\n", status);
2310 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002311
Dean Anderson38f993a2008-06-26 23:15:51 -03002312 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2313 /* reuse urb */
2314 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2315 pipe,
2316 pipe_info->transfer_buffer,
2317 pipe_info->cur_transfer_size,
2318 read_pipe_completion, pipe_info);
2319
2320 if (pipe_info->state != 0) {
2321 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2322 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002323 }
2324 } else {
2325 dprintk(2, "read pipe complete state 0\n");
2326 }
2327 return;
2328}
2329
2330static int s2255_start_readpipe(struct s2255_dev *dev)
2331{
2332 int pipe;
2333 int retval;
2334 int i;
2335 struct s2255_pipeinfo *pipe_info = dev->pipes;
2336 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2337 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2338
2339 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2340 pipe_info->state = 1;
Dean Andersonabce21f2009-04-23 16:04:41 -03002341 pipe_info->err_count = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002342 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2343 if (!pipe_info->stream_urb) {
2344 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002345 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002346 return -ENOMEM;
2347 }
2348 /* transfer buffer allocated in board_init */
2349 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2350 pipe,
2351 pipe_info->transfer_buffer,
2352 pipe_info->cur_transfer_size,
2353 read_pipe_completion, pipe_info);
2354
Dean Anderson38f993a2008-06-26 23:15:51 -03002355 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2356 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2357 if (retval) {
2358 printk(KERN_ERR "s2255: start read pipe failed\n");
2359 return retval;
2360 }
2361 }
2362
2363 return 0;
2364}
2365
2366/* starts acquisition process */
2367static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2368{
2369 unsigned char *buffer;
2370 int res;
2371 unsigned long chn_rev;
2372 int j;
2373 if (chn >= MAX_CHANNELS) {
2374 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2375 return -1;
2376 }
2377
2378 chn_rev = G_chnmap[chn];
2379 dprintk(1, "S2255: start acquire %lu \n", chn);
2380
2381 buffer = kzalloc(512, GFP_KERNEL);
2382 if (buffer == NULL) {
2383 dev_err(&dev->udev->dev, "out of mem\n");
2384 return -ENOMEM;
2385 }
2386
2387 dev->last_frame[chn] = -1;
2388 dev->bad_payload[chn] = 0;
2389 dev->cur_frame[chn] = 0;
2390 for (j = 0; j < SYS_FRAMES; j++) {
2391 dev->buffer[chn].frame[j].ulState = 0;
2392 dev->buffer[chn].frame[j].cur_size = 0;
2393 }
2394
2395 /* send the start command */
Dean Anderson3fa00602010-03-04 20:47:33 -03002396 *(__le32 *) buffer = IN_DATA_TOKEN;
2397 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2398 *((__le32 *) buffer + 2) = CMD_START;
Dean Anderson38f993a2008-06-26 23:15:51 -03002399 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2400 if (res != 0)
2401 dev_err(&dev->udev->dev, "CMD_START error\n");
2402
2403 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2404 kfree(buffer);
2405 return 0;
2406}
2407
2408static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2409{
2410 unsigned char *buffer;
2411 int res;
2412 unsigned long chn_rev;
Dean Anderson38f993a2008-06-26 23:15:51 -03002413 if (chn >= MAX_CHANNELS) {
2414 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2415 return -1;
2416 }
2417 chn_rev = G_chnmap[chn];
Dean Anderson38f993a2008-06-26 23:15:51 -03002418 buffer = kzalloc(512, GFP_KERNEL);
2419 if (buffer == NULL) {
2420 dev_err(&dev->udev->dev, "out of mem\n");
2421 return -ENOMEM;
2422 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002423 /* send the stop command */
2424 dprintk(4, "stop acquire %lu\n", chn);
Dean Anderson3fa00602010-03-04 20:47:33 -03002425 *(__le32 *) buffer = IN_DATA_TOKEN;
2426 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2427 *((__le32 *) buffer + 2) = CMD_STOP;
Dean Anderson38f993a2008-06-26 23:15:51 -03002428 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2429
2430 if (res != 0)
2431 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2432
2433 dprintk(4, "stop acquire: releasing states \n");
2434
2435 kfree(buffer);
2436 dev->b_acquire[chn] = 0;
2437
Dean Anderson14d96262008-08-25 13:58:55 -03002438 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002439}
2440
2441static void s2255_stop_readpipe(struct s2255_dev *dev)
2442{
2443 int j;
2444
2445 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002446 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002447 return;
2448 }
2449 dprintk(4, "stop read pipe\n");
2450 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2451 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2452 if (pipe_info) {
2453 if (pipe_info->state == 0)
2454 continue;
2455 pipe_info->state = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002456 }
2457 }
2458
2459 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2460 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2461 if (pipe_info->stream_urb) {
2462 /* cancel urb */
2463 usb_kill_urb(pipe_info->stream_urb);
2464 usb_free_urb(pipe_info->stream_urb);
2465 pipe_info->stream_urb = NULL;
2466 }
2467 }
2468 dprintk(2, "s2255 stop read pipe: %d\n", j);
2469 return;
2470}
2471
Dean Anderson14d96262008-08-25 13:58:55 -03002472static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002473{
Dean Anderson14d96262008-08-25 13:58:55 -03002474 if (reset)
2475 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002476 dev->fw_data->fw_size = dev->fw_data->fw->size;
2477 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2478 memcpy(dev->fw_data->pfw_data,
2479 dev->fw_data->fw->data, CHUNK_SIZE);
2480 dev->fw_data->fw_loaded = CHUNK_SIZE;
2481 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2482 usb_sndbulkpipe(dev->udev, 2),
2483 dev->fw_data->pfw_data,
2484 CHUNK_SIZE, s2255_fwchunk_complete,
2485 dev->fw_data);
2486 mod_timer(&dev->timer, jiffies + HZ);
2487}
2488
2489/* standard usb probe function */
2490static int s2255_probe(struct usb_interface *interface,
2491 const struct usb_device_id *id)
2492{
2493 struct s2255_dev *dev = NULL;
2494 struct usb_host_interface *iface_desc;
2495 struct usb_endpoint_descriptor *endpoint;
2496 int i;
2497 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002498 __le32 *pdata;
2499 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002500
2501 dprintk(2, "s2255: probe\n");
2502
2503 /* allocate memory for our device state and initialize it to zero */
2504 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2505 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002506 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002507 goto error;
2508 }
2509
2510 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2511 if (!dev->fw_data)
2512 goto error;
2513
2514 mutex_init(&dev->lock);
2515 mutex_init(&dev->open_lock);
2516
2517 /* grab usb_device and save it */
2518 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2519 if (dev->udev == NULL) {
2520 dev_err(&interface->dev, "null usb device\n");
2521 retval = -ENODEV;
2522 goto error;
2523 }
2524 kref_init(&dev->kref);
2525 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2526 dev->udev, interface);
2527 dev->interface = interface;
2528 /* set up the endpoint information */
2529 iface_desc = interface->cur_altsetting;
2530 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2531 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2532 endpoint = &iface_desc->endpoint[i].desc;
2533 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2534 /* we found the bulk in endpoint */
2535 dev->read_endpoint = endpoint->bEndpointAddress;
2536 }
2537 }
2538
2539 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002540 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002541 goto error;
2542 }
2543
2544 /* set intfdata */
2545 usb_set_intfdata(interface, dev);
2546
2547 dprintk(100, "after intfdata %p\n", dev);
2548
2549 init_timer(&dev->timer);
2550 dev->timer.function = s2255_timer;
2551 dev->timer.data = (unsigned long)dev->fw_data;
2552
2553 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson4de39f52010-03-03 19:39:19 -03002554 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03002555 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002556 init_waitqueue_head(&dev->wait_vidstatus[i]);
2557 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002558
2559 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2560
2561 if (!dev->fw_data->fw_urb) {
2562 dev_err(&interface->dev, "out of memory!\n");
2563 goto error;
2564 }
2565 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2566 if (!dev->fw_data->pfw_data) {
2567 dev_err(&interface->dev, "out of memory!\n");
2568 goto error;
2569 }
2570 /* load the first chunk */
2571 if (request_firmware(&dev->fw_data->fw,
2572 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2573 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2574 goto error;
2575 }
Dean Anderson14d96262008-08-25 13:58:55 -03002576 /* check the firmware is valid */
2577 fw_size = dev->fw_data->fw->size;
2578 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002579
Dean Anderson14d96262008-08-25 13:58:55 -03002580 if (*pdata != S2255_FW_MARKER) {
2581 printk(KERN_INFO "Firmware invalid.\n");
2582 retval = -ENODEV;
2583 goto error;
2584 } else {
2585 /* make sure firmware is the latest */
2586 __le32 *pRel;
2587 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2588 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
Dean Anderson4de39f52010-03-03 19:39:19 -03002589 dev->dsp_fw_ver = *pRel;
2590 if (*pRel < S2255_CUR_DSP_FWVER)
2591 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03002592 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002593 /* loads v4l specific */
2594 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002595 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002596 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002597 retval = s2255_board_init(dev);
2598 if (retval)
2599 goto error;
Dean Anderson38f993a2008-06-26 23:15:51 -03002600
2601 dprintk(4, "before probe done %p\n", dev);
2602 spin_lock_init(&dev->slock);
2603
Dean Anderson14d96262008-08-25 13:58:55 -03002604 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002605 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2606 return 0;
2607error:
2608 return retval;
2609}
2610
2611/* disconnect routine. when board is removed physically or with rmmod */
2612static void s2255_disconnect(struct usb_interface *interface)
2613{
2614 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002615 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002616 dprintk(1, "s2255: disconnect interface %p\n", interface);
2617 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002618
2619 /*
2620 * wake up any of the timers to allow open_lock to be
2621 * acquired sooner
2622 */
2623 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2624 wake_up(&dev->fw_data->wait_fw);
2625 for (i = 0; i < MAX_CHANNELS; i++) {
2626 dev->setmode_ready[i] = 1;
2627 wake_up(&dev->wait_setmode[i]);
Dean Anderson4de39f52010-03-03 19:39:19 -03002628 dev->vidstatus_ready[i] = 1;
2629 wake_up(&dev->wait_vidstatus[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03002630 }
2631
2632 mutex_lock(&dev->open_lock);
2633 usb_set_intfdata(interface, NULL);
2634 mutex_unlock(&dev->open_lock);
2635
Dean Anderson38f993a2008-06-26 23:15:51 -03002636 if (dev) {
2637 kref_put(&dev->kref, s2255_destroy);
2638 dprintk(1, "s2255drv: disconnect\n");
2639 dev_info(&interface->dev, "s2255usb now disconnected\n");
2640 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002641}
2642
2643static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002644 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002645 .probe = s2255_probe,
2646 .disconnect = s2255_disconnect,
2647 .id_table = s2255_table,
2648};
2649
2650static int __init usb_s2255_init(void)
2651{
2652 int result;
2653
2654 /* register this driver with the USB subsystem */
2655 result = usb_register(&s2255_driver);
2656
2657 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002658 pr_err(KBUILD_MODNAME
2659 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002660
2661 dprintk(2, "s2255_init: done\n");
2662 return result;
2663}
2664
2665static void __exit usb_s2255_exit(void)
2666{
2667 usb_deregister(&s2255_driver);
2668}
2669
2670module_init(usb_s2255_init);
2671module_exit(usb_s2255_exit);
2672
2673MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2674MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2675MODULE_LICENSE("GPL");