samsung-laptop.c 37 KB

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