ACPI: bay: make drive_bays static
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / bay.c
blobaa1b131c0fceba41b7d3bf7fc6fdaeb178db91cb
1 /*
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 #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); }
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,
58 .ops = {
59 .add = acpi_bay_add,
60 .remove = acpi_bay_remove,
61 .match = acpi_bay_match,
65 struct bay {
66 acpi_handle handle;
67 char *name;
68 struct list_head list;
69 struct platform_device *pdev;
72 static LIST_HEAD(drive_bays);
75 /*****************************************************************************
76 * Drive Bay functions *
77 *****************************************************************************/
78 /**
79 * is_ejectable - see if a device is ejectable
80 * @handle: acpi handle of the device
82 * If an acpi object has a _EJ0 method, then it is ejectable
84 static int is_ejectable(acpi_handle handle)
86 acpi_status status;
87 acpi_handle tmp;
89 status = acpi_get_handle(handle, "_EJ0", &tmp);
90 if (ACPI_FAILURE(status))
91 return 0;
92 return 1;
95 /**
96 * bay_present - see if the bay device is present
97 * @bay: the drive bay
99 * execute the _STA method.
101 static int bay_present(struct bay *bay)
103 unsigned long sta;
104 acpi_status status;
106 if (bay) {
107 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
108 if (ACPI_SUCCESS(status) && sta)
109 return 1;
111 return 0;
115 * eject_device - respond to an eject request
116 * @handle - the device to eject
118 * Call this devices _EJ0 method.
120 static void eject_device(acpi_handle handle)
122 struct acpi_object_list arg_list;
123 union acpi_object arg;
125 bay_dprintk(handle, "Ejecting device");
127 arg_list.count = 1;
128 arg_list.pointer = &arg;
129 arg.type = ACPI_TYPE_INTEGER;
130 arg.integer.value = 1;
132 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
133 &arg_list, NULL)))
134 pr_debug("Failed to evaluate _EJ0!\n");
138 * show_present - read method for "present" file in sysfs
140 static ssize_t show_present(struct device *dev,
141 struct device_attribute *attr, char *buf)
143 struct bay *bay = dev_get_drvdata(dev);
144 return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
147 DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
150 * write_eject - write method for "eject" file in sysfs
152 static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t count)
155 struct bay *bay = dev_get_drvdata(dev);
157 if (!count)
158 return -EINVAL;
160 eject_device(bay->handle);
161 return count;
163 DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
166 * is_ata - see if a device is an ata device
167 * @handle: acpi handle of the device
169 * If an acpi object has one of 4 ATA ACPI methods defined,
170 * then it is an ATA device
172 static int is_ata(acpi_handle handle)
174 acpi_handle tmp;
176 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
177 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
178 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
179 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
180 return 1;
182 return 0;
186 * parent_is_ata(acpi_handle handle)
189 static int parent_is_ata(acpi_handle handle)
191 acpi_handle phandle;
193 if (acpi_get_parent(handle, &phandle))
194 return 0;
196 return is_ata(phandle);
200 * is_ejectable_bay - see if a device is an ejectable drive bay
201 * @handle: acpi handle of the device
203 * If an acpi object is ejectable and has one of the ACPI ATA
204 * methods defined, then we can safely call it an ejectable
205 * drive bay
207 static int is_ejectable_bay(acpi_handle handle)
209 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
210 return 1;
211 return 0;
215 * eject_removable_drive - try to eject this drive
216 * @dev : the device structure of the drive
218 * If a device is a removable drive that requires an _EJ0 method
219 * to be executed in order to safely remove from the system, do
220 * it. ATM - always returns success
222 int eject_removable_drive(struct device *dev)
224 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
226 if (handle) {
227 bay_dprintk(handle, "Got device handle");
228 if (is_ejectable_bay(handle))
229 eject_device(handle);
230 } else {
231 printk("No acpi handle for device\n");
234 /* should I return an error code? */
235 return 0;
237 EXPORT_SYMBOL_GPL(eject_removable_drive);
239 static int acpi_bay_add(struct acpi_device *device)
241 bay_dprintk(device->handle, "adding bay device");
242 strcpy(acpi_device_name(device), "Dockable Bay");
243 strcpy(acpi_device_class(device), "bay");
244 return 0;
247 static int acpi_bay_add_fs(struct bay *bay)
249 int ret;
250 struct device *dev = &bay->pdev->dev;
252 ret = device_create_file(dev, &dev_attr_present);
253 if (ret)
254 goto add_fs_err;
255 ret = device_create_file(dev, &dev_attr_eject);
256 if (ret) {
257 device_remove_file(dev, &dev_attr_present);
258 goto add_fs_err;
260 return 0;
262 add_fs_err:
263 bay_dprintk(bay->handle, "Error adding sysfs files\n");
264 return ret;
267 static void acpi_bay_remove_fs(struct bay *bay)
269 struct device *dev = &bay->pdev->dev;
271 /* cleanup sysfs */
272 device_remove_file(dev, &dev_attr_present);
273 device_remove_file(dev, &dev_attr_eject);
276 static int bay_is_dock_device(acpi_handle handle)
278 acpi_handle parent;
280 acpi_get_parent(handle, &parent);
282 /* if the device or it's parent is dependent on the
283 * dock, then we are a dock device
285 return (is_dock_device(handle) || is_dock_device(parent));
288 static int bay_add(acpi_handle handle, int id)
290 acpi_status status;
291 struct bay *new_bay;
292 struct platform_device *pdev;
293 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
294 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
296 bay_dprintk(handle, "Adding notify handler");
299 * Initialize bay device structure
301 new_bay = kzalloc(GFP_ATOMIC, sizeof(*new_bay));
302 INIT_LIST_HEAD(&new_bay->list);
303 new_bay->handle = handle;
304 new_bay->name = (char *)nbuffer.pointer;
306 /* initialize platform device stuff */
307 pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
308 if (pdev == NULL) {
309 printk(KERN_ERR PREFIX "Error registering bay device\n");
310 goto bay_add_err;
312 new_bay->pdev = pdev;
313 platform_set_drvdata(pdev, new_bay);
315 if (acpi_bay_add_fs(new_bay)) {
316 platform_device_unregister(new_bay->pdev);
317 goto bay_add_err;
320 /* register for events on this device */
321 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
322 bay_notify, new_bay);
323 if (ACPI_FAILURE(status)) {
324 printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
327 /* if we are on a dock station, we should register for dock
328 * notifications.
330 if (bay_is_dock_device(handle)) {
331 bay_dprintk(handle, "Is dependent on dock\n");
332 register_hotplug_dock_device(handle, bay_notify, new_bay);
334 list_add(&new_bay->list, &drive_bays);
335 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
336 return 0;
338 bay_add_err:
339 kfree(new_bay->name);
340 kfree(new_bay);
341 return -ENODEV;
344 static int acpi_bay_remove(struct acpi_device *device, int type)
346 /*** FIXME: do something here */
347 return 0;
350 static int acpi_bay_match(struct acpi_device *device,
351 struct acpi_driver *driver)
353 if (!device || !driver)
354 return -EINVAL;
356 if (is_ejectable_bay(device->handle)) {
357 bay_dprintk(device->handle, "matching bay device");
358 return 0;
361 return -ENODEV;
365 * bay_create_acpi_device - add new devices to acpi
366 * @handle - handle of the device to add
368 * This function will create a new acpi_device for the given
369 * handle if one does not exist already. This should cause
370 * acpi to scan for drivers for the given devices, and call
371 * matching driver's add routine.
373 * Returns a pointer to the acpi_device corresponding to the handle.
375 static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
377 struct acpi_device *device = NULL;
378 struct acpi_device *parent_device;
379 acpi_handle parent;
380 int ret;
382 bay_dprintk(handle, "Trying to get device");
383 if (acpi_bus_get_device(handle, &device)) {
385 * no device created for this object,
386 * so we should create one.
388 bay_dprintk(handle, "No device for handle");
389 acpi_get_parent(handle, &parent);
390 if (acpi_bus_get_device(parent, &parent_device))
391 parent_device = NULL;
393 ret = acpi_bus_add(&device, parent_device, handle,
394 ACPI_BUS_TYPE_DEVICE);
395 if (ret) {
396 pr_debug("error adding bus, %x\n",
397 -ret);
398 return NULL;
401 return device;
405 * bay_notify - act upon an acpi bay notification
406 * @handle: the bay handle
407 * @event: the acpi event
408 * @data: our driver data struct
411 static void bay_notify(acpi_handle handle, u32 event, void *data)
413 struct acpi_device *dev;
415 bay_dprintk(handle, "Bay event");
417 switch(event) {
418 case ACPI_NOTIFY_BUS_CHECK:
419 printk("Bus Check\n");
420 case ACPI_NOTIFY_DEVICE_CHECK:
421 printk("Device Check\n");
422 dev = bay_create_acpi_device(handle);
423 if (dev)
424 acpi_bus_generate_event(dev, event, 0);
425 else
426 printk("No device for generating event\n");
427 /* wouldn't it be a good idea to just rescan SATA
428 * right here?
430 break;
431 case ACPI_NOTIFY_EJECT_REQUEST:
432 printk("Eject request\n");
433 dev = bay_create_acpi_device(handle);
434 if (dev)
435 acpi_bus_generate_event(dev, event, 0);
436 else
437 printk("No device for generating eventn");
439 /* wouldn't it be a good idea to just call the
440 * eject_device here if we were a SATA device?
442 break;
443 default:
444 printk("unknown event %d\n", event);
448 static acpi_status
449 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
451 int *count = (int *)context;
454 * there could be more than one ejectable bay.
455 * so, just return AE_OK always so that every object
456 * will be checked.
458 if (is_ejectable_bay(handle)) {
459 bay_dprintk(handle, "found ejectable bay");
460 if (!bay_add(handle, *count))
461 (*count)++;
463 return AE_OK;
466 static int __init bay_init(void)
468 int bays = 0;
470 INIT_LIST_HEAD(&drive_bays);
472 /* look for dockable drive bays */
473 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
474 ACPI_UINT32_MAX, find_bay, &bays, NULL);
476 if (bays)
477 if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
478 printk(KERN_ERR "Unable to register bay driver\n");
480 if (!bays)
481 return -ENODEV;
483 return 0;
486 static void __exit bay_exit(void)
488 struct bay *bay, *tmp;
490 list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
491 if (is_dock_device(bay->handle))
492 unregister_hotplug_dock_device(bay->handle);
493 acpi_bay_remove_fs(bay);
494 acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
495 bay_notify);
496 platform_device_unregister(bay->pdev);
497 kfree(bay->name);
498 kfree(bay);
501 acpi_bus_unregister_driver(&acpi_bay_driver);
504 postcore_initcall(bay_init);
505 module_exit(bay_exit);