blob: c51a64c49559d59816f6376b224fd830262641e5 [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;
124
Seth Forshee135740d2011-09-20 16:55:49 -0500125 struct mutex mutex;
126};
127
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800128static const struct acpi_device_id toshiba_device_ids[] = {
129 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400130 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800131 {"TOS1900", 0},
132 {"", 0},
133};
134MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
135
Seth Forshee135740d2011-09-20 16:55:49 -0500136static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700137 { KE_KEY, 0x101, { KEY_MUTE } },
138 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
139 { KE_KEY, 0x103, { KEY_ZOOMIN } },
140 { KE_KEY, 0x13b, { KEY_COFFEE } },
141 { KE_KEY, 0x13c, { KEY_BATTERY } },
142 { KE_KEY, 0x13d, { KEY_SLEEP } },
143 { KE_KEY, 0x13e, { KEY_SUSPEND } },
144 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
145 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
146 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
147 { KE_KEY, 0x142, { KEY_WLAN } },
148 { KE_KEY, 0x143, { KEY_PROG1 } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100149 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700150 { KE_KEY, 0xb05, { KEY_PROG2 } },
151 { KE_KEY, 0xb06, { KEY_WWW } },
152 { KE_KEY, 0xb07, { KEY_MAIL } },
153 { KE_KEY, 0xb30, { KEY_STOP } },
154 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
155 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
156 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
157 { KE_KEY, 0xb5a, { KEY_MEDIA } },
158 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* utility
162 */
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 *word = (*word & ~mask) | (mask * value);
167}
168
169/* acpi interface wrappers
170 */
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct acpi_object_list params;
175 union acpi_object in_objs[1];
176 acpi_status status;
177
Ahmed S. Darwishb2b79102007-02-06 16:14:43 -0800178 params.count = ARRAY_SIZE(in_objs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 params.pointer = in_objs;
180 in_objs[0].type = ACPI_TYPE_INTEGER;
181 in_objs[0].integer.value = val;
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500184 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* Perform a raw HCI call. Here we don't care about input or output buffer
188 * format.
189 */
Seth Forshee135740d2011-09-20 16:55:49 -0500190static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
191 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct acpi_object_list params;
194 union acpi_object in_objs[HCI_WORDS];
195 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400196 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 acpi_status status;
198 int i;
199
200 params.count = HCI_WORDS;
201 params.pointer = in_objs;
202 for (i = 0; i < HCI_WORDS; ++i) {
203 in_objs[i].type = ACPI_TYPE_INTEGER;
204 in_objs[i].integer.value = in[i];
205 }
206
207 results.length = sizeof(out_objs);
208 results.pointer = out_objs;
209
Seth Forshee6e02cc72011-09-20 16:55:51 -0500210 status = acpi_evaluate_object(dev->acpi_dev->handle,
211 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400212 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
214 for (i = 0; i < out_objs->package.count; ++i) {
215 out[i] = out_objs->package.elements[i].integer.value;
216 }
217 }
218
219 return status;
220}
221
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400222/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *
224 * In addition to the ACPI status, the HCI system returns a result which
225 * may be useful (such as "not supported").
226 */
227
Seth Forshee135740d2011-09-20 16:55:49 -0500228static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
229 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
232 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500233 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
235 return status;
236}
237
Seth Forshee135740d2011-09-20 16:55:49 -0500238static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
239 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
242 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500243 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *out1 = out[2];
245 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
246 return status;
247}
248
Seth Forshee135740d2011-09-20 16:55:49 -0500249static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
250 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400251{
252 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
253 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500254 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400255 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
256 return status;
257}
258
Seth Forshee135740d2011-09-20 16:55:49 -0500259static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
260 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400261{
262 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
263 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500264 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400265 *out1 = out[2];
266 *out2 = out[3];
267 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
268 return status;
269}
270
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200271/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500272static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200273{
274 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
275 u32 out[HCI_WORDS];
276 acpi_status status;
277
278 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500279 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200280 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700281 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200282 return 0;
283 }
284 in[0] = 0xf400;
Seth Forshee135740d2011-09-20 16:55:49 -0500285 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200286 return 1;
287}
288
289static void toshiba_illumination_set(struct led_classdev *cdev,
290 enum led_brightness brightness)
291{
Seth Forshee135740d2011-09-20 16:55:49 -0500292 struct toshiba_acpi_dev *dev = container_of(cdev,
293 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200294 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
295 u32 out[HCI_WORDS];
296 acpi_status status;
297
298 /* First request : initialize communication. */
299 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500300 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200301 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700302 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200303 return;
304 }
305
306 if (brightness) {
307 /* Switch the illumination on */
308 in[0] = 0xf400;
309 in[1] = 0x14e;
310 in[2] = 1;
Seth Forshee135740d2011-09-20 16:55:49 -0500311 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200312 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700313 pr_info("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200314 return;
315 }
316 } else {
317 /* Switch the illumination off */
318 in[0] = 0xf400;
319 in[1] = 0x14e;
320 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500321 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200322 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700323 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200324 return;
325 }
326 }
327
328 /* Last request : close communication. */
329 in[0] = 0xf200;
330 in[1] = 0;
331 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500332 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200333}
334
335static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
336{
Seth Forshee135740d2011-09-20 16:55:49 -0500337 struct toshiba_acpi_dev *dev = container_of(cdev,
338 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200339 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
340 u32 out[HCI_WORDS];
341 acpi_status status;
342 enum led_brightness result;
343
344 /* First request : initialize communication. */
345 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500346 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200347 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700348 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200349 return LED_OFF;
350 }
351
352 /* Check the illumination */
353 in[0] = 0xf300;
354 in[1] = 0x14e;
Seth Forshee135740d2011-09-20 16:55:49 -0500355 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200356 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700357 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200358 return LED_OFF;
359 }
360
361 result = out[2] ? LED_FULL : LED_OFF;
362
363 /* Last request : close communication. */
364 in[0] = 0xf200;
365 in[1] = 0;
366 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500367 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200368
369 return result;
370}
371
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400372/* Bluetooth rfkill handlers */
373
Seth Forshee135740d2011-09-20 16:55:49 -0500374static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400375{
376 u32 hci_result;
377 u32 value, value2;
378
379 value = 0;
380 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500381 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400382 if (hci_result == HCI_SUCCESS)
383 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
384
385 return hci_result;
386}
387
Seth Forshee135740d2011-09-20 16:55:49 -0500388static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400389{
390 u32 hci_result;
391 u32 value, value2;
392
393 value = 0;
394 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500395 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400396
397 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
398 return hci_result;
399}
400
Johannes Berg19d337d2009-06-02 13:01:37 +0200401static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400402{
Johannes Berg19d337d2009-06-02 13:01:37 +0200403 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400404 u32 result1, result2;
405 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200406 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400407 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400408
Johannes Berg19d337d2009-06-02 13:01:37 +0200409 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400410
411 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500412 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500413 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200414 goto out;
415 }
416
417 if (!radio_state) {
418 err = 0;
419 goto out;
420 }
421
Seth Forshee135740d2011-09-20 16:55:49 -0500422 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
423 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400424
425 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500426 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200427 else
428 err = 0;
429 out:
430 mutex_unlock(&dev->mutex);
431 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400432}
433
Johannes Berg19d337d2009-06-02 13:01:37 +0200434static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400435{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400436 bool new_rfk_state;
437 bool value;
438 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200439 struct toshiba_acpi_dev *dev = data;
440
441 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400442
Seth Forshee135740d2011-09-20 16:55:49 -0500443 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200444 if (hci_result != HCI_SUCCESS) {
445 /* Can't do anything useful */
446 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700447 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200448 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400449
450 new_rfk_state = value;
451
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400452 mutex_unlock(&dev->mutex);
453
Johannes Berg19d337d2009-06-02 13:01:37 +0200454 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
455 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400456}
457
Johannes Berg19d337d2009-06-02 13:01:37 +0200458static const struct rfkill_ops toshiba_rfk_ops = {
459 .set_block = bt_rfkill_set_block,
460 .poll = bt_rfkill_poll,
461};
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Holger Machtc9263552006-10-20 14:30:29 -0700465static int get_lcd(struct backlight_device *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Seth Forshee135740d2011-09-20 16:55:49 -0500467 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 u32 hci_result;
469 u32 value;
470
Seth Forshee135740d2011-09-20 16:55:49 -0500471 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500472 if (hci_result == HCI_SUCCESS)
Holger Machtc9263552006-10-20 14:30:29 -0700473 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500474
475 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700476}
477
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800478static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700479{
Seth Forshee135740d2011-09-20 16:55:49 -0500480 struct toshiba_acpi_dev *dev = m->private;
481 int value;
Holger Machtc9263552006-10-20 14:30:29 -0700482
Seth Forshee135740d2011-09-20 16:55:49 -0500483 if (!dev->backlight_dev)
484 return -ENODEV;
485
486 value = get_lcd(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700487 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800488 seq_printf(m, "brightness: %d\n", value);
489 seq_printf(m, "brightness_levels: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400490 HCI_LCD_BRIGHTNESS_LEVELS);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500494 pr_err("Error reading LCD brightness\n");
495 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800496}
497
498static int lcd_proc_open(struct inode *inode, struct file *file)
499{
Seth Forshee135740d2011-09-20 16:55:49 -0500500 return single_open(file, lcd_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Seth Forshee135740d2011-09-20 16:55:49 -0500503static int set_lcd(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700504{
505 u32 hci_result;
506
507 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Seth Forshee135740d2011-09-20 16:55:49 -0500508 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500509 return hci_result == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700510}
511
512static int set_lcd_status(struct backlight_device *bd)
513{
Seth Forshee135740d2011-09-20 16:55:49 -0500514 struct toshiba_acpi_dev *dev = bl_get_data(bd);
515 return set_lcd(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700516}
517
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800518static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
519 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Seth Forshee135740d2011-09-20 16:55:49 -0500521 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800522 char cmd[42];
523 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800527 len = min(count, sizeof(cmd) - 1);
528 if (copy_from_user(cmd, buf, len))
529 return -EFAULT;
530 cmd[len] = '\0';
531
532 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800533 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500534 ret = set_lcd(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800535 if (ret == 0)
536 ret = count;
537 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700538 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800539 }
Holger Machtc9263552006-10-20 14:30:29 -0700540 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800543static const struct file_operations lcd_proc_fops = {
544 .owner = THIS_MODULE,
545 .open = lcd_proc_open,
546 .read = seq_read,
547 .llseek = seq_lseek,
548 .release = single_release,
549 .write = lcd_proc_write,
550};
551
Seth Forshee36d03f92011-09-20 16:55:53 -0500552static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
553{
554 u32 hci_result;
555
556 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
557 return hci_result == HCI_SUCCESS ? 0 : -EIO;
558}
559
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800560static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Seth Forshee135740d2011-09-20 16:55:49 -0500562 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -0500564 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Seth Forshee36d03f92011-09-20 16:55:53 -0500566 ret = get_video_status(dev, &value);
567 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
569 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400570 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800571 seq_printf(m, "lcd_out: %d\n", is_lcd);
572 seq_printf(m, "crt_out: %d\n", is_crt);
573 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Seth Forshee36d03f92011-09-20 16:55:53 -0500576 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800579static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Seth Forshee135740d2011-09-20 16:55:49 -0500581 return single_open(file, video_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800582}
583
584static ssize_t video_proc_write(struct file *file, const char __user *buf,
585 size_t count, loff_t *pos)
586{
Seth Forshee135740d2011-09-20 16:55:49 -0500587 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800588 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -0500589 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int value;
591 int remain = count;
592 int lcd_out = -1;
593 int crt_out = -1;
594 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +0100595 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800597 cmd = kmalloc(count + 1, GFP_KERNEL);
598 if (!cmd)
599 return -ENOMEM;
600 if (copy_from_user(cmd, buf, count)) {
601 kfree(cmd);
602 return -EFAULT;
603 }
604 cmd[count] = '\0';
605
606 buffer = cmd;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* scan expression. Multiple expressions may be delimited with ;
609 *
610 * NOTE: to keep scanning simple, invalid fields are ignored
611 */
612 while (remain) {
613 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
614 lcd_out = value & 1;
615 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
616 crt_out = value & 1;
617 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
618 tv_out = value & 1;
619 /* advance to one character past the next ; */
620 do {
621 ++buffer;
622 --remain;
623 }
Len Brown4be44fc2005-08-05 00:44:28 -0400624 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800627 kfree(cmd);
628
Seth Forshee36d03f92011-09-20 16:55:53 -0500629 ret = get_video_status(dev, &video_out);
630 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -0700631 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (lcd_out != -1)
633 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
634 if (crt_out != -1)
635 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
636 if (tv_out != -1)
637 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
638 /* To avoid unnecessary video disruption, only write the new
639 * video setting if something changed. */
640 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500641 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500644 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800647static const struct file_operations video_proc_fops = {
648 .owner = THIS_MODULE,
649 .open = video_proc_open,
650 .read = seq_read,
651 .llseek = seq_lseek,
652 .release = single_release,
653 .write = video_proc_write,
654};
655
Seth Forshee36d03f92011-09-20 16:55:53 -0500656static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
657{
658 u32 hci_result;
659
660 hci_read1(dev, HCI_FAN, status, &hci_result);
661 return hci_result == HCI_SUCCESS ? 0 : -EIO;
662}
663
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800664static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Seth Forshee135740d2011-09-20 16:55:49 -0500666 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -0500667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 u32 value;
669
Seth Forshee36d03f92011-09-20 16:55:53 -0500670 ret = get_fan_status(dev, &value);
671 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800672 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -0500673 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Seth Forshee36d03f92011-09-20 16:55:53 -0500676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800679static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Seth Forshee135740d2011-09-20 16:55:49 -0500681 return single_open(file, fan_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800682}
683
684static ssize_t fan_proc_write(struct file *file, const char __user *buf,
685 size_t count, loff_t *pos)
686{
Seth Forshee135740d2011-09-20 16:55:49 -0500687 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800688 char cmd[42];
689 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int value;
691 u32 hci_result;
692
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800693 len = min(count, sizeof(cmd) - 1);
694 if (copy_from_user(cmd, buf, len))
695 return -EFAULT;
696 cmd[len] = '\0';
697
698 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -0400699 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -0500700 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500702 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 else
Seth Forshee135740d2011-09-20 16:55:49 -0500704 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 } else {
706 return -EINVAL;
707 }
708
709 return count;
710}
711
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800712static const struct file_operations fan_proc_fops = {
713 .owner = THIS_MODULE,
714 .open = fan_proc_open,
715 .read = seq_read,
716 .llseek = seq_lseek,
717 .release = single_release,
718 .write = fan_proc_write,
719};
720
721static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Seth Forshee135740d2011-09-20 16:55:49 -0500723 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 u32 hci_result;
725 u32 value;
726
Seth Forshee135740d2011-09-20 16:55:49 -0500727 if (!dev->key_event_valid) {
728 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500730 dev->key_event_valid = 1;
731 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else if (hci_result == HCI_EMPTY) {
733 /* better luck next time */
734 } else if (hci_result == HCI_NOT_SUPPORTED) {
735 /* This is a workaround for an unresolved issue on
736 * some machines where system events sporadically
737 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -0500738 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -0700739 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Joe Perches7e334602011-03-29 15:21:52 -0700741 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500742 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 }
745
Seth Forshee135740d2011-09-20 16:55:49 -0500746 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
747 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800751static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Seth Forshee135740d2011-09-20 16:55:49 -0500753 return single_open(file, keys_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800754}
755
756static ssize_t keys_proc_write(struct file *file, const char __user *buf,
757 size_t count, loff_t *pos)
758{
Seth Forshee135740d2011-09-20 16:55:49 -0500759 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800760 char cmd[42];
761 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int value;
763
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800764 len = min(count, sizeof(cmd) - 1);
765 if (copy_from_user(cmd, buf, len))
766 return -EFAULT;
767 cmd[len] = '\0';
768
769 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -0500770 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else {
772 return -EINVAL;
773 }
774
775 return count;
776}
777
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800778static const struct file_operations keys_proc_fops = {
779 .owner = THIS_MODULE,
780 .open = keys_proc_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = single_release,
784 .write = keys_proc_write,
785};
786
787static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800789 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
790 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800794static int version_proc_open(struct inode *inode, struct file *file)
795{
796 return single_open(file, version_proc_show, PDE(inode)->data);
797}
798
799static const struct file_operations version_proc_fops = {
800 .owner = THIS_MODULE,
801 .open = version_proc_open,
802 .read = seq_read,
803 .llseek = seq_lseek,
804 .release = single_release,
805};
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/* proc and module init
808 */
809
810#define PROC_TOSHIBA "toshiba"
811
Seth Forshee135740d2011-09-20 16:55:49 -0500812static void __devinit
813create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Seth Forshee36d03f92011-09-20 16:55:53 -0500815 if (dev->backlight_dev)
816 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
817 &lcd_proc_fops, dev);
818 if (dev->video_supported)
819 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
820 &video_proc_fops, dev);
821 if (dev->fan_supported)
822 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
823 &fan_proc_fops, dev);
824 if (dev->hotkey_dev)
825 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
826 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -0500827 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
828 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Seth Forshee36d03f92011-09-20 16:55:53 -0500831static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Seth Forshee36d03f92011-09-20 16:55:53 -0500833 if (dev->backlight_dev)
834 remove_proc_entry("lcd", toshiba_proc_dir);
835 if (dev->video_supported)
836 remove_proc_entry("video", toshiba_proc_dir);
837 if (dev->fan_supported)
838 remove_proc_entry("fan", toshiba_proc_dir);
839 if (dev->hotkey_dev)
840 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800841 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Lionel Debrouxacc24722010-11-16 14:14:02 +0100844static const struct backlight_ops toshiba_backlight_data = {
Holger Machtc9263552006-10-20 14:30:29 -0700845 .get_brightness = get_lcd,
846 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -0700847};
848
Seth Forshee6e02cc72011-09-20 16:55:51 -0500849static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500850{
Seth Forshee135740d2011-09-20 16:55:49 -0500851 acpi_status status;
852 int error;
853
Seth Forshee135740d2011-09-20 16:55:49 -0500854 dev->hotkey_dev = input_allocate_device();
855 if (!dev->hotkey_dev) {
856 pr_info("Unable to register input device\n");
857 return -ENOMEM;
858 }
859
860 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -0500861 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -0500862 dev->hotkey_dev->id.bustype = BUS_HOST;
863
864 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
865 if (error)
866 goto err_free_dev;
867
Seth Forshee6e02cc72011-09-20 16:55:51 -0500868 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -0500869 if (ACPI_FAILURE(status)) {
870 pr_info("Unable to enable hotkeys\n");
871 error = -ENODEV;
872 goto err_free_keymap;
873 }
874
875 error = input_register_device(dev->hotkey_dev);
876 if (error) {
877 pr_info("Unable to register input device\n");
878 goto err_free_keymap;
879 }
880
881 return 0;
882
883 err_free_keymap:
884 sparse_keymap_free(dev->hotkey_dev);
885 err_free_dev:
886 input_free_device(dev->hotkey_dev);
887 dev->hotkey_dev = NULL;
888 return error;
889}
890
891static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
892{
893 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
894
Seth Forshee36d03f92011-09-20 16:55:53 -0500895 remove_toshiba_proc_entries(dev);
Seth Forshee135740d2011-09-20 16:55:49 -0500896
897 if (dev->hotkey_dev) {
898 input_unregister_device(dev->hotkey_dev);
899 sparse_keymap_free(dev->hotkey_dev);
900 }
901
902 if (dev->bt_rfk) {
903 rfkill_unregister(dev->bt_rfk);
904 rfkill_destroy(dev->bt_rfk);
905 }
906
907 if (dev->backlight_dev)
908 backlight_device_unregister(dev->backlight_dev);
909
Seth Forshee36d03f92011-09-20 16:55:53 -0500910 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -0500911 led_classdev_unregister(&dev->led_dev);
912
913 kfree(dev);
914
915 return 0;
916}
917
Seth Forsheea540d6b2011-09-20 16:55:52 -0500918static const char * __devinit find_hci_method(acpi_handle handle)
919{
920 acpi_status status;
921 acpi_handle hci_handle;
922
923 status = acpi_get_handle(handle, "GHCI", &hci_handle);
924 if (ACPI_SUCCESS(status))
925 return "GHCI";
926
927 status = acpi_get_handle(handle, "SPFC", &hci_handle);
928 if (ACPI_SUCCESS(status))
929 return "SPFC";
930
931 return NULL;
932}
933
Seth Forshee135740d2011-09-20 16:55:49 -0500934static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
935{
936 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500937 const char *hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500938 u32 hci_result;
Seth Forshee36d03f92011-09-20 16:55:53 -0500939 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -0500940 bool bt_present;
941 int ret = 0;
942 struct backlight_properties props;
943
944 pr_info("Toshiba Laptop ACPI Extras version %s\n",
945 TOSHIBA_ACPI_VERSION);
946
Seth Forsheea540d6b2011-09-20 16:55:52 -0500947 hci_method = find_hci_method(acpi_dev->handle);
948 if (!hci_method) {
949 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -0500950 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500951 }
Seth Forshee6e02cc72011-09-20 16:55:51 -0500952
Seth Forshee135740d2011-09-20 16:55:49 -0500953 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
954 if (!dev)
955 return -ENOMEM;
956 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -0500957 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500958 acpi_dev->driver_data = dev;
959
Seth Forshee6e02cc72011-09-20 16:55:51 -0500960 if (toshiba_acpi_setup_keyboard(dev))
961 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -0500962
963 mutex_init(&dev->mutex);
964
965 /* enable event fifo */
966 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
967
Seth Forshee135740d2011-09-20 16:55:49 -0500968 props.type = BACKLIGHT_PLATFORM;
969 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
970 dev->backlight_dev = backlight_device_register("toshiba",
971 &acpi_dev->dev,
972 dev,
973 &toshiba_backlight_data,
974 &props);
975 if (IS_ERR(dev->backlight_dev)) {
976 ret = PTR_ERR(dev->backlight_dev);
977
978 pr_err("Could not register toshiba backlight device\n");
979 dev->backlight_dev = NULL;
980 goto error;
981 }
982
983 /* Register rfkill switch for Bluetooth */
984 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
985 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
986 &acpi_dev->dev,
987 RFKILL_TYPE_BLUETOOTH,
988 &toshiba_rfk_ops,
989 dev);
990 if (!dev->bt_rfk) {
991 pr_err("unable to allocate rfkill device\n");
992 ret = -ENOMEM;
993 goto error;
994 }
995
996 ret = rfkill_register(dev->bt_rfk);
997 if (ret) {
998 pr_err("unable to register rfkill device\n");
999 rfkill_destroy(dev->bt_rfk);
1000 goto error;
1001 }
1002 }
1003
1004 if (toshiba_illumination_available(dev)) {
1005 dev->led_dev.name = "toshiba::illumination";
1006 dev->led_dev.max_brightness = 1;
1007 dev->led_dev.brightness_set = toshiba_illumination_set;
1008 dev->led_dev.brightness_get = toshiba_illumination_get;
1009 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05001010 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05001011 }
1012
Seth Forshee36d03f92011-09-20 16:55:53 -05001013 /* Determine whether or not BIOS supports fan and video interfaces */
1014
1015 ret = get_video_status(dev, &dummy);
1016 dev->video_supported = !ret;
1017
1018 ret = get_fan_status(dev, &dummy);
1019 dev->fan_supported = !ret;
1020
1021 create_toshiba_proc_entries(dev);
1022
Seth Forshee135740d2011-09-20 16:55:49 -05001023 return 0;
1024
1025error:
1026 toshiba_acpi_remove(acpi_dev, 0);
1027 return ret;
1028}
1029
1030static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1031{
1032 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001033 u32 hci_result, value;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001034
1035 if (event != 0x80)
1036 return;
1037 do {
Seth Forshee135740d2011-09-20 16:55:49 -05001038 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001039 if (hci_result == HCI_SUCCESS) {
1040 if (value == 0x100)
1041 continue;
Frans Popb4663012010-03-01 09:50:46 -05001042 /* act on key press; ignore key release */
1043 if (value & 0x80)
1044 continue;
1045
Seth Forshee135740d2011-09-20 16:55:49 -05001046 if (!sparse_keymap_report_event(dev->hotkey_dev,
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -07001047 value, 1, true)) {
Joe Perches7e334602011-03-29 15:21:52 -07001048 pr_info("Unknown key %x\n",
Frans Popb4663012010-03-01 09:50:46 -05001049 value);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001050 }
1051 } else if (hci_result == HCI_NOT_SUPPORTED) {
1052 /* This is a workaround for an unresolved issue on
1053 * some machines where system events sporadically
1054 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001055 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001056 pr_notice("Re-enabled hotkeys\n");
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001057 }
1058 } while (hci_result != HCI_EMPTY);
1059}
1060
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001061
Seth Forshee135740d2011-09-20 16:55:49 -05001062static struct acpi_driver toshiba_acpi_driver = {
1063 .name = "Toshiba ACPI driver",
1064 .owner = THIS_MODULE,
1065 .ids = toshiba_device_ids,
1066 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1067 .ops = {
1068 .add = toshiba_acpi_add,
1069 .remove = toshiba_acpi_remove,
1070 .notify = toshiba_acpi_notify,
1071 },
1072};
Holger Machtc9263552006-10-20 14:30:29 -07001073
Len Brown4be44fc2005-08-05 00:44:28 -04001074static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Seth Forshee135740d2011-09-20 16:55:49 -05001076 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1079 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05001080 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001081 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Seth Forshee135740d2011-09-20 16:55:49 -05001084 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1085 if (ret) {
1086 pr_err("Failed to register ACPI driver: %d\n", ret);
1087 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07001088 }
1089
Seth Forshee135740d2011-09-20 16:55:49 -05001090 return ret;
1091}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001092
Seth Forshee135740d2011-09-20 16:55:49 -05001093static void __exit toshiba_acpi_exit(void)
1094{
1095 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1096 if (toshiba_proc_dir)
1097 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100module_init(toshiba_acpi_init);
1101module_exit(toshiba_acpi_exit);