blob: 4d74ba36c3a12b0d8acd19cd7115b154522f862b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Telephony registration for Linux
3 *
4 * (c) Copyright 1999 Red Hat Software Inc.
5 *
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.
10 *
11 * Author: Alan Cox, <alan@redhat.com>
12 *
13 * Fixes: Mar 01 2000 Thomas Sparr, <thomas.l.sparr@telia.com>
14 * phone_register_device now works with unit!=PHONE_UNIT_ANY
15 */
16
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/errno.h>
24#include <linux/phonedev.h>
25#include <linux/init.h>
Jonathan Corbet16750c22008-05-15 11:58:31 -060026#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
28#include <asm/system.h>
29
30#include <linux/kmod.h>
31#include <linux/sem.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define PHONE_NUM_DEVICES 256
35
36/*
37 * Active devices
38 */
39
40static struct phone_device *phone_device[PHONE_NUM_DEVICES];
Ingo Molnar14cc3e22006-03-26 01:37:14 -080041static DEFINE_MUTEX(phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Open a phone device.
45 */
46
47static int phone_open(struct inode *inode, struct file *file)
48{
49 unsigned int minor = iminor(inode);
50 int err = 0;
51 struct phone_device *p;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080052 const struct file_operations *old_fops, *new_fops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if (minor >= PHONE_NUM_DEVICES)
55 return -ENODEV;
56
Jonathan Corbet16750c22008-05-15 11:58:31 -060057 lock_kernel();
Ingo Molnar14cc3e22006-03-26 01:37:14 -080058 mutex_lock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 p = phone_device[minor];
60 if (p)
61 new_fops = fops_get(p->f_op);
62 if (!new_fops) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -080063 mutex_unlock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 request_module("char-major-%d-%d", PHONE_MAJOR, minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -080065 mutex_lock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 p = phone_device[minor];
67 if (p == NULL || (new_fops = fops_get(p->f_op)) == NULL)
68 {
69 err=-ENODEV;
70 goto end;
71 }
72 }
73 old_fops = file->f_op;
74 file->f_op = new_fops;
75 if (p->open)
76 err = p->open(p, file); /* Tell the device it is open */
77 if (err) {
78 fops_put(file->f_op);
79 file->f_op = fops_get(old_fops);
80 }
81 fops_put(old_fops);
82end:
Ingo Molnar14cc3e22006-03-26 01:37:14 -080083 mutex_unlock(&phone_lock);
Jonathan Corbet16750c22008-05-15 11:58:31 -060084 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return err;
86}
87
88/*
89 * Telephony For Linux device drivers request registration here.
90 */
91
92int phone_register_device(struct phone_device *p, int unit)
93{
94 int base;
95 int end;
96 int i;
97
98 base = 0;
99 end = PHONE_NUM_DEVICES - 1;
100
101 if (unit != PHONE_UNIT_ANY) {
102 base = unit;
103 end = unit + 1; /* enter the loop at least one time */
104 }
105
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800106 mutex_lock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 for (i = base; i < end; i++) {
108 if (phone_device[i] == NULL) {
109 phone_device[i] = p;
110 p->minor = i;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800111 mutex_unlock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113 }
114 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800115 mutex_unlock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -ENFILE;
117}
118
119/*
120 * Unregister an unused Telephony for linux device
121 */
122
123void phone_unregister_device(struct phone_device *pfd)
124{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800125 mutex_lock(&phone_lock);
Matti Linnanvuori01aae972007-11-05 14:51:02 -0800126 if (likely(phone_device[pfd->minor] == pfd))
127 phone_device[pfd->minor] = NULL;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800128 mutex_unlock(&phone_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131
Arjan van de Ven00977a52007-02-12 00:55:34 -0800132static const struct file_operations phone_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 .owner = THIS_MODULE,
135 .open = phone_open,
136};
137
138/*
139 * Board init functions
140 */
141
142
143/*
144 * Initialise Telephony for linux
145 */
146
147static int __init telephony_init(void)
148{
149 printk(KERN_INFO "Linux telephony interface: v1.00\n");
150 if (register_chrdev(PHONE_MAJOR, "telephony", &phone_fops)) {
151 printk("phonedev: unable to get major %d\n", PHONE_MAJOR);
152 return -EIO;
153 }
154
155 return 0;
156}
157
158static void __exit telephony_exit(void)
159{
160 unregister_chrdev(PHONE_MAJOR, "telephony");
161}
162
163module_init(telephony_init);
164module_exit(telephony_exit);
165
166MODULE_LICENSE("GPL");
167
168EXPORT_SYMBOL(phone_register_device);
169EXPORT_SYMBOL(phone_unregister_device);