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