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>
34 #define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
36 ACPI_MODULE_NAME("bay")
37 MODULE_AUTHOR("Kristen Carlson Accardi");
38 MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME
);
39 MODULE_LICENSE("GPL");
40 #define ACPI_BAY_CLASS "bay"
41 #define ACPI_BAY_COMPONENT 0x10000000
42 #define _COMPONENT ACPI_BAY_COMPONENT
43 #define bay_dprintk(h,s) {\
44 char prefix[80] = {'\0'};\
45 struct acpi_buffer buffer = {sizeof(prefix), prefix};\
46 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
47 printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
49 static void bay_notify(acpi_handle handle
, u32 event
, void *data
);
50 static int acpi_bay_add(struct acpi_device
*device
);
51 static int acpi_bay_remove(struct acpi_device
*device
, int type
);
52 static int acpi_bay_match(struct acpi_device
*device
,
53 struct acpi_driver
*driver
);
55 static struct acpi_driver acpi_bay_driver
= {
56 .name
= ACPI_BAY_DRIVER_NAME
,
57 .class = ACPI_BAY_CLASS
,
60 .remove
= acpi_bay_remove
,
61 .match
= acpi_bay_match
,
68 struct list_head list
;
71 LIST_HEAD(drive_bays
);
74 /*****************************************************************************
75 * Drive Bay functions *
76 *****************************************************************************/
78 * is_ejectable - see if a device is ejectable
79 * @handle: acpi handle of the device
81 * If an acpi object has a _EJ0 method, then it is ejectable
83 static int is_ejectable(acpi_handle handle
)
88 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
89 if (ACPI_FAILURE(status
))
95 * bay_present - see if the bay device is present
98 * execute the _STA method.
100 static int bay_present(struct bay
*bay
)
106 status
= acpi_evaluate_integer(bay
->handle
, "_STA", NULL
, &sta
);
107 if (ACPI_SUCCESS(status
) && sta
)
114 * eject_device - respond to an eject request
115 * @handle - the device to eject
117 * Call this devices _EJ0 method.
119 static void eject_device(acpi_handle handle
)
121 struct acpi_object_list arg_list
;
122 union acpi_object arg
;
124 bay_dprintk(handle
, "Ejecting device");
127 arg_list
.pointer
= &arg
;
128 arg
.type
= ACPI_TYPE_INTEGER
;
129 arg
.integer
.value
= 1;
131 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_EJ0",
133 pr_debug("Failed to evaluate _EJ0!\n");
138 * is_ata - see if a device is an ata device
139 * @handle: acpi handle of the device
141 * If an acpi object has one of 4 ATA ACPI methods defined,
142 * then it is an ATA device
144 static int is_ata(acpi_handle handle
)
148 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
149 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
150 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
151 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
158 * parent_is_ata(acpi_handle handle)
161 static int parent_is_ata(acpi_handle handle
)
165 if (acpi_get_parent(handle
, &phandle
))
168 return is_ata(phandle
);
172 * is_ejectable_bay - see if a device is an ejectable drive bay
173 * @handle: acpi handle of the device
175 * If an acpi object is ejectable and has one of the ACPI ATA
176 * methods defined, then we can safely call it an ejectable
179 static int is_ejectable_bay(acpi_handle handle
)
181 if ((is_ata(handle
) || parent_is_ata(handle
)) && is_ejectable(handle
))
187 * eject_removable_drive - try to eject this drive
188 * @dev : the device structure of the drive
190 * If a device is a removable drive that requires an _EJ0 method
191 * to be executed in order to safely remove from the system, do
192 * it. ATM - always returns success
194 int eject_removable_drive(struct device
*dev
)
196 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
199 bay_dprintk(handle
, "Got device handle");
200 if (is_ejectable_bay(handle
))
201 eject_device(handle
);
203 printk("No acpi handle for device\n");
206 /* should I return an error code? */
209 EXPORT_SYMBOL_GPL(eject_removable_drive
);
211 static int acpi_bay_add(struct acpi_device
*device
)
213 bay_dprintk(device
->handle
, "adding bay device");
214 strcpy(acpi_device_name(device
), "Dockable Bay");
215 strcpy(acpi_device_class(device
), "bay");
219 static int acpi_bay_add_fs(struct bay
*bay
)
225 static void acpi_bay_remove_fs(struct bay
*bay
)
231 static int bay_is_dock_device(acpi_handle handle
)
235 acpi_get_parent(handle
, &parent
);
237 /* if the device or it's parent is dependent on the
238 * dock, then we are a dock device
240 return (is_dock_device(handle
) || is_dock_device(parent
));
243 static int bay_add(acpi_handle handle
)
247 struct acpi_buffer nbuffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
248 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &nbuffer
);
250 bay_dprintk(handle
, "Adding notify handler");
253 * Initialize bay device structure
255 new_bay
= kmalloc(GFP_ATOMIC
, sizeof(*new_bay
));
256 INIT_LIST_HEAD(&new_bay
->list
);
257 new_bay
->handle
= handle
;
258 new_bay
->name
= (char *)nbuffer
.pointer
;
259 list_add(&new_bay
->list
, &drive_bays
);
260 acpi_bay_add_fs(new_bay
);
262 /* register for events on this device */
263 status
= acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
264 bay_notify
, new_bay
);
265 if (ACPI_FAILURE(status
)) {
266 printk(KERN_ERR PREFIX
"Error installing bay notify handler\n");
269 /* if we are on a dock station, we should register for dock
272 if (bay_is_dock_device(handle
)) {
273 bay_dprintk(handle
, "Is dependent on dock\n");
274 register_hotplug_dock_device(handle
, bay_notify
, new_bay
);
276 printk(KERN_INFO PREFIX
"Bay [%s] Added\n", new_bay
->name
);
280 static int acpi_bay_remove(struct acpi_device
*device
, int type
)
282 /*** FIXME: do something here */
286 static int acpi_bay_match(struct acpi_device
*device
,
287 struct acpi_driver
*driver
)
289 if (!device
|| !driver
)
292 if (is_ejectable_bay(device
->handle
)) {
293 bay_dprintk(device
->handle
, "matching bay device");
301 * bay_create_acpi_device - add new devices to acpi
302 * @handle - handle of the device to add
304 * This function will create a new acpi_device for the given
305 * handle if one does not exist already. This should cause
306 * acpi to scan for drivers for the given devices, and call
307 * matching driver's add routine.
309 * Returns a pointer to the acpi_device corresponding to the handle.
311 static struct acpi_device
* bay_create_acpi_device(acpi_handle handle
)
313 struct acpi_device
*device
= NULL
;
314 struct acpi_device
*parent_device
;
318 bay_dprintk(handle
, "Trying to get device");
319 if (acpi_bus_get_device(handle
, &device
)) {
321 * no device created for this object,
322 * so we should create one.
324 bay_dprintk(handle
, "No device for handle");
325 acpi_get_parent(handle
, &parent
);
326 if (acpi_bus_get_device(parent
, &parent_device
))
327 parent_device
= NULL
;
329 ret
= acpi_bus_add(&device
, parent_device
, handle
,
330 ACPI_BUS_TYPE_DEVICE
);
332 pr_debug("error adding bus, %x\n",
341 * bay_notify - act upon an acpi bay notification
342 * @handle: the bay handle
343 * @event: the acpi event
344 * @data: our driver data struct
347 static void bay_notify(acpi_handle handle
, u32 event
, void *data
)
349 struct acpi_device
*dev
;
351 bay_dprintk(handle
, "Bay event");
354 case ACPI_NOTIFY_BUS_CHECK
:
355 printk("Bus Check\n");
356 case ACPI_NOTIFY_DEVICE_CHECK
:
357 printk("Device Check\n");
358 dev
= bay_create_acpi_device(handle
);
360 acpi_bus_generate_event(dev
, event
, 0);
362 printk("No device for generating event\n");
363 /* wouldn't it be a good idea to just rescan SATA
367 case ACPI_NOTIFY_EJECT_REQUEST
:
368 printk("Eject request\n");
369 dev
= bay_create_acpi_device(handle
);
371 acpi_bus_generate_event(dev
, event
, 0);
373 printk("No device for generating eventn");
375 /* wouldn't it be a good idea to just call the
376 * eject_device here if we were a SATA device?
380 printk("unknown event %d\n", event
);
385 find_bay(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
387 int *count
= (int *)context
;
390 * there could be more than one ejectable bay.
391 * so, just return AE_OK always so that every object
394 if (is_ejectable_bay(handle
)) {
395 bay_dprintk(handle
, "found ejectable bay");
402 static int __init
bay_init(void)
406 INIT_LIST_HEAD(&drive_bays
);
408 /* look for dockable drive bays */
409 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
410 ACPI_UINT32_MAX
, find_bay
, &bays
, NULL
);
413 if ((acpi_bus_register_driver(&acpi_bay_driver
) < 0))
414 printk(KERN_ERR
"Unable to register bay driver\n");
422 static void __exit
bay_exit(void)
424 struct bay
*bay
, *tmp
;
426 list_for_each_entry_safe(bay
, tmp
, &drive_bays
, list
) {
427 if (is_dock_device(bay
->handle
))
428 unregister_hotplug_dock_device(bay
->handle
);
429 acpi_bay_remove_fs(bay
);
430 acpi_remove_notify_handler(bay
->handle
, ACPI_SYSTEM_NOTIFY
,
436 acpi_bus_unregister_driver(&acpi_bay_driver
);
439 postcore_initcall(bay_init
);
440 module_exit(bay_exit
);