Blame view

drivers/acpi/button.c 12 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
50a4da890   Bjorn Helgaas   ACPI: button: whi...
2
   *  button.c - ACPI Button Driver
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
   *
   *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
   *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@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>
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
29
30
31
  #include <linux/types.h>
  #include <linux/proc_fs.h>
  #include <linux/seq_file.h>
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
32
  #include <linux/input.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
33
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  #include <acpi/acpi_bus.h>
  #include <acpi/acpi_drivers.h>
6270da6f4   Andy Shevchenko   ACPI: suppress co...
36
  #include <acpi/button.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37

a192a9580   Len Brown   ACPI: Move defini...
38
  #define PREFIX "ACPI: "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  #define ACPI_BUTTON_CLASS		"button"
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
40
41
42
  #define ACPI_BUTTON_FILE_INFO		"info"
  #define ACPI_BUTTON_FILE_STATE		"state"
  #define ACPI_BUTTON_TYPE_UNKNOWN	0x00
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
  #define ACPI_BUTTON_NOTIFY_STATUS	0x80
  
  #define ACPI_BUTTON_SUBCLASS_POWER	"power"
4be44fcd3   Len Brown   [ACPI] Lindent al...
46
  #define ACPI_BUTTON_HID_POWER		"PNP0C0C"
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
47
  #define ACPI_BUTTON_DEVICE_NAME_POWER	"Power Button"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
  #define ACPI_BUTTON_TYPE_POWER		0x01
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
  
  #define ACPI_BUTTON_SUBCLASS_SLEEP	"sleep"
  #define ACPI_BUTTON_HID_SLEEP		"PNP0C0E"
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
52
  #define ACPI_BUTTON_DEVICE_NAME_SLEEP	"Sleep Button"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  #define ACPI_BUTTON_TYPE_SLEEP		0x03
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
  
  #define ACPI_BUTTON_SUBCLASS_LID	"lid"
  #define ACPI_BUTTON_HID_LID		"PNP0C0D"
  #define ACPI_BUTTON_DEVICE_NAME_LID	"Lid Switch"
  #define ACPI_BUTTON_TYPE_LID		0x05
  
  #define _COMPONENT		ACPI_BUTTON_COMPONENT
f52fd66d2   Len Brown   ACPI: clean up AC...
61
  ACPI_MODULE_NAME("button");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
63
  MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e00   Len Brown   ACPI: delete extr...
