ACPI: delete extra #defines in /drivers/acpi/ drivers
[sfrench/cifs-2.6.git] / drivers / acpi / bay.c
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>
31 #include <linux/seq_file.h>
32 #include <asm/uaccess.h>
33 #include <linux/platform_device.h>
34
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);
48 static int acpi_bay_add(struct acpi_device *device);
49 static int acpi_bay_remove(struct acpi_device *device, int type);
50
51 static struct acpi_driver acpi_bay_driver = {
52         .name = "bay",
53         .class = ACPI_BAY_CLASS,
54         .ids = ACPI_BAY_HID,
55         .ops = {
56                 .add = acpi_bay_add,
57                 .remove = acpi_bay_remove,
58                 },
59 };
60
61 struct bay {
62         acpi_handle handle;
63         char *name;
64         struct list_head list;
65         struct platform_device *pdev;
66 };
67
68 static LIST_HEAD(drive_bays);
69
70
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  */
80 static 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  */
97 static 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  */
116 static 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
133 /*
134  * show_present - read method for "present" file in sysfs
135  */
136 static 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 }
143 DEVICE_ATTR(present, S_IRUGO, show_present, NULL);
144
145 /*
146  * write_eject - write method for "eject" file in sysfs
147  */
148 static 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 }
159 DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);
160
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  */
168 static 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  */
185 static 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  */
203 static 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  */
218 int 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 }
233 EXPORT_SYMBOL_GPL(eject_removable_drive);
234
235 static 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
243 static int acpi_bay_add_fs(struct bay *bay)
244 {
245         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;
261 }
262
263 static void acpi_bay_remove_fs(struct bay *bay)
264 {
265         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);
270 }
271
272 static 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
284 static int bay_add(acpi_handle handle, int id)
285 {
286         acpi_status status;
287         struct bay *new_bay;
288         struct platform_device *pdev;
289         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         /*
295          * Initialize bay device structure
296          */
297         new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
298         INIT_LIST_HEAD(&new_bay->list);
299         new_bay->handle = handle;
300         new_bay->name = (char *)nbuffer.pointer;
301
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         }
315
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         }
330         list_add(&new_bay->list, &drive_bays);
331         printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
332         return 0;
333
334 bay_add_err:
335         kfree(new_bay->name);
336         kfree(new_bay);
337         return -ENODEV;
338 }
339
340 static int acpi_bay_remove(struct acpi_device *device, int type)
341 {
342         /*** FIXME: do something here */
343         return 0;
344 }
345
346 /**
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  */
357 static 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  */
393 static void bay_notify(acpi_handle handle, u32 event, void *data)
394 {
395         struct acpi_device *dev;
396
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
430 static acpi_status
431 find_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");
442                 if (!bay_add(handle, *count))
443                         (*count)++;
444         }
445         return AE_OK;
446 }
447
448 static int __init bay_init(void)
449 {
450         int bays = 0;
451
452         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
458         if (bays)
459                 if ((acpi_bus_register_driver(&acpi_bay_driver) < 0))
460                         printk(KERN_ERR "Unable to register bay driver\n");
461
462         if (!bays)
463                 return -ENODEV;
464
465         return 0;
466 }
467
468 static 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);
478                 platform_device_unregister(bay->pdev);
479                 kfree(bay->name);
480                 kfree(bay);
481         }
482
483         acpi_bus_unregister_driver(&acpi_bay_driver);
484 }
485
486 postcore_initcall(bay_init);
487 module_exit(bay_exit);
488