samsung-laptop.c 41 KB

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