2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
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>
9 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
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.
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.
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.
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>
36 #include <linux/video_decoder.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/msp3400.h>
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>"
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)
52 #define em28xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __FUNCTION__ , ##arg); } while (0)
57 MODULE_AUTHOR(DRIVER_AUTHOR
);
58 MODULE_DESCRIPTION(DRIVER_DESC
);
59 MODULE_LICENSE("GPL");
61 static LIST_HEAD(em28xx_devlist
);
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");
73 static unsigned int video_debug
= 0;
74 module_param(video_debug
,int,0644);
75 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
77 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78 static unsigned long em28xx_devused
;
80 /* supported tv norms */
81 static struct em28xx_tvnorm tvnorms
[] = {
85 .mode
= VIDEO_MODE_PAL
,
89 .mode
= VIDEO_MODE_NTSC
,
93 .mode
= VIDEO_MODE_SECAM
,
97 .mode
= VIDEO_MODE_PAL
,
101 #define TVNORMS ARRAY_SIZE(tvnorms)
103 /* supported controls */
104 /* Common to all boards */
105 static struct v4l2_queryctrl em28xx_qctrl
[] = {
107 .id
= V4L2_CID_AUDIO_VOLUME
,
108 .type
= V4L2_CTRL_TYPE_INTEGER
,
113 .default_value
= 0x1f,
116 .id
= V4L2_CID_AUDIO_MUTE
,
117 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
127 static struct usb_driver em28xx_usb_driver
;
129 static DEFINE_MUTEX(em28xx_sysfs_lock
);
130 static DECLARE_RWSEM(em28xx_disconnect
);
132 /********************* v4l2 interface ******************************************/
136 * inits registers with sane defaults
138 static int em28xx_config(struct em28xx
*dev
)
141 /* Sets I2C speed to 100 KHz */
143 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
145 /* enable vbi capturing */
147 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
148 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
149 em28xx_write_regs_req(dev
,0x00,0x11,"\x51",1);
151 em28xx_audio_usb_mute(dev
, 1);
152 dev
->mute
= 1; /* maybe not the right place... */
154 em28xx_audio_analog_set(dev
);
155 em28xx_audio_analog_setup(dev
);
156 em28xx_outfmt_set_yuv422(dev
);
157 em28xx_colorlevels_set_default(dev
);
158 em28xx_compression_disable(dev
);
164 * em28xx_config_i2c()
165 * configure i2c attached devices
167 static void em28xx_config_i2c(struct em28xx
*dev
)
169 struct v4l2_routing route
;
171 route
.input
= INPUT(dev
->ctl_input
)->vmux
;
173 em28xx_i2c_call_clients(dev
, VIDIOC_INT_RESET
, NULL
);
174 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
175 em28xx_i2c_call_clients(dev
, VIDIOC_STREAMON
, NULL
);
179 * em28xx_empty_framequeues()
180 * prepare queues for incoming and outgoing frames
182 static void em28xx_empty_framequeues(struct em28xx
*dev
)
186 INIT_LIST_HEAD(&dev
->inqueue
);
187 INIT_LIST_HEAD(&dev
->outqueue
);
189 for (i
= 0; i
< EM28XX_NUM_FRAMES
; i
++) {
190 dev
->frame
[i
].state
= F_UNUSED
;
191 dev
->frame
[i
].buf
.bytesused
= 0;
195 static void video_mux(struct em28xx
*dev
, int index
)
198 struct v4l2_routing route
;
200 route
.input
= INPUT(index
)->vmux
;
202 dev
->ctl_input
= index
;
203 dev
->ctl_ainput
= INPUT(index
)->amux
;
205 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
207 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index
,route
.input
,dev
->ctl_ainput
);
209 if (dev
->has_msp34xx
) {
211 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
, &dev
->i2s_speed
);
212 route
.input
= dev
->ctl_ainput
;
213 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
214 /* Note: this is msp3400 specific */
215 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
216 ainput
= EM28XX_AUDIO_SRC_TUNER
;
217 em28xx_audio_source(dev
, ainput
);
219 switch (dev
->ctl_ainput
) {
221 ainput
= EM28XX_AUDIO_SRC_TUNER
;
224 ainput
= EM28XX_AUDIO_SRC_LINE
;
226 em28xx_audio_source(dev
, ainput
);
232 * inits the device and starts isoc transfer
234 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
236 int minor
= iminor(inode
);
238 struct em28xx
*h
,*dev
= NULL
;
240 list_for_each_entry(h
, &em28xx_devlist
, devlist
) {
241 if (h
->vdev
->minor
== minor
) {
243 dev
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
245 if (h
->vbi_dev
->minor
== minor
) {
247 dev
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
253 em28xx_videodbg("open minor=%d type=%s users=%d\n",
254 minor
,v4l2_type_names
[dev
->type
],dev
->users
);
256 if (!down_read_trylock(&em28xx_disconnect
))
260 em28xx_warn("this driver can be opened only once\n");
261 up_read(&em28xx_disconnect
);
265 mutex_init(&dev
->fileop_lock
); /* to 1 == available */
266 spin_lock_init(&dev
->queue_lock
);
267 init_waitqueue_head(&dev
->wait_frame
);
268 init_waitqueue_head(&dev
->wait_stream
);
270 mutex_lock(&dev
->lock
);
272 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
273 em28xx_set_alternate(dev
);
275 dev
->width
= norm_maxw(dev
);
276 dev
->height
= norm_maxh(dev
);
277 dev
->frame_size
= dev
->width
* dev
->height
* 2;
278 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
279 dev
->bytesperline
= dev
->width
* 2;
283 em28xx_capture_start(dev
, 1);
284 em28xx_resolution_set(dev
);
286 /* device needs to be initialized before isoc transfer */
289 /* start the transfer */
290 errCode
= em28xx_init_isoc(dev
);
297 filp
->private_data
= dev
;
299 dev
->stream
= STREAM_OFF
;
303 em28xx_empty_framequeues(dev
);
305 dev
->state
|= DEV_INITIALIZED
;
308 mutex_unlock(&dev
->lock
);
309 up_read(&em28xx_disconnect
);
314 * em28xx_realease_resources()
315 * unregisters the v4l2,i2c and usb devices
316 * called when the device gets disconected or at module unload
318 static void em28xx_release_resources(struct em28xx
*dev
)
320 mutex_lock(&em28xx_sysfs_lock
);
322 /*FIXME: I2C IR should be disconnected */
324 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
325 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
326 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
327 list_del(&dev
->devlist
);
328 video_unregister_device(dev
->vdev
);
329 video_unregister_device(dev
->vbi_dev
);
330 em28xx_i2c_unregister(dev
);
331 usb_put_dev(dev
->udev
);
332 mutex_unlock(&em28xx_sysfs_lock
);
335 /* Mark device as unused */
336 em28xx_devused
&=~(1<<dev
->devno
);
340 * em28xx_v4l2_close()
341 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
343 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
346 struct em28xx
*dev
=filp
->private_data
;
348 em28xx_videodbg("users=%d\n", dev
->users
);
350 mutex_lock(&dev
->lock
);
352 em28xx_uninit_isoc(dev
);
354 em28xx_release_buffers(dev
);
356 /* the device is already disconnect, free the remaining resources */
357 if (dev
->state
& DEV_DISCONNECTED
) {
358 em28xx_release_resources(dev
);
359 mutex_unlock(&dev
->lock
);
364 /* set alternate 0 */
366 em28xx_videodbg("setting alternate 0\n");
367 errCode
= usb_set_interface(dev
->udev
, 0, 0);
369 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
374 wake_up_interruptible_nr(&dev
->open
, 1);
375 mutex_unlock(&dev
->lock
);
381 * will allocate buffers when called for the first time
384 em28xx_v4l2_read(struct file
*filp
, char __user
* buf
, size_t count
,
387 struct em28xx_frame_t
*f
, *i
;
388 unsigned long lock_flags
;
390 struct em28xx
*dev
= filp
->private_data
;
392 if (dev
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
393 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
395 if (dev
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
396 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
397 em28xx_videodbg("not supported yet! ...\n");
398 if (copy_to_user(buf
, "", 1)) {
399 mutex_unlock(&dev
->fileop_lock
);
404 if (dev
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
405 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
406 em28xx_videodbg("not supported yet! ...\n");
407 if (copy_to_user(buf
, "", 1)) {
408 mutex_unlock(&dev
->fileop_lock
);
414 if (mutex_lock_interruptible(&dev
->fileop_lock
))
417 if (dev
->state
& DEV_DISCONNECTED
) {
418 em28xx_videodbg("device not present\n");
419 mutex_unlock(&dev
->fileop_lock
);
423 if (dev
->state
& DEV_MISCONFIGURED
) {
424 em28xx_videodbg("device misconfigured; close and open it again\n");
425 mutex_unlock(&dev
->fileop_lock
);
429 if (dev
->io
== IO_MMAP
) {
430 em28xx_videodbg ("IO method is set to mmap; close and open"
431 " the device again to choose the read method\n");
432 mutex_unlock(&dev
->fileop_lock
);
436 if (dev
->io
== IO_NONE
) {
437 if (!em28xx_request_buffers(dev
, EM28XX_NUM_READ_FRAMES
)) {
438 em28xx_errdev("read failed, not enough memory\n");
439 mutex_unlock(&dev
->fileop_lock
);
443 dev
->stream
= STREAM_ON
;
444 em28xx_queue_unusedframes(dev
);
448 mutex_unlock(&dev
->fileop_lock
);
452 if (list_empty(&dev
->outqueue
)) {
453 if (filp
->f_flags
& O_NONBLOCK
) {
454 mutex_unlock(&dev
->fileop_lock
);
457 ret
= wait_event_interruptible
459 (!list_empty(&dev
->outqueue
)) ||
460 (dev
->state
& DEV_DISCONNECTED
));
462 mutex_unlock(&dev
->fileop_lock
);
465 if (dev
->state
& DEV_DISCONNECTED
) {
466 mutex_unlock(&dev
->fileop_lock
);
471 f
= list_entry(dev
->outqueue
.prev
, struct em28xx_frame_t
, frame
);
473 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
474 list_for_each_entry(i
, &dev
->outqueue
, frame
)
476 INIT_LIST_HEAD(&dev
->outqueue
);
477 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
479 em28xx_queue_unusedframes(dev
);
481 if (count
> f
->buf
.length
)
482 count
= f
->buf
.length
;
484 if (copy_to_user(buf
, f
->bufmem
, count
)) {
485 mutex_unlock(&dev
->fileop_lock
);
490 mutex_unlock(&dev
->fileop_lock
);
497 * will allocate buffers when called for the first time
499 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
501 unsigned int mask
= 0;
502 struct em28xx
*dev
= filp
->private_data
;
504 if (mutex_lock_interruptible(&dev
->fileop_lock
))
507 if (dev
->state
& DEV_DISCONNECTED
) {
508 em28xx_videodbg("device not present\n");
509 } else if (dev
->state
& DEV_MISCONFIGURED
) {
510 em28xx_videodbg("device is misconfigured; close and open it again\n");
512 if (dev
->io
== IO_NONE
) {
513 if (!em28xx_request_buffers
514 (dev
, EM28XX_NUM_READ_FRAMES
)) {
516 ("poll() failed, not enough memory\n");
519 dev
->stream
= STREAM_ON
;
523 if (dev
->io
== IO_READ
) {
524 em28xx_queue_unusedframes(dev
);
525 poll_wait(filp
, &dev
->wait_frame
, wait
);
527 if (!list_empty(&dev
->outqueue
))
528 mask
|= POLLIN
| POLLRDNORM
;
530 mutex_unlock(&dev
->fileop_lock
);
536 mutex_unlock(&dev
->fileop_lock
);
543 static void em28xx_vm_open(struct vm_area_struct
*vma
)
545 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
552 static void em28xx_vm_close(struct vm_area_struct
*vma
)
554 /* NOTE: buffers are not freed here */
555 struct em28xx_frame_t
*f
= vma
->vm_private_data
;
557 if (f
->vma_use_count
)
561 static struct vm_operations_struct em28xx_vm_ops
= {
562 .open
= em28xx_vm_open
,
563 .close
= em28xx_vm_close
,
569 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
571 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
572 start
= vma
->vm_start
;
576 struct em28xx
*dev
= filp
->private_data
;
578 if (mutex_lock_interruptible(&dev
->fileop_lock
))
581 if (dev
->state
& DEV_DISCONNECTED
) {
582 em28xx_videodbg("mmap: device not present\n");
583 mutex_unlock(&dev
->fileop_lock
);
587 if (dev
->state
& DEV_MISCONFIGURED
) {
588 em28xx_videodbg ("mmap: Device is misconfigured; close and "
590 mutex_unlock(&dev
->fileop_lock
);
594 if (dev
->io
!= IO_MMAP
|| !(vma
->vm_flags
& VM_WRITE
) ||
595 size
!= PAGE_ALIGN(dev
->frame
[0].buf
.length
)) {
596 mutex_unlock(&dev
->fileop_lock
);
600 for (i
= 0; i
< dev
->num_frames
; i
++) {
601 if ((dev
->frame
[i
].buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
604 if (i
== dev
->num_frames
) {
605 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
606 mutex_unlock(&dev
->fileop_lock
);
610 /* VM_IO is eventually going to replace PageReserved altogether */
611 vma
->vm_flags
|= VM_IO
;
612 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
614 pos
= dev
->frame
[i
].bufmem
;
615 while (size
> 0) { /* size is page-aligned */
616 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
617 em28xx_videodbg("mmap: vm_insert_page failed\n");
618 mutex_unlock(&dev
->fileop_lock
);
626 vma
->vm_ops
= &em28xx_vm_ops
;
627 vma
->vm_private_data
= &dev
->frame
[i
];
630 mutex_unlock(&dev
->fileop_lock
);
636 * return the current saturation, brightness or contrast, mute state
638 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
641 case V4L2_CID_AUDIO_MUTE
:
642 ctrl
->value
= dev
->mute
;
644 case V4L2_CID_AUDIO_VOLUME
:
645 ctrl
->value
= dev
->volume
;
654 * mute or set new saturation, brightness or contrast
656 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
659 case V4L2_CID_AUDIO_MUTE
:
660 if (ctrl
->value
!= dev
->mute
) {
661 dev
->mute
= ctrl
->value
;
662 em28xx_audio_usb_mute(dev
, ctrl
->value
);
663 return em28xx_audio_analog_set(dev
);
666 case V4L2_CID_AUDIO_VOLUME
:
667 dev
->volume
= ctrl
->value
;
668 return em28xx_audio_analog_set(dev
);
675 * em28xx_stream_interrupt()
678 static int em28xx_stream_interrupt(struct em28xx
*dev
)
682 /* stop reading from the device */
684 dev
->stream
= STREAM_INTERRUPT
;
685 ret
= wait_event_timeout(dev
->wait_stream
,
686 (dev
->stream
== STREAM_OFF
) ||
687 (dev
->state
& DEV_DISCONNECTED
),
689 if (dev
->state
& DEV_DISCONNECTED
)
692 dev
->state
|= DEV_MISCONFIGURED
;
693 em28xx_videodbg("device is misconfigured; close and "
694 "open /dev/video%d again\n",
695 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
702 static int em28xx_set_norm(struct em28xx
*dev
, int width
, int height
)
704 unsigned int hscale
, vscale
;
705 unsigned int maxh
, maxw
;
707 maxw
= norm_maxw(dev
);
708 maxh
= norm_maxh(dev
);
710 /* width must even because of the YUYV format */
711 /* height must be even because of interlacing */
724 if ((hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L) >= 0x4000)
726 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
728 if ((vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L) >= 0x4000)
730 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
732 /* set new image size */
734 dev
->height
= height
;
735 dev
->frame_size
= dev
->width
* dev
->height
* 2;
736 dev
->field_size
= dev
->frame_size
>> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
737 dev
->bytesperline
= dev
->width
* 2;
738 dev
->hscale
= hscale
;
739 dev
->vscale
= vscale
;
741 em28xx_resolution_set(dev
);
746 static int em28xx_get_fmt(struct em28xx
*dev
, struct v4l2_format
*format
)
748 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
749 (format
->type
==V4L2_BUF_TYPE_VIDEO_CAPTURE
) ?
750 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
751 (format
->type
==V4L2_BUF_TYPE_VBI_CAPTURE
) ?
752 "V4L2_BUF_TYPE_VBI_CAPTURE" :
753 (format
->type
==V4L2_CAP_SLICED_VBI_CAPTURE
) ?
754 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
757 switch (format
->type
) {
758 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
760 format
->fmt
.pix
.width
= dev
->width
;
761 format
->fmt
.pix
.height
= dev
->height
;
762 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
763 format
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
764 format
->fmt
.pix
.sizeimage
= dev
->frame_size
;
765 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
766 format
->fmt
.pix
.field
= dev
->interlaced
? V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
768 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev
->width
,
773 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
:
775 format
->fmt
.sliced
.service_set
=0;
777 em28xx_i2c_call_clients(dev
,VIDIOC_G_FMT
,format
);
779 if (format
->fmt
.sliced
.service_set
==0)
791 static int em28xx_set_fmt(struct em28xx
*dev
, unsigned int cmd
, struct v4l2_format
*format
)
795 int width
= format
->fmt
.pix
.width
;
796 int height
= format
->fmt
.pix
.height
;
797 unsigned int hscale
, vscale
;
798 unsigned int maxh
, maxw
;
800 maxw
= norm_maxw(dev
);
801 maxh
= norm_maxh(dev
);
803 em28xx_videodbg("%s: type=%s\n",
804 cmd
== VIDIOC_TRY_FMT
?
805 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
806 format
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
?
807 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
808 format
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
?
809 "V4L2_BUF_TYPE_VBI_CAPTURE " :
812 if (format
->type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
813 em28xx_i2c_call_clients(dev
,VIDIOC_G_FMT
,format
);
815 if (format
->fmt
.sliced
.service_set
==0)
822 if (format
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
825 em28xx_videodbg("%s: requested %dx%d\n",
826 cmd
== VIDIOC_TRY_FMT
?
827 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
828 format
->fmt
.pix
.width
, format
->fmt
.pix
.height
);
830 /* FIXME: Move some code away from here */
831 /* width must even because of the YUYV format */
832 /* height must be even because of interlacing */
846 /* the em2800 can only scale down to 50% */
847 if(height
% (maxh
/ 2))
849 if(width
% (maxw
/ 2))
851 /* according to empiatech support */
852 /* the MaxPacketSize is to small to support */
853 /* framesizes larger than 640x480 @ 30 fps */
854 /* or 640x576 @ 25 fps. As this would cut */
855 /* of a part of the image we prefer */
856 /* 360x576 or 360x480 for now */
857 if(width
== maxw
&& height
== maxh
)
861 if ((hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L) >= 0x4000)
864 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
866 if ((vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L) >= 0x4000)
869 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
871 format
->fmt
.pix
.width
= width
;
872 format
->fmt
.pix
.height
= height
;
873 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
874 format
->fmt
.pix
.bytesperline
= width
* 2;
875 format
->fmt
.pix
.sizeimage
= width
* 2 * height
;
876 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
877 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
879 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
880 cmd
== VIDIOC_TRY_FMT
?
881 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
882 format
->fmt
.pix
.width
, format
->fmt
.pix
.height
, hscale
, vscale
);
884 if (cmd
== VIDIOC_TRY_FMT
)
887 for (i
= 0; i
< dev
->num_frames
; i
++)
888 if (dev
->frame
[i
].vma_use_count
) {
889 em28xx_videodbg("VIDIOC_S_FMT failed. "
890 "Unmap the buffers first.\n");
894 /* stop io in case it is already in progress */
895 if (dev
->stream
== STREAM_ON
) {
896 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
897 if ((ret
= em28xx_stream_interrupt(dev
)))
901 em28xx_release_buffers(dev
);
904 /* set new image size */
906 dev
->height
= height
;
907 dev
->frame_size
= dev
->width
* dev
->height
* 2;
908 dev
->field_size
= dev
->frame_size
>> 1;
909 dev
->bytesperline
= dev
->width
* 2;
910 dev
->hscale
= hscale
;
911 dev
->vscale
= vscale
;
912 em28xx_uninit_isoc(dev
);
913 em28xx_set_alternate(dev
);
914 em28xx_capture_start(dev
, 1);
915 em28xx_resolution_set(dev
);
916 em28xx_init_isoc(dev
);
922 * em28xx_v4l2_do_ioctl()
923 * This function is _not_ called directly, but from
924 * em28xx_v4l2_ioctl. Userspace
925 * copying is done already, arg is a kernel pointer.
927 static int em28xx_do_ioctl(struct inode
*inode
, struct file
*filp
,
928 struct em28xx
*dev
, unsigned int cmd
, void *arg
,
929 v4l2_kioctl driver_ioctl
)
934 /* ---------- tv norms ---------- */
937 struct v4l2_standard
*e
= arg
;
943 ret
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
944 tvnorms
[e
->index
].name
);
952 v4l2_std_id
*id
= arg
;
954 *id
= dev
->tvnorm
->id
;
959 v4l2_std_id
*id
= arg
;
962 for (i
= 0; i
< TVNORMS
; i
++)
963 if (*id
== tvnorms
[i
].id
)
966 for (i
= 0; i
< TVNORMS
; i
++)
967 if (*id
& tvnorms
[i
].id
)
972 mutex_lock(&dev
->lock
);
973 dev
->tvnorm
= &tvnorms
[i
];
975 em28xx_set_norm(dev
, dev
->width
, dev
->height
);
977 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
,
980 mutex_unlock(&dev
->lock
);
985 /* ------ input switching ---------- */
986 case VIDIOC_ENUMINPUT
:
988 struct v4l2_input
*i
= arg
;
990 static const char *iname
[] = {
991 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
992 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
993 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
994 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
995 [EM28XX_VMUX_SVIDEO
] = "S-Video",
996 [EM28XX_VMUX_TELEVISION
] = "Television",
997 [EM28XX_VMUX_CABLE
] = "Cable TV",
998 [EM28XX_VMUX_DVB
] = "DVB",
999 [EM28XX_VMUX_DEBUG
] = "for debug only",
1003 if (n
>= MAX_EM28XX_INPUT
)
1005 if (0 == INPUT(n
)->type
)
1007 memset(i
, 0, sizeof(*i
));
1009 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1010 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1011 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1012 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
1013 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1014 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1015 i
->std
|= tvnorms
[n
].id
;
1018 case VIDIOC_G_INPUT
:
1021 *i
= dev
->ctl_input
;
1025 case VIDIOC_S_INPUT
:
1029 if (*index
>= MAX_EM28XX_INPUT
)
1031 if (0 == INPUT(*index
)->type
)
1034 mutex_lock(&dev
->lock
);
1035 video_mux(dev
, *index
);
1036 mutex_unlock(&dev
->lock
);
1040 case VIDIOC_G_AUDIO
:
1042 struct v4l2_audio
*a
= arg
;
1043 unsigned int index
= a
->index
;
1047 memset(a
, 0, sizeof(*a
));
1048 index
= dev
->ctl_ainput
;
1051 strcpy(a
->name
, "Television");
1053 strcpy(a
->name
, "Line In");
1055 a
->capability
= V4L2_AUDCAP_STEREO
;
1059 case VIDIOC_S_AUDIO
:
1061 struct v4l2_audio
*a
= arg
;
1063 if (a
->index
!= dev
->ctl_ainput
)
1069 /* --- controls ---------------------------------------------- */
1070 case VIDIOC_QUERYCTRL
:
1072 struct v4l2_queryctrl
*qc
= arg
;
1075 memset(qc
,0,sizeof(*qc
));
1078 if (!dev
->has_msp34xx
) {
1079 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1080 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1081 memcpy(qc
, &(em28xx_qctrl
[i
]),
1087 em28xx_i2c_call_clients(dev
,cmd
,qc
);
1095 struct v4l2_control
*ctrl
= arg
;
1098 if (!dev
->has_msp34xx
)
1099 retval
=em28xx_get_ctrl(dev
, ctrl
);
1100 if (retval
==-EINVAL
) {
1101 em28xx_i2c_call_clients(dev
,cmd
,arg
);
1103 } else return retval
;
1107 struct v4l2_control
*ctrl
= arg
;
1110 if (!dev
->has_msp34xx
){
1111 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1112 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
1114 em28xx_qctrl
[i
].minimum
1116 em28xx_qctrl
[i
].maximum
)
1118 return em28xx_set_ctrl(dev
, ctrl
);
1123 em28xx_i2c_call_clients(dev
,cmd
,arg
);
1126 /* --- tuner ioctls ------------------------------------------ */
1127 case VIDIOC_G_TUNER
:
1129 struct v4l2_tuner
*t
= arg
;
1134 memset(t
, 0, sizeof(*t
));
1135 strcpy(t
->name
, "Tuner");
1136 mutex_lock(&dev
->lock
);
1137 /* let clients fill in the remainder of this struct */
1138 em28xx_i2c_call_clients(dev
, cmd
, t
);
1139 mutex_unlock(&dev
->lock
);
1140 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t
->signal
,
1144 case VIDIOC_S_TUNER
:
1146 struct v4l2_tuner
*t
= arg
;
1150 mutex_lock(&dev
->lock
);
1151 /* let clients handle this */
1152 em28xx_i2c_call_clients(dev
, cmd
, t
);
1153 mutex_unlock(&dev
->lock
);
1156 case VIDIOC_G_FREQUENCY
:
1158 struct v4l2_frequency
*f
= arg
;
1160 memset(f
, 0, sizeof(*f
));
1161 f
->type
= V4L2_TUNER_ANALOG_TV
;
1162 f
->frequency
= dev
->ctl_freq
;
1166 case VIDIOC_S_FREQUENCY
:
1168 struct v4l2_frequency
*f
= arg
;
1173 if (V4L2_TUNER_ANALOG_TV
!= f
->type
)
1176 mutex_lock(&dev
->lock
);
1177 dev
->ctl_freq
= f
->frequency
;
1178 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
1179 mutex_unlock(&dev
->lock
);
1182 case VIDIOC_CROPCAP
:
1184 struct v4l2_cropcap
*cc
= arg
;
1186 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1188 cc
->bounds
.left
= 0;
1190 cc
->bounds
.width
= dev
->width
;
1191 cc
->bounds
.height
= dev
->height
;
1192 cc
->defrect
= cc
->bounds
;
1193 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1194 cc
->pixelaspect
.denominator
= 59;
1197 case VIDIOC_STREAMON
:
1201 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1202 || dev
->io
!= IO_MMAP
)
1205 if (list_empty(&dev
->inqueue
))
1208 dev
->stream
= STREAM_ON
; /* FIXME: Start video capture here? */
1210 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
1214 case VIDIOC_STREAMOFF
:
1219 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1220 || dev
->io
!= IO_MMAP
)
1223 if (dev
->stream
== STREAM_ON
) {
1224 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1225 if ((ret
= em28xx_stream_interrupt(dev
)))
1228 em28xx_empty_framequeues(dev
);
1233 return v4l_compat_translate_ioctl(inode
, filp
, cmd
, arg
,
1240 * em28xx_v4l2_do_ioctl()
1241 * This function is _not_ called directly, but from
1242 * em28xx_v4l2_ioctl. Userspace
1243 * copying is done already, arg is a kernel pointer.
1245 static int em28xx_video_do_ioctl(struct inode
*inode
, struct file
*filp
,
1246 unsigned int cmd
, void *arg
)
1248 struct em28xx
*dev
= filp
->private_data
;
1253 if (video_debug
> 1)
1254 v4l_print_ioctl(dev
->name
,cmd
);
1258 /* --- capabilities ------------------------------------------ */
1259 case VIDIOC_QUERYCAP
:
1261 struct v4l2_capability
*cap
= arg
;
1263 memset(cap
, 0, sizeof(*cap
));
1264 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1265 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
,
1267 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
,
1268 sizeof(cap
->bus_info
));
1269 cap
->version
= EM28XX_VERSION_CODE
;
1271 V4L2_CAP_SLICED_VBI_CAPTURE
|
1272 V4L2_CAP_VIDEO_CAPTURE
|
1274 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1276 cap
->capabilities
|= V4L2_CAP_TUNER
;
1279 /* --- capture ioctls ---------------------------------------- */
1280 case VIDIOC_ENUM_FMT
:
1282 struct v4l2_fmtdesc
*fmtd
= arg
;
1284 if (fmtd
->index
!= 0)
1286 memset(fmtd
, 0, sizeof(*fmtd
));
1287 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1288 strcpy(fmtd
->description
, "Packed YUY2");
1289 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
1290 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
1294 return em28xx_get_fmt(dev
, (struct v4l2_format
*) arg
);
1296 case VIDIOC_TRY_FMT
:
1298 return em28xx_set_fmt(dev
, cmd
, (struct v4l2_format
*)arg
);
1300 case VIDIOC_REQBUFS
:
1302 struct v4l2_requestbuffers
*rb
= arg
;
1306 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1307 rb
->memory
!= V4L2_MEMORY_MMAP
)
1310 if (dev
->io
== IO_READ
) {
1311 em28xx_videodbg ("method is set to read;"
1312 " close and open the device again to"
1313 " choose the mmap I/O method\n");
1317 for (i
= 0; i
< dev
->num_frames
; i
++)
1318 if (dev
->frame
[i
].vma_use_count
) {
1319 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1323 if (dev
->stream
== STREAM_ON
) {
1324 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1325 if ((ret
= em28xx_stream_interrupt(dev
)))
1329 em28xx_empty_framequeues(dev
);
1331 em28xx_release_buffers(dev
);
1334 em28xx_request_buffers(dev
, rb
->count
);
1336 dev
->frame_current
= NULL
;
1338 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1340 dev
->io
= rb
->count
? IO_MMAP
: IO_NONE
;
1343 case VIDIOC_QUERYBUF
:
1345 struct v4l2_buffer
*b
= arg
;
1347 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1348 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
)
1351 memcpy(b
, &dev
->frame
[b
->index
].buf
, sizeof(*b
));
1353 if (dev
->frame
[b
->index
].vma_use_count
) {
1354 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1356 if (dev
->frame
[b
->index
].state
== F_DONE
)
1357 b
->flags
|= V4L2_BUF_FLAG_DONE
;
1358 else if (dev
->frame
[b
->index
].state
!= F_UNUSED
)
1359 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1364 struct v4l2_buffer
*b
= arg
;
1365 unsigned long lock_flags
;
1367 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1368 b
->index
>= dev
->num_frames
|| dev
->io
!= IO_MMAP
) {
1372 if (dev
->frame
[b
->index
].state
!= F_UNUSED
) {
1375 dev
->frame
[b
->index
].state
= F_QUEUED
;
1377 /* add frame to fifo */
1378 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1379 list_add_tail(&dev
->frame
[b
->index
].frame
,
1381 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1387 struct v4l2_buffer
*b
= arg
;
1388 struct em28xx_frame_t
*f
;
1389 unsigned long lock_flags
;
1392 if (b
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1393 || dev
->io
!= IO_MMAP
)
1396 if (list_empty(&dev
->outqueue
)) {
1397 if (dev
->stream
== STREAM_OFF
)
1399 if (filp
->f_flags
& O_NONBLOCK
)
1401 ret
= wait_event_interruptible
1403 (!list_empty(&dev
->outqueue
)) ||
1404 (dev
->state
& DEV_DISCONNECTED
));
1407 if (dev
->state
& DEV_DISCONNECTED
)
1411 spin_lock_irqsave(&dev
->queue_lock
, lock_flags
);
1412 f
= list_entry(dev
->outqueue
.next
,
1413 struct em28xx_frame_t
, frame
);
1414 list_del(dev
->outqueue
.next
);
1415 spin_unlock_irqrestore(&dev
->queue_lock
, lock_flags
);
1417 f
->state
= F_UNUSED
;
1418 memcpy(b
, &f
->buf
, sizeof(*b
));
1420 if (f
->vma_use_count
)
1421 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
1426 return em28xx_do_ioctl(inode
, filp
, dev
, cmd
, arg
,
1427 em28xx_video_do_ioctl
);
1433 * em28xx_v4l2_ioctl()
1434 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
1436 static int em28xx_v4l2_ioctl(struct inode
*inode
, struct file
*filp
,
1437 unsigned int cmd
, unsigned long arg
)
1440 struct em28xx
*dev
= filp
->private_data
;
1442 if (mutex_lock_interruptible(&dev
->fileop_lock
))
1443 return -ERESTARTSYS
;
1445 if (dev
->state
& DEV_DISCONNECTED
) {
1446 em28xx_errdev("v4l2 ioctl: device not present\n");
1447 mutex_unlock(&dev
->fileop_lock
);
1451 if (dev
->state
& DEV_MISCONFIGURED
) {
1453 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1454 mutex_unlock(&dev
->fileop_lock
);
1458 ret
= video_usercopy(inode
, filp
, cmd
, arg
, em28xx_video_do_ioctl
);
1460 mutex_unlock(&dev
->fileop_lock
);
1465 static const struct file_operations em28xx_v4l_fops
= {
1466 .owner
= THIS_MODULE
,
1467 .open
= em28xx_v4l2_open
,
1468 .release
= em28xx_v4l2_close
,
1469 .ioctl
= em28xx_v4l2_ioctl
,
1470 .read
= em28xx_v4l2_read
,
1471 .poll
= em28xx_v4l2_poll
,
1472 .mmap
= em28xx_v4l2_mmap
,
1473 .llseek
= no_llseek
,
1474 .compat_ioctl
= v4l_compat_ioctl32
,
1478 /******************************** usb interface *****************************************/
1482 * allocates and inits the device structs, registers i2c bus and v4l device
1484 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1487 struct em28xx
*dev
= *devhandle
;
1488 int retval
= -ENOMEM
;
1490 unsigned int maxh
, maxw
;
1493 mutex_init(&dev
->lock
);
1494 init_waitqueue_head(&dev
->open
);
1496 dev
->em28xx_write_regs
= em28xx_write_regs
;
1497 dev
->em28xx_read_reg
= em28xx_read_reg
;
1498 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1499 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1500 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1502 /* setup video picture settings for saa7113h */
1503 memset(&dev
->vpic
, 0, sizeof(dev
->vpic
));
1504 dev
->vpic
.colour
= 128 << 8;
1505 dev
->vpic
.hue
= 128 << 8;
1506 dev
->vpic
.brightness
= 128 << 8;
1507 dev
->vpic
.contrast
= 192 << 8;
1508 dev
->vpic
.whiteness
= 128 << 8; /* This one isn't used */
1509 dev
->vpic
.depth
= 16;
1510 dev
->vpic
.palette
= VIDEO_PALETTE_YUV422
;
1512 em28xx_pre_card_setup(dev
);
1514 errCode
= em28xx_config(dev
);
1516 em28xx_errdev("error configuring device\n");
1517 em28xx_devused
&= ~(1<<dev
->devno
);
1522 mutex_lock(&dev
->lock
);
1524 /* register i2c bus */
1525 em28xx_i2c_register(dev
);
1527 /* Do board specific init and eeprom reading */
1528 em28xx_card_setup(dev
);
1530 /* configure the device */
1531 em28xx_config_i2c(dev
);
1533 mutex_unlock(&dev
->lock
);
1535 for (i
= 0; i
< TVNORMS
; i
++)
1536 if (em28xx_boards
[dev
->model
].norm
== tvnorms
[i
].mode
)
1541 dev
->tvnorm
= &tvnorms
[i
]; /* set default norm */
1543 em28xx_videodbg("tvnorm=%s\n", dev
->tvnorm
->name
);
1545 maxw
= norm_maxw(dev
);
1546 maxh
= norm_maxh(dev
);
1548 /* set default image size */
1551 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1552 dev
->field_size
= dev
->width
* dev
->height
;
1554 dev
->interlaced
? dev
->field_size
<< 1 : dev
->field_size
;
1555 dev
->bytesperline
= dev
->width
* 2;
1560 errCode
= em28xx_config(dev
);
1562 /* allocate and fill v4l2 device struct */
1563 dev
->vdev
= video_device_alloc();
1564 if (NULL
== dev
->vdev
) {
1565 em28xx_errdev("cannot allocate video_device.\n");
1566 em28xx_devused
&=~(1<<dev
->devno
);
1571 dev
->vbi_dev
= video_device_alloc();
1572 if (NULL
== dev
->vbi_dev
) {
1573 em28xx_errdev("cannot allocate video_device.\n");
1575 em28xx_devused
&=~(1<<dev
->devno
);
1580 /* Fills VBI device info */
1581 dev
->vbi_dev
->type
= VFL_TYPE_VBI
;
1582 dev
->vbi_dev
->fops
= &em28xx_v4l_fops
;
1583 dev
->vbi_dev
->minor
= -1;
1584 dev
->vbi_dev
->dev
= &dev
->udev
->dev
;
1585 dev
->vbi_dev
->release
= video_device_release
;
1586 snprintf(dev
->vbi_dev
->name
, sizeof(dev
->vbi_dev
->name
), "%s#%d %s",
1587 "em28xx",dev
->devno
,"vbi");
1589 /* Fills CAPTURE device info */
1590 dev
->vdev
->type
= VID_TYPE_CAPTURE
;
1592 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1593 dev
->vdev
->fops
= &em28xx_v4l_fops
;
1594 dev
->vdev
->minor
= -1;
1595 dev
->vdev
->dev
= &dev
->udev
->dev
;
1596 dev
->vdev
->release
= video_device_release
;
1597 snprintf(dev
->vdev
->name
, sizeof(dev
->vbi_dev
->name
), "%s#%d %s",
1598 "em28xx",dev
->devno
,"video");
1600 list_add_tail(&dev
->devlist
,&em28xx_devlist
);
1602 /* register v4l2 device */
1603 mutex_lock(&dev
->lock
);
1604 if ((retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1605 video_nr
[dev
->devno
]))) {
1606 em28xx_errdev("unable to register video device (error=%i).\n",
1608 mutex_unlock(&dev
->lock
);
1609 list_del(&dev
->devlist
);
1610 video_device_release(dev
->vdev
);
1611 em28xx_devused
&=~(1<<dev
->devno
);
1616 if (video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1617 vbi_nr
[dev
->devno
]) < 0) {
1618 printk("unable to register vbi device\n");
1619 mutex_unlock(&dev
->lock
);
1620 list_del(&dev
->devlist
);
1621 video_device_release(dev
->vbi_dev
);
1622 video_device_release(dev
->vdev
);
1623 em28xx_devused
&=~(1<<dev
->devno
);
1627 printk("registered VBI\n");
1630 if (dev
->has_msp34xx
) {
1631 /* Send a reset to other chips via gpio */
1632 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
1634 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
1640 mutex_unlock(&dev
->lock
);
1642 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1643 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1644 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1650 * em28xx_usb_probe()
1651 * checks for supported devices
1653 static int em28xx_usb_probe(struct usb_interface
*interface
,
1654 const struct usb_device_id
*id
)
1656 const struct usb_endpoint_descriptor
*endpoint
;
1657 struct usb_device
*udev
;
1658 struct usb_interface
*uif
;
1659 struct em28xx
*dev
= NULL
;
1660 int retval
= -ENODEV
;
1663 udev
= usb_get_dev(interface_to_usbdev(interface
));
1664 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
1666 /* Check to see next free device and mark as used */
1667 nr
=find_first_zero_bit(&em28xx_devused
,EM28XX_MAXBOARDS
);
1668 em28xx_devused
|=1<<nr
;
1670 /* Don't register audio interfaces */
1671 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
1672 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
1673 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1675 interface
->altsetting
[0].desc
.bInterfaceClass
);
1677 em28xx_devused
&=~(1<<nr
);
1681 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
1682 udev
->descriptor
.idVendor
,udev
->descriptor
.idProduct
,
1684 interface
->altsetting
[0].desc
.bInterfaceClass
);
1686 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
1688 /* check if the device has the iso in endpoint at the correct place */
1689 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
1690 USB_ENDPOINT_XFER_ISOC
) {
1691 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
1692 em28xx_devused
&=~(1<<nr
);
1695 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1696 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
1697 em28xx_devused
&=~(1<<nr
);
1701 if (nr
>= EM28XX_MAXBOARDS
) {
1702 printk (DRIVER_NAME
": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS
);
1703 em28xx_devused
&=~(1<<nr
);
1707 /* allocate memory for our device state and initialize it */
1708 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1710 em28xx_err(DRIVER_NAME
": out of memory!\n");
1711 em28xx_devused
&=~(1<<nr
);
1715 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
1717 dev
->model
= id
->driver_info
;
1719 /* compute alternate max packet sizes */
1720 uif
= udev
->actconfig
->interface
[0];
1722 dev
->num_alt
=uif
->num_altsetting
;
1723 em28xx_info("Alternate settings: %i\n",dev
->num_alt
);
1724 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1725 dev
->alt_max_pkt_size
= kmalloc(32*
1726 dev
->num_alt
,GFP_KERNEL
);
1727 if (dev
->alt_max_pkt_size
== NULL
) {
1728 em28xx_errdev("out of memory!\n");
1729 em28xx_devused
&=~(1<<nr
);
1734 for (i
= 0; i
< dev
->num_alt
; i
++) {
1735 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1737 dev
->alt_max_pkt_size
[i
] =
1738 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1739 em28xx_info("Alternate setting %i, max size= %i\n",i
,
1740 dev
->alt_max_pkt_size
[i
]);
1743 if ((card
[nr
]>=0)&&(card
[nr
]<em28xx_bcount
))
1744 dev
->model
= card
[nr
];
1746 /* allocate device struct */
1747 retval
= em28xx_init_dev(&dev
, udev
, nr
);
1751 em28xx_info("Found %s\n", em28xx_boards
[dev
->model
].name
);
1753 /* save our data pointer in this interface device */
1754 usb_set_intfdata(interface
, dev
);
1759 * em28xx_usb_disconnect()
1760 * called when the device gets diconencted
1761 * video device will be unregistered on v4l2_close in case it is still open
1763 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
1765 struct em28xx
*dev
= usb_get_intfdata(interface
);
1766 usb_set_intfdata(interface
, NULL
);
1771 down_write(&em28xx_disconnect
);
1773 mutex_lock(&dev
->lock
);
1775 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
1777 wake_up_interruptible_all(&dev
->open
);
1781 ("device /dev/video%d is open! Deregistration and memory "
1782 "deallocation are deferred on close.\n",
1783 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
1785 dev
->state
|= DEV_MISCONFIGURED
;
1786 em28xx_uninit_isoc(dev
);
1787 dev
->state
|= DEV_DISCONNECTED
;
1788 wake_up_interruptible(&dev
->wait_frame
);
1789 wake_up_interruptible(&dev
->wait_stream
);
1791 dev
->state
|= DEV_DISCONNECTED
;
1792 em28xx_release_resources(dev
);
1795 mutex_unlock(&dev
->lock
);
1798 kfree(dev
->alt_max_pkt_size
);
1802 up_write(&em28xx_disconnect
);
1805 static struct usb_driver em28xx_usb_driver
= {
1807 .probe
= em28xx_usb_probe
,
1808 .disconnect
= em28xx_usb_disconnect
,
1809 .id_table
= em28xx_id_table
,
1812 static int __init
em28xx_module_init(void)
1816 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
1817 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1818 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1820 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
1821 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
1824 /* register this driver with the USB subsystem */
1825 result
= usb_register(&em28xx_usb_driver
);
1827 em28xx_err(DRIVER_NAME
1828 " usb_register failed. Error number %d.\n", result
);
1833 static void __exit
em28xx_module_exit(void)
1835 /* deregister this driver with the USB subsystem */
1836 usb_deregister(&em28xx_usb_driver
);
1839 module_init(em28xx_module_init
);
1840 module_exit(em28xx_module_exit
);