samsung-laptop.c 37 KB

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