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