samsung-laptop.c 42 KB

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