dd7ea4c075dbb319d5148c25fad0601592858d82
[safe/jmp/linux-2.6] / 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 /* Brightness is 0 - 8, as described above.  Value 0 is for the BIOS to use */
37 #define GET_BRIGHTNESS                  0x00
38 #define SET_BRIGHTNESS                  0x01
39
40 /* first byte:
41  * 0x00 - wireless is off
42  * 0x01 - wireless is on
43  * second byte:
44  * 0x02 - 3G is off
45  * 0x03 - 3G is on
46  * TODO, verify 3G is correct, that doesn't seem right...
47  */
48 #define GET_WIRELESS_BUTTON             0x02
49 #define SET_WIRELESS_BUTTON             0x03
50
51 /* 0 is off, 1 is on */
52 #define GET_BACKLIGHT                   0x04
53 #define SET_BACKLIGHT                   0x05
54
55 /*
56  * 0x80 or 0x00 - no action
57  * 0x81 - recovery key pressed
58  */
59 #define GET_RECOVERY_METHOD             0x06
60 #define SET_RECOVERY_METHOD             0x07
61
62 /* 0 is low, 1 is high */
63 #define GET_PERFORMANCE_LEVEL           0x08
64 #define SET_PERFORMANCE_LEVEL           0x09
65
66 /*
67  * Tell the BIOS that Linux is running on this machine.
68  * 81 is on, 80 is off
69  */
70 #define SET_LINUX                       0x0a
71
72
73 #define MAIN_FUNCTION                   0x4c49
74
75 #define SABI_HEADER_PORT                0x00
76 #define SABI_HEADER_RE_MEM              0x02
77 #define SABI_HEADER_IFACEFUNC           0x03
78 #define SABI_HEADER_EN_MEM              0x04
79 #define SABI_HEADER_DATA_OFFSET         0x05
80 #define SABI_HEADER_DATA_SEGMENT        0x07
81
82 #define SABI_IFACE_MAIN                 0x00
83 #define SABI_IFACE_SUB                  0x02
84 #define SABI_IFACE_COMPLETE             0x04
85 #define SABI_IFACE_DATA                 0x05
86
87 /* Structure to get data back to the calling function */
88 struct sabi_retval {
89         u8 retval[20];
90 };
91
92 static void __iomem *sabi;
93 static void __iomem *sabi_iface;
94 static void __iomem *f0000_segment;
95 static struct backlight_device *backlight_device;
96 static struct mutex sabi_mutex;
97 static struct platform_device *sdev;
98 static struct rfkill *rfk;
99
100 static int force;
101 module_param(force, bool, 0);
102 MODULE_PARM_DESC(force,
103                 "Disable the DMI check and forces the driver to be loaded");
104
105 static int debug;
106 module_param(debug, bool, S_IRUGO | S_IWUSR);
107 MODULE_PARM_DESC(debug, "Debug enabled or not");
108
109 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
110 {
111         int retval = 0;
112         u16 port = readw(sabi + SABI_HEADER_PORT);
113
114         mutex_lock(&sabi_mutex);
115
116         /* enable memory to be able to write to it */
117         outb(readb(sabi + SABI_HEADER_EN_MEM), port);
118
119         /* write out the command */
120         writew(MAIN_FUNCTION, sabi_iface + SABI_IFACE_MAIN);
121         writew(command, sabi_iface + SABI_IFACE_SUB);
122         writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
123         outb(readb(sabi + SABI_HEADER_IFACEFUNC), port);
124
125         /* write protect memory to make it safe */
126         outb(readb(sabi + SABI_HEADER_RE_MEM), port);
127
128         /* see if the command actually succeeded */
129         if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
130             readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
131                 /*
132                  * It did!
133                  * Save off the data into a structure so the caller use it.
134                  * Right now we only care about the first 4 bytes,
135                  * I suppose there are commands that need more, but I don't
136                  * know about them.
137                  */
138                 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
139                 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
140                 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
141                 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
142                 goto exit;
143         }
144
145         /* Something bad happened, so report it and error out */
146         printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
147                 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
148                 readb(sabi_iface + SABI_IFACE_DATA));
149         retval = -EINVAL;
150 exit:
151         mutex_unlock(&sabi_mutex);
152         return retval;
153
154 }
155
156 static int sabi_set_command(u8 command, u8 data)
157 {
158         int retval = 0;
159         u16 port = readw(sabi + SABI_HEADER_PORT);
160
161         mutex_lock(&sabi_mutex);
162
163         /* enable memory to be able to write to it */
164         outb(readb(sabi + SABI_HEADER_EN_MEM), port);
165
166         /* write out the command */
167         writew(MAIN_FUNCTION, sabi_iface + SABI_IFACE_MAIN);
168         writew(command, sabi_iface + SABI_IFACE_SUB);
169         writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
170         writeb(data, sabi_iface + SABI_IFACE_DATA);
171         outb(readb(sabi + SABI_HEADER_IFACEFUNC), port);
172
173         /* write protect memory to make it safe */
174         outb(readb(sabi + SABI_HEADER_RE_MEM), port);
175
176         /* see if the command actually succeeded */
177         if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
178             readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
179                 /* it did! */
180                 goto exit;
181         }
182
183         /* Something bad happened, so report it and error out */
184         printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
185                 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
186                 readb(sabi_iface + SABI_IFACE_DATA));
187         retval = -EINVAL;
188 exit:
189         mutex_unlock(&sabi_mutex);
190         return retval;
191 }
192
193 static void test_backlight(void)
194 {
195         struct sabi_retval sretval;
196
197         sabi_get_command(GET_BACKLIGHT, &sretval);
198         printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
199
200         sabi_set_command(SET_BACKLIGHT, 0);
201         printk(KERN_DEBUG "backlight should be off\n");
202
203         sabi_get_command(GET_BACKLIGHT, &sretval);
204         printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
205
206         msleep(1000);
207
208         sabi_set_command(SET_BACKLIGHT, 1);
209         printk(KERN_DEBUG "backlight should be on\n");
210
211         sabi_get_command(GET_BACKLIGHT, &sretval);
212         printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
213 }
214
215 static void test_wireless(void)
216 {
217         struct sabi_retval sretval;
218
219         sabi_get_command(GET_WIRELESS_BUTTON, &sretval);
220         printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
221
222         sabi_set_command(SET_WIRELESS_BUTTON, 0);
223         printk(KERN_DEBUG "wireless led should be off\n");
224
225         sabi_get_command(GET_WIRELESS_BUTTON, &sretval);
226         printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
227
228         msleep(1000);
229
230         sabi_set_command(SET_WIRELESS_BUTTON, 1);
231         printk(KERN_DEBUG "wireless led should be on\n");
232
233         sabi_get_command(GET_WIRELESS_BUTTON, &sretval);
234         printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
235 }
236
237 static u8 read_brightness(void)
238 {
239         struct sabi_retval sretval;
240         int user_brightness = 0;
241         int retval;
242
243         retval = sabi_get_command(GET_BRIGHTNESS, &sretval);
244         if (!retval)
245                 user_brightness = sretval.retval[0];
246                 if (user_brightness != 0)
247                         --user_brightness;
248         return user_brightness;
249 }
250
251 static void set_brightness(u8 user_brightness)
252 {
253         sabi_set_command(SET_BRIGHTNESS, user_brightness + 1);
254 }
255
256 static int get_brightness(struct backlight_device *bd)
257 {
258         return (int)read_brightness();
259 }
260
261 static int update_status(struct backlight_device *bd)
262 {
263         set_brightness(bd->props.brightness);
264
265         if (bd->props.power == FB_BLANK_UNBLANK)
266                 sabi_set_command(SET_BACKLIGHT, 1);
267         else
268                 sabi_set_command(SET_BACKLIGHT, 0);
269         return 0;
270 }
271
272 static struct backlight_ops backlight_ops = {
273         .get_brightness = get_brightness,
274         .update_status  = update_status,
275 };
276
277 static int rfkill_set(void *data, bool blocked)
278 {
279         /* Do something with blocked...*/
280         /*
281          * blocked == false is on
282          * blocked == true is off
283          */
284         if (blocked)
285                 sabi_set_command(SET_WIRELESS_BUTTON, 0);
286         else
287                 sabi_set_command(SET_WIRELESS_BUTTON, 1);
288
289         return 0;
290 }
291
292 static struct rfkill_ops rfkill_ops = {
293         .set_block = rfkill_set,
294 };
295
296 static int init_wireless(struct platform_device *sdev)
297 {
298         int retval;
299
300         rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
301                            &rfkill_ops, NULL);
302         if (!rfk)
303                 return -ENOMEM;
304
305         retval = rfkill_register(rfk);
306         if (retval) {
307                 rfkill_destroy(rfk);
308                 return -ENODEV;
309         }
310
311         return 0;
312 }
313
314 static void destroy_wireless(void)
315 {
316         rfkill_unregister(rfk);
317         rfkill_destroy(rfk);
318 }
319
320 static ssize_t get_silent_state(struct device *dev,
321                                 struct device_attribute *attr, char *buf)
322 {
323         struct sabi_retval sretval;
324         int retval;
325
326         /* Read the state */
327         retval = sabi_get_command(GET_PERFORMANCE_LEVEL, &sretval);
328         if (retval)
329                 return retval;
330
331         /* The logic is backwards, yeah, lots of fun... */
332         if (sretval.retval[0] == 0)
333                 retval = 1;
334         else
335                 retval = 0;
336         return sprintf(buf, "%d\n", retval);
337 }
338
339 static ssize_t set_silent_state(struct device *dev,
340                                 struct device_attribute *attr, const char *buf,
341                                 size_t count)
342 {
343         char value;
344
345         if (count >= 1) {
346                 value = buf[0];
347                 if ((value == '0') || (value == 'n') || (value == 'N')) {
348                         /* Turn speed up */
349                         sabi_set_command(SET_PERFORMANCE_LEVEL, 0x01);
350                 } else if ((value == '1') || (value == 'y') || (value == 'Y')) {
351                         /* Turn speed down */
352                         sabi_set_command(SET_PERFORMANCE_LEVEL, 0x00);
353                 } else {
354                         return -EINVAL;
355                 }
356         }
357         return count;
358 }
359 static DEVICE_ATTR(silent, S_IWUGO | S_IRUGO,
360                    get_silent_state, set_silent_state);
361
362
363 static int __init dmi_check_cb(const struct dmi_system_id *id)
364 {
365         printk(KERN_INFO KBUILD_MODNAME ": found laptop model '%s'\n",
366                 id->ident);
367         return 0;
368 }
369
370 static struct dmi_system_id __initdata samsung_dmi_table[] = {
371         {
372                 .ident = "N128",
373                 .matches = {
374                         DMI_MATCH(DMI_SYS_VENDOR,
375                                         "SAMSUNG ELECTRONICS CO., LTD."),
376                         DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
377                         DMI_MATCH(DMI_BOARD_NAME, "N128"),
378                 },
379                 .callback = dmi_check_cb,
380         },
381         {
382                 .ident = "N130",
383                 .matches = {
384                         DMI_MATCH(DMI_SYS_VENDOR,
385                                         "SAMSUNG ELECTRONICS CO., LTD."),
386                         DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
387                         DMI_MATCH(DMI_BOARD_NAME, "N130"),
388                 },
389                 .callback = dmi_check_cb,
390         },
391         { },
392 };
393 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
394
395 static int __init samsung_init(void)
396 {
397         struct sabi_retval sretval;
398         const char *testStr = "SECLINUX";
399         void __iomem *memcheck;
400         unsigned int ifaceP;
401         int pStr;
402         int loca;
403         int retval;
404
405         mutex_init(&sabi_mutex);
406
407         if (!force && !dmi_check_system(samsung_dmi_table))
408                 return -ENODEV;
409
410         f0000_segment = ioremap(0xf0000, 0xffff);
411         if (!f0000_segment) {
412                 printk(KERN_ERR "Can't map the segment at 0xf0000\n");
413                 return -EINVAL;
414         }
415
416         /* Try to find the signature "SECLINUX" in memory to find the header */
417         pStr = 0;
418         memcheck = f0000_segment;
419         for (loca = 0; loca < 0xffff; loca++) {
420                 char temp = readb(memcheck + loca);
421
422                 if (temp == testStr[pStr]) {
423                         if (pStr == strlen(testStr)-1)
424                                 break;
425                         ++pStr;
426                 } else {
427                         pStr = 0;
428                 }
429         }
430         if (loca == 0xffff) {
431                 printk(KERN_ERR "This computer does not support SABI\n");
432                 goto error_no_signature;
433                 }
434
435         /* point to the SMI port Number */
436         loca += 1;
437         sabi = (memcheck + loca);
438
439         if (debug) {
440                 printk(KERN_DEBUG "This computer supports SABI==%x\n",
441                         loca + 0xf0000 - 6);
442                 printk(KERN_DEBUG "SABI header:\n");
443                 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
444                         readw(sabi + SABI_HEADER_PORT));
445                 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
446                         readb(sabi + SABI_HEADER_IFACEFUNC));
447                 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
448                         readb(sabi + SABI_HEADER_EN_MEM));
449                 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
450                         readb(sabi + SABI_HEADER_RE_MEM));
451                 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
452                         readw(sabi + SABI_HEADER_DATA_OFFSET));
453                 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
454                         readw(sabi + SABI_HEADER_DATA_SEGMENT));
455         }
456
457         /* Get a pointer to the SABI Interface */
458         ifaceP = (readw(sabi + SABI_HEADER_DATA_SEGMENT) & 0x0ffff) << 4;
459         ifaceP += readw(sabi + SABI_HEADER_DATA_OFFSET) & 0x0ffff;
460         sabi_iface = ioremap(ifaceP, 16);
461         if (!sabi_iface) {
462                 printk(KERN_ERR "Can't remap %x\n", ifaceP);
463                 goto exit;
464         }
465         if (debug) {
466                 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
467                 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
468
469                 test_backlight();
470                 test_wireless();
471
472                 retval = sabi_get_command(GET_BRIGHTNESS, &sretval);
473                 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
474         }
475
476         /* Turn on "Linux" mode in the BIOS */
477         retval = sabi_set_command(SET_LINUX, 0x81);
478         if (retval) {
479                 printk(KERN_ERR KBUILD_MODNAME ": Linux mode was not set!\n");
480                 goto error_no_platform;
481         }
482
483         /* knock up a platform device to hang stuff off of */
484         sdev = platform_device_register_simple("samsung", -1, NULL, 0);
485         if (IS_ERR(sdev))
486                 goto error_no_platform;
487
488         /* create a backlight device to talk to this one */
489         backlight_device = backlight_device_register("samsung", &sdev->dev,
490                                                      NULL, &backlight_ops);
491         if (IS_ERR(backlight_device))
492                 goto error_no_backlight;
493
494         backlight_device->props.max_brightness = MAX_BRIGHT;
495         backlight_device->props.brightness = read_brightness();
496         backlight_device->props.power = FB_BLANK_UNBLANK;
497         backlight_update_status(backlight_device);
498
499         retval = init_wireless(sdev);
500         if (retval)
501                 goto error_no_rfk;
502
503         retval = device_create_file(&sdev->dev, &dev_attr_silent);
504         if (retval)
505                 goto error_file_create;
506
507 exit:
508         return 0;
509
510 error_file_create:
511         destroy_wireless();
512
513 error_no_rfk:
514         backlight_device_unregister(backlight_device);
515
516 error_no_backlight:
517         platform_device_unregister(sdev);
518
519 error_no_platform:
520         iounmap(sabi_iface);
521
522 error_no_signature:
523         iounmap(f0000_segment);
524         return -EINVAL;
525 }
526
527 static void __exit samsung_exit(void)
528 {
529         /* Turn off "Linux" mode in the BIOS */
530         sabi_set_command(SET_LINUX, 0x80);
531
532         device_remove_file(&sdev->dev, &dev_attr_silent);
533         backlight_device_unregister(backlight_device);
534         destroy_wireless();
535         iounmap(sabi_iface);
536         iounmap(f0000_segment);
537         platform_device_unregister(sdev);
538 }
539
540 module_init(samsung_init);
541 module_exit(samsung_exit);
542
543 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
544 MODULE_DESCRIPTION("Samsung Backlight driver");
545 MODULE_LICENSE("GPL");