samsung-laptop.c 43 KB

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