Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[sfrench/cifs-2.6.git] / drivers / media / video / videodev.c
1 /*
2  * Video capture interface for Linux version 2
3  *
4  *      A generic video device interface for the LINUX operating system
5  *      using a set of device structures/vectors for low level operations.
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  * Authors:     Alan Cox, <alan@redhat.com> (version 1)
13  *              Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
14  *
15  * Fixes:       20000516  Claudio Matsuoka <claudio@conectiva.com>
16  *              - Added procfs support
17  */
18
19 #define dbgarg(cmd, fmt, arg...) \
20                 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {                \
21                         printk (KERN_DEBUG "%s: ",  vfd->name);         \
22                         v4l_printk_ioctl(cmd);                          \
23                         printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \
24                 }
25
26 #define dbgarg2(fmt, arg...) \
27                 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)                  \
28                         printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
29
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/smp_lock.h>
34 #include <linux/mm.h>
35 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/kmod.h>
39 #include <linux/slab.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42
43 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
44 #include <linux/videodev2.h>
45
46 #ifdef CONFIG_VIDEO_V4L1
47 #include <linux/videodev.h>
48 #endif
49 #include <media/v4l2-common.h>
50
51 #define VIDEO_NUM_DEVICES       256
52 #define VIDEO_NAME              "video4linux"
53
54 /*
55  *      sysfs stuff
56  */
57
58 static ssize_t show_name(struct class_device *cd, char *buf)
59 {
60         struct video_device *vfd = container_of(cd, struct video_device,
61                                                                 class_dev);
62         return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
63 }
64
65 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
66
67 struct video_device *video_device_alloc(void)
68 {
69         struct video_device *vfd;
70
71         vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
72         return vfd;
73 }
74
75 void video_device_release(struct video_device *vfd)
76 {
77         kfree(vfd);
78 }
79
80 static void video_release(struct class_device *cd)
81 {
82         struct video_device *vfd = container_of(cd, struct video_device,
83                                                                 class_dev);
84
85 #if 1
86         /* needed until all drivers are fixed */
87         if (!vfd->release)
88                 return;
89 #endif
90         vfd->release(vfd);
91 }
92
93 static struct class video_class = {
94         .name    = VIDEO_NAME,
95         .release = video_release,
96 };
97
98 /*
99  *      Active devices
100  */
101
102 static struct video_device *video_device[VIDEO_NUM_DEVICES];
103 static DEFINE_MUTEX(videodev_lock);
104
105 struct video_device* video_devdata(struct file *file)
106 {
107         return video_device[iminor(file->f_path.dentry->d_inode)];
108 }
109
110 /*
111  *      Open a video device - FIXME: Obsoleted
112  */
113 static int video_open(struct inode *inode, struct file *file)
114 {
115         unsigned int minor = iminor(inode);
116         int err = 0;
117         struct video_device *vfl;
118         const struct file_operations *old_fops;
119
120         if(minor>=VIDEO_NUM_DEVICES)
121                 return -ENODEV;
122         mutex_lock(&videodev_lock);
123         vfl=video_device[minor];
124         if(vfl==NULL) {
125                 mutex_unlock(&videodev_lock);
126                 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
127                 mutex_lock(&videodev_lock);
128                 vfl=video_device[minor];
129                 if (vfl==NULL) {
130                         mutex_unlock(&videodev_lock);
131                         return -ENODEV;
132                 }
133         }
134         old_fops = file->f_op;
135         file->f_op = fops_get(vfl->fops);
136         if(file->f_op->open)
137                 err = file->f_op->open(inode,file);
138         if (err) {
139                 fops_put(file->f_op);
140                 file->f_op = fops_get(old_fops);
141         }
142         fops_put(old_fops);
143         mutex_unlock(&videodev_lock);
144         return err;
145 }
146
147 /*
148  * helper function -- handles userspace copying for ioctl arguments
149  */
150
151 #ifdef __OLD_VIDIOC_
152 static unsigned int
153 video_fix_command(unsigned int cmd)
154 {
155         switch (cmd) {
156         case VIDIOC_OVERLAY_OLD:
157                 cmd = VIDIOC_OVERLAY;
158                 break;
159         case VIDIOC_S_PARM_OLD:
160                 cmd = VIDIOC_S_PARM;
161                 break;
162         case VIDIOC_S_CTRL_OLD:
163                 cmd = VIDIOC_S_CTRL;
164                 break;
165         case VIDIOC_G_AUDIO_OLD:
166                 cmd = VIDIOC_G_AUDIO;
167                 break;
168         case VIDIOC_G_AUDOUT_OLD:
169                 cmd = VIDIOC_G_AUDOUT;
170                 break;
171         case VIDIOC_CROPCAP_OLD:
172                 cmd = VIDIOC_CROPCAP;
173                 break;
174         }
175         return cmd;
176 }
177 #endif
178
179 /*
180  * Obsolete usercopy function - Should be removed soon
181  */
182 int
183 video_usercopy(struct inode *inode, struct file *file,
184                unsigned int cmd, unsigned long arg,
185                int (*func)(struct inode *inode, struct file *file,
186                            unsigned int cmd, void *arg))
187 {
188         char    sbuf[128];
189         void    *mbuf = NULL;
190         void    *parg = NULL;
191         int     err  = -EINVAL;
192         int     is_ext_ctrl;
193         size_t  ctrls_size = 0;
194         void __user *user_ptr = NULL;
195
196 #ifdef __OLD_VIDIOC_
197         cmd = video_fix_command(cmd);
198 #endif
199         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
200                        cmd == VIDIOC_TRY_EXT_CTRLS);
201
202         /*  Copy arguments into temp kernel buffer  */
203         switch (_IOC_DIR(cmd)) {
204         case _IOC_NONE:
205                 parg = NULL;
206                 break;
207         case _IOC_READ:
208         case _IOC_WRITE:
209         case (_IOC_WRITE | _IOC_READ):
210                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
211                         parg = sbuf;
212                 } else {
213                         /* too big to allocate from stack */
214                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
215                         if (NULL == mbuf)
216                                 return -ENOMEM;
217                         parg = mbuf;
218                 }
219
220                 err = -EFAULT;
221                 if (_IOC_DIR(cmd) & _IOC_WRITE)
222                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
223                                 goto out;
224                 break;
225         }
226         if (is_ext_ctrl) {
227                 struct v4l2_ext_controls *p = parg;
228
229                 /* In case of an error, tell the caller that it wasn't
230                    a specific control that caused it. */
231                 p->error_idx = p->count;
232                 user_ptr = (void __user *)p->controls;
233                 if (p->count) {
234                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
235                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
236                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
237                         err = -ENOMEM;
238                         if (NULL == mbuf)
239                                 goto out_ext_ctrl;
240                         err = -EFAULT;
241                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
242                                 goto out_ext_ctrl;
243                         p->controls = mbuf;
244                 }
245         }
246
247         /* call driver */
248         err = func(inode, file, cmd, parg);
249         if (err == -ENOIOCTLCMD)
250                 err = -EINVAL;
251         if (is_ext_ctrl) {
252                 struct v4l2_ext_controls *p = parg;
253
254                 p->controls = (void *)user_ptr;
255                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
256                         err = -EFAULT;
257                 goto out_ext_ctrl;
258         }
259         if (err < 0)
260                 goto out;
261
262 out_ext_ctrl:
263         /*  Copy results into user buffer  */
264         switch (_IOC_DIR(cmd))
265         {
266         case _IOC_READ:
267         case (_IOC_WRITE | _IOC_READ):
268                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
269                         err = -EFAULT;
270                 break;
271         }
272
273 out:
274         kfree(mbuf);
275         return err;
276 }
277
278 /*
279  * open/release helper functions -- handle exclusive opens
280  * Should be removed soon
281  */
282 int video_exclusive_open(struct inode *inode, struct file *file)
283 {
284         struct  video_device *vfl = video_devdata(file);
285         int retval = 0;
286
287         mutex_lock(&vfl->lock);
288         if (vfl->users) {
289                 retval = -EBUSY;
290         } else {
291                 vfl->users++;
292         }
293         mutex_unlock(&vfl->lock);
294         return retval;
295 }
296
297 int video_exclusive_release(struct inode *inode, struct file *file)
298 {
299         struct  video_device *vfl = video_devdata(file);
300
301         vfl->users--;
302         return 0;
303 }
304
305 static char *v4l2_memory_names[] = {
306         [V4L2_MEMORY_MMAP]    = "mmap",
307         [V4L2_MEMORY_USERPTR] = "userptr",
308         [V4L2_MEMORY_OVERLAY] = "overlay",
309 };
310
311
312 /* FIXME: Those stuff are replicated also on v4l2-common.c */
313 static char *v4l2_type_names_FIXME[] = {
314         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "video-cap",
315         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "video-over",
316         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "video-out",
317         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
318         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
319         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
320         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
321         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
322         [V4L2_BUF_TYPE_PRIVATE]            = "private",
323 };
324
325 static char *v4l2_field_names_FIXME[] = {
326         [V4L2_FIELD_ANY]        = "any",
327         [V4L2_FIELD_NONE]       = "none",
328         [V4L2_FIELD_TOP]        = "top",
329         [V4L2_FIELD_BOTTOM]     = "bottom",
330         [V4L2_FIELD_INTERLACED] = "interlaced",
331         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
332         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
333         [V4L2_FIELD_ALTERNATE]  = "alternate",
334         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
335         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
336 };
337
338 #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
339
340 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
341                                         struct v4l2_buffer *p)
342 {
343         struct v4l2_timecode *tc=&p->timecode;
344
345         dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
346                 "bytesused=%d, flags=0x%08d, "
347                 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
348                         (p->timestamp.tv_sec/3600),
349                         (int)(p->timestamp.tv_sec/60)%60,
350                         (int)(p->timestamp.tv_sec%60),
351                         p->timestamp.tv_usec,
352                         p->index,
353                         prt_names(p->type,v4l2_type_names_FIXME),
354                         p->bytesused,p->flags,
355                         p->field,p->sequence,
356                         prt_names(p->memory,v4l2_memory_names),
357                         p->m.userptr, p->length);
358         dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
359                 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
360                         tc->hours,tc->minutes,tc->seconds,
361                         tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
362 }
363
364 static inline void dbgrect(struct video_device *vfd, char *s,
365                                                         struct v4l2_rect *r)
366 {
367         dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
368                                                 r->width, r->height);
369 };
370
371 static inline void v4l_print_pix_fmt (struct video_device *vfd,
372                                                 struct v4l2_pix_format *fmt)
373 {
374         dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
375                 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
376                 fmt->width,fmt->height,
377                 (fmt->pixelformat & 0xff),
378                 (fmt->pixelformat >>  8) & 0xff,
379                 (fmt->pixelformat >> 16) & 0xff,
380                 (fmt->pixelformat >> 24) & 0xff,
381                 prt_names(fmt->field,v4l2_field_names_FIXME),
382                 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
383 };
384
385
386 static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
387 {
388         switch (type) {
389         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
390                 if (vfd->vidioc_try_fmt_cap)
391                         return (0);
392                 break;
393         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
394                 if (vfd->vidioc_try_fmt_overlay)
395                         return (0);
396                 break;
397         case V4L2_BUF_TYPE_VBI_CAPTURE:
398                 if (vfd->vidioc_try_fmt_vbi)
399                         return (0);
400                 break;
401         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
402                 if (vfd->vidioc_try_fmt_vbi_output)
403                         return (0);
404                 break;
405         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
406                 if (vfd->vidioc_try_fmt_vbi_capture)
407                         return (0);
408                 break;
409         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
410                 if (vfd->vidioc_try_fmt_video_output)
411                         return (0);
412                 break;
413         case V4L2_BUF_TYPE_VBI_OUTPUT:
414                 if (vfd->vidioc_try_fmt_vbi_output)
415                         return (0);
416                 break;
417         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
418                 if (vfd->vidioc_try_fmt_output_overlay)
419                         return (0);
420                 break;
421         case V4L2_BUF_TYPE_PRIVATE:
422                 if (vfd->vidioc_try_fmt_type_private)
423                         return (0);
424                 break;
425         }
426         return (-EINVAL);
427 }
428
429 static int __video_do_ioctl(struct inode *inode, struct file *file,
430                 unsigned int cmd, void *arg)
431 {
432         struct video_device *vfd = video_devdata(file);
433         void                 *fh = file->private_data;
434         int                  ret = -EINVAL;
435
436         if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
437                                 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
438                 v4l_print_ioctl(vfd->name, cmd);
439         }
440
441         if (_IOC_TYPE(cmd)=='v')
442                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
443                                                 __video_do_ioctl);
444
445         switch(cmd) {
446         /* --- capabilities ------------------------------------------ */
447         case VIDIOC_QUERYCAP:
448         {
449                 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
450                 memset(cap, 0, sizeof(*cap));
451
452                 if (!vfd->vidioc_querycap)
453                         break;
454
455                 ret=vfd->vidioc_querycap(file, fh, cap);
456                 if (!ret)
457                         dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
458                                         "version=0x%08x, "
459                                         "capabilities=0x%08x\n",
460                                         cap->driver,cap->card,cap->bus_info,
461                                         cap->version,
462                                         cap->capabilities);
463                 break;
464         }
465
466         /* --- priority ------------------------------------------ */
467         case VIDIOC_G_PRIORITY:
468         {
469                 enum v4l2_priority *p=arg;
470
471                 if (!vfd->vidioc_g_priority)
472                         break;
473                 ret=vfd->vidioc_g_priority(file, fh, p);
474                 if (!ret)
475                         dbgarg(cmd, "priority is %d\n", *p);
476                 break;
477         }
478         case VIDIOC_S_PRIORITY:
479         {
480                 enum v4l2_priority *p=arg;
481
482                 if (!vfd->vidioc_s_priority)
483                         break;
484                 dbgarg(cmd, "setting priority to %d\n", *p);
485                 ret=vfd->vidioc_s_priority(file, fh, *p);
486                 break;
487         }
488
489         /* --- capture ioctls ---------------------------------------- */
490         case VIDIOC_ENUM_FMT:
491         {
492                 struct v4l2_fmtdesc *f = arg;
493                 enum v4l2_buf_type type;
494                 unsigned int index;
495
496                 index = f->index;
497                 type  = f->type;
498                 memset(f,0,sizeof(*f));
499                 f->index = index;
500                 f->type  = type;
501
502                 switch (type) {
503                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
504                         if (vfd->vidioc_enum_fmt_cap)
505                                 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
506                         break;
507                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
508                         if (vfd->vidioc_enum_fmt_overlay)
509                                 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
510                         break;
511                 case V4L2_BUF_TYPE_VBI_CAPTURE:
512                         if (vfd->vidioc_enum_fmt_vbi)
513                                 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
514                         break;
515                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
516                         if (vfd->vidioc_enum_fmt_vbi_output)
517                                 ret=vfd->vidioc_enum_fmt_vbi_output(file,
518                                                                 fh, f);
519                         break;
520                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
521                         if (vfd->vidioc_enum_fmt_vbi_capture)
522                                 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
523                                                                 fh, f);
524                         break;
525                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
526                         if (vfd->vidioc_enum_fmt_video_output)
527                                 ret=vfd->vidioc_enum_fmt_video_output(file,
528                                                                 fh, f);
529                         break;
530                 case V4L2_BUF_TYPE_VBI_OUTPUT:
531                         if (vfd->vidioc_enum_fmt_vbi_output)
532                                 ret=vfd->vidioc_enum_fmt_vbi_output(file,
533                                                                 fh, f);
534                         break;
535                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
536                         if (vfd->vidioc_enum_fmt_output_overlay)
537                                 ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
538                         break;
539                 case V4L2_BUF_TYPE_PRIVATE:
540                         if (vfd->vidioc_enum_fmt_type_private)
541                                 ret=vfd->vidioc_enum_fmt_type_private(file,
542                                                                 fh, f);
543                         break;
544                 }
545                 if (!ret)
546                         dbgarg (cmd, "index=%d, type=%d, flags=%d, "
547                                         "pixelformat=%c%c%c%c, description='%s'\n",
548                                         f->index, f->type, f->flags,
549                                         (f->pixelformat & 0xff),
550                                         (f->pixelformat >>  8) & 0xff,
551                                         (f->pixelformat >> 16) & 0xff,
552                                         (f->pixelformat >> 24) & 0xff,
553                                         f->description);
554                 break;
555         }
556         case VIDIOC_G_FMT:
557         {
558                 struct v4l2_format *f = (struct v4l2_format *)arg;
559                 enum v4l2_buf_type type=f->type;
560
561                 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
562                 f->type=type;
563
564                 /* FIXME: Should be one dump per type */
565                 dbgarg (cmd, "type=%s\n", prt_names(type,
566                                         v4l2_type_names_FIXME));
567
568                 switch (type) {
569                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
570                         if (vfd->vidioc_g_fmt_cap)
571                                 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
572                         if (!ret)
573                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
574                         break;
575                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
576                         if (vfd->vidioc_g_fmt_overlay)
577                                 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
578                         break;
579                 case V4L2_BUF_TYPE_VBI_CAPTURE:
580                         if (vfd->vidioc_g_fmt_vbi)
581                                 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
582                         break;
583                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
584                         if (vfd->vidioc_g_fmt_vbi_output)
585                                 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
586                         break;
587                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
588                         if (vfd->vidioc_g_fmt_vbi_capture)
589                                 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
590                         break;
591                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
592                         if (vfd->vidioc_g_fmt_video_output)
593                                 ret=vfd->vidioc_g_fmt_video_output(file,
594                                                                 fh, f);
595                         break;
596                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
597                         if (vfd->vidioc_g_fmt_output_overlay)
598                                 ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
599                         break;
600                 case V4L2_BUF_TYPE_VBI_OUTPUT:
601                         if (vfd->vidioc_g_fmt_vbi_output)
602                                 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
603                         break;
604                 case V4L2_BUF_TYPE_PRIVATE:
605                         if (vfd->vidioc_g_fmt_type_private)
606                                 ret=vfd->vidioc_g_fmt_type_private(file,
607                                                                 fh, f);
608                         break;
609                 }
610
611                 break;
612         }
613         case VIDIOC_S_FMT:
614         {
615                 struct v4l2_format *f = (struct v4l2_format *)arg;
616
617                 /* FIXME: Should be one dump per type */
618                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
619                                         v4l2_type_names_FIXME));
620
621                 switch (f->type) {
622                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
623                         v4l_print_pix_fmt(vfd,&f->fmt.pix);
624                         if (vfd->vidioc_s_fmt_cap)
625                                 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
626                         break;
627                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
628                         if (vfd->vidioc_s_fmt_overlay)
629                                 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
630                         break;
631                 case V4L2_BUF_TYPE_VBI_CAPTURE:
632                         if (vfd->vidioc_s_fmt_vbi)
633                                 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
634                         break;
635                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
636                         if (vfd->vidioc_s_fmt_vbi_output)
637                                 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
638                         break;
639                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
640                         if (vfd->vidioc_s_fmt_vbi_capture)
641                                 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
642                         break;
643                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
644                         if (vfd->vidioc_s_fmt_video_output)
645                                 ret=vfd->vidioc_s_fmt_video_output(file,
646                                                                 fh, f);
647                         break;
648                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
649                         if (vfd->vidioc_s_fmt_output_overlay)
650                                 ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
651                         break;
652                 case V4L2_BUF_TYPE_VBI_OUTPUT:
653                         if (vfd->vidioc_s_fmt_vbi_output)
654                                 ret=vfd->vidioc_s_fmt_vbi_output(file,
655                                                                 fh, f);
656                         break;
657                 case V4L2_BUF_TYPE_PRIVATE:
658                         if (vfd->vidioc_s_fmt_type_private)
659                                 ret=vfd->vidioc_s_fmt_type_private(file,
660                                                                 fh, f);
661                         break;
662                 }
663                 break;
664         }
665         case VIDIOC_TRY_FMT:
666         {
667                 struct v4l2_format *f = (struct v4l2_format *)arg;
668
669                 /* FIXME: Should be one dump per type */
670                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
671                                                 v4l2_type_names_FIXME));
672                 switch (f->type) {
673                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
674                         if (vfd->vidioc_try_fmt_cap)
675                                 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
676                         if (!ret)
677                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
678                         break;
679                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
680                         if (vfd->vidioc_try_fmt_overlay)
681                                 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
682                         break;
683                 case V4L2_BUF_TYPE_VBI_CAPTURE:
684                         if (vfd->vidioc_try_fmt_vbi)
685                                 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
686                         break;
687                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
688                         if (vfd->vidioc_try_fmt_vbi_output)
689                                 ret=vfd->vidioc_try_fmt_vbi_output(file,
690                                                                 fh, f);
691                         break;
692                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
693                         if (vfd->vidioc_try_fmt_vbi_capture)
694                                 ret=vfd->vidioc_try_fmt_vbi_capture(file,
695                                                                 fh, f);
696                         break;
697                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
698                         if (vfd->vidioc_try_fmt_video_output)
699                                 ret=vfd->vidioc_try_fmt_video_output(file,
700                                                                 fh, f);
701                         break;
702                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
703                         if (vfd->vidioc_try_fmt_output_overlay)
704                                 ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
705                         break;
706                 case V4L2_BUF_TYPE_VBI_OUTPUT:
707                         if (vfd->vidioc_try_fmt_vbi_output)
708                                 ret=vfd->vidioc_try_fmt_vbi_output(file,
709                                                                 fh, f);
710                         break;
711                 case V4L2_BUF_TYPE_PRIVATE:
712                         if (vfd->vidioc_try_fmt_type_private)
713                                 ret=vfd->vidioc_try_fmt_type_private(file,
714                                                                 fh, f);
715                         break;
716                 }
717
718                 break;
719         }
720         /* FIXME: Those buf reqs could be handled here,
721            with some changes on videobuf to allow its header to be included at
722            videodev2.h or being merged at videodev2.
723          */
724         case VIDIOC_REQBUFS:
725         {
726                 struct v4l2_requestbuffers *p=arg;
727
728                 if (!vfd->vidioc_reqbufs)
729                         break;
730                 ret = check_fmt (vfd, p->type);
731                 if (ret)
732                         break;
733
734                 ret=vfd->vidioc_reqbufs(file, fh, p);
735                 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
736                                 p->count,
737                                 prt_names(p->type,v4l2_type_names_FIXME),
738                                 prt_names(p->memory,v4l2_memory_names));
739                 break;
740         }
741         case VIDIOC_QUERYBUF:
742         {
743                 struct v4l2_buffer *p=arg;
744
745                 if (!vfd->vidioc_querybuf)
746                         break;
747                 ret = check_fmt (vfd, p->type);
748                 if (ret)
749                         break;
750
751                 ret=vfd->vidioc_querybuf(file, fh, p);
752                 if (!ret)
753                         dbgbuf(cmd,vfd,p);
754                 break;
755         }
756         case VIDIOC_QBUF:
757         {
758                 struct v4l2_buffer *p=arg;
759
760                 if (!vfd->vidioc_qbuf)
761                         break;
762                 ret = check_fmt (vfd, p->type);
763                 if (ret)
764                         break;
765
766                 ret=vfd->vidioc_qbuf(file, fh, p);
767                 if (!ret)
768                         dbgbuf(cmd,vfd,p);
769                 break;
770         }
771         case VIDIOC_DQBUF:
772         {
773                 struct v4l2_buffer *p=arg;
774                 if (!vfd->vidioc_dqbuf)
775                         break;
776                 ret = check_fmt (vfd, p->type);
777                 if (ret)
778                         break;
779
780                 ret=vfd->vidioc_dqbuf(file, fh, p);
781                 if (!ret)
782                         dbgbuf(cmd,vfd,p);
783                 break;
784         }
785         case VIDIOC_OVERLAY:
786         {
787                 int *i = arg;
788
789                 if (!vfd->vidioc_overlay)
790                         break;
791                 dbgarg (cmd, "value=%d\n",*i);
792                 ret=vfd->vidioc_overlay(file, fh, *i);
793                 break;
794         }
795 #ifdef CONFIG_VIDEO_V4L1_COMPAT
796         /* --- streaming capture ------------------------------------- */
797         case VIDIOCGMBUF:
798         {
799                 struct video_mbuf *p=arg;
800
801                 memset(p,0,sizeof(p));
802
803                 if (!vfd->vidiocgmbuf)
804                         break;
805                 ret=vfd->vidiocgmbuf(file, fh, p);
806                 if (!ret)
807                         dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
808                                                 p->size, p->frames,
809                                                 (unsigned long)p->offsets);
810                 break;
811         }
812 #endif
813         case VIDIOC_G_FBUF:
814         {
815                 struct v4l2_framebuffer *p=arg;
816                 if (!vfd->vidioc_g_fbuf)
817                         break;
818                 ret=vfd->vidioc_g_fbuf(file, fh, arg);
819                 if (!ret) {
820                         dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
821                                         p->capability,p->flags,
822                                         (unsigned long)p->base);
823                         v4l_print_pix_fmt (vfd, &p->fmt);
824                 }
825                 break;
826         }
827         case VIDIOC_S_FBUF:
828         {
829                 struct v4l2_framebuffer *p=arg;
830                 if (!vfd->vidioc_s_fbuf)
831                         break;
832
833                 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
834                                 p->capability,p->flags,(unsigned long)p->base);
835                 v4l_print_pix_fmt (vfd, &p->fmt);
836                 ret=vfd->vidioc_s_fbuf(file, fh, arg);
837
838                 break;
839         }
840         case VIDIOC_STREAMON:
841         {
842                 enum v4l2_buf_type i = *(int *)arg;
843                 if (!vfd->vidioc_streamon)
844                         break;
845                 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
846                 ret=vfd->vidioc_streamon(file, fh,i);
847                 break;
848         }
849         case VIDIOC_STREAMOFF:
850         {
851                 enum v4l2_buf_type i = *(int *)arg;
852
853                 if (!vfd->vidioc_streamoff)
854                         break;
855                 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
856                 ret=vfd->vidioc_streamoff(file, fh, i);
857                 break;
858         }
859         /* ---------- tv norms ---------- */
860         case VIDIOC_ENUMSTD:
861         {
862                 struct v4l2_standard *p = arg;
863                 v4l2_std_id id = vfd->tvnorms,curr_id=0;
864                 unsigned int index = p->index,i;
865
866                 if (index<0) {
867                         ret=-EINVAL;
868                         break;
869                 }
870
871                 /* Return norm array on a canonical way */
872                 for (i=0;i<= index && id; i++) {
873                         if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
874                                 curr_id = V4L2_STD_PAL;
875                         } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
876                                 curr_id = V4L2_STD_PAL_BG;
877                         } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
878                                 curr_id = V4L2_STD_PAL_DK;
879                         } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
880                                 curr_id = V4L2_STD_PAL_B;
881                         } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
882                                 curr_id = V4L2_STD_PAL_B1;
883                         } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
884                                 curr_id = V4L2_STD_PAL_G;
885                         } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
886                                 curr_id = V4L2_STD_PAL_H;
887                         } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
888                                 curr_id = V4L2_STD_PAL_I;
889                         } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
890                                 curr_id = V4L2_STD_PAL_D;
891                         } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
892                                 curr_id = V4L2_STD_PAL_D1;
893                         } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
894                                 curr_id = V4L2_STD_PAL_K;
895                         } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
896                                 curr_id = V4L2_STD_PAL_M;
897                         } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
898                                 curr_id = V4L2_STD_PAL_N;
899                         } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
900                                 curr_id = V4L2_STD_PAL_Nc;
901                         } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
902                                 curr_id = V4L2_STD_PAL_60;
903                         } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
904                                 curr_id = V4L2_STD_NTSC;
905                         } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
906                                 curr_id = V4L2_STD_NTSC_M;
907                         } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
908                                 curr_id = V4L2_STD_NTSC_M_JP;
909                         } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
910                                 curr_id = V4L2_STD_NTSC_443;
911                         } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
912                                 curr_id = V4L2_STD_NTSC_M_KR;
913                         } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
914                                 curr_id = V4L2_STD_SECAM;
915                         } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
916                                 curr_id = V4L2_STD_SECAM_DK;
917                         } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
918                                 curr_id = V4L2_STD_SECAM_B;
919                         } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
920                                 curr_id = V4L2_STD_SECAM_D;
921                         } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
922                                 curr_id = V4L2_STD_SECAM_G;
923                         } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
924                                 curr_id = V4L2_STD_SECAM_H;
925                         } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
926                                 curr_id = V4L2_STD_SECAM_K;
927                         } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
928                                 curr_id = V4L2_STD_SECAM_K1;
929                         } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
930                                 curr_id = V4L2_STD_SECAM_L;
931                         } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
932                                 curr_id = V4L2_STD_SECAM_LC;
933                         } else {
934                                 break;
935                         }
936                         id &= ~curr_id;
937                 }
938                 if (i<=index)
939                         return -EINVAL;
940
941                 v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
942                 p->index = index;
943
944                 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
945                                 "framelines=%d\n", p->index,
946                                 (unsigned long long)p->id, p->name,
947                                 p->frameperiod.numerator,
948                                 p->frameperiod.denominator,
949                                 p->framelines);
950
951                 ret=0;
952                 break;
953         }
954         case VIDIOC_G_STD:
955         {
956                 v4l2_std_id *id = arg;
957
958                 *id = vfd->current_norm;
959
960                 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
961
962                 ret=0;
963                 break;
964         }
965         case VIDIOC_S_STD:
966         {
967                 v4l2_std_id *id = arg,norm;
968
969                 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
970
971                 norm = (*id) & vfd->tvnorms;
972                 if ( vfd->tvnorms && !norm)     /* Check if std is supported */
973                         break;
974
975                 /* Calls the specific handler */
976                 if (vfd->vidioc_s_std)
977                         ret=vfd->vidioc_s_std(file, fh, &norm);
978                 else
979                         ret=-EINVAL;
980
981                 /* Updates standard information */
982                 if (ret>=0)
983                         vfd->current_norm=norm;
984
985                 break;
986         }
987         case VIDIOC_QUERYSTD:
988         {
989                 v4l2_std_id *p=arg;
990
991                 if (!vfd->vidioc_querystd)
992                         break;
993                 ret=vfd->vidioc_querystd(file, fh, arg);
994                 if (!ret)
995                         dbgarg (cmd, "detected std=%Lu\n",
996                                                 (unsigned long long)*p);
997                 break;
998         }
999         /* ------ input switching ---------- */
1000         /* FIXME: Inputs can be handled inside videodev2 */
1001         case VIDIOC_ENUMINPUT:
1002         {
1003                 struct v4l2_input *p=arg;
1004                 int i=p->index;
1005
1006                 if (!vfd->vidioc_enum_input)
1007                         break;
1008                 memset(p, 0, sizeof(*p));
1009                 p->index=i;
1010
1011                 ret=vfd->vidioc_enum_input(file, fh, p);
1012                 if (!ret)
1013                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1014                                         "audioset=%d, "
1015                                         "tuner=%d, std=%Ld, status=%d\n",
1016                                         p->index,p->name,p->type,p->audioset,
1017                                         p->tuner,
1018                                         (unsigned long long)p->std,
1019                                         p->status);
1020                 break;
1021         }
1022         case VIDIOC_G_INPUT:
1023         {
1024                 unsigned int *i = arg;
1025
1026                 if (!vfd->vidioc_g_input)
1027                         break;
1028                 ret=vfd->vidioc_g_input(file, fh, i);
1029                 if (!ret)
1030                         dbgarg (cmd, "value=%d\n",*i);
1031                 break;
1032         }
1033         case VIDIOC_S_INPUT:
1034         {
1035                 unsigned int *i = arg;
1036
1037                 if (!vfd->vidioc_s_input)
1038                         break;
1039                 dbgarg (cmd, "value=%d\n",*i);
1040                 ret=vfd->vidioc_s_input(file, fh, *i);
1041                 break;
1042         }
1043
1044         /* ------ output switching ---------- */
1045         case VIDIOC_G_OUTPUT:
1046         {
1047                 unsigned int *i = arg;
1048
1049                 if (!vfd->vidioc_g_output)
1050                         break;
1051                 ret=vfd->vidioc_g_output(file, fh, i);
1052                 if (!ret)
1053                         dbgarg (cmd, "value=%d\n",*i);
1054                 break;
1055         }
1056         case VIDIOC_S_OUTPUT:
1057         {
1058                 unsigned int *i = arg;
1059
1060                 if (!vfd->vidioc_s_output)
1061                         break;
1062                 dbgarg (cmd, "value=%d\n",*i);
1063                 ret=vfd->vidioc_s_output(file, fh, *i);
1064                 break;
1065         }
1066
1067         /* --- controls ---------------------------------------------- */
1068         case VIDIOC_QUERYCTRL:
1069         {
1070                 struct v4l2_queryctrl *p=arg;
1071
1072                 if (!vfd->vidioc_queryctrl)
1073                         break;
1074                 ret=vfd->vidioc_queryctrl(file, fh, p);
1075
1076                 if (!ret)
1077                         dbgarg (cmd, "id=%d, type=%d, name=%s, "
1078                                         "min/max=%d/%d,"
1079                                         " step=%d, default=%d, flags=0x%08x\n",
1080                                         p->id,p->type,p->name,p->minimum,
1081                                         p->maximum,p->step,p->default_value,
1082                                         p->flags);
1083                 break;
1084         }
1085         case VIDIOC_G_CTRL:
1086         {
1087                 struct v4l2_control *p = arg;
1088
1089                 if (!vfd->vidioc_g_ctrl)
1090                         break;
1091                 dbgarg(cmd, "Enum for index=%d\n", p->id);
1092
1093                 ret=vfd->vidioc_g_ctrl(file, fh, p);
1094                 if (!ret)
1095                         dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1096                 break;
1097         }
1098         case VIDIOC_S_CTRL:
1099         {
1100                 struct v4l2_control *p = arg;
1101
1102                 if (!vfd->vidioc_s_ctrl)
1103                         break;
1104                 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1105
1106                 ret=vfd->vidioc_s_ctrl(file, fh, p);
1107                 break;
1108         }
1109         case VIDIOC_G_EXT_CTRLS:
1110         {
1111                 struct v4l2_ext_controls *p = arg;
1112
1113                 if (vfd->vidioc_g_ext_ctrls) {
1114                         dbgarg(cmd, "count=%d\n", p->count);
1115
1116                         ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1117                 }
1118                 break;
1119         }
1120         case VIDIOC_S_EXT_CTRLS:
1121         {
1122                 struct v4l2_ext_controls *p = arg;
1123
1124                 if (vfd->vidioc_s_ext_ctrls) {
1125                         dbgarg(cmd, "count=%d\n", p->count);
1126
1127                         ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1128                 }
1129                 break;
1130         }
1131         case VIDIOC_TRY_EXT_CTRLS:
1132         {
1133                 struct v4l2_ext_controls *p = arg;
1134
1135                 if (vfd->vidioc_try_ext_ctrls) {
1136                         dbgarg(cmd, "count=%d\n", p->count);
1137
1138                         ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1139                 }
1140                 break;
1141         }
1142         case VIDIOC_QUERYMENU:
1143         {
1144                 struct v4l2_querymenu *p=arg;
1145                 if (!vfd->vidioc_querymenu)
1146                         break;
1147                 ret=vfd->vidioc_querymenu(file, fh, p);
1148                 if (!ret)
1149                         dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1150                                                 p->id,p->index,p->name);
1151                 break;
1152         }
1153         /* --- audio ---------------------------------------------- */
1154         case VIDIOC_ENUMAUDIO:
1155         {
1156                 struct v4l2_audio *p=arg;
1157
1158                 if (!vfd->vidioc_enumaudio)
1159                         break;
1160                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1161                 ret=vfd->vidioc_enumaudio(file, fh, p);
1162                 if (!ret)
1163                         dbgarg2("index=%d, name=%s, capability=%d, "
1164                                         "mode=%d\n",p->index,p->name,
1165                                         p->capability, p->mode);
1166                 break;
1167         }
1168         case VIDIOC_G_AUDIO:
1169         {
1170                 struct v4l2_audio *p=arg;
1171                 __u32 index=p->index;
1172
1173                 if (!vfd->vidioc_g_audio)
1174                         break;
1175
1176                 memset(p,0,sizeof(*p));
1177                 p->index=index;
1178                 dbgarg(cmd, "Get for index=%d\n", p->index);
1179                 ret=vfd->vidioc_g_audio(file, fh, p);
1180                 if (!ret)
1181                         dbgarg2("index=%d, name=%s, capability=%d, "
1182                                         "mode=%d\n",p->index,
1183                                         p->name,p->capability, p->mode);
1184                 break;
1185         }
1186         case VIDIOC_S_AUDIO:
1187         {
1188                 struct v4l2_audio *p=arg;
1189
1190                 if (!vfd->vidioc_s_audio)
1191                         break;
1192                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1193                                         "mode=%d\n", p->index, p->name,
1194                                         p->capability, p->mode);
1195                 ret=vfd->vidioc_s_audio(file, fh, p);
1196                 break;
1197         }
1198         case VIDIOC_ENUMAUDOUT:
1199         {
1200                 struct v4l2_audioout *p=arg;
1201
1202                 if (!vfd->vidioc_enumaudout)
1203                         break;
1204                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1205                 ret=vfd->vidioc_enumaudout(file, fh, p);
1206                 if (!ret)
1207                         dbgarg2("index=%d, name=%s, capability=%d, "
1208                                         "mode=%d\n", p->index, p->name,
1209                                         p->capability,p->mode);
1210                 break;
1211         }
1212         case VIDIOC_G_AUDOUT:
1213         {
1214                 struct v4l2_audioout *p=arg;
1215
1216                 if (!vfd->vidioc_g_audout)
1217                         break;
1218                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1219                 ret=vfd->vidioc_g_audout(file, fh, p);
1220                 if (!ret)
1221                         dbgarg2("index=%d, name=%s, capability=%d, "
1222                                         "mode=%d\n", p->index, p->name,
1223                                         p->capability,p->mode);
1224                 break;
1225         }
1226         case VIDIOC_S_AUDOUT:
1227         {
1228                 struct v4l2_audioout *p=arg;
1229
1230                 if (!vfd->vidioc_s_audout)
1231                         break;
1232                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1233                                         "mode=%d\n", p->index, p->name,
1234                                         p->capability,p->mode);
1235
1236                 ret=vfd->vidioc_s_audout(file, fh, p);
1237                 break;
1238         }
1239         case VIDIOC_G_MODULATOR:
1240         {
1241                 struct v4l2_modulator *p=arg;
1242                 if (!vfd->vidioc_g_modulator)
1243                         break;
1244                 ret=vfd->vidioc_g_modulator(file, fh, p);
1245                 if (!ret)
1246                         dbgarg(cmd, "index=%d, name=%s, "
1247                                         "capability=%d, rangelow=%d,"
1248                                         " rangehigh=%d, txsubchans=%d\n",
1249                                         p->index, p->name,p->capability,
1250                                         p->rangelow, p->rangehigh,
1251                                         p->txsubchans);
1252                 break;
1253         }
1254         case VIDIOC_S_MODULATOR:
1255         {
1256                 struct v4l2_modulator *p=arg;
1257                 if (!vfd->vidioc_s_modulator)
1258                         break;
1259                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1260                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1261                                 p->index, p->name,p->capability,p->rangelow,
1262                                 p->rangehigh,p->txsubchans);
1263                         ret=vfd->vidioc_s_modulator(file, fh, p);
1264                 break;
1265         }
1266         case VIDIOC_G_CROP:
1267         {
1268                 struct v4l2_crop *p=arg;
1269                 if (!vfd->vidioc_g_crop)
1270                         break;
1271                 ret=vfd->vidioc_g_crop(file, fh, p);
1272                 if (!ret) {
1273                         dbgarg(cmd, "type=%d\n", p->type);
1274                         dbgrect(vfd, "", &p->c);
1275                 }
1276                 break;
1277         }
1278         case VIDIOC_S_CROP:
1279         {
1280                 struct v4l2_crop *p=arg;
1281                 if (!vfd->vidioc_s_crop)
1282                         break;
1283                 dbgarg(cmd, "type=%d\n", p->type);
1284                 dbgrect(vfd, "", &p->c);
1285                 ret=vfd->vidioc_s_crop(file, fh, p);
1286                 break;
1287         }
1288         case VIDIOC_CROPCAP:
1289         {
1290                 struct v4l2_cropcap *p=arg;
1291                 /*FIXME: Should also show v4l2_fract pixelaspect */
1292                 if (!vfd->vidioc_cropcap)
1293                         break;
1294                 dbgarg(cmd, "type=%d\n", p->type);
1295                 dbgrect(vfd, "bounds ", &p->bounds);
1296                 dbgrect(vfd, "defrect ", &p->defrect);
1297                 ret=vfd->vidioc_cropcap(file, fh, p);
1298                 break;
1299         }
1300         case VIDIOC_G_MPEGCOMP:
1301         {
1302                 struct v4l2_mpeg_compression *p=arg;
1303
1304                 /*FIXME: Several fields not shown */
1305                 if (!vfd->vidioc_g_mpegcomp)
1306                         break;
1307                 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1308                 if (!ret)
1309                         dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1310                                         " ts_pid_video=%d, ts_pid_pcr=%d, "
1311                                         "ps_size=%d, au_sample_rate=%d, "
1312                                         "au_pesid=%c, vi_frame_rate=%d, "
1313                                         "vi_frames_per_gop=%d, "
1314                                         "vi_bframes_count=%d, vi_pesid=%c\n",
1315                                         p->ts_pid_pmt,p->ts_pid_audio,
1316                                         p->ts_pid_video,p->ts_pid_pcr,
1317                                         p->ps_size, p->au_sample_rate,
1318                                         p->au_pesid, p->vi_frame_rate,
1319                                         p->vi_frames_per_gop,
1320                                         p->vi_bframes_count, p->vi_pesid);
1321                 break;
1322         }
1323         case VIDIOC_S_MPEGCOMP:
1324         {
1325                 struct v4l2_mpeg_compression *p=arg;
1326                 /*FIXME: Several fields not shown */
1327                 if (!vfd->vidioc_s_mpegcomp)
1328                         break;
1329                 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1330                                 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1331                                 "au_sample_rate=%d, au_pesid=%c, "
1332                                 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1333                                 "vi_bframes_count=%d, vi_pesid=%c\n",
1334                                 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1335                                 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1336                                 p->au_pesid, p->vi_frame_rate,
1337                                 p->vi_frames_per_gop, p->vi_bframes_count,
1338                                 p->vi_pesid);
1339                 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1340                 break;
1341         }
1342         case VIDIOC_G_JPEGCOMP:
1343         {
1344                 struct v4l2_jpegcompression *p=arg;
1345                 if (!vfd->vidioc_g_jpegcomp)
1346                         break;
1347                 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1348                 if (!ret)
1349                         dbgarg (cmd, "quality=%d, APPn=%d, "
1350                                                 "APP_len=%d, COM_len=%d, "
1351                                                 "jpeg_markers=%d\n",
1352                                                 p->quality,p->APPn,p->APP_len,
1353                                                 p->COM_len,p->jpeg_markers);
1354                 break;
1355         }
1356         case VIDIOC_S_JPEGCOMP:
1357         {
1358                 struct v4l2_jpegcompression *p=arg;
1359                 if (!vfd->vidioc_g_jpegcomp)
1360                         break;
1361                 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1362                                         "COM_len=%d, jpeg_markers=%d\n",
1363                                         p->quality,p->APPn,p->APP_len,
1364                                         p->COM_len,p->jpeg_markers);
1365                         ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1366                 break;
1367         }
1368         case VIDIOC_G_ENC_INDEX:
1369         {
1370                 struct v4l2_enc_idx *p=arg;
1371
1372                 if (!vfd->vidioc_g_enc_index)
1373                         break;
1374                 ret=vfd->vidioc_g_enc_index(file, fh, p);
1375                 if (!ret)
1376                         dbgarg (cmd, "entries=%d, entries_cap=%d\n",
1377                                         p->entries,p->entries_cap);
1378                 break;
1379         }
1380         case VIDIOC_ENCODER_CMD:
1381         {
1382                 struct v4l2_encoder_cmd *p=arg;
1383
1384                 if (!vfd->vidioc_encoder_cmd)
1385                         break;
1386                 ret=vfd->vidioc_encoder_cmd(file, fh, p);
1387                 if (!ret)
1388                         dbgarg (cmd, "cmd=%d, flags=%d\n",
1389                                         p->cmd,p->flags);
1390                 break;
1391         }
1392         case VIDIOC_TRY_ENCODER_CMD:
1393         {
1394                 struct v4l2_encoder_cmd *p=arg;
1395
1396                 if (!vfd->vidioc_try_encoder_cmd)
1397                         break;
1398                 ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
1399                 if (!ret)
1400                         dbgarg (cmd, "cmd=%d, flags=%d\n",
1401                                         p->cmd,p->flags);
1402                 break;
1403         }
1404         case VIDIOC_G_PARM:
1405         {
1406                 struct v4l2_streamparm *p=arg;
1407                 __u32 type=p->type;
1408
1409                 memset(p,0,sizeof(*p));
1410                 p->type=type;
1411
1412                 if (vfd->vidioc_g_parm) {
1413                         ret=vfd->vidioc_g_parm(file, fh, p);
1414                 } else {
1415                         struct v4l2_standard s;
1416
1417                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1418                                 return -EINVAL;
1419
1420                         v4l2_video_std_construct(&s, vfd->current_norm,
1421                                                  v4l2_norm_to_name(vfd->current_norm));
1422
1423                         p->parm.capture.timeperframe = s.frameperiod;
1424                         ret=0;
1425                 }
1426
1427                 dbgarg (cmd, "type=%d\n", p->type);
1428                 break;
1429         }
1430         case VIDIOC_S_PARM:
1431         {
1432                 struct v4l2_streamparm *p=arg;
1433                 if (!vfd->vidioc_s_parm)
1434                         break;
1435                 dbgarg (cmd, "type=%d\n", p->type);
1436                 ret=vfd->vidioc_s_parm(file, fh, p);
1437                 break;
1438         }
1439         case VIDIOC_G_TUNER:
1440         {
1441                 struct v4l2_tuner *p=arg;
1442                 __u32 index=p->index;
1443
1444                 if (!vfd->vidioc_g_tuner)
1445                         break;
1446
1447                 memset(p,0,sizeof(*p));
1448                 p->index=index;
1449
1450                 ret=vfd->vidioc_g_tuner(file, fh, p);
1451                 if (!ret)
1452                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1453                                         "capability=%d, rangelow=%d, "
1454                                         "rangehigh=%d, signal=%d, afc=%d, "
1455                                         "rxsubchans=%d, audmode=%d\n",
1456                                         p->index, p->name, p->type,
1457                                         p->capability, p->rangelow,
1458                                         p->rangehigh, p->rxsubchans,
1459                                         p->audmode, p->signal, p->afc);
1460                 break;
1461         }
1462         case VIDIOC_S_TUNER:
1463         {
1464                 struct v4l2_tuner *p=arg;
1465                 if (!vfd->vidioc_s_tuner)
1466                         break;
1467                 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1468                                 "capability=%d, rangelow=%d, rangehigh=%d, "
1469                                 "signal=%d, afc=%d, rxsubchans=%d, "
1470                                 "audmode=%d\n",p->index, p->name, p->type,
1471                                 p->capability, p->rangelow,p->rangehigh,
1472                                 p->rxsubchans, p->audmode, p->signal,
1473                                 p->afc);
1474                 ret=vfd->vidioc_s_tuner(file, fh, p);
1475                 break;
1476         }
1477         case VIDIOC_G_FREQUENCY:
1478         {
1479                 struct v4l2_frequency *p=arg;
1480                 if (!vfd->vidioc_g_frequency)
1481                         break;
1482
1483                 memset(p,0,sizeof(*p));
1484
1485                 ret=vfd->vidioc_g_frequency(file, fh, p);
1486                 if (!ret)
1487                         dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1488                                                 p->tuner,p->type,p->frequency);
1489                 break;
1490         }
1491         case VIDIOC_S_FREQUENCY:
1492         {
1493                 struct v4l2_frequency *p=arg;
1494                 if (!vfd->vidioc_s_frequency)
1495                         break;
1496                 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1497                                 p->tuner,p->type,p->frequency);
1498                 ret=vfd->vidioc_s_frequency(file, fh, p);
1499                 break;
1500         }
1501         case VIDIOC_G_SLICED_VBI_CAP:
1502         {
1503                 struct v4l2_sliced_vbi_cap *p=arg;
1504                 if (!vfd->vidioc_g_sliced_vbi_cap)
1505                         break;
1506                 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1507                 if (!ret)
1508                         dbgarg (cmd, "service_set=%d\n", p->service_set);
1509                 break;
1510         }
1511         case VIDIOC_LOG_STATUS:
1512         {
1513                 if (!vfd->vidioc_log_status)
1514                         break;
1515                 ret=vfd->vidioc_log_status(file, fh);
1516                 break;
1517         }
1518 #ifdef CONFIG_VIDEO_ADV_DEBUG
1519         case VIDIOC_DBG_G_REGISTER:
1520         {
1521                 struct v4l2_register *p=arg;
1522                 if (!capable(CAP_SYS_ADMIN))
1523                         ret=-EPERM;
1524                 else if (vfd->vidioc_g_register)
1525                         ret=vfd->vidioc_g_register(file, fh, p);
1526                 break;
1527         }
1528         case VIDIOC_DBG_S_REGISTER:
1529         {
1530                 struct v4l2_register *p=arg;
1531                 if (!capable(CAP_SYS_ADMIN))
1532                         ret=-EPERM;
1533                 else if (vfd->vidioc_s_register)
1534                         ret=vfd->vidioc_s_register(file, fh, p);
1535                 break;
1536         }
1537 #endif
1538         case VIDIOC_G_CHIP_IDENT:
1539         {
1540                 struct v4l2_chip_ident *p=arg;
1541                 if (!vfd->vidioc_g_chip_ident)
1542                         break;
1543                 ret=vfd->vidioc_g_chip_ident(file, fh, p);
1544                 if (!ret)
1545                         dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1546                 break;
1547         }
1548         } /* switch */
1549
1550         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1551                 if (ret<0) {
1552                         printk ("%s: err:\n", vfd->name);
1553                         v4l_print_ioctl(vfd->name, cmd);
1554                 }
1555         }
1556
1557         return ret;
1558 }
1559
1560 int video_ioctl2 (struct inode *inode, struct file *file,
1561                unsigned int cmd, unsigned long arg)
1562 {
1563         char    sbuf[128];
1564         void    *mbuf = NULL;
1565         void    *parg = NULL;
1566         int     err  = -EINVAL;
1567         int     is_ext_ctrl;
1568         size_t  ctrls_size = 0;
1569         void __user *user_ptr = NULL;
1570
1571 #ifdef __OLD_VIDIOC_
1572         cmd = video_fix_command(cmd);
1573 #endif
1574         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1575                        cmd == VIDIOC_TRY_EXT_CTRLS);
1576
1577         /*  Copy arguments into temp kernel buffer  */
1578         switch (_IOC_DIR(cmd)) {
1579         case _IOC_NONE:
1580                 parg = NULL;
1581                 break;
1582         case _IOC_READ:
1583         case _IOC_WRITE:
1584         case (_IOC_WRITE | _IOC_READ):
1585                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1586                         parg = sbuf;
1587                 } else {
1588                         /* too big to allocate from stack */
1589                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1590                         if (NULL == mbuf)
1591                                 return -ENOMEM;
1592                         parg = mbuf;
1593                 }
1594
1595                 err = -EFAULT;
1596                 if (_IOC_DIR(cmd) & _IOC_WRITE)
1597                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1598                                 goto out;
1599                 break;
1600         }
1601
1602         if (is_ext_ctrl) {
1603                 struct v4l2_ext_controls *p = parg;
1604
1605                 /* In case of an error, tell the caller that it wasn't
1606                    a specific control that caused it. */
1607                 p->error_idx = p->count;
1608                 user_ptr = (void __user *)p->controls;
1609                 if (p->count) {
1610                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1611                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1612                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1613                         err = -ENOMEM;
1614                         if (NULL == mbuf)
1615                                 goto out_ext_ctrl;
1616                         err = -EFAULT;
1617                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
1618                                 goto out_ext_ctrl;
1619                         p->controls = mbuf;
1620                 }
1621         }
1622
1623         /* Handles IOCTL */
1624         err = __video_do_ioctl(inode, file, cmd, parg);
1625         if (err == -ENOIOCTLCMD)
1626                 err = -EINVAL;
1627         if (is_ext_ctrl) {
1628                 struct v4l2_ext_controls *p = parg;
1629
1630                 p->controls = (void *)user_ptr;
1631                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1632                         err = -EFAULT;
1633                 goto out_ext_ctrl;
1634         }
1635         if (err < 0)
1636                 goto out;
1637
1638 out_ext_ctrl:
1639         /*  Copy results into user buffer  */
1640         switch (_IOC_DIR(cmd))
1641         {
1642         case _IOC_READ:
1643         case (_IOC_WRITE | _IOC_READ):
1644                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1645                         err = -EFAULT;
1646                 break;
1647         }
1648
1649 out:
1650         kfree(mbuf);
1651         return err;
1652 }
1653
1654
1655 static const struct file_operations video_fops;
1656
1657 /**
1658  *      video_register_device - register video4linux devices
1659  *      @vfd:  video device structure we want to register
1660  *      @type: type of device to register
1661  *      @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
1662  *             -1 == first free)
1663  *
1664  *      The registration code assigns minor numbers based on the type
1665  *      requested. -ENFILE is returned in all the device slots for this
1666  *      category are full. If not then the minor field is set and the
1667  *      driver initialize function is called (if non %NULL).
1668  *
1669  *      Zero is returned on success.
1670  *
1671  *      Valid types are
1672  *
1673  *      %VFL_TYPE_GRABBER - A frame grabber
1674  *
1675  *      %VFL_TYPE_VTX - A teletext device
1676  *
1677  *      %VFL_TYPE_VBI - Vertical blank data (undecoded)
1678  *
1679  *      %VFL_TYPE_RADIO - A radio card
1680  */
1681
1682 int video_register_device(struct video_device *vfd, int type, int nr)
1683 {
1684         int i=0;
1685         int base;
1686         int end;
1687         int ret;
1688         char *name_base;
1689
1690         switch(type)
1691         {
1692                 case VFL_TYPE_GRABBER:
1693                         base=MINOR_VFL_TYPE_GRABBER_MIN;
1694                         end=MINOR_VFL_TYPE_GRABBER_MAX+1;
1695                         name_base = "video";
1696                         break;
1697                 case VFL_TYPE_VTX:
1698                         base=MINOR_VFL_TYPE_VTX_MIN;
1699                         end=MINOR_VFL_TYPE_VTX_MAX+1;
1700                         name_base = "vtx";
1701                         break;
1702                 case VFL_TYPE_VBI:
1703                         base=MINOR_VFL_TYPE_VBI_MIN;
1704                         end=MINOR_VFL_TYPE_VBI_MAX+1;
1705                         name_base = "vbi";
1706                         break;
1707                 case VFL_TYPE_RADIO:
1708                         base=MINOR_VFL_TYPE_RADIO_MIN;
1709                         end=MINOR_VFL_TYPE_RADIO_MAX+1;
1710                         name_base = "radio";
1711                         break;
1712                 default:
1713                         printk(KERN_ERR "%s called with unknown type: %d\n",
1714                                __FUNCTION__, type);
1715                         return -1;
1716         }
1717
1718         /* pick a minor number */
1719         mutex_lock(&videodev_lock);
1720         if (nr >= 0  &&  nr < end-base) {
1721                 /* use the one the driver asked for */
1722                 i = base+nr;
1723                 if (NULL != video_device[i]) {
1724                         mutex_unlock(&videodev_lock);
1725                         return -ENFILE;
1726                 }
1727         } else {
1728                 /* use first free */
1729                 for(i=base;i<end;i++)
1730                         if (NULL == video_device[i])
1731                                 break;
1732                 if (i == end) {
1733                         mutex_unlock(&videodev_lock);
1734                         return -ENFILE;
1735                 }
1736         }
1737         video_device[i]=vfd;
1738         vfd->minor=i;
1739         mutex_unlock(&videodev_lock);
1740         mutex_init(&vfd->lock);
1741
1742         /* sysfs class */
1743         memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
1744         if (vfd->dev)
1745                 vfd->class_dev.dev = vfd->dev;
1746         vfd->class_dev.class       = &video_class;
1747         vfd->class_dev.devt        = MKDEV(VIDEO_MAJOR, vfd->minor);
1748         sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
1749         ret = class_device_register(&vfd->class_dev);
1750         if (ret < 0) {
1751                 printk(KERN_ERR "%s: class_device_register failed\n",
1752                        __FUNCTION__);
1753                 goto fail_minor;
1754         }
1755         ret = class_device_create_file(&vfd->class_dev, &class_device_attr_name);
1756         if (ret < 0) {
1757                 printk(KERN_ERR "%s: class_device_create_file 'name' failed\n",
1758                        __FUNCTION__);
1759                 goto fail_classdev;
1760         }
1761
1762 #if 1
1763         /* needed until all drivers are fixed */
1764         if (!vfd->release)
1765                 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1766                        "Please fix your driver for proper sysfs support, see "
1767                        "http://lwn.net/Articles/36850/\n", vfd->name);
1768 #endif
1769         return 0;
1770
1771 fail_classdev:
1772         class_device_unregister(&vfd->class_dev);
1773 fail_minor:
1774         mutex_lock(&videodev_lock);
1775         video_device[vfd->minor] = NULL;
1776         vfd->minor = -1;
1777         mutex_unlock(&videodev_lock);
1778         return ret;
1779 }
1780
1781 /**
1782  *      video_unregister_device - unregister a video4linux device
1783  *      @vfd: the device to unregister
1784  *
1785  *      This unregisters the passed device and deassigns the minor
1786  *      number. Future open calls will be met with errors.
1787  */
1788
1789 void video_unregister_device(struct video_device *vfd)
1790 {
1791         mutex_lock(&videodev_lock);
1792         if(video_device[vfd->minor]!=vfd)
1793                 panic("videodev: bad unregister");
1794
1795         video_device[vfd->minor]=NULL;
1796         class_device_unregister(&vfd->class_dev);
1797         mutex_unlock(&videodev_lock);
1798 }
1799
1800 /*
1801  * Video fs operations
1802  */
1803 static const struct file_operations video_fops=
1804 {
1805         .owner          = THIS_MODULE,
1806         .llseek         = no_llseek,
1807         .open           = video_open,
1808 };
1809
1810 /*
1811  *      Initialise video for linux
1812  */
1813
1814 static int __init videodev_init(void)
1815 {
1816         int ret;
1817
1818         printk(KERN_INFO "Linux video capture interface: v2.00\n");
1819         if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1820                 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1821                 return -EIO;
1822         }
1823
1824         ret = class_register(&video_class);
1825         if (ret < 0) {
1826                 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1827                 printk(KERN_WARNING "video_dev: class_register failed\n");
1828                 return -EIO;
1829         }
1830
1831         return 0;
1832 }
1833
1834 static void __exit videodev_exit(void)
1835 {
1836         class_unregister(&video_class);
1837         unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1838 }
1839
1840 module_init(videodev_init)
1841 module_exit(videodev_exit)
1842
1843 EXPORT_SYMBOL(video_register_device);
1844 EXPORT_SYMBOL(video_unregister_device);
1845 EXPORT_SYMBOL(video_devdata);
1846 EXPORT_SYMBOL(video_usercopy);
1847 EXPORT_SYMBOL(video_exclusive_open);
1848 EXPORT_SYMBOL(video_exclusive_release);
1849 EXPORT_SYMBOL(video_ioctl2);
1850 EXPORT_SYMBOL(video_device_alloc);
1851 EXPORT_SYMBOL(video_device_release);
1852
1853 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1854 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1855 MODULE_LICENSE("GPL");
1856
1857
1858 /*
1859  * Local variables:
1860  * c-basic-offset: 8
1861  * End:
1862  */