blob: f3fc44b352b4532b672d1ad42f11449968b7acb1 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002 em2820-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080030#include <linux/i2c.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080031#include <linux/video_decoder.h>
32
33#include "em2820.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080034#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035
36#define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \
37 "Ludovico Cavedon <cavedon@sssup.it>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>"
39
40#define DRIVER_NAME "em2820"
41#define DRIVER_DESC "Empia em2820 based USB video device driver"
42#define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1)
43
44#define em2820_videodbg(fmt, arg...) do {\
45 if (video_debug) \
46 printk(KERN_INFO "%s %s :"fmt, \
47 dev->name, __FUNCTION__ , ##arg); } while (0)
48
49MODULE_AUTHOR(DRIVER_AUTHOR);
50MODULE_DESCRIPTION(DRIVER_DESC);
51MODULE_LICENSE("GPL");
52
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080053static unsigned int card[] = {[0 ... (EM2820_MAXBOARDS - 1)] = UNSET };
54
55module_param_array(card, int, NULL, 0444);
56MODULE_PARM_DESC(card,"card type");
57
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058static int tuner = -1;
59module_param(tuner, int, 0444);
60MODULE_PARM_DESC(tuner, "tuner type");
61
62static unsigned int video_debug = 0;
63module_param(video_debug,int,0644);
64MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
65
66/* supported tv norms */
67static struct em2820_tvnorm tvnorms[] = {
68 {
69 .name = "PAL",
70 .id = V4L2_STD_PAL,
71 .mode = VIDEO_MODE_PAL,
72 }, {
73 .name = "NTSC",
74 .id = V4L2_STD_NTSC,
75 .mode = VIDEO_MODE_NTSC,
76 }, {
77 .name = "SECAM",
78 .id = V4L2_STD_SECAM,
79 .mode = VIDEO_MODE_SECAM,
80 }, {
81 .name = "PAL-M",
82 .id = V4L2_STD_PAL_M,
83 .mode = VIDEO_MODE_PAL,
84 }
85};
86
87#define TVNORMS ARRAY_SIZE(tvnorms)
88
89/* supported controls */
90static struct v4l2_queryctrl em2820_qctrl[] = {
91 {
92 .id = V4L2_CID_BRIGHTNESS,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Brightness",
95 .minimum = -128,
96 .maximum = 127,
97 .step = 1,
98 .default_value = 0,
99 .flags = 0,
100 },{
101 .id = V4L2_CID_CONTRAST,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Contrast",
104 .minimum = 0x0,
105 .maximum = 0x1f,
106 .step = 0x1,
107 .default_value = 0x10,
108 .flags = 0,
109 },{
110 .id = V4L2_CID_SATURATION,
111 .type = V4L2_CTRL_TYPE_INTEGER,
112 .name = "Saturation",
113 .minimum = 0x0,
114 .maximum = 0x1f,
115 .step = 0x1,
116 .default_value = 0x10,
117 .flags = 0,
118 },{
119 .id = V4L2_CID_AUDIO_VOLUME,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .name = "Volume",
122 .minimum = 0x0,
123 .maximum = 0x1f,
124 .step = 0x1,
125 .default_value = 0x1f,
126 .flags = 0,
127 },{
128 .id = V4L2_CID_AUDIO_MUTE,
129 .type = V4L2_CTRL_TYPE_BOOLEAN,
130 .name = "Mute",
131 .minimum = 0,
132 .maximum = 1,
133 .step = 1,
134 .default_value = 1,
135 .flags = 0,
136 },{
137 .id = V4L2_CID_RED_BALANCE,
138 .type = V4L2_CTRL_TYPE_INTEGER,
139 .name = "Red chroma balance",
140 .minimum = -128,
141 .maximum = 127,
142 .step = 1,
143 .default_value = 0,
144 .flags = 0,
145 },{
146 .id = V4L2_CID_BLUE_BALANCE,
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 .name = "Blue chroma balance",
149 .minimum = -128,
150 .maximum = 127,
151 .step = 1,
152 .default_value = 0,
153 .flags = 0,
154 },{
155 .id = V4L2_CID_GAMMA,
156 .type = V4L2_CTRL_TYPE_INTEGER,
157 .name = "Gamma",
158 .minimum = 0x0,
159 .maximum = 0x3f,
160 .step = 0x1,
161 .default_value = 0x20,
162 .flags = 0,
163 }
164};
165
166static struct usb_driver em2820_usb_driver;
167
168static DECLARE_MUTEX(em2820_sysfs_lock);
169static DECLARE_RWSEM(em2820_disconnect);
170
171/********************* v4l2 interface ******************************************/
172
173static inline unsigned long kvirt_to_pa(unsigned long adr)
174{
175 unsigned long kva, ret;
176
177 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
178 kva |= adr & (PAGE_SIZE - 1);
179 ret = __pa(kva);
180 return ret;
181}
182
183/*
184 * em2820_config()
185 * inits registers with sane defaults
186 */
187static int em2820_config(struct em2820 *dev)
188{
189
190 /* Sets I2C speed to 100 KHz */
191 em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
192
193 /* enable vbi capturing */
194 em2820_audio_usb_mute(dev, 1);
195 dev->mute = 1; /* maybe not the right place... */
196 dev->volume = 0x1f;
197 em2820_audio_analog_set(dev);
198 em2820_audio_analog_setup(dev);
199 em2820_outfmt_set_yuv422(dev);
200 em2820_colorlevels_set_default(dev);
201 em2820_compression_disable(dev);
202
203 return 0;
204}
205
206/*
207 * em2820_config_i2c()
208 * configure i2c attached devices
209 */
210void em2820_config_i2c(struct em2820 *dev)
211{
212 struct v4l2_frequency f;
213 struct video_decoder_init em2820_vdi = {.data = NULL };
214
215
216 /* configure decoder */
217 em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
218 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
219/* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
220/* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
221/* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
222/* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
223
224 /* configure tuner */
225 f.tuner = 0;
226 f.type = V4L2_TUNER_ANALOG_TV;
227 f.frequency = 9076; /* FIXME:remove magic number */
228 dev->ctl_freq = f.frequency;
229 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
230
231 /* configure tda9887 */
232
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800233
234/* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
235}
236
237/*
238 * em2820_empty_framequeues()
239 * prepare queues for incoming and outgoing frames
240 */
241static void em2820_empty_framequeues(struct em2820 *dev)
242{
243 u32 i;
244
245 INIT_LIST_HEAD(&dev->inqueue);
246 INIT_LIST_HEAD(&dev->outqueue);
247
248 for (i = 0; i < EM2820_NUM_FRAMES; i++) {
249 dev->frame[i].state = F_UNUSED;
250 dev->frame[i].buf.bytesused = 0;
251 }
252}
253
254/*
255 * em2820_v4l2_open()
256 * inits the device and starts isoc transfer
257 */
258static int em2820_v4l2_open(struct inode *inode, struct file *filp)
259{
260 struct video_device *vdev = video_devdata(filp);
261 int minor = iminor(inode);
262 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
263 int errCode = 0;
264
265 em2820_videodbg("users=%d", dev->users);
266
267 if (!down_read_trylock(&em2820_disconnect))
268 return -ERESTARTSYS;
269
270 if (dev->users) {
271 em2820_warn("this driver can be opened only once\n");
272 up_read(&em2820_disconnect);
273 return -EBUSY;
274 }
275
276/* if(dev->vbi_dev->minor == minor){
277 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
278 }*/
279 if (dev->vdev->minor == minor) {
280 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
281 }
282
283 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
284 spin_lock_init(&dev->queue_lock);
285 init_waitqueue_head(&dev->wait_frame);
286 init_waitqueue_head(&dev->wait_stream);
287
288 down(&dev->lock);
289
290 em2820_set_alternate(dev);
291
292 dev->width = norm_maxw(dev);
293 dev->height = norm_maxh(dev);
294 dev->frame_size = dev->width * dev->height * 2;
295 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
296 dev->bytesperline = dev->width * 2;
297 dev->hscale = 0;
298 dev->vscale = 0;
299
300 em2820_capture_start(dev, 1);
301 em2820_resolution_set(dev);
302
303 /* start the transfer */
304 errCode = em2820_init_isoc(dev);
305 if (errCode)
306 goto err;
307
308 dev->users++;
309 filp->private_data = dev;
310 dev->io = IO_NONE;
311 dev->stream = STREAM_OFF;
312 dev->num_frames = 0;
313
314 /* prepare queues */
315 em2820_empty_framequeues(dev);
316
317 dev->state |= DEV_INITIALIZED;
318
319 err:
320 up(&dev->lock);
321 up_read(&em2820_disconnect);
322 return errCode;
323}
324
325/*
326 * em2820_realease_resources()
327 * unregisters the v4l2,i2c and usb devices
328 * called when the device gets disconected or at module unload
329*/
330static void em2820_release_resources(struct em2820 *dev)
331{
332 down(&em2820_sysfs_lock);
333
334 em2820_info("V4L2 device /dev/video%d deregistered\n",
335 dev->vdev->minor);
336 video_set_drvdata(dev->vdev, NULL);
337 video_unregister_device(dev->vdev);
338/* video_unregister_device(dev->vbi_dev); */
339 em2820_i2c_unregister(dev);
340 usb_put_dev(dev->udev);
341 up(&em2820_sysfs_lock);
342}
343
344/*
345 * em2820_v4l2_close()
346 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
347 */
348static int em2820_v4l2_close(struct inode *inode, struct file *file)
349{
350 struct video_device *vdev = video_devdata(file);
351 struct em2820 *dev = (struct em2820 *)video_get_drvdata(vdev);
352 int errCode;
353
354 em2820_videodbg("users=%d", dev->users);
355
356 down(&dev->lock);
357
358 em2820_uninit_isoc(dev);
359
360 em2820_release_buffers(dev);
361
362 /* the device is already disconnect, free the remaining resources */
363 if (dev->state & DEV_DISCONNECTED) {
364 em2820_release_resources(dev);
365 up(&dev->lock);
366 kfree(dev);
367 return 0;
368 }
369
370 /* set alternate 0 */
371 dev->alt = 0;
372 em2820_videodbg("setting alternate 0");
373 errCode = usb_set_interface(dev->udev, 0, 0);
374 if (errCode < 0) {
375 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
376 errCode);
377 }
378
379 dev->users--;
380 wake_up_interruptible_nr(&dev->open, 1);
381 up(&dev->lock);
382 return 0;
383}
384
385/*
386 * em2820_v4l2_read()
387 * will allocate buffers when called for the first time
388 */
389static ssize_t
390em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
391 loff_t * f_pos)
392{
393 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
394 struct em2820_frame_t *f, *i;
395 unsigned long lock_flags;
396 int ret = 0;
397
398 if (down_interruptible(&dev->fileop_lock))
399 return -ERESTARTSYS;
400
401 if (dev->state & DEV_DISCONNECTED) {
402 em2820_videodbg("device not present");
403 up(&dev->fileop_lock);
404 return -ENODEV;
405 }
406
407 if (dev->state & DEV_MISCONFIGURED) {
408 em2820_videodbg("device misconfigured; close and open it again");
409 up(&dev->fileop_lock);
410 return -EIO;
411 }
412
413 if (dev->io == IO_MMAP) {
414 em2820_videodbg ("IO method is set to mmap; close and open"
415 " the device again to choose the read method");
416 up(&dev->fileop_lock);
417 return -EINVAL;
418 }
419
420 if (dev->io == IO_NONE) {
421 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
422 em2820_errdev("read failed, not enough memory\n");
423 up(&dev->fileop_lock);
424 return -ENOMEM;
425 }
426 dev->io = IO_READ;
427 dev->stream = STREAM_ON;
428 em2820_queue_unusedframes(dev);
429 }
430
431 if (!count) {
432 up(&dev->fileop_lock);
433 return 0;
434 }
435
436 if (list_empty(&dev->outqueue)) {
437 if (filp->f_flags & O_NONBLOCK) {
438 up(&dev->fileop_lock);
439 return -EAGAIN;
440 }
441 ret = wait_event_interruptible
442 (dev->wait_frame,
443 (!list_empty(&dev->outqueue)) ||
444 (dev->state & DEV_DISCONNECTED));
445 if (ret) {
446 up(&dev->fileop_lock);
447 return ret;
448 }
449 if (dev->state & DEV_DISCONNECTED) {
450 up(&dev->fileop_lock);
451 return -ENODEV;
452 }
453 }
454
455 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
456
457 spin_lock_irqsave(&dev->queue_lock, lock_flags);
458 list_for_each_entry(i, &dev->outqueue, frame)
459 i->state = F_UNUSED;
460 INIT_LIST_HEAD(&dev->outqueue);
461 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
462
463 em2820_queue_unusedframes(dev);
464
465 if (count > f->buf.length)
466 count = f->buf.length;
467
468 if (copy_to_user(buf, f->bufmem, count)) {
469 up(&dev->fileop_lock);
470 return -EFAULT;
471 }
472 *f_pos += count;
473
474 up(&dev->fileop_lock);
475
476 return count;
477}
478
479/*
480 * em2820_v4l2_poll()
481 * will allocate buffers when called for the first time
482 */
483static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
484{
485 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
486 unsigned int mask = 0;
487
488 if (down_interruptible(&dev->fileop_lock))
489 return POLLERR;
490
491 if (dev->state & DEV_DISCONNECTED) {
492 em2820_videodbg("device not present");
493 } else if (dev->state & DEV_MISCONFIGURED) {
494 em2820_videodbg("device is misconfigured; close and open it again");
495 } else {
496 if (dev->io == IO_NONE) {
497 if (!em2820_request_buffers
498 (dev, EM2820_NUM_READ_FRAMES)) {
499 em2820_warn
500 ("poll() failed, not enough memory\n");
501 } else {
502 dev->io = IO_READ;
503 dev->stream = STREAM_ON;
504 }
505 }
506
507 if (dev->io == IO_READ) {
508 em2820_queue_unusedframes(dev);
509 poll_wait(filp, &dev->wait_frame, wait);
510
511 if (!list_empty(&dev->outqueue))
512 mask |= POLLIN | POLLRDNORM;
513
514 up(&dev->fileop_lock);
515
516 return mask;
517 }
518 }
519
520 up(&dev->fileop_lock);
521 return POLLERR;
522}
523
524/*
525 * em2820_vm_open()
526 */
527static void em2820_vm_open(struct vm_area_struct *vma)
528{
529 struct em2820_frame_t *f = vma->vm_private_data;
530 f->vma_use_count++;
531}
532
533/*
534 * em2820_vm_close()
535 */
536static void em2820_vm_close(struct vm_area_struct *vma)
537{
538 /* NOTE: buffers are not freed here */
539 struct em2820_frame_t *f = vma->vm_private_data;
540 f->vma_use_count--;
541}
542
543static struct vm_operations_struct em2820_vm_ops = {
544 .open = em2820_vm_open,
545 .close = em2820_vm_close,
546};
547
548/*
549 * em2820_v4l2_mmap()
550 */
551static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
552{
553 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
554 unsigned long size = vma->vm_end - vma->vm_start,
555 start = vma->vm_start, pos, page;
556 u32 i;
557 if (down_interruptible(&dev->fileop_lock))
558 return -ERESTARTSYS;
559
560 if (dev->state & DEV_DISCONNECTED) {
561 em2820_videodbg("mmap: device not present");
562 up(&dev->fileop_lock);
563 return -ENODEV;
564 }
565
566 if (dev->state & DEV_MISCONFIGURED) {
567 em2820_videodbg ("mmap: Device is misconfigured; close and "
568 "open it again");
569 up(&dev->fileop_lock);
570 return -EIO;
571 }
572
573 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
574 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
575 up(&dev->fileop_lock);
576 return -EINVAL;
577 }
578
579 for (i = 0; i < dev->num_frames; i++) {
580 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
581 break;
582 }
583 if (i == dev->num_frames) {
584 em2820_videodbg("mmap: user supplied mapping address is out of range");
585 up(&dev->fileop_lock);
586 return -EINVAL;
587 }
588
589 /* VM_IO is eventually going to replace PageReserved altogether */
590 vma->vm_flags |= VM_IO;
591 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
592
593 pos = (unsigned long)dev->frame[i].bufmem;
594 while (size > 0) { /* size is page-aligned */
595 page = vmalloc_to_pfn((void *)pos);
596 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
597 vma->vm_page_prot)) {
598 em2820_videodbg("mmap: rename page map failed");
599 up(&dev->fileop_lock);
600 return -EAGAIN;
601 }
602 start += PAGE_SIZE;
603 pos += PAGE_SIZE;
604 size -= PAGE_SIZE;
605 }
606
607 vma->vm_ops = &em2820_vm_ops;
608 vma->vm_private_data = &dev->frame[i];
609
610 em2820_vm_open(vma);
611 up(&dev->fileop_lock);
612 return 0;
613}
614
615/*
616 * em2820_get_ctrl()
617 * return the current saturation, brightness or contrast, mute state
618 */
619static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
620{
621 s32 tmp;
622 switch (ctrl->id) {
623 case V4L2_CID_AUDIO_MUTE:
624 ctrl->value = dev->mute;
625 return 0;
626 case V4L2_CID_AUDIO_VOLUME:
627 ctrl->value = dev->volume;
628 return 0;
629 case V4L2_CID_BRIGHTNESS:
630 if ((tmp = em2820_brightness_get(dev)) < 0)
631 return -EIO;
632 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
633 return 0;
634 case V4L2_CID_CONTRAST:
635 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
636 return -EIO;
637 return 0;
638 case V4L2_CID_SATURATION:
639 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
640 return -EIO;
641 return 0;
642 case V4L2_CID_RED_BALANCE:
643 if ((tmp = em2820_v_balance_get(dev)) < 0)
644 return -EIO;
645 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
646 return 0;
647 case V4L2_CID_BLUE_BALANCE:
648 if ((tmp = em2820_u_balance_get(dev)) < 0)
649 return -EIO;
650 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
651 return 0;
652 case V4L2_CID_GAMMA:
653 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
654 return -EIO;
655 return 0;
656 default:
657 return -EINVAL;
658 }
659}
660
661/*
662 * em2820_set_ctrl()
663 * mute or set new saturation, brightness or contrast
664 */
665static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
666{
667 switch (ctrl->id) {
668 case V4L2_CID_AUDIO_MUTE:
669 if (ctrl->value != dev->mute) {
670 dev->mute = ctrl->value;
671 em2820_audio_usb_mute(dev, ctrl->value);
672 return em2820_audio_analog_set(dev);
673 }
674 return 0;
675 case V4L2_CID_AUDIO_VOLUME:
676 dev->volume = ctrl->value;
677 return em2820_audio_analog_set(dev);
678 case V4L2_CID_BRIGHTNESS:
679 return em2820_brightness_set(dev, ctrl->value);
680 case V4L2_CID_CONTRAST:
681 return em2820_contrast_set(dev, ctrl->value);
682 case V4L2_CID_SATURATION:
683 return em2820_saturation_set(dev, ctrl->value);
684 case V4L2_CID_RED_BALANCE:
685 return em2820_v_balance_set(dev, ctrl->value);
686 case V4L2_CID_BLUE_BALANCE:
687 return em2820_u_balance_set(dev, ctrl->value);
688 case V4L2_CID_GAMMA:
689 return em2820_gamma_set(dev, ctrl->value);
690 default:
691 return -EINVAL;
692 }
693}
694
695/*
696 * em2820_stream_interrupt()
697 * stops streaming
698 */
699static int em2820_stream_interrupt(struct em2820 *dev)
700{
701 int ret = 0;
702
703 /* stop reading from the device */
704
705 dev->stream = STREAM_INTERRUPT;
706 ret = wait_event_timeout(dev->wait_stream,
707 (dev->stream == STREAM_OFF) ||
708 (dev->state & DEV_DISCONNECTED),
709 EM2820_URB_TIMEOUT);
710 if (dev->state & DEV_DISCONNECTED)
711 return -ENODEV;
712 else if (ret) {
713 dev->state |= DEV_MISCONFIGURED;
714 em2820_videodbg("device is misconfigured; close and "
715 "open /dev/video%d again", dev->vdev->minor);
716 return ret;
717 }
718
719 return 0;
720}
721
722static int em2820_set_norm(struct em2820 *dev, int width, int height)
723{
724 unsigned int hscale, vscale;
725 unsigned int maxh, maxw;
726
727 maxw = norm_maxw(dev);
728 maxh = norm_maxh(dev);
729
730 /* width must even because of the YUYV format */
731 /* height must be even because of interlacing */
732 height &= 0xfffe;
733 width &= 0xfffe;
734
735 if (height < 32)
736 height = 32;
737 if (height > maxh)
738 height = maxh;
739 if (width < 48)
740 width = 48;
741 if (width > maxw)
742 width = maxw;
743
744 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
745 hscale = 0x3fff;
746 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
747
748 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
749 vscale = 0x3fff;
750 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
751
752 /* set new image size */
753 dev->width = width;
754 dev->height = height;
755 dev->frame_size = dev->width * dev->height * 2;
756 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
757 dev->bytesperline = dev->width * 2;
758 dev->hscale = hscale;
759 dev->vscale = vscale;
760
761 em2820_resolution_set(dev);
762
763 return 0;
764}
765
766static void video_mux(struct em2820 *dev, int index)
767{
768 int input, ainput;
769
770 input = INPUT(index)->vmux;
771 dev->ctl_input = index;
772
773 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
774
775 dev->ctl_ainput = INPUT(index)->amux;
776
777 switch (dev->ctl_ainput) {
778 case 0:
779 ainput = EM2820_AUDIO_SRC_TUNER;
780 break;
781 default:
782 ainput = EM2820_AUDIO_SRC_LINE;
783 }
784
785 em2820_audio_source(dev, ainput);
786}
787
788/*
789 * em2820_v4l2_do_ioctl()
790 * This function is _not_ called directly, but from
791 * em2820_v4l2_ioctl. Userspace
792 * copying is done already, arg is a kernel pointer.
793 */
794static int em2820_do_ioctl(struct inode *inode, struct file *filp,
795 struct em2820 *dev, unsigned int cmd, void *arg,
796 v4l2_kioctl driver_ioctl)
797{
798 int ret;
799
800 switch (cmd) {
801 /* ---------- tv norms ---------- */
802 case VIDIOC_ENUMSTD:
803 {
804 struct v4l2_standard *e = arg;
805 unsigned int i;
806
807 i = e->index;
808 if (i >= TVNORMS)
809 return -EINVAL;
810 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
811 tvnorms[e->index].name);
812 e->index = i;
813 if (ret < 0)
814 return ret;
815 return 0;
816 }
817 case VIDIOC_G_STD:
818 {
819 v4l2_std_id *id = arg;
820
821 *id = dev->tvnorm->id;
822 return 0;
823 }
824 case VIDIOC_S_STD:
825 {
826 v4l2_std_id *id = arg;
827 unsigned int i;
828
829 for (i = 0; i < TVNORMS; i++)
830 if (*id == tvnorms[i].id)
831 break;
832 if (i == TVNORMS)
833 for (i = 0; i < TVNORMS; i++)
834 if (*id & tvnorms[i].id)
835 break;
836 if (i == TVNORMS)
837 return -EINVAL;
838
839 down(&dev->lock);
840 dev->tvnorm = &tvnorms[i];
841
842 em2820_set_norm(dev, dev->width, dev->height);
843
844/*
845 dev->width=norm_maxw(dev);
846 dev->height=norm_maxh(dev);
847 dev->frame_size=dev->width*dev->height*2;
848 dev->field_size=dev->frame_size>>1;
849 dev->bytesperline=dev->width*2;
850 dev->hscale=0;
851 dev->vscale=0;
852
853 em2820_resolution_set(dev);
854*/
855/*
856 em2820_uninit_isoc(dev);
857 em2820_set_alternate(dev);
858 em2820_capture_start(dev, 1);
859 em2820_resolution_set(dev);
860 em2820_init_isoc(dev);
861*/
862 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
863 &tvnorms[i].mode);
864 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
865 &dev->tvnorm->id);
866
867 up(&dev->lock);
868
869 return 0;
870 }
871
872 /* ------ input switching ---------- */
873 case VIDIOC_ENUMINPUT:
874 {
875 struct v4l2_input *i = arg;
876 unsigned int n;
877 static const char *iname[] = {
878 [EM2820_VMUX_COMPOSITE1] = "Composite1",
879 [EM2820_VMUX_COMPOSITE2] = "Composite2",
880 [EM2820_VMUX_COMPOSITE3] = "Composite3",
881 [EM2820_VMUX_COMPOSITE4] = "Composite4",
882 [EM2820_VMUX_SVIDEO] = "S-Video",
883 [EM2820_VMUX_TELEVISION] = "Television",
884 [EM2820_VMUX_CABLE] = "Cable TV",
885 [EM2820_VMUX_DVB] = "DVB",
886 [EM2820_VMUX_DEBUG] = "for debug only",
887 };
888
889 n = i->index;
890 if (n >= MAX_EM2820_INPUT)
891 return -EINVAL;
892 if (0 == INPUT(n)->type)
893 return -EINVAL;
894 memset(i, 0, sizeof(*i));
895 i->index = n;
896 i->type = V4L2_INPUT_TYPE_CAMERA;
897 strcpy(i->name, iname[INPUT(n)->type]);
898 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
899 (EM2820_VMUX_CABLE == INPUT(n)->type))
900 i->type = V4L2_INPUT_TYPE_TUNER;
901 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
902 i->std |= tvnorms[n].id;
903 return 0;
904 }
905
906 case VIDIOC_G_INPUT:
907 {
908 int *i = arg;
909 *i = dev->ctl_input;
910
911 return 0;
912 }
913
914 case VIDIOC_S_INPUT:
915 {
916 int *index = arg;
917
918 if (*index >= MAX_EM2820_INPUT)
919 return -EINVAL;
920 if (0 == INPUT(*index)->type)
921 return -EINVAL;
922
923 down(&dev->lock);
924 video_mux(dev, *index);
925 up(&dev->lock);
926
927 return 0;
928 }
929
930 case VIDIOC_G_AUDIO:
931 {
932 struct v4l2_audio *a = arg;
933 unsigned int index = a->index;
934
935 if (a->index > 1)
936 return -EINVAL;
937 memset(a, 0, sizeof(*a));
938 index = dev->ctl_ainput;
939
940 if (index == 0) {
941 strcpy(a->name, "Television");
942 } else {
943 strcpy(a->name, "Line In");
944 }
945 a->capability = V4L2_AUDCAP_STEREO;
946 a->index = index;
947 return 0;
948 }
949
950 case VIDIOC_S_AUDIO:
951 {
952 struct v4l2_audio *a = arg;
953 if (a->index != dev->ctl_ainput)
954 return -EINVAL;
955
956 return 0;
957 }
958
959 /* --- controls ---------------------------------------------- */
960 case VIDIOC_QUERYCTRL:
961 {
962 struct v4l2_queryctrl *qc = arg;
963 u8 i, n;
964 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
965 for (i = 0; i < n; i++)
966 if (qc->id && qc->id == em2820_qctrl[i].id) {
967 memcpy(qc, &(em2820_qctrl[i]),
968 sizeof(*qc));
969 return 0;
970 }
971
972 return -EINVAL;
973 }
974
975 case VIDIOC_G_CTRL:
976 {
977 struct v4l2_control *ctrl = arg;
978
979
980 return em2820_get_ctrl(dev, ctrl);
981 }
982
983 case VIDIOC_S_CTRL_OLD: /* ??? */
984 case VIDIOC_S_CTRL:
985 {
986 struct v4l2_control *ctrl = arg;
987 u8 i, n;
988
989
990 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
991 for (i = 0; i < n; i++)
992 if (ctrl->id == em2820_qctrl[i].id) {
993 if (ctrl->value <
994 em2820_qctrl[i].minimum
995 || ctrl->value >
996 em2820_qctrl[i].maximum)
997 return -ERANGE;
998
999 return em2820_set_ctrl(dev, ctrl);
1000 }
1001 return -EINVAL;
1002 }
1003
1004 /* --- tuner ioctls ------------------------------------------ */
1005 case VIDIOC_G_TUNER:
1006 {
1007 struct v4l2_tuner *t = arg;
1008 int status = 0;
1009
1010 if (0 != t->index)
1011 return -EINVAL;
1012
1013 memset(t, 0, sizeof(*t));
1014 strcpy(t->name, "Tuner");
1015 t->type = V4L2_TUNER_ANALOG_TV;
1016 t->capability = V4L2_TUNER_CAP_NORM;
1017 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1018/* t->signal = 0xffff;*/
1019/* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1020 /* No way to get signal strength? */
1021 down(&dev->lock);
1022 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1023 &status);
1024 up(&dev->lock);
1025 t->signal =
1026 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1027
1028 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1029 t->afc);
1030 return 0;
1031 }
1032 case VIDIOC_S_TUNER:
1033 {
1034 struct v4l2_tuner *t = arg;
1035 int status = 0;
1036
1037 if (0 != t->index)
1038 return -EINVAL;
1039 memset(t, 0, sizeof(*t));
1040 strcpy(t->name, "Tuner");
1041 t->type = V4L2_TUNER_ANALOG_TV;
1042 t->capability = V4L2_TUNER_CAP_NORM;
1043 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1044/* t->signal = 0xffff; */
1045 /* No way to get signal strength? */
1046 down(&dev->lock);
1047 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1048 &status);
1049 up(&dev->lock);
1050 t->signal =
1051 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1052
1053 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1054 t->signal, t->afc);
1055 return 0;
1056 }
1057 case VIDIOC_G_FREQUENCY:
1058 {
1059 struct v4l2_frequency *f = arg;
1060
1061 memset(f, 0, sizeof(*f));
1062 f->type = V4L2_TUNER_ANALOG_TV;
1063 f->frequency = dev->ctl_freq;
1064
1065 return 0;
1066 }
1067 case VIDIOC_S_FREQUENCY:
1068 {
1069 struct v4l2_frequency *f = arg;
1070
1071 if (0 != f->tuner)
1072 return -EINVAL;
1073
1074 if (V4L2_TUNER_ANALOG_TV != f->type)
1075 return -EINVAL;
1076
1077 down(&dev->lock);
1078 dev->ctl_freq = f->frequency;
1079 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1080 up(&dev->lock);
1081 return 0;
1082 }
1083
1084 case VIDIOC_CROPCAP:
1085 {
1086 struct v4l2_cropcap *cc = arg;
1087
1088 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001089 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001090 cc->bounds.left = 0;
1091 cc->bounds.top = 0;
1092 cc->bounds.width = dev->width;
1093 cc->bounds.height = dev->height;
1094 cc->defrect = cc->bounds;
1095 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1096 cc->pixelaspect.denominator = 59;
1097 return 0;
1098 }
1099 case VIDIOC_STREAMON:
1100 {
1101 int *type = arg;
1102
1103 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1104 || dev->io != IO_MMAP)
1105 return -EINVAL;
1106
1107 if (list_empty(&dev->inqueue))
1108 return -EINVAL;
1109
1110 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1111
1112 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1113
1114 return 0;
1115 }
1116 case VIDIOC_STREAMOFF:
1117 {
1118 int *type = arg;
1119 int ret;
1120
1121 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1122 || dev->io != IO_MMAP)
1123 return -EINVAL;
1124
1125 if (dev->stream == STREAM_ON) {
1126 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1127 if ((ret = em2820_stream_interrupt(dev)))
1128 return ret;
1129 }
1130 em2820_empty_framequeues(dev);
1131
1132 return 0;
1133 }
1134 default:
1135 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1136 driver_ioctl);
1137 }
1138 return 0;
1139}
1140
1141/*
1142 * em2820_v4l2_do_ioctl()
1143 * This function is _not_ called directly, but from
1144 * em2820_v4l2_ioctl. Userspace
1145 * copying is done already, arg is a kernel pointer.
1146 */
1147static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1148 unsigned int cmd, void *arg)
1149{
1150 struct em2820 *dev = filp->private_data;
1151
1152 if (!dev)
1153 return -ENODEV;
1154
1155 if (video_debug > 1)
1156 em2820_print_ioctl(dev->name,cmd);
1157
1158 switch (cmd) {
1159
1160 /* --- capabilities ------------------------------------------ */
1161 case VIDIOC_QUERYCAP:
1162 {
1163 struct v4l2_capability *cap = arg;
1164
1165 memset(cap, 0, sizeof(*cap));
1166 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1167 strlcpy(cap->card, em2820_boards[dev->model].name,
1168 sizeof(cap->card));
1169 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1170 sizeof(cap->bus_info));
1171 cap->version = EM2820_VERSION_CODE;
1172 cap->capabilities =
1173 V4L2_CAP_VIDEO_CAPTURE |
1174 V4L2_CAP_AUDIO |
1175 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1176 if (dev->has_tuner)
1177 cap->capabilities |= V4L2_CAP_TUNER;
1178 return 0;
1179 }
1180
1181 /* --- capture ioctls ---------------------------------------- */
1182 case VIDIOC_ENUM_FMT:
1183 {
1184 struct v4l2_fmtdesc *fmtd = arg;
1185
1186 if (fmtd->index != 0)
1187 return -EINVAL;
1188 memset(fmtd, 0, sizeof(*fmtd));
1189 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1190 strcpy(fmtd->description, "Packed YUY2");
1191 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1192 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1193 return 0;
1194 }
1195
1196 case VIDIOC_G_FMT:
1197 {
1198 struct v4l2_format *format = arg;
1199
1200 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1201 format->type ==
1202 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1203 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1204 V4L2_BUF_TYPE_VBI_CAPTURE ?
1205 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1206 "not supported");
1207
1208 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1209 return -EINVAL;
1210
1211 format->fmt.pix.width = dev->width;
1212 format->fmt.pix.height = dev->height;
1213 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1214 format->fmt.pix.bytesperline = dev->bytesperline;
1215 format->fmt.pix.sizeimage = dev->frame_size;
1216 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1217 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1218
1219 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1220 dev->height);
1221 return 0;
1222 }
1223
1224 case VIDIOC_TRY_FMT:
1225 case VIDIOC_S_FMT:
1226 {
1227 struct v4l2_format *format = arg;
1228 u32 i;
1229 int ret = 0;
1230 int width = format->fmt.pix.width;
1231 int height = format->fmt.pix.height;
1232 unsigned int hscale, vscale;
1233 unsigned int maxh, maxw;
1234
1235 maxw = norm_maxw(dev);
1236 maxh = norm_maxh(dev);
1237
1238/* int both_fields; */
1239
1240 em2820_videodbg("%s: type=%s",
1241 cmd ==
1242 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1243 "VIDIOC_S_FMT",
1244 format->type ==
1245 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1246 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1247 V4L2_BUF_TYPE_VBI_CAPTURE ?
1248 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1249 "not supported");
1250
1251 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1252 return -EINVAL;
1253
1254 em2820_videodbg("%s: requested %dx%d",
1255 cmd ==
1256 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1257 "VIDIOC_S_FMT", format->fmt.pix.width,
1258 format->fmt.pix.height);
1259
1260 /* FIXME: Move some code away from here */
1261 /* width must even because of the YUYV format */
1262 /* height must be even because of interlacing */
1263 height &= 0xfffe;
1264 width &= 0xfffe;
1265
1266 if (height < 32)
1267 height = 32;
1268 if (height > maxh)
1269 height = maxh;
1270 if (width < 48)
1271 width = 48;
1272 if (width > maxw)
1273 width = maxw;
1274
1275 if ((hscale =
1276 (((unsigned long)maxw) << 12) / width - 4096L) >=
1277 0x4000)
1278 hscale = 0x3fff;
1279 width =
1280 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1281
1282 if ((vscale =
1283 (((unsigned long)maxh) << 12) / height - 4096L) >=
1284 0x4000)
1285 vscale = 0x3fff;
1286 height =
1287 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1288
1289 format->fmt.pix.width = width;
1290 format->fmt.pix.height = height;
1291 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1292 format->fmt.pix.bytesperline = width * 2;
1293 format->fmt.pix.sizeimage = width * 2 * height;
1294 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1295 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1296
1297 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1298 cmd ==
1299 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1300 "VIDIOC_S_FMT", format->fmt.pix.width,
1301 format->fmt.pix.height, hscale, vscale);
1302
1303 if (cmd == VIDIOC_TRY_FMT)
1304 return 0;
1305
1306 for (i = 0; i < dev->num_frames; i++)
1307 if (dev->frame[i].vma_use_count) {
1308 em2820_videodbg("VIDIOC_S_FMT failed. "
1309 "Unmap the buffers first.");
1310 return -EINVAL;
1311 }
1312
1313 /* stop io in case it is already in progress */
1314 if (dev->stream == STREAM_ON) {
1315 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1316 if ((ret = em2820_stream_interrupt(dev)))
1317 return ret;
1318 }
1319
1320 em2820_release_buffers(dev);
1321 dev->io = IO_NONE;
1322
1323 /* set new image size */
1324 dev->width = width;
1325 dev->height = height;
1326 dev->frame_size = dev->width * dev->height * 2;
1327 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1328 dev->bytesperline = dev->width * 2;
1329 dev->hscale = hscale;
1330 dev->vscale = vscale;
1331/* dev->both_fileds = both_fileds; */
1332 em2820_uninit_isoc(dev);
1333 em2820_set_alternate(dev);
1334 em2820_capture_start(dev, 1);
1335 em2820_resolution_set(dev);
1336 em2820_init_isoc(dev);
1337
1338 return 0;
1339 }
1340
1341 /* --- streaming capture ------------------------------------- */
1342 case VIDIOC_REQBUFS:
1343 {
1344 struct v4l2_requestbuffers *rb = arg;
1345 u32 i;
1346 int ret;
1347
1348 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1349 rb->memory != V4L2_MEMORY_MMAP)
1350 return -EINVAL;
1351
1352 if (dev->io == IO_READ) {
1353 em2820_videodbg ("method is set to read;"
1354 " close and open the device again to"
1355 " choose the mmap I/O method");
1356 return -EINVAL;
1357 }
1358
1359 for (i = 0; i < dev->num_frames; i++)
1360 if (dev->frame[i].vma_use_count) {
1361 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1362 return -EINVAL;
1363 }
1364
1365 if (dev->stream == STREAM_ON) {
1366 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1367 if ((ret = em2820_stream_interrupt(dev)))
1368 return ret;
1369 }
1370
1371 em2820_empty_framequeues(dev);
1372
1373 em2820_release_buffers(dev);
1374 if (rb->count)
1375 rb->count =
1376 em2820_request_buffers(dev, rb->count);
1377
1378 dev->frame_current = NULL;
1379
1380 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1381 rb->count);
1382 dev->io = rb->count ? IO_MMAP : IO_NONE;
1383 return 0;
1384 }
1385
1386 case VIDIOC_QUERYBUF:
1387 {
1388 struct v4l2_buffer *b = arg;
1389
1390 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1391 b->index >= dev->num_frames || dev->io != IO_MMAP)
1392 return -EINVAL;
1393
1394 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1395
1396 if (dev->frame[b->index].vma_use_count) {
1397 b->flags |= V4L2_BUF_FLAG_MAPPED;
1398 }
1399 if (dev->frame[b->index].state == F_DONE)
1400 b->flags |= V4L2_BUF_FLAG_DONE;
1401 else if (dev->frame[b->index].state != F_UNUSED)
1402 b->flags |= V4L2_BUF_FLAG_QUEUED;
1403 return 0;
1404 }
1405 case VIDIOC_QBUF:
1406 {
1407 struct v4l2_buffer *b = arg;
1408 unsigned long lock_flags;
1409
1410 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1411 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1412 return -EINVAL;
1413 }
1414
1415 if (dev->frame[b->index].state != F_UNUSED) {
1416 return -EAGAIN;
1417 }
1418 dev->frame[b->index].state = F_QUEUED;
1419
1420 /* add frame to fifo */
1421 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1422 list_add_tail(&dev->frame[b->index].frame,
1423 &dev->inqueue);
1424 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1425
1426 return 0;
1427 }
1428 case VIDIOC_DQBUF:
1429 {
1430 struct v4l2_buffer *b = arg;
1431 struct em2820_frame_t *f;
1432 unsigned long lock_flags;
1433 int ret = 0;
1434
1435 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1436 || dev->io != IO_MMAP)
1437 return -EINVAL;
1438
1439 if (list_empty(&dev->outqueue)) {
1440 if (dev->stream == STREAM_OFF)
1441 return -EINVAL;
1442 if (filp->f_flags & O_NONBLOCK)
1443 return -EAGAIN;
1444 ret = wait_event_interruptible
1445 (dev->wait_frame,
1446 (!list_empty(&dev->outqueue)) ||
1447 (dev->state & DEV_DISCONNECTED));
1448 if (ret)
1449 return ret;
1450 if (dev->state & DEV_DISCONNECTED)
1451 return -ENODEV;
1452 }
1453
1454 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1455 f = list_entry(dev->outqueue.next,
1456 struct em2820_frame_t, frame);
1457 list_del(dev->outqueue.next);
1458 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1459
1460 f->state = F_UNUSED;
1461 memcpy(b, &f->buf, sizeof(*b));
1462
1463 if (f->vma_use_count)
1464 b->flags |= V4L2_BUF_FLAG_MAPPED;
1465
1466 return 0;
1467 }
1468 default:
1469 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1470 em2820_video_do_ioctl);
1471 }
1472 return 0;
1473}
1474
1475/*
1476 * em2820_v4l2_ioctl()
1477 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1478 */
1479static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1480 unsigned int cmd, unsigned long arg)
1481{
1482 struct em2820 *dev = video_get_drvdata(video_devdata(filp));
1483 int ret = 0;
1484
1485 if (down_interruptible(&dev->fileop_lock))
1486 return -ERESTARTSYS;
1487
1488 if (dev->state & DEV_DISCONNECTED) {
1489 em2820_errdev("v4l2 ioctl: device not present\n");
1490 up(&dev->fileop_lock);
1491 return -ENODEV;
1492 }
1493
1494 if (dev->state & DEV_MISCONFIGURED) {
1495 em2820_errdev
1496 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1497 up(&dev->fileop_lock);
1498 return -EIO;
1499 }
1500
1501 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1502
1503 up(&dev->fileop_lock);
1504
1505 return ret;
1506}
1507
1508static struct file_operations em2820_v4l_fops = {
1509 .owner = THIS_MODULE,
1510 .open = em2820_v4l2_open,
1511 .release = em2820_v4l2_close,
1512 .ioctl = em2820_v4l2_ioctl,
1513 .read = em2820_v4l2_read,
1514 .poll = em2820_v4l2_poll,
1515 .mmap = em2820_v4l2_mmap,
1516 .llseek = no_llseek,
1517};
1518
1519/******************************** usb interface *****************************************/
1520
1521/*
1522 * em2820_init_dev()
1523 * allocates and inits the device structs, registers i2c bus and v4l device
1524 */
1525static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1526 int minor, int model)
1527{
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001528 struct em2820 *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001529 int retval = -ENOMEM;
1530 int errCode, i;
1531 unsigned int maxh, maxw;
1532 struct usb_interface *uif;
1533
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534 dev->udev = udev;
1535 dev->model = model;
1536 init_MUTEX(&dev->lock);
1537 init_waitqueue_head(&dev->open);
1538
1539 dev->em2820_write_regs = em2820_write_regs;
1540 dev->em2820_read_reg = em2820_read_reg;
1541 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1542 dev->em2820_write_regs_req = em2820_write_regs_req;
1543 dev->em2820_read_reg_req = em2820_read_reg_req;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001544 dev->is_em2800 = em2820_boards[model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001545 dev->has_tuner = em2820_boards[model].has_tuner;
1546 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1547 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1548 dev->decoder = em2820_boards[model].decoder;
1549
1550 if (tuner >= 0)
1551 dev->tuner_type = tuner;
1552 else
1553 dev->tuner_type = em2820_boards[model].tuner_type;
1554
1555 dev->video_inputs = em2820_boards[model].vchannels;
1556
1557 for (i = 0; i < TVNORMS; i++)
1558 if (em2820_boards[model].norm == tvnorms[i].mode)
1559 break;
1560 if (i == TVNORMS)
1561 i = 0;
1562
1563 dev->tvnorm = &tvnorms[i]; /* set default norm */
1564
1565 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1566
1567 maxw = norm_maxw(dev);
1568 maxh = norm_maxh(dev);
1569
1570 /* set default image size */
1571 dev->width = maxw;
1572 dev->height = maxh;
1573 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1574 dev->field_size = dev->width * dev->height;
1575 dev->frame_size =
1576 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1577 dev->bytesperline = dev->width * 2;
1578 dev->hscale = 0;
1579 dev->vscale = 0;
1580 dev->ctl_input = 2;
1581
1582 /* setup video picture settings for saa7113h */
1583 memset(&dev->vpic, 0, sizeof(dev->vpic));
1584 dev->vpic.colour = 128 << 8;
1585 dev->vpic.hue = 128 << 8;
1586 dev->vpic.brightness = 128 << 8;
1587 dev->vpic.contrast = 192 << 8;
1588 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1589 dev->vpic.depth = 16;
1590 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1591
1592 /* compute alternate max packet sizes */
1593 uif = dev->udev->actconfig->interface[0];
1594 dev->alt_max_pkt_size[0] = 0;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001595 for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001596 u16 tmp =
1597 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1598 wMaxPacketSize);
1599 dev->alt_max_pkt_size[i] =
1600 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1601 }
1602
1603#ifdef CONFIG_MODULES
1604 /* request some modules */
1605 if (dev->decoder == EM2820_SAA7113)
1606 request_module("saa7113");
1607 if (dev->decoder == EM2820_SAA7114)
1608 request_module("saa7114");
1609 if (dev->decoder == EM2820_TVP5150)
1610 request_module("tvp5150");
1611 if (dev->has_tuner)
1612 request_module("tuner");
1613 if (dev->tda9887_conf)
1614 request_module("tda9887");
1615#endif
1616 errCode = em2820_config(dev);
1617 if (errCode) {
1618 em2820_errdev("error configuring device\n");
1619 kfree(dev);
1620 return -ENOMEM;
1621 }
1622
1623 down(&dev->lock);
1624 /* register i2c bus */
1625 em2820_i2c_register(dev);
1626
1627 /* Do board specific init and eeprom reading */
1628 em2820_card_setup(dev);
1629
1630 /* configure the device */
1631 em2820_config_i2c(dev);
1632
1633 up(&dev->lock);
1634
1635 errCode = em2820_config(dev);
1636
1637#ifdef CONFIG_MODULES
1638 if (dev->has_msp34xx)
1639 request_module("msp3400");
1640#endif
1641 /* allocate and fill v4l2 device struct */
1642 dev->vdev = video_device_alloc();
1643 if (NULL == dev->vdev) {
1644 em2820_errdev("cannot allocate video_device.\n");
1645 kfree(dev);
1646 return -ENOMEM;
1647 }
1648
1649 dev->vdev->owner = THIS_MODULE;
1650 dev->vdev->type = VID_TYPE_CAPTURE;
1651 if (dev->has_tuner)
1652 dev->vdev->type |= VID_TYPE_TUNER;
1653 dev->vdev->hardware = 0;
1654 dev->vdev->fops = &em2820_v4l_fops;
1655 dev->vdev->minor = -1;
1656 dev->vdev->dev = &dev->udev->dev;
1657 dev->vdev->release = video_device_release;
1658 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1659 "em2820 video");
1660 video_set_drvdata(dev->vdev, dev);
1661
1662 /* register v4l2 device */
1663 down(&dev->lock);
1664 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1665 em2820_errdev("unable to register video device (error=%i).\n",
1666 retval);
1667 up(&dev->lock);
1668 video_set_drvdata(dev->vdev, NULL);
1669 video_device_release(dev->vdev);
1670 kfree(dev);
1671 return -ENODEV;
1672 }
1673 if (dev->has_msp34xx) {
1674 /* Send a reset to other chips via gpio */
1675 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1676 udelay(2500);
1677 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1678 udelay(2500);
1679
1680 }
1681 video_mux(dev, 0);
1682
1683 up(&dev->lock);
1684
1685 em2820_info("V4L2 device registered as /dev/video%d\n",
1686 dev->vdev->minor);
1687
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001688 return 0;
1689}
1690
1691/*
1692 * em2820_usb_probe()
1693 * checks for supported devices
1694 */
1695static int em2820_usb_probe(struct usb_interface *interface,
1696 const struct usb_device_id *id)
1697{
1698 const struct usb_endpoint_descriptor *endpoint;
1699 struct usb_device *udev;
1700 struct em2820 *dev = NULL;
1701 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001702 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001703
1704 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001705 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1706
1707 em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n",
1708 udev->descriptor.idVendor,udev->descriptor.idProduct,
1709 ifnum,
1710 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001711
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001712 /* Don't register audio interfaces */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001713 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001714 return -ENODEV;
1715
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001716 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1717
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718 /* check if the the device has the iso in endpoint at the correct place */
1719 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1720 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001721 em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722 return -ENODEV;
1723 }
1724 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001725 em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726 return -ENODEV;
1727 }
1728
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001729 model=id->driver_info;
1730 nr=interface->minor;
1731
1732 if (nr>EM2820_MAXBOARDS) {
1733 printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS);
1734 return -ENOMEM;
1735 }
1736
1737 /* allocate memory for our device state and initialize it */
1738 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1739 if (dev == NULL) {
1740 em2820_err(DRIVER_NAME ": out of memory!\n");
1741 return -ENOMEM;
1742 }
1743 memset(dev, 0, sizeof(*dev));
1744
1745 snprintf(dev->name, 29, "em2820 #%d", nr);
1746
1747 if ((card[nr]>=0)&&(card[nr]<em2820_bcount))
1748 model=card[nr];
1749
1750 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1751 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1752 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1753 "%s: workaround that. Redirect complaints to the vendor of\n"
1754 "%s: the TV card. Best regards,\n"
1755 "%s: -- tux\n",
1756 dev->name,dev->name,dev->name,dev->name,dev->name);
1757 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1758 dev->name);
1759 for (i = 0; i < em2820_bcount; i++) {
1760 printk("%s: card=%d -> %s\n",
1761 dev->name, i, em2820_boards[i].name);
1762 }
1763 }
1764
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765 /* allocate device struct */
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001766 retval = em2820_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001767 if (retval)
1768 return retval;
1769
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001770 em2820_info("Found %s\n", em2820_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001771
1772 /* save our data pointer in this interface device */
1773 usb_set_intfdata(interface, dev);
1774 return 0;
1775}
1776
1777/*
1778 * em2820_usb_disconnect()
1779 * called when the device gets diconencted
1780 * video device will be unregistered on v4l2_close in case it is still open
1781 */
1782static void em2820_usb_disconnect(struct usb_interface *interface)
1783{
1784 struct em2820 *dev = usb_get_intfdata(interface);
1785 usb_set_intfdata(interface, NULL);
1786
1787 if (!dev)
1788 return;
1789
1790 down_write(&em2820_disconnect);
1791
1792 down(&dev->lock);
1793
1794 em2820_info("disconnecting %s\n", dev->vdev->name);
1795
1796 wake_up_interruptible_all(&dev->open);
1797
1798 if (dev->users) {
1799 em2820_warn
1800 ("device /dev/video%d is open! Deregistration and memory "
1801 "deallocation are deferred on close.\n", dev->vdev->minor);
1802 dev->state |= DEV_MISCONFIGURED;
1803 em2820_uninit_isoc(dev);
1804 dev->state |= DEV_DISCONNECTED;
1805 wake_up_interruptible(&dev->wait_frame);
1806 wake_up_interruptible(&dev->wait_stream);
1807 } else {
1808 dev->state |= DEV_DISCONNECTED;
1809 em2820_release_resources(dev);
1810 }
1811
1812 up(&dev->lock);
1813
1814 if (!dev->users)
1815 kfree(dev);
1816
1817 up_write(&em2820_disconnect);
1818
1819}
1820
1821static struct usb_driver em2820_usb_driver = {
1822 .owner = THIS_MODULE,
1823 .name = "em2820",
1824 .probe = em2820_usb_probe,
1825 .disconnect = em2820_usb_disconnect,
1826 .id_table = em2820_id_table,
1827};
1828
1829static int __init em2820_module_init(void)
1830{
1831 int result;
1832
1833 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1834 (EM2820_VERSION_CODE >> 16) & 0xff,
1835 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1836#ifdef SNAPSHOT
1837 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1838 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1839#endif
1840
1841 /* register this driver with the USB subsystem */
1842 result = usb_register(&em2820_usb_driver);
1843 if (result)
1844 em2820_err(DRIVER_NAME
1845 " usb_register failed. Error number %d.\n", result);
1846
1847 return result;
1848}
1849
1850static void __exit em2820_module_exit(void)
1851{
1852 /* deregister this driver with the USB subsystem */
1853 usb_deregister(&em2820_usb_driver);
1854}
1855
1856module_init(em2820_module_init);
1857module_exit(em2820_module_exit);