blob: cc629e621e46cb8bec31de16320c7f7ecc9e8fd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02007 * Copyright (C) 2010 Pierre Ducroquet
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 *
24 * The devolpment page for this driver is located at
25 * http://memebeam.org/toys/ToshibaAcpiDriver.
26 *
27 * Credits:
28 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
29 * engineering the Windows drivers
30 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
31 * Rob Miller - TV out and hotkeys help
32 *
33 *
34 * TODO
35 *
36 */
37
Joe Perches7e334602011-03-29 15:21:52 -070038#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040040#define TOSHIBA_ACPI_VERSION "0.19"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define PROC_INTERFACE_VERSION 1
42
43#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/types.h>
47#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080048#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070049#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040050#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050051#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070052#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020053#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Holger Machtc9263552006-10-20 14:30:29 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/uaccess.h>
57
58#include <acpi/acpi_drivers.h>
59
60MODULE_AUTHOR("John Belmonte");
61MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
62MODULE_LICENSE("GPL");
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
66
67/* Toshiba HCI interface definitions
68 *
69 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
70 * be uniform across all their models. Ideally we would just call
71 * dedicated ACPI methods instead of using this primitive interface.
72 * However the ACPI methods seem to be incomplete in some areas (for
73 * example they allow setting, but not reading, the LCD brightness value),
74 * so this is still useful.
75 */
76
77#define HCI_WORDS 6
78
79/* operations */
80#define HCI_SET 0xff00
81#define HCI_GET 0xfe00
82
83/* return codes */
84#define HCI_SUCCESS 0x0000
85#define HCI_FAILURE 0x1000
86#define HCI_NOT_SUPPORTED 0x8000
87#define HCI_EMPTY 0x8c00
88
89/* registers */
90#define HCI_FAN 0x0004
91#define HCI_SYSTEM_EVENT 0x0016
92#define HCI_VIDEO_OUT 0x001c
93#define HCI_HOTKEY_EVENT 0x001e
94#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040095#define HCI_WIRELESS 0x0056
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* field definitions */
98#define HCI_LCD_BRIGHTNESS_BITS 3
99#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
100#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
101#define HCI_VIDEO_OUT_LCD 0x1
102#define HCI_VIDEO_OUT_CRT 0x2
103#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400104#define HCI_WIRELESS_KILL_SWITCH 0x01
105#define HCI_WIRELESS_BT_PRESENT 0x0f
106#define HCI_WIRELESS_BT_ATTACH 0x40
107#define HCI_WIRELESS_BT_POWER 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Seth Forshee135740d2011-09-20 16:55:49 -0500109struct toshiba_acpi_dev {
110 struct acpi_device *acpi_dev;
111 const char *method_hci;
112 struct rfkill *bt_rfk;
113 struct input_dev *hotkey_dev;
114 struct backlight_device *backlight_dev;
115 struct led_classdev led_dev;
116 int illumination_installed;
117 int force_fan;
118 int last_key_event;
119 int key_event_valid;
Seth Forshee135740d2011-09-20 16:55:49 -0500120
121 struct mutex mutex;
122};
123
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800124static const struct acpi_device_id toshiba_device_ids[] = {
125 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400126 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800127 {"TOS1900", 0},
128 {"", 0},
129};
130MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
131
Seth Forshee135740d2011-09-20 16:55:49 -0500132static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700133 { KE_KEY, 0x101, { KEY_MUTE } },
134 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
135 { KE_KEY, 0x103, { KEY_ZOOMIN } },
136 { KE_KEY, 0x13b, { KEY_COFFEE } },
137 { KE_KEY, 0x13c, { KEY_BATTERY } },
138 { KE_KEY, 0x13d, { KEY_SLEEP } },
139 { KE_KEY, 0x13e, { KEY_SUSPEND } },
140 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
141 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
142 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
143 { KE_KEY, 0x142, { KEY_WLAN } },
144 { KE_KEY, 0x143, { KEY_PROG1 } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100145 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700146 { KE_KEY, 0xb05, { KEY_PROG2 } },
147 { KE_KEY, 0xb06, { KEY_WWW } },
148 { KE_KEY, 0xb07, { KEY_MAIL } },
149 { KE_KEY, 0xb30, { KEY_STOP } },
150 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
151 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
152 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
153 { KE_KEY, 0xb5a, { KEY_MEDIA } },
154 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500155};
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* utility
158 */
159
Len Brown4be44fc2005-08-05 00:44:28 -0400160static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 *word = (*word & ~mask) | (mask * value);
163}
164
165/* acpi interface wrappers
166 */
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct acpi_object_list params;
171 union acpi_object in_objs[1];
172 acpi_status status;
173
Ahmed S. Darwishb2b79102007-02-06 16:14:43 -0800174 params.count = ARRAY_SIZE(in_objs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 params.pointer = in_objs;
176 in_objs[0].type = ACPI_TYPE_INTEGER;
177 in_objs[0].integer.value = val;
178
Len Brown4be44fc2005-08-05 00:44:28 -0400179 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500180 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Perform a raw HCI call. Here we don't care about input or output buffer
184 * format.
185 */
Seth Forshee135740d2011-09-20 16:55:49 -0500186static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
187 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct acpi_object_list params;
190 union acpi_object in_objs[HCI_WORDS];
191 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 acpi_status status;
194 int i;
195
196 params.count = HCI_WORDS;
197 params.pointer = in_objs;
198 for (i = 0; i < HCI_WORDS; ++i) {
199 in_objs[i].type = ACPI_TYPE_INTEGER;
200 in_objs[i].integer.value = in[i];
201 }
202
203 results.length = sizeof(out_objs);
204 results.pointer = out_objs;
205
Seth Forshee6e02cc72011-09-20 16:55:51 -0500206 status = acpi_evaluate_object(dev->acpi_dev->handle,
207 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400208 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
210 for (i = 0; i < out_objs->package.count; ++i) {
211 out[i] = out_objs->package.elements[i].integer.value;
212 }
213 }
214
215 return status;
216}
217
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400218/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
220 * In addition to the ACPI status, the HCI system returns a result which
221 * may be useful (such as "not supported").
222 */
223
Seth Forshee135740d2011-09-20 16:55:49 -0500224static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
225 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
228 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500229 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
231 return status;
232}
233
Seth Forshee135740d2011-09-20 16:55:49 -0500234static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
235 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
238 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500239 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *out1 = out[2];
241 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
242 return status;
243}
244
Seth Forshee135740d2011-09-20 16:55:49 -0500245static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
246 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400247{
248 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
249 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500250 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400251 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
252 return status;
253}
254
Seth Forshee135740d2011-09-20 16:55:49 -0500255static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
256 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400257{
258 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
259 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500260 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400261 *out1 = out[2];
262 *out2 = out[3];
263 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
264 return status;
265}
266
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200267/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500268static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200269{
270 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
271 u32 out[HCI_WORDS];
272 acpi_status status;
273
274 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500275 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200276 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700277 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200278 return 0;
279 }
280 in[0] = 0xf400;
Seth Forshee135740d2011-09-20 16:55:49 -0500281 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200282 return 1;
283}
284
285static void toshiba_illumination_set(struct led_classdev *cdev,
286 enum led_brightness brightness)
287{
Seth Forshee135740d2011-09-20 16:55:49 -0500288 struct toshiba_acpi_dev *dev = container_of(cdev,
289 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200290 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
291 u32 out[HCI_WORDS];
292 acpi_status status;
293
294 /* First request : initialize communication. */
295 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500296 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200297 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700298 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200299 return;
300 }
301
302 if (brightness) {
303 /* Switch the illumination on */
304 in[0] = 0xf400;
305 in[1] = 0x14e;
306 in[2] = 1;
Seth Forshee135740d2011-09-20 16:55:49 -0500307 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200308 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700309 pr_info("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200310 return;
311 }
312 } else {
313 /* Switch the illumination off */
314 in[0] = 0xf400;
315 in[1] = 0x14e;
316 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500317 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200318 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700319 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200320 return;
321 }
322 }
323
324 /* Last request : close communication. */
325 in[0] = 0xf200;
326 in[1] = 0;
327 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500328 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200329}
330
331static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
332{
Seth Forshee135740d2011-09-20 16:55:49 -0500333 struct toshiba_acpi_dev *dev = container_of(cdev,
334 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200335 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
336 u32 out[HCI_WORDS];
337 acpi_status status;
338 enum led_brightness result;
339
340 /* First request : initialize communication. */
341 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500342 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200343 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700344 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200345 return LED_OFF;
346 }
347
348 /* Check the illumination */
349 in[0] = 0xf300;
350 in[1] = 0x14e;
Seth Forshee135740d2011-09-20 16:55:49 -0500351 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200352 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700353 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200354 return LED_OFF;
355 }
356
357 result = out[2] ? LED_FULL : LED_OFF;
358
359 /* Last request : close communication. */
360 in[0] = 0xf200;
361 in[1] = 0;
362 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500363 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200364
365 return result;
366}
367
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400368/* Bluetooth rfkill handlers */
369
Seth Forshee135740d2011-09-20 16:55:49 -0500370static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400371{
372 u32 hci_result;
373 u32 value, value2;
374
375 value = 0;
376 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500377 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400378 if (hci_result == HCI_SUCCESS)
379 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
380
381 return hci_result;
382}
383
Seth Forshee135740d2011-09-20 16:55:49 -0500384static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400385{
386 u32 hci_result;
387 u32 value, value2;
388
389 value = 0;
390 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500391 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400392
393 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
394 return hci_result;
395}
396
Johannes Berg19d337d2009-06-02 13:01:37 +0200397static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400398{
Johannes Berg19d337d2009-06-02 13:01:37 +0200399 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400400 u32 result1, result2;
401 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200402 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400403 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400404
Johannes Berg19d337d2009-06-02 13:01:37 +0200405 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400406
407 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500408 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500409 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200410 goto out;
411 }
412
413 if (!radio_state) {
414 err = 0;
415 goto out;
416 }
417
Seth Forshee135740d2011-09-20 16:55:49 -0500418 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
419 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400420
421 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500422 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200423 else
424 err = 0;
425 out:
426 mutex_unlock(&dev->mutex);
427 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400428}
429
Johannes Berg19d337d2009-06-02 13:01:37 +0200430static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400431{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400432 bool new_rfk_state;
433 bool value;
434 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200435 struct toshiba_acpi_dev *dev = data;
436
437 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400438
Seth Forshee135740d2011-09-20 16:55:49 -0500439 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200440 if (hci_result != HCI_SUCCESS) {
441 /* Can't do anything useful */
442 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700443 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200444 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400445
446 new_rfk_state = value;
447
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400448 mutex_unlock(&dev->mutex);
449
Johannes Berg19d337d2009-06-02 13:01:37 +0200450 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
451 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400452}
453
Johannes Berg19d337d2009-06-02 13:01:37 +0200454static const struct rfkill_ops toshiba_rfk_ops = {
455 .set_block = bt_rfkill_set_block,
456 .poll = bt_rfkill_poll,
457};
458
Len Brown4be44fc2005-08-05 00:44:28 -0400459static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Holger Machtc9263552006-10-20 14:30:29 -0700461static int get_lcd(struct backlight_device *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Seth Forshee135740d2011-09-20 16:55:49 -0500463 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 u32 hci_result;
465 u32 value;
466
Seth Forshee135740d2011-09-20 16:55:49 -0500467 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500468 if (hci_result == HCI_SUCCESS)
Holger Machtc9263552006-10-20 14:30:29 -0700469 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500470
471 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700472}
473
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800474static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700475{
Seth Forshee135740d2011-09-20 16:55:49 -0500476 struct toshiba_acpi_dev *dev = m->private;
477 int value;
Holger Machtc9263552006-10-20 14:30:29 -0700478
Seth Forshee135740d2011-09-20 16:55:49 -0500479 if (!dev->backlight_dev)
480 return -ENODEV;
481
482 value = get_lcd(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700483 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800484 seq_printf(m, "brightness: %d\n", value);
485 seq_printf(m, "brightness_levels: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400486 HCI_LCD_BRIGHTNESS_LEVELS);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500490 pr_err("Error reading LCD brightness\n");
491 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800492}
493
494static int lcd_proc_open(struct inode *inode, struct file *file)
495{
Seth Forshee135740d2011-09-20 16:55:49 -0500496 return single_open(file, lcd_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Seth Forshee135740d2011-09-20 16:55:49 -0500499static int set_lcd(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700500{
501 u32 hci_result;
502
503 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Seth Forshee135740d2011-09-20 16:55:49 -0500504 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500505 return hci_result == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700506}
507
508static int set_lcd_status(struct backlight_device *bd)
509{
Seth Forshee135740d2011-09-20 16:55:49 -0500510 struct toshiba_acpi_dev *dev = bl_get_data(bd);
511 return set_lcd(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700512}
513
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800514static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
515 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Seth Forshee135740d2011-09-20 16:55:49 -0500517 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800518 char cmd[42];
519 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800521 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800523 len = min(count, sizeof(cmd) - 1);
524 if (copy_from_user(cmd, buf, len))
525 return -EFAULT;
526 cmd[len] = '\0';
527
528 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800529 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500530 ret = set_lcd(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800531 if (ret == 0)
532 ret = count;
533 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700534 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800535 }
Holger Machtc9263552006-10-20 14:30:29 -0700536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800539static const struct file_operations lcd_proc_fops = {
540 .owner = THIS_MODULE,
541 .open = lcd_proc_open,
542 .read = seq_read,
543 .llseek = seq_lseek,
544 .release = single_release,
545 .write = lcd_proc_write,
546};
547
548static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Seth Forshee135740d2011-09-20 16:55:49 -0500550 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 u32 hci_result;
552 u32 value;
553
Seth Forshee135740d2011-09-20 16:55:49 -0500554 hci_read1(dev, HCI_VIDEO_OUT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (hci_result == HCI_SUCCESS) {
556 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
557 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400558 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800559 seq_printf(m, "lcd_out: %d\n", is_lcd);
560 seq_printf(m, "crt_out: %d\n", is_crt);
561 seq_printf(m, "tv_out: %d\n", is_tv);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500565 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800568static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Seth Forshee135740d2011-09-20 16:55:49 -0500570 return single_open(file, video_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800571}
572
573static ssize_t video_proc_write(struct file *file, const char __user *buf,
574 size_t count, loff_t *pos)
575{
Seth Forshee135740d2011-09-20 16:55:49 -0500576 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800577 char *cmd, *buffer;
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500578 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int value;
580 int remain = count;
581 int lcd_out = -1;
582 int crt_out = -1;
583 int tv_out = -1;
584 u32 hci_result;
Al Virob4482a42007-10-14 19:35:40 +0100585 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800587 cmd = kmalloc(count + 1, GFP_KERNEL);
588 if (!cmd)
589 return -ENOMEM;
590 if (copy_from_user(cmd, buf, count)) {
591 kfree(cmd);
592 return -EFAULT;
593 }
594 cmd[count] = '\0';
595
596 buffer = cmd;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* scan expression. Multiple expressions may be delimited with ;
599 *
600 * NOTE: to keep scanning simple, invalid fields are ignored
601 */
602 while (remain) {
603 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
604 lcd_out = value & 1;
605 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
606 crt_out = value & 1;
607 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
608 tv_out = value & 1;
609 /* advance to one character past the next ; */
610 do {
611 ++buffer;
612 --remain;
613 }
Len Brown4be44fc2005-08-05 00:44:28 -0400614 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800617 kfree(cmd);
618
Seth Forshee135740d2011-09-20 16:55:49 -0500619 hci_read1(dev, HCI_VIDEO_OUT, &video_out, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (hci_result == HCI_SUCCESS) {
Harvey Harrison9e113e02008-09-22 14:37:29 -0700621 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (lcd_out != -1)
623 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
624 if (crt_out != -1)
625 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
626 if (tv_out != -1)
627 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
628 /* To avoid unnecessary video disruption, only write the new
629 * video setting if something changed. */
630 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500631 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 } else {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500633 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500636 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800639static const struct file_operations video_proc_fops = {
640 .owner = THIS_MODULE,
641 .open = video_proc_open,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = single_release,
645 .write = video_proc_write,
646};
647
648static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Seth Forshee135740d2011-09-20 16:55:49 -0500650 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 u32 hci_result;
652 u32 value;
653
Seth Forshee135740d2011-09-20 16:55:49 -0500654 hci_read1(dev, HCI_FAN, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (hci_result == HCI_SUCCESS) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800656 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -0500657 seq_printf(m, "force_on: %d\n", dev->force_fan);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500661 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800664static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Seth Forshee135740d2011-09-20 16:55:49 -0500666 return single_open(file, fan_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800667}
668
669static ssize_t fan_proc_write(struct file *file, const char __user *buf,
670 size_t count, loff_t *pos)
671{
Seth Forshee135740d2011-09-20 16:55:49 -0500672 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800673 char cmd[42];
674 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int value;
676 u32 hci_result;
677
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800678 len = min(count, sizeof(cmd) - 1);
679 if (copy_from_user(cmd, buf, len))
680 return -EFAULT;
681 cmd[len] = '\0';
682
683 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -0400684 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -0500685 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500687 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 else
Seth Forshee135740d2011-09-20 16:55:49 -0500689 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else {
691 return -EINVAL;
692 }
693
694 return count;
695}
696
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800697static const struct file_operations fan_proc_fops = {
698 .owner = THIS_MODULE,
699 .open = fan_proc_open,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703 .write = fan_proc_write,
704};
705
706static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Seth Forshee135740d2011-09-20 16:55:49 -0500708 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 u32 hci_result;
710 u32 value;
711
Seth Forshee135740d2011-09-20 16:55:49 -0500712 if (!dev->key_event_valid) {
713 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500715 dev->key_event_valid = 1;
716 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 } else if (hci_result == HCI_EMPTY) {
718 /* better luck next time */
719 } else if (hci_result == HCI_NOT_SUPPORTED) {
720 /* This is a workaround for an unresolved issue on
721 * some machines where system events sporadically
722 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -0500723 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -0700724 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
Joe Perches7e334602011-03-29 15:21:52 -0700726 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500727 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 }
730
Seth Forshee135740d2011-09-20 16:55:49 -0500731 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
732 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800736static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Seth Forshee135740d2011-09-20 16:55:49 -0500738 return single_open(file, keys_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800739}
740
741static ssize_t keys_proc_write(struct file *file, const char __user *buf,
742 size_t count, loff_t *pos)
743{
Seth Forshee135740d2011-09-20 16:55:49 -0500744 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800745 char cmd[42];
746 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int value;
748
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800749 len = min(count, sizeof(cmd) - 1);
750 if (copy_from_user(cmd, buf, len))
751 return -EFAULT;
752 cmd[len] = '\0';
753
754 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -0500755 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else {
757 return -EINVAL;
758 }
759
760 return count;
761}
762
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800763static const struct file_operations keys_proc_fops = {
764 .owner = THIS_MODULE,
765 .open = keys_proc_open,
766 .read = seq_read,
767 .llseek = seq_lseek,
768 .release = single_release,
769 .write = keys_proc_write,
770};
771
772static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800774 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
775 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800779static int version_proc_open(struct inode *inode, struct file *file)
780{
781 return single_open(file, version_proc_show, PDE(inode)->data);
782}
783
784static const struct file_operations version_proc_fops = {
785 .owner = THIS_MODULE,
786 .open = version_proc_open,
787 .read = seq_read,
788 .llseek = seq_lseek,
789 .release = single_release,
790};
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/* proc and module init
793 */
794
795#define PROC_TOSHIBA "toshiba"
796
Seth Forshee135740d2011-09-20 16:55:49 -0500797static void __devinit
798create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Seth Forshee135740d2011-09-20 16:55:49 -0500800 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
801 &lcd_proc_fops, dev);
802 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
803 &video_proc_fops, dev);
804 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
805 &fan_proc_fops, dev);
806 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
807 &keys_proc_fops, dev);
808 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
809 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Axel Linf8ef3ae2010-07-20 15:19:51 -0700812static void remove_toshiba_proc_entries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800814 remove_proc_entry("lcd", toshiba_proc_dir);
815 remove_proc_entry("video", toshiba_proc_dir);
816 remove_proc_entry("fan", toshiba_proc_dir);
817 remove_proc_entry("keys", toshiba_proc_dir);
818 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Lionel Debrouxacc24722010-11-16 14:14:02 +0100821static const struct backlight_ops toshiba_backlight_data = {
Holger Machtc9263552006-10-20 14:30:29 -0700822 .get_brightness = get_lcd,
823 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -0700824};
825
Seth Forshee6e02cc72011-09-20 16:55:51 -0500826static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500827{
Seth Forshee135740d2011-09-20 16:55:49 -0500828 acpi_status status;
829 int error;
830
Seth Forshee135740d2011-09-20 16:55:49 -0500831 dev->hotkey_dev = input_allocate_device();
832 if (!dev->hotkey_dev) {
833 pr_info("Unable to register input device\n");
834 return -ENOMEM;
835 }
836
837 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -0500838 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -0500839 dev->hotkey_dev->id.bustype = BUS_HOST;
840
841 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
842 if (error)
843 goto err_free_dev;
844
Seth Forshee6e02cc72011-09-20 16:55:51 -0500845 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -0500846 if (ACPI_FAILURE(status)) {
847 pr_info("Unable to enable hotkeys\n");
848 error = -ENODEV;
849 goto err_free_keymap;
850 }
851
852 error = input_register_device(dev->hotkey_dev);
853 if (error) {
854 pr_info("Unable to register input device\n");
855 goto err_free_keymap;
856 }
857
858 return 0;
859
860 err_free_keymap:
861 sparse_keymap_free(dev->hotkey_dev);
862 err_free_dev:
863 input_free_device(dev->hotkey_dev);
864 dev->hotkey_dev = NULL;
865 return error;
866}
867
868static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
869{
870 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
871
872 remove_toshiba_proc_entries();
873
874 if (dev->hotkey_dev) {
875 input_unregister_device(dev->hotkey_dev);
876 sparse_keymap_free(dev->hotkey_dev);
877 }
878
879 if (dev->bt_rfk) {
880 rfkill_unregister(dev->bt_rfk);
881 rfkill_destroy(dev->bt_rfk);
882 }
883
884 if (dev->backlight_dev)
885 backlight_device_unregister(dev->backlight_dev);
886
887 if (dev->illumination_installed)
888 led_classdev_unregister(&dev->led_dev);
889
890 kfree(dev);
891
892 return 0;
893}
894
Seth Forsheea540d6b2011-09-20 16:55:52 -0500895static const char * __devinit find_hci_method(acpi_handle handle)
896{
897 acpi_status status;
898 acpi_handle hci_handle;
899
900 status = acpi_get_handle(handle, "GHCI", &hci_handle);
901 if (ACPI_SUCCESS(status))
902 return "GHCI";
903
904 status = acpi_get_handle(handle, "SPFC", &hci_handle);
905 if (ACPI_SUCCESS(status))
906 return "SPFC";
907
908 return NULL;
909}
910
Seth Forshee135740d2011-09-20 16:55:49 -0500911static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
912{
913 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500914 const char *hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500915 u32 hci_result;
916 bool bt_present;
917 int ret = 0;
918 struct backlight_properties props;
919
920 pr_info("Toshiba Laptop ACPI Extras version %s\n",
921 TOSHIBA_ACPI_VERSION);
922
Seth Forsheea540d6b2011-09-20 16:55:52 -0500923 hci_method = find_hci_method(acpi_dev->handle);
924 if (!hci_method) {
925 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -0500926 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500927 }
Seth Forshee6e02cc72011-09-20 16:55:51 -0500928
Seth Forshee135740d2011-09-20 16:55:49 -0500929 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
930 if (!dev)
931 return -ENOMEM;
932 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500933 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500934 acpi_dev->driver_data = dev;
935
Seth Forshee6e02cc72011-09-20 16:55:51 -0500936 if (toshiba_acpi_setup_keyboard(dev))
937 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -0500938
939 mutex_init(&dev->mutex);
940
941 /* enable event fifo */
942 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
943
944 create_toshiba_proc_entries(dev);
945
946 props.type = BACKLIGHT_PLATFORM;
947 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
948 dev->backlight_dev = backlight_device_register("toshiba",
949 &acpi_dev->dev,
950 dev,
951 &toshiba_backlight_data,
952 &props);
953 if (IS_ERR(dev->backlight_dev)) {
954 ret = PTR_ERR(dev->backlight_dev);
955
956 pr_err("Could not register toshiba backlight device\n");
957 dev->backlight_dev = NULL;
958 goto error;
959 }
960
961 /* Register rfkill switch for Bluetooth */
962 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
963 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
964 &acpi_dev->dev,
965 RFKILL_TYPE_BLUETOOTH,
966 &toshiba_rfk_ops,
967 dev);
968 if (!dev->bt_rfk) {
969 pr_err("unable to allocate rfkill device\n");
970 ret = -ENOMEM;
971 goto error;
972 }
973
974 ret = rfkill_register(dev->bt_rfk);
975 if (ret) {
976 pr_err("unable to register rfkill device\n");
977 rfkill_destroy(dev->bt_rfk);
978 goto error;
979 }
980 }
981
982 if (toshiba_illumination_available(dev)) {
983 dev->led_dev.name = "toshiba::illumination";
984 dev->led_dev.max_brightness = 1;
985 dev->led_dev.brightness_set = toshiba_illumination_set;
986 dev->led_dev.brightness_get = toshiba_illumination_get;
987 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
988 dev->illumination_installed = 1;
989 }
990
991 return 0;
992
993error:
994 toshiba_acpi_remove(acpi_dev, 0);
995 return ret;
996}
997
998static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
999{
1000 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001001 u32 hci_result, value;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001002
1003 if (event != 0x80)
1004 return;
1005 do {
Seth Forshee135740d2011-09-20 16:55:49 -05001006 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001007 if (hci_result == HCI_SUCCESS) {
1008 if (value == 0x100)
1009 continue;
Frans Popb4663012010-03-01 09:50:46 -05001010 /* act on key press; ignore key release */
1011 if (value & 0x80)
1012 continue;
1013
Seth Forshee135740d2011-09-20 16:55:49 -05001014 if (!sparse_keymap_report_event(dev->hotkey_dev,
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -07001015 value, 1, true)) {
Joe Perches7e334602011-03-29 15:21:52 -07001016 pr_info("Unknown key %x\n",
Frans Popb4663012010-03-01 09:50:46 -05001017 value);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001018 }
1019 } else if (hci_result == HCI_NOT_SUPPORTED) {
1020 /* This is a workaround for an unresolved issue on
1021 * some machines where system events sporadically
1022 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001023 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001024 pr_notice("Re-enabled hotkeys\n");
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001025 }
1026 } while (hci_result != HCI_EMPTY);
1027}
1028
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001029
Seth Forshee135740d2011-09-20 16:55:49 -05001030static struct acpi_driver toshiba_acpi_driver = {
1031 .name = "Toshiba ACPI driver",
1032 .owner = THIS_MODULE,
1033 .ids = toshiba_device_ids,
1034 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1035 .ops = {
1036 .add = toshiba_acpi_add,
1037 .remove = toshiba_acpi_remove,
1038 .notify = toshiba_acpi_notify,
1039 },
1040};
Holger Machtc9263552006-10-20 14:30:29 -07001041
Len Brown4be44fc2005-08-05 00:44:28 -04001042static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Seth Forshee135740d2011-09-20 16:55:49 -05001044 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1047 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05001048 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001049 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Seth Forshee135740d2011-09-20 16:55:49 -05001052 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1053 if (ret) {
1054 pr_err("Failed to register ACPI driver: %d\n", ret);
1055 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07001056 }
1057
Seth Forshee135740d2011-09-20 16:55:49 -05001058 return ret;
1059}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001060
Seth Forshee135740d2011-09-20 16:55:49 -05001061static void __exit toshiba_acpi_exit(void)
1062{
1063 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1064 if (toshiba_proc_dir)
1065 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068module_init(toshiba_acpi_init);
1069module_exit(toshiba_acpi_exit);