blob: 6b281c040bc028c436716f0966d7377d3f745e1b [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 Ducroquet6c3f6e6c2010-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 Ducroquet6c3f6e6c2010-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 */
65#define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
Matthew Garrett6335e4d2010-02-25 15:20:54 -050066#define TOSH_INTERFACE_1 "\\_SB_.VALD"
67#define TOSH_INTERFACE_2 "\\_SB_.VALZ"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
Matthew Garrett6335e4d2010-02-25 15:20:54 -050069#define GHCI_METHOD ".GHCI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* Toshiba HCI interface definitions
72 *
73 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
74 * be uniform across all their models. Ideally we would just call
75 * dedicated ACPI methods instead of using this primitive interface.
76 * However the ACPI methods seem to be incomplete in some areas (for
77 * example they allow setting, but not reading, the LCD brightness value),
78 * so this is still useful.
79 */
80
81#define HCI_WORDS 6
82
83/* operations */
84#define HCI_SET 0xff00
85#define HCI_GET 0xfe00
86
87/* return codes */
88#define HCI_SUCCESS 0x0000
89#define HCI_FAILURE 0x1000
90#define HCI_NOT_SUPPORTED 0x8000
91#define HCI_EMPTY 0x8c00
92
93/* registers */
94#define HCI_FAN 0x0004
95#define HCI_SYSTEM_EVENT 0x0016
96#define HCI_VIDEO_OUT 0x001c
97#define HCI_HOTKEY_EVENT 0x001e
98#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040099#define HCI_WIRELESS 0x0056
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* field definitions */
102#define HCI_LCD_BRIGHTNESS_BITS 3
103#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
104#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
105#define HCI_VIDEO_OUT_LCD 0x1
106#define HCI_VIDEO_OUT_CRT 0x2
107#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400108#define HCI_WIRELESS_KILL_SWITCH 0x01
109#define HCI_WIRELESS_BT_PRESENT 0x0f
110#define HCI_WIRELESS_BT_ATTACH 0x40
111#define HCI_WIRELESS_BT_POWER 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Seth Forshee135740d2011-09-20 16:55:49 -0500113struct toshiba_acpi_dev {
114 struct acpi_device *acpi_dev;
115 const char *method_hci;
116 struct rfkill *bt_rfk;
117 struct input_dev *hotkey_dev;
118 struct backlight_device *backlight_dev;
119 struct led_classdev led_dev;
120 int illumination_installed;
121 int force_fan;
122 int last_key_event;
123 int key_event_valid;
124 acpi_handle handle;
125
126 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 is_valid_acpi_path(const char *methodName)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 acpi_handle handle;
176 acpi_status status;
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178 status = acpi_get_handle(NULL, (char *)methodName, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return !ACPI_FAILURE(status);
180}
181
Len Brown4be44fc2005-08-05 00:44:28 -0400182static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct acpi_object_list params;
185 union acpi_object in_objs[1];
186 acpi_status status;
187
Ahmed S. Darwishb2b79102007-02-06 16:14:43 -0800188 params.count = ARRAY_SIZE(in_objs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 params.pointer = in_objs;
190 in_objs[0].type = ACPI_TYPE_INTEGER;
191 in_objs[0].integer.value = val;
192
Len Brown4be44fc2005-08-05 00:44:28 -0400193 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500194 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* Perform a raw HCI call. Here we don't care about input or output buffer
198 * format.
199 */
Seth Forshee135740d2011-09-20 16:55:49 -0500200static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
201 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct acpi_object_list params;
204 union acpi_object in_objs[HCI_WORDS];
205 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400206 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 acpi_status status;
208 int i;
209
210 params.count = HCI_WORDS;
211 params.pointer = in_objs;
212 for (i = 0; i < HCI_WORDS; ++i) {
213 in_objs[i].type = ACPI_TYPE_INTEGER;
214 in_objs[i].integer.value = in[i];
215 }
216
217 results.length = sizeof(out_objs);
218 results.pointer = out_objs;
219
Seth Forshee135740d2011-09-20 16:55:49 -0500220 status = acpi_evaluate_object(NULL, (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400221 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
223 for (i = 0; i < out_objs->package.count; ++i) {
224 out[i] = out_objs->package.elements[i].integer.value;
225 }
226 }
227
228 return status;
229}
230
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400231/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
233 * In addition to the ACPI status, the HCI system returns a result which
234 * may be useful (such as "not supported").
235 */
236
Seth Forshee135740d2011-09-20 16:55:49 -0500237static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
238 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
241 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500242 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
244 return status;
245}
246
Seth Forshee135740d2011-09-20 16:55:49 -0500247static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
248 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
251 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500252 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *out1 = out[2];
254 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
255 return status;
256}
257
Seth Forshee135740d2011-09-20 16:55:49 -0500258static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
259 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400260{
261 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
262 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500263 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400264 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
265 return status;
266}
267
Seth Forshee135740d2011-09-20 16:55:49 -0500268static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
269 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400270{
271 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
272 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500273 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400274 *out1 = out[2];
275 *out2 = out[3];
276 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
277 return status;
278}
279
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200280/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500281static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200282{
283 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
284 u32 out[HCI_WORDS];
285 acpi_status status;
286
287 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500288 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200289 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700290 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200291 return 0;
292 }
293 in[0] = 0xf400;
Seth Forshee135740d2011-09-20 16:55:49 -0500294 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200295 return 1;
296}
297
298static void toshiba_illumination_set(struct led_classdev *cdev,
299 enum led_brightness brightness)
300{
Seth Forshee135740d2011-09-20 16:55:49 -0500301 struct toshiba_acpi_dev *dev = container_of(cdev,
302 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200303 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
304 u32 out[HCI_WORDS];
305 acpi_status status;
306
307 /* First request : initialize communication. */
308 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500309 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200310 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700311 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200312 return;
313 }
314
315 if (brightness) {
316 /* Switch the illumination on */
317 in[0] = 0xf400;
318 in[1] = 0x14e;
319 in[2] = 1;
Seth Forshee135740d2011-09-20 16:55:49 -0500320 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200321 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700322 pr_info("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200323 return;
324 }
325 } else {
326 /* Switch the illumination off */
327 in[0] = 0xf400;
328 in[1] = 0x14e;
329 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500330 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200331 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700332 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200333 return;
334 }
335 }
336
337 /* Last request : close communication. */
338 in[0] = 0xf200;
339 in[1] = 0;
340 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500341 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200342}
343
344static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
345{
Seth Forshee135740d2011-09-20 16:55:49 -0500346 struct toshiba_acpi_dev *dev = container_of(cdev,
347 struct toshiba_acpi_dev, led_dev);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200348 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
349 u32 out[HCI_WORDS];
350 acpi_status status;
351 enum led_brightness result;
352
353 /* First request : initialize communication. */
354 in[0] = 0xf100;
Seth Forshee135740d2011-09-20 16:55:49 -0500355 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200356 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700357 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200358 return LED_OFF;
359 }
360
361 /* Check the illumination */
362 in[0] = 0xf300;
363 in[1] = 0x14e;
Seth Forshee135740d2011-09-20 16:55:49 -0500364 status = hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200365 if (ACPI_FAILURE(status)) {
Joe Perches7e334602011-03-29 15:21:52 -0700366 pr_info("ACPI call for illumination failed.\n");
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200367 return LED_OFF;
368 }
369
370 result = out[2] ? LED_FULL : LED_OFF;
371
372 /* Last request : close communication. */
373 in[0] = 0xf200;
374 in[1] = 0;
375 in[2] = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500376 hci_raw(dev, in, out);
Pierre Ducroquet6c3f6e6c2010-07-29 11:56:59 +0200377
378 return result;
379}
380
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400381/* Bluetooth rfkill handlers */
382
Seth Forshee135740d2011-09-20 16:55:49 -0500383static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400384{
385 u32 hci_result;
386 u32 value, value2;
387
388 value = 0;
389 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500390 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400391 if (hci_result == HCI_SUCCESS)
392 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
393
394 return hci_result;
395}
396
Seth Forshee135740d2011-09-20 16:55:49 -0500397static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400398{
399 u32 hci_result;
400 u32 value, value2;
401
402 value = 0;
403 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500404 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400405
406 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
407 return hci_result;
408}
409
Johannes Berg19d337d2009-06-02 13:01:37 +0200410static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400411{
Johannes Berg19d337d2009-06-02 13:01:37 +0200412 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400413 u32 result1, result2;
414 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200415 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400416 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400417
Johannes Berg19d337d2009-06-02 13:01:37 +0200418 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400419
420 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500421 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500422 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200423 goto out;
424 }
425
426 if (!radio_state) {
427 err = 0;
428 goto out;
429 }
430
Seth Forshee135740d2011-09-20 16:55:49 -0500431 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
432 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400433
434 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500435 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200436 else
437 err = 0;
438 out:
439 mutex_unlock(&dev->mutex);
440 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400441}
442
Johannes Berg19d337d2009-06-02 13:01:37 +0200443static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400444{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400445 bool new_rfk_state;
446 bool value;
447 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200448 struct toshiba_acpi_dev *dev = data;
449
450 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400451
Seth Forshee135740d2011-09-20 16:55:49 -0500452 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200453 if (hci_result != HCI_SUCCESS) {
454 /* Can't do anything useful */
455 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700456 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200457 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400458
459 new_rfk_state = value;
460
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400461 mutex_unlock(&dev->mutex);
462
Johannes Berg19d337d2009-06-02 13:01:37 +0200463 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
464 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400465}
466
Johannes Berg19d337d2009-06-02 13:01:37 +0200467static const struct rfkill_ops toshiba_rfk_ops = {
468 .set_block = bt_rfkill_set_block,
469 .poll = bt_rfkill_poll,
470};
471
Len Brown4be44fc2005-08-05 00:44:28 -0400472static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Holger Machtc9263552006-10-20 14:30:29 -0700474static int get_lcd(struct backlight_device *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Seth Forshee135740d2011-09-20 16:55:49 -0500476 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 u32 hci_result;
478 u32 value;
479
Seth Forshee135740d2011-09-20 16:55:49 -0500480 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500481 if (hci_result == HCI_SUCCESS)
Holger Machtc9263552006-10-20 14:30:29 -0700482 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500483
484 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700485}
486
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800487static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700488{
Seth Forshee135740d2011-09-20 16:55:49 -0500489 struct toshiba_acpi_dev *dev = m->private;
490 int value;
Holger Machtc9263552006-10-20 14:30:29 -0700491
Seth Forshee135740d2011-09-20 16:55:49 -0500492 if (!dev->backlight_dev)
493 return -ENODEV;
494
495 value = get_lcd(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700496 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800497 seq_printf(m, "brightness: %d\n", value);
498 seq_printf(m, "brightness_levels: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400499 HCI_LCD_BRIGHTNESS_LEVELS);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500503 pr_err("Error reading LCD brightness\n");
504 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800505}
506
507static int lcd_proc_open(struct inode *inode, struct file *file)
508{
Seth Forshee135740d2011-09-20 16:55:49 -0500509 return single_open(file, lcd_proc_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Seth Forshee135740d2011-09-20 16:55:49 -0500512static int set_lcd(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700513{
514 u32 hci_result;
515
516 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Seth Forshee135740d2011-09-20 16:55:49 -0500517 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500518 return hci_result == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700519}
520
521static int set_lcd_status(struct backlight_device *bd)
522{
Seth Forshee135740d2011-09-20 16:55:49 -0500523 struct toshiba_acpi_dev *dev = bl_get_data(bd);
524 return set_lcd(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700525}
526
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800527static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
528 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Seth Forshee135740d2011-09-20 16:55:49 -0500530 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800531 char cmd[42];
532 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800534 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800536 len = min(count, sizeof(cmd) - 1);
537 if (copy_from_user(cmd, buf, len))
538 return -EFAULT;
539 cmd[len] = '\0';
540
541 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800542 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500543 ret = set_lcd(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800544 if (ret == 0)
545 ret = count;
546 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700547 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800548 }
Holger Machtc9263552006-10-20 14:30:29 -0700549 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800552static const struct file_operations lcd_proc_fops = {
553 .owner = THIS_MODULE,
554 .open = lcd_proc_open,
555 .read = seq_read,
556 .llseek = seq_lseek,
557 .release = single_release,
558 .write = lcd_proc_write,
559};
560
561static 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 hci_result;
565 u32 value;
566
Seth Forshee135740d2011-09-20 16:55:49 -0500567 hci_read1(dev, HCI_VIDEO_OUT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (hci_result == HCI_SUCCESS) {
569 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);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500578 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800581static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Seth Forshee135740d2011-09-20 16:55:49 -0500583 return single_open(file, video_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800584}
585
586static ssize_t video_proc_write(struct file *file, const char __user *buf,
587 size_t count, loff_t *pos)
588{
Seth Forshee135740d2011-09-20 16:55:49 -0500589 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800590 char *cmd, *buffer;
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500591 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int value;
593 int remain = count;
594 int lcd_out = -1;
595 int crt_out = -1;
596 int tv_out = -1;
597 u32 hci_result;
Al Virob4482a42007-10-14 19:35:40 +0100598 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800600 cmd = kmalloc(count + 1, GFP_KERNEL);
601 if (!cmd)
602 return -ENOMEM;
603 if (copy_from_user(cmd, buf, count)) {
604 kfree(cmd);
605 return -EFAULT;
606 }
607 cmd[count] = '\0';
608
609 buffer = cmd;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* scan expression. Multiple expressions may be delimited with ;
612 *
613 * NOTE: to keep scanning simple, invalid fields are ignored
614 */
615 while (remain) {
616 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
617 lcd_out = value & 1;
618 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
619 crt_out = value & 1;
620 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
621 tv_out = value & 1;
622 /* advance to one character past the next ; */
623 do {
624 ++buffer;
625 --remain;
626 }
Len Brown4be44fc2005-08-05 00:44:28 -0400627 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800630 kfree(cmd);
631
Seth Forshee135740d2011-09-20 16:55:49 -0500632 hci_read1(dev, HCI_VIDEO_OUT, &video_out, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (hci_result == HCI_SUCCESS) {
Harvey Harrison9e113e02008-09-22 14:37:29 -0700634 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (lcd_out != -1)
636 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
637 if (crt_out != -1)
638 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
639 if (tv_out != -1)
640 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
641 /* To avoid unnecessary video disruption, only write the new
642 * video setting if something changed. */
643 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500644 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 } else {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500646 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500649 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800652static const struct file_operations video_proc_fops = {
653 .owner = THIS_MODULE,
654 .open = video_proc_open,
655 .read = seq_read,
656 .llseek = seq_lseek,
657 .release = single_release,
658 .write = video_proc_write,
659};
660
661static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Seth Forshee135740d2011-09-20 16:55:49 -0500663 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 u32 hci_result;
665 u32 value;
666
Seth Forshee135740d2011-09-20 16:55:49 -0500667 hci_read1(dev, HCI_FAN, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (hci_result == HCI_SUCCESS) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800669 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -0500670 seq_printf(m, "force_on: %d\n", dev->force_fan);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500674 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800677static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Seth Forshee135740d2011-09-20 16:55:49 -0500679 return single_open(file, fan_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800680}
681
682static ssize_t fan_proc_write(struct file *file, const char __user *buf,
683 size_t count, loff_t *pos)
684{
Seth Forshee135740d2011-09-20 16:55:49 -0500685 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800686 char cmd[42];
687 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int value;
689 u32 hci_result;
690
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800691 len = min(count, sizeof(cmd) - 1);
692 if (copy_from_user(cmd, buf, len))
693 return -EFAULT;
694 cmd[len] = '\0';
695
696 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -0400697 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -0500698 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500700 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 else
Seth Forshee135740d2011-09-20 16:55:49 -0500702 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 } else {
704 return -EINVAL;
705 }
706
707 return count;
708}
709
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800710static const struct file_operations fan_proc_fops = {
711 .owner = THIS_MODULE,
712 .open = fan_proc_open,
713 .read = seq_read,
714 .llseek = seq_lseek,
715 .release = single_release,
716 .write = fan_proc_write,
717};
718
719static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Seth Forshee135740d2011-09-20 16:55:49 -0500721 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 u32 hci_result;
723 u32 value;
724
Seth Forshee135740d2011-09-20 16:55:49 -0500725 if (!dev->key_event_valid) {
726 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -0500728 dev->key_event_valid = 1;
729 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else if (hci_result == HCI_EMPTY) {
731 /* better luck next time */
732 } else if (hci_result == HCI_NOT_SUPPORTED) {
733 /* This is a workaround for an unresolved issue on
734 * some machines where system events sporadically
735 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -0500736 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -0700737 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 } else {
Joe Perches7e334602011-03-29 15:21:52 -0700739 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500740 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 }
743
Seth Forshee135740d2011-09-20 16:55:49 -0500744 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
745 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800749static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Seth Forshee135740d2011-09-20 16:55:49 -0500751 return single_open(file, keys_proc_show, PDE(inode)->data);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800752}
753
754static ssize_t keys_proc_write(struct file *file, const char __user *buf,
755 size_t count, loff_t *pos)
756{
Seth Forshee135740d2011-09-20 16:55:49 -0500757 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800758 char cmd[42];
759 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int value;
761
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800762 len = min(count, sizeof(cmd) - 1);
763 if (copy_from_user(cmd, buf, len))
764 return -EFAULT;
765 cmd[len] = '\0';
766
767 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -0500768 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 } else {
770 return -EINVAL;
771 }
772
773 return count;
774}
775
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800776static const struct file_operations keys_proc_fops = {
777 .owner = THIS_MODULE,
778 .open = keys_proc_open,
779 .read = seq_read,
780 .llseek = seq_lseek,
781 .release = single_release,
782 .write = keys_proc_write,
783};
784
785static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800787 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
788 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800792static int version_proc_open(struct inode *inode, struct file *file)
793{
794 return single_open(file, version_proc_show, PDE(inode)->data);
795}
796
797static const struct file_operations version_proc_fops = {
798 .owner = THIS_MODULE,
799 .open = version_proc_open,
800 .read = seq_read,
801 .llseek = seq_lseek,
802 .release = single_release,
803};
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/* proc and module init
806 */
807
808#define PROC_TOSHIBA "toshiba"
809
Seth Forshee135740d2011-09-20 16:55:49 -0500810static void __devinit
811create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Seth Forshee135740d2011-09-20 16:55:49 -0500813 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
814 &lcd_proc_fops, dev);
815 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
816 &video_proc_fops, dev);
817 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
818 &fan_proc_fops, dev);
819 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
820 &keys_proc_fops, dev);
821 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
822 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Axel Linf8ef3ae2010-07-20 15:19:51 -0700825static void remove_toshiba_proc_entries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800827 remove_proc_entry("lcd", toshiba_proc_dir);
828 remove_proc_entry("video", toshiba_proc_dir);
829 remove_proc_entry("fan", toshiba_proc_dir);
830 remove_proc_entry("keys", toshiba_proc_dir);
831 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Lionel Debrouxacc24722010-11-16 14:14:02 +0100834static const struct backlight_ops toshiba_backlight_data = {
Holger Machtc9263552006-10-20 14:30:29 -0700835 .get_brightness = get_lcd,
836 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -0700837};
838
Seth Forshee135740d2011-09-20 16:55:49 -0500839static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev,
840 char *device_path)
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500841{
Seth Forshee135740d2011-09-20 16:55:49 -0500842 acpi_status status;
843 int error;
844
845 status = acpi_get_handle(NULL, device_path, &dev->handle);
846 if (ACPI_FAILURE(status)) {
847 pr_info("Unable to get notification device\n");
848 return -ENODEV;
849 }
850
851 dev->hotkey_dev = input_allocate_device();
852 if (!dev->hotkey_dev) {
853 pr_info("Unable to register input device\n");
854 return -ENOMEM;
855 }
856
857 dev->hotkey_dev->name = "Toshiba input device";
858 dev->hotkey_dev->phys = device_path;
859 dev->hotkey_dev->id.bustype = BUS_HOST;
860
861 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
862 if (error)
863 goto err_free_dev;
864
865 status = acpi_evaluate_object(dev->handle, "ENAB", NULL, NULL);
866 if (ACPI_FAILURE(status)) {
867 pr_info("Unable to enable hotkeys\n");
868 error = -ENODEV;
869 goto err_free_keymap;
870 }
871
872 error = input_register_device(dev->hotkey_dev);
873 if (error) {
874 pr_info("Unable to register input device\n");
875 goto err_free_keymap;
876 }
877
878 return 0;
879
880 err_free_keymap:
881 sparse_keymap_free(dev->hotkey_dev);
882 err_free_dev:
883 input_free_device(dev->hotkey_dev);
884 dev->hotkey_dev = NULL;
885 return error;
886}
887
888static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
889{
890 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
891
892 remove_toshiba_proc_entries();
893
894 if (dev->hotkey_dev) {
895 input_unregister_device(dev->hotkey_dev);
896 sparse_keymap_free(dev->hotkey_dev);
897 }
898
899 if (dev->bt_rfk) {
900 rfkill_unregister(dev->bt_rfk);
901 rfkill_destroy(dev->bt_rfk);
902 }
903
904 if (dev->backlight_dev)
905 backlight_device_unregister(dev->backlight_dev);
906
907 if (dev->illumination_installed)
908 led_classdev_unregister(&dev->led_dev);
909
910 kfree(dev);
911
912 return 0;
913}
914
915static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
916{
917 struct toshiba_acpi_dev *dev;
918 u32 hci_result;
919 bool bt_present;
920 int ret = 0;
921 struct backlight_properties props;
922
923 pr_info("Toshiba Laptop ACPI Extras version %s\n",
924 TOSHIBA_ACPI_VERSION);
925
926 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
927 if (!dev)
928 return -ENOMEM;
929 dev->acpi_dev = acpi_dev;
930 acpi_dev->driver_data = dev;
931
932 /* simple device detection: look for HCI method */
933 if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
934 dev->method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
935 if (toshiba_acpi_setup_keyboard(dev, TOSH_INTERFACE_1))
936 pr_info("Unable to activate hotkeys\n");
937 } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
938 dev->method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
939 if (toshiba_acpi_setup_keyboard(dev, TOSH_INTERFACE_2))
940 pr_info("Unable to activate hotkeys\n");
941 } else {
942 ret = -ENODEV;
943 goto error;
944 }
945
946 pr_info("HCI method: %s\n", dev->method_hci);
947
948 mutex_init(&dev->mutex);
949
950 /* enable event fifo */
951 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
952
953 create_toshiba_proc_entries(dev);
954
955 props.type = BACKLIGHT_PLATFORM;
956 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
957 dev->backlight_dev = backlight_device_register("toshiba",
958 &acpi_dev->dev,
959 dev,
960 &toshiba_backlight_data,
961 &props);
962 if (IS_ERR(dev->backlight_dev)) {
963 ret = PTR_ERR(dev->backlight_dev);
964
965 pr_err("Could not register toshiba backlight device\n");
966 dev->backlight_dev = NULL;
967 goto error;
968 }
969
970 /* Register rfkill switch for Bluetooth */
971 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
972 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
973 &acpi_dev->dev,
974 RFKILL_TYPE_BLUETOOTH,
975 &toshiba_rfk_ops,
976 dev);
977 if (!dev->bt_rfk) {
978 pr_err("unable to allocate rfkill device\n");
979 ret = -ENOMEM;
980 goto error;
981 }
982
983 ret = rfkill_register(dev->bt_rfk);
984 if (ret) {
985 pr_err("unable to register rfkill device\n");
986 rfkill_destroy(dev->bt_rfk);
987 goto error;
988 }
989 }
990
991 if (toshiba_illumination_available(dev)) {
992 dev->led_dev.name = "toshiba::illumination";
993 dev->led_dev.max_brightness = 1;
994 dev->led_dev.brightness_set = toshiba_illumination_set;
995 dev->led_dev.brightness_get = toshiba_illumination_get;
996 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
997 dev->illumination_installed = 1;
998 }
999
1000 return 0;
1001
1002error:
1003 toshiba_acpi_remove(acpi_dev, 0);
1004 return ret;
1005}
1006
1007static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1008{
1009 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001010 u32 hci_result, value;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001011
1012 if (event != 0x80)
1013 return;
1014 do {
Seth Forshee135740d2011-09-20 16:55:49 -05001015 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001016 if (hci_result == HCI_SUCCESS) {
1017 if (value == 0x100)
1018 continue;
Frans Popb4663012010-03-01 09:50:46 -05001019 /* act on key press; ignore key release */
1020 if (value & 0x80)
1021 continue;
1022
Seth Forshee135740d2011-09-20 16:55:49 -05001023 if (!sparse_keymap_report_event(dev->hotkey_dev,
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -07001024 value, 1, true)) {
Joe Perches7e334602011-03-29 15:21:52 -07001025 pr_info("Unknown key %x\n",
Frans Popb4663012010-03-01 09:50:46 -05001026 value);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001027 }
1028 } else if (hci_result == HCI_NOT_SUPPORTED) {
1029 /* This is a workaround for an unresolved issue on
1030 * some machines where system events sporadically
1031 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001032 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001033 pr_notice("Re-enabled hotkeys\n");
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001034 }
1035 } while (hci_result != HCI_EMPTY);
1036}
1037
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001038
Seth Forshee135740d2011-09-20 16:55:49 -05001039static struct acpi_driver toshiba_acpi_driver = {
1040 .name = "Toshiba ACPI driver",
1041 .owner = THIS_MODULE,
1042 .ids = toshiba_device_ids,
1043 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1044 .ops = {
1045 .add = toshiba_acpi_add,
1046 .remove = toshiba_acpi_remove,
1047 .notify = toshiba_acpi_notify,
1048 },
1049};
Holger Machtc9263552006-10-20 14:30:29 -07001050
Len Brown4be44fc2005-08-05 00:44:28 -04001051static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Seth Forshee135740d2011-09-20 16:55:49 -05001053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1056 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05001057 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001058 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Seth Forshee135740d2011-09-20 16:55:49 -05001061 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1062 if (ret) {
1063 pr_err("Failed to register ACPI driver: %d\n", ret);
1064 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07001065 }
1066
Seth Forshee135740d2011-09-20 16:55:49 -05001067 return ret;
1068}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001069
Seth Forshee135740d2011-09-20 16:55:49 -05001070static void __exit toshiba_acpi_exit(void)
1071{
1072 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1073 if (toshiba_proc_dir)
1074 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077module_init(toshiba_acpi_init);
1078module_exit(toshiba_acpi_exit);