]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (7567): em28xx: Some cleanups
[karo-tx-linux.git] / drivers / media / video / em28xx / em28xx-video.c
1 /*
2    em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@infradead.org>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9         Some parts based on SN9C10x PC Camera Controllers GPL driver made
10                 by Luca Risolia <luca.risolia@studio.unibo.it>
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/bitmap.h>
32 #include <linux/usb.h>
33 #include <linux/i2c.h>
34 #include <linux/version.h>
35 #include <linux/mm.h>
36 #include <linux/mutex.h>
37
38 #include "em28xx.h"
39 #include <media/v4l2-common.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.h>
42
43 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44                       "Markus Rechberger <mrechberger@gmail.com>, " \
45                       "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
46                       "Sascha Sommer <saschasommer@freenet.de>"
47
48 #define DRIVER_NAME         "em28xx"
49 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
50 #define EM28XX_VERSION_CODE  KERNEL_VERSION(0, 1, 0)
51
52 #define em28xx_videodbg(fmt, arg...) do {\
53         if (video_debug) \
54                 printk(KERN_INFO "%s %s :"fmt, \
55                          dev->name, __func__ , ##arg); } while (0)
56
57 static unsigned int isoc_debug;
58 module_param(isoc_debug, int, 0644);
59 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
60
61 #define em28xx_isocdbg(fmt, arg...) do {\
62         if (isoc_debug) \
63                 printk(KERN_INFO "%s %s :"fmt, \
64                          dev->name, __func__ , ##arg); } while (0)
65
66 /* Limits minimum and default number of buffers */
67 #define EM28XX_MIN_BUF 4
68 #define EM28XX_DEF_BUF 8
69
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
73
74 static LIST_HEAD(em28xx_devlist);
75
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 };
80
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");
89
90 static unsigned int video_debug;
91 module_param(video_debug,int,0644);
92 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
93
94 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95 static unsigned long em28xx_devused;
96
97 /* supported controls */
98 /* Common to all boards */
99 static struct v4l2_queryctrl em28xx_qctrl[] = {
100         {
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,
118         }
119 };
120
121 static struct usb_driver em28xx_usb_driver;
122
123 /* ------------------------------------------------------------------
124         DMA and thread functions
125    ------------------------------------------------------------------*/
126
127 /*
128  * Announces that a buffer were filled and request the next
129  */
130 static inline void buffer_filled(struct em28xx *dev,
131                                   struct em28xx_dmaqueue *dma_q,
132                                   struct em28xx_buffer *buf)
133 {
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);
139
140         dev->isoc_ctl.buf = NULL;
141
142         list_del(&buf->vb.queue);
143         wake_up(&buf->vb.done);
144 }
145
146 /*
147  * Identify the buffer header type and properly handles
148  */
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)
154 {
155         void *fieldstart, *startwrite, *startread;
156         int  linesdone, currlinedone, offset, lencopy, remain;
157         int bytesperline = dev->width << 1;
158
159         if (dma_q->pos + len > buf->vb.size)
160                 len = buf->vb.size - dma_q->pos;
161
162         if (p[0] != 0x88 && p[0] != 0x22) {
163                 em28xx_isocdbg("frame is not complete\n");
164                 len += 4;
165         } else
166                 p += 4;
167
168         startread = p;
169         remain = len;
170
171         /* Interlaces frame */
172         if (buf->top_field)
173                 fieldstart = outp;
174         else
175                 fieldstart = outp + bytesperline;
176
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;
183
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;
189         }
190         if (lencopy <= 0)
191                 return;
192         memcpy(startwrite, startread, lencopy);
193
194         remain -= lencopy;
195
196         while (remain > 0) {
197                 startwrite += lencopy + bytesperline;
198                 startread += lencopy;
199                 if (bytesperline > remain)
200                         lencopy = remain;
201                 else
202                         lencopy = bytesperline;
203
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;
210                 }
211                 if (lencopy <= 0)
212                         break;
213
214                 memcpy(startwrite, startread, lencopy);
215
216                 remain -= lencopy;
217         }
218
219         dma_q->pos += len;
220 }
221
222 static inline void print_err_status(struct em28xx *dev,
223                                      int packet, int status)
224 {
225         char *errmsg = "Unknown";
226
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;
252         }
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);
258         }
259 }
260
261 /*
262  * video-buf generic routine to get the next available buffer
263  */
264 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
265                                           struct em28xx_buffer **buf)
266 {
267         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
268         char *outp;
269
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;
275         }
276
277         /* Get the next buffer */
278         *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
279
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);
283
284         dev->isoc_ctl.buf = *buf;
285
286         return;
287 }
288
289 /*
290  * Controls the isoc copy of each urb packet
291  */
292 static inline int em28xx_isoc_copy(struct urb *urb)
293 {
294         struct em28xx_buffer    *buf;
295         struct em28xx_dmaqueue  *dma_q = urb->context;
296         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
297         unsigned char *outp = NULL;
298         int i, len = 0, rc = 1;
299         unsigned char *p;
300
301         if (!dev)
302                 return 0;
303
304         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
305                 return 0;
306
307         if (urb->status < 0) {
308                 print_err_status(dev, -1, urb->status);
309                 if (urb->status == -ENOENT)
310                         return 0;
311         }
312
313         buf = dev->isoc_ctl.buf;
314         if (buf != NULL)
315                 outp = videobuf_to_vmalloc(&buf->vb);
316
317         for (i = 0; i < urb->number_of_packets; i++) {
318                 int status = urb->iso_frame_desc[i].status;
319
320                 if (status < 0) {
321                         print_err_status(dev, i, status);
322                         if (urb->iso_frame_desc[i].status != -EPROTO)
323                                 continue;
324                 }
325
326                 len = urb->iso_frame_desc[i].actual_length - 4;
327
328                 if (urb->iso_frame_desc[i].actual_length <= 0) {
329                         /* em28xx_isocdbg("packet %d is empty",i); - spammy */
330                         continue;
331                 }
332                 if (urb->iso_frame_desc[i].actual_length >
333                                                 dev->max_pkt_size) {
334                         em28xx_isocdbg("packet bigger than packet size");
335                         continue;
336                 }
337
338                 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
339
340                 /* FIXME: incomplete buffer checks where removed to make
341                    logic simpler. Impacts of those changes should be evaluated
342                  */
343                 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
344                         em28xx_isocdbg("VBI HEADER!!!\n");
345                         /* FIXME: Should add vbi copy */
346                         continue;
347                 }
348                 if (p[0] == 0x22 && p[1] == 0x5a) {
349                         em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
350                                        len, (p[2] & 1)? "odd" : "even");
351
352                         if (!(p[2] & 1)) {
353                                 if (buf != NULL)
354                                         buffer_filled(dev, dma_q, buf);
355                                 get_next_buf(dma_q, &buf);
356                                 if (buf == NULL)
357                                         outp = NULL;
358                                 else
359                                         outp = videobuf_to_vmalloc(&buf->vb);
360                         }
361
362                         if (buf != NULL) {
363                                 if (p[2] & 1)
364                                         buf->top_field = 0;
365                                 else
366                                         buf->top_field = 1;
367                         }
368
369                         dma_q->pos = 0;
370                 }
371                 if (buf != NULL)
372                         em28xx_copy_video(dev, dma_q, buf, p, outp, len);
373         }
374         return rc;
375 }
376
377 /* ------------------------------------------------------------------
378         URB control
379    ------------------------------------------------------------------*/
380
381 /*
382  * IRQ callback, called by URB callback
383  */
384 static void em28xx_irq_callback(struct urb *urb)
385 {
386         struct em28xx_dmaqueue  *dma_q = urb->context;
387         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
388         int rc, i;
389
390         /* Copy data from URB */
391         spin_lock(&dev->slock);
392         rc = em28xx_isoc_copy(urb);
393         spin_unlock(&dev->slock);
394
395         /* Reset urb buffers */
396         for (i = 0; i < urb->number_of_packets; i++) {
397                 urb->iso_frame_desc[i].status = 0;
398                 urb->iso_frame_desc[i].actual_length = 0;
399         }
400         urb->status = 0;
401
402         urb->status = usb_submit_urb(urb, GFP_ATOMIC);
403         if (urb->status) {
404                 em28xx_err("urb resubmit failed (error=%i)\n",
405                         urb->status);
406         }
407 }
408
409 /*
410  * Stop and Deallocate URBs
411  */
412 static void em28xx_uninit_isoc(struct em28xx *dev)
413 {
414         struct urb *urb;
415         int i;
416
417         em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
418
419         dev->isoc_ctl.nfields = -1;
420         for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
421                 urb = dev->isoc_ctl.urb[i];
422                 if (urb) {
423                         usb_kill_urb(urb);
424                         usb_unlink_urb(urb);
425                         if (dev->isoc_ctl.transfer_buffer[i]) {
426                                 usb_buffer_free(dev->udev,
427                                                 urb->transfer_buffer_length,
428                                                 dev->isoc_ctl.transfer_buffer[i],
429                                                 urb->transfer_dma);
430                         }
431                         usb_free_urb(urb);
432                         dev->isoc_ctl.urb[i] = NULL;
433                 }
434                 dev->isoc_ctl.transfer_buffer[i] = NULL;
435         }
436
437         kfree(dev->isoc_ctl.urb);
438         kfree(dev->isoc_ctl.transfer_buffer);
439         dev->isoc_ctl.urb = NULL;
440         dev->isoc_ctl.transfer_buffer = NULL;
441
442         dev->isoc_ctl.num_bufs = 0;
443
444         em28xx_capture_start(dev, 0);
445 }
446
447 /*
448  * Allocate URBs and start IRQ
449  */
450 static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
451                                int num_bufs)
452 {
453         struct em28xx_dmaqueue *dma_q = &dev->vidq;
454         int i;
455         int sb_size, pipe;
456         struct urb *urb;
457         int j, k;
458
459         em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
460
461         /* De-allocates all pending stuff */
462         em28xx_uninit_isoc(dev);
463
464         dev->isoc_ctl.num_bufs = num_bufs;
465
466         dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
467         if (!dev->isoc_ctl.urb) {
468                 em28xx_errdev("cannot alloc memory for usb buffers\n");
469                 return -ENOMEM;
470         }
471
472         dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
473                                               GFP_KERNEL);
474         if (!dev->isoc_ctl.urb) {
475                 em28xx_errdev("cannot allocate memory for usbtransfer\n");
476                 kfree(dev->isoc_ctl.urb);
477                 return -ENOMEM;
478         }
479
480         dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
481         dev->isoc_ctl.buf = NULL;
482
483         sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
484
485         /* allocate urbs and transfer buffers */
486         for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
487                 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
488                 if (!urb) {
489                         em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
490                         em28xx_uninit_isoc(dev);
491                         return -ENOMEM;
492                 }
493                 dev->isoc_ctl.urb[i] = urb;
494
495                 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
496                         sb_size, GFP_KERNEL, &urb->transfer_dma);
497                 if (!dev->isoc_ctl.transfer_buffer[i]) {
498                         em28xx_err("unable to allocate %i bytes for transfer"
499                                         " buffer %i%s\n",
500                                         sb_size, i,
501                                         in_interrupt()?" while in int":"");
502                         em28xx_uninit_isoc(dev);
503                         return -ENOMEM;
504                 }
505                 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
506
507                 /* FIXME: this is a hack - should be
508                         'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
509                         should also be using 'desc.bInterval'
510                  */
511                 pipe = usb_rcvisocpipe(dev->udev, 0x82);
512                 usb_fill_int_urb(urb, dev->udev, pipe,
513                                  dev->isoc_ctl.transfer_buffer[i], sb_size,
514                                  em28xx_irq_callback, dma_q, 1);
515
516                 urb->number_of_packets = max_packets;
517                 urb->transfer_flags = URB_ISO_ASAP;
518
519                 k = 0;
520                 for (j = 0; j < max_packets; j++) {
521                         urb->iso_frame_desc[j].offset = k;
522                         urb->iso_frame_desc[j].length =
523                                                 dev->isoc_ctl.max_pkt_size;
524                         k += dev->isoc_ctl.max_pkt_size;
525                 }
526         }
527
528         return 0;
529 }
530
531 static int em28xx_start_thread(struct em28xx_dmaqueue  *dma_q)
532 {
533         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
534         int i, rc = 0;
535
536         em28xx_videodbg("Called em28xx_start_thread\n");
537
538         init_waitqueue_head(&dma_q->wq);
539
540         em28xx_capture_start(dev, 1);
541
542         /* submit urbs and enables IRQ */
543         for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
544                 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
545                 if (rc) {
546                         em28xx_err("submit of urb %i failed (error=%i)\n", i,
547                                    rc);
548                         em28xx_uninit_isoc(dev);
549                         return rc;
550                 }
551         }
552
553         if (rc < 0)
554                 return rc;
555
556         return 0;
557 }
558
559 /* ------------------------------------------------------------------
560         Videobuf operations
561    ------------------------------------------------------------------*/
562
563 static int
564 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
565 {
566         struct em28xx_fh *fh = vq->priv_data;
567
568         *size = 16 * fh->dev->width * fh->dev->height >> 3;
569         if (0 == *count)
570                 *count = EM28XX_DEF_BUF;
571
572         if (*count < EM28XX_MIN_BUF)
573                 *count = EM28XX_MIN_BUF;
574
575         return 0;
576 }
577
578 /* This is called *without* dev->slock held; please keep it that way */
579 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
580 {
581         struct em28xx_fh     *fh  = vq->priv_data;
582         struct em28xx        *dev = fh->dev;
583         unsigned long flags = 0;
584         if (in_interrupt())
585                 BUG();
586
587         /* We used to wait for the buffer to finish here, but this didn't work
588            because, as we were keeping the state as VIDEOBUF_QUEUED,
589            videobuf_queue_cancel marked it as finished for us.
590            (Also, it could wedge forever if the hardware was misconfigured.)
591
592            This should be safe; by the time we get here, the buffer isn't
593            queued anymore. If we ever start marking the buffers as
594            VIDEOBUF_ACTIVE, it won't be, though.
595         */
596         spin_lock_irqsave(&dev->slock, flags);
597         if (dev->isoc_ctl.buf == buf)
598                 dev->isoc_ctl.buf = NULL;
599         spin_unlock_irqrestore(&dev->slock, flags);
600
601         videobuf_vmalloc_free(&buf->vb);
602         buf->vb.state = VIDEOBUF_NEEDS_INIT;
603 }
604
605 static int
606 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
607                                                 enum v4l2_field field)
608 {
609         struct em28xx_fh     *fh  = vq->priv_data;
610         struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
611         struct em28xx        *dev = fh->dev;
612         struct em28xx_dmaqueue *vidq = &dev->vidq;
613         int                  rc = 0, urb_init = 0;
614
615         /* FIXME: It assumes depth = 16 */
616         /* The only currently supported format is 16 bits/pixel */
617         buf->vb.size = 16 * dev->width * dev->height >> 3;
618
619         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
620                 return -EINVAL;
621
622         buf->vb.width  = dev->width;
623         buf->vb.height = dev->height;
624         buf->vb.field  = field;
625
626         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
627                 rc = videobuf_iolock(vq, &buf->vb, NULL);
628                 if (rc < 0)
629                         goto fail;
630         }
631
632         if (!dev->isoc_ctl.num_bufs)
633                 urb_init = 1;
634
635         if (urb_init) {
636                 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
637                                          EM28XX_NUM_BUFS);
638                 if (rc < 0)
639                         goto fail;
640
641                 rc = em28xx_start_thread(vidq);
642                 if (rc < 0)
643                         goto fail;
644         }
645
646         buf->vb.state = VIDEOBUF_PREPARED;
647         return 0;
648
649 fail:
650         free_buffer(vq, buf);
651         return rc;
652 }
653
654 static void
655 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
656 {
657         struct em28xx_buffer    *buf     = container_of(vb, struct em28xx_buffer, vb);
658         struct em28xx_fh        *fh      = vq->priv_data;
659         struct em28xx           *dev     = fh->dev;
660         struct em28xx_dmaqueue  *vidq    = &dev->vidq;
661
662         buf->vb.state = VIDEOBUF_QUEUED;
663         list_add_tail(&buf->vb.queue, &vidq->active);
664
665 }
666
667 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
668 {
669         struct em28xx_buffer   *buf  = container_of(vb, struct em28xx_buffer, vb);
670         struct em28xx_fh       *fh   = vq->priv_data;
671         struct em28xx          *dev  = (struct em28xx *)fh->dev;
672
673         em28xx_isocdbg("em28xx: called buffer_release\n");
674
675         free_buffer(vq, buf);
676 }
677
678 static struct videobuf_queue_ops em28xx_video_qops = {
679         .buf_setup      = buffer_setup,
680         .buf_prepare    = buffer_prepare,
681         .buf_queue      = buffer_queue,
682         .buf_release    = buffer_release,
683 };
684
685 /*********************  v4l2 interface  ******************************************/
686
687 /*
688  * em28xx_config()
689  * inits registers with sane defaults
690  */
691 static int em28xx_config(struct em28xx *dev)
692 {
693
694         /* Sets I2C speed to 100 KHz */
695         if (!dev->is_em2800)
696                 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
697
698         /* enable vbi capturing */
699
700 /*      em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
701 /*      em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
702         em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
703
704         dev->mute = 1;          /* maybe not the right place... */
705         dev->volume = 0x1f;
706
707         em28xx_outfmt_set_yuv422(dev);
708         em28xx_colorlevels_set_default(dev);
709         em28xx_compression_disable(dev);
710
711         return 0;
712 }
713
714 /*
715  * em28xx_config_i2c()
716  * configure i2c attached devices
717  */
718 static void em28xx_config_i2c(struct em28xx *dev)
719 {
720         struct v4l2_routing route;
721
722         route.input = INPUT(dev->ctl_input)->vmux;
723         route.output = 0;
724         em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
725         em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
726         em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
727 }
728
729 static void video_mux(struct em28xx *dev, int index)
730 {
731         struct v4l2_routing route;
732
733         route.input = INPUT(index)->vmux;
734         route.output = 0;
735         dev->ctl_input = index;
736         dev->ctl_ainput = INPUT(index)->amux;
737
738         em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
739
740         if (dev->has_msp34xx) {
741                 if (dev->i2s_speed)
742                         em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
743                 route.input = dev->ctl_ainput;
744                 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
745                 /* Note: this is msp3400 specific */
746                 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
747         }
748
749         em28xx_audio_analog_set(dev);
750 }
751
752 /* Usage lock check functions */
753 static int res_get(struct em28xx_fh *fh)
754 {
755         struct em28xx    *dev = fh->dev;
756         int              rc   = 0;
757
758         /* This instance already has stream_on */
759         if (fh->stream_on)
760                 return rc;
761
762         if (dev->stream_on)
763                 return -EINVAL;
764
765         mutex_lock(&dev->lock);
766         dev->stream_on = 1;
767         fh->stream_on  = 1;
768         mutex_unlock(&dev->lock);
769         return rc;
770 }
771
772 static int res_check(struct em28xx_fh *fh)
773 {
774         return (fh->stream_on);
775 }
776
777 static void res_free(struct em28xx_fh *fh)
778 {
779         struct em28xx    *dev = fh->dev;
780
781         mutex_lock(&dev->lock);
782         fh->stream_on = 0;
783         dev->stream_on = 0;
784         mutex_unlock(&dev->lock);
785 }
786
787 /*
788  * em28xx_get_ctrl()
789  * return the current saturation, brightness or contrast, mute state
790  */
791 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
792 {
793         switch (ctrl->id) {
794         case V4L2_CID_AUDIO_MUTE:
795                 ctrl->value = dev->mute;
796                 return 0;
797         case V4L2_CID_AUDIO_VOLUME:
798                 ctrl->value = dev->volume;
799                 return 0;
800         default:
801                 return -EINVAL;
802         }
803 }
804
805 /*
806  * em28xx_set_ctrl()
807  * mute or set new saturation, brightness or contrast
808  */
809 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
810 {
811         switch (ctrl->id) {
812         case V4L2_CID_AUDIO_MUTE:
813                 if (ctrl->value != dev->mute) {
814                         dev->mute = ctrl->value;
815                         return em28xx_audio_analog_set(dev);
816                 }
817                 return 0;
818         case V4L2_CID_AUDIO_VOLUME:
819                 dev->volume = ctrl->value;
820                 return em28xx_audio_analog_set(dev);
821         default:
822                 return -EINVAL;
823         }
824 }
825
826 static int check_dev(struct em28xx *dev)
827 {
828         if (dev->state & DEV_DISCONNECTED) {
829                 em28xx_errdev("v4l2 ioctl: device not present\n");
830                 return -ENODEV;
831         }
832
833         if (dev->state & DEV_MISCONFIGURED) {
834                 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
835                               "close and open it again\n");
836                 return -EIO;
837         }
838         return 0;
839 }
840
841 static void get_scale(struct em28xx *dev,
842                         unsigned int width, unsigned int height,
843                         unsigned int *hscale, unsigned int *vscale)
844 {
845         unsigned int          maxw   = norm_maxw(dev);
846         unsigned int          maxh   = norm_maxh(dev);
847
848         *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
849         if (*hscale >= 0x4000)
850                 *hscale = 0x3fff;
851
852         *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
853         if (*vscale >= 0x4000)
854                 *vscale = 0x3fff;
855 }
856
857 /* ------------------------------------------------------------------
858         IOCTL vidioc handling
859    ------------------------------------------------------------------*/
860
861 static int vidioc_g_fmt_cap(struct file *file, void *priv,
862                                         struct v4l2_format *f)
863 {
864         struct em28xx_fh      *fh  = priv;
865         struct em28xx         *dev = fh->dev;
866
867         mutex_lock(&dev->lock);
868
869         f->fmt.pix.width = dev->width;
870         f->fmt.pix.height = dev->height;
871         f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
872         f->fmt.pix.bytesperline = dev->width * 2;
873         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline  * dev->height;
874         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
875
876         /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
877         f->fmt.pix.field = dev->interlaced ?
878                            V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
879
880         mutex_unlock(&dev->lock);
881         return 0;
882 }
883
884 static int vidioc_try_fmt_cap(struct file *file, void *priv,
885                         struct v4l2_format *f)
886 {
887         struct em28xx_fh      *fh    = priv;
888         struct em28xx         *dev   = fh->dev;
889         int                   width  = f->fmt.pix.width;
890         int                   height = f->fmt.pix.height;
891         unsigned int          maxw   = norm_maxw(dev);
892         unsigned int          maxh   = norm_maxh(dev);
893         unsigned int          hscale, vscale;
894
895         /* width must even because of the YUYV format
896            height must be even because of interlacing */
897         height &= 0xfffe;
898         width &= 0xfffe;
899
900         if (height < 32)
901                 height = 32;
902         if (height > maxh)
903                 height = maxh;
904         if (width < 48)
905                 width = 48;
906         if (width > maxw)
907                 width = maxw;
908
909         mutex_lock(&dev->lock);
910
911         if (dev->is_em2800) {
912                 /* the em2800 can only scale down to 50% */
913                 if (height % (maxh / 2))
914                         height = maxh;
915                 if (width % (maxw / 2))
916                         width = maxw;
917                 /* according to empiatech support */
918                 /* the MaxPacketSize is to small to support */
919                 /* framesizes larger than 640x480 @ 30 fps */
920                 /* or 640x576 @ 25 fps. As this would cut */
921                 /* of a part of the image we prefer */
922                 /* 360x576 or 360x480 for now */
923                 if (width == maxw && height == maxh)
924                         width /= 2;
925         }
926
927         get_scale(dev, width, height, &hscale, &vscale);
928
929         width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
930         height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
931
932         f->fmt.pix.width = width;
933         f->fmt.pix.height = height;
934         f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
935         f->fmt.pix.bytesperline = width * 2;
936         f->fmt.pix.sizeimage = width * 2 * height;
937         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
938         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
939
940         mutex_unlock(&dev->lock);
941         return 0;
942 }
943
944 static int vidioc_s_fmt_cap(struct file *file, void *priv,
945                         struct v4l2_format *f)
946 {
947         struct em28xx_fh      *fh  = priv;
948         struct em28xx         *dev = fh->dev;
949         int                   rc;
950
951         rc = check_dev(dev);
952         if (rc < 0)
953                 return rc;
954
955         vidioc_try_fmt_cap(file, priv, f);
956
957         mutex_lock(&dev->lock);
958
959         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
960                 em28xx_errdev("%s queue busy\n", __func__);
961                 rc = -EBUSY;
962                 goto out;
963         }
964
965         if (dev->stream_on && !fh->stream_on) {
966                 em28xx_errdev("%s device in use by another fh\n", __func__);
967                 rc = -EBUSY;
968                 goto out;
969         }
970
971         /* set new image size */
972         dev->width = f->fmt.pix.width;
973         dev->height = f->fmt.pix.height;
974         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
975
976         em28xx_set_alternate(dev);
977         em28xx_resolution_set(dev);
978
979         rc = 0;
980
981 out:
982         mutex_unlock(&dev->lock);
983         return rc;
984 }
985
986 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
987 {
988         struct em28xx_fh   *fh  = priv;
989         struct em28xx      *dev = fh->dev;
990         struct v4l2_format f;
991         int                rc;
992
993         rc = check_dev(dev);
994         if (rc < 0)
995                 return rc;
996
997         mutex_lock(&dev->lock);
998         dev->norm = *norm;
999         mutex_unlock(&dev->lock);
1000
1001         /* Adjusts width/height, if needed */
1002         f.fmt.pix.width = dev->width;
1003         f.fmt.pix.height = dev->height;
1004         vidioc_try_fmt_cap(file, priv, &f);
1005
1006         mutex_lock(&dev->lock);
1007
1008         /* set new image size */
1009         dev->width = f.fmt.pix.width;
1010         dev->height = f.fmt.pix.height;
1011         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1012
1013         em28xx_resolution_set(dev);
1014         em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1015
1016         mutex_unlock(&dev->lock);
1017         return 0;
1018 }
1019
1020 static const char *iname[] = {
1021         [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1022         [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1023         [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1024         [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1025         [EM28XX_VMUX_SVIDEO]     = "S-Video",
1026         [EM28XX_VMUX_TELEVISION] = "Television",
1027         [EM28XX_VMUX_CABLE]      = "Cable TV",
1028         [EM28XX_VMUX_DVB]        = "DVB",
1029         [EM28XX_VMUX_DEBUG]      = "for debug only",
1030 };
1031
1032 static int vidioc_enum_input(struct file *file, void *priv,
1033                                 struct v4l2_input *i)
1034 {
1035         struct em28xx_fh   *fh  = priv;
1036         struct em28xx      *dev = fh->dev;
1037         unsigned int       n;
1038
1039         n = i->index;
1040         if (n >= MAX_EM28XX_INPUT)
1041                 return -EINVAL;
1042         if (0 == INPUT(n)->type)
1043                 return -EINVAL;
1044
1045         i->index = n;
1046         i->type = V4L2_INPUT_TYPE_CAMERA;
1047
1048         strcpy(i->name, iname[INPUT(n)->type]);
1049
1050         if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1051                 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1052                 i->type = V4L2_INPUT_TYPE_TUNER;
1053
1054         i->std = dev->vdev->tvnorms;
1055
1056         return 0;
1057 }
1058
1059 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1060 {
1061         struct em28xx_fh   *fh  = priv;
1062         struct em28xx      *dev = fh->dev;
1063
1064         *i = dev->ctl_input;
1065
1066         return 0;
1067 }
1068
1069 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1070 {
1071         struct em28xx_fh   *fh  = priv;
1072         struct em28xx      *dev = fh->dev;
1073         int                rc;
1074
1075         rc = check_dev(dev);
1076         if (rc < 0)
1077                 return rc;
1078
1079         if (i >= MAX_EM28XX_INPUT)
1080                 return -EINVAL;
1081         if (0 == INPUT(i)->type)
1082                 return -EINVAL;
1083
1084         mutex_lock(&dev->lock);
1085
1086         video_mux(dev, i);
1087
1088         mutex_unlock(&dev->lock);
1089         return 0;
1090 }
1091
1092 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1093 {
1094         struct em28xx_fh   *fh    = priv;
1095         struct em28xx      *dev   = fh->dev;
1096         unsigned int        index = a->index;
1097
1098         if (a->index > 1)
1099                 return -EINVAL;
1100
1101         index = dev->ctl_ainput;
1102
1103         if (index == 0) {
1104                 strcpy(a->name, "Television");
1105         } else {
1106                 strcpy(a->name, "Line In");
1107         }
1108         a->capability = V4L2_AUDCAP_STEREO;
1109         a->index = index;
1110
1111         return 0;
1112 }
1113
1114 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1115 {
1116         struct em28xx_fh   *fh  = priv;
1117         struct em28xx      *dev = fh->dev;
1118
1119         if (a->index != dev->ctl_ainput)
1120                 return -EINVAL;
1121
1122         return 0;
1123 }
1124
1125 static int vidioc_queryctrl(struct file *file, void *priv,
1126                                 struct v4l2_queryctrl *qc)
1127 {
1128         struct em28xx_fh      *fh  = priv;
1129         struct em28xx         *dev = fh->dev;
1130         int                   id  = qc->id;
1131         int                   i;
1132         int                   rc;
1133
1134         rc = check_dev(dev);
1135         if (rc < 0)
1136                 return rc;
1137
1138         memset(qc, 0, sizeof(*qc));
1139
1140         qc->id = id;
1141
1142         if (!dev->has_msp34xx) {
1143                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1144                         if (qc->id && qc->id == em28xx_qctrl[i].id) {
1145                                 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1146                                 return 0;
1147                         }
1148                 }
1149         }
1150         mutex_lock(&dev->lock);
1151         em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1152         mutex_unlock(&dev->lock);
1153
1154         if (qc->type)
1155                 return 0;
1156         else
1157                 return -EINVAL;
1158 }
1159
1160 static int vidioc_g_ctrl(struct file *file, void *priv,
1161                                 struct v4l2_control *ctrl)
1162 {
1163         struct em28xx_fh      *fh  = priv;
1164         struct em28xx         *dev = fh->dev;
1165         int                   rc;
1166
1167         rc = check_dev(dev);
1168         if (rc < 0)
1169                 return rc;
1170         mutex_lock(&dev->lock);
1171
1172         if (!dev->has_msp34xx)
1173                 rc = em28xx_get_ctrl(dev, ctrl);
1174         else
1175                 rc = -EINVAL;
1176
1177         if (rc == -EINVAL) {
1178                 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1179                 rc = 0;
1180         }
1181
1182         mutex_unlock(&dev->lock);
1183         return rc;
1184 }
1185
1186 static int vidioc_s_ctrl(struct file *file, void *priv,
1187                                 struct v4l2_control *ctrl)
1188 {
1189         struct em28xx_fh      *fh  = priv;
1190         struct em28xx         *dev = fh->dev;
1191         u8                    i;
1192         int                   rc;
1193
1194         rc = check_dev(dev);
1195         if (rc < 0)
1196                 return rc;
1197
1198         mutex_lock(&dev->lock);
1199
1200         if (dev->has_msp34xx)
1201                 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1202         else {
1203                 rc = 1;
1204                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1205                         if (ctrl->id == em28xx_qctrl[i].id) {
1206                                 if (ctrl->value < em28xx_qctrl[i].minimum ||
1207                                     ctrl->value > em28xx_qctrl[i].maximum) {
1208                                         rc = -ERANGE;
1209                                         break;
1210                                 }
1211
1212                                 rc = em28xx_set_ctrl(dev, ctrl);
1213                                 break;
1214                         }
1215                 }
1216         }
1217
1218         /* Control not found - try to send it to the attached devices */
1219         if (rc == 1) {
1220                 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1221                 rc = 0;
1222         }
1223
1224         mutex_unlock(&dev->lock);
1225         return rc;
1226 }
1227
1228 static int vidioc_g_tuner(struct file *file, void *priv,
1229                                 struct v4l2_tuner *t)
1230 {
1231         struct em28xx_fh      *fh  = priv;
1232         struct em28xx         *dev = fh->dev;
1233         int                   rc;
1234
1235         rc = check_dev(dev);
1236         if (rc < 0)
1237                 return rc;
1238
1239         if (0 != t->index)
1240                 return -EINVAL;
1241
1242         strcpy(t->name, "Tuner");
1243
1244         mutex_lock(&dev->lock);
1245
1246         em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1247
1248         mutex_unlock(&dev->lock);
1249         return 0;
1250 }
1251
1252 static int vidioc_s_tuner(struct file *file, void *priv,
1253                                 struct v4l2_tuner *t)
1254 {
1255         struct em28xx_fh      *fh  = priv;
1256         struct em28xx         *dev = fh->dev;
1257         int                   rc;
1258
1259         rc = check_dev(dev);
1260         if (rc < 0)
1261                 return rc;
1262
1263         if (0 != t->index)
1264                 return -EINVAL;
1265
1266         mutex_lock(&dev->lock);
1267
1268         em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1269
1270         mutex_unlock(&dev->lock);
1271         return 0;
1272 }
1273
1274 static int vidioc_g_frequency(struct file *file, void *priv,
1275                                 struct v4l2_frequency *f)
1276 {
1277         struct em28xx_fh      *fh  = priv;
1278         struct em28xx         *dev = fh->dev;
1279
1280         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1281         f->frequency = dev->ctl_freq;
1282
1283         return 0;
1284 }
1285
1286 static int vidioc_s_frequency(struct file *file, void *priv,
1287                                 struct v4l2_frequency *f)
1288 {
1289         struct em28xx_fh      *fh  = priv;
1290         struct em28xx         *dev = fh->dev;
1291         int                   rc;
1292
1293         rc = check_dev(dev);
1294         if (rc < 0)
1295                 return rc;
1296
1297         if (0 != f->tuner)
1298                 return -EINVAL;
1299
1300         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1301                 return -EINVAL;
1302         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1303                 return -EINVAL;
1304
1305         mutex_lock(&dev->lock);
1306
1307         dev->ctl_freq = f->frequency;
1308         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1309
1310         mutex_unlock(&dev->lock);
1311         return 0;
1312 }
1313
1314 #ifdef CONFIG_VIDEO_ADV_DEBUG
1315 static int em28xx_reg_len(int reg)
1316 {
1317         switch (reg) {
1318         case AC97LSB_REG:
1319         case HSCALELOW_REG:
1320         case VSCALELOW_REG:
1321                 return 2;
1322         default:
1323                 return 1;
1324         }
1325 }
1326
1327 static int vidioc_g_register(struct file *file, void *priv,
1328                              struct v4l2_register *reg)
1329 {
1330         struct em28xx_fh      *fh  = priv;
1331         struct em28xx         *dev = fh->dev;
1332         int ret;
1333
1334         if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1335                 return -EINVAL;
1336
1337         if (em28xx_reg_len(reg->reg) == 1) {
1338                 ret = em28xx_read_reg(dev, reg->reg);
1339                 if (ret < 0)
1340                         return ret;
1341
1342                 reg->val = ret;
1343         } else {
1344                 u64 val = 0;
1345                 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1346                                                    reg->reg, (char *)&val, 2);
1347                 if (ret < 0)
1348                         return ret;
1349
1350                 reg->val = cpu_to_le64((__u64)val);
1351         }
1352
1353         return 0;
1354 }
1355
1356 static int vidioc_s_register(struct file *file, void *priv,
1357                              struct v4l2_register *reg)
1358 {
1359         struct em28xx_fh      *fh  = priv;
1360         struct em28xx         *dev = fh->dev;
1361         u64 buf;
1362
1363         buf = le64_to_cpu((__u64)reg->val);
1364
1365         return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1366                                  em28xx_reg_len(reg->reg));
1367 }
1368 #endif
1369
1370
1371 static int vidioc_cropcap(struct file *file, void *priv,
1372                                         struct v4l2_cropcap *cc)
1373 {
1374         struct em28xx_fh      *fh  = priv;
1375         struct em28xx         *dev = fh->dev;
1376
1377         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1378                 return -EINVAL;
1379
1380         cc->bounds.left = 0;
1381         cc->bounds.top = 0;
1382         cc->bounds.width = dev->width;
1383         cc->bounds.height = dev->height;
1384         cc->defrect = cc->bounds;
1385         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1386         cc->pixelaspect.denominator = 59;
1387
1388         return 0;
1389 }
1390
1391 static int vidioc_streamon(struct file *file, void *priv,
1392                                         enum v4l2_buf_type type)
1393 {
1394         struct em28xx_fh      *fh  = priv;
1395         struct em28xx         *dev = fh->dev;
1396         int                   rc;
1397
1398         rc = check_dev(dev);
1399         if (rc < 0)
1400                 return rc;
1401
1402
1403         if (unlikely(res_get(fh) < 0))
1404                 return -EBUSY;
1405
1406         return (videobuf_streamon(&fh->vb_vidq));
1407 }
1408
1409 static int vidioc_streamoff(struct file *file, void *priv,
1410                                         enum v4l2_buf_type type)
1411 {
1412         struct em28xx_fh      *fh  = priv;
1413         struct em28xx         *dev = fh->dev;
1414         int                   rc;
1415
1416         rc = check_dev(dev);
1417         if (rc < 0)
1418                 return rc;
1419
1420         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1421                 return -EINVAL;
1422         if (type != fh->type)
1423                 return -EINVAL;
1424
1425         videobuf_streamoff(&fh->vb_vidq);
1426         res_free(fh);
1427
1428         return 0;
1429 }
1430
1431 static int vidioc_querycap(struct file *file, void  *priv,
1432                                         struct v4l2_capability *cap)
1433 {
1434         struct em28xx_fh      *fh  = priv;
1435         struct em28xx         *dev = fh->dev;
1436
1437         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1438         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1439         strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1440
1441         cap->version = EM28XX_VERSION_CODE;
1442
1443         cap->capabilities =
1444                         V4L2_CAP_SLICED_VBI_CAPTURE |
1445                         V4L2_CAP_VIDEO_CAPTURE |
1446                         V4L2_CAP_AUDIO |
1447                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1448
1449         if (dev->tuner_type != TUNER_ABSENT)
1450                 cap->capabilities |= V4L2_CAP_TUNER;
1451
1452         return 0;
1453 }
1454
1455 static int vidioc_enum_fmt_cap(struct file *file, void  *priv,
1456                                         struct v4l2_fmtdesc *fmtd)
1457 {
1458         if (fmtd->index != 0)
1459                 return -EINVAL;
1460
1461         fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1462         strcpy(fmtd->description, "Packed YUY2");
1463         fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1464         memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1465
1466         return 0;
1467 }
1468
1469 /* Sliced VBI ioctls */
1470 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1471                                         struct v4l2_format *f)
1472 {
1473         struct em28xx_fh      *fh  = priv;
1474         struct em28xx         *dev = fh->dev;
1475         int                   rc;
1476
1477         rc = check_dev(dev);
1478         if (rc < 0)
1479                 return rc;
1480
1481         mutex_lock(&dev->lock);
1482
1483         f->fmt.sliced.service_set = 0;
1484
1485         em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1486
1487         if (f->fmt.sliced.service_set == 0)
1488                 rc = -EINVAL;
1489
1490         mutex_unlock(&dev->lock);
1491         return rc;
1492 }
1493
1494 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1495                         struct v4l2_format *f)
1496 {
1497         struct em28xx_fh      *fh  = priv;
1498         struct em28xx         *dev = fh->dev;
1499         int                   rc;
1500
1501         rc = check_dev(dev);
1502         if (rc < 0)
1503                 return rc;
1504
1505         mutex_lock(&dev->lock);
1506         em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1507         mutex_unlock(&dev->lock);
1508
1509         if (f->fmt.sliced.service_set == 0)
1510                 return -EINVAL;
1511
1512         return 0;
1513 }
1514
1515
1516 static int vidioc_reqbufs(struct file *file, void *priv,
1517                           struct v4l2_requestbuffers *rb)
1518 {
1519         struct em28xx_fh      *fh  = priv;
1520         struct em28xx         *dev = fh->dev;
1521         int                   rc;
1522
1523         rc = check_dev(dev);
1524         if (rc < 0)
1525                 return rc;
1526
1527         return (videobuf_reqbufs(&fh->vb_vidq, rb));
1528 }
1529
1530 static int vidioc_querybuf(struct file *file, void *priv,
1531                            struct v4l2_buffer *b)
1532 {
1533         struct em28xx_fh      *fh  = priv;
1534         struct em28xx         *dev = fh->dev;
1535         int                   rc;
1536
1537         rc = check_dev(dev);
1538         if (rc < 0)
1539                 return rc;
1540
1541         return (videobuf_querybuf(&fh->vb_vidq, b));
1542 }
1543
1544 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1545 {
1546         struct em28xx_fh      *fh  = priv;
1547         struct em28xx         *dev = fh->dev;
1548         int                   rc;
1549
1550         rc = check_dev(dev);
1551         if (rc < 0)
1552                 return rc;
1553
1554         return (videobuf_qbuf(&fh->vb_vidq, b));
1555 }
1556
1557 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1558 {
1559         struct em28xx_fh      *fh  = priv;
1560         struct em28xx         *dev = fh->dev;
1561         int                   rc;
1562
1563         rc = check_dev(dev);
1564         if (rc < 0)
1565                 return rc;
1566
1567         return (videobuf_dqbuf(&fh->vb_vidq, b,
1568                                 file->f_flags & O_NONBLOCK));
1569 }
1570
1571 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1572 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1573 {
1574         struct em28xx_fh  *fh = priv;
1575
1576         return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1577 }
1578 #endif
1579
1580
1581 /* ----------------------------------------------------------- */
1582 /* RADIO ESPECIFIC IOCTLS                                      */
1583 /* ----------------------------------------------------------- */
1584
1585 static int radio_querycap(struct file *file, void  *priv,
1586                           struct v4l2_capability *cap)
1587 {
1588         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1589
1590         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1591         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1592         strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1593
1594         cap->version = EM28XX_VERSION_CODE;
1595         cap->capabilities = V4L2_CAP_TUNER;
1596         return 0;
1597 }
1598
1599 static int radio_g_tuner(struct file *file, void *priv,
1600                          struct v4l2_tuner *t)
1601 {
1602         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1603
1604         if (unlikely(t->index > 0))
1605                 return -EINVAL;
1606
1607         strcpy(t->name, "Radio");
1608         t->type = V4L2_TUNER_RADIO;
1609
1610         em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1611         return 0;
1612 }
1613
1614 static int radio_enum_input(struct file *file, void *priv,
1615                             struct v4l2_input *i)
1616 {
1617         if (i->index != 0)
1618                 return -EINVAL;
1619         strcpy(i->name, "Radio");
1620         i->type = V4L2_INPUT_TYPE_TUNER;
1621
1622         return 0;
1623 }
1624
1625 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1626 {
1627         if (unlikely(a->index))
1628                 return -EINVAL;
1629
1630         strcpy(a->name, "Radio");
1631         return 0;
1632 }
1633
1634 static int radio_s_tuner(struct file *file, void *priv,
1635                          struct v4l2_tuner *t)
1636 {
1637         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1638
1639         if (0 != t->index)
1640                 return -EINVAL;
1641
1642         em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1643
1644         return 0;
1645 }
1646
1647 static int radio_s_audio(struct file *file, void *fh,
1648                          struct v4l2_audio *a)
1649 {
1650         return 0;
1651 }
1652
1653 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1654 {
1655         return 0;
1656 }
1657
1658 static int radio_queryctrl(struct file *file, void *priv,
1659                            struct v4l2_queryctrl *qc)
1660 {
1661         int i;
1662
1663         if (qc->id <  V4L2_CID_BASE ||
1664                 qc->id >= V4L2_CID_LASTP1)
1665                 return -EINVAL;
1666
1667         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1668                 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1669                         memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1670                         return 0;
1671                 }
1672         }
1673
1674         return -EINVAL;
1675 }
1676
1677 /*
1678  * em28xx_v4l2_open()
1679  * inits the device and starts isoc transfer
1680  */
1681 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1682 {
1683         int minor = iminor(inode);
1684         int errCode = 0, radio = 0;
1685         struct em28xx *h,*dev = NULL;
1686         struct em28xx_fh *fh;
1687         enum v4l2_buf_type fh_type = 0;
1688
1689         list_for_each_entry(h, &em28xx_devlist, devlist) {
1690                 if (h->vdev->minor == minor) {
1691                         dev  = h;
1692                         fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1693                 }
1694                 if (h->vbi_dev->minor == minor) {
1695                         dev  = h;
1696                         fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1697                 }
1698                 if (h->radio_dev &&
1699                     h->radio_dev->minor == minor) {
1700                         radio = 1;
1701                         dev   = h;
1702                 }
1703         }
1704         if (NULL == dev)
1705                 return -ENODEV;
1706
1707         em28xx_videodbg("open minor=%d type=%s users=%d\n",
1708                                 minor, v4l2_type_names[fh_type], dev->users);
1709
1710         fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1711
1712         if (!fh) {
1713                 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1714                 return -ENOMEM;
1715         }
1716         mutex_lock(&dev->lock);
1717         fh->dev = dev;
1718         fh->radio = radio;
1719         fh->type = fh_type;
1720         filp->private_data = fh;
1721
1722         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1723                 dev->width = norm_maxw(dev);
1724                 dev->height = norm_maxh(dev);
1725                 dev->hscale = 0;
1726                 dev->vscale = 0;
1727
1728                 em28xx_set_alternate(dev);
1729                 em28xx_resolution_set(dev);
1730
1731         }
1732         if (fh->radio) {
1733                 em28xx_videodbg("video_open: setting radio device\n");
1734                 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1735         }
1736
1737         dev->users++;
1738
1739         videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1740                         NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1741                         sizeof(struct em28xx_buffer), fh);
1742
1743         mutex_unlock(&dev->lock);
1744         return errCode;
1745 }
1746
1747 /*
1748  * em28xx_realease_resources()
1749  * unregisters the v4l2,i2c and usb devices
1750  * called when the device gets disconected or at module unload
1751 */
1752 static void em28xx_release_resources(struct em28xx *dev)
1753 {
1754
1755         /*FIXME: I2C IR should be disconnected */
1756
1757         em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1758                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1759                                 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1760         list_del(&dev->devlist);
1761         if (dev->radio_dev) {
1762                 if (-1 != dev->radio_dev->minor)
1763                         video_unregister_device(dev->radio_dev);
1764                 else
1765                         video_device_release(dev->radio_dev);
1766                 dev->radio_dev = NULL;
1767         }
1768         if (dev->vbi_dev) {
1769                 if (-1 != dev->vbi_dev->minor)
1770                         video_unregister_device(dev->vbi_dev);
1771                 else
1772                         video_device_release(dev->vbi_dev);
1773                 dev->vbi_dev = NULL;
1774         }
1775         if (dev->vdev) {
1776                 if (-1 != dev->vdev->minor)
1777                         video_unregister_device(dev->vdev);
1778                 else
1779                         video_device_release(dev->vdev);
1780                 dev->vdev = NULL;
1781         }
1782         em28xx_i2c_unregister(dev);
1783         usb_put_dev(dev->udev);
1784
1785         /* Mark device as unused */
1786         em28xx_devused&=~(1<<dev->devno);
1787 }
1788
1789 /*
1790  * em28xx_v4l2_close()
1791  * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1792  */
1793 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1794 {
1795         struct em28xx_fh *fh  = filp->private_data;
1796         struct em28xx    *dev = fh->dev;
1797         int              errCode;
1798
1799         em28xx_videodbg("users=%d\n", dev->users);
1800
1801
1802         if (res_check(fh))
1803                 res_free(fh);
1804
1805         mutex_lock(&dev->lock);
1806
1807         if (dev->users == 1) {
1808                 videobuf_stop(&fh->vb_vidq);
1809                 videobuf_mmap_free(&fh->vb_vidq);
1810
1811                 /* the device is already disconnect,
1812                    free the remaining resources */
1813                 if (dev->state & DEV_DISCONNECTED) {
1814                         em28xx_release_resources(dev);
1815                         mutex_unlock(&dev->lock);
1816                         kfree(dev);
1817                         return 0;
1818                 }
1819
1820                 /* do this before setting alternate! */
1821                 em28xx_uninit_isoc(dev);
1822
1823                 /* set alternate 0 */
1824                 dev->alt = 0;
1825                 em28xx_videodbg("setting alternate 0\n");
1826                 errCode = usb_set_interface(dev->udev, 0, 0);
1827                 if (errCode < 0) {
1828                         em28xx_errdev("cannot change alternate number to "
1829                                         "0 (error=%i)\n", errCode);
1830                 }
1831         }
1832         kfree(fh);
1833         dev->users--;
1834         wake_up_interruptible_nr(&dev->open, 1);
1835         mutex_unlock(&dev->lock);
1836         return 0;
1837 }
1838
1839 /*
1840  * em28xx_v4l2_read()
1841  * will allocate buffers when called for the first time
1842  */
1843 static ssize_t
1844 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1845                  loff_t *pos)
1846 {
1847         struct em28xx_fh *fh = filp->private_data;
1848         struct em28xx *dev = fh->dev;
1849         int rc;
1850
1851         rc = check_dev(dev);
1852         if (rc < 0)
1853                 return rc;
1854
1855         /* FIXME: read() is not prepared to allow changing the video
1856            resolution while streaming. Seems a bug at em28xx_set_fmt
1857          */
1858
1859         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1860                 if (unlikely(res_get(fh)))
1861                         return -EBUSY;
1862
1863                 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1864                                         filp->f_flags & O_NONBLOCK);
1865         }
1866         return 0;
1867 }
1868
1869 /*
1870  * em28xx_v4l2_poll()
1871  * will allocate buffers when called for the first time
1872  */
1873 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1874 {
1875         struct em28xx_fh *fh = filp->private_data;
1876         struct em28xx *dev = fh->dev;
1877         int rc;
1878
1879         rc = check_dev(dev);
1880         if (rc < 0)
1881                 return rc;
1882
1883         if (unlikely(res_get(fh) < 0))
1884                 return POLLERR;
1885
1886         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1887                 return POLLERR;
1888
1889         return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1890 }
1891
1892 /*
1893  * em28xx_v4l2_mmap()
1894  */
1895 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1896 {
1897         struct em28xx_fh *fh    = filp->private_data;
1898         struct em28xx    *dev   = fh->dev;
1899         int              rc;
1900
1901         if (unlikely(res_get(fh) < 0))
1902                 return -EBUSY;
1903
1904         rc = check_dev(dev);
1905         if (rc < 0)
1906                 return rc;
1907
1908         rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1909
1910         em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1911                 (unsigned long)vma->vm_start,
1912                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1913                 rc);
1914
1915         return rc;
1916 }
1917
1918 static const struct file_operations em28xx_v4l_fops = {
1919         .owner         = THIS_MODULE,
1920         .open          = em28xx_v4l2_open,
1921         .release       = em28xx_v4l2_close,
1922         .read          = em28xx_v4l2_read,
1923         .poll          = em28xx_v4l2_poll,
1924         .mmap          = em28xx_v4l2_mmap,
1925         .ioctl         = video_ioctl2,
1926         .llseek        = no_llseek,
1927         .compat_ioctl  = v4l_compat_ioctl32,
1928 };
1929
1930 static const struct file_operations radio_fops = {
1931         .owner         = THIS_MODULE,
1932         .open          = em28xx_v4l2_open,
1933         .release       = em28xx_v4l2_close,
1934         .ioctl         = video_ioctl2,
1935         .compat_ioctl  = v4l_compat_ioctl32,
1936         .llseek        = no_llseek,
1937 };
1938
1939 static const struct video_device em28xx_video_template = {
1940         .fops                       = &em28xx_v4l_fops,
1941         .release                    = video_device_release,
1942
1943         .minor                      = -1,
1944         .vidioc_querycap            = vidioc_querycap,
1945         .vidioc_enum_fmt_cap        = vidioc_enum_fmt_cap,
1946         .vidioc_g_fmt_cap           = vidioc_g_fmt_cap,
1947         .vidioc_try_fmt_cap         = vidioc_try_fmt_cap,
1948         .vidioc_s_fmt_cap           = vidioc_s_fmt_cap,
1949         .vidioc_g_audio             = vidioc_g_audio,
1950         .vidioc_s_audio             = vidioc_s_audio,
1951         .vidioc_cropcap             = vidioc_cropcap,
1952
1953         .vidioc_g_fmt_vbi_capture   = vidioc_g_fmt_vbi_capture,
1954         .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1955         .vidioc_s_fmt_vbi_capture   = vidioc_try_set_vbi_capture,
1956
1957         .vidioc_reqbufs             = vidioc_reqbufs,
1958         .vidioc_querybuf            = vidioc_querybuf,
1959         .vidioc_qbuf                = vidioc_qbuf,
1960         .vidioc_dqbuf               = vidioc_dqbuf,
1961         .vidioc_s_std               = vidioc_s_std,
1962         .vidioc_enum_input          = vidioc_enum_input,
1963         .vidioc_g_input             = vidioc_g_input,
1964         .vidioc_s_input             = vidioc_s_input,
1965         .vidioc_queryctrl           = vidioc_queryctrl,
1966         .vidioc_g_ctrl              = vidioc_g_ctrl,
1967         .vidioc_s_ctrl              = vidioc_s_ctrl,
1968         .vidioc_streamon            = vidioc_streamon,
1969         .vidioc_streamoff           = vidioc_streamoff,
1970         .vidioc_g_tuner             = vidioc_g_tuner,
1971         .vidioc_s_tuner             = vidioc_s_tuner,
1972         .vidioc_g_frequency         = vidioc_g_frequency,
1973         .vidioc_s_frequency         = vidioc_s_frequency,
1974 #ifdef CONFIG_VIDEO_ADV_DEBUG
1975         .vidioc_g_register          = vidioc_g_register,
1976         .vidioc_s_register          = vidioc_s_register,
1977 #endif
1978 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1979         .vidiocgmbuf                = vidiocgmbuf,
1980 #endif
1981
1982         .tvnorms                    = V4L2_STD_ALL,
1983         .current_norm               = V4L2_STD_PAL,
1984 };
1985
1986 static struct video_device em28xx_radio_template = {
1987         .name                 = "em28xx-radio",
1988         .type                 = VID_TYPE_TUNER,
1989         .fops                 = &radio_fops,
1990         .minor                = -1,
1991         .vidioc_querycap      = radio_querycap,
1992         .vidioc_g_tuner       = radio_g_tuner,
1993         .vidioc_enum_input    = radio_enum_input,
1994         .vidioc_g_audio       = radio_g_audio,
1995         .vidioc_s_tuner       = radio_s_tuner,
1996         .vidioc_s_audio       = radio_s_audio,
1997         .vidioc_s_input       = radio_s_input,
1998         .vidioc_queryctrl     = radio_queryctrl,
1999         .vidioc_g_ctrl        = vidioc_g_ctrl,
2000         .vidioc_s_ctrl        = vidioc_s_ctrl,
2001         .vidioc_g_frequency   = vidioc_g_frequency,
2002         .vidioc_s_frequency   = vidioc_s_frequency,
2003 #ifdef CONFIG_VIDEO_ADV_DEBUG
2004         .vidioc_g_register    = vidioc_g_register,
2005         .vidioc_s_register    = vidioc_s_register,
2006 #endif
2007 };
2008
2009 /******************************** usb interface *****************************************/
2010
2011
2012 static LIST_HEAD(em28xx_extension_devlist);
2013 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2014
2015 int em28xx_register_extension(struct em28xx_ops *ops)
2016 {
2017         struct em28xx *h, *dev = NULL;
2018
2019         list_for_each_entry(h, &em28xx_devlist, devlist)
2020                 dev = h;
2021
2022         mutex_lock(&em28xx_extension_devlist_lock);
2023         list_add_tail(&ops->next, &em28xx_extension_devlist);
2024         if (dev)
2025                 ops->init(dev);
2026
2027         printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2028         mutex_unlock(&em28xx_extension_devlist_lock);
2029
2030         return 0;
2031 }
2032 EXPORT_SYMBOL(em28xx_register_extension);
2033
2034 void em28xx_unregister_extension(struct em28xx_ops *ops)
2035 {
2036         struct em28xx *h, *dev = NULL;
2037
2038         list_for_each_entry(h, &em28xx_devlist, devlist)
2039                 dev = h;
2040
2041         if (dev)
2042                 ops->fini(dev);
2043
2044         mutex_lock(&em28xx_extension_devlist_lock);
2045         printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2046         list_del(&ops->next);
2047         mutex_unlock(&em28xx_extension_devlist_lock);
2048 }
2049 EXPORT_SYMBOL(em28xx_unregister_extension);
2050
2051 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2052                                              const struct video_device *template,
2053                                              const int type,
2054                                              const char *type_name)
2055 {
2056         struct video_device *vfd;
2057
2058         vfd = video_device_alloc();
2059         if (NULL == vfd)
2060                 return NULL;
2061         *vfd = *template;
2062         vfd->minor   = -1;
2063         vfd->dev = &dev->udev->dev;
2064         vfd->release = video_device_release;
2065         vfd->type = type;
2066         vfd->debug = video_debug;
2067
2068         snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2069                  dev->name, type_name);
2070
2071         return vfd;
2072 }
2073
2074
2075 /*
2076  * em28xx_init_dev()
2077  * allocates and inits the device structs, registers i2c bus and v4l device
2078  */
2079 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2080                            int minor)
2081 {
2082         struct em28xx_ops *ops = NULL;
2083         struct em28xx *dev = *devhandle;
2084         int retval = -ENOMEM;
2085         int errCode;
2086         unsigned int maxh, maxw;
2087
2088         dev->udev = udev;
2089         mutex_init(&dev->lock);
2090         spin_lock_init(&dev->slock);
2091         init_waitqueue_head(&dev->open);
2092         init_waitqueue_head(&dev->wait_frame);
2093         init_waitqueue_head(&dev->wait_stream);
2094
2095         dev->em28xx_write_regs = em28xx_write_regs;
2096         dev->em28xx_read_reg = em28xx_read_reg;
2097         dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2098         dev->em28xx_write_regs_req = em28xx_write_regs_req;
2099         dev->em28xx_read_reg_req = em28xx_read_reg_req;
2100         dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2101
2102         errCode = em28xx_read_reg(dev, CHIPID_REG);
2103         if (errCode >= 0)
2104                 em28xx_info("em28xx chip ID = %d\n", errCode);
2105
2106         em28xx_pre_card_setup(dev);
2107
2108         errCode = em28xx_config(dev);
2109         if (errCode) {
2110                 em28xx_errdev("error configuring device\n");
2111                 em28xx_devused &= ~(1<<dev->devno);
2112                 kfree(dev);
2113                 return -ENOMEM;
2114         }
2115
2116         /* register i2c bus */
2117         em28xx_i2c_register(dev);
2118
2119         /* Do board specific init and eeprom reading */
2120         em28xx_card_setup(dev);
2121
2122         /* Configure audio */
2123         em28xx_audio_analog_set(dev);
2124
2125         /* configure the device */
2126         em28xx_config_i2c(dev);
2127
2128         /* set default norm */
2129         dev->norm = em28xx_video_template.current_norm;
2130
2131         maxw = norm_maxw(dev);
2132         maxh = norm_maxh(dev);
2133
2134         /* set default image size */
2135         dev->width = maxw;
2136         dev->height = maxh;
2137         dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2138         dev->hscale = 0;
2139         dev->vscale = 0;
2140         dev->ctl_input = 2;
2141
2142         errCode = em28xx_config(dev);
2143
2144         list_add_tail(&dev->devlist, &em28xx_devlist);
2145
2146         /* allocate and fill video video_device struct */
2147         dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2148                                           VID_TYPE_CAPTURE, "video");
2149         if (NULL == dev->vdev) {
2150                 em28xx_errdev("cannot allocate video_device.\n");
2151                 goto fail_unreg;
2152         }
2153         if (dev->tuner_type != TUNER_ABSENT)
2154                 dev->vdev->type |= VID_TYPE_TUNER;
2155
2156         /* register v4l2 video video_device */
2157         retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2158                                        video_nr[dev->devno]);
2159         if (retval) {
2160                 em28xx_errdev("unable to register video device (error=%i).\n",
2161                               retval);
2162                 goto fail_unreg;
2163         }
2164
2165         /* Allocate and fill vbi video_device struct */
2166         dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2167                                           VFL_TYPE_VBI, "vbi");
2168         /* register v4l2 vbi video_device */
2169         if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2170                                         vbi_nr[dev->devno]) < 0) {
2171                 em28xx_errdev("unable to register vbi device\n");
2172                 retval = -ENODEV;
2173                 goto fail_unreg;
2174         }
2175
2176         if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2177                 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2178                                         VFL_TYPE_RADIO, "radio");
2179                 if (NULL == dev->radio_dev) {
2180                         em28xx_errdev("cannot allocate video_device.\n");
2181                         goto fail_unreg;
2182                 }
2183                 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2184                                             radio_nr[dev->devno]);
2185                 if (retval < 0) {
2186                         em28xx_errdev("can't register radio device\n");
2187                         goto fail_unreg;
2188                 }
2189                 em28xx_info("Registered radio device as /dev/radio%d\n",
2190                             dev->radio_dev->minor & 0x1f);
2191         }
2192
2193         /* init video dma queues */
2194         INIT_LIST_HEAD(&dev->vidq.active);
2195         INIT_LIST_HEAD(&dev->vidq.queued);
2196
2197
2198         if (dev->has_msp34xx) {
2199                 /* Send a reset to other chips via gpio */
2200                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2201                 msleep(3);
2202                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2203                 msleep(3);
2204         }
2205
2206         video_mux(dev, 0);
2207
2208         em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2209                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2210                                 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2211
2212         mutex_lock(&em28xx_extension_devlist_lock);
2213         if (!list_empty(&em28xx_extension_devlist)) {
2214                 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2215                         if (ops->id)
2216                                 ops->init(dev);
2217                 }
2218         }
2219         mutex_unlock(&em28xx_extension_devlist_lock);
2220
2221         return 0;
2222
2223 fail_unreg:
2224         em28xx_release_resources(dev);
2225         mutex_unlock(&dev->lock);
2226         kfree(dev);
2227         return retval;
2228 }
2229
2230 #if defined(CONFIG_MODULES) && defined(MODULE)
2231 static void request_module_async(struct work_struct *work)
2232 {
2233         struct em28xx *dev = container_of(work,
2234                              struct em28xx, request_module_wk);
2235
2236         if (dev->has_audio_class)
2237                 request_module("snd-usb-audio");
2238         else
2239                 request_module("em28xx-alsa");
2240 }
2241
2242 static void request_modules(struct em28xx *dev)
2243 {
2244         INIT_WORK(&dev->request_module_wk, request_module_async);
2245         schedule_work(&dev->request_module_wk);
2246 }
2247 #else
2248 #define request_modules(dev)
2249 #endif /* CONFIG_MODULES */
2250
2251 /*
2252  * em28xx_usb_probe()
2253  * checks for supported devices
2254  */
2255 static int em28xx_usb_probe(struct usb_interface *interface,
2256                             const struct usb_device_id *id)
2257 {
2258         const struct usb_endpoint_descriptor *endpoint;
2259         struct usb_device *udev;
2260         struct usb_interface *uif;
2261         struct em28xx *dev = NULL;
2262         int retval = -ENODEV;
2263         int i, nr, ifnum;
2264
2265         udev = usb_get_dev(interface_to_usbdev(interface));
2266         ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2267
2268         /* Check to see next free device and mark as used */
2269         nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2270         em28xx_devused|=1<<nr;
2271
2272         /* Don't register audio interfaces */
2273         if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2274                 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2275                                 udev->descriptor.idVendor,udev->descriptor.idProduct,
2276                                 ifnum,
2277                                 interface->altsetting[0].desc.bInterfaceClass);
2278
2279                 em28xx_devused&=~(1<<nr);
2280                 return -ENODEV;
2281         }
2282
2283         em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2284                         udev->descriptor.idVendor,udev->descriptor.idProduct,
2285                         ifnum,
2286                         interface->altsetting[0].desc.bInterfaceClass);
2287
2288         endpoint = &interface->cur_altsetting->endpoint[1].desc;
2289
2290         /* check if the device has the iso in endpoint at the correct place */
2291         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2292             USB_ENDPOINT_XFER_ISOC) {
2293                 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2294                 em28xx_devused&=~(1<<nr);
2295                 return -ENODEV;
2296         }
2297         if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2298                 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2299                 em28xx_devused&=~(1<<nr);
2300                 return -ENODEV;
2301         }
2302
2303         if (nr >= EM28XX_MAXBOARDS) {
2304                 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2305                 em28xx_devused&=~(1<<nr);
2306                 return -ENOMEM;
2307         }
2308
2309         /* allocate memory for our device state and initialize it */
2310         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2311         if (dev == NULL) {
2312                 em28xx_err(DRIVER_NAME ": out of memory!\n");
2313                 em28xx_devused&=~(1<<nr);
2314                 return -ENOMEM;
2315         }
2316
2317         snprintf(dev->name, 29, "em28xx #%d", nr);
2318         dev->devno = nr;
2319         dev->model = id->driver_info;
2320         dev->alt   = -1;
2321
2322         /* Checks if audio is provided by some interface */
2323         for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2324                 uif = udev->config->interface[i];
2325                 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2326                         dev->has_audio_class = 1;
2327                         break;
2328                 }
2329         }
2330
2331         printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2332                    dev->has_audio_class ? "Has" : "Doesn't have");
2333
2334         /* compute alternate max packet sizes */
2335         uif = udev->actconfig->interface[0];
2336
2337         dev->num_alt=uif->num_altsetting;
2338         em28xx_info("Alternate settings: %i\n",dev->num_alt);
2339 //      dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2340         dev->alt_max_pkt_size = kmalloc(32*
2341                                                 dev->num_alt,GFP_KERNEL);
2342         if (dev->alt_max_pkt_size == NULL) {
2343                 em28xx_errdev("out of memory!\n");
2344                 em28xx_devused&=~(1<<nr);
2345                 kfree(dev);
2346                 return -ENOMEM;
2347         }
2348
2349         for (i = 0; i < dev->num_alt ; i++) {
2350                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2351                                                         wMaxPacketSize);
2352                 dev->alt_max_pkt_size[i] =
2353                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2354                 em28xx_info("Alternate setting %i, max size= %i\n",i,
2355                                                         dev->alt_max_pkt_size[i]);
2356         }
2357
2358         if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2359                 dev->model = card[nr];
2360
2361         /* allocate device struct */
2362         retval = em28xx_init_dev(&dev, udev, nr);
2363         if (retval)
2364                 return retval;
2365
2366         em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2367
2368         /* save our data pointer in this interface device */
2369         usb_set_intfdata(interface, dev);
2370
2371         request_modules(dev);
2372
2373         return 0;
2374 }
2375
2376 /*
2377  * em28xx_usb_disconnect()
2378  * called when the device gets diconencted
2379  * video device will be unregistered on v4l2_close in case it is still open
2380  */
2381 static void em28xx_usb_disconnect(struct usb_interface *interface)
2382 {
2383         struct em28xx *dev;
2384         struct em28xx_ops *ops = NULL;
2385
2386         dev = usb_get_intfdata(interface);
2387         usb_set_intfdata(interface, NULL);
2388
2389         if (!dev)
2390                 return;
2391
2392         em28xx_info("disconnecting %s\n", dev->vdev->name);
2393
2394         /* wait until all current v4l2 io is finished then deallocate resources */
2395         mutex_lock(&dev->lock);
2396
2397         wake_up_interruptible_all(&dev->open);
2398
2399         if (dev->users) {
2400                 em28xx_warn
2401                     ("device /dev/video%d is open! Deregistration and memory "
2402                      "deallocation are deferred on close.\n",
2403                                 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2404
2405                 dev->state |= DEV_MISCONFIGURED;
2406                 em28xx_uninit_isoc(dev);
2407                 dev->state |= DEV_DISCONNECTED;
2408                 wake_up_interruptible(&dev->wait_frame);
2409                 wake_up_interruptible(&dev->wait_stream);
2410         } else {
2411                 dev->state |= DEV_DISCONNECTED;
2412                 em28xx_release_resources(dev);
2413         }
2414         mutex_unlock(&dev->lock);
2415
2416         mutex_lock(&em28xx_extension_devlist_lock);
2417         if (!list_empty(&em28xx_extension_devlist)) {
2418                 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2419                         ops->fini(dev);
2420                 }
2421         }
2422         mutex_unlock(&em28xx_extension_devlist_lock);
2423
2424         if (!dev->users) {
2425                 kfree(dev->alt_max_pkt_size);
2426                 kfree(dev);
2427         }
2428 }
2429
2430 static struct usb_driver em28xx_usb_driver = {
2431         .name = "em28xx",
2432         .probe = em28xx_usb_probe,
2433         .disconnect = em28xx_usb_disconnect,
2434         .id_table = em28xx_id_table,
2435 };
2436
2437 static int __init em28xx_module_init(void)
2438 {
2439         int result;
2440
2441         printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2442                (EM28XX_VERSION_CODE >> 16) & 0xff,
2443                (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2444 #ifdef SNAPSHOT
2445         printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2446                SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2447 #endif
2448
2449         /* register this driver with the USB subsystem */
2450         result = usb_register(&em28xx_usb_driver);
2451         if (result)
2452                 em28xx_err(DRIVER_NAME
2453                            " usb_register failed. Error number %d.\n", result);
2454
2455         return result;
2456 }
2457
2458 static void __exit em28xx_module_exit(void)
2459 {
2460         /* deregister this driver with the USB subsystem */
2461         usb_deregister(&em28xx_usb_driver);
2462 }
2463
2464 module_init(em28xx_module_init);
2465 module_exit(em28xx_module_exit);