V4L/DVB (9770): em28xx: turn off tuner when not used
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / em28xx / em28xx-video.c
blob6e558d2ca0f3b4801bcedd20aefbad1b500a211d
1 /*
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
39 #include "em28xx.h"
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/msp3400.h>
43 #include <media/tuner.h>
45 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
47 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
48 "Sascha Sommer <saschasommer@freenet.de>"
50 #define DRIVER_NAME "em28xx"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
54 #define em28xx_videodbg(fmt, arg...) do {\
55 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
64 do {\
65 if (isoc_debug) { \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
75 static LIST_HEAD(em28xx_devlist);
76 static DEFINE_MUTEX(em28xx_devlist_mutex);
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,
105 .name = "Volume",
106 .minimum = 0x0,
107 .maximum = 0x1f,
108 .step = 0x1,
109 .default_value = 0x1f,
110 .flags = 0,
111 }, {
112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Mute",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 .flags = 0,
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 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
142 dev->isoc_ctl.buf = NULL;
144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
149 * Identify the buffer header type and properly handles
151 static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
154 unsigned char *p,
155 unsigned char *outp, unsigned long len)
157 void *fieldstart, *startwrite, *startread;
158 int linesdone, currlinedone, offset, lencopy, remain;
159 int bytesperline = dev->width << 1;
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
164 if (p[0] != 0x88 && p[0] != 0x22) {
165 em28xx_isocdbg("frame is not complete\n");
166 len += 4;
167 } else
168 p += 4;
170 startread = p;
171 remain = len;
173 /* Interlaces frame */
174 if (buf->top_field)
175 fieldstart = outp;
176 else
177 fieldstart = outp + bytesperline;
179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
182 startwrite = fieldstart + offset;
183 lencopy = bytesperline - currlinedone;
184 lencopy = lencopy > remain ? remain : lencopy;
186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
192 if (lencopy <= 0)
193 return;
194 memcpy(startwrite, startread, lencopy);
196 remain -= lencopy;
198 while (remain > 0) {
199 startwrite += lencopy + bytesperline;
200 startread += lencopy;
201 if (bytesperline > remain)
202 lencopy = remain;
203 else
204 lencopy = bytesperline;
206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
211 (char *)startwrite;
213 if (lencopy <= 0)
214 break;
216 memcpy(startwrite, startread, lencopy);
218 remain -= lencopy;
221 dma_q->pos += len;
224 static inline void print_err_status(struct em28xx *dev,
225 int packet, int status)
227 char *errmsg = "Unknown";
229 switch (status) {
230 case -ENOENT:
231 errmsg = "unlinked synchronuously";
232 break;
233 case -ECONNRESET:
234 errmsg = "unlinked asynchronuously";
235 break;
236 case -ENOSR:
237 errmsg = "Buffer error (overrun)";
238 break;
239 case -EPIPE:
240 errmsg = "Stalled (device not responding)";
241 break;
242 case -EOVERFLOW:
243 errmsg = "Babble (bad cable?)";
244 break;
245 case -EPROTO:
246 errmsg = "Bit-stuff error (bad cable?)";
247 break;
248 case -EILSEQ:
249 errmsg = "CRC/Timeout (could be anything)";
250 break;
251 case -ETIME:
252 errmsg = "Device does not respond";
253 break;
255 if (packet < 0) {
256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 } else {
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
264 * video-buf generic routine to get the next available buffer
266 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
267 struct em28xx_buffer **buf)
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
270 char *outp;
272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 dev->isoc_ctl.buf = NULL;
275 *buf = NULL;
276 return;
279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
286 dev->isoc_ctl.buf = *buf;
288 return;
292 * Controls the isoc copy of each urb packet
294 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
296 struct em28xx_buffer *buf;
297 struct em28xx_dmaqueue *dma_q = urb->context;
298 unsigned char *outp = NULL;
299 int i, len = 0, rc = 1;
300 unsigned char *p;
302 if (!dev)
303 return 0;
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 return 0;
308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
310 if (urb->status == -ENOENT)
311 return 0;
314 buf = dev->isoc_ctl.buf;
315 if (buf != NULL)
316 outp = videobuf_to_vmalloc(&buf->vb);
318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
321 if (status < 0) {
322 print_err_status(dev, i, status);
323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
327 len = urb->iso_frame_desc[i].actual_length - 4;
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
331 continue;
333 if (urb->iso_frame_desc[i].actual_length >
334 dev->max_pkt_size) {
335 em28xx_isocdbg("packet bigger than packet size");
336 continue;
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
347 continue;
349 if (p[0] == 0x22 && p[1] == 0x5a) {
350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
353 if (!(p[2] & 1)) {
354 if (buf != NULL)
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
357 if (buf == NULL)
358 outp = NULL;
359 else
360 outp = videobuf_to_vmalloc(&buf->vb);
363 if (buf != NULL) {
364 if (p[2] & 1)
365 buf->top_field = 0;
366 else
367 buf->top_field = 1;
370 dma_q->pos = 0;
372 if (buf != NULL)
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
375 return rc;
378 /* ------------------------------------------------------------------
379 Videobuf operations
380 ------------------------------------------------------------------*/
382 static int
383 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
385 struct em28xx_fh *fh = vq->priv_data;
386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
390 if (0 == *count)
391 *count = EM28XX_DEF_BUF;
393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
396 /* Ask tuner to go to analog mode */
397 memset(&f, 0, sizeof(f));
398 f.frequency = dev->ctl_freq;
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
402 return 0;
405 /* This is called *without* dev->slock held; please keep it that way */
406 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
411 if (in_interrupt())
412 BUG();
414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
432 static int
433 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
436 struct em28xx_fh *fh = vq->priv_data;
437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
443 buf->vb.size = 16 * dev->width * dev->height >> 3;
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
455 goto fail;
458 if (!dev->isoc_ctl.num_bufs)
459 urb_init = 1;
461 if (urb_init) {
462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
464 em28xx_isoc_copy);
465 if (rc < 0)
466 goto fail;
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
472 fail:
473 free_buffer(vq, buf);
474 return rc;
477 static void
478 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
482 struct em28xx *dev = fh->dev;
483 struct em28xx_dmaqueue *vidq = &dev->vidq;
485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
490 static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
494 struct em28xx_fh *fh = vq->priv_data;
495 struct em28xx *dev = (struct em28xx *)fh->dev;
497 em28xx_isocdbg("em28xx: called buffer_release\n");
499 free_buffer(vq, buf);
502 static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
509 /********************* v4l2 interface **************************************/
512 * em28xx_config()
513 * inits registers with sane defaults
515 static int em28xx_config(struct em28xx *dev)
517 int retval;
519 /* Sets I2C speed to 100 KHz */
520 if (!dev->board.is_em2800) {
521 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
522 if (retval < 0) {
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
524 __func__, retval);
525 return retval;
529 /* enable vbi capturing */
531 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
532 /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
533 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
535 dev->mute = 1; /* maybe not the right place... */
536 dev->volume = 0x1f;
538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
542 return 0;
546 * em28xx_config_i2c()
547 * configure i2c attached devices
549 static void em28xx_config_i2c(struct em28xx *dev)
551 struct v4l2_routing route;
552 int zero = 0;
554 route.input = INPUT(dev->ctl_input)->vmux;
555 route.output = 0;
556 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
558 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
561 static void video_mux(struct em28xx *dev, int index)
563 struct v4l2_routing route;
565 route.input = INPUT(index)->vmux;
566 route.output = 0;
567 dev->ctl_input = index;
568 dev->ctl_ainput = INPUT(index)->amux;
569 dev->ctl_aoutput = INPUT(index)->aout;
571 if (!dev->ctl_aoutput)
572 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
574 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
576 if (dev->board.has_msp34xx) {
577 if (dev->i2s_speed) {
578 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
579 &dev->i2s_speed);
581 route.input = dev->ctl_ainput;
582 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
583 /* Note: this is msp3400 specific */
584 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
585 &route);
588 em28xx_audio_analog_set(dev);
591 /* Usage lock check functions */
592 static int res_get(struct em28xx_fh *fh)
594 struct em28xx *dev = fh->dev;
595 int rc = 0;
597 /* This instance already has stream_on */
598 if (fh->stream_on)
599 return rc;
601 if (dev->stream_on)
602 return -EINVAL;
604 mutex_lock(&dev->lock);
605 dev->stream_on = 1;
606 fh->stream_on = 1;
607 mutex_unlock(&dev->lock);
608 return rc;
611 static int res_check(struct em28xx_fh *fh)
613 return (fh->stream_on);
616 static void res_free(struct em28xx_fh *fh)
618 struct em28xx *dev = fh->dev;
620 mutex_lock(&dev->lock);
621 fh->stream_on = 0;
622 dev->stream_on = 0;
623 mutex_unlock(&dev->lock);
627 * em28xx_get_ctrl()
628 * return the current saturation, brightness or contrast, mute state
630 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
632 switch (ctrl->id) {
633 case V4L2_CID_AUDIO_MUTE:
634 ctrl->value = dev->mute;
635 return 0;
636 case V4L2_CID_AUDIO_VOLUME:
637 ctrl->value = dev->volume;
638 return 0;
639 default:
640 return -EINVAL;
645 * em28xx_set_ctrl()
646 * mute or set new saturation, brightness or contrast
648 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
650 switch (ctrl->id) {
651 case V4L2_CID_AUDIO_MUTE:
652 if (ctrl->value != dev->mute) {
653 dev->mute = ctrl->value;
654 return em28xx_audio_analog_set(dev);
656 return 0;
657 case V4L2_CID_AUDIO_VOLUME:
658 dev->volume = ctrl->value;
659 return em28xx_audio_analog_set(dev);
660 default:
661 return -EINVAL;
665 static int check_dev(struct em28xx *dev)
667 if (dev->state & DEV_DISCONNECTED) {
668 em28xx_errdev("v4l2 ioctl: device not present\n");
669 return -ENODEV;
672 if (dev->state & DEV_MISCONFIGURED) {
673 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
674 "close and open it again\n");
675 return -EIO;
677 return 0;
680 static void get_scale(struct em28xx *dev,
681 unsigned int width, unsigned int height,
682 unsigned int *hscale, unsigned int *vscale)
684 unsigned int maxw = norm_maxw(dev);
685 unsigned int maxh = norm_maxh(dev);
687 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
688 if (*hscale >= 0x4000)
689 *hscale = 0x3fff;
691 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
692 if (*vscale >= 0x4000)
693 *vscale = 0x3fff;
696 /* ------------------------------------------------------------------
697 IOCTL vidioc handling
698 ------------------------------------------------------------------*/
700 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
701 struct v4l2_format *f)
703 struct em28xx_fh *fh = priv;
704 struct em28xx *dev = fh->dev;
706 mutex_lock(&dev->lock);
708 f->fmt.pix.width = dev->width;
709 f->fmt.pix.height = dev->height;
710 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
711 f->fmt.pix.bytesperline = dev->width * 2;
712 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
713 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
715 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
716 f->fmt.pix.field = dev->interlaced ?
717 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
719 mutex_unlock(&dev->lock);
720 return 0;
723 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
724 struct v4l2_format *f)
726 struct em28xx_fh *fh = priv;
727 struct em28xx *dev = fh->dev;
728 int width = f->fmt.pix.width;
729 int height = f->fmt.pix.height;
730 unsigned int maxw = norm_maxw(dev);
731 unsigned int maxh = norm_maxh(dev);
732 unsigned int hscale, vscale;
734 /* width must even because of the YUYV format
735 height must be even because of interlacing */
736 height &= 0xfffe;
737 width &= 0xfffe;
739 if (height < 32)
740 height = 32;
741 if (height > maxh)
742 height = maxh;
743 if (width < 48)
744 width = 48;
745 if (width > maxw)
746 width = maxw;
748 mutex_lock(&dev->lock);
750 if (dev->board.is_em2800) {
751 /* the em2800 can only scale down to 50% */
752 if (height % (maxh / 2))
753 height = maxh;
754 if (width % (maxw / 2))
755 width = maxw;
756 /* according to empiatech support */
757 /* the MaxPacketSize is to small to support */
758 /* framesizes larger than 640x480 @ 30 fps */
759 /* or 640x576 @ 25 fps. As this would cut */
760 /* of a part of the image we prefer */
761 /* 360x576 or 360x480 for now */
762 if (width == maxw && height == maxh)
763 width /= 2;
766 get_scale(dev, width, height, &hscale, &vscale);
768 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
769 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
771 f->fmt.pix.width = width;
772 f->fmt.pix.height = height;
773 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
774 f->fmt.pix.bytesperline = width * 2;
775 f->fmt.pix.sizeimage = width * 2 * height;
776 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
777 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
779 mutex_unlock(&dev->lock);
780 return 0;
783 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
784 struct v4l2_format *f)
786 struct em28xx_fh *fh = priv;
787 struct em28xx *dev = fh->dev;
788 int rc;
790 rc = check_dev(dev);
791 if (rc < 0)
792 return rc;
794 vidioc_try_fmt_vid_cap(file, priv, f);
796 mutex_lock(&dev->lock);
798 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
799 em28xx_errdev("%s queue busy\n", __func__);
800 rc = -EBUSY;
801 goto out;
804 if (dev->stream_on && !fh->stream_on) {
805 em28xx_errdev("%s device in use by another fh\n", __func__);
806 rc = -EBUSY;
807 goto out;
810 /* set new image size */
811 dev->width = f->fmt.pix.width;
812 dev->height = f->fmt.pix.height;
813 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
815 em28xx_set_alternate(dev);
816 em28xx_resolution_set(dev);
818 rc = 0;
820 out:
821 mutex_unlock(&dev->lock);
822 return rc;
825 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
827 struct em28xx_fh *fh = priv;
828 struct em28xx *dev = fh->dev;
829 struct v4l2_format f;
830 int rc;
832 rc = check_dev(dev);
833 if (rc < 0)
834 return rc;
836 mutex_lock(&dev->lock);
837 dev->norm = *norm;
838 mutex_unlock(&dev->lock);
840 /* Adjusts width/height, if needed */
841 f.fmt.pix.width = dev->width;
842 f.fmt.pix.height = dev->height;
843 vidioc_try_fmt_vid_cap(file, priv, &f);
845 mutex_lock(&dev->lock);
847 /* set new image size */
848 dev->width = f.fmt.pix.width;
849 dev->height = f.fmt.pix.height;
850 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
852 em28xx_resolution_set(dev);
853 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
855 mutex_unlock(&dev->lock);
856 return 0;
859 static const char *iname[] = {
860 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
861 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
862 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
863 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
864 [EM28XX_VMUX_SVIDEO] = "S-Video",
865 [EM28XX_VMUX_TELEVISION] = "Television",
866 [EM28XX_VMUX_CABLE] = "Cable TV",
867 [EM28XX_VMUX_DVB] = "DVB",
868 [EM28XX_VMUX_DEBUG] = "for debug only",
871 static int vidioc_enum_input(struct file *file, void *priv,
872 struct v4l2_input *i)
874 struct em28xx_fh *fh = priv;
875 struct em28xx *dev = fh->dev;
876 unsigned int n;
878 n = i->index;
879 if (n >= MAX_EM28XX_INPUT)
880 return -EINVAL;
881 if (0 == INPUT(n)->type)
882 return -EINVAL;
884 i->index = n;
885 i->type = V4L2_INPUT_TYPE_CAMERA;
887 strcpy(i->name, iname[INPUT(n)->type]);
889 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
890 (EM28XX_VMUX_CABLE == INPUT(n)->type))
891 i->type = V4L2_INPUT_TYPE_TUNER;
893 i->std = dev->vdev->tvnorms;
895 return 0;
898 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
900 struct em28xx_fh *fh = priv;
901 struct em28xx *dev = fh->dev;
903 *i = dev->ctl_input;
905 return 0;
908 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
910 struct em28xx_fh *fh = priv;
911 struct em28xx *dev = fh->dev;
912 int rc;
914 rc = check_dev(dev);
915 if (rc < 0)
916 return rc;
918 if (i >= MAX_EM28XX_INPUT)
919 return -EINVAL;
920 if (0 == INPUT(i)->type)
921 return -EINVAL;
923 mutex_lock(&dev->lock);
925 video_mux(dev, i);
927 mutex_unlock(&dev->lock);
928 return 0;
931 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
933 struct em28xx_fh *fh = priv;
934 struct em28xx *dev = fh->dev;
936 switch (a->index) {
937 case EM28XX_AMUX_VIDEO:
938 strcpy(a->name, "Television");
939 break;
940 case EM28XX_AMUX_LINE_IN:
941 strcpy(a->name, "Line In");
942 break;
943 case EM28XX_AMUX_VIDEO2:
944 strcpy(a->name, "Television alt");
945 break;
946 case EM28XX_AMUX_PHONE:
947 strcpy(a->name, "Phone");
948 break;
949 case EM28XX_AMUX_MIC:
950 strcpy(a->name, "Mic");
951 break;
952 case EM28XX_AMUX_CD:
953 strcpy(a->name, "CD");
954 break;
955 case EM28XX_AMUX_AUX:
956 strcpy(a->name, "Aux");
957 break;
958 case EM28XX_AMUX_PCM_OUT:
959 strcpy(a->name, "PCM");
960 break;
961 default:
962 return -EINVAL;
965 a->index = dev->ctl_ainput;
966 a->capability = V4L2_AUDCAP_STEREO;
968 return 0;
971 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
973 struct em28xx_fh *fh = priv;
974 struct em28xx *dev = fh->dev;
976 dev->ctl_ainput = INPUT(a->index)->amux;
977 dev->ctl_aoutput = INPUT(a->index)->aout;
979 if (!dev->ctl_aoutput)
980 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
981 return 0;
984 static int vidioc_queryctrl(struct file *file, void *priv,
985 struct v4l2_queryctrl *qc)
987 struct em28xx_fh *fh = priv;
988 struct em28xx *dev = fh->dev;
989 int id = qc->id;
990 int i;
991 int rc;
993 rc = check_dev(dev);
994 if (rc < 0)
995 return rc;
997 memset(qc, 0, sizeof(*qc));
999 qc->id = id;
1001 if (!dev->board.has_msp34xx) {
1002 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1003 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1004 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1005 return 0;
1009 mutex_lock(&dev->lock);
1010 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1011 mutex_unlock(&dev->lock);
1013 if (qc->type)
1014 return 0;
1015 else
1016 return -EINVAL;
1019 static int vidioc_g_ctrl(struct file *file, void *priv,
1020 struct v4l2_control *ctrl)
1022 struct em28xx_fh *fh = priv;
1023 struct em28xx *dev = fh->dev;
1024 int rc;
1026 rc = check_dev(dev);
1027 if (rc < 0)
1028 return rc;
1029 mutex_lock(&dev->lock);
1031 if (!dev->board.has_msp34xx)
1032 rc = em28xx_get_ctrl(dev, ctrl);
1033 else
1034 rc = -EINVAL;
1036 if (rc == -EINVAL) {
1037 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1038 rc = 0;
1041 mutex_unlock(&dev->lock);
1042 return rc;
1045 static int vidioc_s_ctrl(struct file *file, void *priv,
1046 struct v4l2_control *ctrl)
1048 struct em28xx_fh *fh = priv;
1049 struct em28xx *dev = fh->dev;
1050 u8 i;
1051 int rc;
1053 rc = check_dev(dev);
1054 if (rc < 0)
1055 return rc;
1057 mutex_lock(&dev->lock);
1059 if (dev->board.has_msp34xx)
1060 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1061 else {
1062 rc = 1;
1063 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1064 if (ctrl->id == em28xx_qctrl[i].id) {
1065 if (ctrl->value < em28xx_qctrl[i].minimum ||
1066 ctrl->value > em28xx_qctrl[i].maximum) {
1067 rc = -ERANGE;
1068 break;
1071 rc = em28xx_set_ctrl(dev, ctrl);
1072 break;
1077 /* Control not found - try to send it to the attached devices */
1078 if (rc == 1) {
1079 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1080 rc = 0;
1083 mutex_unlock(&dev->lock);
1084 return rc;
1087 static int vidioc_g_tuner(struct file *file, void *priv,
1088 struct v4l2_tuner *t)
1090 struct em28xx_fh *fh = priv;
1091 struct em28xx *dev = fh->dev;
1092 int rc;
1094 rc = check_dev(dev);
1095 if (rc < 0)
1096 return rc;
1098 if (0 != t->index)
1099 return -EINVAL;
1101 strcpy(t->name, "Tuner");
1103 mutex_lock(&dev->lock);
1105 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1107 mutex_unlock(&dev->lock);
1108 return 0;
1111 static int vidioc_s_tuner(struct file *file, void *priv,
1112 struct v4l2_tuner *t)
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1116 int rc;
1118 rc = check_dev(dev);
1119 if (rc < 0)
1120 return rc;
1122 if (0 != t->index)
1123 return -EINVAL;
1125 mutex_lock(&dev->lock);
1127 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1129 mutex_unlock(&dev->lock);
1130 return 0;
1133 static int vidioc_g_frequency(struct file *file, void *priv,
1134 struct v4l2_frequency *f)
1136 struct em28xx_fh *fh = priv;
1137 struct em28xx *dev = fh->dev;
1139 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1140 f->frequency = dev->ctl_freq;
1142 return 0;
1145 static int vidioc_s_frequency(struct file *file, void *priv,
1146 struct v4l2_frequency *f)
1148 struct em28xx_fh *fh = priv;
1149 struct em28xx *dev = fh->dev;
1150 int rc;
1152 rc = check_dev(dev);
1153 if (rc < 0)
1154 return rc;
1156 if (0 != f->tuner)
1157 return -EINVAL;
1159 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1160 return -EINVAL;
1161 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1162 return -EINVAL;
1164 mutex_lock(&dev->lock);
1166 dev->ctl_freq = f->frequency;
1167 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1169 mutex_unlock(&dev->lock);
1170 return 0;
1173 #ifdef CONFIG_VIDEO_ADV_DEBUG
1174 static int em28xx_reg_len(int reg)
1176 switch (reg) {
1177 case EM28XX_R40_AC97LSB:
1178 case EM28XX_R30_HSCALELOW:
1179 case EM28XX_R32_VSCALELOW:
1180 return 2;
1181 default:
1182 return 1;
1186 static int vidioc_g_register(struct file *file, void *priv,
1187 struct v4l2_register *reg)
1189 struct em28xx_fh *fh = priv;
1190 struct em28xx *dev = fh->dev;
1191 int ret;
1193 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1194 return -EINVAL;
1196 if (em28xx_reg_len(reg->reg) == 1) {
1197 ret = em28xx_read_reg(dev, reg->reg);
1198 if (ret < 0)
1199 return ret;
1201 reg->val = ret;
1202 } else {
1203 __le64 val = 0;
1204 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1205 reg->reg, (char *)&val, 2);
1206 if (ret < 0)
1207 return ret;
1209 reg->val = le64_to_cpu(val);
1212 return 0;
1215 static int vidioc_s_register(struct file *file, void *priv,
1216 struct v4l2_register *reg)
1218 struct em28xx_fh *fh = priv;
1219 struct em28xx *dev = fh->dev;
1220 __le64 buf;
1222 buf = cpu_to_le64(reg->val);
1224 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1225 em28xx_reg_len(reg->reg));
1227 #endif
1230 static int vidioc_cropcap(struct file *file, void *priv,
1231 struct v4l2_cropcap *cc)
1233 struct em28xx_fh *fh = priv;
1234 struct em28xx *dev = fh->dev;
1236 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1237 return -EINVAL;
1239 cc->bounds.left = 0;
1240 cc->bounds.top = 0;
1241 cc->bounds.width = dev->width;
1242 cc->bounds.height = dev->height;
1243 cc->defrect = cc->bounds;
1244 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1245 cc->pixelaspect.denominator = 59;
1247 return 0;
1250 static int vidioc_streamon(struct file *file, void *priv,
1251 enum v4l2_buf_type type)
1253 struct em28xx_fh *fh = priv;
1254 struct em28xx *dev = fh->dev;
1255 int rc;
1257 rc = check_dev(dev);
1258 if (rc < 0)
1259 return rc;
1262 if (unlikely(res_get(fh) < 0))
1263 return -EBUSY;
1265 return (videobuf_streamon(&fh->vb_vidq));
1268 static int vidioc_streamoff(struct file *file, void *priv,
1269 enum v4l2_buf_type type)
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
1273 int rc;
1275 rc = check_dev(dev);
1276 if (rc < 0)
1277 return rc;
1279 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1280 return -EINVAL;
1281 if (type != fh->type)
1282 return -EINVAL;
1284 videobuf_streamoff(&fh->vb_vidq);
1285 res_free(fh);
1287 return 0;
1290 static int vidioc_querycap(struct file *file, void *priv,
1291 struct v4l2_capability *cap)
1293 struct em28xx_fh *fh = priv;
1294 struct em28xx *dev = fh->dev;
1296 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1297 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1298 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1300 cap->version = EM28XX_VERSION_CODE;
1302 cap->capabilities =
1303 V4L2_CAP_SLICED_VBI_CAPTURE |
1304 V4L2_CAP_VIDEO_CAPTURE |
1305 V4L2_CAP_AUDIO |
1306 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1308 if (dev->tuner_type != TUNER_ABSENT)
1309 cap->capabilities |= V4L2_CAP_TUNER;
1311 return 0;
1314 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1315 struct v4l2_fmtdesc *fmtd)
1317 if (fmtd->index != 0)
1318 return -EINVAL;
1320 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1321 strcpy(fmtd->description, "Packed YUY2");
1322 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1323 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1325 return 0;
1328 /* Sliced VBI ioctls */
1329 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1330 struct v4l2_format *f)
1332 struct em28xx_fh *fh = priv;
1333 struct em28xx *dev = fh->dev;
1334 int rc;
1336 rc = check_dev(dev);
1337 if (rc < 0)
1338 return rc;
1340 mutex_lock(&dev->lock);
1342 f->fmt.sliced.service_set = 0;
1344 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1346 if (f->fmt.sliced.service_set == 0)
1347 rc = -EINVAL;
1349 mutex_unlock(&dev->lock);
1350 return rc;
1353 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1354 struct v4l2_format *f)
1356 struct em28xx_fh *fh = priv;
1357 struct em28xx *dev = fh->dev;
1358 int rc;
1360 rc = check_dev(dev);
1361 if (rc < 0)
1362 return rc;
1364 mutex_lock(&dev->lock);
1365 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1366 mutex_unlock(&dev->lock);
1368 if (f->fmt.sliced.service_set == 0)
1369 return -EINVAL;
1371 return 0;
1375 static int vidioc_reqbufs(struct file *file, void *priv,
1376 struct v4l2_requestbuffers *rb)
1378 struct em28xx_fh *fh = priv;
1379 struct em28xx *dev = fh->dev;
1380 int rc;
1382 rc = check_dev(dev);
1383 if (rc < 0)
1384 return rc;
1386 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1389 static int vidioc_querybuf(struct file *file, void *priv,
1390 struct v4l2_buffer *b)
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1394 int rc;
1396 rc = check_dev(dev);
1397 if (rc < 0)
1398 return rc;
1400 return (videobuf_querybuf(&fh->vb_vidq, b));
1403 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1405 struct em28xx_fh *fh = priv;
1406 struct em28xx *dev = fh->dev;
1407 int rc;
1409 rc = check_dev(dev);
1410 if (rc < 0)
1411 return rc;
1413 return (videobuf_qbuf(&fh->vb_vidq, b));
1416 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1420 int rc;
1422 rc = check_dev(dev);
1423 if (rc < 0)
1424 return rc;
1426 return (videobuf_dqbuf(&fh->vb_vidq, b,
1427 file->f_flags & O_NONBLOCK));
1430 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1431 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1433 struct em28xx_fh *fh = priv;
1435 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1437 #endif
1440 /* ----------------------------------------------------------- */
1441 /* RADIO ESPECIFIC IOCTLS */
1442 /* ----------------------------------------------------------- */
1444 static int radio_querycap(struct file *file, void *priv,
1445 struct v4l2_capability *cap)
1447 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1449 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1450 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1451 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1453 cap->version = EM28XX_VERSION_CODE;
1454 cap->capabilities = V4L2_CAP_TUNER;
1455 return 0;
1458 static int radio_g_tuner(struct file *file, void *priv,
1459 struct v4l2_tuner *t)
1461 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1463 if (unlikely(t->index > 0))
1464 return -EINVAL;
1466 strcpy(t->name, "Radio");
1467 t->type = V4L2_TUNER_RADIO;
1469 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1470 return 0;
1473 static int radio_enum_input(struct file *file, void *priv,
1474 struct v4l2_input *i)
1476 if (i->index != 0)
1477 return -EINVAL;
1478 strcpy(i->name, "Radio");
1479 i->type = V4L2_INPUT_TYPE_TUNER;
1481 return 0;
1484 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1486 if (unlikely(a->index))
1487 return -EINVAL;
1489 strcpy(a->name, "Radio");
1490 return 0;
1493 static int radio_s_tuner(struct file *file, void *priv,
1494 struct v4l2_tuner *t)
1496 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1498 if (0 != t->index)
1499 return -EINVAL;
1501 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1503 return 0;
1506 static int radio_s_audio(struct file *file, void *fh,
1507 struct v4l2_audio *a)
1509 return 0;
1512 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1514 return 0;
1517 static int radio_queryctrl(struct file *file, void *priv,
1518 struct v4l2_queryctrl *qc)
1520 int i;
1522 if (qc->id < V4L2_CID_BASE ||
1523 qc->id >= V4L2_CID_LASTP1)
1524 return -EINVAL;
1526 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1527 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1528 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1529 return 0;
1533 return -EINVAL;
1537 * em28xx_v4l2_open()
1538 * inits the device and starts isoc transfer
1540 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1542 int minor = iminor(inode);
1543 int errCode = 0, radio = 0;
1544 struct em28xx *h, *dev = NULL;
1545 struct em28xx_fh *fh;
1546 enum v4l2_buf_type fh_type = 0;
1548 mutex_lock(&em28xx_devlist_mutex);
1549 list_for_each_entry(h, &em28xx_devlist, devlist) {
1550 if (h->vdev->minor == minor) {
1551 dev = h;
1552 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1554 if (h->vbi_dev->minor == minor) {
1555 dev = h;
1556 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1558 if (h->radio_dev &&
1559 h->radio_dev->minor == minor) {
1560 radio = 1;
1561 dev = h;
1564 mutex_unlock(&em28xx_devlist_mutex);
1565 if (NULL == dev)
1566 return -ENODEV;
1568 mutex_lock(&dev->lock);
1570 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1571 minor, v4l2_type_names[fh_type], dev->users);
1574 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1575 if (!fh) {
1576 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1577 mutex_unlock(&dev->lock);
1578 return -ENOMEM;
1580 fh->dev = dev;
1581 fh->radio = radio;
1582 fh->type = fh_type;
1583 filp->private_data = fh;
1585 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1586 dev->width = norm_maxw(dev);
1587 dev->height = norm_maxh(dev);
1588 dev->hscale = 0;
1589 dev->vscale = 0;
1591 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1592 em28xx_set_alternate(dev);
1593 em28xx_resolution_set(dev);
1595 /* Needed, since GPIO might have disabled power of
1596 some i2c device
1598 em28xx_config_i2c(dev);
1601 if (fh->radio) {
1602 em28xx_videodbg("video_open: setting radio device\n");
1603 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1606 dev->users++;
1608 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1609 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1610 sizeof(struct em28xx_buffer), fh);
1612 mutex_unlock(&dev->lock);
1614 return errCode;
1618 * em28xx_realease_resources()
1619 * unregisters the v4l2,i2c and usb devices
1620 * called when the device gets disconected or at module unload
1622 static void em28xx_release_resources(struct em28xx *dev)
1625 /*FIXME: I2C IR should be disconnected */
1627 list_del(&dev->devlist);
1628 if (dev->sbutton_input_dev)
1629 em28xx_deregister_snapshot_button(dev);
1631 if (dev->ir)
1632 em28xx_ir_fini(dev);
1634 if (dev->radio_dev) {
1635 if (-1 != dev->radio_dev->minor)
1636 video_unregister_device(dev->radio_dev);
1637 else
1638 video_device_release(dev->radio_dev);
1639 dev->radio_dev = NULL;
1641 if (dev->vbi_dev) {
1642 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1643 dev->vbi_dev->num);
1644 if (-1 != dev->vbi_dev->minor)
1645 video_unregister_device(dev->vbi_dev);
1646 else
1647 video_device_release(dev->vbi_dev);
1648 dev->vbi_dev = NULL;
1650 if (dev->vdev) {
1651 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1652 dev->vdev->num);
1653 if (-1 != dev->vdev->minor)
1654 video_unregister_device(dev->vdev);
1655 else
1656 video_device_release(dev->vdev);
1657 dev->vdev = NULL;
1659 em28xx_i2c_unregister(dev);
1660 usb_put_dev(dev->udev);
1662 /* Mark device as unused */
1663 em28xx_devused &= ~(1<<dev->devno);
1667 * em28xx_v4l2_close()
1668 * stops streaming and deallocates all resources allocated by the v4l2
1669 * calls and ioctls
1671 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1673 struct em28xx_fh *fh = filp->private_data;
1674 struct em28xx *dev = fh->dev;
1675 int errCode;
1677 em28xx_videodbg("users=%d\n", dev->users);
1680 if (res_check(fh))
1681 res_free(fh);
1683 mutex_lock(&dev->lock);
1685 if (dev->users == 1) {
1686 videobuf_stop(&fh->vb_vidq);
1687 videobuf_mmap_free(&fh->vb_vidq);
1689 /* the device is already disconnect,
1690 free the remaining resources */
1691 if (dev->state & DEV_DISCONNECTED) {
1692 em28xx_release_resources(dev);
1693 mutex_unlock(&dev->lock);
1694 kfree(dev);
1695 return 0;
1698 /* Save some power by putting tuner to sleep */
1699 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1701 /* do this before setting alternate! */
1702 em28xx_uninit_isoc(dev);
1703 em28xx_set_mode(dev, EM28XX_SUSPEND);
1705 /* set alternate 0 */
1706 dev->alt = 0;
1707 em28xx_videodbg("setting alternate 0\n");
1708 errCode = usb_set_interface(dev->udev, 0, 0);
1709 if (errCode < 0) {
1710 em28xx_errdev("cannot change alternate number to "
1711 "0 (error=%i)\n", errCode);
1714 kfree(fh);
1715 dev->users--;
1716 wake_up_interruptible_nr(&dev->open, 1);
1717 mutex_unlock(&dev->lock);
1718 return 0;
1722 * em28xx_v4l2_read()
1723 * will allocate buffers when called for the first time
1725 static ssize_t
1726 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1727 loff_t *pos)
1729 struct em28xx_fh *fh = filp->private_data;
1730 struct em28xx *dev = fh->dev;
1731 int rc;
1733 rc = check_dev(dev);
1734 if (rc < 0)
1735 return rc;
1737 /* FIXME: read() is not prepared to allow changing the video
1738 resolution while streaming. Seems a bug at em28xx_set_fmt
1741 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1742 if (unlikely(res_get(fh)))
1743 return -EBUSY;
1745 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1746 filp->f_flags & O_NONBLOCK);
1748 return 0;
1752 * em28xx_v4l2_poll()
1753 * will allocate buffers when called for the first time
1755 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1757 struct em28xx_fh *fh = filp->private_data;
1758 struct em28xx *dev = fh->dev;
1759 int rc;
1761 rc = check_dev(dev);
1762 if (rc < 0)
1763 return rc;
1765 if (unlikely(res_get(fh) < 0))
1766 return POLLERR;
1768 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1769 return POLLERR;
1771 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1775 * em28xx_v4l2_mmap()
1777 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1779 struct em28xx_fh *fh = filp->private_data;
1780 struct em28xx *dev = fh->dev;
1781 int rc;
1783 if (unlikely(res_get(fh) < 0))
1784 return -EBUSY;
1786 rc = check_dev(dev);
1787 if (rc < 0)
1788 return rc;
1790 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1792 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1793 (unsigned long)vma->vm_start,
1794 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1795 rc);
1797 return rc;
1800 static const struct file_operations em28xx_v4l_fops = {
1801 .owner = THIS_MODULE,
1802 .open = em28xx_v4l2_open,
1803 .release = em28xx_v4l2_close,
1804 .read = em28xx_v4l2_read,
1805 .poll = em28xx_v4l2_poll,
1806 .mmap = em28xx_v4l2_mmap,
1807 .ioctl = video_ioctl2,
1808 .llseek = no_llseek,
1809 .compat_ioctl = v4l_compat_ioctl32,
1812 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1813 .vidioc_querycap = vidioc_querycap,
1814 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1815 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1816 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1817 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1818 .vidioc_g_audio = vidioc_g_audio,
1819 .vidioc_s_audio = vidioc_s_audio,
1820 .vidioc_cropcap = vidioc_cropcap,
1822 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1823 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1824 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1826 .vidioc_reqbufs = vidioc_reqbufs,
1827 .vidioc_querybuf = vidioc_querybuf,
1828 .vidioc_qbuf = vidioc_qbuf,
1829 .vidioc_dqbuf = vidioc_dqbuf,
1830 .vidioc_s_std = vidioc_s_std,
1831 .vidioc_enum_input = vidioc_enum_input,
1832 .vidioc_g_input = vidioc_g_input,
1833 .vidioc_s_input = vidioc_s_input,
1834 .vidioc_queryctrl = vidioc_queryctrl,
1835 .vidioc_g_ctrl = vidioc_g_ctrl,
1836 .vidioc_s_ctrl = vidioc_s_ctrl,
1837 .vidioc_streamon = vidioc_streamon,
1838 .vidioc_streamoff = vidioc_streamoff,
1839 .vidioc_g_tuner = vidioc_g_tuner,
1840 .vidioc_s_tuner = vidioc_s_tuner,
1841 .vidioc_g_frequency = vidioc_g_frequency,
1842 .vidioc_s_frequency = vidioc_s_frequency,
1843 #ifdef CONFIG_VIDEO_ADV_DEBUG
1844 .vidioc_g_register = vidioc_g_register,
1845 .vidioc_s_register = vidioc_s_register,
1846 #endif
1847 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1848 .vidiocgmbuf = vidiocgmbuf,
1849 #endif
1852 static const struct video_device em28xx_video_template = {
1853 .fops = &em28xx_v4l_fops,
1854 .release = video_device_release,
1855 .ioctl_ops = &video_ioctl_ops,
1857 .minor = -1,
1859 .tvnorms = V4L2_STD_ALL,
1860 .current_norm = V4L2_STD_PAL,
1863 static const struct file_operations radio_fops = {
1864 .owner = THIS_MODULE,
1865 .open = em28xx_v4l2_open,
1866 .release = em28xx_v4l2_close,
1867 .ioctl = video_ioctl2,
1868 .compat_ioctl = v4l_compat_ioctl32,
1869 .llseek = no_llseek,
1872 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1873 .vidioc_querycap = radio_querycap,
1874 .vidioc_g_tuner = radio_g_tuner,
1875 .vidioc_enum_input = radio_enum_input,
1876 .vidioc_g_audio = radio_g_audio,
1877 .vidioc_s_tuner = radio_s_tuner,
1878 .vidioc_s_audio = radio_s_audio,
1879 .vidioc_s_input = radio_s_input,
1880 .vidioc_queryctrl = radio_queryctrl,
1881 .vidioc_g_ctrl = vidioc_g_ctrl,
1882 .vidioc_s_ctrl = vidioc_s_ctrl,
1883 .vidioc_g_frequency = vidioc_g_frequency,
1884 .vidioc_s_frequency = vidioc_s_frequency,
1885 #ifdef CONFIG_VIDEO_ADV_DEBUG
1886 .vidioc_g_register = vidioc_g_register,
1887 .vidioc_s_register = vidioc_s_register,
1888 #endif
1891 static struct video_device em28xx_radio_template = {
1892 .name = "em28xx-radio",
1893 .fops = &radio_fops,
1894 .ioctl_ops = &radio_ioctl_ops,
1895 .minor = -1,
1898 /******************************** usb interface ******************************/
1901 static LIST_HEAD(em28xx_extension_devlist);
1902 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1904 int em28xx_register_extension(struct em28xx_ops *ops)
1906 struct em28xx *dev = NULL;
1908 mutex_lock(&em28xx_devlist_mutex);
1909 mutex_lock(&em28xx_extension_devlist_lock);
1910 list_add_tail(&ops->next, &em28xx_extension_devlist);
1911 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1912 if (dev)
1913 ops->init(dev);
1915 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1916 mutex_unlock(&em28xx_extension_devlist_lock);
1917 mutex_unlock(&em28xx_devlist_mutex);
1918 return 0;
1920 EXPORT_SYMBOL(em28xx_register_extension);
1922 void em28xx_unregister_extension(struct em28xx_ops *ops)
1924 struct em28xx *dev = NULL;
1926 mutex_lock(&em28xx_devlist_mutex);
1927 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1928 if (dev)
1929 ops->fini(dev);
1932 mutex_lock(&em28xx_extension_devlist_lock);
1933 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1934 list_del(&ops->next);
1935 mutex_unlock(&em28xx_extension_devlist_lock);
1936 mutex_unlock(&em28xx_devlist_mutex);
1938 EXPORT_SYMBOL(em28xx_unregister_extension);
1940 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1941 const struct video_device *template,
1942 const char *type_name)
1944 struct video_device *vfd;
1946 vfd = video_device_alloc();
1947 if (NULL == vfd)
1948 return NULL;
1949 *vfd = *template;
1950 vfd->minor = -1;
1951 vfd->parent = &dev->udev->dev;
1952 vfd->release = video_device_release;
1953 vfd->debug = video_debug;
1955 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1956 dev->name, type_name);
1958 return vfd;
1961 static int register_analog_devices(struct em28xx *dev)
1963 int ret;
1965 /* allocate and fill video video_device struct */
1966 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1967 if (!dev->vdev) {
1968 em28xx_errdev("cannot allocate video_device.\n");
1969 return -ENODEV;
1972 /* register v4l2 video video_device */
1973 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1974 video_nr[dev->devno]);
1975 if (ret) {
1976 em28xx_errdev("unable to register video device (error=%i).\n",
1977 ret);
1978 return ret;
1981 /* Allocate and fill vbi video_device struct */
1982 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1984 /* register v4l2 vbi video_device */
1985 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1986 vbi_nr[dev->devno]);
1987 if (ret < 0) {
1988 em28xx_errdev("unable to register vbi device\n");
1989 return ret;
1992 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1993 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1994 if (!dev->radio_dev) {
1995 em28xx_errdev("cannot allocate video_device.\n");
1996 return -ENODEV;
1998 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1999 radio_nr[dev->devno]);
2000 if (ret < 0) {
2001 em28xx_errdev("can't register radio device\n");
2002 return ret;
2004 em28xx_info("Registered radio device as /dev/radio%d\n",
2005 dev->radio_dev->num);
2008 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2009 dev->vdev->num, dev->vbi_dev->num);
2011 return 0;
2016 * em28xx_init_dev()
2017 * allocates and inits the device structs, registers i2c bus and v4l device
2019 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2020 int minor)
2022 struct em28xx_ops *ops = NULL;
2023 struct em28xx *dev = *devhandle;
2024 int retval = -ENOMEM;
2025 int errCode;
2026 unsigned int maxh, maxw;
2028 dev->udev = udev;
2029 mutex_init(&dev->lock);
2030 mutex_init(&dev->ctrl_urb_lock);
2031 spin_lock_init(&dev->slock);
2032 init_waitqueue_head(&dev->open);
2033 init_waitqueue_head(&dev->wait_frame);
2034 init_waitqueue_head(&dev->wait_stream);
2036 dev->em28xx_write_regs = em28xx_write_regs;
2037 dev->em28xx_read_reg = em28xx_read_reg;
2038 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2039 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2040 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2041 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
2043 em28xx_pre_card_setup(dev);
2045 errCode = em28xx_config(dev);
2046 if (errCode) {
2047 em28xx_errdev("error configuring device\n");
2048 return -ENOMEM;
2051 /* register i2c bus */
2052 errCode = em28xx_i2c_register(dev);
2053 if (errCode < 0) {
2054 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2055 __func__, errCode);
2056 return errCode;
2059 /* Do board specific init and eeprom reading */
2060 em28xx_card_setup(dev);
2062 /* Configure audio */
2063 errCode = em28xx_audio_setup(dev);
2064 if (errCode < 0) {
2065 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
2066 __func__, errCode);
2069 /* configure the device */
2070 em28xx_config_i2c(dev);
2072 /* set default norm */
2073 dev->norm = em28xx_video_template.current_norm;
2075 maxw = norm_maxw(dev);
2076 maxh = norm_maxh(dev);
2078 /* set default image size */
2079 dev->width = maxw;
2080 dev->height = maxh;
2081 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2082 dev->hscale = 0;
2083 dev->vscale = 0;
2084 dev->ctl_input = 2;
2086 errCode = em28xx_config(dev);
2087 if (errCode < 0) {
2088 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2089 __func__, errCode);
2090 return errCode;
2093 /* init video dma queues */
2094 INIT_LIST_HEAD(&dev->vidq.active);
2095 INIT_LIST_HEAD(&dev->vidq.queued);
2098 if (dev->board.has_msp34xx) {
2099 /* Send a reset to other chips via gpio */
2100 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
2101 if (errCode < 0) {
2102 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2103 __func__, errCode);
2104 return errCode;
2106 msleep(3);
2108 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
2109 if (errCode < 0) {
2110 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2111 __func__, errCode);
2112 return errCode;
2114 msleep(3);
2117 video_mux(dev, 0);
2119 mutex_lock(&em28xx_devlist_mutex);
2120 list_add_tail(&dev->devlist, &em28xx_devlist);
2121 retval = register_analog_devices(dev);
2122 if (retval < 0) {
2123 em28xx_release_resources(dev);
2124 mutex_unlock(&em28xx_devlist_mutex);
2125 goto fail_reg_devices;
2128 mutex_lock(&em28xx_extension_devlist_lock);
2129 if (!list_empty(&em28xx_extension_devlist)) {
2130 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2131 if (ops->id)
2132 ops->init(dev);
2135 mutex_unlock(&em28xx_extension_devlist_lock);
2136 mutex_unlock(&em28xx_devlist_mutex);
2138 /* Save some power by putting tuner to sleep */
2139 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2141 return 0;
2143 fail_reg_devices:
2144 mutex_unlock(&dev->lock);
2145 return retval;
2148 #if defined(CONFIG_MODULES) && defined(MODULE)
2149 static void request_module_async(struct work_struct *work)
2151 struct em28xx *dev = container_of(work,
2152 struct em28xx, request_module_wk);
2154 if (dev->has_audio_class)
2155 request_module("snd-usb-audio");
2156 else if (dev->has_alsa_audio)
2157 request_module("em28xx-alsa");
2159 if (dev->board.has_dvb)
2160 request_module("em28xx-dvb");
2163 static void request_modules(struct em28xx *dev)
2165 INIT_WORK(&dev->request_module_wk, request_module_async);
2166 schedule_work(&dev->request_module_wk);
2168 #else
2169 #define request_modules(dev)
2170 #endif /* CONFIG_MODULES */
2173 * em28xx_usb_probe()
2174 * checks for supported devices
2176 static int em28xx_usb_probe(struct usb_interface *interface,
2177 const struct usb_device_id *id)
2179 const struct usb_endpoint_descriptor *endpoint;
2180 struct usb_device *udev;
2181 struct usb_interface *uif;
2182 struct em28xx *dev = NULL;
2183 int retval = -ENODEV;
2184 int i, nr, ifnum, isoc_pipe;
2185 char *speed;
2186 char descr[255] = "";
2188 udev = usb_get_dev(interface_to_usbdev(interface));
2189 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2191 /* Check to see next free device and mark as used */
2192 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2193 em28xx_devused |= 1<<nr;
2195 /* Don't register audio interfaces */
2196 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2197 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2198 "interface %i, class %i\n",
2199 le16_to_cpu(udev->descriptor.idVendor),
2200 le16_to_cpu(udev->descriptor.idProduct),
2201 ifnum,
2202 interface->altsetting[0].desc.bInterfaceClass);
2204 em28xx_devused &= ~(1<<nr);
2205 return -ENODEV;
2208 endpoint = &interface->cur_altsetting->endpoint[0].desc;
2210 /* check if the device has the iso in endpoint at the correct place */
2211 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2212 USB_ENDPOINT_XFER_ISOC &&
2213 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2215 /* It's a newer em2874/em2875 device */
2216 isoc_pipe = 0;
2217 } else {
2218 int check_interface = 1;
2219 isoc_pipe = 1;
2220 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2221 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2222 USB_ENDPOINT_XFER_ISOC)
2223 check_interface = 0;
2225 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2226 check_interface = 0;
2228 if (!check_interface) {
2229 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2230 "interface %i, class %i found.\n",
2231 le16_to_cpu(udev->descriptor.idVendor),
2232 le16_to_cpu(udev->descriptor.idProduct),
2233 ifnum,
2234 interface->altsetting[0].desc.bInterfaceClass);
2236 em28xx_err(DRIVER_NAME " This is an anciliary "
2237 "interface not used by the driver\n");
2239 em28xx_devused &= ~(1<<nr);
2240 return -ENODEV;
2244 switch (udev->speed) {
2245 case USB_SPEED_LOW:
2246 speed = "1.5";
2247 break;
2248 case USB_SPEED_UNKNOWN:
2249 case USB_SPEED_FULL:
2250 speed = "12";
2251 break;
2252 case USB_SPEED_HIGH:
2253 speed = "480";
2254 break;
2255 default:
2256 speed = "unknown";
2259 if (udev->manufacturer)
2260 strlcpy(descr, udev->manufacturer, sizeof(descr));
2262 if (udev->product) {
2263 if (*descr)
2264 strlcat(descr, " ", sizeof(descr));
2265 strlcat(descr, udev->product, sizeof(descr));
2267 if (*descr)
2268 strlcat(descr, " ", sizeof(descr));
2270 printk(DRIVER_NAME ": New device %s@ %s Mbps "
2271 "(%04x:%04x, interface %d, class %d)\n",
2272 descr,
2273 speed,
2274 le16_to_cpu(udev->descriptor.idVendor),
2275 le16_to_cpu(udev->descriptor.idProduct),
2276 ifnum,
2277 interface->altsetting->desc.bInterfaceNumber);
2279 if (nr >= EM28XX_MAXBOARDS) {
2280 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2281 EM28XX_MAXBOARDS);
2282 em28xx_devused &= ~(1<<nr);
2283 return -ENOMEM;
2286 /* allocate memory for our device state and initialize it */
2287 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2288 if (dev == NULL) {
2289 em28xx_err(DRIVER_NAME ": out of memory!\n");
2290 em28xx_devused &= ~(1<<nr);
2291 return -ENOMEM;
2294 snprintf(dev->name, 29, "em28xx #%d", nr);
2295 dev->devno = nr;
2296 dev->model = id->driver_info;
2297 dev->alt = -1;
2299 /* Checks if audio is provided by some interface */
2300 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2301 uif = udev->config->interface[i];
2302 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2303 dev->has_audio_class = 1;
2304 break;
2308 /* compute alternate max packet sizes */
2309 uif = udev->actconfig->interface[0];
2311 dev->num_alt = uif->num_altsetting;
2312 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
2313 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2314 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2316 if (dev->alt_max_pkt_size == NULL) {
2317 em28xx_errdev("out of memory!\n");
2318 em28xx_devused &= ~(1<<nr);
2319 kfree(dev);
2320 return -ENOMEM;
2323 for (i = 0; i < dev->num_alt ; i++) {
2324 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
2325 wMaxPacketSize);
2326 dev->alt_max_pkt_size[i] =
2327 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2328 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
2329 dev->alt_max_pkt_size[i]);
2332 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2333 dev->model = card[nr];
2335 /* allocate device struct */
2336 retval = em28xx_init_dev(&dev, udev, nr);
2337 if (retval) {
2338 em28xx_devused &= ~(1<<dev->devno);
2339 kfree(dev);
2341 return retval;
2344 /* save our data pointer in this interface device */
2345 usb_set_intfdata(interface, dev);
2347 request_modules(dev);
2349 return 0;
2353 * em28xx_usb_disconnect()
2354 * called when the device gets diconencted
2355 * video device will be unregistered on v4l2_close in case it is still open
2357 static void em28xx_usb_disconnect(struct usb_interface *interface)
2359 struct em28xx *dev;
2360 struct em28xx_ops *ops = NULL;
2362 dev = usb_get_intfdata(interface);
2363 usb_set_intfdata(interface, NULL);
2365 if (!dev)
2366 return;
2368 em28xx_info("disconnecting %s\n", dev->vdev->name);
2370 /* wait until all current v4l2 io is finished then deallocate
2371 resources */
2372 mutex_lock(&dev->lock);
2374 wake_up_interruptible_all(&dev->open);
2376 if (dev->users) {
2377 em28xx_warn
2378 ("device /dev/video%d is open! Deregistration and memory "
2379 "deallocation are deferred on close.\n",
2380 dev->vdev->num);
2382 dev->state |= DEV_MISCONFIGURED;
2383 em28xx_uninit_isoc(dev);
2384 dev->state |= DEV_DISCONNECTED;
2385 wake_up_interruptible(&dev->wait_frame);
2386 wake_up_interruptible(&dev->wait_stream);
2387 } else {
2388 dev->state |= DEV_DISCONNECTED;
2389 em28xx_release_resources(dev);
2391 mutex_unlock(&dev->lock);
2393 mutex_lock(&em28xx_extension_devlist_lock);
2394 if (!list_empty(&em28xx_extension_devlist)) {
2395 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2396 ops->fini(dev);
2399 mutex_unlock(&em28xx_extension_devlist_lock);
2401 if (!dev->users) {
2402 kfree(dev->alt_max_pkt_size);
2403 kfree(dev);
2407 static struct usb_driver em28xx_usb_driver = {
2408 .name = "em28xx",
2409 .probe = em28xx_usb_probe,
2410 .disconnect = em28xx_usb_disconnect,
2411 .id_table = em28xx_id_table,
2414 static int __init em28xx_module_init(void)
2416 int result;
2418 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2419 (EM28XX_VERSION_CODE >> 16) & 0xff,
2420 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2421 #ifdef SNAPSHOT
2422 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2423 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2424 #endif
2426 /* register this driver with the USB subsystem */
2427 result = usb_register(&em28xx_usb_driver);
2428 if (result)
2429 em28xx_err(DRIVER_NAME
2430 " usb_register failed. Error number %d.\n", result);
2432 return result;
2435 static void __exit em28xx_module_exit(void)
2437 /* deregister this driver with the USB subsystem */
2438 usb_deregister(&em28xx_usb_driver);
2441 module_init(em28xx_module_init);
2442 module_exit(em28xx_module_exit);