64
  MODULE_DESCRIPTION("ACPI Button Driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
  MODULE_LICENSE("GPL");
1ba90e3a8   Thomas Renninger   ACPI: autoload mo...
66
67
68
69
70
71
72
73
74
  static const struct acpi_device_id button_device_ids[] = {
  	{ACPI_BUTTON_HID_LID,    0},
  	{ACPI_BUTTON_HID_SLEEP,  0},
  	{ACPI_BUTTON_HID_SLEEPF, 0},
  	{ACPI_BUTTON_HID_POWER,  0},
  	{ACPI_BUTTON_HID_POWERF, 0},
  	{"", 0},
  };
  MODULE_DEVICE_TABLE(acpi, button_device_ids);
4be44fcd3   Len Brown   [ACPI] Lindent al...
75
  static int acpi_button_add(struct acpi_device *device);
51fac8388   Rafael J. Wysocki   ACPI: Remove usel...
76
  static int acpi_button_remove(struct acpi_device *device);
373cfc360   Bjorn Helgaas   ACPI: button: use...
77
  static void acpi_button_notify(struct acpi_device *device, u32 event);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78

906924048   Rafael J. Wysocki   ACPI / PM: Fix un...
79
  #ifdef CONFIG_PM_SLEEP
1be532de8   Rafael J. Wysocki   ACPI: Use struct ...
80
  static int acpi_button_resume(struct device *dev);
906924048   Rafael J. Wysocki   ACPI / PM: Fix un...
81
  #endif
1be532de8   Rafael J. Wysocki   ACPI: Use struct ...
82
  static SIMPLE_DEV_PM_OPS(acpi_button_pm, NULL, acpi_button_resume);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
  static struct acpi_driver acpi_button_driver = {
c2b6705b7   Len Brown   ACPI: fix acpi_dr...
84
  	.name = "button",
4be44fcd3   Len Brown   [ACPI] Lindent al...
85
  	.class = ACPI_BUTTON_CLASS,
1ba90e3a8   Thomas Renninger   ACPI: autoload mo...
86
  	.ids = button_device_ids,
4be44fcd3   Len Brown   [ACPI] Lindent al...
87
88
89
  	.ops = {
  		.add = acpi_button_add,
  		.remove = acpi_button_remove,
373cfc360   Bjorn Helgaas   ACPI: button: use...
90
  		.notify = acpi_button_notify,
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
91
  	},
1be532de8   Rafael J. Wysocki   ACPI: Use struct ...
92
  	.drv.pm = &acpi_button_pm,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
  };
  
  struct acpi_button {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
96
97
98
  	unsigned int type;
  	struct input_dev *input;
  	char phys[32];			/* for input device */
4be44fcd3   Len Brown   [ACPI] Lindent al...
99
  	unsigned long pushed;
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
100
  	bool wakeup_enabled;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  };
7e12715ec   Jesse Barnes   ACPI button: prov...
102
103
  static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  static struct acpi_device *lid_device;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
104
105
106
  /* --------------------------------------------------------------------------
                                FS Interface (/proc)
     -------------------------------------------------------------------------- */
4be44fcd3   Len Brown   [ACPI] Lindent al...
107
  static struct proc_dir_entry *acpi_button_dir;
912b7427f   Zhang Rui   ACPI button: remo...
108
  static struct proc_dir_entry *acpi_lid_dir;
4be44fcd3   Len Brown   [ACPI] Lindent al...
109

b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
110
111
  static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
112
  	struct acpi_device *device = seq->private;
4be44fcd3   Len Brown   [ACPI] Lindent al...
113
  	acpi_status status;
27663c585   Matthew Wilcox   ACPI: Change acpi...
114
  	unsigned long long state;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
115

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
116
  	status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
117
118
119
120
  	seq_printf(seq, "state:      %s
  ",
  		   ACPI_FAILURE(status) ? "unsupported" :
  			(state ? "open" : "closed"));
d550d98d3   Patrick Mochel   ACPI: delete trac...
121
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
122
123
124
125
  }
  
  static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  {
d9dda78ba   Al Viro   procfs: new helpe...
126
  	return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
127
  }
912b7427f   Zhang Rui   ACPI button: remo...
128
129
130
131
132
133
134
  static const struct file_operations acpi_button_state_fops = {
  	.owner = THIS_MODULE,
  	.open = acpi_button_state_open_fs,
  	.read = seq_read,
  	.llseek = seq_lseek,
  	.release = single_release,
  };
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
135

4be44fcd3   Len Brown   [ACPI] Lindent al...
136
  static int acpi_button_add_fs(struct acpi_device *device)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
137
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
138
  	struct acpi_button *button = acpi_driver_data(device);
4be44fcd3   Len Brown   [ACPI] Lindent al...
139
  	struct proc_dir_entry *entry = NULL;
912b7427f   Zhang Rui   ACPI button: remo...
140
  	int ret = 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
141

