Blame view

drivers/acpi/button.c 13.1 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);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
77
78
  static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
  static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
  
  static struct acpi_driver acpi_button_driver = {
c2b6705b7   Len Brown   ACPI: fix acpi_dr...
81
  	.name = "button",
4be44fcd3   Len Brown   [ACPI] Lindent al...
82
  	.class = ACPI_BUTTON_CLASS,
1ba90e3a8   Thomas Renninger   ACPI: autoload mo...
83
  	.ids = button_device_ids,
4be44fcd3   Len Brown   [ACPI] Lindent al...
84
85
  	.ops = {
  		.add = acpi_button_add,
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
86
  		.resume = acpi_button_resume,
4be44fcd3   Len Brown   [ACPI] Lindent al...
87
  		.remove = acpi_button_remove,
373cfc360   Bjorn Helgaas   ACPI: button: use...
88
  		.notify = acpi_button_notify,
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
89
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
  };
  
  struct acpi_button {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
93
94
95
  	unsigned int type;
  	struct input_dev *input;
  	char phys[32];			/* for input device */
4be44fcd3   Len Brown   [ACPI] Lindent al...
96
  	unsigned long pushed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  };
d75080328   Arjan van de Ven   ACPI: add 'const'...
98
  static const struct file_operations acpi_button_info_fops = {
cf7acfab0   Denis V. Lunev   acpi: use non-rac...
99
  	.owner = THIS_MODULE,
4be44fcd3   Len Brown   [ACPI] Lindent al...
100
101
102
103
  	.open = acpi_button_info_open_fs,
  	.read = seq_read,
  	.llseek = seq_lseek,
  	.release = single_release,
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
104
  };
d75080328   Arjan van de Ven   ACPI: add 'const'...
105
  static const struct file_operations acpi_button_state_fops = {
cf7acfab0   Denis V. Lunev   acpi: use non-rac...
106
  	.owner = THIS_MODULE,
4be44fcd3   Len Brown   [ACPI] Lindent al...
107
108
109
110
  	.open = acpi_button_state_open_fs,
  	.read = seq_read,
  	.llseek = seq_lseek,
  	.release = single_release,
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
111
  };
4be44fcd3   Len Brown   [ACPI] Lindent al...
112

7e12715ec   Jesse Barnes   ACPI button: prov...
113
114
  static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  static struct acpi_device *lid_device;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
115
116
117
  /* --------------------------------------------------------------------------
                                FS Interface (/proc)
     -------------------------------------------------------------------------- */
4be44fcd3   Len Brown   [ACPI] Lindent al...
118
  static struct proc_dir_entry *acpi_button_dir;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
119
120
121
  
  static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
  {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
122
  	struct acpi_device *device = seq->private;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
123

4be44fcd3   Len Brown   [ACPI] Lindent al...
124
125
  	seq_printf(seq, "type:                    %s
  ",
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
126
  		   acpi_device_name(device));
d550d98d3   Patrick Mochel   ACPI: delete trac...
127
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
128
129
130
131
132
133
  }
  
  static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
  {
  	return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
134

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

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
141
  	status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
142
143
144
145
  	seq_printf(seq, "state:      %s
  ",
  		   ACPI_FAILURE(status) ? "unsupported" :
  			(state ? "open" : "closed"));
d550d98d3   Patrick Mochel   ACPI: delete trac...
146
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
147
148
149
150
151
152
153
154
155
156
  }
  
  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);
  }
  
  static struct proc_dir_entry *acpi_power_dir;
  static struct proc_dir_entry *acpi_sleep_dir;
  static struct proc_dir_entry *acpi_lid_dir;
4be44fcd3   Len Brown   [ACPI] Lindent al...
157
  static int acpi_button_add_fs(struct acpi_device *device)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
158
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
159
  	struct acpi_button *button = acpi_driver_data(device);
4be44fcd3   Len Brown   [ACPI] Lindent al...
160
  	struct proc_dir_entry *entry = NULL;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
161
162
163
  
  	switch (button->type) {
  	case ACPI_BUTTON_TYPE_POWER:
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
164
  		if (!acpi_power_dir)
4be44fcd3   Len Brown   [ACPI] Lindent al...
165
166
  			acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  						    acpi_button_dir);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
167
168
169
  		entry = acpi_power_dir;
  		break;
  	case ACPI_BUTTON_TYPE_SLEEP:
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
170
  		if (!acpi_sleep_dir)
4be44fcd3   Len Brown   [ACPI] Lindent al...
171
172
  			acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  						    acpi_button_dir);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
173
174
175
176
  		entry = acpi_sleep_dir;
  		break;
  	case ACPI_BUTTON_TYPE_LID:
  		if (!acpi_lid_dir)
4be44fcd3   Len Brown   [ACPI] Lindent al...
177
178
  			acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  						  acpi_button_dir);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
179
180
181
182
183
  		entry = acpi_lid_dir;
  		break;
  	}
  
  	if (!entry)
