V4L/DVB (7615): em28xx: Provide the proper support for switching between analog/digital
[linux-2.6/kvm.git] / drivers / media / video / em28xx / em28xx-video.c
blob4ffd064c402ae0acef043b352d1f18222ad2e0a8
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/msp3400.h>
42 #include <media/tuner.h>
44 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
45 "Markus Rechberger <mrechberger@gmail.com>, " \
46 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
47 "Sascha Sommer <saschasommer@freenet.de>"
49 #define DRIVER_NAME "em28xx"
50 #define DRIVER_DESC "Empia em28xx based USB video device driver"
51 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
53 #define em28xx_videodbg(fmt, arg...) do {\
54 if (video_debug) \
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
58 static unsigned int isoc_debug;
59 module_param(isoc_debug, int, 0644);
60 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
62 #define em28xx_isocdbg(fmt, arg...) \
63 do {\
64 if (isoc_debug) { \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
67 } \
68 } while (0)
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
74 static LIST_HEAD(em28xx_devlist);
76 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81 module_param_array(card, int, NULL, 0444);
82 module_param_array(video_nr, int, NULL, 0444);
83 module_param_array(vbi_nr, int, NULL, 0444);
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(card, "card type");
86 MODULE_PARM_DESC(video_nr, "video device numbers");
87 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88 MODULE_PARM_DESC(radio_nr, "radio device numbers");
90 static unsigned int video_debug;
91 module_param(video_debug, int, 0644);
92 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
94 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95 static unsigned long em28xx_devused;
97 /* supported controls */
98 /* Common to all boards */
99 static struct v4l2_queryctrl em28xx_qctrl[] = {
101 .id = V4L2_CID_AUDIO_VOLUME,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Volume",
104 .minimum = 0x0,
105 .maximum = 0x1f,
106 .step = 0x1,
107 .default_value = 0x1f,
108 .flags = 0,
109 }, {
110 .id = V4L2_CID_AUDIO_MUTE,
111 .type = V4L2_CTRL_TYPE_BOOLEAN,
112 .name = "Mute",
113 .minimum = 0,
114 .maximum = 1,
115 .step = 1,
116 .default_value = 1,
117 .flags = 0,
121 static struct usb_driver em28xx_usb_driver;
123 /* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
128 * Announces that a buffer were filled and request the next
130 static inline void buffer_filled(struct em28xx *dev,
131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
140 dev->isoc_ctl.buf = NULL;
142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
147 * Identify the buffer header type and properly handles
149 static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
152 unsigned char *p,
153 unsigned char *outp, unsigned long len)
155 void *fieldstart, *startwrite, *startread;
156 int linesdone, currlinedone, offset, lencopy, remain;
157 int bytesperline = dev->width << 1;
159 if (dma_q->pos + len > buf->vb.size)
160 len = buf->vb.size - dma_q->pos;
162 if (p[0] != 0x88 && p[0] != 0x22) {
163 em28xx_isocdbg("frame is not complete\n");
164 len += 4;
165 } else
166 p += 4;
168 startread = p;
169 remain = len;
171 /* Interlaces frame */
172 if (buf->top_field)
173 fieldstart = outp;
174 else
175 fieldstart = outp + bytesperline;
177 linesdone = dma_q->pos / bytesperline;
178 currlinedone = dma_q->pos % bytesperline;
179 offset = linesdone * bytesperline * 2 + currlinedone;
180 startwrite = fieldstart + offset;
181 lencopy = bytesperline - currlinedone;
182 lencopy = lencopy > remain ? remain : lencopy;
184 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
185 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
186 ((char *)startwrite + lencopy) -
187 ((char *)outp + buf->vb.size));
188 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
190 if (lencopy <= 0)
191 return;
192 memcpy(startwrite, startread, lencopy);
194 remain -= lencopy;
196 while (remain > 0) {
197 startwrite += lencopy + bytesperline;
198 startread += lencopy;
199 if (bytesperline > remain)
200 lencopy = remain;
201 else
202 lencopy = bytesperline;
204 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
205 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
206 ((char *)startwrite + lencopy) -
207 ((char *)outp + buf->vb.size));
208 lencopy = remain = (char *)outp + buf->vb.size -
209 (char *)startwrite;
211 if (lencopy <= 0)
212 break;
214 memcpy(startwrite, startread, lencopy);
216 remain -= lencopy;
219 dma_q->pos += len;
222 static inline void print_err_status(struct em28xx *dev,
223 int packet, int status)
225 char *errmsg = "Unknown";
227 switch (status) {
228 case -ENOENT:
229 errmsg = "unlinked synchronuously";
230 break;
231 case -ECONNRESET:
232 errmsg = "unlinked asynchronuously";
233 break;
234 case -ENOSR:
235 errmsg = "Buffer error (overrun)";
236 break;
237 case -EPIPE:
238 errmsg = "Stalled (device not responding)";
239 break;
240 case -EOVERFLOW:
241 errmsg = "Babble (bad cable?)";
242 break;
243 case -EPROTO:
244 errmsg = "Bit-stuff error (bad cable?)";
245 break;
246 case -EILSEQ:
247 errmsg = "CRC/Timeout (could be anything)";
248 break;
249 case -ETIME:
250 errmsg = "Device does not respond";
251 break;
253 if (packet < 0) {
254 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
255 } else {
256 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
257 packet, status, errmsg);
262 * video-buf generic routine to get the next available buffer
264 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
265 struct em28xx_buffer **buf)
267 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
268 char *outp;
270 if (list_empty(&dma_q->active)) {
271 em28xx_isocdbg("No active queue to serve\n");
272 dev->isoc_ctl.buf = NULL;
273 *buf = NULL;
274 return;
277 /* Get the next buffer */
278 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280 /* Cleans up buffer - Usefull for testing for frame/URB loss */
281 outp = videobuf_to_vmalloc(&(*buf)->vb);
282 memset(outp, 0, (*buf)->vb.size);
284 dev->isoc_ctl.buf = *buf;
286 return;
290 * Controls the isoc copy of each urb packet
292 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
294 struct em28xx_buffer *buf;
295 struct em28xx_dmaqueue *dma_q = urb->context;
296 unsigned char *outp = NULL;
297 int i, len = 0, rc = 1;
298 unsigned char *p;
300 if (!dev)
301 return 0;
303 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
304 return 0;
306 if (urb->status < 0) {
307 print_err_status(dev, -1, urb->status);
308 if (urb->status == -ENOENT)
309 return 0;
312 buf = dev->isoc_ctl.buf;
313 if (buf != NULL)
314 outp = videobuf_to_vmalloc(&buf->vb);
316 for (i = 0; i < urb->number_of_packets; i++) {
317 int status = urb->iso_frame_desc[i].status;
319 if (status < 0) {
320 print_err_status(dev, i, status);
321 if (urb->iso_frame_desc[i].status != -EPROTO)
322 continue;
325 len = urb->iso_frame_desc[i].actual_length - 4;
327 if (urb->iso_frame_desc[i].actual_length <= 0) {
328 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
329 continue;
331 if (urb->iso_frame_desc[i].actual_length >
332 dev->max_pkt_size) {
333 em28xx_isocdbg("packet bigger than packet size");
334 continue;
337 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
339 /* FIXME: incomplete buffer checks where removed to make
340 logic simpler. Impacts of those changes should be evaluated
342 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
343 em28xx_isocdbg("VBI HEADER!!!\n");
344 /* FIXME: Should add vbi copy */
345 continue;
347 if (p[0] == 0x22 && p[1] == 0x5a) {
348 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
349 len, (p[2] & 1)? "odd" : "even");
351 if (!(p[2] & 1)) {
352 if (buf != NULL)
353 buffer_filled(dev, dma_q, buf);
354 get_next_buf(dma_q, &buf);
355 if (buf == NULL)
356 outp = NULL;
357 else
358 outp = videobuf_to_vmalloc(&buf->vb);
361 if (buf != NULL) {
362 if (p[2] & 1)
363 buf->top_field = 0;
364 else
365 buf->top_field = 1;
368 dma_q->pos = 0;
370 if (buf != NULL)
371 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
373 return rc;
376 /* ------------------------------------------------------------------
377 Videobuf operations
378 ------------------------------------------------------------------*/
380 static int
381 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383 struct em28xx_fh *fh = vq->priv_data;
384 struct em28xx *dev = fh->dev;
385 struct v4l2_frequency f;
387 *size = 16 * fh->dev->width * fh->dev->height >> 3;
388 if (0 == *count)
389 *count = EM28XX_DEF_BUF;
391 if (*count < EM28XX_MIN_BUF)
392 *count = EM28XX_MIN_BUF;
394 dev->mode = EM28XX_ANALOG_MODE;
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)
518 /* Sets I2C speed to 100 KHz */
519 if (!dev->is_em2800)
520 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
522 /* enable vbi capturing */
524 /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
525 /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
526 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
528 dev->mute = 1; /* maybe not the right place... */
529 dev->volume = 0x1f;
531 em28xx_outfmt_set_yuv422(dev);
532 em28xx_colorlevels_set_default(dev);
533 em28xx_compression_disable(dev);
535 return 0;
539 * em28xx_config_i2c()
540 * configure i2c attached devices
542 static void em28xx_config_i2c(struct em28xx *dev)
544 struct v4l2_routing route;
546 route.input = INPUT(dev->ctl_input)->vmux;
547 route.output = 0;
548 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
549 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
550 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
553 static void video_mux(struct em28xx *dev, int index)
555 struct v4l2_routing route;
557 route.input = INPUT(index)->vmux;
558 route.output = 0;
559 dev->ctl_input = index;
560 dev->ctl_ainput = INPUT(index)->amux;
562 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
564 if (dev->has_msp34xx) {
565 if (dev->i2s_speed) {
566 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
567 &dev->i2s_speed);
569 route.input = dev->ctl_ainput;
570 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
571 /* Note: this is msp3400 specific */
572 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
573 &route);
576 em28xx_audio_analog_set(dev);
579 /* Usage lock check functions */
580 static int res_get(struct em28xx_fh *fh)
582 struct em28xx *dev = fh->dev;
583 int rc = 0;
585 /* This instance already has stream_on */
586 if (fh->stream_on)
587 return rc;
589 if (dev->stream_on)
590 return -EINVAL;
592 mutex_lock(&dev->lock);
593 dev->stream_on = 1;
594 fh->stream_on = 1;
595 mutex_unlock(&dev->lock);
596 return rc;
599 static int res_check(struct em28xx_fh *fh)
601 return (fh->stream_on);
604 static void res_free(struct em28xx_fh *fh)
606 struct em28xx *dev = fh->dev;
608 mutex_lock(&dev->lock);
609 fh->stream_on = 0;
610 dev->stream_on = 0;
611 mutex_unlock(&dev->lock);
615 * em28xx_get_ctrl()
616 * return the current saturation, brightness or contrast, mute state
618 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
620 switch (ctrl->id) {
621 case V4L2_CID_AUDIO_MUTE:
622 ctrl->value = dev->mute;
623 return 0;
624 case V4L2_CID_AUDIO_VOLUME:
625 ctrl->value = dev->volume;
626 return 0;
627 default:
628 return -EINVAL;
633 * em28xx_set_ctrl()
634 * mute or set new saturation, brightness or contrast
636 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
638 switch (ctrl->id) {
639 case V4L2_CID_AUDIO_MUTE:
640 if (ctrl->value != dev->mute) {
641 dev->mute = ctrl->value;
642 return em28xx_audio_analog_set(dev);
644 return 0;
645 case V4L2_CID_AUDIO_VOLUME:
646 dev->volume = ctrl->value;
647 return em28xx_audio_analog_set(dev);
648 default:
649 return -EINVAL;
653 static int check_dev(struct em28xx *dev)
655 if (dev->state & DEV_DISCONNECTED) {
656 em28xx_errdev("v4l2 ioctl: device not present\n");
657 return -ENODEV;
660 if (dev->state & DEV_MISCONFIGURED) {
661 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
662 "close and open it again\n");
663 return -EIO;
665 return 0;
668 static void get_scale(struct em28xx *dev,
669 unsigned int width, unsigned int height,
670 unsigned int *hscale, unsigned int *vscale)
672 unsigned int maxw = norm_maxw(dev);
673 unsigned int maxh = norm_maxh(dev);
675 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
676 if (*hscale >= 0x4000)
677 *hscale = 0x3fff;
679 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
680 if (*vscale >= 0x4000)
681 *vscale = 0x3fff;
684 /* ------------------------------------------------------------------
685 IOCTL vidioc handling
686 ------------------------------------------------------------------*/
688 static int vidioc_g_fmt_cap(struct file *file, void *priv,
689 struct v4l2_format *f)
691 struct em28xx_fh *fh = priv;
692 struct em28xx *dev = fh->dev;
694 mutex_lock(&dev->lock);
696 f->fmt.pix.width = dev->width;
697 f->fmt.pix.height = dev->height;
698 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
699 f->fmt.pix.bytesperline = dev->width * 2;
700 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
701 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
703 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
704 f->fmt.pix.field = dev->interlaced ?
705 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
707 mutex_unlock(&dev->lock);
708 return 0;
711 static int vidioc_try_fmt_cap(struct file *file, void *priv,
712 struct v4l2_format *f)
714 struct em28xx_fh *fh = priv;
715 struct em28xx *dev = fh->dev;
716 int width = f->fmt.pix.width;
717 int height = f->fmt.pix.height;
718 unsigned int maxw = norm_maxw(dev);
719 unsigned int maxh = norm_maxh(dev);
720 unsigned int hscale, vscale;
722 /* width must even because of the YUYV format
723 height must be even because of interlacing */
724 height &= 0xfffe;
725 width &= 0xfffe;
727 if (height < 32)
728 height = 32;
729 if (height > maxh)
730 height = maxh;
731 if (width < 48)
732 width = 48;
733 if (width > maxw)
734 width = maxw;
736 mutex_lock(&dev->lock);
738 if (dev->is_em2800) {
739 /* the em2800 can only scale down to 50% */
740 if (height % (maxh / 2))
741 height = maxh;
742 if (width % (maxw / 2))
743 width = maxw;
744 /* according to empiatech support */
745 /* the MaxPacketSize is to small to support */
746 /* framesizes larger than 640x480 @ 30 fps */
747 /* or 640x576 @ 25 fps. As this would cut */
748 /* of a part of the image we prefer */
749 /* 360x576 or 360x480 for now */
750 if (width == maxw && height == maxh)
751 width /= 2;
754 get_scale(dev, width, height, &hscale, &vscale);
756 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
757 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
759 f->fmt.pix.width = width;
760 f->fmt.pix.height = height;
761 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
762 f->fmt.pix.bytesperline = width * 2;
763 f->fmt.pix.sizeimage = width * 2 * height;
764 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
765 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
767 mutex_unlock(&dev->lock);
768 return 0;
771 static int vidioc_s_fmt_cap(struct file *file, void *priv,
772 struct v4l2_format *f)
774 struct em28xx_fh *fh = priv;
775 struct em28xx *dev = fh->dev;
776 int rc;
778 rc = check_dev(dev);
779 if (rc < 0)
780 return rc;
782 vidioc_try_fmt_cap(file, priv, f);
784 mutex_lock(&dev->lock);
786 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
787 em28xx_errdev("%s queue busy\n", __func__);
788 rc = -EBUSY;
789 goto out;
792 if (dev->stream_on && !fh->stream_on) {
793 em28xx_errdev("%s device in use by another fh\n", __func__);
794 rc = -EBUSY;
795 goto out;
798 /* set new image size */
799 dev->width = f->fmt.pix.width;
800 dev->height = f->fmt.pix.height;
801 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
803 em28xx_set_alternate(dev);
804 em28xx_resolution_set(dev);
806 rc = 0;
808 out:
809 mutex_unlock(&dev->lock);
810 return rc;
813 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
815 struct em28xx_fh *fh = priv;
816 struct em28xx *dev = fh->dev;
817 struct v4l2_format f;
818 int rc;
820 rc = check_dev(dev);
821 if (rc < 0)
822 return rc;
824 mutex_lock(&dev->lock);
825 dev->norm = *norm;
826 mutex_unlock(&dev->lock);
828 /* Adjusts width/height, if needed */
829 f.fmt.pix.width = dev->width;
830 f.fmt.pix.height = dev->height;
831 vidioc_try_fmt_cap(file, priv, &f);
833 mutex_lock(&dev->lock);
835 /* set new image size */
836 dev->width = f.fmt.pix.width;
837 dev->height = f.fmt.pix.height;
838 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
840 em28xx_resolution_set(dev);
841 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
843 mutex_unlock(&dev->lock);
844 return 0;
847 static const char *iname[] = {
848 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
849 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
850 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
851 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
852 [EM28XX_VMUX_SVIDEO] = "S-Video",
853 [EM28XX_VMUX_TELEVISION] = "Television",
854 [EM28XX_VMUX_CABLE] = "Cable TV",
855 [EM28XX_VMUX_DVB] = "DVB",
856 [EM28XX_VMUX_DEBUG] = "for debug only",
859 static int vidioc_enum_input(struct file *file, void *priv,
860 struct v4l2_input *i)
862 struct em28xx_fh *fh = priv;
863 struct em28xx *dev = fh->dev;
864 unsigned int n;
866 n = i->index;
867 if (n >= MAX_EM28XX_INPUT)
868 return -EINVAL;
869 if (0 == INPUT(n)->type)
870 return -EINVAL;
872 i->index = n;
873 i->type = V4L2_INPUT_TYPE_CAMERA;
875 strcpy(i->name, iname[INPUT(n)->type]);
877 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
878 (EM28XX_VMUX_CABLE == INPUT(n)->type))
879 i->type = V4L2_INPUT_TYPE_TUNER;
881 i->std = dev->vdev->tvnorms;
883 return 0;
886 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
888 struct em28xx_fh *fh = priv;
889 struct em28xx *dev = fh->dev;
891 *i = dev->ctl_input;
893 return 0;
896 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
898 struct em28xx_fh *fh = priv;
899 struct em28xx *dev = fh->dev;
900 int rc;
902 rc = check_dev(dev);
903 if (rc < 0)
904 return rc;
906 if (i >= MAX_EM28XX_INPUT)
907 return -EINVAL;
908 if (0 == INPUT(i)->type)
909 return -EINVAL;
911 mutex_lock(&dev->lock);
913 video_mux(dev, i);
915 mutex_unlock(&dev->lock);
916 return 0;
919 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
921 struct em28xx_fh *fh = priv;
922 struct em28xx *dev = fh->dev;
923 unsigned int index = a->index;
925 if (a->index > 1)
926 return -EINVAL;
928 index = dev->ctl_ainput;
930 if (index == 0)
931 strcpy(a->name, "Television");
932 else
933 strcpy(a->name, "Line In");
935 a->capability = V4L2_AUDCAP_STEREO;
936 a->index = index;
938 return 0;
941 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
943 struct em28xx_fh *fh = priv;
944 struct em28xx *dev = fh->dev;
946 if (a->index != dev->ctl_ainput)
947 return -EINVAL;
949 return 0;
952 static int vidioc_queryctrl(struct file *file, void *priv,
953 struct v4l2_queryctrl *qc)
955 struct em28xx_fh *fh = priv;
956 struct em28xx *dev = fh->dev;
957 int id = qc->id;
958 int i;
959 int rc;
961 rc = check_dev(dev);
962 if (rc < 0)
963 return rc;
965 memset(qc, 0, sizeof(*qc));
967 qc->id = id;
969 if (!dev->has_msp34xx) {
970 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
971 if (qc->id && qc->id == em28xx_qctrl[i].id) {
972 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
973 return 0;
977 mutex_lock(&dev->lock);
978 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
979 mutex_unlock(&dev->lock);
981 if (qc->type)
982 return 0;
983 else
984 return -EINVAL;
987 static int vidioc_g_ctrl(struct file *file, void *priv,
988 struct v4l2_control *ctrl)
990 struct em28xx_fh *fh = priv;
991 struct em28xx *dev = fh->dev;
992 int rc;
994 rc = check_dev(dev);
995 if (rc < 0)
996 return rc;
997 mutex_lock(&dev->lock);
999 if (!dev->has_msp34xx)
1000 rc = em28xx_get_ctrl(dev, ctrl);
1001 else
1002 rc = -EINVAL;
1004 if (rc == -EINVAL) {
1005 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1006 rc = 0;
1009 mutex_unlock(&dev->lock);
1010 return rc;
1013 static int vidioc_s_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1018 u8 i;
1019 int rc;
1021 rc = check_dev(dev);
1022 if (rc < 0)
1023 return rc;
1025 mutex_lock(&dev->lock);
1027 if (dev->has_msp34xx)
1028 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1029 else {
1030 rc = 1;
1031 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1032 if (ctrl->id == em28xx_qctrl[i].id) {
1033 if (ctrl->value < em28xx_qctrl[i].minimum ||
1034 ctrl->value > em28xx_qctrl[i].maximum) {
1035 rc = -ERANGE;
1036 break;
1039 rc = em28xx_set_ctrl(dev, ctrl);
1040 break;
1045 /* Control not found - try to send it to the attached devices */
1046 if (rc == 1) {
1047 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1048 rc = 0;
1051 mutex_unlock(&dev->lock);
1052 return rc;
1055 static int vidioc_g_tuner(struct file *file, void *priv,
1056 struct v4l2_tuner *t)
1058 struct em28xx_fh *fh = priv;
1059 struct em28xx *dev = fh->dev;
1060 int rc;
1062 rc = check_dev(dev);
1063 if (rc < 0)
1064 return rc;
1066 if (0 != t->index)
1067 return -EINVAL;
1069 strcpy(t->name, "Tuner");
1071 mutex_lock(&dev->lock);
1073 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1075 mutex_unlock(&dev->lock);
1076 return 0;
1079 static int vidioc_s_tuner(struct file *file, void *priv,
1080 struct v4l2_tuner *t)
1082 struct em28xx_fh *fh = priv;
1083 struct em28xx *dev = fh->dev;
1084 int rc;
1086 rc = check_dev(dev);
1087 if (rc < 0)
1088 return rc;
1090 if (0 != t->index)
1091 return -EINVAL;
1093 mutex_lock(&dev->lock);
1095 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1097 mutex_unlock(&dev->lock);
1098 return 0;
1101 static int vidioc_g_frequency(struct file *file, void *priv,
1102 struct v4l2_frequency *f)
1104 struct em28xx_fh *fh = priv;
1105 struct em28xx *dev = fh->dev;
1107 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1108 f->frequency = dev->ctl_freq;
1110 return 0;
1113 static int vidioc_s_frequency(struct file *file, void *priv,
1114 struct v4l2_frequency *f)
1116 struct em28xx_fh *fh = priv;
1117 struct em28xx *dev = fh->dev;
1118 int rc;
1120 rc = check_dev(dev);
1121 if (rc < 0)
1122 return rc;
1124 if (0 != f->tuner)
1125 return -EINVAL;
1127 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1128 return -EINVAL;
1129 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1130 return -EINVAL;
1132 mutex_lock(&dev->lock);
1134 dev->ctl_freq = f->frequency;
1135 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1137 mutex_unlock(&dev->lock);
1138 return 0;
1141 #ifdef CONFIG_VIDEO_ADV_DEBUG
1142 static int em28xx_reg_len(int reg)
1144 switch (reg) {
1145 case EM28XX_R40_AC97LSB:
1146 case EM28XX_R30_HSCALELOW:
1147 case EM28XX_R32_VSCALELOW:
1148 return 2;
1149 default:
1150 return 1;
1154 static int vidioc_g_register(struct file *file, void *priv,
1155 struct v4l2_register *reg)
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 int ret;
1161 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1162 return -EINVAL;
1164 if (em28xx_reg_len(reg->reg) == 1) {
1165 ret = em28xx_read_reg(dev, reg->reg);
1166 if (ret < 0)
1167 return ret;
1169 reg->val = ret;
1170 } else {
1171 u64 val = 0;
1172 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1173 reg->reg, (char *)&val, 2);
1174 if (ret < 0)
1175 return ret;
1177 reg->val = cpu_to_le64((__u64)val);
1180 return 0;
1183 static int vidioc_s_register(struct file *file, void *priv,
1184 struct v4l2_register *reg)
1186 struct em28xx_fh *fh = priv;
1187 struct em28xx *dev = fh->dev;
1188 u64 buf;
1190 buf = le64_to_cpu((__u64)reg->val);
1192 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1193 em28xx_reg_len(reg->reg));
1195 #endif
1198 static int vidioc_cropcap(struct file *file, void *priv,
1199 struct v4l2_cropcap *cc)
1201 struct em28xx_fh *fh = priv;
1202 struct em28xx *dev = fh->dev;
1204 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 return -EINVAL;
1207 cc->bounds.left = 0;
1208 cc->bounds.top = 0;
1209 cc->bounds.width = dev->width;
1210 cc->bounds.height = dev->height;
1211 cc->defrect = cc->bounds;
1212 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1213 cc->pixelaspect.denominator = 59;
1215 return 0;
1218 static int vidioc_streamon(struct file *file, void *priv,
1219 enum v4l2_buf_type type)
1221 struct em28xx_fh *fh = priv;
1222 struct em28xx *dev = fh->dev;
1223 int rc;
1225 rc = check_dev(dev);
1226 if (rc < 0)
1227 return rc;
1230 if (unlikely(res_get(fh) < 0))
1231 return -EBUSY;
1233 return (videobuf_streamon(&fh->vb_vidq));
1236 static int vidioc_streamoff(struct file *file, void *priv,
1237 enum v4l2_buf_type type)
1239 struct em28xx_fh *fh = priv;
1240 struct em28xx *dev = fh->dev;
1241 int rc;
1243 rc = check_dev(dev);
1244 if (rc < 0)
1245 return rc;
1247 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1248 return -EINVAL;
1249 if (type != fh->type)
1250 return -EINVAL;
1252 videobuf_streamoff(&fh->vb_vidq);
1253 res_free(fh);
1255 return 0;
1258 static int vidioc_querycap(struct file *file, void *priv,
1259 struct v4l2_capability *cap)
1261 struct em28xx_fh *fh = priv;
1262 struct em28xx *dev = fh->dev;
1264 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1265 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1266 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1268 cap->version = EM28XX_VERSION_CODE;
1270 cap->capabilities =
1271 V4L2_CAP_SLICED_VBI_CAPTURE |
1272 V4L2_CAP_VIDEO_CAPTURE |
1273 V4L2_CAP_AUDIO |
1274 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1276 if (dev->tuner_type != TUNER_ABSENT)
1277 cap->capabilities |= V4L2_CAP_TUNER;
1279 return 0;
1282 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1283 struct v4l2_fmtdesc *fmtd)
1285 if (fmtd->index != 0)
1286 return -EINVAL;
1288 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1289 strcpy(fmtd->description, "Packed YUY2");
1290 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1291 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1293 return 0;
1296 /* Sliced VBI ioctls */
1297 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1298 struct v4l2_format *f)
1300 struct em28xx_fh *fh = priv;
1301 struct em28xx *dev = fh->dev;
1302 int rc;
1304 rc = check_dev(dev);
1305 if (rc < 0)
1306 return rc;
1308 mutex_lock(&dev->lock);
1310 f->fmt.sliced.service_set = 0;
1312 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1314 if (f->fmt.sliced.service_set == 0)
1315 rc = -EINVAL;
1317 mutex_unlock(&dev->lock);
1318 return rc;
1321 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1322 struct v4l2_format *f)
1324 struct em28xx_fh *fh = priv;
1325 struct em28xx *dev = fh->dev;
1326 int rc;
1328 rc = check_dev(dev);
1329 if (rc < 0)
1330 return rc;
1332 mutex_lock(&dev->lock);
1333 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1334 mutex_unlock(&dev->lock);
1336 if (f->fmt.sliced.service_set == 0)
1337 return -EINVAL;
1339 return 0;
1343 static int vidioc_reqbufs(struct file *file, void *priv,
1344 struct v4l2_requestbuffers *rb)
1346 struct em28xx_fh *fh = priv;
1347 struct em28xx *dev = fh->dev;
1348 int rc;
1350 rc = check_dev(dev);
1351 if (rc < 0)
1352 return rc;
1354 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1357 static int vidioc_querybuf(struct file *file, void *priv,
1358 struct v4l2_buffer *b)
1360 struct em28xx_fh *fh = priv;
1361 struct em28xx *dev = fh->dev;
1362 int rc;
1364 rc = check_dev(dev);
1365 if (rc < 0)
1366 return rc;
1368 return (videobuf_querybuf(&fh->vb_vidq, b));
1371 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1373 struct em28xx_fh *fh = priv;
1374 struct em28xx *dev = fh->dev;
1375 int rc;
1377 rc = check_dev(dev);
1378 if (rc < 0)
1379 return rc;
1381 return (videobuf_qbuf(&fh->vb_vidq, b));
1384 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1386 struct em28xx_fh *fh = priv;
1387 struct em28xx *dev = fh->dev;
1388 int rc;
1390 rc = check_dev(dev);
1391 if (rc < 0)
1392 return rc;
1394 return (videobuf_dqbuf(&fh->vb_vidq, b,
1395 file->f_flags & O_NONBLOCK));
1398 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1399 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1401 struct em28xx_fh *fh = priv;
1403 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1405 #endif
1408 /* ----------------------------------------------------------- */
1409 /* RADIO ESPECIFIC IOCTLS */
1410 /* ----------------------------------------------------------- */
1412 static int radio_querycap(struct file *file, void *priv,
1413 struct v4l2_capability *cap)
1415 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1417 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1418 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1419 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1421 cap->version = EM28XX_VERSION_CODE;
1422 cap->capabilities = V4L2_CAP_TUNER;
1423 return 0;
1426 static int radio_g_tuner(struct file *file, void *priv,
1427 struct v4l2_tuner *t)
1429 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1431 if (unlikely(t->index > 0))
1432 return -EINVAL;
1434 strcpy(t->name, "Radio");
1435 t->type = V4L2_TUNER_RADIO;
1437 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1438 return 0;
1441 static int radio_enum_input(struct file *file, void *priv,
1442 struct v4l2_input *i)
1444 if (i->index != 0)
1445 return -EINVAL;
1446 strcpy(i->name, "Radio");
1447 i->type = V4L2_INPUT_TYPE_TUNER;
1449 return 0;
1452 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1454 if (unlikely(a->index))
1455 return -EINVAL;
1457 strcpy(a->name, "Radio");
1458 return 0;
1461 static int radio_s_tuner(struct file *file, void *priv,
1462 struct v4l2_tuner *t)
1464 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1466 if (0 != t->index)
1467 return -EINVAL;
1469 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1471 return 0;
1474 static int radio_s_audio(struct file *file, void *fh,
1475 struct v4l2_audio *a)
1477 return 0;
1480 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1482 return 0;
1485 static int radio_queryctrl(struct file *file, void *priv,
1486 struct v4l2_queryctrl *qc)
1488 int i;
1490 if (qc->id < V4L2_CID_BASE ||
1491 qc->id >= V4L2_CID_LASTP1)
1492 return -EINVAL;
1494 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1495 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1496 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1497 return 0;
1501 return -EINVAL;
1505 * em28xx_v4l2_open()
1506 * inits the device and starts isoc transfer
1508 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1510 int minor = iminor(inode);
1511 int errCode = 0, radio = 0;
1512 struct em28xx *h, *dev = NULL;
1513 struct em28xx_fh *fh;
1514 enum v4l2_buf_type fh_type = 0;
1516 list_for_each_entry(h, &em28xx_devlist, devlist) {
1517 if (h->vdev->minor == minor) {
1518 dev = h;
1519 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1521 if (h->vbi_dev->minor == minor) {
1522 dev = h;
1523 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1525 if (h->radio_dev &&
1526 h->radio_dev->minor == minor) {
1527 radio = 1;
1528 dev = h;
1531 if (NULL == dev)
1532 return -ENODEV;
1534 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1535 minor, v4l2_type_names[fh_type], dev->users);
1538 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1539 if (!fh) {
1540 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1541 return -ENOMEM;
1543 mutex_lock(&dev->lock);
1544 fh->dev = dev;
1545 fh->radio = radio;
1546 fh->type = fh_type;
1547 filp->private_data = fh;
1549 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1550 dev->width = norm_maxw(dev);
1551 dev->height = norm_maxh(dev);
1552 dev->hscale = 0;
1553 dev->vscale = 0;
1555 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1556 em28xx_set_alternate(dev);
1557 em28xx_resolution_set(dev);
1559 /* Needed, since GPIO might have disabled power of
1560 some i2c device
1562 em28xx_config_i2c(dev);
1565 if (fh->radio) {
1566 em28xx_videodbg("video_open: setting radio device\n");
1567 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1570 dev->users++;
1572 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1573 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1574 sizeof(struct em28xx_buffer), fh);
1576 mutex_unlock(&dev->lock);
1578 return errCode;
1582 * em28xx_realease_resources()
1583 * unregisters the v4l2,i2c and usb devices
1584 * called when the device gets disconected or at module unload
1586 static void em28xx_release_resources(struct em28xx *dev)
1589 /*FIXME: I2C IR should be disconnected */
1591 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1592 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1593 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1594 list_del(&dev->devlist);
1595 if (dev->radio_dev) {
1596 if (-1 != dev->radio_dev->minor)
1597 video_unregister_device(dev->radio_dev);
1598 else
1599 video_device_release(dev->radio_dev);
1600 dev->radio_dev = NULL;
1602 if (dev->vbi_dev) {
1603 if (-1 != dev->vbi_dev->minor)
1604 video_unregister_device(dev->vbi_dev);
1605 else
1606 video_device_release(dev->vbi_dev);
1607 dev->vbi_dev = NULL;
1609 if (dev->vdev) {
1610 if (-1 != dev->vdev->minor)
1611 video_unregister_device(dev->vdev);
1612 else
1613 video_device_release(dev->vdev);
1614 dev->vdev = NULL;
1616 em28xx_i2c_unregister(dev);
1617 usb_put_dev(dev->udev);
1619 /* Mark device as unused */
1620 em28xx_devused &= ~(1<<dev->devno);
1624 * em28xx_v4l2_close()
1625 * stops streaming and deallocates all resources allocated by the v4l2
1626 * calls and ioctls
1628 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1630 struct em28xx_fh *fh = filp->private_data;
1631 struct em28xx *dev = fh->dev;
1632 int errCode;
1634 em28xx_videodbg("users=%d\n", dev->users);
1637 if (res_check(fh))
1638 res_free(fh);
1640 mutex_lock(&dev->lock);
1642 if (dev->users == 1) {
1643 videobuf_stop(&fh->vb_vidq);
1644 videobuf_mmap_free(&fh->vb_vidq);
1646 /* the device is already disconnect,
1647 free the remaining resources */
1648 if (dev->state & DEV_DISCONNECTED) {
1649 em28xx_release_resources(dev);
1650 mutex_unlock(&dev->lock);
1651 kfree(dev);
1652 return 0;
1655 /* do this before setting alternate! */
1656 em28xx_uninit_isoc(dev);
1657 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
1659 /* set alternate 0 */
1660 dev->alt = 0;
1661 em28xx_videodbg("setting alternate 0\n");
1662 errCode = usb_set_interface(dev->udev, 0, 0);
1663 if (errCode < 0) {
1664 em28xx_errdev("cannot change alternate number to "
1665 "0 (error=%i)\n", errCode);
1668 kfree(fh);
1669 dev->users--;
1670 wake_up_interruptible_nr(&dev->open, 1);
1671 mutex_unlock(&dev->lock);
1672 return 0;
1676 * em28xx_v4l2_read()
1677 * will allocate buffers when called for the first time
1679 static ssize_t
1680 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1681 loff_t *pos)
1683 struct em28xx_fh *fh = filp->private_data;
1684 struct em28xx *dev = fh->dev;
1685 int rc;
1687 rc = check_dev(dev);
1688 if (rc < 0)
1689 return rc;
1691 /* FIXME: read() is not prepared to allow changing the video
1692 resolution while streaming. Seems a bug at em28xx_set_fmt
1695 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1696 if (unlikely(res_get(fh)))
1697 return -EBUSY;
1699 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1700 filp->f_flags & O_NONBLOCK);
1702 return 0;
1706 * em28xx_v4l2_poll()
1707 * will allocate buffers when called for the first time
1709 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1711 struct em28xx_fh *fh = filp->private_data;
1712 struct em28xx *dev = fh->dev;
1713 int rc;
1715 rc = check_dev(dev);
1716 if (rc < 0)
1717 return rc;
1719 if (unlikely(res_get(fh) < 0))
1720 return POLLERR;
1722 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1723 return POLLERR;
1725 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1729 * em28xx_v4l2_mmap()
1731 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1733 struct em28xx_fh *fh = filp->private_data;
1734 struct em28xx *dev = fh->dev;
1735 int rc;
1737 if (unlikely(res_get(fh) < 0))
1738 return -EBUSY;
1740 rc = check_dev(dev);
1741 if (rc < 0)
1742 return rc;
1744 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1746 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1747 (unsigned long)vma->vm_start,
1748 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1749 rc);
1751 return rc;
1754 static const struct file_operations em28xx_v4l_fops = {
1755 .owner = THIS_MODULE,
1756 .open = em28xx_v4l2_open,
1757 .release = em28xx_v4l2_close,
1758 .read = em28xx_v4l2_read,
1759 .poll = em28xx_v4l2_poll,
1760 .mmap = em28xx_v4l2_mmap,
1761 .ioctl = video_ioctl2,
1762 .llseek = no_llseek,
1763 .compat_ioctl = v4l_compat_ioctl32,
1766 static const struct file_operations radio_fops = {
1767 .owner = THIS_MODULE,
1768 .open = em28xx_v4l2_open,
1769 .release = em28xx_v4l2_close,
1770 .ioctl = video_ioctl2,
1771 .compat_ioctl = v4l_compat_ioctl32,
1772 .llseek = no_llseek,
1775 static const struct video_device em28xx_video_template = {
1776 .fops = &em28xx_v4l_fops,
1777 .release = video_device_release,
1779 .minor = -1,
1780 .vidioc_querycap = vidioc_querycap,
1781 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1782 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1783 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1784 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1785 .vidioc_g_audio = vidioc_g_audio,
1786 .vidioc_s_audio = vidioc_s_audio,
1787 .vidioc_cropcap = vidioc_cropcap,
1789 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1790 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1791 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1793 .vidioc_reqbufs = vidioc_reqbufs,
1794 .vidioc_querybuf = vidioc_querybuf,
1795 .vidioc_qbuf = vidioc_qbuf,
1796 .vidioc_dqbuf = vidioc_dqbuf,
1797 .vidioc_s_std = vidioc_s_std,
1798 .vidioc_enum_input = vidioc_enum_input,
1799 .vidioc_g_input = vidioc_g_input,
1800 .vidioc_s_input = vidioc_s_input,
1801 .vidioc_queryctrl = vidioc_queryctrl,
1802 .vidioc_g_ctrl = vidioc_g_ctrl,
1803 .vidioc_s_ctrl = vidioc_s_ctrl,
1804 .vidioc_streamon = vidioc_streamon,
1805 .vidioc_streamoff = vidioc_streamoff,
1806 .vidioc_g_tuner = vidioc_g_tuner,
1807 .vidioc_s_tuner = vidioc_s_tuner,
1808 .vidioc_g_frequency = vidioc_g_frequency,
1809 .vidioc_s_frequency = vidioc_s_frequency,
1810 #ifdef CONFIG_VIDEO_ADV_DEBUG
1811 .vidioc_g_register = vidioc_g_register,
1812 .vidioc_s_register = vidioc_s_register,
1813 #endif
1814 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1815 .vidiocgmbuf = vidiocgmbuf,
1816 #endif
1818 .tvnorms = V4L2_STD_ALL,
1819 .current_norm = V4L2_STD_PAL,
1822 static struct video_device em28xx_radio_template = {
1823 .name = "em28xx-radio",
1824 .type = VID_TYPE_TUNER,
1825 .fops = &radio_fops,
1826 .minor = -1,
1827 .vidioc_querycap = radio_querycap,
1828 .vidioc_g_tuner = radio_g_tuner,
1829 .vidioc_enum_input = radio_enum_input,
1830 .vidioc_g_audio = radio_g_audio,
1831 .vidioc_s_tuner = radio_s_tuner,
1832 .vidioc_s_audio = radio_s_audio,
1833 .vidioc_s_input = radio_s_input,
1834 .vidioc_queryctrl = radio_queryctrl,
1835 .vidioc_g_ctrl = vidioc_g_ctrl,
1836 .vidioc_s_ctrl = vidioc_s_ctrl,
1837 .vidioc_g_frequency = vidioc_g_frequency,
1838 .vidioc_s_frequency = vidioc_s_frequency,
1839 #ifdef CONFIG_VIDEO_ADV_DEBUG
1840 .vidioc_g_register = vidioc_g_register,
1841 .vidioc_s_register = vidioc_s_register,
1842 #endif
1845 /******************************** usb interface ******************************/
1848 static LIST_HEAD(em28xx_extension_devlist);
1849 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1851 int em28xx_register_extension(struct em28xx_ops *ops)
1853 struct em28xx *h, *dev = NULL;
1855 list_for_each_entry(h, &em28xx_devlist, devlist)
1856 dev = h;
1858 mutex_lock(&em28xx_extension_devlist_lock);
1859 list_add_tail(&ops->next, &em28xx_extension_devlist);
1860 if (dev)
1861 ops->init(dev);
1863 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1864 mutex_unlock(&em28xx_extension_devlist_lock);
1866 return 0;
1868 EXPORT_SYMBOL(em28xx_register_extension);
1870 void em28xx_unregister_extension(struct em28xx_ops *ops)
1872 struct em28xx *h, *dev = NULL;
1874 list_for_each_entry(h, &em28xx_devlist, devlist)
1875 dev = h;
1877 if (dev)
1878 ops->fini(dev);
1880 mutex_lock(&em28xx_extension_devlist_lock);
1881 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1882 list_del(&ops->next);
1883 mutex_unlock(&em28xx_extension_devlist_lock);
1885 EXPORT_SYMBOL(em28xx_unregister_extension);
1887 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1888 const struct video_device *template,
1889 const int type,
1890 const char *type_name)
1892 struct video_device *vfd;
1894 vfd = video_device_alloc();
1895 if (NULL == vfd)
1896 return NULL;
1897 *vfd = *template;
1898 vfd->minor = -1;
1899 vfd->dev = &dev->udev->dev;
1900 vfd->release = video_device_release;
1901 vfd->type = type;
1902 vfd->debug = video_debug;
1904 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1905 dev->name, type_name);
1907 return vfd;
1912 * em28xx_init_dev()
1913 * allocates and inits the device structs, registers i2c bus and v4l device
1915 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1916 int minor)
1918 struct em28xx_ops *ops = NULL;
1919 struct em28xx *dev = *devhandle;
1920 int retval = -ENOMEM;
1921 int errCode;
1922 unsigned int maxh, maxw;
1924 dev->udev = udev;
1925 mutex_init(&dev->lock);
1926 spin_lock_init(&dev->slock);
1927 init_waitqueue_head(&dev->open);
1928 init_waitqueue_head(&dev->wait_frame);
1929 init_waitqueue_head(&dev->wait_stream);
1931 dev->em28xx_write_regs = em28xx_write_regs;
1932 dev->em28xx_read_reg = em28xx_read_reg;
1933 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1934 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1935 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1936 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1938 em28xx_pre_card_setup(dev);
1940 errCode = em28xx_config(dev);
1941 if (errCode) {
1942 em28xx_errdev("error configuring device\n");
1943 em28xx_devused &= ~(1<<dev->devno);
1944 kfree(dev);
1945 return -ENOMEM;
1948 /* register i2c bus */
1949 em28xx_i2c_register(dev);
1951 /* Do board specific init and eeprom reading */
1952 em28xx_card_setup(dev);
1954 /* Configure audio */
1955 em28xx_audio_analog_set(dev);
1957 /* configure the device */
1958 em28xx_config_i2c(dev);
1960 /* set default norm */
1961 dev->norm = em28xx_video_template.current_norm;
1963 maxw = norm_maxw(dev);
1964 maxh = norm_maxh(dev);
1966 /* set default image size */
1967 dev->width = maxw;
1968 dev->height = maxh;
1969 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1970 dev->hscale = 0;
1971 dev->vscale = 0;
1972 dev->ctl_input = 2;
1974 errCode = em28xx_config(dev);
1976 list_add_tail(&dev->devlist, &em28xx_devlist);
1978 /* allocate and fill video video_device struct */
1979 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1980 VID_TYPE_CAPTURE, "video");
1981 if (NULL == dev->vdev) {
1982 em28xx_errdev("cannot allocate video_device.\n");
1983 goto fail_unreg;
1985 if (dev->tuner_type != TUNER_ABSENT)
1986 dev->vdev->type |= VID_TYPE_TUNER;
1988 /* register v4l2 video video_device */
1989 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1990 video_nr[dev->devno]);
1991 if (retval) {
1992 em28xx_errdev("unable to register video device (error=%i).\n",
1993 retval);
1994 goto fail_unreg;
1997 /* Allocate and fill vbi video_device struct */
1998 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1999 VFL_TYPE_VBI, "vbi");
2000 /* register v4l2 vbi video_device */
2001 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2002 vbi_nr[dev->devno]) < 0) {
2003 em28xx_errdev("unable to register vbi device\n");
2004 retval = -ENODEV;
2005 goto fail_unreg;
2008 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2009 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2010 VFL_TYPE_RADIO, "radio");
2011 if (NULL == dev->radio_dev) {
2012 em28xx_errdev("cannot allocate video_device.\n");
2013 goto fail_unreg;
2015 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2016 radio_nr[dev->devno]);
2017 if (retval < 0) {
2018 em28xx_errdev("can't register radio device\n");
2019 goto fail_unreg;
2021 em28xx_info("Registered radio device as /dev/radio%d\n",
2022 dev->radio_dev->minor & 0x1f);
2025 /* init video dma queues */
2026 INIT_LIST_HEAD(&dev->vidq.active);
2027 INIT_LIST_HEAD(&dev->vidq.queued);
2030 if (dev->has_msp34xx) {
2031 /* Send a reset to other chips via gpio */
2032 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2033 msleep(3);
2034 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2035 msleep(3);
2038 video_mux(dev, 0);
2040 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2041 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2042 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2044 mutex_lock(&em28xx_extension_devlist_lock);
2045 if (!list_empty(&em28xx_extension_devlist)) {
2046 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2047 if (ops->id)
2048 ops->init(dev);
2051 mutex_unlock(&em28xx_extension_devlist_lock);
2053 return 0;
2055 fail_unreg:
2056 em28xx_release_resources(dev);
2057 mutex_unlock(&dev->lock);
2058 kfree(dev);
2059 return retval;
2062 #if defined(CONFIG_MODULES) && defined(MODULE)
2063 static void request_module_async(struct work_struct *work)
2065 struct em28xx *dev = container_of(work,
2066 struct em28xx, request_module_wk);
2068 if (dev->has_audio_class)
2069 request_module("snd-usb-audio");
2070 else
2071 request_module("em28xx-alsa");
2073 if (dev->has_dvb)
2074 request_module("em28xx-dvb");
2077 static void request_modules(struct em28xx *dev)
2079 INIT_WORK(&dev->request_module_wk, request_module_async);
2080 schedule_work(&dev->request_module_wk);
2082 #else
2083 #define request_modules(dev)
2084 #endif /* CONFIG_MODULES */
2087 * em28xx_usb_probe()
2088 * checks for supported devices
2090 static int em28xx_usb_probe(struct usb_interface *interface,
2091 const struct usb_device_id *id)
2093 const struct usb_endpoint_descriptor *endpoint;
2094 struct usb_device *udev;
2095 struct usb_interface *uif;
2096 struct em28xx *dev = NULL;
2097 int retval = -ENODEV;
2098 int i, nr, ifnum;
2100 udev = usb_get_dev(interface_to_usbdev(interface));
2101 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2103 /* Check to see next free device and mark as used */
2104 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2105 em28xx_devused |= 1<<nr;
2107 /* Don't register audio interfaces */
2108 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2109 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2110 udev->descriptor.idVendor,
2111 udev->descriptor.idProduct,
2112 ifnum,
2113 interface->altsetting[0].desc.bInterfaceClass);
2115 em28xx_devused &= ~(1<<nr);
2116 return -ENODEV;
2119 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2120 udev->descriptor.idVendor,
2121 udev->descriptor.idProduct,
2122 ifnum,
2123 interface->altsetting[0].desc.bInterfaceClass);
2125 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2127 /* check if the device has the iso in endpoint at the correct place */
2128 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2129 USB_ENDPOINT_XFER_ISOC) {
2130 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2131 em28xx_devused &= ~(1<<nr);
2132 return -ENODEV;
2134 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2135 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2136 em28xx_devused &= ~(1<<nr);
2137 return -ENODEV;
2140 if (nr >= EM28XX_MAXBOARDS) {
2141 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2142 EM28XX_MAXBOARDS);
2143 em28xx_devused &= ~(1<<nr);
2144 return -ENOMEM;
2147 /* allocate memory for our device state and initialize it */
2148 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2149 if (dev == NULL) {
2150 em28xx_err(DRIVER_NAME ": out of memory!\n");
2151 em28xx_devused &= ~(1<<nr);
2152 return -ENOMEM;
2155 snprintf(dev->name, 29, "em28xx #%d", nr);
2156 dev->devno = nr;
2157 dev->model = id->driver_info;
2158 dev->alt = -1;
2160 /* Checks if audio is provided by some interface */
2161 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2162 uif = udev->config->interface[i];
2163 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2164 dev->has_audio_class = 1;
2165 break;
2169 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2170 dev->has_audio_class ? "Has" : "Doesn't have");
2172 /* compute alternate max packet sizes */
2173 uif = udev->actconfig->interface[0];
2175 dev->num_alt = uif->num_altsetting;
2176 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2177 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2178 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2180 if (dev->alt_max_pkt_size == NULL) {
2181 em28xx_errdev("out of memory!\n");
2182 em28xx_devused &= ~(1<<nr);
2183 kfree(dev);
2184 return -ENOMEM;
2187 for (i = 0; i < dev->num_alt ; i++) {
2188 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2189 wMaxPacketSize);
2190 dev->alt_max_pkt_size[i] =
2191 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2192 em28xx_info("Alternate setting %i, max size= %i\n", i,
2193 dev->alt_max_pkt_size[i]);
2196 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2197 dev->model = card[nr];
2199 /* allocate device struct */
2200 retval = em28xx_init_dev(&dev, udev, nr);
2201 if (retval)
2202 return retval;
2204 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2206 /* save our data pointer in this interface device */
2207 usb_set_intfdata(interface, dev);
2209 request_modules(dev);
2211 return 0;
2215 * em28xx_usb_disconnect()
2216 * called when the device gets diconencted
2217 * video device will be unregistered on v4l2_close in case it is still open
2219 static void em28xx_usb_disconnect(struct usb_interface *interface)
2221 struct em28xx *dev;
2222 struct em28xx_ops *ops = NULL;
2224 dev = usb_get_intfdata(interface);
2225 usb_set_intfdata(interface, NULL);
2227 if (!dev)
2228 return;
2230 em28xx_info("disconnecting %s\n", dev->vdev->name);
2232 /* wait until all current v4l2 io is finished then deallocate
2233 resources */
2234 mutex_lock(&dev->lock);
2236 wake_up_interruptible_all(&dev->open);
2238 if (dev->users) {
2239 em28xx_warn
2240 ("device /dev/video%d is open! Deregistration and memory "
2241 "deallocation are deferred on close.\n",
2242 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2244 dev->state |= DEV_MISCONFIGURED;
2245 em28xx_uninit_isoc(dev);
2246 dev->state |= DEV_DISCONNECTED;
2247 wake_up_interruptible(&dev->wait_frame);
2248 wake_up_interruptible(&dev->wait_stream);
2249 } else {
2250 dev->state |= DEV_DISCONNECTED;
2251 em28xx_release_resources(dev);
2253 mutex_unlock(&dev->lock);
2255 mutex_lock(&em28xx_extension_devlist_lock);
2256 if (!list_empty(&em28xx_extension_devlist)) {
2257 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2258 ops->fini(dev);
2261 mutex_unlock(&em28xx_extension_devlist_lock);
2263 if (!dev->users) {
2264 kfree(dev->alt_max_pkt_size);
2265 kfree(dev);
2269 static struct usb_driver em28xx_usb_driver = {
2270 .name = "em28xx",
2271 .probe = em28xx_usb_probe,
2272 .disconnect = em28xx_usb_disconnect,
2273 .id_table = em28xx_id_table,
2276 static int __init em28xx_module_init(void)
2278 int result;
2280 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2281 (EM28XX_VERSION_CODE >> 16) & 0xff,
2282 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2283 #ifdef SNAPSHOT
2284 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2285 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2286 #endif
2288 /* register this driver with the USB subsystem */
2289 result = usb_register(&em28xx_usb_driver);
2290 if (result)
2291 em28xx_err(DRIVER_NAME
2292 " usb_register failed. Error number %d.\n", result);
2294 return result;
2297 static void __exit em28xx_module_exit(void)
2299 /* deregister this driver with the USB subsystem */
2300 usb_deregister(&em28xx_usb_driver);
2303 module_init(em28xx_module_init);
2304 module_exit(em28xx_module_exit);