[media] adv7180.c: convert to v4l2 control framework
[deliverable/linux.git] / drivers / media / video / s2255drv.c
CommitLineData
38f993ad
DA
1/*
2 * s2255drv.c - a driver for the Sensoray 2255 USB video capture device
3 *
4de39f5d 4 * Copyright (C) 2007-2010 by Sensoray Company Inc.
38f993ad
DA
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
38f993ad 19 * -full or half size Grey scale: all 4 channels at once
38f993ad 20 * -half size, color mode YUYV or YUV422P: all 4 channels at once
38f993ad
DA
21 * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
22 * at once.
38f993ad
DA
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 */
38
39#include <linux/module.h>
40#include <linux/firmware.h>
41#include <linux/kernel.h>
42#include <linux/mutex.h>
5a0e3ad6 43#include <linux/slab.h>
38f993ad 44#include <linux/videodev2.h>
feb75f07 45#include <linux/mm.h>
38f993ad
DA
46#include <media/videobuf-vmalloc.h>
47#include <media/v4l2-common.h>
3a67b5cc 48#include <media/v4l2-device.h>
35ea11ff 49#include <media/v4l2-ioctl.h>
38f993ad
DA
50#include <linux/vmalloc.h>
51#include <linux/usb.h>
52
64dc3c1a 53#define S2255_VERSION "1.22.1"
38f993ad
DA
54#define FIRMWARE_FILE_NAME "f2255usb.bin"
55
22b88d48
DA
56/* default JPEG quality */
57#define S2255_DEF_JPEG_QUAL 50
38f993ad
DA
58/* vendor request in */
59#define S2255_VR_IN 0
60/* vendor request out */
61#define S2255_VR_OUT 1
62/* firmware query */
63#define S2255_VR_FW 0x30
64/* USB endpoint number for configuring the device */
65#define S2255_CONFIG_EP 2
66/* maximum time for DSP to start responding after last FW word loaded(ms) */
14d96260 67#define S2255_DSP_BOOTTIME 800
38f993ad 68/* maximum time to wait for firmware to load (ms) */
3f8d6f73 69#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
38f993ad 70#define S2255_DEF_BUFS 16
14d96260 71#define S2255_SETMODE_TIMEOUT 500
4de39f5d 72#define S2255_VIDSTATUS_TIMEOUT 350
3fa00605
DA
73#define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL)
74#define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL)
75#define S2255_RESPONSE_SETMODE cpu_to_le32(0x01)
76#define S2255_RESPONSE_FW cpu_to_le32(0x10)
77#define S2255_RESPONSE_STATUS cpu_to_le32(0x20)
14d96260 78#define S2255_USB_XFER_SIZE (16 * 1024)
38f993ad 79#define MAX_CHANNELS 4
38f993ad
DA
80#define SYS_FRAMES 4
81/* maximum size is PAL full size plus room for the marker header(s) */
14d96260
DA
82#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
83#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
38f993ad
DA
84#define LINE_SZ_4CIFS_NTSC 640
85#define LINE_SZ_2CIFS_NTSC 640
86#define LINE_SZ_1CIFS_NTSC 320
87#define LINE_SZ_4CIFS_PAL 704
88#define LINE_SZ_2CIFS_PAL 704
89#define LINE_SZ_1CIFS_PAL 352
90#define NUM_LINES_4CIFS_NTSC 240
91#define NUM_LINES_2CIFS_NTSC 240
92#define NUM_LINES_1CIFS_NTSC 240
93#define NUM_LINES_4CIFS_PAL 288
94#define NUM_LINES_2CIFS_PAL 288
95#define NUM_LINES_1CIFS_PAL 288
96#define LINE_SZ_DEF 640
97#define NUM_LINES_DEF 240
98
99
100/* predefined settings */
101#define FORMAT_NTSC 1
102#define FORMAT_PAL 2
103
104#define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */
105#define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */
106#define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */
7d853532
DA
107/* SCALE_4CIFSI is the 2 fields interpolated into one */
108#define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */
38f993ad
DA
109
110#define COLOR_YUVPL 1 /* YUV planar */
111#define COLOR_YUVPK 2 /* YUV packed */
112#define COLOR_Y8 4 /* monochrome */
14d96260 113#define COLOR_JPG 5 /* JPEG */
38f993ad 114
5a34d9df
DA
115#define MASK_COLOR 0x000000ff
116#define MASK_JPG_QUALITY 0x0000ff00
117#define MASK_INPUT_TYPE 0x000f0000
8a8cc952 118/* frame decimation. */
38f993ad
DA
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 */
3fa00605 136#define IN_DATA_TOKEN cpu_to_le32(0x2255c0de)
3b2a6306 137#define CMD_2255 0xc2255000
3fa00605
DA
138#define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10))
139#define CMD_START cpu_to_le32((CMD_2255 | 0x20))
140#define CMD_STOP cpu_to_le32((CMD_2255 | 0x30))
141#define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40))
38f993ad
DA
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
38f993ad 157
14d96260
DA
158#define S2255_READ_IDLE 0
159#define S2255_READ_FRAME 1
38f993ad 160
14d96260 161/* frame structure */
38f993ad
DA
162struct s2255_framei {
163 unsigned long size;
14d96260 164 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
38f993ad
DA
165 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, \
3f8d6f73 177 DEF_HUE, 0, DEF_USB_BLOCK, 0}
38f993ad
DA
178
179struct s2255_dmaqueue {
180 struct list_head active;
38f993ad 181 struct s2255_dev *dev;
38f993ad
DA
182};
183
184/* for firmware loading, fw_state */
185#define S2255_FW_NOTLOADED 0
186#define S2255_FW_LOADED_DSPWAIT 1
187#define S2255_FW_SUCCESS 2
188#define S2255_FW_FAILED 3
f78d92c9 189#define S2255_FW_DISCONNECTING 4
deaf53e3 190#define S2255_FW_MARKER cpu_to_le32(0x22552f2f)
14d96260
DA
191/* 2255 read states */
192#define S2255_READ_IDLE 0
193#define S2255_READ_FRAME 1
38f993ad
DA
194struct s2255_fw {
195 int fw_loaded;
196 int fw_size;
197 struct urb *fw_urb;
198 atomic_t fw_state;
199 void *pfw_data;
200 wait_queue_head_t wait_fw;
38f993ad
DA
201 const struct firmware *fw;
202};
203
204struct s2255_pipeinfo {
205 u32 max_transfer_size;
206 u32 cur_transfer_size;
207 u8 *transfer_buffer;
38f993ad 208 u32 state;
38f993ad
DA
209 void *stream_urb;
210 void *dev; /* back pointer to s2255_dev struct*/
211 u32 err_count;
38f993ad 212 u32 idx;
38f993ad
DA
213};
214
215struct s2255_fmt; /*forward declaration */
fe85ce90
DA
216struct s2255_dev;
217
218struct s2255_channel {
219 struct video_device vdev;
220 int resources;
221 struct s2255_dmaqueue vidq;
222 struct s2255_bufferi buffer;
223 struct s2255_mode mode;
224 /* jpeg compression */
225 struct v4l2_jpegcompression jc;
226 /* capture parameters (for high quality mode full size) */
227 struct v4l2_captureparm cap_parm;
228 int cur_frame;
229 int last_frame;
230
231 int b_acquire;
232 /* allocated image size */
233 unsigned long req_image_size;
234 /* received packet size */
235 unsigned long pkt_size;
236 int bad_payload;
237 unsigned long frame_count;
238 /* if JPEG image */
239 int jpg_size;
240 /* if channel configured to default state */
241 int configured;
242 wait_queue_head_t wait_setmode;
243 int setmode_ready;
244 /* video status items */
245 int vidstatus;
246 wait_queue_head_t wait_vidstatus;
247 int vidstatus_ready;
248 unsigned int width;
249 unsigned int height;
250 const struct s2255_fmt *fmt;
251 int idx; /* channel number on device, 0-3 */
252};
253
38f993ad
DA
254
255struct s2255_dev {
fe85ce90 256 struct s2255_channel channel[MAX_CHANNELS];
65c6edb3 257 struct v4l2_device v4l2_dev;
fe85ce90 258 atomic_t num_channels;
38f993ad 259 int frames;
a19a5cd7 260 struct mutex lock; /* channels[].vdev.lock */
38f993ad 261 struct mutex open_lock;
38f993ad
DA
262 struct usb_device *udev;
263 struct usb_interface *interface;
264 u8 read_endpoint;
38f993ad
DA
265 struct timer_list timer;
266 struct s2255_fw *fw_data;
ab85c6a3 267 struct s2255_pipeinfo pipe;
38f993ad 268 u32 cc; /* current channel */
38f993ad 269 int frame_ready;
14d96260 270 int chn_ready;
38f993ad 271 spinlock_t slock;
4de39f5d
DA
272 /* dsp firmware version (f2255usb.bin) */
273 int dsp_fw_ver;
5a34d9df 274 u16 pid; /* product id */
38f993ad 275};
65c6edb3 276
65c6edb3
DA
277static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
278{
279 return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
280}
38f993ad
DA
281
282struct s2255_fmt {
283 char *name;
284 u32 fourcc;
285 int depth;
286};
287
288/* buffer for one video frame */
289struct s2255_buffer {
290 /* common v4l buffer stuff -- must be first */
291 struct videobuf_buffer vb;
292 const struct s2255_fmt *fmt;
293};
294
295struct s2255_fh {
296 struct s2255_dev *dev;
38f993ad
DA
297 struct videobuf_queue vb_vidq;
298 enum v4l2_buf_type type;
fe85ce90
DA
299 struct s2255_channel *channel;
300 int resources;
38f993ad
DA
301};
302
abce21f4 303/* current cypress EEPROM firmware version */
8a8cc952 304#define S2255_CUR_USB_FWVER ((3 << 8) | 12)
4de39f5d 305/* current DSP FW version */
8a8cc952 306#define S2255_CUR_DSP_FWVER 10104
4de39f5d 307/* Need DSP version 5+ for video status feature */
5a34d9df
DA
308#define S2255_MIN_DSP_STATUS 5
309#define S2255_MIN_DSP_COLORFILTER 8
38f993ad 310#define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC)
5a34d9df
DA
311
312/* private V4L2 controls */
313
314/*
315 * The following chart displays how COLORFILTER should be set
316 * =========================================================
317 * = fourcc = COLORFILTER =
318 * = ===============================
319 * = = 0 = 1 =
320 * =========================================================
321 * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome=
322 * = = s-video or = composite =
323 * = = B/W camera = input =
324 * =========================================================
325 * = other = color, svideo = color, =
326 * = = = composite =
327 * =========================================================
328 *
329 * Notes:
330 * channels 0-3 on 2255 are composite
331 * channels 0-1 on 2257 are composite, 2-3 are s-video
332 * If COLORFILTER is 0 with a composite color camera connected,
333 * the output will appear monochrome but hatching
334 * will occur.
335 * COLORFILTER is different from "color killer" and "color effects"
336 * for reasons above.
337 */
338#define S2255_V4L2_YC_ON 1
339#define S2255_V4L2_YC_OFF 0
340#define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0)
341
38f993ad
DA
342/* frame prefix size (sent once every frame) */
343#define PREFIX_SIZE 512
344
345/* Channels on box are in reverse order */
3f8d6f73 346static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
38f993ad 347
38f993ad
DA
348static int debug;
349static int *s2255_debug = &debug;
350
351static int s2255_start_readpipe(struct s2255_dev *dev);
352static void s2255_stop_readpipe(struct s2255_dev *dev);
fe85ce90
DA
353static int s2255_start_acquire(struct s2255_channel *channel);
354static int s2255_stop_acquire(struct s2255_channel *channel);
355static void s2255_fillbuff(struct s2255_channel *chn, struct s2255_buffer *buf,
356 int jpgsize);
357static int s2255_set_mode(struct s2255_channel *chan, struct s2255_mode *mode);
38f993ad 358static int s2255_board_shutdown(struct s2255_dev *dev);
14d96260 359static void s2255_fwload_start(struct s2255_dev *dev, int reset);
d62e85a0 360static void s2255_destroy(struct s2255_dev *dev);
14d96260
DA
361static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
362 u16 index, u16 value, void *buf,
363 s32 buf_len, int bOut);
38f993ad 364
be9ed511
MCC
365/* dev_err macro with driver name */
366#define S2255_DRIVER_NAME "s2255"
367#define s2255_dev_err(dev, fmt, arg...) \
368 dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
369
38f993ad
DA
370#define dprintk(level, fmt, arg...) \
371 do { \
372 if (*s2255_debug >= (level)) { \
be9ed511
MCC
373 printk(KERN_DEBUG S2255_DRIVER_NAME \
374 ": " fmt, ##arg); \
38f993ad
DA
375 } \
376 } while (0)
377
38f993ad
DA
378static struct usb_driver s2255_driver;
379
38f993ad
DA
380/* Declare static vars that will be used as parameters */
381static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
382
383/* start video number */
384static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
385
e42e28f9
SLD
386/* Enable jpeg capture. */
387static int jpeg_enable = 1;
388
3f8d6f73 389module_param(debug, int, 0644);
38f993ad 390MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
3f8d6f73 391module_param(vid_limit, int, 0644);
38f993ad 392MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)");
3f8d6f73 393module_param(video_nr, int, 0644);
38f993ad 394MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
e42e28f9
SLD
395module_param(jpeg_enable, int, 0644);
396MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1");
38f993ad
DA
397
398/* USB device table */
5a34d9df 399#define USB_SENSORAY_VID 0x1943
38f993ad 400static struct usb_device_id s2255_table[] = {
5a34d9df
DA
401 {USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
402 {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
38f993ad
DA
403 { } /* Terminating entry */
404};
405MODULE_DEVICE_TABLE(usb, s2255_table);
406
38f993ad
DA
407#define BUFFER_TIMEOUT msecs_to_jiffies(400)
408
38f993ad 409/* image formats. */
e42e28f9 410/* JPEG formats must be defined last to support jpeg_enable parameter */
38f993ad
DA
411static const struct s2255_fmt formats[] = {
412 {
413 .name = "4:2:2, planar, YUV422P",
414 .fourcc = V4L2_PIX_FMT_YUV422P,
415 .depth = 16
416
417 }, {
418 .name = "4:2:2, packed, YUYV",
419 .fourcc = V4L2_PIX_FMT_YUYV,
420 .depth = 16
421
422 }, {
423 .name = "4:2:2, packed, UYVY",
424 .fourcc = V4L2_PIX_FMT_UYVY,
425 .depth = 16
e42e28f9
SLD
426 }, {
427 .name = "8bpp GREY",
428 .fourcc = V4L2_PIX_FMT_GREY,
429 .depth = 8
14d96260
DA
430 }, {
431 .name = "JPG",
432 .fourcc = V4L2_PIX_FMT_JPEG,
433 .depth = 24
d0ef8540
SLD
434 }, {
435 .name = "MJPG",
436 .fourcc = V4L2_PIX_FMT_MJPEG,
437 .depth = 24
38f993ad
DA
438 }
439};
440
441static int norm_maxw(struct video_device *vdev)
442{
443 return (vdev->current_norm & V4L2_STD_NTSC) ?
444 LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
445}
446
447static int norm_maxh(struct video_device *vdev)
448{
449 return (vdev->current_norm & V4L2_STD_NTSC) ?
450 (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
451}
452
453static int norm_minw(struct video_device *vdev)
454{
455 return (vdev->current_norm & V4L2_STD_NTSC) ?
456 LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
457}
458
459static int norm_minh(struct video_device *vdev)
460{
461 return (vdev->current_norm & V4L2_STD_NTSC) ?
462 (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
463}
464
465
3f8d6f73
DA
466/*
467 * TODO: fixme: move YUV reordering to hardware
468 * converts 2255 planar format to yuyv or uyvy
469 */
38f993ad
DA
470static void planar422p_to_yuv_packed(const unsigned char *in,
471 unsigned char *out,
472 int width, int height,
473 int fmt)
474{
475 unsigned char *pY;
476 unsigned char *pCb;
477 unsigned char *pCr;
478 unsigned long size = height * width;
479 unsigned int i;
480 pY = (unsigned char *)in;
481 pCr = (unsigned char *)in + height * width;
482 pCb = (unsigned char *)in + height * width + (height * width / 2);
483 for (i = 0; i < size * 2; i += 4) {
484 out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
485 out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
486 out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
487 out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
488 }
489 return;
490}
491
d45b9b8a 492static void s2255_reset_dsppower(struct s2255_dev *dev)
14d96260 493{
8a8cc952 494 s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1);
14d96260
DA
495 msleep(10);
496 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
752eb7ae 497 msleep(600);
498 s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1);
14d96260
DA
499 return;
500}
38f993ad
DA
501
502/* kickstarts the firmware loading. from probe
503 */
504static void s2255_timer(unsigned long user_data)
505{
506 struct s2255_fw *data = (struct s2255_fw *)user_data;
85b85482 507 dprintk(100, "%s\n", __func__);
38f993ad
DA
508 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
509 printk(KERN_ERR "s2255: can't submit urb\n");
f78d92c9
DA
510 atomic_set(&data->fw_state, S2255_FW_FAILED);
511 /* wake up anything waiting for the firmware */
512 wake_up(&data->wait_fw);
38f993ad
DA
513 return;
514 }
515}
516
38f993ad
DA
517
518/* this loads the firmware asynchronously.
519 Originally this was done synchroously in probe.
520 But it is better to load it asynchronously here than block
521 inside the probe function. Blocking inside probe affects boot time.
522 FW loading is triggered by the timer in the probe function
523*/
524static void s2255_fwchunk_complete(struct urb *urb)
525{
526 struct s2255_fw *data = urb->context;
527 struct usb_device *udev = urb->dev;
528 int len;
85b85482 529 dprintk(100, "%s: udev %p urb %p", __func__, udev, urb);
38f993ad 530 if (urb->status) {
be9ed511 531 dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
f78d92c9
DA
532 atomic_set(&data->fw_state, S2255_FW_FAILED);
533 /* wake up anything waiting for the firmware */
534 wake_up(&data->wait_fw);
38f993ad
DA
535 return;
536 }
537 if (data->fw_urb == NULL) {
be9ed511 538 s2255_dev_err(&udev->dev, "disconnected\n");
f78d92c9
DA
539 atomic_set(&data->fw_state, S2255_FW_FAILED);
540 /* wake up anything waiting for the firmware */
541 wake_up(&data->wait_fw);
38f993ad
DA
542 return;
543 }
544#define CHUNK_SIZE 512
545 /* all USB transfers must be done with continuous kernel memory.
546 can't allocate more than 128k in current linux kernel, so
547 upload the firmware in chunks
548 */
549 if (data->fw_loaded < data->fw_size) {
550 len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
551 data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
552
553 if (len < CHUNK_SIZE)
554 memset(data->pfw_data, 0, CHUNK_SIZE);
555
556 dprintk(100, "completed len %d, loaded %d \n", len,
557 data->fw_loaded);
558
559 memcpy(data->pfw_data,
560 (char *) data->fw->data + data->fw_loaded, len);
561
562 usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
563 data->pfw_data, CHUNK_SIZE,
564 s2255_fwchunk_complete, data);
565 if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
566 dev_err(&udev->dev, "failed submit URB\n");
567 atomic_set(&data->fw_state, S2255_FW_FAILED);
568 /* wake up anything waiting for the firmware */
569 wake_up(&data->wait_fw);
570 return;
571 }
572 data->fw_loaded += len;
573 } else {
38f993ad 574 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
85b85482 575 dprintk(100, "%s: firmware upload complete\n", __func__);
38f993ad 576 }
38f993ad
DA
577 return;
578
579}
580
fe85ce90 581static int s2255_got_frame(struct s2255_channel *channel, int jpgsize)
38f993ad 582{
fe85ce90 583 struct s2255_dmaqueue *dma_q = &channel->vidq;
38f993ad 584 struct s2255_buffer *buf;
fe85ce90 585 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
38f993ad
DA
586 unsigned long flags = 0;
587 int rc = 0;
38f993ad 588 spin_lock_irqsave(&dev->slock, flags);
38f993ad
DA
589 if (list_empty(&dma_q->active)) {
590 dprintk(1, "No active queue to serve\n");
591 rc = -1;
592 goto unlock;
593 }
594 buf = list_entry(dma_q->active.next,
595 struct s2255_buffer, vb.queue);
38f993ad
DA
596 list_del(&buf->vb.queue);
597 do_gettimeofday(&buf->vb.ts);
fe85ce90 598 s2255_fillbuff(channel, buf, jpgsize);
38f993ad 599 wake_up(&buf->vb.done);
85b85482 600 dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i);
38f993ad
DA
601unlock:
602 spin_unlock_irqrestore(&dev->slock, flags);
f2770979 603 return rc;
38f993ad
DA
604}
605
38f993ad
DA
606static const struct s2255_fmt *format_by_fourcc(int fourcc)
607{
608 unsigned int i;
38f993ad
DA
609 for (i = 0; i < ARRAY_SIZE(formats); i++) {
610 if (-1 == formats[i].fourcc)
611 continue;
e42e28f9
SLD
612 if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) ||
613 (formats[i].fourcc == V4L2_PIX_FMT_MJPEG)))
614 continue;
38f993ad
DA
615 if (formats[i].fourcc == fourcc)
616 return formats + i;
617 }
618 return NULL;
619}
620
38f993ad
DA
621/* video buffer vmalloc implementation based partly on VIVI driver which is
622 * Copyright (c) 2006 by
623 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
624 * Ted Walther <ted--a.t--enumera.com>
625 * John Sokol <sokol--a.t--videotechnology.com>
626 * http://v4l.videotechnology.com/
627 *
628 */
fe85ce90
DA
629static void s2255_fillbuff(struct s2255_channel *channel,
630 struct s2255_buffer *buf, int jpgsize)
38f993ad
DA
631{
632 int pos = 0;
633 struct timeval ts;
634 const char *tmpbuf;
635 char *vbuf = videobuf_to_vmalloc(&buf->vb);
636 unsigned long last_frame;
38f993ad
DA
637
638 if (!vbuf)
639 return;
fe85ce90 640 last_frame = channel->last_frame;
38f993ad 641 if (last_frame != -1) {
38f993ad 642 tmpbuf =
fe85ce90 643 (const char *)channel->buffer.frame[last_frame].lpvbits;
38f993ad
DA
644 switch (buf->fmt->fourcc) {
645 case V4L2_PIX_FMT_YUYV:
646 case V4L2_PIX_FMT_UYVY:
647 planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
648 vbuf, buf->vb.width,
649 buf->vb.height,
650 buf->fmt->fourcc);
651 break;
652 case V4L2_PIX_FMT_GREY:
653 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
654 break;
14d96260 655 case V4L2_PIX_FMT_JPEG:
d0ef8540 656 case V4L2_PIX_FMT_MJPEG:
14d96260
DA
657 buf->vb.size = jpgsize;
658 memcpy(vbuf, tmpbuf, buf->vb.size);
659 break;
38f993ad
DA
660 case V4L2_PIX_FMT_YUV422P:
661 memcpy(vbuf, tmpbuf,
662 buf->vb.width * buf->vb.height * 2);
663 break;
664 default:
665 printk(KERN_DEBUG "s2255: unknown format?\n");
666 }
fe85ce90 667 channel->last_frame = -1;
38f993ad
DA
668 } else {
669 printk(KERN_ERR "s2255: =======no frame\n");
670 return;
671
672 }
673 dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n",
674 (unsigned long)vbuf, pos);
675 /* tell v4l buffer was filled */
676
fe85ce90 677 buf->vb.field_count = channel->frame_count * 2;
38f993ad
DA
678 do_gettimeofday(&ts);
679 buf->vb.ts = ts;
680 buf->vb.state = VIDEOBUF_DONE;
681}
682
683
684/* ------------------------------------------------------------------
685 Videobuf operations
686 ------------------------------------------------------------------*/
687
688static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
689 unsigned int *size)
690{
691 struct s2255_fh *fh = vq->priv_data;
fe85ce90
DA
692 struct s2255_channel *channel = fh->channel;
693 *size = channel->width * channel->height * (channel->fmt->depth >> 3);
38f993ad
DA
694
695 if (0 == *count)
696 *count = S2255_DEF_BUFS;
697
dab7e310
AB
698 if (*size * *count > vid_limit * 1024 * 1024)
699 *count = (vid_limit * 1024 * 1024) / *size;
38f993ad
DA
700
701 return 0;
702}
703
704static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf)
705{
706 dprintk(4, "%s\n", __func__);
707
38f993ad
DA
708 videobuf_vmalloc_free(&buf->vb);
709 buf->vb.state = VIDEOBUF_NEEDS_INIT;
710}
711
712static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
713 enum v4l2_field field)
714{
715 struct s2255_fh *fh = vq->priv_data;
fe85ce90 716 struct s2255_channel *channel = fh->channel;
38f993ad
DA
717 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
718 int rc;
fe85ce90
DA
719 int w = channel->width;
720 int h = channel->height;
38f993ad 721 dprintk(4, "%s, field=%d\n", __func__, field);
fe85ce90 722 if (channel->fmt == NULL)
38f993ad
DA
723 return -EINVAL;
724
fe85ce90
DA
725 if ((w < norm_minw(&channel->vdev)) ||
726 (w > norm_maxw(&channel->vdev)) ||
727 (h < norm_minh(&channel->vdev)) ||
728 (h > norm_maxh(&channel->vdev))) {
38f993ad
DA
729 dprintk(4, "invalid buffer prepare\n");
730 return -EINVAL;
731 }
fe85ce90 732 buf->vb.size = w * h * (channel->fmt->depth >> 3);
38f993ad
DA
733 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) {
734 dprintk(4, "invalid buffer prepare\n");
735 return -EINVAL;
736 }
737
fe85ce90
DA
738 buf->fmt = channel->fmt;
739 buf->vb.width = w;
740 buf->vb.height = h;
38f993ad
DA
741 buf->vb.field = field;
742
38f993ad
DA
743 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
744 rc = videobuf_iolock(vq, &buf->vb, NULL);
745 if (rc < 0)
746 goto fail;
747 }
748
749 buf->vb.state = VIDEOBUF_PREPARED;
750 return 0;
751fail:
752 free_buffer(vq, buf);
753 return rc;
754}
755
756static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
757{
758 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
759 struct s2255_fh *fh = vq->priv_data;
fe85ce90
DA
760 struct s2255_channel *channel = fh->channel;
761 struct s2255_dmaqueue *vidq = &channel->vidq;
38f993ad 762 dprintk(1, "%s\n", __func__);
38f993ad
DA
763 buf->vb.state = VIDEOBUF_QUEUED;
764 list_add_tail(&buf->vb.queue, &vidq->active);
765}
766
767static void buffer_release(struct videobuf_queue *vq,
768 struct videobuf_buffer *vb)
769{
770 struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb);
771 struct s2255_fh *fh = vq->priv_data;
fe85ce90 772 dprintk(4, "%s %d\n", __func__, fh->channel->idx);
38f993ad
DA
773 free_buffer(vq, buf);
774}
775
776static struct videobuf_queue_ops s2255_video_qops = {
777 .buf_setup = buffer_setup,
778 .buf_prepare = buffer_prepare,
779 .buf_queue = buffer_queue,
780 .buf_release = buffer_release,
781};
782
783
fe85ce90 784static int res_get(struct s2255_fh *fh)
38f993ad 785{
fe85ce90 786 struct s2255_channel *channel = fh->channel;
a19a5cd7
PE
787 /* is it free? */
788 if (channel->resources)
789 return 0; /* no, someone else uses it */
38f993ad 790 /* it's free, grab it */
fe85ce90
DA
791 channel->resources = 1;
792 fh->resources = 1;
f78d92c9 793 dprintk(1, "s2255: res: get\n");
38f993ad
DA
794 return 1;
795}
796
fe85ce90 797static int res_locked(struct s2255_fh *fh)
38f993ad 798{
fe85ce90 799 return fh->channel->resources;
38f993ad
DA
800}
801
f78d92c9
DA
802static int res_check(struct s2255_fh *fh)
803{
fe85ce90 804 return fh->resources;
f78d92c9
DA
805}
806
807
fe85ce90 808static void res_free(struct s2255_fh *fh)
38f993ad 809{
fe85ce90 810 struct s2255_channel *channel = fh->channel;
fe85ce90
DA
811 channel->resources = 0;
812 fh->resources = 0;
38f993ad
DA
813 dprintk(1, "res: put\n");
814}
815
5a34d9df
DA
816static int vidioc_querymenu(struct file *file, void *priv,
817 struct v4l2_querymenu *qmenu)
818{
819 static const char *colorfilter[] = {
820 "Off",
821 "On",
822 NULL
823 };
824 if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) {
825 int i;
826 const char **menu_items = colorfilter;
827 for (i = 0; i < qmenu->index && menu_items[i]; i++)
828 ; /* do nothing (from v4l2-common.c) */
829 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
830 return -EINVAL;
831 strlcpy(qmenu->name, menu_items[qmenu->index],
832 sizeof(qmenu->name));
833 return 0;
834 }
835 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
836}
837
38f993ad
DA
838static int vidioc_querycap(struct file *file, void *priv,
839 struct v4l2_capability *cap)
840{
841 struct s2255_fh *fh = file->private_data;
842 struct s2255_dev *dev = fh->dev;
843 strlcpy(cap->driver, "s2255", sizeof(cap->driver));
844 strlcpy(cap->card, "s2255", sizeof(cap->card));
e22ed887 845 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
38f993ad
DA
846 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
847 return 0;
848}
849
850static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
851 struct v4l2_fmtdesc *f)
852{
6c61ac63 853 int index = f->index;
38f993ad
DA
854
855 if (index >= ARRAY_SIZE(formats))
856 return -EINVAL;
6c61ac63
DC
857 if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) ||
858 (formats[index].fourcc == V4L2_PIX_FMT_MJPEG)))
859 return -EINVAL;
38f993ad
DA
860 dprintk(4, "name %s\n", formats[index].name);
861 strlcpy(f->description, formats[index].name, sizeof(f->description));
862 f->pixelformat = formats[index].fourcc;
863 return 0;
864}
865
866static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
867 struct v4l2_format *f)
868{
869 struct s2255_fh *fh = priv;
fe85ce90 870 struct s2255_channel *channel = fh->channel;
38f993ad 871
fe85ce90
DA
872 f->fmt.pix.width = channel->width;
873 f->fmt.pix.height = channel->height;
38f993ad 874 f->fmt.pix.field = fh->vb_vidq.field;
fe85ce90
DA
875 f->fmt.pix.pixelformat = channel->fmt->fourcc;
876 f->fmt.pix.bytesperline = f->fmt.pix.width * (channel->fmt->depth >> 3);
38f993ad 877 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
3f8d6f73 878 return 0;
38f993ad
DA
879}
880
881static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
882 struct v4l2_format *f)
883{
884 const struct s2255_fmt *fmt;
885 enum v4l2_field field;
886 int b_any_field = 0;
887 struct s2255_fh *fh = priv;
fe85ce90 888 struct s2255_channel *channel = fh->channel;
38f993ad 889 int is_ntsc;
38f993ad 890 is_ntsc =
fe85ce90 891 (channel->vdev.current_norm & V4L2_STD_NTSC) ? 1 : 0;
38f993ad
DA
892
893 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
894
895 if (fmt == NULL)
896 return -EINVAL;
897
898 field = f->fmt.pix.field;
899 if (field == V4L2_FIELD_ANY)
900 b_any_field = 1;
901
85b85482
DA
902 dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n",
903 __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
38f993ad
DA
904 if (is_ntsc) {
905 /* NTSC */
906 if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
907 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
908 if (b_any_field) {
909 field = V4L2_FIELD_SEQ_TB;
910 } else if (!((field == V4L2_FIELD_INTERLACED) ||
911 (field == V4L2_FIELD_SEQ_TB) ||
912 (field == V4L2_FIELD_INTERLACED_TB))) {
913 dprintk(1, "unsupported field setting\n");
914 return -EINVAL;
915 }
916 } else {
917 f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
918 if (b_any_field) {
919 field = V4L2_FIELD_TOP;
920 } else if (!((field == V4L2_FIELD_TOP) ||
921 (field == V4L2_FIELD_BOTTOM))) {
922 dprintk(1, "unsupported field setting\n");
923 return -EINVAL;
924 }
925
926 }
927 if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
928 f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
929 else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
930 f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
931 else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
932 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
933 else
934 f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
935 } else {
936 /* PAL */
937 if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
938 f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
939 if (b_any_field) {
940 field = V4L2_FIELD_SEQ_TB;
941 } else if (!((field == V4L2_FIELD_INTERLACED) ||
942 (field == V4L2_FIELD_SEQ_TB) ||
943 (field == V4L2_FIELD_INTERLACED_TB))) {
944 dprintk(1, "unsupported field setting\n");
945 return -EINVAL;
946 }
947 } else {
948 f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
949 if (b_any_field) {
950 field = V4L2_FIELD_TOP;
951 } else if (!((field == V4L2_FIELD_TOP) ||
952 (field == V4L2_FIELD_BOTTOM))) {
953 dprintk(1, "unsupported field setting\n");
954 return -EINVAL;
955 }
956 }
957 if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) {
38f993ad
DA
958 f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
959 field = V4L2_FIELD_SEQ_TB;
960 } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) {
38f993ad
DA
961 f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
962 field = V4L2_FIELD_TOP;
963 } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) {
38f993ad
DA
964 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
965 field = V4L2_FIELD_TOP;
966 } else {
38f993ad
DA
967 f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
968 field = V4L2_FIELD_TOP;
969 }
970 }
38f993ad
DA
971 f->fmt.pix.field = field;
972 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
973 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
85b85482
DA
974 dprintk(50, "%s: set width %d height %d field %d\n", __func__,
975 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
38f993ad
DA
976 return 0;
977}
978
979static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
980 struct v4l2_format *f)
981{
982 struct s2255_fh *fh = priv;
fe85ce90 983 struct s2255_channel *channel = fh->channel;
38f993ad
DA
984 const struct s2255_fmt *fmt;
985 struct videobuf_queue *q = &fh->vb_vidq;
fe85ce90 986 struct s2255_mode mode;
38f993ad 987 int ret;
38f993ad
DA
988
989 ret = vidioc_try_fmt_vid_cap(file, fh, f);
990
991 if (ret < 0)
3f8d6f73 992 return ret;
38f993ad
DA
993
994 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
995
996 if (fmt == NULL)
997 return -EINVAL;
998
999 mutex_lock(&q->vb_lock);
1000
1001 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1002 dprintk(1, "queue busy\n");
1003 ret = -EBUSY;
1004 goto out_s_fmt;
1005 }
1006
fe85ce90 1007 if (res_locked(fh)) {
85b85482 1008 dprintk(1, "%s: channel busy\n", __func__);
38f993ad
DA
1009 ret = -EBUSY;
1010 goto out_s_fmt;
1011 }
fe85ce90
DA
1012 mode = channel->mode;
1013 channel->fmt = fmt;
1014 channel->width = f->fmt.pix.width;
1015 channel->height = f->fmt.pix.height;
38f993ad
DA
1016 fh->vb_vidq.field = f->fmt.pix.field;
1017 fh->type = f->type;
fe85ce90
DA
1018 if (channel->width > norm_minw(&channel->vdev)) {
1019 if (channel->height > norm_minh(&channel->vdev)) {
1020 if (channel->cap_parm.capturemode &
85b85482 1021 V4L2_MODE_HIGHQUALITY)
fe85ce90 1022 mode.scale = SCALE_4CIFSI;
85b85482 1023 else
fe85ce90 1024 mode.scale = SCALE_4CIFS;
7d853532 1025 } else
fe85ce90 1026 mode.scale = SCALE_2CIFS;
38f993ad
DA
1027
1028 } else {
fe85ce90 1029 mode.scale = SCALE_1CIFS;
38f993ad 1030 }
38f993ad 1031 /* color mode */
fe85ce90 1032 switch (channel->fmt->fourcc) {
38f993ad 1033 case V4L2_PIX_FMT_GREY:
fe85ce90
DA
1034 mode.color &= ~MASK_COLOR;
1035 mode.color |= COLOR_Y8;
38f993ad 1036 break;
14d96260 1037 case V4L2_PIX_FMT_JPEG:
d0ef8540 1038 case V4L2_PIX_FMT_MJPEG:
fe85ce90
DA
1039 mode.color &= ~MASK_COLOR;
1040 mode.color |= COLOR_JPG;
1041 mode.color |= (channel->jc.quality << 8);
14d96260 1042 break;
38f993ad 1043 case V4L2_PIX_FMT_YUV422P:
fe85ce90
DA
1044 mode.color &= ~MASK_COLOR;
1045 mode.color |= COLOR_YUVPL;
38f993ad
DA
1046 break;
1047 case V4L2_PIX_FMT_YUYV:
1048 case V4L2_PIX_FMT_UYVY:
1049 default:
fe85ce90
DA
1050 mode.color &= ~MASK_COLOR;
1051 mode.color |= COLOR_YUVPK;
38f993ad
DA
1052 break;
1053 }
fe85ce90
DA
1054 if ((mode.color & MASK_COLOR) != (channel->mode.color & MASK_COLOR))
1055 mode.restart = 1;
1056 else if (mode.scale != channel->mode.scale)
1057 mode.restart = 1;
1058 else if (mode.format != channel->mode.format)
1059 mode.restart = 1;
1060 channel->mode = mode;
1061 (void) s2255_set_mode(channel, &mode);
38f993ad
DA
1062 ret = 0;
1063out_s_fmt:
1064 mutex_unlock(&q->vb_lock);
1065 return ret;
1066}
1067
1068static int vidioc_reqbufs(struct file *file, void *priv,
1069 struct v4l2_requestbuffers *p)
1070{
1071 int rc;
1072 struct s2255_fh *fh = priv;
1073 rc = videobuf_reqbufs(&fh->vb_vidq, p);
1074 return rc;
1075}
1076
1077static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1078{
1079 int rc;
1080 struct s2255_fh *fh = priv;
1081 rc = videobuf_querybuf(&fh->vb_vidq, p);
1082 return rc;
1083}
1084
1085static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1086{
1087 int rc;
1088 struct s2255_fh *fh = priv;
1089 rc = videobuf_qbuf(&fh->vb_vidq, p);
1090 return rc;
1091}
1092
1093static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1094{
1095 int rc;
1096 struct s2255_fh *fh = priv;
1097 rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK);
1098 return rc;
1099}
1100
38f993ad
DA
1101/* write to the configuration pipe, synchronously */
1102static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
1103 int size)
1104{
1105 int pipe;
1106 int done;
1107 long retval = -1;
1108 if (udev) {
1109 pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
1110 retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
1111 }
1112 return retval;
1113}
1114
1115static u32 get_transfer_size(struct s2255_mode *mode)
1116{
1117 int linesPerFrame = LINE_SZ_DEF;
1118 int pixelsPerLine = NUM_LINES_DEF;
1119 u32 outImageSize;
1120 u32 usbInSize;
1121 unsigned int mask_mult;
1122
1123 if (mode == NULL)
1124 return 0;
1125
1126 if (mode->format == FORMAT_NTSC) {
1127 switch (mode->scale) {
1128 case SCALE_4CIFS:
7d853532 1129 case SCALE_4CIFSI:
38f993ad
DA
1130 linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
1131 pixelsPerLine = LINE_SZ_4CIFS_NTSC;
1132 break;
1133 case SCALE_2CIFS:
1134 linesPerFrame = NUM_LINES_2CIFS_NTSC;
1135 pixelsPerLine = LINE_SZ_2CIFS_NTSC;
1136 break;
1137 case SCALE_1CIFS:
1138 linesPerFrame = NUM_LINES_1CIFS_NTSC;
1139 pixelsPerLine = LINE_SZ_1CIFS_NTSC;
1140 break;
1141 default:
1142 break;
1143 }
1144 } else if (mode->format == FORMAT_PAL) {
1145 switch (mode->scale) {
1146 case SCALE_4CIFS:
7d853532 1147 case SCALE_4CIFSI:
38f993ad
DA
1148 linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
1149 pixelsPerLine = LINE_SZ_4CIFS_PAL;
1150 break;
1151 case SCALE_2CIFS:
1152 linesPerFrame = NUM_LINES_2CIFS_PAL;
1153 pixelsPerLine = LINE_SZ_2CIFS_PAL;
1154 break;
1155 case SCALE_1CIFS:
1156 linesPerFrame = NUM_LINES_1CIFS_PAL;
1157 pixelsPerLine = LINE_SZ_1CIFS_PAL;
1158 break;
1159 default:
1160 break;
1161 }
1162 }
1163 outImageSize = linesPerFrame * pixelsPerLine;
14d96260 1164 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
38f993ad
DA
1165 /* 2 bytes/pixel if not monochrome */
1166 outImageSize *= 2;
1167 }
1168
1169 /* total bytes to send including prefix and 4K padding;
1170 must be a multiple of USB_READ_SIZE */
1171 usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */
1172 mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
1173 /* if size not a multiple of USB_READ_SIZE */
1174 if (usbInSize & ~mask_mult)
1175 usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
1176 return usbInSize;
1177}
1178
85b85482 1179static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
38f993ad
DA
1180{
1181 struct device *dev = &sdev->udev->dev;
1182 dev_info(dev, "------------------------------------------------\n");
85b85482
DA
1183 dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
1184 dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
38f993ad 1185 dev_info(dev, "bright: 0x%x\n", mode->bright);
38f993ad
DA
1186 dev_info(dev, "------------------------------------------------\n");
1187}
1188
1189/*
1190 * set mode is the function which controls the DSP.
1191 * the restart parameter in struct s2255_mode should be set whenever
1192 * the image size could change via color format, video system or image
1193 * size.
1194 * When the restart parameter is set, we sleep for ONE frame to allow the
1195 * DSP time to get the new frame
1196 */
fe85ce90 1197static int s2255_set_mode(struct s2255_channel *channel,
38f993ad
DA
1198 struct s2255_mode *mode)
1199{
1200 int res;
3fa00605 1201 __le32 *buffer;
38f993ad 1202 unsigned long chn_rev;
fe85ce90 1203 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
fe85ce90
DA
1204 chn_rev = G_chnmap[channel->idx];
1205 dprintk(3, "%s channel: %d\n", __func__, channel->idx);
22b88d48 1206 /* if JPEG, set the quality */
5a34d9df
DA
1207 if ((mode->color & MASK_COLOR) == COLOR_JPG) {
1208 mode->color &= ~MASK_COLOR;
1209 mode->color |= COLOR_JPG;
1210 mode->color &= ~MASK_JPG_QUALITY;
fe85ce90 1211 mode->color |= (channel->jc.quality << 8);
5a34d9df 1212 }
38f993ad 1213 /* save the mode */
fe85ce90
DA
1214 channel->mode = *mode;
1215 channel->req_image_size = get_transfer_size(mode);
1216 dprintk(1, "%s: reqsize %ld\n", __func__, channel->req_image_size);
38f993ad
DA
1217 buffer = kzalloc(512, GFP_KERNEL);
1218 if (buffer == NULL) {
1219 dev_err(&dev->udev->dev, "out of mem\n");
1220 return -ENOMEM;
1221 }
38f993ad
DA
1222 /* set the mode */
1223 buffer[0] = IN_DATA_TOKEN;
3fa00605 1224 buffer[1] = (__le32) cpu_to_le32(chn_rev);
38f993ad 1225 buffer[2] = CMD_SET_MODE;
fe85ce90
DA
1226 memcpy(&buffer[3], &channel->mode, sizeof(struct s2255_mode));
1227 channel->setmode_ready = 0;
38f993ad
DA
1228 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1229 if (debug)
85b85482 1230 s2255_print_cfg(dev, mode);
38f993ad 1231 kfree(buffer);
38f993ad 1232 /* wait at least 3 frames before continuing */
14d96260 1233 if (mode->restart) {
fe85ce90
DA
1234 wait_event_timeout(channel->wait_setmode,
1235 (channel->setmode_ready != 0),
14d96260 1236 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
fe85ce90 1237 if (channel->setmode_ready != 1) {
14d96260
DA
1238 printk(KERN_DEBUG "s2255: no set mode response\n");
1239 res = -EFAULT;
1240 }
1241 }
38f993ad 1242 /* clear the restart flag */
fe85ce90 1243 channel->mode.restart = 0;
fe85ce90 1244 dprintk(1, "%s chn %d, result: %d\n", __func__, channel->idx, res);
38f993ad
DA
1245 return res;
1246}
1247
fe85ce90 1248static int s2255_cmd_status(struct s2255_channel *channel, u32 *pstatus)
4de39f5d
DA
1249{
1250 int res;
3fa00605 1251 __le32 *buffer;
4de39f5d 1252 u32 chn_rev;
fe85ce90 1253 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
fe85ce90
DA
1254 chn_rev = G_chnmap[channel->idx];
1255 dprintk(4, "%s chan %d\n", __func__, channel->idx);
4de39f5d
DA
1256 buffer = kzalloc(512, GFP_KERNEL);
1257 if (buffer == NULL) {
1258 dev_err(&dev->udev->dev, "out of mem\n");
4de39f5d
DA
1259 return -ENOMEM;
1260 }
1261 /* form the get vid status command */
1262 buffer[0] = IN_DATA_TOKEN;
3fa00605 1263 buffer[1] = (__le32) cpu_to_le32(chn_rev);
4de39f5d
DA
1264 buffer[2] = CMD_STATUS;
1265 *pstatus = 0;
fe85ce90 1266 channel->vidstatus_ready = 0;
4de39f5d
DA
1267 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1268 kfree(buffer);
fe85ce90
DA
1269 wait_event_timeout(channel->wait_vidstatus,
1270 (channel->vidstatus_ready != 0),
4de39f5d 1271 msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
fe85ce90 1272 if (channel->vidstatus_ready != 1) {
4de39f5d
DA
1273 printk(KERN_DEBUG "s2255: no vidstatus response\n");
1274 res = -EFAULT;
1275 }
fe85ce90 1276 *pstatus = channel->vidstatus;
4de39f5d 1277 dprintk(4, "%s, vid status %d\n", __func__, *pstatus);
4de39f5d
DA
1278 return res;
1279}
1280
38f993ad
DA
1281static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1282{
1283 int res;
1284 struct s2255_fh *fh = priv;
1285 struct s2255_dev *dev = fh->dev;
fe85ce90 1286 struct s2255_channel *channel = fh->channel;
38f993ad
DA
1287 int j;
1288 dprintk(4, "%s\n", __func__);
1289 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1290 dev_err(&dev->udev->dev, "invalid fh type0\n");
1291 return -EINVAL;
1292 }
1293 if (i != fh->type) {
1294 dev_err(&dev->udev->dev, "invalid fh type1\n");
1295 return -EINVAL;
1296 }
1297
fe85ce90 1298 if (!res_get(fh)) {
be9ed511 1299 s2255_dev_err(&dev->udev->dev, "stream busy\n");
38f993ad
DA
1300 return -EBUSY;
1301 }
fe85ce90
DA
1302 channel->last_frame = -1;
1303 channel->bad_payload = 0;
1304 channel->cur_frame = 0;
1305 channel->frame_count = 0;
38f993ad 1306 for (j = 0; j < SYS_FRAMES; j++) {
fe85ce90
DA
1307 channel->buffer.frame[j].ulState = S2255_READ_IDLE;
1308 channel->buffer.frame[j].cur_size = 0;
38f993ad
DA
1309 }
1310 res = videobuf_streamon(&fh->vb_vidq);
1311 if (res == 0) {
fe85ce90
DA
1312 s2255_start_acquire(channel);
1313 channel->b_acquire = 1;
1314 } else
1315 res_free(fh);
1316
38f993ad
DA
1317 return res;
1318}
1319
1320static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1321{
38f993ad 1322 struct s2255_fh *fh = priv;
fe85ce90 1323 dprintk(4, "%s\n, channel: %d", __func__, fh->channel->idx);
38f993ad
DA
1324 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1325 printk(KERN_ERR "invalid fh type0\n");
1326 return -EINVAL;
1327 }
1328 if (i != fh->type) {
1329 printk(KERN_ERR "invalid type i\n");
1330 return -EINVAL;
1331 }
fe85ce90 1332 s2255_stop_acquire(fh->channel);
b7732a32 1333 videobuf_streamoff(&fh->vb_vidq);
fe85ce90 1334 res_free(fh);
f78d92c9 1335 return 0;
38f993ad
DA
1336}
1337
1338static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1339{
1340 struct s2255_fh *fh = priv;
fe85ce90 1341 struct s2255_mode mode;
38f993ad
DA
1342 struct videobuf_queue *q = &fh->vb_vidq;
1343 int ret = 0;
38f993ad
DA
1344 mutex_lock(&q->vb_lock);
1345 if (videobuf_queue_is_busy(q)) {
1346 dprintk(1, "queue busy\n");
1347 ret = -EBUSY;
1348 goto out_s_std;
1349 }
fe85ce90 1350 if (res_locked(fh)) {
38f993ad
DA
1351 dprintk(1, "can't change standard after started\n");
1352 ret = -EBUSY;
1353 goto out_s_std;
1354 }
fe85ce90 1355 mode = fh->channel->mode;
38f993ad 1356 if (*i & V4L2_STD_NTSC) {
e6b44bc5
DA
1357 dprintk(4, "%s NTSC\n", __func__);
1358 /* if changing format, reset frame decimation/intervals */
fe85ce90
DA
1359 if (mode.format != FORMAT_NTSC) {
1360 mode.restart = 1;
1361 mode.format = FORMAT_NTSC;
1362 mode.fdec = FDEC_1;
e6b44bc5 1363 }
38f993ad 1364 } else if (*i & V4L2_STD_PAL) {
e6b44bc5 1365 dprintk(4, "%s PAL\n", __func__);
fe85ce90
DA
1366 if (mode.format != FORMAT_PAL) {
1367 mode.restart = 1;
1368 mode.format = FORMAT_PAL;
1369 mode.fdec = FDEC_1;
e6b44bc5 1370 }
38f993ad
DA
1371 } else {
1372 ret = -EINVAL;
1373 }
fe85ce90
DA
1374 if (mode.restart)
1375 s2255_set_mode(fh->channel, &mode);
38f993ad
DA
1376out_s_std:
1377 mutex_unlock(&q->vb_lock);
1378 return ret;
1379}
1380
1381/* Sensoray 2255 is a multiple channel capture device.
1382 It does not have a "crossbar" of inputs.
1383 We use one V4L device per channel. The user must
1384 be aware that certain combinations are not allowed.
1385 For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1386 at once in color(you can do full fps on 4 channels with greyscale.
1387*/
1388static int vidioc_enum_input(struct file *file, void *priv,
1389 struct v4l2_input *inp)
1390{
4de39f5d
DA
1391 struct s2255_fh *fh = priv;
1392 struct s2255_dev *dev = fh->dev;
fe85ce90 1393 struct s2255_channel *channel = fh->channel;
4de39f5d 1394 u32 status = 0;
38f993ad
DA
1395 if (inp->index != 0)
1396 return -EINVAL;
38f993ad
DA
1397 inp->type = V4L2_INPUT_TYPE_CAMERA;
1398 inp->std = S2255_NORMS;
4de39f5d
DA
1399 inp->status = 0;
1400 if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1401 int rc;
fe85ce90 1402 rc = s2255_cmd_status(fh->channel, &status);
4de39f5d
DA
1403 dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status);
1404 if (rc == 0)
1405 inp->status = (status & 0x01) ? 0
1406 : V4L2_IN_ST_NO_SIGNAL;
1407 }
5a34d9df
DA
1408 switch (dev->pid) {
1409 case 0x2255:
1410 default:
1411 strlcpy(inp->name, "Composite", sizeof(inp->name));
1412 break;
1413 case 0x2257:
fe85ce90 1414 strlcpy(inp->name, (channel->idx < 2) ? "Composite" : "S-Video",
5a34d9df
DA
1415 sizeof(inp->name));
1416 break;
1417 }
3f8d6f73 1418 return 0;
38f993ad
DA
1419}
1420
1421static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1422{
1423 *i = 0;
1424 return 0;
1425}
1426static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1427{
1428 if (i > 0)
1429 return -EINVAL;
1430 return 0;
1431}
1432
1433/* --- controls ---------------------------------------------- */
1434static int vidioc_queryctrl(struct file *file, void *priv,
1435 struct v4l2_queryctrl *qc)
1436{
5a34d9df 1437 struct s2255_fh *fh = priv;
fe85ce90 1438 struct s2255_channel *channel = fh->channel;
5a34d9df 1439 struct s2255_dev *dev = fh->dev;
2e70db9a
DA
1440 switch (qc->id) {
1441 case V4L2_CID_BRIGHTNESS:
1442 v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT);
1443 break;
1444 case V4L2_CID_CONTRAST:
1445 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST);
1446 break;
1447 case V4L2_CID_SATURATION:
1448 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION);
1449 break;
1450 case V4L2_CID_HUE:
1451 v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE);
1452 break;
5a34d9df
DA
1453 case V4L2_CID_PRIVATE_COLORFILTER:
1454 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1455 return -EINVAL;
fe85ce90 1456 if ((dev->pid == 0x2257) && (channel->idx > 1))
5a34d9df
DA
1457 return -EINVAL;
1458 strlcpy(qc->name, "Color Filter", sizeof(qc->name));
1459 qc->type = V4L2_CTRL_TYPE_MENU;
1460 qc->minimum = 0;
1461 qc->maximum = 1;
1462 qc->step = 1;
1463 qc->default_value = 1;
1464 qc->flags = 0;
1465 break;
2e70db9a
DA
1466 default:
1467 return -EINVAL;
1468 }
1469 dprintk(4, "%s, id %d\n", __func__, qc->id);
1470 return 0;
38f993ad
DA
1471}
1472
1473static int vidioc_g_ctrl(struct file *file, void *priv,
1474 struct v4l2_control *ctrl)
1475{
2e70db9a 1476 struct s2255_fh *fh = priv;
5a34d9df 1477 struct s2255_dev *dev = fh->dev;
fe85ce90 1478 struct s2255_channel *channel = fh->channel;
2e70db9a
DA
1479 switch (ctrl->id) {
1480 case V4L2_CID_BRIGHTNESS:
fe85ce90 1481 ctrl->value = channel->mode.bright;
2e70db9a
DA
1482 break;
1483 case V4L2_CID_CONTRAST:
fe85ce90 1484 ctrl->value = channel->mode.contrast;
2e70db9a
DA
1485 break;
1486 case V4L2_CID_SATURATION:
fe85ce90 1487 ctrl->value = channel->mode.saturation;
2e70db9a
DA
1488 break;
1489 case V4L2_CID_HUE:
fe85ce90 1490 ctrl->value = channel->mode.hue;
2e70db9a 1491 break;
5a34d9df
DA
1492 case V4L2_CID_PRIVATE_COLORFILTER:
1493 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1494 return -EINVAL;
fe85ce90 1495 if ((dev->pid == 0x2257) && (channel->idx > 1))
5a34d9df 1496 return -EINVAL;
fe85ce90 1497 ctrl->value = !((channel->mode.color & MASK_INPUT_TYPE) >> 16);
5a34d9df 1498 break;
2e70db9a
DA
1499 default:
1500 return -EINVAL;
1501 }
1502 dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value);
1503 return 0;
38f993ad
DA
1504}
1505
1506static int vidioc_s_ctrl(struct file *file, void *priv,
1507 struct v4l2_control *ctrl)
1508{
38f993ad 1509 struct s2255_fh *fh = priv;
fe85ce90
DA
1510 struct s2255_channel *channel = fh->channel;
1511 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
1512 struct s2255_mode mode;
1513 mode = channel->mode;
2e70db9a
DA
1514 dprintk(4, "%s\n", __func__);
1515 /* update the mode to the corresponding value */
1516 switch (ctrl->id) {
1517 case V4L2_CID_BRIGHTNESS:
fe85ce90 1518 mode.bright = ctrl->value;
2e70db9a
DA
1519 break;
1520 case V4L2_CID_CONTRAST:
fe85ce90 1521 mode.contrast = ctrl->value;
2e70db9a
DA
1522 break;
1523 case V4L2_CID_HUE:
fe85ce90 1524 mode.hue = ctrl->value;
2e70db9a
DA
1525 break;
1526 case V4L2_CID_SATURATION:
fe85ce90 1527 mode.saturation = ctrl->value;
2e70db9a 1528 break;
5a34d9df
DA
1529 case V4L2_CID_PRIVATE_COLORFILTER:
1530 if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
1531 return -EINVAL;
fe85ce90 1532 if ((dev->pid == 0x2257) && (channel->idx > 1))
5a34d9df 1533 return -EINVAL;
fe85ce90
DA
1534 mode.color &= ~MASK_INPUT_TYPE;
1535 mode.color |= ((ctrl->value ? 0 : 1) << 16);
5a34d9df 1536 break;
2e70db9a
DA
1537 default:
1538 return -EINVAL;
38f993ad 1539 }
fe85ce90 1540 mode.restart = 0;
2e70db9a
DA
1541 /* set mode here. Note: stream does not need restarted.
1542 some V4L programs restart stream unnecessarily
1543 after a s_crtl.
1544 */
fe85ce90 1545 s2255_set_mode(fh->channel, &mode);
2e70db9a 1546 return 0;
38f993ad
DA
1547}
1548
22b88d48
DA
1549static int vidioc_g_jpegcomp(struct file *file, void *priv,
1550 struct v4l2_jpegcompression *jc)
1551{
1552 struct s2255_fh *fh = priv;
fe85ce90
DA
1553 struct s2255_channel *channel = fh->channel;
1554 *jc = channel->jc;
85b85482 1555 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
22b88d48
DA
1556 return 0;
1557}
1558
1559static int vidioc_s_jpegcomp(struct file *file, void *priv,
1560 struct v4l2_jpegcompression *jc)
1561{
1562 struct s2255_fh *fh = priv;
fe85ce90 1563 struct s2255_channel *channel = fh->channel;
22b88d48
DA
1564 if (jc->quality < 0 || jc->quality > 100)
1565 return -EINVAL;
fe85ce90 1566 channel->jc.quality = jc->quality;
85b85482 1567 dprintk(2, "%s: quality %d\n", __func__, jc->quality);
22b88d48
DA
1568 return 0;
1569}
7d853532
DA
1570
1571static int vidioc_g_parm(struct file *file, void *priv,
1572 struct v4l2_streamparm *sp)
1573{
1574 struct s2255_fh *fh = priv;
e6b44bc5 1575 __u32 def_num, def_dem;
fe85ce90 1576 struct s2255_channel *channel = fh->channel;
7d853532
DA
1577 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1578 return -EINVAL;
e6b44bc5
DA
1579 memset(sp, 0, sizeof(struct v4l2_streamparm));
1580 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
fe85ce90
DA
1581 sp->parm.capture.capturemode = channel->cap_parm.capturemode;
1582 def_num = (channel->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1583 def_dem = (channel->mode.format == FORMAT_NTSC) ? 30000 : 25000;
e6b44bc5 1584 sp->parm.capture.timeperframe.denominator = def_dem;
fe85ce90 1585 switch (channel->mode.fdec) {
e6b44bc5
DA
1586 default:
1587 case FDEC_1:
1588 sp->parm.capture.timeperframe.numerator = def_num;
1589 break;
1590 case FDEC_2:
1591 sp->parm.capture.timeperframe.numerator = def_num * 2;
1592 break;
1593 case FDEC_3:
1594 sp->parm.capture.timeperframe.numerator = def_num * 3;
1595 break;
1596 case FDEC_5:
1597 sp->parm.capture.timeperframe.numerator = def_num * 5;
1598 break;
1599 }
1600 dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__,
1601 sp->parm.capture.capturemode,
1602 sp->parm.capture.timeperframe.numerator,
1603 sp->parm.capture.timeperframe.denominator);
7d853532
DA
1604 return 0;
1605}
1606
1607static int vidioc_s_parm(struct file *file, void *priv,
1608 struct v4l2_streamparm *sp)
1609{
1610 struct s2255_fh *fh = priv;
fe85ce90
DA
1611 struct s2255_channel *channel = fh->channel;
1612 struct s2255_mode mode;
e6b44bc5
DA
1613 int fdec = FDEC_1;
1614 __u32 def_num, def_dem;
7d853532
DA
1615 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1616 return -EINVAL;
fe85ce90 1617 mode = channel->mode;
e6b44bc5 1618 /* high quality capture mode requires a stream restart */
fe85ce90
DA
1619 if (channel->cap_parm.capturemode
1620 != sp->parm.capture.capturemode && res_locked(fh))
e6b44bc5 1621 return -EBUSY;
fe85ce90
DA
1622 def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
1623 def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
e6b44bc5
DA
1624 if (def_dem != sp->parm.capture.timeperframe.denominator)
1625 sp->parm.capture.timeperframe.numerator = def_num;
1626 else if (sp->parm.capture.timeperframe.numerator <= def_num)
1627 sp->parm.capture.timeperframe.numerator = def_num;
1628 else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1629 sp->parm.capture.timeperframe.numerator = def_num * 2;
1630 fdec = FDEC_2;
1631 } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1632 sp->parm.capture.timeperframe.numerator = def_num * 3;
1633 fdec = FDEC_3;
1634 } else {
1635 sp->parm.capture.timeperframe.numerator = def_num * 5;
1636 fdec = FDEC_5;
1637 }
fe85ce90 1638 mode.fdec = fdec;
e6b44bc5 1639 sp->parm.capture.timeperframe.denominator = def_dem;
fe85ce90 1640 s2255_set_mode(channel, &mode);
e6b44bc5
DA
1641 dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1642 __func__,
1643 sp->parm.capture.capturemode,
1644 sp->parm.capture.timeperframe.numerator,
1645 sp->parm.capture.timeperframe.denominator, fdec);
1646 return 0;
1647}
7d853532 1648
e6b44bc5
DA
1649static int vidioc_enum_frameintervals(struct file *file, void *priv,
1650 struct v4l2_frmivalenum *fe)
1651{
1652 int is_ntsc = 0;
1653#define NUM_FRAME_ENUMS 4
1654 int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1655 if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS)
1656 return -EINVAL;
1657 switch (fe->width) {
1658 case 640:
1659 if (fe->height != 240 && fe->height != 480)
1660 return -EINVAL;
1661 is_ntsc = 1;
1662 break;
1663 case 320:
1664 if (fe->height != 240)
1665 return -EINVAL;
1666 is_ntsc = 1;
1667 break;
1668 case 704:
1669 if (fe->height != 288 && fe->height != 576)
1670 return -EINVAL;
1671 break;
1672 case 352:
1673 if (fe->height != 288)
1674 return -EINVAL;
1675 break;
1676 default:
1677 return -EINVAL;
1678 }
1679 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1680 fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1681 fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1682 dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator,
1683 fe->discrete.denominator);
7d853532
DA
1684 return 0;
1685}
e6b44bc5 1686
bec43661 1687static int s2255_open(struct file *file)
38f993ad 1688{
63b0d5ad 1689 struct video_device *vdev = video_devdata(file);
fe85ce90
DA
1690 struct s2255_channel *channel = video_drvdata(file);
1691 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
38f993ad 1692 struct s2255_fh *fh;
63b0d5ad 1693 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
14d96260 1694 int state;
50462eb0
LP
1695 dprintk(1, "s2255: open called (dev=%s)\n",
1696 video_device_node_name(vdev));
ff7e22df
DA
1697 /*
1698 * open lock necessary to prevent multiple instances
1699 * of v4l-conf (or other programs) from simultaneously
1700 * reloading firmware.
1701 */
1702 mutex_lock(&dev->open_lock);
1703 state = atomic_read(&dev->fw_data->fw_state);
1704 switch (state) {
1705 case S2255_FW_DISCONNECTING:
1706 mutex_unlock(&dev->open_lock);
14d96260 1707 return -ENODEV;
14d96260 1708 case S2255_FW_FAILED:
be9ed511
MCC
1709 s2255_dev_err(&dev->udev->dev,
1710 "firmware load failed. retrying.\n");
14d96260 1711 s2255_fwload_start(dev, 1);
38f993ad 1712 wait_event_timeout(dev->fw_data->wait_fw,
14d96260
DA
1713 ((atomic_read(&dev->fw_data->fw_state)
1714 == S2255_FW_SUCCESS) ||
1715 (atomic_read(&dev->fw_data->fw_state)
1716 == S2255_FW_DISCONNECTING)),
38f993ad 1717 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
ff7e22df
DA
1718 /* state may have changed, re-read */
1719 state = atomic_read(&dev->fw_data->fw_state);
14d96260
DA
1720 break;
1721 case S2255_FW_NOTLOADED:
1722 case S2255_FW_LOADED_DSPWAIT:
38f993ad
DA
1723 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1724 driver loaded and then device immediately opened */
1725 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1726 wait_event_timeout(dev->fw_data->wait_fw,
14d96260
DA
1727 ((atomic_read(&dev->fw_data->fw_state)
1728 == S2255_FW_SUCCESS) ||
1729 (atomic_read(&dev->fw_data->fw_state)
1730 == S2255_FW_DISCONNECTING)),
eb78deec 1731 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
ff7e22df
DA
1732 /* state may have changed, re-read */
1733 state = atomic_read(&dev->fw_data->fw_state);
14d96260
DA
1734 break;
1735 case S2255_FW_SUCCESS:
1736 default:
1737 break;
1738 }
ff7e22df
DA
1739 /* state may have changed in above switch statement */
1740 switch (state) {
1741 case S2255_FW_SUCCESS:
1742 break;
1743 case S2255_FW_FAILED:
1744 printk(KERN_INFO "2255 firmware load failed.\n");
eb78deec 1745 mutex_unlock(&dev->open_lock);
ff7e22df
DA
1746 return -ENODEV;
1747 case S2255_FW_DISCONNECTING:
1748 printk(KERN_INFO "%s: disconnecting\n", __func__);
eb78deec 1749 mutex_unlock(&dev->open_lock);
ff7e22df
DA
1750 return -ENODEV;
1751 case S2255_FW_LOADED_DSPWAIT:
1752 case S2255_FW_NOTLOADED:
1753 printk(KERN_INFO "%s: firmware not loaded yet"
1754 "please try again later\n",
1755 __func__);
eb78deec
DA
1756 /*
1757 * Timeout on firmware load means device unusable.
1758 * Set firmware failure state.
1759 * On next s2255_open the firmware will be reloaded.
1760 */
1761 atomic_set(&dev->fw_data->fw_state,
1762 S2255_FW_FAILED);
1763 mutex_unlock(&dev->open_lock);
ff7e22df
DA
1764 return -EAGAIN;
1765 default:
1766 printk(KERN_INFO "%s: unknown state\n", __func__);
eb78deec 1767 mutex_unlock(&dev->open_lock);
ff7e22df 1768 return -EFAULT;
38f993ad 1769 }
eb78deec 1770 mutex_unlock(&dev->open_lock);
38f993ad
DA
1771 /* allocate + initialize per filehandle data */
1772 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
a5ef91c9 1773 if (NULL == fh)
38f993ad 1774 return -ENOMEM;
38f993ad
DA
1775 file->private_data = fh;
1776 fh->dev = dev;
1777 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fe85ce90
DA
1778 fh->channel = channel;
1779 if (!channel->configured) {
1780 /* configure channel to default state */
1781 channel->fmt = &formats[0];
1782 s2255_set_mode(channel, &channel->mode);
1783 channel->configured = 1;
14d96260 1784 }
85b85482 1785 dprintk(1, "%s: dev=%s type=%s\n", __func__,
ff7e22df 1786 video_device_node_name(vdev), v4l2_type_names[type]);
85b85482 1787 dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__,
38f993ad 1788 (unsigned long)fh, (unsigned long)dev,
fe85ce90 1789 (unsigned long)&channel->vidq);
85b85482 1790 dprintk(4, "%s: list_empty active=%d\n", __func__,
fe85ce90 1791 list_empty(&channel->vidq.active));
38f993ad
DA
1792 videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops,
1793 NULL, &dev->slock,
1794 fh->type,
1795 V4L2_FIELD_INTERLACED,
a19a5cd7
PE
1796 sizeof(struct s2255_buffer),
1797 fh, vdev->lock);
38f993ad
DA
1798 return 0;
1799}
1800
1801
1802static unsigned int s2255_poll(struct file *file,
1803 struct poll_table_struct *wait)
1804{
1805 struct s2255_fh *fh = file->private_data;
1806 int rc;
1807 dprintk(100, "%s\n", __func__);
38f993ad
DA
1808 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1809 return POLLERR;
38f993ad
DA
1810 rc = videobuf_poll_stream(file, &fh->vb_vidq, wait);
1811 return rc;
1812}
1813
d62e85a0 1814static void s2255_destroy(struct s2255_dev *dev)
38f993ad 1815{
38f993ad
DA
1816 /* board shutdown stops the read pipe if it is running */
1817 s2255_board_shutdown(dev);
38f993ad 1818 /* make sure firmware still not trying to load */
f78d92c9 1819 del_timer(&dev->timer); /* only started in .probe and .open */
38f993ad 1820 if (dev->fw_data->fw_urb) {
38f993ad
DA
1821 usb_kill_urb(dev->fw_data->fw_urb);
1822 usb_free_urb(dev->fw_data->fw_urb);
1823 dev->fw_data->fw_urb = NULL;
1824 }
3fc82fa0 1825 release_firmware(dev->fw_data->fw);
f78d92c9
DA
1826 kfree(dev->fw_data->pfw_data);
1827 kfree(dev->fw_data);
ff7e22df
DA
1828 /* reset the DSP so firmware can be reloaded next time */
1829 s2255_reset_dsppower(dev);
1830 mutex_destroy(&dev->open_lock);
1831 mutex_destroy(&dev->lock);
38f993ad 1832 usb_put_dev(dev->udev);
fe85ce90 1833 v4l2_device_unregister(&dev->v4l2_dev);
38f993ad 1834 dprintk(1, "%s", __func__);
b7732a32 1835 kfree(dev);
38f993ad
DA
1836}
1837
ff7e22df 1838static int s2255_release(struct file *file)
38f993ad
DA
1839{
1840 struct s2255_fh *fh = file->private_data;
1841 struct s2255_dev *dev = fh->dev;
50462eb0 1842 struct video_device *vdev = video_devdata(file);
fe85ce90 1843 struct s2255_channel *channel = fh->channel;
38f993ad
DA
1844 if (!dev)
1845 return -ENODEV;
f78d92c9
DA
1846 /* turn off stream */
1847 if (res_check(fh)) {
fe85ce90
DA
1848 if (channel->b_acquire)
1849 s2255_stop_acquire(fh->channel);
f78d92c9 1850 videobuf_streamoff(&fh->vb_vidq);
fe85ce90 1851 res_free(fh);
f78d92c9 1852 }
38f993ad 1853 videobuf_mmap_free(&fh->vb_vidq);
ff7e22df 1854 dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev));
f78d92c9 1855 kfree(fh);
38f993ad
DA
1856 return 0;
1857}
1858
1859static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma)
1860{
1861 struct s2255_fh *fh = file->private_data;
1862 int ret;
1863
1864 if (!fh)
1865 return -ENODEV;
85b85482 1866 dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma);
38f993ad 1867 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
85b85482 1868 dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__,
38f993ad
DA
1869 (unsigned long)vma->vm_start,
1870 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
38f993ad
DA
1871 return ret;
1872}
1873
bec43661 1874static const struct v4l2_file_operations s2255_fops_v4l = {
38f993ad
DA
1875 .owner = THIS_MODULE,
1876 .open = s2255_open,
ff7e22df 1877 .release = s2255_release,
38f993ad 1878 .poll = s2255_poll,
a19a5cd7 1879 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
38f993ad 1880 .mmap = s2255_mmap_v4l,
38f993ad
DA
1881};
1882
a399810c 1883static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
5a34d9df 1884 .vidioc_querymenu = vidioc_querymenu,
38f993ad
DA
1885 .vidioc_querycap = vidioc_querycap,
1886 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1887 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1888 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1889 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1890 .vidioc_reqbufs = vidioc_reqbufs,
1891 .vidioc_querybuf = vidioc_querybuf,
1892 .vidioc_qbuf = vidioc_qbuf,
1893 .vidioc_dqbuf = vidioc_dqbuf,
1894 .vidioc_s_std = vidioc_s_std,
1895 .vidioc_enum_input = vidioc_enum_input,
1896 .vidioc_g_input = vidioc_g_input,
1897 .vidioc_s_input = vidioc_s_input,
1898 .vidioc_queryctrl = vidioc_queryctrl,
1899 .vidioc_g_ctrl = vidioc_g_ctrl,
1900 .vidioc_s_ctrl = vidioc_s_ctrl,
1901 .vidioc_streamon = vidioc_streamon,
1902 .vidioc_streamoff = vidioc_streamoff,
22b88d48
DA
1903 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1904 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
7d853532
DA
1905 .vidioc_s_parm = vidioc_s_parm,
1906 .vidioc_g_parm = vidioc_g_parm,
e6b44bc5 1907 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
a399810c
HV
1908};
1909
ff7e22df
DA
1910static void s2255_video_device_release(struct video_device *vdev)
1911{
fe85ce90
DA
1912 struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1913 dprintk(4, "%s, chnls: %d \n", __func__,
1914 atomic_read(&dev->num_channels));
1915 if (atomic_dec_and_test(&dev->num_channels))
d62e85a0 1916 s2255_destroy(dev);
ff7e22df
DA
1917 return;
1918}
1919
a399810c
HV
1920static struct video_device template = {
1921 .name = "s2255v",
a399810c
HV
1922 .fops = &s2255_fops_v4l,
1923 .ioctl_ops = &s2255_ioctl_ops,
ff7e22df 1924 .release = s2255_video_device_release,
38f993ad
DA
1925 .tvnorms = S2255_NORMS,
1926 .current_norm = V4L2_STD_NTSC_M,
1927};
1928
1929static int s2255_probe_v4l(struct s2255_dev *dev)
1930{
1931 int ret;
1932 int i;
1933 int cur_nr = video_nr;
fe85ce90 1934 struct s2255_channel *channel;
65c6edb3
DA
1935 ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1936 if (ret)
1937 return ret;
38f993ad 1938 /* initialize all video 4 linux */
38f993ad
DA
1939 /* register 4 video devices */
1940 for (i = 0; i < MAX_CHANNELS; i++) {
fe85ce90
DA
1941 channel = &dev->channel[i];
1942 INIT_LIST_HEAD(&channel->vidq.active);
1943 channel->vidq.dev = dev;
38f993ad 1944 /* register 4 video devices */
fe85ce90 1945 channel->vdev = template;
a19a5cd7 1946 channel->vdev.lock = &dev->lock;
5126f259
HV
1947 /* Locking in file operations other than ioctl should be done
1948 by the driver, not the V4L2 core.
1949 This driver needs auditing so that this flag can be removed. */
1950 set_bit(V4L2_FL_LOCK_ALL_FOPS, &channel->vdev.flags);
fe85ce90
DA
1951 channel->vdev.v4l2_dev = &dev->v4l2_dev;
1952 video_set_drvdata(&channel->vdev, channel);
38f993ad 1953 if (video_nr == -1)
fe85ce90 1954 ret = video_register_device(&channel->vdev,
38f993ad
DA
1955 VFL_TYPE_GRABBER,
1956 video_nr);
1957 else
fe85ce90 1958 ret = video_register_device(&channel->vdev,
38f993ad
DA
1959 VFL_TYPE_GRABBER,
1960 cur_nr + i);
fe85ce90 1961
3a67b5cc 1962 if (ret) {
38f993ad
DA
1963 dev_err(&dev->udev->dev,
1964 "failed to register video device!\n");
3a67b5cc 1965 break;
38f993ad 1966 }
fe85ce90 1967 atomic_inc(&dev->num_channels);
65c6edb3 1968 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
fe85ce90 1969 video_device_node_name(&channel->vdev));
3a67b5cc 1970
38f993ad 1971 }
64dc3c1a
MCC
1972 printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %s\n",
1973 S2255_VERSION);
3a67b5cc 1974 /* if no channels registered, return error and probe will fail*/
fe85ce90 1975 if (atomic_read(&dev->num_channels) == 0) {
65c6edb3 1976 v4l2_device_unregister(&dev->v4l2_dev);
3a67b5cc 1977 return ret;
65c6edb3 1978 }
fe85ce90 1979 if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
3a67b5cc
DA
1980 printk(KERN_WARNING "s2255: Not all channels available.\n");
1981 return 0;
38f993ad
DA
1982}
1983
38f993ad
DA
1984/* this function moves the usb stream read pipe data
1985 * into the system buffers.
1986 * returns 0 on success, EAGAIN if more data to process( call this
1987 * function again).
1988 *
1989 * Received frame structure:
14d96260 1990 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
38f993ad
DA
1991 * bytes 4-7: channel: 0-3
1992 * bytes 8-11: payload size: size of the frame
1993 * bytes 12-payloadsize+12: frame data
1994 */
1995static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1996{
38f993ad
DA
1997 char *pdest;
1998 u32 offset = 0;
14d96260 1999 int bframe = 0;
38f993ad
DA
2000 char *psrc;
2001 unsigned long copy_size;
2002 unsigned long size;
2003 s32 idx = -1;
2004 struct s2255_framei *frm;
2005 unsigned char *pdata;
fe85ce90 2006 struct s2255_channel *channel;
38f993ad 2007 dprintk(100, "buffer to user\n");
fe85ce90
DA
2008 channel = &dev->channel[dev->cc];
2009 idx = channel->cur_frame;
2010 frm = &channel->buffer.frame[idx];
14d96260
DA
2011 if (frm->ulState == S2255_READ_IDLE) {
2012 int jj;
2013 unsigned int cc;
3fa00605 2014 __le32 *pdword; /*data from dsp is little endian */
14d96260
DA
2015 int payload;
2016 /* search for marker codes */
2017 pdata = (unsigned char *)pipe_info->transfer_buffer;
3fa00605 2018 pdword = (__le32 *)pdata;
14d96260 2019 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
3fa00605 2020 switch (*pdword) {
14d96260 2021 case S2255_MARKER_FRAME:
14d96260
DA
2022 dprintk(4, "found frame marker at offset:"
2023 " %d [%x %x]\n", jj, pdata[0],
2024 pdata[1]);
2025 offset = jj + PREFIX_SIZE;
2026 bframe = 1;
3b2a6306 2027 cc = le32_to_cpu(pdword[1]);
14d96260
DA
2028 if (cc >= MAX_CHANNELS) {
2029 printk(KERN_ERR
2030 "bad channel\n");
2031 return -EINVAL;
2032 }
2033 /* reverse it */
2034 dev->cc = G_chnmap[cc];
fe85ce90 2035 channel = &dev->channel[dev->cc];
3b2a6306 2036 payload = le32_to_cpu(pdword[3]);
fe85ce90
DA
2037 if (payload > channel->req_image_size) {
2038 channel->bad_payload++;
14d96260
DA
2039 /* discard the bad frame */
2040 return -EINVAL;
2041 }
fe85ce90 2042 channel->pkt_size = payload;
3b2a6306 2043 channel->jpg_size = le32_to_cpu(pdword[4]);
14d96260
DA
2044 break;
2045 case S2255_MARKER_RESPONSE:
fe85ce90 2046
14d96260
DA
2047 pdata += DEF_USB_BLOCK;
2048 jj += DEF_USB_BLOCK;
3b2a6306 2049 if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
14d96260 2050 break;
3b2a6306 2051 cc = G_chnmap[le32_to_cpu(pdword[1])];
f14a2972 2052 if (cc >= MAX_CHANNELS)
14d96260 2053 break;
fe85ce90 2054 channel = &dev->channel[cc];
14d96260 2055 switch (pdword[2]) {
abce21f4 2056 case S2255_RESPONSE_SETMODE:
14d96260
DA
2057 /* check if channel valid */
2058 /* set mode ready */
fe85ce90
DA
2059 channel->setmode_ready = 1;
2060 wake_up(&channel->wait_setmode);
14d96260 2061 dprintk(5, "setmode ready %d\n", cc);
38f993ad 2062 break;
abce21f4 2063 case S2255_RESPONSE_FW:
14d96260
DA
2064 dev->chn_ready |= (1 << cc);
2065 if ((dev->chn_ready & 0x0f) != 0x0f)
2066 break;
2067 /* all channels ready */
2068 printk(KERN_INFO "s2255: fw loaded\n");
2069 atomic_set(&dev->fw_data->fw_state,
2070 S2255_FW_SUCCESS);
2071 wake_up(&dev->fw_data->wait_fw);
2072 break;
4de39f5d 2073 case S2255_RESPONSE_STATUS:
3b2a6306 2074 channel->vidstatus = le32_to_cpu(pdword[3]);
fe85ce90
DA
2075 channel->vidstatus_ready = 1;
2076 wake_up(&channel->wait_vidstatus);
4de39f5d 2077 dprintk(5, "got vidstatus %x chan %d\n",
3b2a6306 2078 le32_to_cpu(pdword[3]), cc);
4de39f5d 2079 break;
14d96260 2080 default:
af901ca1 2081 printk(KERN_INFO "s2255 unknown resp\n");
38f993ad 2082 }
14d96260 2083 default:
38f993ad 2084 pdata++;
14d96260 2085 break;
38f993ad 2086 }
14d96260
DA
2087 if (bframe)
2088 break;
2089 } /* for */
2090 if (!bframe)
2091 return -EINVAL;
38f993ad 2092 }
fe85ce90
DA
2093 channel = &dev->channel[dev->cc];
2094 idx = channel->cur_frame;
2095 frm = &channel->buffer.frame[idx];
14d96260 2096 /* search done. now find out if should be acquiring on this channel */
fe85ce90 2097 if (!channel->b_acquire) {
14d96260
DA
2098 /* we found a frame, but this channel is turned off */
2099 frm->ulState = S2255_READ_IDLE;
2100 return -EINVAL;
38f993ad
DA
2101 }
2102
14d96260
DA
2103 if (frm->ulState == S2255_READ_IDLE) {
2104 frm->ulState = S2255_READ_FRAME;
2105 frm->cur_size = 0;
38f993ad
DA
2106 }
2107
14d96260
DA
2108 /* skip the marker 512 bytes (and offset if out of sync) */
2109 psrc = (u8 *)pipe_info->transfer_buffer + offset;
2110
2111
38f993ad
DA
2112 if (frm->lpvbits == NULL) {
2113 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
2114 frm, dev, dev->cc, idx);
2115 return -ENOMEM;
2116 }
2117
2118 pdest = frm->lpvbits + frm->cur_size;
2119
14d96260 2120 copy_size = (pipe_info->cur_transfer_size - offset);
38f993ad 2121
fe85ce90 2122 size = channel->pkt_size - PREFIX_SIZE;
38f993ad 2123
14d96260 2124 /* sanity check on pdest */
fe85ce90 2125 if ((copy_size + frm->cur_size) < channel->req_image_size)
14d96260 2126 memcpy(pdest, psrc, copy_size);
38f993ad 2127
38f993ad 2128 frm->cur_size += copy_size;
14d96260
DA
2129 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
2130
2131 if (frm->cur_size >= size) {
38f993ad 2132 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
fe85ce90
DA
2133 dev->cc, idx);
2134 channel->last_frame = channel->cur_frame;
2135 channel->cur_frame++;
38f993ad 2136 /* end of system frame ring buffer, start at zero */
fe85ce90
DA
2137 if ((channel->cur_frame == SYS_FRAMES) ||
2138 (channel->cur_frame == channel->buffer.dwFrames))
2139 channel->cur_frame = 0;
14d96260 2140 /* frame ready */
fe85ce90
DA
2141 if (channel->b_acquire)
2142 s2255_got_frame(channel, channel->jpg_size);
2143 channel->frame_count++;
14d96260
DA
2144 frm->ulState = S2255_READ_IDLE;
2145 frm->cur_size = 0;
2146
38f993ad
DA
2147 }
2148 /* done successfully */
2149 return 0;
2150}
2151
2152static void s2255_read_video_callback(struct s2255_dev *dev,
2153 struct s2255_pipeinfo *pipe_info)
2154{
2155 int res;
2156 dprintk(50, "callback read video \n");
2157
2158 if (dev->cc >= MAX_CHANNELS) {
2159 dev->cc = 0;
2160 dev_err(&dev->udev->dev, "invalid channel\n");
2161 return;
2162 }
2163 /* otherwise copy to the system buffers */
2164 res = save_frame(dev, pipe_info);
14d96260
DA
2165 if (res != 0)
2166 dprintk(4, "s2255: read callback failed\n");
38f993ad
DA
2167
2168 dprintk(50, "callback read video done\n");
2169 return;
2170}
2171
2172static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
2173 u16 Index, u16 Value, void *TransferBuffer,
2174 s32 TransferBufferLength, int bOut)
2175{
2176 int r;
2177 if (!bOut) {
2178 r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2179 Request,
2180 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
2181 USB_DIR_IN,
2182 Value, Index, TransferBuffer,
2183 TransferBufferLength, HZ * 5);
2184 } else {
2185 r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2186 Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2187 Value, Index, TransferBuffer,
2188 TransferBufferLength, HZ * 5);
2189 }
2190 return r;
2191}
2192
2193/*
2194 * retrieve FX2 firmware version. future use.
2195 * @param dev pointer to device extension
2196 * @return -1 for fail, else returns firmware version as an int(16 bits)
2197 */
2198static int s2255_get_fx2fw(struct s2255_dev *dev)
2199{
2200 int fw;
2201 int ret;
2202 unsigned char transBuffer[64];
2203 ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
2204 S2255_VR_IN);
2205 if (ret < 0)
2206 dprintk(2, "get fw error: %x\n", ret);
2207 fw = transBuffer[0] + (transBuffer[1] << 8);
2208 dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
2209 return fw;
2210}
2211
2212/*
2213 * Create the system ring buffer to copy frames into from the
2214 * usb read pipe.
2215 */
fe85ce90 2216static int s2255_create_sys_buffers(struct s2255_channel *channel)
38f993ad
DA
2217{
2218 unsigned long i;
2219 unsigned long reqsize;
2220 dprintk(1, "create sys buffers\n");
fe85ce90 2221 channel->buffer.dwFrames = SYS_FRAMES;
38f993ad
DA
2222 /* always allocate maximum size(PAL) for system buffers */
2223 reqsize = SYS_FRAMES_MAXSIZE;
2224
2225 if (reqsize > SYS_FRAMES_MAXSIZE)
2226 reqsize = SYS_FRAMES_MAXSIZE;
2227
2228 for (i = 0; i < SYS_FRAMES; i++) {
2229 /* allocate the frames */
fe85ce90
DA
2230 channel->buffer.frame[i].lpvbits = vmalloc(reqsize);
2231 dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n",
2232 &channel->buffer.frame[i], channel->idx, i,
2233 channel->buffer.frame[i].lpvbits);
2234 channel->buffer.frame[i].size = reqsize;
2235 if (channel->buffer.frame[i].lpvbits == NULL) {
38f993ad 2236 printk(KERN_INFO "out of memory. using less frames\n");
fe85ce90 2237 channel->buffer.dwFrames = i;
38f993ad
DA
2238 break;
2239 }
2240 }
2241
2242 /* make sure internal states are set */
2243 for (i = 0; i < SYS_FRAMES; i++) {
fe85ce90
DA
2244 channel->buffer.frame[i].ulState = 0;
2245 channel->buffer.frame[i].cur_size = 0;
38f993ad
DA
2246 }
2247
fe85ce90
DA
2248 channel->cur_frame = 0;
2249 channel->last_frame = -1;
38f993ad
DA
2250 return 0;
2251}
2252
fe85ce90 2253static int s2255_release_sys_buffers(struct s2255_channel *channel)
38f993ad
DA
2254{
2255 unsigned long i;
2256 dprintk(1, "release sys buffers\n");
2257 for (i = 0; i < SYS_FRAMES; i++) {
fe85ce90 2258 if (channel->buffer.frame[i].lpvbits) {
38f993ad 2259 dprintk(1, "vfree %p\n",
fe85ce90
DA
2260 channel->buffer.frame[i].lpvbits);
2261 vfree(channel->buffer.frame[i].lpvbits);
38f993ad 2262 }
fe85ce90 2263 channel->buffer.frame[i].lpvbits = NULL;
38f993ad
DA
2264 }
2265 return 0;
2266}
2267
2268static int s2255_board_init(struct s2255_dev *dev)
2269{
38f993ad
DA
2270 struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
2271 int fw_ver;
ab85c6a3
DA
2272 int j;
2273 struct s2255_pipeinfo *pipe = &dev->pipe;
38f993ad 2274 dprintk(4, "board init: %p", dev);
ab85c6a3
DA
2275 memset(pipe, 0, sizeof(*pipe));
2276 pipe->dev = dev;
2277 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2278 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2279
2280 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2281 GFP_KERNEL);
2282 if (pipe->transfer_buffer == NULL) {
2283 dprintk(1, "out of memory!\n");
2284 return -ENOMEM;
38f993ad 2285 }
38f993ad
DA
2286 /* query the firmware */
2287 fw_ver = s2255_get_fx2fw(dev);
2288
8a8cc952 2289 printk(KERN_INFO "s2255: usb firmware version %d.%d\n",
abce21f4
DA
2290 (fw_ver >> 8) & 0xff,
2291 fw_ver & 0xff);
2292
2293 if (fw_ver < S2255_CUR_USB_FWVER)
8a8cc952 2294 printk(KERN_INFO "s2255: newer USB firmware available\n");
38f993ad
DA
2295
2296 for (j = 0; j < MAX_CHANNELS; j++) {
fe85ce90
DA
2297 struct s2255_channel *channel = &dev->channel[j];
2298 channel->b_acquire = 0;
2299 channel->mode = mode_def;
5a34d9df 2300 if (dev->pid == 0x2257 && j > 1)
fe85ce90
DA
2301 channel->mode.color |= (1 << 16);
2302 channel->jc.quality = S2255_DEF_JPEG_QUAL;
2303 channel->width = LINE_SZ_4CIFS_NTSC;
2304 channel->height = NUM_LINES_4CIFS_NTSC * 2;
2305 channel->fmt = &formats[0];
2306 channel->mode.restart = 1;
2307 channel->req_image_size = get_transfer_size(&mode_def);
2308 channel->frame_count = 0;
38f993ad 2309 /* create the system buffers */
fe85ce90 2310 s2255_create_sys_buffers(channel);
38f993ad
DA
2311 }
2312 /* start read pipe */
2313 s2255_start_readpipe(dev);
85b85482 2314 dprintk(1, "%s: success\n", __func__);
38f993ad
DA
2315 return 0;
2316}
2317
2318static int s2255_board_shutdown(struct s2255_dev *dev)
2319{
2320 u32 i;
85b85482 2321 dprintk(1, "%s: dev: %p", __func__, dev);
38f993ad
DA
2322
2323 for (i = 0; i < MAX_CHANNELS; i++) {
fe85ce90
DA
2324 if (dev->channel[i].b_acquire)
2325 s2255_stop_acquire(&dev->channel[i]);
38f993ad 2326 }
38f993ad 2327 s2255_stop_readpipe(dev);
38f993ad 2328 for (i = 0; i < MAX_CHANNELS; i++)
fe85ce90 2329 s2255_release_sys_buffers(&dev->channel[i]);
ab85c6a3
DA
2330 /* release transfer buffer */
2331 kfree(dev->pipe.transfer_buffer);
38f993ad
DA
2332 return 0;
2333}
2334
2335static void read_pipe_completion(struct urb *purb)
2336{
2337 struct s2255_pipeinfo *pipe_info;
2338 struct s2255_dev *dev;
2339 int status;
2340 int pipe;
38f993ad 2341 pipe_info = purb->context;
85b85482 2342 dprintk(100, "%s: urb:%p, status %d\n", __func__, purb,
38f993ad
DA
2343 purb->status);
2344 if (pipe_info == NULL) {
be9ed511 2345 dev_err(&purb->dev->dev, "no context!\n");
38f993ad
DA
2346 return;
2347 }
2348
2349 dev = pipe_info->dev;
2350 if (dev == NULL) {
be9ed511 2351 dev_err(&purb->dev->dev, "no context!\n");
38f993ad
DA
2352 return;
2353 }
2354 status = purb->status;
b02064ca
DA
2355 /* if shutting down, do not resubmit, exit immediately */
2356 if (status == -ESHUTDOWN) {
85b85482 2357 dprintk(2, "%s: err shutdown\n", __func__);
b02064ca 2358 pipe_info->err_count++;
38f993ad
DA
2359 return;
2360 }
2361
2362 if (pipe_info->state == 0) {
85b85482 2363 dprintk(2, "%s: exiting USB pipe", __func__);
38f993ad
DA
2364 return;
2365 }
2366
b02064ca
DA
2367 if (status == 0)
2368 s2255_read_video_callback(dev, pipe_info);
2369 else {
2370 pipe_info->err_count++;
85b85482 2371 dprintk(1, "%s: failed URB %d\n", __func__, status);
b02064ca 2372 }
38f993ad 2373
38f993ad
DA
2374 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2375 /* reuse urb */
2376 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2377 pipe,
2378 pipe_info->transfer_buffer,
2379 pipe_info->cur_transfer_size,
2380 read_pipe_completion, pipe_info);
2381
2382 if (pipe_info->state != 0) {
a1b4c86b 2383 if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC)) {
38f993ad 2384 dev_err(&dev->udev->dev, "error submitting urb\n");
38f993ad
DA
2385 }
2386 } else {
85b85482 2387 dprintk(2, "%s :complete state 0\n", __func__);
38f993ad
DA
2388 }
2389 return;
2390}
2391
2392static int s2255_start_readpipe(struct s2255_dev *dev)
2393{
2394 int pipe;
2395 int retval;
ab85c6a3 2396 struct s2255_pipeinfo *pipe_info = &dev->pipe;
38f993ad 2397 pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
85b85482 2398 dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint);
ab85c6a3
DA
2399 pipe_info->state = 1;
2400 pipe_info->err_count = 0;
2401 pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2402 if (!pipe_info->stream_urb) {
2403 dev_err(&dev->udev->dev,
2404 "ReadStream: Unable to alloc URB\n");
2405 return -ENOMEM;
38f993ad 2406 }
ab85c6a3
DA
2407 /* transfer buffer allocated in board_init */
2408 usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2409 pipe,
2410 pipe_info->transfer_buffer,
2411 pipe_info->cur_transfer_size,
2412 read_pipe_completion, pipe_info);
ab85c6a3
DA
2413 retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2414 if (retval) {
2415 printk(KERN_ERR "s2255: start read pipe failed\n");
2416 return retval;
2417 }
38f993ad
DA
2418 return 0;
2419}
2420
2421/* starts acquisition process */
fe85ce90 2422static int s2255_start_acquire(struct s2255_channel *channel)
38f993ad
DA
2423{
2424 unsigned char *buffer;
2425 int res;
2426 unsigned long chn_rev;
2427 int j;
fe85ce90
DA
2428 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2429 chn_rev = G_chnmap[channel->idx];
38f993ad
DA
2430 buffer = kzalloc(512, GFP_KERNEL);
2431 if (buffer == NULL) {
2432 dev_err(&dev->udev->dev, "out of mem\n");
2433 return -ENOMEM;
2434 }
2435
fe85ce90
DA
2436 channel->last_frame = -1;
2437 channel->bad_payload = 0;
2438 channel->cur_frame = 0;
38f993ad 2439 for (j = 0; j < SYS_FRAMES; j++) {
fe85ce90
DA
2440 channel->buffer.frame[j].ulState = 0;
2441 channel->buffer.frame[j].cur_size = 0;
38f993ad
DA
2442 }
2443
2444 /* send the start command */
3fa00605
DA
2445 *(__le32 *) buffer = IN_DATA_TOKEN;
2446 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2447 *((__le32 *) buffer + 2) = CMD_START;
38f993ad
DA
2448 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2449 if (res != 0)
2450 dev_err(&dev->udev->dev, "CMD_START error\n");
2451
fe85ce90 2452 dprintk(2, "start acquire exit[%d] %d \n", channel->idx, res);
38f993ad
DA
2453 kfree(buffer);
2454 return 0;
2455}
2456
fe85ce90 2457static int s2255_stop_acquire(struct s2255_channel *channel)
38f993ad
DA
2458{
2459 unsigned char *buffer;
2460 int res;
2461 unsigned long chn_rev;
fe85ce90
DA
2462 struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev);
2463 chn_rev = G_chnmap[channel->idx];
38f993ad
DA
2464 buffer = kzalloc(512, GFP_KERNEL);
2465 if (buffer == NULL) {
2466 dev_err(&dev->udev->dev, "out of mem\n");
2467 return -ENOMEM;
2468 }
38f993ad 2469 /* send the stop command */
3fa00605
DA
2470 *(__le32 *) buffer = IN_DATA_TOKEN;
2471 *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev);
2472 *((__le32 *) buffer + 2) = CMD_STOP;
38f993ad 2473 res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
38f993ad
DA
2474 if (res != 0)
2475 dev_err(&dev->udev->dev, "CMD_STOP error\n");
38f993ad 2476 kfree(buffer);
fe85ce90
DA
2477 channel->b_acquire = 0;
2478 dprintk(4, "%s: chn %d, res %d\n", __func__, channel->idx, res);
14d96260 2479 return res;
38f993ad
DA
2480}
2481
2482static void s2255_stop_readpipe(struct s2255_dev *dev)
2483{
ab85c6a3 2484 struct s2255_pipeinfo *pipe = &dev->pipe;
8b661b50 2485
ab85c6a3
DA
2486 pipe->state = 0;
2487 if (pipe->stream_urb) {
2488 /* cancel urb */
2489 usb_kill_urb(pipe->stream_urb);
2490 usb_free_urb(pipe->stream_urb);
2491 pipe->stream_urb = NULL;
38f993ad 2492 }
ab85c6a3 2493 dprintk(4, "%s", __func__);
38f993ad
DA
2494 return;
2495}
2496
14d96260 2497static void s2255_fwload_start(struct s2255_dev *dev, int reset)
38f993ad 2498{
14d96260
DA
2499 if (reset)
2500 s2255_reset_dsppower(dev);
38f993ad
DA
2501 dev->fw_data->fw_size = dev->fw_data->fw->size;
2502 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2503 memcpy(dev->fw_data->pfw_data,
2504 dev->fw_data->fw->data, CHUNK_SIZE);
2505 dev->fw_data->fw_loaded = CHUNK_SIZE;
2506 usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2507 usb_sndbulkpipe(dev->udev, 2),
2508 dev->fw_data->pfw_data,
2509 CHUNK_SIZE, s2255_fwchunk_complete,
2510 dev->fw_data);
2511 mod_timer(&dev->timer, jiffies + HZ);
2512}
2513
2514/* standard usb probe function */
2515static int s2255_probe(struct usb_interface *interface,
2516 const struct usb_device_id *id)
2517{
2518 struct s2255_dev *dev = NULL;
2519 struct usb_host_interface *iface_desc;
2520 struct usb_endpoint_descriptor *endpoint;
2521 int i;
2522 int retval = -ENOMEM;
14d96260
DA
2523 __le32 *pdata;
2524 int fw_size;
85b85482 2525 dprintk(2, "%s\n", __func__);
38f993ad
DA
2526 /* allocate memory for our device state and initialize it to zero */
2527 dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2528 if (dev == NULL) {
be9ed511 2529 s2255_dev_err(&interface->dev, "out of memory\n");
ff7e22df 2530 return -ENOMEM;
38f993ad 2531 }
fe85ce90 2532 atomic_set(&dev->num_channels, 0);
5a34d9df 2533 dev->pid = id->idProduct;
38f993ad
DA
2534 dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2535 if (!dev->fw_data)
ff7e22df 2536 goto errorFWDATA1;
38f993ad
DA
2537 mutex_init(&dev->lock);
2538 mutex_init(&dev->open_lock);
38f993ad
DA
2539 /* grab usb_device and save it */
2540 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2541 if (dev->udev == NULL) {
2542 dev_err(&interface->dev, "null usb device\n");
2543 retval = -ENODEV;
ff7e22df 2544 goto errorUDEV;
38f993ad 2545 }
d62e85a0 2546 dprintk(1, "dev: %p, udev %p interface %p\n", dev,
38f993ad
DA
2547 dev->udev, interface);
2548 dev->interface = interface;
2549 /* set up the endpoint information */
2550 iface_desc = interface->cur_altsetting;
2551 dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints);
2552 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2553 endpoint = &iface_desc->endpoint[i].desc;
2554 if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2555 /* we found the bulk in endpoint */
2556 dev->read_endpoint = endpoint->bEndpointAddress;
2557 }
2558 }
2559
2560 if (!dev->read_endpoint) {
be9ed511 2561 dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
ff7e22df 2562 goto errorEP;
38f993ad 2563 }
38f993ad
DA
2564 init_timer(&dev->timer);
2565 dev->timer.function = s2255_timer;
2566 dev->timer.data = (unsigned long)dev->fw_data;
38f993ad 2567 init_waitqueue_head(&dev->fw_data->wait_fw);
4de39f5d 2568 for (i = 0; i < MAX_CHANNELS; i++) {
fe85ce90
DA
2569 struct s2255_channel *channel = &dev->channel[i];
2570 dev->channel[i].idx = i;
2571 init_waitqueue_head(&channel->wait_setmode);
2572 init_waitqueue_head(&channel->wait_vidstatus);
4de39f5d 2573 }
38f993ad
DA
2574
2575 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
38f993ad
DA
2576 if (!dev->fw_data->fw_urb) {
2577 dev_err(&interface->dev, "out of memory!\n");
ff7e22df 2578 goto errorFWURB;
38f993ad 2579 }
ff7e22df 2580
38f993ad
DA
2581 dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2582 if (!dev->fw_data->pfw_data) {
2583 dev_err(&interface->dev, "out of memory!\n");
ff7e22df 2584 goto errorFWDATA2;
38f993ad
DA
2585 }
2586 /* load the first chunk */
2587 if (request_firmware(&dev->fw_data->fw,
2588 FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2589 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
ff7e22df 2590 goto errorREQFW;
38f993ad 2591 }
14d96260
DA
2592 /* check the firmware is valid */
2593 fw_size = dev->fw_data->fw->size;
2594 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
38f993ad 2595
14d96260
DA
2596 if (*pdata != S2255_FW_MARKER) {
2597 printk(KERN_INFO "Firmware invalid.\n");
2598 retval = -ENODEV;
ff7e22df 2599 goto errorFWMARKER;
14d96260
DA
2600 } else {
2601 /* make sure firmware is the latest */
2602 __le32 *pRel;
2603 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2604 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
3b2a6306
DC
2605 dev->dsp_fw_ver = le32_to_cpu(*pRel);
2606 if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER)
4de39f5d 2607 printk(KERN_INFO "s2255: f2255usb.bin out of date.\n");
3b2a6306
DC
2608 if (dev->pid == 0x2257 &&
2609 dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
ff7e22df 2610 printk(KERN_WARNING "s2255: 2257 requires firmware %d"
fe85ce90 2611 " or above.\n", S2255_MIN_DSP_COLORFILTER);
14d96260 2612 }
14d96260 2613 usb_reset_device(dev->udev);
38f993ad 2614 /* load 2255 board specific */
abce21f4
DA
2615 retval = s2255_board_init(dev);
2616 if (retval)
ff7e22df 2617 goto errorBOARDINIT;
38f993ad 2618 spin_lock_init(&dev->slock);
14d96260 2619 s2255_fwload_start(dev, 0);
ff7e22df
DA
2620 /* loads v4l specific */
2621 retval = s2255_probe_v4l(dev);
2622 if (retval)
3a67b5cc 2623 goto errorBOARDINIT;
38f993ad
DA
2624 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2625 return 0;
ff7e22df
DA
2626errorBOARDINIT:
2627 s2255_board_shutdown(dev);
2628errorFWMARKER:
2629 release_firmware(dev->fw_data->fw);
2630errorREQFW:
2631 kfree(dev->fw_data->pfw_data);
2632errorFWDATA2:
2633 usb_free_urb(dev->fw_data->fw_urb);
2634errorFWURB:
2635 del_timer(&dev->timer);
2636errorEP:
2637 usb_put_dev(dev->udev);
2638errorUDEV:
2639 kfree(dev->fw_data);
2640 mutex_destroy(&dev->open_lock);
2641 mutex_destroy(&dev->lock);
2642errorFWDATA1:
2643 kfree(dev);
2644 printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval);
38f993ad
DA
2645 return retval;
2646}
2647
2648/* disconnect routine. when board is removed physically or with rmmod */
2649static void s2255_disconnect(struct usb_interface *interface)
2650{
65c6edb3 2651 struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
14d96260 2652 int i;
fe85ce90 2653 int channels = atomic_read(&dev->num_channels);
a19a5cd7 2654 mutex_lock(&dev->lock);
fe85ce90 2655 v4l2_device_disconnect(&dev->v4l2_dev);
a19a5cd7 2656 mutex_unlock(&dev->lock);
d62e85a0 2657 /*see comments in the uvc_driver.c usb disconnect function */
fe85ce90 2658 atomic_inc(&dev->num_channels);
ff7e22df 2659 /* unregister each video device. */
fe85ce90
DA
2660 for (i = 0; i < channels; i++)
2661 video_unregister_device(&dev->channel[i].vdev);
ff7e22df 2662 /* wake up any of our timers */
14d96260
DA
2663 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2664 wake_up(&dev->fw_data->wait_fw);
2665 for (i = 0; i < MAX_CHANNELS; i++) {
fe85ce90
DA
2666 dev->channel[i].setmode_ready = 1;
2667 wake_up(&dev->channel[i].wait_setmode);
2668 dev->channel[i].vidstatus_ready = 1;
2669 wake_up(&dev->channel[i].wait_vidstatus);
14d96260 2670 }
fe85ce90 2671 if (atomic_dec_and_test(&dev->num_channels))
d62e85a0 2672 s2255_destroy(dev);
ff7e22df 2673 dev_info(&interface->dev, "%s\n", __func__);
38f993ad
DA
2674}
2675
2676static struct usb_driver s2255_driver = {
be9ed511 2677 .name = S2255_DRIVER_NAME,
38f993ad
DA
2678 .probe = s2255_probe,
2679 .disconnect = s2255_disconnect,
2680 .id_table = s2255_table,
2681};
2682
ecb3b2b3 2683module_usb_driver(s2255_driver);
38f993ad
DA
2684
2685MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2686MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2687MODULE_LICENSE("GPL");
64dc3c1a 2688MODULE_VERSION(S2255_VERSION);
This page took 0.51534 seconds and 5 git commands to generate.