samsung-laptop.c 36 KB

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