blob: 68f1cc0d7fae52629b4e14e1db41743b79b660ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030024#define IBM_VERSION "0.14"
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030025#define TPACPI_SYSFS_VERSION 0x000100
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030029 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020031 *
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030035 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040036 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030045 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040046 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040069 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030085#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020087MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040088MODULE_DESCRIPTION(IBM_DESC);
89MODULE_VERSION(IBM_VERSION);
90MODULE_LICENSE("GPL");
91
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030092/* Please remove this in year 2009 */
93MODULE_ALIAS("ibm_acpi");
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define __unused __attribute__ ((unused))
96
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030097/****************************************************************************
98 ****************************************************************************
99 *
100 * ACPI Helpers and device model
101 *
102 ****************************************************************************
103 ****************************************************************************/
104
105/*************************************************************************
106 * ACPI basic handles
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static acpi_handle root_handle = NULL;
110
111#define IBM_HANDLE(object, parent, paths...) \
112 static acpi_handle object##_handle; \
113 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400114 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 static char *object##_paths[] = { paths }
116
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400117IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
118 "\\_SB.PCI.ISA.EC", /* 570 */
119 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
120 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
121 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
122 "\\_SB.PCI0.ICH3.EC0", /* R31 */
123 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300124 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300126IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
127IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300129
130/*************************************************************************
131 * Misc ACPI handles
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400134IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
135 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
136 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300137 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400138
139IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
140 "^HKEY", /* R30, R31 */
141 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300142 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300145/*************************************************************************
146 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200147 */
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int acpi_evalf(acpi_handle handle,
150 void *res, char *method, char *fmt, ...)
151{
152 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400153 struct acpi_object_list params;
154 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
155 struct acpi_buffer result, *resultp;
156 union acpi_object out_obj;
157 acpi_status status;
158 va_list ap;
159 char res_type;
160 int success;
161 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!*fmt) {
164 printk(IBM_ERR "acpi_evalf() called with empty format\n");
165 return 0;
166 }
167
168 if (*fmt == 'q') {
169 quiet = 1;
170 fmt++;
171 } else
172 quiet = 0;
173
174 res_type = *(fmt++);
175
176 params.count = 0;
177 params.pointer = &in_objs[0];
178
179 va_start(ap, fmt);
180 while (*fmt) {
181 char c = *(fmt++);
182 switch (c) {
183 case 'd': /* int */
184 in_objs[params.count].integer.value = va_arg(ap, int);
185 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
186 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400187 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 default:
189 printk(IBM_ERR "acpi_evalf() called "
190 "with invalid format character '%c'\n", c);
191 return 0;
192 }
193 }
194 va_end(ap);
195
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400196 if (res_type != 'v') {
197 result.length = sizeof(out_obj);
198 result.pointer = &out_obj;
199 resultp = &result;
200 } else
201 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400203 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400206 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (res)
208 *(int *)res = out_obj.integer.value;
209 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
210 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400211 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 success = status == AE_OK;
213 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400214 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 default:
216 printk(IBM_ERR "acpi_evalf() called "
217 "with invalid format character '%c'\n", res_type);
218 return 0;
219 }
220
221 if (!success && !quiet)
222 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
223 method, fmt0, status);
224
225 return success;
226}
227
228static void __unused acpi_print_int(acpi_handle handle, char *method)
229{
230 int i;
231
232 if (acpi_evalf(handle, &i, method, "d"))
233 printk(IBM_INFO "%s = 0x%x\n", method, i);
234 else
235 printk(IBM_ERR "error calling %s\n", method);
236}
237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300238static int acpi_ec_read(int i, u8 * p)
239{
240 int v;
241
242 if (ecrd_handle) {
243 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
244 return 0;
245 *p = v;
246 } else {
247 if (ec_read(i, p) < 0)
248 return 0;
249 }
250
251 return 1;
252}
253
254static int acpi_ec_write(int i, u8 v)
255{
256 if (ecwr_handle) {
257 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
258 return 0;
259 } else {
260 if (ec_write(i, v) < 0)
261 return 0;
262 }
263
264 return 1;
265}
266
267static int _sta(acpi_handle handle)
268{
269 int status;
270
271 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
272 status = 0;
273
274 return status;
275}
276
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300277static int issue_thinkpad_cmos_command(int cmos_cmd)
278{
279 if (!cmos_handle)
280 return -ENXIO;
281
282 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
283 return -EIO;
284
285 return 0;
286}
287
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288/*************************************************************************
289 * ACPI device model
290 */
291
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300292static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300293 acpi_handle *handle, acpi_handle parent,
294 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300295{
296 int i;
297 acpi_status status;
298
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300299 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
300 name);
301
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300302 for (i = 0; i < num_paths; i++) {
303 status = acpi_get_handle(parent, paths[i], handle);
304 if (ACPI_SUCCESS(status)) {
305 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300306 dbg_printk(TPACPI_DBG_INIT,
307 "Found ACPI handle %s for %s\n",
308 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300309 return;
310 }
311 }
312
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300313 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
314 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300315 *handle = NULL;
316}
317
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300318static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300319{
320 struct ibm_struct *ibm = data;
321
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300322 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300323 return;
324
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300325 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300326}
327
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300328static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300329{
330 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300331 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300332
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300333 BUG_ON(!ibm->acpi);
334
335 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300336 return 0;
337
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300338 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300339 "setting up ACPI notify for %s\n", ibm->name);
340
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300341 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
342 if (rc < 0) {
343 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
344 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300345 return -ENODEV;
346 }
347
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300348 acpi_driver_data(ibm->acpi->device) = ibm;
349 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300350 IBM_ACPI_EVENT_PREFIX,
351 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300352
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300353 status = acpi_install_notify_handler(*ibm->acpi->handle,
354 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355 if (ACPI_FAILURE(status)) {
356 if (status == AE_ALREADY_EXISTS) {
357 printk(IBM_NOTICE "another device driver is already handling %s events\n",
358 ibm->name);
359 } else {
360 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
361 ibm->name, status);
362 }
363 return -ENODEV;
364 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300365 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300366 return 0;
367}
368
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300369static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300370{
371 return 0;
372}
373
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300374static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300375{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300376 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300378 dbg_printk(TPACPI_DBG_INIT,
379 "registering %s as an ACPI driver\n", ibm->name);
380
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300381 BUG_ON(!ibm->acpi);
382
383 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
384 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300385 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
386 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387 }
388
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300389 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
390 ibm->acpi->driver->ids = ibm->acpi->hid;
391 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300392
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300393 rc = acpi_bus_register_driver(ibm->acpi->driver);
394 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300395 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300396 ibm->acpi->hid, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300397 kfree(ibm->acpi->driver);
398 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300399 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300400 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300401
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300402 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300403}
404
405
406/****************************************************************************
407 ****************************************************************************
408 *
409 * Procfs Helpers
410 *
411 ****************************************************************************
412 ****************************************************************************/
413
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300414static int dispatch_procfs_read(char *page, char **start, off_t off,
415 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300416{
417 struct ibm_struct *ibm = data;
418 int len;
419
420 if (!ibm || !ibm->read)
421 return -EINVAL;
422
423 len = ibm->read(page);
424 if (len < 0)
425 return len;
426
427 if (len <= off + count)
428 *eof = 1;
429 *start = page + off;
430 len -= off;
431 if (len > count)
432 len = count;
433 if (len < 0)
434 len = 0;
435
436 return len;
437}
438
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300439static int dispatch_procfs_write(struct file *file,
440 const char __user * userbuf,
441 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300442{
443 struct ibm_struct *ibm = data;
444 char *kernbuf;
445 int ret;
446
447 if (!ibm || !ibm->write)
448 return -EINVAL;
449
450 kernbuf = kmalloc(count + 2, GFP_KERNEL);
451 if (!kernbuf)
452 return -ENOMEM;
453
454 if (copy_from_user(kernbuf, userbuf, count)) {
455 kfree(kernbuf);
456 return -EFAULT;
457 }
458
459 kernbuf[count] = 0;
460 strcat(kernbuf, ",");
461 ret = ibm->write(kernbuf);
462 if (ret == 0)
463 ret = count;
464
465 kfree(kernbuf);
466
467 return ret;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static char *next_cmd(char **cmds)
471{
472 char *start = *cmds;
473 char *end;
474
475 while ((end = strchr(start, ',')) && end == start)
476 start = end + 1;
477
478 if (!end)
479 return NULL;
480
481 *end = 0;
482 *cmds = end + 1;
483 return start;
484}
485
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300486
487/****************************************************************************
488 ****************************************************************************
489 *
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300490 * Device model: hwmon and platform
491 *
492 ****************************************************************************
493 ****************************************************************************/
494
495static struct platform_device *tpacpi_pdev = NULL;
496static struct class_device *tpacpi_hwmon = NULL;
497
498static struct platform_driver tpacpi_pdriver = {
499 .driver = {
500 .name = IBM_DRVR_NAME,
501 .owner = THIS_MODULE,
502 },
503};
504
505
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300506/*************************************************************************
507 * thinkpad-acpi driver attributes
508 */
509
510/* interface_version --------------------------------------------------- */
511static ssize_t tpacpi_driver_interface_version_show(
512 struct device_driver *drv,
513 char *buf)
514{
515 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
516}
517
518static DRIVER_ATTR(interface_version, S_IRUGO,
519 tpacpi_driver_interface_version_show, NULL);
520
521/* debug_level --------------------------------------------------------- */
522static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
523 char *buf)
524{
525 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
526}
527
528static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
529 const char *buf, size_t count)
530{
531 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300532
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300533 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300534 return -EINVAL;
535
536 dbg_level = t;
537
538 return count;
539}
540
541static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
542 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
543
544/* version ------------------------------------------------------------- */
545static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
546 char *buf)
547{
548 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
549}
550
551static DRIVER_ATTR(version, S_IRUGO,
552 tpacpi_driver_version_show, NULL);
553
554/* --------------------------------------------------------------------- */
555
556static struct driver_attribute* tpacpi_driver_attributes[] = {
557 &driver_attr_debug_level, &driver_attr_version,
558 &driver_attr_interface_version,
559};
560
561static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
562{
563 int i, res;
564
565 i = 0;
566 res = 0;
567 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
568 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
569 i++;
570 }
571
572 return res;
573}
574
575static void tpacpi_remove_driver_attributes(struct device_driver *drv)
576{
577 int i;
578
579 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
580 driver_remove_file(drv, tpacpi_driver_attributes[i]);
581}
582
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300583/*************************************************************************
584 * sysfs support helpers
585 */
586
587struct attribute_set_obj {
588 struct attribute_set s;
589 struct attribute *a;
590} __attribute__((packed));
591
592static struct attribute_set *create_attr_set(unsigned int max_members,
593 const char* name)
594{
595 struct attribute_set_obj *sobj;
596
597 if (max_members == 0)
598 return NULL;
599
600 /* Allocates space for implicit NULL at the end too */
601 sobj = kzalloc(sizeof(struct attribute_set_obj) +
602 max_members * sizeof(struct attribute *),
603 GFP_KERNEL);
604 if (!sobj)
605 return NULL;
606 sobj->s.max_members = max_members;
607 sobj->s.group.attrs = &sobj->a;
608 sobj->s.group.name = name;
609
610 return &sobj->s;
611}
612
613/* not multi-threaded safe, use it in a single thread per set */
614static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
615{
616 if (!s || !attr)
617 return -EINVAL;
618
619 if (s->members >= s->max_members)
620 return -ENOMEM;
621
622 s->group.attrs[s->members] = attr;
623 s->members++;
624
625 return 0;
626}
627
628static int add_many_to_attr_set(struct attribute_set* s,
629 struct attribute **attr,
630 unsigned int count)
631{
632 int i, res;
633
634 for (i = 0; i < count; i++) {
635 res = add_to_attr_set(s, attr[i]);
636 if (res)
637 return res;
638 }
639
640 return 0;
641}
642
643static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
644{
645 sysfs_remove_group(kobj, &s->group);
646 destroy_attr_set(s);
647}
648
649static int parse_strtoul(const char *buf,
650 unsigned long max, unsigned long *value)
651{
652 char *endp;
653
654 *value = simple_strtoul(buf, &endp, 0);
655 while (*endp && isspace(*endp))
656 endp++;
657 if (*endp || *value > max)
658 return -EINVAL;
659
660 return 0;
661}
662
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300663/****************************************************************************
664 ****************************************************************************
665 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300666 * Subdrivers
667 *
668 ****************************************************************************
669 ****************************************************************************/
670
671/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300672 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673 */
674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300675static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
678 printk(IBM_INFO "%s\n", IBM_URL);
679
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200680 if (ibm_thinkpad_ec_found)
681 printk(IBM_INFO "ThinkPad EC firmware %s\n",
682 ibm_thinkpad_ec_found);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685}
686
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300687static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int len = 0;
690
691 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
692 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
693
694 return len;
695}
696
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300697static struct ibm_struct thinkpad_acpi_driver_data = {
698 .name = "driver",
699 .read = thinkpad_acpi_driver_read,
700};
701
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300702/*************************************************************************
703 * Hotkey subdriver
704 */
705
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400706static int hotkey_orig_status;
707static int hotkey_orig_mask;
708
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300709static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300710{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300711 int res;
712
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300713 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
714
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300715 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300716 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300717
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300718 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300719 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300720
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300721 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300722 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300723
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300724 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
726 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300727 tp_features.hotkey_mask =
728 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300730 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300731 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300732
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300733 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
734 if (res)
735 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300736 }
737
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300738 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300739}
740
741static void hotkey_exit(void)
742{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300743 int res;
744
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300745 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300746 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300747 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
748 if (res)
749 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300750 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751}
752
753static void hotkey_notify(struct ibm_struct *ibm, u32 event)
754{
755 int hkey;
756
757 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300758 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759 else {
760 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300761 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762 }
763}
764
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300765/*
766 * Call with hotkey_mutex held
767 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400768static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300771 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400772
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300773 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300775 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400776
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300780/*
781 * Call with hotkey_mutex held
782 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int i;
786
787 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300788 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300790 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400791 for (i = 0; i < 32; i++) {
792 int bit = ((1 << i) & mask) != 0;
793 if (!acpi_evalf(hkey_handle,
794 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300795 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400796 }
797
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300798 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400799}
800
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400801static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300803 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int len = 0;
805
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300806 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400807 len += sprintf(p + len, "status:\t\tnot supported\n");
808 return len;
809 }
810
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300811 res = mutex_lock_interruptible(&hotkey_mutex);
812 if (res < 0)
813 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300814 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300815 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300816 if (res)
817 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300820 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
822 len += sprintf(p + len,
823 "commands:\tenable, disable, reset, <mask>\n");
824 } else {
825 len += sprintf(p + len, "mask:\t\tnot supported\n");
826 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
827 }
828
829 return len;
830}
831
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400832static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300834 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 char *cmd;
836 int do_cmd = 0;
837
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300838 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENODEV;
840
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300841 res = mutex_lock_interruptible(&hotkey_mutex);
842 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300843 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400844
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300845 res = hotkey_get(&status, &mask);
846 if (res)
847 goto errexit;
848
849 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 while ((cmd = next_cmd(&buf))) {
851 if (strlencmp(cmd, "enable") == 0) {
852 status = 1;
853 } else if (strlencmp(cmd, "disable") == 0) {
854 status = 0;
855 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856 status = hotkey_orig_status;
857 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
859 /* mask set */
860 } else if (sscanf(cmd, "%x", &mask) == 1) {
861 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300862 } else {
863 res = -EINVAL;
864 goto errexit;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 do_cmd = 1;
867 }
868
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300869 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300870 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300872errexit:
873 mutex_unlock(&hotkey_mutex);
874 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300877static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300878 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300879 .notify = hotkey_notify,
880 .handle = &hkey_handle,
881 .type = ACPI_DEVICE_NOTIFY,
882};
883
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300884static struct ibm_struct hotkey_driver_data = {
885 .name = "hotkey",
886 .read = hotkey_read,
887 .write = hotkey_write,
888 .exit = hotkey_exit,
889 .acpi = &ibm_hotkey_acpidriver,
890};
891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300892/*************************************************************************
893 * Bluetooth subdriver
894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300896static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300898 int status = 0;
899
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300900 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
901
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300902 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300903
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400904 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
905 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300906 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300907 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300909 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
910 str_supported(tp_features.bluetooth),
911 status);
912
913 if (tp_features.bluetooth &&
914 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
915 /* no bluetooth hardware present in system */
916 tp_features.bluetooth = 0;
917 dbg_printk(TPACPI_DBG_INIT,
918 "bluetooth hardware not installed\n");
919 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300920
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300921 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300924static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 int status;
927
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300928 if (!tp_features.bluetooth)
929 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300931 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
932 return -EIO;
933
934 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
935}
936
937static int bluetooth_set_radiosw(int radio_on)
938{
939 int status;
940
941 if (!tp_features.bluetooth)
942 return -ENODEV;
943
944 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
945 return -EIO;
946 if (radio_on)
947 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
948 else
949 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
950 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
951 return -EIO;
952
953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400956static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300959 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300961 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300964 len += sprintf(p + len, "status:\t\t%s\n",
965 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 len += sprintf(p + len, "commands:\tenable, disable\n");
967 }
968
969 return len;
970}
971
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400972static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300976 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400977 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 while ((cmd = next_cmd(&buf))) {
980 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300981 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300983 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 } else
985 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989}
990
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300991static struct ibm_struct bluetooth_driver_data = {
992 .name = "bluetooth",
993 .read = bluetooth_read,
994 .write = bluetooth_write,
995};
996
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300997/*************************************************************************
998 * Wan subdriver
999 */
1000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001001static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001002{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001003 int status = 0;
1004
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001005 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1006
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001007 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001008
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001009 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001010 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001011
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001012 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1013 str_supported(tp_features.wan),
1014 status);
1015
1016 if (tp_features.wan &&
1017 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
1018 /* no wan hardware present in system */
1019 tp_features.wan = 0;
1020 dbg_printk(TPACPI_DBG_INIT,
1021 "wan hardware not installed\n");
1022 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001023
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001024 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001025}
1026
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001027static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001028{
1029 int status;
1030
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001031 if (!tp_features.wan)
1032 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001033
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001034 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1035 return -EIO;
1036
1037 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1038}
1039
1040static int wan_set_radiosw(int radio_on)
1041{
1042 int status;
1043
1044 if (!tp_features.wan)
1045 return -ENODEV;
1046
1047 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1048 return -EIO;
1049 if (radio_on)
1050 status |= TP_ACPI_WANCARD_RADIOSSW;
1051 else
1052 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1053 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1054 return -EIO;
1055
1056 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001057}
1058
1059static int wan_read(char *p)
1060{
1061 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001062 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001063
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001064 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001065 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001066 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001067 len += sprintf(p + len, "status:\t\t%s\n",
1068 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001069 len += sprintf(p + len, "commands:\tenable, disable\n");
1070 }
1071
1072 return len;
1073}
1074
1075static int wan_write(char *buf)
1076{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001077 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001078
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001079 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001080 return -ENODEV;
1081
1082 while ((cmd = next_cmd(&buf))) {
1083 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001084 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001085 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001086 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001087 } else
1088 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001089 }
1090
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001091 return 0;
1092}
1093
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001094static struct ibm_struct wan_driver_data = {
1095 .name = "wan",
1096 .read = wan_read,
1097 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001098 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001099};
1100
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001101/*************************************************************************
1102 * Video subdriver
1103 */
1104
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001105static enum video_access_mode video_supported;
1106static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001108IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1109 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1110 "\\_SB.PCI0.VID0", /* 770e */
1111 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1112 "\\_SB.PCI0.AGP.VID", /* all others */
1113 ); /* R30, R31 */
1114
1115IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1116
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001117static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001119 int ivga;
1120
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001121 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1122
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001123 IBM_ACPIHANDLE_INIT(vid);
1124 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001125
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001126 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1127 /* G41, assume IVGA doesn't change */
1128 vid_handle = vid2_handle;
1129
1130 if (!vid_handle)
1131 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001132 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001133 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1134 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001135 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001136 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1137 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001138 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001139 else
1140 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001141 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001143 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1144 str_supported(video_supported != TPACPI_VIDEO_NONE),
1145 video_supported);
1146
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001147 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001150static void video_exit(void)
1151{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001152 dbg_printk(TPACPI_DBG_EXIT,
1153 "restoring original video autoswitch mode\n");
1154 if (video_autosw_set(video_orig_autosw))
1155 printk(IBM_ERR "error while trying to restore original "
1156 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001157}
1158
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001159static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 int status = 0;
1162 int i;
1163
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001164 switch (video_supported) {
1165 case TPACPI_VIDEO_570:
1166 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1167 TP_ACPI_VIDEO_570_PHSCMD))
1168 return -EIO;
1169 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1170 break;
1171 case TPACPI_VIDEO_770:
1172 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1173 return -EIO;
1174 if (i)
1175 status |= TP_ACPI_VIDEO_S_LCD;
1176 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1177 return -EIO;
1178 if (i)
1179 status |= TP_ACPI_VIDEO_S_CRT;
1180 break;
1181 case TPACPI_VIDEO_NEW:
1182 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1183 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1184 return -EIO;
1185 if (i)
1186 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001188 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1189 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1190 return -EIO;
1191 if (i)
1192 status |= TP_ACPI_VIDEO_S_LCD;
1193 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1194 return -EIO;
1195 if (i)
1196 status |= TP_ACPI_VIDEO_S_DVI;
1197 break;
1198 default:
1199 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 return status;
1203}
1204
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001205static int video_outputsw_set(int status)
1206{
1207 int autosw;
1208 int res = 0;
1209
1210 switch (video_supported) {
1211 case TPACPI_VIDEO_570:
1212 res = acpi_evalf(NULL, NULL,
1213 "\\_SB.PHS2", "vdd",
1214 TP_ACPI_VIDEO_570_PHS2CMD,
1215 status | TP_ACPI_VIDEO_570_PHS2SET);
1216 break;
1217 case TPACPI_VIDEO_770:
1218 autosw = video_autosw_get();
1219 if (autosw < 0)
1220 return autosw;
1221
1222 res = video_autosw_set(1);
1223 if (res)
1224 return res;
1225 res = acpi_evalf(vid_handle, NULL,
1226 "ASWT", "vdd", status * 0x100, 0);
1227 if (!autosw && video_autosw_set(autosw)) {
1228 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1229 return -EIO;
1230 }
1231 break;
1232 case TPACPI_VIDEO_NEW:
1233 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1234 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1235 break;
1236 default:
1237 return -ENOSYS;
1238 }
1239
1240 return (res)? 0 : -EIO;
1241}
1242
1243static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001245 int autosw = 0;
1246
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001247 switch (video_supported) {
1248 case TPACPI_VIDEO_570:
1249 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1250 return -EIO;
1251 break;
1252 case TPACPI_VIDEO_770:
1253 case TPACPI_VIDEO_NEW:
1254 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1255 return -EIO;
1256 break;
1257 default:
1258 return -ENOSYS;
1259 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001260
1261 return autosw & 1;
1262}
1263
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001264static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001265{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001266 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001267 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001268 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001269}
1270
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001271static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001272{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001273 int autosw = video_autosw_get();
1274 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001275
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001276 if (autosw < 0)
1277 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001278
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001279 switch (video_supported) {
1280 case TPACPI_VIDEO_570:
1281 res = video_autosw_set(1);
1282 if (res)
1283 return res;
1284 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1285 break;
1286 case TPACPI_VIDEO_770:
1287 case TPACPI_VIDEO_NEW:
1288 res = video_autosw_set(1);
1289 if (res)
1290 return res;
1291 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1292 break;
1293 default:
1294 return -ENOSYS;
1295 }
1296 if (!autosw && video_autosw_set(autosw)) {
1297 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1298 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001299 }
1300
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001301 return (res)? 0 : -EIO;
1302}
1303
1304static int video_expand_toggle(void)
1305{
1306 switch (video_supported) {
1307 case TPACPI_VIDEO_570:
1308 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1309 0 : -EIO;
1310 case TPACPI_VIDEO_770:
1311 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1312 0 : -EIO;
1313 case TPACPI_VIDEO_NEW:
1314 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1315 0 : -EIO;
1316 default:
1317 return -ENOSYS;
1318 }
1319 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001320}
1321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001322static int video_read(char *p)
1323{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001324 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001325 int len = 0;
1326
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001327 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001328 len += sprintf(p + len, "status:\t\tnot supported\n");
1329 return len;
1330 }
1331
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001332 status = video_outputsw_get();
1333 if (status < 0)
1334 return status;
1335
1336 autosw = video_autosw_get();
1337 if (autosw < 0)
1338 return autosw;
1339
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001340 len += sprintf(p + len, "status:\t\tsupported\n");
1341 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1342 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001343 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001344 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1345 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1346 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1347 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001348 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001349 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1350 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1351 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1352
1353 return len;
1354}
1355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001356static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 char *cmd;
1359 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001360 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001362 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001363 return -ENODEV;
1364
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001365 enable = 0;
1366 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 while ((cmd = next_cmd(&buf))) {
1369 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001370 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001372 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001374 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001376 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001377 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001378 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001379 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001380 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001381 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001382 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001384 res = video_autosw_set(1);
1385 if (res)
1386 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001388 res = video_autosw_set(0);
1389 if (res)
1390 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001392 res = video_outputsw_cycle();
1393 if (res)
1394 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001396 res = video_expand_toggle();
1397 if (res)
1398 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 } else
1400 return -EINVAL;
1401 }
1402
1403 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001404 status = video_outputsw_get();
1405 if (status < 0)
1406 return status;
1407 res = video_outputsw_set((status & ~disable) | enable);
1408 if (res)
1409 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
1412 return 0;
1413}
1414
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001415static struct ibm_struct video_driver_data = {
1416 .name = "video",
1417 .read = video_read,
1418 .write = video_write,
1419 .exit = video_exit,
1420};
1421
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001422/*************************************************************************
1423 * Light (thinklight) subdriver
1424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001426IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1427IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1428
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001429static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001431 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1432
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001433 IBM_ACPIHANDLE_INIT(ledb);
1434 IBM_ACPIHANDLE_INIT(lght);
1435 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001436
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001437 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001438 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001439
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001440 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001441 /* light status not supported on
1442 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001443 tp_features.light_status =
1444 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001446 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001447 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001448
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001449 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001452static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 int len = 0;
1455 int status = 0;
1456
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001457 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001458 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001459 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001460 len += sprintf(p + len, "status:\t\tunknown\n");
1461 len += sprintf(p + len, "commands:\ton, off\n");
1462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1464 return -EIO;
1465 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001466 len += sprintf(p + len, "commands:\ton, off\n");
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 return len;
1470}
1471
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001472static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 int cmos_cmd, lght_cmd;
1475 char *cmd;
1476 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001477
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001478 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001479 return -ENODEV;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 while ((cmd = next_cmd(&buf))) {
1482 if (strlencmp(cmd, "on") == 0) {
1483 cmos_cmd = 0x0c;
1484 lght_cmd = 1;
1485 } else if (strlencmp(cmd, "off") == 0) {
1486 cmos_cmd = 0x0d;
1487 lght_cmd = 0;
1488 } else
1489 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001492 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1493 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!success)
1495 return -EIO;
1496 }
1497
1498 return 0;
1499}
1500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001501static struct ibm_struct light_driver_data = {
1502 .name = "light",
1503 .read = light_read,
1504 .write = light_write,
1505};
1506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001507/*************************************************************************
1508 * Dock subdriver
1509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001511#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001512
1513IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1514 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1515 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1516 "\\_SB.PCI.ISA.SLCE", /* 570 */
1517 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1518
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001519/* don't list other alternatives as we install a notify handler on the 570 */
1520IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522#define dock_docked() (_sta(dock_handle) & 1)
1523
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001524static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001525{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001526 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1527
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001528 IBM_ACPIHANDLE_INIT(dock);
1529 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001530
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001531 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1532 str_supported(dock_handle != NULL));
1533
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001534 return (dock_handle)? 0 : 1;
1535}
1536
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001537static void dock_notify(struct ibm_struct *ibm, u32 event)
1538{
1539 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001540 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001541
1542 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001543 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001544 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001545 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001546 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001547 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001548 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001549 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001550 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001551 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001552 else {
1553 printk(IBM_ERR "unknown dock event %d, status %d\n",
1554 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001555 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001556 }
1557}
1558
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001559static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 int len = 0;
1562 int docked = dock_docked();
1563
1564 if (!dock_handle)
1565 len += sprintf(p + len, "status:\t\tnot supported\n");
1566 else if (!docked)
1567 len += sprintf(p + len, "status:\t\tundocked\n");
1568 else {
1569 len += sprintf(p + len, "status:\t\tdocked\n");
1570 len += sprintf(p + len, "commands:\tdock, undock\n");
1571 }
1572
1573 return len;
1574}
1575
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001576static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 char *cmd;
1579
1580 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001581 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 while ((cmd = next_cmd(&buf))) {
1584 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001585 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1586 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EIO;
1588 } else if (strlencmp(cmd, "dock") == 0) {
1589 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1590 return -EIO;
1591 } else
1592 return -EINVAL;
1593 }
1594
1595 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001598static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001599 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001600 .notify = dock_notify,
1601 .handle = &dock_handle,
1602 .type = ACPI_SYSTEM_NOTIFY,
1603 },
1604 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001605 .hid = IBM_PCI_HID,
1606 .notify = dock_notify,
1607 .handle = &pci_handle,
1608 .type = ACPI_SYSTEM_NOTIFY,
1609 },
1610};
1611
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001612static struct ibm_struct dock_driver_data[2] = {
1613 {
1614 .name = "dock",
1615 .read = dock_read,
1616 .write = dock_write,
1617 .acpi = &ibm_dock_acpidriver[0],
1618 },
1619 {
1620 .name = "dock",
1621 .acpi = &ibm_dock_acpidriver[1],
1622 },
1623};
1624
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001625#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001627/*************************************************************************
1628 * Bay subdriver
1629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001631#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001632IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1633 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1634 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1635 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1636 ); /* A21e, R30, R31 */
1637IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1638 "_EJ0", /* all others */
1639 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1640IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1641 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1642 ); /* all others */
1643IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1644 "_EJ0", /* 770x */
1645 ); /* all others */
1646
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001647static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001649 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1650
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001651 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001652 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001653 IBM_ACPIHANDLE_INIT(bay_ej);
1654 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001655 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001656 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001657
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001658 tp_features.bay_status = bay_handle &&
1659 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1660 tp_features.bay_status2 = bay2_handle &&
1661 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001662
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001663 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1664 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1665 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1666 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001668 vdbg_printk(TPACPI_DBG_INIT,
1669 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001670 str_supported(tp_features.bay_status),
1671 str_supported(tp_features.bay_eject),
1672 str_supported(tp_features.bay_status2),
1673 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001674
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001675 return (tp_features.bay_status || tp_features.bay_eject ||
1676 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001679static void bay_notify(struct ibm_struct *ibm, u32 event)
1680{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001681 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001682}
1683
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001684#define bay_occupied(b) (_sta(b##_handle) & 1)
1685
1686static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001689 int occupied = bay_occupied(bay);
1690 int occupied2 = bay_occupied(bay2);
1691 int eject, eject2;
1692
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001693 len += sprintf(p + len, "status:\t\t%s\n",
1694 tp_features.bay_status ?
1695 (occupied ? "occupied" : "unoccupied") :
1696 "not supported");
1697 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001698 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1699 "occupied" : "unoccupied");
1700
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001701 eject = tp_features.bay_eject && occupied;
1702 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001703
1704 if (eject && eject2)
1705 len += sprintf(p + len, "commands:\teject, eject2\n");
1706 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001708 else if (eject2)
1709 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 return len;
1712}
1713
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001714static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 char *cmd;
1717
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001718 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001719 return -ENODEV;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001722 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001723 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1724 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001725 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001726 strlencmp(cmd, "eject2") == 0) {
1727 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -EIO;
1729 } else
1730 return -EINVAL;
1731 }
1732
1733 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001734}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001735
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001736static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1737 .notify = bay_notify,
1738 .handle = &bay_handle,
1739 .type = ACPI_SYSTEM_NOTIFY,
1740};
1741
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001742static struct ibm_struct bay_driver_data = {
1743 .name = "bay",
1744 .read = bay_read,
1745 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001746 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001747};
1748
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001749#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001751/*************************************************************************
1752 * CMOS subdriver
1753 */
1754
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03001755/* sysfs cmos_command -------------------------------------------------- */
1756static ssize_t cmos_command_store(struct device *dev,
1757 struct device_attribute *attr,
1758 const char *buf, size_t count)
1759{
1760 unsigned long cmos_cmd;
1761 int res;
1762
1763 if (parse_strtoul(buf, 21, &cmos_cmd))
1764 return -EINVAL;
1765
1766 res = issue_thinkpad_cmos_command(cmos_cmd);
1767 return (res)? res : count;
1768}
1769
1770static struct device_attribute dev_attr_cmos_command =
1771 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
1772
1773/* --------------------------------------------------------------------- */
1774
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001775static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001776{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03001777 int res;
1778
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001779 vdbg_printk(TPACPI_DBG_INIT,
1780 "initializing cmos commands subdriver\n");
1781
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001782 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001783
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001784 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1785 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03001786
1787 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
1788 if (res)
1789 return res;
1790
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001791 return (cmos_handle)? 0 : 1;
1792}
1793
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03001794static void cmos_exit(void)
1795{
1796 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
1797}
1798
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001799static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 int len = 0;
1802
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001803 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1804 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!cmos_handle)
1806 len += sprintf(p + len, "status:\t\tnot supported\n");
1807 else {
1808 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001809 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
1812 return len;
1813}
1814
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001815static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001818 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001821 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1822 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* cmos_cmd set */
1824 } else
1825 return -EINVAL;
1826
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001827 res = issue_thinkpad_cmos_command(cmos_cmd);
1828 if (res)
1829 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831
1832 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001833}
1834
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001835static struct ibm_struct cmos_driver_data = {
1836 .name = "cmos",
1837 .read = cmos_read,
1838 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03001839 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001840};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001841
1842/*************************************************************************
1843 * LED subdriver
1844 */
1845
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001846static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001847
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001848IBM_HANDLE(led, ec, "SLED", /* 570 */
1849 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1850 "LED", /* all others */
1851 ); /* R30, R31 */
1852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001853static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001854{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001855 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1856
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001857 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001858
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001859 if (!led_handle)
1860 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001861 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001862 else if (strlencmp(led_path, "SLED") == 0)
1863 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001864 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001865 else if (strlencmp(led_path, "SYSL") == 0)
1866 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001867 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001868 else
1869 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001870 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001871
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001872 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1873 str_supported(led_supported), led_supported);
1874
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001875 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001876}
1877
1878#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1879
1880static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 int len = 0;
1883
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001884 if (!led_supported) {
1885 len += sprintf(p + len, "status:\t\tnot supported\n");
1886 return len;
1887 }
1888 len += sprintf(p + len, "status:\t\tsupported\n");
1889
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001890 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001891 /* 570 */
1892 int i, status;
1893 for (i = 0; i < 8; i++) {
1894 if (!acpi_evalf(ec_handle,
1895 &status, "GLED", "dd", 1 << i))
1896 return -EIO;
1897 len += sprintf(p + len, "%d:\t\t%s\n",
1898 i, led_status(status));
1899 }
1900 }
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001903 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 return len;
1906}
1907
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001908/* off, on, blink */
1909static const int led_sled_arg1[] = { 0, 1, 3 };
1910static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1911static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1912static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1913
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001914static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001917 int led, ind, ret;
1918
1919 if (!led_supported)
1920 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001923 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return -EINVAL;
1925
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001926 if (strstr(cmd, "off")) {
1927 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001929 ind = 1;
1930 } else if (strstr(cmd, "blink")) {
1931 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 } else
1933 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001934
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001935 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001936 /* 570 */
1937 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001939 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001941 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001942 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1943 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001944 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001945 if (ret >= 0)
1946 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001947 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001948 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001949 if (ret >= 0)
1950 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001951 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001952 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001953 if (ret < 0)
1954 return ret;
1955 } else {
1956 /* all others */
1957 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1958 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001964}
1965
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001966static struct ibm_struct led_driver_data = {
1967 .name = "led",
1968 .read = led_read,
1969 .write = led_write,
1970};
1971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001972/*************************************************************************
1973 * Beep subdriver
1974 */
1975
1976IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1977
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001978static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001979{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001980 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1981
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001982 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001983
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001984 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1985 str_supported(beep_handle != NULL));
1986
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001987 return (beep_handle)? 0 : 1;
1988}
1989
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001990static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 int len = 0;
1993
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001994 if (!beep_handle)
1995 len += sprintf(p + len, "status:\t\tnot supported\n");
1996 else {
1997 len += sprintf(p + len, "status:\t\tsupported\n");
1998 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 return len;
2002}
2003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002004static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 char *cmd;
2007 int beep_cmd;
2008
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002009 if (!beep_handle)
2010 return -ENODEV;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002013 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2014 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* beep_cmd set */
2016 } else
2017 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002018 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -EIO;
2020 }
2021
2022 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002023}
2024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002025static struct ibm_struct beep_driver_data = {
2026 .name = "beep",
2027 .read = beep_read,
2028 .write = beep_write,
2029};
2030
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002031/*************************************************************************
2032 * Thermal subdriver
2033 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002034
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002035static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002036
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002037/* sysfs temp##_input -------------------------------------------------- */
2038
2039static ssize_t thermal_temp_input_show(struct device *dev,
2040 struct device_attribute *attr,
2041 char *buf)
2042{
2043 struct sensor_device_attribute *sensor_attr =
2044 to_sensor_dev_attr(attr);
2045 int idx = sensor_attr->index;
2046 s32 value;
2047 int res;
2048
2049 res = thermal_get_sensor(idx, &value);
2050 if (res)
2051 return res;
2052 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2053 return -ENXIO;
2054
2055 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2056}
2057
2058#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2059 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2060
2061static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2062 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2063 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2064 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2065 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2066 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2067 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2068 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2069 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2070 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2071 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2072 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2073 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2074 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2075 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2076 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2077 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2078};
2079
2080#define THERMAL_ATTRS(X) \
2081 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2082
2083static struct attribute *thermal_temp_input_attr[] = {
2084 THERMAL_ATTRS(8),
2085 THERMAL_ATTRS(9),
2086 THERMAL_ATTRS(10),
2087 THERMAL_ATTRS(11),
2088 THERMAL_ATTRS(12),
2089 THERMAL_ATTRS(13),
2090 THERMAL_ATTRS(14),
2091 THERMAL_ATTRS(15),
2092 THERMAL_ATTRS(0),
2093 THERMAL_ATTRS(1),
2094 THERMAL_ATTRS(2),
2095 THERMAL_ATTRS(3),
2096 THERMAL_ATTRS(4),
2097 THERMAL_ATTRS(5),
2098 THERMAL_ATTRS(6),
2099 THERMAL_ATTRS(7),
2100 NULL
2101};
2102
2103static const struct attribute_group thermal_temp_input16_group = {
2104 .attrs = thermal_temp_input_attr
2105};
2106
2107static const struct attribute_group thermal_temp_input8_group = {
2108 .attrs = &thermal_temp_input_attr[8]
2109};
2110
2111#undef THERMAL_SENSOR_ATTR_TEMP
2112#undef THERMAL_ATTRS
2113
2114/* --------------------------------------------------------------------- */
2115
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002116static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002117{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002118 u8 t, ta1, ta2;
2119 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002120 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002121 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002122
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002123 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2124
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002125 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002126
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002127 if (ibm_thinkpad_ec_found && experimental) {
2128 /*
2129 * Direct EC access mode: sensors at registers
2130 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2131 * non-implemented, thermal sensors return 0x80 when
2132 * not available
2133 */
2134
2135 ta1 = ta2 = 0;
2136 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002137 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002138 ta1 |= t;
2139 } else {
2140 ta1 = 0;
2141 break;
2142 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002143 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002144 ta2 |= t;
2145 } else {
2146 ta1 = 0;
2147 break;
2148 }
2149 }
2150 if (ta1 == 0) {
2151 /* This is sheer paranoia, but we handle it anyway */
2152 if (acpi_tmp7) {
2153 printk(IBM_ERR
2154 "ThinkPad ACPI EC access misbehaving, "
2155 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002156 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002157 } else {
2158 printk(IBM_ERR
2159 "ThinkPad ACPI EC access misbehaving, "
2160 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002161 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002162 }
2163 } else {
2164 thermal_read_mode =
2165 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002166 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002167 }
2168 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002169 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2170 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002171 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002172 } else {
2173 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002174 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002175 }
2176 } else {
2177 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002178 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002179 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002180
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002181 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2182 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2183 thermal_read_mode);
2184
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002185 switch(thermal_read_mode) {
2186 case TPACPI_THERMAL_TPEC_16:
2187 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2188 &thermal_temp_input16_group);
2189 if (res)
2190 return res;
2191 break;
2192 case TPACPI_THERMAL_TPEC_8:
2193 case TPACPI_THERMAL_ACPI_TMP07:
2194 case TPACPI_THERMAL_ACPI_UPDT:
2195 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2196 &thermal_temp_input8_group);
2197 if (res)
2198 return res;
2199 break;
2200 case TPACPI_THERMAL_NONE:
2201 default:
2202 return 1;
2203 }
2204
2205 return 0;
2206}
2207
2208static void thermal_exit(void)
2209{
2210 switch(thermal_read_mode) {
2211 case TPACPI_THERMAL_TPEC_16:
2212 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2213 &thermal_temp_input16_group);
2214 break;
2215 case TPACPI_THERMAL_TPEC_8:
2216 case TPACPI_THERMAL_ACPI_TMP07:
2217 case TPACPI_THERMAL_ACPI_UPDT:
2218 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2219 &thermal_temp_input16_group);
2220 break;
2221 case TPACPI_THERMAL_NONE:
2222 default:
2223 break;
2224 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002225}
2226
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002227/* idx is zero-based */
2228static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002229{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002230 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002231 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002232 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002233
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002234 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002235
2236 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002237#if TPACPI_MAX_THERMAL_SENSORS >= 16
2238 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002239 if (idx >= 8 && idx <= 15) {
2240 t = TP_EC_THERMAL_TMP8;
2241 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002242 }
2243 /* fallthrough */
2244#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002245 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002246 if (idx <= 7) {
2247 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002248 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002249 *value = tmp * 1000;
2250 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002251 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002252 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002253
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002254 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002255 if (idx <= 7) {
2256 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2257 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2258 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002259 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2260 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002261 *value = (t - 2732) * 100;
2262 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002263 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002264 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002265
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002266 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002267 if (idx <= 7) {
2268 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002269 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2270 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002271 *value = t * 1000;
2272 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002273 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002274 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002275
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002276 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002277 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002278 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002279 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002280
2281 return -EINVAL;
2282}
2283
2284static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2285{
2286 int res, i;
2287 int n;
2288
2289 n = 8;
2290 i = 0;
2291
2292 if (!s)
2293 return -EINVAL;
2294
2295 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2296 n = 16;
2297
2298 for(i = 0 ; i < n; i++) {
2299 res = thermal_get_sensor(i, &s->temp[i]);
2300 if (res)
2301 return res;
2302 }
2303
2304 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002305}
2306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002307static int thermal_read(char *p)
2308{
2309 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002310 int n, i;
2311 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002312
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002313 n = thermal_get_sensors(&t);
2314 if (unlikely(n < 0))
2315 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002316
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002317 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002318
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002319 if (n > 0) {
2320 for (i = 0; i < (n - 1); i++)
2321 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2322 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2323 } else
2324 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002325
2326 return len;
2327}
2328
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002329static struct ibm_struct thermal_driver_data = {
2330 .name = "thermal",
2331 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002332 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002333};
2334
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002335/*************************************************************************
2336 * EC Dump subdriver
2337 */
2338
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002339static u8 ecdump_regs[256];
2340
2341static int ecdump_read(char *p)
2342{
2343 int len = 0;
2344 int i, j;
2345 u8 v;
2346
2347 len += sprintf(p + len, "EC "
2348 " +00 +01 +02 +03 +04 +05 +06 +07"
2349 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2350 for (i = 0; i < 256; i += 16) {
2351 len += sprintf(p + len, "EC 0x%02x:", i);
2352 for (j = 0; j < 16; j++) {
2353 if (!acpi_ec_read(i + j, &v))
2354 break;
2355 if (v != ecdump_regs[i + j])
2356 len += sprintf(p + len, " *%02x", v);
2357 else
2358 len += sprintf(p + len, " %02x", v);
2359 ecdump_regs[i + j] = v;
2360 }
2361 len += sprintf(p + len, "\n");
2362 if (j != 16)
2363 break;
2364 }
2365
2366 /* These are way too dangerous to advertise openly... */
2367#if 0
2368 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2369 " (<offset> is 00-ff, <value> is 00-ff)\n");
2370 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2371 " (<offset> is 00-ff, <value> is 0-255)\n");
2372#endif
2373 return len;
2374}
2375
2376static int ecdump_write(char *buf)
2377{
2378 char *cmd;
2379 int i, v;
2380
2381 while ((cmd = next_cmd(&buf))) {
2382 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2383 /* i and v set */
2384 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2385 /* i and v set */
2386 } else
2387 return -EINVAL;
2388 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2389 if (!acpi_ec_write(i, v))
2390 return -EIO;
2391 } else
2392 return -EINVAL;
2393 }
2394
2395 return 0;
2396}
2397
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002398static struct ibm_struct ecdump_driver_data = {
2399 .name = "ecdump",
2400 .read = ecdump_read,
2401 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002402 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002403};
2404
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002405/*************************************************************************
2406 * Backlight/brightness subdriver
2407 */
Holger Macht8acb0252006-10-20 14:30:28 -07002408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002409static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002410
Richard Purdie599a52d2007-02-10 23:07:48 +00002411static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002412 .get_brightness = brightness_get,
2413 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002414};
2415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002416static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002417{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002418 int b;
2419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002420 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2421
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002422 b = brightness_get(NULL);
2423 if (b < 0)
2424 return b;
2425
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03002426 ibm_backlight_device = backlight_device_register(
2427 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2428 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002429 if (IS_ERR(ibm_backlight_device)) {
2430 printk(IBM_ERR "Could not register backlight device\n");
2431 return PTR_ERR(ibm_backlight_device);
2432 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002433 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002434
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002435 ibm_backlight_device->props.max_brightness = 7;
2436 ibm_backlight_device->props.brightness = b;
2437 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002438
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002439 return 0;
2440}
2441
2442static void brightness_exit(void)
2443{
2444 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002445 vdbg_printk(TPACPI_DBG_EXIT,
2446 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002447 backlight_device_unregister(ibm_backlight_device);
2448 ibm_backlight_device = NULL;
2449 }
2450}
2451
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002452static int brightness_update_status(struct backlight_device *bd)
2453{
2454 return brightness_set(
2455 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2456 bd->props.power == FB_BLANK_UNBLANK) ?
2457 bd->props.brightness : 0);
2458}
2459
2460static int brightness_get(struct backlight_device *bd)
2461{
2462 u8 level;
2463 if (!acpi_ec_read(brightness_offset, &level))
2464 return -EIO;
2465
2466 level &= 0x7;
2467
2468 return level;
2469}
2470
2471static int brightness_set(int value)
2472{
2473 int cmos_cmd, inc, i;
2474 int current_value = brightness_get(NULL);
2475
2476 value &= 7;
2477
2478 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2479 inc = value > current_value ? 1 : -1;
2480 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002481 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002482 return -EIO;
2483 if (!acpi_ec_write(brightness_offset, i + inc))
2484 return -EIO;
2485 }
2486
2487 return 0;
2488}
2489
2490static int brightness_read(char *p)
2491{
2492 int len = 0;
2493 int level;
2494
2495 if ((level = brightness_get(NULL)) < 0) {
2496 len += sprintf(p + len, "level:\t\tunreadable\n");
2497 } else {
2498 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2499 len += sprintf(p + len, "commands:\tup, down\n");
2500 len += sprintf(p + len, "commands:\tlevel <level>"
2501 " (<level> is 0-7)\n");
2502 }
2503
2504 return len;
2505}
2506
2507static int brightness_write(char *buf)
2508{
2509 int level;
2510 int new_level;
2511 char *cmd;
2512
2513 while ((cmd = next_cmd(&buf))) {
2514 if ((level = brightness_get(NULL)) < 0)
2515 return level;
2516 level &= 7;
2517
2518 if (strlencmp(cmd, "up") == 0) {
2519 new_level = level == 7 ? 7 : level + 1;
2520 } else if (strlencmp(cmd, "down") == 0) {
2521 new_level = level == 0 ? 0 : level - 1;
2522 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2523 new_level >= 0 && new_level <= 7) {
2524 /* new_level set */
2525 } else
2526 return -EINVAL;
2527
2528 brightness_set(new_level);
2529 }
2530
2531 return 0;
2532}
2533
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002534static struct ibm_struct brightness_driver_data = {
2535 .name = "brightness",
2536 .read = brightness_read,
2537 .write = brightness_write,
2538 .exit = brightness_exit,
2539};
2540
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002541/*************************************************************************
2542 * Volume subdriver
2543 */
2544
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002545static int volume_read(char *p)
2546{
2547 int len = 0;
2548 u8 level;
2549
2550 if (!acpi_ec_read(volume_offset, &level)) {
2551 len += sprintf(p + len, "level:\t\tunreadable\n");
2552 } else {
2553 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2554 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2555 len += sprintf(p + len, "commands:\tup, down, mute\n");
2556 len += sprintf(p + len, "commands:\tlevel <level>"
2557 " (<level> is 0-15)\n");
2558 }
2559
2560 return len;
2561}
2562
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002563static int volume_write(char *buf)
2564{
2565 int cmos_cmd, inc, i;
2566 u8 level, mute;
2567 int new_level, new_mute;
2568 char *cmd;
2569
2570 while ((cmd = next_cmd(&buf))) {
2571 if (!acpi_ec_read(volume_offset, &level))
2572 return -EIO;
2573 new_mute = mute = level & 0x40;
2574 new_level = level = level & 0xf;
2575
2576 if (strlencmp(cmd, "up") == 0) {
2577 if (mute)
2578 new_mute = 0;
2579 else
2580 new_level = level == 15 ? 15 : level + 1;
2581 } else if (strlencmp(cmd, "down") == 0) {
2582 if (mute)
2583 new_mute = 0;
2584 else
2585 new_level = level == 0 ? 0 : level - 1;
2586 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2587 new_level >= 0 && new_level <= 15) {
2588 /* new_level set */
2589 } else if (strlencmp(cmd, "mute") == 0) {
2590 new_mute = 0x40;
2591 } else
2592 return -EINVAL;
2593
2594 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002595 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002596 inc = new_level > level ? 1 : -1;
2597
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002598 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002599 !acpi_ec_write(volume_offset, level)))
2600 return -EIO;
2601
2602 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002603 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002604 !acpi_ec_write(volume_offset, i + inc))
2605 return -EIO;
2606
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002607 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608 !acpi_ec_write(volume_offset,
2609 new_level + mute)))
2610 return -EIO;
2611 }
2612
2613 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002614 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002615
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002616 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002617 !acpi_ec_write(volume_offset, level + new_mute))
2618 return -EIO;
2619 }
2620 }
2621
2622 return 0;
2623}
2624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002625static struct ibm_struct volume_driver_data = {
2626 .name = "volume",
2627 .read = volume_read,
2628 .write = volume_write,
2629};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002630
2631/*************************************************************************
2632 * Fan subdriver
2633 */
2634
2635/*
2636 * FAN ACCESS MODES
2637 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002638 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002639 * ACPI GFAN method: returns fan level
2640 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002641 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002642 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002643 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002644 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002645 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2646 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002647 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2648 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002649 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002650 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002651 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2652 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002653 *
2654 * Fan speed changes of any sort (including those caused by the
2655 * disengaged mode) are usually done slowly by the firmware as the
2656 * maximum ammount of fan duty cycle change per second seems to be
2657 * limited.
2658 *
2659 * Reading is not available if GFAN exists.
2660 * Writing is not available if SFAN exists.
2661 *
2662 * Bits
2663 * 7 automatic mode engaged;
2664 * (default operation mode of the ThinkPad)
2665 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002666 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002667 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002668 * the tachometer while the fan controller ramps up
2669 * the speed (which can take up to a few *minutes*).
2670 * Speeds up fan to 100% duty-cycle, which is far above
2671 * the standard RPM levels. It is not impossible that
2672 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002673 * 5-3 unused in some models. Extra bits for fan level
2674 * in others, but still useless as all values above
2675 * 7 map to the same speed as level 7 in these models.
2676 * 2-0 fan level (0..7 usually)
2677 * 0x00 = stop
2678 * 0x07 = max (set when temperatures critical)
2679 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002680 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002681 *
2682 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2683 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2684 * does so, its initial value is meaningless (0x07).
2685 *
2686 * For firmware bugs, refer to:
2687 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2688 *
2689 * ----
2690 *
2691 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2692 * Main fan tachometer reading (in RPM)
2693 *
2694 * This register is present on all ThinkPads with a new-style EC, and
2695 * it is known not to be present on the A21m/e, and T22, as there is
2696 * something else in offset 0x84 according to the ACPI DSDT. Other
2697 * ThinkPads from this same time period (and earlier) probably lack the
2698 * tachometer as well.
2699 *
2700 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2701 * was never fixed by IBM to report the EC firmware version string
2702 * probably support the tachometer (like the early X models), so
2703 * detecting it is quite hard. We need more data to know for sure.
2704 *
2705 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2706 * might result.
2707 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002708 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2709 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002710 *
2711 * For firmware bugs, refer to:
2712 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2713 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002714 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002715 * ThinkPad X31, X40, X41. Not available in the X60.
2716 *
2717 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2718 * high speed. ACPI DSDT seems to map these three speeds to levels
2719 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2720 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2721 *
2722 * The speeds are stored on handles
2723 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2724 *
2725 * There are three default speed sets, acessible as handles:
2726 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2727 *
2728 * ACPI DSDT switches which set is in use depending on various
2729 * factors.
2730 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002731 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002732 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2733 * but the ACPI tables just mention level 7.
2734 */
2735
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002736static enum fan_status_access_mode fan_status_access_mode;
2737static enum fan_control_access_mode fan_control_access_mode;
2738static enum fan_control_commands fan_control_commands;
2739
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002740static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002741static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002742
Len Brown25c68a32006-12-08 04:43:41 -05002743static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002744static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002745static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002746
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002747IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2748IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2749 "\\FSPD", /* 600e/x, 770e, 770x */
2750 ); /* all others */
2751IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2752 "JFNS", /* 770x-JL */
2753 ); /* all others */
2754
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002755/*
2756 * SYSFS fan layout: hwmon compatible (device)
2757 *
2758 * pwm*_enable:
2759 * 0: "disengaged" mode
2760 * 1: manual mode
2761 * 2: native EC "auto" mode (recommended, hardware default)
2762 *
2763 * pwm*: set speed in manual mode, ignored otherwise.
2764 * 0 is level 0; 255 is level 7. Intermediate points done with linear
2765 * interpolation.
2766 *
2767 * fan*_input: tachometer reading, RPM
2768 *
2769 *
2770 * SYSFS fan layout: extensions
2771 *
2772 * fan_watchdog (driver):
2773 * fan watchdog interval in seconds, 0 disables (default), max 120
2774 */
2775
2776/* sysfs fan pwm1_enable ----------------------------------------------- */
2777static ssize_t fan_pwm1_enable_show(struct device *dev,
2778 struct device_attribute *attr,
2779 char *buf)
2780{
2781 int res, mode;
2782 u8 status;
2783
2784 res = fan_get_status_safe(&status);
2785 if (res)
2786 return res;
2787
2788 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2789 if (status != fan_control_initial_status) {
2790 tp_features.fan_ctrl_status_undef = 0;
2791 } else {
2792 /* Return most likely status. In fact, it
2793 * might be the only possible status */
2794 status = TP_EC_FAN_AUTO;
2795 }
2796 }
2797
2798 if (status & TP_EC_FAN_FULLSPEED) {
2799 mode = 0;
2800 } else if (status & TP_EC_FAN_AUTO) {
2801 mode = 2;
2802 } else
2803 mode = 1;
2804
2805 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
2806}
2807
2808static ssize_t fan_pwm1_enable_store(struct device *dev,
2809 struct device_attribute *attr,
2810 const char *buf, size_t count)
2811{
2812 unsigned long t;
2813 int res, level;
2814
2815 if (parse_strtoul(buf, 2, &t))
2816 return -EINVAL;
2817
2818 switch (t) {
2819 case 0:
2820 level = TP_EC_FAN_FULLSPEED;
2821 break;
2822 case 1:
2823 level = TPACPI_FAN_LAST_LEVEL;
2824 break;
2825 case 2:
2826 level = TP_EC_FAN_AUTO;
2827 break;
2828 case 3:
2829 /* reserved for software-controlled auto mode */
2830 return -ENOSYS;
2831 default:
2832 return -EINVAL;
2833 }
2834
2835 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03002836 if (res == -ENXIO)
2837 return -EINVAL;
2838 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002839 return res;
2840
2841 fan_watchdog_reset();
2842
2843 return count;
2844}
2845
2846static struct device_attribute dev_attr_fan_pwm1_enable =
2847 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
2848 fan_pwm1_enable_show, fan_pwm1_enable_store);
2849
2850/* sysfs fan pwm1 ------------------------------------------------------ */
2851static ssize_t fan_pwm1_show(struct device *dev,
2852 struct device_attribute *attr,
2853 char *buf)
2854{
2855 int res;
2856 u8 status;
2857
2858 res = fan_get_status_safe(&status);
2859 if (res)
2860 return res;
2861
2862 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2863 if (status != fan_control_initial_status) {
2864 tp_features.fan_ctrl_status_undef = 0;
2865 } else {
2866 status = TP_EC_FAN_AUTO;
2867 }
2868 }
2869
2870 if ((status &
2871 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
2872 status = fan_control_desired_level;
2873
2874 if (status > 7)
2875 status = 7;
2876
2877 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
2878}
2879
2880static ssize_t fan_pwm1_store(struct device *dev,
2881 struct device_attribute *attr,
2882 const char *buf, size_t count)
2883{
2884 unsigned long s;
2885 int rc;
2886 u8 status, newlevel;
2887
2888 if (parse_strtoul(buf, 255, &s))
2889 return -EINVAL;
2890
2891 /* scale down from 0-255 to 0-7 */
2892 newlevel = (s >> 5) & 0x07;
2893
2894 rc = mutex_lock_interruptible(&fan_mutex);
2895 if (rc < 0)
2896 return rc;
2897
2898 rc = fan_get_status(&status);
2899 if (!rc && (status &
2900 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
2901 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03002902 if (rc == -ENXIO)
2903 rc = -EINVAL;
2904 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002905 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03002906 fan_watchdog_reset();
2907 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002908 }
2909
2910 mutex_unlock(&fan_mutex);
2911 return (rc)? rc : count;
2912}
2913
2914static struct device_attribute dev_attr_fan_pwm1 =
2915 __ATTR(pwm1, S_IWUSR | S_IRUGO,
2916 fan_pwm1_show, fan_pwm1_store);
2917
2918/* sysfs fan fan1_input ------------------------------------------------ */
2919static ssize_t fan_fan1_input_show(struct device *dev,
2920 struct device_attribute *attr,
2921 char *buf)
2922{
2923 int res;
2924 unsigned int speed;
2925
2926 res = fan_get_speed(&speed);
2927 if (res < 0)
2928 return res;
2929
2930 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
2931}
2932
2933static struct device_attribute dev_attr_fan_fan1_input =
2934 __ATTR(fan1_input, S_IRUGO,
2935 fan_fan1_input_show, NULL);
2936
2937/* sysfs fan fan_watchdog (driver) ------------------------------------- */
2938static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
2939 char *buf)
2940{
2941 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
2942}
2943
2944static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
2945 const char *buf, size_t count)
2946{
2947 unsigned long t;
2948
2949 if (parse_strtoul(buf, 120, &t))
2950 return -EINVAL;
2951
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03002952 if (!fan_control_allowed)
2953 return -EPERM;
2954
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002955 fan_watchdog_maxinterval = t;
2956 fan_watchdog_reset();
2957
2958 return count;
2959}
2960
2961static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
2962 fan_fan_watchdog_show, fan_fan_watchdog_store);
2963
2964/* --------------------------------------------------------------------- */
2965static struct attribute *fan_attributes[] = {
2966 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
2967 &dev_attr_fan_fan1_input.attr,
2968 NULL
2969};
2970
2971static const struct attribute_group fan_attr_group = {
2972 .attrs = fan_attributes,
2973};
2974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002975static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002976{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002977 int rc;
2978
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002979 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2980
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002981 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002982 fan_status_access_mode = TPACPI_FAN_NONE;
2983 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002984 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002985 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002986 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002987 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002988
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002989 IBM_ACPIHANDLE_INIT(fans);
2990 IBM_ACPIHANDLE_INIT(gfan);
2991 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002992
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002993 if (gfan_handle) {
2994 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002995 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002996 } else {
2997 /* all other ThinkPads: note that even old-style
2998 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002999 if (likely(acpi_ec_read(fan_status_offset,
3000 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003001 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003002
3003 /* In some ThinkPads, neither the EC nor the ACPI
3004 * DSDT initialize the fan status, and it ends up
3005 * being set to 0x07 when it *could* be either
3006 * 0x07 or 0x80.
3007 *
3008 * Enable for TP-1Y (T43), TP-78 (R51e),
3009 * TP-76 (R52), TP-70 (T43, R52), which are known
3010 * to be buggy. */
3011 if (fan_control_initial_status == 0x07 &&
3012 ibm_thinkpad_ec_found &&
3013 ((ibm_thinkpad_ec_found[0] == '1' &&
3014 ibm_thinkpad_ec_found[1] == 'Y') ||
3015 (ibm_thinkpad_ec_found[0] == '7' &&
3016 (ibm_thinkpad_ec_found[1] == '6' ||
3017 ibm_thinkpad_ec_found[1] == '8' ||
3018 ibm_thinkpad_ec_found[1] == '0'))
3019 )) {
3020 printk(IBM_NOTICE
3021 "fan_init: initial fan status is "
3022 "unknown, assuming it is in auto "
3023 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003024 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003025 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003026 } else {
3027 printk(IBM_ERR
3028 "ThinkPad ACPI EC access misbehaving, "
3029 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003030 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003031 }
3032 }
3033
3034 if (sfan_handle) {
3035 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003036 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003037 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003038 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003039 } else {
3040 if (!gfan_handle) {
3041 /* gfan without sfan means no fan control */
3042 /* all other models implement TP EC 0x2f control */
3043
3044 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003045 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003046 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003047 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003048 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003049 TPACPI_FAN_CMD_SPEED |
3050 TPACPI_FAN_CMD_LEVEL |
3051 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003052 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003053 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003054 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003055 TPACPI_FAN_CMD_LEVEL |
3056 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003057 }
3058 }
3059 }
3060
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003061 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3062 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3063 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3064 fan_status_access_mode, fan_control_access_mode);
3065
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003066 /* fan control master switch */
3067 if (!fan_control_allowed) {
3068 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3069 fan_control_commands = 0;
3070 dbg_printk(TPACPI_DBG_INIT,
3071 "fan control features disabled by parameter\n");
3072 }
3073
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003074 /* update fan_control_desired_level */
3075 if (fan_status_access_mode != TPACPI_FAN_NONE)
3076 fan_get_status_safe(NULL);
3077
3078 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3079 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3080 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3081 &fan_attr_group);
3082 if (!(rc < 0))
3083 rc = driver_create_file(&tpacpi_pdriver.driver,
3084 &driver_attr_fan_watchdog);
3085 if (rc < 0)
3086 return rc;
3087 return 0;
3088 } else
3089 return 1;
3090}
3091
3092/*
3093 * Call with fan_mutex held
3094 */
3095static void fan_update_desired_level(u8 status)
3096{
3097 if ((status &
3098 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3099 if (status > 7)
3100 fan_control_desired_level = 7;
3101 else
3102 fan_control_desired_level = status;
3103 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003104}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003105
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003106static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003107{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003108 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003109
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003110 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003111 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003112
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003113 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003114 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003115 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003116
3117 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003118 return -EIO;
3119
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003120 if (likely(status))
3121 *status = s & 0x07;
3122
3123 break;
3124
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003125 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003126 /* all except 570, 600e/x, 770e, 770x */
3127 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3128 return -EIO;
3129
3130 if (likely(status))
3131 *status = s;
3132
3133 break;
3134
3135 default:
3136 return -ENXIO;
3137 }
3138
3139 return 0;
3140}
3141
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003142static int fan_get_status_safe(u8 *status)
3143{
3144 int rc;
3145 u8 s;
3146
3147 rc = mutex_lock_interruptible(&fan_mutex);
3148 if (rc < 0)
3149 return rc;
3150 rc = fan_get_status(&s);
3151 if (!rc)
3152 fan_update_desired_level(s);
3153 mutex_unlock(&fan_mutex);
3154
3155 if (status)
3156 *status = s;
3157
3158 return rc;
3159}
3160
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003161static void fan_exit(void)
3162{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003163 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003164
3165 /* FIXME: can we really do this unconditionally? */
3166 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3167 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3168
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003169 cancel_delayed_work(&fan_watchdog_task);
3170 flush_scheduled_work();
3171}
3172
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003173static int fan_get_speed(unsigned int *speed)
3174{
3175 u8 hi, lo;
3176
3177 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003178 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003179 /* all except 570, 600e/x, 770e, 770x */
3180 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3181 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3182 return -EIO;
3183
3184 if (likely(speed))
3185 *speed = (hi << 8) | lo;
3186
3187 break;
3188
3189 default:
3190 return -ENXIO;
3191 }
3192
3193 return 0;
3194}
3195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003196static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003197{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003198 int rc;
3199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003200 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003201 rc = fan_set_enable();
3202 if (rc < 0) {
3203 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3204 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003205 /* reschedule for later */
3206 fan_watchdog_reset();
3207 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003208}
3209
3210static void fan_watchdog_reset(void)
3211{
3212 static int fan_watchdog_active = 0;
3213
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003214 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3215 return;
3216
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003217 if (fan_watchdog_active)
3218 cancel_delayed_work(&fan_watchdog_task);
3219
3220 if (fan_watchdog_maxinterval > 0) {
3221 fan_watchdog_active = 1;
3222 if (!schedule_delayed_work(&fan_watchdog_task,
3223 msecs_to_jiffies(fan_watchdog_maxinterval
3224 * 1000))) {
3225 printk(IBM_ERR "failed to schedule the fan watchdog, "
3226 "watchdog will not trigger\n");
3227 }
3228 } else
3229 fan_watchdog_active = 0;
3230}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003231
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003232static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003233{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003234 if (!fan_control_allowed)
3235 return -EPERM;
3236
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003237 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003238 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003239 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003240 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003241 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003242 } else
3243 return -EINVAL;
3244 break;
3245
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003246 case TPACPI_FAN_WR_ACPI_FANS:
3247 case TPACPI_FAN_WR_TPEC:
3248 if ((level != TP_EC_FAN_AUTO) &&
3249 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003250 ((level < 0) || (level > 7)))
3251 return -EINVAL;
3252
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003253 /* safety net should the EC not support AUTO
3254 * or FULLSPEED mode bits and just ignore them */
3255 if (level & TP_EC_FAN_FULLSPEED)
3256 level |= 7; /* safety min speed 7 */
3257 else if (level & TP_EC_FAN_FULLSPEED)
3258 level |= 4; /* safety min speed 4 */
3259
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003260 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003261 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003262 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003263 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003264 break;
3265
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003266 default:
3267 return -ENXIO;
3268 }
3269 return 0;
3270}
3271
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003272static int fan_set_level_safe(int level)
3273{
3274 int rc;
3275
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003276 if (!fan_control_allowed)
3277 return -EPERM;
3278
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003279 rc = mutex_lock_interruptible(&fan_mutex);
3280 if (rc < 0)
3281 return rc;
3282
3283 if (level == TPACPI_FAN_LAST_LEVEL)
3284 level = fan_control_desired_level;
3285
3286 rc = fan_set_level(level);
3287 if (!rc)
3288 fan_update_desired_level(level);
3289
3290 mutex_unlock(&fan_mutex);
3291 return rc;
3292}
3293
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003294static int fan_set_enable(void)
3295{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003296 u8 s;
3297 int rc;
3298
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003299 if (!fan_control_allowed)
3300 return -EPERM;
3301
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003302 rc = mutex_lock_interruptible(&fan_mutex);
3303 if (rc < 0)
3304 return rc;
3305
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003306 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003307 case TPACPI_FAN_WR_ACPI_FANS:
3308 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003309 rc = fan_get_status(&s);
3310 if (rc < 0)
3311 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003312
3313 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003314 if (s != 7) {
3315 s &= 0x07;
3316 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3317 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003318
3319 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003320 rc = -EIO;
3321 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003322 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003323 rc = 0;
3324 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003325 break;
3326
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003327 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003328 rc = fan_get_status(&s);
3329 if (rc < 0)
3330 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003331
3332 s &= 0x07;
3333
3334 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003335 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003336
3337 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003338 rc= -EIO;
3339 else
3340 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003341 break;
3342
3343 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003344 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003345 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003346
3347 mutex_unlock(&fan_mutex);
3348 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003349}
3350
3351static int fan_set_disable(void)
3352{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003353 int rc;
3354
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003355 if (!fan_control_allowed)
3356 return -EPERM;
3357
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003358 rc = mutex_lock_interruptible(&fan_mutex);
3359 if (rc < 0)
3360 return rc;
3361
3362 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003363 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003364 case TPACPI_FAN_WR_ACPI_FANS:
3365 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003366 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003367 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003368 else {
3369 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003370 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003371 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003372 break;
3373
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003374 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003375 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003376 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003377 else
3378 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003379 break;
3380
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003381 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003382 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003383 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003384
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003385
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003386 mutex_unlock(&fan_mutex);
3387 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003388}
3389
3390static int fan_set_speed(int speed)
3391{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003392 int rc;
3393
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003394 if (!fan_control_allowed)
3395 return -EPERM;
3396
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003397 rc = mutex_lock_interruptible(&fan_mutex);
3398 if (rc < 0)
3399 return rc;
3400
3401 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003402 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003403 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003404 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003405 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3406 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003407 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003408 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003409 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003410 break;
3411
3412 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003413 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003414 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003415
3416 mutex_unlock(&fan_mutex);
3417 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003418}
3419
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420static int fan_read(char *p)
3421{
3422 int len = 0;
3423 int rc;
3424 u8 status;
3425 unsigned int speed = 0;
3426
3427 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003428 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003429 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003430 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003431 return rc;
3432
3433 len += sprintf(p + len, "status:\t\t%s\n"
3434 "level:\t\t%d\n",
3435 (status != 0) ? "enabled" : "disabled", status);
3436 break;
3437
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003438 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003439 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003440 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003441 return rc;
3442
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003443 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003444 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003445 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003446 else
3447 /* Return most likely status. In fact, it
3448 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003449 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003450 }
3451
3452 len += sprintf(p + len, "status:\t\t%s\n",
3453 (status != 0) ? "enabled" : "disabled");
3454
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003455 if ((rc = fan_get_speed(&speed)) < 0)
3456 return rc;
3457
3458 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3459
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003460 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003461 /* Disengaged mode takes precedence */
3462 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003463 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003464 len += sprintf(p + len, "level:\t\tauto\n");
3465 else
3466 len += sprintf(p + len, "level:\t\t%d\n", status);
3467 break;
3468
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003469 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003470 default:
3471 len += sprintf(p + len, "status:\t\tnot supported\n");
3472 }
3473
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003474 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003475 len += sprintf(p + len, "commands:\tlevel <level>");
3476
3477 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003478 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003479 len += sprintf(p + len, " (<level> is 0-7)\n");
3480 break;
3481
3482 default:
3483 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003484 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003485 break;
3486 }
3487 }
3488
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003489 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003490 len += sprintf(p + len, "commands:\tenable, disable\n"
3491 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3492 "1-120 (seconds))\n");
3493
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003494 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003495 len += sprintf(p + len, "commands:\tspeed <speed>"
3496 " (<speed> is 0-65535)\n");
3497
3498 return len;
3499}
3500
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003501static int fan_write_cmd_level(const char *cmd, int *rc)
3502{
3503 int level;
3504
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003505 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003506 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003507 else if ((strlencmp(cmd, "level disengaged") == 0) |
3508 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003509 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003510 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003511 return 0;
3512
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003513 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003514 printk(IBM_ERR "level command accepted for unsupported "
3515 "access mode %d", fan_control_access_mode);
3516
3517 return 1;
3518}
3519
3520static int fan_write_cmd_enable(const char *cmd, int *rc)
3521{
3522 if (strlencmp(cmd, "enable") != 0)
3523 return 0;
3524
3525 if ((*rc = fan_set_enable()) == -ENXIO)
3526 printk(IBM_ERR "enable command accepted for unsupported "
3527 "access mode %d", fan_control_access_mode);
3528
3529 return 1;
3530}
3531
3532static int fan_write_cmd_disable(const char *cmd, int *rc)
3533{
3534 if (strlencmp(cmd, "disable") != 0)
3535 return 0;
3536
3537 if ((*rc = fan_set_disable()) == -ENXIO)
3538 printk(IBM_ERR "disable command accepted for unsupported "
3539 "access mode %d", fan_control_access_mode);
3540
3541 return 1;
3542}
3543
3544static int fan_write_cmd_speed(const char *cmd, int *rc)
3545{
3546 int speed;
3547
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003548 /* TODO:
3549 * Support speed <low> <medium> <high> ? */
3550
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003551 if (sscanf(cmd, "speed %d", &speed) != 1)
3552 return 0;
3553
3554 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3555 printk(IBM_ERR "speed command accepted for unsupported "
3556 "access mode %d", fan_control_access_mode);
3557
3558 return 1;
3559}
3560
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003561static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3562{
3563 int interval;
3564
3565 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3566 return 0;
3567
3568 if (interval < 0 || interval > 120)
3569 *rc = -EINVAL;
3570 else
3571 fan_watchdog_maxinterval = interval;
3572
3573 return 1;
3574}
3575
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003576static int fan_write(char *buf)
3577{
3578 char *cmd;
3579 int rc = 0;
3580
3581 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003582 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003583 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003584 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003585 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003586 fan_write_cmd_disable(cmd, &rc) ||
3587 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003588 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003589 fan_write_cmd_speed(cmd, &rc))
3590 )
3591 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003592 else if (!rc)
3593 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003594 }
3595
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003596 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003597}
3598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003599static struct ibm_struct fan_driver_data = {
3600 .name = "fan",
3601 .read = fan_read,
3602 .write = fan_write,
3603 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003604};
3605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003606/****************************************************************************
3607 ****************************************************************************
3608 *
3609 * Infrastructure
3610 *
3611 ****************************************************************************
3612 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614/* /proc support */
3615static struct proc_dir_entry *proc_dir = NULL;
3616
3617/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003618static LIST_HEAD(tpacpi_all_drivers);
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003621/*
3622 * Module and infrastructure proble, init and exit handling
3623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003625#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003626static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003627{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003628 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003629
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003630 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003631}
3632#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003634static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003637 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 struct proc_dir_entry *entry;
3639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003640 BUG_ON(ibm == NULL);
3641
3642 INIT_LIST_HEAD(&ibm->all_drivers);
3643
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003644 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return 0;
3646
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003647 dbg_printk(TPACPI_DBG_INIT,
3648 "probing for %s\n", ibm->name);
3649
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003650 if (iibm->init) {
3651 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003652 if (ret > 0)
3653 return 0; /* probe failed */
3654 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003656
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003657 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003660 if (ibm->acpi) {
3661 if (ibm->acpi->hid) {
3662 ret = register_tpacpi_subdriver(ibm);
3663 if (ret)
3664 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003665 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003666
3667 if (ibm->acpi->notify) {
3668 ret = setup_acpi_notify(ibm);
3669 if (ret == -ENODEV) {
3670 printk(IBM_NOTICE "disabling subdriver %s\n",
3671 ibm->name);
3672 ret = 0;
3673 goto err_out;
3674 }
3675 if (ret < 0)
3676 goto err_out;
3677 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003678 }
3679
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003680 dbg_printk(TPACPI_DBG_INIT,
3681 "%s installed\n", ibm->name);
3682
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003683 if (ibm->read) {
3684 entry = create_proc_entry(ibm->name,
3685 S_IFREG | S_IRUGO | S_IWUSR,
3686 proc_dir);
3687 if (!entry) {
3688 printk(IBM_ERR "unable to create proc entry %s\n",
3689 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003690 ret = -ENODEV;
3691 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003692 }
3693 entry->owner = THIS_MODULE;
3694 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003695 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003696 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003697 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003698 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003701 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003704
3705err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003706 dbg_printk(TPACPI_DBG_INIT,
3707 "%s: at error exit path with result %d\n",
3708 ibm->name, ret);
3709
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003710 ibm_exit(ibm);
3711 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
3714static void ibm_exit(struct ibm_struct *ibm)
3715{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003716 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003717
3718 list_del_init(&ibm->all_drivers);
3719
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003720 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003721 dbg_printk(TPACPI_DBG_EXIT,
3722 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003723 BUG_ON(!ibm->acpi);
3724 acpi_remove_notify_handler(*ibm->acpi->handle,
3725 ibm->acpi->type,
3726 dispatch_acpi_notify);
3727 ibm->flags.acpi_notify_installed = 0;
3728 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003731 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003732 dbg_printk(TPACPI_DBG_EXIT,
3733 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003735 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003738 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003739 dbg_printk(TPACPI_DBG_EXIT,
3740 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003741 BUG_ON(!ibm->acpi);
3742 acpi_bus_unregister_driver(ibm->acpi->driver);
3743 kfree(ibm->acpi->driver);
3744 ibm->acpi->driver = NULL;
3745 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003746 }
3747
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003748 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003749 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003750 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003752
3753 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003756/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003758static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003760static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003761{
3762 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003763 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003764
3765 /*
3766 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3767 * X32 or newer, all Z series; Some models must have an
3768 * up-to-date BIOS or they will not be detected.
3769 *
3770 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3771 */
3772 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003773 if (sscanf(dev->name,
3774 "IBM ThinkPad Embedded Controller -[%17c",
3775 ec_fw_string) == 1) {
3776 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3777 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3778 return kstrdup(ec_fw_string, GFP_KERNEL);
3779 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003780 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003781 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003784static int __init probe_for_thinkpad(void)
3785{
3786 int is_thinkpad;
3787
3788 if (acpi_disabled)
3789 return -ENODEV;
3790
3791 /*
3792 * Non-ancient models have better DMI tagging, but very old models
3793 * don't.
3794 */
3795 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3796
3797 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003798 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003799 if (!ec_handle) {
3800 if (is_thinkpad)
3801 printk(IBM_ERR
3802 "Not yet supported ThinkPad detected!\n");
3803 return -ENODEV;
3804 }
3805
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003806 /*
3807 * Risks a regression on very old machines, but reduces potential
3808 * false positives a damn great deal
3809 */
3810 if (!is_thinkpad)
3811 is_thinkpad = dmi_name_in_vendors("IBM");
3812
3813 if (!is_thinkpad && !force_load)
3814 return -ENODEV;
3815
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003816 return 0;
3817}
3818
3819
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003820/* Module init, exit, parameters */
3821
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003822static struct ibm_init_struct ibms_init[] __initdata = {
3823 {
3824 .init = thinkpad_acpi_driver_init,
3825 .data = &thinkpad_acpi_driver_data,
3826 },
3827 {
3828 .init = hotkey_init,
3829 .data = &hotkey_driver_data,
3830 },
3831 {
3832 .init = bluetooth_init,
3833 .data = &bluetooth_driver_data,
3834 },
3835 {
3836 .init = wan_init,
3837 .data = &wan_driver_data,
3838 },
3839 {
3840 .init = video_init,
3841 .data = &video_driver_data,
3842 },
3843 {
3844 .init = light_init,
3845 .data = &light_driver_data,
3846 },
3847#ifdef CONFIG_THINKPAD_ACPI_DOCK
3848 {
3849 .init = dock_init,
3850 .data = &dock_driver_data[0],
3851 },
3852 {
3853 .data = &dock_driver_data[1],
3854 },
3855#endif
3856#ifdef CONFIG_THINKPAD_ACPI_BAY
3857 {
3858 .init = bay_init,
3859 .data = &bay_driver_data,
3860 },
3861#endif
3862 {
3863 .init = cmos_init,
3864 .data = &cmos_driver_data,
3865 },
3866 {
3867 .init = led_init,
3868 .data = &led_driver_data,
3869 },
3870 {
3871 .init = beep_init,
3872 .data = &beep_driver_data,
3873 },
3874 {
3875 .init = thermal_init,
3876 .data = &thermal_driver_data,
3877 },
3878 {
3879 .data = &ecdump_driver_data,
3880 },
3881 {
3882 .init = brightness_init,
3883 .data = &brightness_driver_data,
3884 },
3885 {
3886 .data = &volume_driver_data,
3887 },
3888 {
3889 .init = fan_init,
3890 .data = &fan_driver_data,
3891 },
3892};
3893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3895{
3896 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003897 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003899 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3900 ibm = ibms_init[i].data;
3901 BUG_ON(ibm == NULL);
3902
3903 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3904 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003905 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003906 strcpy(ibms_init[i].param, val);
3907 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003908 return 0;
3909 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003910 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003911
3912 return -EINVAL;
3913}
3914
3915static int experimental;
3916module_param(experimental, int, 0);
3917
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003918static u32 dbg_level;
3919module_param_named(debug, dbg_level, uint, 0);
3920
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003921static int force_load;
3922module_param(force_load, int, 0);
3923
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003924static int fan_control_allowed;
3925module_param_named(fan_control, fan_control_allowed, int, 0);
3926
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003927#define IBM_PARAM(feature) \
3928 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3929
3930IBM_PARAM(hotkey);
3931IBM_PARAM(bluetooth);
3932IBM_PARAM(video);
3933IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003934#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003935IBM_PARAM(dock);
3936#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003937#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003938IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003939#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003940IBM_PARAM(cmos);
3941IBM_PARAM(led);
3942IBM_PARAM(beep);
3943IBM_PARAM(ecdump);
3944IBM_PARAM(brightness);
3945IBM_PARAM(volume);
3946IBM_PARAM(fan);
3947
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003948static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 int ret, i;
3951
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003952 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003953 ret = probe_for_thinkpad();
3954 if (ret)
3955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003957 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003958 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003959 IBM_ACPIHANDLE_INIT(ecrd);
3960 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003962 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003964 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003965 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return -ENODEV;
3967 }
3968 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003969
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003970 ret = platform_driver_register(&tpacpi_pdriver);
3971 if (ret) {
3972 printk(IBM_ERR "unable to register platform driver\n");
3973 thinkpad_acpi_module_exit();
3974 return ret;
3975 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003976 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3977 if (ret) {
3978 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3979 thinkpad_acpi_module_exit();
3980 return ret;
3981 }
3982
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003983
3984 /* Device initialization */
3985 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3986 NULL, 0);
3987 if (IS_ERR(tpacpi_pdev)) {
3988 ret = PTR_ERR(tpacpi_pdev);
3989 tpacpi_pdev = NULL;
3990 printk(IBM_ERR "unable to register platform device\n");
3991 thinkpad_acpi_module_exit();
3992 return ret;
3993 }
3994 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3995 if (IS_ERR(tpacpi_hwmon)) {
3996 ret = PTR_ERR(tpacpi_hwmon);
3997 tpacpi_hwmon = NULL;
3998 printk(IBM_ERR "unable to register hwmon device\n");
3999 thinkpad_acpi_module_exit();
4000 return ret;
4001 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004002 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4003 ret = ibm_init(&ibms_init[i]);
4004 if (ret >= 0 && *ibms_init[i].param)
4005 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004007 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return ret;
4009 }
4010 }
4011
4012 return 0;
4013}
4014
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004015static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004016{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004017 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004019 list_for_each_entry_safe_reverse(ibm, itmp,
4020 &tpacpi_all_drivers,
4021 all_drivers) {
4022 ibm_exit(ibm);
4023 }
4024
4025 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004026
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004027 if (tpacpi_hwmon)
4028 hwmon_device_unregister(tpacpi_hwmon);
4029
4030 if (tpacpi_pdev)
4031 platform_device_unregister(tpacpi_pdev);
4032
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004033 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004034 platform_driver_unregister(&tpacpi_pdriver);
4035
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004036 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004037 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004038
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004039 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004040}
4041
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004042module_init(thinkpad_acpi_module_init);
4043module_exit(thinkpad_acpi_module_exit);