]>
Commit | Line | Data |
---|---|---|
2d70b73a GKH |
1 | /* |
2 | * Samsung Laptop driver | |
3 | * | |
4 | * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de) | |
5 | * Copyright (C) 2009,2011 Novell Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License version 2 as published by | |
9 | * the Free Software Foundation. | |
10 | * | |
11 | */ | |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/pci.h> | |
19 | #include <linux/backlight.h> | |
f674ebf1 | 20 | #include <linux/leds.h> |
2d70b73a GKH |
21 | #include <linux/fb.h> |
22 | #include <linux/dmi.h> | |
23 | #include <linux/platform_device.h> | |
24 | #include <linux/rfkill.h> | |
f34cd9ca | 25 | #include <linux/acpi.h> |
5b80fc40 CC |
26 | #include <linux/seq_file.h> |
27 | #include <linux/debugfs.h> | |
82c333aa | 28 | #include <linux/ctype.h> |
2d70b73a GKH |
29 | |
30 | /* | |
31 | * This driver is needed because a number of Samsung laptops do not hook | |
32 | * their control settings through ACPI. So we have to poke around in the | |
33 | * BIOS to do things like brightness values, and "special" key controls. | |
34 | */ | |
35 | ||
36 | /* | |
37 | * We have 0 - 8 as valid brightness levels. The specs say that level 0 should | |
38 | * be reserved by the BIOS (which really doesn't make much sense), we tell | |
39 | * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8 | |
40 | */ | |
41 | #define MAX_BRIGHT 0x07 | |
42 | ||
43 | ||
44 | #define SABI_IFACE_MAIN 0x00 | |
45 | #define SABI_IFACE_SUB 0x02 | |
46 | #define SABI_IFACE_COMPLETE 0x04 | |
47 | #define SABI_IFACE_DATA 0x05 | |
48 | ||
84d482f2 CC |
49 | #define WL_STATUS_WLAN 0x0 |
50 | #define WL_STATUS_BT 0x2 | |
51 | ||
7e960711 CC |
52 | /* Structure get/set data using sabi */ |
53 | struct sabi_data { | |
54 | union { | |
55 | struct { | |
56 | u32 d0; | |
57 | u32 d1; | |
58 | u16 d2; | |
59 | u8 d3; | |
60 | }; | |
61 | u8 data[11]; | |
62 | }; | |
2d70b73a GKH |
63 | }; |
64 | ||
65 | struct sabi_header_offsets { | |
66 | u8 port; | |
67 | u8 re_mem; | |
68 | u8 iface_func; | |
69 | u8 en_mem; | |
70 | u8 data_offset; | |
71 | u8 data_segment; | |
72 | }; | |
73 | ||
74 | struct sabi_commands { | |
75 | /* | |
76 | * Brightness is 0 - 8, as described above. | |
77 | * Value 0 is for the BIOS to use | |
78 | */ | |
7e960711 CC |
79 | u16 get_brightness; |
80 | u16 set_brightness; | |
2d70b73a GKH |
81 | |
82 | /* | |
83 | * first byte: | |
84 | * 0x00 - wireless is off | |
85 | * 0x01 - wireless is on | |
86 | * second byte: | |
87 | * 0x02 - 3G is off | |
88 | * 0x03 - 3G is on | |
89 | * TODO, verify 3G is correct, that doesn't seem right... | |
90 | */ | |
7e960711 CC |
91 | u16 get_wireless_button; |
92 | u16 set_wireless_button; | |
2d70b73a GKH |
93 | |
94 | /* 0 is off, 1 is on */ | |
7e960711 CC |
95 | u16 get_backlight; |
96 | u16 set_backlight; | |
2d70b73a GKH |
97 | |
98 | /* | |
99 | * 0x80 or 0x00 - no action | |
100 | * 0x81 - recovery key pressed | |
101 | */ | |
7e960711 CC |
102 | u16 get_recovery_mode; |
103 | u16 set_recovery_mode; | |
2d70b73a GKH |
104 | |
105 | /* | |
106 | * on seclinux: 0 is low, 1 is high, | |
107 | * on swsmi: 0 is normal, 1 is silent, 2 is turbo | |
108 | */ | |
7e960711 CC |
109 | u16 get_performance_level; |
110 | u16 set_performance_level; | |
2d70b73a | 111 | |
cb5b5c91 CC |
112 | /* 0x80 is off, 0x81 is on */ |
113 | u16 get_battery_life_extender; | |
114 | u16 set_battery_life_extender; | |
115 | ||
3a75d378 CC |
116 | /* 0x80 is off, 0x81 is on */ |
117 | u16 get_usb_charge; | |
118 | u16 set_usb_charge; | |
119 | ||
84d482f2 CC |
120 | /* the first byte is for bluetooth and the third one is for wlan */ |
121 | u16 get_wireless_status; | |
122 | u16 set_wireless_status; | |
123 | ||
f674ebf1 CC |
124 | /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */ |
125 | u16 kbd_backlight; | |
126 | ||
2d70b73a GKH |
127 | /* |
128 | * Tell the BIOS that Linux is running on this machine. | |
129 | * 81 is on, 80 is off | |
130 | */ | |
7e960711 | 131 | u16 set_linux; |
2d70b73a GKH |
132 | }; |
133 | ||
134 | struct sabi_performance_level { | |
135 | const char *name; | |
7e960711 | 136 | u16 value; |
2d70b73a GKH |
137 | }; |
138 | ||
139 | struct sabi_config { | |
84d482f2 | 140 | int sabi_version; |
2d70b73a GKH |
141 | const char *test_string; |
142 | u16 main_function; | |
143 | const struct sabi_header_offsets header_offsets; | |
144 | const struct sabi_commands commands; | |
145 | const struct sabi_performance_level performance_levels[4]; | |
146 | u8 min_brightness; | |
147 | u8 max_brightness; | |
148 | }; | |
149 | ||
150 | static const struct sabi_config sabi_configs[] = { | |
151 | { | |
84d482f2 CC |
152 | /* I don't know if it is really 2, but it it is |
153 | * less than 3 anyway */ | |
154 | .sabi_version = 2, | |
155 | ||
2d70b73a GKH |
156 | .test_string = "SECLINUX", |
157 | ||
158 | .main_function = 0x4c49, | |
159 | ||
160 | .header_offsets = { | |
161 | .port = 0x00, | |
162 | .re_mem = 0x02, | |
163 | .iface_func = 0x03, | |
164 | .en_mem = 0x04, | |
165 | .data_offset = 0x05, | |
166 | .data_segment = 0x07, | |
167 | }, | |
168 | ||
169 | .commands = { | |
170 | .get_brightness = 0x00, | |
171 | .set_brightness = 0x01, | |
172 | ||
173 | .get_wireless_button = 0x02, | |
174 | .set_wireless_button = 0x03, | |
175 | ||
176 | .get_backlight = 0x04, | |
177 | .set_backlight = 0x05, | |
178 | ||
179 | .get_recovery_mode = 0x06, | |
180 | .set_recovery_mode = 0x07, | |
181 | ||
182 | .get_performance_level = 0x08, | |
183 | .set_performance_level = 0x09, | |
184 | ||
cb5b5c91 CC |
185 | .get_battery_life_extender = 0xFFFF, |
186 | .set_battery_life_extender = 0xFFFF, | |
187 | ||
3a75d378 CC |
188 | .get_usb_charge = 0xFFFF, |
189 | .set_usb_charge = 0xFFFF, | |
190 | ||
84d482f2 CC |
191 | .get_wireless_status = 0xFFFF, |
192 | .set_wireless_status = 0xFFFF, | |
193 | ||
f674ebf1 CC |
194 | .kbd_backlight = 0xFFFF, |
195 | ||
2d70b73a GKH |
196 | .set_linux = 0x0a, |
197 | }, | |
198 | ||
199 | .performance_levels = { | |
200 | { | |
201 | .name = "silent", | |
202 | .value = 0, | |
203 | }, | |
204 | { | |
205 | .name = "normal", | |
206 | .value = 1, | |
207 | }, | |
208 | { }, | |
209 | }, | |
210 | .min_brightness = 1, | |
211 | .max_brightness = 8, | |
212 | }, | |
213 | { | |
84d482f2 CC |
214 | .sabi_version = 3, |
215 | ||
2d70b73a GKH |
216 | .test_string = "SwSmi@", |
217 | ||
218 | .main_function = 0x5843, | |
219 | ||
220 | .header_offsets = { | |
221 | .port = 0x00, | |
222 | .re_mem = 0x04, | |
223 | .iface_func = 0x02, | |
224 | .en_mem = 0x03, | |
225 | .data_offset = 0x05, | |
226 | .data_segment = 0x07, | |
227 | }, | |
228 | ||
229 | .commands = { | |
230 | .get_brightness = 0x10, | |
231 | .set_brightness = 0x11, | |
232 | ||
233 | .get_wireless_button = 0x12, | |
234 | .set_wireless_button = 0x13, | |
235 | ||
236 | .get_backlight = 0x2d, | |
237 | .set_backlight = 0x2e, | |
238 | ||
239 | .get_recovery_mode = 0xff, | |
240 | .set_recovery_mode = 0xff, | |
241 | ||
242 | .get_performance_level = 0x31, | |
243 | .set_performance_level = 0x32, | |
244 | ||
cb5b5c91 CC |
245 | .get_battery_life_extender = 0x65, |
246 | .set_battery_life_extender = 0x66, | |
247 | ||
3a75d378 CC |
248 | .get_usb_charge = 0x67, |
249 | .set_usb_charge = 0x68, | |
250 | ||
84d482f2 CC |
251 | .get_wireless_status = 0x69, |
252 | .set_wireless_status = 0x6a, | |
253 | ||
f674ebf1 CC |
254 | .kbd_backlight = 0x78, |
255 | ||
2d70b73a GKH |
256 | .set_linux = 0xff, |
257 | }, | |
258 | ||
259 | .performance_levels = { | |
260 | { | |
261 | .name = "normal", | |
262 | .value = 0, | |
263 | }, | |
264 | { | |
265 | .name = "silent", | |
266 | .value = 1, | |
267 | }, | |
268 | { | |
269 | .name = "overclock", | |
270 | .value = 2, | |
271 | }, | |
272 | { }, | |
273 | }, | |
274 | .min_brightness = 0, | |
275 | .max_brightness = 8, | |
276 | }, | |
277 | { }, | |
278 | }; | |
279 | ||
5b80fc40 CC |
280 | /* |
281 | * samsung-laptop/ - debugfs root directory | |
282 | * f0000_segment - dump f0000 segment | |
283 | * command - current command | |
284 | * data - current data | |
285 | * d0, d1, d2, d3 - data fields | |
286 | * call - call SABI using command and data | |
287 | * | |
288 | * This allow to call arbitrary sabi commands wihout | |
289 | * modifying the driver at all. | |
290 | * For example, setting the keyboard backlight brightness to 5 | |
291 | * | |
292 | * echo 0x78 > command | |
293 | * echo 0x0582 > d0 | |
294 | * echo 0 > d1 | |
295 | * echo 0 > d2 | |
296 | * echo 0 > d3 | |
297 | * cat call | |
298 | */ | |
299 | ||
300 | struct samsung_laptop_debug { | |
301 | struct dentry *root; | |
302 | struct sabi_data data; | |
303 | u16 command; | |
304 | ||
305 | struct debugfs_blob_wrapper f0000_wrapper; | |
306 | struct debugfs_blob_wrapper data_wrapper; | |
6f6ae06e | 307 | struct debugfs_blob_wrapper sdiag_wrapper; |
5b80fc40 CC |
308 | }; |
309 | ||
84d482f2 CC |
310 | struct samsung_laptop; |
311 | ||
312 | struct samsung_rfkill { | |
313 | struct samsung_laptop *samsung; | |
314 | struct rfkill *rfkill; | |
315 | enum rfkill_type type; | |
316 | }; | |
317 | ||
a6df4894 CC |
318 | struct samsung_laptop { |
319 | const struct sabi_config *config; | |
2d70b73a | 320 | |
a6df4894 CC |
321 | void __iomem *sabi; |
322 | void __iomem *sabi_iface; | |
323 | void __iomem *f0000_segment; | |
324 | ||
325 | struct mutex sabi_mutex; | |
326 | ||
5dea7a20 | 327 | struct platform_device *platform_device; |
a6df4894 | 328 | struct backlight_device *backlight_device; |
84d482f2 CC |
329 | |
330 | struct samsung_rfkill wlan; | |
331 | struct samsung_rfkill bluetooth; | |
a6df4894 | 332 | |
f674ebf1 CC |
333 | struct led_classdev kbd_led; |
334 | int kbd_led_wk; | |
335 | struct workqueue_struct *led_workqueue; | |
336 | struct work_struct kbd_led_work; | |
337 | ||
5b80fc40 CC |
338 | struct samsung_laptop_debug debug; |
339 | ||
f34cd9ca | 340 | bool handle_backlight; |
a6df4894 | 341 | bool has_stepping_quirk; |
6f6ae06e CC |
342 | |
343 | char sdiag[64]; | |
a6df4894 CC |
344 | }; |
345 | ||
5dea7a20 | 346 | |
2d70b73a | 347 | |
90ab5ee9 | 348 | static bool force; |
2d70b73a GKH |
349 | module_param(force, bool, 0); |
350 | MODULE_PARM_DESC(force, | |
351 | "Disable the DMI check and forces the driver to be loaded"); | |
352 | ||
90ab5ee9 | 353 | static bool debug; |
2d70b73a GKH |
354 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
355 | MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
356 | ||
7e960711 CC |
357 | static int sabi_command(struct samsung_laptop *samsung, u16 command, |
358 | struct sabi_data *in, | |
359 | struct sabi_data *out) | |
2d70b73a | 360 | { |
a6df4894 | 361 | const struct sabi_config *config = samsung->config; |
7e960711 | 362 | int ret = 0; |
a6df4894 | 363 | u16 port = readw(samsung->sabi + config->header_offsets.port); |
2d70b73a GKH |
364 | u8 complete, iface_data; |
365 | ||
a6df4894 | 366 | mutex_lock(&samsung->sabi_mutex); |
2d70b73a | 367 | |
7e960711 CC |
368 | if (debug) { |
369 | if (in) | |
2e777187 CC |
370 | pr_info("SABI command:0x%04x " |
371 | "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}", | |
7e960711 CC |
372 | command, in->d0, in->d1, in->d2, in->d3); |
373 | else | |
2e777187 | 374 | pr_info("SABI command:0x%04x", command); |
7e960711 CC |
375 | } |
376 | ||
2d70b73a | 377 | /* enable memory to be able to write to it */ |
a6df4894 | 378 | outb(readb(samsung->sabi + config->header_offsets.en_mem), port); |
2d70b73a GKH |
379 | |
380 | /* write out the command */ | |
a6df4894 CC |
381 | writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN); |
382 | writew(command, samsung->sabi_iface + SABI_IFACE_SUB); | |
383 | writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE); | |
7e960711 CC |
384 | if (in) { |
385 | writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA); | |
386 | writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4); | |
387 | writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8); | |
388 | writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10); | |
389 | } | |
a6df4894 | 390 | outb(readb(samsung->sabi + config->header_offsets.iface_func), port); |
2d70b73a GKH |
391 | |
392 | /* write protect memory to make it safe */ | |
a6df4894 | 393 | outb(readb(samsung->sabi + config->header_offsets.re_mem), port); |
2d70b73a GKH |
394 | |
395 | /* see if the command actually succeeded */ | |
a6df4894 CC |
396 | complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE); |
397 | iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA); | |
2e777187 CC |
398 | |
399 | /* iface_data = 0xFF happens when a command is not known | |
400 | * so we only add a warning in debug mode since we will | |
401 | * probably issue some unknown command at startup to find | |
402 | * out which features are supported */ | |
403 | if (complete != 0xaa || (iface_data == 0xff && debug)) | |
7e960711 CC |
404 | pr_warn("SABI command 0x%04x failed with" |
405 | " completion flag 0x%02x and interface data 0x%02x", | |
406 | command, complete, iface_data); | |
2e777187 CC |
407 | |
408 | if (complete != 0xaa || iface_data == 0xff) { | |
7e960711 | 409 | ret = -EINVAL; |
2d70b73a GKH |
410 | goto exit; |
411 | } | |
7e960711 CC |
412 | |
413 | if (out) { | |
414 | out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA); | |
415 | out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4); | |
416 | out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2); | |
417 | out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1); | |
418 | } | |
419 | ||
420 | if (debug && out) { | |
2e777187 | 421 | pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}", |
7e960711 CC |
422 | out->d0, out->d1, out->d2, out->d3); |
423 | } | |
2d70b73a GKH |
424 | |
425 | exit: | |
a6df4894 | 426 | mutex_unlock(&samsung->sabi_mutex); |
7e960711 | 427 | return ret; |
2d70b73a GKH |
428 | } |
429 | ||
7e960711 CC |
430 | /* simple wrappers usable with most commands */ |
431 | static int sabi_set_commandb(struct samsung_laptop *samsung, | |
432 | u16 command, u8 data) | |
2d70b73a | 433 | { |
85229440 | 434 | struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } }; |
2d70b73a | 435 | |
7e960711 CC |
436 | in.data[0] = data; |
437 | return sabi_command(samsung, command, &in, NULL); | |
2d70b73a GKH |
438 | } |
439 | ||
5dea7a20 | 440 | static int read_brightness(struct samsung_laptop *samsung) |
2d70b73a | 441 | { |
a6df4894 CC |
442 | const struct sabi_config *config = samsung->config; |
443 | const struct sabi_commands *commands = &samsung->config->commands; | |
7e960711 | 444 | struct sabi_data sretval; |
2d70b73a GKH |
445 | int user_brightness = 0; |
446 | int retval; | |
447 | ||
7e960711 CC |
448 | retval = sabi_command(samsung, commands->get_brightness, |
449 | NULL, &sretval); | |
450 | if (retval) | |
451 | return retval; | |
452 | ||
453 | user_brightness = sretval.data[0]; | |
454 | if (user_brightness > config->min_brightness) | |
455 | user_brightness -= config->min_brightness; | |
456 | else | |
457 | user_brightness = 0; | |
458 | ||
2d70b73a GKH |
459 | return user_brightness; |
460 | } | |
461 | ||
5dea7a20 | 462 | static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness) |
2d70b73a | 463 | { |
a6df4894 CC |
464 | const struct sabi_config *config = samsung->config; |
465 | const struct sabi_commands *commands = &samsung->config->commands; | |
466 | u8 user_level = user_brightness + config->min_brightness; | |
2d70b73a | 467 | |
a6df4894 | 468 | if (samsung->has_stepping_quirk && user_level != 0) { |
ac080523 JS |
469 | /* |
470 | * short circuit if the specified level is what's already set | |
471 | * to prevent the screen from flickering needlessly | |
472 | */ | |
5dea7a20 | 473 | if (user_brightness == read_brightness(samsung)) |
ac080523 JS |
474 | return; |
475 | ||
7e960711 | 476 | sabi_set_commandb(samsung, commands->set_brightness, 0); |
ac080523 JS |
477 | } |
478 | ||
7e960711 | 479 | sabi_set_commandb(samsung, commands->set_brightness, user_level); |
2d70b73a GKH |
480 | } |
481 | ||
482 | static int get_brightness(struct backlight_device *bd) | |
483 | { | |
5dea7a20 CC |
484 | struct samsung_laptop *samsung = bl_get_data(bd); |
485 | ||
486 | return read_brightness(samsung); | |
2d70b73a GKH |
487 | } |
488 | ||
5dea7a20 | 489 | static void check_for_stepping_quirk(struct samsung_laptop *samsung) |
ac080523 | 490 | { |
5dea7a20 CC |
491 | int initial_level; |
492 | int check_level; | |
493 | int orig_level = read_brightness(samsung); | |
ac080523 JS |
494 | |
495 | /* | |
496 | * Some laptops exhibit the strange behaviour of stepping toward | |
497 | * (rather than setting) the brightness except when changing to/from | |
498 | * brightness level 0. This behaviour is checked for here and worked | |
499 | * around in set_brightness. | |
500 | */ | |
501 | ||
ba05b237 | 502 | if (orig_level == 0) |
5dea7a20 | 503 | set_brightness(samsung, 1); |
ba05b237 | 504 | |
5dea7a20 | 505 | initial_level = read_brightness(samsung); |
ba05b237 | 506 | |
ac080523 JS |
507 | if (initial_level <= 2) |
508 | check_level = initial_level + 2; | |
509 | else | |
510 | check_level = initial_level - 2; | |
511 | ||
a6df4894 | 512 | samsung->has_stepping_quirk = false; |
5dea7a20 | 513 | set_brightness(samsung, check_level); |
ac080523 | 514 | |
5dea7a20 | 515 | if (read_brightness(samsung) != check_level) { |
a6df4894 | 516 | samsung->has_stepping_quirk = true; |
ac080523 JS |
517 | pr_info("enabled workaround for brightness stepping quirk\n"); |
518 | } | |
519 | ||
5dea7a20 | 520 | set_brightness(samsung, orig_level); |
ac080523 JS |
521 | } |
522 | ||
2d70b73a GKH |
523 | static int update_status(struct backlight_device *bd) |
524 | { | |
5dea7a20 | 525 | struct samsung_laptop *samsung = bl_get_data(bd); |
a6df4894 CC |
526 | const struct sabi_commands *commands = &samsung->config->commands; |
527 | ||
5dea7a20 | 528 | set_brightness(samsung, bd->props.brightness); |
2d70b73a GKH |
529 | |
530 | if (bd->props.power == FB_BLANK_UNBLANK) | |
7e960711 | 531 | sabi_set_commandb(samsung, commands->set_backlight, 1); |
2d70b73a | 532 | else |
7e960711 | 533 | sabi_set_commandb(samsung, commands->set_backlight, 0); |
5dea7a20 | 534 | |
2d70b73a GKH |
535 | return 0; |
536 | } | |
537 | ||
538 | static const struct backlight_ops backlight_ops = { | |
539 | .get_brightness = get_brightness, | |
540 | .update_status = update_status, | |
541 | }; | |
542 | ||
84d482f2 | 543 | static int seclinux_rfkill_set(void *data, bool blocked) |
2d70b73a | 544 | { |
20db88e3 CC |
545 | struct samsung_rfkill *srfkill = data; |
546 | struct samsung_laptop *samsung = srfkill->samsung; | |
a6df4894 CC |
547 | const struct sabi_commands *commands = &samsung->config->commands; |
548 | ||
84d482f2 CC |
549 | return sabi_set_commandb(samsung, commands->set_wireless_button, |
550 | !blocked); | |
551 | } | |
552 | ||
553 | static struct rfkill_ops seclinux_rfkill_ops = { | |
554 | .set_block = seclinux_rfkill_set, | |
555 | }; | |
556 | ||
557 | static int swsmi_wireless_status(struct samsung_laptop *samsung, | |
558 | struct sabi_data *data) | |
559 | { | |
560 | const struct sabi_commands *commands = &samsung->config->commands; | |
561 | ||
562 | return sabi_command(samsung, commands->get_wireless_status, | |
563 | NULL, data); | |
564 | } | |
565 | ||
566 | static int swsmi_rfkill_set(void *priv, bool blocked) | |
567 | { | |
568 | struct samsung_rfkill *srfkill = priv; | |
569 | struct samsung_laptop *samsung = srfkill->samsung; | |
570 | const struct sabi_commands *commands = &samsung->config->commands; | |
571 | struct sabi_data data; | |
572 | int ret, i; | |
573 | ||
574 | ret = swsmi_wireless_status(samsung, &data); | |
575 | if (ret) | |
576 | return ret; | |
577 | ||
578 | /* Don't set the state for non-present devices */ | |
579 | for (i = 0; i < 4; i++) | |
580 | if (data.data[i] == 0x02) | |
581 | data.data[1] = 0; | |
582 | ||
583 | if (srfkill->type == RFKILL_TYPE_WLAN) | |
584 | data.data[WL_STATUS_WLAN] = !blocked; | |
585 | else if (srfkill->type == RFKILL_TYPE_BLUETOOTH) | |
586 | data.data[WL_STATUS_BT] = !blocked; | |
587 | ||
588 | return sabi_command(samsung, commands->set_wireless_status, | |
589 | &data, &data); | |
590 | } | |
591 | ||
592 | static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv) | |
593 | { | |
594 | struct samsung_rfkill *srfkill = priv; | |
595 | struct samsung_laptop *samsung = srfkill->samsung; | |
596 | struct sabi_data data; | |
597 | int ret; | |
598 | ||
599 | ret = swsmi_wireless_status(samsung, &data); | |
600 | if (ret) | |
601 | return ; | |
602 | ||
603 | if (srfkill->type == RFKILL_TYPE_WLAN) | |
604 | ret = data.data[WL_STATUS_WLAN]; | |
605 | else if (srfkill->type == RFKILL_TYPE_BLUETOOTH) | |
606 | ret = data.data[WL_STATUS_BT]; | |
2d70b73a | 607 | else |
84d482f2 | 608 | return ; |
2d70b73a | 609 | |
84d482f2 | 610 | rfkill_set_sw_state(rfkill, !ret); |
2d70b73a GKH |
611 | } |
612 | ||
84d482f2 CC |
613 | static struct rfkill_ops swsmi_rfkill_ops = { |
614 | .set_block = swsmi_rfkill_set, | |
615 | .query = swsmi_rfkill_query, | |
2d70b73a GKH |
616 | }; |
617 | ||
2d70b73a GKH |
618 | static ssize_t get_performance_level(struct device *dev, |
619 | struct device_attribute *attr, char *buf) | |
620 | { | |
5dea7a20 | 621 | struct samsung_laptop *samsung = dev_get_drvdata(dev); |
a6df4894 | 622 | const struct sabi_config *config = samsung->config; |
5dea7a20 | 623 | const struct sabi_commands *commands = &config->commands; |
7e960711 | 624 | struct sabi_data sretval; |
2d70b73a GKH |
625 | int retval; |
626 | int i; | |
627 | ||
628 | /* Read the state */ | |
7e960711 CC |
629 | retval = sabi_command(samsung, commands->get_performance_level, |
630 | NULL, &sretval); | |
2d70b73a GKH |
631 | if (retval) |
632 | return retval; | |
633 | ||
634 | /* The logic is backwards, yeah, lots of fun... */ | |
a6df4894 | 635 | for (i = 0; config->performance_levels[i].name; ++i) { |
7e960711 | 636 | if (sretval.data[0] == config->performance_levels[i].value) |
a6df4894 | 637 | return sprintf(buf, "%s\n", config->performance_levels[i].name); |
2d70b73a GKH |
638 | } |
639 | return sprintf(buf, "%s\n", "unknown"); | |
640 | } | |
641 | ||
642 | static ssize_t set_performance_level(struct device *dev, | |
643 | struct device_attribute *attr, const char *buf, | |
644 | size_t count) | |
645 | { | |
5dea7a20 | 646 | struct samsung_laptop *samsung = dev_get_drvdata(dev); |
a6df4894 | 647 | const struct sabi_config *config = samsung->config; |
5dea7a20 CC |
648 | const struct sabi_commands *commands = &config->commands; |
649 | int i; | |
a6df4894 | 650 | |
5dea7a20 CC |
651 | if (count < 1) |
652 | return count; | |
653 | ||
654 | for (i = 0; config->performance_levels[i].name; ++i) { | |
655 | const struct sabi_performance_level *level = | |
656 | &config->performance_levels[i]; | |
657 | if (!strncasecmp(level->name, buf, strlen(level->name))) { | |
7e960711 CC |
658 | sabi_set_commandb(samsung, |
659 | commands->set_performance_level, | |
660 | level->value); | |
5dea7a20 | 661 | break; |
2d70b73a | 662 | } |
2d70b73a | 663 | } |
5dea7a20 CC |
664 | |
665 | if (!config->performance_levels[i].name) | |
666 | return -EINVAL; | |
667 | ||
2d70b73a GKH |
668 | return count; |
669 | } | |
5dea7a20 | 670 | |
2d70b73a GKH |
671 | static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO, |
672 | get_performance_level, set_performance_level); | |
673 | ||
cb5b5c91 CC |
674 | static int read_battery_life_extender(struct samsung_laptop *samsung) |
675 | { | |
676 | const struct sabi_commands *commands = &samsung->config->commands; | |
677 | struct sabi_data data; | |
678 | int retval; | |
679 | ||
680 | if (commands->get_battery_life_extender == 0xFFFF) | |
681 | return -ENODEV; | |
682 | ||
683 | memset(&data, 0, sizeof(data)); | |
684 | data.data[0] = 0x80; | |
685 | retval = sabi_command(samsung, commands->get_battery_life_extender, | |
686 | &data, &data); | |
687 | ||
688 | if (retval) | |
689 | return retval; | |
690 | ||
691 | if (data.data[0] != 0 && data.data[0] != 1) | |
692 | return -ENODEV; | |
693 | ||
694 | return data.data[0]; | |
695 | } | |
696 | ||
697 | static int write_battery_life_extender(struct samsung_laptop *samsung, | |
698 | int enabled) | |
699 | { | |
700 | const struct sabi_commands *commands = &samsung->config->commands; | |
701 | struct sabi_data data; | |
702 | ||
703 | memset(&data, 0, sizeof(data)); | |
704 | data.data[0] = 0x80 | enabled; | |
705 | return sabi_command(samsung, commands->set_battery_life_extender, | |
706 | &data, NULL); | |
707 | } | |
708 | ||
709 | static ssize_t get_battery_life_extender(struct device *dev, | |
710 | struct device_attribute *attr, | |
711 | char *buf) | |
712 | { | |
713 | struct samsung_laptop *samsung = dev_get_drvdata(dev); | |
714 | int ret; | |
715 | ||
716 | ret = read_battery_life_extender(samsung); | |
717 | if (ret < 0) | |
718 | return ret; | |
719 | ||
720 | return sprintf(buf, "%d\n", ret); | |
721 | } | |
722 | ||
723 | static ssize_t set_battery_life_extender(struct device *dev, | |
724 | struct device_attribute *attr, | |
725 | const char *buf, size_t count) | |
726 | { | |
727 | struct samsung_laptop *samsung = dev_get_drvdata(dev); | |
728 | int ret, value; | |
729 | ||
730 | if (!count || sscanf(buf, "%i", &value) != 1) | |
731 | return -EINVAL; | |
732 | ||
733 | ret = write_battery_life_extender(samsung, !!value); | |
734 | if (ret < 0) | |
735 | return ret; | |
736 | ||
737 | return count; | |
738 | } | |
739 | ||
740 | static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO, | |
741 | get_battery_life_extender, set_battery_life_extender); | |
742 | ||
3a75d378 CC |
743 | static int read_usb_charge(struct samsung_laptop *samsung) |
744 | { | |
745 | const struct sabi_commands *commands = &samsung->config->commands; | |
746 | struct sabi_data data; | |
747 | int retval; | |
748 | ||
749 | if (commands->get_usb_charge == 0xFFFF) | |
750 | return -ENODEV; | |
751 | ||
752 | memset(&data, 0, sizeof(data)); | |
753 | data.data[0] = 0x80; | |
754 | retval = sabi_command(samsung, commands->get_usb_charge, | |
755 | &data, &data); | |
756 | ||
757 | if (retval) | |
758 | return retval; | |
759 | ||
760 | if (data.data[0] != 0 && data.data[0] != 1) | |
761 | return -ENODEV; | |
762 | ||
763 | return data.data[0]; | |
764 | } | |
765 | ||
766 | static int write_usb_charge(struct samsung_laptop *samsung, | |
767 | int enabled) | |
768 | { | |
769 | const struct sabi_commands *commands = &samsung->config->commands; | |
770 | struct sabi_data data; | |
771 | ||
772 | memset(&data, 0, sizeof(data)); | |
773 | data.data[0] = 0x80 | enabled; | |
774 | return sabi_command(samsung, commands->set_usb_charge, | |
775 | &data, NULL); | |
776 | } | |
777 | ||
778 | static ssize_t get_usb_charge(struct device *dev, | |
779 | struct device_attribute *attr, | |
780 | char *buf) | |
781 | { | |
782 | struct samsung_laptop *samsung = dev_get_drvdata(dev); | |
783 | int ret; | |
784 | ||
785 | ret = read_usb_charge(samsung); | |
786 | if (ret < 0) | |
787 | return ret; | |
788 | ||
789 | return sprintf(buf, "%d\n", ret); | |
790 | } | |
791 | ||
792 | static ssize_t set_usb_charge(struct device *dev, | |
793 | struct device_attribute *attr, | |
794 | const char *buf, size_t count) | |
795 | { | |
796 | struct samsung_laptop *samsung = dev_get_drvdata(dev); | |
797 | int ret, value; | |
798 | ||
799 | if (!count || sscanf(buf, "%i", &value) != 1) | |
800 | return -EINVAL; | |
801 | ||
802 | ret = write_usb_charge(samsung, !!value); | |
803 | if (ret < 0) | |
804 | return ret; | |
805 | ||
806 | return count; | |
807 | } | |
808 | ||
809 | static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO, | |
810 | get_usb_charge, set_usb_charge); | |
811 | ||
a66c1662 CC |
812 | static struct attribute *platform_attributes[] = { |
813 | &dev_attr_performance_level.attr, | |
cb5b5c91 | 814 | &dev_attr_battery_life_extender.attr, |
3a75d378 | 815 | &dev_attr_usb_charge.attr, |
a66c1662 CC |
816 | NULL |
817 | }; | |
2d70b73a | 818 | |
5dea7a20 CC |
819 | static int find_signature(void __iomem *memcheck, const char *testStr) |
820 | { | |
821 | int i = 0; | |
822 | int loca; | |
823 | ||
824 | for (loca = 0; loca < 0xffff; loca++) { | |
825 | char temp = readb(memcheck + loca); | |
826 | ||
827 | if (temp == testStr[i]) { | |
828 | if (i == strlen(testStr)-1) | |
829 | break; | |
830 | ++i; | |
831 | } else { | |
832 | i = 0; | |
833 | } | |
834 | } | |
835 | return loca; | |
836 | } | |
837 | ||
838 | static void samsung_rfkill_exit(struct samsung_laptop *samsung) | |
839 | { | |
84d482f2 CC |
840 | if (samsung->wlan.rfkill) { |
841 | rfkill_unregister(samsung->wlan.rfkill); | |
842 | rfkill_destroy(samsung->wlan.rfkill); | |
843 | samsung->wlan.rfkill = NULL; | |
844 | } | |
845 | if (samsung->bluetooth.rfkill) { | |
846 | rfkill_unregister(samsung->bluetooth.rfkill); | |
847 | rfkill_destroy(samsung->bluetooth.rfkill); | |
848 | samsung->bluetooth.rfkill = NULL; | |
5dea7a20 CC |
849 | } |
850 | } | |
851 | ||
84d482f2 CC |
852 | static int samsung_new_rfkill(struct samsung_laptop *samsung, |
853 | struct samsung_rfkill *arfkill, | |
854 | const char *name, enum rfkill_type type, | |
855 | const struct rfkill_ops *ops, | |
856 | int blocked) | |
5dea7a20 | 857 | { |
84d482f2 CC |
858 | struct rfkill **rfkill = &arfkill->rfkill; |
859 | int ret; | |
5dea7a20 | 860 | |
84d482f2 CC |
861 | arfkill->type = type; |
862 | arfkill->samsung = samsung; | |
863 | ||
864 | *rfkill = rfkill_alloc(name, &samsung->platform_device->dev, | |
865 | type, ops, arfkill); | |
866 | ||
867 | if (!*rfkill) | |
868 | return -EINVAL; | |
869 | ||
870 | if (blocked != -1) | |
871 | rfkill_init_sw_state(*rfkill, blocked); | |
872 | ||
873 | ret = rfkill_register(*rfkill); | |
874 | if (ret) { | |
875 | rfkill_destroy(*rfkill); | |
876 | *rfkill = NULL; | |
877 | return ret; | |
5dea7a20 | 878 | } |
84d482f2 CC |
879 | return 0; |
880 | } | |
5dea7a20 | 881 | |
84d482f2 CC |
882 | static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung) |
883 | { | |
884 | return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan", | |
885 | RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1); | |
886 | } | |
887 | ||
888 | static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung) | |
889 | { | |
890 | struct sabi_data data; | |
891 | int ret; | |
892 | ||
893 | ret = swsmi_wireless_status(samsung, &data); | |
20db88e3 CC |
894 | if (ret) { |
895 | /* Some swsmi laptops use the old seclinux way to control | |
896 | * wireless devices */ | |
897 | if (ret == -EINVAL) | |
898 | ret = samsung_rfkill_init_seclinux(samsung); | |
84d482f2 | 899 | return ret; |
20db88e3 | 900 | } |
84d482f2 CC |
901 | |
902 | /* 0x02 seems to mean that the device is no present/available */ | |
903 | ||
904 | if (data.data[WL_STATUS_WLAN] != 0x02) | |
905 | ret = samsung_new_rfkill(samsung, &samsung->wlan, | |
906 | "samsung-wlan", | |
907 | RFKILL_TYPE_WLAN, | |
908 | &swsmi_rfkill_ops, | |
909 | !data.data[WL_STATUS_WLAN]); | |
910 | if (ret) | |
911 | goto exit; | |
912 | ||
913 | if (data.data[WL_STATUS_BT] != 0x02) | |
914 | ret = samsung_new_rfkill(samsung, &samsung->bluetooth, | |
915 | "samsung-bluetooth", | |
916 | RFKILL_TYPE_BLUETOOTH, | |
917 | &swsmi_rfkill_ops, | |
918 | !data.data[WL_STATUS_BT]); | |
919 | if (ret) | |
920 | goto exit; | |
921 | ||
922 | exit: | |
923 | if (ret) | |
924 | samsung_rfkill_exit(samsung); | |
925 | ||
926 | return ret; | |
927 | } | |
928 | ||
929 | static int __init samsung_rfkill_init(struct samsung_laptop *samsung) | |
930 | { | |
931 | if (samsung->config->sabi_version == 2) | |
932 | return samsung_rfkill_init_seclinux(samsung); | |
933 | if (samsung->config->sabi_version == 3) | |
934 | return samsung_rfkill_init_swsmi(samsung); | |
5dea7a20 CC |
935 | return 0; |
936 | } | |
937 | ||
f674ebf1 CC |
938 | static int kbd_backlight_enable(struct samsung_laptop *samsung) |
939 | { | |
940 | const struct sabi_commands *commands = &samsung->config->commands; | |
941 | struct sabi_data data; | |
942 | int retval; | |
943 | ||
944 | if (commands->kbd_backlight == 0xFFFF) | |
945 | return -ENODEV; | |
946 | ||
947 | memset(&data, 0, sizeof(data)); | |
948 | data.d0 = 0xaabb; | |
949 | retval = sabi_command(samsung, commands->kbd_backlight, | |
950 | &data, &data); | |
951 | ||
952 | if (retval) | |
953 | return retval; | |
954 | ||
955 | if (data.d0 != 0xccdd) | |
956 | return -ENODEV; | |
957 | return 0; | |
958 | } | |
959 | ||
960 | static int kbd_backlight_read(struct samsung_laptop *samsung) | |
961 | { | |
962 | const struct sabi_commands *commands = &samsung->config->commands; | |
963 | struct sabi_data data; | |
964 | int retval; | |
965 | ||
966 | memset(&data, 0, sizeof(data)); | |
967 | data.data[0] = 0x81; | |
968 | retval = sabi_command(samsung, commands->kbd_backlight, | |
969 | &data, &data); | |
970 | ||
971 | if (retval) | |
972 | return retval; | |
973 | ||
974 | return data.data[0]; | |
975 | } | |
976 | ||
977 | static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness) | |
978 | { | |
979 | const struct sabi_commands *commands = &samsung->config->commands; | |
980 | struct sabi_data data; | |
981 | ||
982 | memset(&data, 0, sizeof(data)); | |
983 | data.d0 = 0x82 | ((brightness & 0xFF) << 8); | |
984 | return sabi_command(samsung, commands->kbd_backlight, | |
985 | &data, NULL); | |
986 | } | |
987 | ||
988 | static void kbd_led_update(struct work_struct *work) | |
989 | { | |
990 | struct samsung_laptop *samsung; | |
991 | ||
992 | samsung = container_of(work, struct samsung_laptop, kbd_led_work); | |
993 | kbd_backlight_write(samsung, samsung->kbd_led_wk); | |
994 | } | |
995 | ||
996 | static void kbd_led_set(struct led_classdev *led_cdev, | |
997 | enum led_brightness value) | |
998 | { | |
999 | struct samsung_laptop *samsung; | |
1000 | ||
1001 | samsung = container_of(led_cdev, struct samsung_laptop, kbd_led); | |
1002 | ||
1003 | if (value > samsung->kbd_led.max_brightness) | |
1004 | value = samsung->kbd_led.max_brightness; | |
1005 | else if (value < 0) | |
1006 | value = 0; | |
1007 | ||
1008 | samsung->kbd_led_wk = value; | |
1009 | queue_work(samsung->led_workqueue, &samsung->kbd_led_work); | |
1010 | } | |
1011 | ||
1012 | static enum led_brightness kbd_led_get(struct led_classdev *led_cdev) | |
1013 | { | |
1014 | struct samsung_laptop *samsung; | |
1015 | ||
1016 | samsung = container_of(led_cdev, struct samsung_laptop, kbd_led); | |
1017 | return kbd_backlight_read(samsung); | |
1018 | } | |
1019 | ||
1020 | static void samsung_leds_exit(struct samsung_laptop *samsung) | |
1021 | { | |
1022 | if (!IS_ERR_OR_NULL(samsung->kbd_led.dev)) | |
1023 | led_classdev_unregister(&samsung->kbd_led); | |
1024 | if (samsung->led_workqueue) | |
1025 | destroy_workqueue(samsung->led_workqueue); | |
1026 | } | |
1027 | ||
1028 | static int __init samsung_leds_init(struct samsung_laptop *samsung) | |
1029 | { | |
1030 | int ret = 0; | |
1031 | ||
1032 | samsung->led_workqueue = create_singlethread_workqueue("led_workqueue"); | |
1033 | if (!samsung->led_workqueue) | |
1034 | return -ENOMEM; | |
1035 | ||
1036 | if (kbd_backlight_enable(samsung) >= 0) { | |
1037 | INIT_WORK(&samsung->kbd_led_work, kbd_led_update); | |
1038 | ||
1039 | samsung->kbd_led.name = "samsung::kbd_backlight"; | |
1040 | samsung->kbd_led.brightness_set = kbd_led_set; | |
1041 | samsung->kbd_led.brightness_get = kbd_led_get; | |
1042 | samsung->kbd_led.max_brightness = 8; | |
1043 | ||
1044 | ret = led_classdev_register(&samsung->platform_device->dev, | |
1045 | &samsung->kbd_led); | |
1046 | } | |
1047 | ||
1048 | if (ret) | |
1049 | samsung_leds_exit(samsung); | |
1050 | ||
1051 | return ret; | |
1052 | } | |
1053 | ||
5dea7a20 CC |
1054 | static void samsung_backlight_exit(struct samsung_laptop *samsung) |
1055 | { | |
1056 | if (samsung->backlight_device) { | |
1057 | backlight_device_unregister(samsung->backlight_device); | |
1058 | samsung->backlight_device = NULL; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | static int __init samsung_backlight_init(struct samsung_laptop *samsung) | |
1063 | { | |
1064 | struct backlight_device *bd; | |
1065 | struct backlight_properties props; | |
1066 | ||
f34cd9ca CC |
1067 | if (!samsung->handle_backlight) |
1068 | return 0; | |
1069 | ||
5dea7a20 CC |
1070 | memset(&props, 0, sizeof(struct backlight_properties)); |
1071 | props.type = BACKLIGHT_PLATFORM; | |
1072 | props.max_brightness = samsung->config->max_brightness - | |
1073 | samsung->config->min_brightness; | |
1074 | ||
1075 | bd = backlight_device_register("samsung", | |
1076 | &samsung->platform_device->dev, | |
1077 | samsung, &backlight_ops, | |
1078 | &props); | |
1079 | if (IS_ERR(bd)) | |
1080 | return PTR_ERR(bd); | |
1081 | ||
1082 | samsung->backlight_device = bd; | |
1083 | samsung->backlight_device->props.brightness = read_brightness(samsung); | |
1084 | samsung->backlight_device->props.power = FB_BLANK_UNBLANK; | |
1085 | backlight_update_status(samsung->backlight_device); | |
1086 | ||
1087 | return 0; | |
1088 | } | |
1089 | ||
a66c1662 CC |
1090 | static mode_t samsung_sysfs_is_visible(struct kobject *kobj, |
1091 | struct attribute *attr, int idx) | |
1092 | { | |
1093 | struct device *dev = container_of(kobj, struct device, kobj); | |
1094 | struct platform_device *pdev = to_platform_device(dev); | |
1095 | struct samsung_laptop *samsung = platform_get_drvdata(pdev); | |
1096 | bool ok = true; | |
1097 | ||
1098 | if (attr == &dev_attr_performance_level.attr) | |
1099 | ok = !!samsung->config->performance_levels[0].name; | |
cb5b5c91 CC |
1100 | if (attr == &dev_attr_battery_life_extender.attr) |
1101 | ok = !!(read_battery_life_extender(samsung) >= 0); | |
3a75d378 CC |
1102 | if (attr == &dev_attr_usb_charge.attr) |
1103 | ok = !!(read_usb_charge(samsung) >= 0); | |
a66c1662 CC |
1104 | |
1105 | return ok ? attr->mode : 0; | |
1106 | } | |
1107 | ||
1108 | static struct attribute_group platform_attribute_group = { | |
1109 | .is_visible = samsung_sysfs_is_visible, | |
1110 | .attrs = platform_attributes | |
1111 | }; | |
1112 | ||
5dea7a20 CC |
1113 | static void samsung_sysfs_exit(struct samsung_laptop *samsung) |
1114 | { | |
a66c1662 CC |
1115 | struct platform_device *device = samsung->platform_device; |
1116 | ||
1117 | sysfs_remove_group(&device->dev.kobj, &platform_attribute_group); | |
5dea7a20 CC |
1118 | } |
1119 | ||
1120 | static int __init samsung_sysfs_init(struct samsung_laptop *samsung) | |
1121 | { | |
a66c1662 CC |
1122 | struct platform_device *device = samsung->platform_device; |
1123 | ||
1124 | return sysfs_create_group(&device->dev.kobj, &platform_attribute_group); | |
1125 | ||
5dea7a20 CC |
1126 | } |
1127 | ||
5b80fc40 CC |
1128 | static int show_call(struct seq_file *m, void *data) |
1129 | { | |
1130 | struct samsung_laptop *samsung = m->private; | |
1131 | struct sabi_data *sdata = &samsung->debug.data; | |
1132 | int ret; | |
1133 | ||
1134 | seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n", | |
1135 | samsung->debug.command, | |
1136 | sdata->d0, sdata->d1, sdata->d2, sdata->d3); | |
1137 | ||
1138 | ret = sabi_command(samsung, samsung->debug.command, sdata, sdata); | |
1139 | ||
1140 | if (ret) { | |
1141 | seq_printf(m, "SABI command 0x%04x failed\n", | |
1142 | samsung->debug.command); | |
1143 | return ret; | |
1144 | } | |
1145 | ||
1146 | seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n", | |
1147 | sdata->d0, sdata->d1, sdata->d2, sdata->d3); | |
1148 | return 0; | |
1149 | } | |
1150 | ||
1151 | static int samsung_debugfs_open(struct inode *inode, struct file *file) | |
1152 | { | |
1153 | return single_open(file, show_call, inode->i_private); | |
1154 | } | |
1155 | ||
1156 | static const struct file_operations samsung_laptop_call_io_ops = { | |
1157 | .owner = THIS_MODULE, | |
1158 | .open = samsung_debugfs_open, | |
1159 | .read = seq_read, | |
1160 | .llseek = seq_lseek, | |
1161 | .release = single_release, | |
1162 | }; | |
1163 | ||
1164 | static void samsung_debugfs_exit(struct samsung_laptop *samsung) | |
1165 | { | |
1166 | debugfs_remove_recursive(samsung->debug.root); | |
1167 | } | |
1168 | ||
1169 | static int samsung_debugfs_init(struct samsung_laptop *samsung) | |
1170 | { | |
1171 | struct dentry *dent; | |
1172 | ||
1173 | samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL); | |
1174 | if (!samsung->debug.root) { | |
1175 | pr_err("failed to create debugfs directory"); | |
1176 | goto error_debugfs; | |
1177 | } | |
1178 | ||
1179 | samsung->debug.f0000_wrapper.data = samsung->f0000_segment; | |
1180 | samsung->debug.f0000_wrapper.size = 0xffff; | |
1181 | ||
1182 | samsung->debug.data_wrapper.data = &samsung->debug.data; | |
1183 | samsung->debug.data_wrapper.size = sizeof(samsung->debug.data); | |
1184 | ||
6f6ae06e CC |
1185 | samsung->debug.sdiag_wrapper.data = samsung->sdiag; |
1186 | samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag); | |
1187 | ||
5b80fc40 CC |
1188 | dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR, |
1189 | samsung->debug.root, &samsung->debug.command); | |
1190 | if (!dent) | |
1191 | goto error_debugfs; | |
1192 | ||
1193 | dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root, | |
1194 | &samsung->debug.data.d0); | |
1195 | if (!dent) | |
1196 | goto error_debugfs; | |
1197 | ||
1198 | dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root, | |
1199 | &samsung->debug.data.d1); | |
1200 | if (!dent) | |
1201 | goto error_debugfs; | |
1202 | ||
1203 | dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root, | |
1204 | &samsung->debug.data.d2); | |
1205 | if (!dent) | |
1206 | goto error_debugfs; | |
1207 | ||
1208 | dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root, | |
1209 | &samsung->debug.data.d3); | |
1210 | if (!dent) | |
1211 | goto error_debugfs; | |
1212 | ||
1213 | dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR, | |
1214 | samsung->debug.root, | |
1215 | &samsung->debug.data_wrapper); | |
1216 | if (!dent) | |
1217 | goto error_debugfs; | |
1218 | ||
1219 | dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR, | |
1220 | samsung->debug.root, | |
1221 | &samsung->debug.f0000_wrapper); | |
1222 | if (!dent) | |
1223 | goto error_debugfs; | |
1224 | ||
1225 | dent = debugfs_create_file("call", S_IFREG | S_IRUGO, | |
1226 | samsung->debug.root, samsung, | |
1227 | &samsung_laptop_call_io_ops); | |
1228 | if (!dent) | |
1229 | goto error_debugfs; | |
1230 | ||
6f6ae06e CC |
1231 | dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR, |
1232 | samsung->debug.root, | |
1233 | &samsung->debug.sdiag_wrapper); | |
1234 | if (!dent) | |
1235 | goto error_debugfs; | |
1236 | ||
5b80fc40 CC |
1237 | return 0; |
1238 | ||
1239 | error_debugfs: | |
1240 | samsung_debugfs_exit(samsung); | |
1241 | return -ENOMEM; | |
1242 | } | |
1243 | ||
5dea7a20 CC |
1244 | static void samsung_sabi_exit(struct samsung_laptop *samsung) |
1245 | { | |
1246 | const struct sabi_config *config = samsung->config; | |
1247 | ||
1248 | /* Turn off "Linux" mode in the BIOS */ | |
1249 | if (config && config->commands.set_linux != 0xff) | |
7e960711 | 1250 | sabi_set_commandb(samsung, config->commands.set_linux, 0x80); |
5dea7a20 CC |
1251 | |
1252 | if (samsung->sabi_iface) { | |
1253 | iounmap(samsung->sabi_iface); | |
1254 | samsung->sabi_iface = NULL; | |
1255 | } | |
1256 | if (samsung->f0000_segment) { | |
1257 | iounmap(samsung->f0000_segment); | |
1258 | samsung->f0000_segment = NULL; | |
1259 | } | |
1260 | ||
1261 | samsung->config = NULL; | |
1262 | } | |
1263 | ||
49dd7730 CC |
1264 | static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca, |
1265 | unsigned int ifaceP) | |
5dea7a20 CC |
1266 | { |
1267 | const struct sabi_config *config = samsung->config; | |
1268 | ||
1269 | printk(KERN_DEBUG "This computer supports SABI==%x\n", | |
1270 | loca + 0xf0000 - 6); | |
49dd7730 | 1271 | |
5dea7a20 CC |
1272 | printk(KERN_DEBUG "SABI header:\n"); |
1273 | printk(KERN_DEBUG " SMI Port Number = 0x%04x\n", | |
1274 | readw(samsung->sabi + config->header_offsets.port)); | |
1275 | printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n", | |
1276 | readb(samsung->sabi + config->header_offsets.iface_func)); | |
1277 | printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n", | |
1278 | readb(samsung->sabi + config->header_offsets.en_mem)); | |
1279 | printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n", | |
1280 | readb(samsung->sabi + config->header_offsets.re_mem)); | |
1281 | printk(KERN_DEBUG " SABI data offset = 0x%04x\n", | |
1282 | readw(samsung->sabi + config->header_offsets.data_offset)); | |
1283 | printk(KERN_DEBUG " SABI data segment = 0x%04x\n", | |
1284 | readw(samsung->sabi + config->header_offsets.data_segment)); | |
5dea7a20 | 1285 | |
49dd7730 | 1286 | printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP); |
5dea7a20 CC |
1287 | } |
1288 | ||
6f6ae06e CC |
1289 | static void __init samsung_sabi_diag(struct samsung_laptop *samsung) |
1290 | { | |
1291 | int loca = find_signature(samsung->f0000_segment, "SDiaG@"); | |
1292 | int i; | |
1293 | ||
1294 | if (loca == 0xffff) | |
1295 | return ; | |
1296 | ||
1297 | /* Example: | |
1298 | * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A | |
1299 | * | |
1300 | * Product name: 90X3A | |
1301 | * BIOS Version: 07HL | |
1302 | */ | |
1303 | loca += 1; | |
1304 | for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) { | |
1305 | char temp = readb(samsung->f0000_segment + loca); | |
1306 | ||
1307 | if (isalnum(temp) || temp == '/' || temp == '-') | |
1308 | samsung->sdiag[i++] = temp; | |
1309 | else | |
1310 | break ; | |
1311 | } | |
1312 | ||
1313 | if (debug && samsung->sdiag[0]) | |
1314 | pr_info("sdiag: %s", samsung->sdiag); | |
1315 | } | |
1316 | ||
5dea7a20 CC |
1317 | static int __init samsung_sabi_init(struct samsung_laptop *samsung) |
1318 | { | |
1319 | const struct sabi_config *config = NULL; | |
1320 | const struct sabi_commands *commands; | |
1321 | unsigned int ifaceP; | |
1322 | int ret = 0; | |
1323 | int i; | |
1324 | int loca; | |
1325 | ||
1326 | samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff); | |
1327 | if (!samsung->f0000_segment) { | |
3be324a9 CC |
1328 | if (debug || force) |
1329 | pr_err("Can't map the segment at 0xf0000\n"); | |
5dea7a20 CC |
1330 | ret = -EINVAL; |
1331 | goto exit; | |
1332 | } | |
1333 | ||
6f6ae06e CC |
1334 | samsung_sabi_diag(samsung); |
1335 | ||
5dea7a20 CC |
1336 | /* Try to find one of the signatures in memory to find the header */ |
1337 | for (i = 0; sabi_configs[i].test_string != 0; ++i) { | |
1338 | samsung->config = &sabi_configs[i]; | |
1339 | loca = find_signature(samsung->f0000_segment, | |
1340 | samsung->config->test_string); | |
1341 | if (loca != 0xffff) | |
1342 | break; | |
1343 | } | |
1344 | ||
1345 | if (loca == 0xffff) { | |
3be324a9 CC |
1346 | if (debug || force) |
1347 | pr_err("This computer does not support SABI\n"); | |
5dea7a20 CC |
1348 | ret = -ENODEV; |
1349 | goto exit; | |
1350 | } | |
1351 | ||
1352 | config = samsung->config; | |
1353 | commands = &config->commands; | |
1354 | ||
1355 | /* point to the SMI port Number */ | |
1356 | loca += 1; | |
1357 | samsung->sabi = (samsung->f0000_segment + loca); | |
1358 | ||
5dea7a20 CC |
1359 | /* Get a pointer to the SABI Interface */ |
1360 | ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4; | |
1361 | ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff; | |
49dd7730 CC |
1362 | |
1363 | if (debug) | |
1364 | samsung_sabi_infos(samsung, loca, ifaceP); | |
1365 | ||
5dea7a20 CC |
1366 | samsung->sabi_iface = ioremap_nocache(ifaceP, 16); |
1367 | if (!samsung->sabi_iface) { | |
1368 | pr_err("Can't remap %x\n", ifaceP); | |
1369 | ret = -EINVAL; | |
1370 | goto exit; | |
1371 | } | |
1372 | ||
5dea7a20 CC |
1373 | /* Turn on "Linux" mode in the BIOS */ |
1374 | if (commands->set_linux != 0xff) { | |
7e960711 CC |
1375 | int retval = sabi_set_commandb(samsung, |
1376 | commands->set_linux, 0x81); | |
5dea7a20 CC |
1377 | if (retval) { |
1378 | pr_warn("Linux mode was not set!\n"); | |
1379 | ret = -ENODEV; | |
1380 | goto exit; | |
1381 | } | |
1382 | } | |
1383 | ||
1384 | /* Check for stepping quirk */ | |
f34cd9ca CC |
1385 | if (samsung->handle_backlight) |
1386 | check_for_stepping_quirk(samsung); | |
5dea7a20 | 1387 | |
2e777187 CC |
1388 | pr_info("detected SABI interface: %s\n", |
1389 | samsung->config->test_string); | |
1390 | ||
5dea7a20 CC |
1391 | exit: |
1392 | if (ret) | |
1393 | samsung_sabi_exit(samsung); | |
1394 | ||
1395 | return ret; | |
1396 | } | |
1397 | ||
1398 | static void samsung_platform_exit(struct samsung_laptop *samsung) | |
1399 | { | |
1400 | if (samsung->platform_device) { | |
1401 | platform_device_unregister(samsung->platform_device); | |
1402 | samsung->platform_device = NULL; | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | static int __init samsung_platform_init(struct samsung_laptop *samsung) | |
1407 | { | |
1408 | struct platform_device *pdev; | |
1409 | ||
1410 | pdev = platform_device_register_simple("samsung", -1, NULL, 0); | |
1411 | if (IS_ERR(pdev)) | |
1412 | return PTR_ERR(pdev); | |
1413 | ||
1414 | samsung->platform_device = pdev; | |
1415 | platform_set_drvdata(samsung->platform_device, samsung); | |
1416 | return 0; | |
1417 | } | |
1418 | ||
2d70b73a GKH |
1419 | static struct dmi_system_id __initdata samsung_dmi_table[] = { |
1420 | { | |
2d70b73a GKH |
1421 | .matches = { |
1422 | DMI_MATCH(DMI_SYS_VENDOR, | |
1423 | "SAMSUNG ELECTRONICS CO., LTD."), | |
3be324a9 | 1424 | DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */ |
2d70b73a | 1425 | }, |
2d70b73a GKH |
1426 | }, |
1427 | { | |
2d70b73a GKH |
1428 | .matches = { |
1429 | DMI_MATCH(DMI_SYS_VENDOR, | |
1430 | "SAMSUNG ELECTRONICS CO., LTD."), | |
3be324a9 | 1431 | DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */ |
2d70b73a | 1432 | }, |
2d70b73a | 1433 | }, |
78a7539b | 1434 | { |
78a7539b TC |
1435 | .matches = { |
1436 | DMI_MATCH(DMI_SYS_VENDOR, | |
1437 | "SAMSUNG ELECTRONICS CO., LTD."), | |
3be324a9 | 1438 | DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */ |
78a7539b | 1439 | }, |
78a7539b | 1440 | }, |
f689c875 | 1441 | { |
f689c875 RGS |
1442 | .matches = { |
1443 | DMI_MATCH(DMI_SYS_VENDOR, | |
1444 | "SAMSUNG ELECTRONICS CO., LTD."), | |
3be324a9 | 1445 | DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */ |
f689c875 | 1446 | }, |
7b3c257c | 1447 | }, |
2d70b73a GKH |
1448 | { }, |
1449 | }; | |
1450 | MODULE_DEVICE_TABLE(dmi, samsung_dmi_table); | |
1451 | ||
5dea7a20 | 1452 | static struct platform_device *samsung_platform_device; |
2d70b73a GKH |
1453 | |
1454 | static int __init samsung_init(void) | |
1455 | { | |
5dea7a20 CC |
1456 | struct samsung_laptop *samsung; |
1457 | int ret; | |
2d70b73a | 1458 | |
2d70b73a GKH |
1459 | if (!force && !dmi_check_system(samsung_dmi_table)) |
1460 | return -ENODEV; | |
1461 | ||
a6df4894 CC |
1462 | samsung = kzalloc(sizeof(*samsung), GFP_KERNEL); |
1463 | if (!samsung) | |
1464 | return -ENOMEM; | |
1465 | ||
1466 | mutex_init(&samsung->sabi_mutex); | |
f34cd9ca CC |
1467 | samsung->handle_backlight = true; |
1468 | ||
1469 | #ifdef CONFIG_ACPI | |
1470 | /* Don't handle backlight here if the acpi video already handle it */ | |
3be324a9 | 1471 | if (acpi_video_backlight_support()) |
f34cd9ca | 1472 | samsung->handle_backlight = false; |
f34cd9ca | 1473 | #endif |
5dea7a20 CC |
1474 | ret = samsung_platform_init(samsung); |
1475 | if (ret) | |
1476 | goto error_platform; | |
1477 | ||
1478 | ret = samsung_sabi_init(samsung); | |
1479 | if (ret) | |
1480 | goto error_sabi; | |
1481 | ||
3be324a9 CC |
1482 | #ifdef CONFIG_ACPI |
1483 | /* Only log that if we are really on a sabi platform */ | |
1484 | if (acpi_video_backlight_support()) | |
1485 | pr_info("Backlight controlled by ACPI video driver\n"); | |
1486 | #endif | |
1487 | ||
5dea7a20 CC |
1488 | ret = samsung_sysfs_init(samsung); |
1489 | if (ret) | |
1490 | goto error_sysfs; | |
1491 | ||
1492 | ret = samsung_backlight_init(samsung); | |
1493 | if (ret) | |
1494 | goto error_backlight; | |
1495 | ||
1496 | ret = samsung_rfkill_init(samsung); | |
1497 | if (ret) | |
1498 | goto error_rfkill; | |
1499 | ||
f674ebf1 CC |
1500 | ret = samsung_leds_init(samsung); |
1501 | if (ret) | |
1502 | goto error_leds; | |
1503 | ||
5b80fc40 CC |
1504 | ret = samsung_debugfs_init(samsung); |
1505 | if (ret) | |
1506 | goto error_debugfs; | |
1507 | ||
5dea7a20 CC |
1508 | samsung_platform_device = samsung->platform_device; |
1509 | return ret; | |
1510 | ||
5b80fc40 | 1511 | error_debugfs: |
f674ebf1 CC |
1512 | samsung_leds_exit(samsung); |
1513 | error_leds: | |
5b80fc40 | 1514 | samsung_rfkill_exit(samsung); |
5dea7a20 CC |
1515 | error_rfkill: |
1516 | samsung_backlight_exit(samsung); | |
1517 | error_backlight: | |
1518 | samsung_sysfs_exit(samsung); | |
1519 | error_sysfs: | |
1520 | samsung_sabi_exit(samsung); | |
1521 | error_sabi: | |
1522 | samsung_platform_exit(samsung); | |
1523 | error_platform: | |
a6df4894 | 1524 | kfree(samsung); |
5dea7a20 | 1525 | return ret; |
2d70b73a GKH |
1526 | } |
1527 | ||
1528 | static void __exit samsung_exit(void) | |
1529 | { | |
5dea7a20 CC |
1530 | struct samsung_laptop *samsung; |
1531 | ||
1532 | samsung = platform_get_drvdata(samsung_platform_device); | |
1533 | ||
5b80fc40 | 1534 | samsung_debugfs_exit(samsung); |
f674ebf1 | 1535 | samsung_leds_exit(samsung); |
5dea7a20 CC |
1536 | samsung_rfkill_exit(samsung); |
1537 | samsung_backlight_exit(samsung); | |
1538 | samsung_sysfs_exit(samsung); | |
1539 | samsung_sabi_exit(samsung); | |
1540 | samsung_platform_exit(samsung); | |
a6df4894 | 1541 | |
a6df4894 | 1542 | kfree(samsung); |
5dea7a20 | 1543 | samsung_platform_device = NULL; |
2d70b73a GKH |
1544 | } |
1545 | ||
1546 | module_init(samsung_init); | |
1547 | module_exit(samsung_exit); | |
1548 | ||
1549 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>"); | |
1550 | MODULE_DESCRIPTION("Samsung Backlight driver"); | |
1551 | MODULE_LICENSE("GPL"); |