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