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 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
68 /* Limits minimum and default number of buffers */
69 #define EM28XX_MIN_BUF 4
70 #define EM28XX_DEF_BUF 8
72 MODULE_AUTHOR(DRIVER_AUTHOR);
73 MODULE_DESCRIPTION(DRIVER_DESC);
74 MODULE_LICENSE("GPL");
76 static LIST_HEAD(em28xx_devlist);
78 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
83 module_param_array(card, int, NULL, 0444);
84 module_param_array(video_nr, int, NULL, 0444);
85 module_param_array(vbi_nr, int, NULL, 0444);
86 module_param_array(radio_nr, int, NULL, 0444);
87 MODULE_PARM_DESC(card, "card type");
88 MODULE_PARM_DESC(video_nr, "video device numbers");
89 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90 MODULE_PARM_DESC(radio_nr, "radio device numbers");
92 static unsigned int video_debug;
93 module_param(video_debug,int,0644);
94 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
96 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97 static unsigned long em28xx_devused;
99 /* supported controls */
100 /* Common to all boards */
101 static struct v4l2_queryctrl em28xx_qctrl[] = {
103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
109 .default_value = 0x1f,
112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
123 static struct usb_driver em28xx_usb_driver;
125 /* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
130 * Announces that a buffer were filled and request the next
132 static inline void buffer_filled(struct em28xx *dev,
133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
136 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
138 /* Nobody is waiting something to be done, just return */
139 if (!waitqueue_active(&buf->vb.done)) {
140 printk(KERN_ERR "em28xx: buffer underrun at %ld\n",
146 /* Advice that buffer was filled */
147 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
148 buf->vb.state = VIDEOBUF_DONE;
149 buf->vb.field_count++;
150 do_gettimeofday(&buf->vb.ts);
152 list_del(&buf->vb.queue);
153 wake_up(&buf->vb.done);
157 * Identify the buffer header type and properly handles
159 static void em28xx_copy_video(struct em28xx *dev,
160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf,
163 unsigned char *outp, unsigned long len)
165 void *fieldstart, *startwrite, *startread;
166 int linesdone, currlinedone, offset, lencopy, remain;
168 if (dev->frame_size != buf->vb.size) {
169 em28xx_errdev("size %i and buf.length %lu are different!\n",
170 dev->frame_size, buf->vb.size);
174 if (dma_q->pos + len > buf->vb.size)
175 len = buf->vb.size - dma_q->pos;
177 if (p[0] != 0x88 && p[0] != 0x22) {
178 em28xx_isocdbg("frame is not complete\n");
186 /* Interlaces frame */
190 fieldstart = outp + dev->bytesperline;
192 linesdone = dma_q->pos / dev->bytesperline;
193 currlinedone = dma_q->pos % dev->bytesperline;
194 offset = linesdone * dev->bytesperline * 2 + currlinedone;
195 startwrite = fieldstart + offset;
196 lencopy = dev->bytesperline - currlinedone;
197 lencopy = lencopy > remain ? remain : lencopy;
199 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
200 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
201 ((char *)startwrite + lencopy) -
202 ((char *)outp + buf->vb.size));
203 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
205 BUG_ON(lencopy <= 0);
206 memcpy(startwrite, startread, lencopy);
211 startwrite += lencopy + dev->bytesperline;
212 startread += lencopy;
213 if (dev->bytesperline > remain)
216 lencopy = dev->bytesperline;
218 BUG_ON(lencopy <= 0);
220 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
221 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
222 ((char *)startwrite + lencopy) -
223 ((char *)outp + buf->vb.size));
224 lencopy = remain = (char *)outp + buf->vb.size -
230 memcpy(startwrite, startread, lencopy);
238 static inline void print_err_status(struct em28xx *dev,
239 int packet, int status)
241 char *errmsg = "Unknown";
245 errmsg = "unlinked synchronuously";
248 errmsg = "unlinked asynchronuously";
251 errmsg = "Buffer error (overrun)";
254 errmsg = "Stalled (device not responding)";
257 errmsg = "Babble (bad cable?)";
260 errmsg = "Bit-stuff error (bad cable?)";
263 errmsg = "CRC/Timeout (could be anything)";
266 errmsg = "Device does not respond";
270 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
272 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
273 packet, status, errmsg);
278 * video-buf generic routine to get the next available buffer
280 static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
281 struct em28xx_buffer **buf)
283 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
285 if (list_empty(&dma_q->active)) {
286 em28xx_isocdbg("No active queue to serve\n");
290 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
296 * Controls the isoc copy of each urb packet
298 static inline int em28xx_isoc_copy(struct urb *urb)
300 struct em28xx_buffer *buf;
301 struct em28xx_dmaqueue *dma_q = urb->context;
302 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
304 int i, len = 0, rc = 1;
310 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
313 if (urb->status < 0) {
314 print_err_status(dev, -1, urb->status);
315 if (urb->status == -ENOENT)
319 buf = dev->isoc_ctl.buf;
322 rc = get_next_buf(dma_q, &buf);
327 outp = videobuf_to_vmalloc(&buf->vb);
330 for (i = 0; i < urb->number_of_packets; i++) {
331 int status = urb->iso_frame_desc[i].status;
334 print_err_status(dev, i, status);
335 if (urb->iso_frame_desc[i].status != -EPROTO)
339 len = urb->iso_frame_desc[i].actual_length - 4;
341 if (urb->iso_frame_desc[i].actual_length <= 0) {
342 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
345 if (urb->iso_frame_desc[i].actual_length >
347 em28xx_isocdbg("packet bigger than packet size");
351 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
353 /* FIXME: incomplete buffer checks where removed to make
354 logic simpler. Impacts of those changes should be evaluated
356 if (p[0] == 0x22 && p[1] == 0x5a) {
357 /* FIXME - are the fields the right way around? */
358 em28xx_isocdbg("Video frame, length=%i, %s\n", len,
359 (p[2] & 1)? "top" : "bottom");
360 em28xx_isocdbg("Current buffer is: outp = 0x%p,"
361 " len = %i\n", outp, (int)buf->vb.size);
364 if (buf->receiving) {
365 buffer_filled(dev, dma_q, buf);
366 rc = get_next_buf(dma_q, &buf);
370 outp = videobuf_to_vmalloc(&buf->vb);
378 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
379 em28xx_isocdbg("VBI HEADER!!!\n");
382 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
384 /* FIXME: Should add vbi copy */
389 /* ------------------------------------------------------------------
391 ------------------------------------------------------------------*/
394 * IRQ callback, called by URB callback
396 static void em28xx_irq_callback(struct urb *urb)
398 struct em28xx_dmaqueue *dma_q = urb->context;
399 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
403 spin_lock_irqsave(&dev->slock, flags);
405 /* Copy data from URB */
406 rc = em28xx_isoc_copy(urb);
408 /* Reset urb buffers */
409 for (i = 0; i < urb->number_of_packets; i++) {
410 urb->iso_frame_desc[i].status = 0;
411 urb->iso_frame_desc[i].actual_length = 0;
415 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
417 em28xx_err("urb resubmit failed (error=%i)\n",
421 spin_unlock_irqrestore(&dev->slock, flags);
425 * Stop and Deallocate URBs
427 static void em28xx_uninit_isoc(struct em28xx *dev)
432 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
434 dev->isoc_ctl.nfields = -1;
435 dev->isoc_ctl.buf = NULL;
436 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
437 urb = dev->isoc_ctl.urb[i];
441 if (dev->isoc_ctl.transfer_buffer[i]) {
442 usb_buffer_free(dev->udev,
443 urb->transfer_buffer_length,
444 dev->isoc_ctl.transfer_buffer[i],
448 dev->isoc_ctl.urb[i] = NULL;
450 dev->isoc_ctl.transfer_buffer[i] = NULL;
453 kfree(dev->isoc_ctl.urb);
454 kfree(dev->isoc_ctl.transfer_buffer);
455 dev->isoc_ctl.urb = NULL;
456 dev->isoc_ctl.transfer_buffer = NULL;
458 dev->isoc_ctl.num_bufs = 0;
460 del_timer(&dev->vidq.timeout);
461 em28xx_capture_start(dev, 0);
466 * Allocate URBs and start IRQ
468 static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
471 struct em28xx_dmaqueue *dma_q = &dev->vidq;
477 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
479 /* De-allocates all pending stuff */
480 em28xx_uninit_isoc(dev);
482 dev->isoc_ctl.num_bufs = num_bufs;
484 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
485 if (!dev->isoc_ctl.urb) {
486 em28xx_errdev("cannot alloc memory for usb buffers\n");
490 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
492 if (!dev->isoc_ctl.urb) {
493 em28xx_errdev("cannot allocate memory for usbtransfer\n");
494 kfree(dev->isoc_ctl.urb);
498 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
500 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
502 /* allocate urbs and transfer buffers */
503 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
504 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
506 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
507 em28xx_uninit_isoc(dev);
510 dev->isoc_ctl.urb[i] = urb;
512 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
513 sb_size, GFP_KERNEL, &urb->transfer_dma);
514 if (!dev->isoc_ctl.transfer_buffer[i]) {
515 em28xx_err("unable to allocate %i bytes for transfer"
518 in_interrupt()?" while in int":"");
519 em28xx_uninit_isoc(dev);
522 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
524 /* FIXME: this is a hack - should be
525 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
526 should also be using 'desc.bInterval'
528 pipe = usb_rcvisocpipe(dev->udev, 0x82);
529 usb_fill_int_urb(urb, dev->udev, pipe,
530 dev->isoc_ctl.transfer_buffer[i], sb_size,
531 em28xx_irq_callback, dma_q, 1);
533 urb->number_of_packets = max_packets;
534 urb->transfer_flags = URB_ISO_ASAP;
537 for (j = 0; j < max_packets; j++) {
538 urb->iso_frame_desc[j].offset = k;
539 urb->iso_frame_desc[j].length =
540 dev->isoc_ctl.max_pkt_size;
541 k += dev->isoc_ctl.max_pkt_size;
548 static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
550 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
553 em28xx_videodbg("Called em28xx_start_thread\n");
555 init_waitqueue_head(&dma_q->wq);
557 em28xx_capture_start(dev, 1);
559 /* submit urbs and enables IRQ */
560 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
561 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
563 em28xx_err("submit of urb %i failed (error=%i)\n", i,
565 em28xx_uninit_isoc(dev);
576 static void em28xx_vid_timeout(unsigned long data)
578 struct em28xx *dev = (struct em28xx *)data;
579 struct em28xx_dmaqueue *vidq = &dev->vidq;
580 struct em28xx_buffer *buf;
583 spin_lock_irqsave(&dev->slock, flags);
585 list_for_each_entry(buf, vidq->active.next, vb.queue) {
586 list_del(&buf->vb.queue);
587 buf->vb.state = VIDEOBUF_ERROR;
588 wake_up(&buf->vb.done);
589 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
592 /* Instead of trying to restart, just sets timeout again */
593 mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT);
595 spin_unlock_irqrestore(&dev->slock, flags);
598 /* ------------------------------------------------------------------
600 ------------------------------------------------------------------*/
603 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
605 struct em28xx_fh *fh = vq->priv_data;
607 *size = 16 * fh->dev->width * fh->dev->height >> 3;
609 *count = EM28XX_DEF_BUF;
611 if (*count < EM28XX_MIN_BUF)
612 *count = EM28XX_MIN_BUF;
617 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
622 videobuf_waiton(&buf->vb, 0, 0);
623 videobuf_vmalloc_free(&buf->vb);
624 buf->vb.state = VIDEOBUF_NEEDS_INIT;
628 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
629 enum v4l2_field field)
631 struct em28xx_fh *fh = vq->priv_data;
632 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
633 struct em28xx *dev = fh->dev;
634 struct em28xx_dmaqueue *vidq = &dev->vidq;
635 int rc = 0, urb_init = 0;
637 /* BUG_ON(NULL == fh->fmt); */
639 /* FIXME: It assumes depth = 16 */
640 /* The only currently supported format is 16 bits/pixel */
641 buf->vb.size = 16 * dev->width * dev->height >> 3;
643 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
646 if (buf->fmt != fh->fmt ||
647 buf->vb.width != dev->width ||
648 buf->vb.height != dev->height ||
649 buf->vb.field != field) {
651 buf->vb.width = dev->width;
652 buf->vb.height = dev->height;
653 buf->vb.field = field;
654 buf->vb.state = VIDEOBUF_NEEDS_INIT;
657 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
658 rc = videobuf_iolock(vq, &buf->vb, NULL);
663 if (!dev->isoc_ctl.num_bufs)
667 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
672 rc = em28xx_start_thread(vidq);
677 buf->vb.state = VIDEOBUF_PREPARED;
681 free_buffer(vq, buf);
686 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
688 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
689 struct em28xx_fh *fh = vq->priv_data;
690 struct em28xx *dev = fh->dev;
691 struct em28xx_dmaqueue *vidq = &dev->vidq;
693 buf->vb.state = VIDEOBUF_QUEUED;
694 list_add_tail(&buf->vb.queue, &vidq->active);
698 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
700 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
701 struct em28xx_fh *fh = vq->priv_data;
702 struct em28xx *dev = (struct em28xx *)fh->dev;
704 em28xx_isocdbg("em28xx: called buffer_release\n");
706 free_buffer(vq, buf);
709 static struct videobuf_queue_ops em28xx_video_qops = {
710 .buf_setup = buffer_setup,
711 .buf_prepare = buffer_prepare,
712 .buf_queue = buffer_queue,
713 .buf_release = buffer_release,
716 /********************* v4l2 interface ******************************************/
720 * inits registers with sane defaults
722 static int em28xx_config(struct em28xx *dev)
725 /* Sets I2C speed to 100 KHz */
727 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
729 /* enable vbi capturing */
731 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
732 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
733 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
735 dev->mute = 1; /* maybe not the right place... */
738 em28xx_outfmt_set_yuv422(dev);
739 em28xx_colorlevels_set_default(dev);
740 em28xx_compression_disable(dev);
746 * em28xx_config_i2c()
747 * configure i2c attached devices
749 static void em28xx_config_i2c(struct em28xx *dev)
751 struct v4l2_routing route;
753 route.input = INPUT(dev->ctl_input)->vmux;
755 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
756 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
757 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
760 static void video_mux(struct em28xx *dev, int index)
762 struct v4l2_routing route;
764 route.input = INPUT(index)->vmux;
766 dev->ctl_input = index;
767 dev->ctl_ainput = INPUT(index)->amux;
769 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
771 if (dev->has_msp34xx) {
773 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
774 route.input = dev->ctl_ainput;
775 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
776 /* Note: this is msp3400 specific */
777 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
780 em28xx_audio_analog_set(dev);
783 /* Usage lock check functions */
784 static int res_get(struct em28xx_fh *fh)
786 struct em28xx *dev = fh->dev;
789 /* This instance already has stream_on */
793 mutex_lock(&dev->lock);
802 mutex_unlock(&dev->lock);
806 static int res_check(struct em28xx_fh *fh)
808 return (fh->stream_on);
811 static void res_free(struct em28xx_fh *fh)
813 struct em28xx *dev = fh->dev;
815 mutex_lock(&dev->lock);
818 mutex_unlock(&dev->lock);
823 * return the current saturation, brightness or contrast, mute state
825 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
828 case V4L2_CID_AUDIO_MUTE:
829 ctrl->value = dev->mute;
831 case V4L2_CID_AUDIO_VOLUME:
832 ctrl->value = dev->volume;
841 * mute or set new saturation, brightness or contrast
843 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
846 case V4L2_CID_AUDIO_MUTE:
847 if (ctrl->value != dev->mute) {
848 dev->mute = ctrl->value;
849 return em28xx_audio_analog_set(dev);
852 case V4L2_CID_AUDIO_VOLUME:
853 dev->volume = ctrl->value;
854 return em28xx_audio_analog_set(dev);
860 static int check_dev(struct em28xx *dev)
862 if (dev->state & DEV_DISCONNECTED) {
863 em28xx_errdev("v4l2 ioctl: device not present\n");
867 if (dev->state & DEV_MISCONFIGURED) {
868 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
869 "close and open it again\n");
875 static void get_scale(struct em28xx *dev,
876 unsigned int width, unsigned int height,
877 unsigned int *hscale, unsigned int *vscale)
879 unsigned int maxw = norm_maxw(dev);
880 unsigned int maxh = norm_maxh(dev);
882 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
883 if (*hscale >= 0x4000)
886 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
887 if (*vscale >= 0x4000)
891 /* ------------------------------------------------------------------
892 IOCTL vidioc handling
893 ------------------------------------------------------------------*/
895 static int vidioc_g_fmt_cap(struct file *file, void *priv,
896 struct v4l2_format *f)
898 struct em28xx_fh *fh = priv;
899 struct em28xx *dev = fh->dev;
901 mutex_lock(&dev->lock);
903 f->fmt.pix.width = dev->width;
904 f->fmt.pix.height = dev->height;
905 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
906 f->fmt.pix.bytesperline = dev->bytesperline;
907 f->fmt.pix.sizeimage = dev->frame_size;
908 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
910 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
911 f->fmt.pix.field = dev->interlaced ?
912 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
914 mutex_unlock(&dev->lock);
918 static int vidioc_try_fmt_cap(struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct em28xx_fh *fh = priv;
922 struct em28xx *dev = fh->dev;
923 int width = f->fmt.pix.width;
924 int height = f->fmt.pix.height;
925 unsigned int maxw = norm_maxw(dev);
926 unsigned int maxh = norm_maxh(dev);
927 unsigned int hscale, vscale;
929 /* width must even because of the YUYV format
930 height must be even because of interlacing */
943 mutex_lock(&dev->lock);
945 if (dev->is_em2800) {
946 /* the em2800 can only scale down to 50% */
947 if (height % (maxh / 2))
949 if (width % (maxw / 2))
951 /* according to empiatech support */
952 /* the MaxPacketSize is to small to support */
953 /* framesizes larger than 640x480 @ 30 fps */
954 /* or 640x576 @ 25 fps. As this would cut */
955 /* of a part of the image we prefer */
956 /* 360x576 or 360x480 for now */
957 if (width == maxw && height == maxh)
961 get_scale(dev, width, height, &hscale, &vscale);
963 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
964 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
966 f->fmt.pix.width = width;
967 f->fmt.pix.height = height;
968 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
969 f->fmt.pix.bytesperline = width * 2;
970 f->fmt.pix.sizeimage = width * 2 * height;
971 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
972 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
974 mutex_unlock(&dev->lock);
978 static int vidioc_s_fmt_cap(struct file *file, void *priv,
979 struct v4l2_format *f)
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
989 vidioc_try_fmt_cap(file, priv, f);
991 mutex_lock(&dev->lock);
993 /* set new image size */
994 dev->width = f->fmt.pix.width;
995 dev->height = f->fmt.pix.height;
996 dev->frame_size = dev->width * dev->height * 2;
997 dev->field_size = dev->frame_size >> 1;
998 dev->bytesperline = dev->width * 2;
999 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1001 em28xx_set_alternate(dev);
1002 em28xx_resolution_set(dev);
1004 mutex_unlock(&dev->lock);
1008 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1010 struct em28xx_fh *fh = priv;
1011 struct em28xx *dev = fh->dev;
1012 struct v4l2_format f;
1015 rc = check_dev(dev);
1019 mutex_lock(&dev->lock);
1021 mutex_unlock(&dev->lock);
1023 /* Adjusts width/height, if needed */
1024 f.fmt.pix.width = dev->width;
1025 f.fmt.pix.height = dev->height;
1026 vidioc_try_fmt_cap(file, priv, &f);
1028 mutex_lock(&dev->lock);
1030 /* set new image size */
1031 dev->width = f.fmt.pix.width;
1032 dev->height = f.fmt.pix.height;
1033 dev->frame_size = dev->width * dev->height * 2;
1034 dev->field_size = dev->frame_size >> 1;
1035 dev->bytesperline = dev->width * 2;
1036 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1038 em28xx_resolution_set(dev);
1039 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1041 mutex_unlock(&dev->lock);
1045 static const char *iname[] = {
1046 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1047 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1048 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1049 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1050 [EM28XX_VMUX_SVIDEO] = "S-Video",
1051 [EM28XX_VMUX_TELEVISION] = "Television",
1052 [EM28XX_VMUX_CABLE] = "Cable TV",
1053 [EM28XX_VMUX_DVB] = "DVB",
1054 [EM28XX_VMUX_DEBUG] = "for debug only",
1057 static int vidioc_enum_input(struct file *file, void *priv,
1058 struct v4l2_input *i)
1060 struct em28xx_fh *fh = priv;
1061 struct em28xx *dev = fh->dev;
1065 if (n >= MAX_EM28XX_INPUT)
1067 if (0 == INPUT(n)->type)
1071 i->type = V4L2_INPUT_TYPE_CAMERA;
1073 strcpy(i->name, iname[INPUT(n)->type]);
1075 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1076 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1077 i->type = V4L2_INPUT_TYPE_TUNER;
1079 i->std = dev->vdev->tvnorms;
1084 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1086 struct em28xx_fh *fh = priv;
1087 struct em28xx *dev = fh->dev;
1089 *i = dev->ctl_input;
1094 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1096 struct em28xx_fh *fh = priv;
1097 struct em28xx *dev = fh->dev;
1100 rc = check_dev(dev);
1104 if (i >= MAX_EM28XX_INPUT)
1106 if (0 == INPUT(i)->type)
1109 mutex_lock(&dev->lock);
1113 mutex_unlock(&dev->lock);
1117 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1119 struct em28xx_fh *fh = priv;
1120 struct em28xx *dev = fh->dev;
1121 unsigned int index = a->index;
1126 index = dev->ctl_ainput;
1129 strcpy(a->name, "Television");
1131 strcpy(a->name, "Line In");
1133 a->capability = V4L2_AUDCAP_STEREO;
1139 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1144 if (a->index != dev->ctl_ainput)
1150 static int vidioc_queryctrl(struct file *file, void *priv,
1151 struct v4l2_queryctrl *qc)
1153 struct em28xx_fh *fh = priv;
1154 struct em28xx *dev = fh->dev;
1159 rc = check_dev(dev);
1163 memset(qc, 0, sizeof(*qc));
1167 if (!dev->has_msp34xx) {
1168 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1169 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1170 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1175 mutex_lock(&dev->lock);
1176 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1177 mutex_unlock(&dev->lock);
1185 static int vidioc_g_ctrl(struct file *file, void *priv,
1186 struct v4l2_control *ctrl)
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1192 rc = check_dev(dev);
1195 mutex_lock(&dev->lock);
1197 if (!dev->has_msp34xx)
1198 rc = em28xx_get_ctrl(dev, ctrl);
1202 if (rc == -EINVAL) {
1203 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1207 mutex_unlock(&dev->lock);
1211 static int vidioc_s_ctrl(struct file *file, void *priv,
1212 struct v4l2_control *ctrl)
1214 struct em28xx_fh *fh = priv;
1215 struct em28xx *dev = fh->dev;
1219 rc = check_dev(dev);
1223 mutex_lock(&dev->lock);
1225 if (dev->has_msp34xx)
1226 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1229 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1230 if (ctrl->id == em28xx_qctrl[i].id) {
1231 if (ctrl->value < em28xx_qctrl[i].minimum ||
1232 ctrl->value > em28xx_qctrl[i].maximum) {
1237 rc = em28xx_set_ctrl(dev, ctrl);
1243 /* Control not found - try to send it to the attached devices */
1245 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1249 mutex_unlock(&dev->lock);
1253 static int vidioc_g_tuner(struct file *file, void *priv,
1254 struct v4l2_tuner *t)
1256 struct em28xx_fh *fh = priv;
1257 struct em28xx *dev = fh->dev;
1260 rc = check_dev(dev);
1267 strcpy(t->name, "Tuner");
1269 mutex_lock(&dev->lock);
1271 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1273 mutex_unlock(&dev->lock);
1277 static int vidioc_s_tuner(struct file *file, void *priv,
1278 struct v4l2_tuner *t)
1280 struct em28xx_fh *fh = priv;
1281 struct em28xx *dev = fh->dev;
1284 rc = check_dev(dev);
1291 mutex_lock(&dev->lock);
1293 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1295 mutex_unlock(&dev->lock);
1299 static int vidioc_g_frequency(struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
1305 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1306 f->frequency = dev->ctl_freq;
1311 static int vidioc_s_frequency(struct file *file, void *priv,
1312 struct v4l2_frequency *f)
1314 struct em28xx_fh *fh = priv;
1315 struct em28xx *dev = fh->dev;
1318 rc = check_dev(dev);
1325 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1327 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1330 mutex_lock(&dev->lock);
1332 dev->ctl_freq = f->frequency;
1333 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1335 mutex_unlock(&dev->lock);
1339 #ifdef CONFIG_VIDEO_ADV_DEBUG
1340 static int em28xx_reg_len(int reg)
1352 static int vidioc_g_register(struct file *file, void *priv,
1353 struct v4l2_register *reg)
1355 struct em28xx_fh *fh = priv;
1356 struct em28xx *dev = fh->dev;
1359 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1362 if (em28xx_reg_len(reg->reg) == 1) {
1363 ret = em28xx_read_reg(dev, reg->reg);
1370 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1371 reg->reg, (char *)&val, 2);
1375 reg->val = cpu_to_le64((__u64)val);
1381 static int vidioc_s_register(struct file *file, void *priv,
1382 struct v4l2_register *reg)
1384 struct em28xx_fh *fh = priv;
1385 struct em28xx *dev = fh->dev;
1388 buf = le64_to_cpu((__u64)reg->val);
1390 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1391 em28xx_reg_len(reg->reg));
1396 static int vidioc_cropcap(struct file *file, void *priv,
1397 struct v4l2_cropcap *cc)
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1402 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1405 cc->bounds.left = 0;
1407 cc->bounds.width = dev->width;
1408 cc->bounds.height = dev->height;
1409 cc->defrect = cc->bounds;
1410 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1411 cc->pixelaspect.denominator = 59;
1416 static int vidioc_streamon(struct file *file, void *priv,
1417 enum v4l2_buf_type type)
1419 struct em28xx_fh *fh = priv;
1420 struct em28xx *dev = fh->dev;
1423 rc = check_dev(dev);
1428 if (unlikely(res_get(fh) < 0))
1431 return (videobuf_streamon(&fh->vb_vidq));
1434 static int vidioc_streamoff(struct file *file, void *priv,
1435 enum v4l2_buf_type type)
1437 struct em28xx_fh *fh = priv;
1438 struct em28xx *dev = fh->dev;
1441 rc = check_dev(dev);
1445 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1447 if (type != fh->type)
1450 videobuf_streamoff(&fh->vb_vidq);
1456 static int vidioc_querycap(struct file *file, void *priv,
1457 struct v4l2_capability *cap)
1459 struct em28xx_fh *fh = priv;
1460 struct em28xx *dev = fh->dev;
1462 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1463 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1464 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1466 cap->version = EM28XX_VERSION_CODE;
1469 V4L2_CAP_SLICED_VBI_CAPTURE |
1470 V4L2_CAP_VIDEO_CAPTURE |
1472 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1474 if (dev->tuner_type != TUNER_ABSENT)
1475 cap->capabilities |= V4L2_CAP_TUNER;
1480 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1481 struct v4l2_fmtdesc *fmtd)
1483 if (fmtd->index != 0)
1486 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1487 strcpy(fmtd->description, "Packed YUY2");
1488 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1489 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1494 /* Sliced VBI ioctls */
1495 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1496 struct v4l2_format *f)
1498 struct em28xx_fh *fh = priv;
1499 struct em28xx *dev = fh->dev;
1502 rc = check_dev(dev);
1506 mutex_lock(&dev->lock);
1508 f->fmt.sliced.service_set = 0;
1510 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1512 if (f->fmt.sliced.service_set == 0)
1515 mutex_unlock(&dev->lock);
1519 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1520 struct v4l2_format *f)
1522 struct em28xx_fh *fh = priv;
1523 struct em28xx *dev = fh->dev;
1526 rc = check_dev(dev);
1530 mutex_lock(&dev->lock);
1531 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1532 mutex_unlock(&dev->lock);
1534 if (f->fmt.sliced.service_set == 0)
1541 static int vidioc_reqbufs(struct file *file, void *priv,
1542 struct v4l2_requestbuffers *rb)
1544 struct em28xx_fh *fh = priv;
1545 struct em28xx *dev = fh->dev;
1548 rc = check_dev(dev);
1552 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1555 static int vidioc_querybuf(struct file *file, void *priv,
1556 struct v4l2_buffer *b)
1558 struct em28xx_fh *fh = priv;
1559 struct em28xx *dev = fh->dev;
1562 rc = check_dev(dev);
1566 return (videobuf_querybuf(&fh->vb_vidq, b));
1569 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1571 struct em28xx_fh *fh = priv;
1572 struct em28xx *dev = fh->dev;
1575 rc = check_dev(dev);
1579 return (videobuf_qbuf(&fh->vb_vidq, b));
1582 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1584 struct em28xx_fh *fh = priv;
1585 struct em28xx *dev = fh->dev;
1588 rc = check_dev(dev);
1592 return (videobuf_dqbuf(&fh->vb_vidq, b,
1593 file->f_flags & O_NONBLOCK));
1596 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1597 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1599 struct em28xx_fh *fh = priv;
1601 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1606 /* ----------------------------------------------------------- */
1607 /* RADIO ESPECIFIC IOCTLS */
1608 /* ----------------------------------------------------------- */
1610 static int radio_querycap(struct file *file, void *priv,
1611 struct v4l2_capability *cap)
1613 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1615 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1616 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1617 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1619 cap->version = EM28XX_VERSION_CODE;
1620 cap->capabilities = V4L2_CAP_TUNER;
1624 static int radio_g_tuner(struct file *file, void *priv,
1625 struct v4l2_tuner *t)
1627 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1629 if (unlikely(t->index > 0))
1632 strcpy(t->name, "Radio");
1633 t->type = V4L2_TUNER_RADIO;
1635 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1639 static int radio_enum_input(struct file *file, void *priv,
1640 struct v4l2_input *i)
1644 strcpy(i->name, "Radio");
1645 i->type = V4L2_INPUT_TYPE_TUNER;
1650 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1652 if (unlikely(a->index))
1655 strcpy(a->name, "Radio");
1659 static int radio_s_tuner(struct file *file, void *priv,
1660 struct v4l2_tuner *t)
1662 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1667 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1672 static int radio_s_audio(struct file *file, void *fh,
1673 struct v4l2_audio *a)
1678 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1683 static int radio_queryctrl(struct file *file, void *priv,
1684 struct v4l2_queryctrl *qc)
1688 if (qc->id < V4L2_CID_BASE ||
1689 qc->id >= V4L2_CID_LASTP1)
1692 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1693 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1694 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1703 * em28xx_v4l2_open()
1704 * inits the device and starts isoc transfer
1706 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1708 int minor = iminor(inode);
1709 int errCode = 0, radio = 0;
1710 struct em28xx *h,*dev = NULL;
1711 struct em28xx_fh *fh;
1712 enum v4l2_buf_type fh_type = 0;
1714 list_for_each_entry(h, &em28xx_devlist, devlist) {
1715 if (h->vdev->minor == minor) {
1717 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1719 if (h->vbi_dev->minor == minor) {
1721 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1724 h->radio_dev->minor == minor) {
1732 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1733 minor, v4l2_type_names[fh_type], dev->users);
1735 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1738 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1741 mutex_lock(&dev->lock);
1745 filp->private_data = fh;
1747 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1748 dev->width = norm_maxw(dev);
1749 dev->height = norm_maxh(dev);
1750 dev->frame_size = dev->width * dev->height * 2;
1751 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1752 dev->bytesperline = dev->width * 2;
1756 em28xx_set_alternate(dev);
1757 em28xx_resolution_set(dev);
1761 em28xx_videodbg("video_open: setting radio device\n");
1762 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1767 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1768 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1769 sizeof(struct em28xx_buffer), fh);
1771 mutex_unlock(&dev->lock);
1776 * em28xx_realease_resources()
1777 * unregisters the v4l2,i2c and usb devices
1778 * called when the device gets disconected or at module unload
1780 static void em28xx_release_resources(struct em28xx *dev)
1783 /*FIXME: I2C IR should be disconnected */
1785 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1786 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1787 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1788 list_del(&dev->devlist);
1789 if (dev->radio_dev) {
1790 if (-1 != dev->radio_dev->minor)
1791 video_unregister_device(dev->radio_dev);
1793 video_device_release(dev->radio_dev);
1794 dev->radio_dev = NULL;
1797 if (-1 != dev->vbi_dev->minor)
1798 video_unregister_device(dev->vbi_dev);
1800 video_device_release(dev->vbi_dev);
1801 dev->vbi_dev = NULL;
1804 if (-1 != dev->vdev->minor)
1805 video_unregister_device(dev->vdev);
1807 video_device_release(dev->vdev);
1810 em28xx_i2c_unregister(dev);
1811 usb_put_dev(dev->udev);
1813 /* Mark device as unused */
1814 em28xx_devused&=~(1<<dev->devno);
1818 * em28xx_v4l2_close()
1819 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1821 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1823 struct em28xx_fh *fh = filp->private_data;
1824 struct em28xx *dev = fh->dev;
1827 em28xx_videodbg("users=%d\n", dev->users);
1833 mutex_lock(&dev->lock);
1835 if (dev->users == 1) {
1836 videobuf_stop(&fh->vb_vidq);
1837 videobuf_mmap_free(&fh->vb_vidq);
1839 /* the device is already disconnect,
1840 free the remaining resources */
1841 if (dev->state & DEV_DISCONNECTED) {
1842 em28xx_release_resources(dev);
1843 mutex_unlock(&dev->lock);
1848 /* do this before setting alternate! */
1849 em28xx_uninit_isoc(dev);
1851 /* set alternate 0 */
1853 em28xx_videodbg("setting alternate 0\n");
1854 errCode = usb_set_interface(dev->udev, 0, 0);
1856 em28xx_errdev("cannot change alternate number to "
1857 "0 (error=%i)\n", errCode);
1862 wake_up_interruptible_nr(&dev->open, 1);
1863 mutex_unlock(&dev->lock);
1868 * em28xx_v4l2_read()
1869 * will allocate buffers when called for the first time
1872 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1875 struct em28xx_fh *fh = filp->private_data;
1876 struct em28xx *dev = fh->dev;
1879 rc = check_dev(dev);
1883 /* FIXME: read() is not prepared to allow changing the video
1884 resolution while streaming. Seems a bug at em28xx_set_fmt
1887 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1888 if (unlikely(res_get(fh)))
1891 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1892 filp->f_flags & O_NONBLOCK);
1898 * em28xx_v4l2_poll()
1899 * will allocate buffers when called for the first time
1901 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1903 struct em28xx_fh *fh = filp->private_data;
1904 struct em28xx *dev = fh->dev;
1907 rc = check_dev(dev);
1911 if (unlikely(res_get(fh) < 0))
1914 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1917 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1921 * em28xx_v4l2_mmap()
1923 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1925 struct em28xx_fh *fh = filp->private_data;
1926 struct em28xx *dev = fh->dev;
1929 if (unlikely(res_get(fh) < 0))
1932 rc = check_dev(dev);
1936 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1938 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1939 (unsigned long)vma->vm_start,
1940 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1946 static const struct file_operations em28xx_v4l_fops = {
1947 .owner = THIS_MODULE,
1948 .open = em28xx_v4l2_open,
1949 .release = em28xx_v4l2_close,
1950 .read = em28xx_v4l2_read,
1951 .poll = em28xx_v4l2_poll,
1952 .mmap = em28xx_v4l2_mmap,
1953 .ioctl = video_ioctl2,
1954 .llseek = no_llseek,
1955 .compat_ioctl = v4l_compat_ioctl32,
1958 static const struct file_operations radio_fops = {
1959 .owner = THIS_MODULE,
1960 .open = em28xx_v4l2_open,
1961 .release = em28xx_v4l2_close,
1962 .ioctl = video_ioctl2,
1963 .compat_ioctl = v4l_compat_ioctl32,
1964 .llseek = no_llseek,
1967 static const struct video_device em28xx_video_template = {
1968 .fops = &em28xx_v4l_fops,
1969 .release = video_device_release,
1972 .vidioc_querycap = vidioc_querycap,
1973 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1974 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1975 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1976 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1977 .vidioc_g_audio = vidioc_g_audio,
1978 .vidioc_s_audio = vidioc_s_audio,
1979 .vidioc_cropcap = vidioc_cropcap,
1981 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1982 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1983 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1985 .vidioc_reqbufs = vidioc_reqbufs,
1986 .vidioc_querybuf = vidioc_querybuf,
1987 .vidioc_qbuf = vidioc_qbuf,
1988 .vidioc_dqbuf = vidioc_dqbuf,
1989 .vidioc_s_std = vidioc_s_std,
1990 .vidioc_enum_input = vidioc_enum_input,
1991 .vidioc_g_input = vidioc_g_input,
1992 .vidioc_s_input = vidioc_s_input,
1993 .vidioc_queryctrl = vidioc_queryctrl,
1994 .vidioc_g_ctrl = vidioc_g_ctrl,
1995 .vidioc_s_ctrl = vidioc_s_ctrl,
1996 .vidioc_streamon = vidioc_streamon,
1997 .vidioc_streamoff = vidioc_streamoff,
1998 .vidioc_g_tuner = vidioc_g_tuner,
1999 .vidioc_s_tuner = vidioc_s_tuner,
2000 .vidioc_g_frequency = vidioc_g_frequency,
2001 .vidioc_s_frequency = vidioc_s_frequency,
2002 #ifdef CONFIG_VIDEO_ADV_DEBUG
2003 .vidioc_g_register = vidioc_g_register,
2004 .vidioc_s_register = vidioc_s_register,
2006 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2007 .vidiocgmbuf = vidiocgmbuf,
2010 .tvnorms = V4L2_STD_ALL,
2011 .current_norm = V4L2_STD_PAL,
2014 static struct video_device em28xx_radio_template = {
2015 .name = "em28xx-radio",
2016 .type = VID_TYPE_TUNER,
2017 .fops = &radio_fops,
2019 .vidioc_querycap = radio_querycap,
2020 .vidioc_g_tuner = radio_g_tuner,
2021 .vidioc_enum_input = radio_enum_input,
2022 .vidioc_g_audio = radio_g_audio,
2023 .vidioc_s_tuner = radio_s_tuner,
2024 .vidioc_s_audio = radio_s_audio,
2025 .vidioc_s_input = radio_s_input,
2026 .vidioc_queryctrl = radio_queryctrl,
2027 .vidioc_g_ctrl = vidioc_g_ctrl,
2028 .vidioc_s_ctrl = vidioc_s_ctrl,
2029 .vidioc_g_frequency = vidioc_g_frequency,
2030 .vidioc_s_frequency = vidioc_s_frequency,
2031 #ifdef CONFIG_VIDEO_ADV_DEBUG
2032 .vidioc_g_register = vidioc_g_register,
2033 .vidioc_s_register = vidioc_s_register,
2037 /******************************** usb interface *****************************************/
2040 static LIST_HEAD(em28xx_extension_devlist);
2041 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2043 int em28xx_register_extension(struct em28xx_ops *ops)
2045 struct em28xx *h, *dev = NULL;
2047 list_for_each_entry(h, &em28xx_devlist, devlist)
2050 mutex_lock(&em28xx_extension_devlist_lock);
2051 list_add_tail(&ops->next, &em28xx_extension_devlist);
2055 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2056 mutex_unlock(&em28xx_extension_devlist_lock);
2060 EXPORT_SYMBOL(em28xx_register_extension);
2062 void em28xx_unregister_extension(struct em28xx_ops *ops)
2064 struct em28xx *h, *dev = NULL;
2066 list_for_each_entry(h, &em28xx_devlist, devlist)
2072 mutex_lock(&em28xx_extension_devlist_lock);
2073 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2074 list_del(&ops->next);
2075 mutex_unlock(&em28xx_extension_devlist_lock);
2077 EXPORT_SYMBOL(em28xx_unregister_extension);
2079 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2080 const struct video_device *template,
2082 const char *type_name)
2084 struct video_device *vfd;
2086 vfd = video_device_alloc();
2091 vfd->dev = &dev->udev->dev;
2092 vfd->release = video_device_release;
2095 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2096 dev->name, type_name);
2104 * allocates and inits the device structs, registers i2c bus and v4l device
2106 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2109 struct em28xx_ops *ops = NULL;
2110 struct em28xx *dev = *devhandle;
2111 int retval = -ENOMEM;
2113 unsigned int maxh, maxw;
2116 mutex_init(&dev->lock);
2117 spin_lock_init(&dev->slock);
2118 init_waitqueue_head(&dev->open);
2119 init_waitqueue_head(&dev->wait_frame);
2120 init_waitqueue_head(&dev->wait_stream);
2122 dev->em28xx_write_regs = em28xx_write_regs;
2123 dev->em28xx_read_reg = em28xx_read_reg;
2124 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2125 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2126 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2127 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2129 errCode = em28xx_read_reg(dev, CHIPID_REG);
2131 em28xx_info("em28xx chip ID = %d\n", errCode);
2133 em28xx_pre_card_setup(dev);
2135 errCode = em28xx_config(dev);
2137 em28xx_errdev("error configuring device\n");
2138 em28xx_devused &= ~(1<<dev->devno);
2143 /* register i2c bus */
2144 em28xx_i2c_register(dev);
2146 /* Do board specific init and eeprom reading */
2147 em28xx_card_setup(dev);
2149 /* Configure audio */
2150 em28xx_audio_analog_set(dev);
2152 /* configure the device */
2153 em28xx_config_i2c(dev);
2155 /* set default norm */
2156 dev->norm = em28xx_video_template.current_norm;
2158 maxw = norm_maxw(dev);
2159 maxh = norm_maxh(dev);
2161 /* set default image size */
2164 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2165 dev->field_size = dev->width * dev->height;
2167 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2168 dev->bytesperline = dev->width * 2;
2173 errCode = em28xx_config(dev);
2175 list_add_tail(&dev->devlist, &em28xx_devlist);
2177 /* allocate and fill video video_device struct */
2178 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2179 VID_TYPE_CAPTURE, "video");
2180 if (NULL == dev->vdev) {
2181 em28xx_errdev("cannot allocate video_device.\n");
2184 if (dev->tuner_type != TUNER_ABSENT)
2185 dev->vdev->type |= VID_TYPE_TUNER;
2187 /* register v4l2 video video_device */
2188 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2189 video_nr[dev->devno]);
2191 em28xx_errdev("unable to register video device (error=%i).\n",
2196 /* Allocate and fill vbi video_device struct */
2197 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2198 VFL_TYPE_VBI, "vbi");
2199 /* register v4l2 vbi video_device */
2200 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2201 vbi_nr[dev->devno]) < 0) {
2202 em28xx_errdev("unable to register vbi device\n");
2207 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2208 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2209 VFL_TYPE_RADIO, "radio");
2210 if (NULL == dev->radio_dev) {
2211 em28xx_errdev("cannot allocate video_device.\n");
2214 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2215 radio_nr[dev->devno]);
2217 em28xx_errdev("can't register radio device\n");
2220 em28xx_info("Registered radio device as /dev/radio%d\n",
2221 dev->radio_dev->minor & 0x1f);
2224 /* init video dma queues */
2225 INIT_LIST_HEAD(&dev->vidq.active);
2226 INIT_LIST_HEAD(&dev->vidq.queued);
2228 dev->vidq.timeout.function = em28xx_vid_timeout;
2229 dev->vidq.timeout.data = (unsigned long)dev;
2230 init_timer(&dev->vidq.timeout);
2233 if (dev->has_msp34xx) {
2234 /* Send a reset to other chips via gpio */
2235 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2237 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2243 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2244 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2245 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2247 mutex_lock(&em28xx_extension_devlist_lock);
2248 if (!list_empty(&em28xx_extension_devlist)) {
2249 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2254 mutex_unlock(&em28xx_extension_devlist_lock);
2259 em28xx_release_resources(dev);
2260 mutex_unlock(&dev->lock);
2265 #if defined(CONFIG_MODULES) && defined(MODULE)
2266 static void request_module_async(struct work_struct *work)
2268 struct em28xx *dev = container_of(work,
2269 struct em28xx, request_module_wk);
2271 if (dev->has_audio_class)
2272 request_module("snd-usb-audio");
2274 request_module("em28xx-alsa");
2277 static void request_modules(struct em28xx *dev)
2279 INIT_WORK(&dev->request_module_wk, request_module_async);
2280 schedule_work(&dev->request_module_wk);
2283 #define request_modules(dev)
2284 #endif /* CONFIG_MODULES */
2287 * em28xx_usb_probe()
2288 * checks for supported devices
2290 static int em28xx_usb_probe(struct usb_interface *interface,
2291 const struct usb_device_id *id)
2293 const struct usb_endpoint_descriptor *endpoint;
2294 struct usb_device *udev;
2295 struct usb_interface *uif;
2296 struct em28xx *dev = NULL;
2297 int retval = -ENODEV;
2300 udev = usb_get_dev(interface_to_usbdev(interface));
2301 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2303 /* Check to see next free device and mark as used */
2304 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2305 em28xx_devused|=1<<nr;
2307 /* Don't register audio interfaces */
2308 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2309 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2310 udev->descriptor.idVendor,udev->descriptor.idProduct,
2312 interface->altsetting[0].desc.bInterfaceClass);
2314 em28xx_devused&=~(1<<nr);
2318 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2319 udev->descriptor.idVendor,udev->descriptor.idProduct,
2321 interface->altsetting[0].desc.bInterfaceClass);
2323 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2325 /* check if the device has the iso in endpoint at the correct place */
2326 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2327 USB_ENDPOINT_XFER_ISOC) {
2328 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2329 em28xx_devused&=~(1<<nr);
2332 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2333 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2334 em28xx_devused&=~(1<<nr);
2338 if (nr >= EM28XX_MAXBOARDS) {
2339 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2340 em28xx_devused&=~(1<<nr);
2344 /* allocate memory for our device state and initialize it */
2345 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2347 em28xx_err(DRIVER_NAME ": out of memory!\n");
2348 em28xx_devused&=~(1<<nr);
2352 snprintf(dev->name, 29, "em28xx #%d", nr);
2354 dev->model = id->driver_info;
2357 /* Checks if audio is provided by some interface */
2358 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2359 uif = udev->config->interface[i];
2360 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2361 dev->has_audio_class = 1;
2366 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2367 dev->has_audio_class ? "Has" : "Doesn't have");
2369 /* compute alternate max packet sizes */
2370 uif = udev->actconfig->interface[0];
2372 dev->num_alt=uif->num_altsetting;
2373 em28xx_info("Alternate settings: %i\n",dev->num_alt);
2374 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2375 dev->alt_max_pkt_size = kmalloc(32*
2376 dev->num_alt,GFP_KERNEL);
2377 if (dev->alt_max_pkt_size == NULL) {
2378 em28xx_errdev("out of memory!\n");
2379 em28xx_devused&=~(1<<nr);
2384 for (i = 0; i < dev->num_alt ; i++) {
2385 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2387 dev->alt_max_pkt_size[i] =
2388 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2389 em28xx_info("Alternate setting %i, max size= %i\n",i,
2390 dev->alt_max_pkt_size[i]);
2393 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2394 dev->model = card[nr];
2396 /* allocate device struct */
2397 retval = em28xx_init_dev(&dev, udev, nr);
2401 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2403 /* save our data pointer in this interface device */
2404 usb_set_intfdata(interface, dev);
2406 request_modules(dev);
2412 * em28xx_usb_disconnect()
2413 * called when the device gets diconencted
2414 * video device will be unregistered on v4l2_close in case it is still open
2416 static void em28xx_usb_disconnect(struct usb_interface *interface)
2419 struct em28xx_ops *ops = NULL;
2421 dev = usb_get_intfdata(interface);
2422 usb_set_intfdata(interface, NULL);
2427 em28xx_info("disconnecting %s\n", dev->vdev->name);
2429 /* wait until all current v4l2 io is finished then deallocate resources */
2430 mutex_lock(&dev->lock);
2432 wake_up_interruptible_all(&dev->open);
2436 ("device /dev/video%d is open! Deregistration and memory "
2437 "deallocation are deferred on close.\n",
2438 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2440 dev->state |= DEV_MISCONFIGURED;
2441 em28xx_uninit_isoc(dev);
2442 dev->state |= DEV_DISCONNECTED;
2443 wake_up_interruptible(&dev->wait_frame);
2444 wake_up_interruptible(&dev->wait_stream);
2446 dev->state |= DEV_DISCONNECTED;
2447 em28xx_release_resources(dev);
2449 mutex_unlock(&dev->lock);
2451 mutex_lock(&em28xx_extension_devlist_lock);
2452 if (!list_empty(&em28xx_extension_devlist)) {
2453 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2457 mutex_unlock(&em28xx_extension_devlist_lock);
2460 kfree(dev->alt_max_pkt_size);
2465 static struct usb_driver em28xx_usb_driver = {
2467 .probe = em28xx_usb_probe,
2468 .disconnect = em28xx_usb_disconnect,
2469 .id_table = em28xx_id_table,
2472 static int __init em28xx_module_init(void)
2476 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2477 (EM28XX_VERSION_CODE >> 16) & 0xff,
2478 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2480 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2481 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2484 /* register this driver with the USB subsystem */
2485 result = usb_register(&em28xx_usb_driver);
2487 em28xx_err(DRIVER_NAME
2488 " usb_register failed. Error number %d.\n", result);
2493 static void __exit em28xx_module_exit(void)
2495 /* deregister this driver with the USB subsystem */
2496 usb_deregister(&em28xx_usb_driver);
2499 module_init(em28xx_module_init);
2500 module_exit(em28xx_module_exit);