summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/bay.c
blob: 56a5b3fffeb364a14e8fe3ff7f5b9a1ff103f51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 *  bay.c - ACPI removable drive bay driver
 *
 *  Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/notifier.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
#include <linux/platform_device.h>

ACPI_MODULE_NAME("bay");
MODULE_AUTHOR("Kristen Carlson Accardi");
MODULE_DESCRIPTION("ACPI Removable Drive Bay Driver");
MODULE_LICENSE("GPL");
#define ACPI_BAY_CLASS "bay"
#define ACPI_BAY_COMPONENT	0x10000000
#define _COMPONENT ACPI_BAY_COMPONENT
#define bay_dprintk(h,s) {\
	char prefix[80] = {'\0'};\
	struct acpi_buffer buffer = {sizeof(prefix), prefix};\
	acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
	printk(KERN_DEBUG PREFIX "%s: %s\n", prefix, s); }
static void bay_notify(acpi_handle handle, u32 event, void *data);

struct bay {
	acpi_handle handle;
	char *name;
	struct list_head list;
	struct platform_device *pdev;
};

static LIST_HEAD(drive_bays);


/*****************************************************************************
 *                         Drive Bay functions                               *
 *****************************************************************************/
/**
 * is_ejectable - see if a device is ejectable
 * @handle: acpi handle of the device
 *
 * If an acpi object has a _EJ0 method, then it is ejectable
 */
static int is_ejectable(acpi_handle handle)
{
	acpi_status status;
	acpi_handle tmp;

	status = acpi_get_handle(handle, "_EJ0", &tmp);
	if (ACPI_FAILURE(status))
		return 0;
	return 1;
}

/**
 * bay_present - see if the bay device is present
 * @bay: the drive bay
 *
 * execute the _STA method.
 */
static int bay_present(struct bay *bay)
{
	unsigned long sta;
	acpi_status status;

	if (bay) {
		status = acpi_evaluate_integer(bay->handle, "_STA", NULL, &sta);
		if (ACPI_SUCCESS(status) && sta)
			return 1;
	}
	return 0;
}

/**
 * eject_device - respond to an eject request
 * @handle - the device to eject
 *
 * Call this devices _EJ0 method.
 */
static void eject_device(acpi_handle handle)
{
	struct acpi_object_list arg_list;
	union acpi_object arg;

	bay_dprintk(handle, "Ejecting device");

	arg_list.count = 1;
	arg_list.pointer = &arg;
	arg.type = ACPI_TYPE_INTEGER;
	arg.integer.value = 1;

	if (ACPI_FAILURE(acpi_evaluate_object(handle, "_EJ0",
					      &arg_list, NULL)))
		pr_debug("Failed to evaluate _EJ0!\n");
}

/*
 * show_present - read method for "present" file in sysfs
 */
static ssize_t show_present(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	struct bay *bay = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", bay_present(bay));

}
DEVICE_ATTR(present, S_IRUGO, show_present, NULL);

/*
 * write_eject - write method for "eject" file in sysfs
 */
static ssize_t write_eject(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct bay *bay = dev_get_drvdata(dev);

	if (!count)
		return -EINVAL;

	eject_device(bay->handle);
	return count;
}
DEVICE_ATTR(eject, S_IWUSR, NULL, write_eject);

/**
 * is_ata - see if a device is an ata device
 * @handle: acpi handle of the device
 *
 * If an acpi object has one of 4 ATA ACPI methods defined,
 * then it is an ATA device
 */
