blob: 16a6d2da7391075d614b3c93b3612ce5786ec6fb [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 */
Markus Rechberger86bb4a22005-11-08 21:37:53 -0800360static int em2820_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800361{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800362 int errCode;
Markus Rechberger86bb4a22005-11-08 21:37:53 -0800363 struct em2820 *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800364
365 em2820_videodbg("users=%d", dev->users);
366
367 down(&dev->lock);
368
369 em2820_uninit_isoc(dev);
370
371 em2820_release_buffers(dev);
372
373 /* the device is already disconnect, free the remaining resources */
374 if (dev->state & DEV_DISCONNECTED) {
375 em2820_release_resources(dev);
376 up(&dev->lock);
377 kfree(dev);
378 return 0;
379 }
380
381 /* set alternate 0 */
382 dev->alt = 0;
383 em2820_videodbg("setting alternate 0");
384 errCode = usb_set_interface(dev->udev, 0, 0);
385 if (errCode < 0) {
386 em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
387 errCode);
388 }
389
390 dev->users--;
391 wake_up_interruptible_nr(&dev->open, 1);
392 up(&dev->lock);
393 return 0;
394}
395
396/*
397 * em2820_v4l2_read()
398 * will allocate buffers when called for the first time
399 */
400static ssize_t
401em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
402 loff_t * f_pos)
403{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800404 struct em2820_frame_t *f, *i;
405 unsigned long lock_flags;
406 int ret = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -0800407 struct em2820 *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800408
409 if (down_interruptible(&dev->fileop_lock))
410 return -ERESTARTSYS;
411
412 if (dev->state & DEV_DISCONNECTED) {
413 em2820_videodbg("device not present");
414 up(&dev->fileop_lock);
415 return -ENODEV;
416 }
417
418 if (dev->state & DEV_MISCONFIGURED) {
419 em2820_videodbg("device misconfigured; close and open it again");
420 up(&dev->fileop_lock);
421 return -EIO;
422 }
423
424 if (dev->io == IO_MMAP) {
425 em2820_videodbg ("IO method is set to mmap; close and open"
426 " the device again to choose the read method");
427 up(&dev->fileop_lock);
428 return -EINVAL;
429 }
430
431 if (dev->io == IO_NONE) {
432 if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
433 em2820_errdev("read failed, not enough memory\n");
434 up(&dev->fileop_lock);
435 return -ENOMEM;
436 }
437 dev->io = IO_READ;
438 dev->stream = STREAM_ON;
439 em2820_queue_unusedframes(dev);
440 }
441
442 if (!count) {
443 up(&dev->fileop_lock);
444 return 0;
445 }
446
447 if (list_empty(&dev->outqueue)) {
448 if (filp->f_flags & O_NONBLOCK) {
449 up(&dev->fileop_lock);
450 return -EAGAIN;
451 }
452 ret = wait_event_interruptible
453 (dev->wait_frame,
454 (!list_empty(&dev->outqueue)) ||
455 (dev->state & DEV_DISCONNECTED));
456 if (ret) {
457 up(&dev->fileop_lock);
458 return ret;
459 }
460 if (dev->state & DEV_DISCONNECTED) {
461 up(&dev->fileop_lock);
462 return -ENODEV;
463 }
464 }
465
466 f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
467
468 spin_lock_irqsave(&dev->queue_lock, lock_flags);
469 list_for_each_entry(i, &dev->outqueue, frame)
470 i->state = F_UNUSED;
471 INIT_LIST_HEAD(&dev->outqueue);
472 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
473
474 em2820_queue_unusedframes(dev);
475
476 if (count > f->buf.length)
477 count = f->buf.length;
478
479 if (copy_to_user(buf, f->bufmem, count)) {
480 up(&dev->fileop_lock);
481 return -EFAULT;
482 }
483 *f_pos += count;
484
485 up(&dev->fileop_lock);
486
487 return count;
488}
489
490/*
491 * em2820_v4l2_poll()
492 * will allocate buffers when called for the first time
493 */
494static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
495{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800496 unsigned int mask = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -0800497 struct em2820 *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800498
499 if (down_interruptible(&dev->fileop_lock))
500 return POLLERR;
501
502 if (dev->state & DEV_DISCONNECTED) {
503 em2820_videodbg("device not present");
504 } else if (dev->state & DEV_MISCONFIGURED) {
505 em2820_videodbg("device is misconfigured; close and open it again");
506 } else {
507 if (dev->io == IO_NONE) {
508 if (!em2820_request_buffers
509 (dev, EM2820_NUM_READ_FRAMES)) {
510 em2820_warn
511 ("poll() failed, not enough memory\n");
512 } else {
513 dev->io = IO_READ;
514 dev->stream = STREAM_ON;
515 }
516 }
517
518 if (dev->io == IO_READ) {
519 em2820_queue_unusedframes(dev);
520 poll_wait(filp, &dev->wait_frame, wait);
521
522 if (!list_empty(&dev->outqueue))
523 mask |= POLLIN | POLLRDNORM;
524
525 up(&dev->fileop_lock);
526
527 return mask;
528 }
529 }
530
531 up(&dev->fileop_lock);
532 return POLLERR;
533}
534
535/*
536 * em2820_vm_open()
537 */
538static void em2820_vm_open(struct vm_area_struct *vma)
539{
540 struct em2820_frame_t *f = vma->vm_private_data;
541 f->vma_use_count++;
542}
543
544/*
545 * em2820_vm_close()
546 */
547static void em2820_vm_close(struct vm_area_struct *vma)
548{
549 /* NOTE: buffers are not freed here */
550 struct em2820_frame_t *f = vma->vm_private_data;
551 f->vma_use_count--;
552}
553
554static struct vm_operations_struct em2820_vm_ops = {
555 .open = em2820_vm_open,
556 .close = em2820_vm_close,
557};
558
559/*
560 * em2820_v4l2_mmap()
561 */
562static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
563{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800564 unsigned long size = vma->vm_end - vma->vm_start,
565 start = vma->vm_start, pos, page;
566 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800567
568 struct em2820 *dev = filp->private_data;
569
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800570 if (down_interruptible(&dev->fileop_lock))
571 return -ERESTARTSYS;
572
573 if (dev->state & DEV_DISCONNECTED) {
574 em2820_videodbg("mmap: device not present");
575 up(&dev->fileop_lock);
576 return -ENODEV;
577 }
578
579 if (dev->state & DEV_MISCONFIGURED) {
580 em2820_videodbg ("mmap: Device is misconfigured; close and "
581 "open it again");
582 up(&dev->fileop_lock);
583 return -EIO;
584 }
585
586 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
587 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
588 up(&dev->fileop_lock);
589 return -EINVAL;
590 }
591
592 for (i = 0; i < dev->num_frames; i++) {
593 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
594 break;
595 }
596 if (i == dev->num_frames) {
597 em2820_videodbg("mmap: user supplied mapping address is out of range");
598 up(&dev->fileop_lock);
599 return -EINVAL;
600 }
601
602 /* VM_IO is eventually going to replace PageReserved altogether */
603 vma->vm_flags |= VM_IO;
604 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
605
606 pos = (unsigned long)dev->frame[i].bufmem;
607 while (size > 0) { /* size is page-aligned */
608 page = vmalloc_to_pfn((void *)pos);
609 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
610 vma->vm_page_prot)) {
611 em2820_videodbg("mmap: rename page map failed");
612 up(&dev->fileop_lock);
613 return -EAGAIN;
614 }
615 start += PAGE_SIZE;
616 pos += PAGE_SIZE;
617 size -= PAGE_SIZE;
618 }
619
620 vma->vm_ops = &em2820_vm_ops;
621 vma->vm_private_data = &dev->frame[i];
622
623 em2820_vm_open(vma);
624 up(&dev->fileop_lock);
625 return 0;
626}
627
628/*
629 * em2820_get_ctrl()
630 * return the current saturation, brightness or contrast, mute state
631 */
632static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
633{
634 s32 tmp;
635 switch (ctrl->id) {
636 case V4L2_CID_AUDIO_MUTE:
637 ctrl->value = dev->mute;
638 return 0;
639 case V4L2_CID_AUDIO_VOLUME:
640 ctrl->value = dev->volume;
641 return 0;
642 case V4L2_CID_BRIGHTNESS:
643 if ((tmp = em2820_brightness_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_CONTRAST:
648 if ((ctrl->value = em2820_contrast_get(dev)) < 0)
649 return -EIO;
650 return 0;
651 case V4L2_CID_SATURATION:
652 if ((ctrl->value = em2820_saturation_get(dev)) < 0)
653 return -EIO;
654 return 0;
655 case V4L2_CID_RED_BALANCE:
656 if ((tmp = em2820_v_balance_get(dev)) < 0)
657 return -EIO;
658 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
659 return 0;
660 case V4L2_CID_BLUE_BALANCE:
661 if ((tmp = em2820_u_balance_get(dev)) < 0)
662 return -EIO;
663 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
664 return 0;
665 case V4L2_CID_GAMMA:
666 if ((ctrl->value = em2820_gamma_get(dev)) < 0)
667 return -EIO;
668 return 0;
669 default:
670 return -EINVAL;
671 }
672}
673
674/*
675 * em2820_set_ctrl()
676 * mute or set new saturation, brightness or contrast
677 */
678static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
679{
680 switch (ctrl->id) {
681 case V4L2_CID_AUDIO_MUTE:
682 if (ctrl->value != dev->mute) {
683 dev->mute = ctrl->value;
684 em2820_audio_usb_mute(dev, ctrl->value);
685 return em2820_audio_analog_set(dev);
686 }
687 return 0;
688 case V4L2_CID_AUDIO_VOLUME:
689 dev->volume = ctrl->value;
690 return em2820_audio_analog_set(dev);
691 case V4L2_CID_BRIGHTNESS:
692 return em2820_brightness_set(dev, ctrl->value);
693 case V4L2_CID_CONTRAST:
694 return em2820_contrast_set(dev, ctrl->value);
695 case V4L2_CID_SATURATION:
696 return em2820_saturation_set(dev, ctrl->value);
697 case V4L2_CID_RED_BALANCE:
698 return em2820_v_balance_set(dev, ctrl->value);
699 case V4L2_CID_BLUE_BALANCE:
700 return em2820_u_balance_set(dev, ctrl->value);
701 case V4L2_CID_GAMMA:
702 return em2820_gamma_set(dev, ctrl->value);
703 default:
704 return -EINVAL;
705 }
706}
707
708/*
709 * em2820_stream_interrupt()
710 * stops streaming
711 */
712static int em2820_stream_interrupt(struct em2820 *dev)
713{
714 int ret = 0;
715
716 /* stop reading from the device */
717
718 dev->stream = STREAM_INTERRUPT;
719 ret = wait_event_timeout(dev->wait_stream,
720 (dev->stream == STREAM_OFF) ||
721 (dev->state & DEV_DISCONNECTED),
722 EM2820_URB_TIMEOUT);
723 if (dev->state & DEV_DISCONNECTED)
724 return -ENODEV;
725 else if (ret) {
726 dev->state |= DEV_MISCONFIGURED;
727 em2820_videodbg("device is misconfigured; close and "
728 "open /dev/video%d again", dev->vdev->minor);
729 return ret;
730 }
731
732 return 0;
733}
734
735static int em2820_set_norm(struct em2820 *dev, int width, int height)
736{
737 unsigned int hscale, vscale;
738 unsigned int maxh, maxw;
739
740 maxw = norm_maxw(dev);
741 maxh = norm_maxh(dev);
742
743 /* width must even because of the YUYV format */
744 /* height must be even because of interlacing */
745 height &= 0xfffe;
746 width &= 0xfffe;
747
748 if (height < 32)
749 height = 32;
750 if (height > maxh)
751 height = maxh;
752 if (width < 48)
753 width = 48;
754 if (width > maxw)
755 width = maxw;
756
757 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
758 hscale = 0x3fff;
759 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
760
761 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
762 vscale = 0x3fff;
763 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
764
765 /* set new image size */
766 dev->width = width;
767 dev->height = height;
768 dev->frame_size = dev->width * dev->height * 2;
769 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
770 dev->bytesperline = dev->width * 2;
771 dev->hscale = hscale;
772 dev->vscale = vscale;
773
774 em2820_resolution_set(dev);
775
776 return 0;
777}
778
779static void video_mux(struct em2820 *dev, int index)
780{
781 int input, ainput;
782
783 input = INPUT(index)->vmux;
784 dev->ctl_input = index;
785
786 em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
787
788 dev->ctl_ainput = INPUT(index)->amux;
789
790 switch (dev->ctl_ainput) {
791 case 0:
792 ainput = EM2820_AUDIO_SRC_TUNER;
793 break;
794 default:
795 ainput = EM2820_AUDIO_SRC_LINE;
796 }
797
798 em2820_audio_source(dev, ainput);
799}
800
801/*
802 * em2820_v4l2_do_ioctl()
803 * This function is _not_ called directly, but from
804 * em2820_v4l2_ioctl. Userspace
805 * copying is done already, arg is a kernel pointer.
806 */
807static int em2820_do_ioctl(struct inode *inode, struct file *filp,
808 struct em2820 *dev, unsigned int cmd, void *arg,
809 v4l2_kioctl driver_ioctl)
810{
811 int ret;
812
813 switch (cmd) {
814 /* ---------- tv norms ---------- */
815 case VIDIOC_ENUMSTD:
816 {
817 struct v4l2_standard *e = arg;
818 unsigned int i;
819
820 i = e->index;
821 if (i >= TVNORMS)
822 return -EINVAL;
823 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
824 tvnorms[e->index].name);
825 e->index = i;
826 if (ret < 0)
827 return ret;
828 return 0;
829 }
830 case VIDIOC_G_STD:
831 {
832 v4l2_std_id *id = arg;
833
834 *id = dev->tvnorm->id;
835 return 0;
836 }
837 case VIDIOC_S_STD:
838 {
839 v4l2_std_id *id = arg;
840 unsigned int i;
841
842 for (i = 0; i < TVNORMS; i++)
843 if (*id == tvnorms[i].id)
844 break;
845 if (i == TVNORMS)
846 for (i = 0; i < TVNORMS; i++)
847 if (*id & tvnorms[i].id)
848 break;
849 if (i == TVNORMS)
850 return -EINVAL;
851
852 down(&dev->lock);
853 dev->tvnorm = &tvnorms[i];
854
855 em2820_set_norm(dev, dev->width, dev->height);
856
857/*
858 dev->width=norm_maxw(dev);
859 dev->height=norm_maxh(dev);
860 dev->frame_size=dev->width*dev->height*2;
861 dev->field_size=dev->frame_size>>1;
862 dev->bytesperline=dev->width*2;
863 dev->hscale=0;
864 dev->vscale=0;
865
866 em2820_resolution_set(dev);
867*/
868/*
869 em2820_uninit_isoc(dev);
870 em2820_set_alternate(dev);
871 em2820_capture_start(dev, 1);
872 em2820_resolution_set(dev);
873 em2820_init_isoc(dev);
874*/
875 em2820_i2c_call_clients(dev, DECODER_SET_NORM,
876 &tvnorms[i].mode);
877 em2820_i2c_call_clients(dev, VIDIOC_S_STD,
878 &dev->tvnorm->id);
879
880 up(&dev->lock);
881
882 return 0;
883 }
884
885 /* ------ input switching ---------- */
886 case VIDIOC_ENUMINPUT:
887 {
888 struct v4l2_input *i = arg;
889 unsigned int n;
890 static const char *iname[] = {
891 [EM2820_VMUX_COMPOSITE1] = "Composite1",
892 [EM2820_VMUX_COMPOSITE2] = "Composite2",
893 [EM2820_VMUX_COMPOSITE3] = "Composite3",
894 [EM2820_VMUX_COMPOSITE4] = "Composite4",
895 [EM2820_VMUX_SVIDEO] = "S-Video",
896 [EM2820_VMUX_TELEVISION] = "Television",
897 [EM2820_VMUX_CABLE] = "Cable TV",
898 [EM2820_VMUX_DVB] = "DVB",
899 [EM2820_VMUX_DEBUG] = "for debug only",
900 };
901
902 n = i->index;
903 if (n >= MAX_EM2820_INPUT)
904 return -EINVAL;
905 if (0 == INPUT(n)->type)
906 return -EINVAL;
907 memset(i, 0, sizeof(*i));
908 i->index = n;
909 i->type = V4L2_INPUT_TYPE_CAMERA;
910 strcpy(i->name, iname[INPUT(n)->type]);
911 if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
912 (EM2820_VMUX_CABLE == INPUT(n)->type))
913 i->type = V4L2_INPUT_TYPE_TUNER;
914 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
915 i->std |= tvnorms[n].id;
916 return 0;
917 }
918
919 case VIDIOC_G_INPUT:
920 {
921 int *i = arg;
922 *i = dev->ctl_input;
923
924 return 0;
925 }
926
927 case VIDIOC_S_INPUT:
928 {
929 int *index = arg;
930
931 if (*index >= MAX_EM2820_INPUT)
932 return -EINVAL;
933 if (0 == INPUT(*index)->type)
934 return -EINVAL;
935
936 down(&dev->lock);
937 video_mux(dev, *index);
938 up(&dev->lock);
939
940 return 0;
941 }
942
943 case VIDIOC_G_AUDIO:
944 {
945 struct v4l2_audio *a = arg;
946 unsigned int index = a->index;
947
948 if (a->index > 1)
949 return -EINVAL;
950 memset(a, 0, sizeof(*a));
951 index = dev->ctl_ainput;
952
953 if (index == 0) {
954 strcpy(a->name, "Television");
955 } else {
956 strcpy(a->name, "Line In");
957 }
958 a->capability = V4L2_AUDCAP_STEREO;
959 a->index = index;
960 return 0;
961 }
962
963 case VIDIOC_S_AUDIO:
964 {
965 struct v4l2_audio *a = arg;
966 if (a->index != dev->ctl_ainput)
967 return -EINVAL;
968
969 return 0;
970 }
971
972 /* --- controls ---------------------------------------------- */
973 case VIDIOC_QUERYCTRL:
974 {
975 struct v4l2_queryctrl *qc = arg;
976 u8 i, n;
977 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
978 for (i = 0; i < n; i++)
979 if (qc->id && qc->id == em2820_qctrl[i].id) {
980 memcpy(qc, &(em2820_qctrl[i]),
981 sizeof(*qc));
982 return 0;
983 }
984
985 return -EINVAL;
986 }
987
988 case VIDIOC_G_CTRL:
989 {
990 struct v4l2_control *ctrl = arg;
991
992
993 return em2820_get_ctrl(dev, ctrl);
994 }
995
996 case VIDIOC_S_CTRL_OLD: /* ??? */
997 case VIDIOC_S_CTRL:
998 {
999 struct v4l2_control *ctrl = arg;
1000 u8 i, n;
1001
1002
1003 n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
1004 for (i = 0; i < n; i++)
1005 if (ctrl->id == em2820_qctrl[i].id) {
1006 if (ctrl->value <
1007 em2820_qctrl[i].minimum
1008 || ctrl->value >
1009 em2820_qctrl[i].maximum)
1010 return -ERANGE;
1011
1012 return em2820_set_ctrl(dev, ctrl);
1013 }
1014 return -EINVAL;
1015 }
1016
1017 /* --- tuner ioctls ------------------------------------------ */
1018 case VIDIOC_G_TUNER:
1019 {
1020 struct v4l2_tuner *t = arg;
1021 int status = 0;
1022
1023 if (0 != t->index)
1024 return -EINVAL;
1025
1026 memset(t, 0, sizeof(*t));
1027 strcpy(t->name, "Tuner");
1028 t->type = V4L2_TUNER_ANALOG_TV;
1029 t->capability = V4L2_TUNER_CAP_NORM;
1030 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1031/* t->signal = 0xffff;*/
1032/* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
1033 /* No way to get signal strength? */
1034 down(&dev->lock);
1035 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1036 &status);
1037 up(&dev->lock);
1038 t->signal =
1039 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1040
1041 em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
1042 t->afc);
1043 return 0;
1044 }
1045 case VIDIOC_S_TUNER:
1046 {
1047 struct v4l2_tuner *t = arg;
1048 int status = 0;
1049
1050 if (0 != t->index)
1051 return -EINVAL;
1052 memset(t, 0, sizeof(*t));
1053 strcpy(t->name, "Tuner");
1054 t->type = V4L2_TUNER_ANALOG_TV;
1055 t->capability = V4L2_TUNER_CAP_NORM;
1056 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1057/* t->signal = 0xffff; */
1058 /* No way to get signal strength? */
1059 down(&dev->lock);
1060 em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
1061 &status);
1062 up(&dev->lock);
1063 t->signal =
1064 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1065
1066 em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
1067 t->signal, t->afc);
1068 return 0;
1069 }
1070 case VIDIOC_G_FREQUENCY:
1071 {
1072 struct v4l2_frequency *f = arg;
1073
1074 memset(f, 0, sizeof(*f));
1075 f->type = V4L2_TUNER_ANALOG_TV;
1076 f->frequency = dev->ctl_freq;
1077
1078 return 0;
1079 }
1080 case VIDIOC_S_FREQUENCY:
1081 {
1082 struct v4l2_frequency *f = arg;
1083
1084 if (0 != f->tuner)
1085 return -EINVAL;
1086
1087 if (V4L2_TUNER_ANALOG_TV != f->type)
1088 return -EINVAL;
1089
1090 down(&dev->lock);
1091 dev->ctl_freq = f->frequency;
1092 em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1093 up(&dev->lock);
1094 return 0;
1095 }
1096
1097 case VIDIOC_CROPCAP:
1098 {
1099 struct v4l2_cropcap *cc = arg;
1100
1101 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001102 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001103 cc->bounds.left = 0;
1104 cc->bounds.top = 0;
1105 cc->bounds.width = dev->width;
1106 cc->bounds.height = dev->height;
1107 cc->defrect = cc->bounds;
1108 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1109 cc->pixelaspect.denominator = 59;
1110 return 0;
1111 }
1112 case VIDIOC_STREAMON:
1113 {
1114 int *type = arg;
1115
1116 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1117 || dev->io != IO_MMAP)
1118 return -EINVAL;
1119
1120 if (list_empty(&dev->inqueue))
1121 return -EINVAL;
1122
1123 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1124
1125 em2820_videodbg("VIDIOC_STREAMON: starting stream");
1126
1127 return 0;
1128 }
1129 case VIDIOC_STREAMOFF:
1130 {
1131 int *type = arg;
1132 int ret;
1133
1134 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1135 || dev->io != IO_MMAP)
1136 return -EINVAL;
1137
1138 if (dev->stream == STREAM_ON) {
1139 em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1140 if ((ret = em2820_stream_interrupt(dev)))
1141 return ret;
1142 }
1143 em2820_empty_framequeues(dev);
1144
1145 return 0;
1146 }
1147 default:
1148 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1149 driver_ioctl);
1150 }
1151 return 0;
1152}
1153
1154/*
1155 * em2820_v4l2_do_ioctl()
1156 * This function is _not_ called directly, but from
1157 * em2820_v4l2_ioctl. Userspace
1158 * copying is done already, arg is a kernel pointer.
1159 */
1160static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
1161 unsigned int cmd, void *arg)
1162{
1163 struct em2820 *dev = filp->private_data;
1164
1165 if (!dev)
1166 return -ENODEV;
1167
1168 if (video_debug > 1)
1169 em2820_print_ioctl(dev->name,cmd);
1170
1171 switch (cmd) {
1172
1173 /* --- capabilities ------------------------------------------ */
1174 case VIDIOC_QUERYCAP:
1175 {
1176 struct v4l2_capability *cap = arg;
1177
1178 memset(cap, 0, sizeof(*cap));
1179 strlcpy(cap->driver, "em2820", sizeof(cap->driver));
1180 strlcpy(cap->card, em2820_boards[dev->model].name,
1181 sizeof(cap->card));
1182 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1183 sizeof(cap->bus_info));
1184 cap->version = EM2820_VERSION_CODE;
1185 cap->capabilities =
1186 V4L2_CAP_VIDEO_CAPTURE |
1187 V4L2_CAP_AUDIO |
1188 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1189 if (dev->has_tuner)
1190 cap->capabilities |= V4L2_CAP_TUNER;
1191 return 0;
1192 }
1193
1194 /* --- capture ioctls ---------------------------------------- */
1195 case VIDIOC_ENUM_FMT:
1196 {
1197 struct v4l2_fmtdesc *fmtd = arg;
1198
1199 if (fmtd->index != 0)
1200 return -EINVAL;
1201 memset(fmtd, 0, sizeof(*fmtd));
1202 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1203 strcpy(fmtd->description, "Packed YUY2");
1204 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1205 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1206 return 0;
1207 }
1208
1209 case VIDIOC_G_FMT:
1210 {
1211 struct v4l2_format *format = arg;
1212
1213 em2820_videodbg("VIDIOC_G_FMT: type=%s",
1214 format->type ==
1215 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1216 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1217 V4L2_BUF_TYPE_VBI_CAPTURE ?
1218 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1219 "not supported");
1220
1221 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1222 return -EINVAL;
1223
1224 format->fmt.pix.width = dev->width;
1225 format->fmt.pix.height = dev->height;
1226 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1227 format->fmt.pix.bytesperline = dev->bytesperline;
1228 format->fmt.pix.sizeimage = dev->frame_size;
1229 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1230 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1231
1232 em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
1233 dev->height);
1234 return 0;
1235 }
1236
1237 case VIDIOC_TRY_FMT:
1238 case VIDIOC_S_FMT:
1239 {
1240 struct v4l2_format *format = arg;
1241 u32 i;
1242 int ret = 0;
1243 int width = format->fmt.pix.width;
1244 int height = format->fmt.pix.height;
1245 unsigned int hscale, vscale;
1246 unsigned int maxh, maxw;
1247
1248 maxw = norm_maxw(dev);
1249 maxh = norm_maxh(dev);
1250
1251/* int both_fields; */
1252
1253 em2820_videodbg("%s: type=%s",
1254 cmd ==
1255 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1256 "VIDIOC_S_FMT",
1257 format->type ==
1258 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1259 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1260 V4L2_BUF_TYPE_VBI_CAPTURE ?
1261 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1262 "not supported");
1263
1264 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1265 return -EINVAL;
1266
1267 em2820_videodbg("%s: requested %dx%d",
1268 cmd ==
1269 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1270 "VIDIOC_S_FMT", format->fmt.pix.width,
1271 format->fmt.pix.height);
1272
1273 /* FIXME: Move some code away from here */
1274 /* width must even because of the YUYV format */
1275 /* height must be even because of interlacing */
1276 height &= 0xfffe;
1277 width &= 0xfffe;
1278
1279 if (height < 32)
1280 height = 32;
1281 if (height > maxh)
1282 height = maxh;
1283 if (width < 48)
1284 width = 48;
1285 if (width > maxw)
1286 width = maxw;
1287
Sascha Sommer74458e62005-11-08 21:37:33 -08001288 /* FIXME*/
1289 if(dev->is_em2800){
1290 /* we only know how to scale to 50% */
1291 if(height % (maxh / 2))
1292 height=maxh;
1293 if(width % (maxw / 2))
1294 width=maxw;
1295 /* larger resoltion don't seem to work either */
1296 if(width == maxw && height == maxh)
1297 width /= 2;
1298 }
1299
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001300 if ((hscale =
1301 (((unsigned long)maxw) << 12) / width - 4096L) >=
1302 0x4000)
1303 hscale = 0x3fff;
1304 width =
1305 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1306
1307 if ((vscale =
1308 (((unsigned long)maxh) << 12) / height - 4096L) >=
1309 0x4000)
1310 vscale = 0x3fff;
1311 height =
1312 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1313
1314 format->fmt.pix.width = width;
1315 format->fmt.pix.height = height;
1316 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1317 format->fmt.pix.bytesperline = width * 2;
1318 format->fmt.pix.sizeimage = width * 2 * height;
1319 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1320 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1321
1322 em2820_videodbg("%s: returned %dx%d (%d, %d)",
1323 cmd ==
1324 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1325 "VIDIOC_S_FMT", format->fmt.pix.width,
1326 format->fmt.pix.height, hscale, vscale);
1327
1328 if (cmd == VIDIOC_TRY_FMT)
1329 return 0;
1330
1331 for (i = 0; i < dev->num_frames; i++)
1332 if (dev->frame[i].vma_use_count) {
1333 em2820_videodbg("VIDIOC_S_FMT failed. "
1334 "Unmap the buffers first.");
1335 return -EINVAL;
1336 }
1337
1338 /* stop io in case it is already in progress */
1339 if (dev->stream == STREAM_ON) {
1340 em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
1341 if ((ret = em2820_stream_interrupt(dev)))
1342 return ret;
1343 }
1344
1345 em2820_release_buffers(dev);
1346 dev->io = IO_NONE;
1347
1348 /* set new image size */
1349 dev->width = width;
1350 dev->height = height;
1351 dev->frame_size = dev->width * dev->height * 2;
1352 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1353 dev->bytesperline = dev->width * 2;
1354 dev->hscale = hscale;
1355 dev->vscale = vscale;
1356/* dev->both_fileds = both_fileds; */
1357 em2820_uninit_isoc(dev);
1358 em2820_set_alternate(dev);
1359 em2820_capture_start(dev, 1);
1360 em2820_resolution_set(dev);
1361 em2820_init_isoc(dev);
1362
1363 return 0;
1364 }
1365
1366 /* --- streaming capture ------------------------------------- */
1367 case VIDIOC_REQBUFS:
1368 {
1369 struct v4l2_requestbuffers *rb = arg;
1370 u32 i;
1371 int ret;
1372
1373 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1374 rb->memory != V4L2_MEMORY_MMAP)
1375 return -EINVAL;
1376
1377 if (dev->io == IO_READ) {
1378 em2820_videodbg ("method is set to read;"
1379 " close and open the device again to"
1380 " choose the mmap I/O method");
1381 return -EINVAL;
1382 }
1383
1384 for (i = 0; i < dev->num_frames; i++)
1385 if (dev->frame[i].vma_use_count) {
1386 em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
1387 return -EINVAL;
1388 }
1389
1390 if (dev->stream == STREAM_ON) {
1391 em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
1392 if ((ret = em2820_stream_interrupt(dev)))
1393 return ret;
1394 }
1395
1396 em2820_empty_framequeues(dev);
1397
1398 em2820_release_buffers(dev);
1399 if (rb->count)
1400 rb->count =
1401 em2820_request_buffers(dev, rb->count);
1402
1403 dev->frame_current = NULL;
1404
1405 em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
1406 rb->count);
1407 dev->io = rb->count ? IO_MMAP : IO_NONE;
1408 return 0;
1409 }
1410
1411 case VIDIOC_QUERYBUF:
1412 {
1413 struct v4l2_buffer *b = arg;
1414
1415 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1416 b->index >= dev->num_frames || dev->io != IO_MMAP)
1417 return -EINVAL;
1418
1419 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1420
1421 if (dev->frame[b->index].vma_use_count) {
1422 b->flags |= V4L2_BUF_FLAG_MAPPED;
1423 }
1424 if (dev->frame[b->index].state == F_DONE)
1425 b->flags |= V4L2_BUF_FLAG_DONE;
1426 else if (dev->frame[b->index].state != F_UNUSED)
1427 b->flags |= V4L2_BUF_FLAG_QUEUED;
1428 return 0;
1429 }
1430 case VIDIOC_QBUF:
1431 {
1432 struct v4l2_buffer *b = arg;
1433 unsigned long lock_flags;
1434
1435 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1436 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1437 return -EINVAL;
1438 }
1439
1440 if (dev->frame[b->index].state != F_UNUSED) {
1441 return -EAGAIN;
1442 }
1443 dev->frame[b->index].state = F_QUEUED;
1444
1445 /* add frame to fifo */
1446 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1447 list_add_tail(&dev->frame[b->index].frame,
1448 &dev->inqueue);
1449 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1450
1451 return 0;
1452 }
1453 case VIDIOC_DQBUF:
1454 {
1455 struct v4l2_buffer *b = arg;
1456 struct em2820_frame_t *f;
1457 unsigned long lock_flags;
1458 int ret = 0;
1459
1460 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1461 || dev->io != IO_MMAP)
1462 return -EINVAL;
1463
1464 if (list_empty(&dev->outqueue)) {
1465 if (dev->stream == STREAM_OFF)
1466 return -EINVAL;
1467 if (filp->f_flags & O_NONBLOCK)
1468 return -EAGAIN;
1469 ret = wait_event_interruptible
1470 (dev->wait_frame,
1471 (!list_empty(&dev->outqueue)) ||
1472 (dev->state & DEV_DISCONNECTED));
1473 if (ret)
1474 return ret;
1475 if (dev->state & DEV_DISCONNECTED)
1476 return -ENODEV;
1477 }
1478
1479 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1480 f = list_entry(dev->outqueue.next,
1481 struct em2820_frame_t, frame);
1482 list_del(dev->outqueue.next);
1483 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1484
1485 f->state = F_UNUSED;
1486 memcpy(b, &f->buf, sizeof(*b));
1487
1488 if (f->vma_use_count)
1489 b->flags |= V4L2_BUF_FLAG_MAPPED;
1490
1491 return 0;
1492 }
1493 default:
1494 return em2820_do_ioctl(inode, filp, dev, cmd, arg,
1495 em2820_video_do_ioctl);
1496 }
1497 return 0;
1498}
1499
1500/*
1501 * em2820_v4l2_ioctl()
1502 * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
1503 */
1504static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
1505 unsigned int cmd, unsigned long arg)
1506{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001507 int ret = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001508 struct em2820 *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001509
1510 if (down_interruptible(&dev->fileop_lock))
1511 return -ERESTARTSYS;
1512
1513 if (dev->state & DEV_DISCONNECTED) {
1514 em2820_errdev("v4l2 ioctl: device not present\n");
1515 up(&dev->fileop_lock);
1516 return -ENODEV;
1517 }
1518
1519 if (dev->state & DEV_MISCONFIGURED) {
1520 em2820_errdev
1521 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1522 up(&dev->fileop_lock);
1523 return -EIO;
1524 }
1525
1526 ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
1527
1528 up(&dev->fileop_lock);
1529
1530 return ret;
1531}
1532
1533static struct file_operations em2820_v4l_fops = {
1534 .owner = THIS_MODULE,
1535 .open = em2820_v4l2_open,
1536 .release = em2820_v4l2_close,
1537 .ioctl = em2820_v4l2_ioctl,
1538 .read = em2820_v4l2_read,
1539 .poll = em2820_v4l2_poll,
1540 .mmap = em2820_v4l2_mmap,
1541 .llseek = no_llseek,
1542};
1543
1544/******************************** usb interface *****************************************/
1545
1546/*
1547 * em2820_init_dev()
1548 * allocates and inits the device structs, registers i2c bus and v4l device
1549 */
1550static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
1551 int minor, int model)
1552{
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001553 struct em2820 *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001554 int retval = -ENOMEM;
1555 int errCode, i;
1556 unsigned int maxh, maxw;
1557 struct usb_interface *uif;
1558
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001559 dev->udev = udev;
1560 dev->model = model;
1561 init_MUTEX(&dev->lock);
1562 init_waitqueue_head(&dev->open);
1563
1564 dev->em2820_write_regs = em2820_write_regs;
1565 dev->em2820_read_reg = em2820_read_reg;
1566 dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
1567 dev->em2820_write_regs_req = em2820_write_regs_req;
1568 dev->em2820_read_reg_req = em2820_read_reg_req;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001569 dev->is_em2800 = em2820_boards[model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570 dev->has_tuner = em2820_boards[model].has_tuner;
1571 dev->has_msp34xx = em2820_boards[model].has_msp34xx;
1572 dev->tda9887_conf = em2820_boards[model].tda9887_conf;
1573 dev->decoder = em2820_boards[model].decoder;
1574
1575 if (tuner >= 0)
1576 dev->tuner_type = tuner;
1577 else
1578 dev->tuner_type = em2820_boards[model].tuner_type;
1579
1580 dev->video_inputs = em2820_boards[model].vchannels;
1581
1582 for (i = 0; i < TVNORMS; i++)
1583 if (em2820_boards[model].norm == tvnorms[i].mode)
1584 break;
1585 if (i == TVNORMS)
1586 i = 0;
1587
1588 dev->tvnorm = &tvnorms[i]; /* set default norm */
1589
1590 em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1591
1592 maxw = norm_maxw(dev);
1593 maxh = norm_maxh(dev);
1594
1595 /* set default image size */
1596 dev->width = maxw;
1597 dev->height = maxh;
1598 dev->interlaced = EM2820_INTERLACED_DEFAULT;
1599 dev->field_size = dev->width * dev->height;
1600 dev->frame_size =
1601 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1602 dev->bytesperline = dev->width * 2;
1603 dev->hscale = 0;
1604 dev->vscale = 0;
1605 dev->ctl_input = 2;
1606
1607 /* setup video picture settings for saa7113h */
1608 memset(&dev->vpic, 0, sizeof(dev->vpic));
1609 dev->vpic.colour = 128 << 8;
1610 dev->vpic.hue = 128 << 8;
1611 dev->vpic.brightness = 128 << 8;
1612 dev->vpic.contrast = 192 << 8;
1613 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1614 dev->vpic.depth = 16;
1615 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1616
1617 /* compute alternate max packet sizes */
1618 uif = dev->udev->actconfig->interface[0];
1619 dev->alt_max_pkt_size[0] = 0;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001620 for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001621 u16 tmp =
1622 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1623 wMaxPacketSize);
1624 dev->alt_max_pkt_size[i] =
1625 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1626 }
1627
1628#ifdef CONFIG_MODULES
1629 /* request some modules */
1630 if (dev->decoder == EM2820_SAA7113)
1631 request_module("saa7113");
1632 if (dev->decoder == EM2820_SAA7114)
1633 request_module("saa7114");
1634 if (dev->decoder == EM2820_TVP5150)
1635 request_module("tvp5150");
1636 if (dev->has_tuner)
1637 request_module("tuner");
1638 if (dev->tda9887_conf)
1639 request_module("tda9887");
1640#endif
1641 errCode = em2820_config(dev);
1642 if (errCode) {
1643 em2820_errdev("error configuring device\n");
1644 kfree(dev);
1645 return -ENOMEM;
1646 }
1647
1648 down(&dev->lock);
1649 /* register i2c bus */
1650 em2820_i2c_register(dev);
1651
1652 /* Do board specific init and eeprom reading */
1653 em2820_card_setup(dev);
1654
1655 /* configure the device */
1656 em2820_config_i2c(dev);
1657
1658 up(&dev->lock);
1659
1660 errCode = em2820_config(dev);
1661
1662#ifdef CONFIG_MODULES
1663 if (dev->has_msp34xx)
1664 request_module("msp3400");
1665#endif
1666 /* allocate and fill v4l2 device struct */
1667 dev->vdev = video_device_alloc();
1668 if (NULL == dev->vdev) {
1669 em2820_errdev("cannot allocate video_device.\n");
1670 kfree(dev);
1671 return -ENOMEM;
1672 }
1673
1674 dev->vdev->owner = THIS_MODULE;
1675 dev->vdev->type = VID_TYPE_CAPTURE;
1676 if (dev->has_tuner)
1677 dev->vdev->type |= VID_TYPE_TUNER;
1678 dev->vdev->hardware = 0;
1679 dev->vdev->fops = &em2820_v4l_fops;
1680 dev->vdev->minor = -1;
1681 dev->vdev->dev = &dev->udev->dev;
1682 dev->vdev->release = video_device_release;
1683 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
1684 "em2820 video");
Markus Rechberger9c755412005-11-08 21:37:52 -08001685 list_add_tail(&dev->devlist,&em2820_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001686
1687 /* register v4l2 device */
1688 down(&dev->lock);
1689 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
1690 em2820_errdev("unable to register video device (error=%i).\n",
1691 retval);
1692 up(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001693 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001694 video_device_release(dev->vdev);
1695 kfree(dev);
1696 return -ENODEV;
1697 }
1698 if (dev->has_msp34xx) {
1699 /* Send a reset to other chips via gpio */
1700 em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1701 udelay(2500);
1702 em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1703 udelay(2500);
1704
1705 }
1706 video_mux(dev, 0);
1707
1708 up(&dev->lock);
1709
1710 em2820_info("V4L2 device registered as /dev/video%d\n",
1711 dev->vdev->minor);
1712
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713 return 0;
1714}
1715
1716/*
1717 * em2820_usb_probe()
1718 * checks for supported devices
1719 */
1720static int em2820_usb_probe(struct usb_interface *interface,
1721 const struct usb_device_id *id)
1722{
1723 const struct usb_endpoint_descriptor *endpoint;
1724 struct usb_device *udev;
1725 struct em2820 *dev = NULL;
1726 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001727 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728
1729 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001730 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1731
1732 em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n",
1733 udev->descriptor.idVendor,udev->descriptor.idProduct,
1734 ifnum,
1735 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001737 /* Don't register audio interfaces */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001738 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001739 return -ENODEV;
1740
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001741 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1742
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743 /* check if the the device has the iso in endpoint at the correct place */
1744 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1745 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001746 em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747 return -ENODEV;
1748 }
1749 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001750 em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001751 return -ENODEV;
1752 }
1753
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001754 model=id->driver_info;
1755 nr=interface->minor;
1756
1757 if (nr>EM2820_MAXBOARDS) {
1758 printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS);
1759 return -ENOMEM;
1760 }
1761
1762 /* allocate memory for our device state and initialize it */
1763 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1764 if (dev == NULL) {
1765 em2820_err(DRIVER_NAME ": out of memory!\n");
1766 return -ENOMEM;
1767 }
1768 memset(dev, 0, sizeof(*dev));
1769
1770 snprintf(dev->name, 29, "em2820 #%d", nr);
1771
1772 if ((card[nr]>=0)&&(card[nr]<em2820_bcount))
1773 model=card[nr];
1774
1775 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1776 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1777 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1778 "%s: workaround that. Redirect complaints to the vendor of\n"
1779 "%s: the TV card. Best regards,\n"
1780 "%s: -- tux\n",
1781 dev->name,dev->name,dev->name,dev->name,dev->name);
1782 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1783 dev->name);
1784 for (i = 0; i < em2820_bcount; i++) {
1785 printk("%s: card=%d -> %s\n",
1786 dev->name, i, em2820_boards[i].name);
1787 }
1788 }
1789
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790 /* allocate device struct */
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001791 retval = em2820_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001792 if (retval)
1793 return retval;
1794
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001795 em2820_info("Found %s\n", em2820_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001796
1797 /* save our data pointer in this interface device */
1798 usb_set_intfdata(interface, dev);
1799 return 0;
1800}
1801
1802/*
1803 * em2820_usb_disconnect()
1804 * called when the device gets diconencted
1805 * video device will be unregistered on v4l2_close in case it is still open
1806 */
1807static void em2820_usb_disconnect(struct usb_interface *interface)
1808{
1809 struct em2820 *dev = usb_get_intfdata(interface);
1810 usb_set_intfdata(interface, NULL);
1811
1812 if (!dev)
1813 return;
1814
1815 down_write(&em2820_disconnect);
1816
1817 down(&dev->lock);
1818
1819 em2820_info("disconnecting %s\n", dev->vdev->name);
1820
1821 wake_up_interruptible_all(&dev->open);
1822
1823 if (dev->users) {
1824 em2820_warn
1825 ("device /dev/video%d is open! Deregistration and memory "
1826 "deallocation are deferred on close.\n", dev->vdev->minor);
1827 dev->state |= DEV_MISCONFIGURED;
1828 em2820_uninit_isoc(dev);
1829 dev->state |= DEV_DISCONNECTED;
1830 wake_up_interruptible(&dev->wait_frame);
1831 wake_up_interruptible(&dev->wait_stream);
1832 } else {
1833 dev->state |= DEV_DISCONNECTED;
1834 em2820_release_resources(dev);
1835 }
1836
1837 up(&dev->lock);
1838
1839 if (!dev->users)
1840 kfree(dev);
1841
1842 up_write(&em2820_disconnect);
1843
1844}
1845
1846static struct usb_driver em2820_usb_driver = {
1847 .owner = THIS_MODULE,
1848 .name = "em2820",
1849 .probe = em2820_usb_probe,
1850 .disconnect = em2820_usb_disconnect,
1851 .id_table = em2820_id_table,
1852};
1853
1854static int __init em2820_module_init(void)
1855{
1856 int result;
1857
1858 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
1859 (EM2820_VERSION_CODE >> 16) & 0xff,
1860 (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
1861#ifdef SNAPSHOT
1862 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1863 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1864#endif
1865
1866 /* register this driver with the USB subsystem */
1867 result = usb_register(&em2820_usb_driver);
1868 if (result)
1869 em2820_err(DRIVER_NAME
1870 " usb_register failed. Error number %d.\n", result);
1871
1872 return result;
1873}
1874
1875static void __exit em2820_module_exit(void)
1876{
1877 /* deregister this driver with the USB subsystem */
1878 usb_deregister(&em2820_usb_driver);
1879}
1880
1881module_init(em2820_module_init);
1882module_exit(em2820_module_exit);