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