samsung-laptop.c 30 KB

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