blob: 36dcb899b23ce37924af881460cf4b1ae8b3805f [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);
48static int acpi_bay_add(struct acpi_device *device);
49static int acpi_bay_remove(struct acpi_device *device, int type);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070050
51static struct acpi_driver acpi_bay_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050052 .name = "bay",
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070053 .class = ACPI_BAY_CLASS,
Zhang Rui54735262007-01-11 02:09:09 -050054 .ids = ACPI_BAY_HID,
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070055 .ops = {
56 .add = acpi_bay_add,
57 .remove = acpi_bay_remove,
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070058 },
59};
60
61struct bay {
62 acpi_handle handle;
63 char *name;
64 struct list_head list;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -050065 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070066};
67
Adrian Bunk5d22e1e2006-12-04 14:49:39 -080068static LIST_HEAD(drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070069
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -070070
71/*****************************************************************************
72 * Drive Bay functions *
73 *****************************************************************************/
74/**
75 * is_ejectable - see if a device is ejectable
76 * @handle: acpi handle of the device
77 *
78 * If an acpi object has a _EJ0 method, then it is ejectable
79 */
80static int is_ejectable(acpi_handle handle)
81{
82 acpi_status status;
83 acpi_handle tmp;
84
85 status = acpi_get_handle(handle, "_EJ0", &tmp);
86 if (ACPI_FAILURE(status))
87 return 0;
88 return 1;
89}
90
91/**
92 * bay_present - see if the bay device is present
93 * @bay: the drive bay
94 *
95 * execute the _STA method.
96 */
97static int bay_present(struct bay *bay)
98{
99 unsigned long sta;
100 acpi_status status;
101
102 if (bay) {
103 status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
104 if (ACPI_SUCCESS(status) && sta)
105 return 1;
106 }
107 return 0;
108}
109
110/**
111 * eject_device - respond to an eject request
112 * @handle - the device to eject
113 *
114 * Call this devices _EJ0 method.
115 */
116static void eject_device(acpi_handle handle)
117{
118 struct acpi_object_list arg_list;
119 union acpi_object arg;
120
121 bay_dprintk(handle, "Ejecting device");
122
123 arg_list.count = 1;
124 arg_list.pointer = &arg;
125 arg.type = ACPI_TYPE_INTEGER;
126 arg.integer.value = 1;
127
128 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
129 &arg_list, NULL)))
130 pr_debug("Failed to evaluate _EJ0!\n");
131}
132
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500133/*
134 * show_present - read method for "present" file in sysfs
135 */
136static ssize_t show_present(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 struct bay *bay = dev_get_drvdata(dev);
140 return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));
141
142}
143DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
144
145/*
146 * write_eject - write method for "eject" file in sysfs
147 */
148static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
149 const char *buf, size_t count)
150{
151 struct bay *bay = dev_get_drvdata(dev);
152
153 if (!count)
154 return -EINVAL;
155
156 eject_device(bay->handle);
157 return count;
158}
159DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700160
161/**
162 * is_ata - see if a device is an ata device
163 * @handle: acpi handle of the device
164 *
165 * If an acpi object has one of 4 ATA ACPI methods defined,
166 * then it is an ATA device
167 */
168static int is_ata(acpi_handle handle)
169{
170 acpi_handle tmp;
171
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))))
176 return 1;
177
178 return 0;
179}
180
181/**
182 * parent_is_ata(acpi_handle handle)
183 *
184 */
185static int parent_is_ata(acpi_handle handle)
186{
187 acpi_handle phandle;
188
189 if (acpi_get_parent(handle, &phandle))
190 return 0;
191
192 return is_ata(phandle);
193}
194
195/**
196 * is_ejectable_bay - see if a device is an ejectable drive bay
197 * @handle: acpi handle of the device
198 *
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
201 * drive bay
202 */
203static int is_ejectable_bay(acpi_handle handle)
204{
205 if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
206 return 1;
207 return 0;
208}
209
210/**
211 * eject_removable_drive - try to eject this drive
212 * @dev : the device structure of the drive
213 *
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
217 */
218int eject_removable_drive(struct device *dev)
219{
220 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
221
222 if (handle) {
223 bay_dprintk(handle, "Got device handle");
224 if (is_ejectable_bay(handle))
225 eject_device(handle);
226 } else {
227 printk("No acpi handle for device\n");
228 }
229
230 /* should I return an error code? */
231 return 0;
232}
233EXPORT_SYMBOL_GPL(eject_removable_drive);
234
235static int acpi_bay_add(struct acpi_device *device)
236{
237 bay_dprintk(device->handle, "adding bay device");
238 strcpy(acpi_device_name(device), "Dockable Bay");
239 strcpy(acpi_device_class(device), "bay");
240 return 0;
241}
242
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700243static int acpi_bay_add_fs(struct bay *bay)
244{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500245 int ret;
246 struct device *dev = &bay->pdev->dev;
247
248 ret = device_create_file(dev, &dev_attr_present);
249 if (ret)
250 goto add_fs_err;
251 ret = device_create_file(dev, &dev_attr_eject);
252 if (ret) {
253 device_remove_file(dev, &dev_attr_present);
254 goto add_fs_err;
255 }
256 return 0;
257
258 add_fs_err:
259 bay_dprintk(bay->handle, "Error adding sysfs files\n");
260 return ret;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700261}
262
263static void acpi_bay_remove_fs(struct bay *bay)
264{
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500265 struct device *dev = &bay->pdev->dev;
266
267 /* cleanup sysfs */
268 device_remove_file(dev, &dev_attr_present);
269 device_remove_file(dev, &dev_attr_eject);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700270}
271
272static int bay_is_dock_device(acpi_handle handle)
273{
274 acpi_handle parent;
275
276 acpi_get_parent(handle, &parent);
277
278 /* if the device or it's parent is dependent on the
279 * dock, then we are a dock device
280 */
281 return (is_dock_device(handle) || is_dock_device(parent));
282}
283
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500284static int bay_add(acpi_handle handle, int id)
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700285{
286 acpi_status status;
287 struct bay *new_bay;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500288 struct platform_device *pdev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700289 struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
290 acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);
291
292 bay_dprintk(handle, "Adding notify handler");
293
294 /*
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700295 * Initialize bay device structure
296 */
Al Viro35e00fb2007-02-09 16:05:07 +0000297 new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700298 INIT_LIST_HEAD(&new_bay->list);
299 new_bay->handle = handle;
300 new_bay->name = (char *)nbuffer.pointer;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500301
302 /* initialize platform device stuff */
303 pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
304 if (pdev == NULL) {
305 printk(KERN_ERR PREFIX "Error registering bay device\n");
306 goto bay_add_err;
307 }
308 new_bay->pdev = pdev;
309 platform_set_drvdata(pdev, new_bay);
310
311 if (acpi_bay_add_fs(new_bay)) {
312 platform_device_unregister(new_bay->pdev);
313 goto bay_add_err;
314 }
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700315
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");
321 }
322
323 /* if we are on a dock station, we should register for dock
324 * notifications.
325 */
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);
329 }
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500330 list_add(&new_bay->list, &drive_bays);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700331 printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
332 return 0;
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500333
334bay_add_err:
335 kfree(new_bay->name);
336 kfree(new_bay);
337 return -ENODEV;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700338}
339
340static int acpi_bay_remove(struct acpi_device *device, int type)
341{
342 /*** FIXME: do something here */
343 return 0;
344}
345
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700346/**
347 * bay_create_acpi_device - add new devices to acpi
348 * @handle - handle of the device to add
349 *
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.
354 *
355 * Returns a pointer to the acpi_device corresponding to the handle.
356 */
357static struct acpi_device * bay_create_acpi_device(acpi_handle handle)
358{
359 struct acpi_device *device = NULL;
360 struct acpi_device *parent_device;
361 acpi_handle parent;
362 int ret;
363
364 bay_dprintk(handle, "Trying to get device");
365 if (acpi_bus_get_device(handle, &device)) {
366 /*
367 * no device created for this object,
368 * so we should create one.
369 */
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;
374
375 ret = acpi_bus_add(&device, parent_device, handle,
376 ACPI_BUS_TYPE_DEVICE);
377 if (ret) {
378 pr_debug("error adding bus, %x\n",
379 -ret);
380 return NULL;
381 }
382 }
383 return device;
384}
385
386/**
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
391 *
392 */
393static void bay_notify(acpi_handle handle, u32 event, void *data)
394{
395 struct acpi_device *dev;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700396
397 bay_dprintk(handle, "Bay event");
398
399 switch(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);
405 if (dev)
406 acpi_bus_generate_event(dev, event, 0);
407 else
408 printk("No device for generating event\n");
409 /* wouldn't it be a good idea to just rescan SATA
410 * right here?
411 */
412 break;
413 case ACPI_NOTIFY_EJECT_REQUEST:
414 printk("Eject request\n");
415 dev = bay_create_acpi_device(handle);
416 if (dev)
417 acpi_bus_generate_event(dev, event, 0);
418 else
419 printk("No device for generating eventn");
420
421 /* wouldn't it be a good idea to just call the
422 * eject_device here if we were a SATA device?
423 */
424 break;
425 default:
426 printk("unknown event %d\n", event);
427 }
428}
429
430static acpi_status
431find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
432{
433 int *count = (int *)context;
434
435 /*
436 * there could be more than one ejectable bay.
437 * so, just return AE_OK always so that every object
438 * will be checked.
439 */
440 if (is_ejectable_bay(handle)) {
441 bay_dprintk(handle, "found ejectable bay");
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500442 if (!bay_add(handle, *count))
443 (*count)++;
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700444 }
445 return AE_OK;
446}
447
448static int __init bay_init(void)
449{
450 int bays = 0;
451
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700452 INIT_LIST_HEAD(&drive_bays);
453
454 /* look for dockable drive bays */
455 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
456 ACPI_UINT32_MAX, find_bay, &bays, NULL);
457
Kristen Carlson Accardie9dd85e2006-12-18 18:06:00 -0500458 if (bays)
459 if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700460 printk(KERN_ERR "Unable to register bay driver\n");
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700461
462 if (!bays)
463 return -ENODEV;
464
465 return 0;
466}
467
468static void __exit bay_exit(void)
469{
470 struct bay *bay, *tmp;
471
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,
477 bay_notify);
Kristen Carlson Accardi2b167c02006-12-18 18:07:00 -0500478 platform_device_unregister(bay->pdev);
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700479 kfree(bay->name);
480 kfree(bay);
481 }
482
Kristen Carlson Accardi01b57e72006-10-20 14:30:25 -0700483 acpi_bus_unregister_driver(&acpi_bay_driver);
484}
485
486postcore_initcall(bay_init);
487module_exit(bay_exit);
488