static int is_ata(acpi_handle handle)
{
	acpi_handle tmp;

	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
	   (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
	   (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
	   (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
		return 1;

	return 0;
}

/**
 * parent_is_ata(acpi_handle handle)
 *
 */
static int parent_is_ata(acpi_handle handle)
{
	acpi_handle phandle;

	if (acpi_get_parent(handle, &phandle))
		return 0;

	return is_ata(phandle);
}

/**
 * is_ejectable_bay - see if a device is an ejectable drive bay
 * @handle: acpi handle of the device
 *
 * If an acpi object is ejectable and has one of the ACPI ATA
 * methods defined, then we can safely call it an ejectable
 * drive bay
 */
static int is_ejectable_bay(acpi_handle handle)
{
	if ((is_ata(handle) || parent_is_ata(handle)) && is_ejectable(handle))
		return 1;
	return 0;
}

/**
 * eject_removable_drive - try to eject this drive
 * @dev : the device structure of the drive
 *
 * If a device is a removable drive that requires an _EJ0 method
 * to be executed in order to safely remove from the system, do
 * it.  ATM - always returns success
 */
int eject_removable_drive(struct device *dev)
{
	acpi_handle handle = DEVICE_ACPI_HANDLE(dev);

	if (handle) {
		bay_dprintk(handle, "Got device handle");
		if (is_ejectable_bay(handle))
			eject_device(handle);
	} else {
		printk("No acpi handle for device\n");
	}

	/* should I return an error code? */
	return 0;
}
EXPORT_SYMBOL_GPL(eject_removable_drive);

static int acpi_bay_add_fs(struct bay *bay)
{
	int ret;
	struct device *dev = &bay->pdev->dev;

	ret = device_create_file(dev, &dev_attr_present);
	if (ret)
		goto add_fs_err;
	ret = device_create_file(dev, &dev_attr_eject);
	if (ret) {
		device_remove_file(dev, &dev_attr_present);
		goto add_fs_err;
	}
	return 0;

 add_fs_err:
	bay_dprintk(bay->handle, "Error adding sysfs files\n");
	return ret;
}

static void acpi_bay_remove_fs(struct bay *bay)
{
	struct device *dev = &bay->pdev->dev;

	/* cleanup sysfs */
	device_remove_file(dev, &dev_attr_present);
	device_remove_file(dev, &dev_attr_eject);
}

static int bay_is_dock_device(acpi_handle handle)
{
	acpi_handle parent;

	acpi_get_parent(handle, &parent);

	/* if the device or it's parent is dependent on the
	 * dock, then we are a dock device
	 */
	return (is_dock_device(handle) || is_dock_device(parent));
}

static int bay_add(acpi_handle handle, int id)
{
	acpi_status status;
	struct bay *new_bay;
	struct platform_device *pdev;
	struct acpi_buffer nbuffer = {ACPI_ALLOCATE_BUFFER, NULL};
	acpi_get_name(handle, ACPI_FULL_PATHNAME, &nbuffer);

	bay_dprintk(handle, "Adding notify handler");

	/*
	 * Initialize bay device structure
	 */
	new_bay = kzalloc(sizeof(*new_bay), GFP_ATOMIC);
	INIT_LIST_HEAD(&new_bay->list);
	new_bay->handle = handle;
	new_bay->name = (char *)nbuffer.pointer;

	/* initialize platform device stuff */
	pdev = platform_device_register_simple(ACPI_BAY_CLASS, id, NULL, 0);
	if (IS_ERR(pdev)) {
		printk(KERN_ERR PREFIX "Error registering bay device\n");
		goto bay_add_err;
	}
	new_bay->pdev = pdev;
	platform_set_drvdata(pdev, new_bay);

	/*
	 * we want the bay driver to be able to send uevents
	 */
	pdev->dev.uevent_suppress = 0;

	if (acpi_bay_add_fs(new_bay)) {
		platform_device_unregister(new_bay->pdev);
		goto bay_add_err;
	}

	/* register for events on this device */
	status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
			bay_notify, new_bay);
	if (ACPI_FAILURE(status)) {
		printk(KERN_ERR PREFIX "Error installing bay notify handler\n");
	}

	/* if we are on a dock station, we should register for dock
	 * notifications.
	 */
	if (bay_is_dock_device(handle)) {
		bay_dprintk(handle, "Is dependent on dock\n");
		register_hotplug_dock_device(handle, bay_notify, new_bay);
	}
	list_add(&new_bay->list, &drive_bays);
	printk(KERN_INFO PREFIX "Bay [%s] Added\n", new_bay->name);
	return 0;

bay_add_err:
	kfree(new_bay->name);
	kfree(new_bay);
	return -ENODEV;
}

/**
 * bay_notify - act upon an acpi bay notification
 * @handle: the bay handle
 * @event: the acpi event
 * @data: our driver data struct
 *
 */
static void bay_notify(acpi_handle handle, u32 event, void *data)
{
	struct bay *bay_dev = (struct bay *)data;
	struct device *dev = &bay_dev->pdev->dev;
	char event_string[12];
	char *envp[] = { event_string, NULL };

	bay_dprintk(handle, "Bay event");
	sprintf(event_string, "BAY_EVENT=%d\n", event);
	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
}

static acpi_status
find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
{
	int *count = (int *)context;

	/*
	 * there could be more than one ejectable bay.
	 * so, just return AE_OK always so that every object
	 * will be checked.
	 */
	if (is_ejectable_bay(handle)) {
		bay_dprintk(handle, "found ejectable bay");
		if (!bay_add(handle, *count))
			(*count)++;
	}
	return AE_OK;
}

static int __init bay_init(void)
{
	int bays = 0;

	INIT_LIST_HEAD(&drive_bays);

	/* look for dockable drive bays */
	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
		ACPI_UINT32_MAX, find_bay, &bays, NULL);

	if (!bays)
		return -ENODEV;

	return 0;
}

static void __exit bay_exit(void)
{
	struct bay *bay, *tmp;

	list_for_each_entry_safe(bay, tmp, &drive_bays, list) {
		if (is_dock_device(bay->handle))
			unregister_hotplug_dock_device(bay->handle);
		acpi_bay_remove_fs(bay);
		acpi_remove_notify_handler(bay->handle, ACPI_SYSTEM_NOTIFY,
			bay_notify);
		platform_device_unregister(bay->pdev);
		kfree(bay->name);
		kfree(bay);
	}
}

postcore_initcall(bay_init);
module_exit(bay_exit);