blob: dcdc1f4a4624d782d35f3776d1a69bbfdf983cdb [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;
Seth Forshee36d03f92011-09-20 16:55:53 -0500116
Seth Forshee135740d2011-09-20 16:55:49 -0500117 int force_fan;
118 int last_key_event;
119 int key_event_valid;
Seth Forshee135740d2011-09-20 16:55:49 -0500120
Seth Forshee36d03f92011-09-20 16:55:53 -0500121 int illumination_supported:1;
122 int video_supported:1;
123 int fan_supported:1;
Seth Forshee11948b92011-11-16 17:37:45 -0600124 int system_event_supported:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500125
Seth Forshee135740d2011-09-20 16:55:49 -0500126 struct mutex mutex;
127};
128
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800129static const struct acpi_device_id toshiba_device_ids[] = {
130 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400131 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800132 {"TOS1900", 0},
133 {"", 0},
134};
135MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
136
Seth Forshee135740d2011-09-20 16:55:49 -0500137static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700138 { KE_KEY, 0x101, { KEY_MUTE } },
139 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
140 { KE_KEY, 0x103, { KEY_ZOOMIN } },
141 { KE_KEY, 0x13b, { KEY_COFFEE } },
142 { KE_KEY, 0x13c, { KEY_BATTERY } },
143 { KE_KEY, 0x13d, { KEY_SLEEP } },
144 { KE_KEY, 0x13e, { KEY_SUSPEND } },
145 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
146 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
147 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
148 { KE_KEY, 0x142, { KEY_WLAN } },
149 { KE_KEY, 0x143, { KEY_PROG1 } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100150 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700151 { KE_KEY, 0xb05, { KEY_PROG2 } },
152 { KE_KEY, 0xb06, { KEY_WWW } },
153 { KE_KEY, 0xb07, { KEY_MAIL } },
154 { KE_KEY, 0xb30, { KEY_STOP } },
155 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
156 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
157 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
158 { KE_KEY, 0xb5a, { KEY_MEDIA } },
159 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500160};
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* utility
163 */
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 *word = (*word & ~mask) | (mask * value);
168}
169
170/* acpi interface wrappers
171 */
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct acpi_object_list params;
176 union acpi_object in_objs[1];
177 acpi_status status;
178
Ahmed S. Darwishb2b79102007-02-06 16:14:43 -0800179 params.count = ARRAY_SIZE(in_objs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 params.pointer = in_objs;
181 in_objs[0].type = ACPI_TYPE_INTEGER;
182 in_objs[0].integer.value = val;
183
Len Brown4be44fc2005-08-05 00:44:28 -0400184 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500185 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* Perform a raw HCI call. Here we don't care about input or output buffer
189 * format.
190 */
Seth Forshee135740d2011-09-20 16:55:49 -0500191static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
192 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct acpi_object_list params;
195 union acpi_object in_objs[HCI_WORDS];
196 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400197 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 acpi_status status;
199 int i;
200
201 params.count = HCI_WORDS;
202 params.pointer = in_objs;
203 for (i = 0; i < HCI_WORDS; ++i) {
204 in_objs[i].type = ACPI_TYPE_INTEGER;
205 in_objs[i].integer.value = in[i];
206 }
207
208 results.length = sizeof(out_objs);
209 results.pointer = out_objs;
210
Seth Forshee6e02cc72011-09-20 16:55:51 -0500211 status = acpi_evaluate_object(dev->acpi_dev->handle,
212 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
215 for (i = 0; i < out_objs->package.count; ++i) {
216 out[i] = out_objs->package.elements[i].integer.value;
217 }
218 }
219
220 return status;
221}
222
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400223/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * In addition to the ACPI status, the HCI system returns a result which
226 * may be useful (such as "not supported").
227 */
228
Seth Forshee135740d2011-09-20 16:55:49 -0500229static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
230 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
233 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500234 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
236 return status;
237}
238
Seth Forshee135740d2011-09-20 16:55:49 -0500239static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
240 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
243 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500244 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *out1 = out[2];
246 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
247 return status;
248}
249
Seth Forshee135740d2011-09-20 16:55:49 -0500250static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
251 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400252{
253 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
254 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500255 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400256 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
257 return status;
258}
259
Seth Forshee135740d2011-09-20 16:55:49 -0500260static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
261 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400262{
263 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
264 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500265 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400266 *out1 = out[2];
267 *out2 = out[3];
268 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
269 return status;
270}
271
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200272/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500273static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200274{
275 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
276 u32 out[HCI_WORDS];
277 acpi_status status;
278
279 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500280 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200281 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700282 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200283 return 0;
284 }
285 in[0] = 0xf400;
Seth Forshee135740d2011-09-20 16:55:49 -0500286 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200287 return 1;
288}
289
290static void toshiba_illumination_set(struct led_classdev *cdev,
291 enum led_brightness brightness)
292{
Seth Forshee135740d2011-09-20 16:55:49 -0500293 struct toshiba_acpi_dev *dev = container_of(cdev,
294 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200295 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
296 u32 out[HCI_WORDS];
297 acpi_status status;
298
299 /* First request : initialize communication. */
300 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500301 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200302 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700303 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200304 return;
305 }
306
307 if (brightness) {
308 /* Switch the illumination on */
309 in[0] = 0xf400;
310 in[1] = 0x14e;
311 in[2] = 1;
Seth Forshee135740d2011-09-20 16:55:49 -0500312 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200313 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700314 pr_info("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200315 return;
316 }
317 } else {
318 /* Switch the illumination off */
319 in[0] = 0xf400;
320 in[1] = 0x14e;
321 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500322 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200323 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700324 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200325 return;
326 }
327 }
328
329 /* Last request : close communication. */
330 in[0] = 0xf200;
331 in[1] = 0;
332 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500333 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200334}
335
336static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
337{
Seth Forshee135740d2011-09-20 16:55:49 -0500338 struct toshiba_acpi_dev *dev = container_of(cdev,
339 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200340 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
341 u32 out[HCI_WORDS];
342 acpi_status status;
343 enum led_brightness result;
344
345 /* First request : initialize communication. */
346 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500347 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200348 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700349 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200350 return LED_OFF;
351 }
352
353 /* Check the illumination */
354 in[0] = 0xf300;
355 in[1] = 0x14e;
Seth Forshee135740d2011-09-20 16:55:49 -0500356 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200357 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700358 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200359 return LED_OFF;
360 }
361
362 result = out[2] ? LED_FULL : LED_OFF;
363
364 /* Last request : close communication. */
365 in[0] = 0xf200;
366 in[1] = 0;
367 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500368 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200369
370 return result;
371}
372
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400373/* Bluetooth rfkill handlers */
374
Seth Forshee135740d2011-09-20 16:55:49 -0500375static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400376{
377 u32 hci_result;
378 u32 value, value2;
379
380 value = 0;
381 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500382 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400383 if (hci_result == HCI_SUCCESS)
384 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
385
386 return hci_result;
387}
388
Seth Forshee135740d2011-09-20 16:55:49 -0500389static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400390{
391 u32 hci_result;
392 u32 value, value2;
393
394 value = 0;
395 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500396 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400397
398 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
399 return hci_result;
400}
401
Johannes Berg19d337d2009-06-02 13:01:37 +0200402static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400403{
Johannes Berg19d337d2009-06-02 13:01:37 +0200404 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400405 u32 result1, result2;
406 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200407 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400408 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400409
Johannes Berg19d337d2009-06-02 13:01:37 +0200410 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400411
412 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500413 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500414 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200415 goto out;
416 }
417
418 if (!radio_state) {
419 err = 0;
420 goto out;
421 }
422
Seth Forshee135740d2011-09-20 16:55:49 -0500423 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
424 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400425
426 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500427 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200428 else
429 err = 0;
430 out:
431 mutex_unlock(&dev->mutex);
432 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400433}
434
Johannes Berg19d337d2009-06-02 13:01:37 +0200435static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400436{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400437 bool new_rfk_state;
438 bool value;
439 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200440 struct toshiba_acpi_dev *dev = data;
441
442 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400443
Seth Forshee135740d2011-09-20 16:55:49 -0500444 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200445 if (hci_result != HCI_SUCCESS) {
446 /* Can't do anything useful */
447 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700448 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200449 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400450
451 new_rfk_state = value;
452
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400453 mutex_unlock(&dev->mutex);
454
Johannes Berg19d337d2009-06-02 13:01:37 +0200455 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
456 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400457}
458
Johannes Berg19d337d2009-06-02 13:01:37 +0200459static const struct rfkill_ops toshiba_rfk_ops = {
460 .set_block = bt_rfkill_set_block,
461 .poll = bt_rfkill_poll,
462};
463
Len Brown4be44fc2005-08-05 00:44:28 -0400464static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Holger Machtc9263552006-10-20 14:30:29 -0700466static int get_lcd(struct backlight_device *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Seth Forshee135740d2011-09-20 16:55:49 -0500468 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 u32 hci_result;
470 u32 value;
471
Seth Forshee135740d2011-09-20 16:55:49 -0500472 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500473 if (hci_result == HCI_SUCCESS)
Holger Machtc9263552006-10-20 14:30:29 -0700474 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500475
476 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700477}
478
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800479static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700480{
Seth Forshee135740d2011-09-20 16:55:49 -0500481 struct toshiba_acpi_dev *dev = m->private;
482 int value;
Holger Machtc9263552006-10-20 14:30:29 -0700483
Seth Forshee135740d2011-09-20 16:55:49 -0500484 if (!dev->backlight_dev)
485 return -ENODEV;
486
487 value = get_lcd(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700488 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800489 seq_printf(m, "brightness: %d\n", value);
490 seq_printf(m, "brightness_levels: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400491 HCI_LCD_BRIGHTNESS_LEVELS);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500495 pr_err("Error reading LCD brightness\n");
496 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800497}
498
499static int lcd_proc_open(struct inode *inode, struct file *file)
500{
Seth Forshee135740d2011-09-20 16:55:49 -0500501 return single_open(file, lcd_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Seth Forshee135740d2011-09-20 16:55:49 -0500504static int set_lcd(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700505{
506 u32 hci_result;
507
508 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Seth Forshee135740d2011-09-20 16:55:49 -0500509 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500510 return hci_result == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700511}
512
513static int set_lcd_status(struct backlight_device *bd)
514{
Seth Forshee135740d2011-09-20 16:55:49 -0500515 struct toshiba_acpi_dev *dev = bl_get_data(bd);
516 return set_lcd(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700517}
518
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800519static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
520 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Seth Forshee135740d2011-09-20 16:55:49 -0500522 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800523 char cmd[42];
524 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800526 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800528 len = min(count, sizeof(cmd) - 1);
529 if (copy_from_user(cmd, buf, len))
530 return -EFAULT;
531 cmd[len] = '\0';
532
533 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800534 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500535 ret = set_lcd(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800536 if (ret == 0)
537 ret = count;
538 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700539 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800540 }
Holger Machtc9263552006-10-20 14:30:29 -0700541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800544static const struct file_operations lcd_proc_fops = {
545 .owner = THIS_MODULE,
546 .open = lcd_proc_open,
547 .read = seq_read,
548 .llseek = seq_lseek,
549 .release = single_release,
550 .write = lcd_proc_write,
551};
552
Seth Forshee36d03f92011-09-20 16:55:53 -0500553static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
554{
555 u32 hci_result;
556
557 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
558 return hci_result == HCI_SUCCESS ? 0 : -EIO;
559}
560
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800561static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Seth Forshee135740d2011-09-20 16:55:49 -0500563 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -0500565 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Seth Forshee36d03f92011-09-20 16:55:53 -0500567 ret = get_video_status(dev, &value);
568 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
570 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400571 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800572 seq_printf(m, "lcd_out: %d\n", is_lcd);
573 seq_printf(m, "crt_out: %d\n", is_crt);
574 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Seth Forshee36d03f92011-09-20 16:55:53 -0500577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800580static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Seth Forshee135740d2011-09-20 16:55:49 -0500582 return single_open(file, video_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800583}
584
585static ssize_t video_proc_write(struct file *file, const char __user *buf,
586 size_t count, loff_t *pos)
587{
Seth Forshee135740d2011-09-20 16:55:49 -0500588 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800589 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -0500590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int value;
592 int remain = count;
593 int lcd_out = -1;
594 int crt_out = -1;
595 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +0100596 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800598 cmd = kmalloc(count + 1, GFP_KERNEL);
599 if (!cmd)
600 return -ENOMEM;
601 if (copy_from_user(cmd, buf, count)) {
602 kfree(cmd);
603 return -EFAULT;
604 }
605 cmd[count] = '\0';
606
607 buffer = cmd;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* scan expression. Multiple expressions may be delimited with ;
610 *
611 * NOTE: to keep scanning simple, invalid fields are ignored
612 */
613 while (remain) {
614 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
615 lcd_out = value & 1;
616 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
617 crt_out = value & 1;
618 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
619 tv_out = value & 1;
620 /* advance to one character past the next ; */
621 do {
622 ++buffer;
623 --remain;
624 }
Len Brown4be44fc2005-08-05 00:44:28 -0400625 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800628 kfree(cmd);
629
Seth Forshee36d03f92011-09-20 16:55:53 -0500630 ret = get_video_status(dev, &video_out);
631 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -0700632 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (lcd_out != -1)
634 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
635 if (crt_out != -1)
636 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
637 if (tv_out != -1)
638 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
639 /* To avoid unnecessary video disruption, only write the new
640 * video setting if something changed. */
641 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500642 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500645 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800648static const struct file_operations video_proc_fops = {
649 .owner = THIS_MODULE,
650 .open = video_proc_open,
651 .read = seq_read,
652 .llseek = seq_lseek,
653 .release = single_release,
654 .write = video_proc_write,
655};
656
Seth Forshee36d03f92011-09-20 16:55:53 -0500657static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
658{
659 u32 hci_result;
660
661 hci_read1(dev, HCI_FAN, status, &hci_result);
662 return hci_result == HCI_SUCCESS ? 0 : -EIO;
663}
664
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800665static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Seth Forshee135740d2011-09-20 16:55:49 -0500667 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -0500668 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 u32 value;
670
Seth Forshee36d03f92011-09-20 16:55:53 -0500671 ret = get_fan_status(dev, &value);
672 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800673 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -0500674 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Seth Forshee36d03f92011-09-20 16:55:53 -0500677 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800680static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Seth Forshee135740d2011-09-20 16:55:49 -0500682 return single_open(file, fan_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800683}
684
685static ssize_t fan_proc_write(struct file *file, const char __user *buf,
686 size_t count, loff_t *pos)
687{
Seth Forshee135740d2011-09-20 16:55:49 -0500688 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800689 char cmd[42];
690 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int value;
692 u32 hci_result;
693
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800694 len = min(count, sizeof(cmd) - 1);
695 if (copy_from_user(cmd, buf, len))
696 return -EFAULT;
697 cmd[len] = '\0';
698
699 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -0400700 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -0500701 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500703 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else
Seth Forshee135740d2011-09-20 16:55:49 -0500705 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 } else {
707 return -EINVAL;
708 }
709
710 return count;
711}
712
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800713static const struct file_operations fan_proc_fops = {
714 .owner = THIS_MODULE,
715 .open = fan_proc_open,
716 .read = seq_read,
717 .llseek = seq_lseek,
718 .release = single_release,
719 .write = fan_proc_write,
720};
721
722static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Seth Forshee135740d2011-09-20 16:55:49 -0500724 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 u32 hci_result;
726 u32 value;
727
Seth Forshee11948b92011-11-16 17:37:45 -0600728 if (!dev->key_event_valid && dev->system_event_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -0500729 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500731 dev->key_event_valid = 1;
732 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } else if (hci_result == HCI_EMPTY) {
734 /* better luck next time */
735 } else if (hci_result == HCI_NOT_SUPPORTED) {
736 /* This is a workaround for an unresolved issue on
737 * some machines where system events sporadically
738 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -0500739 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -0700740 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
Joe Perches7e334602011-03-29 15:21:52 -0700742 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500743 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 }
746
Seth Forshee135740d2011-09-20 16:55:49 -0500747 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
748 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800752static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Seth Forshee135740d2011-09-20 16:55:49 -0500754 return single_open(file, keys_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800755}
756
757static ssize_t keys_proc_write(struct file *file, const char __user *buf,
758 size_t count, loff_t *pos)
759{
Seth Forshee135740d2011-09-20 16:55:49 -0500760 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800761 char cmd[42];
762 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int value;
764
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800765 len = min(count, sizeof(cmd) - 1);
766 if (copy_from_user(cmd, buf, len))
767 return -EFAULT;
768 cmd[len] = '\0';
769
770 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -0500771 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 } else {
773 return -EINVAL;
774 }
775
776 return count;
777}
778
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800779static const struct file_operations keys_proc_fops = {
780 .owner = THIS_MODULE,
781 .open = keys_proc_open,
782 .read = seq_read,
783 .llseek = seq_lseek,
784 .release = single_release,
785 .write = keys_proc_write,
786};
787
788static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800790 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
791 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800795static int version_proc_open(struct inode *inode, struct file *file)
796{
797 return single_open(file, version_proc_show, PDE(inode)->data);
798}
799
800static const struct file_operations version_proc_fops = {
801 .owner = THIS_MODULE,
802 .open = version_proc_open,
803 .read = seq_read,
804 .llseek = seq_lseek,
805 .release = single_release,
806};
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/* proc and module init
809 */
810
811#define PROC_TOSHIBA "toshiba"
812
Seth Forshee135740d2011-09-20 16:55:49 -0500813static void __devinit
814create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Seth Forshee36d03f92011-09-20 16:55:53 -0500816 if (dev->backlight_dev)
817 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
818 &lcd_proc_fops, dev);
819 if (dev->video_supported)
820 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
821 &video_proc_fops, dev);
822 if (dev->fan_supported)
823 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
824 &fan_proc_fops, dev);
825 if (dev->hotkey_dev)
826 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
827 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -0500828 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
829 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Seth Forshee36d03f92011-09-20 16:55:53 -0500832static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Seth Forshee36d03f92011-09-20 16:55:53 -0500834 if (dev->backlight_dev)
835 remove_proc_entry("lcd", toshiba_proc_dir);
836 if (dev->video_supported)
837 remove_proc_entry("video", toshiba_proc_dir);
838 if (dev->fan_supported)
839 remove_proc_entry("fan", toshiba_proc_dir);
840 if (dev->hotkey_dev)
841 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800842 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Lionel Debrouxacc24722010-11-16 14:14:02 +0100845static const struct backlight_ops toshiba_backlight_data = {
Holger Machtc9263552006-10-20 14:30:29 -0700846 .get_brightness = get_lcd,
847 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -0700848};
849
Seth Forshee6e02cc72011-09-20 16:55:51 -0500850static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500851{
Seth Forshee135740d2011-09-20 16:55:49 -0500852 acpi_status status;
853 int error;
854
Seth Forshee135740d2011-09-20 16:55:49 -0500855 dev->hotkey_dev = input_allocate_device();
856 if (!dev->hotkey_dev) {
857 pr_info("Unable to register input device\n");
858 return -ENOMEM;
859 }
860
861 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -0500862 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -0500863 dev->hotkey_dev->id.bustype = BUS_HOST;
864
865 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
866 if (error)
867 goto err_free_dev;
868
Seth Forshee6e02cc72011-09-20 16:55:51 -0500869 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -0500870 if (ACPI_FAILURE(status)) {
871 pr_info("Unable to enable hotkeys\n");
872 error = -ENODEV;
873 goto err_free_keymap;
874 }
875
876 error = input_register_device(dev->hotkey_dev);
877 if (error) {
878 pr_info("Unable to register input device\n");
879 goto err_free_keymap;
880 }
881
882 return 0;
883
884 err_free_keymap:
885 sparse_keymap_free(dev->hotkey_dev);
886 err_free_dev:
887 input_free_device(dev->hotkey_dev);
888 dev->hotkey_dev = NULL;
889 return error;
890}
891
892static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
893{
894 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
895
Seth Forshee36d03f92011-09-20 16:55:53 -0500896 remove_toshiba_proc_entries(dev);
Seth Forshee135740d2011-09-20 16:55:49 -0500897
898 if (dev->hotkey_dev) {
899 input_unregister_device(dev->hotkey_dev);
900 sparse_keymap_free(dev->hotkey_dev);
901 }
902
903 if (dev->bt_rfk) {
904 rfkill_unregister(dev->bt_rfk);
905 rfkill_destroy(dev->bt_rfk);
906 }
907
908 if (dev->backlight_dev)
909 backlight_device_unregister(dev->backlight_dev);
910
Seth Forshee36d03f92011-09-20 16:55:53 -0500911 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -0500912 led_classdev_unregister(&dev->led_dev);
913
914 kfree(dev);
915
916 return 0;
917}
918
Seth Forsheea540d6b2011-09-20 16:55:52 -0500919static const char * __devinit find_hci_method(acpi_handle handle)
920{
921 acpi_status status;
922 acpi_handle hci_handle;
923
924 status = acpi_get_handle(handle, "GHCI", &hci_handle);
925 if (ACPI_SUCCESS(status))
926 return "GHCI";
927
928 status = acpi_get_handle(handle, "SPFC", &hci_handle);
929 if (ACPI_SUCCESS(status))
930 return "SPFC";
931
932 return NULL;
933}
934
Seth Forshee135740d2011-09-20 16:55:49 -0500935static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
936{
937 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500938 const char *hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500939 u32 hci_result;
Seth Forshee36d03f92011-09-20 16:55:53 -0500940 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -0500941 bool bt_present;
942 int ret = 0;
943 struct backlight_properties props;
944
945 pr_info("Toshiba Laptop ACPI Extras version %s\n",
946 TOSHIBA_ACPI_VERSION);
947
Seth Forsheea540d6b2011-09-20 16:55:52 -0500948 hci_method = find_hci_method(acpi_dev->handle);
949 if (!hci_method) {
950 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -0500951 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500952 }
Seth Forshee6e02cc72011-09-20 16:55:51 -0500953
Seth Forshee135740d2011-09-20 16:55:49 -0500954 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
955 if (!dev)
956 return -ENOMEM;
957 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500958 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500959 acpi_dev->driver_data = dev;
960
Seth Forshee6e02cc72011-09-20 16:55:51 -0500961 if (toshiba_acpi_setup_keyboard(dev))
962 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -0500963
964 mutex_init(&dev->mutex);
965
966 /* enable event fifo */
967 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Seth Forshee11948b92011-11-16 17:37:45 -0600968 if (hci_result == HCI_SUCCESS)
969 dev->system_event_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -0500970
Seth Forshee135740d2011-09-20 16:55:49 -0500971 props.type = BACKLIGHT_PLATFORM;
972 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
973 dev->backlight_dev = backlight_device_register("toshiba",
974 &acpi_dev->dev,
975 dev,
976 &toshiba_backlight_data,
977 &props);
978 if (IS_ERR(dev->backlight_dev)) {
979 ret = PTR_ERR(dev->backlight_dev);
980
981 pr_err("Could not register toshiba backlight device\n");
982 dev->backlight_dev = NULL;
983 goto error;
984 }
Seth Forsheeac2dad82011-09-20 16:55:54 -0500985 dev->backlight_dev->props.brightness = get_lcd(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -0500986
987 /* Register rfkill switch for Bluetooth */
988 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
989 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
990 &acpi_dev->dev,
991 RFKILL_TYPE_BLUETOOTH,
992 &toshiba_rfk_ops,
993 dev);
994 if (!dev->bt_rfk) {
995 pr_err("unable to allocate rfkill device\n");
996 ret = -ENOMEM;
997 goto error;
998 }
999
1000 ret = rfkill_register(dev->bt_rfk);
1001 if (ret) {
1002 pr_err("unable to register rfkill device\n");
1003 rfkill_destroy(dev->bt_rfk);
1004 goto error;
1005 }
1006 }
1007
1008 if (toshiba_illumination_available(dev)) {
1009 dev->led_dev.name = "toshiba::illumination";
1010 dev->led_dev.max_brightness = 1;
1011 dev->led_dev.brightness_set = toshiba_illumination_set;
1012 dev->led_dev.brightness_get = toshiba_illumination_get;
1013 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05001014 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05001015 }
1016
Seth Forshee36d03f92011-09-20 16:55:53 -05001017 /* Determine whether or not BIOS supports fan and video interfaces */
1018
1019 ret = get_video_status(dev, &dummy);
1020 dev->video_supported = !ret;
1021
1022 ret = get_fan_status(dev, &dummy);
1023 dev->fan_supported = !ret;
1024
1025 create_toshiba_proc_entries(dev);
1026
Seth Forshee135740d2011-09-20 16:55:49 -05001027 return 0;
1028
1029error:
1030 toshiba_acpi_remove(acpi_dev, 0);
1031 return ret;
1032}
1033
1034static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1035{
1036 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001037 u32 hci_result, value;
Seth Forshee11948b92011-11-16 17:37:45 -06001038 int retries = 3;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001039
Seth Forshee11948b92011-11-16 17:37:45 -06001040 if (!dev->system_event_supported || event != 0x80)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001041 return;
Seth Forshee11948b92011-11-16 17:37:45 -06001042
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001043 do {
Seth Forshee135740d2011-09-20 16:55:49 -05001044 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Seth Forshee11948b92011-11-16 17:37:45 -06001045 switch (hci_result) {
1046 case HCI_SUCCESS:
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001047 if (value == 0x100)
1048 continue;
Frans Popb4663012010-03-01 09:50:46 -05001049 /* act on key press; ignore key release */
1050 if (value & 0x80)
1051 continue;
1052
Seth Forshee135740d2011-09-20 16:55:49 -05001053 if (!sparse_keymap_report_event(dev->hotkey_dev,
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -07001054 value, 1, true)) {
Joe Perches7e334602011-03-29 15:21:52 -07001055 pr_info("Unknown key %x\n",
Frans Popb4663012010-03-01 09:50:46 -05001056 value);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001057 }
Seth Forshee11948b92011-11-16 17:37:45 -06001058 break;
1059 case HCI_NOT_SUPPORTED:
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001060 /* This is a workaround for an unresolved issue on
1061 * some machines where system events sporadically
1062 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001063 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001064 pr_notice("Re-enabled hotkeys\n");
Seth Forshee11948b92011-11-16 17:37:45 -06001065 /* fall through */
1066 default:
1067 retries--;
1068 break;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001069 }
Seth Forshee11948b92011-11-16 17:37:45 -06001070 } while (retries && hci_result != HCI_EMPTY);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001071}
1072
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001073
Seth Forshee135740d2011-09-20 16:55:49 -05001074static struct acpi_driver toshiba_acpi_driver = {
1075 .name = "Toshiba ACPI driver",
1076 .owner = THIS_MODULE,
1077 .ids = toshiba_device_ids,
1078 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1079 .ops = {
1080 .add = toshiba_acpi_add,
1081 .remove = toshiba_acpi_remove,
1082 .notify = toshiba_acpi_notify,
1083 },
1084};
Holger Machtc9263552006-10-20 14:30:29 -07001085
Len Brown4be44fc2005-08-05 00:44:28 -04001086static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Seth Forshee135740d2011-09-20 16:55:49 -05001088 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1091 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05001092 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001093 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
Seth Forshee135740d2011-09-20 16:55:49 -05001096 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1097 if (ret) {
1098 pr_err("Failed to register ACPI driver: %d\n", ret);
1099 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07001100 }
1101
Seth Forshee135740d2011-09-20 16:55:49 -05001102 return ret;
1103}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001104
Seth Forshee135740d2011-09-20 16:55:49 -05001105static void __exit toshiba_acpi_exit(void)
1106{
1107 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1108 if (toshiba_proc_dir)
1109 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112module_init(toshiba_acpi_init);
1113module_exit(toshiba_acpi_exit);