samsung-laptop.c 40 KB

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