blob: 5047642d166261c7e14db6c9b2039e1d4a0995b8 [file] [log] [blame]
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001/*
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>
Corentin Charyf674ebf2011-11-26 11:00:08 +010020#include <linux/leds.h>
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050021#include <linux/fb.h>
22#include <linux/dmi.h>
23#include <linux/platform_device.h>
24#include <linux/rfkill.h>
Corentin Charyf34cd9c2011-11-26 11:00:00 +010025#include <linux/acpi.h>
Corentin Chary5b80fc42011-11-26 11:00:03 +010026#include <linux/seq_file.h>
27#include <linux/debugfs.h>
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050028
29/*
30 * This driver is needed because a number of Samsung laptops do not hook
31 * their control settings through ACPI. So we have to poke around in the
32 * BIOS to do things like brightness values, and "special" key controls.
33 */
34
35/*
36 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
37 * be reserved by the BIOS (which really doesn't make much sense), we tell
38 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
39 */
40#define MAX_BRIGHT 0x07
41
42
43#define SABI_IFACE_MAIN 0x00
44#define SABI_IFACE_SUB 0x02
45#define SABI_IFACE_COMPLETE 0x04
46#define SABI_IFACE_DATA 0x05
47
Corentin Chary84d482f2011-11-26 11:00:09 +010048#define WL_STATUS_WLAN 0x0
49#define WL_STATUS_BT 0x2
50
Corentin Chary7e960712011-11-26 11:00:02 +010051/* Structure get/set data using sabi */
52struct sabi_data {
53 union {
54 struct {
55 u32 d0;
56 u32 d1;
57 u16 d2;
58 u8 d3;
59 };
60 u8 data[11];
61 };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050062};
63
64struct sabi_header_offsets {
65 u8 port;
66 u8 re_mem;
67 u8 iface_func;
68 u8 en_mem;
69 u8 data_offset;
70 u8 data_segment;
71};
72
73struct sabi_commands {
74 /*
75 * Brightness is 0 - 8, as described above.
76 * Value 0 is for the BIOS to use
77 */
Corentin Chary7e960712011-11-26 11:00:02 +010078 u16 get_brightness;
79 u16 set_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050080
81 /*
82 * first byte:
83 * 0x00 - wireless is off
84 * 0x01 - wireless is on
85 * second byte:
86 * 0x02 - 3G is off
87 * 0x03 - 3G is on
88 * TODO, verify 3G is correct, that doesn't seem right...
89 */
Corentin Chary7e960712011-11-26 11:00:02 +010090 u16 get_wireless_button;
91 u16 set_wireless_button;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050092
93 /* 0 is off, 1 is on */
Corentin Chary7e960712011-11-26 11:00:02 +010094 u16 get_backlight;
95 u16 set_backlight;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -050096
97 /*
98 * 0x80 or 0x00 - no action
99 * 0x81 - recovery key pressed
100 */
Corentin Chary7e960712011-11-26 11:00:02 +0100101 u16 get_recovery_mode;
102 u16 set_recovery_mode;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500103
104 /*
105 * on seclinux: 0 is low, 1 is high,
106 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
107 */
Corentin Chary7e960712011-11-26 11:00:02 +0100108 u16 get_performance_level;
109 u16 set_performance_level;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500110
Corentin Charycb5b5c92011-11-26 11:00:05 +0100111 /* 0x80 is off, 0x81 is on */
112 u16 get_battery_life_extender;
113 u16 set_battery_life_extender;
114
Corentin Chary3a75d372011-11-26 11:00:06 +0100115 /* 0x80 is off, 0x81 is on */
116 u16 get_usb_charge;
117 u16 set_usb_charge;
118
Corentin Chary84d482f2011-11-26 11:00:09 +0100119 /* the first byte is for bluetooth and the third one is for wlan */
120 u16 get_wireless_status;
121 u16 set_wireless_status;
122
Corentin Charyf674ebf2011-11-26 11:00:08 +0100123 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
124 u16 kbd_backlight;
125
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500126 /*
127 * Tell the BIOS that Linux is running on this machine.
128 * 81 is on, 80 is off
129 */
Corentin Chary7e960712011-11-26 11:00:02 +0100130 u16 set_linux;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500131};
132
133struct sabi_performance_level {
134 const char *name;
Corentin Chary7e960712011-11-26 11:00:02 +0100135 u16 value;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500136};
137
138struct sabi_config {
Corentin Chary84d482f2011-11-26 11:00:09 +0100139 int sabi_version;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500140 const char *test_string;
141 u16 main_function;
142 const struct sabi_header_offsets header_offsets;
143 const struct sabi_commands commands;
144 const struct sabi_performance_level performance_levels[4];
145 u8 min_brightness;
146 u8 max_brightness;
147};
148
149static const struct sabi_config sabi_configs[] = {
150 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100151 /* I don't know if it is really 2, but it it is
152 * less than 3 anyway */
153 .sabi_version = 2,
154
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500155 .test_string = "SECLINUX",
156
157 .main_function = 0x4c49,
158
159 .header_offsets = {
160 .port = 0x00,
161 .re_mem = 0x02,
162 .iface_func = 0x03,
163 .en_mem = 0x04,
164 .data_offset = 0x05,
165 .data_segment = 0x07,
166 },
167
168 .commands = {
169 .get_brightness = 0x00,
170 .set_brightness = 0x01,
171
172 .get_wireless_button = 0x02,
173 .set_wireless_button = 0x03,
174
175 .get_backlight = 0x04,
176 .set_backlight = 0x05,
177
178 .get_recovery_mode = 0x06,
179 .set_recovery_mode = 0x07,
180
181 .get_performance_level = 0x08,
182 .set_performance_level = 0x09,
183
Corentin Charycb5b5c92011-11-26 11:00:05 +0100184 .get_battery_life_extender = 0xFFFF,
185 .set_battery_life_extender = 0xFFFF,
186
Corentin Chary3a75d372011-11-26 11:00:06 +0100187 .get_usb_charge = 0xFFFF,
188 .set_usb_charge = 0xFFFF,
189
Corentin Chary84d482f2011-11-26 11:00:09 +0100190 .get_wireless_status = 0xFFFF,
191 .set_wireless_status = 0xFFFF,
192
Corentin Charyf674ebf2011-11-26 11:00:08 +0100193 .kbd_backlight = 0xFFFF,
194
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500195 .set_linux = 0x0a,
196 },
197
198 .performance_levels = {
199 {
200 .name = "silent",
201 .value = 0,
202 },
203 {
204 .name = "normal",
205 .value = 1,
206 },
207 { },
208 },
209 .min_brightness = 1,
210 .max_brightness = 8,
211 },
212 {
Corentin Chary84d482f2011-11-26 11:00:09 +0100213 .sabi_version = 3,
214
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500215 .test_string = "SwSmi@",
216
217 .main_function = 0x5843,
218
219 .header_offsets = {
220 .port = 0x00,
221 .re_mem = 0x04,
222 .iface_func = 0x02,
223 .en_mem = 0x03,
224 .data_offset = 0x05,
225 .data_segment = 0x07,
226 },
227
228 .commands = {
229 .get_brightness = 0x10,
230 .set_brightness = 0x11,
231
232 .get_wireless_button = 0x12,
233 .set_wireless_button = 0x13,
234
235 .get_backlight = 0x2d,
236 .set_backlight = 0x2e,
237
238 .get_recovery_mode = 0xff,
239 .set_recovery_mode = 0xff,
240
241 .get_performance_level = 0x31,
242 .set_performance_level = 0x32,
243
Corentin Charycb5b5c92011-11-26 11:00:05 +0100244 .get_battery_life_extender = 0x65,
245 .set_battery_life_extender = 0x66,
246
Corentin Chary3a75d372011-11-26 11:00:06 +0100247 .get_usb_charge = 0x67,
248 .set_usb_charge = 0x68,
249
Corentin Chary84d482f2011-11-26 11:00:09 +0100250 .get_wireless_status = 0x69,
251 .set_wireless_status = 0x6a,
252
Corentin Charyf674ebf2011-11-26 11:00:08 +0100253 .kbd_backlight = 0x78,
254
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500255 .set_linux = 0xff,
256 },
257
258 .performance_levels = {
259 {
260 .name = "normal",
261 .value = 0,
262 },
263 {
264 .name = "silent",
265 .value = 1,
266 },
267 {
268 .name = "overclock",
269 .value = 2,
270 },
271 { },
272 },
273 .min_brightness = 0,
274 .max_brightness = 8,
275 },
276 { },
277};
278
Corentin Chary5b80fc42011-11-26 11:00:03 +0100279/*
280 * samsung-laptop/ - debugfs root directory
281 * f0000_segment - dump f0000 segment
282 * command - current command
283 * data - current data
284 * d0, d1, d2, d3 - data fields
285 * call - call SABI using command and data
286 *
287 * This allow to call arbitrary sabi commands wihout
288 * modifying the driver at all.
289 * For example, setting the keyboard backlight brightness to 5
290 *
291 * echo 0x78 > command
292 * echo 0x0582 > d0
293 * echo 0 > d1
294 * echo 0 > d2
295 * echo 0 > d3
296 * cat call
297 */
298
299struct samsung_laptop_debug {
300 struct dentry *root;
301 struct sabi_data data;
302 u16 command;
303
304 struct debugfs_blob_wrapper f0000_wrapper;
305 struct debugfs_blob_wrapper data_wrapper;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100306 struct debugfs_blob_wrapper sdiag_wrapper;
Corentin Chary5b80fc42011-11-26 11:00:03 +0100307};
308
Corentin Chary84d482f2011-11-26 11:00:09 +0100309struct samsung_laptop;
310
311struct samsung_rfkill {
312 struct samsung_laptop *samsung;
313 struct rfkill *rfkill;
314 enum rfkill_type type;
315};
316
Corentin Charya6df4892011-11-26 10:59:58 +0100317struct samsung_laptop {
318 const struct sabi_config *config;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500319
Corentin Charya6df4892011-11-26 10:59:58 +0100320 void __iomem *sabi;
321 void __iomem *sabi_iface;
322 void __iomem *f0000_segment;
323
324 struct mutex sabi_mutex;
325
Corentin Chary5dea7a22011-11-26 10:59:59 +0100326 struct platform_device *platform_device;
Corentin Charya6df4892011-11-26 10:59:58 +0100327 struct backlight_device *backlight_device;
Corentin Chary84d482f2011-11-26 11:00:09 +0100328
329 struct samsung_rfkill wlan;
330 struct samsung_rfkill bluetooth;
Corentin Charya6df4892011-11-26 10:59:58 +0100331
Corentin Charyf674ebf2011-11-26 11:00:08 +0100332 struct led_classdev kbd_led;
333 int kbd_led_wk;
334 struct workqueue_struct *led_workqueue;
335 struct work_struct kbd_led_work;
336
Corentin Chary5b80fc42011-11-26 11:00:03 +0100337 struct samsung_laptop_debug debug;
338
Corentin Charyf34cd9c2011-11-26 11:00:00 +0100339 bool handle_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +0100340 bool has_stepping_quirk;
Corentin Chary6f6ae062011-11-26 11:00:11 +0100341
342 char sdiag[64];
Corentin Charya6df4892011-11-26 10:59:58 +0100343};
344
Corentin Chary5dea7a22011-11-26 10:59:59 +0100345
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500346
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030347static bool force;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500348module_param(force, bool, 0);
349MODULE_PARM_DESC(force,
350 "Disable the DMI check and forces the driver to be loaded");
351
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030352static bool debug;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500353module_param(debug, bool, S_IRUGO | S_IWUSR);
354MODULE_PARM_DESC(debug, "Debug enabled or not");
355
Corentin Chary7e960712011-11-26 11:00:02 +0100356static int sabi_command(struct samsung_laptop *samsung, u16 command,
357 struct sabi_data *in,
358 struct sabi_data *out)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500359{
Corentin Charya6df4892011-11-26 10:59:58 +0100360 const struct sabi_config *config = samsung->config;
Corentin Chary7e960712011-11-26 11:00:02 +0100361 int ret = 0;
Corentin Charya6df4892011-11-26 10:59:58 +0100362 u16 port = readw(samsung->sabi + config->header_offsets.port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500363 u8 complete, iface_data;
364
Corentin Charya6df4892011-11-26 10:59:58 +0100365 mutex_lock(&samsung->sabi_mutex);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500366
Corentin Chary7e960712011-11-26 11:00:02 +0100367 if (debug) {
368 if (in)
Corentin Chary2e777182011-11-26 11:00:12 +0100369 pr_info("SABI command:0x%04x "
370 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100371 command, in->d0, in->d1, in->d2, in->d3);
372 else
Corentin Chary2e777182011-11-26 11:00:12 +0100373 pr_info("SABI command:0x%04x", command);
Corentin Chary7e960712011-11-26 11:00:02 +0100374 }
375
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500376 /* enable memory to be able to write to it */
Corentin Charya6df4892011-11-26 10:59:58 +0100377 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500378
379 /* write out the command */
Corentin Charya6df4892011-11-26 10:59:58 +0100380 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
381 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
382 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
Corentin Chary7e960712011-11-26 11:00:02 +0100383 if (in) {
384 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
385 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
386 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
387 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
388 }
Corentin Charya6df4892011-11-26 10:59:58 +0100389 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500390
391 /* write protect memory to make it safe */
Corentin Charya6df4892011-11-26 10:59:58 +0100392 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500393
394 /* see if the command actually succeeded */
Corentin Charya6df4892011-11-26 10:59:58 +0100395 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
396 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
Corentin Chary2e777182011-11-26 11:00:12 +0100397
398 /* iface_data = 0xFF happens when a command is not known
399 * so we only add a warning in debug mode since we will
400 * probably issue some unknown command at startup to find
401 * out which features are supported */
402 if (complete != 0xaa || (iface_data == 0xff && debug))
Corentin Chary7e960712011-11-26 11:00:02 +0100403 pr_warn("SABI command 0x%04x failed with"
404 " completion flag 0x%02x and interface data 0x%02x",
405 command, complete, iface_data);
Corentin Chary2e777182011-11-26 11:00:12 +0100406
407 if (complete != 0xaa || iface_data == 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +0100408 ret = -EINVAL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500409 goto exit;
410 }
Corentin Chary7e960712011-11-26 11:00:02 +0100411
412 if (out) {
413 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
414 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
415 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
416 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
417 }
418
419 if (debug && out) {
Corentin Chary2e777182011-11-26 11:00:12 +0100420 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
Corentin Chary7e960712011-11-26 11:00:02 +0100421 out->d0, out->d1, out->d2, out->d3);
422 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500423
424exit:
Corentin Charya6df4892011-11-26 10:59:58 +0100425 mutex_unlock(&samsung->sabi_mutex);
Corentin Chary7e960712011-11-26 11:00:02 +0100426 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500427}
428
Corentin Chary7e960712011-11-26 11:00:02 +0100429/* simple wrappers usable with most commands */
430static int sabi_set_commandb(struct samsung_laptop *samsung,
431 u16 command, u8 data)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500432{
Corentin Chary7e960712011-11-26 11:00:02 +0100433 struct sabi_data in = { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 };
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500434
Corentin Chary7e960712011-11-26 11:00:02 +0100435 in.data[0] = data;
436 return sabi_command(samsung, command, &in, NULL);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500437}
438
Corentin Chary5dea7a22011-11-26 10:59:59 +0100439static int read_brightness(struct samsung_laptop *samsung)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500440{
Corentin Charya6df4892011-11-26 10:59:58 +0100441 const struct sabi_config *config = samsung->config;
442 const struct sabi_commands *commands = &samsung->config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100443 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500444 int user_brightness = 0;
445 int retval;
446
Corentin Chary7e960712011-11-26 11:00:02 +0100447 retval = sabi_command(samsung, commands->get_brightness,
448 NULL, &sretval);
449 if (retval)
450 return retval;
451
452 user_brightness = sretval.data[0];
453 if (user_brightness > config->min_brightness)
454 user_brightness -= config->min_brightness;
455 else
456 user_brightness = 0;
457
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500458 return user_brightness;
459}
460
Corentin Chary5dea7a22011-11-26 10:59:59 +0100461static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500462{
Corentin Charya6df4892011-11-26 10:59:58 +0100463 const struct sabi_config *config = samsung->config;
464 const struct sabi_commands *commands = &samsung->config->commands;
465 u8 user_level = user_brightness + config->min_brightness;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500466
Corentin Charya6df4892011-11-26 10:59:58 +0100467 if (samsung->has_stepping_quirk && user_level != 0) {
Jason Stubbsac080522011-09-20 09:16:13 -0700468 /*
469 * short circuit if the specified level is what's already set
470 * to prevent the screen from flickering needlessly
471 */
Corentin Chary5dea7a22011-11-26 10:59:59 +0100472 if (user_brightness == read_brightness(samsung))
Jason Stubbsac080522011-09-20 09:16:13 -0700473 return;
474
Corentin Chary7e960712011-11-26 11:00:02 +0100475 sabi_set_commandb(samsung, commands->set_brightness, 0);
Jason Stubbsac080522011-09-20 09:16:13 -0700476 }
477
Corentin Chary7e960712011-11-26 11:00:02 +0100478 sabi_set_commandb(samsung, commands->set_brightness, user_level);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500479}
480
481static int get_brightness(struct backlight_device *bd)
482{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100483 struct samsung_laptop *samsung = bl_get_data(bd);
484
485 return read_brightness(samsung);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500486}
487
Corentin Chary5dea7a22011-11-26 10:59:59 +0100488static void check_for_stepping_quirk(struct samsung_laptop *samsung)
Jason Stubbsac080522011-09-20 09:16:13 -0700489{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100490 int initial_level;
491 int check_level;
492 int orig_level = read_brightness(samsung);
Jason Stubbsac080522011-09-20 09:16:13 -0700493
494 /*
495 * Some laptops exhibit the strange behaviour of stepping toward
496 * (rather than setting) the brightness except when changing to/from
497 * brightness level 0. This behaviour is checked for here and worked
498 * around in set_brightness.
499 */
500
John Serockba05b232011-10-13 06:42:01 -0400501 if (orig_level == 0)
Corentin Chary5dea7a22011-11-26 10:59:59 +0100502 set_brightness(samsung, 1);
John Serockba05b232011-10-13 06:42:01 -0400503
Corentin Chary5dea7a22011-11-26 10:59:59 +0100504 initial_level = read_brightness(samsung);
John Serockba05b232011-10-13 06:42:01 -0400505
Jason Stubbsac080522011-09-20 09:16:13 -0700506 if (initial_level <= 2)
507 check_level = initial_level + 2;
508 else
509 check_level = initial_level - 2;
510
Corentin Charya6df4892011-11-26 10:59:58 +0100511 samsung->has_stepping_quirk = false;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100512 set_brightness(samsung, check_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700513
Corentin Chary5dea7a22011-11-26 10:59:59 +0100514 if (read_brightness(samsung) != check_level) {
Corentin Charya6df4892011-11-26 10:59:58 +0100515 samsung->has_stepping_quirk = true;
Jason Stubbsac080522011-09-20 09:16:13 -0700516 pr_info("enabled workaround for brightness stepping quirk\n");
517 }
518
Corentin Chary5dea7a22011-11-26 10:59:59 +0100519 set_brightness(samsung, orig_level);
Jason Stubbsac080522011-09-20 09:16:13 -0700520}
521
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500522static int update_status(struct backlight_device *bd)
523{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100524 struct samsung_laptop *samsung = bl_get_data(bd);
Corentin Charya6df4892011-11-26 10:59:58 +0100525 const struct sabi_commands *commands = &samsung->config->commands;
526
Corentin Chary5dea7a22011-11-26 10:59:59 +0100527 set_brightness(samsung, bd->props.brightness);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500528
529 if (bd->props.power == FB_BLANK_UNBLANK)
Corentin Chary7e960712011-11-26 11:00:02 +0100530 sabi_set_commandb(samsung, commands->set_backlight, 1);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500531 else
Corentin Chary7e960712011-11-26 11:00:02 +0100532 sabi_set_commandb(samsung, commands->set_backlight, 0);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100533
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500534 return 0;
535}
536
537static const struct backlight_ops backlight_ops = {
538 .get_brightness = get_brightness,
539 .update_status = update_status,
540};
541
Corentin Chary84d482f2011-11-26 11:00:09 +0100542static int seclinux_rfkill_set(void *data, bool blocked)
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500543{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100544 struct samsung_laptop *samsung = data;
Corentin Charya6df4892011-11-26 10:59:58 +0100545 const struct sabi_commands *commands = &samsung->config->commands;
546
Corentin Chary84d482f2011-11-26 11:00:09 +0100547 return sabi_set_commandb(samsung, commands->set_wireless_button,
548 !blocked);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500549}
550
Corentin Chary84d482f2011-11-26 11:00:09 +0100551static struct rfkill_ops seclinux_rfkill_ops = {
552 .set_block = seclinux_rfkill_set,
553};
554
555static int swsmi_wireless_status(struct samsung_laptop *samsung,
556 struct sabi_data *data)
557{
558 const struct sabi_commands *commands = &samsung->config->commands;
559
560 return sabi_command(samsung, commands->get_wireless_status,
561 NULL, data);
562}
563
564static int swsmi_rfkill_set(void *priv, bool blocked)
565{
566 struct samsung_rfkill *srfkill = priv;
567 struct samsung_laptop *samsung = srfkill->samsung;
568 const struct sabi_commands *commands = &samsung->config->commands;
569 struct sabi_data data;
570 int ret, i;
571
572 ret = swsmi_wireless_status(samsung, &data);
573 if (ret)
574 return ret;
575
576 /* Don't set the state for non-present devices */
577 for (i = 0; i < 4; i++)
578 if (data.data[i] == 0x02)
579 data.data[1] = 0;
580
581 if (srfkill->type == RFKILL_TYPE_WLAN)
582 data.data[WL_STATUS_WLAN] = !blocked;
583 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
584 data.data[WL_STATUS_BT] = !blocked;
585
586 return sabi_command(samsung, commands->set_wireless_status,
587 &data, &data);
588}
589
590static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
591{
592 struct samsung_rfkill *srfkill = priv;
593 struct samsung_laptop *samsung = srfkill->samsung;
594 struct sabi_data data;
595 int ret;
596
597 ret = swsmi_wireless_status(samsung, &data);
598 if (ret)
599 return ;
600
601 if (srfkill->type == RFKILL_TYPE_WLAN)
602 ret = data.data[WL_STATUS_WLAN];
603 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
604 ret = data.data[WL_STATUS_BT];
605 else
606 return ;
607
608 rfkill_set_sw_state(rfkill, !ret);
609}
610
611static struct rfkill_ops swsmi_rfkill_ops = {
612 .set_block = swsmi_rfkill_set,
613 .query = swsmi_rfkill_query,
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500614};
615
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500616static ssize_t get_performance_level(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100619 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100620 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100621 const struct sabi_commands *commands = &config->commands;
Corentin Chary7e960712011-11-26 11:00:02 +0100622 struct sabi_data sretval;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500623 int retval;
624 int i;
625
626 /* Read the state */
Corentin Chary7e960712011-11-26 11:00:02 +0100627 retval = sabi_command(samsung, commands->get_performance_level,
628 NULL, &sretval);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500629 if (retval)
630 return retval;
631
632 /* The logic is backwards, yeah, lots of fun... */
Corentin Charya6df4892011-11-26 10:59:58 +0100633 for (i = 0; config->performance_levels[i].name; ++i) {
Corentin Chary7e960712011-11-26 11:00:02 +0100634 if (sretval.data[0] == config->performance_levels[i].value)
Corentin Charya6df4892011-11-26 10:59:58 +0100635 return sprintf(buf, "%s\n", config->performance_levels[i].name);
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500636 }
637 return sprintf(buf, "%s\n", "unknown");
638}
639
640static ssize_t set_performance_level(struct device *dev,
641 struct device_attribute *attr, const char *buf,
642 size_t count)
643{
Corentin Chary5dea7a22011-11-26 10:59:59 +0100644 struct samsung_laptop *samsung = dev_get_drvdata(dev);
Corentin Charya6df4892011-11-26 10:59:58 +0100645 const struct sabi_config *config = samsung->config;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100646 const struct sabi_commands *commands = &config->commands;
647 int i;
Corentin Charya6df4892011-11-26 10:59:58 +0100648
Corentin Chary5dea7a22011-11-26 10:59:59 +0100649 if (count < 1)
650 return count;
651
652 for (i = 0; config->performance_levels[i].name; ++i) {
653 const struct sabi_performance_level *level =
654 &config->performance_levels[i];
655 if (!strncasecmp(level->name, buf, strlen(level->name))) {
Corentin Chary7e960712011-11-26 11:00:02 +0100656 sabi_set_commandb(samsung,
657 commands->set_performance_level,
658 level->value);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100659 break;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500660 }
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500661 }
Corentin Chary5dea7a22011-11-26 10:59:59 +0100662
663 if (!config->performance_levels[i].name)
664 return -EINVAL;
665
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500666 return count;
667}
Corentin Chary5dea7a22011-11-26 10:59:59 +0100668
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500669static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
670 get_performance_level, set_performance_level);
671
Corentin Charycb5b5c92011-11-26 11:00:05 +0100672static int read_battery_life_extender(struct samsung_laptop *samsung)
673{
674 const struct sabi_commands *commands = &samsung->config->commands;
675 struct sabi_data data;
676 int retval;
677
678 if (commands->get_battery_life_extender == 0xFFFF)
679 return -ENODEV;
680
681 memset(&data, 0, sizeof(data));
682 data.data[0] = 0x80;
683 retval = sabi_command(samsung, commands->get_battery_life_extender,
684 &data, &data);
685
686 if (retval)
687 return retval;
688
689 if (data.data[0] != 0 && data.data[0] != 1)
690 return -ENODEV;
691
692 return data.data[0];
693}
694
695static int write_battery_life_extender(struct samsung_laptop *samsung,
696 int enabled)
697{
698 const struct sabi_commands *commands = &samsung->config->commands;
699 struct sabi_data data;
700
701 memset(&data, 0, sizeof(data));
702 data.data[0] = 0x80 | enabled;
703 return sabi_command(samsung, commands->set_battery_life_extender,
704 &data, NULL);
705}
706
707static ssize_t get_battery_life_extender(struct device *dev,
708 struct device_attribute *attr,
709 char *buf)
710{
711 struct samsung_laptop *samsung = dev_get_drvdata(dev);
712 int ret;
713
714 ret = read_battery_life_extender(samsung);
715 if (ret < 0)
716 return ret;
717
718 return sprintf(buf, "%d\n", ret);
719}
720
721static ssize_t set_battery_life_extender(struct device *dev,
722 struct device_attribute *attr,
723 const char *buf, size_t count)
724{
725 struct samsung_laptop *samsung = dev_get_drvdata(dev);
726 int ret, value;
727
728 if (!count || sscanf(buf, "%i", &value) != 1)
729 return -EINVAL;
730
731 ret = write_battery_life_extender(samsung, !!value);
732 if (ret < 0)
733 return ret;
734
735 return count;
736}
737
738static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
739 get_battery_life_extender, set_battery_life_extender);
740
Corentin Chary3a75d372011-11-26 11:00:06 +0100741static int read_usb_charge(struct samsung_laptop *samsung)
742{
743 const struct sabi_commands *commands = &samsung->config->commands;
744 struct sabi_data data;
745 int retval;
746
747 if (commands->get_usb_charge == 0xFFFF)
748 return -ENODEV;
749
750 memset(&data, 0, sizeof(data));
751 data.data[0] = 0x80;
752 retval = sabi_command(samsung, commands->get_usb_charge,
753 &data, &data);
754
755 if (retval)
756 return retval;
757
758 if (data.data[0] != 0 && data.data[0] != 1)
759 return -ENODEV;
760
761 return data.data[0];
762}
763
764static int write_usb_charge(struct samsung_laptop *samsung,
765 int enabled)
766{
767 const struct sabi_commands *commands = &samsung->config->commands;
768 struct sabi_data data;
769
770 memset(&data, 0, sizeof(data));
771 data.data[0] = 0x80 | enabled;
772 return sabi_command(samsung, commands->set_usb_charge,
773 &data, NULL);
774}
775
776static ssize_t get_usb_charge(struct device *dev,
777 struct device_attribute *attr,
778 char *buf)
779{
780 struct samsung_laptop *samsung = dev_get_drvdata(dev);
781 int ret;
782
783 ret = read_usb_charge(samsung);
784 if (ret < 0)
785 return ret;
786
787 return sprintf(buf, "%d\n", ret);
788}
789
790static ssize_t set_usb_charge(struct device *dev,
791 struct device_attribute *attr,
792 const char *buf, size_t count)
793{
794 struct samsung_laptop *samsung = dev_get_drvdata(dev);
795 int ret, value;
796
797 if (!count || sscanf(buf, "%i", &value) != 1)
798 return -EINVAL;
799
800 ret = write_usb_charge(samsung, !!value);
801 if (ret < 0)
802 return ret;
803
804 return count;
805}
806
807static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
808 get_usb_charge, set_usb_charge);
809
Corentin Charya66c1662011-11-26 11:00:01 +0100810static struct attribute *platform_attributes[] = {
811 &dev_attr_performance_level.attr,
Corentin Charycb5b5c92011-11-26 11:00:05 +0100812 &dev_attr_battery_life_extender.attr,
Corentin Chary3a75d372011-11-26 11:00:06 +0100813 &dev_attr_usb_charge.attr,
Corentin Charya66c1662011-11-26 11:00:01 +0100814 NULL
815};
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -0500816
Corentin Chary5dea7a22011-11-26 10:59:59 +0100817static int find_signature(void __iomem *memcheck, const char *testStr)
818{
819 int i = 0;
820 int loca;
821
822 for (loca = 0; loca < 0xffff; loca++) {
823 char temp = readb(memcheck + loca);
824
825 if (temp == testStr[i]) {
826 if (i == strlen(testStr)-1)
827 break;
828 ++i;
829 } else {
830 i = 0;
831 }
832 }
833 return loca;
834}
835
836static void samsung_rfkill_exit(struct samsung_laptop *samsung)
837{
Corentin Chary84d482f2011-11-26 11:00:09 +0100838 if (samsung->wlan.rfkill) {
839 rfkill_unregister(samsung->wlan.rfkill);
840 rfkill_destroy(samsung->wlan.rfkill);
841 samsung->wlan.rfkill = NULL;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100842 }
Corentin Chary84d482f2011-11-26 11:00:09 +0100843 if (samsung->bluetooth.rfkill) {
844 rfkill_unregister(samsung->bluetooth.rfkill);
845 rfkill_destroy(samsung->bluetooth.rfkill);
846 samsung->bluetooth.rfkill = NULL;
847 }
848}
849
850static int samsung_new_rfkill(struct samsung_laptop *samsung,
851 struct samsung_rfkill *arfkill,
852 const char *name, enum rfkill_type type,
853 const struct rfkill_ops *ops,
854 int blocked)
855{
856 struct rfkill **rfkill = &arfkill->rfkill;
857 int ret;
858
859 arfkill->type = type;
860 arfkill->samsung = samsung;
861
862 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
863 type, ops, arfkill);
864
865 if (!*rfkill)
866 return -EINVAL;
867
868 if (blocked != -1)
869 rfkill_init_sw_state(*rfkill, blocked);
870
871 ret = rfkill_register(*rfkill);
872 if (ret) {
873 rfkill_destroy(*rfkill);
874 *rfkill = NULL;
875 return ret;
876 }
877 return 0;
878}
879
880static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
881{
882 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
883 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
884}
885
886static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
887{
888 struct sabi_data data;
889 int ret;
890
891 ret = swsmi_wireless_status(samsung, &data);
892 if (ret)
893 return ret;
894
895 /* 0x02 seems to mean that the device is no present/available */
896
897 if (data.data[WL_STATUS_WLAN] != 0x02)
898 ret = samsung_new_rfkill(samsung, &samsung->wlan,
899 "samsung-wlan",
900 RFKILL_TYPE_WLAN,
901 &swsmi_rfkill_ops,
902 !data.data[WL_STATUS_WLAN]);
903 if (ret)
904 goto exit;
905
906 if (data.data[WL_STATUS_BT] != 0x02)
907 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
908 "samsung-bluetooth",
909 RFKILL_TYPE_BLUETOOTH,
910 &swsmi_rfkill_ops,
911 !data.data[WL_STATUS_BT]);
912 if (ret)
913 goto exit;
914
915exit:
916 if (ret)
917 samsung_rfkill_exit(samsung);
918
919 return ret;
Corentin Chary5dea7a22011-11-26 10:59:59 +0100920}
921
922static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
923{
Corentin Chary84d482f2011-11-26 11:00:09 +0100924 if (samsung->config->sabi_version == 2)
925 return samsung_rfkill_init_seclinux(samsung);
926 if (samsung->config->sabi_version == 3)
927 return samsung_rfkill_init_swsmi(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +0100928 return 0;
929}
930
Corentin Charyf674ebf2011-11-26 11:00:08 +0100931static int kbd_backlight_enable(struct samsung_laptop *samsung)
932{
933 const struct sabi_commands *commands = &samsung->config->commands;
934 struct sabi_data data;
935 int retval;
936
937 if (commands->kbd_backlight == 0xFFFF)
938 return -ENODEV;
939
940 memset(&data, 0, sizeof(data));
941 data.d0 = 0xaabb;
942 retval = sabi_command(samsung, commands->kbd_backlight,
943 &data, &data);
944
945 if (retval)
946 return retval;
947
948 if (data.d0 != 0xccdd)
949 return -ENODEV;
950 return 0;
951}
952
953static int kbd_backlight_read(struct samsung_laptop *samsung)
954{
955 const struct sabi_commands *commands = &samsung->config->commands;
956 struct sabi_data data;
957 int retval;
958
959 memset(&data, 0, sizeof(data));
960 data.data[0] = 0x81;
961 retval = sabi_command(samsung, commands->kbd_backlight,
962 &data, &data);
963
964 if (retval)
965 return retval;
966
967 return data.data[0];
968}
969
970static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
971{
972 const struct sabi_commands *commands = &samsung->config->commands;
973 struct sabi_data data;
974
975 memset(&data, 0, sizeof(data));
976 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
977 return sabi_command(samsung, commands->kbd_backlight,
978 &data, NULL);
979}
980
981static void kbd_led_update(struct work_struct *work)
982{
983 struct samsung_laptop *samsung;
984
985 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
986 kbd_backlight_write(samsung, samsung->kbd_led_wk);
987}
988
989static void kbd_led_set(struct led_classdev *led_cdev,
990 enum led_brightness value)
991{
992 struct samsung_laptop *samsung;
993
994 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
995
996 if (value > samsung->kbd_led.max_brightness)
997 value = samsung->kbd_led.max_brightness;
998 else if (value < 0)
999 value = 0;
1000
1001 samsung->kbd_led_wk = value;
1002 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1003}
1004
1005static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1006{
1007 struct samsung_laptop *samsung;
1008
1009 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1010 return kbd_backlight_read(samsung);
1011}
1012
1013static void samsung_leds_exit(struct samsung_laptop *samsung)
1014{
1015 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1016 led_classdev_unregister(&samsung->kbd_led);
1017 if (samsung->led_workqueue)
1018 destroy_workqueue(samsung->led_workqueue);
1019}
1020
1021static int __init samsung_leds_init(struct samsung_laptop *samsung)
1022{
1023 int ret = 0;
1024
1025 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1026 if (!samsung->led_workqueue)
1027 return -ENOMEM;
1028
1029 if (kbd_backlight_enable(samsung) >= 0) {
1030 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1031
1032 samsung->kbd_led.name = "samsung::kbd_backlight";
1033 samsung->kbd_led.brightness_set = kbd_led_set;
1034 samsung->kbd_led.brightness_get = kbd_led_get;
1035 samsung->kbd_led.max_brightness = 8;
1036
1037 ret = led_classdev_register(&samsung->platform_device->dev,
1038 &samsung->kbd_led);
1039 }
1040
1041 if (ret)
1042 samsung_leds_exit(samsung);
1043
1044 return ret;
1045}
1046
Corentin Chary5dea7a22011-11-26 10:59:59 +01001047static void samsung_backlight_exit(struct samsung_laptop *samsung)
1048{
1049 if (samsung->backlight_device) {
1050 backlight_device_unregister(samsung->backlight_device);
1051 samsung->backlight_device = NULL;
1052 }
1053}
1054
1055static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1056{
1057 struct backlight_device *bd;
1058 struct backlight_properties props;
1059
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001060 if (!samsung->handle_backlight)
1061 return 0;
1062
Corentin Chary5dea7a22011-11-26 10:59:59 +01001063 memset(&props, 0, sizeof(struct backlight_properties));
1064 props.type = BACKLIGHT_PLATFORM;
1065 props.max_brightness = samsung->config->max_brightness -
1066 samsung->config->min_brightness;
1067
1068 bd = backlight_device_register("samsung",
1069 &samsung->platform_device->dev,
1070 samsung, &backlight_ops,
1071 &props);
1072 if (IS_ERR(bd))
1073 return PTR_ERR(bd);
1074
1075 samsung->backlight_device = bd;
1076 samsung->backlight_device->props.brightness = read_brightness(samsung);
1077 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1078 backlight_update_status(samsung->backlight_device);
1079
1080 return 0;
1081}
1082
Corentin Charya66c1662011-11-26 11:00:01 +01001083static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
1084 struct attribute *attr, int idx)
1085{
1086 struct device *dev = container_of(kobj, struct device, kobj);
1087 struct platform_device *pdev = to_platform_device(dev);
1088 struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1089 bool ok = true;
1090
1091 if (attr == &dev_attr_performance_level.attr)
1092 ok = !!samsung->config->performance_levels[0].name;
Corentin Charycb5b5c92011-11-26 11:00:05 +01001093 if (attr == &dev_attr_battery_life_extender.attr)
1094 ok = !!(read_battery_life_extender(samsung) >= 0);
Corentin Chary3a75d372011-11-26 11:00:06 +01001095 if (attr == &dev_attr_usb_charge.attr)
1096 ok = !!(read_usb_charge(samsung) >= 0);
Corentin Charya66c1662011-11-26 11:00:01 +01001097
1098 return ok ? attr->mode : 0;
1099}
1100
1101static struct attribute_group platform_attribute_group = {
1102 .is_visible = samsung_sysfs_is_visible,
1103 .attrs = platform_attributes
1104};
1105
Corentin Chary5dea7a22011-11-26 10:59:59 +01001106static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1107{
Corentin Charya66c1662011-11-26 11:00:01 +01001108 struct platform_device *device = samsung->platform_device;
1109
1110 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001111}
1112
1113static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1114{
Corentin Charya66c1662011-11-26 11:00:01 +01001115 struct platform_device *device = samsung->platform_device;
1116
1117 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1118
Corentin Chary5dea7a22011-11-26 10:59:59 +01001119}
1120
Corentin Chary5b80fc42011-11-26 11:00:03 +01001121static int show_call(struct seq_file *m, void *data)
1122{
1123 struct samsung_laptop *samsung = m->private;
1124 struct sabi_data *sdata = &samsung->debug.data;
1125 int ret;
1126
1127 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1128 samsung->debug.command,
1129 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1130
1131 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1132
1133 if (ret) {
1134 seq_printf(m, "SABI command 0x%04x failed\n",
1135 samsung->debug.command);
1136 return ret;
1137 }
1138
1139 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1140 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1141 return 0;
1142}
1143
1144static int samsung_debugfs_open(struct inode *inode, struct file *file)
1145{
1146 return single_open(file, show_call, inode->i_private);
1147}
1148
1149static const struct file_operations samsung_laptop_call_io_ops = {
1150 .owner = THIS_MODULE,
1151 .open = samsung_debugfs_open,
1152 .read = seq_read,
1153 .llseek = seq_lseek,
1154 .release = single_release,
1155};
1156
1157static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1158{
1159 debugfs_remove_recursive(samsung->debug.root);
1160}
1161
1162static int samsung_debugfs_init(struct samsung_laptop *samsung)
1163{
1164 struct dentry *dent;
1165
1166 samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1167 if (!samsung->debug.root) {
1168 pr_err("failed to create debugfs directory");
1169 goto error_debugfs;
1170 }
1171
1172 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1173 samsung->debug.f0000_wrapper.size = 0xffff;
1174
1175 samsung->debug.data_wrapper.data = &samsung->debug.data;
1176 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1177
Corentin Chary6f6ae062011-11-26 11:00:11 +01001178 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1179 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1180
Corentin Chary5b80fc42011-11-26 11:00:03 +01001181 dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1182 samsung->debug.root, &samsung->debug.command);
1183 if (!dent)
1184 goto error_debugfs;
1185
1186 dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1187 &samsung->debug.data.d0);
1188 if (!dent)
1189 goto error_debugfs;
1190
1191 dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1192 &samsung->debug.data.d1);
1193 if (!dent)
1194 goto error_debugfs;
1195
1196 dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1197 &samsung->debug.data.d2);
1198 if (!dent)
1199 goto error_debugfs;
1200
1201 dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1202 &samsung->debug.data.d3);
1203 if (!dent)
1204 goto error_debugfs;
1205
1206 dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1207 samsung->debug.root,
1208 &samsung->debug.data_wrapper);
1209 if (!dent)
1210 goto error_debugfs;
1211
1212 dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1213 samsung->debug.root,
1214 &samsung->debug.f0000_wrapper);
1215 if (!dent)
1216 goto error_debugfs;
1217
1218 dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1219 samsung->debug.root, samsung,
1220 &samsung_laptop_call_io_ops);
1221 if (!dent)
1222 goto error_debugfs;
1223
Corentin Chary6f6ae062011-11-26 11:00:11 +01001224 dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1225 samsung->debug.root,
1226 &samsung->debug.sdiag_wrapper);
1227 if (!dent)
1228 goto error_debugfs;
1229
Corentin Chary5b80fc42011-11-26 11:00:03 +01001230 return 0;
1231
1232error_debugfs:
1233 samsung_debugfs_exit(samsung);
1234 return -ENOMEM;
1235}
1236
Corentin Chary5dea7a22011-11-26 10:59:59 +01001237static void samsung_sabi_exit(struct samsung_laptop *samsung)
1238{
1239 const struct sabi_config *config = samsung->config;
1240
1241 /* Turn off "Linux" mode in the BIOS */
1242 if (config && config->commands.set_linux != 0xff)
Corentin Chary7e960712011-11-26 11:00:02 +01001243 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001244
1245 if (samsung->sabi_iface) {
1246 iounmap(samsung->sabi_iface);
1247 samsung->sabi_iface = NULL;
1248 }
1249 if (samsung->f0000_segment) {
1250 iounmap(samsung->f0000_segment);
1251 samsung->f0000_segment = NULL;
1252 }
1253
1254 samsung->config = NULL;
1255}
1256
Corentin Chary49dd7732011-11-26 11:00:04 +01001257static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1258 unsigned int ifaceP)
Corentin Chary5dea7a22011-11-26 10:59:59 +01001259{
1260 const struct sabi_config *config = samsung->config;
1261
1262 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1263 loca + 0xf0000 - 6);
Corentin Chary49dd7732011-11-26 11:00:04 +01001264
Corentin Chary5dea7a22011-11-26 10:59:59 +01001265 printk(KERN_DEBUG "SABI header:\n");
1266 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1267 readw(samsung->sabi + config->header_offsets.port));
1268 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1269 readb(samsung->sabi + config->header_offsets.iface_func));
1270 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1271 readb(samsung->sabi + config->header_offsets.en_mem));
1272 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1273 readb(samsung->sabi + config->header_offsets.re_mem));
1274 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1275 readw(samsung->sabi + config->header_offsets.data_offset));
1276 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1277 readw(samsung->sabi + config->header_offsets.data_segment));
Corentin Chary5dea7a22011-11-26 10:59:59 +01001278
Corentin Chary49dd7732011-11-26 11:00:04 +01001279 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001280}
1281
Corentin Chary6f6ae062011-11-26 11:00:11 +01001282static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1283{
1284 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1285 int i;
1286
1287 if (loca == 0xffff)
1288 return ;
1289
1290 /* Example:
1291 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1292 *
1293 * Product name: 90X3A
1294 * BIOS Version: 07HL
1295 */
1296 loca += 1;
1297 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1298 char temp = readb(samsung->f0000_segment + loca);
1299
1300 if (isalnum(temp) || temp == '/' || temp == '-')
1301 samsung->sdiag[i++] = temp;
1302 else
1303 break ;
1304 }
1305
1306 if (debug && samsung->sdiag[0])
1307 pr_info("sdiag: %s", samsung->sdiag);
1308}
1309
Corentin Chary5dea7a22011-11-26 10:59:59 +01001310static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1311{
1312 const struct sabi_config *config = NULL;
1313 const struct sabi_commands *commands;
1314 unsigned int ifaceP;
1315 int ret = 0;
1316 int i;
1317 int loca;
1318
1319 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1320 if (!samsung->f0000_segment) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001321 if (debug || force)
1322 pr_err("Can't map the segment at 0xf0000\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001323 ret = -EINVAL;
1324 goto exit;
1325 }
1326
Corentin Chary6f6ae062011-11-26 11:00:11 +01001327 samsung_sabi_diag(samsung);
1328
Corentin Chary5dea7a22011-11-26 10:59:59 +01001329 /* Try to find one of the signatures in memory to find the header */
1330 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1331 samsung->config = &sabi_configs[i];
1332 loca = find_signature(samsung->f0000_segment,
1333 samsung->config->test_string);
1334 if (loca != 0xffff)
1335 break;
1336 }
1337
1338 if (loca == 0xffff) {
Corentin Chary3be324a2011-11-26 11:00:10 +01001339 if (debug || force)
1340 pr_err("This computer does not support SABI\n");
Corentin Chary5dea7a22011-11-26 10:59:59 +01001341 ret = -ENODEV;
1342 goto exit;
1343 }
1344
1345 config = samsung->config;
1346 commands = &config->commands;
1347
1348 /* point to the SMI port Number */
1349 loca += 1;
1350 samsung->sabi = (samsung->f0000_segment + loca);
1351
Corentin Chary5dea7a22011-11-26 10:59:59 +01001352 /* Get a pointer to the SABI Interface */
1353 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1354 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
Corentin Chary49dd7732011-11-26 11:00:04 +01001355
1356 if (debug)
1357 samsung_sabi_infos(samsung, loca, ifaceP);
1358
Corentin Chary5dea7a22011-11-26 10:59:59 +01001359 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1360 if (!samsung->sabi_iface) {
1361 pr_err("Can't remap %x\n", ifaceP);
1362 ret = -EINVAL;
1363 goto exit;
1364 }
1365
Corentin Chary5dea7a22011-11-26 10:59:59 +01001366 /* Turn on "Linux" mode in the BIOS */
1367 if (commands->set_linux != 0xff) {
Corentin Chary7e960712011-11-26 11:00:02 +01001368 int retval = sabi_set_commandb(samsung,
1369 commands->set_linux, 0x81);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001370 if (retval) {
1371 pr_warn("Linux mode was not set!\n");
1372 ret = -ENODEV;
1373 goto exit;
1374 }
1375 }
1376
1377 /* Check for stepping quirk */
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001378 if (samsung->handle_backlight)
1379 check_for_stepping_quirk(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001380
Corentin Chary2e777182011-11-26 11:00:12 +01001381 pr_info("detected SABI interface: %s\n",
1382 samsung->config->test_string);
1383
Corentin Chary5dea7a22011-11-26 10:59:59 +01001384exit:
1385 if (ret)
1386 samsung_sabi_exit(samsung);
1387
1388 return ret;
1389}
1390
1391static void samsung_platform_exit(struct samsung_laptop *samsung)
1392{
1393 if (samsung->platform_device) {
1394 platform_device_unregister(samsung->platform_device);
1395 samsung->platform_device = NULL;
1396 }
1397}
1398
1399static int __init samsung_platform_init(struct samsung_laptop *samsung)
1400{
1401 struct platform_device *pdev;
1402
1403 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1404 if (IS_ERR(pdev))
1405 return PTR_ERR(pdev);
1406
1407 samsung->platform_device = pdev;
1408 platform_set_drvdata(samsung->platform_device, samsung);
1409 return 0;
1410}
1411
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001412static struct dmi_system_id __initdata samsung_dmi_table[] = {
1413 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001414 .matches = {
1415 DMI_MATCH(DMI_SYS_VENDOR,
1416 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001417 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001418 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001419 },
1420 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001421 .matches = {
1422 DMI_MATCH(DMI_SYS_VENDOR,
1423 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001424 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001425 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001426 },
1427 {
J Witteveen4e2441c2011-07-03 13:15:44 +02001428 .matches = {
1429 DMI_MATCH(DMI_SYS_VENDOR,
1430 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001431 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
J Witteveen4e2441c2011-07-03 13:15:44 +02001432 },
J Witteveen4e2441c2011-07-03 13:15:44 +02001433 },
1434 {
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001435 .matches = {
1436 DMI_MATCH(DMI_SYS_VENDOR,
1437 "SAMSUNG ELECTRONICS CO., LTD."),
Corentin Chary3be324a2011-11-26 11:00:10 +01001438 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001439 },
Tommaso Massimi7500eeb2011-09-20 09:16:09 -07001440 },
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001441 { },
1442};
1443MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1444
Corentin Chary5dea7a22011-11-26 10:59:59 +01001445static struct platform_device *samsung_platform_device;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001446
1447static int __init samsung_init(void)
1448{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001449 struct samsung_laptop *samsung;
1450 int ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001451
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001452 if (!force && !dmi_check_system(samsung_dmi_table))
1453 return -ENODEV;
1454
Corentin Charya6df4892011-11-26 10:59:58 +01001455 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1456 if (!samsung)
1457 return -ENOMEM;
1458
1459 mutex_init(&samsung->sabi_mutex);
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001460 samsung->handle_backlight = true;
1461
1462#ifdef CONFIG_ACPI
1463 /* Don't handle backlight here if the acpi video already handle it */
Corentin Chary3be324a2011-11-26 11:00:10 +01001464 if (acpi_video_backlight_support())
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001465 samsung->handle_backlight = false;
Corentin Charyf34cd9c2011-11-26 11:00:00 +01001466#endif
Corentin Chary5dea7a22011-11-26 10:59:59 +01001467 ret = samsung_platform_init(samsung);
1468 if (ret)
1469 goto error_platform;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001470
Corentin Chary5dea7a22011-11-26 10:59:59 +01001471 ret = samsung_sabi_init(samsung);
1472 if (ret)
1473 goto error_sabi;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001474
Corentin Chary3be324a2011-11-26 11:00:10 +01001475#ifdef CONFIG_ACPI
1476 /* Only log that if we are really on a sabi platform */
1477 if (acpi_video_backlight_support())
1478 pr_info("Backlight controlled by ACPI video driver\n");
1479#endif
1480
Corentin Chary5dea7a22011-11-26 10:59:59 +01001481 ret = samsung_sysfs_init(samsung);
1482 if (ret)
1483 goto error_sysfs;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001484
Corentin Chary5dea7a22011-11-26 10:59:59 +01001485 ret = samsung_backlight_init(samsung);
1486 if (ret)
1487 goto error_backlight;
Corentin Charya6df4892011-11-26 10:59:58 +01001488
Corentin Chary5dea7a22011-11-26 10:59:59 +01001489 ret = samsung_rfkill_init(samsung);
1490 if (ret)
1491 goto error_rfkill;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001492
Corentin Charyf674ebf2011-11-26 11:00:08 +01001493 ret = samsung_leds_init(samsung);
1494 if (ret)
1495 goto error_leds;
1496
Corentin Chary5b80fc42011-11-26 11:00:03 +01001497 ret = samsung_debugfs_init(samsung);
1498 if (ret)
1499 goto error_debugfs;
1500
Corentin Chary5dea7a22011-11-26 10:59:59 +01001501 samsung_platform_device = samsung->platform_device;
1502 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001503
Corentin Chary5b80fc42011-11-26 11:00:03 +01001504error_debugfs:
Corentin Charyf674ebf2011-11-26 11:00:08 +01001505 samsung_leds_exit(samsung);
1506error_leds:
Corentin Chary5b80fc42011-11-26 11:00:03 +01001507 samsung_rfkill_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001508error_rfkill:
1509 samsung_backlight_exit(samsung);
1510error_backlight:
1511 samsung_sysfs_exit(samsung);
1512error_sysfs:
1513 samsung_sabi_exit(samsung);
1514error_sabi:
1515 samsung_platform_exit(samsung);
1516error_platform:
Corentin Charya6df4892011-11-26 10:59:58 +01001517 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001518 return ret;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001519}
1520
1521static void __exit samsung_exit(void)
1522{
Corentin Chary5dea7a22011-11-26 10:59:59 +01001523 struct samsung_laptop *samsung;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001524
Corentin Chary5dea7a22011-11-26 10:59:59 +01001525 samsung = platform_get_drvdata(samsung_platform_device);
Corentin Charya6df4892011-11-26 10:59:58 +01001526
Corentin Chary5b80fc42011-11-26 11:00:03 +01001527 samsung_debugfs_exit(samsung);
Corentin Charyf674ebf2011-11-26 11:00:08 +01001528 samsung_leds_exit(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001529 samsung_rfkill_exit(samsung);
1530 samsung_backlight_exit(samsung);
1531 samsung_sysfs_exit(samsung);
1532 samsung_sabi_exit(samsung);
1533 samsung_platform_exit(samsung);
1534
Corentin Charya6df4892011-11-26 10:59:58 +01001535 kfree(samsung);
Corentin Chary5dea7a22011-11-26 10:59:59 +01001536 samsung_platform_device = NULL;
Greg Kroah-Hartman2d70b732011-03-11 12:41:19 -05001537}
1538
1539module_init(samsung_init);
1540module_exit(samsung_exit);
1541
1542MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1543MODULE_DESCRIPTION("Samsung Backlight driver");
1544MODULE_LICENSE("GPL");