File: /usr/src/linux/drivers/media/video/videodev.c

1     /*
2      * Video capture interface for Linux
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      * Author:	Alan Cox, <alan@redhat.com>
13      *
14      * Fixes:	20000516  Claudio Matsuoka <claudio@conectiva.com>
15      *		- Added procfs support
16      */
17     
18     #include <linux/config.h>
19     #include <linux/version.h>
20     #include <linux/module.h>
21     #include <linux/types.h>
22     #include <linux/kernel.h>
23     #include <linux/sched.h>
24     #include <linux/smp_lock.h>
25     #include <linux/mm.h>
26     #include <linux/string.h>
27     #include <linux/errno.h>
28     #include <linux/videodev.h>
29     #include <linux/init.h>
30     
31     #include <asm/uaccess.h>
32     #include <asm/system.h>
33     #include <asm/semaphore.h>
34     
35     #include <linux/kmod.h>
36     
37     
38     #define VIDEO_NUM_DEVICES	256 
39     
40     /*
41      *	Active devices 
42      */
43      
44     static struct video_device *video_device[VIDEO_NUM_DEVICES];
45     
46     
47     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
48     
49     #include <linux/proc_fs.h>
50     
51     struct videodev_proc_data {
52     	struct list_head proc_list;
53     	char name[16];
54     	struct video_device *vdev;
55     	struct proc_dir_entry *proc_entry;
56     };
57     
58     static struct proc_dir_entry *video_dev_proc_entry = NULL;
59     struct proc_dir_entry *video_proc_entry = NULL;
60     EXPORT_SYMBOL(video_proc_entry);
61     LIST_HEAD(videodev_proc_list);
62     
63     #endif /* CONFIG_PROC_FS && CONFIG_VIDEO_PROC_FS */
64     
65     
66     #ifdef CONFIG_VIDEO_BWQCAM
67     extern int init_bw_qcams(struct video_init *);
68     #endif
69     #ifdef CONFIG_VIDEO_CPIA
70     extern int cpia_init(struct video_init *);
71     #endif
72     #ifdef CONFIG_VIDEO_PLANB
73     extern int init_planbs(struct video_init *);
74     #endif
75     
76     static struct video_init video_init_list[]={
77     #ifdef CONFIG_VIDEO_BWQCAM
78     	{"bw-qcam", init_bw_qcams},
79     #endif	
80     #ifdef CONFIG_VIDEO_CPIA
81     	{"cpia", cpia_init},
82     #endif	
83     #ifdef CONFIG_VIDEO_PLANB
84     	{"planb", init_planbs},
85     #endif
86     	{"end", NULL}
87     };
88     
89     /*
90      *	Read will do some smarts later on. Buffer pin etc.
91      */
92      
93     static ssize_t video_read(struct file *file,
94     	char *buf, size_t count, loff_t *ppos)
95     {
96     	struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];
97     	if(vfl->read)
98     		return vfl->read(vfl, buf, count, file->f_flags&O_NONBLOCK);
99     	else
100     		return -EINVAL;
101     }
102     
103     
104     /*
105      *	Write for now does nothing. No reason it shouldnt do overlay setting
106      *	for some boards I guess..
107      */
108     
109     static ssize_t video_write(struct file *file, const char *buf, 
110     	size_t count, loff_t *ppos)
111     {
112     	struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];
113     	if(vfl->write)
114     		return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK);
115     	else
116     		return 0;
117     }
118     
119     /*
120      *	Poll to see if we're readable, can probably be used for timing on incoming
121      *  frames, etc..
122      */
123     
124     static unsigned int video_poll(struct file *file, poll_table * wait)
125     {
126     	struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];
127     	if(vfl->poll)
128     		return vfl->poll(vfl, file, wait);
129     	else
130     		return 0;
131     }
132     
133     
134     /*
135      *	Open a video device.
136      */
137     
138     static int video_open(struct inode *inode, struct file *file)
139     {
140     	unsigned int minor = MINOR(inode->i_rdev);
141     	int err, retval = 0;
142     	struct video_device *vfl;
143     	
144     	if(minor>=VIDEO_NUM_DEVICES)
145     		return -ENODEV;
146     	lock_kernel();		
147     	vfl=video_device[minor];
148     	if(vfl==NULL) {
149     		char modname[20];
150     
151     		sprintf (modname, "char-major-%d-%d", VIDEO_MAJOR, minor);
152     		request_module(modname);
153     		vfl=video_device[minor];
154     		if (vfl==NULL) {
155     			retval = -ENODEV;
156     			goto error_out;
157     		}
158     	}
159     	if(vfl->busy) {
160     		retval = -EBUSY;
161     		goto error_out;
162     	}
163     	vfl->busy=1;		/* In case vfl->open sleeps */
164     	
165     	if(vfl->owner)
166     		__MOD_INC_USE_COUNT(vfl->owner);
167     	
168     	if(vfl->open)
169     	{
170     		err=vfl->open(vfl,0);	/* Tell the device it is open */
171     		if(err)
172     		{
173     			vfl->busy=0;
174     			if(vfl->owner)
175     				__MOD_DEC_USE_COUNT(vfl->owner);
176     			
177     			unlock_kernel();
178     			return err;
179     		}
180     	}
181     	unlock_kernel();
182     	return 0;
183     error_out:
184     	unlock_kernel();
185     	return retval;
186     }
187     
188     /*
189      *	Last close of a video for Linux device
190      */
191     	
192     static int video_release(struct inode *inode, struct file *file)
193     {
194     	struct video_device *vfl;
195     	lock_kernel();
196     	vfl=video_device[MINOR(inode->i_rdev)];
197     	if(vfl->close)
198     		vfl->close(vfl);
199     	vfl->busy=0;
200     	if(vfl->owner)
201     		__MOD_DEC_USE_COUNT(vfl->owner);
202     	unlock_kernel();
203     	return 0;
204     }
205     
206     static int video_ioctl(struct inode *inode, struct file *file,
207     	unsigned int cmd, unsigned long arg)
208     {
209     	struct video_device *vfl=video_device[MINOR(inode->i_rdev)];
210     	int err=vfl->ioctl(vfl, cmd, (void *)arg);
211     
212     	if(err!=-ENOIOCTLCMD)
213     		return err;
214     	
215     	switch(cmd)
216     	{
217     		default:
218     			return -EINVAL;
219     	}
220     }
221     
222     /*
223      *	We need to do MMAP support
224      */
225      
226      
227     int video_mmap(struct file *file, struct vm_area_struct *vma)
228     {
229     	int ret = -EINVAL;
230     	struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];
231     	if(vfl->mmap) {
232     		lock_kernel();
233     		ret = vfl->mmap(vfl, (char *)vma->vm_start, 
234     				(unsigned long)(vma->vm_end-vma->vm_start));
235     		unlock_kernel();
236     	}
237     	return ret;
238     }
239     
240     /*
241      *	/proc support
242      */
243     
244     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
245     
246     /* Hmm... i'd like to see video_capability information here, but
247      * how can I access it (without changing the other drivers? -claudio
248      */
249     static int videodev_proc_read(char *page, char **start, off_t off,
250     			       int count, int *eof, void *data)
251     {
252     	char *out = page;
253     	struct video_device *vfd = data;
254     	struct videodev_proc_data *d;
255     	struct list_head *tmp;
256     	int len;
257     	char c = ' ';
258     
259     	list_for_each (tmp, &videodev_proc_list) {
260     		d = list_entry(tmp, struct videodev_proc_data, proc_list);
261     		if (vfd == d->vdev)
262     			break;
263     	}
264     
265     	/* Sanity check */
266     	if (tmp == &videodev_proc_list)
267     		goto skip;
268     		
269     #define PRINT_VID_TYPE(x) do { if (vfd->type & x) \
270     	out += sprintf (out, "%c%s", c, #x); c='|';} while (0)
271     
272     	out += sprintf (out, "name            : %s\n", vfd->name);
273     	out += sprintf (out, "type            :");
274     		PRINT_VID_TYPE(VID_TYPE_CAPTURE);
275     		PRINT_VID_TYPE(VID_TYPE_TUNER);
276     		PRINT_VID_TYPE(VID_TYPE_TELETEXT);
277     		PRINT_VID_TYPE(VID_TYPE_OVERLAY);
278     		PRINT_VID_TYPE(VID_TYPE_CHROMAKEY);
279     		PRINT_VID_TYPE(VID_TYPE_CLIPPING);
280     		PRINT_VID_TYPE(VID_TYPE_FRAMERAM);
281     		PRINT_VID_TYPE(VID_TYPE_SCALES);
282     		PRINT_VID_TYPE(VID_TYPE_MONOCHROME);
283     		PRINT_VID_TYPE(VID_TYPE_SUBCAPTURE);
284     		PRINT_VID_TYPE(VID_TYPE_MPEG_DECODER);
285     		PRINT_VID_TYPE(VID_TYPE_MPEG_ENCODER);
286     		PRINT_VID_TYPE(VID_TYPE_MJPEG_DECODER);
287     		PRINT_VID_TYPE(VID_TYPE_MJPEG_ENCODER);
288     	out += sprintf (out, "\n");
289     	out += sprintf (out, "hardware        : 0x%x\n", vfd->hardware);
290     #if 0
291     	out += sprintf (out, "channels        : %d\n", d->vcap.channels);
292     	out += sprintf (out, "audios          : %d\n", d->vcap.audios);
293     	out += sprintf (out, "maxwidth        : %d\n", d->vcap.maxwidth);
294     	out += sprintf (out, "maxheight       : %d\n", d->vcap.maxheight);
295     	out += sprintf (out, "minwidth        : %d\n", d->vcap.minwidth);
296     	out += sprintf (out, "minheight       : %d\n", d->vcap.minheight);
297     #endif
298     
299     skip:
300     	len = out - page;
301     	len -= off;
302     	if (len < count) {
303     		*eof = 1;
304     		if (len <= 0)
305     			return 0;
306     	} else
307     		len = count;
308     
309     	*start = page + off;
310     
311     	return len;
312     }
313     
314     static void videodev_proc_create(void)
315     {
316     	video_proc_entry = create_proc_entry("video", S_IFDIR, &proc_root);
317     
318     	if (video_proc_entry == NULL) {
319     		printk("video_dev: unable to initialise /proc/video\n");
320     		return;
321     	}
322     
323     	video_proc_entry->owner = THIS_MODULE;
324     	video_dev_proc_entry = create_proc_entry("dev", S_IFDIR, video_proc_entry);
325     
326     	if (video_dev_proc_entry == NULL) {
327     		printk("video_dev: unable to initialise /proc/video/dev\n");
328     		return;
329     	}
330     
331     	video_dev_proc_entry->owner = THIS_MODULE;
332     }
333     
334     #ifdef MODULE
335     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
336     static void videodev_proc_destroy(void)
337     {
338     	if (video_dev_proc_entry != NULL)
339     		remove_proc_entry("dev", video_proc_entry);
340     
341     	if (video_proc_entry != NULL)
342     		remove_proc_entry("video", &proc_root);
343     }
344     #endif
345     #endif
346     
347     static void videodev_proc_create_dev (struct video_device *vfd, char *name)
348     {
349     	struct videodev_proc_data *d;
350     	struct proc_dir_entry *p;
351     
352     	if (video_dev_proc_entry == NULL)
353     		return;
354     
355     	d = kmalloc (sizeof (struct videodev_proc_data), GFP_KERNEL);
356     	if (!d)
357     		return;
358     
359     	p = create_proc_entry(name, S_IFREG|S_IRUGO|S_IWUSR, video_dev_proc_entry);
360     	if (!p)
361     		return;
362     	p->data = vfd;
363     	p->read_proc = videodev_proc_read;
364     
365     	d->proc_entry = p;
366     	d->vdev = vfd;
367     	strcpy (d->name, name);
368     
369     	/* How can I get capability information ? */
370     
371     	list_add (&d->proc_list, &videodev_proc_list);
372     }
373     
374     static void videodev_proc_destroy_dev (struct video_device *vfd)
375     {
376     	struct list_head *tmp;
377     	struct videodev_proc_data *d;
378     
379     	list_for_each (tmp, &videodev_proc_list) {
380     		d = list_entry(tmp, struct videodev_proc_data, proc_list);
381     		if (vfd == d->vdev) {
382     			remove_proc_entry(d->name, video_dev_proc_entry);
383     			list_del (&d->proc_list);
384     			kfree(d);
385     			break;
386     		}
387     	}
388     }
389     
390     #endif /* CONFIG_VIDEO_PROC_FS */
391     
392     extern struct file_operations video_fops;
393     
394     /**
395      *	video_register_device - register video4linux devices
396      *	@vfd:  video device structure we want to register
397      *	@type: type of device to register
398      *	@nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
399      *             -1 == first free)
400      *	
401      *	The registration code assigns minor numbers based on the type
402      *	requested. -ENFILE is returned in all the device slots for this
403      *	category are full. If not then the minor field is set and the
404      *	driver initialize function is called (if non %NULL).
405      *
406      *	Zero is returned on success.
407      *
408      *	Valid types are
409      *
410      *	%VFL_TYPE_GRABBER - A frame grabber
411      *
412      *	%VFL_TYPE_VTX - A teletext device
413      *
414      *	%VFL_TYPE_VBI - Vertical blank data (undecoded)
415      *
416      *	%VFL_TYPE_RADIO - A radio card	
417      */
418     
419     static DECLARE_MUTEX(videodev_register_lock);
420     
421     int video_register_device(struct video_device *vfd, int type, int nr)
422     {
423     	int i=0;
424     	int base;
425     	int err;
426     	int end;
427     	char *name_base;
428     	char name[16];
429     	
430     	switch(type)
431     	{
432     		case VFL_TYPE_GRABBER:
433     			base=0;
434     			end=64;
435     			name_base = "video";
436     			break;
437     		case VFL_TYPE_VTX:
438     			base=192;
439     			end=224;
440     			name_base = "vtx";
441     			break;
442     		case VFL_TYPE_VBI:
443     			base=224;
444     			end=240;
445     			name_base = "vbi";
446     			break;
447     		case VFL_TYPE_RADIO:
448     			base=64;
449     			end=128;
450     			name_base = "radio";
451     			break;
452     		default:
453     			return -1;
454     	}
455     
456     	/* pick a minor number */
457     	down(&videodev_register_lock);
458     	if (-1 == nr) {
459     		/* use first free */
460     		for(i=base;i<end;i++)
461     			if (NULL == video_device[i])
462     				break;
463     		if (i == end) {
464     			up(&videodev_register_lock);
465     			return -ENFILE;
466     		}
467     	} else {
468     		/* use the one the driver asked for */
469     		i = base+nr;
470     		if (NULL != video_device[i]) {
471     			up(&videodev_register_lock);
472     			return -ENFILE;
473     		}
474     	}
475     	video_device[i]=vfd;
476     	vfd->minor=i;
477     	up(&videodev_register_lock);
478     
479     	/* The init call may sleep so we book the slot out
480     	   then call */
481     	MOD_INC_USE_COUNT;
482     	if(vfd->initialize) {
483     		err=vfd->initialize(vfd);
484     		if(err<0) {
485     			video_device[i]=NULL;
486     			MOD_DEC_USE_COUNT;
487     			return err;
488     		}
489     	}
490     	sprintf (name, "v4l/%s%d", name_base, i - base);
491     	/*
492     	 *	Start the device root only. Anything else
493     	 *	has serious privacy issues.
494     	 */
495     	vfd->devfs_handle =
496     		devfs_register (NULL, name, DEVFS_FL_DEFAULT,
497     				VIDEO_MAJOR, vfd->minor,
498     				S_IFCHR | S_IRUSR | S_IWUSR,
499     				&video_fops,
500     				NULL);
501     	
502     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
503     	sprintf (name, "%s%d", name_base, i - base);
504     	videodev_proc_create_dev (vfd, name);
505     #endif
506     	return 0;
507     }
508     
509     /**
510      *	video_unregister_device - unregister a video4linux device
511      *	@vfd: the device to unregister
512      *
513      *	This unregisters the passed device and deassigns the minor
514      *	number. Future open calls will be met with errors.
515      */
516      
517     void video_unregister_device(struct video_device *vfd)
518     {
519     	if(video_device[vfd->minor]!=vfd)
520     		panic("vfd: bad unregister");
521     
522     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
523     	videodev_proc_destroy_dev (vfd);
524     #endif
525     
526     	devfs_unregister (vfd->devfs_handle);
527     	video_device[vfd->minor]=NULL;
528     	MOD_DEC_USE_COUNT;
529     }
530     
531     
532     static struct file_operations video_fops=
533     {
534     	owner:		THIS_MODULE,
535     	llseek:		no_llseek,
536     	read:		video_read,
537     	write:		video_write,
538     	ioctl:		video_ioctl,
539     	mmap:		video_mmap,
540     	open:		video_open,
541     	release:	video_release,
542     	poll:		video_poll,
543     };
544     
545     /*
546      *	Initialise video for linux
547      */
548      
549     static int __init videodev_init(void)
550     {
551     	struct video_init *vfli = video_init_list;
552     	
553     	printk(KERN_INFO "Linux video capture interface: v1.00\n");
554     	if(devfs_register_chrdev(VIDEO_MAJOR,"video_capture", &video_fops))
555     	{
556     		printk("video_dev: unable to get major %d\n", VIDEO_MAJOR);
557     		return -EIO;
558     	}
559     
560     	/*
561     	 *	Init kernel installed video drivers
562     	 */
563     	 	
564     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
565     	videodev_proc_create ();
566     #endif
567     	
568     	while(vfli->init!=NULL)
569     	{
570     		vfli->init(vfli);
571     		vfli++;
572     	}
573     	return 0;
574     }
575     
576     static void __exit videodev_exit(void)
577     {
578     #ifdef MODULE		
579     #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
580     	videodev_proc_destroy ();
581     #endif
582     #endif
583     	
584     	devfs_unregister_chrdev(VIDEO_MAJOR, "video_capture");
585     }
586     
587     module_init(videodev_init)
588     module_exit(videodev_exit)
589     
590     EXPORT_SYMBOL(video_register_device);
591     EXPORT_SYMBOL(video_unregister_device);
592     
593     MODULE_AUTHOR("Alan Cox");
594     MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
595     
596     /*
597      * Local variables:
598      * c-basic-offset: 8
599      * End:
600      */
601