2 * bay.c - ACPI removable drive bay driver
4 * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <asm/uaccess.h>
35 #define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
37 ACPI_MODULE_NAME("bay")
38 MODULE_AUTHOR("Kristen Carlson Accardi");
39 MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME);
40 MODULE_LICENSE("GPL");
41 #define ACPI_BAY_CLASS "bay"
42 #define ACPI_BAY_COMPONENT 0x10000000
43 #define _COMPONENT ACPI_BAY_COMPONENT
44 #define bay_dprintk(h,s) {\
45 char prefix[80] = {'\0'};\
46 struct acpi_buffer buffer = {sizeof(prefix), prefix};\
47 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
48 printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
50 static void bay_notify(acpi_handle handle, u32 event, void *data);
51 static int acpi_bay_add(struct acpi_device *device);
52 static int acpi_bay_remove(struct acpi_device *device, int type);
53 static int acpi_bay_match(struct acpi_device *device,
54 struct acpi_driver *driver);
56 static struct acpi_driver acpi_bay_driver = {
57 .name = ACPI_BAY_DRIVER_NAME,
58 .class = ACPI_BAY_CLASS,
61 .remove = acpi_bay_remove,
62 .match = acpi_bay_match,
69 struct list_head list;
70 struct proc_dir_entry *proc;
73 LIST_HEAD(drive_bays);
75 static struct proc_dir_entry *acpi_bay_dir;
77 /*****************************************************************************
78 * Drive Bay functions *
79 *****************************************************************************/
81 * is_ejectable - see if a device is ejectable
82 * @handle: acpi handle of the device
84 * If an acpi object has a _EJ0 method, then it is ejectable
86 static int is_ejectable(acpi_handle handle)
91 status = acpi_get_handle(handle, "_EJ0", &tmp);
92 if (ACPI_FAILURE(status))
98 * bay_present - see if the bay device is present
101 * execute the _STA method.
103 static int bay_present(struct bay *bay)
109 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
110 if (ACPI_SUCCESS(status) && sta)
117 * eject_device - respond to an eject request
118 * @handle - the device to eject
120 * Call this devices _EJ0 method.
122 static void eject_device(acpi_handle handle)
124 struct acpi_object_list arg_list;
125 union acpi_object arg;
127 bay_dprintk(handle, "Ejecting device");
130 arg_list.pointer = &arg;
131 arg.type = ACPI_TYPE_INTEGER;
132 arg.integer.value = 1;
134 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
136 pr_debug("Failed to evaluate _EJ0!\n");
141 * is_ata - see if a device is an ata device
142 * @handle: acpi handle of the device
144 * If an acpi object has one of 4 ATA ACPI methods defined,
145 * then it is an ATA device
147 static int is_ata(acpi_handle handle)
151 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
152 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
153 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
154 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
161 * parent_is_ata(acpi_handle handle)
164 static int parent_is_ata(acpi_handle handle)
168 if (acpi_get_parent(handle, &phandle))
171 return is_ata(phandle);
175 * is_ejectable_bay - see if a device is an ejectable drive bay
176 * @handle: acpi handle of the device
178 * If an acpi object is ejectable and has one of the ACPI ATA
179 * methods defined, then we can safely call it an ejectable
182 static int is_ejectable_bay(acpi_handle handle)
184 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
190 * eject_removable_drive - try to eject this drive
191 * @dev : the device structure of the drive
193 * If a device is a removable drive that requires an _EJ0 method
194 * to be executed in order to safely remove from the system, do
195 * it. ATM - always returns success
197 int eject_removable_drive(struct device *dev)
199 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
202 bay_dprintk(handle, "Got device handle");
203 if (is_ejectable_bay(handle))
204 eject_device(handle);
206 printk("No acpi handle for device\n");
209 /* should I return an error code? */
212 EXPORT_SYMBOL_GPL(eject_removable_drive);
214 static int acpi_bay_add(struct acpi_device *device)
216 bay_dprintk(device->handle, "adding bay device");
217 strcpy(acpi_device_name(device), "Dockable Bay");
218 strcpy(acpi_device_class(device), "bay");
222 static int acpi_bay_status_seq_show(struct seq_file *seq, void *offset)
224 struct bay *bay = (struct bay *)seq->private;
229 if (bay_present(bay))
230 seq_printf(seq, "present\n");
232 seq_printf(seq, "removed\n");
238 acpi_bay_write_eject(struct file *file,
239 const char __user * buffer,
240 size_t count, loff_t * data)
242 struct seq_file *m = (struct seq_file *)file->private_data;
243 struct bay *bay = (struct bay *)m->private;
244 char str[12] = { 0 };
247 /* FIXME - our only valid value here is 1 */
248 if (!bay || count + 1 > sizeof str)
251 if (copy_from_user(str, buffer, count))
255 state = simple_strtoul(str, NULL, 0);
257 eject_device(bay->handle);
263 acpi_bay_status_open_fs(struct inode *inode, struct file *file)
265 return single_open(file, acpi_bay_status_seq_show,
270 acpi_bay_eject_open_fs(struct inode *inode, struct file *file)
272 return single_open(file, acpi_bay_status_seq_show,
276 static struct file_operations acpi_bay_status_fops = {
277 .open = acpi_bay_status_open_fs,
280 .release = single_release,
283 static struct file_operations acpi_bay_eject_fops = {
284 .open = acpi_bay_eject_open_fs,
286 .write = acpi_bay_write_eject,
288 .release = single_release,
291 static struct file_operations acpi_bay_insert_fops = {
292 .open = acpi_bay_insert_open_fs,
295 .release = single_release,
298 static int acpi_bay_add_fs(struct bay *bay)
300 struct proc_dir_entry *entry = NULL;
306 * create a proc entry for this device
307 * we need to do this a little bit differently than normal
308 * acpi device drivers because our device may not be present
309 * at the moment, and therefore we have no acpi_device struct
312 bay->proc = proc_mkdir(bay->name, acpi_bay_dir);
315 entry = create_proc_entry("status",
320 entry->proc_fops = &acpi_bay_status_fops;
322 entry->owner = THIS_MODULE;
325 entry = create_proc_entry("eject",
330 entry->proc_fops = &acpi_bay_eject_fops;
332 entry->owner = THIS_MODULE;
336 entry = create_proc_entry("insert",
341 entry->proc_fops = &acpi_bay_insert_fops;
343 entry->owner = THIS_MODULE;
349 static void acpi_bay_remove_fs(struct bay *bay)
355 remove_proc_entry("status", bay->proc);
356 remove_proc_entry("eject", bay->proc);
358 remove_proc_entry("insert", bay->proc);
360 remove_proc_entry(bay->name, acpi_bay_dir);
365 static int bay_is_dock_device(acpi_handle handle)
369 acpi_get_parent(handle, &parent);
371 /* if the device or it's parent is dependent on the
372 * dock, then we are a dock device
374 return (is_dock_device(handle) || is_dock_device(parent));
377 static int bay_add(acpi_handle handle)
381 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
382 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
384 bay_dprintk(handle, "Adding notify handler");
387 * if this is the first bay device found, make the root
390 if (acpi_bay_dir == NULL)
391 acpi_bay_dir = proc_mkdir(ACPI_BAY_CLASS, acpi_root_dir);
394 * Initialize bay device structure
396 new_bay = kmalloc(GFP_ATOMIC, sizeof(*new_bay));
397 INIT_LIST_HEAD(&new_bay->list);
398 new_bay->handle = handle;
399 new_bay->name = (char *)nbuffer.pointer;
400 list_add(&new_bay->list, &drive_bays);
401 acpi_bay_add_fs(new_bay);
403 /* register for events on this device */
404 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
405 bay_notify, new_bay);
406 if (ACPI_FAILURE(status)) {
407 printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
410 /* if we are on a dock station, we should register for dock
413 if (bay_is_dock_device(handle)) {
414 bay_dprintk(handle, "Is dependent on dock\n");
415 register_hotplug_dock_device(handle, bay_notify, new_bay);
417 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
421 static int acpi_bay_remove(struct acpi_device *device, int type)
423 /*** FIXME: do something here */
427 static int acpi_bay_match(struct acpi_device *device,
428 struct acpi_driver *driver)
430 if (!device || !driver)
433 if (is_ejectable_bay(device->handle)) {
434 bay_dprintk(device->handle, "matching bay device");
442 * bay_create_acpi_device - add new devices to acpi
443 * @handle - handle of the device to add
445 * This function will create a new acpi_device for the given
446 * handle if one does not exist already. This should cause
447 * acpi to scan for drivers for the given devices, and call
448 * matching driver's add routine.
450 * Returns a pointer to the acpi_device corresponding to the handle.
452 static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
454 struct acpi_device *device = NULL;
455 struct acpi_device *parent_device;
459 bay_dprintk(handle, "Trying to get device");
460 if (acpi_bus_get_device(handle, &device)) {
462 * no device created for this object,
463 * so we should create one.
465 bay_dprintk(handle, "No device for handle");
466 acpi_get_parent(handle, &parent);
467 if (acpi_bus_get_device(parent, &parent_device))
468 parent_device = NULL;
470 ret = acpi_bus_add(&device, parent_device, handle,
471 ACPI_BUS_TYPE_DEVICE);
473 pr_debug("error adding bus, %x\n",
482 * bay_notify - act upon an acpi bay notification
483 * @handle: the bay handle
484 * @event: the acpi event
485 * @data: our driver data struct
488 static void bay_notify(acpi_handle handle, u32 event, void *data)
490 struct acpi_device *dev;
492 bay_dprintk(handle, "Bay event");
495 case ACPI_NOTIFY_BUS_CHECK:
496 printk("Bus Check\n");
497 case ACPI_NOTIFY_DEVICE_CHECK:
498 printk("Device Check\n");
499 dev = bay_create_acpi_device(handle);
501 acpi_bus_generate_event(dev, event, 0);
503 printk("No device for generating event\n");
504 /* wouldn't it be a good idea to just rescan SATA
508 case ACPI_NOTIFY_EJECT_REQUEST:
509 printk("Eject request\n");
510 dev = bay_create_acpi_device(handle);
512 acpi_bus_generate_event(dev, event, 0);
514 printk("No device for generating eventn");
516 /* wouldn't it be a good idea to just call the
517 * eject_device here if we were a SATA device?
521 printk("unknown event %d\n", event);
526 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
528 int *count = (int *)context;
531 * there could be more than one ejectable bay.
532 * so, just return AE_OK always so that every object
535 if (is_ejectable_bay(handle)) {
536 bay_dprintk(handle, "found ejectable bay");
543 static int __init bay_init(void)
548 INIT_LIST_HEAD(&drive_bays);
550 /* look for dockable drive bays */
551 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
552 ACPI_UINT32_MAX, find_bay, &bays, NULL);
555 if ((acpi_bus_register_driver(&acpi_bay_driver) < 0)) {
556 printk(KERN_ERR "Unable to register bay driver\n");
558 remove_proc_entry(ACPI_BAY_CLASS,
569 static void __exit bay_exit(void)
571 struct bay *bay, *tmp;
573 list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
574 if (is_dock_device(bay->handle))
575 unregister_hotplug_dock_device(bay->handle);
576 acpi_bay_remove_fs(bay);
577 acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
584 remove_proc_entry(ACPI_BAY_CLASS, acpi_root_dir);
586 acpi_bus_unregister_driver(&acpi_bay_driver);
589 postcore_initcall(bay_init);
590 module_exit(bay_exit);