samsung-laptop.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Samsung Laptop driver
  3. *
  4. * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
  5. * Copyright (C) 2009,2011 Novell Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/pci.h>
  18. #include <linux/backlight.h>
  19. #include <linux/fb.h>
  20. #include <linux/dmi.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/rfkill.h>
  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. * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
  30. * be reserved by the BIOS (which really doesn't make much sense), we tell
  31. * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
  32. */
  33. #define MAX_BRIGHT 0x07
  34. #define SABI_IFACE_MAIN 0x00
  35. #define SABI_IFACE_SUB 0x02
  36. #define SABI_IFACE_COMPLETE 0x04
  37. #define SABI_IFACE_DATA 0x05
  38. /* Structure to get data back to the calling function */
  39. struct sabi_retval {
  40. u8 retval[20];
  41. };
  42. struct sabi_header_offsets {
  43. u8 port;
  44. u8 re_mem;
  45. u8 iface_func;
  46. u8 en_mem;
  47. u8 data_offset;
  48. u8 data_segment;
  49. };
  50. struct sabi_commands {
  51. /*
  52. * Brightness is 0 - 8, as described above.
  53. * Value 0 is for the BIOS to use
  54. */
  55. u8 get_brightness;
  56. u8 set_brightness;
  57. /*
  58. * first byte:
  59. * 0x00 - wireless is off
  60. * 0x01 - wireless is on
  61. * second byte:
  62. * 0x02 - 3G is off
  63. * 0x03 - 3G is on
  64. * TODO, verify 3G is correct, that doesn't seem right...
  65. */
  66. u8 get_wireless_button;
  67. u8 set_wireless_button;
  68. /* 0 is off, 1 is on */
  69. u8 get_backlight;
  70. u8 set_backlight;
  71. /*
  72. * 0x80 or 0x00 - no action
  73. * 0x81 - recovery key pressed
  74. */
  75. u8 get_recovery_mode;
  76. u8 set_recovery_mode;
  77. /*
  78. * on seclinux: 0 is low, 1 is high,
  79. * on swsmi: 0 is normal, 1 is silent, 2 is turbo
  80. */
  81. u8 get_performance_level;
  82. u8 set_performance_level;
  83. /*
  84. * Tell the BIOS that Linux is running on this machine.
  85. * 81 is on, 80 is off
  86. */
  87. u8 set_linux;
  88. };
  89. struct sabi_performance_level {
  90. const char *name;
  91. u8 value;
  92. };
  93. struct sabi_config {
  94. const char *test_string;
  95. u16 main_function;
  96. const struct sabi_header_offsets header_offsets;
  97. const struct sabi_commands commands;
  98. const struct sabi_performance_level performance_levels[4];
  99. u8 min_brightness;
  100. u8 max_brightness;
  101. };
  102. static const struct sabi_config sabi_configs[] = {
  103. {
  104. .test_string = "SECLINUX",
  105. .main_function = 0x4c49,
  106. .header_offsets = {
  107. .port = 0x00,
  108. .re_mem = 0x02,
  109. .iface_func = 0x03,
  110. .en_mem = 0x04,
  111. .data_offset = 0x05,
  112. .data_segment = 0x07,
  113. },
  114. .commands = {
  115. .get_brightness = 0x00,
  116. .set_brightness = 0x01,
  117. .get_wireless_button = 0x02,
  118. .set_wireless_button = 0x03,
  119. .get_backlight = 0x04,
  120. .set_backlight = 0x05,
  121. .get_recovery_mode = 0x06,
  122. .set_recovery_mode = 0x07,
  123. .get_performance_level = 0x08,
  124. .set_performance_level = 0x09,
  125. .set_linux = 0x0a,
  126. },
  127. .performance_levels = {
  128. {
  129. .name = "silent",
  130. .value = 0,
  131. },
  132. {
  133. .name = "normal",
  134. .value = 1,
  135. },
  136. { },
  137. },
  138. .min_brightness = 1,
  139. .max_brightness = 8,
  140. },
  141. {
  142. .test_string = "SwSmi@",
  143. .main_function = 0x5843,
  144. .header_offsets = {
  145. .port = 0x00,
  146. .re_mem = 0x04,
  147. .iface_func = 0x02,
  148. .en_mem = 0x03,
  149. .data_offset = 0x05,
  150. .data_segment = 0x07,
  151. },
  152. .commands = {
  153. .get_brightness = 0x10,
  154. .set_brightness = 0x11,
  155. .get_wireless_button = 0x12,
  156. .set_wireless_button = 0x13,
  157. .get_backlight = 0x2d,
  158. .set_backlight = 0x2e,
  159. .get_recovery_mode = 0xff,
  160. .set_recovery_mode = 0xff,
  161. .get_performance_level = 0x31,
  162. .set_performance_level = 0x32,
  163. .set_linux = 0xff,
  164. },
  165. .performance_levels = {
  166. {
  167. .name = "normal",
  168. .value = 0,
  169. },
  170. {
  171. .name = "silent",
  172. .value = 1,
  173. },
  174. {
  175. .name = "overclock",
  176. .value = 2,
  177. },
  178. { },
  179. },
  180. .min_brightness = 0,
  181. .max_brightness = 8,
  182. },
  183. { },
  184. };
  185. struct samsung_laptop {
  186. const struct sabi_config *config;
  187. void __iomem *sabi;
  188. void __iomem *sabi_iface;
  189. void __iomem *f0000_segment;
  190. struct mutex sabi_mutex;
  191. struct platform_device *platform_device;
  192. struct backlight_device *backlight_device;
  193. struct rfkill *rfk;
  194. bool has_stepping_quirk;
  195. };
  196. static bool force;
  197. module_param(force, bool, 0);
  198. MODULE_PARM_DESC(force,
  199. "Disable the DMI check and forces the driver to be loaded");
  200. static bool debug;
  201. module_param(debug, bool, S_IRUGO | S_IWUSR);
  202. MODULE_PARM_DESC(debug, "Debug enabled or not");
  203. static int sabi_get_command(struct samsung_laptop *samsung,
  204. u8 command, struct sabi_retval *sretval)
  205. {
  206. const struct sabi_config *config = samsung->config;
  207. int retval = 0;
  208. u16 port = readw(samsung->sabi + config->header_offsets.port);
  209. u8 complete, iface_data;
  210. mutex_lock(&samsung->sabi_mutex);
  211. /* enable memory to be able to write to it */
  212. outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
  213. /* write out the command */
  214. writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
  215. writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
  216. writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
  217. outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
  218. /* write protect memory to make it safe */
  219. outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
  220. /* see if the command actually succeeded */
  221. complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
  222. iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
  223. if (complete != 0xaa || iface_data == 0xff) {
  224. pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
  225. command, complete, iface_data);
  226. retval = -EINVAL;
  227. goto exit;
  228. }
  229. /*
  230. * Save off the data into a structure so the caller use it.
  231. * Right now we only want the first 4 bytes,
  232. * There are commands that need more, but not for the ones we
  233. * currently care about.
  234. */
  235. sretval->retval[0] = readb(samsung->sabi_iface + SABI_IFACE_DATA);
  236. sretval->retval[1] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
  237. sretval->retval[2] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 2);
  238. sretval->retval[3] = readb(samsung->sabi_iface + SABI_IFACE_DATA + 3);
  239. exit:
  240. mutex_unlock(&samsung->sabi_mutex);
  241. return retval;
  242. }
  243. static int sabi_set_command(struct samsung_laptop *samsung,
  244. u8 command, u8 data)
  245. {
  246. const struct sabi_config *config = samsung->config;
  247. int retval = 0;
  248. u16 port = readw(samsung->sabi + config->header_offsets.port);
  249. u8 complete, iface_data;
  250. mutex_lock(&samsung->sabi_mutex);
  251. /* enable memory to be able to write to it */
  252. outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
  253. /* write out the command */
  254. writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
  255. writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
  256. writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
  257. writeb(data, samsung->sabi_iface + SABI_IFACE_DATA);
  258. outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
  259. /* write protect memory to make it safe */
  260. outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
  261. /* see if the command actually succeeded */
  262. complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
  263. iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
  264. if (complete != 0xaa || iface_data == 0xff) {
  265. pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
  266. command, complete, iface_data);
  267. retval = -EINVAL;
  268. }
  269. mutex_unlock(&samsung->sabi_mutex);
  270. return retval;
  271. }
  272. static void test_backlight(struct samsung_laptop *samsung)
  273. {
  274. const struct sabi_commands *commands = &samsung->config->commands;
  275. struct sabi_retval sretval;
  276. sabi_get_command(samsung, commands->get_backlight, &sretval);
  277. printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
  278. sabi_set_command(samsung, commands->set_backlight, 0);
  279. printk(KERN_DEBUG "backlight should be off\n");
  280. sabi_get_command(samsung, commands->get_backlight, &sretval);
  281. printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
  282. msleep(1000);
  283. sabi_set_command(samsung, commands->set_backlight, 1);
  284. printk(KERN_DEBUG "backlight should be on\n");
  285. sabi_get_command(samsung, commands->get_backlight, &sretval);
  286. printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
  287. }
  288. static void test_wireless(struct samsung_laptop *samsung)
  289. {
  290. const struct sabi_commands *commands = &samsung->config->commands;
  291. struct sabi_retval sretval;
  292. sabi_get_command(samsung, commands->get_wireless_button, &sretval);
  293. printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
  294. sabi_set_command(samsung, commands->set_wireless_button, 0);
  295. printk(KERN_DEBUG "wireless led should be off\n");
  296. sabi_get_command(samsung, commands->get_wireless_button, &sretval);
  297. printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
  298. msleep(1000);
  299. sabi_set_command(samsung, commands->set_wireless_button, 1);
  300. printk(KERN_DEBUG "wireless led should be on\n");
  301. sabi_get_command(samsung, commands->get_wireless_button, &sretval);
  302. printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
  303. }
  304. static int read_brightness(struct samsung_laptop *samsung)
  305. {
  306. const struct sabi_config *config = samsung->config;
  307. const struct sabi_commands *commands = &samsung->config->commands;
  308. struct sabi_retval sretval;
  309. int user_brightness = 0;
  310. int retval;
  311. retval = sabi_get_command(samsung, commands->get_brightness,
  312. &sretval);
  313. if (!retval) {
  314. user_brightness = sretval.retval[0];
  315. if (user_brightness > config->min_brightness)
  316. user_brightness -= config->min_brightness;
  317. else
  318. user_brightness = 0;
  319. }
  320. return user_brightness;
  321. }
  322. static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
  323. {
  324. const struct sabi_config *config = samsung->config;
  325. const struct sabi_commands *commands = &samsung->config->commands;
  326. u8 user_level = user_brightness + config->min_brightness;
  327. if (samsung->has_stepping_quirk && user_level != 0) {
  328. /*
  329. * short circuit if the specified level is what's already set
  330. * to prevent the screen from flickering needlessly
  331. */
  332. if (user_brightness == read_brightness(samsung))
  333. return;
  334. sabi_set_command(samsung, commands->set_brightness, 0);
  335. }
  336. sabi_set_command(samsung, commands->set_brightness, user_level);
  337. }
  338. static int get_brightness(struct backlight_device *bd)
  339. {
  340. struct samsung_laptop *samsung = bl_get_data(bd);
  341. return read_brightness(samsung);
  342. }
  343. static void check_for_stepping_quirk(struct samsung_laptop *samsung)
  344. {
  345. int initial_level;
  346. int check_level;
  347. int orig_level = read_brightness(samsung);
  348. /*
  349. * Some laptops exhibit the strange behaviour of stepping toward
  350. * (rather than setting) the brightness except when changing to/from
  351. * brightness level 0. This behaviour is checked for here and worked
  352. * around in set_brightness.
  353. */
  354. if (orig_level == 0)
  355. set_brightness(samsung, 1);
  356. initial_level = read_brightness(samsung);
  357. if (initial_level <= 2)
  358. check_level = initial_level + 2;
  359. else
  360. check_level = initial_level - 2;
  361. samsung->has_stepping_quirk = false;
  362. set_brightness(samsung, check_level);
  363. if (read_brightness(samsung) != check_level) {
  364. samsung->has_stepping_quirk = true;
  365. pr_info("enabled workaround for brightness stepping quirk\n");
  366. }
  367. set_brightness(samsung, orig_level);
  368. }
  369. static int update_status(struct backlight_device *bd)
  370. {
  371. struct samsung_laptop *samsung = bl_get_data(bd);
  372. const struct sabi_commands *commands = &samsung->config->commands;
  373. set_brightness(samsung, bd->props.brightness);
  374. if (bd->props.power == FB_BLANK_UNBLANK)
  375. sabi_set_command(samsung, commands->set_backlight, 1);
  376. else
  377. sabi_set_command(samsung, commands->set_backlight, 0);
  378. return 0;
  379. }
  380. static const struct backlight_ops backlight_ops = {
  381. .get_brightness = get_brightness,
  382. .update_status = update_status,
  383. };
  384. static int rfkill_set(void *data, bool blocked)
  385. {
  386. struct samsung_laptop *samsung = data;
  387. const struct sabi_commands *commands = &samsung->config->commands;
  388. /* Do something with blocked...*/
  389. /*
  390. * blocked == false is on
  391. * blocked == true is off
  392. */
  393. if (blocked)
  394. sabi_set_command(samsung, commands->set_wireless_button, 0);
  395. else
  396. sabi_set_command(samsung, commands->set_wireless_button, 1);
  397. return 0;
  398. }
  399. static struct rfkill_ops rfkill_ops = {
  400. .set_block = rfkill_set,
  401. };
  402. static ssize_t get_performance_level(struct device *dev,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  406. const struct sabi_config *config = samsung->config;
  407. const struct sabi_commands *commands = &config->commands;
  408. struct sabi_retval sretval;
  409. int retval;
  410. int i;
  411. /* Read the state */
  412. retval = sabi_get_command(samsung, commands->get_performance_level,
  413. &sretval);
  414. if (retval)
  415. return retval;
  416. /* The logic is backwards, yeah, lots of fun... */
  417. for (i = 0; config->performance_levels[i].name; ++i) {
  418. if (sretval.retval[0] == config->performance_levels[i].value)
  419. return sprintf(buf, "%s\n", config->performance_levels[i].name);
  420. }
  421. return sprintf(buf, "%s\n", "unknown");
  422. }
  423. static ssize_t set_performance_level(struct device *dev,
  424. struct device_attribute *attr, const char *buf,
  425. size_t count)
  426. {
  427. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  428. const struct sabi_config *config = samsung->config;
  429. const struct sabi_commands *commands = &config->commands;
  430. int i;
  431. if (count < 1)
  432. return count;
  433. for (i = 0; config->performance_levels[i].name; ++i) {
  434. const struct sabi_performance_level *level =
  435. &config->performance_levels[i];
  436. if (!strncasecmp(level->name, buf, strlen(level->name))) {
  437. sabi_set_command(samsung,
  438. commands->set_performance_level,
  439. level->value);
  440. break;
  441. }
  442. }
  443. if (!config->performance_levels[i].name)
  444. return -EINVAL;
  445. return count;
  446. }
  447. static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
  448. get_performance_level, set_performance_level);
  449. static int find_signature(void __iomem *memcheck, const char *testStr)
  450. {
  451. int i = 0;
  452. int loca;
  453. for (loca = 0; loca < 0xffff; loca++) {
  454. char temp = readb(memcheck + loca);
  455. if (temp == testStr[i]) {
  456. if (i == strlen(testStr)-1)
  457. break;
  458. ++i;
  459. } else {
  460. i = 0;
  461. }
  462. }
  463. return loca;
  464. }
  465. static void samsung_rfkill_exit(struct samsung_laptop *samsung)
  466. {
  467. if (samsung->rfk) {
  468. rfkill_unregister(samsung->rfk);
  469. rfkill_destroy(samsung->rfk);
  470. samsung->rfk = NULL;
  471. }
  472. }
  473. static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
  474. {
  475. int retval;
  476. samsung->rfk = rfkill_alloc("samsung-wifi",
  477. &samsung->platform_device->dev,
  478. RFKILL_TYPE_WLAN,
  479. &rfkill_ops, samsung);
  480. if (!samsung->rfk)
  481. return -ENOMEM;
  482. retval = rfkill_register(samsung->rfk);
  483. if (retval) {
  484. rfkill_destroy(samsung->rfk);
  485. samsung->rfk = NULL;
  486. return -ENODEV;
  487. }
  488. return 0;
  489. }
  490. static void samsung_backlight_exit(struct samsung_laptop *samsung)
  491. {
  492. if (samsung->backlight_device) {
  493. backlight_device_unregister(samsung->backlight_device);
  494. samsung->backlight_device = NULL;
  495. }
  496. }
  497. static int __init samsung_backlight_init(struct samsung_laptop *samsung)
  498. {
  499. struct backlight_device *bd;
  500. struct backlight_properties props;
  501. memset(&props, 0, sizeof(struct backlight_properties));
  502. props.type = BACKLIGHT_PLATFORM;
  503. props.max_brightness = samsung->config->max_brightness -
  504. samsung->config->min_brightness;
  505. bd = backlight_device_register("samsung",
  506. &samsung->platform_device->dev,
  507. samsung, &backlight_ops,
  508. &props);
  509. if (IS_ERR(bd))
  510. return PTR_ERR(bd);
  511. samsung->backlight_device = bd;
  512. samsung->backlight_device->props.brightness = read_brightness(samsung);
  513. samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
  514. backlight_update_status(samsung->backlight_device);
  515. return 0;
  516. }
  517. static void samsung_sysfs_exit(struct samsung_laptop *samsung)
  518. {
  519. device_remove_file(&samsung->platform_device->dev,
  520. &dev_attr_performance_level);
  521. }
  522. static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
  523. {
  524. return device_create_file(&samsung->platform_device->dev,
  525. &dev_attr_performance_level);
  526. }
  527. static void samsung_sabi_exit(struct samsung_laptop *samsung)
  528. {
  529. const struct sabi_config *config = samsung->config;
  530. /* Turn off "Linux" mode in the BIOS */
  531. if (config && config->commands.set_linux != 0xff)
  532. sabi_set_command(samsung, config->commands.set_linux, 0x80);
  533. if (samsung->sabi_iface) {
  534. iounmap(samsung->sabi_iface);
  535. samsung->sabi_iface = NULL;
  536. }
  537. if (samsung->f0000_segment) {
  538. iounmap(samsung->f0000_segment);
  539. samsung->f0000_segment = NULL;
  540. }
  541. samsung->config = NULL;
  542. }
  543. static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca)
  544. {
  545. const struct sabi_config *config = samsung->config;
  546. printk(KERN_DEBUG "This computer supports SABI==%x\n",
  547. loca + 0xf0000 - 6);
  548. printk(KERN_DEBUG "SABI header:\n");
  549. printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
  550. readw(samsung->sabi + config->header_offsets.port));
  551. printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
  552. readb(samsung->sabi + config->header_offsets.iface_func));
  553. printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
  554. readb(samsung->sabi + config->header_offsets.en_mem));
  555. printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
  556. readb(samsung->sabi + config->header_offsets.re_mem));
  557. printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
  558. readw(samsung->sabi + config->header_offsets.data_offset));
  559. printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
  560. readw(samsung->sabi + config->header_offsets.data_segment));
  561. }
  562. static void __init samsung_sabi_selftest(struct samsung_laptop *samsung,
  563. unsigned int ifaceP)
  564. {
  565. const struct sabi_config *config = samsung->config;
  566. struct sabi_retval sretval;
  567. printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
  568. printk(KERN_DEBUG "sabi_iface = %p\n", samsung->sabi_iface);
  569. test_backlight(samsung);
  570. test_wireless(samsung);
  571. sabi_get_command(samsung, config->commands.get_brightness, &sretval);
  572. printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
  573. }
  574. static int __init samsung_sabi_init(struct samsung_laptop *samsung)
  575. {
  576. const struct sabi_config *config = NULL;
  577. const struct sabi_commands *commands;
  578. unsigned int ifaceP;
  579. int ret = 0;
  580. int i;
  581. int loca;
  582. samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
  583. if (!samsung->f0000_segment) {
  584. pr_err("Can't map the segment at 0xf0000\n");
  585. ret = -EINVAL;
  586. goto exit;
  587. }
  588. /* Try to find one of the signatures in memory to find the header */
  589. for (i = 0; sabi_configs[i].test_string != 0; ++i) {
  590. samsung->config = &sabi_configs[i];
  591. loca = find_signature(samsung->f0000_segment,
  592. samsung->config->test_string);
  593. if (loca != 0xffff)
  594. break;
  595. }
  596. if (loca == 0xffff) {
  597. pr_err("This computer does not support SABI\n");
  598. ret = -ENODEV;
  599. goto exit;
  600. }
  601. config = samsung->config;
  602. commands = &config->commands;
  603. /* point to the SMI port Number */
  604. loca += 1;
  605. samsung->sabi = (samsung->f0000_segment + loca);
  606. if (debug)
  607. samsung_sabi_infos(samsung, loca);
  608. /* Get a pointer to the SABI Interface */
  609. ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
  610. ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
  611. samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
  612. if (!samsung->sabi_iface) {
  613. pr_err("Can't remap %x\n", ifaceP);
  614. ret = -EINVAL;
  615. goto exit;
  616. }
  617. if (debug)
  618. samsung_sabi_selftest(samsung, ifaceP);
  619. /* Turn on "Linux" mode in the BIOS */
  620. if (commands->set_linux != 0xff) {
  621. int retval = sabi_set_command(samsung,
  622. commands->set_linux, 0x81);
  623. if (retval) {
  624. pr_warn("Linux mode was not set!\n");
  625. ret = -ENODEV;
  626. goto exit;
  627. }
  628. }
  629. /* Check for stepping quirk */
  630. check_for_stepping_quirk(samsung);
  631. exit:
  632. if (ret)
  633. samsung_sabi_exit(samsung);
  634. return ret;
  635. }
  636. static void samsung_platform_exit(struct samsung_laptop *samsung)
  637. {
  638. if (samsung->platform_device) {
  639. platform_device_unregister(samsung->platform_device);
  640. samsung->platform_device = NULL;
  641. }
  642. }
  643. static int __init samsung_platform_init(struct samsung_laptop *samsung)
  644. {
  645. struct platform_device *pdev;
  646. pdev = platform_device_register_simple("samsung", -1, NULL, 0);
  647. if (IS_ERR(pdev))
  648. return PTR_ERR(pdev);
  649. samsung->platform_device = pdev;
  650. platform_set_drvdata(samsung->platform_device, samsung);
  651. return 0;
  652. }
  653. static int __init dmi_check_cb(const struct dmi_system_id *id)
  654. {
  655. pr_info("found laptop model '%s'\n", id->ident);
  656. return 1;
  657. }
  658. static struct dmi_system_id __initdata samsung_dmi_table[] = {
  659. {
  660. .ident = "N128",
  661. .matches = {
  662. DMI_MATCH(DMI_SYS_VENDOR,
  663. "SAMSUNG ELECTRONICS CO., LTD."),
  664. DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
  665. DMI_MATCH(DMI_BOARD_NAME, "N128"),
  666. },
  667. .callback = dmi_check_cb,
  668. },
  669. {
  670. .ident = "N130",
  671. .matches = {
  672. DMI_MATCH(DMI_SYS_VENDOR,
  673. "SAMSUNG ELECTRONICS CO., LTD."),
  674. DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
  675. DMI_MATCH(DMI_BOARD_NAME, "N130"),
  676. },
  677. .callback = dmi_check_cb,
  678. },
  679. {
  680. .ident = "N510",
  681. .matches = {
  682. DMI_MATCH(DMI_SYS_VENDOR,
  683. "SAMSUNG ELECTRONICS CO., LTD."),
  684. DMI_MATCH(DMI_PRODUCT_NAME, "N510"),
  685. DMI_MATCH(DMI_BOARD_NAME, "N510"),
  686. },
  687. .callback = dmi_check_cb,
  688. },
  689. {
  690. .ident = "X125",
  691. .matches = {
  692. DMI_MATCH(DMI_SYS_VENDOR,
  693. "SAMSUNG ELECTRONICS CO., LTD."),
  694. DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
  695. DMI_MATCH(DMI_BOARD_NAME, "X125"),
  696. },
  697. .callback = dmi_check_cb,
  698. },
  699. {
  700. .ident = "X120/X170",
  701. .matches = {
  702. DMI_MATCH(DMI_SYS_VENDOR,
  703. "SAMSUNG ELECTRONICS CO., LTD."),
  704. DMI_MATCH(DMI_PRODUCT_NAME, "X120/X170"),
  705. DMI_MATCH(DMI_BOARD_NAME, "X120/X170"),
  706. },
  707. .callback = dmi_check_cb,
  708. },
  709. {
  710. .ident = "NC10",
  711. .matches = {
  712. DMI_MATCH(DMI_SYS_VENDOR,
  713. "SAMSUNG ELECTRONICS CO., LTD."),
  714. DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
  715. DMI_MATCH(DMI_BOARD_NAME, "NC10"),
  716. },
  717. .callback = dmi_check_cb,
  718. },
  719. {
  720. .ident = "NP-Q45",
  721. .matches = {
  722. DMI_MATCH(DMI_SYS_VENDOR,
  723. "SAMSUNG ELECTRONICS CO., LTD."),
  724. DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"),
  725. DMI_MATCH(DMI_BOARD_NAME, "SQ45S70S"),
  726. },
  727. .callback = dmi_check_cb,
  728. },
  729. {
  730. .ident = "X360",
  731. .matches = {
  732. DMI_MATCH(DMI_SYS_VENDOR,
  733. "SAMSUNG ELECTRONICS CO., LTD."),
  734. DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
  735. DMI_MATCH(DMI_BOARD_NAME, "X360"),
  736. },
  737. .callback = dmi_check_cb,
  738. },
  739. {
  740. .ident = "R410 Plus",
  741. .matches = {
  742. DMI_MATCH(DMI_SYS_VENDOR,
  743. "SAMSUNG ELECTRONICS CO., LTD."),
  744. DMI_MATCH(DMI_PRODUCT_NAME, "R410P"),
  745. DMI_MATCH(DMI_BOARD_NAME, "R460"),
  746. },
  747. .callback = dmi_check_cb,
  748. },
  749. {
  750. .ident = "R518",
  751. .matches = {
  752. DMI_MATCH(DMI_SYS_VENDOR,
  753. "SAMSUNG ELECTRONICS CO., LTD."),
  754. DMI_MATCH(DMI_PRODUCT_NAME, "R518"),
  755. DMI_MATCH(DMI_BOARD_NAME, "R518"),
  756. },
  757. .callback = dmi_check_cb,
  758. },
  759. {
  760. .ident = "R519/R719",
  761. .matches = {
  762. DMI_MATCH(DMI_SYS_VENDOR,
  763. "SAMSUNG ELECTRONICS CO., LTD."),
  764. DMI_MATCH(DMI_PRODUCT_NAME, "R519/R719"),
  765. DMI_MATCH(DMI_BOARD_NAME, "R519/R719"),
  766. },
  767. .callback = dmi_check_cb,
  768. },
  769. {
  770. .ident = "N150/N210/N220",
  771. .matches = {
  772. DMI_MATCH(DMI_SYS_VENDOR,
  773. "SAMSUNG ELECTRONICS CO., LTD."),
  774. DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
  775. DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
  776. },
  777. .callback = dmi_check_cb,
  778. },
  779. {
  780. .ident = "N220",
  781. .matches = {
  782. DMI_MATCH(DMI_SYS_VENDOR,
  783. "SAMSUNG ELECTRONICS CO., LTD."),
  784. DMI_MATCH(DMI_PRODUCT_NAME, "N220"),
  785. DMI_MATCH(DMI_BOARD_NAME, "N220"),
  786. },
  787. .callback = dmi_check_cb,
  788. },
  789. {
  790. .ident = "N150/N210/N220/N230",
  791. .matches = {
  792. DMI_MATCH(DMI_SYS_VENDOR,
  793. "SAMSUNG ELECTRONICS CO., LTD."),
  794. DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220/N230"),
  795. DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220/N230"),
  796. },
  797. .callback = dmi_check_cb,
  798. },
  799. {
  800. .ident = "N150P/N210P/N220P",
  801. .matches = {
  802. DMI_MATCH(DMI_SYS_VENDOR,
  803. "SAMSUNG ELECTRONICS CO., LTD."),
  804. DMI_MATCH(DMI_PRODUCT_NAME, "N150P/N210P/N220P"),
  805. DMI_MATCH(DMI_BOARD_NAME, "N150P/N210P/N220P"),
  806. },
  807. .callback = dmi_check_cb,
  808. },
  809. {
  810. .ident = "R700",
  811. .matches = {
  812. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  813. DMI_MATCH(DMI_PRODUCT_NAME, "SR700"),
  814. DMI_MATCH(DMI_BOARD_NAME, "SR700"),
  815. },
  816. .callback = dmi_check_cb,
  817. },
  818. {
  819. .ident = "R530/R730",
  820. .matches = {
  821. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  822. DMI_MATCH(DMI_PRODUCT_NAME, "R530/R730"),
  823. DMI_MATCH(DMI_BOARD_NAME, "R530/R730"),
  824. },
  825. .callback = dmi_check_cb,
  826. },
  827. {
  828. .ident = "NF110/NF210/NF310",
  829. .matches = {
  830. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  831. DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
  832. DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
  833. },
  834. .callback = dmi_check_cb,
  835. },
  836. {
  837. .ident = "N145P/N250P/N260P",
  838. .matches = {
  839. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  840. DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
  841. DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
  842. },
  843. .callback = dmi_check_cb,
  844. },
  845. {
  846. .ident = "R70/R71",
  847. .matches = {
  848. DMI_MATCH(DMI_SYS_VENDOR,
  849. "SAMSUNG ELECTRONICS CO., LTD."),
  850. DMI_MATCH(DMI_PRODUCT_NAME, "R70/R71"),
  851. DMI_MATCH(DMI_BOARD_NAME, "R70/R71"),
  852. },
  853. .callback = dmi_check_cb,
  854. },
  855. {
  856. .ident = "P460",
  857. .matches = {
  858. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  859. DMI_MATCH(DMI_PRODUCT_NAME, "P460"),
  860. DMI_MATCH(DMI_BOARD_NAME, "P460"),
  861. },
  862. .callback = dmi_check_cb,
  863. },
  864. {
  865. .ident = "R528/R728",
  866. .matches = {
  867. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  868. DMI_MATCH(DMI_PRODUCT_NAME, "R528/R728"),
  869. DMI_MATCH(DMI_BOARD_NAME, "R528/R728"),
  870. },
  871. .callback = dmi_check_cb,
  872. },
  873. {
  874. .ident = "NC210/NC110",
  875. .matches = {
  876. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  877. DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
  878. DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
  879. },
  880. .callback = dmi_check_cb,
  881. },
  882. {
  883. .ident = "X520",
  884. .matches = {
  885. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  886. DMI_MATCH(DMI_PRODUCT_NAME, "X520"),
  887. DMI_MATCH(DMI_BOARD_NAME, "X520"),
  888. },
  889. .callback = dmi_check_cb,
  890. },
  891. { },
  892. };
  893. MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
  894. static struct platform_device *samsung_platform_device;
  895. static int __init samsung_init(void)
  896. {
  897. struct samsung_laptop *samsung;
  898. int ret;
  899. if (!force && !dmi_check_system(samsung_dmi_table))
  900. return -ENODEV;
  901. samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
  902. if (!samsung)
  903. return -ENOMEM;
  904. mutex_init(&samsung->sabi_mutex);
  905. ret = samsung_platform_init(samsung);
  906. if (ret)
  907. goto error_platform;
  908. ret = samsung_sabi_init(samsung);
  909. if (ret)
  910. goto error_sabi;
  911. ret = samsung_sysfs_init(samsung);
  912. if (ret)
  913. goto error_sysfs;
  914. ret = samsung_backlight_init(samsung);
  915. if (ret)
  916. goto error_backlight;
  917. ret = samsung_rfkill_init(samsung);
  918. if (ret)
  919. goto error_rfkill;
  920. samsung_platform_device = samsung->platform_device;
  921. return ret;
  922. error_rfkill:
  923. samsung_backlight_exit(samsung);
  924. error_backlight:
  925. samsung_sysfs_exit(samsung);
  926. error_sysfs:
  927. samsung_sabi_exit(samsung);
  928. error_sabi:
  929. samsung_platform_exit(samsung);
  930. error_platform:
  931. kfree(samsung);
  932. return ret;
  933. }
  934. static void __exit samsung_exit(void)
  935. {
  936. struct samsung_laptop *samsung;
  937. samsung = platform_get_drvdata(samsung_platform_device);
  938. samsung_rfkill_exit(samsung);
  939. samsung_backlight_exit(samsung);
  940. samsung_sysfs_exit(samsung);
  941. samsung_sabi_exit(samsung);
  942. samsung_platform_exit(samsung);
  943. kfree(samsung);
  944. samsung_platform_device = NULL;
  945. }
  946. module_init(samsung_init);
  947. module_exit(samsung_exit);
  948. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
  949. MODULE_DESCRIPTION("Samsung Backlight driver");
  950. MODULE_LICENSE("GPL");