Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bay.c - ACPI removable drive bay driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com> |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or (at |
| 11 | * your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 | * |
| 22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | */ |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/notifier.h> |
| 29 | #include <acpi/acpi_bus.h> |
| 30 | #include <acpi/acpi_drivers.h> |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 31 | #include <linux/seq_file.h> |
| 32 | #include <asm/uaccess.h> |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 34 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 35 | ACPI_MODULE_NAME("bay"); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 36 | MODULE_AUTHOR("Kristen Carlson Accardi"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 37 | MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver"); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 38 | MODULE_LICENSE("GPL"); |
| 39 | #define ACPI_BAY_CLASS "bay" |
| 40 | #define ACPI_BAY_COMPONENT 0x10000000 |
| 41 | #define _COMPONENT ACPI_BAY_COMPONENT |
| 42 | #define bay_dprintk(h,s) {\ |
| 43 | char prefix[80] = {'\0'};\ |
| 44 | struct acpi_buffer buffer = {sizeof(prefix), prefix};\ |
| 45 | acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\ |
| 46 | printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); } |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 47 | static void bay_notify(acpi_handle handle, u32 event, void *data); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 48 | |
| 49 | struct bay { |
| 50 | acpi_handle handle; |
| 51 | char *name; |
| 52 | struct list_head list; |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 53 | struct platform_device *pdev; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Adrian Bunk | 5d22e1e | 2006-12-04 14:49:39 -0800 | [diff] [blame] | 56 | static LIST_HEAD(drive_bays); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 57 | |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 58 | |
| 59 | /***************************************************************************** |
| 60 | * Drive Bay functions * |
| 61 | *****************************************************************************/ |
| 62 | /** |
| 63 | * is_ejectable - see if a device is ejectable |
| 64 | * @handle: acpi handle of the device |
| 65 | * |
| 66 | * If an acpi object has a _EJ0 method, then it is ejectable |
| 67 | */ |
| 68 | static int is_ejectable(acpi_handle handle) |
| 69 | { |
| 70 | acpi_status status; |
| 71 | acpi_handle tmp; |
| 72 | |
| 73 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 74 | if (ACPI_FAILURE(status)) |
| 75 | return 0; |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * bay_present - see if the bay device is present |
| 81 | * @bay: the drive bay |
| 82 | * |
| 83 | * execute the _STA method. |
| 84 | */ |
| 85 | static int bay_present(struct bay *bay) |
| 86 | { |
| 87 | unsigned long sta; |
| 88 | acpi_status status; |
| 89 | |
| 90 | if (bay) { |
| 91 | status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta); |
| 92 | if (ACPI_SUCCESS(status) && sta) |
| 93 | return 1; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * eject_device - respond to an eject request |
| 100 | * @handle - the device to eject |
| 101 | * |
| 102 | * Call this devices _EJ0 method. |
| 103 | */ |
| 104 | static void eject_device(acpi_handle handle) |
| 105 | { |
| 106 | struct acpi_object_list arg_list; |
| 107 | union acpi_object arg; |
| 108 | |
| 109 | bay_dprintk(handle, "Ejecting device"); |
| 110 | |
| 111 | arg_list.count = 1; |
| 112 | arg_list.pointer = &arg; |
| 113 | arg.type = ACPI_TYPE_INTEGER; |
| 114 | arg.integer.value = 1; |
| 115 | |
| 116 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0", |
| 117 | &arg_list, NULL))) |
| 118 | pr_debug("Failed to evaluate _EJ0!\n"); |
| 119 | } |
| 120 | |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 121 | /* |
| 122 | * show_present - read method for "present" file in sysfs |
| 123 | */ |
| 124 | static ssize_t show_present(struct device *dev, |
| 125 | struct device_attribute *attr, char *buf) |
| 126 | { |
| 127 | struct bay *bay = dev_get_drvdata(dev); |
| 128 | return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay)); |
| 129 | |
| 130 | } |
| 131 | DEVICE_ATTR(present, S_IRUGO, show_present, NULL); |
| 132 | |
| 133 | /* |
| 134 | * write_eject - write method for "eject" file in sysfs |
| 135 | */ |
| 136 | static ssize_t write_eject(struct device *dev, struct device_attribute *attr, |
| 137 | const char *buf, size_t count) |
| 138 | { |
| 139 | struct bay *bay = dev_get_drvdata(dev); |
| 140 | |
| 141 | if (!count) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | eject_device(bay->handle); |
| 145 | return count; |
| 146 | } |
| 147 | DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * is_ata - see if a device is an ata device |
| 151 | * @handle: acpi handle of the device |
| 152 | * |
| 153 | * If an acpi object has one of 4 ATA ACPI methods defined, |
| 154 | * then it is an ATA device |
| 155 | */ |
| 156 | static int is_ata(acpi_handle handle) |
| 157 | { |
| 158 | acpi_handle tmp; |
| 159 | |
| 160 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || |
| 161 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || |
| 162 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || |
| 163 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) |
| 164 | return 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * parent_is_ata(acpi_handle handle) |
| 171 | * |
| 172 | */ |
| 173 | static int parent_is_ata(acpi_handle handle) |
| 174 | { |
| 175 | acpi_handle phandle; |
| 176 | |
| 177 | if (acpi_get_parent(handle, &phandle)) |
| 178 | return 0; |
| 179 | |
| 180 | return is_ata(phandle); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * is_ejectable_bay - see if a device is an ejectable drive bay |
| 185 | * @handle: acpi handle of the device |
| 186 | * |
| 187 | * If an acpi object is ejectable and has one of the ACPI ATA |
| 188 | * methods defined, then we can safely call it an ejectable |
| 189 | * drive bay |
| 190 | */ |
| 191 | static int is_ejectable_bay(acpi_handle handle) |
| 192 | { |
| 193 | if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle)) |
| 194 | return 1; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * eject_removable_drive - try to eject this drive |
| 200 | * @dev : the device structure of the drive |
| 201 | * |
| 202 | * If a device is a removable drive that requires an _EJ0 method |
| 203 | * to be executed in order to safely remove from the system, do |
| 204 | * it. ATM - always returns success |
| 205 | */ |
| 206 | int eject_removable_drive(struct device *dev) |
| 207 | { |
| 208 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); |
| 209 | |
| 210 | if (handle) { |
| 211 | bay_dprintk(handle, "Got device handle"); |
| 212 | if (is_ejectable_bay(handle)) |
| 213 | eject_device(handle); |
| 214 | } else { |
| 215 | printk("No acpi handle for device\n"); |
| 216 | } |
| 217 | |
| 218 | /* should I return an error code? */ |
| 219 | return 0; |
| 220 | } |
| 221 | EXPORT_SYMBOL_GPL(eject_removable_drive); |
| 222 | |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 223 | static int acpi_bay_add_fs(struct bay *bay) |
| 224 | { |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 225 | int ret; |
| 226 | struct device *dev = &bay->pdev->dev; |
| 227 | |
| 228 | ret = device_create_file(dev, &dev_attr_present); |
| 229 | if (ret) |
| 230 | goto add_fs_err; |
| 231 | ret = device_create_file(dev, &dev_attr_eject); |
| 232 | if (ret) { |
| 233 | device_remove_file(dev, &dev_attr_present); |
| 234 | goto add_fs_err; |
| 235 | } |
| 236 | return 0; |
| 237 | |
| 238 | add_fs_err: |
| 239 | bay_dprintk(bay->handle, "Error adding sysfs files\n"); |
| 240 | return ret; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void acpi_bay_remove_fs(struct bay *bay) |
| 244 | { |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 245 | struct device *dev = &bay->pdev->dev; |
| 246 | |
| 247 | /* cleanup sysfs */ |
| 248 | device_remove_file(dev, &dev_attr_present); |
| 249 | device_remove_file(dev, &dev_attr_eject); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int bay_is_dock_device(acpi_handle handle) |
| 253 | { |
| 254 | acpi_handle parent; |
| 255 | |
| 256 | acpi_get_parent(handle, &parent); |
| 257 | |
| 258 | /* if the device or it's parent is dependent on the |
| 259 | * dock, then we are a dock device |
| 260 | */ |
| 261 | return (is_dock_device(handle) || is_dock_device(parent)); |
| 262 | } |
| 263 | |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 264 | static int bay_add(acpi_handle handle, int id) |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 265 | { |
| 266 | acpi_status status; |
| 267 | struct bay *new_bay; |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 268 | struct platform_device *pdev; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 269 | struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 270 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer); |
| 271 | |
| 272 | bay_dprintk(handle, "Adding notify handler"); |
| 273 | |
| 274 | /* |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 275 | * Initialize bay device structure |
| 276 | */ |
Al Viro | a3c94e5 | 2007-02-09 16:05:07 +0000 | [diff] [blame] | 277 | new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 278 | INIT_LIST_HEAD(&new_bay->list); |
| 279 | new_bay->handle = handle; |
| 280 | new_bay->name = (char *)nbuffer.pointer; |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 281 | |
| 282 | /* initialize platform device stuff */ |
| 283 | pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0); |
Henrique de Moraes Holschuh | 0539771 | 2007-02-14 10:55:00 -0200 | [diff] [blame] | 284 | if (IS_ERR(pdev)) { |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 285 | printk(KERN_ERR PREFIX "Error registering bay device\n"); |
| 286 | goto bay_add_err; |
| 287 | } |
| 288 | new_bay->pdev = pdev; |
| 289 | platform_set_drvdata(pdev, new_bay); |
| 290 | |
Kristen Carlson Accardi | 1f9767d | 2007-05-09 15:55:53 -0700 | [diff] [blame] | 291 | /* |
| 292 | * we want the bay driver to be able to send uevents |
| 293 | */ |
| 294 | pdev->dev.uevent_suppress = 0; |
| 295 | |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 296 | if (acpi_bay_add_fs(new_bay)) { |
| 297 | platform_device_unregister(new_bay->pdev); |
| 298 | goto bay_add_err; |
| 299 | } |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 300 | |
| 301 | /* register for events on this device */ |
| 302 | status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 303 | bay_notify, new_bay); |
| 304 | if (ACPI_FAILURE(status)) { |
| 305 | printk(KERN_ERR PREFIX "Error installing bay notify handler\n"); |
| 306 | } |
| 307 | |
| 308 | /* if we are on a dock station, we should register for dock |
| 309 | * notifications. |
| 310 | */ |
| 311 | if (bay_is_dock_device(handle)) { |
| 312 | bay_dprintk(handle, "Is dependent on dock\n"); |
| 313 | register_hotplug_dock_device(handle, bay_notify, new_bay); |
| 314 | } |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 315 | list_add(&new_bay->list, &drive_bays); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 316 | printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name); |
| 317 | return 0; |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 318 | |
| 319 | bay_add_err: |
| 320 | kfree(new_bay->name); |
| 321 | kfree(new_bay); |
| 322 | return -ENODEV; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 325 | /** |
| 326 | * bay_notify - act upon an acpi bay notification |
| 327 | * @handle: the bay handle |
| 328 | * @event: the acpi event |
| 329 | * @data: our driver data struct |
| 330 | * |
| 331 | */ |
| 332 | static void bay_notify(acpi_handle handle, u32 event, void *data) |
| 333 | { |
Kristen Carlson Accardi | c41458a | 2007-02-05 16:09:07 -0800 | [diff] [blame] | 334 | struct bay *bay_dev = (struct bay *)data; |
| 335 | struct device *dev = &bay_dev->pdev->dev; |
Kristen Carlson Accardi | 3f8698d | 2007-05-23 14:12:29 -0700 | [diff] [blame] | 336 | char event_string[12]; |
| 337 | char *envp[] = { event_string, NULL }; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 338 | |
| 339 | bay_dprintk(handle, "Bay event"); |
Stephan Berberig | 7aa763c | 2007-08-10 13:10:31 -0700 | [diff] [blame] | 340 | sprintf(event_string, "BAY_EVENT=%d", event); |
Kristen Carlson Accardi | 3f8698d | 2007-05-23 14:12:29 -0700 | [diff] [blame] | 341 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static acpi_status |
| 345 | find_bay(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 346 | { |
| 347 | int *count = (int *)context; |
| 348 | |
| 349 | /* |
| 350 | * there could be more than one ejectable bay. |
| 351 | * so, just return AE_OK always so that every object |
| 352 | * will be checked. |
| 353 | */ |
| 354 | if (is_ejectable_bay(handle)) { |
| 355 | bay_dprintk(handle, "found ejectable bay"); |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 356 | if (!bay_add(handle, *count)) |
| 357 | (*count)++; |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 358 | } |
| 359 | return AE_OK; |
| 360 | } |
| 361 | |
| 362 | static int __init bay_init(void) |
| 363 | { |
| 364 | int bays = 0; |
| 365 | |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 366 | INIT_LIST_HEAD(&drive_bays); |
| 367 | |
| 368 | /* look for dockable drive bays */ |
| 369 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 370 | ACPI_UINT32_MAX, find_bay, &bays, NULL); |
| 371 | |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 372 | if (!bays) |
| 373 | return -ENODEV; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static void __exit bay_exit(void) |
| 379 | { |
| 380 | struct bay *bay, *tmp; |
| 381 | |
| 382 | list_for_each_entry_safe(bay, tmp, &drive_bays, list) { |
| 383 | if (is_dock_device(bay->handle)) |
| 384 | unregister_hotplug_dock_device(bay->handle); |
| 385 | acpi_bay_remove_fs(bay); |
| 386 | acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY, |
| 387 | bay_notify); |
Kristen Carlson Accardi | 2b167c0 | 2006-12-18 18:07:00 -0500 | [diff] [blame] | 388 | platform_device_unregister(bay->pdev); |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 389 | kfree(bay->name); |
| 390 | kfree(bay); |
| 391 | } |
Kristen Carlson Accardi | 01b57e7 | 2006-10-20 14:30:25 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | postcore_initcall(bay_init); |
| 395 | module_exit(bay_exit); |
| 396 | |