[PATCH] phonedev has an owner so this is ok too I think
[linux-2.6/history.git] / drivers / telephony / phonedev.c
bloba5c1d3337141af06d307298aba1077b7ee00391f
1 /*
2 * Telephony registration for Linux
4 * (c) Copyright 1999 Red Hat Software Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Author: Alan Cox, <alan@redhat.com>
13 * Fixes: Mar 01 2000 Thomas Sparr, <thomas.l.sparr@telia.com>
14 * phone_register_device now works with unit!=PHONE_UNIT_ANY
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/fs.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/phonedev.h>
26 #include <linux/init.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
30 #include <linux/kmod.h>
31 #include <linux/sem.h>
34 #define PHONE_NUM_DEVICES 256
37 * Active devices
40 static struct phone_device *phone_device[PHONE_NUM_DEVICES];
41 static DECLARE_MUTEX(phone_lock);
44 * Open a phone device.
47 static int phone_open(struct inode *inode, struct file *file)
49 unsigned int minor = minor(inode->i_rdev);
50 int err = 0;
51 struct phone_device *p;
52 struct file_operations *old_fops, *new_fops = NULL;
54 if (minor >= PHONE_NUM_DEVICES)
55 return -ENODEV;
57 down(&phone_lock);
58 p = phone_device[minor];
59 if (p)
60 new_fops = fops_get(p->f_op);
61 if (!new_fops) {
62 up(&phone_lock);
63 request_module("char-major-%d-%d", PHONE_MAJOR, minor);
64 down(&phone_lock);
65 p = phone_device[minor];
66 if (p == NULL || (new_fops = fops_get(p->f_op)) == NULL)
68 err=-ENODEV;
69 goto end;
72 old_fops = file->f_op;
73 file->f_op = new_fops;
74 if (p->open)
75 err = p->open(p, file); /* Tell the device it is open */
76 if (err) {
77 fops_put(file->f_op);
78 file->f_op = fops_get(old_fops);
80 fops_put(old_fops);
81 end:
82 up(&phone_lock);
83 return err;
87 * Telephony For Linux device drivers request registration here.
90 int phone_register_device(struct phone_device *p, int unit)
92 int base;
93 int end;
94 int i;
96 base = 0;
97 end = PHONE_NUM_DEVICES - 1;
99 if (unit != PHONE_UNIT_ANY) {
100 base = unit;
101 end = unit + 1; /* enter the loop at least one time */
104 down(&phone_lock);
105 for (i = base; i < end; i++) {
106 if (phone_device[i] == NULL) {
107 phone_device[i] = p;
108 p->minor = i;
109 up(&phone_lock);
110 return 0;
113 up(&phone_lock);
114 return -ENFILE;
118 * Unregister an unused Telephony for linux device
121 void phone_unregister_device(struct phone_device *pfd)
123 down(&phone_lock);
124 if (phone_device[pfd->minor] != pfd)
125 panic("phone: bad unregister");
126 phone_device[pfd->minor] = NULL;
127 up(&phone_lock);
131 static struct file_operations phone_fops =
133 .owner = THIS_MODULE,
134 .open = phone_open,
138 * Board init functions
143 * Initialise Telephony for linux
146 static int __init telephony_init(void)
148 printk(KERN_INFO "Linux telephony interface: v1.00\n");
149 if (register_chrdev(PHONE_MAJOR, "telephony", &phone_fops)) {
150 printk("phonedev: unable to get major %d\n", PHONE_MAJOR);
151 return -EIO;
154 return 0;
157 static void __exit telephony_exit(void)
159 unregister_chrdev(PHONE_MAJOR, "telephony");
162 module_init(telephony_init);
163 module_exit(telephony_exit);
165 MODULE_LICENSE("GPL");
167 EXPORT_SYMBOL(phone_register_device);
168 EXPORT_SYMBOL(phone_unregister_device);