Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Corbet | 16750c2 | 2008-05-15 11:58:31 -0600 | [diff] [blame] | 26 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/uaccess.h> |
| 28 | #include <asm/system.h> |
| 29 | |
| 30 | #include <linux/kmod.h> |
| 31 | #include <linux/sem.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #define PHONE_NUM_DEVICES 256 |
| 35 | |
| 36 | /* |
| 37 | * Active devices |
| 38 | */ |
| 39 | |
| 40 | static struct phone_device *phone_device[PHONE_NUM_DEVICES]; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 41 | static DEFINE_MUTEX(phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Open a phone device. |
| 45 | */ |
| 46 | |
| 47 | static 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 Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 52 | const struct file_operations *old_fops, *new_fops = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | if (minor >= PHONE_NUM_DEVICES) |
| 55 | return -ENODEV; |
| 56 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 57 | mutex_lock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | p = phone_device[minor]; |
| 59 | if (p) |
| 60 | new_fops = fops_get(p->f_op); |
| 61 | if (!new_fops) { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 62 | mutex_unlock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | request_module("char-major-%d-%d", PHONE_MAJOR, minor); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 64 | mutex_lock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | p = phone_device[minor]; |
| 66 | if (p == NULL || (new_fops = fops_get(p->f_op)) == NULL) |
| 67 | { |
| 68 | err=-ENODEV; |
| 69 | goto end; |
| 70 | } |
| 71 | } |
| 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); |
| 79 | } |
| 80 | fops_put(old_fops); |
| 81 | end: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 82 | mutex_unlock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Telephony For Linux device drivers request registration here. |
| 88 | */ |
| 89 | |
| 90 | int phone_register_device(struct phone_device *p, int unit) |
| 91 | { |
| 92 | int base; |
| 93 | int end; |
| 94 | int i; |
| 95 | |
| 96 | base = 0; |
| 97 | end = PHONE_NUM_DEVICES - 1; |
| 98 | |
| 99 | if (unit != PHONE_UNIT_ANY) { |
| 100 | base = unit; |
| 101 | end = unit + 1; /* enter the loop at least one time */ |
| 102 | } |
| 103 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 104 | mutex_lock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | for (i = base; i < end; i++) { |
| 106 | if (phone_device[i] == NULL) { |
| 107 | phone_device[i] = p; |
| 108 | p->minor = i; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 109 | mutex_unlock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 113 | mutex_unlock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return -ENFILE; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Unregister an unused Telephony for linux device |
| 119 | */ |
| 120 | |
| 121 | void phone_unregister_device(struct phone_device *pfd) |
| 122 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 123 | mutex_lock(&phone_lock); |
Matti Linnanvuori | 01aae97 | 2007-11-05 14:51:02 -0800 | [diff] [blame] | 124 | if (likely(phone_device[pfd->minor] == pfd)) |
| 125 | phone_device[pfd->minor] = NULL; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 126 | mutex_unlock(&phone_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 130 | static const struct file_operations phone_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | .owner = THIS_MODULE, |
| 133 | .open = phone_open, |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Board init functions |
| 138 | */ |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Initialise Telephony for linux |
| 143 | */ |
| 144 | |
| 145 | static int __init telephony_init(void) |
| 146 | { |
| 147 | printk(KERN_INFO "Linux telephony interface: v1.00\n"); |
| 148 | if (register_chrdev(PHONE_MAJOR, "telephony", &phone_fops)) { |
| 149 | printk("phonedev: unable to get major %d\n", PHONE_MAJOR); |
| 150 | return -EIO; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static void __exit telephony_exit(void) |
| 157 | { |
| 158 | unregister_chrdev(PHONE_MAJOR, "telephony"); |
| 159 | } |
| 160 | |
| 161 | module_init(telephony_init); |
| 162 | module_exit(telephony_exit); |
| 163 | |
| 164 | MODULE_LICENSE("GPL"); |
| 165 | |
| 166 | EXPORT_SYMBOL(phone_register_device); |
| 167 | EXPORT_SYMBOL(phone_unregister_device); |