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