blob: f6a4929867db74eaa7e0b92711d2ef004b834428 [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];
Dean Anderson38f993a2008-06-26 23:15:51 -0300236 struct timer_list timer;
237 struct s2255_fw *fw_data;
Dean Anderson38f993a2008-06-26 23:15:51 -0300238 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
239 struct s2255_bufferi buffer[MAX_CHANNELS];
240 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300241 /* jpeg compression */
242 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson7d853532009-05-15 14:32:04 -0300243 /* capture parameters (for high quality mode full size) */
244 struct v4l2_captureparm cap_parm[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300245 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
246 int cur_frame[MAX_CHANNELS];
247 int last_frame[MAX_CHANNELS];
248 u32 cc; /* current channel */
249 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300250 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300251 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300252 /* received packet size */
253 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300254 int bad_payload[MAX_CHANNELS];
255 unsigned long frame_count[MAX_CHANNELS];
256 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300257 /* if JPEG image */
258 int jpg_size[MAX_CHANNELS];
259 /* if channel configured to default state */
260 int chn_configured[MAX_CHANNELS];
261 wait_queue_head_t wait_setmode[MAX_CHANNELS];
262 int setmode_ready[MAX_CHANNELS];
263 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300264 struct kref kref;
265 spinlock_t slock;
266};
267#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
268
269struct s2255_fmt {
270 char *name;
271 u32 fourcc;
272 int depth;
273};
274
275/* buffer for one video frame */
276struct s2255_buffer {
277 /* common v4l buffer stuff -- must be first */
278 struct videobuf_buffer vb;
279 const struct s2255_fmt *fmt;
280};
281
282struct s2255_fh {
283 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300284 const struct s2255_fmt *fmt;
285 unsigned int width;
286 unsigned int height;
287 struct videobuf_queue vb_vidq;
288 enum v4l2_buf_type type;
289 int channel;
290 /* mode below is the desired mode.
291 mode in s2255_dev is the current mode that was last set */
292 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300293 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300294};
295
Dean Andersonabce21f2009-04-23 16:04:41 -0300296/* current cypress EEPROM firmware version */
297#define S2255_CUR_USB_FWVER ((3 << 8) | 6)
Dean Anderson38f993a2008-06-26 23:15:51 -0300298#define S2255_MAJOR_VERSION 1
Dean Andersonabce21f2009-04-23 16:04:41 -0300299#define S2255_MINOR_VERSION 14
Dean Anderson38f993a2008-06-26 23:15:51 -0300300#define S2255_RELEASE 0
301#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
302 S2255_MINOR_VERSION, \
303 S2255_RELEASE)
304
305/* vendor ids */
306#define USB_S2255_VENDOR_ID 0x1943
307#define USB_S2255_PRODUCT_ID 0x2255
308#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
309/* frame prefix size (sent once every frame) */
310#define PREFIX_SIZE 512
311
312/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300313static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300314
Dean Anderson38f993a2008-06-26 23:15:51 -0300315static int debug;
316static int *s2255_debug = &debug;
317
318static int s2255_start_readpipe(struct s2255_dev *dev);
319static void s2255_stop_readpipe(struct s2255_dev *dev);
320static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
321static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
322static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300323 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300324static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
325 struct s2255_mode *mode);
326static int s2255_board_shutdown(struct s2255_dev *dev);
327static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300328static void s2255_fwload_start(struct s2255_dev *dev, int reset);
329static void s2255_destroy(struct kref *kref);
330static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
331 u16 index, u16 value, void *buf,
332 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300333
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300334/* dev_err macro with driver name */
335#define S2255_DRIVER_NAME "s2255"
336#define s2255_dev_err(dev, fmt, arg...) \
337 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
338
Dean Anderson38f993a2008-06-26 23:15:51 -0300339#define dprintk(level, fmt, arg...) \
340 do { \
341 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300342 printk(KERN_DEBUG S2255_DRIVER_NAME \
343 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300344 } \
345 } while (0)
346
Dean Anderson38f993a2008-06-26 23:15:51 -0300347static struct usb_driver s2255_driver;
348
349
350/* Declare static vars that will be used as parameters */
351static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
352
353/* start video number */
354static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
355
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300356module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300357MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300358module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300359MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300360module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300361MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
362
363/* USB device table */
364static struct usb_device_id s2255_table[] = {
365 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
366 { } /* Terminating entry */
367};
368MODULE_DEVICE_TABLE(usb, s2255_table);
369
370
371#define BUFFER_TIMEOUT msecs_to_jiffies(400)
372
373/* supported controls */
374static struct v4l2_queryctrl s2255_qctrl[] = {
375 {
376 .id = V4L2_CID_BRIGHTNESS,
377 .type = V4L2_CTRL_TYPE_INTEGER,
378 .name = "Brightness",
379 .minimum = -127,
380 .maximum = 128,
381 .step = 1,
382 .default_value = 0,
383 .flags = 0,
384 }, {
385 .id = V4L2_CID_CONTRAST,
386 .type = V4L2_CTRL_TYPE_INTEGER,
387 .name = "Contrast",
388 .minimum = 0,
389 .maximum = 255,
390 .step = 0x1,
391 .default_value = DEF_CONTRAST,
392 .flags = 0,
393 }, {
394 .id = V4L2_CID_SATURATION,
395 .type = V4L2_CTRL_TYPE_INTEGER,
396 .name = "Saturation",
397 .minimum = 0,
398 .maximum = 255,
399 .step = 0x1,
400 .default_value = DEF_SATURATION,
401 .flags = 0,
402 }, {
403 .id = V4L2_CID_HUE,
404 .type = V4L2_CTRL_TYPE_INTEGER,
405 .name = "Hue",
406 .minimum = 0,
407 .maximum = 255,
408 .step = 0x1,
409 .default_value = DEF_HUE,
410 .flags = 0,
411 }
412};
413
414static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
415
416/* image formats. */
417static const struct s2255_fmt formats[] = {
418 {
419 .name = "4:2:2, planar, YUV422P",
420 .fourcc = V4L2_PIX_FMT_YUV422P,
421 .depth = 16
422
423 }, {
424 .name = "4:2:2, packed, YUYV",
425 .fourcc = V4L2_PIX_FMT_YUYV,
426 .depth = 16
427
428 }, {
429 .name = "4:2:2, packed, UYVY",
430 .fourcc = V4L2_PIX_FMT_UYVY,
431 .depth = 16
432 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300433 .name = "JPG",
434 .fourcc = V4L2_PIX_FMT_JPEG,
435 .depth = 24
436 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300437 .name = "8bpp GREY",
438 .fourcc = V4L2_PIX_FMT_GREY,
439 .depth = 8
440 }
441};
442
443static int norm_maxw(struct video_device *vdev)
444{
445 return (vdev->current_norm & V4L2_STD_NTSC) ?
446 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
447}
448
449static int norm_maxh(struct video_device *vdev)
450{
451 return (vdev->current_norm & V4L2_STD_NTSC) ?
452 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
453}
454
455static int norm_minw(struct video_device *vdev)
456{
457 return (vdev->current_norm & V4L2_STD_NTSC) ?
458 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
459}
460
461static int norm_minh(struct video_device *vdev)
462{
463 return (vdev->current_norm & V4L2_STD_NTSC) ?
464 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
465}
466
467
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300468/*
469 * TODO: fixme: move YUV reordering to hardware
470 * converts 2255 planar format to yuyv or uyvy
471 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300472static void planar422p_to_yuv_packed(const unsigned char *in,
473 unsigned char *out,
474 int width, int height,
475 int fmt)
476{
477 unsigned char *pY;
478 unsigned char *pCb;
479 unsigned char *pCr;
480 unsigned long size = height * width;
481 unsigned int i;
482 pY = (unsigned char *)in;
483 pCr = (unsigned char *)in + height * width;
484 pCb = (unsigned char *)in + height * width + (height * width / 2);
485 for (i = 0; i < size * 2; i += 4) {
486 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
487 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
488 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
489 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
490 }
491 return;
492}
493
Hans Verkuild45b9b82008-09-04 03:33:43 -0300494static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300495{
496 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
497 msleep(10);
498 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
499 return;
500}
Dean Anderson38f993a2008-06-26 23:15:51 -0300501
502/* kickstarts the firmware loading. from probe
503 */
504static void s2255_timer(unsigned long user_data)
505{
506 struct s2255_fw *data = (struct s2255_fw *)user_data;
507 dprintk(100, "s2255 timer\n");
508 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
509 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300510 atomic_set(&data->fw_state, S2255_FW_FAILED);
511 /* wake up anything waiting for the firmware */
512 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300513 return;
514 }
515}
516
Dean Anderson38f993a2008-06-26 23:15:51 -0300517
518/* this loads the firmware asynchronously.
519 Originally this was done synchroously in probe.
520 But it is better to load it asynchronously here than block
521 inside the probe function. Blocking inside probe affects boot time.
522 FW loading is triggered by the timer in the probe function
523*/
524static void s2255_fwchunk_complete(struct urb *urb)
525{
526 struct s2255_fw *data = urb->context;
527 struct usb_device *udev = urb->dev;
528 int len;
529 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300530 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300531 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300532 atomic_set(&data->fw_state, S2255_FW_FAILED);
533 /* wake up anything waiting for the firmware */
534 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300535 return;
536 }
537 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300538 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300539 atomic_set(&data->fw_state, S2255_FW_FAILED);
540 /* wake up anything waiting for the firmware */
541 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300542 return;
543 }
544#define CHUNK_SIZE 512
545 /* all USB transfers must be done with continuous kernel memory.
546 can't allocate more than 128k in current linux kernel, so
547 upload the firmware in chunks
548 */
549 if (data->fw_loaded < data->fw_size) {
550 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
551 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
552
553 if (len < CHUNK_SIZE)
554 memset(data->pfw_data, 0, CHUNK_SIZE);
555
556 dprintk(100, "completed len %d, loaded %d \n", len,
557 data->fw_loaded);
558
559 memcpy(data->pfw_data,
560 (char *) data->fw->data + data->fw_loaded, len);
561
562 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
563 data->pfw_data, CHUNK_SIZE,
564 s2255_fwchunk_complete, data);
565 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
566 dev_err(&udev->dev, "failed submit URB\n");
567 atomic_set(&data->fw_state, S2255_FW_FAILED);
568 /* wake up anything waiting for the firmware */
569 wake_up(&data->wait_fw);
570 return;
571 }
572 data->fw_loaded += len;
573 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300574 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300575 }
576 dprintk(100, "2255 complete done\n");
577 return;
578
579}
580
Dean Anderson14d96262008-08-25 13:58:55 -0300581static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300582{
583 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
584 struct s2255_buffer *buf;
585 unsigned long flags = 0;
586 int rc = 0;
587 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
588 spin_lock_irqsave(&dev->slock, flags);
589
590 if (list_empty(&dma_q->active)) {
591 dprintk(1, "No active queue to serve\n");
592 rc = -1;
593 goto unlock;
594 }
595 buf = list_entry(dma_q->active.next,
596 struct s2255_buffer, vb.queue);
597
Dean Anderson38f993a2008-06-26 23:15:51 -0300598 list_del(&buf->vb.queue);
599 do_gettimeofday(&buf->vb.ts);
600 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300601 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300602 wake_up(&buf->vb.done);
603 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
604unlock:
605 spin_unlock_irqrestore(&dev->slock, flags);
606 return 0;
607}
608
609
610static const struct s2255_fmt *format_by_fourcc(int fourcc)
611{
612 unsigned int i;
613
614 for (i = 0; i < ARRAY_SIZE(formats); i++) {
615 if (-1 == formats[i].fourcc)
616 continue;
617 if (formats[i].fourcc == fourcc)
618 return formats + i;
619 }
620 return NULL;
621}
622
623
624
625
626/* video buffer vmalloc implementation based partly on VIVI driver which is
627 * Copyright (c) 2006 by
628 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
629 * Ted Walther <ted--a.t--enumera.com>
630 * John Sokol <sokol--a.t--videotechnology.com>
631 * http://v4l.videotechnology.com/
632 *
633 */
634static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300635 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300636{
637 int pos = 0;
638 struct timeval ts;
639 const char *tmpbuf;
640 char *vbuf = videobuf_to_vmalloc(&buf->vb);
641 unsigned long last_frame;
642 struct s2255_framei *frm;
643
644 if (!vbuf)
645 return;
646
647 last_frame = dev->last_frame[chn];
648 if (last_frame != -1) {
649 frm = &dev->buffer[chn].frame[last_frame];
650 tmpbuf =
651 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
652 switch (buf->fmt->fourcc) {
653 case V4L2_PIX_FMT_YUYV:
654 case V4L2_PIX_FMT_UYVY:
655 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
656 vbuf, buf->vb.width,
657 buf->vb.height,
658 buf->fmt->fourcc);
659 break;
660 case V4L2_PIX_FMT_GREY:
661 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
662 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300663 case V4L2_PIX_FMT_JPEG:
664 buf->vb.size = jpgsize;
665 memcpy(vbuf, tmpbuf, buf->vb.size);
666 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300667 case V4L2_PIX_FMT_YUV422P:
668 memcpy(vbuf, tmpbuf,
669 buf->vb.width * buf->vb.height * 2);
670 break;
671 default:
672 printk(KERN_DEBUG "s2255: unknown format?\n");
673 }
674 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300675 } else {
676 printk(KERN_ERR "s2255: =======no frame\n");
677 return;
678
679 }
680 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
681 (unsigned long)vbuf, pos);
682 /* tell v4l buffer was filled */
683
Dean Andersona1c45302008-09-09 12:29:56 -0300684 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300685 do_gettimeofday(&ts);
686 buf->vb.ts = ts;
687 buf->vb.state = VIDEOBUF_DONE;
688}
689
690
691/* ------------------------------------------------------------------
692 Videobuf operations
693 ------------------------------------------------------------------*/
694
695static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
696 unsigned int *size)
697{
698 struct s2255_fh *fh = vq->priv_data;
699
700 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
701
702 if (0 == *count)
703 *count = S2255_DEF_BUFS;
704
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300705 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300706 (*count)--;
707
708 return 0;
709}
710
711static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
712{
713 dprintk(4, "%s\n", __func__);
714
Dean Anderson38f993a2008-06-26 23:15:51 -0300715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
717}
718
719static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
720 enum v4l2_field field)
721{
722 struct s2255_fh *fh = vq->priv_data;
723 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
724 int rc;
725 dprintk(4, "%s, field=%d\n", __func__, field);
726 if (fh->fmt == NULL)
727 return -EINVAL;
728
729 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
730 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
731 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
732 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
733 dprintk(4, "invalid buffer prepare\n");
734 return -EINVAL;
735 }
736
737 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
738
739 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
740 dprintk(4, "invalid buffer prepare\n");
741 return -EINVAL;
742 }
743
744 buf->fmt = fh->fmt;
745 buf->vb.width = fh->width;
746 buf->vb.height = fh->height;
747 buf->vb.field = field;
748
749
750 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
751 rc = videobuf_iolock(vq, &buf->vb, NULL);
752 if (rc < 0)
753 goto fail;
754 }
755
756 buf->vb.state = VIDEOBUF_PREPARED;
757 return 0;
758fail:
759 free_buffer(vq, buf);
760 return rc;
761}
762
763static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
764{
765 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
766 struct s2255_fh *fh = vq->priv_data;
767 struct s2255_dev *dev = fh->dev;
768 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
769
770 dprintk(1, "%s\n", __func__);
771
772 buf->vb.state = VIDEOBUF_QUEUED;
773 list_add_tail(&buf->vb.queue, &vidq->active);
774}
775
776static void buffer_release(struct videobuf_queue *vq,
777 struct videobuf_buffer *vb)
778{
779 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
780 struct s2255_fh *fh = vq->priv_data;
781 dprintk(4, "%s %d\n", __func__, fh->channel);
782 free_buffer(vq, buf);
783}
784
785static struct videobuf_queue_ops s2255_video_qops = {
786 .buf_setup = buffer_setup,
787 .buf_prepare = buffer_prepare,
788 .buf_queue = buffer_queue,
789 .buf_release = buffer_release,
790};
791
792
793static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
794{
795 /* is it free? */
796 mutex_lock(&dev->lock);
797 if (dev->resources[fh->channel]) {
798 /* no, someone else uses it */
799 mutex_unlock(&dev->lock);
800 return 0;
801 }
802 /* it's free, grab it */
803 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300804 fh->resources[fh->channel] = 1;
805 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300806 mutex_unlock(&dev->lock);
807 return 1;
808}
809
810static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
811{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300812 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300813}
814
Dean Andersonf78d92c2008-07-22 14:43:27 -0300815static int res_check(struct s2255_fh *fh)
816{
817 return fh->resources[fh->channel];
818}
819
820
Dean Anderson38f993a2008-06-26 23:15:51 -0300821static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
822{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300823 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300824 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300825 fh->resources[fh->channel] = 0;
826 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300827 dprintk(1, "res: put\n");
828}
829
830
831static int vidioc_querycap(struct file *file, void *priv,
832 struct v4l2_capability *cap)
833{
834 struct s2255_fh *fh = file->private_data;
835 struct s2255_dev *dev = fh->dev;
836 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
837 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300838 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300839 cap->version = S2255_VERSION;
840 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
841 return 0;
842}
843
844static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
845 struct v4l2_fmtdesc *f)
846{
847 int index = 0;
848 if (f)
849 index = f->index;
850
851 if (index >= ARRAY_SIZE(formats))
852 return -EINVAL;
853
854 dprintk(4, "name %s\n", formats[index].name);
855 strlcpy(f->description, formats[index].name, sizeof(f->description));
856 f->pixelformat = formats[index].fourcc;
857 return 0;
858}
859
860static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
861 struct v4l2_format *f)
862{
863 struct s2255_fh *fh = priv;
864
865 f->fmt.pix.width = fh->width;
866 f->fmt.pix.height = fh->height;
867 f->fmt.pix.field = fh->vb_vidq.field;
868 f->fmt.pix.pixelformat = fh->fmt->fourcc;
869 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
870 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300871 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300872}
873
874static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_format *f)
876{
877 const struct s2255_fmt *fmt;
878 enum v4l2_field field;
879 int b_any_field = 0;
880 struct s2255_fh *fh = priv;
881 struct s2255_dev *dev = fh->dev;
882 int is_ntsc;
883
884 is_ntsc =
885 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
886
887 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
888
889 if (fmt == NULL)
890 return -EINVAL;
891
892 field = f->fmt.pix.field;
893 if (field == V4L2_FIELD_ANY)
894 b_any_field = 1;
895
896 dprintk(4, "try format %d \n", is_ntsc);
897 /* supports 3 sizes. see s2255drv.h */
898 dprintk(50, "width test %d, height %d\n",
899 f->fmt.pix.width, f->fmt.pix.height);
900 if (is_ntsc) {
901 /* NTSC */
902 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
903 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
904 if (b_any_field) {
905 field = V4L2_FIELD_SEQ_TB;
906 } else if (!((field == V4L2_FIELD_INTERLACED) ||
907 (field == V4L2_FIELD_SEQ_TB) ||
908 (field == V4L2_FIELD_INTERLACED_TB))) {
909 dprintk(1, "unsupported field setting\n");
910 return -EINVAL;
911 }
912 } else {
913 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
914 if (b_any_field) {
915 field = V4L2_FIELD_TOP;
916 } else if (!((field == V4L2_FIELD_TOP) ||
917 (field == V4L2_FIELD_BOTTOM))) {
918 dprintk(1, "unsupported field setting\n");
919 return -EINVAL;
920 }
921
922 }
923 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
924 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
925 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
926 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
927 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
928 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
929 else
930 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
931 } else {
932 /* PAL */
933 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
934 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
935 if (b_any_field) {
936 field = V4L2_FIELD_SEQ_TB;
937 } else if (!((field == V4L2_FIELD_INTERLACED) ||
938 (field == V4L2_FIELD_SEQ_TB) ||
939 (field == V4L2_FIELD_INTERLACED_TB))) {
940 dprintk(1, "unsupported field setting\n");
941 return -EINVAL;
942 }
943 } else {
944 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
945 if (b_any_field) {
946 field = V4L2_FIELD_TOP;
947 } else if (!((field == V4L2_FIELD_TOP) ||
948 (field == V4L2_FIELD_BOTTOM))) {
949 dprintk(1, "unsupported field setting\n");
950 return -EINVAL;
951 }
952 }
953 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
954 dprintk(50, "pal 704\n");
955 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
956 field = V4L2_FIELD_SEQ_TB;
957 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
958 dprintk(50, "pal 352A\n");
959 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
960 field = V4L2_FIELD_TOP;
961 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
962 dprintk(50, "pal 352B\n");
963 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
964 field = V4L2_FIELD_TOP;
965 } else {
966 dprintk(50, "pal 352C\n");
967 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
968 field = V4L2_FIELD_TOP;
969 }
970 }
971
972 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
973 f->fmt.pix.height, f->fmt.pix.field);
974 f->fmt.pix.field = field;
975 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
976 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
977 return 0;
978}
979
980static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
981 struct v4l2_format *f)
982{
983 struct s2255_fh *fh = priv;
984 const struct s2255_fmt *fmt;
985 struct videobuf_queue *q = &fh->vb_vidq;
986 int ret;
987 int norm;
988
989 ret = vidioc_try_fmt_vid_cap(file, fh, f);
990
991 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300992 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300993
994 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
995
996 if (fmt == NULL)
997 return -EINVAL;
998
999 mutex_lock(&q->vb_lock);
1000
1001 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1002 dprintk(1, "queue busy\n");
1003 ret = -EBUSY;
1004 goto out_s_fmt;
1005 }
1006
1007 if (res_locked(fh->dev, fh)) {
1008 dprintk(1, "can't change format after started\n");
1009 ret = -EBUSY;
1010 goto out_s_fmt;
1011 }
1012
1013 fh->fmt = fmt;
1014 fh->width = f->fmt.pix.width;
1015 fh->height = f->fmt.pix.height;
1016 fh->vb_vidq.field = f->fmt.pix.field;
1017 fh->type = f->type;
1018 norm = norm_minw(fh->dev->vdev[fh->channel]);
1019 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
Dean Anderson7d853532009-05-15 14:32:04 -03001020 if (fh->height > norm_minh(fh->dev->vdev[fh->channel])) {
1021 if (fh->dev->cap_parm[fh->channel].capturemode &
1022 V4L2_MODE_HIGHQUALITY) {
1023 fh->mode.scale = SCALE_4CIFSI;
1024 dprintk(2, "scale 4CIFSI\n");
1025 } else {
1026 fh->mode.scale = SCALE_4CIFS;
1027 dprintk(2, "scale 4CIFS\n");
1028 }
1029 } else
Dean Anderson38f993a2008-06-26 23:15:51 -03001030 fh->mode.scale = SCALE_2CIFS;
1031
1032 } else {
1033 fh->mode.scale = SCALE_1CIFS;
1034 }
1035
1036 /* color mode */
1037 switch (fh->fmt->fourcc) {
1038 case V4L2_PIX_FMT_GREY:
1039 fh->mode.color = COLOR_Y8;
1040 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001041 case V4L2_PIX_FMT_JPEG:
Dean Anderson22b88d42008-08-29 15:33:19 -03001042 fh->mode.color = COLOR_JPG |
1043 (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001044 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001045 case V4L2_PIX_FMT_YUV422P:
1046 fh->mode.color = COLOR_YUVPL;
1047 break;
1048 case V4L2_PIX_FMT_YUYV:
1049 case V4L2_PIX_FMT_UYVY:
1050 default:
1051 fh->mode.color = COLOR_YUVPK;
1052 break;
1053 }
1054 ret = 0;
1055out_s_fmt:
1056 mutex_unlock(&q->vb_lock);
1057 return ret;
1058}
1059
1060static int vidioc_reqbufs(struct file *file, void *priv,
1061 struct v4l2_requestbuffers *p)
1062{
1063 int rc;
1064 struct s2255_fh *fh = priv;
1065 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1066 return rc;
1067}
1068
1069static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1070{
1071 int rc;
1072 struct s2255_fh *fh = priv;
1073 rc = videobuf_querybuf(&fh->vb_vidq, p);
1074 return rc;
1075}
1076
1077static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1078{
1079 int rc;
1080 struct s2255_fh *fh = priv;
1081 rc = videobuf_qbuf(&fh->vb_vidq, p);
1082 return rc;
1083}
1084
1085static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1086{
1087 int rc;
1088 struct s2255_fh *fh = priv;
1089 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1090 return rc;
1091}
1092
1093#ifdef CONFIG_VIDEO_V4L1_COMPAT
1094static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1095{
1096 struct s2255_fh *fh = priv;
1097
1098 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1099}
1100#endif
1101
1102/* write to the configuration pipe, synchronously */
1103static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1104 int size)
1105{
1106 int pipe;
1107 int done;
1108 long retval = -1;
1109 if (udev) {
1110 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1111 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1112 }
1113 return retval;
1114}
1115
1116static u32 get_transfer_size(struct s2255_mode *mode)
1117{
1118 int linesPerFrame = LINE_SZ_DEF;
1119 int pixelsPerLine = NUM_LINES_DEF;
1120 u32 outImageSize;
1121 u32 usbInSize;
1122 unsigned int mask_mult;
1123
1124 if (mode == NULL)
1125 return 0;
1126
1127 if (mode->format == FORMAT_NTSC) {
1128 switch (mode->scale) {
1129 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001130 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001131 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1132 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1133 break;
1134 case SCALE_2CIFS:
1135 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1136 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1137 break;
1138 case SCALE_1CIFS:
1139 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1140 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1141 break;
1142 default:
1143 break;
1144 }
1145 } else if (mode->format == FORMAT_PAL) {
1146 switch (mode->scale) {
1147 case SCALE_4CIFS:
Dean Anderson7d853532009-05-15 14:32:04 -03001148 case SCALE_4CIFSI:
Dean Anderson38f993a2008-06-26 23:15:51 -03001149 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1150 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1151 break;
1152 case SCALE_2CIFS:
1153 linesPerFrame = NUM_LINES_2CIFS_PAL;
1154 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1155 break;
1156 case SCALE_1CIFS:
1157 linesPerFrame = NUM_LINES_1CIFS_PAL;
1158 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1159 break;
1160 default:
1161 break;
1162 }
1163 }
1164 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001165 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001166 /* 2 bytes/pixel if not monochrome */
1167 outImageSize *= 2;
1168 }
1169
1170 /* total bytes to send including prefix and 4K padding;
1171 must be a multiple of USB_READ_SIZE */
1172 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1173 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1174 /* if size not a multiple of USB_READ_SIZE */
1175 if (usbInSize & ~mask_mult)
1176 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1177 return usbInSize;
1178}
1179
1180static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1181{
1182 struct device *dev = &sdev->udev->dev;
1183 dev_info(dev, "------------------------------------------------\n");
1184 dev_info(dev, "verify mode\n");
1185 dev_info(dev, "format: %d\n", mode->format);
1186 dev_info(dev, "scale: %d\n", mode->scale);
1187 dev_info(dev, "fdec: %d\n", mode->fdec);
1188 dev_info(dev, "color: %d\n", mode->color);
1189 dev_info(dev, "bright: 0x%x\n", mode->bright);
1190 dev_info(dev, "restart: 0x%x\n", mode->restart);
1191 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1192 dev_info(dev, "single: 0x%x\n", mode->single);
1193 dev_info(dev, "------------------------------------------------\n");
1194}
1195
1196/*
1197 * set mode is the function which controls the DSP.
1198 * the restart parameter in struct s2255_mode should be set whenever
1199 * the image size could change via color format, video system or image
1200 * size.
1201 * When the restart parameter is set, we sleep for ONE frame to allow the
1202 * DSP time to get the new frame
1203 */
1204static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1205 struct s2255_mode *mode)
1206{
1207 int res;
1208 u32 *buffer;
1209 unsigned long chn_rev;
1210
Dean Anderson14d96262008-08-25 13:58:55 -03001211 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001212 chn_rev = G_chnmap[chn];
1213 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1214 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1215 dev->mode[chn].scale);
1216 dprintk(2, "mode contrast %x\n", mode->contrast);
1217
Dean Anderson22b88d42008-08-29 15:33:19 -03001218 /* if JPEG, set the quality */
1219 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1220 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1221
Dean Anderson38f993a2008-06-26 23:15:51 -03001222 /* save the mode */
1223 dev->mode[chn] = *mode;
1224 dev->req_image_size[chn] = get_transfer_size(mode);
1225 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1226
1227 buffer = kzalloc(512, GFP_KERNEL);
1228 if (buffer == NULL) {
1229 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001230 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001231 return -ENOMEM;
1232 }
1233
1234 /* set the mode */
1235 buffer[0] = IN_DATA_TOKEN;
1236 buffer[1] = (u32) chn_rev;
1237 buffer[2] = CMD_SET_MODE;
1238 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
Dean Anderson9d63cec2009-04-20 19:07:44 -03001239 dev->setmode_ready[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001240 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1241 if (debug)
1242 dump_verify_mode(dev, mode);
1243 kfree(buffer);
1244 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1245
1246 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001247 if (mode->restart) {
Dean Anderson14d96262008-08-25 13:58:55 -03001248 wait_event_timeout(dev->wait_setmode[chn],
1249 (dev->setmode_ready[chn] != 0),
1250 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1251 if (dev->setmode_ready[chn] != 1) {
1252 printk(KERN_DEBUG "s2255: no set mode response\n");
1253 res = -EFAULT;
1254 }
1255 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001256
1257 /* clear the restart flag */
1258 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001259 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001260 return res;
1261}
1262
1263static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1264{
1265 int res;
1266 struct s2255_fh *fh = priv;
1267 struct s2255_dev *dev = fh->dev;
1268 struct s2255_mode *new_mode;
1269 struct s2255_mode *old_mode;
1270 int chn;
1271 int j;
1272 dprintk(4, "%s\n", __func__);
1273 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1274 dev_err(&dev->udev->dev, "invalid fh type0\n");
1275 return -EINVAL;
1276 }
1277 if (i != fh->type) {
1278 dev_err(&dev->udev->dev, "invalid fh type1\n");
1279 return -EINVAL;
1280 }
1281
1282 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001283 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001284 return -EBUSY;
1285 }
1286
1287 /* send a set mode command everytime with restart.
1288 in case we switch resolutions or other parameters */
1289 chn = fh->channel;
1290 new_mode = &fh->mode;
1291 old_mode = &fh->dev->mode[chn];
1292
1293 if (new_mode->color != old_mode->color)
1294 new_mode->restart = 1;
1295 else if (new_mode->scale != old_mode->scale)
1296 new_mode->restart = 1;
1297 else if (new_mode->format != old_mode->format)
1298 new_mode->restart = 1;
1299
1300 s2255_set_mode(dev, chn, new_mode);
1301 new_mode->restart = 0;
1302 *old_mode = *new_mode;
1303 dev->cur_fmt[chn] = fh->fmt;
1304 dprintk(1, "%s[%d]\n", __func__, chn);
1305 dev->last_frame[chn] = -1;
1306 dev->bad_payload[chn] = 0;
1307 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001308 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001309 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001310 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001311 dev->buffer[chn].frame[j].cur_size = 0;
1312 }
1313 res = videobuf_streamon(&fh->vb_vidq);
1314 if (res == 0) {
1315 s2255_start_acquire(dev, chn);
1316 dev->b_acquire[chn] = 1;
1317 } else {
1318 res_free(dev, fh);
1319 }
1320 return res;
1321}
1322
1323static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1324{
Dean Anderson38f993a2008-06-26 23:15:51 -03001325 struct s2255_fh *fh = priv;
1326 struct s2255_dev *dev = fh->dev;
1327
1328 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1329 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1330 printk(KERN_ERR "invalid fh type0\n");
1331 return -EINVAL;
1332 }
1333 if (i != fh->type) {
1334 printk(KERN_ERR "invalid type i\n");
1335 return -EINVAL;
1336 }
1337 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001338 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001339 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001340 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001341}
1342
1343static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1344{
1345 struct s2255_fh *fh = priv;
1346 struct s2255_mode *mode;
1347 struct videobuf_queue *q = &fh->vb_vidq;
1348 int ret = 0;
1349
1350 mutex_lock(&q->vb_lock);
1351 if (videobuf_queue_is_busy(q)) {
1352 dprintk(1, "queue busy\n");
1353 ret = -EBUSY;
1354 goto out_s_std;
1355 }
1356
1357 if (res_locked(fh->dev, fh)) {
1358 dprintk(1, "can't change standard after started\n");
1359 ret = -EBUSY;
1360 goto out_s_std;
1361 }
1362 mode = &fh->mode;
1363
1364 if (*i & V4L2_STD_NTSC) {
1365 dprintk(4, "vidioc_s_std NTSC\n");
1366 mode->format = FORMAT_NTSC;
1367 } else if (*i & V4L2_STD_PAL) {
1368 dprintk(4, "vidioc_s_std PAL\n");
1369 mode->format = FORMAT_PAL;
1370 } else {
1371 ret = -EINVAL;
1372 }
1373out_s_std:
1374 mutex_unlock(&q->vb_lock);
1375 return ret;
1376}
1377
1378/* Sensoray 2255 is a multiple channel capture device.
1379 It does not have a "crossbar" of inputs.
1380 We use one V4L device per channel. The user must
1381 be aware that certain combinations are not allowed.
1382 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1383 at once in color(you can do full fps on 4 channels with greyscale.
1384*/
1385static int vidioc_enum_input(struct file *file, void *priv,
1386 struct v4l2_input *inp)
1387{
1388 if (inp->index != 0)
1389 return -EINVAL;
1390
1391 inp->type = V4L2_INPUT_TYPE_CAMERA;
1392 inp->std = S2255_NORMS;
1393 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001394 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001395}
1396
1397static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1398{
1399 *i = 0;
1400 return 0;
1401}
1402static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1403{
1404 if (i > 0)
1405 return -EINVAL;
1406 return 0;
1407}
1408
1409/* --- controls ---------------------------------------------- */
1410static int vidioc_queryctrl(struct file *file, void *priv,
1411 struct v4l2_queryctrl *qc)
1412{
1413 int i;
1414
1415 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1416 if (qc->id && qc->id == s2255_qctrl[i].id) {
1417 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001418 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001419 }
1420
1421 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1422 return -EINVAL;
1423}
1424
1425static int vidioc_g_ctrl(struct file *file, void *priv,
1426 struct v4l2_control *ctrl)
1427{
1428 int i;
1429
1430 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1431 if (ctrl->id == s2255_qctrl[i].id) {
1432 ctrl->value = qctl_regs[i];
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001433 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001434 }
1435 dprintk(4, "g_ctrl -EINVAL\n");
1436
1437 return -EINVAL;
1438}
1439
1440static int vidioc_s_ctrl(struct file *file, void *priv,
1441 struct v4l2_control *ctrl)
1442{
1443 int i;
1444 struct s2255_fh *fh = priv;
1445 struct s2255_dev *dev = fh->dev;
1446 struct s2255_mode *mode;
1447 mode = &fh->mode;
1448 dprintk(4, "vidioc_s_ctrl\n");
1449 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1450 if (ctrl->id == s2255_qctrl[i].id) {
1451 if (ctrl->value < s2255_qctrl[i].minimum ||
1452 ctrl->value > s2255_qctrl[i].maximum)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001453 return -ERANGE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001454
1455 qctl_regs[i] = ctrl->value;
1456 /* update the mode to the corresponding value */
1457 switch (ctrl->id) {
1458 case V4L2_CID_BRIGHTNESS:
1459 mode->bright = ctrl->value;
1460 break;
1461 case V4L2_CID_CONTRAST:
1462 mode->contrast = ctrl->value;
1463 break;
1464 case V4L2_CID_HUE:
1465 mode->hue = ctrl->value;
1466 break;
1467 case V4L2_CID_SATURATION:
1468 mode->saturation = ctrl->value;
1469 break;
1470 }
1471 mode->restart = 0;
1472 /* set mode here. Note: stream does not need restarted.
1473 some V4L programs restart stream unnecessarily
1474 after a s_crtl.
1475 */
1476 s2255_set_mode(dev, fh->channel, mode);
1477 return 0;
1478 }
1479 }
1480 return -EINVAL;
1481}
1482
Dean Anderson22b88d42008-08-29 15:33:19 -03001483static int vidioc_g_jpegcomp(struct file *file, void *priv,
1484 struct v4l2_jpegcompression *jc)
1485{
1486 struct s2255_fh *fh = priv;
1487 struct s2255_dev *dev = fh->dev;
1488 *jc = dev->jc[fh->channel];
1489 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1490 return 0;
1491}
1492
1493static int vidioc_s_jpegcomp(struct file *file, void *priv,
1494 struct v4l2_jpegcompression *jc)
1495{
1496 struct s2255_fh *fh = priv;
1497 struct s2255_dev *dev = fh->dev;
1498 if (jc->quality < 0 || jc->quality > 100)
1499 return -EINVAL;
1500 dev->jc[fh->channel].quality = jc->quality;
1501 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1502 return 0;
1503}
Dean Anderson7d853532009-05-15 14:32:04 -03001504
1505static int vidioc_g_parm(struct file *file, void *priv,
1506 struct v4l2_streamparm *sp)
1507{
1508 struct s2255_fh *fh = priv;
1509 struct s2255_dev *dev = fh->dev;
1510 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1511 return -EINVAL;
1512 sp->parm.capture.capturemode = dev->cap_parm[fh->channel].capturemode;
1513 dprintk(2, "getting parm %d\n", sp->parm.capture.capturemode);
1514 return 0;
1515}
1516
1517static int vidioc_s_parm(struct file *file, void *priv,
1518 struct v4l2_streamparm *sp)
1519{
1520 struct s2255_fh *fh = priv;
1521 struct s2255_dev *dev = fh->dev;
1522
1523 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1524 return -EINVAL;
1525
1526 dev->cap_parm[fh->channel].capturemode = sp->parm.capture.capturemode;
1527 dprintk(2, "setting param capture mode %d\n",
1528 sp->parm.capture.capturemode);
1529 return 0;
1530}
Hans Verkuilbec43662008-12-30 06:58:20 -03001531static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001532{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001533 struct video_device *vdev = video_devdata(file);
1534 struct s2255_dev *dev = video_drvdata(file);
Dean Anderson38f993a2008-06-26 23:15:51 -03001535 struct s2255_fh *fh;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001536 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001537 int i = 0;
1538 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001539 int state;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001540
1541 dprintk(1, "s2255: open called (dev=%s)\n",
1542 video_device_node_name(vdev));
Dean Anderson38f993a2008-06-26 23:15:51 -03001543
Hans Verkuild56dc612008-07-30 08:43:36 -03001544 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001545
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001546 for (i = 0; i < MAX_CHANNELS; i++) {
1547 if (dev->vdev[i] == vdev) {
1548 cur_channel = i;
1549 break;
1550 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001551 }
1552
Dean Anderson14d96262008-08-25 13:58:55 -03001553 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1554 unlock_kernel();
1555 printk(KERN_INFO "disconnecting\n");
1556 return -ENODEV;
1557 }
1558 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001559 mutex_lock(&dev->open_lock);
1560
1561 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001562 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001563
Dean Anderson14d96262008-08-25 13:58:55 -03001564 switch (atomic_read(&dev->fw_data->fw_state)) {
1565 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001566 s2255_dev_err(&dev->udev->dev,
1567 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001568 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001569 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001570 ((atomic_read(&dev->fw_data->fw_state)
1571 == S2255_FW_SUCCESS) ||
1572 (atomic_read(&dev->fw_data->fw_state)
1573 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001574 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001575 break;
1576 case S2255_FW_NOTLOADED:
1577 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001578 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1579 driver loaded and then device immediately opened */
1580 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1581 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001582 ((atomic_read(&dev->fw_data->fw_state)
1583 == S2255_FW_SUCCESS) ||
1584 (atomic_read(&dev->fw_data->fw_state)
1585 == S2255_FW_DISCONNECTING)),
1586 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1587 break;
1588 case S2255_FW_SUCCESS:
1589 default:
1590 break;
1591 }
1592 state = atomic_read(&dev->fw_data->fw_state);
1593 if (state != S2255_FW_SUCCESS) {
1594 int rc;
1595 switch (state) {
1596 case S2255_FW_FAILED:
1597 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1598 rc = -ENODEV;
1599 break;
1600 case S2255_FW_DISCONNECTING:
1601 printk(KERN_INFO "%s: disconnecting\n", __func__);
1602 rc = -ENODEV;
1603 break;
1604 case S2255_FW_LOADED_DSPWAIT:
1605 case S2255_FW_NOTLOADED:
1606 printk(KERN_INFO "%s: firmware not loaded yet"
1607 "please try again later\n",
1608 __func__);
1609 rc = -EAGAIN;
1610 break;
1611 default:
1612 printk(KERN_INFO "%s: unknown state\n", __func__);
1613 rc = -EFAULT;
1614 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001615 }
Dean Anderson14d96262008-08-25 13:58:55 -03001616 dev->users[cur_channel]--;
1617 mutex_unlock(&dev->open_lock);
1618 kref_put(&dev->kref, s2255_destroy);
1619 unlock_kernel();
1620 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001621 }
1622
1623 /* allocate + initialize per filehandle data */
1624 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1625 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001626 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001627 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001628 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001629 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001630 return -ENOMEM;
1631 }
1632
1633 file->private_data = fh;
1634 fh->dev = dev;
1635 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1636 fh->mode = dev->mode[cur_channel];
1637 fh->fmt = dev->cur_fmt[cur_channel];
1638 /* default 4CIF NTSC */
1639 fh->width = LINE_SZ_4CIFS_NTSC;
1640 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1641 fh->channel = cur_channel;
1642
Dean Anderson14d96262008-08-25 13:58:55 -03001643 /* configure channel to default state */
1644 if (!dev->chn_configured[cur_channel]) {
1645 s2255_set_mode(dev, cur_channel, &fh->mode);
1646 dev->chn_configured[cur_channel] = 1;
1647 }
1648
1649
Dean Anderson38f993a2008-06-26 23:15:51 -03001650 /* Put all controls at a sane state */
1651 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1652 qctl_regs[i] = s2255_qctrl[i].default_value;
1653
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001654 dprintk(1, "s2255drv: open dev=%s type=%s users=%d\n",
1655 video_device_node_name(vdev), v4l2_type_names[type],
1656 dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001657 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1658 (unsigned long)fh, (unsigned long)dev,
1659 (unsigned long)&dev->vidq[cur_channel]);
1660 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1661 list_empty(&dev->vidq[cur_channel].active));
1662
1663 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1664 NULL, &dev->slock,
1665 fh->type,
1666 V4L2_FIELD_INTERLACED,
1667 sizeof(struct s2255_buffer), fh);
1668
Dean Anderson38f993a2008-06-26 23:15:51 -03001669 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001670 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001671 return 0;
1672}
1673
1674
1675static unsigned int s2255_poll(struct file *file,
1676 struct poll_table_struct *wait)
1677{
1678 struct s2255_fh *fh = file->private_data;
1679 int rc;
1680 dprintk(100, "%s\n", __func__);
1681
1682 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1683 return POLLERR;
1684
1685 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1686 return rc;
1687}
1688
1689static void s2255_destroy(struct kref *kref)
1690{
1691 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001692 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001693 if (!dev) {
1694 printk(KERN_ERR "s2255drv: kref problem\n");
1695 return;
1696 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001697 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1698 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001699 for (i = 0; i < MAX_CHANNELS; i++) {
1700 dev->setmode_ready[i] = 1;
1701 wake_up(&dev->wait_setmode[i]);
1702 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001703 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001704 /* reset the DSP so firmware can be reload next time */
1705 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001706 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001707 /* board shutdown stops the read pipe if it is running */
1708 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001709 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001710 del_timer(&dev->timer); /* only started in .probe and .open */
1711
Dean Anderson38f993a2008-06-26 23:15:51 -03001712 if (dev->fw_data->fw_urb) {
1713 dprintk(2, "kill fw_urb\n");
1714 usb_kill_urb(dev->fw_data->fw_urb);
1715 usb_free_urb(dev->fw_data->fw_urb);
1716 dev->fw_data->fw_urb = NULL;
1717 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001718 if (dev->fw_data->fw)
1719 release_firmware(dev->fw_data->fw);
1720 kfree(dev->fw_data->pfw_data);
1721 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001722 usb_put_dev(dev->udev);
1723 dprintk(1, "%s", __func__);
Dean Anderson14d96262008-08-25 13:58:55 -03001724
Dean Anderson14d96262008-08-25 13:58:55 -03001725 mutex_unlock(&dev->open_lock);
Dean Andersonb7732a32009-03-30 11:59:56 -03001726 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001727}
1728
Hans Verkuilbec43662008-12-30 06:58:20 -03001729static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001730{
1731 struct s2255_fh *fh = file->private_data;
1732 struct s2255_dev *dev = fh->dev;
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001733 struct video_device *vdev = video_devdata(file);
1734
Dean Anderson38f993a2008-06-26 23:15:51 -03001735 if (!dev)
1736 return -ENODEV;
1737
1738 mutex_lock(&dev->open_lock);
1739
Dean Andersonf78d92c2008-07-22 14:43:27 -03001740 /* turn off stream */
1741 if (res_check(fh)) {
1742 if (dev->b_acquire[fh->channel])
1743 s2255_stop_acquire(dev, fh->channel);
1744 videobuf_streamoff(&fh->vb_vidq);
1745 res_free(dev, fh);
1746 }
1747
Dean Anderson38f993a2008-06-26 23:15:51 -03001748 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001749 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001750
Dean Anderson38f993a2008-06-26 23:15:51 -03001751 mutex_unlock(&dev->open_lock);
1752
1753 kref_put(&dev->kref, s2255_destroy);
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001754 dprintk(1, "s2255: close called (dev=%s, users=%d)\n",
1755 video_device_node_name(vdev), dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001756 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001757 return 0;
1758}
1759
1760static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1761{
1762 struct s2255_fh *fh = file->private_data;
1763 int ret;
1764
1765 if (!fh)
1766 return -ENODEV;
1767 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1768
1769 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1770
1771 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1772 (unsigned long)vma->vm_start,
1773 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1774
1775 return ret;
1776}
1777
Hans Verkuilbec43662008-12-30 06:58:20 -03001778static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001779 .owner = THIS_MODULE,
1780 .open = s2255_open,
1781 .release = s2255_close,
1782 .poll = s2255_poll,
1783 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001784 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001785};
1786
Hans Verkuila3998102008-07-21 02:57:38 -03001787static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001788 .vidioc_querycap = vidioc_querycap,
1789 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1790 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1791 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1792 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1793 .vidioc_reqbufs = vidioc_reqbufs,
1794 .vidioc_querybuf = vidioc_querybuf,
1795 .vidioc_qbuf = vidioc_qbuf,
1796 .vidioc_dqbuf = vidioc_dqbuf,
1797 .vidioc_s_std = vidioc_s_std,
1798 .vidioc_enum_input = vidioc_enum_input,
1799 .vidioc_g_input = vidioc_g_input,
1800 .vidioc_s_input = vidioc_s_input,
1801 .vidioc_queryctrl = vidioc_queryctrl,
1802 .vidioc_g_ctrl = vidioc_g_ctrl,
1803 .vidioc_s_ctrl = vidioc_s_ctrl,
1804 .vidioc_streamon = vidioc_streamon,
1805 .vidioc_streamoff = vidioc_streamoff,
1806#ifdef CONFIG_VIDEO_V4L1_COMPAT
1807 .vidiocgmbuf = vidioc_cgmbuf,
1808#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001809 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1810 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Dean Anderson7d853532009-05-15 14:32:04 -03001811 .vidioc_s_parm = vidioc_s_parm,
1812 .vidioc_g_parm = vidioc_g_parm,
Hans Verkuila3998102008-07-21 02:57:38 -03001813};
1814
1815static struct video_device template = {
1816 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001817 .fops = &s2255_fops_v4l,
1818 .ioctl_ops = &s2255_ioctl_ops,
1819 .minor = -1,
1820 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001821 .tvnorms = S2255_NORMS,
1822 .current_norm = V4L2_STD_NTSC_M,
1823};
1824
1825static int s2255_probe_v4l(struct s2255_dev *dev)
1826{
1827 int ret;
1828 int i;
1829 int cur_nr = video_nr;
1830
1831 /* initialize all video 4 linux */
Dean Anderson38f993a2008-06-26 23:15:51 -03001832 /* register 4 video devices */
1833 for (i = 0; i < MAX_CHANNELS; i++) {
1834 INIT_LIST_HEAD(&dev->vidq[i].active);
1835 dev->vidq[i].dev = dev;
1836 dev->vidq[i].channel = i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001837 /* register 4 video devices */
1838 dev->vdev[i] = video_device_alloc();
1839 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001840 dev->vdev[i]->parent = &dev->interface->dev;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001841 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001842 if (video_nr == -1)
1843 ret = video_register_device(dev->vdev[i],
1844 VFL_TYPE_GRABBER,
1845 video_nr);
1846 else
1847 ret = video_register_device(dev->vdev[i],
1848 VFL_TYPE_GRABBER,
1849 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001850 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001851
1852 if (ret != 0) {
1853 dev_err(&dev->udev->dev,
1854 "failed to register video device!\n");
1855 return ret;
1856 }
1857 }
Dean Andersonabce21f2009-04-23 16:04:41 -03001858 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n",
1859 S2255_MAJOR_VERSION,
1860 S2255_MINOR_VERSION);
Dean Anderson38f993a2008-06-26 23:15:51 -03001861 return ret;
1862}
1863
1864static void s2255_exit_v4l(struct s2255_dev *dev)
1865{
Dean Anderson14d96262008-08-25 13:58:55 -03001866
Dean Anderson38f993a2008-06-26 23:15:51 -03001867 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001868 for (i = 0; i < MAX_CHANNELS; i++) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001869 if (video_is_registered(dev->vdev[i])) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001870 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001871 printk(KERN_INFO "s2255 unregistered\n");
1872 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001873 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001874 printk(KERN_INFO "s2255 released\n");
1875 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001876 }
1877}
1878
1879/* this function moves the usb stream read pipe data
1880 * into the system buffers.
1881 * returns 0 on success, EAGAIN if more data to process( call this
1882 * function again).
1883 *
1884 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001885 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001886 * bytes 4-7: channel: 0-3
1887 * bytes 8-11: payload size: size of the frame
1888 * bytes 12-payloadsize+12: frame data
1889 */
1890static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1891{
Dean Anderson38f993a2008-06-26 23:15:51 -03001892 char *pdest;
1893 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001894 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001895 char *psrc;
1896 unsigned long copy_size;
1897 unsigned long size;
1898 s32 idx = -1;
1899 struct s2255_framei *frm;
1900 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001901
Dean Anderson38f993a2008-06-26 23:15:51 -03001902 dprintk(100, "buffer to user\n");
1903
1904 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001905 frm = &dev->buffer[dev->cc].frame[idx];
1906
Dean Anderson14d96262008-08-25 13:58:55 -03001907 if (frm->ulState == S2255_READ_IDLE) {
1908 int jj;
1909 unsigned int cc;
1910 s32 *pdword;
1911 int payload;
1912 /* search for marker codes */
1913 pdata = (unsigned char *)pipe_info->transfer_buffer;
1914 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1915 switch (*(s32 *) pdata) {
1916 case S2255_MARKER_FRAME:
1917 pdword = (s32 *)pdata;
1918 dprintk(4, "found frame marker at offset:"
1919 " %d [%x %x]\n", jj, pdata[0],
1920 pdata[1]);
1921 offset = jj + PREFIX_SIZE;
1922 bframe = 1;
1923 cc = pdword[1];
1924 if (cc >= MAX_CHANNELS) {
1925 printk(KERN_ERR
1926 "bad channel\n");
1927 return -EINVAL;
1928 }
1929 /* reverse it */
1930 dev->cc = G_chnmap[cc];
1931 payload = pdword[3];
1932 if (payload > dev->req_image_size[dev->cc]) {
1933 dev->bad_payload[dev->cc]++;
1934 /* discard the bad frame */
1935 return -EINVAL;
1936 }
1937 dev->pkt_size[dev->cc] = payload;
1938 dev->jpg_size[dev->cc] = pdword[4];
1939 break;
1940 case S2255_MARKER_RESPONSE:
1941 pdword = (s32 *)pdata;
1942 pdata += DEF_USB_BLOCK;
1943 jj += DEF_USB_BLOCK;
1944 if (pdword[1] >= MAX_CHANNELS)
1945 break;
1946 cc = G_chnmap[pdword[1]];
Roel Kluinf14a2972009-10-23 07:59:42 -03001947 if (cc >= MAX_CHANNELS)
Dean Anderson14d96262008-08-25 13:58:55 -03001948 break;
1949 switch (pdword[2]) {
Dean Andersonabce21f2009-04-23 16:04:41 -03001950 case S2255_RESPONSE_SETMODE:
Dean Anderson14d96262008-08-25 13:58:55 -03001951 /* check if channel valid */
1952 /* set mode ready */
1953 dev->setmode_ready[cc] = 1;
1954 wake_up(&dev->wait_setmode[cc]);
1955 dprintk(5, "setmode ready %d\n", cc);
1956 break;
Dean Andersonabce21f2009-04-23 16:04:41 -03001957 case S2255_RESPONSE_FW:
Dean Anderson14d96262008-08-25 13:58:55 -03001958
1959 dev->chn_ready |= (1 << cc);
1960 if ((dev->chn_ready & 0x0f) != 0x0f)
1961 break;
1962 /* all channels ready */
1963 printk(KERN_INFO "s2255: fw loaded\n");
1964 atomic_set(&dev->fw_data->fw_state,
1965 S2255_FW_SUCCESS);
1966 wake_up(&dev->fw_data->wait_fw);
1967 break;
1968 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001969 printk(KERN_INFO "s2255 unknown resp\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001970 }
1971 default:
1972 pdata++;
1973 break;
1974 }
1975 if (bframe)
1976 break;
1977 } /* for */
1978 if (!bframe)
1979 return -EINVAL;
1980 }
1981
1982
1983 idx = dev->cur_frame[dev->cc];
1984 frm = &dev->buffer[dev->cc].frame[idx];
1985
1986 /* search done. now find out if should be acquiring on this channel */
1987 if (!dev->b_acquire[dev->cc]) {
1988 /* we found a frame, but this channel is turned off */
1989 frm->ulState = S2255_READ_IDLE;
1990 return -EINVAL;
1991 }
1992
1993 if (frm->ulState == S2255_READ_IDLE) {
1994 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03001995 frm->cur_size = 0;
1996 }
1997
Dean Anderson14d96262008-08-25 13:58:55 -03001998 /* skip the marker 512 bytes (and offset if out of sync) */
1999 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2000
Dean Anderson38f993a2008-06-26 23:15:51 -03002001
2002 if (frm->lpvbits == NULL) {
2003 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2004 frm, dev, dev->cc, idx);
2005 return -ENOMEM;
2006 }
2007
2008 pdest = frm->lpvbits + frm->cur_size;
2009
Dean Anderson14d96262008-08-25 13:58:55 -03002010 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03002011
Dean Anderson14d96262008-08-25 13:58:55 -03002012 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002013
Dean Anderson14d96262008-08-25 13:58:55 -03002014 /* sanity check on pdest */
2015 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2016 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002017
Dean Anderson38f993a2008-06-26 23:15:51 -03002018 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002019 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002020
Dean Anderson14d96262008-08-25 13:58:55 -03002021 if (frm->cur_size >= size) {
2022
Dean Anderson38f993a2008-06-26 23:15:51 -03002023 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002024 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2025 cc, idx);
2026 dev->last_frame[cc] = dev->cur_frame[cc];
2027 dev->cur_frame[cc]++;
2028 /* end of system frame ring buffer, start at zero */
2029 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2030 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2031 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002032 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002033 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002034 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002035 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002036 frm->ulState = S2255_READ_IDLE;
2037 frm->cur_size = 0;
2038
Dean Anderson38f993a2008-06-26 23:15:51 -03002039 }
2040 /* done successfully */
2041 return 0;
2042}
2043
2044static void s2255_read_video_callback(struct s2255_dev *dev,
2045 struct s2255_pipeinfo *pipe_info)
2046{
2047 int res;
2048 dprintk(50, "callback read video \n");
2049
2050 if (dev->cc >= MAX_CHANNELS) {
2051 dev->cc = 0;
2052 dev_err(&dev->udev->dev, "invalid channel\n");
2053 return;
2054 }
2055 /* otherwise copy to the system buffers */
2056 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002057 if (res != 0)
2058 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002059
2060 dprintk(50, "callback read video done\n");
2061 return;
2062}
2063
2064static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2065 u16 Index, u16 Value, void *TransferBuffer,
2066 s32 TransferBufferLength, int bOut)
2067{
2068 int r;
2069 if (!bOut) {
2070 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2071 Request,
2072 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2073 USB_DIR_IN,
2074 Value, Index, TransferBuffer,
2075 TransferBufferLength, HZ * 5);
2076 } else {
2077 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2078 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2079 Value, Index, TransferBuffer,
2080 TransferBufferLength, HZ * 5);
2081 }
2082 return r;
2083}
2084
2085/*
2086 * retrieve FX2 firmware version. future use.
2087 * @param dev pointer to device extension
2088 * @return -1 for fail, else returns firmware version as an int(16 bits)
2089 */
2090static int s2255_get_fx2fw(struct s2255_dev *dev)
2091{
2092 int fw;
2093 int ret;
2094 unsigned char transBuffer[64];
2095 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2096 S2255_VR_IN);
2097 if (ret < 0)
2098 dprintk(2, "get fw error: %x\n", ret);
2099 fw = transBuffer[0] + (transBuffer[1] << 8);
2100 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2101 return fw;
2102}
2103
2104/*
2105 * Create the system ring buffer to copy frames into from the
2106 * usb read pipe.
2107 */
2108static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2109{
2110 unsigned long i;
2111 unsigned long reqsize;
2112 dprintk(1, "create sys buffers\n");
2113 if (chn >= MAX_CHANNELS)
2114 return -1;
2115
2116 dev->buffer[chn].dwFrames = SYS_FRAMES;
2117
2118 /* always allocate maximum size(PAL) for system buffers */
2119 reqsize = SYS_FRAMES_MAXSIZE;
2120
2121 if (reqsize > SYS_FRAMES_MAXSIZE)
2122 reqsize = SYS_FRAMES_MAXSIZE;
2123
2124 for (i = 0; i < SYS_FRAMES; i++) {
2125 /* allocate the frames */
2126 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2127
2128 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2129 &dev->buffer[chn].frame[i], chn, i,
2130 dev->buffer[chn].frame[i].lpvbits);
2131 dev->buffer[chn].frame[i].size = reqsize;
2132 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2133 printk(KERN_INFO "out of memory. using less frames\n");
2134 dev->buffer[chn].dwFrames = i;
2135 break;
2136 }
2137 }
2138
2139 /* make sure internal states are set */
2140 for (i = 0; i < SYS_FRAMES; i++) {
2141 dev->buffer[chn].frame[i].ulState = 0;
2142 dev->buffer[chn].frame[i].cur_size = 0;
2143 }
2144
2145 dev->cur_frame[chn] = 0;
2146 dev->last_frame[chn] = -1;
2147 return 0;
2148}
2149
2150static int s2255_release_sys_buffers(struct s2255_dev *dev,
2151 unsigned long channel)
2152{
2153 unsigned long i;
2154 dprintk(1, "release sys buffers\n");
2155 for (i = 0; i < SYS_FRAMES; i++) {
2156 if (dev->buffer[channel].frame[i].lpvbits) {
2157 dprintk(1, "vfree %p\n",
2158 dev->buffer[channel].frame[i].lpvbits);
2159 vfree(dev->buffer[channel].frame[i].lpvbits);
2160 }
2161 dev->buffer[channel].frame[i].lpvbits = NULL;
2162 }
2163 return 0;
2164}
2165
2166static int s2255_board_init(struct s2255_dev *dev)
2167{
2168 int j;
2169 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2170 int fw_ver;
2171 dprintk(4, "board init: %p", dev);
2172
2173 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2174 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2175
2176 memset(pipe, 0, sizeof(*pipe));
2177 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002178 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2179 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002180
Dean Anderson38f993a2008-06-26 23:15:51 -03002181 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2182 GFP_KERNEL);
2183 if (pipe->transfer_buffer == NULL) {
2184 dprintk(1, "out of memory!\n");
2185 return -ENOMEM;
2186 }
2187
2188 }
2189
2190 /* query the firmware */
2191 fw_ver = s2255_get_fx2fw(dev);
2192
Dean Andersonabce21f2009-04-23 16:04:41 -03002193 printk(KERN_INFO "2255 usb firmware version %d.%d\n",
2194 (fw_ver >> 8) & 0xff,
2195 fw_ver & 0xff);
2196
2197 if (fw_ver < S2255_CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002198 dev_err(&dev->udev->dev,
Dean Andersonabce21f2009-04-23 16:04:41 -03002199 "usb firmware not up to date %d.%d\n",
2200 (fw_ver >> 8) & 0xff,
2201 fw_ver & 0xff);
Dean Anderson38f993a2008-06-26 23:15:51 -03002202
2203 for (j = 0; j < MAX_CHANNELS; j++) {
2204 dev->b_acquire[j] = 0;
2205 dev->mode[j] = mode_def;
Dean Anderson22b88d42008-08-29 15:33:19 -03002206 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002207 dev->cur_fmt[j] = &formats[0];
2208 dev->mode[j].restart = 1;
2209 dev->req_image_size[j] = get_transfer_size(&mode_def);
2210 dev->frame_count[j] = 0;
2211 /* create the system buffers */
2212 s2255_create_sys_buffers(dev, j);
2213 }
2214 /* start read pipe */
2215 s2255_start_readpipe(dev);
2216
2217 dprintk(1, "S2255: board initialized\n");
2218 return 0;
2219}
2220
2221static int s2255_board_shutdown(struct s2255_dev *dev)
2222{
2223 u32 i;
2224
2225 dprintk(1, "S2255: board shutdown: %p", dev);
2226
2227 for (i = 0; i < MAX_CHANNELS; i++) {
2228 if (dev->b_acquire[i])
2229 s2255_stop_acquire(dev, i);
2230 }
2231
2232 s2255_stop_readpipe(dev);
2233
2234 for (i = 0; i < MAX_CHANNELS; i++)
2235 s2255_release_sys_buffers(dev, i);
2236
2237 /* release transfer buffers */
2238 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2239 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2240 kfree(pipe->transfer_buffer);
2241 }
2242 return 0;
2243}
2244
2245static void read_pipe_completion(struct urb *purb)
2246{
2247 struct s2255_pipeinfo *pipe_info;
2248 struct s2255_dev *dev;
2249 int status;
2250 int pipe;
2251
2252 pipe_info = purb->context;
2253 dprintk(100, "read pipe completion %p, status %d\n", purb,
2254 purb->status);
2255 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002256 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002257 return;
2258 }
2259
2260 dev = pipe_info->dev;
2261 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002262 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002263 return;
2264 }
2265 status = purb->status;
Dean Andersonb02064c2009-04-30 12:29:38 -03002266 /* if shutting down, do not resubmit, exit immediately */
2267 if (status == -ESHUTDOWN) {
2268 dprintk(2, "read_pipe_completion: err shutdown\n");
2269 pipe_info->err_count++;
Dean Anderson38f993a2008-06-26 23:15:51 -03002270 return;
2271 }
2272
2273 if (pipe_info->state == 0) {
2274 dprintk(2, "exiting USB pipe");
2275 return;
2276 }
2277
Dean Andersonb02064c2009-04-30 12:29:38 -03002278 if (status == 0)
2279 s2255_read_video_callback(dev, pipe_info);
2280 else {
2281 pipe_info->err_count++;
2282 dprintk(1, "s2255drv: failed URB %d\n", status);
2283 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002284
Dean Anderson38f993a2008-06-26 23:15:51 -03002285 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2286 /* reuse urb */
2287 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2288 pipe,
2289 pipe_info->transfer_buffer,
2290 pipe_info->cur_transfer_size,
2291 read_pipe_completion, pipe_info);
2292
2293 if (pipe_info->state != 0) {
2294 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2295 dev_err(&dev->udev->dev, "error submitting urb\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002296 }
2297 } else {
2298 dprintk(2, "read pipe complete state 0\n");
2299 }
2300 return;
2301}
2302
2303static int s2255_start_readpipe(struct s2255_dev *dev)
2304{
2305 int pipe;
2306 int retval;
2307 int i;
2308 struct s2255_pipeinfo *pipe_info = dev->pipes;
2309 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2310 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2311
2312 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2313 pipe_info->state = 1;
Dean Andersonabce21f2009-04-23 16:04:41 -03002314 pipe_info->err_count = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002315 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2316 if (!pipe_info->stream_urb) {
2317 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002318 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002319 return -ENOMEM;
2320 }
2321 /* transfer buffer allocated in board_init */
2322 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2323 pipe,
2324 pipe_info->transfer_buffer,
2325 pipe_info->cur_transfer_size,
2326 read_pipe_completion, pipe_info);
2327
Dean Anderson38f993a2008-06-26 23:15:51 -03002328 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2329 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2330 if (retval) {
2331 printk(KERN_ERR "s2255: start read pipe failed\n");
2332 return retval;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339/* starts acquisition process */
2340static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2341{
2342 unsigned char *buffer;
2343 int res;
2344 unsigned long chn_rev;
2345 int j;
2346 if (chn >= MAX_CHANNELS) {
2347 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2348 return -1;
2349 }
2350
2351 chn_rev = G_chnmap[chn];
2352 dprintk(1, "S2255: start acquire %lu \n", chn);
2353
2354 buffer = kzalloc(512, GFP_KERNEL);
2355 if (buffer == NULL) {
2356 dev_err(&dev->udev->dev, "out of mem\n");
2357 return -ENOMEM;
2358 }
2359
2360 dev->last_frame[chn] = -1;
2361 dev->bad_payload[chn] = 0;
2362 dev->cur_frame[chn] = 0;
2363 for (j = 0; j < SYS_FRAMES; j++) {
2364 dev->buffer[chn].frame[j].ulState = 0;
2365 dev->buffer[chn].frame[j].cur_size = 0;
2366 }
2367
2368 /* send the start command */
2369 *(u32 *) buffer = IN_DATA_TOKEN;
2370 *((u32 *) buffer + 1) = (u32) chn_rev;
2371 *((u32 *) buffer + 2) = (u32) CMD_START;
2372 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2373 if (res != 0)
2374 dev_err(&dev->udev->dev, "CMD_START error\n");
2375
2376 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2377 kfree(buffer);
2378 return 0;
2379}
2380
2381static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2382{
2383 unsigned char *buffer;
2384 int res;
2385 unsigned long chn_rev;
2386
2387 if (chn >= MAX_CHANNELS) {
2388 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2389 return -1;
2390 }
2391 chn_rev = G_chnmap[chn];
2392
2393 buffer = kzalloc(512, GFP_KERNEL);
2394 if (buffer == NULL) {
2395 dev_err(&dev->udev->dev, "out of mem\n");
2396 return -ENOMEM;
2397 }
2398
2399 /* send the stop command */
2400 dprintk(4, "stop acquire %lu\n", chn);
2401 *(u32 *) buffer = IN_DATA_TOKEN;
2402 *((u32 *) buffer + 1) = (u32) chn_rev;
2403 *((u32 *) buffer + 2) = CMD_STOP;
2404 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2405
2406 if (res != 0)
2407 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2408
2409 dprintk(4, "stop acquire: releasing states \n");
2410
2411 kfree(buffer);
2412 dev->b_acquire[chn] = 0;
2413
Dean Anderson14d96262008-08-25 13:58:55 -03002414 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002415}
2416
2417static void s2255_stop_readpipe(struct s2255_dev *dev)
2418{
2419 int j;
2420
2421 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002422 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002423 return;
2424 }
2425 dprintk(4, "stop read pipe\n");
2426 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2427 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2428 if (pipe_info) {
2429 if (pipe_info->state == 0)
2430 continue;
2431 pipe_info->state = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03002432 }
2433 }
2434
2435 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2436 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2437 if (pipe_info->stream_urb) {
2438 /* cancel urb */
2439 usb_kill_urb(pipe_info->stream_urb);
2440 usb_free_urb(pipe_info->stream_urb);
2441 pipe_info->stream_urb = NULL;
2442 }
2443 }
2444 dprintk(2, "s2255 stop read pipe: %d\n", j);
2445 return;
2446}
2447
Dean Anderson14d96262008-08-25 13:58:55 -03002448static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002449{
Dean Anderson14d96262008-08-25 13:58:55 -03002450 if (reset)
2451 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002452 dev->fw_data->fw_size = dev->fw_data->fw->size;
2453 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2454 memcpy(dev->fw_data->pfw_data,
2455 dev->fw_data->fw->data, CHUNK_SIZE);
2456 dev->fw_data->fw_loaded = CHUNK_SIZE;
2457 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2458 usb_sndbulkpipe(dev->udev, 2),
2459 dev->fw_data->pfw_data,
2460 CHUNK_SIZE, s2255_fwchunk_complete,
2461 dev->fw_data);
2462 mod_timer(&dev->timer, jiffies + HZ);
2463}
2464
2465/* standard usb probe function */
2466static int s2255_probe(struct usb_interface *interface,
2467 const struct usb_device_id *id)
2468{
2469 struct s2255_dev *dev = NULL;
2470 struct usb_host_interface *iface_desc;
2471 struct usb_endpoint_descriptor *endpoint;
2472 int i;
2473 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002474 __le32 *pdata;
2475 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002476
2477 dprintk(2, "s2255: probe\n");
2478
2479 /* allocate memory for our device state and initialize it to zero */
2480 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2481 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002482 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002483 goto error;
2484 }
2485
2486 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2487 if (!dev->fw_data)
2488 goto error;
2489
2490 mutex_init(&dev->lock);
2491 mutex_init(&dev->open_lock);
2492
2493 /* grab usb_device and save it */
2494 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2495 if (dev->udev == NULL) {
2496 dev_err(&interface->dev, "null usb device\n");
2497 retval = -ENODEV;
2498 goto error;
2499 }
2500 kref_init(&dev->kref);
2501 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2502 dev->udev, interface);
2503 dev->interface = interface;
2504 /* set up the endpoint information */
2505 iface_desc = interface->cur_altsetting;
2506 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2507 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2508 endpoint = &iface_desc->endpoint[i].desc;
2509 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2510 /* we found the bulk in endpoint */
2511 dev->read_endpoint = endpoint->bEndpointAddress;
2512 }
2513 }
2514
2515 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002516 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002517 goto error;
2518 }
2519
2520 /* set intfdata */
2521 usb_set_intfdata(interface, dev);
2522
2523 dprintk(100, "after intfdata %p\n", dev);
2524
2525 init_timer(&dev->timer);
2526 dev->timer.function = s2255_timer;
2527 dev->timer.data = (unsigned long)dev->fw_data;
2528
2529 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03002530 for (i = 0; i < MAX_CHANNELS; i++)
2531 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002532
2533
2534 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2535
2536 if (!dev->fw_data->fw_urb) {
2537 dev_err(&interface->dev, "out of memory!\n");
2538 goto error;
2539 }
2540 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2541 if (!dev->fw_data->pfw_data) {
2542 dev_err(&interface->dev, "out of memory!\n");
2543 goto error;
2544 }
2545 /* load the first chunk */
2546 if (request_firmware(&dev->fw_data->fw,
2547 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2548 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2549 goto error;
2550 }
Dean Anderson14d96262008-08-25 13:58:55 -03002551 /* check the firmware is valid */
2552 fw_size = dev->fw_data->fw->size;
2553 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002554
Dean Anderson14d96262008-08-25 13:58:55 -03002555 if (*pdata != S2255_FW_MARKER) {
2556 printk(KERN_INFO "Firmware invalid.\n");
2557 retval = -ENODEV;
2558 goto error;
2559 } else {
2560 /* make sure firmware is the latest */
2561 __le32 *pRel;
2562 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2563 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2564 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002565 /* loads v4l specific */
2566 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002567 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002568 /* load 2255 board specific */
Dean Andersonabce21f2009-04-23 16:04:41 -03002569 retval = s2255_board_init(dev);
2570 if (retval)
2571 goto error;
Dean Anderson38f993a2008-06-26 23:15:51 -03002572
2573 dprintk(4, "before probe done %p\n", dev);
2574 spin_lock_init(&dev->slock);
2575
Dean Anderson14d96262008-08-25 13:58:55 -03002576 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002577 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2578 return 0;
2579error:
2580 return retval;
2581}
2582
2583/* disconnect routine. when board is removed physically or with rmmod */
2584static void s2255_disconnect(struct usb_interface *interface)
2585{
2586 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002587 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002588 dprintk(1, "s2255: disconnect interface %p\n", interface);
2589 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002590
2591 /*
2592 * wake up any of the timers to allow open_lock to be
2593 * acquired sooner
2594 */
2595 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2596 wake_up(&dev->fw_data->wait_fw);
2597 for (i = 0; i < MAX_CHANNELS; i++) {
2598 dev->setmode_ready[i] = 1;
2599 wake_up(&dev->wait_setmode[i]);
2600 }
2601
2602 mutex_lock(&dev->open_lock);
2603 usb_set_intfdata(interface, NULL);
2604 mutex_unlock(&dev->open_lock);
2605
Dean Anderson38f993a2008-06-26 23:15:51 -03002606 if (dev) {
2607 kref_put(&dev->kref, s2255_destroy);
2608 dprintk(1, "s2255drv: disconnect\n");
2609 dev_info(&interface->dev, "s2255usb now disconnected\n");
2610 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002611}
2612
2613static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002614 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002615 .probe = s2255_probe,
2616 .disconnect = s2255_disconnect,
2617 .id_table = s2255_table,
2618};
2619
2620static int __init usb_s2255_init(void)
2621{
2622 int result;
2623
2624 /* register this driver with the USB subsystem */
2625 result = usb_register(&s2255_driver);
2626
2627 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002628 pr_err(KBUILD_MODNAME
2629 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002630
2631 dprintk(2, "s2255_init: done\n");
2632 return result;
2633}
2634
2635static void __exit usb_s2255_exit(void)
2636{
2637 usb_deregister(&s2255_driver);
2638}
2639
2640module_init(usb_s2255_init);
2641module_exit(usb_s2255_exit);
2642
2643MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2644MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2645MODULE_LICENSE("GPL");