samsung-laptop.c 28 KB

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