Blame view

drivers/acpi/button.c 12.2 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>
a192a9580   Len Brown   ACPI: Move defini...
36
  #define PREFIX "ACPI: "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  #define ACPI_BUTTON_CLASS		"button"
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
38
39
40
  #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
41
42
43
  #define ACPI_BUTTON_NOTIFY_STATUS	0x80
  
  #define ACPI_BUTTON_SUBCLASS_POWER	"power"
4be44fcd3   Len Brown   [ACPI] Lindent al...
44
  #define ACPI_BUTTON_HID_POWER		"PNP0C0C"
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
45
  #define ACPI_BUTTON_DEVICE_NAME_POWER	"Power Button"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  #define ACPI_BUTTON_TYPE_POWER		0x01
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
  
  #define ACPI_BUTTON_SUBCLASS_SLEEP	"sleep"
  #define ACPI_BUTTON_HID_SLEEP		"PNP0C0E"
d68b597c8   Bjorn Helgaas   ACPI: button: rem...
50
  #define ACPI_BUTTON_DEVICE_NAME_SLEEP	"Sleep Button"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  #define ACPI_BUTTON_TYPE_SLEEP		0x03
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
57
58
  
  #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...
59
  ACPI_MODULE_NAME("button");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
61
  MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e00   Len Brown   ACPI: delete extr...
