Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6] / drivers / media / video / em28xx / em28xx-video.c
1 /*
2    em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3                     video capture devices
4
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>
9
10         Some parts based on SN9C10x PC Camera Controllers GPL driver made
11                 by Luca Risolia <luca.risolia@studio.unibo.it>
12
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.
17
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.
22
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.
26  */
27
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>
38
39 #include "em28xx.h"
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
45
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47                       "Markus Rechberger <mrechberger@gmail.com>, " \
48                       "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49                       "Sascha Sommer <saschasommer@freenet.de>"
50
51 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE  KERNEL_VERSION(0, 1, 1)
53
54 #define em28xx_videodbg(fmt, arg...) do {\
55         if (video_debug) \
56                 printk(KERN_INFO "%s %s :"fmt, \
57                          dev->name, __func__ , ##arg); } while (0)
58
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
62
63 #define em28xx_isocdbg(fmt, arg...) \
64 do {\
65         if (isoc_debug) { \
66                 printk(KERN_INFO "%s %s :"fmt, \
67                          dev->name, __func__ , ##arg); \
68         } \
69   } while (0)
70
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
74
75 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[]   = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
79 module_param_array(video_nr, int, NULL, 0444);
80 module_param_array(vbi_nr, int, NULL, 0444);
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr,   "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
85
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
89
90 /* supported video standards */
91 static struct em28xx_fmt format[] = {
92         {
93                 .name     = "16bpp YUY2, 4:2:2, packed",
94                 .fourcc   = V4L2_PIX_FMT_YUYV,
95                 .depth    = 16,
96                 .reg      = EM28XX_OUTFMT_YUV422_Y0UY1V,
97         },
98 };
99
100 /* supported controls */
101 /* Common to all boards */
102 static struct v4l2_queryctrl em28xx_qctrl[] = {
103         {
104                 .id = V4L2_CID_AUDIO_VOLUME,
105                 .type = V4L2_CTRL_TYPE_INTEGER,
106                 .name = "Volume",
107                 .minimum = 0x0,
108                 .maximum = 0x1f,
109                 .step = 0x1,
110                 .default_value = 0x1f,
111                 .flags = 0,
112         }, {
113                 .id = V4L2_CID_AUDIO_MUTE,
114                 .type = V4L2_CTRL_TYPE_BOOLEAN,
115                 .name = "Mute",
116                 .minimum = 0,
117                 .maximum = 1,
118                 .step = 1,
119                 .default_value = 1,
120                 .flags = 0,
121         }
122 };
123
124 /* ------------------------------------------------------------------
125         DMA and thread functions
126    ------------------------------------------------------------------*/
127
128 /*
129  * Announces that a buffer were filled and request the next
130  */
131 static inline void buffer_filled(struct em28xx *dev,
132                                   struct em28xx_dmaqueue *dma_q,
133                                   struct em28xx_buffer *buf)
134 {
135         /* Advice that buffer was filled */
136         em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137         buf->vb.state = VIDEOBUF_DONE;
138         buf->vb.field_count++;
139         do_gettimeofday(&buf->vb.ts);
140
141         dev->isoc_ctl.buf = NULL;
142
143         list_del(&buf->vb.queue);
144         wake_up(&buf->vb.done);
145 }
146
147 /*
148  * Identify the buffer header type and properly handles
149  */
150 static void em28xx_copy_video(struct em28xx *dev,
151                               struct em28xx_dmaqueue  *dma_q,
152                               struct em28xx_buffer *buf,
153                               unsigned char *p,
154                               unsigned char *outp, unsigned long len)
155 {
156         void *fieldstart, *startwrite, *startread;
157         int  linesdone, currlinedone, offset, lencopy, remain;
158         int bytesperline = dev->width << 1;
159
160         if (dma_q->pos + len > buf->vb.size)
161                 len = buf->vb.size - dma_q->pos;
162
163         if (p[0] != 0x88 && p[0] != 0x22) {
164                 em28xx_isocdbg("frame is not complete\n");
165                 len += 4;
166         } else
167                 p += 4;
168
169         startread = p;
170         remain = len;
171
172         /* Interlaces frame */
173         if (buf->top_field)
174                 fieldstart = outp;
175         else
176                 fieldstart = outp + bytesperline;
177
178         linesdone = dma_q->pos / bytesperline;
179         currlinedone = dma_q->pos % bytesperline;
180         offset = linesdone * bytesperline * 2 + currlinedone;
181         startwrite = fieldstart + offset;
182         lencopy = bytesperline - currlinedone;
183         lencopy = lencopy > remain ? remain : lencopy;
184
185         if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
186                 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
187                                ((char *)startwrite + lencopy) -
188                                ((char *)outp + buf->vb.size));
189                 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
190         }
191         if (lencopy <= 0)
192                 return;
193         memcpy(startwrite, startread, lencopy);
194
195         remain -= lencopy;
196
197         while (remain > 0) {
198                 startwrite += lencopy + bytesperline;
199                 startread += lencopy;
200                 if (bytesperline > remain)
201                         lencopy = remain;
202                 else
203                         lencopy = bytesperline;
204
205                 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
206                         em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
207                                        ((char *)startwrite + lencopy) -
208                                        ((char *)outp + buf->vb.size));
209                         lencopy = remain = (char *)outp + buf->vb.size -
210                                            (char *)startwrite;
211                 }
212                 if (lencopy <= 0)
213                         break;
214
215                 memcpy(startwrite, startread, lencopy);
216
217                 remain -= lencopy;
218         }
219
220         dma_q->pos += len;
221 }
222
223 static inline void print_err_status(struct em28xx *dev,
224                                      int packet, int status)
225 {
226         char *errmsg = "Unknown";
227
228         switch (status) {
229         case -ENOENT:
230                 errmsg = "unlinked synchronuously";
231                 break;
232         case -ECONNRESET:
233                 errmsg = "unlinked asynchronuously";
234                 break;
235         case -ENOSR:
236                 errmsg = "Buffer error (overrun)";
237                 break;
238         case -EPIPE:
239                 errmsg = "Stalled (device not responding)";
240                 break;
241         case -EOVERFLOW:
242                 errmsg = "Babble (bad cable?)";
243                 break;
244         case -EPROTO:
245                 errmsg = "Bit-stuff error (bad cable?)";
246                 break;
247         case -EILSEQ:
248                 errmsg = "CRC/Timeout (could be anything)";
249                 break;
250         case -ETIME:
251                 errmsg = "Device does not respond";
252                 break;
253         }
254         if (packet < 0) {
255                 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
256         } else {
257                 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258                                packet, status, errmsg);
259         }
260 }
261
262 /*
263  * video-buf generic routine to get the next available buffer
264  */
265 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
266                                           struct em28xx_buffer **buf)
267 {
268         struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
269         char *outp;
270
271         if (list_empty(&dma_q->active)) {
272                 em28xx_isocdbg("No active queue to serve\n");
273                 dev->isoc_ctl.buf = NULL;
274                 *buf = NULL;
275                 return;
276         }
277
278         /* Get the next buffer */
279         *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280
281         /* Cleans up buffer - Usefull for testing for frame/URB loss */
282         outp = videobuf_to_vmalloc(&(*buf)->vb);
283         memset(outp, 0, (*buf)->vb.size);
284
285         dev->isoc_ctl.buf = *buf;
286
287         return;
288 }
289
290 /*
291  * Controls the isoc copy of each urb packet
292  */
293 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
294 {
295         struct em28xx_buffer    *buf;
296         struct em28xx_dmaqueue  *dma_q = urb->context;
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         Videobuf operations
379    ------------------------------------------------------------------*/
380
381 static int
382 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383 {
384         struct em28xx_fh *fh = vq->priv_data;
385         struct em28xx        *dev = fh->dev;
386         struct v4l2_frequency f;
387
388         *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
389
390         if (0 == *count)
391                 *count = EM28XX_DEF_BUF;
392
393         if (*count < EM28XX_MIN_BUF)
394                 *count = EM28XX_MIN_BUF;
395
396         /* Ask tuner to go to analog or radio mode */
397         memset(&f, 0, sizeof(f));
398         f.frequency = dev->ctl_freq;
399         f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
400
401         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
402
403         return 0;
404 }
405
406 /* This is called *without* dev->slock held; please keep it that way */
407 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
408 {
409         struct em28xx_fh     *fh  = vq->priv_data;
410         struct em28xx        *dev = fh->dev;
411         unsigned long flags = 0;
412         if (in_interrupt())
413                 BUG();
414
415         /* We used to wait for the buffer to finish here, but this didn't work
416            because, as we were keeping the state as VIDEOBUF_QUEUED,
417            videobuf_queue_cancel marked it as finished for us.
418            (Also, it could wedge forever if the hardware was misconfigured.)
419
420            This should be safe; by the time we get here, the buffer isn't
421            queued anymore. If we ever start marking the buffers as
422            VIDEOBUF_ACTIVE, it won't be, though.
423         */
424         spin_lock_irqsave(&dev->slock, flags);
425         if (dev->isoc_ctl.buf == buf)
426                 dev->isoc_ctl.buf = NULL;
427         spin_unlock_irqrestore(&dev->slock, flags);
428
429         videobuf_vmalloc_free(&buf->vb);
430         buf->vb.state = VIDEOBUF_NEEDS_INIT;
431 }
432
433 static int
434 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
435                                                 enum v4l2_field field)
436 {
437         struct em28xx_fh     *fh  = vq->priv_data;
438         struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
439         struct em28xx        *dev = fh->dev;
440         int                  rc = 0, urb_init = 0;
441
442         buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
443
444         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
445                 return -EINVAL;
446
447         buf->vb.width  = dev->width;
448         buf->vb.height = dev->height;
449         buf->vb.field  = field;
450
451         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
452                 rc = videobuf_iolock(vq, &buf->vb, NULL);
453                 if (rc < 0)
454                         goto fail;
455         }
456
457         if (!dev->isoc_ctl.num_bufs)
458                 urb_init = 1;
459
460         if (urb_init) {
461                 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
462                                       EM28XX_NUM_BUFS, dev->max_pkt_size,
463                                       em28xx_isoc_copy);
464                 if (rc < 0)
465                         goto fail;
466         }
467
468         buf->vb.state = VIDEOBUF_PREPARED;
469         return 0;
470
471 fail:
472         free_buffer(vq, buf);
473         return rc;
474 }
475
476 static void
477 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
478 {
479         struct em28xx_buffer    *buf     = container_of(vb, struct em28xx_buffer, vb);
480         struct em28xx_fh        *fh      = vq->priv_data;
481         struct em28xx           *dev     = fh->dev;
482         struct em28xx_dmaqueue  *vidq    = &dev->vidq;
483
484         buf->vb.state = VIDEOBUF_QUEUED;
485         list_add_tail(&buf->vb.queue, &vidq->active);
486
487 }
488
489 static void buffer_release(struct videobuf_queue *vq,
490                                 struct videobuf_buffer *vb)
491 {
492         struct em28xx_buffer   *buf  = container_of(vb, struct em28xx_buffer, vb);
493         struct em28xx_fh       *fh   = vq->priv_data;
494         struct em28xx          *dev  = (struct em28xx *)fh->dev;
495
496         em28xx_isocdbg("em28xx: called buffer_release\n");
497
498         free_buffer(vq, buf);
499 }
500
501 static struct videobuf_queue_ops em28xx_video_qops = {
502         .buf_setup      = buffer_setup,
503         .buf_prepare    = buffer_prepare,
504         .buf_queue      = buffer_queue,
505         .buf_release    = buffer_release,
506 };
507
508 /*********************  v4l2 interface  **************************************/
509
510 static void video_mux(struct em28xx *dev, int index)
511 {
512         struct v4l2_routing route;
513
514         route.input = INPUT(index)->vmux;
515         route.output = 0;
516         dev->ctl_input = index;
517         dev->ctl_ainput = INPUT(index)->amux;
518         dev->ctl_aoutput = INPUT(index)->aout;
519
520         if (!dev->ctl_aoutput)
521                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
522
523         em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
524
525         if (dev->board.has_msp34xx) {
526                 if (dev->i2s_speed) {
527                         em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
528                                 &dev->i2s_speed);
529                 }
530                 route.input = dev->ctl_ainput;
531                 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
532                 /* Note: this is msp3400 specific */
533                 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
534                         &route);
535         }
536
537         em28xx_audio_analog_set(dev);
538 }
539
540 /* Usage lock check functions */
541 static int res_get(struct em28xx_fh *fh)
542 {
543         struct em28xx    *dev = fh->dev;
544         int              rc   = 0;
545
546         /* This instance already has stream_on */
547         if (fh->stream_on)
548                 return rc;
549
550         if (dev->stream_on)
551                 return -EBUSY;
552
553         dev->stream_on = 1;
554         fh->stream_on  = 1;
555         return rc;
556 }
557
558 static int res_check(struct em28xx_fh *fh)
559 {
560         return (fh->stream_on);
561 }
562
563 static void res_free(struct em28xx_fh *fh)
564 {
565         struct em28xx    *dev = fh->dev;
566
567         fh->stream_on = 0;
568         dev->stream_on = 0;
569 }
570
571 /*
572  * em28xx_get_ctrl()
573  * return the current saturation, brightness or contrast, mute state
574  */
575 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
576 {
577         switch (ctrl->id) {
578         case V4L2_CID_AUDIO_MUTE:
579                 ctrl->value = dev->mute;
580                 return 0;
581         case V4L2_CID_AUDIO_VOLUME:
582                 ctrl->value = dev->volume;
583                 return 0;
584         default:
585                 return -EINVAL;
586         }
587 }
588
589 /*
590  * em28xx_set_ctrl()
591  * mute or set new saturation, brightness or contrast
592  */
593 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
594 {
595         switch (ctrl->id) {
596         case V4L2_CID_AUDIO_MUTE:
597                 if (ctrl->value != dev->mute) {
598                         dev->mute = ctrl->value;
599                         return em28xx_audio_analog_set(dev);
600                 }
601                 return 0;
602         case V4L2_CID_AUDIO_VOLUME:
603                 dev->volume = ctrl->value;
604                 return em28xx_audio_analog_set(dev);
605         default:
606                 return -EINVAL;
607         }
608 }
609
610 static int check_dev(struct em28xx *dev)
611 {
612         if (dev->state & DEV_DISCONNECTED) {
613                 em28xx_errdev("v4l2 ioctl: device not present\n");
614                 return -ENODEV;
615         }
616
617         if (dev->state & DEV_MISCONFIGURED) {
618                 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
619                               "close and open it again\n");
620                 return -EIO;
621         }
622         return 0;
623 }
624
625 static void get_scale(struct em28xx *dev,
626                         unsigned int width, unsigned int height,
627                         unsigned int *hscale, unsigned int *vscale)
628 {
629         unsigned int          maxw   = norm_maxw(dev);
630         unsigned int          maxh   = norm_maxh(dev);
631
632         *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
633         if (*hscale >= 0x4000)
634                 *hscale = 0x3fff;
635
636         *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
637         if (*vscale >= 0x4000)
638                 *vscale = 0x3fff;
639 }
640
641 /* ------------------------------------------------------------------
642         IOCTL vidioc handling
643    ------------------------------------------------------------------*/
644
645 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
646                                         struct v4l2_format *f)
647 {
648         struct em28xx_fh      *fh  = priv;
649         struct em28xx         *dev = fh->dev;
650
651         mutex_lock(&dev->lock);
652
653         f->fmt.pix.width = dev->width;
654         f->fmt.pix.height = dev->height;
655         f->fmt.pix.pixelformat = dev->format->fourcc;
656         f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
657         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline  * dev->height;
658         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
659
660         /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
661         f->fmt.pix.field = dev->interlaced ?
662                            V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
663
664         mutex_unlock(&dev->lock);
665         return 0;
666 }
667
668 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
669 {
670         unsigned int i;
671
672         for (i = 0; i < ARRAY_SIZE(format); i++)
673                 if (format[i].fourcc == fourcc)
674                         return &format[i];
675
676         return NULL;
677 }
678
679 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
680                         struct v4l2_format *f)
681 {
682         struct em28xx_fh      *fh    = priv;
683         struct em28xx         *dev   = fh->dev;
684         int                   width  = f->fmt.pix.width;
685         int                   height = f->fmt.pix.height;
686         unsigned int          maxw   = norm_maxw(dev);
687         unsigned int          maxh   = norm_maxh(dev);
688         unsigned int          hscale, vscale;
689         struct em28xx_fmt     *fmt;
690
691         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
692         if (!fmt) {
693                 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
694                                 f->fmt.pix.pixelformat);
695                 return -EINVAL;
696         }
697
698         /* width must even because of the YUYV format
699            height must be even because of interlacing */
700         height &= 0xfffe;
701         width  &= 0xfffe;
702
703         if (unlikely(height < 32))
704                 height = 32;
705         if (unlikely(height > maxh))
706                 height = maxh;
707         if (unlikely(width < 48))
708                 width = 48;
709         if (unlikely(width > maxw))
710                 width = maxw;
711
712         if (dev->board.is_em2800) {
713                 /* the em2800 can only scale down to 50% */
714                 if (height % (maxh / 2))
715                         height = maxh;
716                 if (width % (maxw / 2))
717                         width = maxw;
718                 /* according to empiatech support */
719                 /* the MaxPacketSize is to small to support */
720                 /* framesizes larger than 640x480 @ 30 fps */
721                 /* or 640x576 @ 25 fps. As this would cut */
722                 /* of a part of the image we prefer */
723                 /* 360x576 or 360x480 for now */
724                 if (width == maxw && height == maxh)
725                         width /= 2;
726         }
727
728         get_scale(dev, width, height, &hscale, &vscale);
729
730         width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
731         height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
732
733         f->fmt.pix.width = width;
734         f->fmt.pix.height = height;
735         f->fmt.pix.pixelformat = fmt->fourcc;
736         f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
737         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
738         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
739         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
740
741         return 0;
742 }
743
744 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
745                         struct v4l2_format *f)
746 {
747         struct em28xx_fh      *fh  = priv;
748         struct em28xx         *dev = fh->dev;
749         int                   rc;
750         struct em28xx_fmt     *fmt;
751
752         rc = check_dev(dev);
753         if (rc < 0)
754                 return rc;
755
756         mutex_lock(&dev->lock);
757
758         vidioc_try_fmt_vid_cap(file, priv, f);
759
760         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
761         if (!fmt) {
762                 rc = -EINVAL;
763                 goto out;
764         }
765
766         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
767                 em28xx_errdev("%s queue busy\n", __func__);
768                 rc = -EBUSY;
769                 goto out;
770         }
771
772         if (dev->stream_on && !fh->stream_on) {
773                 em28xx_errdev("%s device in use by another fh\n", __func__);
774                 rc = -EBUSY;
775                 goto out;
776         }
777
778         /* set new image size */
779         dev->width = f->fmt.pix.width;
780         dev->height = f->fmt.pix.height;
781         dev->format = fmt;
782         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
783
784         em28xx_set_alternate(dev);
785         em28xx_resolution_set(dev);
786
787         rc = 0;
788
789 out:
790         mutex_unlock(&dev->lock);
791         return rc;
792 }
793
794 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
795 {
796         struct em28xx_fh   *fh  = priv;
797         struct em28xx      *dev = fh->dev;
798         struct v4l2_format f;
799         int                rc;
800
801         rc = check_dev(dev);
802         if (rc < 0)
803                 return rc;
804
805         mutex_lock(&dev->lock);
806         dev->norm = *norm;
807
808         /* Adjusts width/height, if needed */
809         f.fmt.pix.width = dev->width;
810         f.fmt.pix.height = dev->height;
811         vidioc_try_fmt_vid_cap(file, priv, &f);
812
813         /* set new image size */
814         dev->width = f.fmt.pix.width;
815         dev->height = f.fmt.pix.height;
816         get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
817
818         em28xx_resolution_set(dev);
819         em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
820
821         mutex_unlock(&dev->lock);
822         return 0;
823 }
824
825 static const char *iname[] = {
826         [EM28XX_VMUX_COMPOSITE1] = "Composite1",
827         [EM28XX_VMUX_COMPOSITE2] = "Composite2",
828         [EM28XX_VMUX_COMPOSITE3] = "Composite3",
829         [EM28XX_VMUX_COMPOSITE4] = "Composite4",
830         [EM28XX_VMUX_SVIDEO]     = "S-Video",
831         [EM28XX_VMUX_TELEVISION] = "Television",
832         [EM28XX_VMUX_CABLE]      = "Cable TV",
833         [EM28XX_VMUX_DVB]        = "DVB",
834         [EM28XX_VMUX_DEBUG]      = "for debug only",
835 };
836
837 static int vidioc_enum_input(struct file *file, void *priv,
838                                 struct v4l2_input *i)
839 {
840         struct em28xx_fh   *fh  = priv;
841         struct em28xx      *dev = fh->dev;
842         unsigned int       n;
843
844         n = i->index;
845         if (n >= MAX_EM28XX_INPUT)
846                 return -EINVAL;
847         if (0 == INPUT(n)->type)
848                 return -EINVAL;
849
850         i->index = n;
851         i->type = V4L2_INPUT_TYPE_CAMERA;
852
853         strcpy(i->name, iname[INPUT(n)->type]);
854
855         if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
856                 (EM28XX_VMUX_CABLE == INPUT(n)->type))
857                 i->type = V4L2_INPUT_TYPE_TUNER;
858
859         i->std = dev->vdev->tvnorms;
860
861         return 0;
862 }
863
864 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
865 {
866         struct em28xx_fh   *fh  = priv;
867         struct em28xx      *dev = fh->dev;
868
869         *i = dev->ctl_input;
870
871         return 0;
872 }
873
874 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
875 {
876         struct em28xx_fh   *fh  = priv;
877         struct em28xx      *dev = fh->dev;
878         int                rc;
879
880         rc = check_dev(dev);
881         if (rc < 0)
882                 return rc;
883
884         if (i >= MAX_EM28XX_INPUT)
885                 return -EINVAL;
886         if (0 == INPUT(i)->type)
887                 return -EINVAL;
888
889         dev->ctl_input = i;
890
891         mutex_lock(&dev->lock);
892         video_mux(dev, dev->ctl_input);
893         mutex_unlock(&dev->lock);
894         return 0;
895 }
896
897 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
898 {
899         struct em28xx_fh   *fh    = priv;
900         struct em28xx      *dev   = fh->dev;
901
902         switch (a->index) {
903         case EM28XX_AMUX_VIDEO:
904                 strcpy(a->name, "Television");
905                 break;
906         case EM28XX_AMUX_LINE_IN:
907                 strcpy(a->name, "Line In");
908                 break;
909         case EM28XX_AMUX_VIDEO2:
910                 strcpy(a->name, "Television alt");
911                 break;
912         case EM28XX_AMUX_PHONE:
913                 strcpy(a->name, "Phone");
914                 break;
915         case EM28XX_AMUX_MIC:
916                 strcpy(a->name, "Mic");
917                 break;
918         case EM28XX_AMUX_CD:
919                 strcpy(a->name, "CD");
920                 break;
921         case EM28XX_AMUX_AUX:
922                 strcpy(a->name, "Aux");
923                 break;
924         case EM28XX_AMUX_PCM_OUT:
925                 strcpy(a->name, "PCM");
926                 break;
927         default:
928                 return -EINVAL;
929         }
930
931         a->index = dev->ctl_ainput;
932         a->capability = V4L2_AUDCAP_STEREO;
933
934         return 0;
935 }
936
937 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
938 {
939         struct em28xx_fh   *fh  = priv;
940         struct em28xx      *dev = fh->dev;
941
942
943         if (a->index >= MAX_EM28XX_INPUT)
944                 return -EINVAL;
945         if (0 == INPUT(a->index)->type)
946                 return -EINVAL;
947
948         mutex_lock(&dev->lock);
949
950         dev->ctl_ainput = INPUT(a->index)->amux;
951         dev->ctl_aoutput = INPUT(a->index)->aout;
952
953         if (!dev->ctl_aoutput)
954                 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
955
956         mutex_unlock(&dev->lock);
957         return 0;
958 }
959
960 static int vidioc_queryctrl(struct file *file, void *priv,
961                                 struct v4l2_queryctrl *qc)
962 {
963         struct em28xx_fh      *fh  = priv;
964         struct em28xx         *dev = fh->dev;
965         int                   id  = qc->id;
966         int                   i;
967         int                   rc;
968
969         rc = check_dev(dev);
970         if (rc < 0)
971                 return rc;
972
973         memset(qc, 0, sizeof(*qc));
974
975         qc->id = id;
976
977         if (!dev->board.has_msp34xx) {
978                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
979                         if (qc->id && qc->id == em28xx_qctrl[i].id) {
980                                 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
981                                 return 0;
982                         }
983                 }
984         }
985         mutex_lock(&dev->lock);
986         em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
987         mutex_unlock(&dev->lock);
988
989         if (qc->type)
990                 return 0;
991         else
992                 return -EINVAL;
993 }
994
995 static int vidioc_g_ctrl(struct file *file, void *priv,
996                                 struct v4l2_control *ctrl)
997 {
998         struct em28xx_fh      *fh  = priv;
999         struct em28xx         *dev = fh->dev;
1000         int                   rc;
1001
1002         rc = check_dev(dev);
1003         if (rc < 0)
1004                 return rc;
1005         rc = 0;
1006
1007         mutex_lock(&dev->lock);
1008
1009         if (dev->board.has_msp34xx)
1010                 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1011         else
1012                 rc = em28xx_get_ctrl(dev, ctrl);
1013
1014         mutex_unlock(&dev->lock);
1015         return rc;
1016 }
1017
1018 static int vidioc_s_ctrl(struct file *file, void *priv,
1019                                 struct v4l2_control *ctrl)
1020 {
1021         struct em28xx_fh      *fh  = priv;
1022         struct em28xx         *dev = fh->dev;
1023         u8                    i;
1024         int                   rc;
1025
1026         rc = check_dev(dev);
1027         if (rc < 0)
1028                 return rc;
1029
1030         mutex_lock(&dev->lock);
1031
1032         if (dev->board.has_msp34xx)
1033                 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1034         else {
1035                 rc = 1;
1036                 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1037                         if (ctrl->id == em28xx_qctrl[i].id) {
1038                                 if (ctrl->value < em28xx_qctrl[i].minimum ||
1039                                     ctrl->value > em28xx_qctrl[i].maximum) {
1040                                         rc = -ERANGE;
1041                                         break;
1042                                 }
1043
1044                                 rc = em28xx_set_ctrl(dev, ctrl);
1045                                 break;
1046                         }
1047                 }
1048         }
1049
1050         /* Control not found - try to send it to the attached devices */
1051         if (rc == 1) {
1052                 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1053                 rc = 0;
1054         }
1055
1056         mutex_unlock(&dev->lock);
1057         return rc;
1058 }
1059
1060 static int vidioc_g_tuner(struct file *file, void *priv,
1061                                 struct v4l2_tuner *t)
1062 {
1063         struct em28xx_fh      *fh  = priv;
1064         struct em28xx         *dev = fh->dev;
1065         int                   rc;
1066
1067         rc = check_dev(dev);
1068         if (rc < 0)
1069                 return rc;
1070
1071         if (0 != t->index)
1072                 return -EINVAL;
1073
1074         strcpy(t->name, "Tuner");
1075
1076         mutex_lock(&dev->lock);
1077
1078         em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1079
1080         mutex_unlock(&dev->lock);
1081         return 0;
1082 }
1083
1084 static int vidioc_s_tuner(struct file *file, void *priv,
1085                                 struct v4l2_tuner *t)
1086 {
1087         struct em28xx_fh      *fh  = priv;
1088         struct em28xx         *dev = fh->dev;
1089         int                   rc;
1090
1091         rc = check_dev(dev);
1092         if (rc < 0)
1093                 return rc;
1094
1095         if (0 != t->index)
1096                 return -EINVAL;
1097
1098         mutex_lock(&dev->lock);
1099
1100         em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1101
1102         mutex_unlock(&dev->lock);
1103         return 0;
1104 }
1105
1106 static int vidioc_g_frequency(struct file *file, void *priv,
1107                                 struct v4l2_frequency *f)
1108 {
1109         struct em28xx_fh      *fh  = priv;
1110         struct em28xx         *dev = fh->dev;
1111
1112         mutex_lock(&dev->lock);
1113         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1114         f->frequency = dev->ctl_freq;
1115         mutex_unlock(&dev->lock);
1116
1117         return 0;
1118 }
1119
1120 static int vidioc_s_frequency(struct file *file, void *priv,
1121                                 struct v4l2_frequency *f)
1122 {
1123         struct em28xx_fh      *fh  = priv;
1124         struct em28xx         *dev = fh->dev;
1125         int                   rc;
1126
1127         rc = check_dev(dev);
1128         if (rc < 0)
1129                 return rc;
1130
1131         if (0 != f->tuner)
1132                 return -EINVAL;
1133
1134         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1135                 return -EINVAL;
1136         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1137                 return -EINVAL;
1138
1139         mutex_lock(&dev->lock);
1140
1141         dev->ctl_freq = f->frequency;
1142         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1143
1144         mutex_unlock(&dev->lock);
1145
1146         return 0;
1147 }
1148
1149 #ifdef CONFIG_VIDEO_ADV_DEBUG
1150 static int em28xx_reg_len(int reg)
1151 {
1152         switch (reg) {
1153         case EM28XX_R40_AC97LSB:
1154         case EM28XX_R30_HSCALELOW:
1155         case EM28XX_R32_VSCALELOW:
1156                 return 2;
1157         default:
1158                 return 1;
1159         }
1160 }
1161
1162 static int vidioc_g_chip_ident(struct file *file, void *priv,
1163                struct v4l2_dbg_chip_ident *chip)
1164 {
1165         struct em28xx_fh      *fh  = priv;
1166         struct em28xx         *dev = fh->dev;
1167
1168         chip->ident = V4L2_IDENT_NONE;
1169         chip->revision = 0;
1170
1171         em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
1172
1173         return 0;
1174 }
1175
1176
1177 static int vidioc_g_register(struct file *file, void *priv,
1178                              struct v4l2_dbg_register *reg)
1179 {
1180         struct em28xx_fh      *fh  = priv;
1181         struct em28xx         *dev = fh->dev;
1182         int ret;
1183
1184         switch (reg->match.type) {
1185         case V4L2_CHIP_MATCH_AC97:
1186                 mutex_lock(&dev->lock);
1187                 ret = em28xx_read_ac97(dev, reg->reg);
1188                 mutex_unlock(&dev->lock);
1189                 if (ret < 0)
1190                         return ret;
1191
1192                 reg->val = ret;
1193                 reg->size = 1;
1194                 return 0;
1195         case V4L2_CHIP_MATCH_I2C_DRIVER:
1196                 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1197                 return 0;
1198         case V4L2_CHIP_MATCH_I2C_ADDR:
1199                 /* Not supported yet */
1200                 return -EINVAL;
1201         default:
1202                 if (!v4l2_chip_match_host(&reg->match))
1203                         return -EINVAL;
1204         }
1205
1206         /* Match host */
1207         reg->size = em28xx_reg_len(reg->reg);
1208         if (reg->size == 1) {
1209                 mutex_lock(&dev->lock);
1210                 ret = em28xx_read_reg(dev, reg->reg);
1211                 mutex_unlock(&dev->lock);
1212
1213                 if (ret < 0)
1214                         return ret;
1215
1216                 reg->val = ret;
1217         } else {
1218                 __le16 val = 0;
1219                 mutex_lock(&dev->lock);
1220                 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1221                                                    reg->reg, (char *)&val, 2);
1222                 mutex_unlock(&dev->lock);
1223                 if (ret < 0)
1224                         return ret;
1225
1226                 reg->val = le16_to_cpu(val);
1227         }
1228
1229         return 0;
1230 }
1231
1232 static int vidioc_s_register(struct file *file, void *priv,
1233                              struct v4l2_dbg_register *reg)
1234 {
1235         struct em28xx_fh      *fh  = priv;
1236         struct em28xx         *dev = fh->dev;
1237         __le16 buf;
1238         int    rc;
1239
1240         switch (reg->match.type) {
1241         case V4L2_CHIP_MATCH_AC97:
1242                 mutex_lock(&dev->lock);
1243                 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1244                 mutex_unlock(&dev->lock);
1245
1246                 return rc;
1247         case V4L2_CHIP_MATCH_I2C_DRIVER:
1248                 em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1249                 return 0;
1250         case V4L2_CHIP_MATCH_I2C_ADDR:
1251                 /* Not supported yet */
1252                 return -EINVAL;
1253         default:
1254                 if (!v4l2_chip_match_host(&reg->match))
1255                         return -EINVAL;
1256         }
1257
1258         /* Match host */
1259         buf = cpu_to_le16(reg->val);
1260
1261         mutex_lock(&dev->lock);
1262         rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1263                                em28xx_reg_len(reg->reg));
1264         mutex_unlock(&dev->lock);
1265
1266         return rc;
1267 }
1268 #endif
1269
1270
1271 static int vidioc_cropcap(struct file *file, void *priv,
1272                                         struct v4l2_cropcap *cc)
1273 {
1274         struct em28xx_fh      *fh  = priv;
1275         struct em28xx         *dev = fh->dev;
1276
1277         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278                 return -EINVAL;
1279
1280         cc->bounds.left = 0;
1281         cc->bounds.top = 0;
1282         cc->bounds.width = dev->width;
1283         cc->bounds.height = dev->height;
1284         cc->defrect = cc->bounds;
1285         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1286         cc->pixelaspect.denominator = 59;
1287
1288         return 0;
1289 }
1290
1291 static int vidioc_streamon(struct file *file, void *priv,
1292                                         enum v4l2_buf_type type)
1293 {
1294         struct em28xx_fh      *fh  = priv;
1295         struct em28xx         *dev = fh->dev;
1296         int                   rc;
1297
1298         rc = check_dev(dev);
1299         if (rc < 0)
1300                 return rc;
1301
1302
1303         mutex_lock(&dev->lock);
1304         rc = res_get(fh);
1305
1306         if (likely(rc >= 0))
1307                 rc = videobuf_streamon(&fh->vb_vidq);
1308
1309         mutex_unlock(&dev->lock);
1310
1311         return rc;
1312 }
1313
1314 static int vidioc_streamoff(struct file *file, void *priv,
1315                                         enum v4l2_buf_type type)
1316 {
1317         struct em28xx_fh      *fh  = priv;
1318         struct em28xx         *dev = fh->dev;
1319         int                   rc;
1320
1321         rc = check_dev(dev);
1322         if (rc < 0)
1323                 return rc;
1324
1325         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326                 return -EINVAL;
1327         if (type != fh->type)
1328                 return -EINVAL;
1329
1330         mutex_lock(&dev->lock);
1331
1332         videobuf_streamoff(&fh->vb_vidq);
1333         res_free(fh);
1334
1335         mutex_unlock(&dev->lock);
1336
1337         return 0;
1338 }
1339
1340 static int vidioc_querycap(struct file *file, void  *priv,
1341                                         struct v4l2_capability *cap)
1342 {
1343         struct em28xx_fh      *fh  = priv;
1344         struct em28xx         *dev = fh->dev;
1345
1346         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1347         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1348         strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1349
1350         cap->version = EM28XX_VERSION_CODE;
1351
1352         cap->capabilities =
1353                         V4L2_CAP_SLICED_VBI_CAPTURE |
1354                         V4L2_CAP_VIDEO_CAPTURE |
1355                         V4L2_CAP_AUDIO |
1356                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1357
1358         if (dev->tuner_type != TUNER_ABSENT)
1359                 cap->capabilities |= V4L2_CAP_TUNER;
1360
1361         return 0;
1362 }
1363
1364 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1365                                         struct v4l2_fmtdesc *f)
1366 {
1367         if (unlikely(f->index >= ARRAY_SIZE(format)))
1368                 return -EINVAL;
1369
1370         strlcpy(f->description, format[f->index].name, sizeof(f->description));
1371         f->pixelformat = format[f->index].fourcc;
1372
1373         return 0;
1374 }
1375
1376 /* Sliced VBI ioctls */
1377 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1378                                         struct v4l2_format *f)
1379 {
1380         struct em28xx_fh      *fh  = priv;
1381         struct em28xx         *dev = fh->dev;
1382         int                   rc;
1383
1384         rc = check_dev(dev);
1385         if (rc < 0)
1386                 return rc;
1387
1388         mutex_lock(&dev->lock);
1389
1390         f->fmt.sliced.service_set = 0;
1391
1392         em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1393
1394         if (f->fmt.sliced.service_set == 0)
1395                 rc = -EINVAL;
1396
1397         mutex_unlock(&dev->lock);
1398         return rc;
1399 }
1400
1401 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1402                         struct v4l2_format *f)
1403 {
1404         struct em28xx_fh      *fh  = priv;
1405         struct em28xx         *dev = fh->dev;
1406         int                   rc;
1407
1408         rc = check_dev(dev);
1409         if (rc < 0)
1410                 return rc;
1411
1412         mutex_lock(&dev->lock);
1413         em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1414         mutex_unlock(&dev->lock);
1415
1416         if (f->fmt.sliced.service_set == 0)
1417                 return -EINVAL;
1418
1419         return 0;
1420 }
1421
1422
1423 static int vidioc_reqbufs(struct file *file, void *priv,
1424                           struct v4l2_requestbuffers *rb)
1425 {
1426         struct em28xx_fh      *fh  = priv;
1427         struct em28xx         *dev = fh->dev;
1428         int                   rc;
1429
1430         rc = check_dev(dev);
1431         if (rc < 0)
1432                 return rc;
1433
1434         return (videobuf_reqbufs(&fh->vb_vidq, rb));
1435 }
1436
1437 static int vidioc_querybuf(struct file *file, void *priv,
1438                            struct v4l2_buffer *b)
1439 {
1440         struct em28xx_fh      *fh  = priv;
1441         struct em28xx         *dev = fh->dev;
1442         int                   rc;
1443
1444         rc = check_dev(dev);
1445         if (rc < 0)
1446                 return rc;
1447
1448         return (videobuf_querybuf(&fh->vb_vidq, b));
1449 }
1450
1451 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1452 {
1453         struct em28xx_fh      *fh  = priv;
1454         struct em28xx         *dev = fh->dev;
1455         int                   rc;
1456
1457         rc = check_dev(dev);
1458         if (rc < 0)
1459                 return rc;
1460
1461         return (videobuf_qbuf(&fh->vb_vidq, b));
1462 }
1463
1464 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1465 {
1466         struct em28xx_fh      *fh  = priv;
1467         struct em28xx         *dev = fh->dev;
1468         int                   rc;
1469
1470         rc = check_dev(dev);
1471         if (rc < 0)
1472                 return rc;
1473
1474         return (videobuf_dqbuf(&fh->vb_vidq, b,
1475                                 file->f_flags & O_NONBLOCK));
1476 }
1477
1478 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1479 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1480 {
1481         struct em28xx_fh  *fh = priv;
1482
1483         return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1484 }
1485 #endif
1486
1487
1488 /* ----------------------------------------------------------- */
1489 /* RADIO ESPECIFIC IOCTLS                                      */
1490 /* ----------------------------------------------------------- */
1491
1492 static int radio_querycap(struct file *file, void  *priv,
1493                           struct v4l2_capability *cap)
1494 {
1495         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1496
1497         strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1498         strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1499         strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1500
1501         cap->version = EM28XX_VERSION_CODE;
1502         cap->capabilities = V4L2_CAP_TUNER;
1503         return 0;
1504 }
1505
1506 static int radio_g_tuner(struct file *file, void *priv,
1507                          struct v4l2_tuner *t)
1508 {
1509         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1510
1511         if (unlikely(t->index > 0))
1512                 return -EINVAL;
1513
1514         strcpy(t->name, "Radio");
1515         t->type = V4L2_TUNER_RADIO;
1516
1517         mutex_lock(&dev->lock);
1518         em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1519         mutex_unlock(&dev->lock);
1520
1521         return 0;
1522 }
1523
1524 static int radio_enum_input(struct file *file, void *priv,
1525                             struct v4l2_input *i)
1526 {
1527         if (i->index != 0)
1528                 return -EINVAL;
1529         strcpy(i->name, "Radio");
1530         i->type = V4L2_INPUT_TYPE_TUNER;
1531
1532         return 0;
1533 }
1534
1535 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1536 {
1537         if (unlikely(a->index))
1538                 return -EINVAL;
1539
1540         strcpy(a->name, "Radio");
1541         return 0;
1542 }
1543
1544 static int radio_s_tuner(struct file *file, void *priv,
1545                          struct v4l2_tuner *t)
1546 {
1547         struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1548
1549         if (0 != t->index)
1550                 return -EINVAL;
1551
1552         mutex_lock(&dev->lock);
1553         em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1554         mutex_unlock(&dev->lock);
1555
1556         return 0;
1557 }
1558
1559 static int radio_s_audio(struct file *file, void *fh,
1560                          struct v4l2_audio *a)
1561 {
1562         return 0;
1563 }
1564
1565 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1566 {
1567         return 0;
1568 }
1569
1570 static int radio_queryctrl(struct file *file, void *priv,
1571                            struct v4l2_queryctrl *qc)
1572 {
1573         int i;
1574
1575         if (qc->id <  V4L2_CID_BASE ||
1576                 qc->id >= V4L2_CID_LASTP1)
1577                 return -EINVAL;
1578
1579         for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1580                 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1581                         memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1582                         return 0;
1583                 }
1584         }
1585
1586         return -EINVAL;
1587 }
1588
1589 /*
1590  * em28xx_v4l2_open()
1591  * inits the device and starts isoc transfer
1592  */
1593 static int em28xx_v4l2_open(struct file *filp)
1594 {
1595         int minor = video_devdata(filp)->minor;
1596         int errCode = 0, radio;
1597         struct em28xx *dev;
1598         enum v4l2_buf_type fh_type;
1599         struct em28xx_fh *fh;
1600
1601         dev = em28xx_get_device(minor, &fh_type, &radio);
1602
1603         if (NULL == dev)
1604                 return -ENODEV;
1605
1606         mutex_lock(&dev->lock);
1607
1608         em28xx_videodbg("open minor=%d type=%s users=%d\n",
1609                                 minor, v4l2_type_names[fh_type], dev->users);
1610
1611
1612         fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1613         if (!fh) {
1614                 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1615                 mutex_unlock(&dev->lock);
1616                 return -ENOMEM;
1617         }
1618         fh->dev = dev;
1619         fh->radio = radio;
1620         fh->type = fh_type;
1621         filp->private_data = fh;
1622
1623         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1624                 dev->width = norm_maxw(dev);
1625                 dev->height = norm_maxh(dev);
1626                 dev->hscale = 0;
1627                 dev->vscale = 0;
1628
1629                 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1630                 em28xx_set_alternate(dev);
1631                 em28xx_resolution_set(dev);
1632
1633                 /* Needed, since GPIO might have disabled power of
1634                    some i2c device
1635                  */
1636                 em28xx_wake_i2c(dev);
1637
1638         }
1639         if (fh->radio) {
1640                 em28xx_videodbg("video_open: setting radio device\n");
1641                 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1642         }
1643
1644         dev->users++;
1645
1646         videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1647                         NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1648                         sizeof(struct em28xx_buffer), fh);
1649
1650         mutex_unlock(&dev->lock);
1651
1652         return errCode;
1653 }
1654
1655 /*
1656  * em28xx_realease_resources()
1657  * unregisters the v4l2,i2c and usb devices
1658  * called when the device gets disconected or at module unload
1659 */
1660 void em28xx_release_analog_resources(struct em28xx *dev)
1661 {
1662
1663         /*FIXME: I2C IR should be disconnected */
1664
1665         if (dev->radio_dev) {
1666                 if (-1 != dev->radio_dev->minor)
1667                         video_unregister_device(dev->radio_dev);
1668                 else
1669                         video_device_release(dev->radio_dev);
1670                 dev->radio_dev = NULL;
1671         }
1672         if (dev->vbi_dev) {
1673                 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1674                             dev->vbi_dev->num);
1675                 if (-1 != dev->vbi_dev->minor)
1676                         video_unregister_device(dev->vbi_dev);
1677                 else
1678                         video_device_release(dev->vbi_dev);
1679                 dev->vbi_dev = NULL;
1680         }
1681         if (dev->vdev) {
1682                 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1683                             dev->vdev->num);
1684                 if (-1 != dev->vdev->minor)
1685                         video_unregister_device(dev->vdev);
1686                 else
1687                         video_device_release(dev->vdev);
1688                 dev->vdev = NULL;
1689         }
1690 }
1691
1692 /*
1693  * em28xx_v4l2_close()
1694  * stops streaming and deallocates all resources allocated by the v4l2
1695  * calls and ioctls
1696  */
1697 static int em28xx_v4l2_close(struct file *filp)
1698 {
1699         struct em28xx_fh *fh  = filp->private_data;
1700         struct em28xx    *dev = fh->dev;
1701         int              errCode;
1702
1703         em28xx_videodbg("users=%d\n", dev->users);
1704
1705
1706         mutex_lock(&dev->lock);
1707         if (res_check(fh))
1708                 res_free(fh);
1709
1710         if (dev->users == 1) {
1711                 videobuf_stop(&fh->vb_vidq);
1712                 videobuf_mmap_free(&fh->vb_vidq);
1713
1714                 /* the device is already disconnect,
1715                    free the remaining resources */
1716                 if (dev->state & DEV_DISCONNECTED) {
1717                         em28xx_release_resources(dev);
1718                         mutex_unlock(&dev->lock);
1719                         kfree(dev);
1720                         return 0;
1721                 }
1722
1723                 /* Save some power by putting tuner to sleep */
1724                 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1725
1726                 /* do this before setting alternate! */
1727                 em28xx_uninit_isoc(dev);
1728                 em28xx_set_mode(dev, EM28XX_SUSPEND);
1729
1730                 /* set alternate 0 */
1731                 dev->alt = 0;
1732                 em28xx_videodbg("setting alternate 0\n");
1733                 errCode = usb_set_interface(dev->udev, 0, 0);
1734                 if (errCode < 0) {
1735                         em28xx_errdev("cannot change alternate number to "
1736                                         "0 (error=%i)\n", errCode);
1737                 }
1738         }
1739         kfree(fh);
1740         dev->users--;
1741         wake_up_interruptible_nr(&dev->open, 1);
1742         mutex_unlock(&dev->lock);
1743         return 0;
1744 }
1745
1746 /*
1747  * em28xx_v4l2_read()
1748  * will allocate buffers when called for the first time
1749  */
1750 static ssize_t
1751 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1752                  loff_t *pos)
1753 {
1754         struct em28xx_fh *fh = filp->private_data;
1755         struct em28xx *dev = fh->dev;
1756         int rc;
1757
1758         rc = check_dev(dev);
1759         if (rc < 0)
1760                 return rc;
1761
1762         /* FIXME: read() is not prepared to allow changing the video
1763            resolution while streaming. Seems a bug at em28xx_set_fmt
1764          */
1765
1766         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1767                 mutex_lock(&dev->lock);
1768                 rc = res_get(fh);
1769                 mutex_unlock(&dev->lock);
1770
1771                 if (unlikely(rc < 0))
1772                         return rc;
1773
1774                 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1775                                         filp->f_flags & O_NONBLOCK);
1776         }
1777         return 0;
1778 }
1779
1780 /*
1781  * em28xx_v4l2_poll()
1782  * will allocate buffers when called for the first time
1783  */
1784 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1785 {
1786         struct em28xx_fh *fh = filp->private_data;
1787         struct em28xx *dev = fh->dev;
1788         int rc;
1789
1790         rc = check_dev(dev);
1791         if (rc < 0)
1792                 return rc;
1793
1794         mutex_lock(&dev->lock);
1795         rc = res_get(fh);
1796         mutex_unlock(&dev->lock);
1797
1798         if (unlikely(rc < 0))
1799                 return POLLERR;
1800
1801         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1802                 return POLLERR;
1803
1804         return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1805 }
1806
1807 /*
1808  * em28xx_v4l2_mmap()
1809  */
1810 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1811 {
1812         struct em28xx_fh *fh    = filp->private_data;
1813         struct em28xx    *dev   = fh->dev;
1814         int              rc;
1815
1816         rc = check_dev(dev);
1817         if (rc < 0)
1818                 return rc;
1819
1820         mutex_lock(&dev->lock);
1821         rc = res_get(fh);
1822         mutex_unlock(&dev->lock);
1823
1824         if (unlikely(rc < 0))
1825                 return rc;
1826
1827         rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1828
1829         em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1830                 (unsigned long)vma->vm_start,
1831                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1832                 rc);
1833
1834         return rc;
1835 }
1836
1837 static const struct v4l2_file_operations em28xx_v4l_fops = {
1838         .owner         = THIS_MODULE,
1839         .open          = em28xx_v4l2_open,
1840         .release       = em28xx_v4l2_close,
1841         .read          = em28xx_v4l2_read,
1842         .poll          = em28xx_v4l2_poll,
1843         .mmap          = em28xx_v4l2_mmap,
1844         .ioctl         = video_ioctl2,
1845 };
1846
1847 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1848         .vidioc_querycap            = vidioc_querycap,
1849         .vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1850         .vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1851         .vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1852         .vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1853         .vidioc_g_audio             = vidioc_g_audio,
1854         .vidioc_s_audio             = vidioc_s_audio,
1855         .vidioc_cropcap             = vidioc_cropcap,
1856
1857         .vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
1858         .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1859         .vidioc_s_fmt_sliced_vbi_cap   = vidioc_try_set_sliced_vbi_cap,
1860
1861         .vidioc_reqbufs             = vidioc_reqbufs,
1862         .vidioc_querybuf            = vidioc_querybuf,
1863         .vidioc_qbuf                = vidioc_qbuf,
1864         .vidioc_dqbuf               = vidioc_dqbuf,
1865         .vidioc_s_std               = vidioc_s_std,
1866         .vidioc_enum_input          = vidioc_enum_input,
1867         .vidioc_g_input             = vidioc_g_input,
1868         .vidioc_s_input             = vidioc_s_input,
1869         .vidioc_queryctrl           = vidioc_queryctrl,
1870         .vidioc_g_ctrl              = vidioc_g_ctrl,
1871         .vidioc_s_ctrl              = vidioc_s_ctrl,
1872         .vidioc_streamon            = vidioc_streamon,
1873         .vidioc_streamoff           = vidioc_streamoff,
1874         .vidioc_g_tuner             = vidioc_g_tuner,
1875         .vidioc_s_tuner             = vidioc_s_tuner,
1876         .vidioc_g_frequency         = vidioc_g_frequency,
1877         .vidioc_s_frequency         = vidioc_s_frequency,
1878 #ifdef CONFIG_VIDEO_ADV_DEBUG
1879         .vidioc_g_register          = vidioc_g_register,
1880         .vidioc_s_register          = vidioc_s_register,
1881         .vidioc_g_chip_ident        = vidioc_g_chip_ident,
1882 #endif
1883 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1884         .vidiocgmbuf                = vidiocgmbuf,
1885 #endif
1886 };
1887
1888 static const struct video_device em28xx_video_template = {
1889         .fops                       = &em28xx_v4l_fops,
1890         .release                    = video_device_release,
1891         .ioctl_ops                  = &video_ioctl_ops,
1892
1893         .minor                      = -1,
1894
1895         .tvnorms                    = V4L2_STD_ALL,
1896         .current_norm               = V4L2_STD_PAL,
1897 };
1898
1899 static const struct v4l2_file_operations radio_fops = {
1900         .owner         = THIS_MODULE,
1901         .open          = em28xx_v4l2_open,
1902         .release       = em28xx_v4l2_close,
1903         .ioctl         = video_ioctl2,
1904 };
1905
1906 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1907         .vidioc_querycap      = radio_querycap,
1908         .vidioc_g_tuner       = radio_g_tuner,
1909         .vidioc_enum_input    = radio_enum_input,
1910         .vidioc_g_audio       = radio_g_audio,
1911         .vidioc_s_tuner       = radio_s_tuner,
1912         .vidioc_s_audio       = radio_s_audio,
1913         .vidioc_s_input       = radio_s_input,
1914         .vidioc_queryctrl     = radio_queryctrl,
1915         .vidioc_g_ctrl        = vidioc_g_ctrl,
1916         .vidioc_s_ctrl        = vidioc_s_ctrl,
1917         .vidioc_g_frequency   = vidioc_g_frequency,
1918         .vidioc_s_frequency   = vidioc_s_frequency,
1919 #ifdef CONFIG_VIDEO_ADV_DEBUG
1920         .vidioc_g_register    = vidioc_g_register,
1921         .vidioc_s_register    = vidioc_s_register,
1922 #endif
1923 };
1924
1925 static struct video_device em28xx_radio_template = {
1926         .name                 = "em28xx-radio",
1927         .fops                 = &radio_fops,
1928         .ioctl_ops            = &radio_ioctl_ops,
1929         .minor                = -1,
1930 };
1931
1932 /******************************** usb interface ******************************/
1933
1934
1935
1936 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1937                                              const struct video_device *template,
1938                                              const char *type_name)
1939 {
1940         struct video_device *vfd;
1941
1942         vfd = video_device_alloc();
1943         if (NULL == vfd)
1944                 return NULL;
1945         *vfd = *template;
1946         vfd->minor   = -1;
1947         vfd->parent = &dev->udev->dev;
1948         vfd->release = video_device_release;
1949         vfd->debug = video_debug;
1950
1951         snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1952                  dev->name, type_name);
1953
1954         return vfd;
1955 }
1956
1957 int em28xx_register_analog_devices(struct em28xx *dev)
1958 {
1959       u8 val;
1960         int ret;
1961
1962         printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1963                 dev->name,
1964                 (EM28XX_VERSION_CODE >> 16) & 0xff,
1965                 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1966
1967         /* set default norm */
1968         dev->norm = em28xx_video_template.current_norm;
1969         dev->width = norm_maxw(dev);
1970         dev->height = norm_maxh(dev);
1971         dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1972         dev->hscale = 0;
1973         dev->vscale = 0;
1974         dev->ctl_input = 0;
1975
1976         /* Analog specific initialization */
1977         dev->format = &format[0];
1978         video_mux(dev, dev->ctl_input);
1979
1980         /* Audio defaults */
1981         dev->mute = 1;
1982         dev->volume = 0x1f;
1983
1984         /* enable vbi capturing */
1985
1986 /*      em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
1987        val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1988        em28xx_write_reg(dev, EM28XX_R0F_XCLK, (EM28XX_XCLK_AUDIO_UNMUTE | val));
1989         em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
1990
1991         em28xx_set_outfmt(dev);
1992         em28xx_colorlevels_set_default(dev);
1993         em28xx_compression_disable(dev);
1994
1995         /* allocate and fill video video_device struct */
1996         dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1997         if (!dev->vdev) {
1998                 em28xx_errdev("cannot allocate video_device.\n");
1999                 return -ENODEV;
2000         }
2001
2002         /* register v4l2 video video_device */
2003         ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2004                                        video_nr[dev->devno]);
2005         if (ret) {
2006                 em28xx_errdev("unable to register video device (error=%i).\n",
2007                               ret);
2008                 return ret;
2009         }
2010
2011         /* Allocate and fill vbi video_device struct */
2012         dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2013
2014         /* register v4l2 vbi video_device */
2015         ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2016                                         vbi_nr[dev->devno]);
2017         if (ret < 0) {
2018                 em28xx_errdev("unable to register vbi device\n");
2019                 return ret;
2020         }
2021
2022         if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2023                 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2024                 if (!dev->radio_dev) {
2025                         em28xx_errdev("cannot allocate video_device.\n");
2026                         return -ENODEV;
2027                 }
2028                 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2029                                             radio_nr[dev->devno]);
2030                 if (ret < 0) {
2031                         em28xx_errdev("can't register radio device\n");
2032                         return ret;
2033                 }
2034                 em28xx_info("Registered radio device as /dev/radio%d\n",
2035                             dev->radio_dev->num);
2036         }
2037
2038         em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2039                                 dev->vdev->num, dev->vbi_dev->num);
2040
2041         return 0;
2042 }