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
);
48 static int acpi_bay_add(struct acpi_device
*device
);
49 static int acpi_bay_remove(struct acpi_device
*device
, int type
);
51 static struct acpi_driver acpi_bay_driver
= {
53 .class = ACPI_BAY_CLASS
,
57 .remove
= acpi_bay_remove
,
64 struct list_head list
;
65 struct platform_device
*pdev
;
68 static LIST_HEAD(drive_bays
);
71 /*****************************************************************************
72 * Drive Bay functions *
73 *****************************************************************************/
75 * is_ejectable - see if a device is ejectable
76 * @handle: acpi handle of the device
78 * If an acpi object has a _EJ0 method, then it is ejectable
80 static int is_ejectable(acpi_handle handle
)
85 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
86 if (ACPI_FAILURE(status
))
92 * bay_present - see if the bay device is present
95 * execute the _STA method.
97 static int bay_present(struct bay
*bay
)
103 status
= acpi_evaluate_integer(bay
->handle
, "_STA", NULL
, &sta
);
104 if (ACPI_SUCCESS(status
) && sta
)
111 * eject_device - respond to an eject request
112 * @handle - the device to eject
114 * Call this devices _EJ0 method.
116 static void eject_device(acpi_handle handle
)
118 struct acpi_object_list arg_list
;
119 union acpi_object arg
;
121 bay_dprintk(handle
, "Ejecting device");
124 arg_list
.pointer
= &arg
;
125 arg
.type
= ACPI_TYPE_INTEGER
;
126 arg
.integer
.value
= 1;
128 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_EJ0",
130 pr_debug("Failed to evaluate _EJ0!\n");
134 * show_present - read method for "present" file in sysfs
136 static ssize_t
show_present(struct device
*dev
,
137 struct device_attribute
*attr
, char *buf
)
139 struct bay
*bay
= dev_get_drvdata(dev
);
140 return snprintf(buf
, PAGE_SIZE
, "%d\n", bay_present(bay
));
143 DEVICE_ATTR(present
, S_IRUGO
, show_present
, NULL
);
146 * write_eject - write method for "eject" file in sysfs
148 static ssize_t
write_eject(struct device
*dev
, struct device_attribute
*attr
,
149 const char *buf
, size_t count
)
151 struct bay
*bay
= dev_get_drvdata(dev
);
156 eject_device(bay
->handle
);
159 DEVICE_ATTR(eject
, S_IWUSR
, NULL
, write_eject
);
162 * is_ata - see if a device is an ata device
163 * @handle: acpi handle of the device
165 * If an acpi object has one of 4 ATA ACPI methods defined,
166 * then it is an ATA device
168 static int is_ata(acpi_handle handle
)
172 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
173 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
174 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
175 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
182 * parent_is_ata(acpi_handle handle)
185 static int parent_is_ata(acpi_handle handle
)
189 if (acpi_get_parent(handle
, &phandle
))
192 return is_ata(phandle
);
196 * is_ejectable_bay - see if a device is an ejectable drive bay
197 * @handle: acpi handle of the device
199 * If an acpi object is ejectable and has one of the ACPI ATA
200 * methods defined, then we can safely call it an ejectable
203 static int is_ejectable_bay(acpi_handle handle
)
205 if ((is_ata(handle
) || parent_is_ata(handle
)) && is_ejectable(handle
))
211 * eject_removable_drive - try to eject this drive
212 * @dev : the device structure of the drive
214 * If a device is a removable drive that requires an _EJ0 method
215 * to be executed in order to safely remove from the system, do
216 * it. ATM - always returns success
218 int eject_removable_drive(struct device
*dev
)
220 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
223 bay_dprintk(handle
, "Got device handle");
224 if (is_ejectable_bay(handle
))
225 eject_device(handle
);
227 printk("No acpi handle for device\n");
230 /* should I return an error code? */
233 EXPORT_SYMBOL_GPL(eject_removable_drive
);
235 static int acpi_bay_add(struct acpi_device
*device
)
237 bay_dprintk(device
->handle
, "adding bay device");
238 strcpy(acpi_device_name(device
), "Dockable Bay");
239 strcpy(acpi_device_class(device
), "bay");
243 static int acpi_bay_add_fs(struct bay
*bay
)
246 struct device
*dev
= &bay
->pdev
->dev
;
248 ret
= device_create_file(dev
, &dev_attr_present
);
251 ret
= device_create_file(dev
, &dev_attr_eject
);
253 device_remove_file(dev
, &dev_attr_present
);
259 bay_dprintk(bay
->handle
, "Error adding sysfs files\n");
263 static void acpi_bay_remove_fs(struct bay
*bay
)
265 struct device
*dev
= &bay
->pdev
->dev
;
268 device_remove_file(dev
, &dev_attr_present
);
269 device_remove_file(dev
, &dev_attr_eject
);
272 static int bay_is_dock_device(acpi_handle handle
)
276 acpi_get_parent(handle
, &parent
);
278 /* if the device or it's parent is dependent on the
279 * dock, then we are a dock device
281 return (is_dock_device(handle
) || is_dock_device(parent
));
284 static int bay_add(acpi_handle handle
, int id
)
288 struct platform_device
*pdev
;
289 struct acpi_buffer nbuffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
290 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &nbuffer
);
292 bay_dprintk(handle
, "Adding notify handler");
295 * Initialize bay device structure
297 new_bay
= kzalloc(sizeof(*new_bay
), GFP_ATOMIC
);
298 INIT_LIST_HEAD(&new_bay
->list
);
299 new_bay
->handle
= handle
;
300 new_bay
->name
= (char *)nbuffer
.pointer
;
302 /* initialize platform device stuff */
303 pdev
= platform_device_register_simple(ACPI_BAY_CLASS
, id
, NULL
, 0);
305 printk(KERN_ERR PREFIX
"Error registering bay device\n");
308 new_bay
->pdev
= pdev
;
309 platform_set_drvdata(pdev
, new_bay
);
311 if (acpi_bay_add_fs(new_bay
)) {
312 platform_device_unregister(new_bay
->pdev
);
316 /* register for events on this device */
317 status
= acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
318 bay_notify
, new_bay
);
319 if (ACPI_FAILURE(status
)) {
320 printk(KERN_ERR PREFIX
"Error installing bay notify handler\n");
323 /* if we are on a dock station, we should register for dock
326 if (bay_is_dock_device(handle
)) {
327 bay_dprintk(handle
, "Is dependent on dock\n");
328 register_hotplug_dock_device(handle
, bay_notify
, new_bay
);
330 list_add(&new_bay
->list
, &drive_bays
);
331 printk(KERN_INFO PREFIX
"Bay [%s] Added\n", new_bay
->name
);
335 kfree(new_bay
->name
);
340 static int acpi_bay_remove(struct acpi_device
*device
, int type
)
342 /*** FIXME: do something here */
347 * bay_create_acpi_device - add new devices to acpi
348 * @handle - handle of the device to add
350 * This function will create a new acpi_device for the given
351 * handle if one does not exist already. This should cause
352 * acpi to scan for drivers for the given devices, and call
353 * matching driver's add routine.
355 * Returns a pointer to the acpi_device corresponding to the handle.
357 static struct acpi_device
* bay_create_acpi_device(acpi_handle handle
)
359 struct acpi_device
*device
= NULL
;
360 struct acpi_device
*parent_device
;
364 bay_dprintk(handle
, "Trying to get device");
365 if (acpi_bus_get_device(handle
, &device
)) {
367 * no device created for this object,
368 * so we should create one.
370 bay_dprintk(handle
, "No device for handle");
371 acpi_get_parent(handle
, &parent
);
372 if (acpi_bus_get_device(parent
, &parent_device
))
373 parent_device
= NULL
;
375 ret
= acpi_bus_add(&device
, parent_device
, handle
,
376 ACPI_BUS_TYPE_DEVICE
);
378 pr_debug("error adding bus, %x\n",
387 * bay_notify - act upon an acpi bay notification
388 * @handle: the bay handle
389 * @event: the acpi event
390 * @data: our driver data struct
393 static void bay_notify(acpi_handle handle
, u32 event
, void *data
)
395 struct acpi_device
*dev
;
397 bay_dprintk(handle
, "Bay event");
400 case ACPI_NOTIFY_BUS_CHECK
:
401 printk("Bus Check\n");
402 case ACPI_NOTIFY_DEVICE_CHECK
:
403 printk("Device Check\n");
404 dev
= bay_create_acpi_device(handle
);
406 acpi_bus_generate_event(dev
, event
, 0);
408 printk("No device for generating event\n");
409 /* wouldn't it be a good idea to just rescan SATA
413 case ACPI_NOTIFY_EJECT_REQUEST
:
414 printk("Eject request\n");
415 dev
= bay_create_acpi_device(handle
);
417 acpi_bus_generate_event(dev
, event
, 0);
419 printk("No device for generating eventn");
421 /* wouldn't it be a good idea to just call the
422 * eject_device here if we were a SATA device?
426 printk("unknown event %d\n", event
);
431 find_bay(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
433 int *count
= (int *)context
;
436 * there could be more than one ejectable bay.
437 * so, just return AE_OK always so that every object
440 if (is_ejectable_bay(handle
)) {
441 bay_dprintk(handle
, "found ejectable bay");
442 if (!bay_add(handle
, *count
))
448 static int __init
bay_init(void)
452 INIT_LIST_HEAD(&drive_bays
);
454 /* look for dockable drive bays */
455 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
456 ACPI_UINT32_MAX
, find_bay
, &bays
, NULL
);
459 if ((acpi_bus_register_driver(&acpi_bay_driver
) < 0))
460 printk(KERN_ERR
"Unable to register bay driver\n");
468 static void __exit
bay_exit(void)
470 struct bay
*bay
, *tmp
;
472 list_for_each_entry_safe(bay
, tmp
, &drive_bays
, list
) {
473 if (is_dock_device(bay
->handle
))
474 unregister_hotplug_dock_device(bay
->handle
);
475 acpi_bay_remove_fs(bay
);
476 acpi_remove_notify_handler(bay
->handle
, ACPI_SYSTEM_NOTIFY
,
478 platform_device_unregister(bay
->pdev
);
483 acpi_bus_unregister_driver(&acpi_bay_driver
);
486 postcore_initcall(bay_init
);
487 module_exit(bay_exit
);