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 /* Limits minimum and default number of buffers */
67 #define EM28XX_MIN_BUF 4
68 #define EM28XX_DEF_BUF 8
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
74 static LIST_HEAD(em28xx_devlist);
76 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81 module_param_array(card, int, NULL, 0444);
82 module_param_array(video_nr, int, NULL, 0444);
83 module_param_array(vbi_nr, int, NULL, 0444);
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(card, "card type");
86 MODULE_PARM_DESC(video_nr, "video device numbers");
87 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88 MODULE_PARM_DESC(radio_nr, "radio device numbers");
90 static unsigned int video_debug;
91 module_param(video_debug,int,0644);
92 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
94 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95 static unsigned long em28xx_devused;
97 /* supported controls */
98 /* Common to all boards */
99 static struct v4l2_queryctrl em28xx_qctrl[] = {
101 .id = V4L2_CID_AUDIO_VOLUME,
102 .type = V4L2_CTRL_TYPE_INTEGER,
107 .default_value = 0x1f,
110 .id = V4L2_CID_AUDIO_MUTE,
111 .type = V4L2_CTRL_TYPE_BOOLEAN,
121 static struct usb_driver em28xx_usb_driver;
123 /* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
128 * Announces that a buffer were filled and request the next
130 static inline void buffer_filled(struct em28xx *dev,
131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
140 dev->isoc_ctl.buf = NULL;
142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
147 * Identify the buffer header type and properly handles
149 static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
153 unsigned char *outp, unsigned long len)
155 void *fieldstart, *startwrite, *startread;
156 int linesdone, currlinedone, offset, lencopy, remain;
158 if (dev->frame_size != buf->vb.size) {
159 em28xx_errdev("size %i and buf.length %lu are different!\n",
160 dev->frame_size, buf->vb.size);
164 if (dma_q->pos + len > buf->vb.size)
165 len = buf->vb.size - dma_q->pos;
167 if (p[0] != 0x88 && p[0] != 0x22) {
168 em28xx_isocdbg("frame is not complete\n");
176 /* Interlaces frame */
180 fieldstart = outp + dev->bytesperline;
182 linesdone = dma_q->pos / dev->bytesperline;
183 currlinedone = dma_q->pos % dev->bytesperline;
184 offset = linesdone * dev->bytesperline * 2 + currlinedone;
185 startwrite = fieldstart + offset;
186 lencopy = dev->bytesperline - currlinedone;
187 lencopy = lencopy > remain ? remain : lencopy;
189 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
190 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
191 ((char *)startwrite + lencopy) -
192 ((char *)outp + buf->vb.size));
193 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
197 memcpy(startwrite, startread, lencopy);
202 startwrite += lencopy + dev->bytesperline;
203 startread += lencopy;
204 if (dev->bytesperline > remain)
207 lencopy = dev->bytesperline;
209 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
210 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
211 ((char *)startwrite + lencopy) -
212 ((char *)outp + buf->vb.size));
213 lencopy = remain = (char *)outp + buf->vb.size -
219 memcpy(startwrite, startread, lencopy);
227 static inline void print_err_status(struct em28xx *dev,
228 int packet, int status)
230 char *errmsg = "Unknown";
234 errmsg = "unlinked synchronuously";
237 errmsg = "unlinked asynchronuously";
240 errmsg = "Buffer error (overrun)";
243 errmsg = "Stalled (device not responding)";
246 errmsg = "Babble (bad cable?)";
249 errmsg = "Bit-stuff error (bad cable?)";
252 errmsg = "CRC/Timeout (could be anything)";
255 errmsg = "Device does not respond";
259 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
261 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
262 packet, status, errmsg);
267 * video-buf generic routine to get the next available buffer
269 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
270 struct em28xx_buffer **buf)
272 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
275 if (list_empty(&dma_q->active)) {
276 em28xx_isocdbg("No active queue to serve\n");
277 dev->isoc_ctl.buf = NULL;
282 /* Get the next buffer */
283 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
285 /* Cleans up buffer - Usefull for testing for frame/URB loss */
286 outp = videobuf_to_vmalloc(&(*buf)->vb);
287 memset(outp, 0, (*buf)->vb.size);
289 dev->isoc_ctl.buf = *buf;
295 * Controls the isoc copy of each urb packet
297 static inline int em28xx_isoc_copy(struct urb *urb)
299 struct em28xx_buffer *buf;
300 struct em28xx_dmaqueue *dma_q = urb->context;
301 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
302 unsigned char *outp = NULL;
303 int i, len = 0, rc = 1;
309 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
312 if (urb->status < 0) {
313 print_err_status(dev, -1, urb->status);
314 if (urb->status == -ENOENT)
318 buf = dev->isoc_ctl.buf;
320 outp = videobuf_to_vmalloc(&buf->vb);
322 for (i = 0; i < urb->number_of_packets; i++) {
323 int status = urb->iso_frame_desc[i].status;
326 print_err_status(dev, i, status);
327 if (urb->iso_frame_desc[i].status != -EPROTO)
331 len = urb->iso_frame_desc[i].actual_length - 4;
333 if (urb->iso_frame_desc[i].actual_length <= 0) {
334 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
337 if (urb->iso_frame_desc[i].actual_length >
339 em28xx_isocdbg("packet bigger than packet size");
343 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
345 /* FIXME: incomplete buffer checks where removed to make
346 logic simpler. Impacts of those changes should be evaluated
348 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
349 em28xx_isocdbg("VBI HEADER!!!\n");
350 /* FIXME: Should add vbi copy */
353 if (p[0] == 0x22 && p[1] == 0x5a) {
354 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
355 len, (p[2] & 1)? "odd" : "even");
359 buffer_filled(dev, dma_q, buf);
360 get_next_buf(dma_q, &buf);
364 outp = videobuf_to_vmalloc(&buf->vb);
377 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
382 /* ------------------------------------------------------------------
384 ------------------------------------------------------------------*/
387 * IRQ callback, called by URB callback
389 static void em28xx_irq_callback(struct urb *urb)
391 struct em28xx_dmaqueue *dma_q = urb->context;
392 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
395 /* Copy data from URB */
396 spin_lock(&dev->slock);
397 rc = em28xx_isoc_copy(urb);
398 spin_unlock(&dev->slock);
400 /* Reset urb buffers */
401 for (i = 0; i < urb->number_of_packets; i++) {
402 urb->iso_frame_desc[i].status = 0;
403 urb->iso_frame_desc[i].actual_length = 0;
407 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
409 em28xx_err("urb resubmit failed (error=%i)\n",
415 * Stop and Deallocate URBs
417 static void em28xx_uninit_isoc(struct em28xx *dev)
422 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
424 dev->isoc_ctl.nfields = -1;
425 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
426 urb = dev->isoc_ctl.urb[i];
430 if (dev->isoc_ctl.transfer_buffer[i]) {
431 usb_buffer_free(dev->udev,
432 urb->transfer_buffer_length,
433 dev->isoc_ctl.transfer_buffer[i],
437 dev->isoc_ctl.urb[i] = NULL;
439 dev->isoc_ctl.transfer_buffer[i] = NULL;
442 kfree(dev->isoc_ctl.urb);
443 kfree(dev->isoc_ctl.transfer_buffer);
444 dev->isoc_ctl.urb = NULL;
445 dev->isoc_ctl.transfer_buffer = NULL;
447 dev->isoc_ctl.num_bufs = 0;
449 em28xx_capture_start(dev, 0);
453 * Allocate URBs and start IRQ
455 static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
458 struct em28xx_dmaqueue *dma_q = &dev->vidq;
464 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
466 /* De-allocates all pending stuff */
467 em28xx_uninit_isoc(dev);
469 dev->isoc_ctl.num_bufs = num_bufs;
471 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
472 if (!dev->isoc_ctl.urb) {
473 em28xx_errdev("cannot alloc memory for usb buffers\n");
477 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
479 if (!dev->isoc_ctl.urb) {
480 em28xx_errdev("cannot allocate memory for usbtransfer\n");
481 kfree(dev->isoc_ctl.urb);
485 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
486 dev->isoc_ctl.buf = NULL;
488 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
490 /* allocate urbs and transfer buffers */
491 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
492 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
494 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
495 em28xx_uninit_isoc(dev);
498 dev->isoc_ctl.urb[i] = urb;
500 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
501 sb_size, GFP_KERNEL, &urb->transfer_dma);
502 if (!dev->isoc_ctl.transfer_buffer[i]) {
503 em28xx_err("unable to allocate %i bytes for transfer"
506 in_interrupt()?" while in int":"");
507 em28xx_uninit_isoc(dev);
510 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
512 /* FIXME: this is a hack - should be
513 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
514 should also be using 'desc.bInterval'
516 pipe = usb_rcvisocpipe(dev->udev, 0x82);
517 usb_fill_int_urb(urb, dev->udev, pipe,
518 dev->isoc_ctl.transfer_buffer[i], sb_size,
519 em28xx_irq_callback, dma_q, 1);
521 urb->number_of_packets = max_packets;
522 urb->transfer_flags = URB_ISO_ASAP;
525 for (j = 0; j < max_packets; j++) {
526 urb->iso_frame_desc[j].offset = k;
527 urb->iso_frame_desc[j].length =
528 dev->isoc_ctl.max_pkt_size;
529 k += dev->isoc_ctl.max_pkt_size;
536 static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
538 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
541 em28xx_videodbg("Called em28xx_start_thread\n");
543 init_waitqueue_head(&dma_q->wq);
545 em28xx_capture_start(dev, 1);
547 /* submit urbs and enables IRQ */
548 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
549 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
551 em28xx_err("submit of urb %i failed (error=%i)\n", i,
553 em28xx_uninit_isoc(dev);
564 /* ------------------------------------------------------------------
566 ------------------------------------------------------------------*/
569 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
571 struct em28xx_fh *fh = vq->priv_data;
573 *size = 16 * fh->dev->width * fh->dev->height >> 3;
575 *count = EM28XX_DEF_BUF;
577 if (*count < EM28XX_MIN_BUF)
578 *count = EM28XX_MIN_BUF;
583 /* This is called *without* dev->slock held; please keep it that way */
584 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
586 struct em28xx_fh *fh = vq->priv_data;
587 struct em28xx *dev = fh->dev;
588 unsigned long flags = 0;
592 /* We used to wait for the buffer to finish here, but this didn't work
593 because, as we were keeping the state as VIDEOBUF_QUEUED,
594 videobuf_queue_cancel marked it as finished for us.
595 (Also, it could wedge forever if the hardware was misconfigured.)
597 This should be safe; by the time we get here, the buffer isn't
598 queued anymore. If we ever start marking the buffers as
599 VIDEOBUF_ACTIVE, it won't be, though.
601 spin_lock_irqsave(&dev->slock, flags);
602 if (dev->isoc_ctl.buf == buf)
603 dev->isoc_ctl.buf = NULL;
604 spin_unlock_irqrestore(&dev->slock, flags);
606 videobuf_vmalloc_free(&buf->vb);
607 buf->vb.state = VIDEOBUF_NEEDS_INIT;
611 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
612 enum v4l2_field field)
614 struct em28xx_fh *fh = vq->priv_data;
615 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
616 struct em28xx *dev = fh->dev;
617 struct em28xx_dmaqueue *vidq = &dev->vidq;
618 int rc = 0, urb_init = 0;
620 /* BUG_ON(NULL == fh->fmt); */
622 /* FIXME: It assumes depth = 16 */
623 /* The only currently supported format is 16 bits/pixel */
624 buf->vb.size = 16 * dev->width * dev->height >> 3;
626 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
630 buf->vb.width = dev->width;
631 buf->vb.height = dev->height;
632 buf->vb.field = field;
634 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
635 rc = videobuf_iolock(vq, &buf->vb, NULL);
640 if (!dev->isoc_ctl.num_bufs)
644 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
649 rc = em28xx_start_thread(vidq);
654 buf->vb.state = VIDEOBUF_PREPARED;
658 free_buffer(vq, buf);
663 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
665 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
666 struct em28xx_fh *fh = vq->priv_data;
667 struct em28xx *dev = fh->dev;
668 struct em28xx_dmaqueue *vidq = &dev->vidq;
670 buf->vb.state = VIDEOBUF_QUEUED;
671 list_add_tail(&buf->vb.queue, &vidq->active);
675 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
677 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
678 struct em28xx_fh *fh = vq->priv_data;
679 struct em28xx *dev = (struct em28xx *)fh->dev;
681 em28xx_isocdbg("em28xx: called buffer_release\n");
683 free_buffer(vq, buf);
686 static struct videobuf_queue_ops em28xx_video_qops = {
687 .buf_setup = buffer_setup,
688 .buf_prepare = buffer_prepare,
689 .buf_queue = buffer_queue,
690 .buf_release = buffer_release,
693 /********************* v4l2 interface ******************************************/
697 * inits registers with sane defaults
699 static int em28xx_config(struct em28xx *dev)
702 /* Sets I2C speed to 100 KHz */
704 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
706 /* enable vbi capturing */
708 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
709 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
710 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
712 dev->mute = 1; /* maybe not the right place... */
715 em28xx_outfmt_set_yuv422(dev);
716 em28xx_colorlevels_set_default(dev);
717 em28xx_compression_disable(dev);
723 * em28xx_config_i2c()
724 * configure i2c attached devices
726 static void em28xx_config_i2c(struct em28xx *dev)
728 struct v4l2_routing route;
730 route.input = INPUT(dev->ctl_input)->vmux;
732 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
733 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
734 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
737 static void video_mux(struct em28xx *dev, int index)
739 struct v4l2_routing route;
741 route.input = INPUT(index)->vmux;
743 dev->ctl_input = index;
744 dev->ctl_ainput = INPUT(index)->amux;
746 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
748 if (dev->has_msp34xx) {
750 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
751 route.input = dev->ctl_ainput;
752 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
753 /* Note: this is msp3400 specific */
754 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
757 em28xx_audio_analog_set(dev);
760 /* Usage lock check functions */
761 static int res_get(struct em28xx_fh *fh)
763 struct em28xx *dev = fh->dev;
766 /* This instance already has stream_on */
773 mutex_lock(&dev->lock);
776 mutex_unlock(&dev->lock);
780 static int res_check(struct em28xx_fh *fh)
782 return (fh->stream_on);
785 static void res_free(struct em28xx_fh *fh)
787 struct em28xx *dev = fh->dev;
789 mutex_lock(&dev->lock);
792 mutex_unlock(&dev->lock);
797 * return the current saturation, brightness or contrast, mute state
799 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
802 case V4L2_CID_AUDIO_MUTE:
803 ctrl->value = dev->mute;
805 case V4L2_CID_AUDIO_VOLUME:
806 ctrl->value = dev->volume;
815 * mute or set new saturation, brightness or contrast
817 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
820 case V4L2_CID_AUDIO_MUTE:
821 if (ctrl->value != dev->mute) {
822 dev->mute = ctrl->value;
823 return em28xx_audio_analog_set(dev);
826 case V4L2_CID_AUDIO_VOLUME:
827 dev->volume = ctrl->value;
828 return em28xx_audio_analog_set(dev);
834 static int check_dev(struct em28xx *dev)
836 if (dev->state & DEV_DISCONNECTED) {
837 em28xx_errdev("v4l2 ioctl: device not present\n");
841 if (dev->state & DEV_MISCONFIGURED) {
842 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
843 "close and open it again\n");
849 static void get_scale(struct em28xx *dev,
850 unsigned int width, unsigned int height,
851 unsigned int *hscale, unsigned int *vscale)
853 unsigned int maxw = norm_maxw(dev);
854 unsigned int maxh = norm_maxh(dev);
856 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
857 if (*hscale >= 0x4000)
860 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
861 if (*vscale >= 0x4000)
865 /* ------------------------------------------------------------------
866 IOCTL vidioc handling
867 ------------------------------------------------------------------*/
869 static int vidioc_g_fmt_cap(struct file *file, void *priv,
870 struct v4l2_format *f)
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
875 mutex_lock(&dev->lock);
877 f->fmt.pix.width = dev->width;
878 f->fmt.pix.height = dev->height;
879 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
880 f->fmt.pix.bytesperline = dev->bytesperline;
881 f->fmt.pix.sizeimage = dev->frame_size;
882 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
884 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
885 f->fmt.pix.field = dev->interlaced ?
886 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
888 mutex_unlock(&dev->lock);
892 static int vidioc_try_fmt_cap(struct file *file, void *priv,
893 struct v4l2_format *f)
895 struct em28xx_fh *fh = priv;
896 struct em28xx *dev = fh->dev;
897 int width = f->fmt.pix.width;
898 int height = f->fmt.pix.height;
899 unsigned int maxw = norm_maxw(dev);
900 unsigned int maxh = norm_maxh(dev);
901 unsigned int hscale, vscale;
903 /* width must even because of the YUYV format
904 height must be even because of interlacing */
917 mutex_lock(&dev->lock);
919 if (dev->is_em2800) {
920 /* the em2800 can only scale down to 50% */
921 if (height % (maxh / 2))
923 if (width % (maxw / 2))
925 /* according to empiatech support */
926 /* the MaxPacketSize is to small to support */
927 /* framesizes larger than 640x480 @ 30 fps */
928 /* or 640x576 @ 25 fps. As this would cut */
929 /* of a part of the image we prefer */
930 /* 360x576 or 360x480 for now */
931 if (width == maxw && height == maxh)
935 get_scale(dev, width, height, &hscale, &vscale);
937 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
938 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
940 f->fmt.pix.width = width;
941 f->fmt.pix.height = height;
942 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
943 f->fmt.pix.bytesperline = width * 2;
944 f->fmt.pix.sizeimage = width * 2 * height;
945 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
946 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
948 mutex_unlock(&dev->lock);
952 static int vidioc_s_fmt_cap(struct file *file, void *priv,
953 struct v4l2_format *f)
955 struct em28xx_fh *fh = priv;
956 struct em28xx *dev = fh->dev;
963 vidioc_try_fmt_cap(file, priv, f);
965 mutex_lock(&dev->lock);
967 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
968 em28xx_errdev("%s queue busy\n", __func__);
973 if (dev->stream_on && !fh->stream_on) {
974 em28xx_errdev("%s device in use by another fh\n", __func__);
979 /* set new image size */
980 dev->width = f->fmt.pix.width;
981 dev->height = f->fmt.pix.height;
982 dev->frame_size = dev->width * dev->height * 2;
983 dev->field_size = dev->frame_size >> 1;
984 dev->bytesperline = dev->width * 2;
985 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
987 em28xx_set_alternate(dev);
988 em28xx_resolution_set(dev);
993 mutex_unlock(&dev->lock);
997 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
999 struct em28xx_fh *fh = priv;
1000 struct em28xx *dev = fh->dev;
1001 struct v4l2_format f;
1004 rc = check_dev(dev);
1008 mutex_lock(&dev->lock);
1010 mutex_unlock(&dev->lock);
1012 /* Adjusts width/height, if needed */
1013 f.fmt.pix.width = dev->width;
1014 f.fmt.pix.height = dev->height;
1015 vidioc_try_fmt_cap(file, priv, &f);
1017 mutex_lock(&dev->lock);
1019 /* set new image size */
1020 dev->width = f.fmt.pix.width;
1021 dev->height = f.fmt.pix.height;
1022 dev->frame_size = dev->width * dev->height * 2;
1023 dev->field_size = dev->frame_size >> 1;
1024 dev->bytesperline = dev->width * 2;
1025 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1027 em28xx_resolution_set(dev);
1028 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1030 mutex_unlock(&dev->lock);
1034 static const char *iname[] = {
1035 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1036 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1037 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1038 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1039 [EM28XX_VMUX_SVIDEO] = "S-Video",
1040 [EM28XX_VMUX_TELEVISION] = "Television",
1041 [EM28XX_VMUX_CABLE] = "Cable TV",
1042 [EM28XX_VMUX_DVB] = "DVB",
1043 [EM28XX_VMUX_DEBUG] = "for debug only",
1046 static int vidioc_enum_input(struct file *file, void *priv,
1047 struct v4l2_input *i)
1049 struct em28xx_fh *fh = priv;
1050 struct em28xx *dev = fh->dev;
1054 if (n >= MAX_EM28XX_INPUT)
1056 if (0 == INPUT(n)->type)
1060 i->type = V4L2_INPUT_TYPE_CAMERA;
1062 strcpy(i->name, iname[INPUT(n)->type]);
1064 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1065 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1066 i->type = V4L2_INPUT_TYPE_TUNER;
1068 i->std = dev->vdev->tvnorms;
1073 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1075 struct em28xx_fh *fh = priv;
1076 struct em28xx *dev = fh->dev;
1078 *i = dev->ctl_input;
1083 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1085 struct em28xx_fh *fh = priv;
1086 struct em28xx *dev = fh->dev;
1089 rc = check_dev(dev);
1093 if (i >= MAX_EM28XX_INPUT)
1095 if (0 == INPUT(i)->type)
1098 mutex_lock(&dev->lock);
1102 mutex_unlock(&dev->lock);
1106 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1108 struct em28xx_fh *fh = priv;
1109 struct em28xx *dev = fh->dev;
1110 unsigned int index = a->index;
1115 index = dev->ctl_ainput;
1118 strcpy(a->name, "Television");
1120 strcpy(a->name, "Line In");
1122 a->capability = V4L2_AUDCAP_STEREO;
1128 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1130 struct em28xx_fh *fh = priv;
1131 struct em28xx *dev = fh->dev;
1133 if (a->index != dev->ctl_ainput)
1139 static int vidioc_queryctrl(struct file *file, void *priv,
1140 struct v4l2_queryctrl *qc)
1142 struct em28xx_fh *fh = priv;
1143 struct em28xx *dev = fh->dev;
1148 rc = check_dev(dev);
1152 memset(qc, 0, sizeof(*qc));
1156 if (!dev->has_msp34xx) {
1157 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1158 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1159 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1164 mutex_lock(&dev->lock);
1165 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1166 mutex_unlock(&dev->lock);
1174 static int vidioc_g_ctrl(struct file *file, void *priv,
1175 struct v4l2_control *ctrl)
1177 struct em28xx_fh *fh = priv;
1178 struct em28xx *dev = fh->dev;
1181 rc = check_dev(dev);
1184 mutex_lock(&dev->lock);
1186 if (!dev->has_msp34xx)
1187 rc = em28xx_get_ctrl(dev, ctrl);
1191 if (rc == -EINVAL) {
1192 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1196 mutex_unlock(&dev->lock);
1200 static int vidioc_s_ctrl(struct file *file, void *priv,
1201 struct v4l2_control *ctrl)
1203 struct em28xx_fh *fh = priv;
1204 struct em28xx *dev = fh->dev;
1208 rc = check_dev(dev);
1212 mutex_lock(&dev->lock);
1214 if (dev->has_msp34xx)
1215 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1218 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1219 if (ctrl->id == em28xx_qctrl[i].id) {
1220 if (ctrl->value < em28xx_qctrl[i].minimum ||
1221 ctrl->value > em28xx_qctrl[i].maximum) {
1226 rc = em28xx_set_ctrl(dev, ctrl);
1232 /* Control not found - try to send it to the attached devices */
1234 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1238 mutex_unlock(&dev->lock);
1242 static int vidioc_g_tuner(struct file *file, void *priv,
1243 struct v4l2_tuner *t)
1245 struct em28xx_fh *fh = priv;
1246 struct em28xx *dev = fh->dev;
1249 rc = check_dev(dev);
1256 strcpy(t->name, "Tuner");
1258 mutex_lock(&dev->lock);
1260 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1262 mutex_unlock(&dev->lock);
1266 static int vidioc_s_tuner(struct file *file, void *priv,
1267 struct v4l2_tuner *t)
1269 struct em28xx_fh *fh = priv;
1270 struct em28xx *dev = fh->dev;
1273 rc = check_dev(dev);
1280 mutex_lock(&dev->lock);
1282 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1284 mutex_unlock(&dev->lock);
1288 static int vidioc_g_frequency(struct file *file, void *priv,
1289 struct v4l2_frequency *f)
1291 struct em28xx_fh *fh = priv;
1292 struct em28xx *dev = fh->dev;
1294 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1295 f->frequency = dev->ctl_freq;
1300 static int vidioc_s_frequency(struct file *file, void *priv,
1301 struct v4l2_frequency *f)
1303 struct em28xx_fh *fh = priv;
1304 struct em28xx *dev = fh->dev;
1307 rc = check_dev(dev);
1314 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1316 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1319 mutex_lock(&dev->lock);
1321 dev->ctl_freq = f->frequency;
1322 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1324 mutex_unlock(&dev->lock);
1328 #ifdef CONFIG_VIDEO_ADV_DEBUG
1329 static int em28xx_reg_len(int reg)
1341 static int vidioc_g_register(struct file *file, void *priv,
1342 struct v4l2_register *reg)
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
1348 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1351 if (em28xx_reg_len(reg->reg) == 1) {
1352 ret = em28xx_read_reg(dev, reg->reg);
1359 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1360 reg->reg, (char *)&val, 2);
1364 reg->val = cpu_to_le64((__u64)val);
1370 static int vidioc_s_register(struct file *file, void *priv,
1371 struct v4l2_register *reg)
1373 struct em28xx_fh *fh = priv;
1374 struct em28xx *dev = fh->dev;
1377 buf = le64_to_cpu((__u64)reg->val);
1379 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1380 em28xx_reg_len(reg->reg));
1385 static int vidioc_cropcap(struct file *file, void *priv,
1386 struct v4l2_cropcap *cc)
1388 struct em28xx_fh *fh = priv;
1389 struct em28xx *dev = fh->dev;
1391 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 cc->bounds.left = 0;
1396 cc->bounds.width = dev->width;
1397 cc->bounds.height = dev->height;
1398 cc->defrect = cc->bounds;
1399 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1400 cc->pixelaspect.denominator = 59;
1405 static int vidioc_streamon(struct file *file, void *priv,
1406 enum v4l2_buf_type type)
1408 struct em28xx_fh *fh = priv;
1409 struct em28xx *dev = fh->dev;
1412 rc = check_dev(dev);
1417 if (unlikely(res_get(fh) < 0))
1420 return (videobuf_streamon(&fh->vb_vidq));
1423 static int vidioc_streamoff(struct file *file, void *priv,
1424 enum v4l2_buf_type type)
1426 struct em28xx_fh *fh = priv;
1427 struct em28xx *dev = fh->dev;
1430 rc = check_dev(dev);
1434 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1436 if (type != fh->type)
1439 videobuf_streamoff(&fh->vb_vidq);
1445 static int vidioc_querycap(struct file *file, void *priv,
1446 struct v4l2_capability *cap)
1448 struct em28xx_fh *fh = priv;
1449 struct em28xx *dev = fh->dev;
1451 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1452 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1453 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1455 cap->version = EM28XX_VERSION_CODE;
1458 V4L2_CAP_SLICED_VBI_CAPTURE |
1459 V4L2_CAP_VIDEO_CAPTURE |
1461 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1463 if (dev->tuner_type != TUNER_ABSENT)
1464 cap->capabilities |= V4L2_CAP_TUNER;
1469 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1470 struct v4l2_fmtdesc *fmtd)
1472 if (fmtd->index != 0)
1475 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1476 strcpy(fmtd->description, "Packed YUY2");
1477 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1478 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1483 /* Sliced VBI ioctls */
1484 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1485 struct v4l2_format *f)
1487 struct em28xx_fh *fh = priv;
1488 struct em28xx *dev = fh->dev;
1491 rc = check_dev(dev);
1495 mutex_lock(&dev->lock);
1497 f->fmt.sliced.service_set = 0;
1499 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1501 if (f->fmt.sliced.service_set == 0)
1504 mutex_unlock(&dev->lock);
1508 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1509 struct v4l2_format *f)
1511 struct em28xx_fh *fh = priv;
1512 struct em28xx *dev = fh->dev;
1515 rc = check_dev(dev);
1519 mutex_lock(&dev->lock);
1520 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1521 mutex_unlock(&dev->lock);
1523 if (f->fmt.sliced.service_set == 0)
1530 static int vidioc_reqbufs(struct file *file, void *priv,
1531 struct v4l2_requestbuffers *rb)
1533 struct em28xx_fh *fh = priv;
1534 struct em28xx *dev = fh->dev;
1537 rc = check_dev(dev);
1541 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1544 static int vidioc_querybuf(struct file *file, void *priv,
1545 struct v4l2_buffer *b)
1547 struct em28xx_fh *fh = priv;
1548 struct em28xx *dev = fh->dev;
1551 rc = check_dev(dev);
1555 return (videobuf_querybuf(&fh->vb_vidq, b));
1558 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1560 struct em28xx_fh *fh = priv;
1561 struct em28xx *dev = fh->dev;
1564 rc = check_dev(dev);
1568 return (videobuf_qbuf(&fh->vb_vidq, b));
1571 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1573 struct em28xx_fh *fh = priv;
1574 struct em28xx *dev = fh->dev;
1577 rc = check_dev(dev);
1581 return (videobuf_dqbuf(&fh->vb_vidq, b,
1582 file->f_flags & O_NONBLOCK));
1585 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1586 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1588 struct em28xx_fh *fh = priv;
1590 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1595 /* ----------------------------------------------------------- */
1596 /* RADIO ESPECIFIC IOCTLS */
1597 /* ----------------------------------------------------------- */
1599 static int radio_querycap(struct file *file, void *priv,
1600 struct v4l2_capability *cap)
1602 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1604 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1605 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1606 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1608 cap->version = EM28XX_VERSION_CODE;
1609 cap->capabilities = V4L2_CAP_TUNER;
1613 static int radio_g_tuner(struct file *file, void *priv,
1614 struct v4l2_tuner *t)
1616 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1618 if (unlikely(t->index > 0))
1621 strcpy(t->name, "Radio");
1622 t->type = V4L2_TUNER_RADIO;
1624 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1628 static int radio_enum_input(struct file *file, void *priv,
1629 struct v4l2_input *i)
1633 strcpy(i->name, "Radio");
1634 i->type = V4L2_INPUT_TYPE_TUNER;
1639 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1641 if (unlikely(a->index))
1644 strcpy(a->name, "Radio");
1648 static int radio_s_tuner(struct file *file, void *priv,
1649 struct v4l2_tuner *t)
1651 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1656 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1661 static int radio_s_audio(struct file *file, void *fh,
1662 struct v4l2_audio *a)
1667 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1672 static int radio_queryctrl(struct file *file, void *priv,
1673 struct v4l2_queryctrl *qc)
1677 if (qc->id < V4L2_CID_BASE ||
1678 qc->id >= V4L2_CID_LASTP1)
1681 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1682 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1683 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1692 * em28xx_v4l2_open()
1693 * inits the device and starts isoc transfer
1695 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1697 int minor = iminor(inode);
1698 int errCode = 0, radio = 0;
1699 struct em28xx *h,*dev = NULL;
1700 struct em28xx_fh *fh;
1701 enum v4l2_buf_type fh_type = 0;
1703 list_for_each_entry(h, &em28xx_devlist, devlist) {
1704 if (h->vdev->minor == minor) {
1706 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1708 if (h->vbi_dev->minor == minor) {
1710 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1713 h->radio_dev->minor == minor) {
1721 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1722 minor, v4l2_type_names[fh_type], dev->users);
1724 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1727 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1730 mutex_lock(&dev->lock);
1734 filp->private_data = fh;
1736 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1737 dev->width = norm_maxw(dev);
1738 dev->height = norm_maxh(dev);
1739 dev->frame_size = dev->width * dev->height * 2;
1740 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1741 dev->bytesperline = dev->width * 2;
1745 em28xx_set_alternate(dev);
1746 em28xx_resolution_set(dev);
1750 em28xx_videodbg("video_open: setting radio device\n");
1751 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1756 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1757 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1758 sizeof(struct em28xx_buffer), fh);
1760 mutex_unlock(&dev->lock);
1765 * em28xx_realease_resources()
1766 * unregisters the v4l2,i2c and usb devices
1767 * called when the device gets disconected or at module unload
1769 static void em28xx_release_resources(struct em28xx *dev)
1772 /*FIXME: I2C IR should be disconnected */
1774 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1775 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1776 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1777 list_del(&dev->devlist);
1778 if (dev->radio_dev) {
1779 if (-1 != dev->radio_dev->minor)
1780 video_unregister_device(dev->radio_dev);
1782 video_device_release(dev->radio_dev);
1783 dev->radio_dev = NULL;
1786 if (-1 != dev->vbi_dev->minor)
1787 video_unregister_device(dev->vbi_dev);
1789 video_device_release(dev->vbi_dev);
1790 dev->vbi_dev = NULL;
1793 if (-1 != dev->vdev->minor)
1794 video_unregister_device(dev->vdev);
1796 video_device_release(dev->vdev);
1799 em28xx_i2c_unregister(dev);
1800 usb_put_dev(dev->udev);
1802 /* Mark device as unused */
1803 em28xx_devused&=~(1<<dev->devno);
1807 * em28xx_v4l2_close()
1808 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1810 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1812 struct em28xx_fh *fh = filp->private_data;
1813 struct em28xx *dev = fh->dev;
1816 em28xx_videodbg("users=%d\n", dev->users);
1822 mutex_lock(&dev->lock);
1824 if (dev->users == 1) {
1825 videobuf_stop(&fh->vb_vidq);
1826 videobuf_mmap_free(&fh->vb_vidq);
1828 /* the device is already disconnect,
1829 free the remaining resources */
1830 if (dev->state & DEV_DISCONNECTED) {
1831 em28xx_release_resources(dev);
1832 mutex_unlock(&dev->lock);
1837 /* do this before setting alternate! */
1838 em28xx_uninit_isoc(dev);
1840 /* set alternate 0 */
1842 em28xx_videodbg("setting alternate 0\n");
1843 errCode = usb_set_interface(dev->udev, 0, 0);
1845 em28xx_errdev("cannot change alternate number to "
1846 "0 (error=%i)\n", errCode);
1851 wake_up_interruptible_nr(&dev->open, 1);
1852 mutex_unlock(&dev->lock);
1857 * em28xx_v4l2_read()
1858 * will allocate buffers when called for the first time
1861 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1864 struct em28xx_fh *fh = filp->private_data;
1865 struct em28xx *dev = fh->dev;
1868 rc = check_dev(dev);
1872 /* FIXME: read() is not prepared to allow changing the video
1873 resolution while streaming. Seems a bug at em28xx_set_fmt
1876 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1877 if (unlikely(res_get(fh)))
1880 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1881 filp->f_flags & O_NONBLOCK);
1887 * em28xx_v4l2_poll()
1888 * will allocate buffers when called for the first time
1890 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1892 struct em28xx_fh *fh = filp->private_data;
1893 struct em28xx *dev = fh->dev;
1896 rc = check_dev(dev);
1900 if (unlikely(res_get(fh) < 0))
1903 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1906 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1910 * em28xx_v4l2_mmap()
1912 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1914 struct em28xx_fh *fh = filp->private_data;
1915 struct em28xx *dev = fh->dev;
1918 if (unlikely(res_get(fh) < 0))
1921 rc = check_dev(dev);
1925 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1927 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1928 (unsigned long)vma->vm_start,
1929 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1935 static const struct file_operations em28xx_v4l_fops = {
1936 .owner = THIS_MODULE,
1937 .open = em28xx_v4l2_open,
1938 .release = em28xx_v4l2_close,
1939 .read = em28xx_v4l2_read,
1940 .poll = em28xx_v4l2_poll,
1941 .mmap = em28xx_v4l2_mmap,
1942 .ioctl = video_ioctl2,
1943 .llseek = no_llseek,
1944 .compat_ioctl = v4l_compat_ioctl32,
1947 static const struct file_operations radio_fops = {
1948 .owner = THIS_MODULE,
1949 .open = em28xx_v4l2_open,
1950 .release = em28xx_v4l2_close,
1951 .ioctl = video_ioctl2,
1952 .compat_ioctl = v4l_compat_ioctl32,
1953 .llseek = no_llseek,
1956 static const struct video_device em28xx_video_template = {
1957 .fops = &em28xx_v4l_fops,
1958 .release = video_device_release,
1961 .vidioc_querycap = vidioc_querycap,
1962 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1963 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1964 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1965 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1966 .vidioc_g_audio = vidioc_g_audio,
1967 .vidioc_s_audio = vidioc_s_audio,
1968 .vidioc_cropcap = vidioc_cropcap,
1970 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1971 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1972 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1974 .vidioc_reqbufs = vidioc_reqbufs,
1975 .vidioc_querybuf = vidioc_querybuf,
1976 .vidioc_qbuf = vidioc_qbuf,
1977 .vidioc_dqbuf = vidioc_dqbuf,
1978 .vidioc_s_std = vidioc_s_std,
1979 .vidioc_enum_input = vidioc_enum_input,
1980 .vidioc_g_input = vidioc_g_input,
1981 .vidioc_s_input = vidioc_s_input,
1982 .vidioc_queryctrl = vidioc_queryctrl,
1983 .vidioc_g_ctrl = vidioc_g_ctrl,
1984 .vidioc_s_ctrl = vidioc_s_ctrl,
1985 .vidioc_streamon = vidioc_streamon,
1986 .vidioc_streamoff = vidioc_streamoff,
1987 .vidioc_g_tuner = vidioc_g_tuner,
1988 .vidioc_s_tuner = vidioc_s_tuner,
1989 .vidioc_g_frequency = vidioc_g_frequency,
1990 .vidioc_s_frequency = vidioc_s_frequency,
1991 #ifdef CONFIG_VIDEO_ADV_DEBUG
1992 .vidioc_g_register = vidioc_g_register,
1993 .vidioc_s_register = vidioc_s_register,
1995 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1996 .vidiocgmbuf = vidiocgmbuf,
1999 .tvnorms = V4L2_STD_ALL,
2000 .current_norm = V4L2_STD_PAL,
2003 static struct video_device em28xx_radio_template = {
2004 .name = "em28xx-radio",
2005 .type = VID_TYPE_TUNER,
2006 .fops = &radio_fops,
2008 .vidioc_querycap = radio_querycap,
2009 .vidioc_g_tuner = radio_g_tuner,
2010 .vidioc_enum_input = radio_enum_input,
2011 .vidioc_g_audio = radio_g_audio,
2012 .vidioc_s_tuner = radio_s_tuner,
2013 .vidioc_s_audio = radio_s_audio,
2014 .vidioc_s_input = radio_s_input,
2015 .vidioc_queryctrl = radio_queryctrl,
2016 .vidioc_g_ctrl = vidioc_g_ctrl,
2017 .vidioc_s_ctrl = vidioc_s_ctrl,
2018 .vidioc_g_frequency = vidioc_g_frequency,
2019 .vidioc_s_frequency = vidioc_s_frequency,
2020 #ifdef CONFIG_VIDEO_ADV_DEBUG
2021 .vidioc_g_register = vidioc_g_register,
2022 .vidioc_s_register = vidioc_s_register,
2026 /******************************** usb interface *****************************************/
2029 static LIST_HEAD(em28xx_extension_devlist);
2030 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2032 int em28xx_register_extension(struct em28xx_ops *ops)
2034 struct em28xx *h, *dev = NULL;
2036 list_for_each_entry(h, &em28xx_devlist, devlist)
2039 mutex_lock(&em28xx_extension_devlist_lock);
2040 list_add_tail(&ops->next, &em28xx_extension_devlist);
2044 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2045 mutex_unlock(&em28xx_extension_devlist_lock);
2049 EXPORT_SYMBOL(em28xx_register_extension);
2051 void em28xx_unregister_extension(struct em28xx_ops *ops)
2053 struct em28xx *h, *dev = NULL;
2055 list_for_each_entry(h, &em28xx_devlist, devlist)
2061 mutex_lock(&em28xx_extension_devlist_lock);
2062 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2063 list_del(&ops->next);
2064 mutex_unlock(&em28xx_extension_devlist_lock);
2066 EXPORT_SYMBOL(em28xx_unregister_extension);
2068 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2069 const struct video_device *template,
2071 const char *type_name)
2073 struct video_device *vfd;
2075 vfd = video_device_alloc();
2080 vfd->dev = &dev->udev->dev;
2081 vfd->release = video_device_release;
2083 vfd->debug = video_debug;
2085 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2086 dev->name, type_name);
2094 * allocates and inits the device structs, registers i2c bus and v4l device
2096 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2099 struct em28xx_ops *ops = NULL;
2100 struct em28xx *dev = *devhandle;
2101 int retval = -ENOMEM;
2103 unsigned int maxh, maxw;
2106 mutex_init(&dev->lock);
2107 spin_lock_init(&dev->slock);
2108 init_waitqueue_head(&dev->open);
2109 init_waitqueue_head(&dev->wait_frame);
2110 init_waitqueue_head(&dev->wait_stream);
2112 dev->em28xx_write_regs = em28xx_write_regs;
2113 dev->em28xx_read_reg = em28xx_read_reg;
2114 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2115 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2116 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2117 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2119 errCode = em28xx_read_reg(dev, CHIPID_REG);
2121 em28xx_info("em28xx chip ID = %d\n", errCode);
2123 em28xx_pre_card_setup(dev);
2125 errCode = em28xx_config(dev);
2127 em28xx_errdev("error configuring device\n");
2128 em28xx_devused &= ~(1<<dev->devno);
2133 /* register i2c bus */
2134 em28xx_i2c_register(dev);
2136 /* Do board specific init and eeprom reading */
2137 em28xx_card_setup(dev);
2139 /* Configure audio */
2140 em28xx_audio_analog_set(dev);
2142 /* configure the device */
2143 em28xx_config_i2c(dev);
2145 /* set default norm */
2146 dev->norm = em28xx_video_template.current_norm;
2148 maxw = norm_maxw(dev);
2149 maxh = norm_maxh(dev);
2151 /* set default image size */
2154 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2155 dev->field_size = dev->width * dev->height;
2157 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2158 dev->bytesperline = dev->width * 2;
2163 errCode = em28xx_config(dev);
2165 list_add_tail(&dev->devlist, &em28xx_devlist);
2167 /* allocate and fill video video_device struct */
2168 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2169 VID_TYPE_CAPTURE, "video");
2170 if (NULL == dev->vdev) {
2171 em28xx_errdev("cannot allocate video_device.\n");
2174 if (dev->tuner_type != TUNER_ABSENT)
2175 dev->vdev->type |= VID_TYPE_TUNER;
2177 /* register v4l2 video video_device */
2178 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2179 video_nr[dev->devno]);
2181 em28xx_errdev("unable to register video device (error=%i).\n",
2186 /* Allocate and fill vbi video_device struct */
2187 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2188 VFL_TYPE_VBI, "vbi");
2189 /* register v4l2 vbi video_device */
2190 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2191 vbi_nr[dev->devno]) < 0) {
2192 em28xx_errdev("unable to register vbi device\n");
2197 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2198 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2199 VFL_TYPE_RADIO, "radio");
2200 if (NULL == dev->radio_dev) {
2201 em28xx_errdev("cannot allocate video_device.\n");
2204 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2205 radio_nr[dev->devno]);
2207 em28xx_errdev("can't register radio device\n");
2210 em28xx_info("Registered radio device as /dev/radio%d\n",
2211 dev->radio_dev->minor & 0x1f);
2214 /* init video dma queues */
2215 INIT_LIST_HEAD(&dev->vidq.active);
2216 INIT_LIST_HEAD(&dev->vidq.queued);
2219 if (dev->has_msp34xx) {
2220 /* Send a reset to other chips via gpio */
2221 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2223 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2229 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2230 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2231 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2233 mutex_lock(&em28xx_extension_devlist_lock);
2234 if (!list_empty(&em28xx_extension_devlist)) {
2235 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2240 mutex_unlock(&em28xx_extension_devlist_lock);
2245 em28xx_release_resources(dev);
2246 mutex_unlock(&dev->lock);
2251 #if defined(CONFIG_MODULES) && defined(MODULE)
2252 static void request_module_async(struct work_struct *work)
2254 struct em28xx *dev = container_of(work,
2255 struct em28xx, request_module_wk);
2257 if (dev->has_audio_class)
2258 request_module("snd-usb-audio");
2260 request_module("em28xx-alsa");
2263 static void request_modules(struct em28xx *dev)
2265 INIT_WORK(&dev->request_module_wk, request_module_async);
2266 schedule_work(&dev->request_module_wk);
2269 #define request_modules(dev)
2270 #endif /* CONFIG_MODULES */
2273 * em28xx_usb_probe()
2274 * checks for supported devices
2276 static int em28xx_usb_probe(struct usb_interface *interface,
2277 const struct usb_device_id *id)
2279 const struct usb_endpoint_descriptor *endpoint;
2280 struct usb_device *udev;
2281 struct usb_interface *uif;
2282 struct em28xx *dev = NULL;
2283 int retval = -ENODEV;
2286 udev = usb_get_dev(interface_to_usbdev(interface));
2287 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2289 /* Check to see next free device and mark as used */
2290 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2291 em28xx_devused|=1<<nr;
2293 /* Don't register audio interfaces */
2294 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2295 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2296 udev->descriptor.idVendor,udev->descriptor.idProduct,
2298 interface->altsetting[0].desc.bInterfaceClass);
2300 em28xx_devused&=~(1<<nr);
2304 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2305 udev->descriptor.idVendor,udev->descriptor.idProduct,
2307 interface->altsetting[0].desc.bInterfaceClass);
2309 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2311 /* check if the device has the iso in endpoint at the correct place */
2312 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2313 USB_ENDPOINT_XFER_ISOC) {
2314 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2315 em28xx_devused&=~(1<<nr);
2318 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2319 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2320 em28xx_devused&=~(1<<nr);
2324 if (nr >= EM28XX_MAXBOARDS) {
2325 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2326 em28xx_devused&=~(1<<nr);
2330 /* allocate memory for our device state and initialize it */
2331 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2333 em28xx_err(DRIVER_NAME ": out of memory!\n");
2334 em28xx_devused&=~(1<<nr);
2338 snprintf(dev->name, 29, "em28xx #%d", nr);
2340 dev->model = id->driver_info;
2343 /* Checks if audio is provided by some interface */
2344 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2345 uif = udev->config->interface[i];
2346 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2347 dev->has_audio_class = 1;
2352 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2353 dev->has_audio_class ? "Has" : "Doesn't have");
2355 /* compute alternate max packet sizes */
2356 uif = udev->actconfig->interface[0];
2358 dev->num_alt=uif->num_altsetting;
2359 em28xx_info("Alternate settings: %i\n",dev->num_alt);
2360 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2361 dev->alt_max_pkt_size = kmalloc(32*
2362 dev->num_alt,GFP_KERNEL);
2363 if (dev->alt_max_pkt_size == NULL) {
2364 em28xx_errdev("out of memory!\n");
2365 em28xx_devused&=~(1<<nr);
2370 for (i = 0; i < dev->num_alt ; i++) {
2371 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2373 dev->alt_max_pkt_size[i] =
2374 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2375 em28xx_info("Alternate setting %i, max size= %i\n",i,
2376 dev->alt_max_pkt_size[i]);
2379 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2380 dev->model = card[nr];
2382 /* allocate device struct */
2383 retval = em28xx_init_dev(&dev, udev, nr);
2387 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2389 /* save our data pointer in this interface device */
2390 usb_set_intfdata(interface, dev);
2392 request_modules(dev);
2398 * em28xx_usb_disconnect()
2399 * called when the device gets diconencted
2400 * video device will be unregistered on v4l2_close in case it is still open
2402 static void em28xx_usb_disconnect(struct usb_interface *interface)
2405 struct em28xx_ops *ops = NULL;
2407 dev = usb_get_intfdata(interface);
2408 usb_set_intfdata(interface, NULL);
2413 em28xx_info("disconnecting %s\n", dev->vdev->name);
2415 /* wait until all current v4l2 io is finished then deallocate resources */
2416 mutex_lock(&dev->lock);
2418 wake_up_interruptible_all(&dev->open);
2422 ("device /dev/video%d is open! Deregistration and memory "
2423 "deallocation are deferred on close.\n",
2424 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2426 dev->state |= DEV_MISCONFIGURED;
2427 em28xx_uninit_isoc(dev);
2428 dev->state |= DEV_DISCONNECTED;
2429 wake_up_interruptible(&dev->wait_frame);
2430 wake_up_interruptible(&dev->wait_stream);
2432 dev->state |= DEV_DISCONNECTED;
2433 em28xx_release_resources(dev);
2435 mutex_unlock(&dev->lock);
2437 mutex_lock(&em28xx_extension_devlist_lock);
2438 if (!list_empty(&em28xx_extension_devlist)) {
2439 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2443 mutex_unlock(&em28xx_extension_devlist_lock);
2446 kfree(dev->alt_max_pkt_size);
2451 static struct usb_driver em28xx_usb_driver = {
2453 .probe = em28xx_usb_probe,
2454 .disconnect = em28xx_usb_disconnect,
2455 .id_table = em28xx_id_table,
2458 static int __init em28xx_module_init(void)
2462 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2463 (EM28XX_VERSION_CODE >> 16) & 0xff,
2464 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2466 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2467 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2470 /* register this driver with the USB subsystem */
2471 result = usb_register(&em28xx_usb_driver);
2473 em28xx_err(DRIVER_NAME
2474 " usb_register failed. Error number %d.\n", result);
2479 static void __exit em28xx_module_exit(void)
2481 /* deregister this driver with the USB subsystem */
2482 usb_deregister(&em28xx_usb_driver);
2485 module_init(em28xx_module_init);
2486 module_exit(em28xx_module_exit);