samsung-laptop.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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/leds.h>
  20. #include <linux/fb.h>
  21. #include <linux/dmi.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/rfkill.h>
  24. #include <linux/acpi.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/ctype.h>
  28. #include <linux/efi.h>
  29. #include <linux/suspend.h>
  30. #include <acpi/video.h>
  31. /*
  32. * This driver is needed because a number of Samsung laptops do not hook
  33. * their control settings through ACPI. So we have to poke around in the
  34. * BIOS to do things like brightness values, and "special" key controls.
  35. */
  36. /*
  37. * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
  38. * be reserved by the BIOS (which really doesn't make much sense), we tell
  39. * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
  40. */
  41. #define MAX_BRIGHT 0x07
  42. #define SABI_IFACE_MAIN 0x00
  43. #define SABI_IFACE_SUB 0x02
  44. #define SABI_IFACE_COMPLETE 0x04
  45. #define SABI_IFACE_DATA 0x05
  46. #define WL_STATUS_WLAN 0x0
  47. #define WL_STATUS_BT 0x2
  48. /* Structure get/set data using sabi */
  49. struct sabi_data {
  50. union {
  51. struct {
  52. u32 d0;
  53. u32 d1;
  54. u16 d2;
  55. u8 d3;
  56. };
  57. u8 data[11];
  58. };
  59. };
  60. struct sabi_header_offsets {
  61. u8 port;
  62. u8 re_mem;
  63. u8 iface_func;
  64. u8 en_mem;
  65. u8 data_offset;
  66. u8 data_segment;
  67. };
  68. struct sabi_commands {
  69. /*
  70. * Brightness is 0 - 8, as described above.
  71. * Value 0 is for the BIOS to use
  72. */
  73. u16 get_brightness;
  74. u16 set_brightness;
  75. /*
  76. * first byte:
  77. * 0x00 - wireless is off
  78. * 0x01 - wireless is on
  79. * second byte:
  80. * 0x02 - 3G is off
  81. * 0x03 - 3G is on
  82. * TODO, verify 3G is correct, that doesn't seem right...
  83. */
  84. u16 get_wireless_button;
  85. u16 set_wireless_button;
  86. /* 0 is off, 1 is on */
  87. u16 get_backlight;
  88. u16 set_backlight;
  89. /*
  90. * 0x80 or 0x00 - no action
  91. * 0x81 - recovery key pressed
  92. */
  93. u16 get_recovery_mode;
  94. u16 set_recovery_mode;
  95. /*
  96. * on seclinux: 0 is low, 1 is high,
  97. * on swsmi: 0 is normal, 1 is silent, 2 is turbo
  98. */
  99. u16 get_performance_level;
  100. u16 set_performance_level;
  101. /* 0x80 is off, 0x81 is on */
  102. u16 get_battery_life_extender;
  103. u16 set_battery_life_extender;
  104. /* 0x80 is off, 0x81 is on */
  105. u16 get_usb_charge;
  106. u16 set_usb_charge;
  107. /* the first byte is for bluetooth and the third one is for wlan */
  108. u16 get_wireless_status;
  109. u16 set_wireless_status;
  110. /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
  111. u16 kbd_backlight;
  112. /*
  113. * Tell the BIOS that Linux is running on this machine.
  114. * 81 is on, 80 is off
  115. */
  116. u16 set_linux;
  117. };
  118. struct sabi_performance_level {
  119. const char *name;
  120. u16 value;
  121. };
  122. struct sabi_config {
  123. int sabi_version;
  124. const char *test_string;
  125. u16 main_function;
  126. const struct sabi_header_offsets header_offsets;
  127. const struct sabi_commands commands;
  128. const struct sabi_performance_level performance_levels[4];
  129. u8 min_brightness;
  130. u8 max_brightness;
  131. };
  132. static const struct sabi_config sabi_configs[] = {
  133. {
  134. /* I don't know if it is really 2, but it it is
  135. * less than 3 anyway */
  136. .sabi_version = 2,
  137. .test_string = "SECLINUX",
  138. .main_function = 0x4c49,
  139. .header_offsets = {
  140. .port = 0x00,
  141. .re_mem = 0x02,
  142. .iface_func = 0x03,
  143. .en_mem = 0x04,
  144. .data_offset = 0x05,
  145. .data_segment = 0x07,
  146. },
  147. .commands = {
  148. .get_brightness = 0x00,
  149. .set_brightness = 0x01,
  150. .get_wireless_button = 0x02,
  151. .set_wireless_button = 0x03,
  152. .get_backlight = 0x04,
  153. .set_backlight = 0x05,
  154. .get_recovery_mode = 0x06,
  155. .set_recovery_mode = 0x07,
  156. .get_performance_level = 0x08,
  157. .set_performance_level = 0x09,
  158. .get_battery_life_extender = 0xFFFF,
  159. .set_battery_life_extender = 0xFFFF,
  160. .get_usb_charge = 0xFFFF,
  161. .set_usb_charge = 0xFFFF,
  162. .get_wireless_status = 0xFFFF,
  163. .set_wireless_status = 0xFFFF,
  164. .kbd_backlight = 0xFFFF,
  165. .set_linux = 0x0a,
  166. },
  167. .performance_levels = {
  168. {
  169. .name = "silent",
  170. .value = 0,
  171. },
  172. {
  173. .name = "normal",
  174. .value = 1,
  175. },
  176. { },
  177. },
  178. .min_brightness = 1,
  179. .max_brightness = 8,
  180. },
  181. {
  182. .sabi_version = 3,
  183. .test_string = "SwSmi@",
  184. .main_function = 0x5843,
  185. .header_offsets = {
  186. .port = 0x00,
  187. .re_mem = 0x04,
  188. .iface_func = 0x02,
  189. .en_mem = 0x03,
  190. .data_offset = 0x05,
  191. .data_segment = 0x07,
  192. },
  193. .commands = {
  194. .get_brightness = 0x10,
  195. .set_brightness = 0x11,
  196. .get_wireless_button = 0x12,
  197. .set_wireless_button = 0x13,
  198. .get_backlight = 0x2d,
  199. .set_backlight = 0x2e,
  200. .get_recovery_mode = 0xff,
  201. .set_recovery_mode = 0xff,
  202. .get_performance_level = 0x31,
  203. .set_performance_level = 0x32,
  204. .get_battery_life_extender = 0x65,
  205. .set_battery_life_extender = 0x66,
  206. .get_usb_charge = 0x67,
  207. .set_usb_charge = 0x68,
  208. .get_wireless_status = 0x69,
  209. .set_wireless_status = 0x6a,
  210. .kbd_backlight = 0x78,
  211. .set_linux = 0xff,
  212. },
  213. .performance_levels = {
  214. {
  215. .name = "normal",
  216. .value = 0,
  217. },
  218. {
  219. .name = "silent",
  220. .value = 1,
  221. },
  222. {
  223. .name = "overclock",
  224. .value = 2,
  225. },
  226. { },
  227. },
  228. .min_brightness = 0,
  229. .max_brightness = 8,
  230. },
  231. { },
  232. };
  233. /*
  234. * samsung-laptop/ - debugfs root directory
  235. * f0000_segment - dump f0000 segment
  236. * command - current command
  237. * data - current data
  238. * d0, d1, d2, d3 - data fields
  239. * call - call SABI using command and data
  240. *
  241. * This allow to call arbitrary sabi commands wihout
  242. * modifying the driver at all.
  243. * For example, setting the keyboard backlight brightness to 5
  244. *
  245. * echo 0x78 > command
  246. * echo 0x0582 > d0
  247. * echo 0 > d1
  248. * echo 0 > d2
  249. * echo 0 > d3
  250. * cat call
  251. */
  252. struct samsung_laptop_debug {
  253. struct dentry *root;
  254. struct sabi_data data;
  255. u16 command;
  256. struct debugfs_blob_wrapper f0000_wrapper;
  257. struct debugfs_blob_wrapper data_wrapper;
  258. struct debugfs_blob_wrapper sdiag_wrapper;
  259. };
  260. struct samsung_laptop;
  261. struct samsung_rfkill {
  262. struct samsung_laptop *samsung;
  263. struct rfkill *rfkill;
  264. enum rfkill_type type;
  265. };
  266. struct samsung_laptop {
  267. const struct sabi_config *config;
  268. void __iomem *sabi;
  269. void __iomem *sabi_iface;
  270. void __iomem *f0000_segment;
  271. struct mutex sabi_mutex;
  272. struct platform_device *platform_device;
  273. struct backlight_device *backlight_device;
  274. struct samsung_rfkill wlan;
  275. struct samsung_rfkill bluetooth;
  276. struct led_classdev kbd_led;
  277. int kbd_led_wk;
  278. struct workqueue_struct *led_workqueue;
  279. struct work_struct kbd_led_work;
  280. struct samsung_laptop_debug debug;
  281. struct samsung_quirks *quirks;
  282. struct notifier_block pm_nb;
  283. bool handle_backlight;
  284. bool has_stepping_quirk;
  285. char sdiag[64];
  286. };
  287. struct samsung_quirks {
  288. bool broken_acpi_video;
  289. bool four_kbd_backlight_levels;
  290. bool enable_kbd_backlight;
  291. };
  292. static struct samsung_quirks samsung_unknown = {};
  293. static struct samsung_quirks samsung_broken_acpi_video = {
  294. .broken_acpi_video = true,
  295. };
  296. static struct samsung_quirks samsung_np740u3e = {
  297. .four_kbd_backlight_levels = true,
  298. .enable_kbd_backlight = true,
  299. };
  300. static bool force;
  301. module_param(force, bool, 0);
  302. MODULE_PARM_DESC(force,
  303. "Disable the DMI check and forces the driver to be loaded");
  304. static bool debug;
  305. module_param(debug, bool, S_IRUGO | S_IWUSR);
  306. MODULE_PARM_DESC(debug, "Debug enabled or not");
  307. static int sabi_command(struct samsung_laptop *samsung, u16 command,
  308. struct sabi_data *in,
  309. struct sabi_data *out)
  310. {
  311. const struct sabi_config *config = samsung->config;
  312. int ret = 0;
  313. u16 port = readw(samsung->sabi + config->header_offsets.port);
  314. u8 complete, iface_data;
  315. mutex_lock(&samsung->sabi_mutex);
  316. if (debug) {
  317. if (in)
  318. pr_info("SABI command:0x%04x "
  319. "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
  320. command, in->d0, in->d1, in->d2, in->d3);
  321. else
  322. pr_info("SABI command:0x%04x", command);
  323. }
  324. /* enable memory to be able to write to it */
  325. outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
  326. /* write out the command */
  327. writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
  328. writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
  329. writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
  330. if (in) {
  331. writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
  332. writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
  333. writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
  334. writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
  335. }
  336. outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
  337. /* write protect memory to make it safe */
  338. outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
  339. /* see if the command actually succeeded */
  340. complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
  341. iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
  342. /* iface_data = 0xFF happens when a command is not known
  343. * so we only add a warning in debug mode since we will
  344. * probably issue some unknown command at startup to find
  345. * out which features are supported */
  346. if (complete != 0xaa || (iface_data == 0xff && debug))
  347. pr_warn("SABI command 0x%04x failed with"
  348. " completion flag 0x%02x and interface data 0x%02x",
  349. command, complete, iface_data);
  350. if (complete != 0xaa || iface_data == 0xff) {
  351. ret = -EINVAL;
  352. goto exit;
  353. }
  354. if (out) {
  355. out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
  356. out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
  357. out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
  358. out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
  359. }
  360. if (debug && out) {
  361. pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
  362. out->d0, out->d1, out->d2, out->d3);
  363. }
  364. exit:
  365. mutex_unlock(&samsung->sabi_mutex);
  366. return ret;
  367. }
  368. /* simple wrappers usable with most commands */
  369. static int sabi_set_commandb(struct samsung_laptop *samsung,
  370. u16 command, u8 data)
  371. {
  372. struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
  373. in.data[0] = data;
  374. return sabi_command(samsung, command, &in, NULL);
  375. }
  376. static int read_brightness(struct samsung_laptop *samsung)
  377. {
  378. const struct sabi_config *config = samsung->config;
  379. const struct sabi_commands *commands = &samsung->config->commands;
  380. struct sabi_data sretval;
  381. int user_brightness = 0;
  382. int retval;
  383. retval = sabi_command(samsung, commands->get_brightness,
  384. NULL, &sretval);
  385. if (retval)
  386. return retval;
  387. user_brightness = sretval.data[0];
  388. if (user_brightness > config->min_brightness)
  389. user_brightness -= config->min_brightness;
  390. else
  391. user_brightness = 0;
  392. return user_brightness;
  393. }
  394. static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
  395. {
  396. const struct sabi_config *config = samsung->config;
  397. const struct sabi_commands *commands = &samsung->config->commands;
  398. u8 user_level = user_brightness + config->min_brightness;
  399. if (samsung->has_stepping_quirk && user_level != 0) {
  400. /*
  401. * short circuit if the specified level is what's already set
  402. * to prevent the screen from flickering needlessly
  403. */
  404. if (user_brightness == read_brightness(samsung))
  405. return;
  406. sabi_set_commandb(samsung, commands->set_brightness, 0);
  407. }
  408. sabi_set_commandb(samsung, commands->set_brightness, user_level);
  409. }
  410. static int get_brightness(struct backlight_device *bd)
  411. {
  412. struct samsung_laptop *samsung = bl_get_data(bd);
  413. return read_brightness(samsung);
  414. }
  415. static void check_for_stepping_quirk(struct samsung_laptop *samsung)
  416. {
  417. int initial_level;
  418. int check_level;
  419. int orig_level = read_brightness(samsung);
  420. /*
  421. * Some laptops exhibit the strange behaviour of stepping toward
  422. * (rather than setting) the brightness except when changing to/from
  423. * brightness level 0. This behaviour is checked for here and worked
  424. * around in set_brightness.
  425. */
  426. if (orig_level == 0)
  427. set_brightness(samsung, 1);
  428. initial_level = read_brightness(samsung);
  429. if (initial_level <= 2)
  430. check_level = initial_level + 2;
  431. else
  432. check_level = initial_level - 2;
  433. samsung->has_stepping_quirk = false;
  434. set_brightness(samsung, check_level);
  435. if (read_brightness(samsung) != check_level) {
  436. samsung->has_stepping_quirk = true;
  437. pr_info("enabled workaround for brightness stepping quirk\n");
  438. }
  439. set_brightness(samsung, orig_level);
  440. }
  441. static int update_status(struct backlight_device *bd)
  442. {
  443. struct samsung_laptop *samsung = bl_get_data(bd);
  444. const struct sabi_commands *commands = &samsung->config->commands;
  445. set_brightness(samsung, bd->props.brightness);
  446. if (bd->props.power == FB_BLANK_UNBLANK)
  447. sabi_set_commandb(samsung, commands->set_backlight, 1);
  448. else
  449. sabi_set_commandb(samsung, commands->set_backlight, 0);
  450. return 0;
  451. }
  452. static const struct backlight_ops backlight_ops = {
  453. .get_brightness = get_brightness,
  454. .update_status = update_status,
  455. };
  456. static int seclinux_rfkill_set(void *data, bool blocked)
  457. {
  458. struct samsung_rfkill *srfkill = data;
  459. struct samsung_laptop *samsung = srfkill->samsung;
  460. const struct sabi_commands *commands = &samsung->config->commands;
  461. return sabi_set_commandb(samsung, commands->set_wireless_button,
  462. !blocked);
  463. }
  464. static struct rfkill_ops seclinux_rfkill_ops = {
  465. .set_block = seclinux_rfkill_set,
  466. };
  467. static int swsmi_wireless_status(struct samsung_laptop *samsung,
  468. struct sabi_data *data)
  469. {
  470. const struct sabi_commands *commands = &samsung->config->commands;
  471. return sabi_command(samsung, commands->get_wireless_status,
  472. NULL, data);
  473. }
  474. static int swsmi_rfkill_set(void *priv, bool blocked)
  475. {
  476. struct samsung_rfkill *srfkill = priv;
  477. struct samsung_laptop *samsung = srfkill->samsung;
  478. const struct sabi_commands *commands = &samsung->config->commands;
  479. struct sabi_data data;
  480. int ret, i;
  481. ret = swsmi_wireless_status(samsung, &data);
  482. if (ret)
  483. return ret;
  484. /* Don't set the state for non-present devices */
  485. for (i = 0; i < 4; i++)
  486. if (data.data[i] == 0x02)
  487. data.data[1] = 0;
  488. if (srfkill->type == RFKILL_TYPE_WLAN)
  489. data.data[WL_STATUS_WLAN] = !blocked;
  490. else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
  491. data.data[WL_STATUS_BT] = !blocked;
  492. return sabi_command(samsung, commands->set_wireless_status,
  493. &data, &data);
  494. }
  495. static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
  496. {
  497. struct samsung_rfkill *srfkill = priv;
  498. struct samsung_laptop *samsung = srfkill->samsung;
  499. struct sabi_data data;
  500. int ret;
  501. ret = swsmi_wireless_status(samsung, &data);
  502. if (ret)
  503. return ;
  504. if (srfkill->type == RFKILL_TYPE_WLAN)
  505. ret = data.data[WL_STATUS_WLAN];
  506. else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
  507. ret = data.data[WL_STATUS_BT];
  508. else
  509. return ;
  510. rfkill_set_sw_state(rfkill, !ret);
  511. }
  512. static struct rfkill_ops swsmi_rfkill_ops = {
  513. .set_block = swsmi_rfkill_set,
  514. .query = swsmi_rfkill_query,
  515. };
  516. static ssize_t get_performance_level(struct device *dev,
  517. struct device_attribute *attr, char *buf)
  518. {
  519. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  520. const struct sabi_config *config = samsung->config;
  521. const struct sabi_commands *commands = &config->commands;
  522. struct sabi_data sretval;
  523. int retval;
  524. int i;
  525. /* Read the state */
  526. retval = sabi_command(samsung, commands->get_performance_level,
  527. NULL, &sretval);
  528. if (retval)
  529. return retval;
  530. /* The logic is backwards, yeah, lots of fun... */
  531. for (i = 0; config->performance_levels[i].name; ++i) {
  532. if (sretval.data[0] == config->performance_levels[i].value)
  533. return sprintf(buf, "%s\n", config->performance_levels[i].name);
  534. }
  535. return sprintf(buf, "%s\n", "unknown");
  536. }
  537. static ssize_t set_performance_level(struct device *dev,
  538. struct device_attribute *attr, const char *buf,
  539. size_t count)
  540. {
  541. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  542. const struct sabi_config *config = samsung->config;
  543. const struct sabi_commands *commands = &config->commands;
  544. int i;
  545. if (count < 1)
  546. return count;
  547. for (i = 0; config->performance_levels[i].name; ++i) {
  548. const struct sabi_performance_level *level =
  549. &config->performance_levels[i];
  550. if (!strncasecmp(level->name, buf, strlen(level->name))) {
  551. sabi_set_commandb(samsung,
  552. commands->set_performance_level,
  553. level->value);
  554. break;
  555. }
  556. }
  557. if (!config->performance_levels[i].name)
  558. return -EINVAL;
  559. return count;
  560. }
  561. static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
  562. get_performance_level, set_performance_level);
  563. static int read_battery_life_extender(struct samsung_laptop *samsung)
  564. {
  565. const struct sabi_commands *commands = &samsung->config->commands;
  566. struct sabi_data data;
  567. int retval;
  568. if (commands->get_battery_life_extender == 0xFFFF)
  569. return -ENODEV;
  570. memset(&data, 0, sizeof(data));
  571. data.data[0] = 0x80;
  572. retval = sabi_command(samsung, commands->get_battery_life_extender,
  573. &data, &data);
  574. if (retval)
  575. return retval;
  576. if (data.data[0] != 0 && data.data[0] != 1)
  577. return -ENODEV;
  578. return data.data[0];
  579. }
  580. static int write_battery_life_extender(struct samsung_laptop *samsung,
  581. int enabled)
  582. {
  583. const struct sabi_commands *commands = &samsung->config->commands;
  584. struct sabi_data data;
  585. memset(&data, 0, sizeof(data));
  586. data.data[0] = 0x80 | enabled;
  587. return sabi_command(samsung, commands->set_battery_life_extender,
  588. &data, NULL);
  589. }
  590. static ssize_t get_battery_life_extender(struct device *dev,
  591. struct device_attribute *attr,
  592. char *buf)
  593. {
  594. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  595. int ret;
  596. ret = read_battery_life_extender(samsung);
  597. if (ret < 0)
  598. return ret;
  599. return sprintf(buf, "%d\n", ret);
  600. }
  601. static ssize_t set_battery_life_extender(struct device *dev,
  602. struct device_attribute *attr,
  603. const char *buf, size_t count)
  604. {
  605. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  606. int ret, value;
  607. if (!count || sscanf(buf, "%i", &value) != 1)
  608. return -EINVAL;
  609. ret = write_battery_life_extender(samsung, !!value);
  610. if (ret < 0)
  611. return ret;
  612. return count;
  613. }
  614. static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
  615. get_battery_life_extender, set_battery_life_extender);
  616. static int read_usb_charge(struct samsung_laptop *samsung)
  617. {
  618. const struct sabi_commands *commands = &samsung->config->commands;
  619. struct sabi_data data;
  620. int retval;
  621. if (commands->get_usb_charge == 0xFFFF)
  622. return -ENODEV;
  623. memset(&data, 0, sizeof(data));
  624. data.data[0] = 0x80;
  625. retval = sabi_command(samsung, commands->get_usb_charge,
  626. &data, &data);
  627. if (retval)
  628. return retval;
  629. if (data.data[0] != 0 && data.data[0] != 1)
  630. return -ENODEV;
  631. return data.data[0];
  632. }
  633. static int write_usb_charge(struct samsung_laptop *samsung,
  634. int enabled)
  635. {
  636. const struct sabi_commands *commands = &samsung->config->commands;
  637. struct sabi_data data;
  638. memset(&data, 0, sizeof(data));
  639. data.data[0] = 0x80 | enabled;
  640. return sabi_command(samsung, commands->set_usb_charge,
  641. &data, NULL);
  642. }
  643. static ssize_t get_usb_charge(struct device *dev,
  644. struct device_attribute *attr,
  645. char *buf)
  646. {
  647. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  648. int ret;
  649. ret = read_usb_charge(samsung);
  650. if (ret < 0)
  651. return ret;
  652. return sprintf(buf, "%d\n", ret);
  653. }
  654. static ssize_t set_usb_charge(struct device *dev,
  655. struct device_attribute *attr,
  656. const char *buf, size_t count)
  657. {
  658. struct samsung_laptop *samsung = dev_get_drvdata(dev);
  659. int ret, value;
  660. if (!count || sscanf(buf, "%i", &value) != 1)
  661. return -EINVAL;
  662. ret = write_usb_charge(samsung, !!value);
  663. if (ret < 0)
  664. return ret;
  665. return count;
  666. }
  667. static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
  668. get_usb_charge, set_usb_charge);
  669. static struct attribute *platform_attributes[] = {
  670. &dev_attr_performance_level.attr,
  671. &dev_attr_battery_life_extender.attr,
  672. &dev_attr_usb_charge.attr,
  673. NULL
  674. };
  675. static int find_signature(void __iomem *memcheck, const char *testStr)
  676. {
  677. int i = 0;
  678. int loca;
  679. for (loca = 0; loca < 0xffff; loca++) {
  680. char temp = readb(memcheck + loca);
  681. if (temp == testStr[i]) {
  682. if (i == strlen(testStr)-1)
  683. break;
  684. ++i;
  685. } else {
  686. i = 0;
  687. }
  688. }
  689. return loca;
  690. }
  691. static void samsung_rfkill_exit(struct samsung_laptop *samsung)
  692. {
  693. if (samsung->wlan.rfkill) {
  694. rfkill_unregister(samsung->wlan.rfkill);
  695. rfkill_destroy(samsung->wlan.rfkill);
  696. samsung->wlan.rfkill = NULL;
  697. }
  698. if (samsung->bluetooth.rfkill) {
  699. rfkill_unregister(samsung->bluetooth.rfkill);
  700. rfkill_destroy(samsung->bluetooth.rfkill);
  701. samsung->bluetooth.rfkill = NULL;
  702. }
  703. }
  704. static int samsung_new_rfkill(struct samsung_laptop *samsung,
  705. struct samsung_rfkill *arfkill,
  706. const char *name, enum rfkill_type type,
  707. const struct rfkill_ops *ops,
  708. int blocked)
  709. {
  710. struct rfkill **rfkill = &arfkill->rfkill;
  711. int ret;
  712. arfkill->type = type;
  713. arfkill->samsung = samsung;
  714. *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
  715. type, ops, arfkill);
  716. if (!*rfkill)
  717. return -EINVAL;
  718. if (blocked != -1)
  719. rfkill_init_sw_state(*rfkill, blocked);
  720. ret = rfkill_register(*rfkill);
  721. if (ret) {
  722. rfkill_destroy(*rfkill);
  723. *rfkill = NULL;
  724. return ret;
  725. }
  726. return 0;
  727. }
  728. static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
  729. {
  730. return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
  731. RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
  732. }
  733. static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
  734. {
  735. struct sabi_data data;
  736. int ret;
  737. ret = swsmi_wireless_status(samsung, &data);
  738. if (ret) {
  739. /* Some swsmi laptops use the old seclinux way to control
  740. * wireless devices */
  741. if (ret == -EINVAL)
  742. ret = samsung_rfkill_init_seclinux(samsung);
  743. return ret;
  744. }
  745. /* 0x02 seems to mean that the device is no present/available */
  746. if (data.data[WL_STATUS_WLAN] != 0x02)
  747. ret = samsung_new_rfkill(samsung, &samsung->wlan,
  748. "samsung-wlan",
  749. RFKILL_TYPE_WLAN,
  750. &swsmi_rfkill_ops,
  751. !data.data[WL_STATUS_WLAN]);
  752. if (ret)
  753. goto exit;
  754. if (data.data[WL_STATUS_BT] != 0x02)
  755. ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
  756. "samsung-bluetooth",
  757. RFKILL_TYPE_BLUETOOTH,
  758. &swsmi_rfkill_ops,
  759. !data.data[WL_STATUS_BT]);
  760. if (ret)
  761. goto exit;
  762. exit:
  763. if (ret)
  764. samsung_rfkill_exit(samsung);
  765. return ret;
  766. }
  767. static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
  768. {
  769. if (samsung->config->sabi_version == 2)
  770. return samsung_rfkill_init_seclinux(samsung);
  771. if (samsung->config->sabi_version == 3)
  772. return samsung_rfkill_init_swsmi(samsung);
  773. return 0;
  774. }
  775. static int kbd_backlight_enable(struct samsung_laptop *samsung)
  776. {
  777. const struct sabi_commands *commands = &samsung->config->commands;
  778. struct sabi_data data;
  779. int retval;
  780. if (commands->kbd_backlight == 0xFFFF)
  781. return -ENODEV;
  782. memset(&data, 0, sizeof(data));
  783. data.d0 = 0xaabb;
  784. retval = sabi_command(samsung, commands->kbd_backlight,
  785. &data, &data);
  786. if (retval)
  787. return retval;
  788. if (data.d0 != 0xccdd)
  789. return -ENODEV;
  790. return 0;
  791. }
  792. static int kbd_backlight_read(struct samsung_laptop *samsung)
  793. {
  794. const struct sabi_commands *commands = &samsung->config->commands;
  795. struct sabi_data data;
  796. int retval;
  797. memset(&data, 0, sizeof(data));
  798. data.data[0] = 0x81;
  799. retval = sabi_command(samsung, commands->kbd_backlight,
  800. &data, &data);
  801. if (retval)
  802. return retval;
  803. return data.data[0];
  804. }
  805. static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
  806. {
  807. const struct sabi_commands *commands = &samsung->config->commands;
  808. struct sabi_data data;
  809. memset(&data, 0, sizeof(data));
  810. data.d0 = 0x82 | ((brightness & 0xFF) << 8);
  811. return sabi_command(samsung, commands->kbd_backlight,
  812. &data, NULL);
  813. }
  814. static void kbd_led_update(struct work_struct *work)
  815. {
  816. struct samsung_laptop *samsung;
  817. samsung = container_of(work, struct samsung_laptop, kbd_led_work);
  818. kbd_backlight_write(samsung, samsung->kbd_led_wk);
  819. }
  820. static void kbd_led_set(struct led_classdev *led_cdev,
  821. enum led_brightness value)
  822. {
  823. struct samsung_laptop *samsung;
  824. samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
  825. if (value > samsung->kbd_led.max_brightness)
  826. value = samsung->kbd_led.max_brightness;
  827. else if (value < 0)
  828. value = 0;
  829. samsung->kbd_led_wk = value;
  830. queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
  831. }
  832. static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
  833. {
  834. struct samsung_laptop *samsung;
  835. samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
  836. return kbd_backlight_read(samsung);
  837. }
  838. static void samsung_leds_exit(struct samsung_laptop *samsung)
  839. {
  840. if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
  841. led_classdev_unregister(&samsung->kbd_led);
  842. if (samsung->led_workqueue)
  843. destroy_workqueue(samsung->led_workqueue);
  844. }
  845. static int __init samsung_leds_init(struct samsung_laptop *samsung)
  846. {
  847. int ret = 0;
  848. samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
  849. if (!samsung->led_workqueue)
  850. return -ENOMEM;
  851. if (kbd_backlight_enable(samsung) >= 0) {
  852. INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
  853. samsung->kbd_led.name = "samsung::kbd_backlight";
  854. samsung->kbd_led.brightness_set = kbd_led_set;
  855. samsung->kbd_led.brightness_get = kbd_led_get;
  856. samsung->kbd_led.max_brightness = 8;
  857. if (samsung->quirks->four_kbd_backlight_levels)
  858. samsung->kbd_led.max_brightness = 4;
  859. ret = led_classdev_register(&samsung->platform_device->dev,
  860. &samsung->kbd_led);
  861. }
  862. if (ret)
  863. samsung_leds_exit(samsung);
  864. return ret;
  865. }
  866. static void samsung_backlight_exit(struct samsung_laptop *samsung)
  867. {
  868. if (samsung->backlight_device) {
  869. backlight_device_unregister(samsung->backlight_device);
  870. samsung->backlight_device = NULL;
  871. }
  872. }
  873. static int __init samsung_backlight_init(struct samsung_laptop *samsung)
  874. {
  875. struct backlight_device *bd;
  876. struct backlight_properties props;
  877. if (!samsung->handle_backlight)
  878. return 0;
  879. memset(&props, 0, sizeof(struct backlight_properties));
  880. props.type = BACKLIGHT_PLATFORM;
  881. props.max_brightness = samsung->config->max_brightness -
  882. samsung->config->min_brightness;
  883. bd = backlight_device_register("samsung",
  884. &samsung->platform_device->dev,
  885. samsung, &backlight_ops,
  886. &props);
  887. if (IS_ERR(bd))
  888. return PTR_ERR(bd);
  889. samsung->backlight_device = bd;
  890. samsung->backlight_device->props.brightness = read_brightness(samsung);
  891. samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
  892. backlight_update_status(samsung->backlight_device);
  893. return 0;
  894. }
  895. static umode_t samsung_sysfs_is_visible(struct kobject *kobj,
  896. struct attribute *attr, int idx)
  897. {
  898. struct device *dev = container_of(kobj, struct device, kobj);
  899. struct platform_device *pdev = to_platform_device(dev);
  900. struct samsung_laptop *samsung = platform_get_drvdata(pdev);
  901. bool ok = true;
  902. if (attr == &dev_attr_performance_level.attr)
  903. ok = !!samsung->config->performance_levels[0].name;
  904. if (attr == &dev_attr_battery_life_extender.attr)
  905. ok = !!(read_battery_life_extender(samsung) >= 0);
  906. if (attr == &dev_attr_usb_charge.attr)
  907. ok = !!(read_usb_charge(samsung) >= 0);
  908. return ok ? attr->mode : 0;
  909. }
  910. static struct attribute_group platform_attribute_group = {
  911. .is_visible = samsung_sysfs_is_visible,
  912. .attrs = platform_attributes
  913. };
  914. static void samsung_sysfs_exit(struct samsung_laptop *samsung)
  915. {
  916. struct platform_device *device = samsung->platform_device;
  917. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  918. }
  919. static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
  920. {
  921. struct platform_device *device = samsung->platform_device;
  922. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  923. }
  924. static int show_call(struct seq_file *m, void *data)
  925. {
  926. struct samsung_laptop *samsung = m->private;
  927. struct sabi_data *sdata = &samsung->debug.data;
  928. int ret;
  929. seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
  930. samsung->debug.command,
  931. sdata->d0, sdata->d1, sdata->d2, sdata->d3);
  932. ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
  933. if (ret) {
  934. seq_printf(m, "SABI command 0x%04x failed\n",
  935. samsung->debug.command);
  936. return ret;
  937. }
  938. seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
  939. sdata->d0, sdata->d1, sdata->d2, sdata->d3);
  940. return 0;
  941. }
  942. static int samsung_debugfs_open(struct inode *inode, struct file *file)
  943. {
  944. return single_open(file, show_call, inode->i_private);
  945. }
  946. static const struct file_operations samsung_laptop_call_io_ops = {
  947. .owner = THIS_MODULE,
  948. .open = samsung_debugfs_open,
  949. .read = seq_read,
  950. .llseek = seq_lseek,
  951. .release = single_release,
  952. };
  953. static void samsung_debugfs_exit(struct samsung_laptop *samsung)
  954. {
  955. debugfs_remove_recursive(samsung->debug.root);
  956. }
  957. static int samsung_debugfs_init(struct samsung_laptop *samsung)
  958. {
  959. struct dentry *dent;
  960. samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
  961. if (!samsung->debug.root) {
  962. pr_err("failed to create debugfs directory");
  963. goto error_debugfs;
  964. }
  965. samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
  966. samsung->debug.f0000_wrapper.size = 0xffff;
  967. samsung->debug.data_wrapper.data = &samsung->debug.data;
  968. samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
  969. samsung->debug.sdiag_wrapper.data = samsung->sdiag;
  970. samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
  971. dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
  972. samsung->debug.root, &samsung->debug.command);
  973. if (!dent)
  974. goto error_debugfs;
  975. dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
  976. &samsung->debug.data.d0);
  977. if (!dent)
  978. goto error_debugfs;
  979. dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
  980. &samsung->debug.data.d1);
  981. if (!dent)
  982. goto error_debugfs;
  983. dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
  984. &samsung->debug.data.d2);
  985. if (!dent)
  986. goto error_debugfs;
  987. dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
  988. &samsung->debug.data.d3);
  989. if (!dent)
  990. goto error_debugfs;
  991. dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
  992. samsung->debug.root,
  993. &samsung->debug.data_wrapper);
  994. if (!dent)
  995. goto error_debugfs;
  996. dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
  997. samsung->debug.root,
  998. &samsung->debug.f0000_wrapper);
  999. if (!dent)
  1000. goto error_debugfs;
  1001. dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
  1002. samsung->debug.root, samsung,
  1003. &samsung_laptop_call_io_ops);
  1004. if (!dent)
  1005. goto error_debugfs;
  1006. dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
  1007. samsung->debug.root,
  1008. &samsung->debug.sdiag_wrapper);
  1009. if (!dent)
  1010. goto error_debugfs;
  1011. return 0;
  1012. error_debugfs:
  1013. samsung_debugfs_exit(samsung);
  1014. return -ENOMEM;
  1015. }
  1016. static void samsung_sabi_exit(struct samsung_laptop *samsung)
  1017. {
  1018. const struct sabi_config *config = samsung->config;
  1019. /* Turn off "Linux" mode in the BIOS */
  1020. if (config && config->commands.set_linux != 0xff)
  1021. sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
  1022. if (samsung->sabi_iface) {
  1023. iounmap(samsung->sabi_iface);
  1024. samsung->sabi_iface = NULL;
  1025. }
  1026. if (samsung->f0000_segment) {
  1027. iounmap(samsung->f0000_segment);
  1028. samsung->f0000_segment = NULL;
  1029. }
  1030. samsung->config = NULL;
  1031. }
  1032. static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
  1033. unsigned int ifaceP)
  1034. {
  1035. const struct sabi_config *config = samsung->config;
  1036. printk(KERN_DEBUG "This computer supports SABI==%x\n",
  1037. loca + 0xf0000 - 6);
  1038. printk(KERN_DEBUG "SABI header:\n");
  1039. printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
  1040. readw(samsung->sabi + config->header_offsets.port));
  1041. printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
  1042. readb(samsung->sabi + config->header_offsets.iface_func));
  1043. printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
  1044. readb(samsung->sabi + config->header_offsets.en_mem));
  1045. printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
  1046. readb(samsung->sabi + config->header_offsets.re_mem));
  1047. printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
  1048. readw(samsung->sabi + config->header_offsets.data_offset));
  1049. printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
  1050. readw(samsung->sabi + config->header_offsets.data_segment));
  1051. printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
  1052. }
  1053. static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
  1054. {
  1055. int loca = find_signature(samsung->f0000_segment, "SDiaG@");
  1056. int i;
  1057. if (loca == 0xffff)
  1058. return ;
  1059. /* Example:
  1060. * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
  1061. *
  1062. * Product name: 90X3A
  1063. * BIOS Version: 07HL
  1064. */
  1065. loca += 1;
  1066. for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
  1067. char temp = readb(samsung->f0000_segment + loca);
  1068. if (isalnum(temp) || temp == '/' || temp == '-')
  1069. samsung->sdiag[i++] = temp;
  1070. else
  1071. break ;
  1072. }
  1073. if (debug && samsung->sdiag[0])
  1074. pr_info("sdiag: %s", samsung->sdiag);
  1075. }
  1076. static int __init samsung_sabi_init(struct samsung_laptop *samsung)
  1077. {
  1078. const struct sabi_config *config = NULL;
  1079. const struct sabi_commands *commands;
  1080. unsigned int ifaceP;
  1081. int ret = 0;
  1082. int i;
  1083. int loca;
  1084. samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
  1085. if (!samsung->f0000_segment) {
  1086. if (debug || force)
  1087. pr_err("Can't map the segment at 0xf0000\n");
  1088. ret = -EINVAL;
  1089. goto exit;
  1090. }
  1091. samsung_sabi_diag(samsung);
  1092. /* Try to find one of the signatures in memory to find the header */
  1093. for (i = 0; sabi_configs[i].test_string != 0; ++i) {
  1094. samsung->config = &sabi_configs[i];
  1095. loca = find_signature(samsung->f0000_segment,
  1096. samsung->config->test_string);
  1097. if (loca != 0xffff)
  1098. break;
  1099. }
  1100. if (loca == 0xffff) {
  1101. if (debug || force)
  1102. pr_err("This computer does not support SABI\n");
  1103. ret = -ENODEV;
  1104. goto exit;
  1105. }
  1106. config = samsung->config;
  1107. commands = &config->commands;
  1108. /* point to the SMI port Number */
  1109. loca += 1;
  1110. samsung->sabi = (samsung->f0000_segment + loca);
  1111. /* Get a pointer to the SABI Interface */
  1112. ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
  1113. ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
  1114. if (debug)
  1115. samsung_sabi_infos(samsung, loca, ifaceP);
  1116. samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
  1117. if (!samsung->sabi_iface) {
  1118. pr_err("Can't remap %x\n", ifaceP);
  1119. ret = -EINVAL;
  1120. goto exit;
  1121. }
  1122. /* Turn on "Linux" mode in the BIOS */
  1123. if (commands->set_linux != 0xff) {
  1124. int retval = sabi_set_commandb(samsung,
  1125. commands->set_linux, 0x81);
  1126. if (retval) {
  1127. pr_warn("Linux mode was not set!\n");
  1128. ret = -ENODEV;
  1129. goto exit;
  1130. }
  1131. }
  1132. /* Check for stepping quirk */
  1133. if (samsung->handle_backlight)
  1134. check_for_stepping_quirk(samsung);
  1135. pr_info("detected SABI interface: %s\n",
  1136. samsung->config->test_string);
  1137. exit:
  1138. if (ret)
  1139. samsung_sabi_exit(samsung);
  1140. return ret;
  1141. }
  1142. static void samsung_platform_exit(struct samsung_laptop *samsung)
  1143. {
  1144. if (samsung->platform_device) {
  1145. platform_device_unregister(samsung->platform_device);
  1146. samsung->platform_device = NULL;
  1147. }
  1148. }
  1149. static int samsung_pm_notification(struct notifier_block *nb,
  1150. unsigned long val, void *ptr)
  1151. {
  1152. struct samsung_laptop *samsung;
  1153. samsung = container_of(nb, struct samsung_laptop, pm_nb);
  1154. if (val == PM_POST_HIBERNATION &&
  1155. samsung->quirks->enable_kbd_backlight)
  1156. kbd_backlight_enable(samsung);
  1157. return 0;
  1158. }
  1159. static int __init samsung_platform_init(struct samsung_laptop *samsung)
  1160. {
  1161. struct platform_device *pdev;
  1162. pdev = platform_device_register_simple("samsung", -1, NULL, 0);
  1163. if (IS_ERR(pdev))
  1164. return PTR_ERR(pdev);
  1165. samsung->platform_device = pdev;
  1166. platform_set_drvdata(samsung->platform_device, samsung);
  1167. return 0;
  1168. }
  1169. static struct samsung_quirks *quirks;
  1170. static int __init samsung_dmi_matched(const struct dmi_system_id *d)
  1171. {
  1172. quirks = d->driver_data;
  1173. return 0;
  1174. }
  1175. static struct dmi_system_id __initdata samsung_dmi_table[] = {
  1176. {
  1177. .matches = {
  1178. DMI_MATCH(DMI_SYS_VENDOR,
  1179. "SAMSUNG ELECTRONICS CO., LTD."),
  1180. DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
  1181. },
  1182. },
  1183. {
  1184. .matches = {
  1185. DMI_MATCH(DMI_SYS_VENDOR,
  1186. "SAMSUNG ELECTRONICS CO., LTD."),
  1187. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
  1188. },
  1189. },
  1190. {
  1191. .matches = {
  1192. DMI_MATCH(DMI_SYS_VENDOR,
  1193. "SAMSUNG ELECTRONICS CO., LTD."),
  1194. DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
  1195. },
  1196. },
  1197. {
  1198. .matches = {
  1199. DMI_MATCH(DMI_SYS_VENDOR,
  1200. "SAMSUNG ELECTRONICS CO., LTD."),
  1201. DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
  1202. },
  1203. },
  1204. /* DMI ids for laptops with bad Chassis Type */
  1205. {
  1206. .ident = "R40/R41",
  1207. .matches = {
  1208. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1209. DMI_MATCH(DMI_PRODUCT_NAME, "R40/R41"),
  1210. DMI_MATCH(DMI_BOARD_NAME, "R40/R41"),
  1211. },
  1212. },
  1213. /* Specific DMI ids for laptop with quirks */
  1214. {
  1215. .callback = samsung_dmi_matched,
  1216. .ident = "N150P",
  1217. .matches = {
  1218. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1219. DMI_MATCH(DMI_PRODUCT_NAME, "N150P"),
  1220. DMI_MATCH(DMI_BOARD_NAME, "N150P"),
  1221. },
  1222. .driver_data = &samsung_broken_acpi_video,
  1223. },
  1224. {
  1225. .callback = samsung_dmi_matched,
  1226. .ident = "N145P/N250P/N260P",
  1227. .matches = {
  1228. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1229. DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
  1230. DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
  1231. },
  1232. .driver_data = &samsung_broken_acpi_video,
  1233. },
  1234. {
  1235. .callback = samsung_dmi_matched,
  1236. .ident = "N150/N210/N220",
  1237. .matches = {
  1238. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1239. DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
  1240. DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
  1241. },
  1242. .driver_data = &samsung_broken_acpi_video,
  1243. },
  1244. {
  1245. .callback = samsung_dmi_matched,
  1246. .ident = "NF110/NF210/NF310",
  1247. .matches = {
  1248. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1249. DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
  1250. DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
  1251. },
  1252. .driver_data = &samsung_broken_acpi_video,
  1253. },
  1254. {
  1255. .callback = samsung_dmi_matched,
  1256. .ident = "X360",
  1257. .matches = {
  1258. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1259. DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
  1260. DMI_MATCH(DMI_BOARD_NAME, "X360"),
  1261. },
  1262. .driver_data = &samsung_broken_acpi_video,
  1263. },
  1264. {
  1265. .callback = samsung_dmi_matched,
  1266. .ident = "N250P",
  1267. .matches = {
  1268. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1269. DMI_MATCH(DMI_PRODUCT_NAME, "N250P"),
  1270. DMI_MATCH(DMI_BOARD_NAME, "N250P"),
  1271. },
  1272. .driver_data = &samsung_broken_acpi_video,
  1273. },
  1274. {
  1275. .callback = samsung_dmi_matched,
  1276. .ident = "NC210",
  1277. .matches = {
  1278. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1279. DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
  1280. DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
  1281. },
  1282. .driver_data = &samsung_broken_acpi_video,
  1283. },
  1284. {
  1285. .callback = samsung_dmi_matched,
  1286. .ident = "730U3E/740U3E",
  1287. .matches = {
  1288. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1289. DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
  1290. },
  1291. .driver_data = &samsung_np740u3e,
  1292. },
  1293. { },
  1294. };
  1295. MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
  1296. static struct platform_device *samsung_platform_device;
  1297. static int __init samsung_init(void)
  1298. {
  1299. struct samsung_laptop *samsung;
  1300. int ret;
  1301. if (efi_enabled(EFI_BOOT))
  1302. return -ENODEV;
  1303. quirks = &samsung_unknown;
  1304. if (!force && !dmi_check_system(samsung_dmi_table))
  1305. return -ENODEV;
  1306. samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
  1307. if (!samsung)
  1308. return -ENOMEM;
  1309. mutex_init(&samsung->sabi_mutex);
  1310. samsung->handle_backlight = true;
  1311. samsung->quirks = quirks;
  1312. #ifdef CONFIG_ACPI
  1313. if (samsung->quirks->broken_acpi_video)
  1314. acpi_video_dmi_promote_vendor();
  1315. /* Don't handle backlight here if the acpi video already handle it */
  1316. if (acpi_video_backlight_support()) {
  1317. samsung->handle_backlight = false;
  1318. } else if (samsung->quirks->broken_acpi_video) {
  1319. pr_info("Disabling ACPI video driver\n");
  1320. acpi_video_unregister();
  1321. }
  1322. #endif
  1323. ret = samsung_platform_init(samsung);
  1324. if (ret)
  1325. goto error_platform;
  1326. ret = samsung_sabi_init(samsung);
  1327. if (ret)
  1328. goto error_sabi;
  1329. #ifdef CONFIG_ACPI
  1330. /* Only log that if we are really on a sabi platform */
  1331. if (acpi_video_backlight_support())
  1332. pr_info("Backlight controlled by ACPI video driver\n");
  1333. #endif
  1334. ret = samsung_sysfs_init(samsung);
  1335. if (ret)
  1336. goto error_sysfs;
  1337. ret = samsung_backlight_init(samsung);
  1338. if (ret)
  1339. goto error_backlight;
  1340. ret = samsung_rfkill_init(samsung);
  1341. if (ret)
  1342. goto error_rfkill;
  1343. ret = samsung_leds_init(samsung);
  1344. if (ret)
  1345. goto error_leds;
  1346. ret = samsung_debugfs_init(samsung);
  1347. if (ret)
  1348. goto error_debugfs;
  1349. samsung->pm_nb.notifier_call = samsung_pm_notification;
  1350. register_pm_notifier(&samsung->pm_nb);
  1351. samsung_platform_device = samsung->platform_device;
  1352. return ret;
  1353. error_debugfs:
  1354. samsung_leds_exit(samsung);
  1355. error_leds:
  1356. samsung_rfkill_exit(samsung);
  1357. error_rfkill:
  1358. samsung_backlight_exit(samsung);
  1359. error_backlight:
  1360. samsung_sysfs_exit(samsung);
  1361. error_sysfs:
  1362. samsung_sabi_exit(samsung);
  1363. error_sabi:
  1364. samsung_platform_exit(samsung);
  1365. error_platform:
  1366. kfree(samsung);
  1367. return ret;
  1368. }
  1369. static void __exit samsung_exit(void)
  1370. {
  1371. struct samsung_laptop *samsung;
  1372. samsung = platform_get_drvdata(samsung_platform_device);
  1373. unregister_pm_notifier(&samsung->pm_nb);
  1374. samsung_debugfs_exit(samsung);
  1375. samsung_leds_exit(samsung);
  1376. samsung_rfkill_exit(samsung);
  1377. samsung_backlight_exit(samsung);
  1378. samsung_sysfs_exit(samsung);
  1379. samsung_sabi_exit(samsung);
  1380. samsung_platform_exit(samsung);
  1381. kfree(samsung);
  1382. samsung_platform_device = NULL;
  1383. }
  1384. module_init(samsung_init);
  1385. module_exit(samsung_exit);
  1386. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
  1387. MODULE_DESCRIPTION("Samsung Backlight driver");
  1388. MODULE_LICENSE("GPL");