blob: b5be633e3bb0cb0683d7a085f553eb1a228c710c [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
Dean Anderson22b88d42008-08-29 15:33:19 -030061/* default JPEG quality */
62#define S2255_DEF_JPEG_QUAL 50
Dean Anderson38f993a2008-06-26 23:15:51 -030063/* vendor request in */
64#define S2255_VR_IN 0
65/* vendor request out */
66#define S2255_VR_OUT 1
67/* firmware query */
68#define S2255_VR_FW 0x30
69/* USB endpoint number for configuring the device */
70#define S2255_CONFIG_EP 2
71/* maximum time for DSP to start responding after last FW word loaded(ms) */
Dean Anderson14d96262008-08-25 13:58:55 -030072#define S2255_DSP_BOOTTIME 800
Dean Anderson38f993a2008-06-26 23:15:51 -030073/* maximum time to wait for firmware to load (ms) */
Dean Anderson3f8d6f72008-06-30 21:28:34 -030074#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
Dean Anderson38f993a2008-06-26 23:15:51 -030075#define S2255_DEF_BUFS 16
Dean Anderson14d96262008-08-25 13:58:55 -030076#define S2255_SETMODE_TIMEOUT 500
Dean Anderson38f993a2008-06-26 23:15:51 -030077#define MAX_CHANNELS 4
Dean Anderson14d96262008-08-25 13:58:55 -030078#define S2255_MARKER_FRAME 0x2255DA4AL
79#define S2255_MARKER_RESPONSE 0x2255ACACL
80#define S2255_USB_XFER_SIZE (16 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -030081#define MAX_CHANNELS 4
82#define MAX_PIPE_BUFFERS 1
83#define SYS_FRAMES 4
84/* maximum size is PAL full size plus room for the marker header(s) */
Dean Anderson14d96262008-08-25 13:58:55 -030085#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
86#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
Dean Anderson38f993a2008-06-26 23:15:51 -030087#define LINE_SZ_4CIFS_NTSC 640
88#define LINE_SZ_2CIFS_NTSC 640
89#define LINE_SZ_1CIFS_NTSC 320
90#define LINE_SZ_4CIFS_PAL 704
91#define LINE_SZ_2CIFS_PAL 704
92#define LINE_SZ_1CIFS_PAL 352
93#define NUM_LINES_4CIFS_NTSC 240
94#define NUM_LINES_2CIFS_NTSC 240
95#define NUM_LINES_1CIFS_NTSC 240
96#define NUM_LINES_4CIFS_PAL 288
97#define NUM_LINES_2CIFS_PAL 288
98#define NUM_LINES_1CIFS_PAL 288
99#define LINE_SZ_DEF 640
100#define NUM_LINES_DEF 240
101
102
103/* predefined settings */
104#define FORMAT_NTSC 1
105#define FORMAT_PAL 2
106
107#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
108#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
109#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
110
111#define COLOR_YUVPL 1 /* YUV planar */
112#define COLOR_YUVPK 2 /* YUV packed */
113#define COLOR_Y8 4 /* monochrome */
Dean Anderson14d96262008-08-25 13:58:55 -0300114#define COLOR_JPG 5 /* JPEG */
115#define MASK_COLOR 0xff
116#define MASK_JPG_QUALITY 0xff00
Dean Anderson38f993a2008-06-26 23:15:51 -0300117
118/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
119#define FDEC_1 1 /* capture every frame. default */
120#define FDEC_2 2 /* capture every 2nd frame */
121#define FDEC_3 3 /* capture every 3rd frame */
122#define FDEC_5 5 /* capture every 5th frame */
123
124/*-------------------------------------------------------
125 * Default mode parameters.
126 *-------------------------------------------------------*/
127#define DEF_SCALE SCALE_4CIFS
128#define DEF_COLOR COLOR_YUVPL
129#define DEF_FDEC FDEC_1
130#define DEF_BRIGHT 0
131#define DEF_CONTRAST 0x5c
132#define DEF_SATURATION 0x80
133#define DEF_HUE 0
134
135/* usb config commands */
136#define IN_DATA_TOKEN 0x2255c0de
137#define CMD_2255 0xc2255000
138#define CMD_SET_MODE (CMD_2255 | 0x10)
139#define CMD_START (CMD_2255 | 0x20)
140#define CMD_STOP (CMD_2255 | 0x30)
141#define CMD_STATUS (CMD_2255 | 0x40)
142
143struct s2255_mode {
144 u32 format; /* input video format (NTSC, PAL) */
145 u32 scale; /* output video scale */
146 u32 color; /* output video color format */
147 u32 fdec; /* frame decimation */
148 u32 bright; /* brightness */
149 u32 contrast; /* contrast */
150 u32 saturation; /* saturation */
151 u32 hue; /* hue (NTSC only)*/
152 u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/
153 u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */
154 u32 restart; /* if DSP requires restart */
155};
156
Dean Anderson14d96262008-08-25 13:58:55 -0300157
158#define S2255_READ_IDLE 0
159#define S2255_READ_FRAME 1
160
Dean Anderson38f993a2008-06-26 23:15:51 -0300161/* frame structure */
Dean Anderson38f993a2008-06-26 23:15:51 -0300162struct s2255_framei {
163 unsigned long size;
Dean Anderson14d96262008-08-25 13:58:55 -0300164 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
Dean Anderson38f993a2008-06-26 23:15:51 -0300165 void *lpvbits; /* image data */
166 unsigned long cur_size; /* current data copied to it */
167};
168
169/* image buffer structure */
170struct s2255_bufferi {
171 unsigned long dwFrames; /* number of frames in buffer */
172 struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */
173};
174
175#define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \
176 DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300177 DEF_HUE, 0, DEF_USB_BLOCK, 0}
Dean Anderson38f993a2008-06-26 23:15:51 -0300178
179struct s2255_dmaqueue {
180 struct list_head active;
181 /* thread for acquisition */
182 struct task_struct *kthread;
183 int frame;
184 struct s2255_dev *dev;
185 int channel;
186};
187
188/* for firmware loading, fw_state */
189#define S2255_FW_NOTLOADED 0
190#define S2255_FW_LOADED_DSPWAIT 1
191#define S2255_FW_SUCCESS 2
192#define S2255_FW_FAILED 3
Dean Andersonf78d92c2008-07-22 14:43:27 -0300193#define S2255_FW_DISCONNECTING 4
Dean Anderson38f993a2008-06-26 23:15:51 -0300194
Harvey Harrisondeaf53e2008-11-15 01:10:14 -0300195#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
Dean Anderson14d96262008-08-25 13:58:55 -0300196/* 2255 read states */
197#define S2255_READ_IDLE 0
198#define S2255_READ_FRAME 1
Dean Anderson38f993a2008-06-26 23:15:51 -0300199struct s2255_fw {
200 int fw_loaded;
201 int fw_size;
202 struct urb *fw_urb;
203 atomic_t fw_state;
204 void *pfw_data;
205 wait_queue_head_t wait_fw;
Dean Anderson38f993a2008-06-26 23:15:51 -0300206 const struct firmware *fw;
207};
208
209struct s2255_pipeinfo {
210 u32 max_transfer_size;
211 u32 cur_transfer_size;
212 u8 *transfer_buffer;
213 u32 transfer_flags;;
214 u32 state;
215 u32 prev_state;
216 u32 urb_size;
217 void *stream_urb;
218 void *dev; /* back pointer to s2255_dev struct*/
219 u32 err_count;
220 u32 buf_index;
221 u32 idx;
222 u32 priority_set;
223};
224
225struct s2255_fmt; /*forward declaration */
226
227struct s2255_dev {
228 int frames;
229 int users[MAX_CHANNELS];
230 struct mutex lock;
231 struct mutex open_lock;
232 int resources[MAX_CHANNELS];
233 struct usb_device *udev;
234 struct usb_interface *interface;
235 u8 read_endpoint;
236
237 struct s2255_dmaqueue vidq[MAX_CHANNELS];
238 struct video_device *vdev[MAX_CHANNELS];
239 struct list_head s2255_devlist;
240 struct timer_list timer;
241 struct s2255_fw *fw_data;
242 int board_num;
243 int is_open;
244 struct s2255_pipeinfo pipes[MAX_PIPE_BUFFERS];
245 struct s2255_bufferi buffer[MAX_CHANNELS];
246 struct s2255_mode mode[MAX_CHANNELS];
Dean Anderson22b88d42008-08-29 15:33:19 -0300247 /* jpeg compression */
248 struct v4l2_jpegcompression jc[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300249 const struct s2255_fmt *cur_fmt[MAX_CHANNELS];
250 int cur_frame[MAX_CHANNELS];
251 int last_frame[MAX_CHANNELS];
252 u32 cc; /* current channel */
253 int b_acquire[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300254 /* allocated image size */
Dean Anderson38f993a2008-06-26 23:15:51 -0300255 unsigned long req_image_size[MAX_CHANNELS];
Dean Anderson14d96262008-08-25 13:58:55 -0300256 /* received packet size */
257 unsigned long pkt_size[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300258 int bad_payload[MAX_CHANNELS];
259 unsigned long frame_count[MAX_CHANNELS];
260 int frame_ready;
Dean Anderson14d96262008-08-25 13:58:55 -0300261 /* if JPEG image */
262 int jpg_size[MAX_CHANNELS];
263 /* if channel configured to default state */
264 int chn_configured[MAX_CHANNELS];
265 wait_queue_head_t wait_setmode[MAX_CHANNELS];
266 int setmode_ready[MAX_CHANNELS];
267 int chn_ready;
Dean Anderson38f993a2008-06-26 23:15:51 -0300268 struct kref kref;
269 spinlock_t slock;
270};
271#define to_s2255_dev(d) container_of(d, struct s2255_dev, kref)
272
273struct s2255_fmt {
274 char *name;
275 u32 fourcc;
276 int depth;
277};
278
279/* buffer for one video frame */
280struct s2255_buffer {
281 /* common v4l buffer stuff -- must be first */
282 struct videobuf_buffer vb;
283 const struct s2255_fmt *fmt;
284};
285
286struct s2255_fh {
287 struct s2255_dev *dev;
Dean Anderson38f993a2008-06-26 23:15:51 -0300288 const struct s2255_fmt *fmt;
289 unsigned int width;
290 unsigned int height;
291 struct videobuf_queue vb_vidq;
292 enum v4l2_buf_type type;
293 int channel;
294 /* mode below is the desired mode.
295 mode in s2255_dev is the current mode that was last set */
296 struct s2255_mode mode;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300297 int resources[MAX_CHANNELS];
Dean Anderson38f993a2008-06-26 23:15:51 -0300298};
299
Dean Anderson38f993a2008-06-26 23:15:51 -0300300#define CUR_USB_FWVER 774 /* current cypress EEPROM firmware version */
301#define S2255_MAJOR_VERSION 1
302#define S2255_MINOR_VERSION 13
303#define S2255_RELEASE 0
304#define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \
305 S2255_MINOR_VERSION, \
306 S2255_RELEASE)
307
308/* vendor ids */
309#define USB_S2255_VENDOR_ID 0x1943
310#define USB_S2255_PRODUCT_ID 0x2255
311#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
312/* frame prefix size (sent once every frame) */
313#define PREFIX_SIZE 512
314
315/* Channels on box are in reverse order */
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300316static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
Dean Anderson38f993a2008-06-26 23:15:51 -0300317
318static LIST_HEAD(s2255_devlist);
319
320static int debug;
321static int *s2255_debug = &debug;
322
323static int s2255_start_readpipe(struct s2255_dev *dev);
324static void s2255_stop_readpipe(struct s2255_dev *dev);
325static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
326static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
327static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300328 int chn, int jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300329static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
330 struct s2255_mode *mode);
331static int s2255_board_shutdown(struct s2255_dev *dev);
332static void s2255_exit_v4l(struct s2255_dev *dev);
Dean Anderson14d96262008-08-25 13:58:55 -0300333static void s2255_fwload_start(struct s2255_dev *dev, int reset);
334static void s2255_destroy(struct kref *kref);
335static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
336 u16 index, u16 value, void *buf,
337 s32 buf_len, int bOut);
Dean Anderson38f993a2008-06-26 23:15:51 -0300338
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300339/* dev_err macro with driver name */
340#define S2255_DRIVER_NAME "s2255"
341#define s2255_dev_err(dev, fmt, arg...) \
342 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
343
Dean Anderson38f993a2008-06-26 23:15:51 -0300344#define dprintk(level, fmt, arg...) \
345 do { \
346 if (*s2255_debug >= (level)) { \
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300347 printk(KERN_DEBUG S2255_DRIVER_NAME \
348 ": " fmt, ##arg); \
Dean Anderson38f993a2008-06-26 23:15:51 -0300349 } \
350 } while (0)
351
Dean Anderson38f993a2008-06-26 23:15:51 -0300352static struct usb_driver s2255_driver;
353
354
355/* Declare static vars that will be used as parameters */
356static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
357
358/* start video number */
359static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
360
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300361module_param(debug, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300362MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300363module_param(vid_limit, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300364MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300365module_param(video_nr, int, 0644);
Dean Anderson38f993a2008-06-26 23:15:51 -0300366MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
367
368/* USB device table */
369static struct usb_device_id s2255_table[] = {
370 {USB_DEVICE(USB_S2255_VENDOR_ID, USB_S2255_PRODUCT_ID)},
371 { } /* Terminating entry */
372};
373MODULE_DEVICE_TABLE(usb, s2255_table);
374
375
376#define BUFFER_TIMEOUT msecs_to_jiffies(400)
377
378/* supported controls */
379static struct v4l2_queryctrl s2255_qctrl[] = {
380 {
381 .id = V4L2_CID_BRIGHTNESS,
382 .type = V4L2_CTRL_TYPE_INTEGER,
383 .name = "Brightness",
384 .minimum = -127,
385 .maximum = 128,
386 .step = 1,
387 .default_value = 0,
388 .flags = 0,
389 }, {
390 .id = V4L2_CID_CONTRAST,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 .name = "Contrast",
393 .minimum = 0,
394 .maximum = 255,
395 .step = 0x1,
396 .default_value = DEF_CONTRAST,
397 .flags = 0,
398 }, {
399 .id = V4L2_CID_SATURATION,
400 .type = V4L2_CTRL_TYPE_INTEGER,
401 .name = "Saturation",
402 .minimum = 0,
403 .maximum = 255,
404 .step = 0x1,
405 .default_value = DEF_SATURATION,
406 .flags = 0,
407 }, {
408 .id = V4L2_CID_HUE,
409 .type = V4L2_CTRL_TYPE_INTEGER,
410 .name = "Hue",
411 .minimum = 0,
412 .maximum = 255,
413 .step = 0x1,
414 .default_value = DEF_HUE,
415 .flags = 0,
416 }
417};
418
419static int qctl_regs[ARRAY_SIZE(s2255_qctrl)];
420
421/* image formats. */
422static const struct s2255_fmt formats[] = {
423 {
424 .name = "4:2:2, planar, YUV422P",
425 .fourcc = V4L2_PIX_FMT_YUV422P,
426 .depth = 16
427
428 }, {
429 .name = "4:2:2, packed, YUYV",
430 .fourcc = V4L2_PIX_FMT_YUYV,
431 .depth = 16
432
433 }, {
434 .name = "4:2:2, packed, UYVY",
435 .fourcc = V4L2_PIX_FMT_UYVY,
436 .depth = 16
437 }, {
Dean Anderson14d96262008-08-25 13:58:55 -0300438 .name = "JPG",
439 .fourcc = V4L2_PIX_FMT_JPEG,
440 .depth = 24
441 }, {
Dean Anderson38f993a2008-06-26 23:15:51 -0300442 .name = "8bpp GREY",
443 .fourcc = V4L2_PIX_FMT_GREY,
444 .depth = 8
445 }
446};
447
448static int norm_maxw(struct video_device *vdev)
449{
450 return (vdev->current_norm & V4L2_STD_NTSC) ?
451 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
452}
453
454static int norm_maxh(struct video_device *vdev)
455{
456 return (vdev->current_norm & V4L2_STD_NTSC) ?
457 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
458}
459
460static int norm_minw(struct video_device *vdev)
461{
462 return (vdev->current_norm & V4L2_STD_NTSC) ?
463 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
464}
465
466static int norm_minh(struct video_device *vdev)
467{
468 return (vdev->current_norm & V4L2_STD_NTSC) ?
469 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
470}
471
472
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300473/*
474 * TODO: fixme: move YUV reordering to hardware
475 * converts 2255 planar format to yuyv or uyvy
476 */
Dean Anderson38f993a2008-06-26 23:15:51 -0300477static void planar422p_to_yuv_packed(const unsigned char *in,
478 unsigned char *out,
479 int width, int height,
480 int fmt)
481{
482 unsigned char *pY;
483 unsigned char *pCb;
484 unsigned char *pCr;
485 unsigned long size = height * width;
486 unsigned int i;
487 pY = (unsigned char *)in;
488 pCr = (unsigned char *)in + height * width;
489 pCb = (unsigned char *)in + height * width + (height * width / 2);
490 for (i = 0; i < size * 2; i += 4) {
491 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
492 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
493 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
494 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
495 }
496 return;
497}
498
Hans Verkuild45b9b82008-09-04 03:33:43 -0300499static void s2255_reset_dsppower(struct s2255_dev *dev)
Dean Anderson14d96262008-08-25 13:58:55 -0300500{
501 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
502 msleep(10);
503 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
504 return;
505}
Dean Anderson38f993a2008-06-26 23:15:51 -0300506
507/* kickstarts the firmware loading. from probe
508 */
509static void s2255_timer(unsigned long user_data)
510{
511 struct s2255_fw *data = (struct s2255_fw *)user_data;
512 dprintk(100, "s2255 timer\n");
513 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
514 printk(KERN_ERR "s2255: can't submit urb\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300515 atomic_set(&data->fw_state, S2255_FW_FAILED);
516 /* wake up anything waiting for the firmware */
517 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300518 return;
519 }
520}
521
Dean Anderson38f993a2008-06-26 23:15:51 -0300522
523/* this loads the firmware asynchronously.
524 Originally this was done synchroously in probe.
525 But it is better to load it asynchronously here than block
526 inside the probe function. Blocking inside probe affects boot time.
527 FW loading is triggered by the timer in the probe function
528*/
529static void s2255_fwchunk_complete(struct urb *urb)
530{
531 struct s2255_fw *data = urb->context;
532 struct usb_device *udev = urb->dev;
533 int len;
534 dprintk(100, "udev %p urb %p", udev, urb);
Dean Anderson38f993a2008-06-26 23:15:51 -0300535 if (urb->status) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300536 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
Dean Andersonf78d92c2008-07-22 14:43:27 -0300537 atomic_set(&data->fw_state, S2255_FW_FAILED);
538 /* wake up anything waiting for the firmware */
539 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300540 return;
541 }
542 if (data->fw_urb == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300543 s2255_dev_err(&udev->dev, "disconnected\n");
Dean Andersonf78d92c2008-07-22 14:43:27 -0300544 atomic_set(&data->fw_state, S2255_FW_FAILED);
545 /* wake up anything waiting for the firmware */
546 wake_up(&data->wait_fw);
Dean Anderson38f993a2008-06-26 23:15:51 -0300547 return;
548 }
549#define CHUNK_SIZE 512
550 /* all USB transfers must be done with continuous kernel memory.
551 can't allocate more than 128k in current linux kernel, so
552 upload the firmware in chunks
553 */
554 if (data->fw_loaded < data->fw_size) {
555 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
556 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
557
558 if (len < CHUNK_SIZE)
559 memset(data->pfw_data, 0, CHUNK_SIZE);
560
561 dprintk(100, "completed len %d, loaded %d \n", len,
562 data->fw_loaded);
563
564 memcpy(data->pfw_data,
565 (char *) data->fw->data + data->fw_loaded, len);
566
567 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
568 data->pfw_data, CHUNK_SIZE,
569 s2255_fwchunk_complete, data);
570 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
571 dev_err(&udev->dev, "failed submit URB\n");
572 atomic_set(&data->fw_state, S2255_FW_FAILED);
573 /* wake up anything waiting for the firmware */
574 wake_up(&data->wait_fw);
575 return;
576 }
577 data->fw_loaded += len;
578 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -0300579 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
Dean Anderson38f993a2008-06-26 23:15:51 -0300580 }
581 dprintk(100, "2255 complete done\n");
582 return;
583
584}
585
Dean Anderson14d96262008-08-25 13:58:55 -0300586static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300587{
588 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
589 struct s2255_buffer *buf;
590 unsigned long flags = 0;
591 int rc = 0;
592 dprintk(2, "wakeup: %p channel: %d\n", &dma_q, chn);
593 spin_lock_irqsave(&dev->slock, flags);
594
595 if (list_empty(&dma_q->active)) {
596 dprintk(1, "No active queue to serve\n");
597 rc = -1;
598 goto unlock;
599 }
600 buf = list_entry(dma_q->active.next,
601 struct s2255_buffer, vb.queue);
602
603 if (!waitqueue_active(&buf->vb.done)) {
604 /* no one active */
605 rc = -1;
606 goto unlock;
607 }
608 list_del(&buf->vb.queue);
609 do_gettimeofday(&buf->vb.ts);
610 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
Dean Anderson14d96262008-08-25 13:58:55 -0300611 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
Dean Anderson38f993a2008-06-26 23:15:51 -0300612 wake_up(&buf->vb.done);
613 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
614unlock:
615 spin_unlock_irqrestore(&dev->slock, flags);
616 return 0;
617}
618
619
620static const struct s2255_fmt *format_by_fourcc(int fourcc)
621{
622 unsigned int i;
623
624 for (i = 0; i < ARRAY_SIZE(formats); i++) {
625 if (-1 == formats[i].fourcc)
626 continue;
627 if (formats[i].fourcc == fourcc)
628 return formats + i;
629 }
630 return NULL;
631}
632
633
634
635
636/* video buffer vmalloc implementation based partly on VIVI driver which is
637 * Copyright (c) 2006 by
638 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
639 * Ted Walther <ted--a.t--enumera.com>
640 * John Sokol <sokol--a.t--videotechnology.com>
641 * http://v4l.videotechnology.com/
642 *
643 */
644static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
Dean Anderson14d96262008-08-25 13:58:55 -0300645 int chn, int jpgsize)
Dean Anderson38f993a2008-06-26 23:15:51 -0300646{
647 int pos = 0;
648 struct timeval ts;
649 const char *tmpbuf;
650 char *vbuf = videobuf_to_vmalloc(&buf->vb);
651 unsigned long last_frame;
652 struct s2255_framei *frm;
653
654 if (!vbuf)
655 return;
656
657 last_frame = dev->last_frame[chn];
658 if (last_frame != -1) {
659 frm = &dev->buffer[chn].frame[last_frame];
660 tmpbuf =
661 (const char *)dev->buffer[chn].frame[last_frame].lpvbits;
662 switch (buf->fmt->fourcc) {
663 case V4L2_PIX_FMT_YUYV:
664 case V4L2_PIX_FMT_UYVY:
665 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
666 vbuf, buf->vb.width,
667 buf->vb.height,
668 buf->fmt->fourcc);
669 break;
670 case V4L2_PIX_FMT_GREY:
671 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
672 break;
Dean Anderson14d96262008-08-25 13:58:55 -0300673 case V4L2_PIX_FMT_JPEG:
674 buf->vb.size = jpgsize;
675 memcpy(vbuf, tmpbuf, buf->vb.size);
676 break;
Dean Anderson38f993a2008-06-26 23:15:51 -0300677 case V4L2_PIX_FMT_YUV422P:
678 memcpy(vbuf, tmpbuf,
679 buf->vb.width * buf->vb.height * 2);
680 break;
681 default:
682 printk(KERN_DEBUG "s2255: unknown format?\n");
683 }
684 dev->last_frame[chn] = -1;
Dean Anderson38f993a2008-06-26 23:15:51 -0300685 } else {
686 printk(KERN_ERR "s2255: =======no frame\n");
687 return;
688
689 }
690 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
691 (unsigned long)vbuf, pos);
692 /* tell v4l buffer was filled */
693
Dean Andersona1c45302008-09-09 12:29:56 -0300694 buf->vb.field_count = dev->frame_count[chn] * 2;
Dean Anderson38f993a2008-06-26 23:15:51 -0300695 do_gettimeofday(&ts);
696 buf->vb.ts = ts;
697 buf->vb.state = VIDEOBUF_DONE;
698}
699
700
701/* ------------------------------------------------------------------
702 Videobuf operations
703 ------------------------------------------------------------------*/
704
705static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
706 unsigned int *size)
707{
708 struct s2255_fh *fh = vq->priv_data;
709
710 *size = fh->width * fh->height * (fh->fmt->depth >> 3);
711
712 if (0 == *count)
713 *count = S2255_DEF_BUFS;
714
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300715 while (*size * (*count) > vid_limit * 1024 * 1024)
Dean Anderson38f993a2008-06-26 23:15:51 -0300716 (*count)--;
717
718 return 0;
719}
720
721static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
722{
723 dprintk(4, "%s\n", __func__);
724
725 videobuf_waiton(&buf->vb, 0, 0);
726 videobuf_vmalloc_free(&buf->vb);
727 buf->vb.state = VIDEOBUF_NEEDS_INIT;
728}
729
730static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
731 enum v4l2_field field)
732{
733 struct s2255_fh *fh = vq->priv_data;
734 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
735 int rc;
736 dprintk(4, "%s, field=%d\n", __func__, field);
737 if (fh->fmt == NULL)
738 return -EINVAL;
739
740 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
741 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
742 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
743 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
744 dprintk(4, "invalid buffer prepare\n");
745 return -EINVAL;
746 }
747
748 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
749
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
751 dprintk(4, "invalid buffer prepare\n");
752 return -EINVAL;
753 }
754
755 buf->fmt = fh->fmt;
756 buf->vb.width = fh->width;
757 buf->vb.height = fh->height;
758 buf->vb.field = field;
759
760
761 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
762 rc = videobuf_iolock(vq, &buf->vb, NULL);
763 if (rc < 0)
764 goto fail;
765 }
766
767 buf->vb.state = VIDEOBUF_PREPARED;
768 return 0;
769fail:
770 free_buffer(vq, buf);
771 return rc;
772}
773
774static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
775{
776 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
777 struct s2255_fh *fh = vq->priv_data;
778 struct s2255_dev *dev = fh->dev;
779 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
780
781 dprintk(1, "%s\n", __func__);
782
783 buf->vb.state = VIDEOBUF_QUEUED;
784 list_add_tail(&buf->vb.queue, &vidq->active);
785}
786
787static void buffer_release(struct videobuf_queue *vq,
788 struct videobuf_buffer *vb)
789{
790 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
791 struct s2255_fh *fh = vq->priv_data;
792 dprintk(4, "%s %d\n", __func__, fh->channel);
793 free_buffer(vq, buf);
794}
795
796static struct videobuf_queue_ops s2255_video_qops = {
797 .buf_setup = buffer_setup,
798 .buf_prepare = buffer_prepare,
799 .buf_queue = buffer_queue,
800 .buf_release = buffer_release,
801};
802
803
804static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
805{
806 /* is it free? */
807 mutex_lock(&dev->lock);
808 if (dev->resources[fh->channel]) {
809 /* no, someone else uses it */
810 mutex_unlock(&dev->lock);
811 return 0;
812 }
813 /* it's free, grab it */
814 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300815 fh->resources[fh->channel] = 1;
816 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300817 mutex_unlock(&dev->lock);
818 return 1;
819}
820
821static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
822{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300823 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300824}
825
Dean Andersonf78d92c2008-07-22 14:43:27 -0300826static int res_check(struct s2255_fh *fh)
827{
828 return fh->resources[fh->channel];
829}
830
831
Dean Anderson38f993a2008-06-26 23:15:51 -0300832static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
833{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300834 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300835 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300836 fh->resources[fh->channel] = 0;
837 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300838 dprintk(1, "res: put\n");
839}
840
841
842static int vidioc_querycap(struct file *file, void *priv,
843 struct v4l2_capability *cap)
844{
845 struct s2255_fh *fh = file->private_data;
846 struct s2255_dev *dev = fh->dev;
847 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
848 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300849 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300850 cap->version = S2255_VERSION;
851 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
852 return 0;
853}
854
855static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
856 struct v4l2_fmtdesc *f)
857{
858 int index = 0;
859 if (f)
860 index = f->index;
861
862 if (index >= ARRAY_SIZE(formats))
863 return -EINVAL;
864
865 dprintk(4, "name %s\n", formats[index].name);
866 strlcpy(f->description, formats[index].name, sizeof(f->description));
867 f->pixelformat = formats[index].fourcc;
868 return 0;
869}
870
871static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
872 struct v4l2_format *f)
873{
874 struct s2255_fh *fh = priv;
875
876 f->fmt.pix.width = fh->width;
877 f->fmt.pix.height = fh->height;
878 f->fmt.pix.field = fh->vb_vidq.field;
879 f->fmt.pix.pixelformat = fh->fmt->fourcc;
880 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
881 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300882 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300883}
884
885static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
886 struct v4l2_format *f)
887{
888 const struct s2255_fmt *fmt;
889 enum v4l2_field field;
890 int b_any_field = 0;
891 struct s2255_fh *fh = priv;
892 struct s2255_dev *dev = fh->dev;
893 int is_ntsc;
894
895 is_ntsc =
896 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
897
898 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
899
900 if (fmt == NULL)
901 return -EINVAL;
902
903 field = f->fmt.pix.field;
904 if (field == V4L2_FIELD_ANY)
905 b_any_field = 1;
906
907 dprintk(4, "try format %d \n", is_ntsc);
908 /* supports 3 sizes. see s2255drv.h */
909 dprintk(50, "width test %d, height %d\n",
910 f->fmt.pix.width, f->fmt.pix.height);
911 if (is_ntsc) {
912 /* NTSC */
913 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
914 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
915 if (b_any_field) {
916 field = V4L2_FIELD_SEQ_TB;
917 } else if (!((field == V4L2_FIELD_INTERLACED) ||
918 (field == V4L2_FIELD_SEQ_TB) ||
919 (field == V4L2_FIELD_INTERLACED_TB))) {
920 dprintk(1, "unsupported field setting\n");
921 return -EINVAL;
922 }
923 } else {
924 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
925 if (b_any_field) {
926 field = V4L2_FIELD_TOP;
927 } else if (!((field == V4L2_FIELD_TOP) ||
928 (field == V4L2_FIELD_BOTTOM))) {
929 dprintk(1, "unsupported field setting\n");
930 return -EINVAL;
931 }
932
933 }
934 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
935 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
936 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
937 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
938 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
939 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
940 else
941 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
942 } else {
943 /* PAL */
944 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
945 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
946 if (b_any_field) {
947 field = V4L2_FIELD_SEQ_TB;
948 } else if (!((field == V4L2_FIELD_INTERLACED) ||
949 (field == V4L2_FIELD_SEQ_TB) ||
950 (field == V4L2_FIELD_INTERLACED_TB))) {
951 dprintk(1, "unsupported field setting\n");
952 return -EINVAL;
953 }
954 } else {
955 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
956 if (b_any_field) {
957 field = V4L2_FIELD_TOP;
958 } else if (!((field == V4L2_FIELD_TOP) ||
959 (field == V4L2_FIELD_BOTTOM))) {
960 dprintk(1, "unsupported field setting\n");
961 return -EINVAL;
962 }
963 }
964 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
965 dprintk(50, "pal 704\n");
966 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
967 field = V4L2_FIELD_SEQ_TB;
968 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
969 dprintk(50, "pal 352A\n");
970 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
971 field = V4L2_FIELD_TOP;
972 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
973 dprintk(50, "pal 352B\n");
974 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
975 field = V4L2_FIELD_TOP;
976 } else {
977 dprintk(50, "pal 352C\n");
978 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
979 field = V4L2_FIELD_TOP;
980 }
981 }
982
983 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
984 f->fmt.pix.height, f->fmt.pix.field);
985 f->fmt.pix.field = field;
986 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
987 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
988 return 0;
989}
990
991static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
992 struct v4l2_format *f)
993{
994 struct s2255_fh *fh = priv;
995 const struct s2255_fmt *fmt;
996 struct videobuf_queue *q = &fh->vb_vidq;
997 int ret;
998 int norm;
999
1000 ret = vidioc_try_fmt_vid_cap(file, fh, f);
1001
1002 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001003 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -03001004
1005 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1006
1007 if (fmt == NULL)
1008 return -EINVAL;
1009
1010 mutex_lock(&q->vb_lock);
1011
1012 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1013 dprintk(1, "queue busy\n");
1014 ret = -EBUSY;
1015 goto out_s_fmt;
1016 }
1017
1018 if (res_locked(fh->dev, fh)) {
1019 dprintk(1, "can't change format after started\n");
1020 ret = -EBUSY;
1021 goto out_s_fmt;
1022 }
1023
1024 fh->fmt = fmt;
1025 fh->width = f->fmt.pix.width;
1026 fh->height = f->fmt.pix.height;
1027 fh->vb_vidq.field = f->fmt.pix.field;
1028 fh->type = f->type;
1029 norm = norm_minw(fh->dev->vdev[fh->channel]);
1030 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1031 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
1032 fh->mode.scale = SCALE_4CIFS;
1033 else
1034 fh->mode.scale = SCALE_2CIFS;
1035
1036 } else {
1037 fh->mode.scale = SCALE_1CIFS;
1038 }
1039
1040 /* color mode */
1041 switch (fh->fmt->fourcc) {
1042 case V4L2_PIX_FMT_GREY:
1043 fh->mode.color = COLOR_Y8;
1044 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001045 case V4L2_PIX_FMT_JPEG:
Dean Anderson22b88d42008-08-29 15:33:19 -03001046 fh->mode.color = COLOR_JPG |
1047 (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001048 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001049 case V4L2_PIX_FMT_YUV422P:
1050 fh->mode.color = COLOR_YUVPL;
1051 break;
1052 case V4L2_PIX_FMT_YUYV:
1053 case V4L2_PIX_FMT_UYVY:
1054 default:
1055 fh->mode.color = COLOR_YUVPK;
1056 break;
1057 }
1058 ret = 0;
1059out_s_fmt:
1060 mutex_unlock(&q->vb_lock);
1061 return ret;
1062}
1063
1064static int vidioc_reqbufs(struct file *file, void *priv,
1065 struct v4l2_requestbuffers *p)
1066{
1067 int rc;
1068 struct s2255_fh *fh = priv;
1069 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1070 return rc;
1071}
1072
1073static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1074{
1075 int rc;
1076 struct s2255_fh *fh = priv;
1077 rc = videobuf_querybuf(&fh->vb_vidq, p);
1078 return rc;
1079}
1080
1081static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1082{
1083 int rc;
1084 struct s2255_fh *fh = priv;
1085 rc = videobuf_qbuf(&fh->vb_vidq, p);
1086 return rc;
1087}
1088
1089static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1090{
1091 int rc;
1092 struct s2255_fh *fh = priv;
1093 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1094 return rc;
1095}
1096
1097#ifdef CONFIG_VIDEO_V4L1_COMPAT
1098static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1099{
1100 struct s2255_fh *fh = priv;
1101
1102 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1103}
1104#endif
1105
1106/* write to the configuration pipe, synchronously */
1107static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1108 int size)
1109{
1110 int pipe;
1111 int done;
1112 long retval = -1;
1113 if (udev) {
1114 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1115 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1116 }
1117 return retval;
1118}
1119
1120static u32 get_transfer_size(struct s2255_mode *mode)
1121{
1122 int linesPerFrame = LINE_SZ_DEF;
1123 int pixelsPerLine = NUM_LINES_DEF;
1124 u32 outImageSize;
1125 u32 usbInSize;
1126 unsigned int mask_mult;
1127
1128 if (mode == NULL)
1129 return 0;
1130
1131 if (mode->format == FORMAT_NTSC) {
1132 switch (mode->scale) {
1133 case SCALE_4CIFS:
1134 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1135 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1136 break;
1137 case SCALE_2CIFS:
1138 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1139 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1140 break;
1141 case SCALE_1CIFS:
1142 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1143 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1144 break;
1145 default:
1146 break;
1147 }
1148 } else if (mode->format == FORMAT_PAL) {
1149 switch (mode->scale) {
1150 case SCALE_4CIFS:
1151 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1152 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1153 break;
1154 case SCALE_2CIFS:
1155 linesPerFrame = NUM_LINES_2CIFS_PAL;
1156 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1157 break;
1158 case SCALE_1CIFS:
1159 linesPerFrame = NUM_LINES_1CIFS_PAL;
1160 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1161 break;
1162 default:
1163 break;
1164 }
1165 }
1166 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001167 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001168 /* 2 bytes/pixel if not monochrome */
1169 outImageSize *= 2;
1170 }
1171
1172 /* total bytes to send including prefix and 4K padding;
1173 must be a multiple of USB_READ_SIZE */
1174 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1175 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1176 /* if size not a multiple of USB_READ_SIZE */
1177 if (usbInSize & ~mask_mult)
1178 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1179 return usbInSize;
1180}
1181
1182static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1183{
1184 struct device *dev = &sdev->udev->dev;
1185 dev_info(dev, "------------------------------------------------\n");
1186 dev_info(dev, "verify mode\n");
1187 dev_info(dev, "format: %d\n", mode->format);
1188 dev_info(dev, "scale: %d\n", mode->scale);
1189 dev_info(dev, "fdec: %d\n", mode->fdec);
1190 dev_info(dev, "color: %d\n", mode->color);
1191 dev_info(dev, "bright: 0x%x\n", mode->bright);
1192 dev_info(dev, "restart: 0x%x\n", mode->restart);
1193 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1194 dev_info(dev, "single: 0x%x\n", mode->single);
1195 dev_info(dev, "------------------------------------------------\n");
1196}
1197
1198/*
1199 * set mode is the function which controls the DSP.
1200 * the restart parameter in struct s2255_mode should be set whenever
1201 * the image size could change via color format, video system or image
1202 * size.
1203 * When the restart parameter is set, we sleep for ONE frame to allow the
1204 * DSP time to get the new frame
1205 */
1206static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1207 struct s2255_mode *mode)
1208{
1209 int res;
1210 u32 *buffer;
1211 unsigned long chn_rev;
1212
Dean Anderson14d96262008-08-25 13:58:55 -03001213 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001214 chn_rev = G_chnmap[chn];
1215 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1216 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1217 dev->mode[chn].scale);
1218 dprintk(2, "mode contrast %x\n", mode->contrast);
1219
Dean Anderson22b88d42008-08-29 15:33:19 -03001220 /* if JPEG, set the quality */
1221 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1222 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1223
Dean Anderson38f993a2008-06-26 23:15:51 -03001224 /* save the mode */
1225 dev->mode[chn] = *mode;
1226 dev->req_image_size[chn] = get_transfer_size(mode);
1227 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1228
1229 buffer = kzalloc(512, GFP_KERNEL);
1230 if (buffer == NULL) {
1231 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001232 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001233 return -ENOMEM;
1234 }
1235
1236 /* set the mode */
1237 buffer[0] = IN_DATA_TOKEN;
1238 buffer[1] = (u32) chn_rev;
1239 buffer[2] = CMD_SET_MODE;
1240 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1241 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1242 if (debug)
1243 dump_verify_mode(dev, mode);
1244 kfree(buffer);
1245 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1246
1247 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001248 if (mode->restart) {
1249 dev->setmode_ready[chn] = 0;
1250 wait_event_timeout(dev->wait_setmode[chn],
1251 (dev->setmode_ready[chn] != 0),
1252 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1253 if (dev->setmode_ready[chn] != 1) {
1254 printk(KERN_DEBUG "s2255: no set mode response\n");
1255 res = -EFAULT;
1256 }
1257 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001258
1259 /* clear the restart flag */
1260 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001261 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001262 return res;
1263}
1264
1265static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1266{
1267 int res;
1268 struct s2255_fh *fh = priv;
1269 struct s2255_dev *dev = fh->dev;
1270 struct s2255_mode *new_mode;
1271 struct s2255_mode *old_mode;
1272 int chn;
1273 int j;
1274 dprintk(4, "%s\n", __func__);
1275 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1276 dev_err(&dev->udev->dev, "invalid fh type0\n");
1277 return -EINVAL;
1278 }
1279 if (i != fh->type) {
1280 dev_err(&dev->udev->dev, "invalid fh type1\n");
1281 return -EINVAL;
1282 }
1283
1284 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001285 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001286 return -EBUSY;
1287 }
1288
1289 /* send a set mode command everytime with restart.
1290 in case we switch resolutions or other parameters */
1291 chn = fh->channel;
1292 new_mode = &fh->mode;
1293 old_mode = &fh->dev->mode[chn];
1294
1295 if (new_mode->color != old_mode->color)
1296 new_mode->restart = 1;
1297 else if (new_mode->scale != old_mode->scale)
1298 new_mode->restart = 1;
1299 else if (new_mode->format != old_mode->format)
1300 new_mode->restart = 1;
1301
1302 s2255_set_mode(dev, chn, new_mode);
1303 new_mode->restart = 0;
1304 *old_mode = *new_mode;
1305 dev->cur_fmt[chn] = fh->fmt;
1306 dprintk(1, "%s[%d]\n", __func__, chn);
1307 dev->last_frame[chn] = -1;
1308 dev->bad_payload[chn] = 0;
1309 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001310 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001311 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001312 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001313 dev->buffer[chn].frame[j].cur_size = 0;
1314 }
1315 res = videobuf_streamon(&fh->vb_vidq);
1316 if (res == 0) {
1317 s2255_start_acquire(dev, chn);
1318 dev->b_acquire[chn] = 1;
1319 } else {
1320 res_free(dev, fh);
1321 }
1322 return res;
1323}
1324
1325static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1326{
1327 int res;
1328 struct s2255_fh *fh = priv;
1329 struct s2255_dev *dev = fh->dev;
1330
1331 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1332 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1333 printk(KERN_ERR "invalid fh type0\n");
1334 return -EINVAL;
1335 }
1336 if (i != fh->type) {
1337 printk(KERN_ERR "invalid type i\n");
1338 return -EINVAL;
1339 }
1340 s2255_stop_acquire(dev, fh->channel);
1341 res = videobuf_streamoff(&fh->vb_vidq);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001342 if (res < 0)
1343 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03001344 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001345 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001346}
1347
1348static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1349{
1350 struct s2255_fh *fh = priv;
1351 struct s2255_mode *mode;
1352 struct videobuf_queue *q = &fh->vb_vidq;
1353 int ret = 0;
1354
1355 mutex_lock(&q->vb_lock);
1356 if (videobuf_queue_is_busy(q)) {
1357 dprintk(1, "queue busy\n");
1358 ret = -EBUSY;
1359 goto out_s_std;
1360 }
1361
1362 if (res_locked(fh->dev, fh)) {
1363 dprintk(1, "can't change standard after started\n");
1364 ret = -EBUSY;
1365 goto out_s_std;
1366 }
1367 mode = &fh->mode;
1368
1369 if (*i & V4L2_STD_NTSC) {
1370 dprintk(4, "vidioc_s_std NTSC\n");
1371 mode->format = FORMAT_NTSC;
1372 } else if (*i & V4L2_STD_PAL) {
1373 dprintk(4, "vidioc_s_std PAL\n");
1374 mode->format = FORMAT_PAL;
1375 } else {
1376 ret = -EINVAL;
1377 }
1378out_s_std:
1379 mutex_unlock(&q->vb_lock);
1380 return ret;
1381}
1382
1383/* Sensoray 2255 is a multiple channel capture device.
1384 It does not have a "crossbar" of inputs.
1385 We use one V4L device per channel. The user must
1386 be aware that certain combinations are not allowed.
1387 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1388 at once in color(you can do full fps on 4 channels with greyscale.
1389*/
1390static int vidioc_enum_input(struct file *file, void *priv,
1391 struct v4l2_input *inp)
1392{
1393 if (inp->index != 0)
1394 return -EINVAL;
1395
1396 inp->type = V4L2_INPUT_TYPE_CAMERA;
1397 inp->std = S2255_NORMS;
1398 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001399 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001400}
1401
1402static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1403{
1404 *i = 0;
1405 return 0;
1406}
1407static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1408{
1409 if (i > 0)
1410 return -EINVAL;
1411 return 0;
1412}
1413
1414/* --- controls ---------------------------------------------- */
1415static int vidioc_queryctrl(struct file *file, void *priv,
1416 struct v4l2_queryctrl *qc)
1417{
1418 int i;
1419
1420 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1421 if (qc->id && qc->id == s2255_qctrl[i].id) {
1422 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001423 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001424 }
1425
1426 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1427 return -EINVAL;
1428}
1429
1430static int vidioc_g_ctrl(struct file *file, void *priv,
1431 struct v4l2_control *ctrl)
1432{
1433 int i;
1434
1435 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1436 if (ctrl->id == s2255_qctrl[i].id) {
1437 ctrl->value = qctl_regs[i];
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001438 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001439 }
1440 dprintk(4, "g_ctrl -EINVAL\n");
1441
1442 return -EINVAL;
1443}
1444
1445static int vidioc_s_ctrl(struct file *file, void *priv,
1446 struct v4l2_control *ctrl)
1447{
1448 int i;
1449 struct s2255_fh *fh = priv;
1450 struct s2255_dev *dev = fh->dev;
1451 struct s2255_mode *mode;
1452 mode = &fh->mode;
1453 dprintk(4, "vidioc_s_ctrl\n");
1454 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1455 if (ctrl->id == s2255_qctrl[i].id) {
1456 if (ctrl->value < s2255_qctrl[i].minimum ||
1457 ctrl->value > s2255_qctrl[i].maximum)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001458 return -ERANGE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001459
1460 qctl_regs[i] = ctrl->value;
1461 /* update the mode to the corresponding value */
1462 switch (ctrl->id) {
1463 case V4L2_CID_BRIGHTNESS:
1464 mode->bright = ctrl->value;
1465 break;
1466 case V4L2_CID_CONTRAST:
1467 mode->contrast = ctrl->value;
1468 break;
1469 case V4L2_CID_HUE:
1470 mode->hue = ctrl->value;
1471 break;
1472 case V4L2_CID_SATURATION:
1473 mode->saturation = ctrl->value;
1474 break;
1475 }
1476 mode->restart = 0;
1477 /* set mode here. Note: stream does not need restarted.
1478 some V4L programs restart stream unnecessarily
1479 after a s_crtl.
1480 */
1481 s2255_set_mode(dev, fh->channel, mode);
1482 return 0;
1483 }
1484 }
1485 return -EINVAL;
1486}
1487
Dean Anderson22b88d42008-08-29 15:33:19 -03001488static int vidioc_g_jpegcomp(struct file *file, void *priv,
1489 struct v4l2_jpegcompression *jc)
1490{
1491 struct s2255_fh *fh = priv;
1492 struct s2255_dev *dev = fh->dev;
1493 *jc = dev->jc[fh->channel];
1494 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1495 return 0;
1496}
1497
1498static int vidioc_s_jpegcomp(struct file *file, void *priv,
1499 struct v4l2_jpegcompression *jc)
1500{
1501 struct s2255_fh *fh = priv;
1502 struct s2255_dev *dev = fh->dev;
1503 if (jc->quality < 0 || jc->quality > 100)
1504 return -EINVAL;
1505 dev->jc[fh->channel].quality = jc->quality;
1506 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1507 return 0;
1508}
Hans Verkuilbec43662008-12-30 06:58:20 -03001509static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001510{
Hans Verkuilbec43662008-12-30 06:58:20 -03001511 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001512 struct s2255_dev *h, *dev = NULL;
1513 struct s2255_fh *fh;
1514 struct list_head *list;
1515 enum v4l2_buf_type type = 0;
1516 int i = 0;
1517 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001518 int state;
Dean Anderson38f993a2008-06-26 23:15:51 -03001519 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1520
Hans Verkuild56dc612008-07-30 08:43:36 -03001521 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001522 list_for_each(list, &s2255_devlist) {
1523 h = list_entry(list, struct s2255_dev, s2255_devlist);
1524 for (i = 0; i < MAX_CHANNELS; i++) {
1525 if (h->vdev[i]->minor == minor) {
1526 cur_channel = i;
1527 dev = h;
1528 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1529 }
1530 }
1531 }
1532
1533 if ((NULL == dev) || (cur_channel == -1)) {
Hans Verkuild56dc612008-07-30 08:43:36 -03001534 unlock_kernel();
Dean Anderson14d96262008-08-25 13:58:55 -03001535 printk(KERN_INFO "s2255: openv4l no dev\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001536 return -ENODEV;
1537 }
1538
Dean Anderson14d96262008-08-25 13:58:55 -03001539 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1540 unlock_kernel();
1541 printk(KERN_INFO "disconnecting\n");
1542 return -ENODEV;
1543 }
1544 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001545 mutex_lock(&dev->open_lock);
1546
1547 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001548 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001549
Dean Anderson14d96262008-08-25 13:58:55 -03001550 switch (atomic_read(&dev->fw_data->fw_state)) {
1551 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001552 s2255_dev_err(&dev->udev->dev,
1553 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001554 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001555 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001556 ((atomic_read(&dev->fw_data->fw_state)
1557 == S2255_FW_SUCCESS) ||
1558 (atomic_read(&dev->fw_data->fw_state)
1559 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001560 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001561 break;
1562 case S2255_FW_NOTLOADED:
1563 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001564 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1565 driver loaded and then device immediately opened */
1566 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1567 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001568 ((atomic_read(&dev->fw_data->fw_state)
1569 == S2255_FW_SUCCESS) ||
1570 (atomic_read(&dev->fw_data->fw_state)
1571 == S2255_FW_DISCONNECTING)),
1572 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1573 break;
1574 case S2255_FW_SUCCESS:
1575 default:
1576 break;
1577 }
1578 state = atomic_read(&dev->fw_data->fw_state);
1579 if (state != S2255_FW_SUCCESS) {
1580 int rc;
1581 switch (state) {
1582 case S2255_FW_FAILED:
1583 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1584 rc = -ENODEV;
1585 break;
1586 case S2255_FW_DISCONNECTING:
1587 printk(KERN_INFO "%s: disconnecting\n", __func__);
1588 rc = -ENODEV;
1589 break;
1590 case S2255_FW_LOADED_DSPWAIT:
1591 case S2255_FW_NOTLOADED:
1592 printk(KERN_INFO "%s: firmware not loaded yet"
1593 "please try again later\n",
1594 __func__);
1595 rc = -EAGAIN;
1596 break;
1597 default:
1598 printk(KERN_INFO "%s: unknown state\n", __func__);
1599 rc = -EFAULT;
1600 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001601 }
Dean Anderson14d96262008-08-25 13:58:55 -03001602 dev->users[cur_channel]--;
1603 mutex_unlock(&dev->open_lock);
1604 kref_put(&dev->kref, s2255_destroy);
1605 unlock_kernel();
1606 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001607 }
1608
1609 /* allocate + initialize per filehandle data */
1610 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1611 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001612 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001613 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001614 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001615 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001616 return -ENOMEM;
1617 }
1618
1619 file->private_data = fh;
1620 fh->dev = dev;
1621 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1622 fh->mode = dev->mode[cur_channel];
1623 fh->fmt = dev->cur_fmt[cur_channel];
1624 /* default 4CIF NTSC */
1625 fh->width = LINE_SZ_4CIFS_NTSC;
1626 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1627 fh->channel = cur_channel;
1628
Dean Anderson14d96262008-08-25 13:58:55 -03001629 /* configure channel to default state */
1630 if (!dev->chn_configured[cur_channel]) {
1631 s2255_set_mode(dev, cur_channel, &fh->mode);
1632 dev->chn_configured[cur_channel] = 1;
1633 }
1634
1635
Dean Anderson38f993a2008-06-26 23:15:51 -03001636 /* Put all controls at a sane state */
1637 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1638 qctl_regs[i] = s2255_qctrl[i].default_value;
1639
1640 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1641 minor, v4l2_type_names[type], dev->users[cur_channel]);
1642 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1643 (unsigned long)fh, (unsigned long)dev,
1644 (unsigned long)&dev->vidq[cur_channel]);
1645 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1646 list_empty(&dev->vidq[cur_channel].active));
1647
1648 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1649 NULL, &dev->slock,
1650 fh->type,
1651 V4L2_FIELD_INTERLACED,
1652 sizeof(struct s2255_buffer), fh);
1653
Dean Anderson38f993a2008-06-26 23:15:51 -03001654 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001655 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001656 return 0;
1657}
1658
1659
1660static unsigned int s2255_poll(struct file *file,
1661 struct poll_table_struct *wait)
1662{
1663 struct s2255_fh *fh = file->private_data;
1664 int rc;
1665 dprintk(100, "%s\n", __func__);
1666
1667 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1668 return POLLERR;
1669
1670 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1671 return rc;
1672}
1673
1674static void s2255_destroy(struct kref *kref)
1675{
1676 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001677 struct list_head *list;
1678 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001679 if (!dev) {
1680 printk(KERN_ERR "s2255drv: kref problem\n");
1681 return;
1682 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001683 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1684 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001685 for (i = 0; i < MAX_CHANNELS; i++) {
1686 dev->setmode_ready[i] = 1;
1687 wake_up(&dev->wait_setmode[i]);
1688 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001689 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001690 /* reset the DSP so firmware can be reload next time */
1691 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001692 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001693 /* board shutdown stops the read pipe if it is running */
1694 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001695 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001696 del_timer(&dev->timer); /* only started in .probe and .open */
1697
Dean Anderson38f993a2008-06-26 23:15:51 -03001698 if (dev->fw_data->fw_urb) {
1699 dprintk(2, "kill fw_urb\n");
1700 usb_kill_urb(dev->fw_data->fw_urb);
1701 usb_free_urb(dev->fw_data->fw_urb);
1702 dev->fw_data->fw_urb = NULL;
1703 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001704 if (dev->fw_data->fw)
1705 release_firmware(dev->fw_data->fw);
1706 kfree(dev->fw_data->pfw_data);
1707 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001708 usb_put_dev(dev->udev);
1709 dprintk(1, "%s", __func__);
1710 kfree(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03001711
1712 while (!list_empty(&s2255_devlist)) {
1713 list = s2255_devlist.next;
1714 list_del(list);
1715 }
1716 mutex_unlock(&dev->open_lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001717}
1718
Hans Verkuilbec43662008-12-30 06:58:20 -03001719static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001720{
1721 struct s2255_fh *fh = file->private_data;
1722 struct s2255_dev *dev = fh->dev;
Hans Verkuilbec43662008-12-30 06:58:20 -03001723 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001724 if (!dev)
1725 return -ENODEV;
1726
1727 mutex_lock(&dev->open_lock);
1728
Dean Andersonf78d92c2008-07-22 14:43:27 -03001729 /* turn off stream */
1730 if (res_check(fh)) {
1731 if (dev->b_acquire[fh->channel])
1732 s2255_stop_acquire(dev, fh->channel);
1733 videobuf_streamoff(&fh->vb_vidq);
1734 res_free(dev, fh);
1735 }
1736
Dean Anderson38f993a2008-06-26 23:15:51 -03001737 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001738 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001739
Dean Anderson38f993a2008-06-26 23:15:51 -03001740 mutex_unlock(&dev->open_lock);
1741
1742 kref_put(&dev->kref, s2255_destroy);
1743 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1744 minor, dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001745 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001746 return 0;
1747}
1748
1749static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1750{
1751 struct s2255_fh *fh = file->private_data;
1752 int ret;
1753
1754 if (!fh)
1755 return -ENODEV;
1756 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1757
1758 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1759
1760 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1761 (unsigned long)vma->vm_start,
1762 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1763
1764 return ret;
1765}
1766
Hans Verkuilbec43662008-12-30 06:58:20 -03001767static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001768 .owner = THIS_MODULE,
1769 .open = s2255_open,
1770 .release = s2255_close,
1771 .poll = s2255_poll,
1772 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001773 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001774};
1775
Hans Verkuila3998102008-07-21 02:57:38 -03001776static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001777 .vidioc_querycap = vidioc_querycap,
1778 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1779 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1780 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1781 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1782 .vidioc_reqbufs = vidioc_reqbufs,
1783 .vidioc_querybuf = vidioc_querybuf,
1784 .vidioc_qbuf = vidioc_qbuf,
1785 .vidioc_dqbuf = vidioc_dqbuf,
1786 .vidioc_s_std = vidioc_s_std,
1787 .vidioc_enum_input = vidioc_enum_input,
1788 .vidioc_g_input = vidioc_g_input,
1789 .vidioc_s_input = vidioc_s_input,
1790 .vidioc_queryctrl = vidioc_queryctrl,
1791 .vidioc_g_ctrl = vidioc_g_ctrl,
1792 .vidioc_s_ctrl = vidioc_s_ctrl,
1793 .vidioc_streamon = vidioc_streamon,
1794 .vidioc_streamoff = vidioc_streamoff,
1795#ifdef CONFIG_VIDEO_V4L1_COMPAT
1796 .vidiocgmbuf = vidioc_cgmbuf,
1797#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001798 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1799 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Hans Verkuila3998102008-07-21 02:57:38 -03001800};
1801
1802static struct video_device template = {
1803 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001804 .fops = &s2255_fops_v4l,
1805 .ioctl_ops = &s2255_ioctl_ops,
1806 .minor = -1,
1807 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001808 .tvnorms = S2255_NORMS,
1809 .current_norm = V4L2_STD_NTSC_M,
1810};
1811
1812static int s2255_probe_v4l(struct s2255_dev *dev)
1813{
1814 int ret;
1815 int i;
1816 int cur_nr = video_nr;
1817
1818 /* initialize all video 4 linux */
1819 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1820 /* register 4 video devices */
1821 for (i = 0; i < MAX_CHANNELS; i++) {
1822 INIT_LIST_HEAD(&dev->vidq[i].active);
1823 dev->vidq[i].dev = dev;
1824 dev->vidq[i].channel = i;
1825 dev->vidq[i].kthread = NULL;
1826 /* register 4 video devices */
1827 dev->vdev[i] = video_device_alloc();
1828 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001829 dev->vdev[i]->parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001830 if (video_nr == -1)
1831 ret = video_register_device(dev->vdev[i],
1832 VFL_TYPE_GRABBER,
1833 video_nr);
1834 else
1835 ret = video_register_device(dev->vdev[i],
1836 VFL_TYPE_GRABBER,
1837 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001838 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001839
1840 if (ret != 0) {
1841 dev_err(&dev->udev->dev,
1842 "failed to register video device!\n");
1843 return ret;
1844 }
1845 }
1846 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1847 return ret;
1848}
1849
1850static void s2255_exit_v4l(struct s2255_dev *dev)
1851{
Dean Anderson14d96262008-08-25 13:58:55 -03001852
Dean Anderson38f993a2008-06-26 23:15:51 -03001853 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001854 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001855 if (-1 != dev->vdev[i]->minor) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001856 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001857 printk(KERN_INFO "s2255 unregistered\n");
1858 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001859 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001860 printk(KERN_INFO "s2255 released\n");
1861 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001862 }
1863}
1864
1865/* this function moves the usb stream read pipe data
1866 * into the system buffers.
1867 * returns 0 on success, EAGAIN if more data to process( call this
1868 * function again).
1869 *
1870 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001871 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001872 * bytes 4-7: channel: 0-3
1873 * bytes 8-11: payload size: size of the frame
1874 * bytes 12-payloadsize+12: frame data
1875 */
1876static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1877{
Dean Anderson38f993a2008-06-26 23:15:51 -03001878 char *pdest;
1879 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001880 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001881 char *psrc;
1882 unsigned long copy_size;
1883 unsigned long size;
1884 s32 idx = -1;
1885 struct s2255_framei *frm;
1886 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001887
Dean Anderson38f993a2008-06-26 23:15:51 -03001888 dprintk(100, "buffer to user\n");
1889
1890 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001891 frm = &dev->buffer[dev->cc].frame[idx];
1892
Dean Anderson14d96262008-08-25 13:58:55 -03001893 if (frm->ulState == S2255_READ_IDLE) {
1894 int jj;
1895 unsigned int cc;
1896 s32 *pdword;
1897 int payload;
1898 /* search for marker codes */
1899 pdata = (unsigned char *)pipe_info->transfer_buffer;
1900 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1901 switch (*(s32 *) pdata) {
1902 case S2255_MARKER_FRAME:
1903 pdword = (s32 *)pdata;
1904 dprintk(4, "found frame marker at offset:"
1905 " %d [%x %x]\n", jj, pdata[0],
1906 pdata[1]);
1907 offset = jj + PREFIX_SIZE;
1908 bframe = 1;
1909 cc = pdword[1];
1910 if (cc >= MAX_CHANNELS) {
1911 printk(KERN_ERR
1912 "bad channel\n");
1913 return -EINVAL;
1914 }
1915 /* reverse it */
1916 dev->cc = G_chnmap[cc];
1917 payload = pdword[3];
1918 if (payload > dev->req_image_size[dev->cc]) {
1919 dev->bad_payload[dev->cc]++;
1920 /* discard the bad frame */
1921 return -EINVAL;
1922 }
1923 dev->pkt_size[dev->cc] = payload;
1924 dev->jpg_size[dev->cc] = pdword[4];
1925 break;
1926 case S2255_MARKER_RESPONSE:
1927 pdword = (s32 *)pdata;
1928 pdata += DEF_USB_BLOCK;
1929 jj += DEF_USB_BLOCK;
1930 if (pdword[1] >= MAX_CHANNELS)
1931 break;
1932 cc = G_chnmap[pdword[1]];
1933 if (!(cc >= 0 && cc < MAX_CHANNELS))
1934 break;
1935 switch (pdword[2]) {
1936 case 0x01:
1937 /* check if channel valid */
1938 /* set mode ready */
1939 dev->setmode_ready[cc] = 1;
1940 wake_up(&dev->wait_setmode[cc]);
1941 dprintk(5, "setmode ready %d\n", cc);
1942 break;
1943 case 0x10:
1944
1945 dev->chn_ready |= (1 << cc);
1946 if ((dev->chn_ready & 0x0f) != 0x0f)
1947 break;
1948 /* all channels ready */
1949 printk(KERN_INFO "s2255: fw loaded\n");
1950 atomic_set(&dev->fw_data->fw_state,
1951 S2255_FW_SUCCESS);
1952 wake_up(&dev->fw_data->wait_fw);
1953 break;
1954 default:
1955 printk(KERN_INFO "s2255 unknwn resp\n");
1956 }
1957 default:
1958 pdata++;
1959 break;
1960 }
1961 if (bframe)
1962 break;
1963 } /* for */
1964 if (!bframe)
1965 return -EINVAL;
1966 }
1967
1968
1969 idx = dev->cur_frame[dev->cc];
1970 frm = &dev->buffer[dev->cc].frame[idx];
1971
1972 /* search done. now find out if should be acquiring on this channel */
1973 if (!dev->b_acquire[dev->cc]) {
1974 /* we found a frame, but this channel is turned off */
1975 frm->ulState = S2255_READ_IDLE;
1976 return -EINVAL;
1977 }
1978
1979 if (frm->ulState == S2255_READ_IDLE) {
1980 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03001981 frm->cur_size = 0;
1982 }
1983
Dean Anderson14d96262008-08-25 13:58:55 -03001984 /* skip the marker 512 bytes (and offset if out of sync) */
1985 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1986
Dean Anderson38f993a2008-06-26 23:15:51 -03001987
1988 if (frm->lpvbits == NULL) {
1989 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1990 frm, dev, dev->cc, idx);
1991 return -ENOMEM;
1992 }
1993
1994 pdest = frm->lpvbits + frm->cur_size;
1995
Dean Anderson14d96262008-08-25 13:58:55 -03001996 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03001997
Dean Anderson14d96262008-08-25 13:58:55 -03001998 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001999
Dean Anderson14d96262008-08-25 13:58:55 -03002000 /* sanity check on pdest */
2001 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
2002 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002003
Dean Anderson38f993a2008-06-26 23:15:51 -03002004 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002005 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002006
Dean Anderson14d96262008-08-25 13:58:55 -03002007 if (frm->cur_size >= size) {
2008
Dean Anderson38f993a2008-06-26 23:15:51 -03002009 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002010 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2011 cc, idx);
2012 dev->last_frame[cc] = dev->cur_frame[cc];
2013 dev->cur_frame[cc]++;
2014 /* end of system frame ring buffer, start at zero */
2015 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2016 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2017 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002018 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002019 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002020 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002021 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002022 frm->ulState = S2255_READ_IDLE;
2023 frm->cur_size = 0;
2024
Dean Anderson38f993a2008-06-26 23:15:51 -03002025 }
2026 /* done successfully */
2027 return 0;
2028}
2029
2030static void s2255_read_video_callback(struct s2255_dev *dev,
2031 struct s2255_pipeinfo *pipe_info)
2032{
2033 int res;
2034 dprintk(50, "callback read video \n");
2035
2036 if (dev->cc >= MAX_CHANNELS) {
2037 dev->cc = 0;
2038 dev_err(&dev->udev->dev, "invalid channel\n");
2039 return;
2040 }
2041 /* otherwise copy to the system buffers */
2042 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002043 if (res != 0)
2044 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002045
2046 dprintk(50, "callback read video done\n");
2047 return;
2048}
2049
2050static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2051 u16 Index, u16 Value, void *TransferBuffer,
2052 s32 TransferBufferLength, int bOut)
2053{
2054 int r;
2055 if (!bOut) {
2056 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2057 Request,
2058 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2059 USB_DIR_IN,
2060 Value, Index, TransferBuffer,
2061 TransferBufferLength, HZ * 5);
2062 } else {
2063 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2064 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2065 Value, Index, TransferBuffer,
2066 TransferBufferLength, HZ * 5);
2067 }
2068 return r;
2069}
2070
2071/*
2072 * retrieve FX2 firmware version. future use.
2073 * @param dev pointer to device extension
2074 * @return -1 for fail, else returns firmware version as an int(16 bits)
2075 */
2076static int s2255_get_fx2fw(struct s2255_dev *dev)
2077{
2078 int fw;
2079 int ret;
2080 unsigned char transBuffer[64];
2081 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2082 S2255_VR_IN);
2083 if (ret < 0)
2084 dprintk(2, "get fw error: %x\n", ret);
2085 fw = transBuffer[0] + (transBuffer[1] << 8);
2086 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2087 return fw;
2088}
2089
2090/*
2091 * Create the system ring buffer to copy frames into from the
2092 * usb read pipe.
2093 */
2094static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2095{
2096 unsigned long i;
2097 unsigned long reqsize;
2098 dprintk(1, "create sys buffers\n");
2099 if (chn >= MAX_CHANNELS)
2100 return -1;
2101
2102 dev->buffer[chn].dwFrames = SYS_FRAMES;
2103
2104 /* always allocate maximum size(PAL) for system buffers */
2105 reqsize = SYS_FRAMES_MAXSIZE;
2106
2107 if (reqsize > SYS_FRAMES_MAXSIZE)
2108 reqsize = SYS_FRAMES_MAXSIZE;
2109
2110 for (i = 0; i < SYS_FRAMES; i++) {
2111 /* allocate the frames */
2112 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2113
2114 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2115 &dev->buffer[chn].frame[i], chn, i,
2116 dev->buffer[chn].frame[i].lpvbits);
2117 dev->buffer[chn].frame[i].size = reqsize;
2118 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2119 printk(KERN_INFO "out of memory. using less frames\n");
2120 dev->buffer[chn].dwFrames = i;
2121 break;
2122 }
2123 }
2124
2125 /* make sure internal states are set */
2126 for (i = 0; i < SYS_FRAMES; i++) {
2127 dev->buffer[chn].frame[i].ulState = 0;
2128 dev->buffer[chn].frame[i].cur_size = 0;
2129 }
2130
2131 dev->cur_frame[chn] = 0;
2132 dev->last_frame[chn] = -1;
2133 return 0;
2134}
2135
2136static int s2255_release_sys_buffers(struct s2255_dev *dev,
2137 unsigned long channel)
2138{
2139 unsigned long i;
2140 dprintk(1, "release sys buffers\n");
2141 for (i = 0; i < SYS_FRAMES; i++) {
2142 if (dev->buffer[channel].frame[i].lpvbits) {
2143 dprintk(1, "vfree %p\n",
2144 dev->buffer[channel].frame[i].lpvbits);
2145 vfree(dev->buffer[channel].frame[i].lpvbits);
2146 }
2147 dev->buffer[channel].frame[i].lpvbits = NULL;
2148 }
2149 return 0;
2150}
2151
2152static int s2255_board_init(struct s2255_dev *dev)
2153{
2154 int j;
2155 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2156 int fw_ver;
2157 dprintk(4, "board init: %p", dev);
2158
2159 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2160 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2161
2162 memset(pipe, 0, sizeof(*pipe));
2163 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002164 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2165 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002166
Dean Anderson38f993a2008-06-26 23:15:51 -03002167 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2168 GFP_KERNEL);
2169 if (pipe->transfer_buffer == NULL) {
2170 dprintk(1, "out of memory!\n");
2171 return -ENOMEM;
2172 }
2173
2174 }
2175
2176 /* query the firmware */
2177 fw_ver = s2255_get_fx2fw(dev);
2178
2179 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2180 if (fw_ver < CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002181 dev_err(&dev->udev->dev,
2182 "usb firmware not up to date %d\n", fw_ver);
Dean Anderson38f993a2008-06-26 23:15:51 -03002183
2184 for (j = 0; j < MAX_CHANNELS; j++) {
2185 dev->b_acquire[j] = 0;
2186 dev->mode[j] = mode_def;
Dean Anderson22b88d42008-08-29 15:33:19 -03002187 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002188 dev->cur_fmt[j] = &formats[0];
2189 dev->mode[j].restart = 1;
2190 dev->req_image_size[j] = get_transfer_size(&mode_def);
2191 dev->frame_count[j] = 0;
2192 /* create the system buffers */
2193 s2255_create_sys_buffers(dev, j);
2194 }
2195 /* start read pipe */
2196 s2255_start_readpipe(dev);
2197
2198 dprintk(1, "S2255: board initialized\n");
2199 return 0;
2200}
2201
2202static int s2255_board_shutdown(struct s2255_dev *dev)
2203{
2204 u32 i;
2205
2206 dprintk(1, "S2255: board shutdown: %p", dev);
2207
2208 for (i = 0; i < MAX_CHANNELS; i++) {
2209 if (dev->b_acquire[i])
2210 s2255_stop_acquire(dev, i);
2211 }
2212
2213 s2255_stop_readpipe(dev);
2214
2215 for (i = 0; i < MAX_CHANNELS; i++)
2216 s2255_release_sys_buffers(dev, i);
2217
2218 /* release transfer buffers */
2219 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2220 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2221 kfree(pipe->transfer_buffer);
2222 }
2223 return 0;
2224}
2225
2226static void read_pipe_completion(struct urb *purb)
2227{
2228 struct s2255_pipeinfo *pipe_info;
2229 struct s2255_dev *dev;
2230 int status;
2231 int pipe;
2232
2233 pipe_info = purb->context;
2234 dprintk(100, "read pipe completion %p, status %d\n", purb,
2235 purb->status);
2236 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002237 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002238 return;
2239 }
2240
2241 dev = pipe_info->dev;
2242 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002243 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002244 return;
2245 }
2246 status = purb->status;
2247 if (status != 0) {
2248 dprintk(2, "read_pipe_completion: err\n");
2249 return;
2250 }
2251
2252 if (pipe_info->state == 0) {
2253 dprintk(2, "exiting USB pipe");
2254 return;
2255 }
2256
2257 s2255_read_video_callback(dev, pipe_info);
2258
2259 pipe_info->err_count = 0;
2260 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2261 /* reuse urb */
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 if (pipe_info->state != 0) {
2269 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2270 dev_err(&dev->udev->dev, "error submitting urb\n");
2271 usb_free_urb(pipe_info->stream_urb);
2272 }
2273 } else {
2274 dprintk(2, "read pipe complete state 0\n");
2275 }
2276 return;
2277}
2278
2279static int s2255_start_readpipe(struct s2255_dev *dev)
2280{
2281 int pipe;
2282 int retval;
2283 int i;
2284 struct s2255_pipeinfo *pipe_info = dev->pipes;
2285 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2286 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2287
2288 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2289 pipe_info->state = 1;
2290 pipe_info->buf_index = (u32) i;
2291 pipe_info->priority_set = 0;
2292 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2293 if (!pipe_info->stream_urb) {
2294 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002295 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002296 return -ENOMEM;
2297 }
2298 /* transfer buffer allocated in board_init */
2299 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2300 pipe,
2301 pipe_info->transfer_buffer,
2302 pipe_info->cur_transfer_size,
2303 read_pipe_completion, pipe_info);
2304
2305 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2306 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2307 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2308 if (retval) {
2309 printk(KERN_ERR "s2255: start read pipe failed\n");
2310 return retval;
2311 }
2312 }
2313
2314 return 0;
2315}
2316
2317/* starts acquisition process */
2318static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2319{
2320 unsigned char *buffer;
2321 int res;
2322 unsigned long chn_rev;
2323 int j;
2324 if (chn >= MAX_CHANNELS) {
2325 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2326 return -1;
2327 }
2328
2329 chn_rev = G_chnmap[chn];
2330 dprintk(1, "S2255: start acquire %lu \n", chn);
2331
2332 buffer = kzalloc(512, GFP_KERNEL);
2333 if (buffer == NULL) {
2334 dev_err(&dev->udev->dev, "out of mem\n");
2335 return -ENOMEM;
2336 }
2337
2338 dev->last_frame[chn] = -1;
2339 dev->bad_payload[chn] = 0;
2340 dev->cur_frame[chn] = 0;
2341 for (j = 0; j < SYS_FRAMES; j++) {
2342 dev->buffer[chn].frame[j].ulState = 0;
2343 dev->buffer[chn].frame[j].cur_size = 0;
2344 }
2345
2346 /* send the start command */
2347 *(u32 *) buffer = IN_DATA_TOKEN;
2348 *((u32 *) buffer + 1) = (u32) chn_rev;
2349 *((u32 *) buffer + 2) = (u32) CMD_START;
2350 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2351 if (res != 0)
2352 dev_err(&dev->udev->dev, "CMD_START error\n");
2353
2354 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2355 kfree(buffer);
2356 return 0;
2357}
2358
2359static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2360{
2361 unsigned char *buffer;
2362 int res;
2363 unsigned long chn_rev;
2364
2365 if (chn >= MAX_CHANNELS) {
2366 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2367 return -1;
2368 }
2369 chn_rev = G_chnmap[chn];
2370
2371 buffer = kzalloc(512, GFP_KERNEL);
2372 if (buffer == NULL) {
2373 dev_err(&dev->udev->dev, "out of mem\n");
2374 return -ENOMEM;
2375 }
2376
2377 /* send the stop command */
2378 dprintk(4, "stop acquire %lu\n", chn);
2379 *(u32 *) buffer = IN_DATA_TOKEN;
2380 *((u32 *) buffer + 1) = (u32) chn_rev;
2381 *((u32 *) buffer + 2) = CMD_STOP;
2382 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2383
2384 if (res != 0)
2385 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2386
2387 dprintk(4, "stop acquire: releasing states \n");
2388
2389 kfree(buffer);
2390 dev->b_acquire[chn] = 0;
2391
Dean Anderson14d96262008-08-25 13:58:55 -03002392 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002393}
2394
2395static void s2255_stop_readpipe(struct s2255_dev *dev)
2396{
2397 int j;
2398
2399 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002400 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002401 return;
2402 }
2403 dprintk(4, "stop read pipe\n");
2404 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2405 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2406 if (pipe_info) {
2407 if (pipe_info->state == 0)
2408 continue;
2409 pipe_info->state = 0;
2410 pipe_info->prev_state = 1;
2411
2412 }
2413 }
2414
2415 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2416 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2417 if (pipe_info->stream_urb) {
2418 /* cancel urb */
2419 usb_kill_urb(pipe_info->stream_urb);
2420 usb_free_urb(pipe_info->stream_urb);
2421 pipe_info->stream_urb = NULL;
2422 }
2423 }
2424 dprintk(2, "s2255 stop read pipe: %d\n", j);
2425 return;
2426}
2427
Dean Anderson14d96262008-08-25 13:58:55 -03002428static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002429{
Dean Anderson14d96262008-08-25 13:58:55 -03002430 if (reset)
2431 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002432 dev->fw_data->fw_size = dev->fw_data->fw->size;
2433 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2434 memcpy(dev->fw_data->pfw_data,
2435 dev->fw_data->fw->data, CHUNK_SIZE);
2436 dev->fw_data->fw_loaded = CHUNK_SIZE;
2437 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2438 usb_sndbulkpipe(dev->udev, 2),
2439 dev->fw_data->pfw_data,
2440 CHUNK_SIZE, s2255_fwchunk_complete,
2441 dev->fw_data);
2442 mod_timer(&dev->timer, jiffies + HZ);
2443}
2444
2445/* standard usb probe function */
2446static int s2255_probe(struct usb_interface *interface,
2447 const struct usb_device_id *id)
2448{
2449 struct s2255_dev *dev = NULL;
2450 struct usb_host_interface *iface_desc;
2451 struct usb_endpoint_descriptor *endpoint;
2452 int i;
2453 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002454 __le32 *pdata;
2455 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002456
2457 dprintk(2, "s2255: probe\n");
2458
2459 /* allocate memory for our device state and initialize it to zero */
2460 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2461 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002462 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002463 goto error;
2464 }
2465
2466 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2467 if (!dev->fw_data)
2468 goto error;
2469
2470 mutex_init(&dev->lock);
2471 mutex_init(&dev->open_lock);
2472
2473 /* grab usb_device and save it */
2474 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2475 if (dev->udev == NULL) {
2476 dev_err(&interface->dev, "null usb device\n");
2477 retval = -ENODEV;
2478 goto error;
2479 }
2480 kref_init(&dev->kref);
2481 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2482 dev->udev, interface);
2483 dev->interface = interface;
2484 /* set up the endpoint information */
2485 iface_desc = interface->cur_altsetting;
2486 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2487 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2488 endpoint = &iface_desc->endpoint[i].desc;
2489 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2490 /* we found the bulk in endpoint */
2491 dev->read_endpoint = endpoint->bEndpointAddress;
2492 }
2493 }
2494
2495 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002496 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002497 goto error;
2498 }
2499
2500 /* set intfdata */
2501 usb_set_intfdata(interface, dev);
2502
2503 dprintk(100, "after intfdata %p\n", dev);
2504
2505 init_timer(&dev->timer);
2506 dev->timer.function = s2255_timer;
2507 dev->timer.data = (unsigned long)dev->fw_data;
2508
2509 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03002510 for (i = 0; i < MAX_CHANNELS; i++)
2511 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002512
2513
2514 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2515
2516 if (!dev->fw_data->fw_urb) {
2517 dev_err(&interface->dev, "out of memory!\n");
2518 goto error;
2519 }
2520 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2521 if (!dev->fw_data->pfw_data) {
2522 dev_err(&interface->dev, "out of memory!\n");
2523 goto error;
2524 }
2525 /* load the first chunk */
2526 if (request_firmware(&dev->fw_data->fw,
2527 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2528 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2529 goto error;
2530 }
Dean Anderson14d96262008-08-25 13:58:55 -03002531 /* check the firmware is valid */
2532 fw_size = dev->fw_data->fw->size;
2533 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002534
Dean Anderson14d96262008-08-25 13:58:55 -03002535 if (*pdata != S2255_FW_MARKER) {
2536 printk(KERN_INFO "Firmware invalid.\n");
2537 retval = -ENODEV;
2538 goto error;
2539 } else {
2540 /* make sure firmware is the latest */
2541 __le32 *pRel;
2542 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2543 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2544 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002545 /* loads v4l specific */
2546 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002547 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002548 /* load 2255 board specific */
2549 s2255_board_init(dev);
2550
2551 dprintk(4, "before probe done %p\n", dev);
2552 spin_lock_init(&dev->slock);
2553
Dean Anderson14d96262008-08-25 13:58:55 -03002554 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002555 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2556 return 0;
2557error:
2558 return retval;
2559}
2560
2561/* disconnect routine. when board is removed physically or with rmmod */
2562static void s2255_disconnect(struct usb_interface *interface)
2563{
2564 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002565 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002566 dprintk(1, "s2255: disconnect interface %p\n", interface);
2567 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002568
2569 /*
2570 * wake up any of the timers to allow open_lock to be
2571 * acquired sooner
2572 */
2573 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2574 wake_up(&dev->fw_data->wait_fw);
2575 for (i = 0; i < MAX_CHANNELS; i++) {
2576 dev->setmode_ready[i] = 1;
2577 wake_up(&dev->wait_setmode[i]);
2578 }
2579
2580 mutex_lock(&dev->open_lock);
2581 usb_set_intfdata(interface, NULL);
2582 mutex_unlock(&dev->open_lock);
2583
Dean Anderson38f993a2008-06-26 23:15:51 -03002584 if (dev) {
2585 kref_put(&dev->kref, s2255_destroy);
2586 dprintk(1, "s2255drv: disconnect\n");
2587 dev_info(&interface->dev, "s2255usb now disconnected\n");
2588 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002589}
2590
2591static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002592 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002593 .probe = s2255_probe,
2594 .disconnect = s2255_disconnect,
2595 .id_table = s2255_table,
2596};
2597
2598static int __init usb_s2255_init(void)
2599{
2600 int result;
2601
2602 /* register this driver with the USB subsystem */
2603 result = usb_register(&s2255_driver);
2604
2605 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002606 pr_err(KBUILD_MODNAME
2607 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002608
2609 dprintk(2, "s2255_init: done\n");
2610 return result;
2611}
2612
2613static void __exit usb_s2255_exit(void)
2614{
2615 usb_deregister(&s2255_driver);
2616}
2617
2618module_init(usb_s2255_init);
2619module_exit(usb_s2255_exit);
2620
2621MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2622MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2623MODULE_LICENSE("GPL");