samsung-laptop.c 28 KB

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