V4L/DVB (7220): drivers/media/video/sn9c102/sn9c102_core.c Fix Unlikely(x) == y
[linux-2.6/mini2440.git] / drivers / acpi / bay.c
blob1fa86811b8ee6ee8e5e675666fc1092f0b4c6eb5
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 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);
49 static const struct acpi_device_id bay_device_ids[] = {
50 {"LNXIOBAY", 0},
51 {"", 0},
53 MODULE_DEVICE_TABLE(acpi, bay_device_ids);
55 struct bay {
56 acpi_handle handle;
57 char *name;
58 struct list_head list;
59 struct platform_device *pdev;
62 static LIST_HEAD(drive_bays);
65 /*****************************************************************************
66 * Drive Bay functions *
67 *****************************************************************************/
68 /**
69 * is_ejectable - see if a device is ejectable
70 * @handle: acpi handle of the device
72 * If an acpi object has a _EJ0 method, then it is ejectable
74 static int is_ejectable(acpi_handle handle)
76 acpi_status status;
77 acpi_handle tmp;
79 status = acpi_get_handle(handle, "_EJ0", &tmp);
80 if (ACPI_FAILURE(status))
81 return 0;
82 return 1;
85 /**
86 * bay_present - see if the bay device is present
87 * @bay: the drive bay
89 * execute the _STA method.
91 static int bay_present(struct bay *bay)
93 unsigned long sta;
94 acpi_status status;
96 if (bay) {
97 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
98 if (ACPI_SUCCESS(status) && sta)
99 return 1;
101 return 0;
105 * eject_device - respond to an eject request
106 * @handle - the device to eject
108 * Call this devices _EJ0 method.
110 static void eject_device(acpi_handle handle)
112 struct acpi_object_list arg_list;
113 union acpi_object arg;
115 bay_dprintk(handle, "Ejecting device");
117 arg_list.count = 1;
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 1;
122 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
123 &arg_list, NULL)))
124 pr_debug("Failed to evaluate _EJ0!\n");
128 * show_present - read method for "present" file in sysfs
130 static ssize_t show_present(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 struct bay *bay = dev_get_drvdata(dev);
134 return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
137 static DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
140 * write_eject - write method for "eject" file in sysfs
142 static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
143 const char *buf, size_t count)
145 struct bay *bay = dev_get_drvdata(dev);
147 if (!count)
148 return -EINVAL;
150 eject_device(bay->handle);
151 return count;
153 static DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
156 * is_ata - see if a device is an ata device
157 * @handle: acpi handle of the device
159 * If an acpi object has one of 4 ATA ACPI methods defined,
160 * then it is an ATA device
162 static int is_ata(acpi_handle handle)
164 acpi_handle tmp;
166 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
167 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
168 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
169 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
170 return 1;
172 return 0;
176 * parent_is_ata(acpi_handle handle)
179 static int parent_is_ata(acpi_handle handle)
181 acpi_handle phandle;
183 if (acpi_get_parent(handle, &phandle))
184 return 0;
186 return is_ata(phandle);
190 * is_ejectable_bay - see if a device is an ejectable drive bay
191 * @handle: acpi handle of the device
193 * If an acpi object is ejectable and has one of the ACPI ATA
194 * methods defined, then we can safely call it an ejectable
195 * drive bay
197 static int is_ejectable_bay(acpi_handle handle)
199 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
200 return 1;
201 return 0;
205 * eject_removable_drive - try to eject this drive
206 * @dev : the device structure of the drive
208 * If a device is a removable drive that requires an _EJ0 method
209 * to be executed in order to safely remove from the system, do
210 * it. ATM - always returns success
212 int eject_removable_drive(struct device *dev)
214 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
216 if (handle) {
217 bay_dprintk(handle, "Got device handle");
218 if (is_ejectable_bay(handle))
219 eject_device(handle);
220 } else {
221 printk("No acpi handle for device\n");
224 /* should I return an error code? */
225 return 0;
227 EXPORT_SYMBOL_GPL(eject_removable_drive);
229 static int acpi_bay_add_fs(struct bay *bay)
231 int ret;
232 struct device *dev = &bay->pdev->dev;
234 ret = device_create_file(dev, &dev_attr_present);
235 if (ret)
236 goto add_fs_err;
237 ret = device_create_file(dev, &dev_attr_eject);
238 if (ret) {
239 device_remove_file(dev, &dev_attr_present);
240 goto add_fs_err;
242 return 0;
244 add_fs_err:
245 bay_dprintk(bay->handle, "Error adding sysfs files\n");
246 return ret;
249 static void acpi_bay_remove_fs(struct bay *bay)
251 struct device *dev = &bay->pdev->dev;
253 /* cleanup sysfs */
254 device_remove_file(dev, &dev_attr_present);
255 device_remove_file(dev, &dev_attr_eject);
258 static int bay_is_dock_device(acpi_handle handle)
260 acpi_handle parent;
262 acpi_get_parent(handle, &parent);
264 /* if the device or it's parent is dependent on the
265 * dock, then we are a dock device
267 return (is_dock_device(handle) || is_dock_device(parent));
270 static int bay_add(acpi_handle handle, int id)
272 acpi_status status;
273 struct bay *new_bay;
274 struct platform_device *pdev;
275 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
276 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
278 bay_dprintk(handle, "Adding notify handler");
281 * Initialize bay device structure
283 new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
284 INIT_LIST_HEAD(&new_bay->list);
285 new_bay->handle = handle;
286 new_bay->name = (char *)nbuffer.pointer;
288 /* initialize platform device stuff */
289 pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
290 if (IS_ERR(pdev)) {
291 printk(KERN_ERR PREFIX "Error registering bay device\n");
292 goto bay_add_err;
294 new_bay->pdev = pdev;
295 platform_set_drvdata(pdev, new_bay);
298 * we want the bay driver to be able to send uevents
300 pdev->dev.uevent_suppress = 0;
302 if (acpi_bay_add_fs(new_bay)) {
303 platform_device_unregister(new_bay->pdev);
304 goto bay_add_err;
307 /* register for events on this device */
308 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
309 bay_notify, new_bay);
310 if (ACPI_FAILURE(status)) {
311 printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
314 /* if we are on a dock station, we should register for dock
315 * notifications.
317 if (bay_is_dock_device(handle)) {
318 bay_dprintk(handle, "Is dependent on dock\n");
319 register_hotplug_dock_device(handle, bay_notify, new_bay);
321 list_add(&new_bay->list, &drive_bays);
322 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
323 return 0;
325 bay_add_err:
326 kfree(new_bay->name);
327 kfree(new_bay);
328 return -ENODEV;
332 * bay_notify - act upon an acpi bay notification
333 * @handle: the bay handle
334 * @event: the acpi event
335 * @data: our driver data struct
338 static void bay_notify(acpi_handle handle, u32 event, void *data)
340 struct bay *bay_dev = (struct bay *)data;
341 struct device *dev = &bay_dev->pdev->dev;
342 char event_string[12];
343 char *envp[] = { event_string, NULL };
345 bay_dprintk(handle, "Bay event");
346 sprintf(event_string, "BAY_EVENT=%d", event);
347 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
350 static acpi_status
351 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
353 int *count = (int *)context;
356 * there could be more than one ejectable bay.
357 * so, just return AE_OK always so that every object
358 * will be checked.
360 if (is_ejectable_bay(handle)) {
361 bay_dprintk(handle, "found ejectable bay");
362 if (!bay_add(handle, *count))
363 (*count)++;
365 return AE_OK;
368 static int __init bay_init(void)
370 int bays = 0;
372 INIT_LIST_HEAD(&drive_bays);
374 /* look for dockable drive bays */
375 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
376 ACPI_UINT32_MAX, find_bay, &bays, NULL);
378 if (!bays)
379 return -ENODEV;
381 return 0;
384 static void __exit bay_exit(void)
386 struct bay *bay, *tmp;
388 list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
389 if (is_dock_device(bay->handle))
390 unregister_hotplug_dock_device(bay->handle);
391 acpi_bay_remove_fs(bay);
392 acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
393 bay_notify);
394 platform_device_unregister(bay->pdev);
395 kfree(bay->name);
396 kfree(bay);
400 postcore_initcall(bay_init);
401 module_exit(bay_exit);