samsung-laptop.c 41 KB

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