blob: 2c0bb06cab3b5155f81069f81c284247e467bab0 [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 *
4 * Copyright (C) 2007-2008 by Sensoray Company Inc.
5 * 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>
48#include <linux/videodev2.h>
49#include <linux/version.h>
Hans Verkuilfeb75f02008-07-27 06:30:21 -030050#include <linux/mm.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040051#include <linux/smp_lock.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030052#include <media/videobuf-vmalloc.h>
53#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030054#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030055#include <linux/vmalloc.h>
56#include <linux/usb.h>
57
58#define FIRMWARE_FILE_NAME "f2255usb.bin"
59
60
61
Dean Anderson22b88d42008-08-29 15:33:19 -030062/* default JPEG quality */
63#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030064/* vendor request in */
65#define S2255_VR_IN 0
66/* vendor request out */
67#define S2255_VR_OUT 1
68/* firmware query */
69#define S2255_VR_FW 0x30
70/* USB endpoint number for configuring the device */
71#define S2255_CONFIG_EP 2
72/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030073#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030074/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030075#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030076#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030077#define S2255_SETMODE_TIMEOUT 500
Dean Anderson38f993a2008-06-26 23:15:51 -030078#define MAX_CHANNELS 4
Dean Anderson14d96262008-08-25 13:58:55 -030079#define S2255_MARKER_FRAME 0x2255DA4AL
80#define S2255_MARKER_RESPONSE 0x2255ACACL
Dean Andersonabce21f2009-04-23 16:04:41 -030081#define S2255_RESPONSE_SETMODE 0x01
82#define S2255_RESPONSE_FW 0x10
Dean Anderson14d96262008-08-25 13:58:55 -030083#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030084#define MAX_CHANNELS 4
85#define MAX_PIPE_BUFFERS 1
86#define SYS_FRAMES 4
87/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030088#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
89#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030090#define LINE_SZ_4CIFS_NTSC 640
91#define LINE_SZ_2CIFS_NTSC 640
92#define LINE_SZ_1CIFS_NTSC 320
93#define LINE_SZ_4CIFS_PAL 704
94#define LINE_SZ_2CIFS_PAL 704
95#define LINE_SZ_1CIFS_PAL 352
96#define NUM_LINES_4CIFS_NTSC 240
97#define NUM_LINES_2CIFS_NTSC 240
98#define NUM_LINES_1CIFS_NTSC 240
99#define NUM_LINES_4CIFS_PAL 288
100#define NUM_LINES_2CIFS_PAL 288
101#define NUM_LINES_1CIFS_PAL 288
102#define LINE_SZ_DEF 640
103#define NUM_LINES_DEF 240
104
105
106/* predefined settings */
107#define FORMAT_NTSC 1
108#define FORMAT_PAL 2
109
110#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
111#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
112#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
Dean Anderson7d853532009-05-15 14:32:04 -0300113/* SCALE_4CIFSI is the 2 fields interpolated into one */
114#define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
Dean Anderson38f993a2008-06-26 23:15:51 -0300115
116#define COLOR_YUVPL 1 /* YUV planar */
117#define COLOR_YUVPK 2 /* YUV packed */
118#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300119#define COLOR_JPG 5 /* JPEG */
120#define MASK_COLOR 0xff
121#define MASK_JPG_QUALITY 0xff00
Dean Anderson38f993a2008-06-26 23:15:51 -0300122
123/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
124#define FDEC_1 1 /* capture every frame. default */
125#define FDEC_2 2 /* capture every 2nd frame */
126#define FDEC_3 3 /* capture every 3rd frame */
127#define FDEC_5 5 /* capture every 5th frame */
128
129/*-------------------------------------------------------
130 * Default mode parameters.
131 *-------------------------------------------------------*/
132#define DEF_SCALE SCALE_4CIFS
133#define DEF_COLOR COLOR_YUVPL
134#define DEF_FDEC FDEC_1
135#define DEF_BRIGHT 0
136#define DEF_CONTRAST 0x5c
137#define DEF_SATURATION 0x80
138#define DEF_HUE 0
139
140/* usb config commands */
141#define IN_DATA_TOKEN 0x2255c0de
142#define CMD_2255 0xc2255000
143#define CMD_SET_MODE (CMD_2255 | 0x10)
144#define CMD_START (CMD_2255 | 0x20)
145#define CMD_STOP (CMD_2255 | 0x30)
146#define CMD_STATUS (CMD_2255 | 0x40)
147
148struct s2255_mode {
149 u32 format; /* input video format (NTSC, PAL) */
150 u32 scale; /* output video scale */
151 u32 color; /* output video color format */
152 u32 fdec; /* frame decimation */
153 u32 bright; /* brightness */
154 u32 contrast; /* contrast */
155 u32 saturation; /* saturation */
156 u32 hue; /* hue (NTSC only)*/
157 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
158 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
159 u32 restart; /* if DSP requires restart */
160};
161
Dean Anderson14d96262008-08-25 13:58:55 -0300162
163#define S2255_READ_IDLE 0
164#define S2255_READ_FRAME 1
165
Dean Anderson38f993a2008-06-26 23:15:51 -0300166/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300167struct s2255_framei {
168 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300169 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300170 void *lpvbits; /* image data */
171 unsigned long cur_size; /* current data copied to it */
172};
173
174/* image buffer structure */
175struct s2255_bufferi {
176 unsigned long dwFrames; /* number of frames in buffer */
177 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
178};
179
180#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
181 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300182 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300183
184struct s2255_dmaqueue {
185 struct list_head active;
Dean Anderson38f993a2008-06-26 23:15:51 -0300186 struct s2255_dev *dev;
187 int channel;
188};
189
190/* for firmware loading, fw_state */
191#define S2255_FW_NOTLOADED 0
192#define S2255_FW_LOADED_DSPWAIT 1
193#define S2255_FW_SUCCESS 2
194#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300195#define S2255_FW_DISCONNECTING 4
Dean Anderson38f993a2008-06-26 23:15:51 -0300196
Harvey Harrisondeaf53e2008-11-15 01:10:14 -0300197#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
Dean Anderson14d96262008-08-25 13:58:55 -0300198/* 2255 read states */
199#define S2255_READ_IDLE 0
200#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300201struct s2255_fw {
202 int fw_loaded;
203 int fw_size;
204 struct urb *fw_urb;
205 atomic_t fw_state;
206 void *pfw_data;
207 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300208 const struct firmware *fw;
209};
210
211struct s2255_pipeinfo {
212 u32 max_transfer_size;
213 u32 cur_transfer_size;
214 u8 *transfer_buffer;
Dean Anderson38f993a2008-06-26 23:15:51 -0300215 u32 state;
Dean Anderson38f993a2008-06-26 23:15:51 -0300216 void *stream_urb;
217 void *dev; /* back pointer to s2255_dev struct*/
218 u32 err_count;
Dean Anderson38f993a2008-06-26 23:15:51 -0300219 u32 idx;
Dean Anderson38f993a2008-06-26 23:15:51 -0300220};
221
222struct s2255_fmt; /*forward declaration */
223
224struct s2255_dev {
225 int frames;
226 int users[MAX_CHANNELS];
227 struct mutex lock;
228 struct mutex open_lock;
229 int resources[MAX_CHANNELS];
230 struct usb_device *udev;
231 struct usb_interface *interface;
232 u8 read_endpoint;
233
234 struct s2255_dmaqueue vidq[MAX_CHANNELS];
235 struct video_device *vdev[MAX_CHANNELS];
236 struct list_head s2255_devlist;
237 struct timer_list timer;
238 struct s2255_fw *fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -0300239 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
240 struct s2255_bufferi buffer[MAX_CHANNELS];
241 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300242 /* jpeg compression */
243 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300244 /* capture parameters (for high quality mode full size) */
245 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300246 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
247 int cur_frame[MAX_CHANNELS];
248 int last_frame[MAX_CHANNELS];
249 u32 cc; /* current channel */
250 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300251 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300252 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300253 /* received packet size */
254 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300255 int bad_payload[MAX_CHANNELS];
256 unsigned long frame_count[MAX_CHANNELS];
257 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300258 /* if JPEG image */
259 int jpg_size[MAX_CHANNELS];
260 /* if channel configured to default state */
261 int chn_configured[MAX_CHANNELS];
262 wait_queue_head_t wait_setmode[MAX_CHANNELS];
263 int setmode_ready[MAX_CHANNELS];
264 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300265 struct kref kref;
266 spinlock_t slock;
267};
268#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
269
270struct s2255_fmt {
271 char *name;
272 u32 fourcc;
273 int depth;
274};
275
276/* buffer for one video frame */
277struct s2255_buffer {
278 /* common v4l buffer stuff -- must be first */
279 struct videobuf_buffer vb;
280 const struct s2255_fmt *fmt;
281};
282
283struct s2255_fh {
284 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300285 const struct s2255_fmt *fmt;
286 unsigned int width;
287 unsigned int height;
288 struct videobuf_queue vb_vidq;
289 enum v4l2_buf_type type;
290 int channel;
291 /* mode below is the desired mode.
292 mode in s2255_dev is the current mode that was last set */
293 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300294 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300295};
296
Dean Andersonabce21f2009-04-23 16:04:41 -0300297/* current cypress EEPROM firmware version */
298#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson38f993a2008-06-26 23:15:51 -0300299#define S2255_MAJOR_VERSION 1
Dean Andersonabce21f2009-04-23 16:04:41 -0300300#define S2255_MINOR_VERSION 14
Dean Anderson38f993a2008-06-26 23:15:51 -0300301#define S2255_RELEASE 0
302#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
303 S2255_MINOR_VERSION, \
304 S2255_RELEASE)
305
306/* vendor ids */
307#define USB_S2255_VENDOR_ID 0x1943
308#define USB_S2255_PRODUCT_ID 0x2255
309#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
310/* frame prefix size (sent once every frame) */
311#define PREFIX_SIZE 512
312
313/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300314static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300315
316static LIST_HEAD(s2255_devlist);
317
318static int debug;
319static int *s2255_debug = &debug;
320
321static int s2255_start_readpipe(struct s2255_dev *dev);
322static void s2255_stop_readpipe(struct s2255_dev *dev);
323static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
324static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
325static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300326 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300327static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
328 struct s2255_mode *mode);
329static int s2255_board_shutdown(struct s2255_dev *dev);
330static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300331static void s2255_fwload_start(struct s2255_dev *dev, int reset);
332static void s2255_destroy(struct kref *kref);
333static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
334 u16 index, u16 value, void *buf,
335 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300336
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300337/* dev_err macro with driver name */
338#define S2255_DRIVER_NAME "s2255"
339#define s2255_dev_err(dev, fmt, arg...) \
340 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
341
Dean Anderson38f993a2008-06-26 23:15:51 -0300342#define dprintk(level, fmt, arg...) \
343 do { \
344 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300345 printk(KERN_DEBUG S2255_DRIVER_NAME \
346 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300347 } \
348 } while (0)
349
Dean Anderson38f993a2008-06-26 23:15:51 -0300350static struct usb_driver s2255_driver;
351
352
353/* Declare static vars that will be used as parameters */
354static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
355
356/* start video number */
357static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
358
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300359module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300360MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300361module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300362MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300363module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300364MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
365
366/* USB device table */
367static struct usb_device_id s2255_table[] = {
368 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
369 { } /* Terminating entry */
370};
371MODULE_DEVICE_TABLE(usb, s2255_table);
372
373
374#define BUFFER_TIMEOUT msecs_to_jiffies(400)
375
376/* supported controls */
377static struct v4l2_queryctrl s2255_qctrl[] = {
378 {
379 .id = V4L2_CID_BRIGHTNESS,
380 .type = V4L2_CTRL_TYPE_INTEGER,
381 .name = "Brightness",
382 .minimum = -127,
383 .maximum = 128,
384 .step = 1,
385 .default_value = 0,
386 .flags = 0,
387 }, {
388 .id = V4L2_CID_CONTRAST,
389 .type = V4L2_CTRL_TYPE_INTEGER,
390 .name = "Contrast",
391 .minimum = 0,
392 .maximum = 255,
393 .step = 0x1,
394 .default_value = DEF_CONTRAST,
395 .flags = 0,
396 }, {
397 .id = V4L2_CID_SATURATION,
398 .type = V4L2_CTRL_TYPE_INTEGER,
399 .name = "Saturation",
400 .minimum = 0,
401 .maximum = 255,
402 .step = 0x1,
403 .default_value = DEF_SATURATION,
404 .flags = 0,
405 }, {
406 .id = V4L2_CID_HUE,
407 .type = V4L2_CTRL_TYPE_INTEGER,
408 .name = "Hue",
409 .minimum = 0,
410 .maximum = 255,
411 .step = 0x1,
412 .default_value = DEF_HUE,
413 .flags = 0,
414 }
415};
416
417static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
418
419/* image formats. */
420static const struct s2255_fmt formats[] = {
421 {
422 .name = "4:2:2, planar, YUV422P",
423 .fourcc = V4L2_PIX_FMT_YUV422P,
424 .depth = 16
425
426 }, {
427 .name = "4:2:2, packed, YUYV",
428 .fourcc = V4L2_PIX_FMT_YUYV,
429 .depth = 16
430
431 }, {
432 .name = "4:2:2, packed, UYVY",
433 .fourcc = V4L2_PIX_FMT_UYVY,
434 .depth = 16
435 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300436 .name = "JPG",
437 .fourcc = V4L2_PIX_FMT_JPEG,
438 .depth = 24
439 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300440 .name = "8bpp GREY",
441 .fourcc = V4L2_PIX_FMT_GREY,
442 .depth = 8
443 }
444};
445
446static int norm_maxw(struct video_device *vdev)
447{
448 return (vdev->current_norm & V4L2_STD_NTSC) ?
449 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
450}
451
452static int norm_maxh(struct video_device *vdev)
453{
454 return (vdev->current_norm & V4L2_STD_NTSC) ?
455 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
456}
457
458static int norm_minw(struct video_device *vdev)
459{
460 return (vdev->current_norm & V4L2_STD_NTSC) ?
461 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
462}
463
464static int norm_minh(struct video_device *vdev)
465{
466 return (vdev->current_norm & V4L2_STD_NTSC) ?
467 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
468}
469
470
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300471/*
472 * TODO: fixme: move YUV reordering to hardware
473 * converts 2255 planar format to yuyv or uyvy
474 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300475static void planar422p_to_yuv_packed(const unsigned char *in,
476 unsigned char *out,
477 int width, int height,
478 int fmt)
479{
480 unsigned char *pY;
481 unsigned char *pCb;
482 unsigned char *pCr;
483 unsigned long size = height * width;
484 unsigned int i;
485 pY = (unsigned char *)in;
486 pCr = (unsigned char *)in + height * width;
487 pCb = (unsigned char *)in + height * width + (height * width / 2);
488 for (i = 0; i < size * 2; i += 4) {
489 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
490 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
491 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
492 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
493 }
494 return;
495}
496
Hans Verkuild45b9b82008-09-04 03:33:43 -0300497static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300498{
499 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
500 msleep(10);
501 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
502 return;
503}
Dean Anderson38f993a2008-06-26 23:15:51 -0300504
505/* kickstarts the firmware loading. from probe
506 */
507static void s2255_timer(unsigned long user_data)
508{
509 struct s2255_fw *data = (struct s2255_fw *)user_data;
510 dprintk(100, "s2255 timer\n");
511 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
512 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300513 atomic_set(&data->fw_state, S2255_FW_FAILED);
514 /* wake up anything waiting for the firmware */
515 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300516 return;
517 }
518}
519
Dean Anderson38f993a2008-06-26 23:15:51 -0300520
521/* this loads the firmware asynchronously.
522 Originally this was done synchroously in probe.
523 But it is better to load it asynchronously here than block
524 inside the probe function. Blocking inside probe affects boot time.
525 FW loading is triggered by the timer in the probe function
526*/
527static void s2255_fwchunk_complete(struct urb *urb)
528{
529 struct s2255_fw *data = urb->context;
530 struct usb_device *udev = urb->dev;
531 int len;
532 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300533 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300534 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300535 atomic_set(&data->fw_state, S2255_FW_FAILED);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300538 return;
539 }
540 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300541 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300542 atomic_set(&data->fw_state, S2255_FW_FAILED);
543 /* wake up anything waiting for the firmware */
544 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300545 return;
546 }
547#define CHUNK_SIZE 512
548 /* all USB transfers must be done with continuous kernel memory.
549 can't allocate more than 128k in current linux kernel, so
550 upload the firmware in chunks
551 */
552 if (data->fw_loaded < data->fw_size) {
553 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
554 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
555
556 if (len < CHUNK_SIZE)
557 memset(data->pfw_data, 0, CHUNK_SIZE);
558
559 dprintk(100, "completed len %d, loaded %d \n", len,
560 data->fw_loaded);
561
562 memcpy(data->pfw_data,
563 (char *) data->fw->data + data->fw_loaded, len);
564
565 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
566 data->pfw_data, CHUNK_SIZE,
567 s2255_fwchunk_complete, data);
568 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
569 dev_err(&udev->dev, "failed submit URB\n");
570 atomic_set(&data->fw_state, S2255_FW_FAILED);
571 /* wake up anything waiting for the firmware */
572 wake_up(&data->wait_fw);
573 return;
574 }
575 data->fw_loaded += len;
576 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300577 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300578 }
579 dprintk(100, "2255 complete done\n");
580 return;
581
582}
583
Dean Anderson14d96262008-08-25 13:58:55 -0300584static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300585{
586 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
587 struct s2255_buffer *buf;
588 unsigned long flags = 0;
589 int rc = 0;
590 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
591 spin_lock_irqsave(&dev->slock, flags);
592
593 if (list_empty(&dma_q->active)) {
594 dprintk(1, "No active queue to serve\n");
595 rc = -1;
596 goto unlock;
597 }
598 buf = list_entry(dma_q->active.next,
599 struct s2255_buffer, vb.queue);
600
Dean Anderson38f993a2008-06-26 23:15:51 -0300601 list_del(&buf->vb.queue);
602 do_gettimeofday(&buf->vb.ts);
603 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300604 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300605 wake_up(&buf->vb.done);
606 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
607unlock:
608 spin_unlock_irqrestore(&dev->slock, flags);
609 return 0;
610}
611
612
613static const struct s2255_fmt *format_by_fourcc(int fourcc)
614{
615 unsigned int i;
616
617 for (i = 0; i < ARRAY_SIZE(formats); i++) {
618 if (-1 == formats[i].fourcc)
619 continue;
620 if (formats[i].fourcc == fourcc)
621 return formats + i;
622 }
623 return NULL;
624}
625
626
627
628
629/* video buffer vmalloc implementation based partly on VIVI driver which is
630 * Copyright (c) 2006 by
631 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
632 * Ted Walther <ted--a.t--enumera.com>
633 * John Sokol <sokol--a.t--videotechnology.com>
634 * http://v4l.videotechnology.com/
635 *
636 */
637static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300638 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300639{
640 int pos = 0;
641 struct timeval ts;
642 const char *tmpbuf;
643 char *vbuf = videobuf_to_vmalloc(&buf->vb);
644 unsigned long last_frame;
645 struct s2255_framei *frm;
646
647 if (!vbuf)
648 return;
649
650 last_frame = dev->last_frame[chn];
651 if (last_frame != -1) {
652 frm = &dev->buffer[chn].frame[last_frame];
653 tmpbuf =
654 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
655 switch (buf->fmt->fourcc) {
656 case V4L2_PIX_FMT_YUYV:
657 case V4L2_PIX_FMT_UYVY:
658 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
659 vbuf, buf->vb.width,
660 buf->vb.height,
661 buf->fmt->fourcc);
662 break;
663 case V4L2_PIX_FMT_GREY:
664 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
665 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300666 case V4L2_PIX_FMT_JPEG:
667 buf->vb.size = jpgsize;
668 memcpy(vbuf, tmpbuf, buf->vb.size);
669 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300670 case V4L2_PIX_FMT_YUV422P:
671 memcpy(vbuf, tmpbuf,
672 buf->vb.width * buf->vb.height * 2);
673 break;
674 default:
675 printk(KERN_DEBUG "s2255: unknown format?\n");
676 }
677 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300678 } else {
679 printk(KERN_ERR "s2255: =======no frame\n");
680 return;
681
682 }
683 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
684 (unsigned long)vbuf, pos);
685 /* tell v4l buffer was filled */
686
Dean Andersona1c45302008-09-09 12:29:56 -0300687 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300688 do_gettimeofday(&ts);
689 buf->vb.ts = ts;
690 buf->vb.state = VIDEOBUF_DONE;
691}
692
693
694/* ------------------------------------------------------------------
695 Videobuf operations
696 ------------------------------------------------------------------*/
697
698static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
699 unsigned int *size)
700{
701 struct s2255_fh *fh = vq->priv_data;
702
703 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
704
705 if (0 == *count)
706 *count = S2255_DEF_BUFS;
707
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300708 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300709 (*count)--;
710
711 return 0;
712}
713
714static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
715{
716 dprintk(4, "%s\n", __func__);
717
Dean Anderson38f993a2008-06-26 23:15:51 -0300718 videobuf_vmalloc_free(&buf->vb);
719 buf->vb.state = VIDEOBUF_NEEDS_INIT;
720}
721
722static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
723 enum v4l2_field field)
724{
725 struct s2255_fh *fh = vq->priv_data;
726 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
727 int rc;
728 dprintk(4, "%s, field=%d\n", __func__, field);
729 if (fh->fmt == NULL)
730 return -EINVAL;
731
732 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
733 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
734 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
735 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
736 dprintk(4, "invalid buffer prepare\n");
737 return -EINVAL;
738 }
739
740 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
741
742 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
743 dprintk(4, "invalid buffer prepare\n");
744 return -EINVAL;
745 }
746
747 buf->fmt = fh->fmt;
748 buf->vb.width = fh->width;
749 buf->vb.height = fh->height;
750 buf->vb.field = field;
751
752
753 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
754 rc = videobuf_iolock(vq, &buf->vb, NULL);
755 if (rc < 0)
756 goto fail;
757 }
758
759 buf->vb.state = VIDEOBUF_PREPARED;
760 return 0;
761fail:
762 free_buffer(vq, buf);
763 return rc;
764}
765
766static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
767{
768 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
769 struct s2255_fh *fh = vq->priv_data;
770 struct s2255_dev *dev = fh->dev;
771 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
772
773 dprintk(1, "%s\n", __func__);
774
775 buf->vb.state = VIDEOBUF_QUEUED;
776 list_add_tail(&buf->vb.queue, &vidq->active);
777}
778
779static void buffer_release(struct videobuf_queue *vq,
780 struct videobuf_buffer *vb)
781{
782 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
783 struct s2255_fh *fh = vq->priv_data;
784 dprintk(4, "%s %d\n", __func__, fh->channel);
785 free_buffer(vq, buf);
786}
787
788static struct videobuf_queue_ops s2255_video_qops = {
789 .buf_setup = buffer_setup,
790 .buf_prepare = buffer_prepare,
791 .buf_queue = buffer_queue,
792 .buf_release = buffer_release,
793};
794
795
796static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
797{
798 /* is it free? */
799 mutex_lock(&dev->lock);
800 if (dev->resources[fh->channel]) {
801 /* no, someone else uses it */
802 mutex_unlock(&dev->lock);
803 return 0;
804 }
805 /* it's free, grab it */
806 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300807 fh->resources[fh->channel] = 1;
808 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300809 mutex_unlock(&dev->lock);
810 return 1;
811}
812
813static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
814{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300815 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300816}
817
Dean Andersonf78d92c2008-07-22 14:43:27 -0300818static int res_check(struct s2255_fh *fh)
819{
820 return fh->resources[fh->channel];
821}
822
823
Dean Anderson38f993a2008-06-26 23:15:51 -0300824static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
825{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300826 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300827 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300828 fh->resources[fh->channel] = 0;
829 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300830 dprintk(1, "res: put\n");
831}
832
833
834static int vidioc_querycap(struct file *file, void *priv,
835 struct v4l2_capability *cap)
836{
837 struct s2255_fh *fh = file->private_data;
838 struct s2255_dev *dev = fh->dev;
839 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
840 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300841 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300842 cap->version = S2255_VERSION;
843 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
844 return 0;
845}
846
847static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
848 struct v4l2_fmtdesc *f)
849{
850 int index = 0;
851 if (f)
852 index = f->index;
853
854 if (index >= ARRAY_SIZE(formats))
855 return -EINVAL;
856
857 dprintk(4, "name %s\n", formats[index].name);
858 strlcpy(f->description, formats[index].name, sizeof(f->description));
859 f->pixelformat = formats[index].fourcc;
860 return 0;
861}
862
863static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
864 struct v4l2_format *f)
865{
866 struct s2255_fh *fh = priv;
867
868 f->fmt.pix.width = fh->width;
869 f->fmt.pix.height = fh->height;
870 f->fmt.pix.field = fh->vb_vidq.field;
871 f->fmt.pix.pixelformat = fh->fmt->fourcc;
872 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
873 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300874 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300875}
876
877static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_format *f)
879{
880 const struct s2255_fmt *fmt;
881 enum v4l2_field field;
882 int b_any_field = 0;
883 struct s2255_fh *fh = priv;
884 struct s2255_dev *dev = fh->dev;
885 int is_ntsc;
886
887 is_ntsc =
888 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
889
890 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
891
892 if (fmt == NULL)
893 return -EINVAL;
894
895 field = f->fmt.pix.field;
896 if (field == V4L2_FIELD_ANY)
897 b_any_field = 1;
898
899 dprintk(4, "try format %d \n", is_ntsc);
900 /* supports 3 sizes. see s2255drv.h */
901 dprintk(50, "width test %d, height %d\n",
902 f->fmt.pix.width, f->fmt.pix.height);
903 if (is_ntsc) {
904 /* NTSC */
905 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
906 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
907 if (b_any_field) {
908 field = V4L2_FIELD_SEQ_TB;
909 } else if (!((field == V4L2_FIELD_INTERLACED) ||
910 (field == V4L2_FIELD_SEQ_TB) ||
911 (field == V4L2_FIELD_INTERLACED_TB))) {
912 dprintk(1, "unsupported field setting\n");
913 return -EINVAL;
914 }
915 } else {
916 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
917 if (b_any_field) {
918 field = V4L2_FIELD_TOP;
919 } else if (!((field == V4L2_FIELD_TOP) ||
920 (field == V4L2_FIELD_BOTTOM))) {
921 dprintk(1, "unsupported field setting\n");
922 return -EINVAL;
923 }
924
925 }
926 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
930 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
932 else
933 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
934 } else {
935 /* PAL */
936 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
937 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
938 if (b_any_field) {
939 field = V4L2_FIELD_SEQ_TB;
940 } else if (!((field == V4L2_FIELD_INTERLACED) ||
941 (field == V4L2_FIELD_SEQ_TB) ||
942 (field == V4L2_FIELD_INTERLACED_TB))) {
943 dprintk(1, "unsupported field setting\n");
944 return -EINVAL;
945 }
946 } else {
947 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
948 if (b_any_field) {
949 field = V4L2_FIELD_TOP;
950 } else if (!((field == V4L2_FIELD_TOP) ||
951 (field == V4L2_FIELD_BOTTOM))) {
952 dprintk(1, "unsupported field setting\n");
953 return -EINVAL;
954 }
955 }
956 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
957 dprintk(50, "pal 704\n");
958 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
959 field = V4L2_FIELD_SEQ_TB;
960 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
961 dprintk(50, "pal 352A\n");
962 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
963 field = V4L2_FIELD_TOP;
964 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
965 dprintk(50, "pal 352B\n");
966 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
967 field = V4L2_FIELD_TOP;
968 } else {
969 dprintk(50, "pal 352C\n");
970 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
971 field = V4L2_FIELD_TOP;
972 }
973 }
974
975 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
976 f->fmt.pix.height, f->fmt.pix.field);
977 f->fmt.pix.field = field;
978 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
979 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
980 return 0;
981}
982
983static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
984 struct v4l2_format *f)
985{
986 struct s2255_fh *fh = priv;
987 const struct s2255_fmt *fmt;
988 struct videobuf_queue *q = &fh->vb_vidq;
989 int ret;
990 int norm;
991
992 ret = vidioc_try_fmt_vid_cap(file, fh, f);
993
994 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300995 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300996
997 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
998
999 if (fmt == NULL)
1000 return -EINVAL;
1001
1002 mutex_lock(&q->vb_lock);
1003
1004 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1005 dprintk(1, "queue busy\n");
1006 ret = -EBUSY;
1007 goto out_s_fmt;
1008 }
1009
1010 if (res_locked(fh->dev, fh)) {
1011 dprintk(1, "can't change format after started\n");
1012 ret = -EBUSY;
1013 goto out_s_fmt;
1014 }
1015
1016 fh->fmt = fmt;
1017 fh->width = f->fmt.pix.width;
1018 fh->height = f->fmt.pix.height;
1019 fh->vb_vidq.field = f->fmt.pix.field;
1020 fh->type = f->type;
1021 norm = norm_minw(fh->dev->vdev[fh->channel]);
1022 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001023 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1024 if (fh->dev->cap_parm[fh->channel].capturemode &
1025 V4L2_MODE_HIGHQUALITY) {
1026 fh->mode.scale = SCALE_4CIFSI;
1027 dprintk(2, "scale 4CIFSI\n");
1028 } else {
1029 fh->mode.scale = SCALE_4CIFS;
1030 dprintk(2, "scale 4CIFS\n");
1031 }
1032 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001033 fh->mode.scale = SCALE_2CIFS;
1034
1035 } else {
1036 fh->mode.scale = SCALE_1CIFS;
1037 }
1038
1039 /* color mode */
1040 switch (fh->fmt->fourcc) {
1041 case V4L2_PIX_FMT_GREY:
1042 fh->mode.color = COLOR_Y8;
1043 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001044 case V4L2_PIX_FMT_JPEG:
Dean Anderson22b88d42008-08-29 15:33:19 -03001045 fh->mode.color = COLOR_JPG |
1046 (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001047 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001048 case V4L2_PIX_FMT_YUV422P:
1049 fh->mode.color = COLOR_YUVPL;
1050 break;
1051 case V4L2_PIX_FMT_YUYV:
1052 case V4L2_PIX_FMT_UYVY:
1053 default:
1054 fh->mode.color = COLOR_YUVPK;
1055 break;
1056 }
1057 ret = 0;
1058out_s_fmt:
1059 mutex_unlock(&q->vb_lock);
1060 return ret;
1061}
1062
1063static int vidioc_reqbufs(struct file *file, void *priv,
1064 struct v4l2_requestbuffers *p)
1065{
1066 int rc;
1067 struct s2255_fh *fh = priv;
1068 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1069 return rc;
1070}
1071
1072static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1073{
1074 int rc;
1075 struct s2255_fh *fh = priv;
1076 rc = videobuf_querybuf(&fh->vb_vidq, p);
1077 return rc;
1078}
1079
1080static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1081{
1082 int rc;
1083 struct s2255_fh *fh = priv;
1084 rc = videobuf_qbuf(&fh->vb_vidq, p);
1085 return rc;
1086}
1087
1088static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1089{
1090 int rc;
1091 struct s2255_fh *fh = priv;
1092 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1093 return rc;
1094}
1095
1096#ifdef CONFIG_VIDEO_V4L1_COMPAT
1097static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1098{
1099 struct s2255_fh *fh = priv;
1100
1101 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1102}
1103#endif
1104
1105/* write to the configuration pipe, synchronously */
1106static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1107 int size)
1108{
1109 int pipe;
1110 int done;
1111 long retval = -1;
1112 if (udev) {
1113 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1114 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1115 }
1116 return retval;
1117}
1118
1119static u32 get_transfer_size(struct s2255_mode *mode)
1120{
1121 int linesPerFrame = LINE_SZ_DEF;
1122 int pixelsPerLine = NUM_LINES_DEF;
1123 u32 outImageSize;
1124 u32 usbInSize;
1125 unsigned int mask_mult;
1126
1127 if (mode == NULL)
1128 return 0;
1129
1130 if (mode->format == FORMAT_NTSC) {
1131 switch (mode->scale) {
1132 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001133 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001134 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1135 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1136 break;
1137 case SCALE_2CIFS:
1138 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1139 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1140 break;
1141 case SCALE_1CIFS:
1142 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1143 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1144 break;
1145 default:
1146 break;
1147 }
1148 } else if (mode->format == FORMAT_PAL) {
1149 switch (mode->scale) {
1150 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001151 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001152 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1153 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1154 break;
1155 case SCALE_2CIFS:
1156 linesPerFrame = NUM_LINES_2CIFS_PAL;
1157 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1158 break;
1159 case SCALE_1CIFS:
1160 linesPerFrame = NUM_LINES_1CIFS_PAL;
1161 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1162 break;
1163 default:
1164 break;
1165 }
1166 }
1167 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001168 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001169 /* 2 bytes/pixel if not monochrome */
1170 outImageSize *= 2;
1171 }
1172
1173 /* total bytes to send including prefix and 4K padding;
1174 must be a multiple of USB_READ_SIZE */
1175 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1176 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1177 /* if size not a multiple of USB_READ_SIZE */
1178 if (usbInSize & ~mask_mult)
1179 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1180 return usbInSize;
1181}
1182
1183static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1184{
1185 struct device *dev = &sdev->udev->dev;
1186 dev_info(dev, "------------------------------------------------\n");
1187 dev_info(dev, "verify mode\n");
1188 dev_info(dev, "format: %d\n", mode->format);
1189 dev_info(dev, "scale: %d\n", mode->scale);
1190 dev_info(dev, "fdec: %d\n", mode->fdec);
1191 dev_info(dev, "color: %d\n", mode->color);
1192 dev_info(dev, "bright: 0x%x\n", mode->bright);
1193 dev_info(dev, "restart: 0x%x\n", mode->restart);
1194 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1195 dev_info(dev, "single: 0x%x\n", mode->single);
1196 dev_info(dev, "------------------------------------------------\n");
1197}
1198
1199/*
1200 * set mode is the function which controls the DSP.
1201 * the restart parameter in struct s2255_mode should be set whenever
1202 * the image size could change via color format, video system or image
1203 * size.
1204 * When the restart parameter is set, we sleep for ONE frame to allow the
1205 * DSP time to get the new frame
1206 */
1207static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1208 struct s2255_mode *mode)
1209{
1210 int res;
1211 u32 *buffer;
1212 unsigned long chn_rev;
1213
Dean Anderson14d96262008-08-25 13:58:55 -03001214 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001215 chn_rev = G_chnmap[chn];
1216 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1217 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1218 dev->mode[chn].scale);
1219 dprintk(2, "mode contrast %x\n", mode->contrast);
1220
Dean Anderson22b88d42008-08-29 15:33:19 -03001221 /* if JPEG, set the quality */
1222 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1223 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1224
Dean Anderson38f993a2008-06-26 23:15:51 -03001225 /* save the mode */
1226 dev->mode[chn] = *mode;
1227 dev->req_image_size[chn] = get_transfer_size(mode);
1228 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1229
1230 buffer = kzalloc(512, GFP_KERNEL);
1231 if (buffer == NULL) {
1232 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001233 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001234 return -ENOMEM;
1235 }
1236
1237 /* set the mode */
1238 buffer[0] = IN_DATA_TOKEN;
1239 buffer[1] = (u32) chn_rev;
1240 buffer[2] = CMD_SET_MODE;
1241 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001242 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001243 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1244 if (debug)
1245 dump_verify_mode(dev, mode);
1246 kfree(buffer);
1247 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1248
1249 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001250 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001251 wait_event_timeout(dev->wait_setmode[chn],
1252 (dev->setmode_ready[chn] != 0),
1253 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1254 if (dev->setmode_ready[chn] != 1) {
1255 printk(KERN_DEBUG "s2255: no set mode response\n");
1256 res = -EFAULT;
1257 }
1258 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001259
1260 /* clear the restart flag */
1261 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001262 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001263 return res;
1264}
1265
1266static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1267{
1268 int res;
1269 struct s2255_fh *fh = priv;
1270 struct s2255_dev *dev = fh->dev;
1271 struct s2255_mode *new_mode;
1272 struct s2255_mode *old_mode;
1273 int chn;
1274 int j;
1275 dprintk(4, "%s\n", __func__);
1276 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1277 dev_err(&dev->udev->dev, "invalid fh type0\n");
1278 return -EINVAL;
1279 }
1280 if (i != fh->type) {
1281 dev_err(&dev->udev->dev, "invalid fh type1\n");
1282 return -EINVAL;
1283 }
1284
1285 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001286 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001287 return -EBUSY;
1288 }
1289
1290 /* send a set mode command everytime with restart.
1291 in case we switch resolutions or other parameters */
1292 chn = fh->channel;
1293 new_mode = &fh->mode;
1294 old_mode = &fh->dev->mode[chn];
1295
1296 if (new_mode->color != old_mode->color)
1297 new_mode->restart = 1;
1298 else if (new_mode->scale != old_mode->scale)
1299 new_mode->restart = 1;
1300 else if (new_mode->format != old_mode->format)
1301 new_mode->restart = 1;
1302
1303 s2255_set_mode(dev, chn, new_mode);
1304 new_mode->restart = 0;
1305 *old_mode = *new_mode;
1306 dev->cur_fmt[chn] = fh->fmt;
1307 dprintk(1, "%s[%d]\n", __func__, chn);
1308 dev->last_frame[chn] = -1;
1309 dev->bad_payload[chn] = 0;
1310 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001311 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001312 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001313 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001314 dev->buffer[chn].frame[j].cur_size = 0;
1315 }
1316 res = videobuf_streamon(&fh->vb_vidq);
1317 if (res == 0) {
1318 s2255_start_acquire(dev, chn);
1319 dev->b_acquire[chn] = 1;
1320 } else {
1321 res_free(dev, fh);
1322 }
1323 return res;
1324}
1325
1326static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1327{
Dean Anderson38f993a2008-06-26 23:15:51 -03001328 struct s2255_fh *fh = priv;
1329 struct s2255_dev *dev = fh->dev;
1330
1331 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1332 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1333 printk(KERN_ERR "invalid fh type0\n");
1334 return -EINVAL;
1335 }
1336 if (i != fh->type) {
1337 printk(KERN_ERR "invalid type i\n");
1338 return -EINVAL;
1339 }
1340 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001341 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001342 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001343 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001344}
1345
1346static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1347{
1348 struct s2255_fh *fh = priv;
1349 struct s2255_mode *mode;
1350 struct videobuf_queue *q = &fh->vb_vidq;
1351 int ret = 0;
1352
1353 mutex_lock(&q->vb_lock);
1354 if (videobuf_queue_is_busy(q)) {
1355 dprintk(1, "queue busy\n");
1356 ret = -EBUSY;
1357 goto out_s_std;
1358 }
1359
1360 if (res_locked(fh->dev, fh)) {
1361 dprintk(1, "can't change standard after started\n");
1362 ret = -EBUSY;
1363 goto out_s_std;
1364 }
1365 mode = &fh->mode;
1366
1367 if (*i & V4L2_STD_NTSC) {
1368 dprintk(4, "vidioc_s_std NTSC\n");
1369 mode->format = FORMAT_NTSC;
1370 } else if (*i & V4L2_STD_PAL) {
1371 dprintk(4, "vidioc_s_std PAL\n");
1372 mode->format = FORMAT_PAL;
1373 } else {
1374 ret = -EINVAL;
1375 }
1376out_s_std:
1377 mutex_unlock(&q->vb_lock);
1378 return ret;
1379}
1380
1381/* Sensoray 2255 is a multiple channel capture device.
1382 It does not have a "crossbar" of inputs.
1383 We use one V4L device per channel. The user must
1384 be aware that certain combinations are not allowed.
1385 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1386 at once in color(you can do full fps on 4 channels with greyscale.
1387*/
1388static int vidioc_enum_input(struct file *file, void *priv,
1389 struct v4l2_input *inp)
1390{
1391 if (inp->index != 0)
1392 return -EINVAL;
1393
1394 inp->type = V4L2_INPUT_TYPE_CAMERA;
1395 inp->std = S2255_NORMS;
1396 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001397 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001398}
1399
1400static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1401{
1402 *i = 0;
1403 return 0;
1404}
1405static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1406{
1407 if (i > 0)
1408 return -EINVAL;
1409 return 0;
1410}
1411
1412/* --- controls ---------------------------------------------- */
1413static int vidioc_queryctrl(struct file *file, void *priv,
1414 struct v4l2_queryctrl *qc)
1415{
1416 int i;
1417
1418 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1419 if (qc->id && qc->id == s2255_qctrl[i].id) {
1420 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001421 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001422 }
1423
1424 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1425 return -EINVAL;
1426}
1427
1428static int vidioc_g_ctrl(struct file *file, void *priv,
1429 struct v4l2_control *ctrl)
1430{
1431 int i;
1432
1433 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1434 if (ctrl->id == s2255_qctrl[i].id) {
1435 ctrl->value = qctl_regs[i];
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001436 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001437 }
1438 dprintk(4, "g_ctrl -EINVAL\n");
1439
1440 return -EINVAL;
1441}
1442
1443static int vidioc_s_ctrl(struct file *file, void *priv,
1444 struct v4l2_control *ctrl)
1445{
1446 int i;
1447 struct s2255_fh *fh = priv;
1448 struct s2255_dev *dev = fh->dev;
1449 struct s2255_mode *mode;
1450 mode = &fh->mode;
1451 dprintk(4, "vidioc_s_ctrl\n");
1452 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1453 if (ctrl->id == s2255_qctrl[i].id) {
1454 if (ctrl->value < s2255_qctrl[i].minimum ||
1455 ctrl->value > s2255_qctrl[i].maximum)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001456 return -ERANGE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001457
1458 qctl_regs[i] = ctrl->value;
1459 /* update the mode to the corresponding value */
1460 switch (ctrl->id) {
1461 case V4L2_CID_BRIGHTNESS:
1462 mode->bright = ctrl->value;
1463 break;
1464 case V4L2_CID_CONTRAST:
1465 mode->contrast = ctrl->value;
1466 break;
1467 case V4L2_CID_HUE:
1468 mode->hue = ctrl->value;
1469 break;
1470 case V4L2_CID_SATURATION:
1471 mode->saturation = ctrl->value;
1472 break;
1473 }
1474 mode->restart = 0;
1475 /* set mode here. Note: stream does not need restarted.
1476 some V4L programs restart stream unnecessarily
1477 after a s_crtl.
1478 */
1479 s2255_set_mode(dev, fh->channel, mode);
1480 return 0;
1481 }
1482 }
1483 return -EINVAL;
1484}
1485
Dean Anderson22b88d42008-08-29 15:33:19 -03001486static int vidioc_g_jpegcomp(struct file *file, void *priv,
1487 struct v4l2_jpegcompression *jc)
1488{
1489 struct s2255_fh *fh = priv;
1490 struct s2255_dev *dev = fh->dev;
1491 *jc = dev->jc[fh->channel];
1492 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1493 return 0;
1494}
1495
1496static int vidioc_s_jpegcomp(struct file *file, void *priv,
1497 struct v4l2_jpegcompression *jc)
1498{
1499 struct s2255_fh *fh = priv;
1500 struct s2255_dev *dev = fh->dev;
1501 if (jc->quality < 0 || jc->quality > 100)
1502 return -EINVAL;
1503 dev->jc[fh->channel].quality = jc->quality;
1504 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1505 return 0;
1506}
Dean Anderson7d853532009-05-15 14:32:04 -03001507
1508static int vidioc_g_parm(struct file *file, void *priv,
1509 struct v4l2_streamparm *sp)
1510{
1511 struct s2255_fh *fh = priv;
1512 struct s2255_dev *dev = fh->dev;
1513 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1514 return -EINVAL;
1515 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1516 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1517 return 0;
1518}
1519
1520static int vidioc_s_parm(struct file *file, void *priv,
1521 struct v4l2_streamparm *sp)
1522{
1523 struct s2255_fh *fh = priv;
1524 struct s2255_dev *dev = fh->dev;
1525
1526 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1527 return -EINVAL;
1528
1529 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1530 dprintk(2, "setting param capture mode %d\n",
1531 sp->parm.capture.capturemode);
1532 return 0;
1533}
Hans Verkuilbec43662008-12-30 06:58:20 -03001534static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001535{
Hans Verkuilbec43662008-12-30 06:58:20 -03001536 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001537 struct s2255_dev *h, *dev = NULL;
1538 struct s2255_fh *fh;
1539 struct list_head *list;
1540 enum v4l2_buf_type type = 0;
1541 int i = 0;
1542 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001543 int state;
Dean Anderson38f993a2008-06-26 23:15:51 -03001544 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1545
Hans Verkuild56dc612008-07-30 08:43:36 -03001546 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001547 list_for_each(list, &s2255_devlist) {
1548 h = list_entry(list, struct s2255_dev, s2255_devlist);
1549 for (i = 0; i < MAX_CHANNELS; i++) {
1550 if (h->vdev[i]->minor == minor) {
1551 cur_channel = i;
1552 dev = h;
1553 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1554 }
1555 }
1556 }
1557
1558 if ((NULL == dev) || (cur_channel == -1)) {
Hans Verkuild56dc612008-07-30 08:43:36 -03001559 unlock_kernel();
Dean Anderson14d96262008-08-25 13:58:55 -03001560 printk(KERN_INFO "s2255: openv4l no dev\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001561 return -ENODEV;
1562 }
1563
Dean Anderson14d96262008-08-25 13:58:55 -03001564 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1565 unlock_kernel();
1566 printk(KERN_INFO "disconnecting\n");
1567 return -ENODEV;
1568 }
1569 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001570 mutex_lock(&dev->open_lock);
1571
1572 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001573 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001574
Dean Anderson14d96262008-08-25 13:58:55 -03001575 switch (atomic_read(&dev->fw_data->fw_state)) {
1576 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001577 s2255_dev_err(&dev->udev->dev,
1578 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001579 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001580 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001581 ((atomic_read(&dev->fw_data->fw_state)
1582 == S2255_FW_SUCCESS) ||
1583 (atomic_read(&dev->fw_data->fw_state)
1584 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001585 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001586 break;
1587 case S2255_FW_NOTLOADED:
1588 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001589 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1590 driver loaded and then device immediately opened */
1591 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1592 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001593 ((atomic_read(&dev->fw_data->fw_state)
1594 == S2255_FW_SUCCESS) ||
1595 (atomic_read(&dev->fw_data->fw_state)
1596 == S2255_FW_DISCONNECTING)),
1597 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1598 break;
1599 case S2255_FW_SUCCESS:
1600 default:
1601 break;
1602 }
1603 state = atomic_read(&dev->fw_data->fw_state);
1604 if (state != S2255_FW_SUCCESS) {
1605 int rc;
1606 switch (state) {
1607 case S2255_FW_FAILED:
1608 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1609 rc = -ENODEV;
1610 break;
1611 case S2255_FW_DISCONNECTING:
1612 printk(KERN_INFO "%s: disconnecting\n", __func__);
1613 rc = -ENODEV;
1614 break;
1615 case S2255_FW_LOADED_DSPWAIT:
1616 case S2255_FW_NOTLOADED:
1617 printk(KERN_INFO "%s: firmware not loaded yet"
1618 "please try again later\n",
1619 __func__);
1620 rc = -EAGAIN;
1621 break;
1622 default:
1623 printk(KERN_INFO "%s: unknown state\n", __func__);
1624 rc = -EFAULT;
1625 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001626 }
Dean Anderson14d96262008-08-25 13:58:55 -03001627 dev->users[cur_channel]--;
1628 mutex_unlock(&dev->open_lock);
1629 kref_put(&dev->kref, s2255_destroy);
1630 unlock_kernel();
1631 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001632 }
1633
1634 /* allocate + initialize per filehandle data */
1635 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1636 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001637 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001638 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001639 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001640 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001641 return -ENOMEM;
1642 }
1643
1644 file->private_data = fh;
1645 fh->dev = dev;
1646 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1647 fh->mode = dev->mode[cur_channel];
1648 fh->fmt = dev->cur_fmt[cur_channel];
1649 /* default 4CIF NTSC */
1650 fh->width = LINE_SZ_4CIFS_NTSC;
1651 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1652 fh->channel = cur_channel;
1653
Dean Anderson14d96262008-08-25 13:58:55 -03001654 /* configure channel to default state */
1655 if (!dev->chn_configured[cur_channel]) {
1656 s2255_set_mode(dev, cur_channel, &fh->mode);
1657 dev->chn_configured[cur_channel] = 1;
1658 }
1659
1660
Dean Anderson38f993a2008-06-26 23:15:51 -03001661 /* Put all controls at a sane state */
1662 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1663 qctl_regs[i] = s2255_qctrl[i].default_value;
1664
1665 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1666 minor, v4l2_type_names[type], dev->users[cur_channel]);
1667 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1668 (unsigned long)fh, (unsigned long)dev,
1669 (unsigned long)&dev->vidq[cur_channel]);
1670 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1671 list_empty(&dev->vidq[cur_channel].active));
1672
1673 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1674 NULL, &dev->slock,
1675 fh->type,
1676 V4L2_FIELD_INTERLACED,
1677 sizeof(struct s2255_buffer), fh);
1678
Dean Anderson38f993a2008-06-26 23:15:51 -03001679 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001680 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001681 return 0;
1682}
1683
1684
1685static unsigned int s2255_poll(struct file *file,
1686 struct poll_table_struct *wait)
1687{
1688 struct s2255_fh *fh = file->private_data;
1689 int rc;
1690 dprintk(100, "%s\n", __func__);
1691
1692 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1693 return POLLERR;
1694
1695 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1696 return rc;
1697}
1698
1699static void s2255_destroy(struct kref *kref)
1700{
1701 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001702 struct list_head *list;
1703 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001704 if (!dev) {
1705 printk(KERN_ERR "s2255drv: kref problem\n");
1706 return;
1707 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001708 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1709 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001710 for (i = 0; i < MAX_CHANNELS; i++) {
1711 dev->setmode_ready[i] = 1;
1712 wake_up(&dev->wait_setmode[i]);
1713 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001714 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001715 /* reset the DSP so firmware can be reload next time */
1716 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001717 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001718 /* board shutdown stops the read pipe if it is running */
1719 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001720 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001721 del_timer(&dev->timer); /* only started in .probe and .open */
1722
Dean Anderson38f993a2008-06-26 23:15:51 -03001723 if (dev->fw_data->fw_urb) {
1724 dprintk(2, "kill fw_urb\n");
1725 usb_kill_urb(dev->fw_data->fw_urb);
1726 usb_free_urb(dev->fw_data->fw_urb);
1727 dev->fw_data->fw_urb = NULL;
1728 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001729 if (dev->fw_data->fw)
1730 release_firmware(dev->fw_data->fw);
1731 kfree(dev->fw_data->pfw_data);
1732 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001733 usb_put_dev(dev->udev);
1734 dprintk(1, "%s", __func__);
Dean Anderson14d96262008-08-25 13:58:55 -03001735
1736 while (!list_empty(&s2255_devlist)) {
1737 list = s2255_devlist.next;
1738 list_del(list);
1739 }
1740 mutex_unlock(&dev->open_lock);
Dean Andersonb7732a32009-03-30 11:59:56 -03001741 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001742}
1743
Hans Verkuilbec43662008-12-30 06:58:20 -03001744static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001745{
1746 struct s2255_fh *fh = file->private_data;
1747 struct s2255_dev *dev = fh->dev;
Hans Verkuilbec43662008-12-30 06:58:20 -03001748 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001749 if (!dev)
1750 return -ENODEV;
1751
1752 mutex_lock(&dev->open_lock);
1753
Dean Andersonf78d92c2008-07-22 14:43:27 -03001754 /* turn off stream */
1755 if (res_check(fh)) {
1756 if (dev->b_acquire[fh->channel])
1757 s2255_stop_acquire(dev, fh->channel);
1758 videobuf_streamoff(&fh->vb_vidq);
1759 res_free(dev, fh);
1760 }
1761
Dean Anderson38f993a2008-06-26 23:15:51 -03001762 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001763 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001764
Dean Anderson38f993a2008-06-26 23:15:51 -03001765 mutex_unlock(&dev->open_lock);
1766
1767 kref_put(&dev->kref, s2255_destroy);
1768 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1769 minor, dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001770 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001771 return 0;
1772}
1773
1774static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1775{
1776 struct s2255_fh *fh = file->private_data;
1777 int ret;
1778
1779 if (!fh)
1780 return -ENODEV;
1781 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1782
1783 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1784
1785 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1786 (unsigned long)vma->vm_start,
1787 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1788
1789 return ret;
1790}
1791
Hans Verkuilbec43662008-12-30 06:58:20 -03001792static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001793 .owner = THIS_MODULE,
1794 .open = s2255_open,
1795 .release = s2255_close,
1796 .poll = s2255_poll,
1797 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001798 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001799};
1800
Hans Verkuila3998102008-07-21 02:57:38 -03001801static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001802 .vidioc_querycap = vidioc_querycap,
1803 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1804 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1805 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1806 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1807 .vidioc_reqbufs = vidioc_reqbufs,
1808 .vidioc_querybuf = vidioc_querybuf,
1809 .vidioc_qbuf = vidioc_qbuf,
1810 .vidioc_dqbuf = vidioc_dqbuf,
1811 .vidioc_s_std = vidioc_s_std,
1812 .vidioc_enum_input = vidioc_enum_input,
1813 .vidioc_g_input = vidioc_g_input,
1814 .vidioc_s_input = vidioc_s_input,
1815 .vidioc_queryctrl = vidioc_queryctrl,
1816 .vidioc_g_ctrl = vidioc_g_ctrl,
1817 .vidioc_s_ctrl = vidioc_s_ctrl,
1818 .vidioc_streamon = vidioc_streamon,
1819 .vidioc_streamoff = vidioc_streamoff,
1820#ifdef CONFIG_VIDEO_V4L1_COMPAT
1821 .vidiocgmbuf = vidioc_cgmbuf,
1822#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001823 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1824 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001825 .vidioc_s_parm = vidioc_s_parm,
1826 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001827};
1828
1829static struct video_device template = {
1830 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001831 .fops = &s2255_fops_v4l,
1832 .ioctl_ops = &s2255_ioctl_ops,
1833 .minor = -1,
1834 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001835 .tvnorms = S2255_NORMS,
1836 .current_norm = V4L2_STD_NTSC_M,
1837};
1838
1839static int s2255_probe_v4l(struct s2255_dev *dev)
1840{
1841 int ret;
1842 int i;
1843 int cur_nr = video_nr;
1844
1845 /* initialize all video 4 linux */
1846 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1847 /* register 4 video devices */
1848 for (i = 0; i < MAX_CHANNELS; i++) {
1849 INIT_LIST_HEAD(&dev->vidq[i].active);
1850 dev->vidq[i].dev = dev;
1851 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001852 /* register 4 video devices */
1853 dev->vdev[i] = video_device_alloc();
1854 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001855 dev->vdev[i]->parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001856 if (video_nr == -1)
1857 ret = video_register_device(dev->vdev[i],
1858 VFL_TYPE_GRABBER,
1859 video_nr);
1860 else
1861 ret = video_register_device(dev->vdev[i],
1862 VFL_TYPE_GRABBER,
1863 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001864 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001865
1866 if (ret != 0) {
1867 dev_err(&dev->udev->dev,
1868 "failed to register video device!\n");
1869 return ret;
1870 }
1871 }
Dean Andersonabce21f2009-04-23 16:04:41 -03001872 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1873 S2255_MAJOR_VERSION,
1874 S2255_MINOR_VERSION);
Dean Anderson38f993a2008-06-26 23:15:51 -03001875 return ret;
1876}
1877
1878static void s2255_exit_v4l(struct s2255_dev *dev)
1879{
Dean Anderson14d96262008-08-25 13:58:55 -03001880
Dean Anderson38f993a2008-06-26 23:15:51 -03001881 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001882 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001883 if (-1 != dev->vdev[i]->minor) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001884 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001885 printk(KERN_INFO "s2255 unregistered\n");
1886 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001887 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001888 printk(KERN_INFO "s2255 released\n");
1889 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001890 }
1891}
1892
1893/* this function moves the usb stream read pipe data
1894 * into the system buffers.
1895 * returns 0 on success, EAGAIN if more data to process( call this
1896 * function again).
1897 *
1898 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001899 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001900 * bytes 4-7: channel: 0-3
1901 * bytes 8-11: payload size: size of the frame
1902 * bytes 12-payloadsize+12: frame data
1903 */
1904static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1905{
Dean Anderson38f993a2008-06-26 23:15:51 -03001906 char *pdest;
1907 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001908 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001909 char *psrc;
1910 unsigned long copy_size;
1911 unsigned long size;
1912 s32 idx = -1;
1913 struct s2255_framei *frm;
1914 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001915
Dean Anderson38f993a2008-06-26 23:15:51 -03001916 dprintk(100, "buffer to user\n");
1917
1918 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001919 frm = &dev->buffer[dev->cc].frame[idx];
1920
Dean Anderson14d96262008-08-25 13:58:55 -03001921 if (frm->ulState == S2255_READ_IDLE) {
1922 int jj;
1923 unsigned int cc;
1924 s32 *pdword;
1925 int payload;
1926 /* search for marker codes */
1927 pdata = (unsigned char *)pipe_info->transfer_buffer;
1928 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1929 switch (*(s32 *) pdata) {
1930 case S2255_MARKER_FRAME:
1931 pdword = (s32 *)pdata;
1932 dprintk(4, "found frame marker at offset:"
1933 " %d [%x %x]\n", jj, pdata[0],
1934 pdata[1]);
1935 offset = jj + PREFIX_SIZE;
1936 bframe = 1;
1937 cc = pdword[1];
1938 if (cc >= MAX_CHANNELS) {
1939 printk(KERN_ERR
1940 "bad channel\n");
1941 return -EINVAL;
1942 }
1943 /* reverse it */
1944 dev->cc = G_chnmap[cc];
1945 payload = pdword[3];
1946 if (payload > dev->req_image_size[dev->cc]) {
1947 dev->bad_payload[dev->cc]++;
1948 /* discard the bad frame */
1949 return -EINVAL;
1950 }
1951 dev->pkt_size[dev->cc] = payload;
1952 dev->jpg_size[dev->cc] = pdword[4];
1953 break;
1954 case S2255_MARKER_RESPONSE:
1955 pdword = (s32 *)pdata;
1956 pdata += DEF_USB_BLOCK;
1957 jj += DEF_USB_BLOCK;
1958 if (pdword[1] >= MAX_CHANNELS)
1959 break;
1960 cc = G_chnmap[pdword[1]];
1961 if (!(cc >= 0 && cc < MAX_CHANNELS))
1962 break;
1963 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03001964 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03001965 /* check if channel valid */
1966 /* set mode ready */
1967 dev->setmode_ready[cc] = 1;
1968 wake_up(&dev->wait_setmode[cc]);
1969 dprintk(5, "setmode ready %d\n", cc);
1970 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03001971 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03001972
1973 dev->chn_ready |= (1 << cc);
1974 if ((dev->chn_ready & 0x0f) != 0x0f)
1975 break;
1976 /* all channels ready */
1977 printk(KERN_INFO "s2255: fw loaded\n");
1978 atomic_set(&dev->fw_data->fw_state,
1979 S2255_FW_SUCCESS);
1980 wake_up(&dev->fw_data->wait_fw);
1981 break;
1982 default:
1983 printk(KERN_INFO "s2255 unknwn resp\n");
1984 }
1985 default:
1986 pdata++;
1987 break;
1988 }
1989 if (bframe)
1990 break;
1991 } /* for */
1992 if (!bframe)
1993 return -EINVAL;
1994 }
1995
1996
1997 idx = dev->cur_frame[dev->cc];
1998 frm = &dev->buffer[dev->cc].frame[idx];
1999
2000 /* search done. now find out if should be acquiring on this channel */
2001 if (!dev->b_acquire[dev->cc]) {
2002 /* we found a frame, but this channel is turned off */
2003 frm->ulState = S2255_READ_IDLE;
2004 return -EINVAL;
2005 }
2006
2007 if (frm->ulState == S2255_READ_IDLE) {
2008 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03002009 frm->cur_size = 0;
2010 }
2011
Dean Anderson14d96262008-08-25 13:58:55 -03002012 /* skip the marker 512 bytes (and offset if out of sync) */
2013 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2014
Dean Anderson38f993a2008-06-26 23:15:51 -03002015
2016 if (frm->lpvbits == NULL) {
2017 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2018 frm, dev, dev->cc, idx);
2019 return -ENOMEM;
2020 }
2021
2022 pdest = frm->lpvbits + frm->cur_size;
2023
Dean Anderson14d96262008-08-25 13:58:55 -03002024 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002025
Dean Anderson14d96262008-08-25 13:58:55 -03002026 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002027
Dean Anderson14d96262008-08-25 13:58:55 -03002028 /* sanity check on pdest */
2029 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2030 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002031
Dean Anderson38f993a2008-06-26 23:15:51 -03002032 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002033 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002034
Dean Anderson14d96262008-08-25 13:58:55 -03002035 if (frm->cur_size >= size) {
2036
Dean Anderson38f993a2008-06-26 23:15:51 -03002037 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002038 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2039 cc, idx);
2040 dev->last_frame[cc] = dev->cur_frame[cc];
2041 dev->cur_frame[cc]++;
2042 /* end of system frame ring buffer, start at zero */
2043 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2044 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2045 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002046 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002047 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002048 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002049 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002050 frm->ulState = S2255_READ_IDLE;
2051 frm->cur_size = 0;
2052
Dean Anderson38f993a2008-06-26 23:15:51 -03002053 }
2054 /* done successfully */
2055 return 0;
2056}
2057
2058static void s2255_read_video_callback(struct s2255_dev *dev,
2059 struct s2255_pipeinfo *pipe_info)
2060{
2061 int res;
2062 dprintk(50, "callback read video \n");
2063
2064 if (dev->cc >= MAX_CHANNELS) {
2065 dev->cc = 0;
2066 dev_err(&dev->udev->dev, "invalid channel\n");
2067 return;
2068 }
2069 /* otherwise copy to the system buffers */
2070 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002071 if (res != 0)
2072 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002073
2074 dprintk(50, "callback read video done\n");
2075 return;
2076}
2077
2078static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2079 u16 Index, u16 Value, void *TransferBuffer,
2080 s32 TransferBufferLength, int bOut)
2081{
2082 int r;
2083 if (!bOut) {
2084 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2085 Request,
2086 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2087 USB_DIR_IN,
2088 Value, Index, TransferBuffer,
2089 TransferBufferLength, HZ * 5);
2090 } else {
2091 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2092 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2093 Value, Index, TransferBuffer,
2094 TransferBufferLength, HZ * 5);
2095 }
2096 return r;
2097}
2098
2099/*
2100 * retrieve FX2 firmware version. future use.
2101 * @param dev pointer to device extension
2102 * @return -1 for fail, else returns firmware version as an int(16 bits)
2103 */
2104static int s2255_get_fx2fw(struct s2255_dev *dev)
2105{
2106 int fw;
2107 int ret;
2108 unsigned char transBuffer[64];
2109 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2110 S2255_VR_IN);
2111 if (ret < 0)
2112 dprintk(2, "get fw error: %x\n", ret);
2113 fw = transBuffer[0] + (transBuffer[1] << 8);
2114 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2115 return fw;
2116}
2117
2118/*
2119 * Create the system ring buffer to copy frames into from the
2120 * usb read pipe.
2121 */
2122static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2123{
2124 unsigned long i;
2125 unsigned long reqsize;
2126 dprintk(1, "create sys buffers\n");
2127 if (chn >= MAX_CHANNELS)
2128 return -1;
2129
2130 dev->buffer[chn].dwFrames = SYS_FRAMES;
2131
2132 /* always allocate maximum size(PAL) for system buffers */
2133 reqsize = SYS_FRAMES_MAXSIZE;
2134
2135 if (reqsize > SYS_FRAMES_MAXSIZE)
2136 reqsize = SYS_FRAMES_MAXSIZE;
2137
2138 for (i = 0; i < SYS_FRAMES; i++) {
2139 /* allocate the frames */
2140 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2141
2142 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2143 &dev->buffer[chn].frame[i], chn, i,
2144 dev->buffer[chn].frame[i].lpvbits);
2145 dev->buffer[chn].frame[i].size = reqsize;
2146 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2147 printk(KERN_INFO "out of memory. using less frames\n");
2148 dev->buffer[chn].dwFrames = i;
2149 break;
2150 }
2151 }
2152
2153 /* make sure internal states are set */
2154 for (i = 0; i < SYS_FRAMES; i++) {
2155 dev->buffer[chn].frame[i].ulState = 0;
2156 dev->buffer[chn].frame[i].cur_size = 0;
2157 }
2158
2159 dev->cur_frame[chn] = 0;
2160 dev->last_frame[chn] = -1;
2161 return 0;
2162}
2163
2164static int s2255_release_sys_buffers(struct s2255_dev *dev,
2165 unsigned long channel)
2166{
2167 unsigned long i;
2168 dprintk(1, "release sys buffers\n");
2169 for (i = 0; i < SYS_FRAMES; i++) {
2170 if (dev->buffer[channel].frame[i].lpvbits) {
2171 dprintk(1, "vfree %p\n",
2172 dev->buffer[channel].frame[i].lpvbits);
2173 vfree(dev->buffer[channel].frame[i].lpvbits);
2174 }
2175 dev->buffer[channel].frame[i].lpvbits = NULL;
2176 }
2177 return 0;
2178}
2179
2180static int s2255_board_init(struct s2255_dev *dev)
2181{
2182 int j;
2183 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2184 int fw_ver;
2185 dprintk(4, "board init: %p", dev);
2186
2187 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2188 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2189
2190 memset(pipe, 0, sizeof(*pipe));
2191 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002192 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2193 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002194
Dean Anderson38f993a2008-06-26 23:15:51 -03002195 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2196 GFP_KERNEL);
2197 if (pipe->transfer_buffer == NULL) {
2198 dprintk(1, "out of memory!\n");
2199 return -ENOMEM;
2200 }
2201
2202 }
2203
2204 /* query the firmware */
2205 fw_ver = s2255_get_fx2fw(dev);
2206
Dean Andersonabce21f2009-04-23 16:04:41 -03002207 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2208 (fw_ver >> 8) & 0xff,
2209 fw_ver & 0xff);
2210
2211 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002212 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002213 "usb firmware not up to date %d.%d\n",
2214 (fw_ver >> 8) & 0xff,
2215 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002216
2217 for (j = 0; j < MAX_CHANNELS; j++) {
2218 dev->b_acquire[j] = 0;
2219 dev->mode[j] = mode_def;
Dean Anderson22b88d42008-08-29 15:33:19 -03002220 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002221 dev->cur_fmt[j] = &formats[0];
2222 dev->mode[j].restart = 1;
2223 dev->req_image_size[j] = get_transfer_size(&mode_def);
2224 dev->frame_count[j] = 0;
2225 /* create the system buffers */
2226 s2255_create_sys_buffers(dev, j);
2227 }
2228 /* start read pipe */
2229 s2255_start_readpipe(dev);
2230
2231 dprintk(1, "S2255: board initialized\n");
2232 return 0;
2233}
2234
2235static int s2255_board_shutdown(struct s2255_dev *dev)
2236{
2237 u32 i;
2238
2239 dprintk(1, "S2255: board shutdown: %p", dev);
2240
2241 for (i = 0; i < MAX_CHANNELS; i++) {
2242 if (dev->b_acquire[i])
2243 s2255_stop_acquire(dev, i);
2244 }
2245
2246 s2255_stop_readpipe(dev);
2247
2248 for (i = 0; i < MAX_CHANNELS; i++)
2249 s2255_release_sys_buffers(dev, i);
2250
2251 /* release transfer buffers */
2252 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2253 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2254 kfree(pipe->transfer_buffer);
2255 }
2256 return 0;
2257}
2258
2259static void read_pipe_completion(struct urb *purb)
2260{
2261 struct s2255_pipeinfo *pipe_info;
2262 struct s2255_dev *dev;
2263 int status;
2264 int pipe;
2265
2266 pipe_info = purb->context;
2267 dprintk(100, "read pipe completion %p, status %d\n", purb,
2268 purb->status);
2269 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002270 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002271 return;
2272 }
2273
2274 dev = pipe_info->dev;
2275 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002276 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002277 return;
2278 }
2279 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002280 /* if shutting down, do not resubmit, exit immediately */
2281 if (status == -ESHUTDOWN) {
2282 dprintk(2, "read_pipe_completion: err shutdown\n");
2283 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002284 return;
2285 }
2286
2287 if (pipe_info->state == 0) {
2288 dprintk(2, "exiting USB pipe");
2289 return;
2290 }
2291
Dean Andersonb02064c2009-04-30 12:29:38 -03002292 if (status == 0)
2293 s2255_read_video_callback(dev, pipe_info);
2294 else {
2295 pipe_info->err_count++;
2296 dprintk(1, "s2255drv: failed URB %d\n", status);
2297 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002298
Dean Anderson38f993a2008-06-26 23:15:51 -03002299 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2300 /* reuse urb */
2301 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2302 pipe,
2303 pipe_info->transfer_buffer,
2304 pipe_info->cur_transfer_size,
2305 read_pipe_completion, pipe_info);
2306
2307 if (pipe_info->state != 0) {
2308 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2309 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002310 }
2311 } else {
2312 dprintk(2, "read pipe complete state 0\n");
2313 }
2314 return;
2315}
2316
2317static int s2255_start_readpipe(struct s2255_dev *dev)
2318{
2319 int pipe;
2320 int retval;
2321 int i;
2322 struct s2255_pipeinfo *pipe_info = dev->pipes;
2323 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2324 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2325
2326 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2327 pipe_info->state = 1;
Dean Andersonabce21f2009-04-23 16:04:41 -03002328 pipe_info->err_count = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002329 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2330 if (!pipe_info->stream_urb) {
2331 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002332 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002333 return -ENOMEM;
2334 }
2335 /* transfer buffer allocated in board_init */
2336 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2337 pipe,
2338 pipe_info->transfer_buffer,
2339 pipe_info->cur_transfer_size,
2340 read_pipe_completion, pipe_info);
2341
Dean Anderson38f993a2008-06-26 23:15:51 -03002342 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2343 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2344 if (retval) {
2345 printk(KERN_ERR "s2255: start read pipe failed\n");
2346 return retval;
2347 }
2348 }
2349
2350 return 0;
2351}
2352
2353/* starts acquisition process */
2354static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2355{
2356 unsigned char *buffer;
2357 int res;
2358 unsigned long chn_rev;
2359 int j;
2360 if (chn >= MAX_CHANNELS) {
2361 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2362 return -1;
2363 }
2364
2365 chn_rev = G_chnmap[chn];
2366 dprintk(1, "S2255: start acquire %lu \n", chn);
2367
2368 buffer = kzalloc(512, GFP_KERNEL);
2369 if (buffer == NULL) {
2370 dev_err(&dev->udev->dev, "out of mem\n");
2371 return -ENOMEM;
2372 }
2373
2374 dev->last_frame[chn] = -1;
2375 dev->bad_payload[chn] = 0;
2376 dev->cur_frame[chn] = 0;
2377 for (j = 0; j < SYS_FRAMES; j++) {
2378 dev->buffer[chn].frame[j].ulState = 0;
2379 dev->buffer[chn].frame[j].cur_size = 0;
2380 }
2381
2382 /* send the start command */
2383 *(u32 *) buffer = IN_DATA_TOKEN;
2384 *((u32 *) buffer + 1) = (u32) chn_rev;
2385 *((u32 *) buffer + 2) = (u32) CMD_START;
2386 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2387 if (res != 0)
2388 dev_err(&dev->udev->dev, "CMD_START error\n");
2389
2390 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2391 kfree(buffer);
2392 return 0;
2393}
2394
2395static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2396{
2397 unsigned char *buffer;
2398 int res;
2399 unsigned long chn_rev;
2400
2401 if (chn >= MAX_CHANNELS) {
2402 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2403 return -1;
2404 }
2405 chn_rev = G_chnmap[chn];
2406
2407 buffer = kzalloc(512, GFP_KERNEL);
2408 if (buffer == NULL) {
2409 dev_err(&dev->udev->dev, "out of mem\n");
2410 return -ENOMEM;
2411 }
2412
2413 /* send the stop command */
2414 dprintk(4, "stop acquire %lu\n", chn);
2415 *(u32 *) buffer = IN_DATA_TOKEN;
2416 *((u32 *) buffer + 1) = (u32) chn_rev;
2417 *((u32 *) buffer + 2) = CMD_STOP;
2418 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2419
2420 if (res != 0)
2421 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2422
2423 dprintk(4, "stop acquire: releasing states \n");
2424
2425 kfree(buffer);
2426 dev->b_acquire[chn] = 0;
2427
Dean Anderson14d96262008-08-25 13:58:55 -03002428 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002429}
2430
2431static void s2255_stop_readpipe(struct s2255_dev *dev)
2432{
2433 int j;
2434
2435 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002436 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002437 return;
2438 }
2439 dprintk(4, "stop read pipe\n");
2440 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2441 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2442 if (pipe_info) {
2443 if (pipe_info->state == 0)
2444 continue;
2445 pipe_info->state = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002446 }
2447 }
2448
2449 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2450 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2451 if (pipe_info->stream_urb) {
2452 /* cancel urb */
2453 usb_kill_urb(pipe_info->stream_urb);
2454 usb_free_urb(pipe_info->stream_urb);
2455 pipe_info->stream_urb = NULL;
2456 }
2457 }
2458 dprintk(2, "s2255 stop read pipe: %d\n", j);
2459 return;
2460}
2461
Dean Anderson14d96262008-08-25 13:58:55 -03002462static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002463{
Dean Anderson14d96262008-08-25 13:58:55 -03002464 if (reset)
2465 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002466 dev->fw_data->fw_size = dev->fw_data->fw->size;
2467 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2468 memcpy(dev->fw_data->pfw_data,
2469 dev->fw_data->fw->data, CHUNK_SIZE);
2470 dev->fw_data->fw_loaded = CHUNK_SIZE;
2471 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2472 usb_sndbulkpipe(dev->udev, 2),
2473 dev->fw_data->pfw_data,
2474 CHUNK_SIZE, s2255_fwchunk_complete,
2475 dev->fw_data);
2476 mod_timer(&dev->timer, jiffies + HZ);
2477}
2478
2479/* standard usb probe function */
2480static int s2255_probe(struct usb_interface *interface,
2481 const struct usb_device_id *id)
2482{
2483 struct s2255_dev *dev = NULL;
2484 struct usb_host_interface *iface_desc;
2485 struct usb_endpoint_descriptor *endpoint;
2486 int i;
2487 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002488 __le32 *pdata;
2489 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002490
2491 dprintk(2, "s2255: probe\n");
2492
2493 /* allocate memory for our device state and initialize it to zero */
2494 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2495 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002496 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002497 goto error;
2498 }
2499
2500 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2501 if (!dev->fw_data)
2502 goto error;
2503
2504 mutex_init(&dev->lock);
2505 mutex_init(&dev->open_lock);
2506
2507 /* grab usb_device and save it */
2508 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2509 if (dev->udev == NULL) {
2510 dev_err(&interface->dev, "null usb device\n");
2511 retval = -ENODEV;
2512 goto error;
2513 }
2514 kref_init(&dev->kref);
2515 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2516 dev->udev, interface);
2517 dev->interface = interface;
2518 /* set up the endpoint information */
2519 iface_desc = interface->cur_altsetting;
2520 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2521 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2522 endpoint = &iface_desc->endpoint[i].desc;
2523 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2524 /* we found the bulk in endpoint */
2525 dev->read_endpoint = endpoint->bEndpointAddress;
2526 }
2527 }
2528
2529 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002530 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002531 goto error;
2532 }
2533
2534 /* set intfdata */
2535 usb_set_intfdata(interface, dev);
2536
2537 dprintk(100, "after intfdata %p\n", dev);
2538
2539 init_timer(&dev->timer);
2540 dev->timer.function = s2255_timer;
2541 dev->timer.data = (unsigned long)dev->fw_data;
2542
2543 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03002544 for (i = 0; i < MAX_CHANNELS; i++)
2545 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002546
2547
2548 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2549
2550 if (!dev->fw_data->fw_urb) {
2551 dev_err(&interface->dev, "out of memory!\n");
2552 goto error;
2553 }
2554 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2555 if (!dev->fw_data->pfw_data) {
2556 dev_err(&interface->dev, "out of memory!\n");
2557 goto error;
2558 }
2559 /* load the first chunk */
2560 if (request_firmware(&dev->fw_data->fw,
2561 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2562 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2563 goto error;
2564 }
Dean Anderson14d96262008-08-25 13:58:55 -03002565 /* check the firmware is valid */
2566 fw_size = dev->fw_data->fw->size;
2567 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002568
Dean Anderson14d96262008-08-25 13:58:55 -03002569 if (*pdata != S2255_FW_MARKER) {
2570 printk(KERN_INFO "Firmware invalid.\n");
2571 retval = -ENODEV;
2572 goto error;
2573 } else {
2574 /* make sure firmware is the latest */
2575 __le32 *pRel;
2576 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2577 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2578 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002579 /* loads v4l specific */
2580 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002581 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002582 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002583 retval = s2255_board_init(dev);
2584 if (retval)
2585 goto error;
Dean Anderson38f993a2008-06-26 23:15:51 -03002586
2587 dprintk(4, "before probe done %p\n", dev);
2588 spin_lock_init(&dev->slock);
2589
Dean Anderson14d96262008-08-25 13:58:55 -03002590 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002591 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2592 return 0;
2593error:
2594 return retval;
2595}
2596
2597/* disconnect routine. when board is removed physically or with rmmod */
2598static void s2255_disconnect(struct usb_interface *interface)
2599{
2600 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002601 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002602 dprintk(1, "s2255: disconnect interface %p\n", interface);
2603 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002604
2605 /*
2606 * wake up any of the timers to allow open_lock to be
2607 * acquired sooner
2608 */
2609 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2610 wake_up(&dev->fw_data->wait_fw);
2611 for (i = 0; i < MAX_CHANNELS; i++) {
2612 dev->setmode_ready[i] = 1;
2613 wake_up(&dev->wait_setmode[i]);
2614 }
2615
2616 mutex_lock(&dev->open_lock);
2617 usb_set_intfdata(interface, NULL);
2618 mutex_unlock(&dev->open_lock);
2619
Dean Anderson38f993a2008-06-26 23:15:51 -03002620 if (dev) {
2621 kref_put(&dev->kref, s2255_destroy);
2622 dprintk(1, "s2255drv: disconnect\n");
2623 dev_info(&interface->dev, "s2255usb now disconnected\n");
2624 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002625}
2626
2627static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002628 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002629 .probe = s2255_probe,
2630 .disconnect = s2255_disconnect,
2631 .id_table = s2255_table,
2632};
2633
2634static int __init usb_s2255_init(void)
2635{
2636 int result;
2637
2638 /* register this driver with the USB subsystem */
2639 result = usb_register(&s2255_driver);
2640
2641 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002642 pr_err(KBUILD_MODNAME
2643 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002644
2645 dprintk(2, "s2255_init: done\n");
2646 return result;
2647}
2648
2649static void __exit usb_s2255_exit(void)
2650{
2651 usb_deregister(&s2255_driver);
2652}
2653
2654module_init(usb_s2255_init);
2655module_exit(usb_s2255_exit);
2656
2657MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2658MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2659MODULE_LICENSE("GPL");