]>
Commit | Line | Data |
---|---|---|
01b57e73 KCA |
1 | /* |
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> | |
01b57e73 KCA |
31 | #include <linux/seq_file.h> |
32 | #include <asm/uaccess.h> | |
2b167c01 | 33 | #include <linux/platform_device.h> |
01b57e73 KCA |
34 | |
35 | #define ACPI_BAY_DRIVER_NAME "ACPI Removable Drive Bay Driver" | |
36 | ||
f52fd66d | 37 | ACPI_MODULE_NAME("bay"); |
01b57e73 KCA |
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); } | |
01b57e73 KCA |
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); | |
01b57e73 KCA |
52 | |
53 | static struct acpi_driver acpi_bay_driver = { | |
c2b6705b | 54 | .name = "bay", |
01b57e73 | 55 | .class = ACPI_BAY_CLASS, |
54735266 | 56 | .ids = ACPI_BAY_HID, |
01b57e73 KCA |
57 | .ops = { |
58 | .add = acpi_bay_add, | |
59 | .remove = acpi_bay_remove, | |
01b57e73 KCA |
60 | }, |
61 | }; | |
62 | ||
63 | struct bay { | |
64 | acpi_handle handle; | |
65 | char *name; | |
66 | struct list_head list; | |
2b167c01 | 67 | struct platform_device *pdev; |
01b57e73 KCA |
68 | }; |
69 | ||
5d22e1e8 | 70 | static LIST_HEAD(drive_bays); |
01b57e73 | 71 | |
01b57e73 KCA |
72 | |
73 | /***************************************************************************** | |
74 | * Drive Bay functions * | |
75 | *****************************************************************************/ | |
76 | /** | |
77 | * is_ejectable - see if a device is ejectable | |
78 | * @handle: acpi handle of the device | |
79 | * | |
80 | * If an acpi object has a _EJ0 method, then it is ejectable | |
81 | */ | |
82 | static int is_ejectable(acpi_handle handle) | |
83 | { | |
84 | acpi_status status; | |
85 | acpi_handle tmp; | |
86 | ||
87 | status = acpi_get_handle(handle, "_EJ0", &tmp); | |
88 | if (ACPI_FAILURE(status)) | |
89 | return 0; | |
90 | return 1; | |
91 | } | |
92 | ||
93 | /** | |
94 | * bay_present - see if the bay device is present | |
95 | * @bay: the drive bay | |
96 | * | |
97 | * execute the _STA method. | |
98 | */ | |
99 | static int bay_present(struct bay *bay) | |
100 | { | |
101 | unsigned long sta; | |
102 | acpi_status status; | |
103 | ||
104 | if (bay) { | |
105 | status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta); | |
106 | if (ACPI_SUCCESS(status) && sta) | |
107 | return 1; | |
108 | } | |
109 | return 0; | |
110 | } | |
111 | ||
112 | /** | |
113 | * eject_device - respond to an eject request | |
114 | * @handle - the device to eject | |
115 | * | |
116 | * Call this devices _EJ0 method. | |
117 | */ | |
118 | static void eject_device(acpi_handle handle) | |
119 | { | |
120 | struct acpi_object_list arg_list; | |
121 | union acpi_object arg; | |
122 | ||
123 | bay_dprintk(handle, "Ejecting device"); | |
124 | ||
125 | arg_list.count = 1; | |
126 | arg_list.pointer = &arg; | |
127 | arg.type = ACPI_TYPE_INTEGER; | |
128 | arg.integer.value = 1; | |
129 | ||
130 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0", | |
131 | &arg_list, NULL))) | |
132 | pr_debug("Failed to evaluate _EJ0!\n"); | |
133 | } | |
134 | ||
2b167c01 KCA |
135 | /* |
136 | * show_present - read method for "present" file in sysfs | |
137 | */ | |
138 | static ssize_t show_present(struct device *dev, | |
139 | struct device_attribute *attr, char *buf) | |
140 | { | |
141 | struct bay *bay = dev_get_drvdata(dev); | |
142 | return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay)); | |
143 | ||
144 | } | |
145 | DEVICE_ATTR(present, S_IRUGO, show_present, NULL); | |
146 | ||
147 | /* | |
148 | * write_eject - write method for "eject" file in sysfs | |
149 | */ | |
150 | static ssize_t write_eject(struct device *dev, struct device_attribute *attr, | |
151 | const char *buf, size_t count) | |
152 | { | |
153 | struct bay *bay = dev_get_drvdata(dev); | |
154 | ||
155 | if (!count) | |
156 | return -EINVAL; | |
157 | ||
158 | eject_device(bay->handle); | |
159 | return count; | |
160 | } | |
161 | DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject); | |
01b57e73 KCA |
162 | |
163 | /** | |
164 | * is_ata - see if a device is an ata device | |
165 | * @handle: acpi handle of the device | |
166 | * | |
167 | * If an acpi object has one of 4 ATA ACPI methods defined, | |
168 | * then it is an ATA device | |
169 | */ | |
170 | static int is_ata(acpi_handle handle) | |
171 | { | |
172 | acpi_handle tmp; | |
173 | ||
174 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || | |
175 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || | |
176 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || | |
177 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) | |
178 | return 1; | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | /** | |
184 | * parent_is_ata(acpi_handle handle) | |
185 | * | |
186 | */ | |
187 | static int parent_is_ata(acpi_handle handle) | |
188 | { | |
189 | acpi_handle phandle; | |
190 | ||
191 | if (acpi_get_parent(handle, &phandle)) | |
192 | return 0; | |
193 | ||
194 | return is_ata(phandle); | |
195 | } | |
196 | ||
197 | /** | |
198 | * is_ejectable_bay - see if a device is an ejectable drive bay | |
199 | * @handle: acpi handle of the device | |
200 | * | |
201 | * If an acpi object is ejectable and has one of the ACPI ATA | |
202 | * methods defined, then we can safely call it an ejectable | |
203 | * drive bay | |
204 | */ | |
205 | static int is_ejectable_bay(acpi_handle handle) | |
206 | { | |
207 | if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle)) | |
208 | return 1; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | /** | |
213 | * eject_removable_drive - try to eject this drive | |
214 | * @dev : the device structure of the drive | |
215 | * | |
216 | * If a device is a removable drive that requires an _EJ0 method | |
217 | * to be executed in order to safely remove from the system, do | |
218 | * it. ATM - always returns success | |
219 | */ | |
220 | int eject_removable_drive(struct device *dev) | |
221 | { | |
222 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); | |
223 | ||
224 | if (handle) { | |
225 | bay_dprintk(handle, "Got device handle"); | |
226 | if (is_ejectable_bay(handle)) | |
227 | eject_device(handle); | |
228 | } else { | |
229 | printk("No acpi handle for device\n"); | |
230 | } | |
231 | ||
232 | /* should I return an error code? */ | |
233 | return 0; | |
234 | } | |
235 | EXPORT_SYMBOL_GPL(eject_removable_drive); | |
236 | ||
237 | static int acpi_bay_add(struct acpi_device *device) | |
238 | { | |
239 | bay_dprintk(device->handle, "adding bay device"); | |
240 | strcpy(acpi_device_name(device), "Dockable Bay"); | |
241 | strcpy(acpi_device_class(device), "bay"); | |
242 | return 0; | |
243 | } | |
244 | ||
01b57e73 KCA |
245 | static int acpi_bay_add_fs(struct bay *bay) |
246 | { | |
2b167c01 KCA |
247 | int ret; |
248 | struct device *dev = &bay->pdev->dev; | |
249 | ||
250 | ret = device_create_file(dev, &dev_attr_present); | |
251 | if (ret) | |
252 | goto add_fs_err; | |
253 | ret = device_create_file(dev, &dev_attr_eject); | |
254 | if (ret) { | |
255 | device_remove_file(dev, &dev_attr_present); | |
256 | goto add_fs_err; | |
257 | } | |
258 | return 0; | |
259 | ||
260 | add_fs_err: | |
261 | bay_dprintk(bay->handle, "Error adding sysfs files\n"); | |
262 | return ret; | |
01b57e73 KCA |
263 | } |
264 | ||
265 | static void acpi_bay_remove_fs(struct bay *bay) | |
266 | { | |
2b167c01 KCA |
267 | struct device *dev = &bay->pdev->dev; |
268 | ||
269 | /* cleanup sysfs */ | |
270 | device_remove_file(dev, &dev_attr_present); | |
271 | device_remove_file(dev, &dev_attr_eject); | |
01b57e73 KCA |
272 | } |
273 | ||
274 | static int bay_is_dock_device(acpi_handle handle) | |
275 | { | |
276 | acpi_handle parent; | |
277 | ||
278 | acpi_get_parent(handle, &parent); | |
279 | ||
280 | /* if the device or it's parent is dependent on the | |
281 | * dock, then we are a dock device | |
282 | */ | |
283 | return (is_dock_device(handle) || is_dock_device(parent)); | |
284 | } | |
285 | ||
2b167c01 | 286 | static int bay_add(acpi_handle handle, int id) |
01b57e73 KCA |
287 | { |
288 | acpi_status status; | |
289 | struct bay *new_bay; | |
2b167c01 | 290 | struct platform_device *pdev; |
01b57e73 KCA |
291 | struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
292 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer); | |
293 | ||
294 | bay_dprintk(handle, "Adding notify handler"); | |
295 | ||
01b57e73 KCA |
296 | /* |
297 | * Initialize bay device structure | |
298 | */ | |
35e00fbe | 299 | new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC); |
01b57e73 KCA |
300 | INIT_LIST_HEAD(&new_bay->list); |
301 | new_bay->handle = handle; | |
302 | new_bay->name = (char *)nbuffer.pointer; | |
2b167c01 KCA |
303 | |
304 | /* initialize platform device stuff */ | |
305 | pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0); | |
306 | if (pdev == NULL) { | |
307 | printk(KERN_ERR PREFIX "Error registering bay device\n"); | |
308 | goto bay_add_err; | |
309 | } | |
310 | new_bay->pdev = pdev; | |
311 | platform_set_drvdata(pdev, new_bay); | |
312 | ||
313 | if (acpi_bay_add_fs(new_bay)) { | |
314 | platform_device_unregister(new_bay->pdev); | |
315 | goto bay_add_err; | |
316 | } | |
01b57e73 KCA |
317 | |
318 | /* register for events on this device */ | |
319 | status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, | |
320 | bay_notify, new_bay); | |
321 | if (ACPI_FAILURE(status)) { | |
322 | printk(KERN_ERR PREFIX "Error installing bay notify handler\n"); | |
323 | } | |
324 | ||
325 | /* if we are on a dock station, we should register for dock | |
326 | * notifications. | |
327 | */ | |
328 | if (bay_is_dock_device(handle)) { | |
329 | bay_dprintk(handle, "Is dependent on dock\n"); | |
330 | register_hotplug_dock_device(handle, bay_notify, new_bay); | |
331 | } | |
2b167c01 | 332 | list_add(&new_bay->list, &drive_bays); |
01b57e73 KCA |
333 | printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name); |
334 | return 0; | |
2b167c01 KCA |
335 | |
336 | bay_add_err: | |
337 | kfree(new_bay->name); | |
338 | kfree(new_bay); | |
339 | return -ENODEV; | |
01b57e73 KCA |
340 | } |
341 | ||
342 | static int acpi_bay_remove(struct acpi_device *device, int type) | |
343 | { | |
344 | /*** FIXME: do something here */ | |
345 | return 0; | |
346 | } | |
347 | ||
01b57e73 KCA |
348 | /** |
349 | * bay_create_acpi_device - add new devices to acpi | |
350 | * @handle - handle of the device to add | |
351 | * | |
352 | * This function will create a new acpi_device for the given | |
353 | * handle if one does not exist already. This should cause | |
354 | * acpi to scan for drivers for the given devices, and call | |
355 | * matching driver's add routine. | |
356 | * | |
357 | * Returns a pointer to the acpi_device corresponding to the handle. | |
358 | */ | |
359 | static struct acpi_device * bay_create_acpi_device(acpi_handle handle) | |
360 | { | |
361 | struct acpi_device *device = NULL; | |
362 | struct acpi_device *parent_device; | |
363 | acpi_handle parent; | |
364 | int ret; | |
365 | ||
366 | bay_dprintk(handle, "Trying to get device"); | |
367 | if (acpi_bus_get_device(handle, &device)) { | |
368 | /* | |
369 | * no device created for this object, | |
370 | * so we should create one. | |
371 | */ | |
372 | bay_dprintk(handle, "No device for handle"); | |
373 | acpi_get_parent(handle, &parent); | |
374 | if (acpi_bus_get_device(parent, &parent_device)) | |
375 | parent_device = NULL; | |
376 | ||
377 | ret = acpi_bus_add(&device, parent_device, handle, | |
378 | ACPI_BUS_TYPE_DEVICE); | |
379 | if (ret) { | |
380 | pr_debug("error adding bus, %x\n", | |
381 | -ret); | |
382 | return NULL; | |
383 | } | |
384 | } | |
385 | return device; | |
386 | } | |
387 | ||
388 | /** | |
389 | * bay_notify - act upon an acpi bay notification | |
390 | * @handle: the bay handle | |
391 | * @event: the acpi event | |
392 | * @data: our driver data struct | |
393 | * | |
394 | */ | |
395 | static void bay_notify(acpi_handle handle, u32 event, void *data) | |
396 | { | |
397 | struct acpi_device *dev; | |
01b57e73 KCA |
398 | |
399 | bay_dprintk(handle, "Bay event"); | |
400 | ||
401 | switch(event) { | |
402 | case ACPI_NOTIFY_BUS_CHECK: | |
403 | printk("Bus Check\n"); | |
404 | case ACPI_NOTIFY_DEVICE_CHECK: | |
405 | printk("Device Check\n"); | |
406 | dev = bay_create_acpi_device(handle); | |
407 | if (dev) | |
408 | acpi_bus_generate_event(dev, event, 0); | |
409 | else | |
410 | printk("No device for generating event\n"); | |
411 | /* wouldn't it be a good idea to just rescan SATA | |
412 | * right here? | |
413 | */ | |
414 | break; | |
415 | case ACPI_NOTIFY_EJECT_REQUEST: | |
416 | printk("Eject request\n"); | |
417 | dev = bay_create_acpi_device(handle); | |
418 | if (dev) | |
419 | acpi_bus_generate_event(dev, event, 0); | |
420 | else | |
421 | printk("No device for generating eventn"); | |
422 | ||
423 | /* wouldn't it be a good idea to just call the | |
424 | * eject_device here if we were a SATA device? | |
425 | */ | |
426 | break; | |
427 | default: | |
428 | printk("unknown event %d\n", event); | |
429 | } | |
430 | } | |
431 | ||
432 | static acpi_status | |
433 | find_bay(acpi_handle handle, u32 lvl, void *context, void **rv) | |
434 | { | |
435 | int *count = (int *)context; | |
436 | ||
437 | /* | |
438 | * there could be more than one ejectable bay. | |
439 | * so, just return AE_OK always so that every object | |
440 | * will be checked. | |
441 | */ | |
442 | if (is_ejectable_bay(handle)) { | |
443 | bay_dprintk(handle, "found ejectable bay"); | |
2b167c01 KCA |
444 | if (!bay_add(handle, *count)) |
445 | (*count)++; | |
01b57e73 KCA |
446 | } |
447 | return AE_OK; | |
448 | } | |
449 | ||
450 | static int __init bay_init(void) | |
451 | { | |
452 | int bays = 0; | |
453 | ||
01b57e73 KCA |
454 | INIT_LIST_HEAD(&drive_bays); |
455 | ||
456 | /* look for dockable drive bays */ | |
457 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
458 | ACPI_UINT32_MAX, find_bay, &bays, NULL); | |
459 | ||
e9dd85e5 KCA |
460 | if (bays) |
461 | if ((acpi_bus_register_driver(&acpi_bay_driver) < 0)) | |
01b57e73 | 462 | printk(KERN_ERR "Unable to register bay driver\n"); |
01b57e73 KCA |
463 | |
464 | if (!bays) | |
465 | return -ENODEV; | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | static void __exit bay_exit(void) | |
471 | { | |
472 | struct bay *bay, *tmp; | |
473 | ||
474 | list_for_each_entry_safe(bay, tmp, &drive_bays, list) { | |
475 | if (is_dock_device(bay->handle)) | |
476 | unregister_hotplug_dock_device(bay->handle); | |
477 | acpi_bay_remove_fs(bay); | |
478 | acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY, | |
479 | bay_notify); | |
2b167c01 | 480 | platform_device_unregister(bay->pdev); |
01b57e73 KCA |
481 | kfree(bay->name); |
482 | kfree(bay); | |
483 | } | |
484 | ||
01b57e73 KCA |
485 | acpi_bus_unregister_driver(&acpi_bay_driver); |
486 | } | |
487 | ||
488 | postcore_initcall(bay_init); | |
489 | module_exit(bay_exit); | |
490 |