blob: ea40f4c8b33e9e9a3576a6ef27fd3723c2fe55ef [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>
Dean Anderson38f993a2008-06-26 23:15:51 -030051#include <media/videobuf-vmalloc.h>
52#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030053#include <media/v4l2-ioctl.h>
Dean Anderson38f993a2008-06-26 23:15:51 -030054#include <linux/vmalloc.h>
55#include <linux/usb.h>
56
57#define FIRMWARE_FILE_NAME "f2255usb.bin"
58
59
60
61/* vendor request in */
62#define S2255_VR_IN 0
63/* vendor request out */
64#define S2255_VR_OUT 1
65/* firmware query */
66#define S2255_VR_FW 0x30
67/* USB endpoint number for configuring the device */
68#define S2255_CONFIG_EP 2
69/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030070#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030071/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030072#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030073#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030074#define S2255_SETMODE_TIMEOUT 500
Dean Anderson38f993a2008-06-26 23:15:51 -030075#define MAX_CHANNELS 4
Dean Anderson14d96262008-08-25 13:58:55 -030076#define S2255_MARKER_FRAME 0x2255DA4AL
77#define S2255_MARKER_RESPONSE 0x2255ACACL
78#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030079#define MAX_CHANNELS 4
80#define MAX_PIPE_BUFFERS 1
81#define SYS_FRAMES 4
82/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030083#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
84#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030085#define LINE_SZ_4CIFS_NTSC 640
86#define LINE_SZ_2CIFS_NTSC 640
87#define LINE_SZ_1CIFS_NTSC 320
88#define LINE_SZ_4CIFS_PAL 704
89#define LINE_SZ_2CIFS_PAL 704
90#define LINE_SZ_1CIFS_PAL 352
91#define NUM_LINES_4CIFS_NTSC 240
92#define NUM_LINES_2CIFS_NTSC 240
93#define NUM_LINES_1CIFS_NTSC 240
94#define NUM_LINES_4CIFS_PAL 288
95#define NUM_LINES_2CIFS_PAL 288
96#define NUM_LINES_1CIFS_PAL 288
97#define LINE_SZ_DEF 640
98#define NUM_LINES_DEF 240
99
100
101/* predefined settings */
102#define FORMAT_NTSC 1
103#define FORMAT_PAL 2
104
105#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
106#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
107#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
108
109#define COLOR_YUVPL 1 /* YUV planar */
110#define COLOR_YUVPK 2 /* YUV packed */
111#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300112#define COLOR_JPG 5 /* JPEG */
113#define MASK_COLOR 0xff
114#define MASK_JPG_QUALITY 0xff00
Dean Anderson38f993a2008-06-26 23:15:51 -0300115
116/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
117#define FDEC_1 1 /* capture every frame. default */
118#define FDEC_2 2 /* capture every 2nd frame */
119#define FDEC_3 3 /* capture every 3rd frame */
120#define FDEC_5 5 /* capture every 5th frame */
121
122/*-------------------------------------------------------
123 * Default mode parameters.
124 *-------------------------------------------------------*/
125#define DEF_SCALE SCALE_4CIFS
126#define DEF_COLOR COLOR_YUVPL
127#define DEF_FDEC FDEC_1
128#define DEF_BRIGHT 0
129#define DEF_CONTRAST 0x5c
130#define DEF_SATURATION 0x80
131#define DEF_HUE 0
132
133/* usb config commands */
134#define IN_DATA_TOKEN 0x2255c0de
135#define CMD_2255 0xc2255000
136#define CMD_SET_MODE (CMD_2255 | 0x10)
137#define CMD_START (CMD_2255 | 0x20)
138#define CMD_STOP (CMD_2255 | 0x30)
139#define CMD_STATUS (CMD_2255 | 0x40)
140
141struct s2255_mode {
142 u32 format; /* input video format (NTSC, PAL) */
143 u32 scale; /* output video scale */
144 u32 color; /* output video color format */
145 u32 fdec; /* frame decimation */
146 u32 bright; /* brightness */
147 u32 contrast; /* contrast */
148 u32 saturation; /* saturation */
149 u32 hue; /* hue (NTSC only)*/
150 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
151 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
152 u32 restart; /* if DSP requires restart */
153};
154
Dean Anderson14d96262008-08-25 13:58:55 -0300155
156#define S2255_READ_IDLE 0
157#define S2255_READ_FRAME 1
158
Dean Anderson38f993a2008-06-26 23:15:51 -0300159/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300160struct s2255_framei {
161 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300162 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300163 void *lpvbits; /* image data */
164 unsigned long cur_size; /* current data copied to it */
165};
166
167/* image buffer structure */
168struct s2255_bufferi {
169 unsigned long dwFrames; /* number of frames in buffer */
170 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
171};
172
173#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
174 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300175 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300176
177struct s2255_dmaqueue {
178 struct list_head active;
179 /* thread for acquisition */
180 struct task_struct *kthread;
181 int frame;
182 struct s2255_dev *dev;
183 int channel;
184};
185
186/* for firmware loading, fw_state */
187#define S2255_FW_NOTLOADED 0
188#define S2255_FW_LOADED_DSPWAIT 1
189#define S2255_FW_SUCCESS 2
190#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300191#define S2255_FW_DISCONNECTING 4
Dean Anderson38f993a2008-06-26 23:15:51 -0300192
Dean Anderson14d96262008-08-25 13:58:55 -0300193#define S2255_FW_MARKER 0x22552f2f
194/* 2255 read states */
195#define S2255_READ_IDLE 0
196#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300197struct s2255_fw {
198 int fw_loaded;
199 int fw_size;
200 struct urb *fw_urb;
201 atomic_t fw_state;
202 void *pfw_data;
203 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300204 const struct firmware *fw;
205};
206
207struct s2255_pipeinfo {
208 u32 max_transfer_size;
209 u32 cur_transfer_size;
210 u8 *transfer_buffer;
211 u32 transfer_flags;;
212 u32 state;
213 u32 prev_state;
214 u32 urb_size;
215 void *stream_urb;
216 void *dev; /* back pointer to s2255_dev struct*/
217 u32 err_count;
218 u32 buf_index;
219 u32 idx;
220 u32 priority_set;
221};
222
223struct s2255_fmt; /*forward declaration */
224
225struct s2255_dev {
226 int frames;
227 int users[MAX_CHANNELS];
228 struct mutex lock;
229 struct mutex open_lock;
230 int resources[MAX_CHANNELS];
231 struct usb_device *udev;
232 struct usb_interface *interface;
233 u8 read_endpoint;
234
235 struct s2255_dmaqueue vidq[MAX_CHANNELS];
236 struct video_device *vdev[MAX_CHANNELS];
237 struct list_head s2255_devlist;
238 struct timer_list timer;
239 struct s2255_fw *fw_data;
240 int board_num;
241 int is_open;
242 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
243 struct s2255_bufferi buffer[MAX_CHANNELS];
244 struct s2255_mode mode[MAX_CHANNELS];
245 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 Anderson38f993a2008-06-26 23:15:51 -0300296#define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
297#define S2255_MAJOR_VERSION 1
298#define S2255_MINOR_VERSION 13
299#define S2255_RELEASE 0
300#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
301 S2255_MINOR_VERSION, \
302 S2255_RELEASE)
303
304/* vendor ids */
305#define USB_S2255_VENDOR_ID 0x1943
306#define USB_S2255_PRODUCT_ID 0x2255
307#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
308/* frame prefix size (sent once every frame) */
309#define PREFIX_SIZE 512
310
311/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300312static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300313
314static LIST_HEAD(s2255_devlist);
315
316static int debug;
317static int *s2255_debug = &debug;
318
319static int s2255_start_readpipe(struct s2255_dev *dev);
320static void s2255_stop_readpipe(struct s2255_dev *dev);
321static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
322static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
323static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300324 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300325static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
326 struct s2255_mode *mode);
327static int s2255_board_shutdown(struct s2255_dev *dev);
328static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300329static void s2255_fwload_start(struct s2255_dev *dev, int reset);
330static void s2255_destroy(struct kref *kref);
331static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
332 u16 index, u16 value, void *buf,
333 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300334
335#define dprintk(level, fmt, arg...) \
336 do { \
337 if (*s2255_debug >= (level)) { \
338 printk(KERN_DEBUG "s2255: " fmt, ##arg); \
339 } \
340 } while (0)
341
342
343static struct usb_driver s2255_driver;
344
345
346/* Declare static vars that will be used as parameters */
347static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
348
349/* start video number */
350static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
351
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300352module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300353MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300354module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300355MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300356module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300357MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
358
359/* USB device table */
360static struct usb_device_id s2255_table[] = {
361 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
362 { } /* Terminating entry */
363};
364MODULE_DEVICE_TABLE(usb, s2255_table);
365
366
367#define BUFFER_TIMEOUT msecs_to_jiffies(400)
368
369/* supported controls */
370static struct v4l2_queryctrl s2255_qctrl[] = {
371 {
372 .id = V4L2_CID_BRIGHTNESS,
373 .type = V4L2_CTRL_TYPE_INTEGER,
374 .name = "Brightness",
375 .minimum = -127,
376 .maximum = 128,
377 .step = 1,
378 .default_value = 0,
379 .flags = 0,
380 }, {
381 .id = V4L2_CID_CONTRAST,
382 .type = V4L2_CTRL_TYPE_INTEGER,
383 .name = "Contrast",
384 .minimum = 0,
385 .maximum = 255,
386 .step = 0x1,
387 .default_value = DEF_CONTRAST,
388 .flags = 0,
389 }, {
390 .id = V4L2_CID_SATURATION,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 .name = "Saturation",
393 .minimum = 0,
394 .maximum = 255,
395 .step = 0x1,
396 .default_value = DEF_SATURATION,
397 .flags = 0,
398 }, {
399 .id = V4L2_CID_HUE,
400 .type = V4L2_CTRL_TYPE_INTEGER,
401 .name = "Hue",
402 .minimum = 0,
403 .maximum = 255,
404 .step = 0x1,
405 .default_value = DEF_HUE,
406 .flags = 0,
407 }
408};
409
410static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
411
412/* image formats. */
413static const struct s2255_fmt formats[] = {
414 {
415 .name = "4:2:2, planar, YUV422P",
416 .fourcc = V4L2_PIX_FMT_YUV422P,
417 .depth = 16
418
419 }, {
420 .name = "4:2:2, packed, YUYV",
421 .fourcc = V4L2_PIX_FMT_YUYV,
422 .depth = 16
423
424 }, {
425 .name = "4:2:2, packed, UYVY",
426 .fourcc = V4L2_PIX_FMT_UYVY,
427 .depth = 16
428 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300429 .name = "JPG",
430 .fourcc = V4L2_PIX_FMT_JPEG,
431 .depth = 24
432 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300433 .name = "8bpp GREY",
434 .fourcc = V4L2_PIX_FMT_GREY,
435 .depth = 8
436 }
437};
438
439static int norm_maxw(struct video_device *vdev)
440{
441 return (vdev->current_norm & V4L2_STD_NTSC) ?
442 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
443}
444
445static int norm_maxh(struct video_device *vdev)
446{
447 return (vdev->current_norm & V4L2_STD_NTSC) ?
448 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
449}
450
451static int norm_minw(struct video_device *vdev)
452{
453 return (vdev->current_norm & V4L2_STD_NTSC) ?
454 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
455}
456
457static int norm_minh(struct video_device *vdev)
458{
459 return (vdev->current_norm & V4L2_STD_NTSC) ?
460 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
461}
462
463
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300464/*
465 * TODO: fixme: move YUV reordering to hardware
466 * converts 2255 planar format to yuyv or uyvy
467 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300468static void planar422p_to_yuv_packed(const unsigned char *in,
469 unsigned char *out,
470 int width, int height,
471 int fmt)
472{
473 unsigned char *pY;
474 unsigned char *pCb;
475 unsigned char *pCr;
476 unsigned long size = height * width;
477 unsigned int i;
478 pY = (unsigned char *)in;
479 pCr = (unsigned char *)in + height * width;
480 pCb = (unsigned char *)in + height * width + (height * width / 2);
481 for (i = 0; i < size * 2; i += 4) {
482 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
483 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
484 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
485 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
486 }
487 return;
488}
489
Dean Anderson14d96262008-08-25 13:58:55 -0300490void s2255_reset_dsppower(struct s2255_dev *dev)
491{
492 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
493 msleep(10);
494 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
495 return;
496}
Dean Anderson38f993a2008-06-26 23:15:51 -0300497
498/* kickstarts the firmware loading. from probe
499 */
500static void s2255_timer(unsigned long user_data)
501{
502 struct s2255_fw *data = (struct s2255_fw *)user_data;
503 dprintk(100, "s2255 timer\n");
504 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
505 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300506 atomic_set(&data->fw_state, S2255_FW_FAILED);
507 /* wake up anything waiting for the firmware */
508 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300509 return;
510 }
511}
512
Dean Anderson38f993a2008-06-26 23:15:51 -0300513
514/* this loads the firmware asynchronously.
515 Originally this was done synchroously in probe.
516 But it is better to load it asynchronously here than block
517 inside the probe function. Blocking inside probe affects boot time.
518 FW loading is triggered by the timer in the probe function
519*/
520static void s2255_fwchunk_complete(struct urb *urb)
521{
522 struct s2255_fw *data = urb->context;
523 struct usb_device *udev = urb->dev;
524 int len;
525 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300526 if (urb->status) {
527 dev_err(&udev->dev, "URB failed with status %d", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300528 atomic_set(&data->fw_state, S2255_FW_FAILED);
529 /* wake up anything waiting for the firmware */
530 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300531 return;
532 }
533 if (data->fw_urb == NULL) {
Dean Andersonf78d92c2008-07-22 14:43:27 -0300534 dev_err(&udev->dev, "s2255 disconnected\n");
535 atomic_set(&data->fw_state, S2255_FW_FAILED);
536 /* wake up anything waiting for the firmware */
537 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300538 return;
539 }
540#define CHUNK_SIZE 512
541 /* all USB transfers must be done with continuous kernel memory.
542 can't allocate more than 128k in current linux kernel, so
543 upload the firmware in chunks
544 */
545 if (data->fw_loaded < data->fw_size) {
546 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
547 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
548
549 if (len < CHUNK_SIZE)
550 memset(data->pfw_data, 0, CHUNK_SIZE);
551
552 dprintk(100, "completed len %d, loaded %d \n", len,
553 data->fw_loaded);
554
555 memcpy(data->pfw_data,
556 (char *) data->fw->data + data->fw_loaded, len);
557
558 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
559 data->pfw_data, CHUNK_SIZE,
560 s2255_fwchunk_complete, data);
561 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
562 dev_err(&udev->dev, "failed submit URB\n");
563 atomic_set(&data->fw_state, S2255_FW_FAILED);
564 /* wake up anything waiting for the firmware */
565 wake_up(&data->wait_fw);
566 return;
567 }
568 data->fw_loaded += len;
569 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300570 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300571 }
572 dprintk(100, "2255 complete done\n");
573 return;
574
575}
576
Dean Anderson14d96262008-08-25 13:58:55 -0300577static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300578{
579 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
580 struct s2255_buffer *buf;
581 unsigned long flags = 0;
582 int rc = 0;
583 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
584 spin_lock_irqsave(&dev->slock, flags);
585
586 if (list_empty(&dma_q->active)) {
587 dprintk(1, "No active queue to serve\n");
588 rc = -1;
589 goto unlock;
590 }
591 buf = list_entry(dma_q->active.next,
592 struct s2255_buffer, vb.queue);
593
594 if (!waitqueue_active(&buf->vb.done)) {
595 /* no one active */
596 rc = -1;
597 goto unlock;
598 }
599 list_del(&buf->vb.queue);
600 do_gettimeofday(&buf->vb.ts);
601 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300602 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300603 wake_up(&buf->vb.done);
604 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
605unlock:
606 spin_unlock_irqrestore(&dev->slock, flags);
607 return 0;
608}
609
610
611static const struct s2255_fmt *format_by_fourcc(int fourcc)
612{
613 unsigned int i;
614
615 for (i = 0; i < ARRAY_SIZE(formats); i++) {
616 if (-1 == formats[i].fourcc)
617 continue;
618 if (formats[i].fourcc == fourcc)
619 return formats + i;
620 }
621 return NULL;
622}
623
624
625
626
627/* video buffer vmalloc implementation based partly on VIVI driver which is
628 * Copyright (c) 2006 by
629 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
630 * Ted Walther <ted--a.t--enumera.com>
631 * John Sokol <sokol--a.t--videotechnology.com>
632 * http://v4l.videotechnology.com/
633 *
634 */
635static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300636 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300637{
638 int pos = 0;
639 struct timeval ts;
640 const char *tmpbuf;
641 char *vbuf = videobuf_to_vmalloc(&buf->vb);
642 unsigned long last_frame;
643 struct s2255_framei *frm;
644
645 if (!vbuf)
646 return;
647
648 last_frame = dev->last_frame[chn];
649 if (last_frame != -1) {
650 frm = &dev->buffer[chn].frame[last_frame];
651 tmpbuf =
652 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
653 switch (buf->fmt->fourcc) {
654 case V4L2_PIX_FMT_YUYV:
655 case V4L2_PIX_FMT_UYVY:
656 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
657 vbuf, buf->vb.width,
658 buf->vb.height,
659 buf->fmt->fourcc);
660 break;
661 case V4L2_PIX_FMT_GREY:
662 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
663 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300664 case V4L2_PIX_FMT_JPEG:
665 buf->vb.size = jpgsize;
666 memcpy(vbuf, tmpbuf, buf->vb.size);
667 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300668 case V4L2_PIX_FMT_YUV422P:
669 memcpy(vbuf, tmpbuf,
670 buf->vb.width * buf->vb.height * 2);
671 break;
672 default:
673 printk(KERN_DEBUG "s2255: unknown format?\n");
674 }
675 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300676 } else {
677 printk(KERN_ERR "s2255: =======no frame\n");
678 return;
679
680 }
681 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
682 (unsigned long)vbuf, pos);
683 /* tell v4l buffer was filled */
684
Dean Andersona1c45302008-09-09 12:29:56 -0300685 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300686 do_gettimeofday(&ts);
687 buf->vb.ts = ts;
688 buf->vb.state = VIDEOBUF_DONE;
689}
690
691
692/* ------------------------------------------------------------------
693 Videobuf operations
694 ------------------------------------------------------------------*/
695
696static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
697 unsigned int *size)
698{
699 struct s2255_fh *fh = vq->priv_data;
700
701 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
702
703 if (0 == *count)
704 *count = S2255_DEF_BUFS;
705
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300706 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300707 (*count)--;
708
709 return 0;
710}
711
712static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
713{
714 dprintk(4, "%s\n", __func__);
715
716 videobuf_waiton(&buf->vb, 0, 0);
717 videobuf_vmalloc_free(&buf->vb);
718 buf->vb.state = VIDEOBUF_NEEDS_INIT;
719}
720
721static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
722 enum v4l2_field field)
723{
724 struct s2255_fh *fh = vq->priv_data;
725 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
726 int rc;
727 dprintk(4, "%s, field=%d\n", __func__, field);
728 if (fh->fmt == NULL)
729 return -EINVAL;
730
731 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
732 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
733 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
734 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
735 dprintk(4, "invalid buffer prepare\n");
736 return -EINVAL;
737 }
738
739 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
740
741 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
742 dprintk(4, "invalid buffer prepare\n");
743 return -EINVAL;
744 }
745
746 buf->fmt = fh->fmt;
747 buf->vb.width = fh->width;
748 buf->vb.height = fh->height;
749 buf->vb.field = field;
750
751
752 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
753 rc = videobuf_iolock(vq, &buf->vb, NULL);
754 if (rc < 0)
755 goto fail;
756 }
757
758 buf->vb.state = VIDEOBUF_PREPARED;
759 return 0;
760fail:
761 free_buffer(vq, buf);
762 return rc;
763}
764
765static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
766{
767 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
768 struct s2255_fh *fh = vq->priv_data;
769 struct s2255_dev *dev = fh->dev;
770 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
771
772 dprintk(1, "%s\n", __func__);
773
774 buf->vb.state = VIDEOBUF_QUEUED;
775 list_add_tail(&buf->vb.queue, &vidq->active);
776}
777
778static void buffer_release(struct videobuf_queue *vq,
779 struct videobuf_buffer *vb)
780{
781 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
782 struct s2255_fh *fh = vq->priv_data;
783 dprintk(4, "%s %d\n", __func__, fh->channel);
784 free_buffer(vq, buf);
785}
786
787static struct videobuf_queue_ops s2255_video_qops = {
788 .buf_setup = buffer_setup,
789 .buf_prepare = buffer_prepare,
790 .buf_queue = buffer_queue,
791 .buf_release = buffer_release,
792};
793
794
795static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
796{
797 /* is it free? */
798 mutex_lock(&dev->lock);
799 if (dev->resources[fh->channel]) {
800 /* no, someone else uses it */
801 mutex_unlock(&dev->lock);
802 return 0;
803 }
804 /* it's free, grab it */
805 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300806 fh->resources[fh->channel] = 1;
807 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300808 mutex_unlock(&dev->lock);
809 return 1;
810}
811
812static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
813{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300814 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300815}
816
Dean Andersonf78d92c2008-07-22 14:43:27 -0300817static int res_check(struct s2255_fh *fh)
818{
819 return fh->resources[fh->channel];
820}
821
822
Dean Anderson38f993a2008-06-26 23:15:51 -0300823static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
824{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300825 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300826 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300827 fh->resources[fh->channel] = 0;
828 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300829 dprintk(1, "res: put\n");
830}
831
832
833static int vidioc_querycap(struct file *file, void *priv,
834 struct v4l2_capability *cap)
835{
836 struct s2255_fh *fh = file->private_data;
837 struct s2255_dev *dev = fh->dev;
838 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
839 strlcpy(cap->card, "s2255", sizeof(cap->card));
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300840 strlcpy(cap->bus_info, dev_name(&dev->udev->dev),
841 sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300842 cap->version = S2255_VERSION;
843 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
844 return 0;
845}
846
847static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
848 struct v4l2_fmtdesc *f)
849{
850 int index = 0;
851 if (f)
852 index = f->index;
853
854 if (index >= ARRAY_SIZE(formats))
855 return -EINVAL;
856
857 dprintk(4, "name %s\n", formats[index].name);
858 strlcpy(f->description, formats[index].name, sizeof(f->description));
859 f->pixelformat = formats[index].fourcc;
860 return 0;
861}
862
863static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
864 struct v4l2_format *f)
865{
866 struct s2255_fh *fh = priv;
867
868 f->fmt.pix.width = fh->width;
869 f->fmt.pix.height = fh->height;
870 f->fmt.pix.field = fh->vb_vidq.field;
871 f->fmt.pix.pixelformat = fh->fmt->fourcc;
872 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
873 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300874 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300875}
876
877static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_format *f)
879{
880 const struct s2255_fmt *fmt;
881 enum v4l2_field field;
882 int b_any_field = 0;
883 struct s2255_fh *fh = priv;
884 struct s2255_dev *dev = fh->dev;
885 int is_ntsc;
886
887 is_ntsc =
888 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
889
890 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
891
892 if (fmt == NULL)
893 return -EINVAL;
894
895 field = f->fmt.pix.field;
896 if (field == V4L2_FIELD_ANY)
897 b_any_field = 1;
898
899 dprintk(4, "try format %d \n", is_ntsc);
900 /* supports 3 sizes. see s2255drv.h */
901 dprintk(50, "width test %d, height %d\n",
902 f->fmt.pix.width, f->fmt.pix.height);
903 if (is_ntsc) {
904 /* NTSC */
905 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
906 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
907 if (b_any_field) {
908 field = V4L2_FIELD_SEQ_TB;
909 } else if (!((field == V4L2_FIELD_INTERLACED) ||
910 (field == V4L2_FIELD_SEQ_TB) ||
911 (field == V4L2_FIELD_INTERLACED_TB))) {
912 dprintk(1, "unsupported field setting\n");
913 return -EINVAL;
914 }
915 } else {
916 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
917 if (b_any_field) {
918 field = V4L2_FIELD_TOP;
919 } else if (!((field == V4L2_FIELD_TOP) ||
920 (field == V4L2_FIELD_BOTTOM))) {
921 dprintk(1, "unsupported field setting\n");
922 return -EINVAL;
923 }
924
925 }
926 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
927 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
928 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
929 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
930 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
931 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
932 else
933 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
934 } else {
935 /* PAL */
936 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
937 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
938 if (b_any_field) {
939 field = V4L2_FIELD_SEQ_TB;
940 } else if (!((field == V4L2_FIELD_INTERLACED) ||
941 (field == V4L2_FIELD_SEQ_TB) ||
942 (field == V4L2_FIELD_INTERLACED_TB))) {
943 dprintk(1, "unsupported field setting\n");
944 return -EINVAL;
945 }
946 } else {
947 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
948 if (b_any_field) {
949 field = V4L2_FIELD_TOP;
950 } else if (!((field == V4L2_FIELD_TOP) ||
951 (field == V4L2_FIELD_BOTTOM))) {
952 dprintk(1, "unsupported field setting\n");
953 return -EINVAL;
954 }
955 }
956 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
957 dprintk(50, "pal 704\n");
958 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
959 field = V4L2_FIELD_SEQ_TB;
960 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
961 dprintk(50, "pal 352A\n");
962 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
963 field = V4L2_FIELD_TOP;
964 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
965 dprintk(50, "pal 352B\n");
966 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
967 field = V4L2_FIELD_TOP;
968 } else {
969 dprintk(50, "pal 352C\n");
970 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
971 field = V4L2_FIELD_TOP;
972 }
973 }
974
975 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
976 f->fmt.pix.height, f->fmt.pix.field);
977 f->fmt.pix.field = field;
978 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
979 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
980 return 0;
981}
982
983static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
984 struct v4l2_format *f)
985{
986 struct s2255_fh *fh = priv;
987 const struct s2255_fmt *fmt;
988 struct videobuf_queue *q = &fh->vb_vidq;
989 int ret;
990 int norm;
991
992 ret = vidioc_try_fmt_vid_cap(file, fh, f);
993
994 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300995 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -0300996
997 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
998
999 if (fmt == NULL)
1000 return -EINVAL;
1001
1002 mutex_lock(&q->vb_lock);
1003
1004 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1005 dprintk(1, "queue busy\n");
1006 ret = -EBUSY;
1007 goto out_s_fmt;
1008 }
1009
1010 if (res_locked(fh->dev, fh)) {
1011 dprintk(1, "can't change format after started\n");
1012 ret = -EBUSY;
1013 goto out_s_fmt;
1014 }
1015
1016 fh->fmt = fmt;
1017 fh->width = f->fmt.pix.width;
1018 fh->height = f->fmt.pix.height;
1019 fh->vb_vidq.field = f->fmt.pix.field;
1020 fh->type = f->type;
1021 norm = norm_minw(fh->dev->vdev[fh->channel]);
1022 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1023 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
1024 fh->mode.scale = SCALE_4CIFS;
1025 else
1026 fh->mode.scale = SCALE_2CIFS;
1027
1028 } else {
1029 fh->mode.scale = SCALE_1CIFS;
1030 }
1031
1032 /* color mode */
1033 switch (fh->fmt->fourcc) {
1034 case V4L2_PIX_FMT_GREY:
1035 fh->mode.color = COLOR_Y8;
1036 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001037 case V4L2_PIX_FMT_JPEG:
1038 fh->mode.color = COLOR_JPG | (50 << 8);
1039 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001040 case V4L2_PIX_FMT_YUV422P:
1041 fh->mode.color = COLOR_YUVPL;
1042 break;
1043 case V4L2_PIX_FMT_YUYV:
1044 case V4L2_PIX_FMT_UYVY:
1045 default:
1046 fh->mode.color = COLOR_YUVPK;
1047 break;
1048 }
1049 ret = 0;
1050out_s_fmt:
1051 mutex_unlock(&q->vb_lock);
1052 return ret;
1053}
1054
1055static int vidioc_reqbufs(struct file *file, void *priv,
1056 struct v4l2_requestbuffers *p)
1057{
1058 int rc;
1059 struct s2255_fh *fh = priv;
1060 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1061 return rc;
1062}
1063
1064static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1065{
1066 int rc;
1067 struct s2255_fh *fh = priv;
1068 rc = videobuf_querybuf(&fh->vb_vidq, p);
1069 return rc;
1070}
1071
1072static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1073{
1074 int rc;
1075 struct s2255_fh *fh = priv;
1076 rc = videobuf_qbuf(&fh->vb_vidq, p);
1077 return rc;
1078}
1079
1080static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1081{
1082 int rc;
1083 struct s2255_fh *fh = priv;
1084 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1085 return rc;
1086}
1087
1088#ifdef CONFIG_VIDEO_V4L1_COMPAT
1089static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1090{
1091 struct s2255_fh *fh = priv;
1092
1093 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1094}
1095#endif
1096
1097/* write to the configuration pipe, synchronously */
1098static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1099 int size)
1100{
1101 int pipe;
1102 int done;
1103 long retval = -1;
1104 if (udev) {
1105 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1106 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1107 }
1108 return retval;
1109}
1110
1111static u32 get_transfer_size(struct s2255_mode *mode)
1112{
1113 int linesPerFrame = LINE_SZ_DEF;
1114 int pixelsPerLine = NUM_LINES_DEF;
1115 u32 outImageSize;
1116 u32 usbInSize;
1117 unsigned int mask_mult;
1118
1119 if (mode == NULL)
1120 return 0;
1121
1122 if (mode->format == FORMAT_NTSC) {
1123 switch (mode->scale) {
1124 case SCALE_4CIFS:
1125 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1126 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1127 break;
1128 case SCALE_2CIFS:
1129 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1130 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1131 break;
1132 case SCALE_1CIFS:
1133 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1134 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1135 break;
1136 default:
1137 break;
1138 }
1139 } else if (mode->format == FORMAT_PAL) {
1140 switch (mode->scale) {
1141 case SCALE_4CIFS:
1142 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1143 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1144 break;
1145 case SCALE_2CIFS:
1146 linesPerFrame = NUM_LINES_2CIFS_PAL;
1147 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1148 break;
1149 case SCALE_1CIFS:
1150 linesPerFrame = NUM_LINES_1CIFS_PAL;
1151 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1152 break;
1153 default:
1154 break;
1155 }
1156 }
1157 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001158 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001159 /* 2 bytes/pixel if not monochrome */
1160 outImageSize *= 2;
1161 }
1162
1163 /* total bytes to send including prefix and 4K padding;
1164 must be a multiple of USB_READ_SIZE */
1165 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1166 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1167 /* if size not a multiple of USB_READ_SIZE */
1168 if (usbInSize & ~mask_mult)
1169 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1170 return usbInSize;
1171}
1172
1173static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1174{
1175 struct device *dev = &sdev->udev->dev;
1176 dev_info(dev, "------------------------------------------------\n");
1177 dev_info(dev, "verify mode\n");
1178 dev_info(dev, "format: %d\n", mode->format);
1179 dev_info(dev, "scale: %d\n", mode->scale);
1180 dev_info(dev, "fdec: %d\n", mode->fdec);
1181 dev_info(dev, "color: %d\n", mode->color);
1182 dev_info(dev, "bright: 0x%x\n", mode->bright);
1183 dev_info(dev, "restart: 0x%x\n", mode->restart);
1184 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1185 dev_info(dev, "single: 0x%x\n", mode->single);
1186 dev_info(dev, "------------------------------------------------\n");
1187}
1188
1189/*
1190 * set mode is the function which controls the DSP.
1191 * the restart parameter in struct s2255_mode should be set whenever
1192 * the image size could change via color format, video system or image
1193 * size.
1194 * When the restart parameter is set, we sleep for ONE frame to allow the
1195 * DSP time to get the new frame
1196 */
1197static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1198 struct s2255_mode *mode)
1199{
1200 int res;
1201 u32 *buffer;
1202 unsigned long chn_rev;
1203
Dean Anderson14d96262008-08-25 13:58:55 -03001204 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001205 chn_rev = G_chnmap[chn];
1206 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1207 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1208 dev->mode[chn].scale);
1209 dprintk(2, "mode contrast %x\n", mode->contrast);
1210
1211 /* save the mode */
1212 dev->mode[chn] = *mode;
1213 dev->req_image_size[chn] = get_transfer_size(mode);
1214 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1215
1216 buffer = kzalloc(512, GFP_KERNEL);
1217 if (buffer == NULL) {
1218 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001219 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001220 return -ENOMEM;
1221 }
1222
1223 /* set the mode */
1224 buffer[0] = IN_DATA_TOKEN;
1225 buffer[1] = (u32) chn_rev;
1226 buffer[2] = CMD_SET_MODE;
1227 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1228 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1229 if (debug)
1230 dump_verify_mode(dev, mode);
1231 kfree(buffer);
1232 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1233
1234 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001235 if (mode->restart) {
1236 dev->setmode_ready[chn] = 0;
1237 wait_event_timeout(dev->wait_setmode[chn],
1238 (dev->setmode_ready[chn] != 0),
1239 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1240 if (dev->setmode_ready[chn] != 1) {
1241 printk(KERN_DEBUG "s2255: no set mode response\n");
1242 res = -EFAULT;
1243 }
1244 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001245
1246 /* clear the restart flag */
1247 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001248 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001249 return res;
1250}
1251
1252static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1253{
1254 int res;
1255 struct s2255_fh *fh = priv;
1256 struct s2255_dev *dev = fh->dev;
1257 struct s2255_mode *new_mode;
1258 struct s2255_mode *old_mode;
1259 int chn;
1260 int j;
1261 dprintk(4, "%s\n", __func__);
1262 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1263 dev_err(&dev->udev->dev, "invalid fh type0\n");
1264 return -EINVAL;
1265 }
1266 if (i != fh->type) {
1267 dev_err(&dev->udev->dev, "invalid fh type1\n");
1268 return -EINVAL;
1269 }
1270
1271 if (!res_get(dev, fh)) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001272 dev_err(&dev->udev->dev, "s2255: stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001273 return -EBUSY;
1274 }
1275
1276 /* send a set mode command everytime with restart.
1277 in case we switch resolutions or other parameters */
1278 chn = fh->channel;
1279 new_mode = &fh->mode;
1280 old_mode = &fh->dev->mode[chn];
1281
1282 if (new_mode->color != old_mode->color)
1283 new_mode->restart = 1;
1284 else if (new_mode->scale != old_mode->scale)
1285 new_mode->restart = 1;
1286 else if (new_mode->format != old_mode->format)
1287 new_mode->restart = 1;
1288
1289 s2255_set_mode(dev, chn, new_mode);
1290 new_mode->restart = 0;
1291 *old_mode = *new_mode;
1292 dev->cur_fmt[chn] = fh->fmt;
1293 dprintk(1, "%s[%d]\n", __func__, chn);
1294 dev->last_frame[chn] = -1;
1295 dev->bad_payload[chn] = 0;
1296 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001297 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001298 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001299 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001300 dev->buffer[chn].frame[j].cur_size = 0;
1301 }
1302 res = videobuf_streamon(&fh->vb_vidq);
1303 if (res == 0) {
1304 s2255_start_acquire(dev, chn);
1305 dev->b_acquire[chn] = 1;
1306 } else {
1307 res_free(dev, fh);
1308 }
1309 return res;
1310}
1311
1312static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1313{
1314 int res;
1315 struct s2255_fh *fh = priv;
1316 struct s2255_dev *dev = fh->dev;
1317
1318 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1319 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1320 printk(KERN_ERR "invalid fh type0\n");
1321 return -EINVAL;
1322 }
1323 if (i != fh->type) {
1324 printk(KERN_ERR "invalid type i\n");
1325 return -EINVAL;
1326 }
1327 s2255_stop_acquire(dev, fh->channel);
1328 res = videobuf_streamoff(&fh->vb_vidq);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001329 if (res < 0)
1330 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03001331 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001332 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001333}
1334
1335static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1336{
1337 struct s2255_fh *fh = priv;
1338 struct s2255_mode *mode;
1339 struct videobuf_queue *q = &fh->vb_vidq;
1340 int ret = 0;
1341
1342 mutex_lock(&q->vb_lock);
1343 if (videobuf_queue_is_busy(q)) {
1344 dprintk(1, "queue busy\n");
1345 ret = -EBUSY;
1346 goto out_s_std;
1347 }
1348
1349 if (res_locked(fh->dev, fh)) {
1350 dprintk(1, "can't change standard after started\n");
1351 ret = -EBUSY;
1352 goto out_s_std;
1353 }
1354 mode = &fh->mode;
1355
1356 if (*i & V4L2_STD_NTSC) {
1357 dprintk(4, "vidioc_s_std NTSC\n");
1358 mode->format = FORMAT_NTSC;
1359 } else if (*i & V4L2_STD_PAL) {
1360 dprintk(4, "vidioc_s_std PAL\n");
1361 mode->format = FORMAT_PAL;
1362 } else {
1363 ret = -EINVAL;
1364 }
1365out_s_std:
1366 mutex_unlock(&q->vb_lock);
1367 return ret;
1368}
1369
1370/* Sensoray 2255 is a multiple channel capture device.
1371 It does not have a "crossbar" of inputs.
1372 We use one V4L device per channel. The user must
1373 be aware that certain combinations are not allowed.
1374 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1375 at once in color(you can do full fps on 4 channels with greyscale.
1376*/
1377static int vidioc_enum_input(struct file *file, void *priv,
1378 struct v4l2_input *inp)
1379{
1380 if (inp->index != 0)
1381 return -EINVAL;
1382
1383 inp->type = V4L2_INPUT_TYPE_CAMERA;
1384 inp->std = S2255_NORMS;
1385 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001386 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001387}
1388
1389static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1390{
1391 *i = 0;
1392 return 0;
1393}
1394static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1395{
1396 if (i > 0)
1397 return -EINVAL;
1398 return 0;
1399}
1400
1401/* --- controls ---------------------------------------------- */
1402static int vidioc_queryctrl(struct file *file, void *priv,
1403 struct v4l2_queryctrl *qc)
1404{
1405 int i;
1406
1407 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1408 if (qc->id && qc->id == s2255_qctrl[i].id) {
1409 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001410 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001411 }
1412
1413 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1414 return -EINVAL;
1415}
1416
1417static int vidioc_g_ctrl(struct file *file, void *priv,
1418 struct v4l2_control *ctrl)
1419{
1420 int i;
1421
1422 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1423 if (ctrl->id == s2255_qctrl[i].id) {
1424 ctrl->value = qctl_regs[i];
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001425 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001426 }
1427 dprintk(4, "g_ctrl -EINVAL\n");
1428
1429 return -EINVAL;
1430}
1431
1432static int vidioc_s_ctrl(struct file *file, void *priv,
1433 struct v4l2_control *ctrl)
1434{
1435 int i;
1436 struct s2255_fh *fh = priv;
1437 struct s2255_dev *dev = fh->dev;
1438 struct s2255_mode *mode;
1439 mode = &fh->mode;
1440 dprintk(4, "vidioc_s_ctrl\n");
1441 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1442 if (ctrl->id == s2255_qctrl[i].id) {
1443 if (ctrl->value < s2255_qctrl[i].minimum ||
1444 ctrl->value > s2255_qctrl[i].maximum)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001445 return -ERANGE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001446
1447 qctl_regs[i] = ctrl->value;
1448 /* update the mode to the corresponding value */
1449 switch (ctrl->id) {
1450 case V4L2_CID_BRIGHTNESS:
1451 mode->bright = ctrl->value;
1452 break;
1453 case V4L2_CID_CONTRAST:
1454 mode->contrast = ctrl->value;
1455 break;
1456 case V4L2_CID_HUE:
1457 mode->hue = ctrl->value;
1458 break;
1459 case V4L2_CID_SATURATION:
1460 mode->saturation = ctrl->value;
1461 break;
1462 }
1463 mode->restart = 0;
1464 /* set mode here. Note: stream does not need restarted.
1465 some V4L programs restart stream unnecessarily
1466 after a s_crtl.
1467 */
1468 s2255_set_mode(dev, fh->channel, mode);
1469 return 0;
1470 }
1471 }
1472 return -EINVAL;
1473}
1474
1475static int s2255_open(struct inode *inode, struct file *file)
1476{
1477 int minor = iminor(inode);
1478 struct s2255_dev *h, *dev = NULL;
1479 struct s2255_fh *fh;
1480 struct list_head *list;
1481 enum v4l2_buf_type type = 0;
1482 int i = 0;
1483 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001484 int state;
Dean Anderson38f993a2008-06-26 23:15:51 -03001485 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1486
Hans Verkuild56dc612008-07-30 08:43:36 -03001487 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001488 list_for_each(list, &s2255_devlist) {
1489 h = list_entry(list, struct s2255_dev, s2255_devlist);
1490 for (i = 0; i < MAX_CHANNELS; i++) {
1491 if (h->vdev[i]->minor == minor) {
1492 cur_channel = i;
1493 dev = h;
1494 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1495 }
1496 }
1497 }
1498
1499 if ((NULL == dev) || (cur_channel == -1)) {
Hans Verkuild56dc612008-07-30 08:43:36 -03001500 unlock_kernel();
Dean Anderson14d96262008-08-25 13:58:55 -03001501 printk(KERN_INFO "s2255: openv4l no dev\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001502 return -ENODEV;
1503 }
1504
Dean Anderson14d96262008-08-25 13:58:55 -03001505 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1506 unlock_kernel();
1507 printk(KERN_INFO "disconnecting\n");
1508 return -ENODEV;
1509 }
1510 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001511 mutex_lock(&dev->open_lock);
1512
1513 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001514 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001515
Dean Anderson14d96262008-08-25 13:58:55 -03001516 switch (atomic_read(&dev->fw_data->fw_state)) {
1517 case S2255_FW_FAILED:
Dean Anderson38f993a2008-06-26 23:15:51 -03001518 err("2255 firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001519 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001520 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001521 ((atomic_read(&dev->fw_data->fw_state)
1522 == S2255_FW_SUCCESS) ||
1523 (atomic_read(&dev->fw_data->fw_state)
1524 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001525 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001526 break;
1527 case S2255_FW_NOTLOADED:
1528 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001529 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1530 driver loaded and then device immediately opened */
1531 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1532 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001533 ((atomic_read(&dev->fw_data->fw_state)
1534 == S2255_FW_SUCCESS) ||
1535 (atomic_read(&dev->fw_data->fw_state)
1536 == S2255_FW_DISCONNECTING)),
1537 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1538 break;
1539 case S2255_FW_SUCCESS:
1540 default:
1541 break;
1542 }
1543 state = atomic_read(&dev->fw_data->fw_state);
1544 if (state != S2255_FW_SUCCESS) {
1545 int rc;
1546 switch (state) {
1547 case S2255_FW_FAILED:
1548 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1549 rc = -ENODEV;
1550 break;
1551 case S2255_FW_DISCONNECTING:
1552 printk(KERN_INFO "%s: disconnecting\n", __func__);
1553 rc = -ENODEV;
1554 break;
1555 case S2255_FW_LOADED_DSPWAIT:
1556 case S2255_FW_NOTLOADED:
1557 printk(KERN_INFO "%s: firmware not loaded yet"
1558 "please try again later\n",
1559 __func__);
1560 rc = -EAGAIN;
1561 break;
1562 default:
1563 printk(KERN_INFO "%s: unknown state\n", __func__);
1564 rc = -EFAULT;
1565 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001566 }
Dean Anderson14d96262008-08-25 13:58:55 -03001567 dev->users[cur_channel]--;
1568 mutex_unlock(&dev->open_lock);
1569 kref_put(&dev->kref, s2255_destroy);
1570 unlock_kernel();
1571 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001572 }
1573
1574 /* allocate + initialize per filehandle data */
1575 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1576 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001577 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001578 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001579 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001580 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001581 return -ENOMEM;
1582 }
1583
1584 file->private_data = fh;
1585 fh->dev = dev;
1586 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1587 fh->mode = dev->mode[cur_channel];
1588 fh->fmt = dev->cur_fmt[cur_channel];
1589 /* default 4CIF NTSC */
1590 fh->width = LINE_SZ_4CIFS_NTSC;
1591 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1592 fh->channel = cur_channel;
1593
Dean Anderson14d96262008-08-25 13:58:55 -03001594 /* configure channel to default state */
1595 if (!dev->chn_configured[cur_channel]) {
1596 s2255_set_mode(dev, cur_channel, &fh->mode);
1597 dev->chn_configured[cur_channel] = 1;
1598 }
1599
1600
Dean Anderson38f993a2008-06-26 23:15:51 -03001601 /* Put all controls at a sane state */
1602 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1603 qctl_regs[i] = s2255_qctrl[i].default_value;
1604
1605 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1606 minor, v4l2_type_names[type], dev->users[cur_channel]);
1607 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1608 (unsigned long)fh, (unsigned long)dev,
1609 (unsigned long)&dev->vidq[cur_channel]);
1610 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1611 list_empty(&dev->vidq[cur_channel].active));
1612
1613 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1614 NULL, &dev->slock,
1615 fh->type,
1616 V4L2_FIELD_INTERLACED,
1617 sizeof(struct s2255_buffer), fh);
1618
Dean Anderson38f993a2008-06-26 23:15:51 -03001619 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001620 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001621 return 0;
1622}
1623
1624
1625static unsigned int s2255_poll(struct file *file,
1626 struct poll_table_struct *wait)
1627{
1628 struct s2255_fh *fh = file->private_data;
1629 int rc;
1630 dprintk(100, "%s\n", __func__);
1631
1632 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1633 return POLLERR;
1634
1635 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1636 return rc;
1637}
1638
1639static void s2255_destroy(struct kref *kref)
1640{
1641 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001642 struct list_head *list;
1643 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001644 if (!dev) {
1645 printk(KERN_ERR "s2255drv: kref problem\n");
1646 return;
1647 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001648 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1649 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001650 for (i = 0; i < MAX_CHANNELS; i++) {
1651 dev->setmode_ready[i] = 1;
1652 wake_up(&dev->wait_setmode[i]);
1653 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001654 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001655 /* reset the DSP so firmware can be reload next time */
1656 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001657 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001658 /* board shutdown stops the read pipe if it is running */
1659 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001660 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001661 del_timer(&dev->timer); /* only started in .probe and .open */
1662
Dean Anderson38f993a2008-06-26 23:15:51 -03001663 if (dev->fw_data->fw_urb) {
1664 dprintk(2, "kill fw_urb\n");
1665 usb_kill_urb(dev->fw_data->fw_urb);
1666 usb_free_urb(dev->fw_data->fw_urb);
1667 dev->fw_data->fw_urb = NULL;
1668 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001669 if (dev->fw_data->fw)
1670 release_firmware(dev->fw_data->fw);
1671 kfree(dev->fw_data->pfw_data);
1672 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001673 usb_put_dev(dev->udev);
1674 dprintk(1, "%s", __func__);
1675 kfree(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03001676
1677 while (!list_empty(&s2255_devlist)) {
1678 list = s2255_devlist.next;
1679 list_del(list);
1680 }
1681 mutex_unlock(&dev->open_lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001682}
1683
1684static int s2255_close(struct inode *inode, struct file *file)
1685{
1686 struct s2255_fh *fh = file->private_data;
1687 struct s2255_dev *dev = fh->dev;
1688 int minor = iminor(inode);
1689 if (!dev)
1690 return -ENODEV;
1691
1692 mutex_lock(&dev->open_lock);
1693
Dean Andersonf78d92c2008-07-22 14:43:27 -03001694 /* turn off stream */
1695 if (res_check(fh)) {
1696 if (dev->b_acquire[fh->channel])
1697 s2255_stop_acquire(dev, fh->channel);
1698 videobuf_streamoff(&fh->vb_vidq);
1699 res_free(dev, fh);
1700 }
1701
Dean Anderson38f993a2008-06-26 23:15:51 -03001702 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001703 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001704
Dean Anderson38f993a2008-06-26 23:15:51 -03001705 mutex_unlock(&dev->open_lock);
1706
1707 kref_put(&dev->kref, s2255_destroy);
1708 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1709 minor, dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001710 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001711 return 0;
1712}
1713
1714static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1715{
1716 struct s2255_fh *fh = file->private_data;
1717 int ret;
1718
1719 if (!fh)
1720 return -ENODEV;
1721 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1722
1723 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1724
1725 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1726 (unsigned long)vma->vm_start,
1727 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1728
1729 return ret;
1730}
1731
1732static const struct file_operations s2255_fops_v4l = {
1733 .owner = THIS_MODULE,
1734 .open = s2255_open,
1735 .release = s2255_close,
1736 .poll = s2255_poll,
1737 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1738 .compat_ioctl = v4l_compat_ioctl32,
1739 .mmap = s2255_mmap_v4l,
1740 .llseek = no_llseek,
1741};
1742
Hans Verkuila3998102008-07-21 02:57:38 -03001743static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001744 .vidioc_querycap = vidioc_querycap,
1745 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1746 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1747 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1748 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1749 .vidioc_reqbufs = vidioc_reqbufs,
1750 .vidioc_querybuf = vidioc_querybuf,
1751 .vidioc_qbuf = vidioc_qbuf,
1752 .vidioc_dqbuf = vidioc_dqbuf,
1753 .vidioc_s_std = vidioc_s_std,
1754 .vidioc_enum_input = vidioc_enum_input,
1755 .vidioc_g_input = vidioc_g_input,
1756 .vidioc_s_input = vidioc_s_input,
1757 .vidioc_queryctrl = vidioc_queryctrl,
1758 .vidioc_g_ctrl = vidioc_g_ctrl,
1759 .vidioc_s_ctrl = vidioc_s_ctrl,
1760 .vidioc_streamon = vidioc_streamon,
1761 .vidioc_streamoff = vidioc_streamoff,
1762#ifdef CONFIG_VIDEO_V4L1_COMPAT
1763 .vidiocgmbuf = vidioc_cgmbuf,
1764#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001765};
1766
1767static struct video_device template = {
1768 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001769 .fops = &s2255_fops_v4l,
1770 .ioctl_ops = &s2255_ioctl_ops,
1771 .minor = -1,
1772 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001773 .tvnorms = S2255_NORMS,
1774 .current_norm = V4L2_STD_NTSC_M,
1775};
1776
1777static int s2255_probe_v4l(struct s2255_dev *dev)
1778{
1779 int ret;
1780 int i;
1781 int cur_nr = video_nr;
1782
1783 /* initialize all video 4 linux */
1784 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1785 /* register 4 video devices */
1786 for (i = 0; i < MAX_CHANNELS; i++) {
1787 INIT_LIST_HEAD(&dev->vidq[i].active);
1788 dev->vidq[i].dev = dev;
1789 dev->vidq[i].channel = i;
1790 dev->vidq[i].kthread = NULL;
1791 /* register 4 video devices */
1792 dev->vdev[i] = video_device_alloc();
1793 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001794 dev->vdev[i]->parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001795 if (video_nr == -1)
1796 ret = video_register_device(dev->vdev[i],
1797 VFL_TYPE_GRABBER,
1798 video_nr);
1799 else
1800 ret = video_register_device(dev->vdev[i],
1801 VFL_TYPE_GRABBER,
1802 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001803 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001804
1805 if (ret != 0) {
1806 dev_err(&dev->udev->dev,
1807 "failed to register video device!\n");
1808 return ret;
1809 }
1810 }
1811 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1812 return ret;
1813}
1814
1815static void s2255_exit_v4l(struct s2255_dev *dev)
1816{
Dean Anderson14d96262008-08-25 13:58:55 -03001817
Dean Anderson38f993a2008-06-26 23:15:51 -03001818 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001819 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001820 if (-1 != dev->vdev[i]->minor) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001821 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001822 printk(KERN_INFO "s2255 unregistered\n");
1823 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001824 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001825 printk(KERN_INFO "s2255 released\n");
1826 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001827 }
1828}
1829
1830/* this function moves the usb stream read pipe data
1831 * into the system buffers.
1832 * returns 0 on success, EAGAIN if more data to process( call this
1833 * function again).
1834 *
1835 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001836 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001837 * bytes 4-7: channel: 0-3
1838 * bytes 8-11: payload size: size of the frame
1839 * bytes 12-payloadsize+12: frame data
1840 */
1841static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1842{
Dean Anderson38f993a2008-06-26 23:15:51 -03001843 char *pdest;
1844 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001845 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001846 char *psrc;
1847 unsigned long copy_size;
1848 unsigned long size;
1849 s32 idx = -1;
1850 struct s2255_framei *frm;
1851 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001852
Dean Anderson38f993a2008-06-26 23:15:51 -03001853 dprintk(100, "buffer to user\n");
1854
1855 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001856 frm = &dev->buffer[dev->cc].frame[idx];
1857
Dean Anderson14d96262008-08-25 13:58:55 -03001858 if (frm->ulState == S2255_READ_IDLE) {
1859 int jj;
1860 unsigned int cc;
1861 s32 *pdword;
1862 int payload;
1863 /* search for marker codes */
1864 pdata = (unsigned char *)pipe_info->transfer_buffer;
1865 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1866 switch (*(s32 *) pdata) {
1867 case S2255_MARKER_FRAME:
1868 pdword = (s32 *)pdata;
1869 dprintk(4, "found frame marker at offset:"
1870 " %d [%x %x]\n", jj, pdata[0],
1871 pdata[1]);
1872 offset = jj + PREFIX_SIZE;
1873 bframe = 1;
1874 cc = pdword[1];
1875 if (cc >= MAX_CHANNELS) {
1876 printk(KERN_ERR
1877 "bad channel\n");
1878 return -EINVAL;
1879 }
1880 /* reverse it */
1881 dev->cc = G_chnmap[cc];
1882 payload = pdword[3];
1883 if (payload > dev->req_image_size[dev->cc]) {
1884 dev->bad_payload[dev->cc]++;
1885 /* discard the bad frame */
1886 return -EINVAL;
1887 }
1888 dev->pkt_size[dev->cc] = payload;
1889 dev->jpg_size[dev->cc] = pdword[4];
1890 break;
1891 case S2255_MARKER_RESPONSE:
1892 pdword = (s32 *)pdata;
1893 pdata += DEF_USB_BLOCK;
1894 jj += DEF_USB_BLOCK;
1895 if (pdword[1] >= MAX_CHANNELS)
1896 break;
1897 cc = G_chnmap[pdword[1]];
1898 if (!(cc >= 0 && cc < MAX_CHANNELS))
1899 break;
1900 switch (pdword[2]) {
1901 case 0x01:
1902 /* check if channel valid */
1903 /* set mode ready */
1904 dev->setmode_ready[cc] = 1;
1905 wake_up(&dev->wait_setmode[cc]);
1906 dprintk(5, "setmode ready %d\n", cc);
1907 break;
1908 case 0x10:
1909
1910 dev->chn_ready |= (1 << cc);
1911 if ((dev->chn_ready & 0x0f) != 0x0f)
1912 break;
1913 /* all channels ready */
1914 printk(KERN_INFO "s2255: fw loaded\n");
1915 atomic_set(&dev->fw_data->fw_state,
1916 S2255_FW_SUCCESS);
1917 wake_up(&dev->fw_data->wait_fw);
1918 break;
1919 default:
1920 printk(KERN_INFO "s2255 unknwn resp\n");
1921 }
1922 default:
1923 pdata++;
1924 break;
1925 }
1926 if (bframe)
1927 break;
1928 } /* for */
1929 if (!bframe)
1930 return -EINVAL;
1931 }
1932
1933
1934 idx = dev->cur_frame[dev->cc];
1935 frm = &dev->buffer[dev->cc].frame[idx];
1936
1937 /* search done. now find out if should be acquiring on this channel */
1938 if (!dev->b_acquire[dev->cc]) {
1939 /* we found a frame, but this channel is turned off */
1940 frm->ulState = S2255_READ_IDLE;
1941 return -EINVAL;
1942 }
1943
1944 if (frm->ulState == S2255_READ_IDLE) {
1945 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03001946 frm->cur_size = 0;
1947 }
1948
Dean Anderson14d96262008-08-25 13:58:55 -03001949 /* skip the marker 512 bytes (and offset if out of sync) */
1950 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1951
Dean Anderson38f993a2008-06-26 23:15:51 -03001952
1953 if (frm->lpvbits == NULL) {
1954 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1955 frm, dev, dev->cc, idx);
1956 return -ENOMEM;
1957 }
1958
1959 pdest = frm->lpvbits + frm->cur_size;
1960
Dean Anderson14d96262008-08-25 13:58:55 -03001961 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03001962
Dean Anderson14d96262008-08-25 13:58:55 -03001963 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001964
Dean Anderson14d96262008-08-25 13:58:55 -03001965 /* sanity check on pdest */
1966 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
1967 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03001968
Dean Anderson38f993a2008-06-26 23:15:51 -03001969 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03001970 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03001971
Dean Anderson14d96262008-08-25 13:58:55 -03001972 if (frm->cur_size >= size) {
1973
Dean Anderson38f993a2008-06-26 23:15:51 -03001974 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001975 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
1976 cc, idx);
1977 dev->last_frame[cc] = dev->cur_frame[cc];
1978 dev->cur_frame[cc]++;
1979 /* end of system frame ring buffer, start at zero */
1980 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
1981 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
1982 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001983 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03001984 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03001985 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001986 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03001987 frm->ulState = S2255_READ_IDLE;
1988 frm->cur_size = 0;
1989
Dean Anderson38f993a2008-06-26 23:15:51 -03001990 }
1991 /* done successfully */
1992 return 0;
1993}
1994
1995static void s2255_read_video_callback(struct s2255_dev *dev,
1996 struct s2255_pipeinfo *pipe_info)
1997{
1998 int res;
1999 dprintk(50, "callback read video \n");
2000
2001 if (dev->cc >= MAX_CHANNELS) {
2002 dev->cc = 0;
2003 dev_err(&dev->udev->dev, "invalid channel\n");
2004 return;
2005 }
2006 /* otherwise copy to the system buffers */
2007 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002008 if (res != 0)
2009 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002010
2011 dprintk(50, "callback read video done\n");
2012 return;
2013}
2014
2015static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2016 u16 Index, u16 Value, void *TransferBuffer,
2017 s32 TransferBufferLength, int bOut)
2018{
2019 int r;
2020 if (!bOut) {
2021 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2022 Request,
2023 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2024 USB_DIR_IN,
2025 Value, Index, TransferBuffer,
2026 TransferBufferLength, HZ * 5);
2027 } else {
2028 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2029 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2030 Value, Index, TransferBuffer,
2031 TransferBufferLength, HZ * 5);
2032 }
2033 return r;
2034}
2035
2036/*
2037 * retrieve FX2 firmware version. future use.
2038 * @param dev pointer to device extension
2039 * @return -1 for fail, else returns firmware version as an int(16 bits)
2040 */
2041static int s2255_get_fx2fw(struct s2255_dev *dev)
2042{
2043 int fw;
2044 int ret;
2045 unsigned char transBuffer[64];
2046 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2047 S2255_VR_IN);
2048 if (ret < 0)
2049 dprintk(2, "get fw error: %x\n", ret);
2050 fw = transBuffer[0] + (transBuffer[1] << 8);
2051 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2052 return fw;
2053}
2054
2055/*
2056 * Create the system ring buffer to copy frames into from the
2057 * usb read pipe.
2058 */
2059static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2060{
2061 unsigned long i;
2062 unsigned long reqsize;
2063 dprintk(1, "create sys buffers\n");
2064 if (chn >= MAX_CHANNELS)
2065 return -1;
2066
2067 dev->buffer[chn].dwFrames = SYS_FRAMES;
2068
2069 /* always allocate maximum size(PAL) for system buffers */
2070 reqsize = SYS_FRAMES_MAXSIZE;
2071
2072 if (reqsize > SYS_FRAMES_MAXSIZE)
2073 reqsize = SYS_FRAMES_MAXSIZE;
2074
2075 for (i = 0; i < SYS_FRAMES; i++) {
2076 /* allocate the frames */
2077 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2078
2079 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2080 &dev->buffer[chn].frame[i], chn, i,
2081 dev->buffer[chn].frame[i].lpvbits);
2082 dev->buffer[chn].frame[i].size = reqsize;
2083 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2084 printk(KERN_INFO "out of memory. using less frames\n");
2085 dev->buffer[chn].dwFrames = i;
2086 break;
2087 }
2088 }
2089
2090 /* make sure internal states are set */
2091 for (i = 0; i < SYS_FRAMES; i++) {
2092 dev->buffer[chn].frame[i].ulState = 0;
2093 dev->buffer[chn].frame[i].cur_size = 0;
2094 }
2095
2096 dev->cur_frame[chn] = 0;
2097 dev->last_frame[chn] = -1;
2098 return 0;
2099}
2100
2101static int s2255_release_sys_buffers(struct s2255_dev *dev,
2102 unsigned long channel)
2103{
2104 unsigned long i;
2105 dprintk(1, "release sys buffers\n");
2106 for (i = 0; i < SYS_FRAMES; i++) {
2107 if (dev->buffer[channel].frame[i].lpvbits) {
2108 dprintk(1, "vfree %p\n",
2109 dev->buffer[channel].frame[i].lpvbits);
2110 vfree(dev->buffer[channel].frame[i].lpvbits);
2111 }
2112 dev->buffer[channel].frame[i].lpvbits = NULL;
2113 }
2114 return 0;
2115}
2116
2117static int s2255_board_init(struct s2255_dev *dev)
2118{
2119 int j;
2120 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2121 int fw_ver;
2122 dprintk(4, "board init: %p", dev);
2123
2124 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2125 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2126
2127 memset(pipe, 0, sizeof(*pipe));
2128 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002129 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2130 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002131
Dean Anderson38f993a2008-06-26 23:15:51 -03002132 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2133 GFP_KERNEL);
2134 if (pipe->transfer_buffer == NULL) {
2135 dprintk(1, "out of memory!\n");
2136 return -ENOMEM;
2137 }
2138
2139 }
2140
2141 /* query the firmware */
2142 fw_ver = s2255_get_fx2fw(dev);
2143
2144 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2145 if (fw_ver < CUR_USB_FWVER)
2146 err("usb firmware not up to date %d\n", fw_ver);
2147
2148 for (j = 0; j < MAX_CHANNELS; j++) {
2149 dev->b_acquire[j] = 0;
2150 dev->mode[j] = mode_def;
2151 dev->cur_fmt[j] = &formats[0];
2152 dev->mode[j].restart = 1;
2153 dev->req_image_size[j] = get_transfer_size(&mode_def);
2154 dev->frame_count[j] = 0;
2155 /* create the system buffers */
2156 s2255_create_sys_buffers(dev, j);
2157 }
2158 /* start read pipe */
2159 s2255_start_readpipe(dev);
2160
2161 dprintk(1, "S2255: board initialized\n");
2162 return 0;
2163}
2164
2165static int s2255_board_shutdown(struct s2255_dev *dev)
2166{
2167 u32 i;
2168
2169 dprintk(1, "S2255: board shutdown: %p", dev);
2170
2171 for (i = 0; i < MAX_CHANNELS; i++) {
2172 if (dev->b_acquire[i])
2173 s2255_stop_acquire(dev, i);
2174 }
2175
2176 s2255_stop_readpipe(dev);
2177
2178 for (i = 0; i < MAX_CHANNELS; i++)
2179 s2255_release_sys_buffers(dev, i);
2180
2181 /* release transfer buffers */
2182 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2183 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2184 kfree(pipe->transfer_buffer);
2185 }
2186 return 0;
2187}
2188
2189static void read_pipe_completion(struct urb *purb)
2190{
2191 struct s2255_pipeinfo *pipe_info;
2192 struct s2255_dev *dev;
2193 int status;
2194 int pipe;
2195
2196 pipe_info = purb->context;
2197 dprintk(100, "read pipe completion %p, status %d\n", purb,
2198 purb->status);
2199 if (pipe_info == NULL) {
2200 err("no context !");
2201 return;
2202 }
2203
2204 dev = pipe_info->dev;
2205 if (dev == NULL) {
2206 err("no context !");
2207 return;
2208 }
2209 status = purb->status;
2210 if (status != 0) {
2211 dprintk(2, "read_pipe_completion: err\n");
2212 return;
2213 }
2214
2215 if (pipe_info->state == 0) {
2216 dprintk(2, "exiting USB pipe");
2217 return;
2218 }
2219
2220 s2255_read_video_callback(dev, pipe_info);
2221
2222 pipe_info->err_count = 0;
2223 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2224 /* reuse urb */
2225 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2226 pipe,
2227 pipe_info->transfer_buffer,
2228 pipe_info->cur_transfer_size,
2229 read_pipe_completion, pipe_info);
2230
2231 if (pipe_info->state != 0) {
2232 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2233 dev_err(&dev->udev->dev, "error submitting urb\n");
2234 usb_free_urb(pipe_info->stream_urb);
2235 }
2236 } else {
2237 dprintk(2, "read pipe complete state 0\n");
2238 }
2239 return;
2240}
2241
2242static int s2255_start_readpipe(struct s2255_dev *dev)
2243{
2244 int pipe;
2245 int retval;
2246 int i;
2247 struct s2255_pipeinfo *pipe_info = dev->pipes;
2248 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2249 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2250
2251 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2252 pipe_info->state = 1;
2253 pipe_info->buf_index = (u32) i;
2254 pipe_info->priority_set = 0;
2255 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2256 if (!pipe_info->stream_urb) {
2257 dev_err(&dev->udev->dev,
2258 "ReadStream: Unable to alloc URB");
2259 return -ENOMEM;
2260 }
2261 /* transfer buffer allocated in board_init */
2262 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2263 pipe,
2264 pipe_info->transfer_buffer,
2265 pipe_info->cur_transfer_size,
2266 read_pipe_completion, pipe_info);
2267
2268 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2269 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2270 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2271 if (retval) {
2272 printk(KERN_ERR "s2255: start read pipe failed\n");
2273 return retval;
2274 }
2275 }
2276
2277 return 0;
2278}
2279
2280/* starts acquisition process */
2281static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2282{
2283 unsigned char *buffer;
2284 int res;
2285 unsigned long chn_rev;
2286 int j;
2287 if (chn >= MAX_CHANNELS) {
2288 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2289 return -1;
2290 }
2291
2292 chn_rev = G_chnmap[chn];
2293 dprintk(1, "S2255: start acquire %lu \n", chn);
2294
2295 buffer = kzalloc(512, GFP_KERNEL);
2296 if (buffer == NULL) {
2297 dev_err(&dev->udev->dev, "out of mem\n");
2298 return -ENOMEM;
2299 }
2300
2301 dev->last_frame[chn] = -1;
2302 dev->bad_payload[chn] = 0;
2303 dev->cur_frame[chn] = 0;
2304 for (j = 0; j < SYS_FRAMES; j++) {
2305 dev->buffer[chn].frame[j].ulState = 0;
2306 dev->buffer[chn].frame[j].cur_size = 0;
2307 }
2308
2309 /* send the start command */
2310 *(u32 *) buffer = IN_DATA_TOKEN;
2311 *((u32 *) buffer + 1) = (u32) chn_rev;
2312 *((u32 *) buffer + 2) = (u32) CMD_START;
2313 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2314 if (res != 0)
2315 dev_err(&dev->udev->dev, "CMD_START error\n");
2316
2317 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2318 kfree(buffer);
2319 return 0;
2320}
2321
2322static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2323{
2324 unsigned char *buffer;
2325 int res;
2326 unsigned long chn_rev;
2327
2328 if (chn >= MAX_CHANNELS) {
2329 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2330 return -1;
2331 }
2332 chn_rev = G_chnmap[chn];
2333
2334 buffer = kzalloc(512, GFP_KERNEL);
2335 if (buffer == NULL) {
2336 dev_err(&dev->udev->dev, "out of mem\n");
2337 return -ENOMEM;
2338 }
2339
2340 /* send the stop command */
2341 dprintk(4, "stop acquire %lu\n", chn);
2342 *(u32 *) buffer = IN_DATA_TOKEN;
2343 *((u32 *) buffer + 1) = (u32) chn_rev;
2344 *((u32 *) buffer + 2) = CMD_STOP;
2345 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2346
2347 if (res != 0)
2348 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2349
2350 dprintk(4, "stop acquire: releasing states \n");
2351
2352 kfree(buffer);
2353 dev->b_acquire[chn] = 0;
2354
Dean Anderson14d96262008-08-25 13:58:55 -03002355 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002356}
2357
2358static void s2255_stop_readpipe(struct s2255_dev *dev)
2359{
2360 int j;
2361
2362 if (dev == NULL) {
2363 err("s2255: invalid device");
2364 return;
2365 }
2366 dprintk(4, "stop read pipe\n");
2367 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2368 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2369 if (pipe_info) {
2370 if (pipe_info->state == 0)
2371 continue;
2372 pipe_info->state = 0;
2373 pipe_info->prev_state = 1;
2374
2375 }
2376 }
2377
2378 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2379 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2380 if (pipe_info->stream_urb) {
2381 /* cancel urb */
2382 usb_kill_urb(pipe_info->stream_urb);
2383 usb_free_urb(pipe_info->stream_urb);
2384 pipe_info->stream_urb = NULL;
2385 }
2386 }
2387 dprintk(2, "s2255 stop read pipe: %d\n", j);
2388 return;
2389}
2390
Dean Anderson14d96262008-08-25 13:58:55 -03002391static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002392{
Dean Anderson14d96262008-08-25 13:58:55 -03002393 if (reset)
2394 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002395 dev->fw_data->fw_size = dev->fw_data->fw->size;
2396 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2397 memcpy(dev->fw_data->pfw_data,
2398 dev->fw_data->fw->data, CHUNK_SIZE);
2399 dev->fw_data->fw_loaded = CHUNK_SIZE;
2400 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2401 usb_sndbulkpipe(dev->udev, 2),
2402 dev->fw_data->pfw_data,
2403 CHUNK_SIZE, s2255_fwchunk_complete,
2404 dev->fw_data);
2405 mod_timer(&dev->timer, jiffies + HZ);
2406}
2407
2408/* standard usb probe function */
2409static int s2255_probe(struct usb_interface *interface,
2410 const struct usb_device_id *id)
2411{
2412 struct s2255_dev *dev = NULL;
2413 struct usb_host_interface *iface_desc;
2414 struct usb_endpoint_descriptor *endpoint;
2415 int i;
2416 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002417 __le32 *pdata;
2418 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002419
2420 dprintk(2, "s2255: probe\n");
2421
2422 /* allocate memory for our device state and initialize it to zero */
2423 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2424 if (dev == NULL) {
2425 err("s2255: out of memory");
2426 goto error;
2427 }
2428
2429 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2430 if (!dev->fw_data)
2431 goto error;
2432
2433 mutex_init(&dev->lock);
2434 mutex_init(&dev->open_lock);
2435
2436 /* grab usb_device and save it */
2437 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2438 if (dev->udev == NULL) {
2439 dev_err(&interface->dev, "null usb device\n");
2440 retval = -ENODEV;
2441 goto error;
2442 }
2443 kref_init(&dev->kref);
2444 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2445 dev->udev, interface);
2446 dev->interface = interface;
2447 /* set up the endpoint information */
2448 iface_desc = interface->cur_altsetting;
2449 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2450 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2451 endpoint = &iface_desc->endpoint[i].desc;
2452 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2453 /* we found the bulk in endpoint */
2454 dev->read_endpoint = endpoint->bEndpointAddress;
2455 }
2456 }
2457
2458 if (!dev->read_endpoint) {
2459 dev_err(&interface->dev, "Could not find bulk-in endpoint");
2460 goto error;
2461 }
2462
2463 /* set intfdata */
2464 usb_set_intfdata(interface, dev);
2465
2466 dprintk(100, "after intfdata %p\n", dev);
2467
2468 init_timer(&dev->timer);
2469 dev->timer.function = s2255_timer;
2470 dev->timer.data = (unsigned long)dev->fw_data;
2471
2472 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03002473 for (i = 0; i < MAX_CHANNELS; i++)
2474 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002475
2476
2477 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2478
2479 if (!dev->fw_data->fw_urb) {
2480 dev_err(&interface->dev, "out of memory!\n");
2481 goto error;
2482 }
2483 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2484 if (!dev->fw_data->pfw_data) {
2485 dev_err(&interface->dev, "out of memory!\n");
2486 goto error;
2487 }
2488 /* load the first chunk */
2489 if (request_firmware(&dev->fw_data->fw,
2490 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2491 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2492 goto error;
2493 }
Dean Anderson14d96262008-08-25 13:58:55 -03002494 /* check the firmware is valid */
2495 fw_size = dev->fw_data->fw->size;
2496 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002497
Dean Anderson14d96262008-08-25 13:58:55 -03002498 if (*pdata != S2255_FW_MARKER) {
2499 printk(KERN_INFO "Firmware invalid.\n");
2500 retval = -ENODEV;
2501 goto error;
2502 } else {
2503 /* make sure firmware is the latest */
2504 __le32 *pRel;
2505 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2506 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2507 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002508 /* loads v4l specific */
2509 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002510 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002511 /* load 2255 board specific */
2512 s2255_board_init(dev);
2513
2514 dprintk(4, "before probe done %p\n", dev);
2515 spin_lock_init(&dev->slock);
2516
Dean Anderson14d96262008-08-25 13:58:55 -03002517 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002518 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2519 return 0;
2520error:
2521 return retval;
2522}
2523
2524/* disconnect routine. when board is removed physically or with rmmod */
2525static void s2255_disconnect(struct usb_interface *interface)
2526{
2527 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002528 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002529 dprintk(1, "s2255: disconnect interface %p\n", interface);
2530 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002531
2532 /*
2533 * wake up any of the timers to allow open_lock to be
2534 * acquired sooner
2535 */
2536 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2537 wake_up(&dev->fw_data->wait_fw);
2538 for (i = 0; i < MAX_CHANNELS; i++) {
2539 dev->setmode_ready[i] = 1;
2540 wake_up(&dev->wait_setmode[i]);
2541 }
2542
2543 mutex_lock(&dev->open_lock);
2544 usb_set_intfdata(interface, NULL);
2545 mutex_unlock(&dev->open_lock);
2546
Dean Anderson38f993a2008-06-26 23:15:51 -03002547 if (dev) {
2548 kref_put(&dev->kref, s2255_destroy);
2549 dprintk(1, "s2255drv: disconnect\n");
2550 dev_info(&interface->dev, "s2255usb now disconnected\n");
2551 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002552}
2553
2554static struct usb_driver s2255_driver = {
2555 .name = "s2255",
2556 .probe = s2255_probe,
2557 .disconnect = s2255_disconnect,
2558 .id_table = s2255_table,
2559};
2560
2561static int __init usb_s2255_init(void)
2562{
2563 int result;
2564
2565 /* register this driver with the USB subsystem */
2566 result = usb_register(&s2255_driver);
2567
2568 if (result)
2569 err("usb_register failed. Error number %d", result);
2570
2571 dprintk(2, "s2255_init: done\n");
2572 return result;
2573}
2574
2575static void __exit usb_s2255_exit(void)
2576{
2577 usb_deregister(&s2255_driver);
2578}
2579
2580module_init(usb_s2255_init);
2581module_exit(usb_s2255_exit);
2582
2583MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2584MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2585MODULE_LICENSE("GPL");