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/mutex.h>
39 #include <media/v4l2-common.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.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, 1, 0)
52 #define em28xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0)
57 static unsigned int isoc_debug;
58 module_param(isoc_debug, int, 0644);
59 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61 #define em28xx_isocdbg(fmt, arg...) do {\
63 printk(KERN_INFO "%s %s :"fmt, \
64 dev->name, __func__ , ##arg); } while (0)
66 MODULE_AUTHOR(DRIVER_AUTHOR);
67 MODULE_DESCRIPTION(DRIVER_DESC);
68 MODULE_LICENSE("GPL");
70 static LIST_HEAD(em28xx_devlist);
72 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
73 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
74 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
75 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 module_param_array(card, int, NULL, 0444);
78 module_param_array(video_nr, int, NULL, 0444);
79 module_param_array(vbi_nr, int, NULL, 0444);
80 module_param_array(radio_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(card, "card type");
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
86 static unsigned int video_debug;
87 module_param(video_debug,int,0644);
88 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
90 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
91 static unsigned long em28xx_devused;
93 /* supported controls */
94 /* Common to all boards */
95 static struct v4l2_queryctrl em28xx_qctrl[] = {
97 .id = V4L2_CID_AUDIO_VOLUME,
98 .type = V4L2_CTRL_TYPE_INTEGER,
103 .default_value = 0x1f,
106 .id = V4L2_CID_AUDIO_MUTE,
107 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 static struct usb_driver em28xx_usb_driver;
119 /* ------------------------------------------------------------------
120 DMA and thread functions
121 ------------------------------------------------------------------*/
124 * Announces that a buffer were filled and request the next
126 static inline void buffer_filled(struct em28xx *dev,
127 struct em28xx_dmaqueue *dma_q,
128 struct em28xx_buffer *buf)
130 /* Advice that buffer was filled */
131 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
132 buf->vb.state = VIDEOBUF_DONE;
133 buf->vb.field_count++;
134 do_gettimeofday(&buf->vb.ts);
136 dev->isoc_ctl.buf = NULL;
138 list_del(&buf->vb.queue);
139 wake_up(&buf->vb.done);
143 * Identify the buffer header type and properly handles
145 static void em28xx_copy_video(struct em28xx *dev,
146 struct em28xx_dmaqueue *dma_q,
147 struct em28xx_buffer *buf,
149 unsigned char *outp, unsigned long len)
151 void *fieldstart, *startwrite, *startread;
152 int linesdone, currlinedone, offset, lencopy, remain;
153 int bytesperline = dev->width << 1;
155 if (dma_q->pos + len > buf->vb.size)
156 len = buf->vb.size - dma_q->pos;
158 if (p[0] != 0x88 && p[0] != 0x22) {
159 em28xx_isocdbg("frame is not complete\n");
167 /* Interlaces frame */
171 fieldstart = outp + bytesperline;
173 linesdone = dma_q->pos / bytesperline;
174 currlinedone = dma_q->pos % bytesperline;
175 offset = linesdone * bytesperline * 2 + currlinedone;
176 startwrite = fieldstart + offset;
177 lencopy = bytesperline - currlinedone;
178 lencopy = lencopy > remain ? remain : lencopy;
180 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
181 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
182 ((char *)startwrite + lencopy) -
183 ((char *)outp + buf->vb.size));
184 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
188 memcpy(startwrite, startread, lencopy);
193 startwrite += lencopy + bytesperline;
194 startread += lencopy;
195 if (bytesperline > remain)
198 lencopy = bytesperline;
200 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
201 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
202 ((char *)startwrite + lencopy) -
203 ((char *)outp + buf->vb.size));
204 lencopy = remain = (char *)outp + buf->vb.size -
210 memcpy(startwrite, startread, lencopy);
218 static inline void print_err_status(struct em28xx *dev,
219 int packet, int status)
221 char *errmsg = "Unknown";
225 errmsg = "unlinked synchronuously";
228 errmsg = "unlinked asynchronuously";
231 errmsg = "Buffer error (overrun)";
234 errmsg = "Stalled (device not responding)";
237 errmsg = "Babble (bad cable?)";
240 errmsg = "Bit-stuff error (bad cable?)";
243 errmsg = "CRC/Timeout (could be anything)";
246 errmsg = "Device does not respond";
250 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
252 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
253 packet, status, errmsg);
258 * video-buf generic routine to get the next available buffer
260 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
261 struct em28xx_buffer **buf)
263 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
266 if (list_empty(&dma_q->active)) {
267 em28xx_isocdbg("No active queue to serve\n");
268 dev->isoc_ctl.buf = NULL;
273 /* Get the next buffer */
274 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
276 /* Cleans up buffer - Usefull for testing for frame/URB loss */
277 outp = videobuf_to_vmalloc(&(*buf)->vb);
278 memset(outp, 0, (*buf)->vb.size);
280 dev->isoc_ctl.buf = *buf;
286 * Controls the isoc copy of each urb packet
288 static inline int em28xx_isoc_copy(struct urb *urb)
290 struct em28xx_buffer *buf;
291 struct em28xx_dmaqueue *dma_q = urb->context;
292 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
293 unsigned char *outp = NULL;
294 int i, len = 0, rc = 1;
300 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
303 if (urb->status < 0) {
304 print_err_status(dev, -1, urb->status);
305 if (urb->status == -ENOENT)
309 buf = dev->isoc_ctl.buf;
311 outp = videobuf_to_vmalloc(&buf->vb);
313 for (i = 0; i < urb->number_of_packets; i++) {
314 int status = urb->iso_frame_desc[i].status;
317 print_err_status(dev, i, status);
318 if (urb->iso_frame_desc[i].status != -EPROTO)
322 len = urb->iso_frame_desc[i].actual_length - 4;
324 if (urb->iso_frame_desc[i].actual_length <= 0) {
325 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
328 if (urb->iso_frame_desc[i].actual_length >
330 em28xx_isocdbg("packet bigger than packet size");
334 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
336 /* FIXME: incomplete buffer checks where removed to make
337 logic simpler. Impacts of those changes should be evaluated
339 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
340 em28xx_isocdbg("VBI HEADER!!!\n");
341 /* FIXME: Should add vbi copy */
344 if (p[0] == 0x22 && p[1] == 0x5a) {
345 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
346 len, (p[2] & 1)? "odd" : "even");
350 buffer_filled(dev, dma_q, buf);
351 get_next_buf(dma_q, &buf);
355 outp = videobuf_to_vmalloc(&buf->vb);
368 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
373 /* ------------------------------------------------------------------
375 ------------------------------------------------------------------*/
378 * IRQ callback, called by URB callback
380 static void em28xx_irq_callback(struct urb *urb)
382 struct em28xx_dmaqueue *dma_q = urb->context;
383 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
386 /* Copy data from URB */
387 spin_lock(&dev->slock);
388 rc = em28xx_isoc_copy(urb);
389 spin_unlock(&dev->slock);
391 /* Reset urb buffers */
392 for (i = 0; i < urb->number_of_packets; i++) {
393 urb->iso_frame_desc[i].status = 0;
394 urb->iso_frame_desc[i].actual_length = 0;
398 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
400 em28xx_err("urb resubmit failed (error=%i)\n",
406 * Stop and Deallocate URBs
408 static void em28xx_uninit_isoc(struct em28xx *dev)
413 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
415 dev->isoc_ctl.nfields = -1;
416 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
417 urb = dev->isoc_ctl.urb[i];
421 if (dev->isoc_ctl.transfer_buffer[i]) {
422 usb_buffer_free(dev->udev,
423 urb->transfer_buffer_length,
424 dev->isoc_ctl.transfer_buffer[i],
428 dev->isoc_ctl.urb[i] = NULL;
430 dev->isoc_ctl.transfer_buffer[i] = NULL;
433 kfree(dev->isoc_ctl.urb);
434 kfree(dev->isoc_ctl.transfer_buffer);
435 dev->isoc_ctl.urb = NULL;
436 dev->isoc_ctl.transfer_buffer = NULL;
438 dev->isoc_ctl.num_bufs = 0;
440 em28xx_capture_start(dev, 0);
444 * Allocate URBs and start IRQ
446 static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
449 struct em28xx_dmaqueue *dma_q = &dev->vidq;
455 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
457 /* De-allocates all pending stuff */
458 em28xx_uninit_isoc(dev);
460 dev->isoc_ctl.num_bufs = num_bufs;
462 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
463 if (!dev->isoc_ctl.urb) {
464 em28xx_errdev("cannot alloc memory for usb buffers\n");
468 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
470 if (!dev->isoc_ctl.urb) {
471 em28xx_errdev("cannot allocate memory for usbtransfer\n");
472 kfree(dev->isoc_ctl.urb);
476 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
477 dev->isoc_ctl.buf = NULL;
479 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
481 /* allocate urbs and transfer buffers */
482 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
483 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
485 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
486 em28xx_uninit_isoc(dev);
489 dev->isoc_ctl.urb[i] = urb;
491 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
492 sb_size, GFP_KERNEL, &urb->transfer_dma);
493 if (!dev->isoc_ctl.transfer_buffer[i]) {
494 em28xx_err("unable to allocate %i bytes for transfer"
497 in_interrupt()?" while in int":"");
498 em28xx_uninit_isoc(dev);
501 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
503 /* FIXME: this is a hack - should be
504 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
505 should also be using 'desc.bInterval'
507 pipe = usb_rcvisocpipe(dev->udev, 0x82);
508 usb_fill_int_urb(urb, dev->udev, pipe,
509 dev->isoc_ctl.transfer_buffer[i], sb_size,
510 em28xx_irq_callback, dma_q, 1);
512 urb->number_of_packets = max_packets;
513 urb->transfer_flags = URB_ISO_ASAP;
516 for (j = 0; j < max_packets; j++) {
517 urb->iso_frame_desc[j].offset = k;
518 urb->iso_frame_desc[j].length =
519 dev->isoc_ctl.max_pkt_size;
520 k += dev->isoc_ctl.max_pkt_size;
527 static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
529 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
532 em28xx_videodbg("Called em28xx_start_thread\n");
534 init_waitqueue_head(&dma_q->wq);
536 em28xx_capture_start(dev, 1);
538 /* submit urbs and enables IRQ */
539 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
540 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
542 em28xx_err("submit of urb %i failed (error=%i)\n", i,
544 em28xx_uninit_isoc(dev);
555 /* ------------------------------------------------------------------
557 ------------------------------------------------------------------*/
560 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
562 struct em28xx_fh *fh = vq->priv_data;
563 struct em28xx *dev = fh->dev;
564 struct v4l2_frequency f;
566 *size = 16 * fh->dev->width * fh->dev->height >> 3;
568 *count = EM28XX_DEF_BUF;
570 if (*count < EM28XX_MIN_BUF)
571 *count = EM28XX_MIN_BUF;
573 dev->mode = EM28XX_ANALOG_MODE;
575 /* Ask tuner to go to analog mode */
576 memset (&f, 0, sizeof(f));
577 f.frequency = dev->ctl_freq;
579 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
584 /* This is called *without* dev->slock held; please keep it that way */
585 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
587 struct em28xx_fh *fh = vq->priv_data;
588 struct em28xx *dev = fh->dev;
589 unsigned long flags = 0;
593 /* We used to wait for the buffer to finish here, but this didn't work
594 because, as we were keeping the state as VIDEOBUF_QUEUED,
595 videobuf_queue_cancel marked it as finished for us.
596 (Also, it could wedge forever if the hardware was misconfigured.)
598 This should be safe; by the time we get here, the buffer isn't
599 queued anymore. If we ever start marking the buffers as
600 VIDEOBUF_ACTIVE, it won't be, though.
602 spin_lock_irqsave(&dev->slock, flags);
603 if (dev->isoc_ctl.buf == buf)
604 dev->isoc_ctl.buf = NULL;
605 spin_unlock_irqrestore(&dev->slock, flags);
607 videobuf_vmalloc_free(&buf->vb);
608 buf->vb.state = VIDEOBUF_NEEDS_INIT;
612 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
613 enum v4l2_field field)
615 struct em28xx_fh *fh = vq->priv_data;
616 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
617 struct em28xx *dev = fh->dev;
618 struct em28xx_dmaqueue *vidq = &dev->vidq;
619 int rc = 0, urb_init = 0;
621 /* FIXME: It assumes depth = 16 */
622 /* The only currently supported format is 16 bits/pixel */
623 buf->vb.size = 16 * dev->width * dev->height >> 3;
625 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
628 buf->vb.width = dev->width;
629 buf->vb.height = dev->height;
630 buf->vb.field = field;
632 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
633 rc = videobuf_iolock(vq, &buf->vb, NULL);
638 if (!dev->isoc_ctl.num_bufs)
642 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
647 rc = em28xx_start_thread(vidq);
652 buf->vb.state = VIDEOBUF_PREPARED;
656 free_buffer(vq, buf);
661 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
663 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
664 struct em28xx_fh *fh = vq->priv_data;
665 struct em28xx *dev = fh->dev;
666 struct em28xx_dmaqueue *vidq = &dev->vidq;
668 buf->vb.state = VIDEOBUF_QUEUED;
669 list_add_tail(&buf->vb.queue, &vidq->active);
673 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
675 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
676 struct em28xx_fh *fh = vq->priv_data;
677 struct em28xx *dev = (struct em28xx *)fh->dev;
679 em28xx_isocdbg("em28xx: called buffer_release\n");
681 free_buffer(vq, buf);
684 static struct videobuf_queue_ops em28xx_video_qops = {
685 .buf_setup = buffer_setup,
686 .buf_prepare = buffer_prepare,
687 .buf_queue = buffer_queue,
688 .buf_release = buffer_release,
691 /********************* v4l2 interface ******************************************/
695 * inits registers with sane defaults
697 static int em28xx_config(struct em28xx *dev)
700 /* Sets I2C speed to 100 KHz */
702 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
704 /* enable vbi capturing */
706 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
707 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
708 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
710 dev->mute = 1; /* maybe not the right place... */
713 em28xx_outfmt_set_yuv422(dev);
714 em28xx_colorlevels_set_default(dev);
715 em28xx_compression_disable(dev);
721 * em28xx_config_i2c()
722 * configure i2c attached devices
724 static void em28xx_config_i2c(struct em28xx *dev)
726 struct v4l2_routing route;
728 route.input = INPUT(dev->ctl_input)->vmux;
730 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
731 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
732 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
735 static void video_mux(struct em28xx *dev, int index)
737 struct v4l2_routing route;
739 route.input = INPUT(index)->vmux;
741 dev->ctl_input = index;
742 dev->ctl_ainput = INPUT(index)->amux;
744 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
746 if (dev->has_msp34xx) {
748 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
749 route.input = dev->ctl_ainput;
750 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
751 /* Note: this is msp3400 specific */
752 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
755 em28xx_audio_analog_set(dev);
758 /* Usage lock check functions */
759 static int res_get(struct em28xx_fh *fh)
761 struct em28xx *dev = fh->dev;
764 /* This instance already has stream_on */
771 mutex_lock(&dev->lock);
774 mutex_unlock(&dev->lock);
778 static int res_check(struct em28xx_fh *fh)
780 return (fh->stream_on);
783 static void res_free(struct em28xx_fh *fh)
785 struct em28xx *dev = fh->dev;
787 mutex_lock(&dev->lock);
790 mutex_unlock(&dev->lock);
795 * return the current saturation, brightness or contrast, mute state
797 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
800 case V4L2_CID_AUDIO_MUTE:
801 ctrl->value = dev->mute;
803 case V4L2_CID_AUDIO_VOLUME:
804 ctrl->value = dev->volume;
813 * mute or set new saturation, brightness or contrast
815 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
818 case V4L2_CID_AUDIO_MUTE:
819 if (ctrl->value != dev->mute) {
820 dev->mute = ctrl->value;
821 return em28xx_audio_analog_set(dev);
824 case V4L2_CID_AUDIO_VOLUME:
825 dev->volume = ctrl->value;
826 return em28xx_audio_analog_set(dev);
832 static int check_dev(struct em28xx *dev)
834 if (dev->state & DEV_DISCONNECTED) {
835 em28xx_errdev("v4l2 ioctl: device not present\n");
839 if (dev->state & DEV_MISCONFIGURED) {
840 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
841 "close and open it again\n");
847 static void get_scale(struct em28xx *dev,
848 unsigned int width, unsigned int height,
849 unsigned int *hscale, unsigned int *vscale)
851 unsigned int maxw = norm_maxw(dev);
852 unsigned int maxh = norm_maxh(dev);
854 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
855 if (*hscale >= 0x4000)
858 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
859 if (*vscale >= 0x4000)
863 /* ------------------------------------------------------------------
864 IOCTL vidioc handling
865 ------------------------------------------------------------------*/
867 static int vidioc_g_fmt_cap(struct file *file, void *priv,
868 struct v4l2_format *f)
870 struct em28xx_fh *fh = priv;
871 struct em28xx *dev = fh->dev;
873 mutex_lock(&dev->lock);
875 f->fmt.pix.width = dev->width;
876 f->fmt.pix.height = dev->height;
877 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
878 f->fmt.pix.bytesperline = dev->width * 2;
879 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
880 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
882 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
883 f->fmt.pix.field = dev->interlaced ?
884 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
886 mutex_unlock(&dev->lock);
890 static int vidioc_try_fmt_cap(struct file *file, void *priv,
891 struct v4l2_format *f)
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
895 int width = f->fmt.pix.width;
896 int height = f->fmt.pix.height;
897 unsigned int maxw = norm_maxw(dev);
898 unsigned int maxh = norm_maxh(dev);
899 unsigned int hscale, vscale;
901 /* width must even because of the YUYV format
902 height must be even because of interlacing */
915 mutex_lock(&dev->lock);
917 if (dev->is_em2800) {
918 /* the em2800 can only scale down to 50% */
919 if (height % (maxh / 2))
921 if (width % (maxw / 2))
923 /* according to empiatech support */
924 /* the MaxPacketSize is to small to support */
925 /* framesizes larger than 640x480 @ 30 fps */
926 /* or 640x576 @ 25 fps. As this would cut */
927 /* of a part of the image we prefer */
928 /* 360x576 or 360x480 for now */
929 if (width == maxw && height == maxh)
933 get_scale(dev, width, height, &hscale, &vscale);
935 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
936 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
938 f->fmt.pix.width = width;
939 f->fmt.pix.height = height;
940 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
941 f->fmt.pix.bytesperline = width * 2;
942 f->fmt.pix.sizeimage = width * 2 * height;
943 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
944 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
946 mutex_unlock(&dev->lock);
950 static int vidioc_s_fmt_cap(struct file *file, void *priv,
951 struct v4l2_format *f)
953 struct em28xx_fh *fh = priv;
954 struct em28xx *dev = fh->dev;
961 vidioc_try_fmt_cap(file, priv, f);
963 mutex_lock(&dev->lock);
965 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
966 em28xx_errdev("%s queue busy\n", __func__);
971 if (dev->stream_on && !fh->stream_on) {
972 em28xx_errdev("%s device in use by another fh\n", __func__);
977 /* set new image size */
978 dev->width = f->fmt.pix.width;
979 dev->height = f->fmt.pix.height;
980 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
982 em28xx_set_alternate(dev);
983 em28xx_resolution_set(dev);
988 mutex_unlock(&dev->lock);
992 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
994 struct em28xx_fh *fh = priv;
995 struct em28xx *dev = fh->dev;
996 struct v4l2_format f;
1003 mutex_lock(&dev->lock);
1005 mutex_unlock(&dev->lock);
1007 /* Adjusts width/height, if needed */
1008 f.fmt.pix.width = dev->width;
1009 f.fmt.pix.height = dev->height;
1010 vidioc_try_fmt_cap(file, priv, &f);
1012 mutex_lock(&dev->lock);
1014 /* set new image size */
1015 dev->width = f.fmt.pix.width;
1016 dev->height = f.fmt.pix.height;
1017 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1019 em28xx_resolution_set(dev);
1020 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1022 mutex_unlock(&dev->lock);
1026 static const char *iname[] = {
1027 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1028 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1029 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1030 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1031 [EM28XX_VMUX_SVIDEO] = "S-Video",
1032 [EM28XX_VMUX_TELEVISION] = "Television",
1033 [EM28XX_VMUX_CABLE] = "Cable TV",
1034 [EM28XX_VMUX_DVB] = "DVB",
1035 [EM28XX_VMUX_DEBUG] = "for debug only",
1038 static int vidioc_enum_input(struct file *file, void *priv,
1039 struct v4l2_input *i)
1041 struct em28xx_fh *fh = priv;
1042 struct em28xx *dev = fh->dev;
1046 if (n >= MAX_EM28XX_INPUT)
1048 if (0 == INPUT(n)->type)
1052 i->type = V4L2_INPUT_TYPE_CAMERA;
1054 strcpy(i->name, iname[INPUT(n)->type]);
1056 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1057 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1058 i->type = V4L2_INPUT_TYPE_TUNER;
1060 i->std = dev->vdev->tvnorms;
1065 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1067 struct em28xx_fh *fh = priv;
1068 struct em28xx *dev = fh->dev;
1070 *i = dev->ctl_input;
1075 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1077 struct em28xx_fh *fh = priv;
1078 struct em28xx *dev = fh->dev;
1081 rc = check_dev(dev);
1085 if (i >= MAX_EM28XX_INPUT)
1087 if (0 == INPUT(i)->type)
1090 mutex_lock(&dev->lock);
1094 mutex_unlock(&dev->lock);
1098 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1100 struct em28xx_fh *fh = priv;
1101 struct em28xx *dev = fh->dev;
1102 unsigned int index = a->index;
1107 index = dev->ctl_ainput;
1110 strcpy(a->name, "Television");
1112 strcpy(a->name, "Line In");
1114 a->capability = V4L2_AUDCAP_STEREO;
1120 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1122 struct em28xx_fh *fh = priv;
1123 struct em28xx *dev = fh->dev;
1125 if (a->index != dev->ctl_ainput)
1131 static int vidioc_queryctrl(struct file *file, void *priv,
1132 struct v4l2_queryctrl *qc)
1134 struct em28xx_fh *fh = priv;
1135 struct em28xx *dev = fh->dev;
1140 rc = check_dev(dev);
1144 memset(qc, 0, sizeof(*qc));
1148 if (!dev->has_msp34xx) {
1149 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1150 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1151 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1156 mutex_lock(&dev->lock);
1157 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1158 mutex_unlock(&dev->lock);
1166 static int vidioc_g_ctrl(struct file *file, void *priv,
1167 struct v4l2_control *ctrl)
1169 struct em28xx_fh *fh = priv;
1170 struct em28xx *dev = fh->dev;
1173 rc = check_dev(dev);
1176 mutex_lock(&dev->lock);
1178 if (!dev->has_msp34xx)
1179 rc = em28xx_get_ctrl(dev, ctrl);
1183 if (rc == -EINVAL) {
1184 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1188 mutex_unlock(&dev->lock);
1192 static int vidioc_s_ctrl(struct file *file, void *priv,
1193 struct v4l2_control *ctrl)
1195 struct em28xx_fh *fh = priv;
1196 struct em28xx *dev = fh->dev;
1200 rc = check_dev(dev);
1204 mutex_lock(&dev->lock);
1206 if (dev->has_msp34xx)
1207 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1210 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1211 if (ctrl->id == em28xx_qctrl[i].id) {
1212 if (ctrl->value < em28xx_qctrl[i].minimum ||
1213 ctrl->value > em28xx_qctrl[i].maximum) {
1218 rc = em28xx_set_ctrl(dev, ctrl);
1224 /* Control not found - try to send it to the attached devices */
1226 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1230 mutex_unlock(&dev->lock);
1234 static int vidioc_g_tuner(struct file *file, void *priv,
1235 struct v4l2_tuner *t)
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1241 rc = check_dev(dev);
1248 strcpy(t->name, "Tuner");
1250 mutex_lock(&dev->lock);
1252 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1254 mutex_unlock(&dev->lock);
1258 static int vidioc_s_tuner(struct file *file, void *priv,
1259 struct v4l2_tuner *t)
1261 struct em28xx_fh *fh = priv;
1262 struct em28xx *dev = fh->dev;
1265 rc = check_dev(dev);
1272 mutex_lock(&dev->lock);
1274 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1276 mutex_unlock(&dev->lock);
1280 static int vidioc_g_frequency(struct file *file, void *priv,
1281 struct v4l2_frequency *f)
1283 struct em28xx_fh *fh = priv;
1284 struct em28xx *dev = fh->dev;
1286 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1287 f->frequency = dev->ctl_freq;
1292 static int vidioc_s_frequency(struct file *file, void *priv,
1293 struct v4l2_frequency *f)
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
1299 rc = check_dev(dev);
1306 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1308 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1311 mutex_lock(&dev->lock);
1313 dev->ctl_freq = f->frequency;
1314 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1316 mutex_unlock(&dev->lock);
1320 #ifdef CONFIG_VIDEO_ADV_DEBUG
1321 static int em28xx_reg_len(int reg)
1333 static int vidioc_g_register(struct file *file, void *priv,
1334 struct v4l2_register *reg)
1336 struct em28xx_fh *fh = priv;
1337 struct em28xx *dev = fh->dev;
1340 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1343 if (em28xx_reg_len(reg->reg) == 1) {
1344 ret = em28xx_read_reg(dev, reg->reg);
1351 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1352 reg->reg, (char *)&val, 2);
1356 reg->val = cpu_to_le64((__u64)val);
1362 static int vidioc_s_register(struct file *file, void *priv,
1363 struct v4l2_register *reg)
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1369 buf = le64_to_cpu((__u64)reg->val);
1371 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1372 em28xx_reg_len(reg->reg));
1377 static int vidioc_cropcap(struct file *file, void *priv,
1378 struct v4l2_cropcap *cc)
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
1383 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1386 cc->bounds.left = 0;
1388 cc->bounds.width = dev->width;
1389 cc->bounds.height = dev->height;
1390 cc->defrect = cc->bounds;
1391 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1392 cc->pixelaspect.denominator = 59;
1397 static int vidioc_streamon(struct file *file, void *priv,
1398 enum v4l2_buf_type type)
1400 struct em28xx_fh *fh = priv;
1401 struct em28xx *dev = fh->dev;
1404 rc = check_dev(dev);
1409 if (unlikely(res_get(fh) < 0))
1412 return (videobuf_streamon(&fh->vb_vidq));
1415 static int vidioc_streamoff(struct file *file, void *priv,
1416 enum v4l2_buf_type type)
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1422 rc = check_dev(dev);
1426 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1428 if (type != fh->type)
1431 videobuf_streamoff(&fh->vb_vidq);
1437 static int vidioc_querycap(struct file *file, void *priv,
1438 struct v4l2_capability *cap)
1440 struct em28xx_fh *fh = priv;
1441 struct em28xx *dev = fh->dev;
1443 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1444 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1445 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1447 cap->version = EM28XX_VERSION_CODE;
1450 V4L2_CAP_SLICED_VBI_CAPTURE |
1451 V4L2_CAP_VIDEO_CAPTURE |
1453 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1455 if (dev->tuner_type != TUNER_ABSENT)
1456 cap->capabilities |= V4L2_CAP_TUNER;
1461 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1462 struct v4l2_fmtdesc *fmtd)
1464 if (fmtd->index != 0)
1467 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1468 strcpy(fmtd->description, "Packed YUY2");
1469 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1470 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1475 /* Sliced VBI ioctls */
1476 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1477 struct v4l2_format *f)
1479 struct em28xx_fh *fh = priv;
1480 struct em28xx *dev = fh->dev;
1483 rc = check_dev(dev);
1487 mutex_lock(&dev->lock);
1489 f->fmt.sliced.service_set = 0;
1491 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1493 if (f->fmt.sliced.service_set == 0)
1496 mutex_unlock(&dev->lock);
1500 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1501 struct v4l2_format *f)
1503 struct em28xx_fh *fh = priv;
1504 struct em28xx *dev = fh->dev;
1507 rc = check_dev(dev);
1511 mutex_lock(&dev->lock);
1512 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1513 mutex_unlock(&dev->lock);
1515 if (f->fmt.sliced.service_set == 0)
1522 static int vidioc_reqbufs(struct file *file, void *priv,
1523 struct v4l2_requestbuffers *rb)
1525 struct em28xx_fh *fh = priv;
1526 struct em28xx *dev = fh->dev;
1529 rc = check_dev(dev);
1533 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1536 static int vidioc_querybuf(struct file *file, void *priv,
1537 struct v4l2_buffer *b)
1539 struct em28xx_fh *fh = priv;
1540 struct em28xx *dev = fh->dev;
1543 rc = check_dev(dev);
1547 return (videobuf_querybuf(&fh->vb_vidq, b));
1550 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1552 struct em28xx_fh *fh = priv;
1553 struct em28xx *dev = fh->dev;
1556 rc = check_dev(dev);
1560 return (videobuf_qbuf(&fh->vb_vidq, b));
1563 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1565 struct em28xx_fh *fh = priv;
1566 struct em28xx *dev = fh->dev;
1569 rc = check_dev(dev);
1573 return (videobuf_dqbuf(&fh->vb_vidq, b,
1574 file->f_flags & O_NONBLOCK));
1577 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1578 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1580 struct em28xx_fh *fh = priv;
1582 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1587 /* ----------------------------------------------------------- */
1588 /* RADIO ESPECIFIC IOCTLS */
1589 /* ----------------------------------------------------------- */
1591 static int radio_querycap(struct file *file, void *priv,
1592 struct v4l2_capability *cap)
1594 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1596 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1597 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1598 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1600 cap->version = EM28XX_VERSION_CODE;
1601 cap->capabilities = V4L2_CAP_TUNER;
1605 static int radio_g_tuner(struct file *file, void *priv,
1606 struct v4l2_tuner *t)
1608 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1610 if (unlikely(t->index > 0))
1613 strcpy(t->name, "Radio");
1614 t->type = V4L2_TUNER_RADIO;
1616 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1620 static int radio_enum_input(struct file *file, void *priv,
1621 struct v4l2_input *i)
1625 strcpy(i->name, "Radio");
1626 i->type = V4L2_INPUT_TYPE_TUNER;
1631 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1633 if (unlikely(a->index))
1636 strcpy(a->name, "Radio");
1640 static int radio_s_tuner(struct file *file, void *priv,
1641 struct v4l2_tuner *t)
1643 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1648 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1653 static int radio_s_audio(struct file *file, void *fh,
1654 struct v4l2_audio *a)
1659 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1664 static int radio_queryctrl(struct file *file, void *priv,
1665 struct v4l2_queryctrl *qc)
1669 if (qc->id < V4L2_CID_BASE ||
1670 qc->id >= V4L2_CID_LASTP1)
1673 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1674 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1675 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1684 * em28xx_v4l2_open()
1685 * inits the device and starts isoc transfer
1687 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1689 int minor = iminor(inode);
1690 int errCode = 0, radio = 0;
1691 struct em28xx *h,*dev = NULL;
1692 struct em28xx_fh *fh;
1693 enum v4l2_buf_type fh_type = 0;
1695 list_for_each_entry(h, &em28xx_devlist, devlist) {
1696 if (h->vdev->minor == minor) {
1698 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1700 if (h->vbi_dev->minor == minor) {
1702 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1705 h->radio_dev->minor == minor) {
1713 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1714 minor, v4l2_type_names[fh_type], dev->users);
1716 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1719 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1722 mutex_lock(&dev->lock);
1726 filp->private_data = fh;
1728 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1729 dev->width = norm_maxw(dev);
1730 dev->height = norm_maxh(dev);
1734 em28xx_set_alternate(dev);
1735 em28xx_resolution_set(dev);
1739 em28xx_videodbg("video_open: setting radio device\n");
1740 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1745 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1746 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1747 sizeof(struct em28xx_buffer), fh);
1749 mutex_unlock(&dev->lock);
1754 * em28xx_realease_resources()
1755 * unregisters the v4l2,i2c and usb devices
1756 * called when the device gets disconected or at module unload
1758 static void em28xx_release_resources(struct em28xx *dev)
1761 /*FIXME: I2C IR should be disconnected */
1763 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1764 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1765 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1766 list_del(&dev->devlist);
1767 if (dev->radio_dev) {
1768 if (-1 != dev->radio_dev->minor)
1769 video_unregister_device(dev->radio_dev);
1771 video_device_release(dev->radio_dev);
1772 dev->radio_dev = NULL;
1775 if (-1 != dev->vbi_dev->minor)
1776 video_unregister_device(dev->vbi_dev);
1778 video_device_release(dev->vbi_dev);
1779 dev->vbi_dev = NULL;
1782 if (-1 != dev->vdev->minor)
1783 video_unregister_device(dev->vdev);
1785 video_device_release(dev->vdev);
1788 em28xx_i2c_unregister(dev);
1789 usb_put_dev(dev->udev);
1791 /* Mark device as unused */
1792 em28xx_devused&=~(1<<dev->devno);
1796 * em28xx_v4l2_close()
1797 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1799 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1801 struct em28xx_fh *fh = filp->private_data;
1802 struct em28xx *dev = fh->dev;
1805 em28xx_videodbg("users=%d\n", dev->users);
1811 mutex_lock(&dev->lock);
1813 if (dev->users == 1) {
1814 videobuf_stop(&fh->vb_vidq);
1815 videobuf_mmap_free(&fh->vb_vidq);
1817 /* the device is already disconnect,
1818 free the remaining resources */
1819 if (dev->state & DEV_DISCONNECTED) {
1820 em28xx_release_resources(dev);
1821 mutex_unlock(&dev->lock);
1826 /* do this before setting alternate! */
1827 em28xx_uninit_isoc(dev);
1829 /* set alternate 0 */
1831 em28xx_videodbg("setting alternate 0\n");
1832 errCode = usb_set_interface(dev->udev, 0, 0);
1834 em28xx_errdev("cannot change alternate number to "
1835 "0 (error=%i)\n", errCode);
1840 wake_up_interruptible_nr(&dev->open, 1);
1841 mutex_unlock(&dev->lock);
1846 * em28xx_v4l2_read()
1847 * will allocate buffers when called for the first time
1850 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1853 struct em28xx_fh *fh = filp->private_data;
1854 struct em28xx *dev = fh->dev;
1857 rc = check_dev(dev);
1861 /* FIXME: read() is not prepared to allow changing the video
1862 resolution while streaming. Seems a bug at em28xx_set_fmt
1865 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1866 if (unlikely(res_get(fh)))
1869 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1870 filp->f_flags & O_NONBLOCK);
1876 * em28xx_v4l2_poll()
1877 * will allocate buffers when called for the first time
1879 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1881 struct em28xx_fh *fh = filp->private_data;
1882 struct em28xx *dev = fh->dev;
1885 rc = check_dev(dev);
1889 if (unlikely(res_get(fh) < 0))
1892 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1895 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1899 * em28xx_v4l2_mmap()
1901 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1903 struct em28xx_fh *fh = filp->private_data;
1904 struct em28xx *dev = fh->dev;
1907 if (unlikely(res_get(fh) < 0))
1910 rc = check_dev(dev);
1914 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1916 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1917 (unsigned long)vma->vm_start,
1918 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1924 static const struct file_operations em28xx_v4l_fops = {
1925 .owner = THIS_MODULE,
1926 .open = em28xx_v4l2_open,
1927 .release = em28xx_v4l2_close,
1928 .read = em28xx_v4l2_read,
1929 .poll = em28xx_v4l2_poll,
1930 .mmap = em28xx_v4l2_mmap,
1931 .ioctl = video_ioctl2,
1932 .llseek = no_llseek,
1933 .compat_ioctl = v4l_compat_ioctl32,
1936 static const struct file_operations radio_fops = {
1937 .owner = THIS_MODULE,
1938 .open = em28xx_v4l2_open,
1939 .release = em28xx_v4l2_close,
1940 .ioctl = video_ioctl2,
1941 .compat_ioctl = v4l_compat_ioctl32,
1942 .llseek = no_llseek,
1945 static const struct video_device em28xx_video_template = {
1946 .fops = &em28xx_v4l_fops,
1947 .release = video_device_release,
1950 .vidioc_querycap = vidioc_querycap,
1951 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1952 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1953 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1954 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1955 .vidioc_g_audio = vidioc_g_audio,
1956 .vidioc_s_audio = vidioc_s_audio,
1957 .vidioc_cropcap = vidioc_cropcap,
1959 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1960 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1961 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1963 .vidioc_reqbufs = vidioc_reqbufs,
1964 .vidioc_querybuf = vidioc_querybuf,
1965 .vidioc_qbuf = vidioc_qbuf,
1966 .vidioc_dqbuf = vidioc_dqbuf,
1967 .vidioc_s_std = vidioc_s_std,
1968 .vidioc_enum_input = vidioc_enum_input,
1969 .vidioc_g_input = vidioc_g_input,
1970 .vidioc_s_input = vidioc_s_input,
1971 .vidioc_queryctrl = vidioc_queryctrl,
1972 .vidioc_g_ctrl = vidioc_g_ctrl,
1973 .vidioc_s_ctrl = vidioc_s_ctrl,
1974 .vidioc_streamon = vidioc_streamon,
1975 .vidioc_streamoff = vidioc_streamoff,
1976 .vidioc_g_tuner = vidioc_g_tuner,
1977 .vidioc_s_tuner = vidioc_s_tuner,
1978 .vidioc_g_frequency = vidioc_g_frequency,
1979 .vidioc_s_frequency = vidioc_s_frequency,
1980 #ifdef CONFIG_VIDEO_ADV_DEBUG
1981 .vidioc_g_register = vidioc_g_register,
1982 .vidioc_s_register = vidioc_s_register,
1984 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1985 .vidiocgmbuf = vidiocgmbuf,
1988 .tvnorms = V4L2_STD_ALL,
1989 .current_norm = V4L2_STD_PAL,
1992 static struct video_device em28xx_radio_template = {
1993 .name = "em28xx-radio",
1994 .type = VID_TYPE_TUNER,
1995 .fops = &radio_fops,
1997 .vidioc_querycap = radio_querycap,
1998 .vidioc_g_tuner = radio_g_tuner,
1999 .vidioc_enum_input = radio_enum_input,
2000 .vidioc_g_audio = radio_g_audio,
2001 .vidioc_s_tuner = radio_s_tuner,
2002 .vidioc_s_audio = radio_s_audio,
2003 .vidioc_s_input = radio_s_input,
2004 .vidioc_queryctrl = radio_queryctrl,
2005 .vidioc_g_ctrl = vidioc_g_ctrl,
2006 .vidioc_s_ctrl = vidioc_s_ctrl,
2007 .vidioc_g_frequency = vidioc_g_frequency,
2008 .vidioc_s_frequency = vidioc_s_frequency,
2009 #ifdef CONFIG_VIDEO_ADV_DEBUG
2010 .vidioc_g_register = vidioc_g_register,
2011 .vidioc_s_register = vidioc_s_register,
2015 /******************************** usb interface *****************************************/
2018 static LIST_HEAD(em28xx_extension_devlist);
2019 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2021 int em28xx_register_extension(struct em28xx_ops *ops)
2023 struct em28xx *h, *dev = NULL;
2025 list_for_each_entry(h, &em28xx_devlist, devlist)
2028 mutex_lock(&em28xx_extension_devlist_lock);
2029 list_add_tail(&ops->next, &em28xx_extension_devlist);
2033 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2034 mutex_unlock(&em28xx_extension_devlist_lock);
2038 EXPORT_SYMBOL(em28xx_register_extension);
2040 void em28xx_unregister_extension(struct em28xx_ops *ops)
2042 struct em28xx *h, *dev = NULL;
2044 list_for_each_entry(h, &em28xx_devlist, devlist)
2050 mutex_lock(&em28xx_extension_devlist_lock);
2051 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2052 list_del(&ops->next);
2053 mutex_unlock(&em28xx_extension_devlist_lock);
2055 EXPORT_SYMBOL(em28xx_unregister_extension);
2057 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2058 const struct video_device *template,
2060 const char *type_name)
2062 struct video_device *vfd;
2064 vfd = video_device_alloc();
2069 vfd->dev = &dev->udev->dev;
2070 vfd->release = video_device_release;
2072 vfd->debug = video_debug;
2074 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2075 dev->name, type_name);
2083 * allocates and inits the device structs, registers i2c bus and v4l device
2085 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2088 struct em28xx_ops *ops = NULL;
2089 struct em28xx *dev = *devhandle;
2090 int retval = -ENOMEM;
2092 unsigned int maxh, maxw;
2095 mutex_init(&dev->lock);
2096 spin_lock_init(&dev->slock);
2097 init_waitqueue_head(&dev->open);
2098 init_waitqueue_head(&dev->wait_frame);
2099 init_waitqueue_head(&dev->wait_stream);
2101 dev->em28xx_write_regs = em28xx_write_regs;
2102 dev->em28xx_read_reg = em28xx_read_reg;
2103 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2104 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2105 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2106 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2108 errCode = em28xx_read_reg(dev, CHIPID_REG);
2110 em28xx_info("em28xx chip ID = %d\n", errCode);
2112 em28xx_pre_card_setup(dev);
2114 errCode = em28xx_config(dev);
2116 em28xx_errdev("error configuring device\n");
2117 em28xx_devused &= ~(1<<dev->devno);
2122 /* register i2c bus */
2123 em28xx_i2c_register(dev);
2125 /* Do board specific init and eeprom reading */
2126 em28xx_card_setup(dev);
2128 /* Configure audio */
2129 em28xx_audio_analog_set(dev);
2131 /* configure the device */
2132 em28xx_config_i2c(dev);
2134 /* set default norm */
2135 dev->norm = em28xx_video_template.current_norm;
2137 maxw = norm_maxw(dev);
2138 maxh = norm_maxh(dev);
2140 /* set default image size */
2143 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2148 errCode = em28xx_config(dev);
2150 list_add_tail(&dev->devlist, &em28xx_devlist);
2152 /* allocate and fill video video_device struct */
2153 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2154 VID_TYPE_CAPTURE, "video");
2155 if (NULL == dev->vdev) {
2156 em28xx_errdev("cannot allocate video_device.\n");
2159 if (dev->tuner_type != TUNER_ABSENT)
2160 dev->vdev->type |= VID_TYPE_TUNER;
2162 /* register v4l2 video video_device */
2163 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2164 video_nr[dev->devno]);
2166 em28xx_errdev("unable to register video device (error=%i).\n",
2171 /* Allocate and fill vbi video_device struct */
2172 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2173 VFL_TYPE_VBI, "vbi");
2174 /* register v4l2 vbi video_device */
2175 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2176 vbi_nr[dev->devno]) < 0) {
2177 em28xx_errdev("unable to register vbi device\n");
2182 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2183 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2184 VFL_TYPE_RADIO, "radio");
2185 if (NULL == dev->radio_dev) {
2186 em28xx_errdev("cannot allocate video_device.\n");
2189 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2190 radio_nr[dev->devno]);
2192 em28xx_errdev("can't register radio device\n");
2195 em28xx_info("Registered radio device as /dev/radio%d\n",
2196 dev->radio_dev->minor & 0x1f);
2199 /* init video dma queues */
2200 INIT_LIST_HEAD(&dev->vidq.active);
2201 INIT_LIST_HEAD(&dev->vidq.queued);
2204 if (dev->has_msp34xx) {
2205 /* Send a reset to other chips via gpio */
2206 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2208 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2214 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2215 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2216 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2218 mutex_lock(&em28xx_extension_devlist_lock);
2219 if (!list_empty(&em28xx_extension_devlist)) {
2220 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2225 mutex_unlock(&em28xx_extension_devlist_lock);
2230 em28xx_release_resources(dev);
2231 mutex_unlock(&dev->lock);
2236 #if defined(CONFIG_MODULES) && defined(MODULE)
2237 static void request_module_async(struct work_struct *work)
2239 struct em28xx *dev = container_of(work,
2240 struct em28xx, request_module_wk);
2242 if (dev->has_audio_class)
2243 request_module("snd-usb-audio");
2245 request_module("em28xx-alsa");
2248 request_module("em28xx-dvb");
2251 static void request_modules(struct em28xx *dev)
2253 INIT_WORK(&dev->request_module_wk, request_module_async);
2254 schedule_work(&dev->request_module_wk);
2257 #define request_modules(dev)
2258 #endif /* CONFIG_MODULES */
2261 * em28xx_usb_probe()
2262 * checks for supported devices
2264 static int em28xx_usb_probe(struct usb_interface *interface,
2265 const struct usb_device_id *id)
2267 const struct usb_endpoint_descriptor *endpoint;
2268 struct usb_device *udev;
2269 struct usb_interface *uif;
2270 struct em28xx *dev = NULL;
2271 int retval = -ENODEV;
2274 udev = usb_get_dev(interface_to_usbdev(interface));
2275 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2277 /* Check to see next free device and mark as used */
2278 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2279 em28xx_devused|=1<<nr;
2281 /* Don't register audio interfaces */
2282 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2283 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2284 udev->descriptor.idVendor,udev->descriptor.idProduct,
2286 interface->altsetting[0].desc.bInterfaceClass);
2288 em28xx_devused&=~(1<<nr);
2292 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2293 udev->descriptor.idVendor,udev->descriptor.idProduct,
2295 interface->altsetting[0].desc.bInterfaceClass);
2297 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2299 /* check if the device has the iso in endpoint at the correct place */
2300 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2301 USB_ENDPOINT_XFER_ISOC) {
2302 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2303 em28xx_devused&=~(1<<nr);
2306 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2307 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2308 em28xx_devused&=~(1<<nr);
2312 if (nr >= EM28XX_MAXBOARDS) {
2313 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2314 em28xx_devused&=~(1<<nr);
2318 /* allocate memory for our device state and initialize it */
2319 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2321 em28xx_err(DRIVER_NAME ": out of memory!\n");
2322 em28xx_devused&=~(1<<nr);
2326 snprintf(dev->name, 29, "em28xx #%d", nr);
2328 dev->model = id->driver_info;
2331 /* Checks if audio is provided by some interface */
2332 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2333 uif = udev->config->interface[i];
2334 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2335 dev->has_audio_class = 1;
2340 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2341 dev->has_audio_class ? "Has" : "Doesn't have");
2343 /* compute alternate max packet sizes */
2344 uif = udev->actconfig->interface[0];
2346 dev->num_alt=uif->num_altsetting;
2347 em28xx_info("Alternate settings: %i\n",dev->num_alt);
2348 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2349 dev->alt_max_pkt_size = kmalloc(32*
2350 dev->num_alt,GFP_KERNEL);
2351 if (dev->alt_max_pkt_size == NULL) {
2352 em28xx_errdev("out of memory!\n");
2353 em28xx_devused&=~(1<<nr);
2358 for (i = 0; i < dev->num_alt ; i++) {
2359 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2361 dev->alt_max_pkt_size[i] =
2362 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2363 em28xx_info("Alternate setting %i, max size= %i\n",i,
2364 dev->alt_max_pkt_size[i]);
2367 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2368 dev->model = card[nr];
2370 /* allocate device struct */
2371 retval = em28xx_init_dev(&dev, udev, nr);
2375 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2377 /* save our data pointer in this interface device */
2378 usb_set_intfdata(interface, dev);
2380 #if defined(CONFIG_VIDEO_EM28XX_DVB) || defined(CONFIG_VIDEO_EM28XX_DVB_MODULE)
2381 dev->qops = kmalloc(sizeof(em28xx_video_qops), GFP_KERNEL);
2382 memcpy(dev->qops, &em28xx_video_qops, sizeof(em28xx_video_qops));
2385 request_modules(dev);
2391 * em28xx_usb_disconnect()
2392 * called when the device gets diconencted
2393 * video device will be unregistered on v4l2_close in case it is still open
2395 static void em28xx_usb_disconnect(struct usb_interface *interface)
2398 struct em28xx_ops *ops = NULL;
2400 dev = usb_get_intfdata(interface);
2401 usb_set_intfdata(interface, NULL);
2406 em28xx_info("disconnecting %s\n", dev->vdev->name);
2408 /* wait until all current v4l2 io is finished then deallocate resources */
2409 mutex_lock(&dev->lock);
2411 wake_up_interruptible_all(&dev->open);
2415 ("device /dev/video%d is open! Deregistration and memory "
2416 "deallocation are deferred on close.\n",
2417 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2419 dev->state |= DEV_MISCONFIGURED;
2420 em28xx_uninit_isoc(dev);
2421 dev->state |= DEV_DISCONNECTED;
2422 wake_up_interruptible(&dev->wait_frame);
2423 wake_up_interruptible(&dev->wait_stream);
2425 dev->state |= DEV_DISCONNECTED;
2426 em28xx_release_resources(dev);
2428 mutex_unlock(&dev->lock);
2430 mutex_lock(&em28xx_extension_devlist_lock);
2431 if (!list_empty(&em28xx_extension_devlist)) {
2432 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2436 mutex_unlock(&em28xx_extension_devlist_lock);
2439 kfree(dev->alt_max_pkt_size);
2444 static struct usb_driver em28xx_usb_driver = {
2446 .probe = em28xx_usb_probe,
2447 .disconnect = em28xx_usb_disconnect,
2448 .id_table = em28xx_id_table,
2451 static int __init em28xx_module_init(void)
2455 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2456 (EM28XX_VERSION_CODE >> 16) & 0xff,
2457 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2459 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2460 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2463 /* register this driver with the USB subsystem */
2464 result = usb_register(&em28xx_usb_driver);
2466 em28xx_err(DRIVER_NAME
2467 " usb_register failed. Error number %d.\n", result);
2472 static void __exit em28xx_module_exit(void)
2474 /* deregister this driver with the USB subsystem */
2475 usb_deregister(&em28xx_usb_driver);
2478 module_init(em28xx_module_init);
2479 module_exit(em28xx_module_exit);