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