blob: 1be712757e4d8552efe9225d227e0b70f56b9ead [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03002 * Video capture interface for Linux version 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03004 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03007 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030012 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
17 */
18
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030019#define dbgarg(cmd, fmt, arg...) \
Enrico Scholz474ce782006-10-09 16:27:05 -030020 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030021 printk (KERN_DEBUG "%s: ", vfd->name); \
22 v4l_printk_ioctl(cmd); \
Enrico Scholz474ce782006-10-09 16:27:05 -030023 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \
24 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030025
26#define dbgarg2(fmt, arg...) \
27 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
28 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/smp_lock.h>
35#include <linux/mm.h>
36#include <linux/string.h>
37#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/kmod.h>
40#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030044#define __OLD_VIDIOC_ /* To allow fixing old calls*/
45#include <linux/videodev2.h>
46
47#ifdef CONFIG_VIDEO_V4L1
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/videodev.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030049#endif
50#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define VIDEO_NUM_DEVICES 256
53#define VIDEO_NAME "video4linux"
54
55/*
56 * sysfs stuff
57 */
58
59static ssize_t show_name(struct class_device *cd, char *buf)
60{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030061 struct video_device *vfd = container_of(cd, struct video_device,
62 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
64}
65
66static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
67
68struct video_device *video_device_alloc(void)
69{
70 struct video_device *vfd;
71
Panagiotis Issaris74081872006-01-11 19:40:56 -020072 vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return vfd;
74}
75
76void video_device_release(struct video_device *vfd)
77{
78 kfree(vfd);
79}
80
81static void video_release(struct class_device *cd)
82{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030083 struct video_device *vfd = container_of(cd, struct video_device,
84 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -020086#if 1
Michael Krufky50c25ff2006-01-09 15:25:34 -020087 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!vfd->release)
89 return;
90#endif
91 vfd->release(vfd);
92}
93
94static struct class video_class = {
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080095 .name = VIDEO_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .release = video_release,
97};
98
99/*
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800100 * Active devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static struct video_device *video_device[VIDEO_NUM_DEVICES];
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200104static DEFINE_MUTEX(videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106struct video_device* video_devdata(struct file *file)
107{
Josef Sipek723731b2006-12-08 02:37:47 -0800108 return video_device[iminor(file->f_path.dentry->d_inode)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/*
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300112 * Open a video device - FIXME: Obsoleted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114static int video_open(struct inode *inode, struct file *file)
115{
116 unsigned int minor = iminor(inode);
117 int err = 0;
118 struct video_device *vfl;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800119 const struct file_operations *old_fops;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if(minor>=VIDEO_NUM_DEVICES)
122 return -ENODEV;
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200123 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 vfl=video_device[minor];
125 if(vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200126 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200128 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 vfl=video_device[minor];
130 if (vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200131 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -ENODEV;
133 }
134 }
135 old_fops = file->f_op;
136 file->f_op = fops_get(vfl->fops);
137 if(file->f_op->open)
138 err = file->f_op->open(inode,file);
139 if (err) {
140 fops_put(file->f_op);
141 file->f_op = fops_get(old_fops);
142 }
143 fops_put(old_fops);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200144 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return err;
146}
147
148/*
149 * helper function -- handles userspace copying for ioctl arguments
150 */
151
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300152#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static unsigned int
154video_fix_command(unsigned int cmd)
155{
156 switch (cmd) {
157 case VIDIOC_OVERLAY_OLD:
158 cmd = VIDIOC_OVERLAY;
159 break;
160 case VIDIOC_S_PARM_OLD:
161 cmd = VIDIOC_S_PARM;
162 break;
163 case VIDIOC_S_CTRL_OLD:
164 cmd = VIDIOC_S_CTRL;
165 break;
166 case VIDIOC_G_AUDIO_OLD:
167 cmd = VIDIOC_G_AUDIO;
168 break;
169 case VIDIOC_G_AUDOUT_OLD:
170 cmd = VIDIOC_G_AUDOUT;
171 break;
172 case VIDIOC_CROPCAP_OLD:
173 cmd = VIDIOC_CROPCAP;
174 break;
175 }
176 return cmd;
177}
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300180/*
181 * Obsolete usercopy function - Should be removed soon
182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183int
184video_usercopy(struct inode *inode, struct file *file,
185 unsigned int cmd, unsigned long arg,
186 int (*func)(struct inode *inode, struct file *file,
187 unsigned int cmd, void *arg))
188{
189 char sbuf[128];
190 void *mbuf = NULL;
191 void *parg = NULL;
192 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300193 int is_ext_ctrl;
194 size_t ctrls_size = 0;
195 void __user *user_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300197#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cmd = video_fix_command(cmd);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300199#endif
Hans Verkuil05976912006-06-18 13:43:28 -0300200 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
201 cmd == VIDIOC_TRY_EXT_CTRLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Copy arguments into temp kernel buffer */
204 switch (_IOC_DIR(cmd)) {
205 case _IOC_NONE:
206 parg = NULL;
207 break;
208 case _IOC_READ:
209 case _IOC_WRITE:
210 case (_IOC_WRITE | _IOC_READ):
211 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
212 parg = sbuf;
213 } else {
214 /* too big to allocate from stack */
215 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
216 if (NULL == mbuf)
217 return -ENOMEM;
218 parg = mbuf;
219 }
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 err = -EFAULT;
222 if (_IOC_DIR(cmd) & _IOC_WRITE)
Hans Verkuil05976912006-06-18 13:43:28 -0300223 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto out;
225 break;
226 }
Hans Verkuil05976912006-06-18 13:43:28 -0300227 if (is_ext_ctrl) {
228 struct v4l2_ext_controls *p = parg;
229
230 /* In case of an error, tell the caller that it wasn't
231 a specific control that caused it. */
232 p->error_idx = p->count;
233 user_ptr = (void __user *)p->controls;
234 if (p->count) {
235 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
236 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
237 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
238 err = -ENOMEM;
239 if (NULL == mbuf)
240 goto out_ext_ctrl;
241 err = -EFAULT;
242 if (copy_from_user(mbuf, user_ptr, ctrls_size))
243 goto out_ext_ctrl;
244 p->controls = mbuf;
245 }
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* call driver */
249 err = func(inode, file, cmd, parg);
250 if (err == -ENOIOCTLCMD)
251 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300252 if (is_ext_ctrl) {
253 struct v4l2_ext_controls *p = parg;
254
255 p->controls = (void *)user_ptr;
256 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
257 err = -EFAULT;
258 goto out_ext_ctrl;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (err < 0)
261 goto out;
262
Hans Verkuil05976912006-06-18 13:43:28 -0300263out_ext_ctrl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Copy results into user buffer */
265 switch (_IOC_DIR(cmd))
266 {
267 case _IOC_READ:
268 case (_IOC_WRITE | _IOC_READ):
269 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
270 err = -EFAULT;
271 break;
272 }
273
274out:
Jesper Juhl2ea75332005-11-07 01:01:31 -0800275 kfree(mbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return err;
277}
278
279/*
280 * open/release helper functions -- handle exclusive opens
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300281 * Should be removed soon
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283int video_exclusive_open(struct inode *inode, struct file *file)
284{
285 struct video_device *vfl = video_devdata(file);
286 int retval = 0;
287
Ingo Molnar3593cab2006-02-07 06:49:14 -0200288 mutex_lock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (vfl->users) {
290 retval = -EBUSY;
291 } else {
292 vfl->users++;
293 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200294 mutex_unlock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return retval;
296}
297
298int video_exclusive_release(struct inode *inode, struct file *file)
299{
300 struct video_device *vfl = video_devdata(file);
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 vfl->users--;
303 return 0;
304}
305
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300306static char *v4l2_memory_names[] = {
307 [V4L2_MEMORY_MMAP] = "mmap",
308 [V4L2_MEMORY_USERPTR] = "userptr",
309 [V4L2_MEMORY_OVERLAY] = "overlay",
310};
311
312
313/* FIXME: Those stuff are replicated also on v4l2-common.c */
314static char *v4l2_type_names_FIXME[] = {
315 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
316 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
317 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
318 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
319 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
320 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
321 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
322 [V4L2_BUF_TYPE_PRIVATE] = "private",
323};
324
325static 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};
335
336#define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
337
338static void dbgbuf(unsigned int cmd, struct video_device *vfd,
339 struct v4l2_buffer *p)
340{
341 struct v4l2_timecode *tc=&p->timecode;
342
343 dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
344 "bytesused=%d, flags=0x%08d, "
345 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx\n",
346 (p->timestamp.tv_sec/3600),
347 (int)(p->timestamp.tv_sec/60)%60,
348 (int)(p->timestamp.tv_sec%60),
349 p->timestamp.tv_usec,
350 p->index,
351 prt_names(p->type,v4l2_type_names_FIXME),
352 p->bytesused,p->flags,
353 p->field,p->sequence,
354 prt_names(p->memory,v4l2_memory_names),
355 p->m.userptr);
356 dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
357 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
358 tc->hours,tc->minutes,tc->seconds,
Mauro Carvalho Chehabc18cb012006-06-23 07:05:22 -0300359 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300360}
361
362static inline void dbgrect(struct video_device *vfd, char *s,
363 struct v4l2_rect *r)
364{
365 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
366 r->width, r->height);
367};
368
369static inline void v4l_print_pix_fmt (struct video_device *vfd,
370 struct v4l2_pix_format *fmt)
371{
372 dbgarg2 ("width=%d, height=%d, format=0x%08x, field=%s, "
373 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
374 fmt->width,fmt->height,fmt->pixelformat,
375 prt_names(fmt->field,v4l2_field_names_FIXME),
376 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
377};
378
379
380static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
381{
382 switch (type) {
383 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
384 if (vfd->vidioc_try_fmt_cap)
385 return (0);
386 break;
387 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
388 if (vfd->vidioc_try_fmt_overlay)
389 return (0);
390 break;
391 case V4L2_BUF_TYPE_VBI_CAPTURE:
392 if (vfd->vidioc_try_fmt_vbi)
393 return (0);
394 break;
395 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
396 if (vfd->vidioc_try_fmt_vbi_output)
397 return (0);
398 break;
399 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
400 if (vfd->vidioc_try_fmt_vbi_capture)
401 return (0);
402 break;
403 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
404 if (vfd->vidioc_try_fmt_video_output)
405 return (0);
406 break;
407 case V4L2_BUF_TYPE_VBI_OUTPUT:
408 if (vfd->vidioc_try_fmt_vbi_output)
409 return (0);
410 break;
411 case V4L2_BUF_TYPE_PRIVATE:
412 if (vfd->vidioc_try_fmt_type_private)
413 return (0);
414 break;
415 }
416 return (-EINVAL);
417}
418
419static int __video_do_ioctl(struct inode *inode, struct file *file,
420 unsigned int cmd, void *arg)
421{
422 struct video_device *vfd = video_devdata(file);
423 void *fh = file->private_data;
424 int ret = -EINVAL;
425
426 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
427 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
428 v4l_print_ioctl(vfd->name, cmd);
429 }
430
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -0300431 if (_IOC_TYPE(cmd)=='v')
432 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
433 __video_do_ioctl);
434
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300435 switch(cmd) {
436 /* --- capabilities ------------------------------------------ */
437 case VIDIOC_QUERYCAP:
438 {
439 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
440 memset(cap, 0, sizeof(*cap));
441
442 if (!vfd->vidioc_querycap)
443 break;
444
445 ret=vfd->vidioc_querycap(file, fh, cap);
446 if (!ret)
447 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
448 "version=0x%08x, "
449 "capabilities=0x%08x\n",
450 cap->driver,cap->card,cap->bus_info,
451 cap->version,
452 cap->capabilities);
453 break;
454 }
455
456 /* --- priority ------------------------------------------ */
457 case VIDIOC_G_PRIORITY:
458 {
459 enum v4l2_priority *p=arg;
460
461 if (!vfd->vidioc_g_priority)
462 break;
463 ret=vfd->vidioc_g_priority(file, fh, p);
464 if (!ret)
465 dbgarg(cmd, "priority is %d\n", *p);
466 break;
467 }
468 case VIDIOC_S_PRIORITY:
469 {
470 enum v4l2_priority *p=arg;
471
472 if (!vfd->vidioc_s_priority)
473 break;
474 dbgarg(cmd, "setting priority to %d\n", *p);
475 ret=vfd->vidioc_s_priority(file, fh, *p);
476 break;
477 }
478
479 /* --- capture ioctls ---------------------------------------- */
480 case VIDIOC_ENUM_FMT:
481 {
482 struct v4l2_fmtdesc *f = arg;
483 enum v4l2_buf_type type;
484 unsigned int index;
485
486 index = f->index;
487 type = f->type;
488 memset(f,0,sizeof(*f));
489 f->index = index;
490 f->type = type;
491
492 switch (type) {
493 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
494 if (vfd->vidioc_enum_fmt_cap)
495 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
496 break;
497 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
498 if (vfd->vidioc_enum_fmt_overlay)
499 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
500 break;
501 case V4L2_BUF_TYPE_VBI_CAPTURE:
502 if (vfd->vidioc_enum_fmt_vbi)
503 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
504 break;
505 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
506 if (vfd->vidioc_enum_fmt_vbi_output)
507 ret=vfd->vidioc_enum_fmt_vbi_output(file,
508 fh, f);
509 break;
510 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
511 if (vfd->vidioc_enum_fmt_vbi_capture)
512 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
513 fh, f);
514 break;
515 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
516 if (vfd->vidioc_enum_fmt_video_output)
517 ret=vfd->vidioc_enum_fmt_video_output(file,
518 fh, f);
519 break;
520 case V4L2_BUF_TYPE_VBI_OUTPUT:
521 if (vfd->vidioc_enum_fmt_vbi_output)
522 ret=vfd->vidioc_enum_fmt_vbi_output(file,
523 fh, f);
524 break;
525 case V4L2_BUF_TYPE_PRIVATE:
526 if (vfd->vidioc_enum_fmt_type_private)
527 ret=vfd->vidioc_enum_fmt_type_private(file,
528 fh, f);
529 break;
530 }
531 if (!ret)
532 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
533 "description=%s,"
534 " pixelformat=0x%8x\n",
535 f->index, f->type, f->flags,
536 f->description,
537 f->pixelformat);
538
539 break;
540 }
541 case VIDIOC_G_FMT:
542 {
543 struct v4l2_format *f = (struct v4l2_format *)arg;
544 enum v4l2_buf_type type=f->type;
545
546 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
547 f->type=type;
548
549 /* FIXME: Should be one dump per type */
550 dbgarg (cmd, "type=%s\n", prt_names(type,
551 v4l2_type_names_FIXME));
552
553 switch (type) {
554 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
555 if (vfd->vidioc_g_fmt_cap)
556 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
557 if (!ret)
558 v4l_print_pix_fmt(vfd,&f->fmt.pix);
559 break;
560 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
561 if (vfd->vidioc_g_fmt_overlay)
562 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
563 break;
564 case V4L2_BUF_TYPE_VBI_CAPTURE:
565 if (vfd->vidioc_g_fmt_vbi)
566 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
567 break;
568 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
569 if (vfd->vidioc_g_fmt_vbi_output)
570 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
571 break;
572 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
573 if (vfd->vidioc_g_fmt_vbi_capture)
574 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
575 break;
576 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
577 if (vfd->vidioc_g_fmt_video_output)
578 ret=vfd->vidioc_g_fmt_video_output(file,
579 fh, f);
580 break;
581 case V4L2_BUF_TYPE_VBI_OUTPUT:
582 if (vfd->vidioc_g_fmt_vbi_output)
583 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
584 break;
585 case V4L2_BUF_TYPE_PRIVATE:
586 if (vfd->vidioc_g_fmt_type_private)
587 ret=vfd->vidioc_g_fmt_type_private(file,
588 fh, f);
589 break;
590 }
591
592 break;
593 }
594 case VIDIOC_S_FMT:
595 {
596 struct v4l2_format *f = (struct v4l2_format *)arg;
597
598 /* FIXME: Should be one dump per type */
599 dbgarg (cmd, "type=%s\n", prt_names(f->type,
600 v4l2_type_names_FIXME));
601
602 switch (f->type) {
603 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
604 v4l_print_pix_fmt(vfd,&f->fmt.pix);
605 if (vfd->vidioc_s_fmt_cap)
606 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
607 break;
608 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
609 if (vfd->vidioc_s_fmt_overlay)
610 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
611 break;
612 case V4L2_BUF_TYPE_VBI_CAPTURE:
613 if (vfd->vidioc_s_fmt_vbi)
614 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
615 break;
616 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
617 if (vfd->vidioc_s_fmt_vbi_output)
618 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
619 break;
620 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
621 if (vfd->vidioc_s_fmt_vbi_capture)
622 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
623 break;
624 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
625 if (vfd->vidioc_s_fmt_video_output)
626 ret=vfd->vidioc_s_fmt_video_output(file,
627 fh, f);
628 break;
629 case V4L2_BUF_TYPE_VBI_OUTPUT:
630 if (vfd->vidioc_s_fmt_vbi_output)
631 ret=vfd->vidioc_s_fmt_vbi_output(file,
632 fh, f);
633 break;
634 case V4L2_BUF_TYPE_PRIVATE:
635 if (vfd->vidioc_s_fmt_type_private)
636 ret=vfd->vidioc_s_fmt_type_private(file,
637 fh, f);
638 break;
639 }
640 break;
641 }
642 case VIDIOC_TRY_FMT:
643 {
644 struct v4l2_format *f = (struct v4l2_format *)arg;
645
646 /* FIXME: Should be one dump per type */
647 dbgarg (cmd, "type=%s\n", prt_names(f->type,
648 v4l2_type_names_FIXME));
649 switch (f->type) {
650 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
651 if (vfd->vidioc_try_fmt_cap)
652 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
653 if (!ret)
654 v4l_print_pix_fmt(vfd,&f->fmt.pix);
655 break;
656 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
657 if (vfd->vidioc_try_fmt_overlay)
658 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
659 break;
660 case V4L2_BUF_TYPE_VBI_CAPTURE:
661 if (vfd->vidioc_try_fmt_vbi)
662 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
663 break;
664 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
665 if (vfd->vidioc_try_fmt_vbi_output)
666 ret=vfd->vidioc_try_fmt_vbi_output(file,
667 fh, f);
668 break;
669 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
670 if (vfd->vidioc_try_fmt_vbi_capture)
671 ret=vfd->vidioc_try_fmt_vbi_capture(file,
672 fh, f);
673 break;
674 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
675 if (vfd->vidioc_try_fmt_video_output)
676 ret=vfd->vidioc_try_fmt_video_output(file,
677 fh, f);
678 break;
679 case V4L2_BUF_TYPE_VBI_OUTPUT:
680 if (vfd->vidioc_try_fmt_vbi_output)
681 ret=vfd->vidioc_try_fmt_vbi_output(file,
682 fh, f);
683 break;
684 case V4L2_BUF_TYPE_PRIVATE:
685 if (vfd->vidioc_try_fmt_type_private)
686 ret=vfd->vidioc_try_fmt_type_private(file,
687 fh, f);
688 break;
689 }
690
691 break;
692 }
693 /* FIXME: Those buf reqs could be handled here,
694 with some changes on videobuf to allow its header to be included at
695 videodev2.h or being merged at videodev2.
696 */
697 case VIDIOC_REQBUFS:
698 {
699 struct v4l2_requestbuffers *p=arg;
700
701 if (!vfd->vidioc_reqbufs)
702 break;
703 ret = check_fmt (vfd, p->type);
704 if (ret)
705 break;
706
707 ret=vfd->vidioc_reqbufs(file, fh, p);
708 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
709 p->count,
710 prt_names(p->type,v4l2_type_names_FIXME),
711 prt_names(p->memory,v4l2_memory_names));
712 break;
713 }
714 case VIDIOC_QUERYBUF:
715 {
716 struct v4l2_buffer *p=arg;
717
718 if (!vfd->vidioc_querybuf)
719 break;
720 ret = check_fmt (vfd, p->type);
721 if (ret)
722 break;
723
724 ret=vfd->vidioc_querybuf(file, fh, p);
725 if (!ret)
726 dbgbuf(cmd,vfd,p);
727 break;
728 }
729 case VIDIOC_QBUF:
730 {
731 struct v4l2_buffer *p=arg;
732
733 if (!vfd->vidioc_qbuf)
734 break;
735 ret = check_fmt (vfd, p->type);
736 if (ret)
737 break;
738
739 ret=vfd->vidioc_qbuf(file, fh, p);
740 if (!ret)
741 dbgbuf(cmd,vfd,p);
742 break;
743 }
744 case VIDIOC_DQBUF:
745 {
746 struct v4l2_buffer *p=arg;
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300747 if (!vfd->vidioc_dqbuf)
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300748 break;
749 ret = check_fmt (vfd, p->type);
750 if (ret)
751 break;
752
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300753 ret=vfd->vidioc_dqbuf(file, fh, p);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300754 if (!ret)
755 dbgbuf(cmd,vfd,p);
756 break;
757 }
758 case VIDIOC_OVERLAY:
759 {
760 int *i = arg;
761
762 if (!vfd->vidioc_overlay)
763 break;
764 dbgarg (cmd, "value=%d\n",*i);
765 ret=vfd->vidioc_overlay(file, fh, *i);
766 break;
767 }
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -0300768#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300769 /* --- streaming capture ------------------------------------- */
770 case VIDIOCGMBUF:
771 {
772 struct video_mbuf *p=arg;
773
Mauro Carvalho Chehab4ceb04e2006-06-17 08:52:30 -0300774 memset(p,0,sizeof(p));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300775
776 if (!vfd->vidiocgmbuf)
777 break;
778 ret=vfd->vidiocgmbuf(file, fh, p);
779 if (!ret)
780 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
781 p->size, p->frames,
782 (unsigned long)p->offsets);
783 break;
784 }
785#endif
786 case VIDIOC_G_FBUF:
787 {
788 struct v4l2_framebuffer *p=arg;
789 if (!vfd->vidioc_g_fbuf)
790 break;
791 ret=vfd->vidioc_g_fbuf(file, fh, arg);
792 if (!ret) {
793 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
794 p->capability,p->flags,
795 (unsigned long)p->base);
796 v4l_print_pix_fmt (vfd, &p->fmt);
797 }
798 break;
799 }
800 case VIDIOC_S_FBUF:
801 {
802 struct v4l2_framebuffer *p=arg;
803 if (!vfd->vidioc_s_fbuf)
804 break;
805
806 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
807 p->capability,p->flags,(unsigned long)p->base);
808 v4l_print_pix_fmt (vfd, &p->fmt);
809 ret=vfd->vidioc_s_fbuf(file, fh, arg);
810
811 break;
812 }
813 case VIDIOC_STREAMON:
814 {
815 enum v4l2_buf_type i = *(int *)arg;
816 if (!vfd->vidioc_streamon)
817 break;
818 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
819 ret=vfd->vidioc_streamon(file, fh,i);
820 break;
821 }
822 case VIDIOC_STREAMOFF:
823 {
824 enum v4l2_buf_type i = *(int *)arg;
825
826 if (!vfd->vidioc_streamoff)
827 break;
828 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
829 ret=vfd->vidioc_streamoff(file, fh, i);
830 break;
831 }
832 /* ---------- tv norms ---------- */
833 case VIDIOC_ENUMSTD:
834 {
835 struct v4l2_standard *p = arg;
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300836 v4l2_std_id id = vfd->tvnorms,curr_id=0;
837 unsigned int index = p->index,i;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300838
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300839 if (index<0) {
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300840 ret=-EINVAL;
841 break;
842 }
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300843
844 /* Return norm array on a canonical way */
845 for (i=0;i<= index && id; i++) {
846 if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
847 curr_id = V4L2_STD_PAL;
848 } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
849 curr_id = V4L2_STD_PAL_BG;
850 } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
851 curr_id = V4L2_STD_PAL_DK;
852 } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
853 curr_id = V4L2_STD_PAL_B;
854 } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
855 curr_id = V4L2_STD_PAL_B1;
856 } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
857 curr_id = V4L2_STD_PAL_G;
858 } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
859 curr_id = V4L2_STD_PAL_H;
860 } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
861 curr_id = V4L2_STD_PAL_I;
862 } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
863 curr_id = V4L2_STD_PAL_D;
864 } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
865 curr_id = V4L2_STD_PAL_D1;
866 } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
867 curr_id = V4L2_STD_PAL_K;
868 } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
869 curr_id = V4L2_STD_PAL_M;
870 } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
871 curr_id = V4L2_STD_PAL_N;
872 } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
873 curr_id = V4L2_STD_PAL_Nc;
874 } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
875 curr_id = V4L2_STD_PAL_60;
876 } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
877 curr_id = V4L2_STD_NTSC;
878 } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
879 curr_id = V4L2_STD_NTSC_M;
880 } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
881 curr_id = V4L2_STD_NTSC_M_JP;
882 } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
883 curr_id = V4L2_STD_NTSC_443;
884 } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
885 curr_id = V4L2_STD_NTSC_M_KR;
886 } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
887 curr_id = V4L2_STD_SECAM;
888 } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
889 curr_id = V4L2_STD_SECAM_DK;
890 } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
891 curr_id = V4L2_STD_SECAM_B;
892 } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
893 curr_id = V4L2_STD_SECAM_D;
894 } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
895 curr_id = V4L2_STD_SECAM_G;
896 } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
897 curr_id = V4L2_STD_SECAM_H;
898 } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
899 curr_id = V4L2_STD_SECAM_K;
900 } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
901 curr_id = V4L2_STD_SECAM_K1;
902 } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
903 curr_id = V4L2_STD_SECAM_L;
904 } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
905 curr_id = V4L2_STD_SECAM_LC;
906 } else {
907 break;
908 }
909 id &= ~curr_id;
910 }
911 if (i<=index)
912 return -EINVAL;
913
914 v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300915 p->index = index;
916
917 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
918 "framelines=%d\n", p->index,
919 (unsigned long long)p->id, p->name,
920 p->frameperiod.numerator,
921 p->frameperiod.denominator,
922 p->framelines);
923
924 ret=0;
925 break;
926 }
927 case VIDIOC_G_STD:
928 {
929 v4l2_std_id *id = arg;
930
931 *id = vfd->current_norm;
932
933 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
934
935 ret=0;
936 break;
937 }
938 case VIDIOC_S_STD:
939 {
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300940 v4l2_std_id *id = arg,norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300941
942 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
943
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300944 norm = (*id) & vfd->tvnorms;
945 if ( vfd->tvnorms && !norm) /* Check if std is supported */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300946 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300947
948 /* Calls the specific handler */
949 if (vfd->vidioc_s_std)
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300950 ret=vfd->vidioc_s_std(file, fh, &norm);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300951 else
952 ret=-EINVAL;
953
954 /* Updates standard information */
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300955 if (ret>=0)
956 vfd->current_norm=norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300957
958 break;
959 }
960 case VIDIOC_QUERYSTD:
961 {
962 v4l2_std_id *p=arg;
963
964 if (!vfd->vidioc_querystd)
965 break;
966 ret=vfd->vidioc_querystd(file, fh, arg);
967 if (!ret)
968 dbgarg (cmd, "detected std=%Lu\n",
969 (unsigned long long)*p);
970 break;
971 }
972 /* ------ input switching ---------- */
973 /* FIXME: Inputs can be handled inside videodev2 */
974 case VIDIOC_ENUMINPUT:
975 {
976 struct v4l2_input *p=arg;
977 int i=p->index;
978
979 if (!vfd->vidioc_enum_input)
980 break;
981 memset(p, 0, sizeof(*p));
982 p->index=i;
983
984 ret=vfd->vidioc_enum_input(file, fh, p);
985 if (!ret)
986 dbgarg (cmd, "index=%d, name=%s, type=%d, "
987 "audioset=%d, "
988 "tuner=%d, std=%Ld, status=%d\n",
989 p->index,p->name,p->type,p->audioset,
990 p->tuner,
991 (unsigned long long)p->std,
992 p->status);
993 break;
994 }
995 case VIDIOC_G_INPUT:
996 {
997 unsigned int *i = arg;
998
999 if (!vfd->vidioc_g_input)
1000 break;
1001 ret=vfd->vidioc_g_input(file, fh, i);
1002 if (!ret)
1003 dbgarg (cmd, "value=%d\n",*i);
1004 break;
1005 }
1006 case VIDIOC_S_INPUT:
1007 {
1008 unsigned int *i = arg;
1009
1010 if (!vfd->vidioc_s_input)
1011 break;
1012 dbgarg (cmd, "value=%d\n",*i);
1013 ret=vfd->vidioc_s_input(file, fh, *i);
1014 break;
1015 }
1016
1017 /* ------ output switching ---------- */
1018 case VIDIOC_G_OUTPUT:
1019 {
1020 unsigned int *i = arg;
1021
1022 if (!vfd->vidioc_g_output)
1023 break;
1024 ret=vfd->vidioc_g_output(file, fh, i);
1025 if (!ret)
1026 dbgarg (cmd, "value=%d\n",*i);
1027 break;
1028 }
1029 case VIDIOC_S_OUTPUT:
1030 {
1031 unsigned int *i = arg;
1032
1033 if (!vfd->vidioc_s_output)
1034 break;
1035 dbgarg (cmd, "value=%d\n",*i);
1036 ret=vfd->vidioc_s_output(file, fh, *i);
1037 break;
1038 }
1039
1040 /* --- controls ---------------------------------------------- */
1041 case VIDIOC_QUERYCTRL:
1042 {
1043 struct v4l2_queryctrl *p=arg;
1044
1045 if (!vfd->vidioc_queryctrl)
1046 break;
1047 ret=vfd->vidioc_queryctrl(file, fh, p);
1048
1049 if (!ret)
1050 dbgarg (cmd, "id=%d, type=%d, name=%s, "
1051 "min/max=%d/%d,"
1052 " step=%d, default=%d, flags=0x%08x\n",
1053 p->id,p->type,p->name,p->minimum,
1054 p->maximum,p->step,p->default_value,
1055 p->flags);
1056 break;
1057 }
1058 case VIDIOC_G_CTRL:
1059 {
1060 struct v4l2_control *p = arg;
1061
1062 if (!vfd->vidioc_g_ctrl)
1063 break;
1064 dbgarg(cmd, "Enum for index=%d\n", p->id);
1065
1066 ret=vfd->vidioc_g_ctrl(file, fh, p);
1067 if (!ret)
1068 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1069 break;
1070 }
1071 case VIDIOC_S_CTRL:
1072 {
1073 struct v4l2_control *p = arg;
1074
1075 if (!vfd->vidioc_s_ctrl)
1076 break;
1077 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1078
1079 ret=vfd->vidioc_s_ctrl(file, fh, p);
1080 break;
1081 }
Hans Verkuil05976912006-06-18 13:43:28 -03001082 case VIDIOC_G_EXT_CTRLS:
1083 {
1084 struct v4l2_ext_controls *p = arg;
1085
1086 if (vfd->vidioc_g_ext_ctrls) {
1087 dbgarg(cmd, "count=%d\n", p->count);
1088
1089 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1090 }
1091 break;
1092 }
1093 case VIDIOC_S_EXT_CTRLS:
1094 {
1095 struct v4l2_ext_controls *p = arg;
1096
1097 if (vfd->vidioc_s_ext_ctrls) {
1098 dbgarg(cmd, "count=%d\n", p->count);
1099
1100 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1101 }
1102 break;
1103 }
1104 case VIDIOC_TRY_EXT_CTRLS:
1105 {
1106 struct v4l2_ext_controls *p = arg;
1107
1108 if (vfd->vidioc_try_ext_ctrls) {
1109 dbgarg(cmd, "count=%d\n", p->count);
1110
1111 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1112 }
1113 break;
1114 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001115 case VIDIOC_QUERYMENU:
1116 {
1117 struct v4l2_querymenu *p=arg;
1118 if (!vfd->vidioc_querymenu)
1119 break;
1120 ret=vfd->vidioc_querymenu(file, fh, p);
1121 if (!ret)
1122 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1123 p->id,p->index,p->name);
1124 break;
1125 }
1126 /* --- audio ---------------------------------------------- */
1127 case VIDIOC_ENUMAUDIO:
1128 {
1129 struct v4l2_audio *p=arg;
1130
1131 if (!vfd->vidioc_enumaudio)
1132 break;
1133 dbgarg(cmd, "Enum for index=%d\n", p->index);
1134 ret=vfd->vidioc_enumaudio(file, fh, p);
1135 if (!ret)
1136 dbgarg2("index=%d, name=%s, capability=%d, "
1137 "mode=%d\n",p->index,p->name,
1138 p->capability, p->mode);
1139 break;
1140 }
1141 case VIDIOC_G_AUDIO:
1142 {
1143 struct v4l2_audio *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001144 __u32 index=p->index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001145
1146 if (!vfd->vidioc_g_audio)
1147 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001148
1149 memset(p,0,sizeof(*p));
1150 p->index=index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001151 dbgarg(cmd, "Get for index=%d\n", p->index);
1152 ret=vfd->vidioc_g_audio(file, fh, p);
1153 if (!ret)
1154 dbgarg2("index=%d, name=%s, capability=%d, "
1155 "mode=%d\n",p->index,
1156 p->name,p->capability, p->mode);
1157 break;
1158 }
1159 case VIDIOC_S_AUDIO:
1160 {
1161 struct v4l2_audio *p=arg;
1162
1163 if (!vfd->vidioc_s_audio)
1164 break;
1165 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1166 "mode=%d\n", p->index, p->name,
1167 p->capability, p->mode);
1168 ret=vfd->vidioc_s_audio(file, fh, p);
1169 break;
1170 }
1171 case VIDIOC_ENUMAUDOUT:
1172 {
1173 struct v4l2_audioout *p=arg;
1174
1175 if (!vfd->vidioc_enumaudout)
1176 break;
1177 dbgarg(cmd, "Enum for index=%d\n", p->index);
1178 ret=vfd->vidioc_enumaudout(file, fh, p);
1179 if (!ret)
1180 dbgarg2("index=%d, name=%s, capability=%d, "
1181 "mode=%d\n", p->index, p->name,
1182 p->capability,p->mode);
1183 break;
1184 }
1185 case VIDIOC_G_AUDOUT:
1186 {
1187 struct v4l2_audioout *p=arg;
1188
1189 if (!vfd->vidioc_g_audout)
1190 break;
1191 dbgarg(cmd, "Enum for index=%d\n", p->index);
1192 ret=vfd->vidioc_g_audout(file, fh, p);
1193 if (!ret)
1194 dbgarg2("index=%d, name=%s, capability=%d, "
1195 "mode=%d\n", p->index, p->name,
1196 p->capability,p->mode);
1197 break;
1198 }
1199 case VIDIOC_S_AUDOUT:
1200 {
1201 struct v4l2_audioout *p=arg;
1202
1203 if (!vfd->vidioc_s_audout)
1204 break;
1205 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1206 "mode=%d\n", p->index, p->name,
1207 p->capability,p->mode);
1208
1209 ret=vfd->vidioc_s_audout(file, fh, p);
1210 break;
1211 }
1212 case VIDIOC_G_MODULATOR:
1213 {
1214 struct v4l2_modulator *p=arg;
1215 if (!vfd->vidioc_g_modulator)
1216 break;
1217 ret=vfd->vidioc_g_modulator(file, fh, p);
1218 if (!ret)
1219 dbgarg(cmd, "index=%d, name=%s, "
1220 "capability=%d, rangelow=%d,"
1221 " rangehigh=%d, txsubchans=%d\n",
1222 p->index, p->name,p->capability,
1223 p->rangelow, p->rangehigh,
1224 p->txsubchans);
1225 break;
1226 }
1227 case VIDIOC_S_MODULATOR:
1228 {
1229 struct v4l2_modulator *p=arg;
1230 if (!vfd->vidioc_s_modulator)
1231 break;
1232 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1233 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1234 p->index, p->name,p->capability,p->rangelow,
1235 p->rangehigh,p->txsubchans);
1236 ret=vfd->vidioc_s_modulator(file, fh, p);
1237 break;
1238 }
1239 case VIDIOC_G_CROP:
1240 {
1241 struct v4l2_crop *p=arg;
1242 if (!vfd->vidioc_g_crop)
1243 break;
1244 ret=vfd->vidioc_g_crop(file, fh, p);
1245 if (!ret) {
1246 dbgarg(cmd, "type=%d\n", p->type);
1247 dbgrect(vfd, "", &p->c);
1248 }
1249 break;
1250 }
1251 case VIDIOC_S_CROP:
1252 {
1253 struct v4l2_crop *p=arg;
1254 if (!vfd->vidioc_s_crop)
1255 break;
1256 dbgarg(cmd, "type=%d\n", p->type);
1257 dbgrect(vfd, "", &p->c);
1258 ret=vfd->vidioc_s_crop(file, fh, p);
1259 break;
1260 }
1261 case VIDIOC_CROPCAP:
1262 {
1263 struct v4l2_cropcap *p=arg;
1264 /*FIXME: Should also show v4l2_fract pixelaspect */
1265 if (!vfd->vidioc_cropcap)
1266 break;
1267 dbgarg(cmd, "type=%d\n", p->type);
1268 dbgrect(vfd, "bounds ", &p->bounds);
1269 dbgrect(vfd, "defrect ", &p->defrect);
1270 ret=vfd->vidioc_cropcap(file, fh, p);
1271 break;
1272 }
1273 case VIDIOC_G_MPEGCOMP:
1274 {
1275 struct v4l2_mpeg_compression *p=arg;
Hans Verkuilf81cf752006-06-18 16:54:20 -03001276
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001277 /*FIXME: Several fields not shown */
1278 if (!vfd->vidioc_g_mpegcomp)
1279 break;
1280 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1281 if (!ret)
1282 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1283 " ts_pid_video=%d, ts_pid_pcr=%d, "
1284 "ps_size=%d, au_sample_rate=%d, "
1285 "au_pesid=%c, vi_frame_rate=%d, "
1286 "vi_frames_per_gop=%d, "
1287 "vi_bframes_count=%d, vi_pesid=%c\n",
1288 p->ts_pid_pmt,p->ts_pid_audio,
1289 p->ts_pid_video,p->ts_pid_pcr,
1290 p->ps_size, p->au_sample_rate,
1291 p->au_pesid, p->vi_frame_rate,
1292 p->vi_frames_per_gop,
1293 p->vi_bframes_count, p->vi_pesid);
1294 break;
1295 }
1296 case VIDIOC_S_MPEGCOMP:
1297 {
1298 struct v4l2_mpeg_compression *p=arg;
1299 /*FIXME: Several fields not shown */
1300 if (!vfd->vidioc_s_mpegcomp)
1301 break;
1302 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1303 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1304 "au_sample_rate=%d, au_pesid=%c, "
1305 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1306 "vi_bframes_count=%d, vi_pesid=%c\n",
1307 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1308 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1309 p->au_pesid, p->vi_frame_rate,
1310 p->vi_frames_per_gop, p->vi_bframes_count,
1311 p->vi_pesid);
1312 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1313 break;
1314 }
1315 case VIDIOC_G_JPEGCOMP:
1316 {
1317 struct v4l2_jpegcompression *p=arg;
1318 if (!vfd->vidioc_g_jpegcomp)
1319 break;
1320 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1321 if (!ret)
1322 dbgarg (cmd, "quality=%d, APPn=%d, "
1323 "APP_len=%d, COM_len=%d, "
1324 "jpeg_markers=%d\n",
1325 p->quality,p->APPn,p->APP_len,
1326 p->COM_len,p->jpeg_markers);
1327 break;
1328 }
1329 case VIDIOC_S_JPEGCOMP:
1330 {
1331 struct v4l2_jpegcompression *p=arg;
1332 if (!vfd->vidioc_g_jpegcomp)
1333 break;
1334 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1335 "COM_len=%d, jpeg_markers=%d\n",
1336 p->quality,p->APPn,p->APP_len,
1337 p->COM_len,p->jpeg_markers);
1338 ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1339 break;
1340 }
1341 case VIDIOC_G_PARM:
1342 {
1343 struct v4l2_streamparm *p=arg;
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001344 if (vfd->vidioc_g_parm) {
1345 ret=vfd->vidioc_g_parm(file, fh, p);
1346 } else {
1347 struct v4l2_standard s;
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001348
1349 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1350 return -EINVAL;
1351
Jonathan Corbet83427ac2006-10-13 07:51:16 -03001352 v4l2_video_std_construct(&s, vfd->current_norm,
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001353 v4l2_norm_to_name(vfd->current_norm));
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001354
1355 memset(p,0,sizeof(*p));
1356
1357 p->parm.capture.timeperframe = s.frameperiod;
1358 ret=0;
1359 }
1360
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001361 dbgarg (cmd, "type=%d\n", p->type);
1362 break;
1363 }
1364 case VIDIOC_S_PARM:
1365 {
1366 struct v4l2_streamparm *p=arg;
1367 if (!vfd->vidioc_s_parm)
1368 break;
1369 dbgarg (cmd, "type=%d\n", p->type);
1370 ret=vfd->vidioc_s_parm(file, fh, p);
1371 break;
1372 }
1373 case VIDIOC_G_TUNER:
1374 {
1375 struct v4l2_tuner *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001376 __u32 index=p->index;
1377
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001378 if (!vfd->vidioc_g_tuner)
1379 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001380
1381 memset(p,0,sizeof(*p));
1382 p->index=index;
1383
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001384 ret=vfd->vidioc_g_tuner(file, fh, p);
1385 if (!ret)
1386 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1387 "capability=%d, rangelow=%d, "
1388 "rangehigh=%d, signal=%d, afc=%d, "
1389 "rxsubchans=%d, audmode=%d\n",
1390 p->index, p->name, p->type,
1391 p->capability, p->rangelow,
1392 p->rangehigh, p->rxsubchans,
1393 p->audmode, p->signal, p->afc);
1394 break;
1395 }
1396 case VIDIOC_S_TUNER:
1397 {
1398 struct v4l2_tuner *p=arg;
1399 if (!vfd->vidioc_s_tuner)
1400 break;
1401 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1402 "capability=%d, rangelow=%d, rangehigh=%d, "
1403 "signal=%d, afc=%d, rxsubchans=%d, "
1404 "audmode=%d\n",p->index, p->name, p->type,
1405 p->capability, p->rangelow,p->rangehigh,
1406 p->rxsubchans, p->audmode, p->signal,
1407 p->afc);
1408 ret=vfd->vidioc_s_tuner(file, fh, p);
1409 break;
1410 }
1411 case VIDIOC_G_FREQUENCY:
1412 {
1413 struct v4l2_frequency *p=arg;
1414 if (!vfd->vidioc_g_frequency)
1415 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001416
1417 memset(p,0,sizeof(*p));
1418
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001419 ret=vfd->vidioc_g_frequency(file, fh, p);
1420 if (!ret)
1421 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1422 p->tuner,p->type,p->frequency);
1423 break;
1424 }
1425 case VIDIOC_S_FREQUENCY:
1426 {
1427 struct v4l2_frequency *p=arg;
1428 if (!vfd->vidioc_s_frequency)
1429 break;
1430 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1431 p->tuner,p->type,p->frequency);
1432 ret=vfd->vidioc_s_frequency(file, fh, p);
1433 break;
1434 }
1435 case VIDIOC_G_SLICED_VBI_CAP:
1436 {
1437 struct v4l2_sliced_vbi_cap *p=arg;
1438 if (!vfd->vidioc_g_sliced_vbi_cap)
1439 break;
1440 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1441 if (!ret)
1442 dbgarg (cmd, "service_set=%d\n", p->service_set);
1443 break;
1444 }
1445 case VIDIOC_LOG_STATUS:
1446 {
1447 if (!vfd->vidioc_log_status)
1448 break;
1449 ret=vfd->vidioc_log_status(file, fh);
1450 break;
1451 }
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -03001452 } /* switch */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001453
1454 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1455 if (ret<0) {
1456 printk ("%s: err:\n", vfd->name);
1457 v4l_print_ioctl(vfd->name, cmd);
1458 }
1459 }
1460
1461 return ret;
1462}
1463
1464int video_ioctl2 (struct inode *inode, struct file *file,
1465 unsigned int cmd, unsigned long arg)
1466{
1467 char sbuf[128];
1468 void *mbuf = NULL;
1469 void *parg = NULL;
1470 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001471 int is_ext_ctrl;
1472 size_t ctrls_size = 0;
1473 void __user *user_ptr = NULL;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001474
1475#ifdef __OLD_VIDIOC_
1476 cmd = video_fix_command(cmd);
1477#endif
Hans Verkuil05976912006-06-18 13:43:28 -03001478 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1479 cmd == VIDIOC_TRY_EXT_CTRLS);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001480
1481 /* Copy arguments into temp kernel buffer */
1482 switch (_IOC_DIR(cmd)) {
1483 case _IOC_NONE:
1484 parg = NULL;
1485 break;
1486 case _IOC_READ:
1487 case _IOC_WRITE:
1488 case (_IOC_WRITE | _IOC_READ):
1489 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1490 parg = sbuf;
1491 } else {
1492 /* too big to allocate from stack */
1493 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1494 if (NULL == mbuf)
1495 return -ENOMEM;
1496 parg = mbuf;
1497 }
1498
1499 err = -EFAULT;
1500 if (_IOC_DIR(cmd) & _IOC_WRITE)
1501 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1502 goto out;
1503 break;
1504 }
1505
Hans Verkuil05976912006-06-18 13:43:28 -03001506 if (is_ext_ctrl) {
1507 struct v4l2_ext_controls *p = parg;
1508
1509 /* In case of an error, tell the caller that it wasn't
1510 a specific control that caused it. */
1511 p->error_idx = p->count;
1512 user_ptr = (void __user *)p->controls;
1513 if (p->count) {
1514 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1515 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1516 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1517 err = -ENOMEM;
1518 if (NULL == mbuf)
1519 goto out_ext_ctrl;
1520 err = -EFAULT;
1521 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1522 goto out_ext_ctrl;
1523 p->controls = mbuf;
1524 }
1525 }
1526
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001527 /* Handles IOCTL */
1528 err = __video_do_ioctl(inode, file, cmd, parg);
1529 if (err == -ENOIOCTLCMD)
1530 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001531 if (is_ext_ctrl) {
1532 struct v4l2_ext_controls *p = parg;
1533
1534 p->controls = (void *)user_ptr;
1535 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1536 err = -EFAULT;
1537 goto out_ext_ctrl;
1538 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001539 if (err < 0)
1540 goto out;
1541
Hans Verkuil05976912006-06-18 13:43:28 -03001542out_ext_ctrl:
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001543 /* Copy results into user buffer */
1544 switch (_IOC_DIR(cmd))
1545 {
1546 case _IOC_READ:
1547 case (_IOC_WRITE | _IOC_READ):
1548 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1549 err = -EFAULT;
1550 break;
1551 }
1552
1553out:
1554 kfree(mbuf);
1555 return err;
1556}
1557
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static struct file_operations video_fops;
1560
1561/**
1562 * video_register_device - register video4linux devices
1563 * @vfd: video device structure we want to register
1564 * @type: type of device to register
1565 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
1566 * -1 == first free)
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * The registration code assigns minor numbers based on the type
1569 * requested. -ENFILE is returned in all the device slots for this
1570 * category are full. If not then the minor field is set and the
1571 * driver initialize function is called (if non %NULL).
1572 *
1573 * Zero is returned on success.
1574 *
1575 * Valid types are
1576 *
1577 * %VFL_TYPE_GRABBER - A frame grabber
1578 *
1579 * %VFL_TYPE_VTX - A teletext device
1580 *
1581 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1582 *
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001583 * %VFL_TYPE_RADIO - A radio card
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585
1586int video_register_device(struct video_device *vfd, int type, int nr)
1587{
1588 int i=0;
1589 int base;
1590 int end;
Michael Krufky3117bee2006-07-19 13:23:38 -03001591 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 char *name_base;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 switch(type)
1595 {
1596 case VFL_TYPE_GRABBER:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001597 base=MINOR_VFL_TYPE_GRABBER_MIN;
1598 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 name_base = "video";
1600 break;
1601 case VFL_TYPE_VTX:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001602 base=MINOR_VFL_TYPE_VTX_MIN;
1603 end=MINOR_VFL_TYPE_VTX_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 name_base = "vtx";
1605 break;
1606 case VFL_TYPE_VBI:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001607 base=MINOR_VFL_TYPE_VBI_MIN;
1608 end=MINOR_VFL_TYPE_VBI_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 name_base = "vbi";
1610 break;
1611 case VFL_TYPE_RADIO:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001612 base=MINOR_VFL_TYPE_RADIO_MIN;
1613 end=MINOR_VFL_TYPE_RADIO_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 name_base = "radio";
1615 break;
1616 default:
Trent Piepho53dd8de2006-07-25 09:31:42 -03001617 printk(KERN_ERR "%s called with unknown type: %d\n",
1618 __FUNCTION__, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -1;
1620 }
1621
1622 /* pick a minor number */
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001623 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (nr >= 0 && nr < end-base) {
1625 /* use the one the driver asked for */
1626 i = base+nr;
1627 if (NULL != video_device[i]) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001628 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return -ENFILE;
1630 }
1631 } else {
1632 /* use first free */
1633 for(i=base;i<end;i++)
1634 if (NULL == video_device[i])
1635 break;
1636 if (i == end) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001637 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return -ENFILE;
1639 }
1640 }
1641 video_device[i]=vfd;
1642 vfd->minor=i;
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001643 mutex_unlock(&videodev_lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001644 mutex_init(&vfd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* sysfs class */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001647 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (vfd->dev)
1649 vfd->class_dev.dev = vfd->dev;
1650 vfd->class_dev.class = &video_class;
Michael Krufky50c25ff2006-01-09 15:25:34 -02001651 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Greg Kroah-Hartman5e483072005-06-20 21:15:16 -07001652 sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
Michael Krufky3117bee2006-07-19 13:23:38 -03001653 ret = class_device_register(&vfd->class_dev);
Trent Piepho8c313112006-07-25 20:37:03 -03001654 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -03001655 printk(KERN_ERR "%s: class_device_register failed\n",
1656 __FUNCTION__);
Trent Piephod94fc9a2006-07-29 17:18:06 -03001657 goto fail_minor;
Michael Krufky3117bee2006-07-19 13:23:38 -03001658 }
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001659 ret = class_device_create_file(&vfd->class_dev, &class_device_attr_name);
1660 if (ret < 0) {
Trent Piephod94fc9a2006-07-29 17:18:06 -03001661 printk(KERN_ERR "%s: class_device_create_file 'name' failed\n",
1662 __FUNCTION__);
1663 goto fail_classdev;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -02001666#if 1
1667 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (!vfd->release)
1669 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1670 "Please fix your driver for proper sysfs support, see "
1671 "http://lwn.net/Articles/36850/\n", vfd->name);
1672#endif
1673 return 0;
Trent Piepho53dd8de2006-07-25 09:31:42 -03001674
1675fail_classdev:
1676 class_device_unregister(&vfd->class_dev);
1677fail_minor:
1678 mutex_lock(&videodev_lock);
1679 video_device[vfd->minor] = NULL;
1680 vfd->minor = -1;
1681 mutex_unlock(&videodev_lock);
1682 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685/**
1686 * video_unregister_device - unregister a video4linux device
1687 * @vfd: the device to unregister
1688 *
1689 * This unregisters the passed device and deassigns the minor
1690 * number. Future open calls will be met with errors.
1691 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693void video_unregister_device(struct video_device *vfd)
1694{
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001695 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if(video_device[vfd->minor]!=vfd)
1697 panic("videodev: bad unregister");
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 video_device[vfd->minor]=NULL;
1700 class_device_unregister(&vfd->class_dev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001701 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001704/*
1705 * Video fs operations
1706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707static struct file_operations video_fops=
1708{
1709 .owner = THIS_MODULE,
1710 .llseek = no_llseek,
1711 .open = video_open,
1712};
1713
1714/*
1715 * Initialise video for linux
1716 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718static int __init videodev_init(void)
1719{
1720 int ret;
1721
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001722 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1724 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1725 return -EIO;
1726 }
1727
1728 ret = class_register(&video_class);
1729 if (ret < 0) {
1730 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1731 printk(KERN_WARNING "video_dev: class_register failed\n");
1732 return -EIO;
1733 }
1734
1735 return 0;
1736}
1737
1738static void __exit videodev_exit(void)
1739{
1740 class_unregister(&video_class);
1741 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1742}
1743
1744module_init(videodev_init)
1745module_exit(videodev_exit)
1746
1747EXPORT_SYMBOL(video_register_device);
1748EXPORT_SYMBOL(video_unregister_device);
1749EXPORT_SYMBOL(video_devdata);
1750EXPORT_SYMBOL(video_usercopy);
1751EXPORT_SYMBOL(video_exclusive_open);
1752EXPORT_SYMBOL(video_exclusive_release);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001753EXPORT_SYMBOL(video_ioctl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754EXPORT_SYMBOL(video_device_alloc);
1755EXPORT_SYMBOL(video_device_release);
1756
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001757MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1758MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759MODULE_LICENSE("GPL");
1760
1761
1762/*
1763 * Local variables:
1764 * c-basic-offset: 8
1765 * End:
1766 */