d550d98d3   Patrick Mochel   ACPI: delete trac...
184
  		return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
185
186
187
  
  	acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  	if (!acpi_device_dir(device))
d550d98d3   Patrick Mochel   ACPI: delete trac...
188
  		return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
189
190
  
  	/* 'info' [R] */
cf7acfab0   Denis V. Lunev   acpi: use non-rac...
191
192
  	entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
  				 S_IRUGO, acpi_device_dir(device),
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
193
  				 &acpi_button_info_fops, device);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
194
  	if (!entry)
d550d98d3   Patrick Mochel   ACPI: delete trac...
195
  		return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
196
197
198
  
  	/* show lid state [R] */
  	if (button->type == ACPI_BUTTON_TYPE_LID) {
cf7acfab0   Denis V. Lunev   acpi: use non-rac...
199
200
  		entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  					 S_IRUGO, acpi_device_dir(device),
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
201
  					 &acpi_button_state_fops, device);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
202
  		if (!entry)
a6fc67202   Thomas Renninger   ACPI: Enable ACPI...
203
  			return -ENODEV;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
204
  	}
d550d98d3   Patrick Mochel   ACPI: delete trac...
205
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
206
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
207
  static int acpi_button_remove_fs(struct acpi_device *device)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
208
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
209
  	struct acpi_button *button = acpi_driver_data(device);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
210

b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
211
212
213
  	if (acpi_device_dir(device)) {
  		if (button->type == ACPI_BUTTON_TYPE_LID)
  			remove_proc_entry(ACPI_BUTTON_FILE_STATE,
4be44fcd3   Len Brown   [ACPI] Lindent al...
214
  					  acpi_device_dir(device));
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
215
  		remove_proc_entry(ACPI_BUTTON_FILE_INFO,
4be44fcd3   Len Brown   [ACPI] Lindent al...
216
  				  acpi_device_dir(device));
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
217
218
  
  		remove_proc_entry(acpi_device_bid(device),
4be44fcd3   Len Brown   [ACPI] Lindent al...
219
  				  acpi_device_dir(device)->parent);
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
220
221
  		acpi_device_dir(device) = NULL;
  	}
d550d98d3   Patrick Mochel   ACPI: delete trac...
222
  	return 0;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
223
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
  /* --------------------------------------------------------------------------
                                  Driver Interface
     -------------------------------------------------------------------------- */
7e12715ec   Jesse Barnes   ACPI button: prov...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  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'...
243
244
  	if (!lid_device)
  		return -ENODEV;
7e12715ec   Jesse Barnes   ACPI button: prov...
245
246
247
248
249
250
251
252
  	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...
253
  static int acpi_lid_send_state(struct acpi_device *device)
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
254
  {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
255
  	struct acpi_button *button = acpi_driver_data(device);
27663c585   Matthew Wilcox   ACPI: Change acpi...
256
  	unsigned long long state;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
257
  	acpi_status status;
7e12715ec   Jesse Barnes   ACPI button: prov...
258
  	int ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
259

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

23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
264
265
  	/* input layer checks if event is redundant */
  	input_report_switch(button->input, SW_LID, !state);
df316e939   Guillem Jover   ACPI: Always repo...
266
  	input_sync(button->input);
7e12715ec   Jesse Barnes   ACPI button: prov...
267
268
269
270
271
  
  	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...
272
273
274
275
276
277
278
  	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...
279
  	return ret;
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
280
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
  	switch (event) {
373cfc360   Bjorn Helgaas   ACPI: button: use...
288
289
290
  	case ACPI_FIXED_HARDWARE_EVENT:
  		event = ACPI_BUTTON_NOTIFY_STATUS;
  		/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  	case ACPI_BUTTON_NOTIFY_STATUS:
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
292
  		input = button->input;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
293
  		if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
294
  			acpi_lid_send_state(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
295
296
297
298
299
300
301
  		} 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...
302
  			input_sync(input);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
303
  		}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
304

106c19e7b   Bjorn Helgaas   ACPI: button: rem...
305
  		acpi_bus_generate_proc_event(device, event, ++button->pushed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
  		break;
  	default:
  		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd3   Len Brown   [ACPI] Lindent al...
309
310
  				  "Unsupported event [0x%x]
  ", event));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
  		break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
  }
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
314
315
  static int acpi_button_resume(struct acpi_device *device)
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
316
  	struct acpi_button *button = acpi_driver_data(device);
50a4da890   Bjorn Helgaas   ACPI: button: whi...
317

e2fb9754d   Bjorn Helgaas   ACPI: button: rem...
318
  	if (button->type == ACPI_BUTTON_TYPE_LID)
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
319
  		return acpi_lid_send_state(device);
23de5d9ef   Alexey Starikovskiy   ACPI: button: sen...
320
321
  	return 0;
  }
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
322
323
  static int acpi_button_add(struct acpi_device *device)
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
324
325
  	struct acpi_button *button;
  	struct input_dev *input;
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
326
  	char *hid, *name, *class;