912b7427f   Zhang Rui   ACPI button: remo...
142
143
144
145
146
147
148
149
  	/* procfs I/F for ACPI lid device only */
  	if (button->type != ACPI_BUTTON_TYPE_LID)
  		return 0;
  
  	if (acpi_button_dir || acpi_lid_dir) {
  		printk(KERN_ERR PREFIX "More than one Lid device found!
  ");
  		return -EEXIST;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
150
  	}
912b7427f   Zhang Rui   ACPI button: remo...
151
152
153
  	/* create /proc/acpi/button */
  	acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  	if (!acpi_button_dir)
d550d98d3   Patrick Mochel   ACPI: delete trac...
154
  		return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
155

912b7427f   Zhang Rui   ACPI button: remo...
156
157
158
159
160
161
  	/* create /proc/acpi/button/lid */
  	acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  	if (!acpi_lid_dir) {
  		ret = -ENODEV;
  		goto remove_button_dir;
  	}
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
162

912b7427f   Zhang Rui   ACPI button: remo...
163
164
165
166
167
168
  	/* create /proc/acpi/button/lid/LID/ */
  	acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
  	if (!acpi_device_dir(device)) {
  		ret = -ENODEV;
  		goto remove_lid_dir;
  	}
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
169

912b7427f   Zhang Rui   ACPI button: remo...
170
171
172
173
174
175
176
  	/* create /proc/acpi/button/lid/LID/state */
  	entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  				 S_IRUGO, acpi_device_dir(device),
  				 &acpi_button_state_fops, device);
  	if (!entry) {
  		ret = -ENODEV;
  		goto remove_dev_dir;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
177
  	}
912b7427f   Zhang Rui   ACPI button: remo...
178
179
180
181
182
183
184
185
186
187
188
189
  done:
  	return ret;
  
  remove_dev_dir:
  	remove_proc_entry(acpi_device_bid(device),
  			  acpi_lid_dir);
  	acpi_device_dir(device) = NULL;
  remove_lid_dir:
  	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  remove_button_dir:
  	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  	goto done;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
190
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
191
  static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
192
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
193
  	struct acpi_button *button = acpi_driver_data(device);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
194

912b7427f   Zhang Rui   ACPI button: remo...
195
196
  	if (button->type != ACPI_BUTTON_TYPE_LID)
  		return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
197

912b7427f   Zhang Rui   ACPI button: remo...
198
199
200
201
202
203
204
  	remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  			  acpi_device_dir(device));
  	remove_proc_entry(acpi_device_bid(device),
  			  acpi_lid_dir);
  	acpi_device_dir(device) = NULL;
  	remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
205

d550d98d3   Patrick Mochel   ACPI: delete trac...
206
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
207
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
  /* --------------------------------------------------------------------------
                                  Driver Interface
     -------------------------------------------------------------------------- */
7e12715ec   Jesse Barnes   ACPI button: prov...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  int acpi_lid_notifier_register(struct notifier_block *nb)
  {
  	return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  }
  EXPORT_SYMBOL(acpi_lid_notifier_register);
  
  int acpi_lid_notifier_unregister(struct notifier_block *nb)
  {
  	return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  }
  EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  
  int acpi_lid_open(void)
  {
  	acpi_status status;
  	unsigned long long state;
2c907b72d   Jesse Barnes   ACPI button: don'...
227
228
  	if (!lid_device)
  		return -ENODEV;
7e12715ec   Jesse Barnes   ACPI button: prov...
229
230
231
232
233
234
235
236
  	status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
  				       &state);
  	if (ACPI_FAILURE(status))
  		return -ENODEV;
  
  	return !!state;
  }
  EXPORT_SYMBOL(acpi_lid_open);
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
237
  static int acpi_lid_send_state(struct acpi_device *device)
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
238
  {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
239
  	struct acpi_button *button = acpi_driver_data(device);
27663c585   Matthew Wilcox   ACPI: Change acpi...
240
  	unsigned long long state;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
241
  	acpi_status status;
7e12715ec   Jesse Barnes   ACPI button: prov...
242
  	int ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
243

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
244
  	status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
245
246
  	if (ACPI_FAILURE(status))
  		return -ENODEV;
50a4da890   Bjorn Helgaas   ACPI: button: whi...
247

23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
248
249
  	/* input layer checks if event is redundant */
  	input_report_switch(button->input, SW_LID, !state);
df316e939   Guillem Jover   ACPI: Always repo...
250
  	input_sync(button->input);
7e12715ec   Jesse Barnes   ACPI button: prov...
251

1f83511bd   Rafael J. Wysocki   ACPI / PM: Report...
252
253
  	if (state)
  		pm_wakeup_event(&device->dev, 0);
7e12715ec   Jesse Barnes   ACPI button: prov...
254
255
256
257
  	ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  	if (ret == NOTIFY_DONE)
  		ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  						   device);
