samsung-laptop.c 37 KB

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