1bce81131   Bjorn Helgaas   ACPI: button: use...
327
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328

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

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

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

bf04a7722   Bjorn Helgaas   ACPI: button: cac...
371
  	input->name = name;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
372
373
374
  	input->phys = button->phys;
  	input->id.bustype = BUS_HOST;
  	input->id.product = button->type;
3b34e5232   Andrey Borzenkov   ACPI: button: mak...
375
  	input->dev.parent = &device->dev;
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
376

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
  	switch (button->type) {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
378
  	case ACPI_BUTTON_TYPE_POWER:
7b19ada2e   Jiri Slaby   get rid of input ...
379
  		input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
380
  		set_bit(KEY_POWER, input->keybit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
382
383
  
  	case ACPI_BUTTON_TYPE_SLEEP:
7b19ada2e   Jiri Slaby   get rid of input ...
384
  		input->evbit[0] = BIT_MASK(EV_KEY);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
385
  		set_bit(KEY_SLEEP, input->keybit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
  		break;
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
387
388
  
  	case ACPI_BUTTON_TYPE_LID:
7b19ada2e   Jiri Slaby   get rid of input ...
389
  		input->evbit[0] = BIT_MASK(EV_SW);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
390
  		set_bit(SW_LID, input->swbit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
  		break;
  	}
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
393
394
  	error = input_register_device(input);
  	if (error)
373cfc360   Bjorn Helgaas   ACPI: button: use...
395
  		goto err_remove_fs;
7e12715ec   Jesse Barnes   ACPI button: prov...
396
  	if (button->type == ACPI_BUTTON_TYPE_LID) {
106c19e7b   Bjorn Helgaas   ACPI: button: rem...
397
  		acpi_lid_send_state(device);
7e12715ec   Jesse Barnes   ACPI button: prov...
398
399
400
401
402
403
  		/*
  		 * 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
404
405
406
  
  	if (device->wakeup.flags.valid) {
  		/* Button's GPE is run-wake GPE */
4be44fcd3   Len Brown   [ACPI] Lindent al...
407
  		acpi_enable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
408
  				device->wakeup.gpe_number);
f517709d6   Rafael J. Wysocki   ACPI / PM: Add mo...
409
  		device->wakeup.run_wake_count++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
411
  		device->wakeup.state.enabled = 1;
  	}
bf04a7722   Bjorn Helgaas   ACPI: button: cac...
412
413
  	printk(KERN_INFO PREFIX "%s [%s]
  ", name, acpi_device_bid(device));
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
414
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415

c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
416
417
418
419
420
421
422
   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
423
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
424
  static int acpi_button_remove(struct acpi_device *device, int type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  {
1bce81131   Bjorn Helgaas   ACPI: button: use...
426
  	struct acpi_button *button = acpi_driver_data(device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427

9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
428
429
  	if (device->wakeup.flags.valid) {
  		acpi_disable_gpe(device->wakeup.gpe_device,
a44061aa8   Rafael J. Wysocki   ACPICA: Remove wa...
430
  				device->wakeup.gpe_number);
f517709d6   Rafael J. Wysocki   ACPI / PM: Add mo...
431
  		device->wakeup.run_wake_count--;
9630bdd9b   Rafael J. Wysocki   ACPI: Use GPE ref...
432
433
  		device->wakeup.state.enabled = 0;
  	}
4be44fcd3   Len Brown   [ACPI] Lindent al...
434
  	acpi_button_remove_fs(device);
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
435
  	input_unregister_device(button->input);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
  	kfree(button);
d550d98d3   Patrick Mochel   ACPI: delete trac...
437
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
439
  static int __init acpi_button_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  {
c0968f0ea   Dmitry Torokhov   ACPI: button: reg...
441
  	int result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442

b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
443
444
  	acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  	if (!acpi_button_dir)
d550d98d3   Patrick Mochel   ACPI: delete trac...
445
  		return -ENODEV;
50a4da890   Bjorn Helgaas   ACPI: button: whi...
446

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
  	result = acpi_bus_register_driver(&acpi_button_driver);
  	if (result < 0) {
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
449
  		remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
d550d98d3   Patrick Mochel   ACPI: delete trac...
450
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
  	}
d550d98d3   Patrick Mochel   ACPI: delete trac...
452
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
  }
4be44fcd3   Len Brown   [ACPI] Lindent al...
454
  static void __exit acpi_button_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
  	acpi_bus_unregister_driver(&acpi_button_driver);
4be44fcd3   Len Brown   [ACPI] Lindent al...
457
  	if (acpi_power_dir)
b34a8030e   Alexey Starikovskiy   [ACPI] restore /p...
458
459
460
461
462
463
  		remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  	if (acpi_sleep_dir)
  		remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  	if (acpi_lid_dir)
  		remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  	remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
  module_init(acpi_button_init);
  module_exit(acpi_button_exit);