Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
Ivo van Doorn | 811aa9c | 2008-02-03 15:42:53 +0100 | [diff] [blame] | 2 | Copyright (C) 2004 - 2008 rt2x00 SourceForge Project |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 3 | <http://rt2x00.serialmonkey.com> |
| 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 | /* |
| 22 | Module: rt2x00rfkill |
| 23 | Abstract: rt2x00 rfkill routines. |
| 24 | */ |
| 25 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 26 | #include <linux/input-polldev.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/rfkill.h> |
| 30 | |
| 31 | #include "rt2x00.h" |
| 32 | #include "rt2x00lib.h" |
| 33 | |
| 34 | static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state) |
| 35 | { |
| 36 | struct rt2x00_dev *rt2x00dev = data; |
| 37 | int retval = 0; |
| 38 | |
| 39 | if (unlikely(!rt2x00dev)) |
| 40 | return 0; |
| 41 | |
| 42 | /* |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 43 | * Only continue if there are enabled interfaces. |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 44 | */ |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 45 | if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 46 | return 0; |
| 47 | |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame^] | 48 | if (state == RFKILL_STATE_UNBLOCKED) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 49 | INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n"); |
Ivo van Doorn | 81873e9 | 2007-10-06 14:14:06 +0200 | [diff] [blame] | 50 | __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 51 | retval = rt2x00lib_enable_radio(rt2x00dev); |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame^] | 52 | } else if (state == RFKILL_STATE_SOFT_BLOCKED) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 53 | INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n"); |
Ivo van Doorn | 81873e9 | 2007-10-06 14:14:06 +0200 | [diff] [blame] | 54 | __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 55 | rt2x00lib_disable_radio(rt2x00dev); |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame^] | 56 | } else { |
| 57 | WARNING(rt2x00dev, "Received unexpected rfkill state %d.\n", |
| 58 | state); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return retval; |
| 62 | } |
| 63 | |
| 64 | static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev) |
| 65 | { |
| 66 | struct rt2x00_dev *rt2x00dev = poll_dev->private; |
| 67 | int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev); |
| 68 | |
Ivo van Doorn | 4300beb | 2007-10-27 13:40:25 +0200 | [diff] [blame] | 69 | if (rt2x00dev->rfkill->state != state) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 70 | input_report_key(poll_dev->input, KEY_WLAN, 1); |
Ivo van Doorn | 4300beb | 2007-10-27 13:40:25 +0200 | [diff] [blame] | 71 | input_report_key(poll_dev->input, KEY_WLAN, 0); |
| 72 | } |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 75 | void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 76 | { |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 77 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) || |
| 78 | !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state)) |
| 79 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 80 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 81 | if (rfkill_register(rt2x00dev->rfkill)) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 82 | ERROR(rt2x00dev, "Failed to register rfkill handler.\n"); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 83 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 86 | if (input_register_polled_device(rt2x00dev->poll_dev)) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 87 | ERROR(rt2x00dev, "Failed to register polled device.\n"); |
| 88 | rfkill_unregister(rt2x00dev->rfkill); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 89 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 92 | __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state); |
| 93 | |
Ivo van Doorn | d9890b8 | 2007-10-27 13:40:51 +0200 | [diff] [blame] | 94 | /* |
| 95 | * Force initial poll which will detect the initial device state, |
| 96 | * and correctly sends the signal to the rfkill layer about this |
| 97 | * state. |
| 98 | */ |
| 99 | rt2x00rfkill_poll(rt2x00dev->poll_dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev) |
| 103 | { |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 104 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) || |
| 105 | !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 106 | return; |
| 107 | |
| 108 | input_unregister_polled_device(rt2x00dev->poll_dev); |
| 109 | rfkill_unregister(rt2x00dev->rfkill); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 110 | |
| 111 | __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 114 | static struct input_polled_dev * |
| 115 | rt2x00rfkill_allocate_polldev(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 116 | { |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 117 | struct input_polled_dev *poll_dev; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 118 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 119 | poll_dev = input_allocate_polled_device(); |
| 120 | if (!poll_dev) |
| 121 | return NULL; |
| 122 | |
| 123 | poll_dev->private = rt2x00dev; |
| 124 | poll_dev->poll = rt2x00rfkill_poll; |
| 125 | poll_dev->poll_interval = RFKILL_POLL_INTERVAL; |
| 126 | |
| 127 | poll_dev->input->name = rt2x00dev->ops->name; |
| 128 | poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy); |
| 129 | poll_dev->input->id.bustype = BUS_HOST; |
| 130 | poll_dev->input->id.vendor = 0x1814; |
| 131 | poll_dev->input->id.product = rt2x00dev->chip.rt; |
| 132 | poll_dev->input->id.version = rt2x00dev->chip.rev; |
| 133 | poll_dev->input->dev.parent = wiphy_dev(rt2x00dev->hw->wiphy); |
| 134 | poll_dev->input->evbit[0] = BIT(EV_KEY); |
| 135 | set_bit(KEY_WLAN, poll_dev->input->keybit); |
| 136 | |
| 137 | return poll_dev; |
| 138 | } |
| 139 | |
| 140 | void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev) |
| 141 | { |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 142 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags)) |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 143 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 144 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 145 | rt2x00dev->rfkill = |
| 146 | rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 147 | if (!rt2x00dev->rfkill) { |
| 148 | ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n"); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 149 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | rt2x00dev->rfkill->name = rt2x00dev->ops->name; |
| 153 | rt2x00dev->rfkill->data = rt2x00dev; |
Ivo van Doorn | d9890b8 | 2007-10-27 13:40:51 +0200 | [diff] [blame] | 154 | rt2x00dev->rfkill->state = -1; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 155 | rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio; |
| 156 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 157 | rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 158 | if (!rt2x00dev->poll_dev) { |
| 159 | ERROR(rt2x00dev, "Failed to allocate polled device.\n"); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 160 | rfkill_free(rt2x00dev->rfkill); |
| 161 | rt2x00dev->rfkill = NULL; |
| 162 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 165 | return; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev) |
| 169 | { |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 170 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) || |
| 171 | !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 172 | return; |
| 173 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 174 | input_free_polled_device(rt2x00dev->poll_dev); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 175 | rt2x00dev->poll_dev = NULL; |
| 176 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 177 | rfkill_free(rt2x00dev->rfkill); |
Ivo van Doorn | 1682fe6 | 2008-03-13 15:38:03 +0100 | [diff] [blame] | 178 | rt2x00dev->rfkill = NULL; |
| 179 | } |
| 180 | |
| 181 | void rt2x00rfkill_suspend(struct rt2x00_dev *rt2x00dev) |
| 182 | { |
| 183 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) || |
| 184 | !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state)) |
| 185 | return; |
| 186 | |
| 187 | input_free_polled_device(rt2x00dev->poll_dev); |
| 188 | rt2x00dev->poll_dev = NULL; |
| 189 | } |
| 190 | |
| 191 | void rt2x00rfkill_resume(struct rt2x00_dev *rt2x00dev) |
| 192 | { |
| 193 | if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) || |
| 194 | !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state)) |
| 195 | return; |
| 196 | |
| 197 | rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev); |
| 198 | if (!rt2x00dev->poll_dev) { |
| 199 | ERROR(rt2x00dev, "Failed to allocate polled device.\n"); |
| 200 | return; |
| 201 | } |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 202 | } |