62
  MODULE_DESCRIPTION("ACPI Button Driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  MODULE_LICENSE("GPL");
1ba90e3a8   Thomas Renninger   ACPI: autoload mo...
64
65
66
67
68
69
70
71
72
  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...
73
74
  static int acpi_button_add(struct acpi_device *device);
  static int acpi_button_remove(struct acpi_device *device, int type);
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
75
  static int acpi_button_resume(struct acpi_device *device);
373cfc360   Bjorn Helgaas   ACPI: button: use...
76
  static void acpi_button_notify(struct acpi_device *device, u32 event);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  
  static struct acpi_driver acpi_button_driver = {
c2b6705b7   Len Brown   ACPI: fix acpi_dr...
79
  	.name = "button",
4be44fcd3   Len Brown   [ACPI] Lindent al...
80
  	.class = ACPI_BUTTON_CLASS,
1ba90e3a8   Thomas Renninger   ACPI: autoload mo...
81
  	.ids = button_device_ids,
4be44fcd3   Len Brown   [ACPI] Lindent al...
82
83
  	.ops = {
  		.add = acpi_button_add,
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
84
  		.resume = acpi_button_resume,
4be44fcd3   Len Brown   [ACPI] Lindent al...
85
  		.remove = acpi_button_remove,
373cfc360   Bjorn Helgaas   ACPI: button: use...
86
  		.notify = acpi_button_notify,
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
87
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
  };
  
  struct acpi_button {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
91
92
93
  	unsigned int type;
  	struct input_dev *input;
  	char phys[32];			/* for input device */
4be44fcd3   Len Brown   [ACPI] Lindent al...
94
  	unsigned long pushed;
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
95
  	bool wakeup_enabled;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  };
7e12715ec   Jesse Barnes   ACPI button: prov...
97
98
  static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  static struct acpi_device *lid_device;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
99
100
101
  /* --------------------------------------------------------------------------
                                FS Interface (/proc)
     -------------------------------------------------------------------------- */
4be44fcd3   Len Brown   [ACPI] Lindent al...
102
  static struct proc_dir_entry *acpi_button_dir;
912b7427f   Zhang Rui   ACPI button: remo...
103
  static struct proc_dir_entry *acpi_lid_dir;
4be44fcd3   Len Brown   [ACPI] Lindent al...
104

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

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
111
  	status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
112
113
114
115
  	seq_printf(seq, "state:      %s
  ",
  		   ACPI_FAILURE(status) ? "unsupported" :
  			(state ? "open" : "closed"));
d550d98d3   Patrick Mochel   ACPI: delete trac...
116
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
117
118
119
120
121
122
  }
  
  static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  {
  	return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
  }
912b7427f   Zhang Rui   ACPI button: remo...
123
124
125
126
127
128
129
  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...
130

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

912b7427f   Zhang Rui   ACPI button: remo...
137
138
139
140
141
142
143
144
  	/* 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...
145
  	}
912b7427f   Zhang Rui   ACPI button: remo...
146
147
148
  	/* 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...
149
  		return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
150

912b7427f   Zhang Rui   ACPI button: remo...
151
152
153
154
155
156
  	/* 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...
157

912b7427f   Zhang Rui   ACPI button: remo...
158
159
160
161
162
163
  	/* 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...
164

912b7427f   Zhang Rui   ACPI button: remo...
165
166
167
168
169
170
171
  	/* 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...
172
  	}
912b7427f   Zhang Rui   ACPI button: remo...
173
174
175
176
177
178
179
180
181
182
183
184
  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...
185
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
186
  static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
187
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
188
  	struct acpi_button *button = acpi_driver_data(device);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
189

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

912b7427f   Zhang Rui   ACPI button: remo...
193
194
195
196
197
198
199
  	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...
200

d550d98d3   Patrick Mochel   ACPI: delete trac...
201
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
202
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
205
  /* --------------------------------------------------------------------------
                                  Driver Interface
     -------------------------------------------------------------------------- */
7e12715ec   Jesse Barnes   ACPI button: prov...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  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'...
222
223
  	if (!lid_device)
  		return -ENODEV;
7e12715ec   Jesse Barnes   ACPI button: prov...
224
225
226
227
228
229
230
231
  	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...
232
  static int acpi_lid_send_state(struct acpi_device *device)
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
233
  {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
234
  	struct acpi_button *button = acpi_driver_data(device);
27663c585   Matthew Wilcox   ACPI: Change acpi...
235
  	unsigned long long state;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
236
  	acpi_status status;
7e12715ec   Jesse Barnes   ACPI button: prov...
237
  	int ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
238

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

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

1f83511bd   Rafael J. Wysocki   ACPI / PM: Report...
247
248
  	if (state)
  		pm_wakeup_event(&device->dev, 0);
7e12715ec   Jesse Barnes   ACPI button: prov...
249
250
251
252
  	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...
253
254
255
256
257
258
259
  	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...
260
  	return ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
261
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  	switch (event) {
373cfc360   Bjorn Helgaas   ACPI: button: use...
269
270
271
  	case ACPI_FIXED_HARDWARE_EVENT:
  		event = ACPI_BUTTON_NOTIFY_STATUS;
  		/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
  	case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
273
  		input = button->input;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
274
  		if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
275
  			acpi_lid_send_state(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
276
277
278
279
280
281
282
  		} 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...
283
  			input_sync(input);
1f83511bd   Rafael J. Wysocki   ACPI / PM: Report...
284
285
  
  			pm_wakeup_event(&device->dev, 0);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
286
  		}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
287

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
288
  		acpi_bus_generate_proc_event(device, event, ++button->pushed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
  		break;
  	default:
  		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd3   Len Brown   [ACPI] Lindent al...
292
293
  				  "Unsupported event [0x%x]
  ", event));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
  		break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  }
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
297
298
  static int acpi_button_resume(struct acpi_device *device)
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
299
  	struct acpi_button *button = acpi_driver_data(device);
50a4da890   Bjorn Helgaas   ACPI: button: whi...
300

e2fb9754d   Bjorn Helgaas   ACPI: button: rem...
301
  	if (button->type == ACPI_BUTTON_TYPE_LID)
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
302
  		return acpi_lid_send_state(device);
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
303
304
  	return 0;
  }
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
305
306
  static int acpi_button_add(struct acpi_device *device)
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
307
308
  	struct acpi_button *button;
  	struct input_dev *input;
620e112cf   Thomas Renninger   ACPI/PNP: A HID v...
309
310
  	const char *hid = acpi_device_hid(device);
  	char *name, *class;
1bce81131   Bjorn Helgaas   ACPI: button: use...
311
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312

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

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

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

bf04a7722   Bjorn Helgaas   ACPI: button: cac...
354
  	input->name = name;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
355
356
357
  	input->phys = button->phys;
  	input->id.bustype = BUS_HOST;
  	input->id.product = button->type;
3b34e5232   Andrey Borzenkov   ACPI: button: mak...
358
  	input->dev.parent = &device->dev;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
359

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
  	switch (button->type) {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
361
  	case ACPI_BUTTON_TYPE_POWER:
7b19ada2e   Jiri Slaby   get rid of input ...
362
  		input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
363
  		set_bit(KEY_POWER, input->keybit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
365
366
  
  	case ACPI_BUTTON_TYPE_SLEEP:
7b19ada2e   Jiri Slaby   get rid of input ...
367
  		input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
368
  		set_bit(KEY_SLEEP, input->keybit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
370
371
  
  	case ACPI_BUTTON_TYPE_LID:
7b19ada2e   Jiri Slaby   get rid of input ...
372
  		input->evbit[0] = BIT_MASK(EV_SW);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
373
  		set_bit(SW_LID, input->swbit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
375
  		break;
  	}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
376
377
  	error = input_register_device(input);
  	if (error)
373cfc360   Bjorn Helgaas   ACPI: button: use...
378
  		goto err_remove_fs;
7e12715ec   Jesse Barnes   ACPI button: prov...
379
  	if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
380
  		acpi_lid_send_state(device);
7e12715ec   Jesse Barnes   ACPI button: prov...
381
382
383
384
385
386
  		/*
  		 * 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
387
388
389
  
  	if (device->wakeup.flags.valid) {
  		/* Button's GPE is run-wake GPE */
4be44fcd3   Len Brown   [ACPI] Lindent al...
390
  		acpi_enable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
391
  				device->wakeup.gpe_number);
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
392
393
394
395
  		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
396
  	}
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
397
398
  	printk(KERN_INFO PREFIX "%s [%s]
  ", name, acpi_device_bid(device));
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
399
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
401
402
403
404
405
406
407
   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
408
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
409
  static int acpi_button_remove(struct acpi_device *device, int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
411
  	struct acpi_button *button = acpi_driver_data(device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412

9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
413
414
  	if (device->wakeup.flags.valid) {
  		acpi_disable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
415
  				device->wakeup.gpe_number);
c19f9a84e   Rafael J. Wysocki   ACPI / Button: Av...
416
417
  		if (button->wakeup_enabled)
  			device_set_wakeup_enable(&device->dev, false);
9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
418
  	}
4be44fcd3   Len Brown   [ACPI] Lindent al...
419
  	acpi_button_remove_fs(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
420
  	input_unregister_device(button->input);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
  	kfree(button);
d550d98d3   Patrick Mochel   ACPI: delete trac...
422
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
424
  static int __init acpi_button_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  {
912b7427f   Zhang Rui   ACPI button: remo...
426
  	return acpi_bus_register_driver(&acpi_button_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
428
  static void __exit acpi_button_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
  	acpi_bus_unregister_driver(&acpi_button_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
  module_init(acpi_button_init);
  module_exit(acpi_button_exit);