blob: 5202cadb2aaec3f114c33fca69a58d34b756591c [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
Dean Anderson38f993a2008-06-26 23:15:51 -0300725 videobuf_vmalloc_free(&buf->vb);
726 buf->vb.state = VIDEOBUF_NEEDS_INIT;
727}
728
729static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
730 enum v4l2_field field)
731{
732 struct s2255_fh *fh = vq->priv_data;
733 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
734 int rc;
735 dprintk(4, "%s, field=%d\n", __func__, field);
736 if (fh->fmt == NULL)
737 return -EINVAL;
738
739 if ((fh->width < norm_minw(fh->dev->vdev[fh->channel])) ||
740 (fh->width > norm_maxw(fh->dev->vdev[fh->channel])) ||
741 (fh->height < norm_minh(fh->dev->vdev[fh->channel])) ||
742 (fh->height > norm_maxh(fh->dev->vdev[fh->channel]))) {
743 dprintk(4, "invalid buffer prepare\n");
744 return -EINVAL;
745 }
746
747 buf->vb.size = fh->width * fh->height * (fh->fmt->depth >> 3);
748
749 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
750 dprintk(4, "invalid buffer prepare\n");
751 return -EINVAL;
752 }
753
754 buf->fmt = fh->fmt;
755 buf->vb.width = fh->width;
756 buf->vb.height = fh->height;
757 buf->vb.field = field;
758
759
760 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
761 rc = videobuf_iolock(vq, &buf->vb, NULL);
762 if (rc < 0)
763 goto fail;
764 }
765
766 buf->vb.state = VIDEOBUF_PREPARED;
767 return 0;
768fail:
769 free_buffer(vq, buf);
770 return rc;
771}
772
773static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
774{
775 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
776 struct s2255_fh *fh = vq->priv_data;
777 struct s2255_dev *dev = fh->dev;
778 struct s2255_dmaqueue *vidq = &dev->vidq[fh->channel];
779
780 dprintk(1, "%s\n", __func__);
781
782 buf->vb.state = VIDEOBUF_QUEUED;
783 list_add_tail(&buf->vb.queue, &vidq->active);
784}
785
786static void buffer_release(struct videobuf_queue *vq,
787 struct videobuf_buffer *vb)
788{
789 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
790 struct s2255_fh *fh = vq->priv_data;
791 dprintk(4, "%s %d\n", __func__, fh->channel);
792 free_buffer(vq, buf);
793}
794
795static struct videobuf_queue_ops s2255_video_qops = {
796 .buf_setup = buffer_setup,
797 .buf_prepare = buffer_prepare,
798 .buf_queue = buffer_queue,
799 .buf_release = buffer_release,
800};
801
802
803static int res_get(struct s2255_dev *dev, struct s2255_fh *fh)
804{
805 /* is it free? */
806 mutex_lock(&dev->lock);
807 if (dev->resources[fh->channel]) {
808 /* no, someone else uses it */
809 mutex_unlock(&dev->lock);
810 return 0;
811 }
812 /* it's free, grab it */
813 dev->resources[fh->channel] = 1;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300814 fh->resources[fh->channel] = 1;
815 dprintk(1, "s2255: res: get\n");
Dean Anderson38f993a2008-06-26 23:15:51 -0300816 mutex_unlock(&dev->lock);
817 return 1;
818}
819
820static int res_locked(struct s2255_dev *dev, struct s2255_fh *fh)
821{
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300822 return dev->resources[fh->channel];
Dean Anderson38f993a2008-06-26 23:15:51 -0300823}
824
Dean Andersonf78d92c2008-07-22 14:43:27 -0300825static int res_check(struct s2255_fh *fh)
826{
827 return fh->resources[fh->channel];
828}
829
830
Dean Anderson38f993a2008-06-26 23:15:51 -0300831static void res_free(struct s2255_dev *dev, struct s2255_fh *fh)
832{
Dean Andersonf78d92c2008-07-22 14:43:27 -0300833 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300834 dev->resources[fh->channel] = 0;
Dean Andersonf78d92c2008-07-22 14:43:27 -0300835 fh->resources[fh->channel] = 0;
836 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -0300837 dprintk(1, "res: put\n");
838}
839
840
841static int vidioc_querycap(struct file *file, void *priv,
842 struct v4l2_capability *cap)
843{
844 struct s2255_fh *fh = file->private_data;
845 struct s2255_dev *dev = fh->dev;
846 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
847 strlcpy(cap->card, "s2255", sizeof(cap->card));
Thierry MERLEe22ed882009-01-20 18:19:25 -0300848 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Dean Anderson38f993a2008-06-26 23:15:51 -0300849 cap->version = S2255_VERSION;
850 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
851 return 0;
852}
853
854static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
855 struct v4l2_fmtdesc *f)
856{
857 int index = 0;
858 if (f)
859 index = f->index;
860
861 if (index >= ARRAY_SIZE(formats))
862 return -EINVAL;
863
864 dprintk(4, "name %s\n", formats[index].name);
865 strlcpy(f->description, formats[index].name, sizeof(f->description));
866 f->pixelformat = formats[index].fourcc;
867 return 0;
868}
869
870static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
871 struct v4l2_format *f)
872{
873 struct s2255_fh *fh = priv;
874
875 f->fmt.pix.width = fh->width;
876 f->fmt.pix.height = fh->height;
877 f->fmt.pix.field = fh->vb_vidq.field;
878 f->fmt.pix.pixelformat = fh->fmt->fourcc;
879 f->fmt.pix.bytesperline = f->fmt.pix.width * (fh->fmt->depth >> 3);
880 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Dean Anderson3f8d6f72008-06-30 21:28:34 -0300881 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -0300882}
883
884static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
885 struct v4l2_format *f)
886{
887 const struct s2255_fmt *fmt;
888 enum v4l2_field field;
889 int b_any_field = 0;
890 struct s2255_fh *fh = priv;
891 struct s2255_dev *dev = fh->dev;
892 int is_ntsc;
893
894 is_ntsc =
895 (dev->vdev[fh->channel]->current_norm & V4L2_STD_NTSC) ? 1 : 0;
896
897 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
898
899 if (fmt == NULL)
900 return -EINVAL;
901
902 field = f->fmt.pix.field;
903 if (field == V4L2_FIELD_ANY)
904 b_any_field = 1;
905
906 dprintk(4, "try format %d \n", is_ntsc);
907 /* supports 3 sizes. see s2255drv.h */
908 dprintk(50, "width test %d, height %d\n",
909 f->fmt.pix.width, f->fmt.pix.height);
910 if (is_ntsc) {
911 /* NTSC */
912 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
913 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
914 if (b_any_field) {
915 field = V4L2_FIELD_SEQ_TB;
916 } else if (!((field == V4L2_FIELD_INTERLACED) ||
917 (field == V4L2_FIELD_SEQ_TB) ||
918 (field == V4L2_FIELD_INTERLACED_TB))) {
919 dprintk(1, "unsupported field setting\n");
920 return -EINVAL;
921 }
922 } else {
923 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
924 if (b_any_field) {
925 field = V4L2_FIELD_TOP;
926 } else if (!((field == V4L2_FIELD_TOP) ||
927 (field == V4L2_FIELD_BOTTOM))) {
928 dprintk(1, "unsupported field setting\n");
929 return -EINVAL;
930 }
931
932 }
933 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
934 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
935 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
936 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
937 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
938 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
939 else
940 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
941 } else {
942 /* PAL */
943 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
944 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
945 if (b_any_field) {
946 field = V4L2_FIELD_SEQ_TB;
947 } else if (!((field == V4L2_FIELD_INTERLACED) ||
948 (field == V4L2_FIELD_SEQ_TB) ||
949 (field == V4L2_FIELD_INTERLACED_TB))) {
950 dprintk(1, "unsupported field setting\n");
951 return -EINVAL;
952 }
953 } else {
954 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
955 if (b_any_field) {
956 field = V4L2_FIELD_TOP;
957 } else if (!((field == V4L2_FIELD_TOP) ||
958 (field == V4L2_FIELD_BOTTOM))) {
959 dprintk(1, "unsupported field setting\n");
960 return -EINVAL;
961 }
962 }
963 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
964 dprintk(50, "pal 704\n");
965 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
966 field = V4L2_FIELD_SEQ_TB;
967 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
968 dprintk(50, "pal 352A\n");
969 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
970 field = V4L2_FIELD_TOP;
971 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
972 dprintk(50, "pal 352B\n");
973 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
974 field = V4L2_FIELD_TOP;
975 } else {
976 dprintk(50, "pal 352C\n");
977 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
978 field = V4L2_FIELD_TOP;
979 }
980 }
981
982 dprintk(50, "width %d height %d field %d \n", f->fmt.pix.width,
983 f->fmt.pix.height, f->fmt.pix.field);
984 f->fmt.pix.field = field;
985 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
986 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
987 return 0;
988}
989
990static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
991 struct v4l2_format *f)
992{
993 struct s2255_fh *fh = priv;
994 const struct s2255_fmt *fmt;
995 struct videobuf_queue *q = &fh->vb_vidq;
996 int ret;
997 int norm;
998
999 ret = vidioc_try_fmt_vid_cap(file, fh, f);
1000
1001 if (ret < 0)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001002 return ret;
Dean Anderson38f993a2008-06-26 23:15:51 -03001003
1004 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1005
1006 if (fmt == NULL)
1007 return -EINVAL;
1008
1009 mutex_lock(&q->vb_lock);
1010
1011 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1012 dprintk(1, "queue busy\n");
1013 ret = -EBUSY;
1014 goto out_s_fmt;
1015 }
1016
1017 if (res_locked(fh->dev, fh)) {
1018 dprintk(1, "can't change format after started\n");
1019 ret = -EBUSY;
1020 goto out_s_fmt;
1021 }
1022
1023 fh->fmt = fmt;
1024 fh->width = f->fmt.pix.width;
1025 fh->height = f->fmt.pix.height;
1026 fh->vb_vidq.field = f->fmt.pix.field;
1027 fh->type = f->type;
1028 norm = norm_minw(fh->dev->vdev[fh->channel]);
1029 if (fh->width > norm_minw(fh->dev->vdev[fh->channel])) {
1030 if (fh->height > norm_minh(fh->dev->vdev[fh->channel]))
1031 fh->mode.scale = SCALE_4CIFS;
1032 else
1033 fh->mode.scale = SCALE_2CIFS;
1034
1035 } else {
1036 fh->mode.scale = SCALE_1CIFS;
1037 }
1038
1039 /* color mode */
1040 switch (fh->fmt->fourcc) {
1041 case V4L2_PIX_FMT_GREY:
1042 fh->mode.color = COLOR_Y8;
1043 break;
Dean Anderson14d96262008-08-25 13:58:55 -03001044 case V4L2_PIX_FMT_JPEG:
Dean Anderson22b88d42008-08-29 15:33:19 -03001045 fh->mode.color = COLOR_JPG |
1046 (fh->dev->jc[fh->channel].quality << 8);
Dean Anderson14d96262008-08-25 13:58:55 -03001047 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001048 case V4L2_PIX_FMT_YUV422P:
1049 fh->mode.color = COLOR_YUVPL;
1050 break;
1051 case V4L2_PIX_FMT_YUYV:
1052 case V4L2_PIX_FMT_UYVY:
1053 default:
1054 fh->mode.color = COLOR_YUVPK;
1055 break;
1056 }
1057 ret = 0;
1058out_s_fmt:
1059 mutex_unlock(&q->vb_lock);
1060 return ret;
1061}
1062
1063static int vidioc_reqbufs(struct file *file, void *priv,
1064 struct v4l2_requestbuffers *p)
1065{
1066 int rc;
1067 struct s2255_fh *fh = priv;
1068 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1069 return rc;
1070}
1071
1072static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1073{
1074 int rc;
1075 struct s2255_fh *fh = priv;
1076 rc = videobuf_querybuf(&fh->vb_vidq, p);
1077 return rc;
1078}
1079
1080static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1081{
1082 int rc;
1083 struct s2255_fh *fh = priv;
1084 rc = videobuf_qbuf(&fh->vb_vidq, p);
1085 return rc;
1086}
1087
1088static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1089{
1090 int rc;
1091 struct s2255_fh *fh = priv;
1092 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1093 return rc;
1094}
1095
1096#ifdef CONFIG_VIDEO_V4L1_COMPAT
1097static int vidioc_cgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1098{
1099 struct s2255_fh *fh = priv;
1100
1101 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1102}
1103#endif
1104
1105/* write to the configuration pipe, synchronously */
1106static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1107 int size)
1108{
1109 int pipe;
1110 int done;
1111 long retval = -1;
1112 if (udev) {
1113 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1114 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1115 }
1116 return retval;
1117}
1118
1119static u32 get_transfer_size(struct s2255_mode *mode)
1120{
1121 int linesPerFrame = LINE_SZ_DEF;
1122 int pixelsPerLine = NUM_LINES_DEF;
1123 u32 outImageSize;
1124 u32 usbInSize;
1125 unsigned int mask_mult;
1126
1127 if (mode == NULL)
1128 return 0;
1129
1130 if (mode->format == FORMAT_NTSC) {
1131 switch (mode->scale) {
1132 case SCALE_4CIFS:
1133 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1134 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1135 break;
1136 case SCALE_2CIFS:
1137 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1138 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1139 break;
1140 case SCALE_1CIFS:
1141 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1142 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1143 break;
1144 default:
1145 break;
1146 }
1147 } else if (mode->format == FORMAT_PAL) {
1148 switch (mode->scale) {
1149 case SCALE_4CIFS:
1150 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1151 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1152 break;
1153 case SCALE_2CIFS:
1154 linesPerFrame = NUM_LINES_2CIFS_PAL;
1155 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1156 break;
1157 case SCALE_1CIFS:
1158 linesPerFrame = NUM_LINES_1CIFS_PAL;
1159 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1160 break;
1161 default:
1162 break;
1163 }
1164 }
1165 outImageSize = linesPerFrame * pixelsPerLine;
Dean Anderson14d96262008-08-25 13:58:55 -03001166 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001167 /* 2 bytes/pixel if not monochrome */
1168 outImageSize *= 2;
1169 }
1170
1171 /* total bytes to send including prefix and 4K padding;
1172 must be a multiple of USB_READ_SIZE */
1173 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1174 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1175 /* if size not a multiple of USB_READ_SIZE */
1176 if (usbInSize & ~mask_mult)
1177 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1178 return usbInSize;
1179}
1180
1181static void dump_verify_mode(struct s2255_dev *sdev, struct s2255_mode *mode)
1182{
1183 struct device *dev = &sdev->udev->dev;
1184 dev_info(dev, "------------------------------------------------\n");
1185 dev_info(dev, "verify mode\n");
1186 dev_info(dev, "format: %d\n", mode->format);
1187 dev_info(dev, "scale: %d\n", mode->scale);
1188 dev_info(dev, "fdec: %d\n", mode->fdec);
1189 dev_info(dev, "color: %d\n", mode->color);
1190 dev_info(dev, "bright: 0x%x\n", mode->bright);
1191 dev_info(dev, "restart: 0x%x\n", mode->restart);
1192 dev_info(dev, "usb_block: 0x%x\n", mode->usb_block);
1193 dev_info(dev, "single: 0x%x\n", mode->single);
1194 dev_info(dev, "------------------------------------------------\n");
1195}
1196
1197/*
1198 * set mode is the function which controls the DSP.
1199 * the restart parameter in struct s2255_mode should be set whenever
1200 * the image size could change via color format, video system or image
1201 * size.
1202 * When the restart parameter is set, we sleep for ONE frame to allow the
1203 * DSP time to get the new frame
1204 */
1205static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1206 struct s2255_mode *mode)
1207{
1208 int res;
1209 u32 *buffer;
1210 unsigned long chn_rev;
1211
Dean Anderson14d96262008-08-25 13:58:55 -03001212 mutex_lock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001213 chn_rev = G_chnmap[chn];
1214 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1215 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
1216 dev->mode[chn].scale);
1217 dprintk(2, "mode contrast %x\n", mode->contrast);
1218
Dean Anderson22b88d42008-08-29 15:33:19 -03001219 /* if JPEG, set the quality */
1220 if ((mode->color & MASK_COLOR) == COLOR_JPG)
1221 mode->color = (dev->jc[chn].quality << 8) | COLOR_JPG;
1222
Dean Anderson38f993a2008-06-26 23:15:51 -03001223 /* save the mode */
1224 dev->mode[chn] = *mode;
1225 dev->req_image_size[chn] = get_transfer_size(mode);
1226 dprintk(1, "transfer size %ld\n", dev->req_image_size[chn]);
1227
1228 buffer = kzalloc(512, GFP_KERNEL);
1229 if (buffer == NULL) {
1230 dev_err(&dev->udev->dev, "out of mem\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001231 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001232 return -ENOMEM;
1233 }
1234
1235 /* set the mode */
1236 buffer[0] = IN_DATA_TOKEN;
1237 buffer[1] = (u32) chn_rev;
1238 buffer[2] = CMD_SET_MODE;
1239 memcpy(&buffer[3], &dev->mode[chn], sizeof(struct s2255_mode));
1240 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1241 if (debug)
1242 dump_verify_mode(dev, mode);
1243 kfree(buffer);
1244 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1245
1246 /* wait at least 3 frames before continuing */
Dean Anderson14d96262008-08-25 13:58:55 -03001247 if (mode->restart) {
1248 dev->setmode_ready[chn] = 0;
1249 wait_event_timeout(dev->wait_setmode[chn],
1250 (dev->setmode_ready[chn] != 0),
1251 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1252 if (dev->setmode_ready[chn] != 1) {
1253 printk(KERN_DEBUG "s2255: no set mode response\n");
1254 res = -EFAULT;
1255 }
1256 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001257
1258 /* clear the restart flag */
1259 dev->mode[chn].restart = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001260 mutex_unlock(&dev->lock);
Dean Anderson38f993a2008-06-26 23:15:51 -03001261 return res;
1262}
1263
1264static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1265{
1266 int res;
1267 struct s2255_fh *fh = priv;
1268 struct s2255_dev *dev = fh->dev;
1269 struct s2255_mode *new_mode;
1270 struct s2255_mode *old_mode;
1271 int chn;
1272 int j;
1273 dprintk(4, "%s\n", __func__);
1274 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1275 dev_err(&dev->udev->dev, "invalid fh type0\n");
1276 return -EINVAL;
1277 }
1278 if (i != fh->type) {
1279 dev_err(&dev->udev->dev, "invalid fh type1\n");
1280 return -EINVAL;
1281 }
1282
1283 if (!res_get(dev, fh)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001284 s2255_dev_err(&dev->udev->dev, "stream busy\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001285 return -EBUSY;
1286 }
1287
1288 /* send a set mode command everytime with restart.
1289 in case we switch resolutions or other parameters */
1290 chn = fh->channel;
1291 new_mode = &fh->mode;
1292 old_mode = &fh->dev->mode[chn];
1293
1294 if (new_mode->color != old_mode->color)
1295 new_mode->restart = 1;
1296 else if (new_mode->scale != old_mode->scale)
1297 new_mode->restart = 1;
1298 else if (new_mode->format != old_mode->format)
1299 new_mode->restart = 1;
1300
1301 s2255_set_mode(dev, chn, new_mode);
1302 new_mode->restart = 0;
1303 *old_mode = *new_mode;
1304 dev->cur_fmt[chn] = fh->fmt;
1305 dprintk(1, "%s[%d]\n", __func__, chn);
1306 dev->last_frame[chn] = -1;
1307 dev->bad_payload[chn] = 0;
1308 dev->cur_frame[chn] = 0;
Dean Andersona1c45302008-09-09 12:29:56 -03001309 dev->frame_count[chn] = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001310 for (j = 0; j < SYS_FRAMES; j++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001311 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001312 dev->buffer[chn].frame[j].cur_size = 0;
1313 }
1314 res = videobuf_streamon(&fh->vb_vidq);
1315 if (res == 0) {
1316 s2255_start_acquire(dev, chn);
1317 dev->b_acquire[chn] = 1;
1318 } else {
1319 res_free(dev, fh);
1320 }
1321 return res;
1322}
1323
1324static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1325{
Dean Anderson38f993a2008-06-26 23:15:51 -03001326 struct s2255_fh *fh = priv;
1327 struct s2255_dev *dev = fh->dev;
1328
1329 dprintk(4, "%s\n, channel: %d", __func__, fh->channel);
1330 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1331 printk(KERN_ERR "invalid fh type0\n");
1332 return -EINVAL;
1333 }
1334 if (i != fh->type) {
1335 printk(KERN_ERR "invalid type i\n");
1336 return -EINVAL;
1337 }
1338 s2255_stop_acquire(dev, fh->channel);
Dean Andersonb7732a32009-03-30 11:59:56 -03001339 videobuf_streamoff(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001340 res_free(dev, fh);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001341 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001342}
1343
1344static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1345{
1346 struct s2255_fh *fh = priv;
1347 struct s2255_mode *mode;
1348 struct videobuf_queue *q = &fh->vb_vidq;
1349 int ret = 0;
1350
1351 mutex_lock(&q->vb_lock);
1352 if (videobuf_queue_is_busy(q)) {
1353 dprintk(1, "queue busy\n");
1354 ret = -EBUSY;
1355 goto out_s_std;
1356 }
1357
1358 if (res_locked(fh->dev, fh)) {
1359 dprintk(1, "can't change standard after started\n");
1360 ret = -EBUSY;
1361 goto out_s_std;
1362 }
1363 mode = &fh->mode;
1364
1365 if (*i & V4L2_STD_NTSC) {
1366 dprintk(4, "vidioc_s_std NTSC\n");
1367 mode->format = FORMAT_NTSC;
1368 } else if (*i & V4L2_STD_PAL) {
1369 dprintk(4, "vidioc_s_std PAL\n");
1370 mode->format = FORMAT_PAL;
1371 } else {
1372 ret = -EINVAL;
1373 }
1374out_s_std:
1375 mutex_unlock(&q->vb_lock);
1376 return ret;
1377}
1378
1379/* Sensoray 2255 is a multiple channel capture device.
1380 It does not have a "crossbar" of inputs.
1381 We use one V4L device per channel. The user must
1382 be aware that certain combinations are not allowed.
1383 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1384 at once in color(you can do full fps on 4 channels with greyscale.
1385*/
1386static int vidioc_enum_input(struct file *file, void *priv,
1387 struct v4l2_input *inp)
1388{
1389 if (inp->index != 0)
1390 return -EINVAL;
1391
1392 inp->type = V4L2_INPUT_TYPE_CAMERA;
1393 inp->std = S2255_NORMS;
1394 strlcpy(inp->name, "Camera", sizeof(inp->name));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001395 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001396}
1397
1398static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1399{
1400 *i = 0;
1401 return 0;
1402}
1403static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1404{
1405 if (i > 0)
1406 return -EINVAL;
1407 return 0;
1408}
1409
1410/* --- controls ---------------------------------------------- */
1411static int vidioc_queryctrl(struct file *file, void *priv,
1412 struct v4l2_queryctrl *qc)
1413{
1414 int i;
1415
1416 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1417 if (qc->id && qc->id == s2255_qctrl[i].id) {
1418 memcpy(qc, &(s2255_qctrl[i]), sizeof(*qc));
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001419 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001420 }
1421
1422 dprintk(4, "query_ctrl -EINVAL %d\n", qc->id);
1423 return -EINVAL;
1424}
1425
1426static int vidioc_g_ctrl(struct file *file, void *priv,
1427 struct v4l2_control *ctrl)
1428{
1429 int i;
1430
1431 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1432 if (ctrl->id == s2255_qctrl[i].id) {
1433 ctrl->value = qctl_regs[i];
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001434 return 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001435 }
1436 dprintk(4, "g_ctrl -EINVAL\n");
1437
1438 return -EINVAL;
1439}
1440
1441static int vidioc_s_ctrl(struct file *file, void *priv,
1442 struct v4l2_control *ctrl)
1443{
1444 int i;
1445 struct s2255_fh *fh = priv;
1446 struct s2255_dev *dev = fh->dev;
1447 struct s2255_mode *mode;
1448 mode = &fh->mode;
1449 dprintk(4, "vidioc_s_ctrl\n");
1450 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) {
1451 if (ctrl->id == s2255_qctrl[i].id) {
1452 if (ctrl->value < s2255_qctrl[i].minimum ||
1453 ctrl->value > s2255_qctrl[i].maximum)
Dean Anderson3f8d6f72008-06-30 21:28:34 -03001454 return -ERANGE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001455
1456 qctl_regs[i] = ctrl->value;
1457 /* update the mode to the corresponding value */
1458 switch (ctrl->id) {
1459 case V4L2_CID_BRIGHTNESS:
1460 mode->bright = ctrl->value;
1461 break;
1462 case V4L2_CID_CONTRAST:
1463 mode->contrast = ctrl->value;
1464 break;
1465 case V4L2_CID_HUE:
1466 mode->hue = ctrl->value;
1467 break;
1468 case V4L2_CID_SATURATION:
1469 mode->saturation = ctrl->value;
1470 break;
1471 }
1472 mode->restart = 0;
1473 /* set mode here. Note: stream does not need restarted.
1474 some V4L programs restart stream unnecessarily
1475 after a s_crtl.
1476 */
1477 s2255_set_mode(dev, fh->channel, mode);
1478 return 0;
1479 }
1480 }
1481 return -EINVAL;
1482}
1483
Dean Anderson22b88d42008-08-29 15:33:19 -03001484static int vidioc_g_jpegcomp(struct file *file, void *priv,
1485 struct v4l2_jpegcompression *jc)
1486{
1487 struct s2255_fh *fh = priv;
1488 struct s2255_dev *dev = fh->dev;
1489 *jc = dev->jc[fh->channel];
1490 dprintk(2, "getting jpegcompression, quality %d\n", jc->quality);
1491 return 0;
1492}
1493
1494static int vidioc_s_jpegcomp(struct file *file, void *priv,
1495 struct v4l2_jpegcompression *jc)
1496{
1497 struct s2255_fh *fh = priv;
1498 struct s2255_dev *dev = fh->dev;
1499 if (jc->quality < 0 || jc->quality > 100)
1500 return -EINVAL;
1501 dev->jc[fh->channel].quality = jc->quality;
1502 dprintk(2, "setting jpeg quality %d\n", jc->quality);
1503 return 0;
1504}
Hans Verkuilbec43662008-12-30 06:58:20 -03001505static int s2255_open(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001506{
Hans Verkuilbec43662008-12-30 06:58:20 -03001507 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001508 struct s2255_dev *h, *dev = NULL;
1509 struct s2255_fh *fh;
1510 struct list_head *list;
1511 enum v4l2_buf_type type = 0;
1512 int i = 0;
1513 int cur_channel = -1;
Dean Anderson14d96262008-08-25 13:58:55 -03001514 int state;
Dean Anderson38f993a2008-06-26 23:15:51 -03001515 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1516
Hans Verkuild56dc612008-07-30 08:43:36 -03001517 lock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001518 list_for_each(list, &s2255_devlist) {
1519 h = list_entry(list, struct s2255_dev, s2255_devlist);
1520 for (i = 0; i < MAX_CHANNELS; i++) {
1521 if (h->vdev[i]->minor == minor) {
1522 cur_channel = i;
1523 dev = h;
1524 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1525 }
1526 }
1527 }
1528
1529 if ((NULL == dev) || (cur_channel == -1)) {
Hans Verkuild56dc612008-07-30 08:43:36 -03001530 unlock_kernel();
Dean Anderson14d96262008-08-25 13:58:55 -03001531 printk(KERN_INFO "s2255: openv4l no dev\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03001532 return -ENODEV;
1533 }
1534
Dean Anderson14d96262008-08-25 13:58:55 -03001535 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1536 unlock_kernel();
1537 printk(KERN_INFO "disconnecting\n");
1538 return -ENODEV;
1539 }
1540 kref_get(&dev->kref);
Dean Anderson38f993a2008-06-26 23:15:51 -03001541 mutex_lock(&dev->open_lock);
1542
1543 dev->users[cur_channel]++;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001544 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
Dean Anderson38f993a2008-06-26 23:15:51 -03001545
Dean Anderson14d96262008-08-25 13:58:55 -03001546 switch (atomic_read(&dev->fw_data->fw_state)) {
1547 case S2255_FW_FAILED:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001548 s2255_dev_err(&dev->udev->dev,
1549 "firmware load failed. retrying.\n");
Dean Anderson14d96262008-08-25 13:58:55 -03001550 s2255_fwload_start(dev, 1);
Dean Anderson38f993a2008-06-26 23:15:51 -03001551 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001552 ((atomic_read(&dev->fw_data->fw_state)
1553 == S2255_FW_SUCCESS) ||
1554 (atomic_read(&dev->fw_data->fw_state)
1555 == S2255_FW_DISCONNECTING)),
Dean Anderson38f993a2008-06-26 23:15:51 -03001556 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
Dean Anderson14d96262008-08-25 13:58:55 -03001557 break;
1558 case S2255_FW_NOTLOADED:
1559 case S2255_FW_LOADED_DSPWAIT:
Dean Anderson38f993a2008-06-26 23:15:51 -03001560 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1561 driver loaded and then device immediately opened */
1562 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1563 wait_event_timeout(dev->fw_data->wait_fw,
Dean Anderson14d96262008-08-25 13:58:55 -03001564 ((atomic_read(&dev->fw_data->fw_state)
1565 == S2255_FW_SUCCESS) ||
1566 (atomic_read(&dev->fw_data->fw_state)
1567 == S2255_FW_DISCONNECTING)),
1568 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1569 break;
1570 case S2255_FW_SUCCESS:
1571 default:
1572 break;
1573 }
1574 state = atomic_read(&dev->fw_data->fw_state);
1575 if (state != S2255_FW_SUCCESS) {
1576 int rc;
1577 switch (state) {
1578 case S2255_FW_FAILED:
1579 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1580 rc = -ENODEV;
1581 break;
1582 case S2255_FW_DISCONNECTING:
1583 printk(KERN_INFO "%s: disconnecting\n", __func__);
1584 rc = -ENODEV;
1585 break;
1586 case S2255_FW_LOADED_DSPWAIT:
1587 case S2255_FW_NOTLOADED:
1588 printk(KERN_INFO "%s: firmware not loaded yet"
1589 "please try again later\n",
1590 __func__);
1591 rc = -EAGAIN;
1592 break;
1593 default:
1594 printk(KERN_INFO "%s: unknown state\n", __func__);
1595 rc = -EFAULT;
1596 break;
Dean Anderson38f993a2008-06-26 23:15:51 -03001597 }
Dean Anderson14d96262008-08-25 13:58:55 -03001598 dev->users[cur_channel]--;
1599 mutex_unlock(&dev->open_lock);
1600 kref_put(&dev->kref, s2255_destroy);
1601 unlock_kernel();
1602 return rc;
Dean Anderson38f993a2008-06-26 23:15:51 -03001603 }
1604
1605 /* allocate + initialize per filehandle data */
1606 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1607 if (NULL == fh) {
Dean Andersonf78d92c2008-07-22 14:43:27 -03001608 dev->users[cur_channel]--;
Dean Anderson38f993a2008-06-26 23:15:51 -03001609 mutex_unlock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001610 kref_put(&dev->kref, s2255_destroy);
Hans Verkuild56dc612008-07-30 08:43:36 -03001611 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001612 return -ENOMEM;
1613 }
1614
1615 file->private_data = fh;
1616 fh->dev = dev;
1617 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1618 fh->mode = dev->mode[cur_channel];
1619 fh->fmt = dev->cur_fmt[cur_channel];
1620 /* default 4CIF NTSC */
1621 fh->width = LINE_SZ_4CIFS_NTSC;
1622 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1623 fh->channel = cur_channel;
1624
Dean Anderson14d96262008-08-25 13:58:55 -03001625 /* configure channel to default state */
1626 if (!dev->chn_configured[cur_channel]) {
1627 s2255_set_mode(dev, cur_channel, &fh->mode);
1628 dev->chn_configured[cur_channel] = 1;
1629 }
1630
1631
Dean Anderson38f993a2008-06-26 23:15:51 -03001632 /* Put all controls at a sane state */
1633 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1634 qctl_regs[i] = s2255_qctrl[i].default_value;
1635
1636 dprintk(1, "s2255drv: open minor=%d type=%s users=%d\n",
1637 minor, v4l2_type_names[type], dev->users[cur_channel]);
1638 dprintk(2, "s2255drv: open: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n",
1639 (unsigned long)fh, (unsigned long)dev,
1640 (unsigned long)&dev->vidq[cur_channel]);
1641 dprintk(4, "s2255drv: open: list_empty active=%d\n",
1642 list_empty(&dev->vidq[cur_channel].active));
1643
1644 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1645 NULL, &dev->slock,
1646 fh->type,
1647 V4L2_FIELD_INTERLACED,
1648 sizeof(struct s2255_buffer), fh);
1649
Dean Anderson38f993a2008-06-26 23:15:51 -03001650 mutex_unlock(&dev->open_lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001651 unlock_kernel();
Dean Anderson38f993a2008-06-26 23:15:51 -03001652 return 0;
1653}
1654
1655
1656static unsigned int s2255_poll(struct file *file,
1657 struct poll_table_struct *wait)
1658{
1659 struct s2255_fh *fh = file->private_data;
1660 int rc;
1661 dprintk(100, "%s\n", __func__);
1662
1663 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1664 return POLLERR;
1665
1666 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1667 return rc;
1668}
1669
1670static void s2255_destroy(struct kref *kref)
1671{
1672 struct s2255_dev *dev = to_s2255_dev(kref);
Dean Anderson14d96262008-08-25 13:58:55 -03001673 struct list_head *list;
1674 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001675 if (!dev) {
1676 printk(KERN_ERR "s2255drv: kref problem\n");
1677 return;
1678 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001679 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1680 wake_up(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03001681 for (i = 0; i < MAX_CHANNELS; i++) {
1682 dev->setmode_ready[i] = 1;
1683 wake_up(&dev->wait_setmode[i]);
1684 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001685 mutex_lock(&dev->open_lock);
Dean Anderson14d96262008-08-25 13:58:55 -03001686 /* reset the DSP so firmware can be reload next time */
1687 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001688 s2255_exit_v4l(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001689 /* board shutdown stops the read pipe if it is running */
1690 s2255_board_shutdown(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001691 /* make sure firmware still not trying to load */
Dean Andersonf78d92c2008-07-22 14:43:27 -03001692 del_timer(&dev->timer); /* only started in .probe and .open */
1693
Dean Anderson38f993a2008-06-26 23:15:51 -03001694 if (dev->fw_data->fw_urb) {
1695 dprintk(2, "kill fw_urb\n");
1696 usb_kill_urb(dev->fw_data->fw_urb);
1697 usb_free_urb(dev->fw_data->fw_urb);
1698 dev->fw_data->fw_urb = NULL;
1699 }
Dean Andersonf78d92c2008-07-22 14:43:27 -03001700 if (dev->fw_data->fw)
1701 release_firmware(dev->fw_data->fw);
1702 kfree(dev->fw_data->pfw_data);
1703 kfree(dev->fw_data);
Dean Anderson38f993a2008-06-26 23:15:51 -03001704 usb_put_dev(dev->udev);
1705 dprintk(1, "%s", __func__);
Dean Anderson14d96262008-08-25 13:58:55 -03001706
1707 while (!list_empty(&s2255_devlist)) {
1708 list = s2255_devlist.next;
1709 list_del(list);
1710 }
1711 mutex_unlock(&dev->open_lock);
Dean Andersonb7732a32009-03-30 11:59:56 -03001712 kfree(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001713}
1714
Hans Verkuilbec43662008-12-30 06:58:20 -03001715static int s2255_close(struct file *file)
Dean Anderson38f993a2008-06-26 23:15:51 -03001716{
1717 struct s2255_fh *fh = file->private_data;
1718 struct s2255_dev *dev = fh->dev;
Hans Verkuilbec43662008-12-30 06:58:20 -03001719 int minor = video_devdata(file)->minor;
Dean Anderson38f993a2008-06-26 23:15:51 -03001720 if (!dev)
1721 return -ENODEV;
1722
1723 mutex_lock(&dev->open_lock);
1724
Dean Andersonf78d92c2008-07-22 14:43:27 -03001725 /* turn off stream */
1726 if (res_check(fh)) {
1727 if (dev->b_acquire[fh->channel])
1728 s2255_stop_acquire(dev, fh->channel);
1729 videobuf_streamoff(&fh->vb_vidq);
1730 res_free(dev, fh);
1731 }
1732
Dean Anderson38f993a2008-06-26 23:15:51 -03001733 videobuf_mmap_free(&fh->vb_vidq);
Dean Anderson38f993a2008-06-26 23:15:51 -03001734 dev->users[fh->channel]--;
Dean Andersonf78d92c2008-07-22 14:43:27 -03001735
Dean Anderson38f993a2008-06-26 23:15:51 -03001736 mutex_unlock(&dev->open_lock);
1737
1738 kref_put(&dev->kref, s2255_destroy);
1739 dprintk(1, "s2255: close called (minor=%d, users=%d)\n",
1740 minor, dev->users[fh->channel]);
Dean Andersonf78d92c2008-07-22 14:43:27 -03001741 kfree(fh);
Dean Anderson38f993a2008-06-26 23:15:51 -03001742 return 0;
1743}
1744
1745static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1746{
1747 struct s2255_fh *fh = file->private_data;
1748 int ret;
1749
1750 if (!fh)
1751 return -ENODEV;
1752 dprintk(4, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1753
1754 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1755
1756 dprintk(4, "vma start=0x%08lx, size=%ld, ret=%d\n",
1757 (unsigned long)vma->vm_start,
1758 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1759
1760 return ret;
1761}
1762
Hans Verkuilbec43662008-12-30 06:58:20 -03001763static const struct v4l2_file_operations s2255_fops_v4l = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001764 .owner = THIS_MODULE,
1765 .open = s2255_open,
1766 .release = s2255_close,
1767 .poll = s2255_poll,
1768 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Dean Anderson38f993a2008-06-26 23:15:51 -03001769 .mmap = s2255_mmap_v4l,
Dean Anderson38f993a2008-06-26 23:15:51 -03001770};
1771
Hans Verkuila3998102008-07-21 02:57:38 -03001772static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
Dean Anderson38f993a2008-06-26 23:15:51 -03001773 .vidioc_querycap = vidioc_querycap,
1774 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1775 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1776 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1777 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1778 .vidioc_reqbufs = vidioc_reqbufs,
1779 .vidioc_querybuf = vidioc_querybuf,
1780 .vidioc_qbuf = vidioc_qbuf,
1781 .vidioc_dqbuf = vidioc_dqbuf,
1782 .vidioc_s_std = vidioc_s_std,
1783 .vidioc_enum_input = vidioc_enum_input,
1784 .vidioc_g_input = vidioc_g_input,
1785 .vidioc_s_input = vidioc_s_input,
1786 .vidioc_queryctrl = vidioc_queryctrl,
1787 .vidioc_g_ctrl = vidioc_g_ctrl,
1788 .vidioc_s_ctrl = vidioc_s_ctrl,
1789 .vidioc_streamon = vidioc_streamon,
1790 .vidioc_streamoff = vidioc_streamoff,
1791#ifdef CONFIG_VIDEO_V4L1_COMPAT
1792 .vidiocgmbuf = vidioc_cgmbuf,
1793#endif
Dean Anderson22b88d42008-08-29 15:33:19 -03001794 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1795 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
Hans Verkuila3998102008-07-21 02:57:38 -03001796};
1797
1798static struct video_device template = {
1799 .name = "s2255v",
Hans Verkuila3998102008-07-21 02:57:38 -03001800 .fops = &s2255_fops_v4l,
1801 .ioctl_ops = &s2255_ioctl_ops,
1802 .minor = -1,
1803 .release = video_device_release,
Dean Anderson38f993a2008-06-26 23:15:51 -03001804 .tvnorms = S2255_NORMS,
1805 .current_norm = V4L2_STD_NTSC_M,
1806};
1807
1808static int s2255_probe_v4l(struct s2255_dev *dev)
1809{
1810 int ret;
1811 int i;
1812 int cur_nr = video_nr;
1813
1814 /* initialize all video 4 linux */
1815 list_add_tail(&dev->s2255_devlist, &s2255_devlist);
1816 /* register 4 video devices */
1817 for (i = 0; i < MAX_CHANNELS; i++) {
1818 INIT_LIST_HEAD(&dev->vidq[i].active);
1819 dev->vidq[i].dev = dev;
1820 dev->vidq[i].channel = i;
1821 dev->vidq[i].kthread = NULL;
1822 /* register 4 video devices */
1823 dev->vdev[i] = video_device_alloc();
1824 memcpy(dev->vdev[i], &template, sizeof(struct video_device));
Hans Verkuil5e85e732008-07-20 06:31:39 -03001825 dev->vdev[i]->parent = &dev->interface->dev;
Dean Anderson38f993a2008-06-26 23:15:51 -03001826 if (video_nr == -1)
1827 ret = video_register_device(dev->vdev[i],
1828 VFL_TYPE_GRABBER,
1829 video_nr);
1830 else
1831 ret = video_register_device(dev->vdev[i],
1832 VFL_TYPE_GRABBER,
1833 cur_nr + i);
Hans Verkuil601e9442008-08-23 07:24:07 -03001834 video_set_drvdata(dev->vdev[i], dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03001835
1836 if (ret != 0) {
1837 dev_err(&dev->udev->dev,
1838 "failed to register video device!\n");
1839 return ret;
1840 }
1841 }
1842 printk(KERN_INFO "Sensoray 2255 V4L driver\n");
1843 return ret;
1844}
1845
1846static void s2255_exit_v4l(struct s2255_dev *dev)
1847{
Dean Anderson14d96262008-08-25 13:58:55 -03001848
Dean Anderson38f993a2008-06-26 23:15:51 -03001849 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03001850 for (i = 0; i < MAX_CHANNELS; i++) {
Dean Anderson14d96262008-08-25 13:58:55 -03001851 if (-1 != dev->vdev[i]->minor) {
Dean Anderson38f993a2008-06-26 23:15:51 -03001852 video_unregister_device(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001853 printk(KERN_INFO "s2255 unregistered\n");
1854 } else {
Dean Anderson38f993a2008-06-26 23:15:51 -03001855 video_device_release(dev->vdev[i]);
Dean Anderson14d96262008-08-25 13:58:55 -03001856 printk(KERN_INFO "s2255 released\n");
1857 }
Dean Anderson38f993a2008-06-26 23:15:51 -03001858 }
1859}
1860
1861/* this function moves the usb stream read pipe data
1862 * into the system buffers.
1863 * returns 0 on success, EAGAIN if more data to process( call this
1864 * function again).
1865 *
1866 * Received frame structure:
Dean Anderson14d96262008-08-25 13:58:55 -03001867 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
Dean Anderson38f993a2008-06-26 23:15:51 -03001868 * bytes 4-7: channel: 0-3
1869 * bytes 8-11: payload size: size of the frame
1870 * bytes 12-payloadsize+12: frame data
1871 */
1872static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1873{
Dean Anderson38f993a2008-06-26 23:15:51 -03001874 char *pdest;
1875 u32 offset = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03001876 int bframe = 0;
Dean Anderson38f993a2008-06-26 23:15:51 -03001877 char *psrc;
1878 unsigned long copy_size;
1879 unsigned long size;
1880 s32 idx = -1;
1881 struct s2255_framei *frm;
1882 unsigned char *pdata;
Dean Anderson14d96262008-08-25 13:58:55 -03001883
Dean Anderson38f993a2008-06-26 23:15:51 -03001884 dprintk(100, "buffer to user\n");
1885
1886 idx = dev->cur_frame[dev->cc];
Dean Anderson38f993a2008-06-26 23:15:51 -03001887 frm = &dev->buffer[dev->cc].frame[idx];
1888
Dean Anderson14d96262008-08-25 13:58:55 -03001889 if (frm->ulState == S2255_READ_IDLE) {
1890 int jj;
1891 unsigned int cc;
1892 s32 *pdword;
1893 int payload;
1894 /* search for marker codes */
1895 pdata = (unsigned char *)pipe_info->transfer_buffer;
1896 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1897 switch (*(s32 *) pdata) {
1898 case S2255_MARKER_FRAME:
1899 pdword = (s32 *)pdata;
1900 dprintk(4, "found frame marker at offset:"
1901 " %d [%x %x]\n", jj, pdata[0],
1902 pdata[1]);
1903 offset = jj + PREFIX_SIZE;
1904 bframe = 1;
1905 cc = pdword[1];
1906 if (cc >= MAX_CHANNELS) {
1907 printk(KERN_ERR
1908 "bad channel\n");
1909 return -EINVAL;
1910 }
1911 /* reverse it */
1912 dev->cc = G_chnmap[cc];
1913 payload = pdword[3];
1914 if (payload > dev->req_image_size[dev->cc]) {
1915 dev->bad_payload[dev->cc]++;
1916 /* discard the bad frame */
1917 return -EINVAL;
1918 }
1919 dev->pkt_size[dev->cc] = payload;
1920 dev->jpg_size[dev->cc] = pdword[4];
1921 break;
1922 case S2255_MARKER_RESPONSE:
1923 pdword = (s32 *)pdata;
1924 pdata += DEF_USB_BLOCK;
1925 jj += DEF_USB_BLOCK;
1926 if (pdword[1] >= MAX_CHANNELS)
1927 break;
1928 cc = G_chnmap[pdword[1]];
1929 if (!(cc >= 0 && cc < MAX_CHANNELS))
1930 break;
1931 switch (pdword[2]) {
1932 case 0x01:
1933 /* check if channel valid */
1934 /* set mode ready */
1935 dev->setmode_ready[cc] = 1;
1936 wake_up(&dev->wait_setmode[cc]);
1937 dprintk(5, "setmode ready %d\n", cc);
1938 break;
1939 case 0x10:
1940
1941 dev->chn_ready |= (1 << cc);
1942 if ((dev->chn_ready & 0x0f) != 0x0f)
1943 break;
1944 /* all channels ready */
1945 printk(KERN_INFO "s2255: fw loaded\n");
1946 atomic_set(&dev->fw_data->fw_state,
1947 S2255_FW_SUCCESS);
1948 wake_up(&dev->fw_data->wait_fw);
1949 break;
1950 default:
1951 printk(KERN_INFO "s2255 unknwn resp\n");
1952 }
1953 default:
1954 pdata++;
1955 break;
1956 }
1957 if (bframe)
1958 break;
1959 } /* for */
1960 if (!bframe)
1961 return -EINVAL;
1962 }
1963
1964
1965 idx = dev->cur_frame[dev->cc];
1966 frm = &dev->buffer[dev->cc].frame[idx];
1967
1968 /* search done. now find out if should be acquiring on this channel */
1969 if (!dev->b_acquire[dev->cc]) {
1970 /* we found a frame, but this channel is turned off */
1971 frm->ulState = S2255_READ_IDLE;
1972 return -EINVAL;
1973 }
1974
1975 if (frm->ulState == S2255_READ_IDLE) {
1976 frm->ulState = S2255_READ_FRAME;
Dean Anderson38f993a2008-06-26 23:15:51 -03001977 frm->cur_size = 0;
1978 }
1979
Dean Anderson14d96262008-08-25 13:58:55 -03001980 /* skip the marker 512 bytes (and offset if out of sync) */
1981 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1982
Dean Anderson38f993a2008-06-26 23:15:51 -03001983
1984 if (frm->lpvbits == NULL) {
1985 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1986 frm, dev, dev->cc, idx);
1987 return -ENOMEM;
1988 }
1989
1990 pdest = frm->lpvbits + frm->cur_size;
1991
Dean Anderson14d96262008-08-25 13:58:55 -03001992 copy_size = (pipe_info->cur_transfer_size - offset);
Dean Anderson38f993a2008-06-26 23:15:51 -03001993
Dean Anderson14d96262008-08-25 13:58:55 -03001994 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03001995
Dean Anderson14d96262008-08-25 13:58:55 -03001996 /* sanity check on pdest */
1997 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
1998 memcpy(pdest, psrc, copy_size);
Dean Anderson38f993a2008-06-26 23:15:51 -03001999
Dean Anderson38f993a2008-06-26 23:15:51 -03002000 frm->cur_size += copy_size;
Dean Anderson14d96262008-08-25 13:58:55 -03002001 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
Dean Anderson38f993a2008-06-26 23:15:51 -03002002
Dean Anderson14d96262008-08-25 13:58:55 -03002003 if (frm->cur_size >= size) {
2004
Dean Anderson38f993a2008-06-26 23:15:51 -03002005 u32 cc = dev->cc;
Dean Anderson38f993a2008-06-26 23:15:51 -03002006 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
2007 cc, idx);
2008 dev->last_frame[cc] = dev->cur_frame[cc];
2009 dev->cur_frame[cc]++;
2010 /* end of system frame ring buffer, start at zero */
2011 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
2012 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
2013 dev->cur_frame[cc] = 0;
Dean Anderson14d96262008-08-25 13:58:55 -03002014 /* frame ready */
Dean Anderson38f993a2008-06-26 23:15:51 -03002015 if (dev->b_acquire[cc])
Dean Anderson14d96262008-08-25 13:58:55 -03002016 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002017 dev->frame_count[cc]++;
Dean Anderson14d96262008-08-25 13:58:55 -03002018 frm->ulState = S2255_READ_IDLE;
2019 frm->cur_size = 0;
2020
Dean Anderson38f993a2008-06-26 23:15:51 -03002021 }
2022 /* done successfully */
2023 return 0;
2024}
2025
2026static void s2255_read_video_callback(struct s2255_dev *dev,
2027 struct s2255_pipeinfo *pipe_info)
2028{
2029 int res;
2030 dprintk(50, "callback read video \n");
2031
2032 if (dev->cc >= MAX_CHANNELS) {
2033 dev->cc = 0;
2034 dev_err(&dev->udev->dev, "invalid channel\n");
2035 return;
2036 }
2037 /* otherwise copy to the system buffers */
2038 res = save_frame(dev, pipe_info);
Dean Anderson14d96262008-08-25 13:58:55 -03002039 if (res != 0)
2040 dprintk(4, "s2255: read callback failed\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002041
2042 dprintk(50, "callback read video done\n");
2043 return;
2044}
2045
2046static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2047 u16 Index, u16 Value, void *TransferBuffer,
2048 s32 TransferBufferLength, int bOut)
2049{
2050 int r;
2051 if (!bOut) {
2052 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2053 Request,
2054 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2055 USB_DIR_IN,
2056 Value, Index, TransferBuffer,
2057 TransferBufferLength, HZ * 5);
2058 } else {
2059 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2060 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2061 Value, Index, TransferBuffer,
2062 TransferBufferLength, HZ * 5);
2063 }
2064 return r;
2065}
2066
2067/*
2068 * retrieve FX2 firmware version. future use.
2069 * @param dev pointer to device extension
2070 * @return -1 for fail, else returns firmware version as an int(16 bits)
2071 */
2072static int s2255_get_fx2fw(struct s2255_dev *dev)
2073{
2074 int fw;
2075 int ret;
2076 unsigned char transBuffer[64];
2077 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2078 S2255_VR_IN);
2079 if (ret < 0)
2080 dprintk(2, "get fw error: %x\n", ret);
2081 fw = transBuffer[0] + (transBuffer[1] << 8);
2082 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2083 return fw;
2084}
2085
2086/*
2087 * Create the system ring buffer to copy frames into from the
2088 * usb read pipe.
2089 */
2090static int s2255_create_sys_buffers(struct s2255_dev *dev, unsigned long chn)
2091{
2092 unsigned long i;
2093 unsigned long reqsize;
2094 dprintk(1, "create sys buffers\n");
2095 if (chn >= MAX_CHANNELS)
2096 return -1;
2097
2098 dev->buffer[chn].dwFrames = SYS_FRAMES;
2099
2100 /* always allocate maximum size(PAL) for system buffers */
2101 reqsize = SYS_FRAMES_MAXSIZE;
2102
2103 if (reqsize > SYS_FRAMES_MAXSIZE)
2104 reqsize = SYS_FRAMES_MAXSIZE;
2105
2106 for (i = 0; i < SYS_FRAMES; i++) {
2107 /* allocate the frames */
2108 dev->buffer[chn].frame[i].lpvbits = vmalloc(reqsize);
2109
2110 dprintk(1, "valloc %p chan %lu, idx %lu, pdata %p\n",
2111 &dev->buffer[chn].frame[i], chn, i,
2112 dev->buffer[chn].frame[i].lpvbits);
2113 dev->buffer[chn].frame[i].size = reqsize;
2114 if (dev->buffer[chn].frame[i].lpvbits == NULL) {
2115 printk(KERN_INFO "out of memory. using less frames\n");
2116 dev->buffer[chn].dwFrames = i;
2117 break;
2118 }
2119 }
2120
2121 /* make sure internal states are set */
2122 for (i = 0; i < SYS_FRAMES; i++) {
2123 dev->buffer[chn].frame[i].ulState = 0;
2124 dev->buffer[chn].frame[i].cur_size = 0;
2125 }
2126
2127 dev->cur_frame[chn] = 0;
2128 dev->last_frame[chn] = -1;
2129 return 0;
2130}
2131
2132static int s2255_release_sys_buffers(struct s2255_dev *dev,
2133 unsigned long channel)
2134{
2135 unsigned long i;
2136 dprintk(1, "release sys buffers\n");
2137 for (i = 0; i < SYS_FRAMES; i++) {
2138 if (dev->buffer[channel].frame[i].lpvbits) {
2139 dprintk(1, "vfree %p\n",
2140 dev->buffer[channel].frame[i].lpvbits);
2141 vfree(dev->buffer[channel].frame[i].lpvbits);
2142 }
2143 dev->buffer[channel].frame[i].lpvbits = NULL;
2144 }
2145 return 0;
2146}
2147
2148static int s2255_board_init(struct s2255_dev *dev)
2149{
2150 int j;
2151 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2152 int fw_ver;
2153 dprintk(4, "board init: %p", dev);
2154
2155 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2156 struct s2255_pipeinfo *pipe = &dev->pipes[j];
2157
2158 memset(pipe, 0, sizeof(*pipe));
2159 pipe->dev = dev;
Dean Anderson14d96262008-08-25 13:58:55 -03002160 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2161 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
Dean Anderson38f993a2008-06-26 23:15:51 -03002162
Dean Anderson38f993a2008-06-26 23:15:51 -03002163 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2164 GFP_KERNEL);
2165 if (pipe->transfer_buffer == NULL) {
2166 dprintk(1, "out of memory!\n");
2167 return -ENOMEM;
2168 }
2169
2170 }
2171
2172 /* query the firmware */
2173 fw_ver = s2255_get_fx2fw(dev);
2174
2175 printk(KERN_INFO "2255 usb firmware version %d \n", fw_ver);
2176 if (fw_ver < CUR_USB_FWVER)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002177 dev_err(&dev->udev->dev,
2178 "usb firmware not up to date %d\n", fw_ver);
Dean Anderson38f993a2008-06-26 23:15:51 -03002179
2180 for (j = 0; j < MAX_CHANNELS; j++) {
2181 dev->b_acquire[j] = 0;
2182 dev->mode[j] = mode_def;
Dean Anderson22b88d42008-08-29 15:33:19 -03002183 dev->jc[j].quality = S2255_DEF_JPEG_QUAL;
Dean Anderson38f993a2008-06-26 23:15:51 -03002184 dev->cur_fmt[j] = &formats[0];
2185 dev->mode[j].restart = 1;
2186 dev->req_image_size[j] = get_transfer_size(&mode_def);
2187 dev->frame_count[j] = 0;
2188 /* create the system buffers */
2189 s2255_create_sys_buffers(dev, j);
2190 }
2191 /* start read pipe */
2192 s2255_start_readpipe(dev);
2193
2194 dprintk(1, "S2255: board initialized\n");
2195 return 0;
2196}
2197
2198static int s2255_board_shutdown(struct s2255_dev *dev)
2199{
2200 u32 i;
2201
2202 dprintk(1, "S2255: board shutdown: %p", dev);
2203
2204 for (i = 0; i < MAX_CHANNELS; i++) {
2205 if (dev->b_acquire[i])
2206 s2255_stop_acquire(dev, i);
2207 }
2208
2209 s2255_stop_readpipe(dev);
2210
2211 for (i = 0; i < MAX_CHANNELS; i++)
2212 s2255_release_sys_buffers(dev, i);
2213
2214 /* release transfer buffers */
2215 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2216 struct s2255_pipeinfo *pipe = &dev->pipes[i];
2217 kfree(pipe->transfer_buffer);
2218 }
2219 return 0;
2220}
2221
2222static void read_pipe_completion(struct urb *purb)
2223{
2224 struct s2255_pipeinfo *pipe_info;
2225 struct s2255_dev *dev;
2226 int status;
2227 int pipe;
2228
2229 pipe_info = purb->context;
2230 dprintk(100, "read pipe completion %p, status %d\n", purb,
2231 purb->status);
2232 if (pipe_info == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002233 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002234 return;
2235 }
2236
2237 dev = pipe_info->dev;
2238 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002239 dev_err(&purb->dev->dev, "no context!\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002240 return;
2241 }
2242 status = purb->status;
2243 if (status != 0) {
2244 dprintk(2, "read_pipe_completion: err\n");
2245 return;
2246 }
2247
2248 if (pipe_info->state == 0) {
2249 dprintk(2, "exiting USB pipe");
2250 return;
2251 }
2252
2253 s2255_read_video_callback(dev, pipe_info);
2254
2255 pipe_info->err_count = 0;
2256 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2257 /* reuse urb */
2258 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2259 pipe,
2260 pipe_info->transfer_buffer,
2261 pipe_info->cur_transfer_size,
2262 read_pipe_completion, pipe_info);
2263
2264 if (pipe_info->state != 0) {
2265 if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) {
2266 dev_err(&dev->udev->dev, "error submitting urb\n");
2267 usb_free_urb(pipe_info->stream_urb);
2268 }
2269 } else {
2270 dprintk(2, "read pipe complete state 0\n");
2271 }
2272 return;
2273}
2274
2275static int s2255_start_readpipe(struct s2255_dev *dev)
2276{
2277 int pipe;
2278 int retval;
2279 int i;
2280 struct s2255_pipeinfo *pipe_info = dev->pipes;
2281 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2282 dprintk(2, "start pipe IN %d\n", dev->read_endpoint);
2283
2284 for (i = 0; i < MAX_PIPE_BUFFERS; i++) {
2285 pipe_info->state = 1;
2286 pipe_info->buf_index = (u32) i;
2287 pipe_info->priority_set = 0;
2288 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2289 if (!pipe_info->stream_urb) {
2290 dev_err(&dev->udev->dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002291 "ReadStream: Unable to alloc URB\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002292 return -ENOMEM;
2293 }
2294 /* transfer buffer allocated in board_init */
2295 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2296 pipe,
2297 pipe_info->transfer_buffer,
2298 pipe_info->cur_transfer_size,
2299 read_pipe_completion, pipe_info);
2300
2301 pipe_info->urb_size = sizeof(pipe_info->stream_urb);
2302 dprintk(4, "submitting URB %p\n", pipe_info->stream_urb);
2303 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2304 if (retval) {
2305 printk(KERN_ERR "s2255: start read pipe failed\n");
2306 return retval;
2307 }
2308 }
2309
2310 return 0;
2311}
2312
2313/* starts acquisition process */
2314static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn)
2315{
2316 unsigned char *buffer;
2317 int res;
2318 unsigned long chn_rev;
2319 int j;
2320 if (chn >= MAX_CHANNELS) {
2321 dprintk(2, "start acquire failed, bad channel %lu\n", chn);
2322 return -1;
2323 }
2324
2325 chn_rev = G_chnmap[chn];
2326 dprintk(1, "S2255: start acquire %lu \n", chn);
2327
2328 buffer = kzalloc(512, GFP_KERNEL);
2329 if (buffer == NULL) {
2330 dev_err(&dev->udev->dev, "out of mem\n");
2331 return -ENOMEM;
2332 }
2333
2334 dev->last_frame[chn] = -1;
2335 dev->bad_payload[chn] = 0;
2336 dev->cur_frame[chn] = 0;
2337 for (j = 0; j < SYS_FRAMES; j++) {
2338 dev->buffer[chn].frame[j].ulState = 0;
2339 dev->buffer[chn].frame[j].cur_size = 0;
2340 }
2341
2342 /* send the start command */
2343 *(u32 *) buffer = IN_DATA_TOKEN;
2344 *((u32 *) buffer + 1) = (u32) chn_rev;
2345 *((u32 *) buffer + 2) = (u32) CMD_START;
2346 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2347 if (res != 0)
2348 dev_err(&dev->udev->dev, "CMD_START error\n");
2349
2350 dprintk(2, "start acquire exit[%lu] %d \n", chn, res);
2351 kfree(buffer);
2352 return 0;
2353}
2354
2355static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2356{
2357 unsigned char *buffer;
2358 int res;
2359 unsigned long chn_rev;
2360
2361 if (chn >= MAX_CHANNELS) {
2362 dprintk(2, "stop acquire failed, bad channel %lu\n", chn);
2363 return -1;
2364 }
2365 chn_rev = G_chnmap[chn];
2366
2367 buffer = kzalloc(512, GFP_KERNEL);
2368 if (buffer == NULL) {
2369 dev_err(&dev->udev->dev, "out of mem\n");
2370 return -ENOMEM;
2371 }
2372
2373 /* send the stop command */
2374 dprintk(4, "stop acquire %lu\n", chn);
2375 *(u32 *) buffer = IN_DATA_TOKEN;
2376 *((u32 *) buffer + 1) = (u32) chn_rev;
2377 *((u32 *) buffer + 2) = CMD_STOP;
2378 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2379
2380 if (res != 0)
2381 dev_err(&dev->udev->dev, "CMD_STOP error\n");
2382
2383 dprintk(4, "stop acquire: releasing states \n");
2384
2385 kfree(buffer);
2386 dev->b_acquire[chn] = 0;
2387
Dean Anderson14d96262008-08-25 13:58:55 -03002388 return res;
Dean Anderson38f993a2008-06-26 23:15:51 -03002389}
2390
2391static void s2255_stop_readpipe(struct s2255_dev *dev)
2392{
2393 int j;
2394
2395 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002396 s2255_dev_err(&dev->udev->dev, "invalid device\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002397 return;
2398 }
2399 dprintk(4, "stop read pipe\n");
2400 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2401 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2402 if (pipe_info) {
2403 if (pipe_info->state == 0)
2404 continue;
2405 pipe_info->state = 0;
2406 pipe_info->prev_state = 1;
2407
2408 }
2409 }
2410
2411 for (j = 0; j < MAX_PIPE_BUFFERS; j++) {
2412 struct s2255_pipeinfo *pipe_info = &dev->pipes[j];
2413 if (pipe_info->stream_urb) {
2414 /* cancel urb */
2415 usb_kill_urb(pipe_info->stream_urb);
2416 usb_free_urb(pipe_info->stream_urb);
2417 pipe_info->stream_urb = NULL;
2418 }
2419 }
2420 dprintk(2, "s2255 stop read pipe: %d\n", j);
2421 return;
2422}
2423
Dean Anderson14d96262008-08-25 13:58:55 -03002424static void s2255_fwload_start(struct s2255_dev *dev, int reset)
Dean Anderson38f993a2008-06-26 23:15:51 -03002425{
Dean Anderson14d96262008-08-25 13:58:55 -03002426 if (reset)
2427 s2255_reset_dsppower(dev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002428 dev->fw_data->fw_size = dev->fw_data->fw->size;
2429 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2430 memcpy(dev->fw_data->pfw_data,
2431 dev->fw_data->fw->data, CHUNK_SIZE);
2432 dev->fw_data->fw_loaded = CHUNK_SIZE;
2433 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2434 usb_sndbulkpipe(dev->udev, 2),
2435 dev->fw_data->pfw_data,
2436 CHUNK_SIZE, s2255_fwchunk_complete,
2437 dev->fw_data);
2438 mod_timer(&dev->timer, jiffies + HZ);
2439}
2440
2441/* standard usb probe function */
2442static int s2255_probe(struct usb_interface *interface,
2443 const struct usb_device_id *id)
2444{
2445 struct s2255_dev *dev = NULL;
2446 struct usb_host_interface *iface_desc;
2447 struct usb_endpoint_descriptor *endpoint;
2448 int i;
2449 int retval = -ENOMEM;
Dean Anderson14d96262008-08-25 13:58:55 -03002450 __le32 *pdata;
2451 int fw_size;
Dean Anderson38f993a2008-06-26 23:15:51 -03002452
2453 dprintk(2, "s2255: probe\n");
2454
2455 /* allocate memory for our device state and initialize it to zero */
2456 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2457 if (dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002458 s2255_dev_err(&interface->dev, "out of memory\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002459 goto error;
2460 }
2461
2462 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2463 if (!dev->fw_data)
2464 goto error;
2465
2466 mutex_init(&dev->lock);
2467 mutex_init(&dev->open_lock);
2468
2469 /* grab usb_device and save it */
2470 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2471 if (dev->udev == NULL) {
2472 dev_err(&interface->dev, "null usb device\n");
2473 retval = -ENODEV;
2474 goto error;
2475 }
2476 kref_init(&dev->kref);
2477 dprintk(1, "dev: %p, kref: %p udev %p interface %p\n", dev, &dev->kref,
2478 dev->udev, interface);
2479 dev->interface = interface;
2480 /* set up the endpoint information */
2481 iface_desc = interface->cur_altsetting;
2482 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2483 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2484 endpoint = &iface_desc->endpoint[i].desc;
2485 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2486 /* we found the bulk in endpoint */
2487 dev->read_endpoint = endpoint->bEndpointAddress;
2488 }
2489 }
2490
2491 if (!dev->read_endpoint) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002492 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
Dean Anderson38f993a2008-06-26 23:15:51 -03002493 goto error;
2494 }
2495
2496 /* set intfdata */
2497 usb_set_intfdata(interface, dev);
2498
2499 dprintk(100, "after intfdata %p\n", dev);
2500
2501 init_timer(&dev->timer);
2502 dev->timer.function = s2255_timer;
2503 dev->timer.data = (unsigned long)dev->fw_data;
2504
2505 init_waitqueue_head(&dev->fw_data->wait_fw);
Dean Anderson14d96262008-08-25 13:58:55 -03002506 for (i = 0; i < MAX_CHANNELS; i++)
2507 init_waitqueue_head(&dev->wait_setmode[i]);
Dean Anderson38f993a2008-06-26 23:15:51 -03002508
2509
2510 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2511
2512 if (!dev->fw_data->fw_urb) {
2513 dev_err(&interface->dev, "out of memory!\n");
2514 goto error;
2515 }
2516 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2517 if (!dev->fw_data->pfw_data) {
2518 dev_err(&interface->dev, "out of memory!\n");
2519 goto error;
2520 }
2521 /* load the first chunk */
2522 if (request_firmware(&dev->fw_data->fw,
2523 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2524 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2525 goto error;
2526 }
Dean Anderson14d96262008-08-25 13:58:55 -03002527 /* check the firmware is valid */
2528 fw_size = dev->fw_data->fw->size;
2529 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
Dean Anderson38f993a2008-06-26 23:15:51 -03002530
Dean Anderson14d96262008-08-25 13:58:55 -03002531 if (*pdata != S2255_FW_MARKER) {
2532 printk(KERN_INFO "Firmware invalid.\n");
2533 retval = -ENODEV;
2534 goto error;
2535 } else {
2536 /* make sure firmware is the latest */
2537 __le32 *pRel;
2538 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2539 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2540 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002541 /* loads v4l specific */
2542 s2255_probe_v4l(dev);
Dean Anderson14d96262008-08-25 13:58:55 -03002543 usb_reset_device(dev->udev);
Dean Anderson38f993a2008-06-26 23:15:51 -03002544 /* load 2255 board specific */
2545 s2255_board_init(dev);
2546
2547 dprintk(4, "before probe done %p\n", dev);
2548 spin_lock_init(&dev->slock);
2549
Dean Anderson14d96262008-08-25 13:58:55 -03002550 s2255_fwload_start(dev, 0);
Dean Anderson38f993a2008-06-26 23:15:51 -03002551 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2552 return 0;
2553error:
2554 return retval;
2555}
2556
2557/* disconnect routine. when board is removed physically or with rmmod */
2558static void s2255_disconnect(struct usb_interface *interface)
2559{
2560 struct s2255_dev *dev = NULL;
Dean Anderson14d96262008-08-25 13:58:55 -03002561 int i;
Dean Anderson38f993a2008-06-26 23:15:51 -03002562 dprintk(1, "s2255: disconnect interface %p\n", interface);
2563 dev = usb_get_intfdata(interface);
Dean Anderson14d96262008-08-25 13:58:55 -03002564
2565 /*
2566 * wake up any of the timers to allow open_lock to be
2567 * acquired sooner
2568 */
2569 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2570 wake_up(&dev->fw_data->wait_fw);
2571 for (i = 0; i < MAX_CHANNELS; i++) {
2572 dev->setmode_ready[i] = 1;
2573 wake_up(&dev->wait_setmode[i]);
2574 }
2575
2576 mutex_lock(&dev->open_lock);
2577 usb_set_intfdata(interface, NULL);
2578 mutex_unlock(&dev->open_lock);
2579
Dean Anderson38f993a2008-06-26 23:15:51 -03002580 if (dev) {
2581 kref_put(&dev->kref, s2255_destroy);
2582 dprintk(1, "s2255drv: disconnect\n");
2583 dev_info(&interface->dev, "s2255usb now disconnected\n");
2584 }
Dean Anderson38f993a2008-06-26 23:15:51 -03002585}
2586
2587static struct usb_driver s2255_driver = {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002588 .name = S2255_DRIVER_NAME,
Dean Anderson38f993a2008-06-26 23:15:51 -03002589 .probe = s2255_probe,
2590 .disconnect = s2255_disconnect,
2591 .id_table = s2255_table,
2592};
2593
2594static int __init usb_s2255_init(void)
2595{
2596 int result;
2597
2598 /* register this driver with the USB subsystem */
2599 result = usb_register(&s2255_driver);
2600
2601 if (result)
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03002602 pr_err(KBUILD_MODNAME
2603 ": usb_register failed. Error number %d\n", result);
Dean Anderson38f993a2008-06-26 23:15:51 -03002604
2605 dprintk(2, "s2255_init: done\n");
2606 return result;
2607}
2608
2609static void __exit usb_s2255_exit(void)
2610{
2611 usb_deregister(&s2255_driver);
2612}
2613
2614module_init(usb_s2255_init);
2615module_exit(usb_s2255_exit);
2616
2617MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2618MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2619MODULE_LICENSE("GPL");