samsung-laptop.c 39 KB

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