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/seq_file.h>
32 #include <asm/uaccess.h>
33 #include <linux/platform_device.h>
35 ACPI_MODULE_NAME("bay");
36 MODULE_AUTHOR("Kristen Carlson Accardi");
37 MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver");
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); }
47 static void bay_notify(acpi_handle handle
, u32 event
, void *data
);
52 struct list_head list
;
53 struct platform_device
*pdev
;
56 static LIST_HEAD(drive_bays
);
59 /*****************************************************************************
60 * Drive Bay functions *
61 *****************************************************************************/
63 * is_ejectable - see if a device is ejectable
64 * @handle: acpi handle of the device
66 * If an acpi object has a _EJ0 method, then it is ejectable
68 static int is_ejectable(acpi_handle handle
)
73 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
74 if (ACPI_FAILURE(status
))
80 * bay_present - see if the bay device is present
83 * execute the _STA method.
85 static int bay_present(struct bay
*bay
)
91 status
= acpi_evaluate_integer(bay
->handle
, "_STA", NULL
, &sta
);
92 if (ACPI_SUCCESS(status
) && sta
)
99 * eject_device - respond to an eject request
100 * @handle - the device to eject
102 * Call this devices _EJ0 method.
104 static void eject_device(acpi_handle handle
)
106 struct acpi_object_list arg_list
;
107 union acpi_object arg
;
109 bay_dprintk(handle
, "Ejecting device");
112 arg_list
.pointer
= &arg
;
113 arg
.type
= ACPI_TYPE_INTEGER
;
114 arg
.integer
.value
= 1;
116 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_EJ0",
118 pr_debug("Failed to evaluate _EJ0!\n");
122 * show_present - read method for "present" file in sysfs
124 static ssize_t
show_present(struct device
*dev
,
125 struct device_attribute
*attr
, char *buf
)
127 struct bay
*bay
= dev_get_drvdata(dev
);
128 return snprintf(buf
, PAGE_SIZE
, "%d\n", bay_present(bay
));
131 DEVICE_ATTR(present
, S_IRUGO
, show_present
, NULL
);
134 * write_eject - write method for "eject" file in sysfs
136 static ssize_t
write_eject(struct device
*dev
, struct device_attribute
*attr
,
137 const char *buf
, size_t count
)
139 struct bay
*bay
= dev_get_drvdata(dev
);
144 eject_device(bay
->handle
);
147 DEVICE_ATTR(eject
, S_IWUSR
, NULL
, write_eject
);
150 * is_ata - see if a device is an ata device
151 * @handle: acpi handle of the device
153 * If an acpi object has one of 4 ATA ACPI methods defined,
154 * then it is an ATA device
156 static int is_ata(acpi_handle handle
)
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
))))
170 * parent_is_ata(acpi_handle handle)
173 static int parent_is_ata(acpi_handle handle
)
177 if (acpi_get_parent(handle
, &phandle
))
180 return is_ata(phandle
);
184 * is_ejectable_bay - see if a device is an ejectable drive bay
185 * @handle: acpi handle of the device
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
191 static int is_ejectable_bay(acpi_handle handle
)
193 if ((is_ata(handle
) || parent_is_ata(handle
)) && is_ejectable(handle
))
199 * eject_removable_drive - try to eject this drive
200 * @dev : the device structure of the drive
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
206 int eject_removable_drive(struct device
*dev
)
208 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
211 bay_dprintk(handle
, "Got device handle");
212 if (is_ejectable_bay(handle
))
213 eject_device(handle
);
215 printk("No acpi handle for device\n");
218 /* should I return an error code? */
221 EXPORT_SYMBOL_GPL(eject_removable_drive
);
223 static int acpi_bay_add_fs(struct bay
*bay
)
226 struct device
*dev
= &bay
->pdev
->dev
;
228 ret
= device_create_file(dev
, &dev_attr_present
);
231 ret
= device_create_file(dev
, &dev_attr_eject
);
233 device_remove_file(dev
, &dev_attr_present
);
239 bay_dprintk(bay
->handle
, "Error adding sysfs files\n");
243 static void acpi_bay_remove_fs(struct bay
*bay
)
245 struct device
*dev
= &bay
->pdev
->dev
;
248 device_remove_file(dev
, &dev_attr_present
);
249 device_remove_file(dev
, &dev_attr_eject
);
252 static int bay_is_dock_device(acpi_handle handle
)
256 acpi_get_parent(handle
, &parent
);
258 /* if the device or it's parent is dependent on the
259 * dock, then we are a dock device
261 return (is_dock_device(handle
) || is_dock_device(parent
));
264 static int bay_add(acpi_handle handle
, int id
)
268 struct platform_device
*pdev
;
269 struct acpi_buffer nbuffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
270 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &nbuffer
);
272 bay_dprintk(handle
, "Adding notify handler");
275 * Initialize bay device structure
277 new_bay
= kzalloc(sizeof(*new_bay
), GFP_ATOMIC
);
278 INIT_LIST_HEAD(&new_bay
->list
);
279 new_bay
->handle
= handle
;
280 new_bay
->name
= (char *)nbuffer
.pointer
;
282 /* initialize platform device stuff */
283 pdev
= platform_device_register_simple(ACPI_BAY_CLASS
, id
, NULL
, 0);
285 printk(KERN_ERR PREFIX
"Error registering bay device\n");
288 new_bay
->pdev
= pdev
;
289 platform_set_drvdata(pdev
, new_bay
);
291 if (acpi_bay_add_fs(new_bay
)) {
292 platform_device_unregister(new_bay
->pdev
);
296 /* register for events on this device */
297 status
= acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
298 bay_notify
, new_bay
);
299 if (ACPI_FAILURE(status
)) {
300 printk(KERN_ERR PREFIX
"Error installing bay notify handler\n");
303 /* if we are on a dock station, we should register for dock
306 if (bay_is_dock_device(handle
)) {
307 bay_dprintk(handle
, "Is dependent on dock\n");
308 register_hotplug_dock_device(handle
, bay_notify
, new_bay
);
310 list_add(&new_bay
->list
, &drive_bays
);
311 printk(KERN_INFO PREFIX
"Bay [%s] Added\n", new_bay
->name
);
315 kfree(new_bay
->name
);
321 * bay_notify - act upon an acpi bay notification
322 * @handle: the bay handle
323 * @event: the acpi event
324 * @data: our driver data struct
327 static void bay_notify(acpi_handle handle
, u32 event
, void *data
)
329 struct bay
*bay_dev
= (struct bay
*)data
;
330 struct device
*dev
= &bay_dev
->pdev
->dev
;
332 bay_dprintk(handle
, "Bay event");
335 case ACPI_NOTIFY_BUS_CHECK
:
336 case ACPI_NOTIFY_DEVICE_CHECK
:
337 case ACPI_NOTIFY_EJECT_REQUEST
:
338 kobject_uevent(&dev
->kobj
, KOBJ_CHANGE
);
341 printk(KERN_ERR PREFIX
"Bay: unknown event %d\n", event
);
346 find_bay(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
348 int *count
= (int *)context
;
351 * there could be more than one ejectable bay.
352 * so, just return AE_OK always so that every object
355 if (is_ejectable_bay(handle
)) {
356 bay_dprintk(handle
, "found ejectable bay");
357 if (!bay_add(handle
, *count
))
363 static int __init
bay_init(void)
367 INIT_LIST_HEAD(&drive_bays
);
369 /* look for dockable drive bays */
370 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
371 ACPI_UINT32_MAX
, find_bay
, &bays
, NULL
);
379 static void __exit
bay_exit(void)
381 struct bay
*bay
, *tmp
;
383 list_for_each_entry_safe(bay
, tmp
, &drive_bays
, list
) {
384 if (is_dock_device(bay
->handle
))
385 unregister_hotplug_dock_device(bay
->handle
);
386 acpi_bay_remove_fs(bay
);
387 acpi_remove_notify_handler(bay
->handle
, ACPI_SYSTEM_NOTIFY
,
389 platform_device_unregister(bay
->pdev
);
395 postcore_initcall(bay_init
);
396 module_exit(bay_exit
);