blob: 9fdee610ae05351c7da4ed1bc37bb96b956d6608 [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
35#define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver"
36
37ACPI_MODULE_NAME("bay")
38MODULE_AUTHOR("Kristen Carlson Accardi");
39MODULE_DESCRIPTION(ACPI_BAY_DRIVER_NAME);
40MODULE_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); }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070049static void bay_notify(acpi_handle handle, u32 event, void *data);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070050
51struct bay {
52 acpi_handle handle;
53 char *name;
54 struct list_head list;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -050055 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070056};
57
Adrian Bunk5d22e1e2006-12-04 14:49:39 -080058static LIST_HEAD(drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070059
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070060
61/*****************************************************************************
62 * Drive Bay functions *
63 *****************************************************************************/
64/**
65 * is_ejectable - see if a device is ejectable
66 * @handle: acpi handle of the device
67 *
68 * If an acpi object has a _EJ0 method, then it is ejectable
69 */
70static int is_ejectable(acpi_handle handle)
71{
72 acpi_status status;
73 acpi_handle tmp;
74
75 status = acpi_get_handle(handle, "_EJ0", &tmp);
76 if (ACPI_FAILURE(status))
77 return 0;
78 return 1;
79}
80
81/**
82 * bay_present - see if the bay device is present
83 * @bay: the drive bay
84 *
85 * execute the _STA method.
86 */
87static int bay_present(struct bay *bay)
88{
89 unsigned long sta;
90 acpi_status status;
91
92 if (bay) {
93 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
94 if (ACPI_SUCCESS(status) && sta)
95 return 1;
96 }
97 return 0;
98}
99
100/**
101 * eject_device - respond to an eject request
102 * @handle - the device to eject
103 *
104 * Call this devices _EJ0 method.
105 */
106static void eject_device(acpi_handle handle)
107{
108 struct acpi_object_list arg_list;
109 union acpi_object arg;
110
111 bay_dprintk(handle, "Ejecting device");
112
113 arg_list.count = 1;
114 arg_list.pointer = &arg;
115 arg.type = ACPI_TYPE_INTEGER;
116 arg.integer.value = 1;
117
118 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
119 &arg_list, NULL)))
120 pr_debug("Failed to evaluate _EJ0!\n");
121}
122
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500123/*
124 * show_present - read method for "present" file in sysfs
125 */
126static ssize_t show_present(struct device *dev,
127 struct device_attribute *attr, char *buf)
128{
129 struct bay *bay = dev_get_drvdata(dev);
130 return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
131
132}
133DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
134
135/*
136 * write_eject - write method for "eject" file in sysfs
137 */
138static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
139 const char *buf, size_t count)
140{
141 struct bay *bay = dev_get_drvdata(dev);
142
143 if (!count)
144 return -EINVAL;
145
146 eject_device(bay->handle);
147 return count;
148}
149DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700150
151/**
152 * is_ata - see if a device is an ata device
153 * @handle: acpi handle of the device
154 *
155 * If an acpi object has one of 4 ATA ACPI methods defined,
156 * then it is an ATA device
157 */
158static int is_ata(acpi_handle handle)
159{
160 acpi_handle tmp;
161
162 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
163 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
164 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
165 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
166 return 1;
167
168 return 0;
169}
170
171/**
172 * parent_is_ata(acpi_handle handle)
173 *
174 */
175static int parent_is_ata(acpi_handle handle)
176{
177 acpi_handle phandle;
178
179 if (acpi_get_parent(handle, &phandle))
180 return 0;
181
182 return is_ata(phandle);
183}
184
185/**
186 * is_ejectable_bay - see if a device is an ejectable drive bay
187 * @handle: acpi handle of the device
188 *
189 * If an acpi object is ejectable and has one of the ACPI ATA
190 * methods defined, then we can safely call it an ejectable
191 * drive bay
192 */
193static int is_ejectable_bay(acpi_handle handle)
194{
195 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
196 return 1;
197 return 0;
198}
199
200/**
201 * eject_removable_drive - try to eject this drive
202 * @dev : the device structure of the drive
203 *
204 * If a device is a removable drive that requires an _EJ0 method
205 * to be executed in order to safely remove from the system, do
206 * it. ATM - always returns success
207 */
208int eject_removable_drive(struct device *dev)
209{
210 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
211
212 if (handle) {
213 bay_dprintk(handle, "Got device handle");
214 if (is_ejectable_bay(handle))
215 eject_device(handle);
216 } else {
217 printk("No acpi handle for device\n");
218 }
219
220 /* should I return an error code? */
221 return 0;
222}
223EXPORT_SYMBOL_GPL(eject_removable_drive);
224
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700225static int acpi_bay_add_fs(struct bay *bay)
226{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500227 int ret;
228 struct device *dev = &bay->pdev->dev;
229
230 ret = device_create_file(dev, &dev_attr_present);
231 if (ret)
232 goto add_fs_err;
233 ret = device_create_file(dev, &dev_attr_eject);
234 if (ret) {
235 device_remove_file(dev, &dev_attr_present);
236 goto add_fs_err;
237 }
238 return 0;
239
240 add_fs_err:
241 bay_dprintk(bay->handle, "Error adding sysfs files\n");
242 return ret;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700243}
244
245static void acpi_bay_remove_fs(struct bay *bay)
246{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500247 struct device *dev = &bay->pdev->dev;
248
249 /* cleanup sysfs */
250 device_remove_file(dev, &dev_attr_present);
251 device_remove_file(dev, &dev_attr_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700252}
253
254static int bay_is_dock_device(acpi_handle handle)
255{
256 acpi_handle parent;
257
258 acpi_get_parent(handle, &parent);
259
260 /* if the device or it's parent is dependent on the
261 * dock, then we are a dock device
262 */
263 return (is_dock_device(handle) || is_dock_device(parent));
264}
265
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500266static int bay_add(acpi_handle handle, int id)
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700267{
268 acpi_status status;
269 struct bay *new_bay;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500270 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700271 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
272 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
273
274 bay_dprintk(handle, "Adding notify handler");
275
276 /*
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700277 * Initialize bay device structure
278 */
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500279 new_bay = kzalloc(GFP_ATOMIC, sizeof(*new_bay));
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700280 INIT_LIST_HEAD(&new_bay->list);
281 new_bay->handle = handle;
282 new_bay->name = (char *)nbuffer.pointer;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500283
284 /* initialize platform device stuff */
285 pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
286 if (pdev == NULL) {
287 printk(KERN_ERR PREFIX "Error registering bay device\n");
288 goto bay_add_err;
289 }
290 new_bay->pdev = pdev;
291 platform_set_drvdata(pdev, new_bay);
292
293 if (acpi_bay_add_fs(new_bay)) {
294 platform_device_unregister(new_bay->pdev);
295 goto bay_add_err;
296 }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700297
298 /* register for events on this device */
299 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
300 bay_notify, new_bay);
301 if (ACPI_FAILURE(status)) {
302 printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
303 }
304
305 /* if we are on a dock station, we should register for dock
306 * notifications.
307 */
308 if (bay_is_dock_device(handle)) {
309 bay_dprintk(handle, "Is dependent on dock\n");
310 register_hotplug_dock_device(handle, bay_notify, new_bay);
311 }
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500312 list_add(&new_bay->list, &drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700313 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
314 return 0;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500315
316bay_add_err:
317 kfree(new_bay->name);
318 kfree(new_bay);
319 return -ENODEV;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700320}
321
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700322/**
323 * bay_notify - act upon an acpi bay notification
324 * @handle: the bay handle
325 * @event: the acpi event
326 * @data: our driver data struct
327 *
328 */
329static void bay_notify(acpi_handle handle, u32 event, void *data)
330{
Kristen Carlson Accardic41458a2007-02-05 16:09:07 -0800331 struct bay *bay_dev = (struct bay *)data;
332 struct device *dev = &bay_dev->pdev->dev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700333
334 bay_dprintk(handle, "Bay event");
335
336 switch(event) {
337 case ACPI_NOTIFY_BUS_CHECK:
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700338 case ACPI_NOTIFY_DEVICE_CHECK:
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700339 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardic41458a2007-02-05 16:09:07 -0800340 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700341 break;
342 default:
Kristen Carlson Accardic41458a2007-02-05 16:09:07 -0800343 printk(KERN_ERR PREFIX "Bay: unknown event %d\n", event);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700344 }
345}
346
347static acpi_status
348find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
349{
350 int *count = (int *)context;
351
352 /*
353 * there could be more than one ejectable bay.
354 * so, just return AE_OK always so that every object
355 * will be checked.
356 */
357 if (is_ejectable_bay(handle)) {
358 bay_dprintk(handle, "found ejectable bay");
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500359 if (!bay_add(handle, *count))
360 (*count)++;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700361 }
362 return AE_OK;
363}
364
365static int __init bay_init(void)
366{
367 int bays = 0;
368
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700369 INIT_LIST_HEAD(&drive_bays);
370
371 /* look for dockable drive bays */
372 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
373 ACPI_UINT32_MAX, find_bay, &bays, NULL);
374
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700375 if (!bays)
376 return -ENODEV;
377
378 return 0;
379}
380
381static void __exit bay_exit(void)
382{
383 struct bay *bay, *tmp;
384
385 list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
386 if (is_dock_device(bay->handle))
387 unregister_hotplug_dock_device(bay->handle);
388 acpi_bay_remove_fs(bay);
389 acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
390 bay_notify);
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500391 platform_device_unregister(bay->pdev);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700392 kfree(bay->name);
393 kfree(bay);
394 }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700395}
396
397postcore_initcall(bay_init);
398module_exit(bay_exit);
399