13c199c0d   Zhao Yakui   ACPI: Use the ret...
258
259
260
261
262
263
264
  	if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  		/*
  		 * It is also regarded as success if the notifier_chain
  		 * returns NOTIFY_OK or NOTIFY_DONE.
  		 */
  		ret = 0;
  	}
7e12715ec   Jesse Barnes   ACPI button: prov...
265
  	return ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
266
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267

373cfc360   Bjorn Helgaas   ACPI: button: use...
268
  static void acpi_button_notify(struct acpi_device *device, u32 event)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
  {
373cfc360   Bjorn Helgaas   ACPI: button: use...
270
  	struct acpi_button *button = acpi_driver_data(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
271
  	struct input_dev *input;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  	switch (event) {
373cfc360   Bjorn Helgaas   ACPI: button: use...
274
275
276
  	case ACPI_FIXED_HARDWARE_EVENT:
  		event = ACPI_BUTTON_NOTIFY_STATUS;
  		/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
  	case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
278
  		input = button->input;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
279
  		if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
280
  			acpi_lid_send_state(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
281
282
283
284
285
286
287
  		} else {
  			int keycode = test_bit(KEY_SLEEP, input->keybit) ?
  						KEY_SLEEP : KEY_POWER;
  
  			input_report_key(input, keycode, 1);
  			input_sync(input);
  			input_report_key(input, keycode, 0);
df316e939   Guillem Jover   ACPI: Always repo...
288
  			input_sync(input);
1f83511bd   Rafael J. Wysocki   ACPI / PM: Report...
289
290
  
  			pm_wakeup_event(&device->dev, 0);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
291
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
  		break;
  	default:
  		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd3   Len Brown   [ACPI] Lindent al...
295
296
  				  "Unsupported event [0x%x]
  ", event));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
  		break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  }
906924048   Rafael J. Wysocki   ACPI / PM: Fix un...
300
  #ifdef CONFIG_PM_SLEEP
1be532de8   Rafael J. Wysocki   ACPI: Use struct ...
301
  static int acpi_button_resume(struct device *dev)
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
302
  {
1be532de8   Rafael J. Wysocki   ACPI: Use struct ...
303
  	struct acpi_device *device = to_acpi_device(dev);
1bce81131   Bjorn Helgaas   ACPI: button: use...
304
  	struct acpi_button *button = acpi_driver_data(device);
50a4da890   Bjorn Helgaas   ACPI: button: whi...
305

e2fb9754d   Bjorn Helgaas   ACPI: button: rem...
306
  	if (button->type == ACPI_BUTTON_TYPE_LID)
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
307
  		return acpi_lid_send_state(device);
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
308
309
  	return 0;
  }
906924048   Rafael J. Wysocki   ACPI / PM: Fix un...
310
  #endif
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
311

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
312
313
  static int acpi_button_add(struct acpi_device *device)
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
314
315
  	struct acpi_button *button;
  	struct input_dev *input;
620e112cf   Thomas Renninger   ACPI/PNP: A HID v...
316
317
  	const char *hid = acpi_device_hid(device);
  	char *name, *class;
1bce81131   Bjorn Helgaas   ACPI: button: use...
318
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
320
  	button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
  	if (!button)
d550d98d3   Patrick Mochel   ACPI: delete trac...
322
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323

db89b4f0d   Pavel Machek   ACPI: catch calls...
324
  	device->driver_data = button;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
326
327
328
329
330
  	button->input = input = input_allocate_device();
  	if (!input) {
  		error = -ENOMEM;
  		goto err_free_button;
  	}
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
331
332
  	name = acpi_device_name(device);
  	class = acpi_device_class(device);
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
333
334
  	if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  	    !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
  		button->type = ACPI_BUTTON_TYPE_POWER;
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
336
337
  		strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  		sprintf(class, "%s/%s",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
  			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
339
340
  	} else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  		   !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  		button->type = ACPI_BUTTON_TYPE_SLEEP;
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
342
343
  		strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  		sprintf(class, "%s/%s",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
345
  	} else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
  		button->type = ACPI_BUTTON_TYPE_LID;
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
347
348
  		strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  		sprintf(class, "%s/%s",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
  			ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
4be44fcd3   Len Brown   [ACPI] Lindent al...
350
  	} else {
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
351
352
  		printk(KERN_ERR PREFIX "Unsupported hid [%s]
  ", hid);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
353
354
  		error = -ENODEV;
  		goto err_free_input;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
  	}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
356
357
358
  	error = acpi_button_add_fs(device);
  	if (error)
  		goto err_free_input;
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
359
  	snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
360

bf04a7722   Bjorn Helgaas   ACPI: button: cac...
361
  	input->name = name;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
362
363
364
  	input->phys = button->phys;
  	input->id.bustype = BUS_HOST;
  	input->id.product = button->type;
3b34e5232   Andrey Borzenkov   ACPI: button: mak...
365
  	input->dev.parent = &device->dev;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
366

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
  	switch (button->type) {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
368
  	case ACPI_BUTTON_TYPE_POWER:
763f527b6   Lan Tianyu   ACPI / button: Us...
369
  		input_set_capability(input, EV_KEY, KEY_POWER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
371
372
  
  	case ACPI_BUTTON_TYPE_SLEEP:
763f527b6   Lan Tianyu   ACPI / button: Us...
373
  		input_set_capability(input, EV_KEY, KEY_SLEEP);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
375
376
  
  	case ACPI_BUTTON_TYPE_LID:
763f527b6   Lan Tianyu   ACPI / button: Us...
377
  		input_set_capability(input, EV_SW, SW_LID);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
379
  		break;
  	}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
380
381
  	error = input_register_device(input);
  	if (error)
373cfc360   Bjorn Helgaas   ACPI: button: use...
382
  		goto err_remove_fs;
7e12715ec   Jesse Barnes   ACPI button: prov...
383
  	if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
384
  		acpi_lid_send_state(device);
7e12715ec   Jesse Barnes   ACPI button: prov...
385
386
387
388
389
390
  		/*
  		 * This assumes there's only one lid device, or if there are
  		 * more we only care about the last one...
  		 */
  		lid_device = device;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
  
  	if (device->wakeup.flags.valid) {
  		/* Button's GPE is run-wake GPE */
4be44fcd3   Len Brown   [ACPI] Lindent al...
394
  		acpi_enable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
395
  				device->wakeup.gpe_number);
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
396
397
398
399
  		if (!device_may_wakeup(&device->dev)) {
  			device_set_wakeup_enable(&device->dev, true);
  			button->wakeup_enabled = true;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  	}
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
401
402
  	printk(KERN_INFO PREFIX "%s [%s]
  ", name, acpi_device_bid(device));
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
403
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
405
406
407
408
409
410
411
   err_remove_fs:
  	acpi_button_remove_fs(device);
   err_free_input:
  	input_free_device(input);
   err_free_button:
  	kfree(button);
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
  }
51fac8388   Rafael J. Wysocki   ACPI: Remove usel...
413
  static int acpi_button_remove(struct acpi_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
415
  	struct acpi_button *button = acpi_driver_data(device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416

9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
417
418
  	if (device->wakeup.flags.valid) {
  		acpi_disable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
419
  				device->wakeup.gpe_number);
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
420
421
  		if (button->wakeup_enabled)
  			device_set_wakeup_enable(&device->dev, false);
9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
422
  	}
4be44fcd3   Len Brown   [ACPI] Lindent al...
423
  	acpi_button_remove_fs(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
424
  	input_unregister_device(button->input);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  	kfree(button);
d550d98d3   Patrick Mochel   ACPI: delete trac...
426
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
  }
466e78f77   Mika Westerberg   ACPI/button: conv...
428
  module_acpi_driver(acpi_button_driver);