blob: d2fc94161848ff91e424b08df40f5a0d9643aafe [file] [log] [blame]
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -07001/*
2 * bay.c - ACPI removable drive bay driver
3 *
4 * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
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.
12 *
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.
17 *
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.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
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>
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070031#include <linux/seq_file.h>
32#include <asm/uaccess.h>
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -050033#include <linux/platform_device.h>
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070034
Len Brownf52fd662007-02-12 22:42:12 -050035ACPI_MODULE_NAME("bay");
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070036MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050037MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver");
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070038MODULE_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); }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070047static void bay_notify(acpi_handle handle, u32 event, void *data);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070048
Thomas Renninger17196d62007-12-07 13:20:43 +010049static const struct acpi_device_id bay_device_ids[] = {
50 {"LNXIOBAY", 0},
51 {"", 0},
52};
53MODULE_DEVICE_TABLE(acpi, bay_device_ids);
54
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070055struct bay {
56 acpi_handle handle;
57 char *name;
58 struct list_head list;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -050059 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070060};
61
Adrian Bunk5d22e1e2006-12-04 14:49:39 -080062static LIST_HEAD(drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070063
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070064
65/*****************************************************************************
66 * Drive Bay functions *
67 *****************************************************************************/
68/**
69 * is_ejectable - see if a device is ejectable
70 * @handle: acpi handle of the device
71 *
72 * If an acpi object has a _EJ0 method, then it is ejectable
73 */
74static int is_ejectable(acpi_handle handle)
75{
76 acpi_status status;
77 acpi_handle tmp;
78
79 status = acpi_get_handle(handle, "_EJ0", &tmp);
80 if (ACPI_FAILURE(status))
81 return 0;
82 return 1;
83}
84
85/**
86 * bay_present - see if the bay device is present
87 * @bay: the drive bay
88 *
89 * execute the _STA method.
90 */
91static int bay_present(struct bay *bay)
92{
93 unsigned long sta;
94 acpi_status status;
95
96 if (bay) {
97 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
98 if (ACPI_SUCCESS(status) && sta)
99 return 1;
100 }
101 return 0;
102}
103
104/**
105 * eject_device - respond to an eject request
106 * @handle - the device to eject
107 *
108 * Call this devices _EJ0 method.
109 */
110static void eject_device(acpi_handle handle)
111{
112 struct acpi_object_list arg_list;
113 union acpi_object arg;
114
115 bay_dprintk(handle, "Ejecting device");
116
117 arg_list.count = 1;
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 1;
121
122 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
123 &arg_list, NULL)))
124 pr_debug("Failed to evaluate _EJ0!\n");
125}
126
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500127/*
128 * show_present - read method for "present" file in sysfs
129 */
130static ssize_t show_present(struct device *dev,
131 struct device_attribute *attr, char *buf)
132{
133 struct bay *bay = dev_get_drvdata(dev);
134 return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
135
136}
Adrian Bunke5685b92007-10-24 18:24:42 +0200137static DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500138
139/*
140 * write_eject - write method for "eject" file in sysfs
141 */
142static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
143 const char *buf, size_t count)
144{
145 struct bay *bay = dev_get_drvdata(dev);
146
147 if (!count)
148 return -EINVAL;
149
150 eject_device(bay->handle);
151 return count;
152}
Adrian Bunke5685b92007-10-24 18:24:42 +0200153static DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700154
155/**
156 * is_ata - see if a device is an ata device
157 * @handle: acpi handle of the device
158 *
159 * If an acpi object has one of 4 ATA ACPI methods defined,
160 * then it is an ATA device
161 */
162static int is_ata(acpi_handle handle)
163{
164 acpi_handle tmp;
165
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;
171
172 return 0;
173}
174
175/**
176 * parent_is_ata(acpi_handle handle)
177 *
178 */
179static int parent_is_ata(acpi_handle handle)
180{
181 acpi_handle phandle;
182
183 if (acpi_get_parent(handle, &phandle))
184 return 0;
185
186 return is_ata(phandle);
187}
188
189/**
190 * is_ejectable_bay - see if a device is an ejectable drive bay
191 * @handle: acpi handle of the device
192 *
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
196 */
197static int is_ejectable_bay(acpi_handle handle)
198{
199 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
200 return 1;
201 return 0;
202}
203
Adrian Bunk2a241d72008-03-31 02:05:40 +0300204#if 0
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700205/**
206 * eject_removable_drive - try to eject this drive
207 * @dev : the device structure of the drive
208 *
209 * If a device is a removable drive that requires an _EJ0 method
210 * to be executed in order to safely remove from the system, do
211 * it. ATM - always returns success
212 */
213int eject_removable_drive(struct device *dev)
214{
215 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
216
217 if (handle) {
218 bay_dprintk(handle, "Got device handle");
219 if (is_ejectable_bay(handle))
220 eject_device(handle);
221 } else {
222 printk("No acpi handle for device\n");
223 }
224
225 /* should I return an error code? */
226 return 0;
227}
228EXPORT_SYMBOL_GPL(eject_removable_drive);
Adrian Bunk2a241d72008-03-31 02:05:40 +0300229#endif /* 0 */
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700230
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700231static int acpi_bay_add_fs(struct bay *bay)
232{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500233 int ret;
234 struct device *dev = &bay->pdev->dev;
235
236 ret = device_create_file(dev, &dev_attr_present);
237 if (ret)
238 goto add_fs_err;
239 ret = device_create_file(dev, &dev_attr_eject);
240 if (ret) {
241 device_remove_file(dev, &dev_attr_present);
242 goto add_fs_err;
243 }
244 return 0;
245
246 add_fs_err:
247 bay_dprintk(bay->handle, "Error adding sysfs files\n");
248 return ret;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700249}
250
251static void acpi_bay_remove_fs(struct bay *bay)
252{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500253 struct device *dev = &bay->pdev->dev;
254
255 /* cleanup sysfs */
256 device_remove_file(dev, &dev_attr_present);
257 device_remove_file(dev, &dev_attr_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700258}
259
260static int bay_is_dock_device(acpi_handle handle)
261{
262 acpi_handle parent;
263
264 acpi_get_parent(handle, &parent);
265
266 /* if the device or it's parent is dependent on the
267 * dock, then we are a dock device
268 */
269 return (is_dock_device(handle) || is_dock_device(parent));
270}
271
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500272static int bay_add(acpi_handle handle, int id)
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700273{
274 acpi_status status;
275 struct bay *new_bay;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500276 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700277 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
278 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
279
280 bay_dprintk(handle, "Adding notify handler");
281
282 /*
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700283 * Initialize bay device structure
284 */
Al Viroa3c94e52007-02-09 16:05:07 +0000285 new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700286 INIT_LIST_HEAD(&new_bay->list);
287 new_bay->handle = handle;
288 new_bay->name = (char *)nbuffer.pointer;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500289
290 /* initialize platform device stuff */
291 pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
Henrique de Moraes Holschuh05397712007-02-14 10:55:00 -0200292 if (IS_ERR(pdev)) {
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500293 printk(KERN_ERR PREFIX "Error registering bay device\n");
294 goto bay_add_err;
295 }
296 new_bay->pdev = pdev;
297 platform_set_drvdata(pdev, new_bay);
298
Kristen Carlson Accardi1f9767d2007-05-09 15:55:53 -0700299 /*
300 * we want the bay driver to be able to send uevents
301 */
302 pdev->dev.uevent_suppress = 0;
303
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500304 if (acpi_bay_add_fs(new_bay)) {
305 platform_device_unregister(new_bay->pdev);
306 goto bay_add_err;
307 }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700308
309 /* register for events on this device */
310 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
311 bay_notify, new_bay);
312 if (ACPI_FAILURE(status)) {
313 printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
314 }
315
316 /* if we are on a dock station, we should register for dock
317 * notifications.
318 */
319 if (bay_is_dock_device(handle)) {
320 bay_dprintk(handle, "Is dependent on dock\n");
321 register_hotplug_dock_device(handle, bay_notify, new_bay);
322 }
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500323 list_add(&new_bay->list, &drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700324 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
325 return 0;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500326
327bay_add_err:
328 kfree(new_bay->name);
329 kfree(new_bay);
330 return -ENODEV;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700331}
332
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700333/**
334 * bay_notify - act upon an acpi bay notification
335 * @handle: the bay handle
336 * @event: the acpi event
337 * @data: our driver data struct
338 *
339 */
340static void bay_notify(acpi_handle handle, u32 event, void *data)
341{
Kristen Carlson Accardic41458a2007-02-05 16:09:07 -0800342 struct bay *bay_dev = (struct bay *)data;
343 struct device *dev = &bay_dev->pdev->dev;
Kristen Carlson Accardi3f8698d2007-05-23 14:12:29 -0700344 char event_string[12];
345 char *envp[] = { event_string, NULL };
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700346
347 bay_dprintk(handle, "Bay event");
Stephan Berberig7aa763c2007-08-10 13:10:31 -0700348 sprintf(event_string, "BAY_EVENT=%d", event);
Kristen Carlson Accardi3f8698d2007-05-23 14:12:29 -0700349 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700350}
351
352static acpi_status
353find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
354{
355 int *count = (int *)context;
356
357 /*
358 * there could be more than one ejectable bay.
359 * so, just return AE_OK always so that every object
360 * will be checked.
361 */
362 if (is_ejectable_bay(handle)) {
363 bay_dprintk(handle, "found ejectable bay");
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500364 if (!bay_add(handle, *count))
365 (*count)++;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700366 }
367 return AE_OK;
368}
369
370static int __init bay_init(void)
371{
372 int bays = 0;
373
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700374 INIT_LIST_HEAD(&drive_bays);
375
376 /* look for dockable drive bays */
377 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
378 ACPI_UINT32_MAX, find_bay, &bays, NULL);
379
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700380 if (!bays)
381 return -ENODEV;
382
383 return 0;
384}
385
386static void __exit bay_exit(void)
387{
388 struct bay *bay, *tmp;
389
390 list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
391 if (is_dock_device(bay->handle))
392 unregister_hotplug_dock_device(bay->handle);
393 acpi_bay_remove_fs(bay);
394 acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
395 bay_notify);
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500396 platform_device_unregister(bay->pdev);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700397 kfree(bay->name);
398 kfree(bay);
399 }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700400}
401
402postcore_initcall(bay_init);
403module_exit(bay_exit);
404