samsung-laptop.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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 const 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 const 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 samsung_laptop *samsung = dev_get_drvdata(dev);
  978. bool ok = true;
  979. if (attr == &dev_attr_performance_level.attr)
  980. ok = !!samsung->config->performance_levels[0].name;
  981. if (attr == &dev_attr_battery_life_extender.attr)
  982. ok = !!(read_battery_life_extender(samsung) >= 0);
  983. if (attr == &dev_attr_usb_charge.attr)
  984. ok = !!(read_usb_charge(samsung) >= 0);
  985. if (attr == &dev_attr_lid_handling.attr)
  986. ok = !!(read_lid_handling(samsung) >= 0);
  987. return ok ? attr->mode : 0;
  988. }
  989. static const struct attribute_group platform_attribute_group = {
  990. .is_visible = samsung_sysfs_is_visible,
  991. .attrs = platform_attributes
  992. };
  993. static void samsung_sysfs_exit(struct samsung_laptop *samsung)
  994. {
  995. struct platform_device *device = samsung->platform_device;
  996. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  997. }
  998. static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
  999. {
  1000. struct platform_device *device = samsung->platform_device;
  1001. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  1002. }
  1003. static int samsung_laptop_call_show(struct seq_file *m, void *data)
  1004. {
  1005. struct samsung_laptop *samsung = m->private;
  1006. struct sabi_data *sdata = &samsung->debug.data;
  1007. int ret;
  1008. seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
  1009. samsung->debug.command,
  1010. sdata->d0, sdata->d1, sdata->d2, sdata->d3);
  1011. ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
  1012. if (ret) {
  1013. seq_printf(m, "SABI command 0x%04x failed\n",
  1014. samsung->debug.command);
  1015. return ret;
  1016. }
  1017. seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
  1018. sdata->d0, sdata->d1, sdata->d2, sdata->d3);
  1019. return 0;
  1020. }
  1021. DEFINE_SHOW_ATTRIBUTE(samsung_laptop_call);
  1022. static void samsung_debugfs_exit(struct samsung_laptop *samsung)
  1023. {
  1024. debugfs_remove_recursive(samsung->debug.root);
  1025. }
  1026. static int samsung_debugfs_init(struct samsung_laptop *samsung)
  1027. {
  1028. struct dentry *dent;
  1029. samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
  1030. if (!samsung->debug.root) {
  1031. pr_err("failed to create debugfs directory");
  1032. goto error_debugfs;
  1033. }
  1034. samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
  1035. samsung->debug.f0000_wrapper.size = 0xffff;
  1036. samsung->debug.data_wrapper.data = &samsung->debug.data;
  1037. samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
  1038. samsung->debug.sdiag_wrapper.data = samsung->sdiag;
  1039. samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
  1040. dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
  1041. samsung->debug.root, &samsung->debug.command);
  1042. if (!dent)
  1043. goto error_debugfs;
  1044. dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
  1045. &samsung->debug.data.d0);
  1046. if (!dent)
  1047. goto error_debugfs;
  1048. dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
  1049. &samsung->debug.data.d1);
  1050. if (!dent)
  1051. goto error_debugfs;
  1052. dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
  1053. &samsung->debug.data.d2);
  1054. if (!dent)
  1055. goto error_debugfs;
  1056. dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
  1057. &samsung->debug.data.d3);
  1058. if (!dent)
  1059. goto error_debugfs;
  1060. dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
  1061. samsung->debug.root,
  1062. &samsung->debug.data_wrapper);
  1063. if (!dent)
  1064. goto error_debugfs;
  1065. dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
  1066. samsung->debug.root,
  1067. &samsung->debug.f0000_wrapper);
  1068. if (!dent)
  1069. goto error_debugfs;
  1070. dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
  1071. samsung->debug.root, samsung,
  1072. &samsung_laptop_call_fops);
  1073. if (!dent)
  1074. goto error_debugfs;
  1075. dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
  1076. samsung->debug.root,
  1077. &samsung->debug.sdiag_wrapper);
  1078. if (!dent)
  1079. goto error_debugfs;
  1080. return 0;
  1081. error_debugfs:
  1082. samsung_debugfs_exit(samsung);
  1083. return -ENOMEM;
  1084. }
  1085. static void samsung_sabi_exit(struct samsung_laptop *samsung)
  1086. {
  1087. const struct sabi_config *config = samsung->config;
  1088. /* Turn off "Linux" mode in the BIOS */
  1089. if (config && config->commands.set_linux != 0xff)
  1090. sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
  1091. if (samsung->sabi_iface) {
  1092. iounmap(samsung->sabi_iface);
  1093. samsung->sabi_iface = NULL;
  1094. }
  1095. if (samsung->f0000_segment) {
  1096. iounmap(samsung->f0000_segment);
  1097. samsung->f0000_segment = NULL;
  1098. }
  1099. samsung->config = NULL;
  1100. }
  1101. static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
  1102. unsigned int ifaceP)
  1103. {
  1104. const struct sabi_config *config = samsung->config;
  1105. printk(KERN_DEBUG "This computer supports SABI==%x\n",
  1106. loca + 0xf0000 - 6);
  1107. printk(KERN_DEBUG "SABI header:\n");
  1108. printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
  1109. readw(samsung->sabi + config->header_offsets.port));
  1110. printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
  1111. readb(samsung->sabi + config->header_offsets.iface_func));
  1112. printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
  1113. readb(samsung->sabi + config->header_offsets.en_mem));
  1114. printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
  1115. readb(samsung->sabi + config->header_offsets.re_mem));
  1116. printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
  1117. readw(samsung->sabi + config->header_offsets.data_offset));
  1118. printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
  1119. readw(samsung->sabi + config->header_offsets.data_segment));
  1120. printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
  1121. }
  1122. static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
  1123. {
  1124. int loca = find_signature(samsung->f0000_segment, "SDiaG@");
  1125. int i;
  1126. if (loca == 0xffff)
  1127. return ;
  1128. /* Example:
  1129. * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
  1130. *
  1131. * Product name: 90X3A
  1132. * BIOS Version: 07HL
  1133. */
  1134. loca += 1;
  1135. for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
  1136. char temp = readb(samsung->f0000_segment + loca);
  1137. if (isalnum(temp) || temp == '/' || temp == '-')
  1138. samsung->sdiag[i++] = temp;
  1139. else
  1140. break ;
  1141. }
  1142. if (debug && samsung->sdiag[0])
  1143. pr_info("sdiag: %s", samsung->sdiag);
  1144. }
  1145. static int __init samsung_sabi_init(struct samsung_laptop *samsung)
  1146. {
  1147. const struct sabi_config *config = NULL;
  1148. const struct sabi_commands *commands;
  1149. unsigned int ifaceP;
  1150. int loca = 0xffff;
  1151. int ret = 0;
  1152. int i;
  1153. samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
  1154. if (!samsung->f0000_segment) {
  1155. if (debug || force)
  1156. pr_err("Can't map the segment at 0xf0000\n");
  1157. ret = -EINVAL;
  1158. goto exit;
  1159. }
  1160. samsung_sabi_diag(samsung);
  1161. /* Try to find one of the signatures in memory to find the header */
  1162. for (i = 0; sabi_configs[i].test_string != NULL; ++i) {
  1163. samsung->config = &sabi_configs[i];
  1164. loca = find_signature(samsung->f0000_segment,
  1165. samsung->config->test_string);
  1166. if (loca != 0xffff)
  1167. break;
  1168. }
  1169. if (loca == 0xffff) {
  1170. if (debug || force)
  1171. pr_err("This computer does not support SABI\n");
  1172. ret = -ENODEV;
  1173. goto exit;
  1174. }
  1175. config = samsung->config;
  1176. commands = &config->commands;
  1177. /* point to the SMI port Number */
  1178. loca += 1;
  1179. samsung->sabi = (samsung->f0000_segment + loca);
  1180. /* Get a pointer to the SABI Interface */
  1181. ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
  1182. ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
  1183. if (debug)
  1184. samsung_sabi_infos(samsung, loca, ifaceP);
  1185. samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
  1186. if (!samsung->sabi_iface) {
  1187. pr_err("Can't remap %x\n", ifaceP);
  1188. ret = -EINVAL;
  1189. goto exit;
  1190. }
  1191. /* Turn on "Linux" mode in the BIOS */
  1192. if (commands->set_linux != 0xff) {
  1193. int retval = sabi_set_commandb(samsung,
  1194. commands->set_linux, 0x81);
  1195. if (retval) {
  1196. pr_warn("Linux mode was not set!\n");
  1197. ret = -ENODEV;
  1198. goto exit;
  1199. }
  1200. }
  1201. /* Check for stepping quirk */
  1202. if (samsung->handle_backlight)
  1203. check_for_stepping_quirk(samsung);
  1204. pr_info("detected SABI interface: %s\n",
  1205. samsung->config->test_string);
  1206. exit:
  1207. if (ret)
  1208. samsung_sabi_exit(samsung);
  1209. return ret;
  1210. }
  1211. static void samsung_platform_exit(struct samsung_laptop *samsung)
  1212. {
  1213. if (samsung->platform_device) {
  1214. platform_device_unregister(samsung->platform_device);
  1215. samsung->platform_device = NULL;
  1216. }
  1217. }
  1218. static int samsung_pm_notification(struct notifier_block *nb,
  1219. unsigned long val, void *ptr)
  1220. {
  1221. struct samsung_laptop *samsung;
  1222. samsung = container_of(nb, struct samsung_laptop, pm_nb);
  1223. if (val == PM_POST_HIBERNATION &&
  1224. samsung->quirks->enable_kbd_backlight)
  1225. kbd_backlight_enable(samsung);
  1226. if (val == PM_POST_HIBERNATION && samsung->quirks->lid_handling)
  1227. write_lid_handling(samsung, 1);
  1228. return 0;
  1229. }
  1230. static int __init samsung_platform_init(struct samsung_laptop *samsung)
  1231. {
  1232. struct platform_device *pdev;
  1233. pdev = platform_device_register_simple("samsung", -1, NULL, 0);
  1234. if (IS_ERR(pdev))
  1235. return PTR_ERR(pdev);
  1236. samsung->platform_device = pdev;
  1237. platform_set_drvdata(samsung->platform_device, samsung);
  1238. return 0;
  1239. }
  1240. static struct samsung_quirks *quirks;
  1241. static int __init samsung_dmi_matched(const struct dmi_system_id *d)
  1242. {
  1243. quirks = d->driver_data;
  1244. return 0;
  1245. }
  1246. static const struct dmi_system_id samsung_dmi_table[] __initconst = {
  1247. {
  1248. .matches = {
  1249. DMI_MATCH(DMI_SYS_VENDOR,
  1250. "SAMSUNG ELECTRONICS CO., LTD."),
  1251. DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
  1252. },
  1253. },
  1254. {
  1255. .matches = {
  1256. DMI_MATCH(DMI_SYS_VENDOR,
  1257. "SAMSUNG ELECTRONICS CO., LTD."),
  1258. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
  1259. },
  1260. },
  1261. {
  1262. .matches = {
  1263. DMI_MATCH(DMI_SYS_VENDOR,
  1264. "SAMSUNG ELECTRONICS CO., LTD."),
  1265. DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
  1266. },
  1267. },
  1268. {
  1269. .matches = {
  1270. DMI_MATCH(DMI_SYS_VENDOR,
  1271. "SAMSUNG ELECTRONICS CO., LTD."),
  1272. DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
  1273. },
  1274. },
  1275. /* DMI ids for laptops with bad Chassis Type */
  1276. {
  1277. .ident = "R40/R41",
  1278. .matches = {
  1279. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1280. DMI_MATCH(DMI_PRODUCT_NAME, "R40/R41"),
  1281. DMI_MATCH(DMI_BOARD_NAME, "R40/R41"),
  1282. },
  1283. },
  1284. /* Specific DMI ids for laptop with quirks */
  1285. {
  1286. .callback = samsung_dmi_matched,
  1287. .ident = "N150P",
  1288. .matches = {
  1289. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1290. DMI_MATCH(DMI_PRODUCT_NAME, "N150P"),
  1291. DMI_MATCH(DMI_BOARD_NAME, "N150P"),
  1292. },
  1293. .driver_data = &samsung_use_native_backlight,
  1294. },
  1295. {
  1296. .callback = samsung_dmi_matched,
  1297. .ident = "N145P/N250P/N260P",
  1298. .matches = {
  1299. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1300. DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
  1301. DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
  1302. },
  1303. .driver_data = &samsung_use_native_backlight,
  1304. },
  1305. {
  1306. .callback = samsung_dmi_matched,
  1307. .ident = "N150/N210/N220",
  1308. .matches = {
  1309. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1310. DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
  1311. DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
  1312. },
  1313. .driver_data = &samsung_broken_acpi_video,
  1314. },
  1315. {
  1316. .callback = samsung_dmi_matched,
  1317. .ident = "NF110/NF210/NF310",
  1318. .matches = {
  1319. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1320. DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
  1321. DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
  1322. },
  1323. .driver_data = &samsung_broken_acpi_video,
  1324. },
  1325. {
  1326. .callback = samsung_dmi_matched,
  1327. .ident = "X360",
  1328. .matches = {
  1329. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1330. DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
  1331. DMI_MATCH(DMI_BOARD_NAME, "X360"),
  1332. },
  1333. .driver_data = &samsung_broken_acpi_video,
  1334. },
  1335. {
  1336. .callback = samsung_dmi_matched,
  1337. .ident = "N250P",
  1338. .matches = {
  1339. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1340. DMI_MATCH(DMI_PRODUCT_NAME, "N250P"),
  1341. DMI_MATCH(DMI_BOARD_NAME, "N250P"),
  1342. },
  1343. .driver_data = &samsung_use_native_backlight,
  1344. },
  1345. {
  1346. .callback = samsung_dmi_matched,
  1347. .ident = "NC210",
  1348. .matches = {
  1349. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1350. DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
  1351. DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
  1352. },
  1353. .driver_data = &samsung_broken_acpi_video,
  1354. },
  1355. {
  1356. .callback = samsung_dmi_matched,
  1357. .ident = "730U3E/740U3E",
  1358. .matches = {
  1359. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1360. DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
  1361. },
  1362. .driver_data = &samsung_np740u3e,
  1363. },
  1364. {
  1365. .callback = samsung_dmi_matched,
  1366. .ident = "300V3Z/300V4Z/300V5Z",
  1367. .matches = {
  1368. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  1369. DMI_MATCH(DMI_PRODUCT_NAME, "300V3Z/300V4Z/300V5Z"),
  1370. },
  1371. .driver_data = &samsung_lid_handling,
  1372. },
  1373. { },
  1374. };
  1375. MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
  1376. static struct platform_device *samsung_platform_device;
  1377. static int __init samsung_init(void)
  1378. {
  1379. struct samsung_laptop *samsung;
  1380. int ret;
  1381. if (efi_enabled(EFI_BOOT))
  1382. return -ENODEV;
  1383. quirks = &samsung_unknown;
  1384. if (!force && !dmi_check_system(samsung_dmi_table))
  1385. return -ENODEV;
  1386. samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
  1387. if (!samsung)
  1388. return -ENOMEM;
  1389. mutex_init(&samsung->sabi_mutex);
  1390. samsung->handle_backlight = true;
  1391. samsung->quirks = quirks;
  1392. #ifdef CONFIG_ACPI
  1393. if (samsung->quirks->broken_acpi_video)
  1394. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  1395. if (samsung->quirks->use_native_backlight)
  1396. acpi_video_set_dmi_backlight_type(acpi_backlight_native);
  1397. if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
  1398. samsung->handle_backlight = false;
  1399. #endif
  1400. ret = samsung_platform_init(samsung);
  1401. if (ret)
  1402. goto error_platform;
  1403. ret = samsung_sabi_init(samsung);
  1404. if (ret)
  1405. goto error_sabi;
  1406. ret = samsung_sysfs_init(samsung);
  1407. if (ret)
  1408. goto error_sysfs;
  1409. ret = samsung_backlight_init(samsung);
  1410. if (ret)
  1411. goto error_backlight;
  1412. ret = samsung_rfkill_init(samsung);
  1413. if (ret)
  1414. goto error_rfkill;
  1415. ret = samsung_leds_init(samsung);
  1416. if (ret)
  1417. goto error_leds;
  1418. ret = samsung_lid_handling_init(samsung);
  1419. if (ret)
  1420. goto error_lid_handling;
  1421. ret = samsung_debugfs_init(samsung);
  1422. if (ret)
  1423. goto error_debugfs;
  1424. samsung->pm_nb.notifier_call = samsung_pm_notification;
  1425. register_pm_notifier(&samsung->pm_nb);
  1426. samsung_platform_device = samsung->platform_device;
  1427. return ret;
  1428. error_debugfs:
  1429. samsung_lid_handling_exit(samsung);
  1430. error_lid_handling:
  1431. samsung_leds_exit(samsung);
  1432. error_leds:
  1433. samsung_rfkill_exit(samsung);
  1434. error_rfkill:
  1435. samsung_backlight_exit(samsung);
  1436. error_backlight:
  1437. samsung_sysfs_exit(samsung);
  1438. error_sysfs:
  1439. samsung_sabi_exit(samsung);
  1440. error_sabi:
  1441. samsung_platform_exit(samsung);
  1442. error_platform:
  1443. kfree(samsung);
  1444. return ret;
  1445. }
  1446. static void __exit samsung_exit(void)
  1447. {
  1448. struct samsung_laptop *samsung;
  1449. samsung = platform_get_drvdata(samsung_platform_device);
  1450. unregister_pm_notifier(&samsung->pm_nb);
  1451. samsung_debugfs_exit(samsung);
  1452. samsung_lid_handling_exit(samsung);
  1453. samsung_leds_exit(samsung);
  1454. samsung_rfkill_exit(samsung);
  1455. samsung_backlight_exit(samsung);
  1456. samsung_sysfs_exit(samsung);
  1457. samsung_sabi_exit(samsung);
  1458. samsung_platform_exit(samsung);
  1459. kfree(samsung);
  1460. samsung_platform_device = NULL;
  1461. }
  1462. module_init(samsung_init);
  1463. module_exit(samsung_exit);
  1464. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
  1465. MODULE_DESCRIPTION("Samsung Backlight driver");
  1466. MODULE_LICENSE("GPL");