Staging: samsung-laptop: fix up some variable nameing
[deliverable/linux.git] / drivers / staging / samsung-laptop / samsung-laptop.c
1 /*
2 * Samsung N130 Laptop driver
3 *
4 * Copyright (C) 2009 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009 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 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
17 #include <linux/backlight.h>
18 #include <linux/fb.h>
19 #include <linux/dmi.h>
20 #include <linux/platform_device.h>
21 #include <linux/rfkill.h>
22
23 /*
24 * This driver is needed because a number of Samsung laptops do not hook
25 * their control settings through ACPI. So we have to poke around in the
26 * BIOS to do things like brightness values, and "special" key controls.
27 */
28
29 /*
30 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
31 * be reserved by the BIOS (which really doesn't make much sense), we tell
32 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
33 */
34 #define MAX_BRIGHT 0x07
35
36
37 #define SABI_IFACE_MAIN 0x00
38 #define SABI_IFACE_SUB 0x02
39 #define SABI_IFACE_COMPLETE 0x04
40 #define SABI_IFACE_DATA 0x05
41
42 /* Structure to get data back to the calling function */
43 struct sabi_retval {
44 u8 retval[20];
45 };
46
47 struct sabi_header_offsets {
48 u8 port;
49 u8 re_mem;
50 u8 iface_func;
51 u8 en_mem;
52 u8 data_offset;
53 u8 data_segment;
54 };
55
56 struct sabi_commands {
57 /*
58 * Brightness is 0 - 8, as described above.
59 * Value 0 is for the BIOS to use
60 */
61 u8 get_brightness;
62 u8 set_brightness;
63
64 /*
65 * first byte:
66 * 0x00 - wireless is off
67 * 0x01 - wireless is on
68 * second byte:
69 * 0x02 - 3G is off
70 * 0x03 - 3G is on
71 * TODO, verify 3G is correct, that doesn't seem right...
72 */
73 u8 get_wireless_button;
74 u8 set_wireless_button;
75
76 /* 0 is off, 1 is on */
77 u8 get_backlight;
78 u8 set_backlight;
79
80 /*
81 * 0x80 or 0x00 - no action
82 * 0x81 - recovery key pressed
83 */
84 u8 get_recovery_mode;
85 u8 set_recovery_mode;
86
87 /*
88 * on seclinux: 0 is low, 1 is high,
89 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
90 */
91 u8 get_performance_level;
92 u8 set_performance_level;
93
94 /*
95 * Tell the BIOS that Linux is running on this machine.
96 * 81 is on, 80 is off
97 */
98 u8 set_linux;
99 };
100
101 struct sabi_performance_level {
102 const char *name;
103 u8 value;
104 };
105
106 struct sabi_config {
107 const char *test_string;
108 u16 main_function;
109 struct sabi_header_offsets header_offsets;
110 struct sabi_commands commands;
111 struct sabi_performance_level performance_levels[4];
112 };
113
114 static struct sabi_config sabi_configs[] = {
115 {
116 .test_string = "SECLINUX",
117
118 .main_function = 0x4c59,
119
120 .header_offsets = {
121 .port = 0x00,
122 .re_mem = 0x02,
123 .iface_func = 0x03,
124 .en_mem = 0x04,
125 .data_offset = 0x05,
126 .data_segment = 0x07,
127 },
128
129 .commands = {
130 .get_brightness = 0x00,
131 .set_brightness = 0x01,
132
133 .get_wireless_button = 0x02,
134 .set_wireless_button = 0x03,
135
136 .get_backlight = 0x04,
137 .set_backlight = 0x05,
138
139 .get_recovery_mode = 0x06,
140 .set_recovery_mode = 0x07,
141
142 .get_performance_level = 0x08,
143 .set_performance_level = 0x09,
144
145 .set_linux = 0x0a,
146 },
147
148 .performance_levels = {
149 {
150 .name = "silent",
151 .value = 0,
152 },
153 {
154 .name = "normal",
155 .value = 1,
156 },
157 { },
158 },
159 },
160 {
161 .test_string = "SwSmi@",
162
163 .main_function = 0x5843,
164
165 .header_offsets = {
166 .port = 0x00,
167 .re_mem = 0x04,
168 .iface_func = 0x02,
169 .en_mem = 0x03,
170 .data_offset = 0x05,
171 .data_segment = 0x07,
172 },
173
174 .commands = {
175 .get_brightness = 0x10,
176 .set_brightness = 0x11,
177
178 .get_wireless_button = 0x12,
179 .set_wireless_button = 0x13,
180
181 .get_backlight = 0x2d,
182 .set_backlight = 0x2e,
183
184 .get_recovery_mode = 0xff,
185 .set_recovery_mode = 0xff,
186
187 .get_performance_level = 0x31,
188 .set_performance_level = 0x32,
189
190 .set_linux = 0xff,
191 },
192
193 .performance_levels = {
194 {
195 .name = "normal",
196 .value = 0,
197 },
198 {
199 .name = "silent",
200 .value = 1,
201 },
202 {
203 .name = "overclock",
204 .value = 2,
205 },
206 { },
207 },
208 },
209 { },
210 };
211
212 static struct sabi_config *sabi_config;
213
214 static void __iomem *sabi;
215 static void __iomem *sabi_iface;
216 static void __iomem *f0000_segment;
217 static struct backlight_device *backlight_device;
218 static struct mutex sabi_mutex;
219 static struct platform_device *sdev;
220 static struct rfkill *rfk;
221
222 static int force;
223 module_param(force, bool, 0);
224 MODULE_PARM_DESC(force,
225 "Disable the DMI check and forces the driver to be loaded");
226
227 static int debug;
228 module_param(debug, bool, S_IRUGO | S_IWUSR);
229 MODULE_PARM_DESC(debug, "Debug enabled or not");
230
231 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
232 {
233 int retval = 0;
234 u16 port = readw(sabi + sabi_config->header_offsets.port);
235
236 mutex_lock(&sabi_mutex);
237
238 /* enable memory to be able to write to it */
239 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
240
241 /* write out the command */
242 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
243 writew(command, sabi_iface + SABI_IFACE_SUB);
244 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
245 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
246
247 /* write protect memory to make it safe */
248 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
249
250 /* see if the command actually succeeded */
251 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
252 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
253 /*
254 * It did!
255 * Save off the data into a structure so the caller use it.
256 * Right now we only care about the first 4 bytes,
257 * I suppose there are commands that need more, but I don't
258 * know about them.
259 */
260 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
261 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
262 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
263 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
264 goto exit;
265 }
266
267 /* Something bad happened, so report it and error out */
268 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
269 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
270 readb(sabi_iface + SABI_IFACE_DATA));
271 retval = -EINVAL;
272 exit:
273 mutex_unlock(&sabi_mutex);
274 return retval;
275
276 }
277
278 static int sabi_set_command(u8 command, u8 data)
279 {
280 int retval = 0;
281 u16 port = readw(sabi + sabi_config->header_offsets.port);
282
283 mutex_lock(&sabi_mutex);
284
285 /* enable memory to be able to write to it */
286 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
287
288 /* write out the command */
289 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
290 writew(command, sabi_iface + SABI_IFACE_SUB);
291 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
292 writeb(data, sabi_iface + SABI_IFACE_DATA);
293 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
294
295 /* write protect memory to make it safe */
296 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
297
298 /* see if the command actually succeeded */
299 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
300 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
301 /* it did! */
302 goto exit;
303 }
304
305 /* Something bad happened, so report it and error out */
306 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
307 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
308 readb(sabi_iface + SABI_IFACE_DATA));
309 retval = -EINVAL;
310 exit:
311 mutex_unlock(&sabi_mutex);
312 return retval;
313 }
314
315 static void test_backlight(void)
316 {
317 struct sabi_retval sretval;
318
319 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
320 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
321
322 sabi_set_command(sabi_config->commands.set_backlight, 0);
323 printk(KERN_DEBUG "backlight should be off\n");
324
325 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
326 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
327
328 msleep(1000);
329
330 sabi_set_command(sabi_config->commands.set_backlight, 1);
331 printk(KERN_DEBUG "backlight should be on\n");
332
333 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
334 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
335 }
336
337 static void test_wireless(void)
338 {
339 struct sabi_retval sretval;
340
341 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
342 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
343
344 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
345 printk(KERN_DEBUG "wireless led should be off\n");
346
347 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
348 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
349
350 msleep(1000);
351
352 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
353 printk(KERN_DEBUG "wireless led should be on\n");
354
355 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
356 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
357 }
358
359 static u8 read_brightness(void)
360 {
361 struct sabi_retval sretval;
362 int user_brightness = 0;
363 int retval;
364
365 retval = sabi_get_command(sabi_config->commands.get_brightness,
366 &sretval);
367 if (!retval)
368 user_brightness = sretval.retval[0];
369 if (user_brightness != 0)
370 --user_brightness;
371 return user_brightness;
372 }
373
374 static void set_brightness(u8 user_brightness)
375 {
376 sabi_set_command(sabi_config->commands.set_brightness,
377 user_brightness + 1);
378 }
379
380 static int get_brightness(struct backlight_device *bd)
381 {
382 return (int)read_brightness();
383 }
384
385 static int update_status(struct backlight_device *bd)
386 {
387 set_brightness(bd->props.brightness);
388
389 if (bd->props.power == FB_BLANK_UNBLANK)
390 sabi_set_command(sabi_config->commands.set_backlight, 1);
391 else
392 sabi_set_command(sabi_config->commands.set_backlight, 0);
393 return 0;
394 }
395
396 static const struct backlight_ops backlight_ops = {
397 .get_brightness = get_brightness,
398 .update_status = update_status,
399 };
400
401 static int rfkill_set(void *data, bool blocked)
402 {
403 /* Do something with blocked...*/
404 /*
405 * blocked == false is on
406 * blocked == true is off
407 */
408 if (blocked)
409 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
410 else
411 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
412
413 return 0;
414 }
415
416 static struct rfkill_ops rfkill_ops = {
417 .set_block = rfkill_set,
418 };
419
420 static int init_wireless(struct platform_device *sdev)
421 {
422 int retval;
423
424 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
425 &rfkill_ops, NULL);
426 if (!rfk)
427 return -ENOMEM;
428
429 retval = rfkill_register(rfk);
430 if (retval) {
431 rfkill_destroy(rfk);
432 return -ENODEV;
433 }
434
435 return 0;
436 }
437
438 static void destroy_wireless(void)
439 {
440 rfkill_unregister(rfk);
441 rfkill_destroy(rfk);
442 }
443
444 static ssize_t get_performance_level(struct device *dev,
445 struct device_attribute *attr, char *buf)
446 {
447 struct sabi_retval sretval;
448 int retval;
449 int i;
450
451 /* Read the state */
452 retval = sabi_get_command(sabi_config->commands.get_performance_level,
453 &sretval);
454 if (retval)
455 return retval;
456
457 /* The logic is backwards, yeah, lots of fun... */
458 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
459 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
460 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
461 }
462 return sprintf(buf, "%s\n", "unknown");
463 }
464
465 static ssize_t set_performance_level(struct device *dev,
466 struct device_attribute *attr, const char *buf,
467 size_t count)
468 {
469 if (count >= 1) {
470 int i;
471 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
472 struct sabi_performance_level *level =
473 &sabi_config->performance_levels[i];
474 if (!strncasecmp(level->name, buf, strlen(level->name))) {
475 sabi_set_command(sabi_config->commands.set_performance_level,
476 level->value);
477 break;
478 }
479 }
480 if (!sabi_config->performance_levels[i].name)
481 return -EINVAL;
482 }
483 return count;
484 }
485 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
486 get_performance_level, set_performance_level);
487
488
489 static int __init dmi_check_cb(const struct dmi_system_id *id)
490 {
491 printk(KERN_INFO KBUILD_MODNAME ": found laptop model '%s'\n",
492 id->ident);
493 return 0;
494 }
495
496 static struct dmi_system_id __initdata samsung_dmi_table[] = {
497 {
498 .ident = "N128",
499 .matches = {
500 DMI_MATCH(DMI_SYS_VENDOR,
501 "SAMSUNG ELECTRONICS CO., LTD."),
502 DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
503 DMI_MATCH(DMI_BOARD_NAME, "N128"),
504 },
505 .callback = dmi_check_cb,
506 },
507 {
508 .ident = "N130",
509 .matches = {
510 DMI_MATCH(DMI_SYS_VENDOR,
511 "SAMSUNG ELECTRONICS CO., LTD."),
512 DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
513 DMI_MATCH(DMI_BOARD_NAME, "N130"),
514 },
515 .callback = dmi_check_cb,
516 },
517 {
518 .ident = "X125",
519 .matches = {
520 DMI_MATCH(DMI_SYS_VENDOR,
521 "SAMSUNG ELECTRONICS CO., LTD."),
522 DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
523 DMI_MATCH(DMI_BOARD_NAME, "X125"),
524 },
525 .callback = dmi_check_cb,
526 },
527 { },
528 };
529 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
530
531 static int find_signature(void __iomem *memcheck, const char *testStr)
532 {
533 int i = 0;
534 int loca;
535
536 for (loca = 0; loca < 0xffff; loca++) {
537 char temp = readb(memcheck + loca);
538
539 if (temp == testStr[i]) {
540 if (i == strlen(testStr)-1)
541 break;
542 ++i;
543 } else {
544 i = 0;
545 }
546 }
547 return loca;
548 }
549
550 static int __init samsung_init(void)
551 {
552 struct backlight_properties props;
553 struct sabi_retval sretval;
554 unsigned int ifaceP;
555 int i;
556 int loca;
557 int retval;
558
559 mutex_init(&sabi_mutex);
560
561 if (!force && !dmi_check_system(samsung_dmi_table))
562 return -ENODEV;
563
564 f0000_segment = ioremap(0xf0000, 0xffff);
565 if (!f0000_segment) {
566 printk(KERN_ERR "Can't map the segment at 0xf0000\n");
567 return -EINVAL;
568 }
569
570 /* Try to find one of the signatures in memory to find the header */
571 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
572 sabi_config = &sabi_configs[i];
573 loca = find_signature(f0000_segment, sabi_config->test_string);
574 if (loca != 0xffff)
575 break;
576 }
577
578 if (loca == 0xffff) {
579 printk(KERN_ERR "This computer does not support SABI\n");
580 goto error_no_signature;
581 }
582
583 /* point to the SMI port Number */
584 loca += 1;
585 sabi = (f0000_segment + loca);
586
587 if (debug) {
588 printk(KERN_DEBUG "This computer supports SABI==%x\n",
589 loca + 0xf0000 - 6);
590 printk(KERN_DEBUG "SABI header:\n");
591 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
592 readw(sabi + sabi_config->header_offsets.port));
593 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
594 readb(sabi + sabi_config->header_offsets.iface_func));
595 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
596 readb(sabi + sabi_config->header_offsets.en_mem));
597 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
598 readb(sabi + sabi_config->header_offsets.re_mem));
599 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
600 readw(sabi + sabi_config->header_offsets.data_offset));
601 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
602 readw(sabi + sabi_config->header_offsets.data_segment));
603 }
604
605 /* Get a pointer to the SABI Interface */
606 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
607 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
608 sabi_iface = ioremap(ifaceP, 16);
609 if (!sabi_iface) {
610 printk(KERN_ERR "Can't remap %x\n", ifaceP);
611 goto exit;
612 }
613 if (debug) {
614 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
615 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
616
617 test_backlight();
618 test_wireless();
619
620 retval = sabi_get_command(sabi_config->commands.get_brightness,
621 &sretval);
622 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
623 }
624
625 /* Turn on "Linux" mode in the BIOS */
626 if (sabi_config->commands.set_linux != 0xff) {
627 retval = sabi_set_command(sabi_config->commands.set_linux,
628 0x81);
629 if (retval) {
630 printk(KERN_ERR KBUILD_MODNAME ": Linux mode was not set!\n");
631 goto error_no_platform;
632 }
633 }
634
635 /* knock up a platform device to hang stuff off of */
636 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
637 if (IS_ERR(sdev))
638 goto error_no_platform;
639
640 /* create a backlight device to talk to this one */
641 memset(&props, 0, sizeof(struct backlight_properties));
642 props.max_brightness = MAX_BRIGHT;
643 backlight_device = backlight_device_register("samsung", &sdev->dev,
644 NULL, &backlight_ops,
645 &props);
646 if (IS_ERR(backlight_device))
647 goto error_no_backlight;
648
649 backlight_device->props.brightness = read_brightness();
650 backlight_device->props.power = FB_BLANK_UNBLANK;
651 backlight_update_status(backlight_device);
652
653 retval = init_wireless(sdev);
654 if (retval)
655 goto error_no_rfk;
656
657 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
658 if (retval)
659 goto error_file_create;
660
661 exit:
662 return 0;
663
664 error_file_create:
665 destroy_wireless();
666
667 error_no_rfk:
668 backlight_device_unregister(backlight_device);
669
670 error_no_backlight:
671 platform_device_unregister(sdev);
672
673 error_no_platform:
674 iounmap(sabi_iface);
675
676 error_no_signature:
677 iounmap(f0000_segment);
678 return -EINVAL;
679 }
680
681 static void __exit samsung_exit(void)
682 {
683 /* Turn off "Linux" mode in the BIOS */
684 if (sabi_config->commands.set_linux != 0xff)
685 sabi_set_command(sabi_config->commands.set_linux, 0x80);
686
687 device_remove_file(&sdev->dev, &dev_attr_performance_level);
688 backlight_device_unregister(backlight_device);
689 destroy_wireless();
690 iounmap(sabi_iface);
691 iounmap(f0000_segment);
692 platform_device_unregister(sdev);
693 }
694
695 module_init(samsung_init);
696 module_exit(samsung_exit);
697
698 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
699 MODULE_DESCRIPTION("Samsung Backlight driver");
700 MODULE_LICENSE("GPL");
This page took 0.053669 seconds and 5 git commands to generate.