blob: 50e010272e471fb674e2769a1ca0332be3d9f70b [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
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;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700109 default:
110 BUG();
111 }
112
113 return sprintf(buf, "%s\n", type);
114}
115
116static ssize_t rfkill_state_show(struct device *dev,
117 struct device_attribute *attr,
118 char *buf)
119{
120 struct rfkill *rfkill = to_rfkill(dev);
121
122 return sprintf(buf, "%d\n", rfkill->state);
123}
124
125static ssize_t rfkill_state_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
128{
129 struct rfkill *rfkill = to_rfkill(dev);
130 unsigned int state = simple_strtoul(buf, NULL, 0);
131 int error;
132
133 if (!capable(CAP_NET_ADMIN))
134 return -EPERM;
135
136 error = rfkill_toggle_radio(rfkill,
137 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
138 if (error)
139 return error;
140
141 return count;
142}
143
144static ssize_t rfkill_claim_show(struct device *dev,
145 struct device_attribute *attr,
146 char *buf)
147{
148 struct rfkill *rfkill = to_rfkill(dev);
149
150 return sprintf(buf, "%d", rfkill->user_claim);
151}
152
153static ssize_t rfkill_claim_store(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t count)
156{
157 struct rfkill *rfkill = to_rfkill(dev);
158 bool claim = !!simple_strtoul(buf, NULL, 0);
159 int error;
160
161 if (!capable(CAP_NET_ADMIN))
162 return -EPERM;
163
164 /*
165 * Take the global lock to make sure the kernel is not in
166 * the middle of rfkill_switch_all
167 */
168 error = mutex_lock_interruptible(&rfkill_mutex);
169 if (error)
170 return error;
171
172 if (rfkill->user_claim != claim) {
173 if (!claim)
174 rfkill_toggle_radio(rfkill,
175 rfkill_states[rfkill->type]);
176 rfkill->user_claim = claim;
177 }
178
179 mutex_unlock(&rfkill_mutex);
180
181 return count;
182}
183
184static struct device_attribute rfkill_dev_attrs[] = {
185 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
186 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700187 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700188 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
189 __ATTR_NULL
190};
191
192static void rfkill_release(struct device *dev)
193{
194 struct rfkill *rfkill = to_rfkill(dev);
195
196 kfree(rfkill);
197 module_put(THIS_MODULE);
198}
199
200#ifdef CONFIG_PM
201static int rfkill_suspend(struct device *dev, pm_message_t state)
202{
203 struct rfkill *rfkill = to_rfkill(dev);
204
205 if (dev->power.power_state.event != state.event) {
206 if (state.event == PM_EVENT_SUSPEND) {
207 mutex_lock(&rfkill->mutex);
208
209 if (rfkill->state == RFKILL_STATE_ON)
210 rfkill->toggle_radio(rfkill->data,
211 RFKILL_STATE_OFF);
212
213 mutex_unlock(&rfkill->mutex);
214 }
215
216 dev->power.power_state = state;
217 }
218
219 return 0;
220}
221
222static int rfkill_resume(struct device *dev)
223{
224 struct rfkill *rfkill = to_rfkill(dev);
225
226 if (dev->power.power_state.event != PM_EVENT_ON) {
227 mutex_lock(&rfkill->mutex);
228
229 if (rfkill->state == RFKILL_STATE_ON)
230 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
231
232 mutex_unlock(&rfkill->mutex);
233 }
234
235 dev->power.power_state = PMSG_ON;
236 return 0;
237}
238#else
239#define rfkill_suspend NULL
240#define rfkill_resume NULL
241#endif
242
243static struct class rfkill_class = {
244 .name = "rfkill",
245 .dev_release = rfkill_release,
246 .dev_attrs = rfkill_dev_attrs,
247 .suspend = rfkill_suspend,
248 .resume = rfkill_resume,
249};
250
251static int rfkill_add_switch(struct rfkill *rfkill)
252{
253 int retval;
254
255 retval = mutex_lock_interruptible(&rfkill_mutex);
256 if (retval)
257 return retval;
258
259 retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
260 if (retval)
261 goto out;
262
263 list_add_tail(&rfkill->node, &rfkill_list);
264
265 out:
266 mutex_unlock(&rfkill_mutex);
267 return retval;
268}
269
270static void rfkill_remove_switch(struct rfkill *rfkill)
271{
272 mutex_lock(&rfkill_mutex);
273 list_del_init(&rfkill->node);
274 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
275 mutex_unlock(&rfkill_mutex);
276}
277
278/**
279 * rfkill_allocate - allocate memory for rfkill structure.
280 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200281 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700282 *
283 * This function should be called by the network driver when it needs
284 * rfkill structure. Once the structure is allocated the driver shoud
285 * finish its initialization by setting name, private data, enable_radio
286 * and disable_radio methods and then register it with rfkill_register().
287 * NOTE: If registration fails the structure shoudl be freed by calling
288 * rfkill_free() otherwise rfkill_unregister() should be used.
289 */
290struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
291{
292 struct rfkill *rfkill;
293 struct device *dev;
294
295 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700296 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700297 return NULL;
298
299 mutex_init(&rfkill->mutex);
300 INIT_LIST_HEAD(&rfkill->node);
301 rfkill->type = type;
302
303 dev = &rfkill->dev;
304 dev->class = &rfkill_class;
305 dev->parent = parent;
306 device_initialize(dev);
307
308 __module_get(THIS_MODULE);
309
310 return rfkill;
311}
312EXPORT_SYMBOL(rfkill_allocate);
313
314/**
315 * rfkill_free - Mark rfkill structure for deletion
316 * @rfkill: rfkill structure to be destroyed
317 *
318 * Decrements reference count of rfkill structure so it is destoryed.
319 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
320 */
321void rfkill_free(struct rfkill *rfkill)
322{
323 if (rfkill)
324 put_device(&rfkill->dev);
325}
326EXPORT_SYMBOL(rfkill_free);
327
328/**
329 * rfkill_register - Register a rfkill structure.
330 * @rfkill: rfkill structure to be registered
331 *
332 * This function should be called by the network driver when the rfkill
333 * structure needs to be registered. Immediately from registration the
334 * switch driver should be able to service calls to toggle_radio.
335 */
336int rfkill_register(struct rfkill *rfkill)
337{
338 static atomic_t rfkill_no = ATOMIC_INIT(0);
339 struct device *dev = &rfkill->dev;
340 int error;
341
342 if (!rfkill->toggle_radio)
343 return -EINVAL;
344
345 error = rfkill_add_switch(rfkill);
346 if (error)
347 return error;
348
349 snprintf(dev->bus_id, sizeof(dev->bus_id),
350 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
351
352 error = device_add(dev);
353 if (error) {
354 rfkill_remove_switch(rfkill);
355 return error;
356 }
357
358 return 0;
359}
360EXPORT_SYMBOL(rfkill_register);
361
362/**
363 * rfkill_unregister - Uegister a rfkill structure.
364 * @rfkill: rfkill structure to be unregistered
365 *
366 * This function should be called by the network driver during device
367 * teardown to destroy rfkill structure. Note that rfkill_free() should
368 * _not_ be called after rfkill_unregister().
369 */
370void rfkill_unregister(struct rfkill *rfkill)
371{
372 device_del(&rfkill->dev);
373 rfkill_remove_switch(rfkill);
374 put_device(&rfkill->dev);
375}
376EXPORT_SYMBOL(rfkill_unregister);
377
378/*
379 * Rfkill module initialization/deinitialization.
380 */
381static int __init rfkill_init(void)
382{
383 int error;
384 int i;
385
386 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
387 rfkill_states[i] = RFKILL_STATE_ON;
388
389 error = class_register(&rfkill_class);
390 if (error) {
391 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
392 return error;
393 }
394
395 return 0;
396}
397
398static void __exit rfkill_exit(void)
399{
400 class_unregister(&rfkill_class);
401}
402
403module_init(rfkill_init);
404module_exit(rfkill_exit);