aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-07 15:23:31 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-07 15:23:31 -0400
commit9fa0853a85a3a4067e4ad0aaa5d90984c2dd21b5 (patch)
treeac90f6535bc053b3859dc050cbbd577a0a1ef95b /net
parentef93127e4c7b4b8d46421045641048397eaac43d (diff)
parentcf4328cd949c2086091c62c5685f1580fe9b55e4 (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6: [NET]: rfkill: add support for input key to control wireless radio [NET] net/core: Fix error handling [TG3]: Update version and reldate. [TG3]: Eliminate spurious interrupts. [TG3]: Add ASPM workaround. [Bluetooth] Correct SCO buffer for another Broadcom based dongle [Bluetooth] Add support for Targus ACB10US USB dongle [Bluetooth] Disconnect L2CAP connection after last RFCOMM DLC [Bluetooth] Check that device is in rfcomm_dev_list before deleting [Bluetooth] Use in-kernel sockets API [Bluetooth] Attach host adapters to the Bluetooth bus [Bluetooth] Fix L2CAP and HCI setsockopt() information leaks
Diffstat (limited to 'net')
-rw-r--r--net/Kconfig2
-rw-r--r--net/Makefile1
-rw-r--r--net/bluetooth/hci_sock.c9
-rw-r--r--net/bluetooth/hci_sysfs.c9
-rw-r--r--net/bluetooth/l2cap.c6
-rw-r--r--net/bluetooth/rfcomm/core.c29
-rw-r--r--net/bluetooth/rfcomm/tty.c11
-rw-r--r--net/core/dev.c4
-rw-r--r--net/rfkill/Kconfig24
-rw-r--r--net/rfkill/Makefile6
-rw-r--r--net/rfkill/rfkill-input.c174
-rw-r--r--net/rfkill/rfkill.c407
12 files changed, 663 insertions, 19 deletions
diff --git a/net/Kconfig b/net/Kconfig
index 5b2227813923..caeacd16656a 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -225,6 +225,8 @@ source "net/ieee80211/Kconfig"
225 225
226endmenu 226endmenu
227 227
228source "net/rfkill/Kconfig"
229
228endif # if NET 230endif # if NET
229endmenu # Networking 231endmenu # Networking
230 232
diff --git a/net/Makefile b/net/Makefile
index 9fdb60c2e4a1..34e5b2d7f877 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_IEEE80211) += ieee80211/
51obj-$(CONFIG_TIPC) += tipc/ 51obj-$(CONFIG_TIPC) += tipc/
52obj-$(CONFIG_NETLABEL) += netlabel/ 52obj-$(CONFIG_NETLABEL) += netlabel/
53obj-$(CONFIG_IUCV) += iucv/ 53obj-$(CONFIG_IUCV) += iucv/
54obj-$(CONFIG_RFKILL) += rfkill/
54 55
55ifeq ($(CONFIG_NET),y) 56ifeq ($(CONFIG_NET),y)
56obj-$(CONFIG_SYSCTL) += sysctl_net.o 57obj-$(CONFIG_SYSCTL) += sysctl_net.o
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 832b5f44be5c..bfc9a35bad33 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -499,6 +499,15 @@ static int hci_sock_setsockopt(struct socket *sock, int level, int optname, char
499 break; 499 break;
500 500
501 case HCI_FILTER: 501 case HCI_FILTER:
502 {
503 struct hci_filter *f = &hci_pi(sk)->filter;
504
505 uf.type_mask = f->type_mask;
506 uf.opcode = f->opcode;
507 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
508 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
509 }
510
502 len = min_t(unsigned int, len, sizeof(uf)); 511 len = min_t(unsigned int, len, sizeof(uf));
503 if (copy_from_user(&uf, optval, len)) { 512 if (copy_from_user(&uf, optval, len)) {
504 err = -EFAULT; 513 err = -EFAULT;
diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c
index 801d687ea4ef..359e3440cf29 100644
--- a/net/bluetooth/hci_sysfs.c
+++ b/net/bluetooth/hci_sysfs.c
@@ -305,7 +305,7 @@ int hci_register_sysfs(struct hci_dev *hdev)
305 305
306 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); 306 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
307 307
308 dev->class = bt_class; 308 dev->bus = &bt_bus;
309 dev->parent = hdev->parent; 309 dev->parent = hdev->parent;
310 310
311 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE); 311 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
@@ -322,6 +322,10 @@ int hci_register_sysfs(struct hci_dev *hdev)
322 if (device_create_file(dev, bt_attrs[i]) < 0) 322 if (device_create_file(dev, bt_attrs[i]) < 0)
323 BT_ERR("Failed to create device attribute"); 323 BT_ERR("Failed to create device attribute");
324 324
325 if (sysfs_create_link(&bt_class->subsys.kset.kobj,
326 &dev->kobj, kobject_name(&dev->kobj)) < 0)
327 BT_ERR("Failed to create class symlink");
328
325 return 0; 329 return 0;
326} 330}
327 331
@@ -329,6 +333,9 @@ void hci_unregister_sysfs(struct hci_dev *hdev)
329{ 333{
330 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); 334 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
331 335
336 sysfs_remove_link(&bt_class->subsys.kset.kobj,
337 kobject_name(&hdev->dev.kobj));
338
332 device_del(&hdev->dev); 339 device_del(&hdev->dev);
333} 340}
334 341
diff --git a/net/bluetooth/l2cap.c b/net/bluetooth/l2cap.c
index a5867879b615..a59b1fb63b76 100644
--- a/net/bluetooth/l2cap.c
+++ b/net/bluetooth/l2cap.c
@@ -954,11 +954,17 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch
954 954
955 switch (optname) { 955 switch (optname) {
956 case L2CAP_OPTIONS: 956 case L2CAP_OPTIONS:
957 opts.imtu = l2cap_pi(sk)->imtu;
958 opts.omtu = l2cap_pi(sk)->omtu;
959 opts.flush_to = l2cap_pi(sk)->flush_to;
960 opts.mode = 0x00;
961
957 len = min_t(unsigned int, sizeof(opts), optlen); 962 len = min_t(unsigned int, sizeof(opts), optlen);
958 if (copy_from_user((char *) &opts, optval, len)) { 963 if (copy_from_user((char *) &opts, optval, len)) {
959 err = -EFAULT; 964 err = -EFAULT;
960 break; 965 break;
961 } 966 }
967
962 l2cap_pi(sk)->imtu = opts.imtu; 968 l2cap_pi(sk)->imtu = opts.imtu;
963 l2cap_pi(sk)->omtu = opts.omtu; 969 l2cap_pi(sk)->omtu = opts.omtu;
964 break; 970 break;
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index fe7df90eb707..52e04df323ea 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -622,7 +622,7 @@ static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst
622 bacpy(&addr.l2_bdaddr, src); 622 bacpy(&addr.l2_bdaddr, src);
623 addr.l2_family = AF_BLUETOOTH; 623 addr.l2_family = AF_BLUETOOTH;
624 addr.l2_psm = 0; 624 addr.l2_psm = 0;
625 *err = sock->ops->bind(sock, (struct sockaddr *) &addr, sizeof(addr)); 625 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
626 if (*err < 0) 626 if (*err < 0)
627 goto failed; 627 goto failed;
628 628
@@ -643,7 +643,7 @@ static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst
643 bacpy(&addr.l2_bdaddr, dst); 643 bacpy(&addr.l2_bdaddr, dst);
644 addr.l2_family = AF_BLUETOOTH; 644 addr.l2_family = AF_BLUETOOTH;
645 addr.l2_psm = htobs(RFCOMM_PSM); 645 addr.l2_psm = htobs(RFCOMM_PSM);
646 *err = sock->ops->connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK); 646 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
647 if (*err == 0 || *err == -EINPROGRESS) 647 if (*err == 0 || *err == -EINPROGRESS)
648 return s; 648 return s;
649 649
@@ -1058,6 +1058,12 @@ static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
1058 case BT_DISCONN: 1058 case BT_DISCONN:
1059 d->state = BT_CLOSED; 1059 d->state = BT_CLOSED;
1060 __rfcomm_dlc_close(d, 0); 1060 __rfcomm_dlc_close(d, 0);
1061
1062 if (list_empty(&s->dlcs)) {
1063 s->state = BT_DISCONN;
1064 rfcomm_send_disc(s, 0);
1065 }
1066
1061 break; 1067 break;
1062 } 1068 }
1063 } else { 1069 } else {
@@ -1067,6 +1073,10 @@ static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
1067 s->state = BT_CONNECTED; 1073 s->state = BT_CONNECTED;
1068 rfcomm_process_connect(s); 1074 rfcomm_process_connect(s);
1069 break; 1075 break;
1076
1077 case BT_DISCONN:
1078 rfcomm_session_put(s);
1079 break;
1070 } 1080 }
1071 } 1081 }
1072 return 0; 1082 return 0;
@@ -1757,19 +1767,12 @@ static inline void rfcomm_accept_connection(struct rfcomm_session *s)
1757 1767
1758 BT_DBG("session %p", s); 1768 BT_DBG("session %p", s);
1759 1769
1760 if (sock_create_lite(PF_BLUETOOTH, sock->type, BTPROTO_L2CAP, &nsock)) 1770 err = kernel_accept(sock, &nsock, O_NONBLOCK);
1771 if (err < 0)
1761 return; 1772 return;
1762 1773
1763 nsock->ops = sock->ops;
1764
1765 __module_get(nsock->ops->owner); 1774 __module_get(nsock->ops->owner);
1766 1775
1767 err = sock->ops->accept(sock, nsock, O_NONBLOCK);
1768 if (err < 0) {
1769 sock_release(nsock);
1770 return;
1771 }
1772
1773 /* Set our callbacks */ 1776 /* Set our callbacks */
1774 nsock->sk->sk_data_ready = rfcomm_l2data_ready; 1777 nsock->sk->sk_data_ready = rfcomm_l2data_ready;
1775 nsock->sk->sk_state_change = rfcomm_l2state_change; 1778 nsock->sk->sk_state_change = rfcomm_l2state_change;
@@ -1885,7 +1888,7 @@ static int rfcomm_add_listener(bdaddr_t *ba)
1885 bacpy(&addr.l2_bdaddr, ba); 1888 bacpy(&addr.l2_bdaddr, ba);
1886 addr.l2_family = AF_BLUETOOTH; 1889 addr.l2_family = AF_BLUETOOTH;
1887 addr.l2_psm = htobs(RFCOMM_PSM); 1890 addr.l2_psm = htobs(RFCOMM_PSM);
1888 err = sock->ops->bind(sock, (struct sockaddr *) &addr, sizeof(addr)); 1891 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
1889 if (err < 0) { 1892 if (err < 0) {
1890 BT_ERR("Bind failed %d", err); 1893 BT_ERR("Bind failed %d", err);
1891 goto failed; 1894 goto failed;
@@ -1898,7 +1901,7 @@ static int rfcomm_add_listener(bdaddr_t *ba)
1898 release_sock(sk); 1901 release_sock(sk);
1899 1902
1900 /* Start listening on the socket */ 1903 /* Start listening on the socket */
1901 err = sock->ops->listen(sock, 10); 1904 err = kernel_listen(sock, 10);
1902 if (err) { 1905 if (err) {
1903 BT_ERR("Listen failed %d", err); 1906 BT_ERR("Listen failed %d", err);
1904 goto failed; 1907 goto failed;
diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 9a7a44fc721f..b2b1cceb102a 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -517,9 +517,10 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
517 if (dlc->state == BT_CLOSED) { 517 if (dlc->state == BT_CLOSED) {
518 if (!dev->tty) { 518 if (!dev->tty) {
519 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) { 519 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
520 rfcomm_dev_hold(dev); 520 if (rfcomm_dev_get(dev->id) == NULL)
521 rfcomm_dev_del(dev); 521 return;
522 522
523 rfcomm_dev_del(dev);
523 /* We have to drop DLC lock here, otherwise 524 /* We have to drop DLC lock here, otherwise
524 rfcomm_dev_put() will dead lock if it's 525 rfcomm_dev_put() will dead lock if it's
525 the last reference. */ 526 the last reference. */
@@ -974,8 +975,12 @@ static void rfcomm_tty_hangup(struct tty_struct *tty)
974 975
975 rfcomm_tty_flush_buffer(tty); 976 rfcomm_tty_flush_buffer(tty);
976 977
977 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) 978 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
979 if (rfcomm_dev_get(dev->id) == NULL)
980 return;
978 rfcomm_dev_del(dev); 981 rfcomm_dev_del(dev);
982 rfcomm_dev_put(dev);
983 }
979} 984}
980 985
981static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused) 986static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
diff --git a/net/core/dev.c b/net/core/dev.c
index f27d4ab181e6..4317c1be4d3f 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2377,9 +2377,9 @@ static int __init dev_proc_init(void)
2377out: 2377out:
2378 return rc; 2378 return rc;
2379out_softnet: 2379out_softnet:
2380 proc_net_remove("softnet_stat");
2381out_dev2:
2382 proc_net_remove("ptype"); 2380 proc_net_remove("ptype");
2381out_dev2:
2382 proc_net_remove("softnet_stat");
2383out_dev: 2383out_dev:
2384 proc_net_remove("dev"); 2384 proc_net_remove("dev");
2385 goto out; 2385 goto out;
diff --git a/net/rfkill/Kconfig b/net/rfkill/Kconfig
new file mode 100644
index 000000000000..8b31759ee8b0
--- /dev/null
+++ b/net/rfkill/Kconfig
@@ -0,0 +1,24 @@
1#
2# RF switch subsystem configuration
3#
4menuconfig RFKILL
5 tristate "RF switch subsystem support"
6 help
7 Say Y here if you want to have control over RF switches
8 found on many WiFi, Bluetooth and IRDA cards.
9
10 To compile this driver as a module, choose M here: the
11 module will be called rfkill.
12
13config RFKILL_INPUT
14 tristate "Input layer to RF switch connector"
15 depends on RFKILL && INPUT
16 help
17 Say Y here if you want kernel automatically toggle state
18 of RF switches on and off when user presses appropriate
19 button or a key on the keyboard. Without this module you
20 need a some kind of userspace application to control
21 state of the switches.
22
23 To compile this driver as a module, choose M here: the
24 module will be called rfkill-input.
diff --git a/net/rfkill/Makefile b/net/rfkill/Makefile
new file mode 100644
index 000000000000..b38c430be057
--- /dev/null
+++ b/net/rfkill/Makefile
@@ -0,0 +1,6 @@
1#
2# Makefile for the RF switch subsystem.
3#
4
5obj-$(CONFIG_RFKILL) += rfkill.o
6obj-$(CONFIG_RFKILL_INPUT) += rfkill-input.o
diff --git a/net/rfkill/rfkill-input.c b/net/rfkill/rfkill-input.c
new file mode 100644
index 000000000000..e5c840c30284
--- /dev/null
+++ b/net/rfkill/rfkill-input.c
@@ -0,0 +1,174 @@
1/*
2 * Input layer to RF Kill interface connector
3 *
4 * Copyright (c) 2007 Dmitry Torokhov
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/input.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17#include <linux/init.h>
18#include <linux/rfkill.h>
19
20MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21MODULE_DESCRIPTION("Input layer to RF switch connector");
22MODULE_LICENSE("GPL");
23
24struct rfkill_task {
25 struct work_struct work;
26 enum rfkill_type type;
27 struct mutex mutex; /* ensures that task is serialized */
28 spinlock_t lock; /* for accessing last and desired state */
29 unsigned long last; /* last schedule */
30 enum rfkill_state desired_state; /* on/off */
31 enum rfkill_state current_state; /* on/off */
32};
33
34static void rfkill_task_handler(struct work_struct *work)
35{
36 struct rfkill_task *task = container_of(work, struct rfkill_task, work);
37 enum rfkill_state state;
38
39 mutex_lock(&task->mutex);
40
41 /*
42 * Use temp variable to fetch desired state to keep it
43 * consistent even if rfkill_schedule_toggle() runs in
44 * another thread or interrupts us.
45 */
46 state = task->desired_state;
47
48 if (state != task->current_state) {
49 rfkill_switch_all(task->type, state);
50 task->current_state = state;
51 }
52
53 mutex_unlock(&task->mutex);
54}
55
56static void rfkill_schedule_toggle(struct rfkill_task *task)
57{
58 unsigned int flags;
59
60 spin_lock_irqsave(&task->lock, flags);
61
62 if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
63 task->desired_state = !task->desired_state;
64 task->last = jiffies;
65 schedule_work(&task->work);
66 }
67
68 spin_unlock_irqrestore(&task->lock, flags);
69}
70
71#define DEFINE_RFKILL_TASK(n, t) \
72 struct rfkill_task n = { \
73 .work = __WORK_INITIALIZER(n.work, \
74 rfkill_task_handler), \
75 .type = t, \
76 .mutex = __MUTEX_INITIALIZER(n.mutex), \
77 .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
78 .desired_state = RFKILL_STATE_ON, \
79 .current_state = RFKILL_STATE_ON, \
80 }
81
82static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
83static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
84
85static void rfkill_event(struct input_handle *handle, unsigned int type,
86 unsigned int code, int down)
87{
88 if (type == EV_KEY && down == 1) {
89 switch (code) {
90 case KEY_WLAN:
91 rfkill_schedule_toggle(&rfkill_wlan);
92 break;
93 case KEY_BLUETOOTH:
94 rfkill_schedule_toggle(&rfkill_bt);
95 break;
96 default:
97 break;
98 }
99 }
100}
101
102static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
103 const struct input_device_id *id)
104{
105 struct input_handle *handle;
106 int error;
107
108 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
109 if (!handle)
110 return -ENOMEM;
111
112 handle->dev = dev;
113 handle->handler = handler;
114 handle->name = "rfkill";
115
116 error = input_register_handle(handle);
117 if (error)
118 goto err_free_handle;
119
120 error = input_open_device(handle);
121 if (error)
122 goto err_unregister_handle;
123
124 return 0;
125
126 err_unregister_handle:
127 input_unregister_handle(handle);
128 err_free_handle:
129 kfree(handle);
130 return error;
131}
132
133static void rfkill_disconnect(struct input_handle *handle)
134{
135 input_close_device(handle);
136 input_unregister_handle(handle);
137 kfree(handle);
138}
139
140static const struct input_device_id rfkill_ids[] = {
141 {
142 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
143 .evbit = { BIT(EV_KEY) },
144 .keybit = { [LONG(KEY_WLAN)] = BIT(KEY_WLAN) },
145 },
146 {
147 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
148 .evbit = { BIT(EV_KEY) },
149 .keybit = { [LONG(KEY_BLUETOOTH)] = BIT(KEY_BLUETOOTH) },
150 },
151 { }
152};
153
154static struct input_handler rfkill_handler = {
155 .event = rfkill_event,
156 .connect = rfkill_connect,
157 .disconnect = rfkill_disconnect,
158 .name = "rfkill",
159 .id_table = rfkill_ids,
160};
161
162static int __init rfkill_handler_init(void)
163{
164 return input_register_handler(&rfkill_handler);
165}
166
167static void __exit rfkill_handler_exit(void)
168{
169 input_unregister_handler(&rfkill_handler);
170 flush_scheduled_work();
171}
172
173module_init(rfkill_handler_init);
174module_exit(rfkill_handler_exit);
diff --git a/net/rfkill/rfkill.c b/net/rfkill/rfkill.c
new file mode 100644
index 000000000000..a973603e3880
--- /dev/null
+++ b/net/rfkill/rfkill.c
@@ -0,0 +1,407 @@
1/*
2 * Copyright (C) 2006 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/workqueue.h>
25#include <linux/capability.h>
26#include <linux/list.h>
27#include <linux/mutex.h>
28#include <linux/rfkill.h>
29
30MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
31MODULE_VERSION("1.0");
32MODULE_DESCRIPTION("RF switch support");
33MODULE_LICENSE("GPL");
34
35static LIST_HEAD(rfkill_list); /* list of registered rf switches */
36static DEFINE_MUTEX(rfkill_mutex);
37
38static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
39
40static int rfkill_toggle_radio(struct rfkill *rfkill,
41 enum rfkill_state state)
42{
43 int retval;
44
45 retval = mutex_lock_interruptible(&rfkill->mutex);
46 if (retval)
47 return retval;
48
49 if (state != rfkill->state) {
50 retval = rfkill->toggle_radio(rfkill->data, state);
51 if (!retval)
52 rfkill->state = state;
53 }
54
55 mutex_unlock(&rfkill->mutex);
56 return retval;
57}
58
59/**
60 * rfkill_switch_all - Toggle state of all switches of given type
61 * @type: type of interfaces to be affeceted
62 * @state: the new state
63 *
64 * This function toggles state of all switches of given type unless
65 * a specific switch is claimed by userspace in which case it is
66 * left alone.
67 */
68
69void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
70{
71 struct rfkill *rfkill;
72
73 mutex_lock(&rfkill_mutex);
74
75 rfkill_states[type] = state;
76
77 list_for_each_entry(rfkill, &rfkill_list, node) {
78 if (!rfkill->user_claim)
79 rfkill_toggle_radio(rfkill, state);
80 }
81
82 mutex_unlock(&rfkill_mutex);
83}
84EXPORT_SYMBOL(rfkill_switch_all);
85
86static ssize_t rfkill_name_show(struct device *dev,
87 struct device_attribute *attr,
88 char *buf)
89{
90 struct rfkill *rfkill = to_rfkill(dev);
91
92 return sprintf(buf, "%s\n", rfkill->name);
93}
94
95static ssize_t rfkill_type_show(struct device *dev,
96 struct device_attribute *attr,
97 char *buf)
98{
99 struct rfkill *rfkill = to_rfkill(dev);
100 const char *type;
101
102 switch (rfkill->type) {
103 case RFKILL_TYPE_WLAN:
104 type = "wlan";
105 break;
106 case RFKILL_TYPE_BLUETOOTH:
107 type = "bluetooth";
108 break;
109 case RFKILL_TYPE_IRDA:
110 type = "irda";
111 break;
112 default:
113 BUG();
114 }
115
116 return sprintf(buf, "%s\n", type);
117}
118
119static ssize_t rfkill_state_show(struct device *dev,
120 struct device_attribute *attr,
121 char *buf)
122{
123 struct rfkill *rfkill = to_rfkill(dev);
124
125 return sprintf(buf, "%d\n", rfkill->state);
126}
127
128static ssize_t rfkill_state_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t count)
131{
132 struct rfkill *rfkill = to_rfkill(dev);
133 unsigned int state = simple_strtoul(buf, NULL, 0);
134 int error;
135
136 if (!capable(CAP_NET_ADMIN))
137 return -EPERM;
138
139 error = rfkill_toggle_radio(rfkill,
140 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
141 if (error)
142 return error;
143
144 return count;
145}
146
147static ssize_t rfkill_claim_show(struct device *dev,
148 struct device_attribute *attr,
149 char *buf)
150{
151 struct rfkill *rfkill = to_rfkill(dev);
152
153 return sprintf(buf, "%d", rfkill->user_claim);
154}
155
156static ssize_t rfkill_claim_store(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
159{
160 struct rfkill *rfkill = to_rfkill(dev);
161 bool claim = !!simple_strtoul(buf, NULL, 0);
162 int error;
163
164 if (!capable(CAP_NET_ADMIN))
165 return -EPERM;
166
167 /*
168 * Take the global lock to make sure the kernel is not in
169 * the middle of rfkill_switch_all
170 */
171 error = mutex_lock_interruptible(&rfkill_mutex);
172 if (error)
173 return error;
174
175 if (rfkill->user_claim != claim) {
176 if (!claim)
177 rfkill_toggle_radio(rfkill,
178 rfkill_states[rfkill->type]);
179 rfkill->user_claim = claim;
180 }
181
182 mutex_unlock(&rfkill_mutex);
183
184 return count;
185}
186
187static struct device_attribute rfkill_dev_attrs[] = {
188 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
189 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
190 __ATTR(state, S_IRUGO, rfkill_state_show, rfkill_state_store),
191 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
192 __ATTR_NULL
193};
194
195static void rfkill_release(struct device *dev)
196{
197 struct rfkill *rfkill = to_rfkill(dev);
198
199 kfree(rfkill);
200 module_put(THIS_MODULE);
201}
202
203#ifdef CONFIG_PM
204static int rfkill_suspend(struct device *dev, pm_message_t state)
205{
206 struct rfkill *rfkill = to_rfkill(dev);
207
208 if (dev->power.power_state.event != state.event) {
209 if (state.event == PM_EVENT_SUSPEND) {
210 mutex_lock(&rfkill->mutex);
211
212 if (rfkill->state == RFKILL_STATE_ON)
213 rfkill->toggle_radio(rfkill->data,
214 RFKILL_STATE_OFF);
215
216 mutex_unlock(&rfkill->mutex);
217 }
218
219 dev->power.power_state = state;
220 }
221
222 return 0;
223}
224
225static int rfkill_resume(struct device *dev)
226{
227 struct rfkill *rfkill = to_rfkill(dev);
228
229 if (dev->power.power_state.event != PM_EVENT_ON) {
230 mutex_lock(&rfkill->mutex);
231
232 if (rfkill->state == RFKILL_STATE_ON)
233 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
234
235 mutex_unlock(&rfkill->mutex);
236 }
237
238 dev->power.power_state = PMSG_ON;
239 return 0;
240}
241#else
242#define rfkill_suspend NULL
243#define rfkill_resume NULL
244#endif
245
246static struct class rfkill_class = {
247 .name = "rfkill",
248 .dev_release = rfkill_release,
249 .dev_attrs = rfkill_dev_attrs,
250 .suspend = rfkill_suspend,
251 .resume = rfkill_resume,
252};
253
254static int rfkill_add_switch(struct rfkill *rfkill)
255{
256 int retval;
257
258 retval = mutex_lock_interruptible(&rfkill_mutex);
259 if (retval)
260 return retval;
261
262 retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
263 if (retval)
264 goto out;
265
266 list_add_tail(&rfkill->node, &rfkill_list);
267
268 out:
269 mutex_unlock(&rfkill_mutex);
270 return retval;
271}
272
273static void rfkill_remove_switch(struct rfkill *rfkill)
274{
275 mutex_lock(&rfkill_mutex);
276 list_del_init(&rfkill->node);
277 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
278 mutex_unlock(&rfkill_mutex);
279}
280
281/**
282 * rfkill_allocate - allocate memory for rfkill structure.
283 * @parent: device that has rf switch on it
284 * @type: type of the switch (wlan, bluetooth, irda)
285 *
286 * This function should be called by the network driver when it needs
287 * rfkill structure. Once the structure is allocated the driver shoud
288 * finish its initialization by setting name, private data, enable_radio
289 * and disable_radio methods and then register it with rfkill_register().
290 * NOTE: If registration fails the structure shoudl be freed by calling
291 * rfkill_free() otherwise rfkill_unregister() should be used.
292 */
293struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
294{
295 struct rfkill *rfkill;
296 struct device *dev;
297
298 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
299 if (rfkill)
300 return NULL;
301
302 mutex_init(&rfkill->mutex);
303 INIT_LIST_HEAD(&rfkill->node);
304 rfkill->type = type;
305
306 dev = &rfkill->dev;
307 dev->class = &rfkill_class;
308 dev->parent = parent;
309 device_initialize(dev);
310
311 __module_get(THIS_MODULE);
312
313 return rfkill;
314}
315EXPORT_SYMBOL(rfkill_allocate);
316
317/**
318 * rfkill_free - Mark rfkill structure for deletion
319 * @rfkill: rfkill structure to be destroyed
320 *
321 * Decrements reference count of rfkill structure so it is destoryed.
322 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
323 */
324void rfkill_free(struct rfkill *rfkill)
325{
326 if (rfkill)
327 put_device(&rfkill->dev);
328}
329EXPORT_SYMBOL(rfkill_free);
330
331/**
332 * rfkill_register - Register a rfkill structure.
333 * @rfkill: rfkill structure to be registered
334 *
335 * This function should be called by the network driver when the rfkill
336 * structure needs to be registered. Immediately from registration the
337 * switch driver should be able to service calls to toggle_radio.
338 */
339int rfkill_register(struct rfkill *rfkill)
340{
341 static atomic_t rfkill_no = ATOMIC_INIT(0);
342 struct device *dev = &rfkill->dev;
343 int error;
344
345 if (!rfkill->toggle_radio)
346 return -EINVAL;
347
348 error = rfkill_add_switch(rfkill);
349 if (error)
350 return error;
351
352 snprintf(dev->bus_id, sizeof(dev->bus_id),
353 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
354
355 error = device_add(dev);
356 if (error) {
357 rfkill_remove_switch(rfkill);
358 return error;
359 }
360
361 return 0;
362}
363EXPORT_SYMBOL(rfkill_register);
364
365/**
366 * rfkill_unregister - Uegister a rfkill structure.
367 * @rfkill: rfkill structure to be unregistered
368 *
369 * This function should be called by the network driver during device
370 * teardown to destroy rfkill structure. Note that rfkill_free() should
371 * _not_ be called after rfkill_unregister().
372 */
373void rfkill_unregister(struct rfkill *rfkill)
374{
375 device_del(&rfkill->dev);
376 rfkill_remove_switch(rfkill);
377 put_device(&rfkill->dev);
378}
379EXPORT_SYMBOL(rfkill_unregister);
380
381/*
382 * Rfkill module initialization/deinitialization.
383 */
384static int __init rfkill_init(void)
385{
386 int error;
387 int i;
388
389 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
390 rfkill_states[i] = RFKILL_STATE_ON;
391
392 error = class_register(&rfkill_class);
393 if (error) {
394 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
395 return error;
396 }
397
398 return 0;
399}
400
401static void __exit rfkill_exit(void)
402{
403 class_unregister(&rfkill_class);
404}
405
406module_init(rfkill_init);
407module_exit(rfkill_exit);