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, __FUNCTION__ , ##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 void inline 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) - ((char*)outp + buf->vb.size));
202 lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite;
204 BUG_ON(lencopy <= 0);
205 memcpy(startwrite, startread, lencopy);
210 startwrite += lencopy + dev->bytesperline;
211 startread += lencopy;
212 if (dev->bytesperline > remain)
215 lencopy = dev->bytesperline;
217 BUG_ON(lencopy <= 0);
219 if((char*)startwrite + lencopy > (char*)outp + buf->vb.size) {
220 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
221 ((char*)startwrite + lencopy) - ((char*)outp + buf->vb.size));
222 lencopy = remain = (char*)outp + buf->vb.size - (char*)startwrite;
224 if(lencopy <= 0) break;
226 memcpy(startwrite, startread, lencopy);
234 static void inline print_err_status (struct em28xx *dev,
235 int packet, int status)
237 char *errmsg = "Unknown";
241 errmsg = "unlinked synchronuously";
244 errmsg = "unlinked asynchronuously";
247 errmsg = "Buffer error (overrun)";
250 errmsg = "Stalled (device not responding)";
253 errmsg = "Babble (bad cable?)";
256 errmsg = "Bit-stuff error (bad cable?)";
259 errmsg = "CRC/Timeout (could be anything)";
262 errmsg = "Device does not respond";
266 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
268 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
269 packet, status, errmsg);
274 * video-buf generic routine to get the next available buffer
276 static int inline get_next_buf (struct em28xx_dmaqueue *dma_q,
277 struct em28xx_buffer **buf)
279 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
281 if (list_empty(&dma_q->active)) {
282 em28xx_isocdbg("No active queue to serve\n");
286 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
292 * Controls the isoc copy of each urb packet
294 static inline int em28xx_isoc_copy(struct urb *urb)
296 struct em28xx_buffer *buf;
297 struct em28xx_dmaqueue *dma_q = urb->context;
298 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
300 int i, len = 0, rc = 1;
306 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
310 print_err_status (dev,-1,urb->status);
311 if (urb->status == -ENOENT)
315 buf=dev->isoc_ctl.buf;
318 rc=get_next_buf (dma_q, &buf);
323 outp = videobuf_to_vmalloc (&buf->vb);
326 for (i = 0; i < urb->number_of_packets; i++) {
327 int status = urb->iso_frame_desc[i].status;
330 print_err_status (dev,i,status);
331 if (urb->iso_frame_desc[i].status != -EPROTO)
335 len=urb->iso_frame_desc[i].actual_length - 4;
337 if (urb->iso_frame_desc[i].actual_length <= 0) {
338 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
341 if (urb->iso_frame_desc[i].actual_length >
343 em28xx_isocdbg("packet bigger than packet size");
347 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
349 /* FIXME: incomplete buffer checks where removed to make
350 logic simpler. Impacts of those changes should be evaluated
352 if (p[0] == 0x22 && p[1] == 0x5a) {
353 /* FIXME - are the fields the right way around? */
354 em28xx_isocdbg("Video frame, length=%i, %s\n", len,
355 (p[2] & 1)? "top" : "bottom");
356 em28xx_isocdbg("Current buffer is: outp = 0x%p, len = %i\n", outp, (int)buf->vb.size);
359 if (buf->receiving) {
360 buffer_filled (dev, dma_q, buf);
361 rc=get_next_buf (dma_q, &buf);
365 outp = videobuf_to_vmalloc (&buf->vb);
374 } else if (p[0]==0x33 && p[1]==0x95 && p[2]==0x00) {
375 em28xx_isocdbg("VBI HEADER!!!\n");
378 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
380 /* FIXME: Should add vbi copy */
385 /* ------------------------------------------------------------------
387 ------------------------------------------------------------------*/
390 * IRQ callback, called by URB callback
392 static void em28xx_irq_callback(struct urb *urb)
394 struct em28xx_dmaqueue *dma_q = urb->context;
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
399 spin_lock_irqsave(&dev->slock,flags);
401 /* Copy data from URB */
402 rc=em28xx_isoc_copy(urb);
404 /* Reset urb buffers */
405 for (i = 0; i < urb->number_of_packets; i++) {
406 urb->iso_frame_desc[i].status = 0;
407 urb->iso_frame_desc[i].actual_length = 0;
411 if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) {
412 em28xx_err("urb resubmit failed (error=%i)\n",
416 spin_unlock_irqrestore(&dev->slock,flags);
420 * Stop and Deallocate URBs
422 static void em28xx_uninit_isoc(struct em28xx *dev)
427 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
429 dev->isoc_ctl.nfields=-1;
430 dev->isoc_ctl.buf=NULL;
431 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
432 urb=dev->isoc_ctl.urb[i];
436 if (dev->isoc_ctl.transfer_buffer[i]) {
437 usb_buffer_free(dev->udev,
438 urb->transfer_buffer_length,
439 dev->isoc_ctl.transfer_buffer[i],
443 dev->isoc_ctl.urb[i] = NULL;
445 dev->isoc_ctl.transfer_buffer[i] = NULL;
448 kfree (dev->isoc_ctl.urb);
449 kfree (dev->isoc_ctl.transfer_buffer);
450 dev->isoc_ctl.urb=NULL;
451 dev->isoc_ctl.transfer_buffer=NULL;
453 dev->isoc_ctl.num_bufs=0;
455 em28xx_capture_start(dev, 0);
460 * Allocate URBs and start IRQ
462 static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
465 struct em28xx_dmaqueue *dma_q = &dev->vidq;
471 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
473 /* De-allocates all pending stuff */
474 em28xx_uninit_isoc(dev);
476 dev->isoc_ctl.num_bufs = num_bufs;
478 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
479 if (!dev->isoc_ctl.urb) {
480 em28xx_errdev("cannot alloc memory for usb buffers\n");
484 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
486 if (!dev->isoc_ctl.urb) {
487 em28xx_errdev("cannot allocate memory for usbtransfer\n");
488 kfree(dev->isoc_ctl.urb);
492 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
494 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
496 /* allocate urbs and transfer buffers */
497 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
498 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
500 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
501 em28xx_uninit_isoc(dev);
504 dev->isoc_ctl.urb[i] = urb;
506 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
507 sb_size, GFP_KERNEL, &urb->transfer_dma);
508 if (!dev->isoc_ctl.transfer_buffer[i]) {
509 em28xx_err ("unable to allocate %i bytes for transfer"
512 in_interrupt()?" while in int":"");
513 em28xx_uninit_isoc(dev);
516 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
518 /* FIXME: this is a hack - should be
519 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
520 should also be using 'desc.bInterval'
522 pipe=usb_rcvisocpipe(dev->udev, 0x82);
523 usb_fill_int_urb(urb, dev->udev, pipe,
524 dev->isoc_ctl.transfer_buffer[i], sb_size,
525 em28xx_irq_callback, dma_q, 1);
527 urb->number_of_packets = max_packets;
528 urb->transfer_flags = URB_ISO_ASAP;
531 for (j = 0; j < max_packets; j++) {
532 urb->iso_frame_desc[j].offset = k;
533 urb->iso_frame_desc[j].length =
534 dev->isoc_ctl.max_pkt_size;
535 k += dev->isoc_ctl.max_pkt_size;
542 static int em28xx_start_thread( struct em28xx_dmaqueue *dma_q)
544 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
547 em28xx_videodbg("Called em28xx_start_thread\n");
549 init_waitqueue_head(&dma_q->wq);
551 em28xx_capture_start(dev, 1);
553 /* submit urbs and enables IRQ */
554 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
555 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
557 em28xx_err("submit of urb %i failed (error=%i)\n", i,
559 em28xx_uninit_isoc(dev);
570 static void em28xx_vid_timeout(unsigned long data)
572 struct em28xx *dev = (struct em28xx*)data;
573 struct em28xx_dmaqueue *vidq = &dev->vidq;
574 struct em28xx_buffer *buf;
577 spin_lock_irqsave(&dev->slock,flags);
578 while (!list_empty(&vidq->active)) {
579 buf = list_entry(vidq->active.next, struct em28xx_buffer,
581 list_del(&buf->vb.queue);
582 buf->vb.state = VIDEOBUF_ERROR;
583 wake_up(&buf->vb.done);
584 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
587 /* Instead of trying to restart, just sets timeout again */
588 mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT);
590 spin_unlock_irqrestore(&dev->slock,flags);
593 /* ------------------------------------------------------------------
595 ------------------------------------------------------------------*/
598 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
600 struct em28xx_fh *fh = vq->priv_data;
602 *size = 16 * fh->dev->width * fh->dev->height >> 3;
604 *count = EM28XX_DEF_BUF;
606 if (*count < EM28XX_MIN_BUF) {
607 *count=EM28XX_MIN_BUF;
613 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
618 videobuf_waiton(&buf->vb, 0, 0);
619 videobuf_vmalloc_free(&buf->vb);
620 buf->vb.state = VIDEOBUF_NEEDS_INIT;
624 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
625 enum v4l2_field field)
627 struct em28xx_fh *fh = vq->priv_data;
628 struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb);
629 struct em28xx *dev = fh->dev;
630 struct em28xx_dmaqueue *vidq = &dev->vidq;
631 int rc = 0, urb_init = 0;
633 /* BUG_ON(NULL == fh->fmt); */
635 /* FIXME: It assumes depth = 16 */
636 /* The only currently supported format is 16 bits/pixel */
637 buf->vb.size = 16 * dev->width * dev->height >> 3;
639 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
642 if (buf->fmt != fh->fmt ||
643 buf->vb.width != dev->width ||
644 buf->vb.height != dev->height ||
645 buf->vb.field != field) {
647 buf->vb.width = dev->width;
648 buf->vb.height = dev->height;
649 buf->vb.field = field;
650 buf->vb.state = VIDEOBUF_NEEDS_INIT;
653 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
654 rc = videobuf_iolock(vq, &buf->vb, NULL);
659 if (!dev->isoc_ctl.num_bufs)
663 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS, EM28XX_NUM_BUFS);
667 rc = em28xx_start_thread(vidq);
672 buf->vb.state = VIDEOBUF_PREPARED;
681 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
683 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
684 struct em28xx_fh *fh = vq->priv_data;
685 struct em28xx *dev = fh->dev;
686 struct em28xx_dmaqueue *vidq = &dev->vidq;
688 buf->vb.state = VIDEOBUF_QUEUED;
689 list_add_tail(&buf->vb.queue, &vidq->active);
693 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
695 struct em28xx_buffer *buf = container_of(vb,struct em28xx_buffer,vb);
696 struct em28xx_fh *fh = vq->priv_data;
697 struct em28xx *dev = (struct em28xx*)fh->dev;
699 em28xx_isocdbg("em28xx: called buffer_release\n");
704 static struct videobuf_queue_ops em28xx_video_qops = {
705 .buf_setup = buffer_setup,
706 .buf_prepare = buffer_prepare,
707 .buf_queue = buffer_queue,
708 .buf_release = buffer_release,
711 /********************* v4l2 interface ******************************************/
715 * inits registers with sane defaults
717 static int em28xx_config(struct em28xx *dev)
720 /* Sets I2C speed to 100 KHz */
722 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
724 /* enable vbi capturing */
726 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
727 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
728 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
730 dev->mute = 1; /* maybe not the right place... */
733 em28xx_outfmt_set_yuv422(dev);
734 em28xx_colorlevels_set_default(dev);
735 em28xx_compression_disable(dev);
741 * em28xx_config_i2c()
742 * configure i2c attached devices
744 static void em28xx_config_i2c(struct em28xx *dev)
746 struct v4l2_routing route;
748 route.input = INPUT(dev->ctl_input)->vmux;
750 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
751 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
752 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
755 static void video_mux(struct em28xx *dev, int index)
757 struct v4l2_routing route;
759 route.input = INPUT(index)->vmux;
761 dev->ctl_input = index;
762 dev->ctl_ainput = INPUT(index)->amux;
764 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
766 if (dev->has_msp34xx) {
768 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
769 route.input = dev->ctl_ainput;
770 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
771 /* Note: this is msp3400 specific */
772 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
775 em28xx_audio_analog_set(dev);
778 /* Usage lock check functions */
779 static int res_get(struct em28xx_fh *fh)
781 struct em28xx *dev = fh->dev;
784 /* This instance already has stream_on */
788 mutex_lock(&dev->lock);
797 mutex_unlock(&dev->lock);
801 static int res_check(struct em28xx_fh *fh)
803 return (fh->stream_on);
806 static void res_free(struct em28xx_fh *fh)
808 struct em28xx *dev = fh->dev;
810 mutex_lock(&dev->lock);
813 mutex_unlock(&dev->lock);
818 * return the current saturation, brightness or contrast, mute state
820 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
823 case V4L2_CID_AUDIO_MUTE:
824 ctrl->value = dev->mute;
826 case V4L2_CID_AUDIO_VOLUME:
827 ctrl->value = dev->volume;
836 * mute or set new saturation, brightness or contrast
838 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
841 case V4L2_CID_AUDIO_MUTE:
842 if (ctrl->value != dev->mute) {
843 dev->mute = ctrl->value;
844 return em28xx_audio_analog_set(dev);
847 case V4L2_CID_AUDIO_VOLUME:
848 dev->volume = ctrl->value;
849 return em28xx_audio_analog_set(dev);
855 static int check_dev(struct em28xx *dev)
857 if (dev->state & DEV_DISCONNECTED) {
858 em28xx_errdev("v4l2 ioctl: device not present\n");
862 if (dev->state & DEV_MISCONFIGURED) {
863 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
864 "close and open it again\n");
870 static void get_scale(struct em28xx *dev,
871 unsigned int width, unsigned int height,
872 unsigned int *hscale, unsigned int *vscale)
874 unsigned int maxw = norm_maxw(dev);
875 unsigned int maxh = norm_maxh(dev);
877 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
878 if (*hscale >= 0x4000)
881 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
882 if (*vscale >= 0x4000)
886 /* ------------------------------------------------------------------
887 IOCTL vidioc handling
888 ------------------------------------------------------------------*/
890 static int vidioc_g_fmt_cap(struct file *file, void *priv,
891 struct v4l2_format *f)
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
896 mutex_lock(&dev->lock);
898 f->fmt.pix.width = dev->width;
899 f->fmt.pix.height = dev->height;
900 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
901 f->fmt.pix.bytesperline = dev->bytesperline;
902 f->fmt.pix.sizeimage = dev->frame_size;
903 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
905 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
906 f->fmt.pix.field = dev->interlaced ?
907 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
909 mutex_unlock(&dev->lock);
913 static int vidioc_try_fmt_cap(struct file *file, void *priv,
914 struct v4l2_format *f)
916 struct em28xx_fh *fh = priv;
917 struct em28xx *dev = fh->dev;
918 int width = f->fmt.pix.width;
919 int height = f->fmt.pix.height;
920 unsigned int maxw = norm_maxw(dev);
921 unsigned int maxh = norm_maxh(dev);
922 unsigned int hscale, vscale;
924 /* width must even because of the YUYV format
925 height must be even because of interlacing */
938 mutex_lock(&dev->lock);
940 if (dev->is_em2800) {
941 /* the em2800 can only scale down to 50% */
942 if (height % (maxh / 2))
944 if (width % (maxw / 2))
946 /* according to empiatech support */
947 /* the MaxPacketSize is to small to support */
948 /* framesizes larger than 640x480 @ 30 fps */
949 /* or 640x576 @ 25 fps. As this would cut */
950 /* of a part of the image we prefer */
951 /* 360x576 or 360x480 for now */
952 if (width == maxw && height == maxh)
956 get_scale(dev, width, height, &hscale, &vscale);
958 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
959 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
961 f->fmt.pix.width = width;
962 f->fmt.pix.height = height;
963 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
964 f->fmt.pix.bytesperline = width * 2;
965 f->fmt.pix.sizeimage = width * 2 * height;
966 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
967 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
969 mutex_unlock(&dev->lock);
973 static int vidioc_s_fmt_cap(struct file *file, void *priv,
974 struct v4l2_format *f)
976 struct em28xx_fh *fh = priv;
977 struct em28xx *dev = fh->dev;
984 vidioc_try_fmt_cap(file, priv, f);
986 mutex_lock(&dev->lock);
988 /* set new image size */
989 dev->width = f->fmt.pix.width;
990 dev->height = f->fmt.pix.height;
991 dev->frame_size = dev->width * dev->height * 2;
992 dev->field_size = dev->frame_size >> 1;
993 dev->bytesperline = dev->width * 2;
994 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
996 em28xx_set_alternate(dev);
997 em28xx_resolution_set(dev);
999 mutex_unlock(&dev->lock);
1003 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1005 struct em28xx_fh *fh = priv;
1006 struct em28xx *dev = fh->dev;
1007 struct v4l2_format f;
1010 rc = check_dev(dev);
1014 mutex_lock(&dev->lock);
1016 mutex_unlock(&dev->lock);
1018 /* Adjusts width/height, if needed */
1019 f.fmt.pix.width = dev->width;
1020 f.fmt.pix.height = dev->height;
1021 vidioc_try_fmt_cap(file, priv, &f);
1023 mutex_lock(&dev->lock);
1025 /* set new image size */
1026 dev->width = f.fmt.pix.width;
1027 dev->height = f.fmt.pix.height;
1028 dev->frame_size = dev->width * dev->height * 2;
1029 dev->field_size = dev->frame_size >> 1;
1030 dev->bytesperline = dev->width * 2;
1031 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1033 em28xx_resolution_set(dev);
1034 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1036 mutex_unlock(&dev->lock);
1040 static const char *iname[] = {
1041 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1042 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1043 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1044 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1045 [EM28XX_VMUX_SVIDEO] = "S-Video",
1046 [EM28XX_VMUX_TELEVISION] = "Television",
1047 [EM28XX_VMUX_CABLE] = "Cable TV",
1048 [EM28XX_VMUX_DVB] = "DVB",
1049 [EM28XX_VMUX_DEBUG] = "for debug only",
1052 static int vidioc_enum_input(struct file *file, void *priv,
1053 struct v4l2_input *i)
1055 struct em28xx_fh *fh = priv;
1056 struct em28xx *dev = fh->dev;
1060 if (n >= MAX_EM28XX_INPUT)
1062 if (0 == INPUT(n)->type)
1066 i->type = V4L2_INPUT_TYPE_CAMERA;
1068 strcpy(i->name, iname[INPUT(n)->type]);
1070 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1071 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1072 i->type = V4L2_INPUT_TYPE_TUNER;
1074 i->std = dev->vdev->tvnorms;
1079 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1081 struct em28xx_fh *fh = priv;
1082 struct em28xx *dev = fh->dev;
1084 *i = dev->ctl_input;
1089 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1091 struct em28xx_fh *fh = priv;
1092 struct em28xx *dev = fh->dev;
1095 rc = check_dev(dev);
1099 if (i >= MAX_EM28XX_INPUT)
1101 if (0 == INPUT(i)->type)
1104 mutex_lock(&dev->lock);
1108 mutex_unlock(&dev->lock);
1112 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1116 unsigned int index = a->index;
1121 index = dev->ctl_ainput;
1124 strcpy(a->name, "Television");
1126 strcpy(a->name, "Line In");
1128 a->capability = V4L2_AUDCAP_STEREO;
1134 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1136 struct em28xx_fh *fh = priv;
1137 struct em28xx *dev = fh->dev;
1139 if (a->index != dev->ctl_ainput)
1145 static int vidioc_queryctrl(struct file *file, void *priv,
1146 struct v4l2_queryctrl *qc)
1148 struct em28xx_fh *fh = priv;
1149 struct em28xx *dev = fh->dev;
1154 rc = check_dev(dev);
1158 memset(qc, 0, sizeof(*qc));
1162 if (!dev->has_msp34xx) {
1163 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1164 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1165 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1170 mutex_lock(&dev->lock);
1171 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1172 mutex_unlock(&dev->lock);
1180 static int vidioc_g_ctrl(struct file *file, void *priv,
1181 struct v4l2_control *ctrl)
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
1187 rc = check_dev(dev);
1190 mutex_lock(&dev->lock);
1192 if (!dev->has_msp34xx)
1193 rc = em28xx_get_ctrl(dev, ctrl);
1197 if (rc == -EINVAL) {
1198 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1202 mutex_unlock(&dev->lock);
1206 static int vidioc_s_ctrl(struct file *file, void *priv,
1207 struct v4l2_control *ctrl)
1209 struct em28xx_fh *fh = priv;
1210 struct em28xx *dev = fh->dev;
1214 rc = check_dev(dev);
1218 mutex_lock(&dev->lock);
1220 if (dev->has_msp34xx)
1221 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1224 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1225 if (ctrl->id == em28xx_qctrl[i].id) {
1226 if (ctrl->value < em28xx_qctrl[i].minimum ||
1227 ctrl->value > em28xx_qctrl[i].maximum) {
1232 rc = em28xx_set_ctrl(dev, ctrl);
1238 /* Control not found - try to send it to the attached devices */
1240 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1244 mutex_unlock(&dev->lock);
1248 static int vidioc_g_tuner(struct file *file, void *priv,
1249 struct v4l2_tuner *t)
1251 struct em28xx_fh *fh = priv;
1252 struct em28xx *dev = fh->dev;
1255 rc = check_dev(dev);
1262 strcpy(t->name, "Tuner");
1264 mutex_lock(&dev->lock);
1266 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1268 mutex_unlock(&dev->lock);
1272 static int vidioc_s_tuner(struct file *file, void *priv,
1273 struct v4l2_tuner *t)
1275 struct em28xx_fh *fh = priv;
1276 struct em28xx *dev = fh->dev;
1279 rc = check_dev(dev);
1286 mutex_lock(&dev->lock);
1288 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1290 mutex_unlock(&dev->lock);
1294 static int vidioc_g_frequency(struct file *file, void *priv,
1295 struct v4l2_frequency *f)
1297 struct em28xx_fh *fh = priv;
1298 struct em28xx *dev = fh->dev;
1300 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1301 f->frequency = dev->ctl_freq;
1306 static int vidioc_s_frequency(struct file *file, void *priv,
1307 struct v4l2_frequency *f)
1309 struct em28xx_fh *fh = priv;
1310 struct em28xx *dev = fh->dev;
1313 rc = check_dev(dev);
1320 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1322 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1325 mutex_lock(&dev->lock);
1327 dev->ctl_freq = f->frequency;
1328 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1330 mutex_unlock(&dev->lock);
1334 #ifdef CONFIG_VIDEO_ADV_DEBUG
1335 static int em28xx_reg_len(int reg)
1347 static int vidioc_g_register(struct file *file, void *priv,
1348 struct v4l2_register *reg)
1350 struct em28xx_fh *fh = priv;
1351 struct em28xx *dev = fh->dev;
1354 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1357 if (em28xx_reg_len(reg->reg) == 1) {
1358 ret = em28xx_read_reg(dev, reg->reg);
1365 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1366 reg->reg, (char *)&val, 2);
1370 reg->val = cpu_to_le64((__u64)val);
1376 static int vidioc_s_register(struct file *file, void *priv,
1377 struct v4l2_register *reg)
1379 struct em28xx_fh *fh = priv;
1380 struct em28xx *dev = fh->dev;
1383 buf = le64_to_cpu((__u64)reg->val);
1385 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1386 em28xx_reg_len(reg->reg));
1391 static int vidioc_cropcap(struct file *file, void *priv,
1392 struct v4l2_cropcap *cc)
1394 struct em28xx_fh *fh = priv;
1395 struct em28xx *dev = fh->dev;
1397 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1400 cc->bounds.left = 0;
1402 cc->bounds.width = dev->width;
1403 cc->bounds.height = dev->height;
1404 cc->defrect = cc->bounds;
1405 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1406 cc->pixelaspect.denominator = 59;
1411 static int vidioc_streamon(struct file *file, void *priv,
1412 enum v4l2_buf_type type)
1414 struct em28xx_fh *fh = priv;
1415 struct em28xx *dev = fh->dev;
1418 rc = check_dev(dev);
1423 if (unlikely(res_get(fh) < 0))
1426 return (videobuf_streamon(&fh->vb_vidq));
1429 static int vidioc_streamoff(struct file *file, void *priv,
1430 enum v4l2_buf_type type)
1432 struct em28xx_fh *fh = priv;
1433 struct em28xx *dev = fh->dev;
1436 rc = check_dev(dev);
1440 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1442 if (type != fh->type)
1445 videobuf_streamoff(&fh->vb_vidq);
1451 static int vidioc_querycap(struct file *file, void *priv,
1452 struct v4l2_capability *cap)
1454 struct em28xx_fh *fh = priv;
1455 struct em28xx *dev = fh->dev;
1457 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1458 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1459 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1461 cap->version = EM28XX_VERSION_CODE;
1464 V4L2_CAP_SLICED_VBI_CAPTURE |
1465 V4L2_CAP_VIDEO_CAPTURE |
1467 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1469 if (dev->tuner_type != TUNER_ABSENT)
1470 cap->capabilities |= V4L2_CAP_TUNER;
1475 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1476 struct v4l2_fmtdesc *fmtd)
1478 if (fmtd->index != 0)
1481 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1482 strcpy(fmtd->description, "Packed YUY2");
1483 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1484 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1489 /* Sliced VBI ioctls */
1490 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1491 struct v4l2_format *f)
1493 struct em28xx_fh *fh = priv;
1494 struct em28xx *dev = fh->dev;
1497 rc = check_dev(dev);
1501 mutex_lock(&dev->lock);
1503 f->fmt.sliced.service_set = 0;
1505 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1507 if (f->fmt.sliced.service_set == 0)
1510 mutex_unlock(&dev->lock);
1514 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1515 struct v4l2_format *f)
1517 struct em28xx_fh *fh = priv;
1518 struct em28xx *dev = fh->dev;
1521 rc = check_dev(dev);
1525 mutex_lock(&dev->lock);
1526 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1527 mutex_unlock(&dev->lock);
1529 if (f->fmt.sliced.service_set == 0)
1536 static int vidioc_reqbufs(struct file *file, void *priv,
1537 struct v4l2_requestbuffers *rb)
1539 struct em28xx_fh *fh = priv;
1540 struct em28xx *dev = fh->dev;
1543 rc = check_dev(dev);
1547 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1550 static int vidioc_querybuf(struct file *file, void *priv,
1551 struct v4l2_buffer *b)
1553 struct em28xx_fh *fh = priv;
1554 struct em28xx *dev = fh->dev;
1557 rc = check_dev(dev);
1561 return (videobuf_querybuf(&fh->vb_vidq, b));
1564 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1566 struct em28xx_fh *fh = priv;
1567 struct em28xx *dev = fh->dev;
1570 rc = check_dev(dev);
1574 return (videobuf_qbuf(&fh->vb_vidq, b));
1577 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1579 struct em28xx_fh *fh = priv;
1580 struct em28xx *dev = fh->dev;
1583 rc = check_dev(dev);
1587 return (videobuf_dqbuf(&fh->vb_vidq, b,
1588 file->f_flags & O_NONBLOCK));
1591 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1592 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1594 struct em28xx_fh *fh=priv;
1596 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1601 /* ----------------------------------------------------------- */
1602 /* RADIO ESPECIFIC IOCTLS */
1603 /* ----------------------------------------------------------- */
1605 static int radio_querycap(struct file *file, void *priv,
1606 struct v4l2_capability *cap)
1608 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1610 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1611 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1612 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1614 cap->version = EM28XX_VERSION_CODE;
1615 cap->capabilities = V4L2_CAP_TUNER;
1619 static int radio_g_tuner(struct file *file, void *priv,
1620 struct v4l2_tuner *t)
1622 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1624 if (unlikely(t->index > 0))
1627 strcpy(t->name, "Radio");
1628 t->type = V4L2_TUNER_RADIO;
1630 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1634 static int radio_enum_input(struct file *file, void *priv,
1635 struct v4l2_input *i)
1639 strcpy(i->name, "Radio");
1640 i->type = V4L2_INPUT_TYPE_TUNER;
1645 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1647 if (unlikely(a->index))
1650 strcpy(a->name, "Radio");
1654 static int radio_s_tuner(struct file *file, void *priv,
1655 struct v4l2_tuner *t)
1657 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1662 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1667 static int radio_s_audio(struct file *file, void *fh,
1668 struct v4l2_audio *a)
1673 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1678 static int radio_queryctrl(struct file *file, void *priv,
1679 struct v4l2_queryctrl *qc)
1683 if (qc->id < V4L2_CID_BASE ||
1684 qc->id >= V4L2_CID_LASTP1)
1687 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1688 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1689 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1698 * em28xx_v4l2_open()
1699 * inits the device and starts isoc transfer
1701 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1703 int minor = iminor(inode);
1704 int errCode = 0, radio = 0;
1705 struct em28xx *h,*dev = NULL;
1706 struct em28xx_fh *fh;
1707 enum v4l2_buf_type fh_type = 0;
1709 list_for_each_entry(h, &em28xx_devlist, devlist) {
1710 if (h->vdev->minor == minor) {
1712 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1714 if (h->vbi_dev->minor == minor) {
1716 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1719 h->radio_dev->minor == minor) {
1727 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1728 minor, v4l2_type_names[fh_type], dev->users);
1730 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1733 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1736 mutex_lock(&dev->lock);
1740 filp->private_data = fh;
1742 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1743 dev->width = norm_maxw(dev);
1744 dev->height = norm_maxh(dev);
1745 dev->frame_size = dev->width * dev->height * 2;
1746 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1747 dev->bytesperline = dev->width * 2;
1751 em28xx_set_alternate(dev);
1752 em28xx_resolution_set(dev);
1756 em28xx_videodbg("video_open: setting radio device\n");
1757 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1762 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1763 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1764 sizeof(struct em28xx_buffer), fh);
1766 mutex_unlock(&dev->lock);
1771 * em28xx_realease_resources()
1772 * unregisters the v4l2,i2c and usb devices
1773 * called when the device gets disconected or at module unload
1775 static void em28xx_release_resources(struct em28xx *dev)
1778 /*FIXME: I2C IR should be disconnected */
1780 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1781 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1782 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1783 list_del(&dev->devlist);
1784 if (dev->radio_dev) {
1785 if (-1 != dev->radio_dev->minor)
1786 video_unregister_device(dev->radio_dev);
1788 video_device_release(dev->radio_dev);
1789 dev->radio_dev = NULL;
1792 if (-1 != dev->vbi_dev->minor)
1793 video_unregister_device(dev->vbi_dev);
1795 video_device_release(dev->vbi_dev);
1796 dev->vbi_dev = NULL;
1799 if (-1 != dev->vdev->minor)
1800 video_unregister_device(dev->vdev);
1802 video_device_release(dev->vdev);
1805 em28xx_i2c_unregister(dev);
1806 usb_put_dev(dev->udev);
1808 /* Mark device as unused */
1809 em28xx_devused&=~(1<<dev->devno);
1813 * em28xx_v4l2_close()
1814 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1816 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1818 struct em28xx_fh *fh = filp->private_data;
1819 struct em28xx *dev = fh->dev;
1822 em28xx_videodbg("users=%d\n", dev->users);
1828 mutex_lock(&dev->lock);
1830 if (dev->users == 1) {
1831 videobuf_stop(&fh->vb_vidq);
1832 videobuf_mmap_free(&fh->vb_vidq);
1834 /* the device is already disconnect,
1835 free the remaining resources */
1836 if (dev->state & DEV_DISCONNECTED) {
1837 em28xx_release_resources(dev);
1838 mutex_unlock(&dev->lock);
1843 /* do this before setting alternate! */
1844 em28xx_uninit_isoc(dev);
1846 /* set alternate 0 */
1848 em28xx_videodbg("setting alternate 0\n");
1849 errCode = usb_set_interface(dev->udev, 0, 0);
1851 em28xx_errdev("cannot change alternate number to "
1852 "0 (error=%i)\n", errCode);
1857 wake_up_interruptible_nr(&dev->open, 1);
1858 mutex_unlock(&dev->lock);
1863 * em28xx_v4l2_read()
1864 * will allocate buffers when called for the first time
1867 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1870 struct em28xx_fh *fh = filp->private_data;
1871 struct em28xx *dev = fh->dev;
1874 rc = check_dev(dev);
1878 /* FIXME: read() is not prepared to allow changing the video
1879 resolution while streaming. Seems a bug at em28xx_set_fmt
1882 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1883 if (unlikely(res_get(fh)))
1886 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1887 filp->f_flags & O_NONBLOCK);
1893 * em28xx_v4l2_poll()
1894 * will allocate buffers when called for the first time
1896 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1898 struct em28xx_fh *fh = filp->private_data;
1899 struct em28xx *dev = fh->dev;
1902 rc = check_dev(dev);
1906 if (unlikely(res_get(fh) < 0))
1909 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1912 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1916 * em28xx_v4l2_mmap()
1918 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1920 struct em28xx_fh *fh = filp->private_data;
1921 struct em28xx *dev = fh->dev;
1924 if (unlikely(res_get(fh) < 0))
1927 rc = check_dev(dev);
1931 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1933 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1934 (unsigned long)vma->vm_start,
1935 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1941 static const struct file_operations em28xx_v4l_fops = {
1942 .owner = THIS_MODULE,
1943 .open = em28xx_v4l2_open,
1944 .release = em28xx_v4l2_close,
1945 .read = em28xx_v4l2_read,
1946 .poll = em28xx_v4l2_poll,
1947 .mmap = em28xx_v4l2_mmap,
1948 .ioctl = video_ioctl2,
1949 .llseek = no_llseek,
1950 .compat_ioctl = v4l_compat_ioctl32,
1953 static const struct file_operations radio_fops = {
1954 .owner = THIS_MODULE,
1955 .open = em28xx_v4l2_open,
1956 .release = em28xx_v4l2_close,
1957 .ioctl = video_ioctl2,
1958 .compat_ioctl = v4l_compat_ioctl32,
1959 .llseek = no_llseek,
1962 static const struct video_device em28xx_video_template = {
1963 .fops = &em28xx_v4l_fops,
1964 .release = video_device_release,
1967 .vidioc_querycap = vidioc_querycap,
1968 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1969 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1970 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1971 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1972 .vidioc_g_audio = vidioc_g_audio,
1973 .vidioc_s_audio = vidioc_s_audio,
1974 .vidioc_cropcap = vidioc_cropcap,
1976 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1977 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1978 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1980 .vidioc_reqbufs = vidioc_reqbufs,
1981 .vidioc_querybuf = vidioc_querybuf,
1982 .vidioc_qbuf = vidioc_qbuf,
1983 .vidioc_dqbuf = vidioc_dqbuf,
1984 .vidioc_s_std = vidioc_s_std,
1985 .vidioc_enum_input = vidioc_enum_input,
1986 .vidioc_g_input = vidioc_g_input,
1987 .vidioc_s_input = vidioc_s_input,
1988 .vidioc_queryctrl = vidioc_queryctrl,
1989 .vidioc_g_ctrl = vidioc_g_ctrl,
1990 .vidioc_s_ctrl = vidioc_s_ctrl,
1991 .vidioc_streamon = vidioc_streamon,
1992 .vidioc_streamoff = vidioc_streamoff,
1993 .vidioc_g_tuner = vidioc_g_tuner,
1994 .vidioc_s_tuner = vidioc_s_tuner,
1995 .vidioc_g_frequency = vidioc_g_frequency,
1996 .vidioc_s_frequency = vidioc_s_frequency,
1997 #ifdef CONFIG_VIDEO_ADV_DEBUG
1998 .vidioc_g_register = vidioc_g_register,
1999 .vidioc_s_register = vidioc_s_register,
2001 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2002 .vidiocgmbuf = vidiocgmbuf,
2005 .tvnorms = V4L2_STD_ALL,
2006 .current_norm = V4L2_STD_PAL,
2009 static struct video_device em28xx_radio_template = {
2010 .name = "em28xx-radio",
2011 .type = VID_TYPE_TUNER,
2012 .fops = &radio_fops,
2014 .vidioc_querycap = radio_querycap,
2015 .vidioc_g_tuner = radio_g_tuner,
2016 .vidioc_enum_input = radio_enum_input,
2017 .vidioc_g_audio = radio_g_audio,
2018 .vidioc_s_tuner = radio_s_tuner,
2019 .vidioc_s_audio = radio_s_audio,
2020 .vidioc_s_input = radio_s_input,
2021 .vidioc_queryctrl = radio_queryctrl,
2022 .vidioc_g_ctrl = vidioc_g_ctrl,
2023 .vidioc_s_ctrl = vidioc_s_ctrl,
2024 .vidioc_g_frequency = vidioc_g_frequency,
2025 .vidioc_s_frequency = vidioc_s_frequency,
2026 #ifdef CONFIG_VIDEO_ADV_DEBUG
2027 .vidioc_g_register = vidioc_g_register,
2028 .vidioc_s_register = vidioc_s_register,
2032 /******************************** usb interface *****************************************/
2035 static LIST_HEAD(em28xx_extension_devlist);
2036 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2038 int em28xx_register_extension(struct em28xx_ops *ops)
2040 struct em28xx *h, *dev = NULL;
2042 list_for_each_entry(h, &em28xx_devlist, devlist)
2045 mutex_lock(&em28xx_extension_devlist_lock);
2046 list_add_tail(&ops->next, &em28xx_extension_devlist);
2050 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2051 mutex_unlock(&em28xx_extension_devlist_lock);
2055 EXPORT_SYMBOL(em28xx_register_extension);
2057 void em28xx_unregister_extension(struct em28xx_ops *ops)
2059 struct em28xx *h, *dev = NULL;
2061 list_for_each_entry(h, &em28xx_devlist, devlist)
2067 mutex_lock(&em28xx_extension_devlist_lock);
2068 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2069 list_del(&ops->next);
2070 mutex_unlock(&em28xx_extension_devlist_lock);
2072 EXPORT_SYMBOL(em28xx_unregister_extension);
2074 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2075 const struct video_device *template,
2077 const char *type_name)
2079 struct video_device *vfd;
2081 vfd = video_device_alloc();
2086 vfd->dev = &dev->udev->dev;
2087 vfd->release = video_device_release;
2090 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2091 dev->name, type_name);
2099 * allocates and inits the device structs, registers i2c bus and v4l device
2101 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2104 struct em28xx_ops *ops = NULL;
2105 struct em28xx *dev = *devhandle;
2106 int retval = -ENOMEM;
2108 unsigned int maxh, maxw;
2111 mutex_init(&dev->lock);
2112 spin_lock_init(&dev->slock);
2113 init_waitqueue_head(&dev->open);
2114 init_waitqueue_head(&dev->wait_frame);
2115 init_waitqueue_head(&dev->wait_stream);
2117 dev->em28xx_write_regs = em28xx_write_regs;
2118 dev->em28xx_read_reg = em28xx_read_reg;
2119 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2120 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2121 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2122 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2124 errCode = em28xx_read_reg(dev, CHIPID_REG);
2126 em28xx_info("em28xx chip ID = %d\n", errCode);
2128 em28xx_pre_card_setup(dev);
2130 errCode = em28xx_config(dev);
2132 em28xx_errdev("error configuring device\n");
2133 em28xx_devused &= ~(1<<dev->devno);
2138 /* register i2c bus */
2139 em28xx_i2c_register(dev);
2141 /* Do board specific init and eeprom reading */
2142 em28xx_card_setup(dev);
2144 /* Configure audio */
2145 em28xx_audio_analog_set(dev);
2147 /* configure the device */
2148 em28xx_config_i2c(dev);
2150 /* set default norm */
2151 dev->norm = em28xx_video_template.current_norm;
2153 maxw = norm_maxw(dev);
2154 maxh = norm_maxh(dev);
2156 /* set default image size */
2159 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2160 dev->field_size = dev->width * dev->height;
2162 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2163 dev->bytesperline = dev->width * 2;
2168 errCode = em28xx_config(dev);
2170 list_add_tail(&dev->devlist, &em28xx_devlist);
2172 /* allocate and fill video video_device struct */
2173 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2174 VID_TYPE_CAPTURE, "video");
2175 if (NULL == dev->vdev) {
2176 em28xx_errdev("cannot allocate video_device.\n");
2179 if (dev->tuner_type != TUNER_ABSENT)
2180 dev->vdev->type |= VID_TYPE_TUNER;
2182 /* register v4l2 video video_device */
2183 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2184 video_nr[dev->devno]);
2186 em28xx_errdev("unable to register video device (error=%i).\n",
2191 /* Allocate and fill vbi video_device struct */
2192 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2193 VFL_TYPE_VBI, "vbi");
2194 /* register v4l2 vbi video_device */
2195 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2196 vbi_nr[dev->devno]) < 0) {
2197 em28xx_errdev("unable to register vbi device\n");
2202 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2203 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2204 VFL_TYPE_RADIO, "radio");
2205 if (NULL == dev->radio_dev) {
2206 em28xx_errdev("cannot allocate video_device.\n");
2209 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2210 radio_nr[dev->devno]);
2212 em28xx_errdev("can't register radio device\n");
2215 em28xx_info("Registered radio device as /dev/radio%d\n",
2216 dev->radio_dev->minor & 0x1f);
2219 /* init video dma queues */
2220 INIT_LIST_HEAD(&dev->vidq.active);
2221 INIT_LIST_HEAD(&dev->vidq.queued);
2223 dev->vidq.timeout.function = em28xx_vid_timeout;
2224 dev->vidq.timeout.data = (unsigned long)dev;
2225 init_timer(&dev->vidq.timeout);
2228 if (dev->has_msp34xx) {
2229 /* Send a reset to other chips via gpio */
2230 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2232 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2238 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2239 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2240 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2242 mutex_lock(&em28xx_extension_devlist_lock);
2243 if (!list_empty(&em28xx_extension_devlist)) {
2244 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2249 mutex_unlock(&em28xx_extension_devlist_lock);
2254 em28xx_release_resources(dev);
2255 mutex_unlock(&dev->lock);
2260 #if defined(CONFIG_MODULES) && defined(MODULE)
2261 static void request_module_async(struct work_struct *work)
2263 struct em28xx *dev = container_of(work,
2264 struct em28xx, request_module_wk);
2266 if (dev->has_audio_class)
2267 request_module("snd-usb-audio");
2269 request_module("em28xx-alsa");
2272 static void request_modules(struct em28xx *dev)
2274 INIT_WORK(&dev->request_module_wk, request_module_async);
2275 schedule_work(&dev->request_module_wk);
2278 #define request_modules(dev)
2279 #endif /* CONFIG_MODULES */
2282 * em28xx_usb_probe()
2283 * checks for supported devices
2285 static int em28xx_usb_probe(struct usb_interface *interface,
2286 const struct usb_device_id *id)
2288 const struct usb_endpoint_descriptor *endpoint;
2289 struct usb_device *udev;
2290 struct usb_interface *uif;
2291 struct em28xx *dev = NULL;
2292 int retval = -ENODEV;
2295 udev = usb_get_dev(interface_to_usbdev(interface));
2296 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2298 /* Check to see next free device and mark as used */
2299 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2300 em28xx_devused|=1<<nr;
2302 /* Don't register audio interfaces */
2303 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2304 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2305 udev->descriptor.idVendor,udev->descriptor.idProduct,
2307 interface->altsetting[0].desc.bInterfaceClass);
2309 em28xx_devused&=~(1<<nr);
2313 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2314 udev->descriptor.idVendor,udev->descriptor.idProduct,
2316 interface->altsetting[0].desc.bInterfaceClass);
2318 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2320 /* check if the device has the iso in endpoint at the correct place */
2321 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2322 USB_ENDPOINT_XFER_ISOC) {
2323 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2324 em28xx_devused&=~(1<<nr);
2327 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2328 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2329 em28xx_devused&=~(1<<nr);
2333 if (nr >= EM28XX_MAXBOARDS) {
2334 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2335 em28xx_devused&=~(1<<nr);
2339 /* allocate memory for our device state and initialize it */
2340 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2342 em28xx_err(DRIVER_NAME ": out of memory!\n");
2343 em28xx_devused&=~(1<<nr);
2347 snprintf(dev->name, 29, "em28xx #%d", nr);
2349 dev->model = id->driver_info;
2352 /* Checks if audio is provided by some interface */
2353 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2354 uif = udev->config->interface[i];
2355 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2356 dev->has_audio_class = 1;
2361 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2362 dev->has_audio_class ? "Has" : "Doesn't have");
2364 /* compute alternate max packet sizes */
2365 uif = udev->actconfig->interface[0];
2367 dev->num_alt=uif->num_altsetting;
2368 em28xx_info("Alternate settings: %i\n",dev->num_alt);
2369 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2370 dev->alt_max_pkt_size = kmalloc(32*
2371 dev->num_alt,GFP_KERNEL);
2372 if (dev->alt_max_pkt_size == NULL) {
2373 em28xx_errdev("out of memory!\n");
2374 em28xx_devused&=~(1<<nr);
2379 for (i = 0; i < dev->num_alt ; i++) {
2380 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2382 dev->alt_max_pkt_size[i] =
2383 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2384 em28xx_info("Alternate setting %i, max size= %i\n",i,
2385 dev->alt_max_pkt_size[i]);
2388 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2389 dev->model = card[nr];
2391 /* allocate device struct */
2392 retval = em28xx_init_dev(&dev, udev, nr);
2396 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2398 /* save our data pointer in this interface device */
2399 usb_set_intfdata(interface, dev);
2401 request_modules(dev);
2407 * em28xx_usb_disconnect()
2408 * called when the device gets diconencted
2409 * video device will be unregistered on v4l2_close in case it is still open
2411 static void em28xx_usb_disconnect(struct usb_interface *interface)
2414 struct em28xx_ops *ops = NULL;
2416 dev = usb_get_intfdata(interface);
2417 usb_set_intfdata(interface, NULL);
2422 em28xx_info("disconnecting %s\n", dev->vdev->name);
2424 /* wait until all current v4l2 io is finished then deallocate resources */
2425 mutex_lock(&dev->lock);
2427 wake_up_interruptible_all(&dev->open);
2431 ("device /dev/video%d is open! Deregistration and memory "
2432 "deallocation are deferred on close.\n",
2433 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2435 dev->state |= DEV_MISCONFIGURED;
2436 em28xx_uninit_isoc(dev);
2437 dev->state |= DEV_DISCONNECTED;
2438 wake_up_interruptible(&dev->wait_frame);
2439 wake_up_interruptible(&dev->wait_stream);
2441 dev->state |= DEV_DISCONNECTED;
2442 em28xx_release_resources(dev);
2444 mutex_unlock(&dev->lock);
2446 mutex_lock(&em28xx_extension_devlist_lock);
2447 if (!list_empty(&em28xx_extension_devlist)) {
2448 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2452 mutex_unlock(&em28xx_extension_devlist_lock);
2455 kfree(dev->alt_max_pkt_size);
2460 static struct usb_driver em28xx_usb_driver = {
2462 .probe = em28xx_usb_probe,
2463 .disconnect = em28xx_usb_disconnect,
2464 .id_table = em28xx_id_table,
2467 static int __init em28xx_module_init(void)
2471 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2472 (EM28XX_VERSION_CODE >> 16) & 0xff,
2473 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2475 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2476 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2479 /* register this driver with the USB subsystem */
2480 result = usb_register(&em28xx_usb_driver);
2482 em28xx_err(DRIVER_NAME
2483 " usb_register failed. Error number %d.\n", result);
2488 static void __exit em28xx_module_exit(void)
2490 /* deregister this driver with the USB subsystem */
2491 usb_deregister(&em28xx_usb_driver);
2494 module_init(em28xx_module_init);
2495 module_exit(em28xx_module_exit);