dell-laptop.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. /*
  2. * Driver for Dell laptop extras
  3. *
  4. * Copyright (c) Red Hat <mjg@redhat.com>
  5. * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  6. * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
  7. *
  8. * Based on documentation in the libsmbios package:
  9. * Copyright (C) 2005-2014 Dell Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/backlight.h>
  21. #include <linux/err.h>
  22. #include <linux/dmi.h>
  23. #include <linux/io.h>
  24. #include <linux/rfkill.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/acpi.h>
  27. #include <linux/mm.h>
  28. #include <linux/i8042.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <acpi/video.h>
  32. #include "dell-rbtn.h"
  33. #include "dell-smbios.h"
  34. #define BRIGHTNESS_TOKEN 0x7d
  35. #define KBD_LED_OFF_TOKEN 0x01E1
  36. #define KBD_LED_ON_TOKEN 0x01E2
  37. #define KBD_LED_AUTO_TOKEN 0x01E3
  38. #define KBD_LED_AUTO_25_TOKEN 0x02EA
  39. #define KBD_LED_AUTO_50_TOKEN 0x02EB
  40. #define KBD_LED_AUTO_75_TOKEN 0x02EC
  41. #define KBD_LED_AUTO_100_TOKEN 0x02F6
  42. struct quirk_entry {
  43. u8 touchpad_led;
  44. int needs_kbd_timeouts;
  45. /*
  46. * Ordered list of timeouts expressed in seconds.
  47. * The list must end with -1
  48. */
  49. int kbd_timeouts[];
  50. };
  51. static struct quirk_entry *quirks;
  52. static struct quirk_entry quirk_dell_vostro_v130 = {
  53. .touchpad_led = 1,
  54. };
  55. static int __init dmi_matched(const struct dmi_system_id *dmi)
  56. {
  57. quirks = dmi->driver_data;
  58. return 1;
  59. }
  60. /*
  61. * These values come from Windows utility provided by Dell. If any other value
  62. * is used then BIOS silently set timeout to 0 without any error message.
  63. */
  64. static struct quirk_entry quirk_dell_xps13_9333 = {
  65. .needs_kbd_timeouts = 1,
  66. .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
  67. };
  68. static struct platform_driver platform_driver = {
  69. .driver = {
  70. .name = "dell-laptop",
  71. }
  72. };
  73. static struct platform_device *platform_device;
  74. static struct backlight_device *dell_backlight_device;
  75. static struct rfkill *wifi_rfkill;
  76. static struct rfkill *bluetooth_rfkill;
  77. static struct rfkill *wwan_rfkill;
  78. static bool force_rfkill;
  79. module_param(force_rfkill, bool, 0444);
  80. MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
  81. static const struct dmi_system_id dell_device_table[] __initconst = {
  82. {
  83. .ident = "Dell laptop",
  84. .matches = {
  85. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  86. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  87. },
  88. },
  89. {
  90. .matches = {
  91. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  92. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
  93. },
  94. },
  95. {
  96. .ident = "Dell Computer Corporation",
  97. .matches = {
  98. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  99. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  100. },
  101. },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(dmi, dell_device_table);
  105. static const struct dmi_system_id dell_quirks[] __initconst = {
  106. {
  107. .callback = dmi_matched,
  108. .ident = "Dell Vostro V130",
  109. .matches = {
  110. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  111. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
  112. },
  113. .driver_data = &quirk_dell_vostro_v130,
  114. },
  115. {
  116. .callback = dmi_matched,
  117. .ident = "Dell Vostro V131",
  118. .matches = {
  119. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  120. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
  121. },
  122. .driver_data = &quirk_dell_vostro_v130,
  123. },
  124. {
  125. .callback = dmi_matched,
  126. .ident = "Dell Vostro 3350",
  127. .matches = {
  128. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  129. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
  130. },
  131. .driver_data = &quirk_dell_vostro_v130,
  132. },
  133. {
  134. .callback = dmi_matched,
  135. .ident = "Dell Vostro 3555",
  136. .matches = {
  137. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  138. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
  139. },
  140. .driver_data = &quirk_dell_vostro_v130,
  141. },
  142. {
  143. .callback = dmi_matched,
  144. .ident = "Dell Inspiron N311z",
  145. .matches = {
  146. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  147. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
  148. },
  149. .driver_data = &quirk_dell_vostro_v130,
  150. },
  151. {
  152. .callback = dmi_matched,
  153. .ident = "Dell Inspiron M5110",
  154. .matches = {
  155. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  156. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
  157. },
  158. .driver_data = &quirk_dell_vostro_v130,
  159. },
  160. {
  161. .callback = dmi_matched,
  162. .ident = "Dell Vostro 3360",
  163. .matches = {
  164. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  165. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
  166. },
  167. .driver_data = &quirk_dell_vostro_v130,
  168. },
  169. {
  170. .callback = dmi_matched,
  171. .ident = "Dell Vostro 3460",
  172. .matches = {
  173. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  174. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
  175. },
  176. .driver_data = &quirk_dell_vostro_v130,
  177. },
  178. {
  179. .callback = dmi_matched,
  180. .ident = "Dell Vostro 3560",
  181. .matches = {
  182. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  183. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
  184. },
  185. .driver_data = &quirk_dell_vostro_v130,
  186. },
  187. {
  188. .callback = dmi_matched,
  189. .ident = "Dell Vostro 3450",
  190. .matches = {
  191. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  192. DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
  193. },
  194. .driver_data = &quirk_dell_vostro_v130,
  195. },
  196. {
  197. .callback = dmi_matched,
  198. .ident = "Dell Inspiron 5420",
  199. .matches = {
  200. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  201. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
  202. },
  203. .driver_data = &quirk_dell_vostro_v130,
  204. },
  205. {
  206. .callback = dmi_matched,
  207. .ident = "Dell Inspiron 5520",
  208. .matches = {
  209. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  210. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
  211. },
  212. .driver_data = &quirk_dell_vostro_v130,
  213. },
  214. {
  215. .callback = dmi_matched,
  216. .ident = "Dell Inspiron 5720",
  217. .matches = {
  218. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  219. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
  220. },
  221. .driver_data = &quirk_dell_vostro_v130,
  222. },
  223. {
  224. .callback = dmi_matched,
  225. .ident = "Dell Inspiron 7420",
  226. .matches = {
  227. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  228. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
  229. },
  230. .driver_data = &quirk_dell_vostro_v130,
  231. },
  232. {
  233. .callback = dmi_matched,
  234. .ident = "Dell Inspiron 7520",
  235. .matches = {
  236. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  237. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
  238. },
  239. .driver_data = &quirk_dell_vostro_v130,
  240. },
  241. {
  242. .callback = dmi_matched,
  243. .ident = "Dell Inspiron 7720",
  244. .matches = {
  245. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  246. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
  247. },
  248. .driver_data = &quirk_dell_vostro_v130,
  249. },
  250. {
  251. .callback = dmi_matched,
  252. .ident = "Dell XPS13 9333",
  253. .matches = {
  254. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  255. DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
  256. },
  257. .driver_data = &quirk_dell_xps13_9333,
  258. },
  259. { }
  260. };
  261. /*
  262. * Derived from information in smbios-wireless-ctl:
  263. *
  264. * cbSelect 17, Value 11
  265. *
  266. * Return Wireless Info
  267. * cbArg1, byte0 = 0x00
  268. *
  269. * cbRes1 Standard return codes (0, -1, -2)
  270. * cbRes2 Info bit flags:
  271. *
  272. * 0 Hardware switch supported (1)
  273. * 1 WiFi locator supported (1)
  274. * 2 WLAN supported (1)
  275. * 3 Bluetooth (BT) supported (1)
  276. * 4 WWAN supported (1)
  277. * 5 Wireless KBD supported (1)
  278. * 6 Uw b supported (1)
  279. * 7 WiGig supported (1)
  280. * 8 WLAN installed (1)
  281. * 9 BT installed (1)
  282. * 10 WWAN installed (1)
  283. * 11 Uw b installed (1)
  284. * 12 WiGig installed (1)
  285. * 13-15 Reserved (0)
  286. * 16 Hardware (HW) switch is On (1)
  287. * 17 WLAN disabled (1)
  288. * 18 BT disabled (1)
  289. * 19 WWAN disabled (1)
  290. * 20 Uw b disabled (1)
  291. * 21 WiGig disabled (1)
  292. * 20-31 Reserved (0)
  293. *
  294. * cbRes3 NVRAM size in bytes
  295. * cbRes4, byte 0 NVRAM format version number
  296. *
  297. *
  298. * Set QuickSet Radio Disable Flag
  299. * cbArg1, byte0 = 0x01
  300. * cbArg1, byte1
  301. * Radio ID value:
  302. * 0 Radio Status
  303. * 1 WLAN ID
  304. * 2 BT ID
  305. * 3 WWAN ID
  306. * 4 UWB ID
  307. * 5 WIGIG ID
  308. * cbArg1, byte2 Flag bits:
  309. * 0 QuickSet disables radio (1)
  310. * 1-7 Reserved (0)
  311. *
  312. * cbRes1 Standard return codes (0, -1, -2)
  313. * cbRes2 QuickSet (QS) radio disable bit map:
  314. * 0 QS disables WLAN
  315. * 1 QS disables BT
  316. * 2 QS disables WWAN
  317. * 3 QS disables UWB
  318. * 4 QS disables WIGIG
  319. * 5-31 Reserved (0)
  320. *
  321. * Wireless Switch Configuration
  322. * cbArg1, byte0 = 0x02
  323. *
  324. * cbArg1, byte1
  325. * Subcommand:
  326. * 0 Get config
  327. * 1 Set config
  328. * 2 Set WiFi locator enable/disable
  329. * cbArg1,byte2
  330. * Switch settings (if byte 1==1):
  331. * 0 WLAN sw itch control (1)
  332. * 1 BT sw itch control (1)
  333. * 2 WWAN sw itch control (1)
  334. * 3 UWB sw itch control (1)
  335. * 4 WiGig sw itch control (1)
  336. * 5-7 Reserved (0)
  337. * cbArg1, byte2 Enable bits (if byte 1==2):
  338. * 0 Enable WiFi locator (1)
  339. *
  340. * cbRes1 Standard return codes (0, -1, -2)
  341. * cbRes2 QuickSet radio disable bit map:
  342. * 0 WLAN controlled by sw itch (1)
  343. * 1 BT controlled by sw itch (1)
  344. * 2 WWAN controlled by sw itch (1)
  345. * 3 UWB controlled by sw itch (1)
  346. * 4 WiGig controlled by sw itch (1)
  347. * 5-6 Reserved (0)
  348. * 7 Wireless sw itch config locked (1)
  349. * 8 WiFi locator enabled (1)
  350. * 9-14 Reserved (0)
  351. * 15 WiFi locator setting locked (1)
  352. * 16-31 Reserved (0)
  353. *
  354. * Read Local Config Data (LCD)
  355. * cbArg1, byte0 = 0x10
  356. * cbArg1, byte1 NVRAM index low byte
  357. * cbArg1, byte2 NVRAM index high byte
  358. * cbRes1 Standard return codes (0, -1, -2)
  359. * cbRes2 4 bytes read from LCD[index]
  360. * cbRes3 4 bytes read from LCD[index+4]
  361. * cbRes4 4 bytes read from LCD[index+8]
  362. *
  363. * Write Local Config Data (LCD)
  364. * cbArg1, byte0 = 0x11
  365. * cbArg1, byte1 NVRAM index low byte
  366. * cbArg1, byte2 NVRAM index high byte
  367. * cbArg2 4 bytes to w rite at LCD[index]
  368. * cbArg3 4 bytes to w rite at LCD[index+4]
  369. * cbArg4 4 bytes to w rite at LCD[index+8]
  370. * cbRes1 Standard return codes (0, -1, -2)
  371. *
  372. * Populate Local Config Data from NVRAM
  373. * cbArg1, byte0 = 0x12
  374. * cbRes1 Standard return codes (0, -1, -2)
  375. *
  376. * Commit Local Config Data to NVRAM
  377. * cbArg1, byte0 = 0x13
  378. * cbRes1 Standard return codes (0, -1, -2)
  379. */
  380. static int dell_rfkill_set(void *data, bool blocked)
  381. {
  382. struct calling_interface_buffer *buffer;
  383. int disable = blocked ? 1 : 0;
  384. unsigned long radio = (unsigned long)data;
  385. int hwswitch_bit = (unsigned long)data - 1;
  386. int hwswitch;
  387. int status;
  388. int ret;
  389. buffer = dell_smbios_get_buffer();
  390. dell_smbios_send_request(17, 11);
  391. ret = buffer->output[0];
  392. status = buffer->output[1];
  393. if (ret != 0)
  394. goto out;
  395. dell_smbios_clear_buffer();
  396. buffer->input[0] = 0x2;
  397. dell_smbios_send_request(17, 11);
  398. ret = buffer->output[0];
  399. hwswitch = buffer->output[1];
  400. /* If the hardware switch controls this radio, and the hardware
  401. switch is disabled, always disable the radio */
  402. if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
  403. (status & BIT(0)) && !(status & BIT(16)))
  404. disable = 1;
  405. dell_smbios_clear_buffer();
  406. buffer->input[0] = (1 | (radio<<8) | (disable << 16));
  407. dell_smbios_send_request(17, 11);
  408. ret = buffer->output[0];
  409. out:
  410. dell_smbios_release_buffer();
  411. return dell_smbios_error(ret);
  412. }
  413. /* Must be called with the buffer held */
  414. static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
  415. int status,
  416. struct calling_interface_buffer *buffer)
  417. {
  418. if (status & BIT(0)) {
  419. /* Has hw-switch, sync sw_state to BIOS */
  420. int block = rfkill_blocked(rfkill);
  421. dell_smbios_clear_buffer();
  422. buffer->input[0] = (1 | (radio << 8) | (block << 16));
  423. dell_smbios_send_request(17, 11);
  424. } else {
  425. /* No hw-switch, sync BIOS state to sw_state */
  426. rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
  427. }
  428. }
  429. static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
  430. int status, int hwswitch)
  431. {
  432. if (hwswitch & (BIT(radio - 1)))
  433. rfkill_set_hw_state(rfkill, !(status & BIT(16)));
  434. }
  435. static void dell_rfkill_query(struct rfkill *rfkill, void *data)
  436. {
  437. struct calling_interface_buffer *buffer;
  438. int radio = ((unsigned long)data & 0xF);
  439. int hwswitch;
  440. int status;
  441. int ret;
  442. buffer = dell_smbios_get_buffer();
  443. dell_smbios_send_request(17, 11);
  444. ret = buffer->output[0];
  445. status = buffer->output[1];
  446. if (ret != 0 || !(status & BIT(0))) {
  447. dell_smbios_release_buffer();
  448. return;
  449. }
  450. dell_smbios_clear_buffer();
  451. buffer->input[0] = 0x2;
  452. dell_smbios_send_request(17, 11);
  453. ret = buffer->output[0];
  454. hwswitch = buffer->output[1];
  455. dell_smbios_release_buffer();
  456. if (ret != 0)
  457. return;
  458. dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
  459. }
  460. static const struct rfkill_ops dell_rfkill_ops = {
  461. .set_block = dell_rfkill_set,
  462. .query = dell_rfkill_query,
  463. };
  464. static struct dentry *dell_laptop_dir;
  465. static int dell_debugfs_show(struct seq_file *s, void *data)
  466. {
  467. struct calling_interface_buffer *buffer;
  468. int hwswitch_state;
  469. int hwswitch_ret;
  470. int status;
  471. int ret;
  472. buffer = dell_smbios_get_buffer();
  473. dell_smbios_send_request(17, 11);
  474. ret = buffer->output[0];
  475. status = buffer->output[1];
  476. dell_smbios_clear_buffer();
  477. buffer->input[0] = 0x2;
  478. dell_smbios_send_request(17, 11);
  479. hwswitch_ret = buffer->output[0];
  480. hwswitch_state = buffer->output[1];
  481. dell_smbios_release_buffer();
  482. seq_printf(s, "return:\t%d\n", ret);
  483. seq_printf(s, "status:\t0x%X\n", status);
  484. seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
  485. status & BIT(0));
  486. seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
  487. (status & BIT(1)) >> 1);
  488. seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
  489. (status & BIT(2)) >> 2);
  490. seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
  491. (status & BIT(3)) >> 3);
  492. seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
  493. (status & BIT(4)) >> 4);
  494. seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
  495. (status & BIT(5)) >> 5);
  496. seq_printf(s, "Bit 6 : UWB supported: %lu\n",
  497. (status & BIT(6)) >> 6);
  498. seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
  499. (status & BIT(7)) >> 7);
  500. seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
  501. (status & BIT(8)) >> 8);
  502. seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
  503. (status & BIT(9)) >> 9);
  504. seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
  505. (status & BIT(10)) >> 10);
  506. seq_printf(s, "Bit 11: UWB installed: %lu\n",
  507. (status & BIT(11)) >> 11);
  508. seq_printf(s, "Bit 12: WiGig installed: %lu\n",
  509. (status & BIT(12)) >> 12);
  510. seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
  511. (status & BIT(16)) >> 16);
  512. seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
  513. (status & BIT(17)) >> 17);
  514. seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
  515. (status & BIT(18)) >> 18);
  516. seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
  517. (status & BIT(19)) >> 19);
  518. seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
  519. (status & BIT(20)) >> 20);
  520. seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
  521. (status & BIT(21)) >> 21);
  522. seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
  523. seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
  524. seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
  525. hwswitch_state & BIT(0));
  526. seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
  527. (hwswitch_state & BIT(1)) >> 1);
  528. seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
  529. (hwswitch_state & BIT(2)) >> 2);
  530. seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
  531. (hwswitch_state & BIT(3)) >> 3);
  532. seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
  533. (hwswitch_state & BIT(4)) >> 4);
  534. seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
  535. (hwswitch_state & BIT(7)) >> 7);
  536. seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
  537. (hwswitch_state & BIT(8)) >> 8);
  538. seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
  539. (hwswitch_state & BIT(15)) >> 15);
  540. return 0;
  541. }
  542. static int dell_debugfs_open(struct inode *inode, struct file *file)
  543. {
  544. return single_open(file, dell_debugfs_show, inode->i_private);
  545. }
  546. static const struct file_operations dell_debugfs_fops = {
  547. .owner = THIS_MODULE,
  548. .open = dell_debugfs_open,
  549. .read = seq_read,
  550. .llseek = seq_lseek,
  551. .release = single_release,
  552. };
  553. static void dell_update_rfkill(struct work_struct *ignored)
  554. {
  555. struct calling_interface_buffer *buffer;
  556. int hwswitch = 0;
  557. int status;
  558. int ret;
  559. buffer = dell_smbios_get_buffer();
  560. dell_smbios_send_request(17, 11);
  561. ret = buffer->output[0];
  562. status = buffer->output[1];
  563. if (ret != 0)
  564. goto out;
  565. dell_smbios_clear_buffer();
  566. buffer->input[0] = 0x2;
  567. dell_smbios_send_request(17, 11);
  568. ret = buffer->output[0];
  569. if (ret == 0 && (status & BIT(0)))
  570. hwswitch = buffer->output[1];
  571. if (wifi_rfkill) {
  572. dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
  573. dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
  574. }
  575. if (bluetooth_rfkill) {
  576. dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
  577. hwswitch);
  578. dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
  579. buffer);
  580. }
  581. if (wwan_rfkill) {
  582. dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
  583. dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
  584. }
  585. out:
  586. dell_smbios_release_buffer();
  587. }
  588. static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
  589. static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
  590. struct serio *port)
  591. {
  592. static bool extended;
  593. if (str & I8042_STR_AUXDATA)
  594. return false;
  595. if (unlikely(data == 0xe0)) {
  596. extended = true;
  597. return false;
  598. } else if (unlikely(extended)) {
  599. switch (data) {
  600. case 0x8:
  601. schedule_delayed_work(&dell_rfkill_work,
  602. round_jiffies_relative(HZ / 4));
  603. break;
  604. }
  605. extended = false;
  606. }
  607. return false;
  608. }
  609. static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
  610. static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
  611. static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
  612. unsigned long action, void *data)
  613. {
  614. schedule_delayed_work(&dell_rfkill_work, 0);
  615. return NOTIFY_OK;
  616. }
  617. static struct notifier_block dell_laptop_rbtn_notifier = {
  618. .notifier_call = dell_laptop_rbtn_notifier_call,
  619. };
  620. static int __init dell_setup_rfkill(void)
  621. {
  622. struct calling_interface_buffer *buffer;
  623. int status, ret, whitelisted;
  624. const char *product;
  625. /*
  626. * rfkill support causes trouble on various models, mostly Inspirons.
  627. * So we whitelist certain series, and don't support rfkill on others.
  628. */
  629. whitelisted = 0;
  630. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  631. if (product && (strncmp(product, "Latitude", 8) == 0 ||
  632. strncmp(product, "Precision", 9) == 0))
  633. whitelisted = 1;
  634. if (!force_rfkill && !whitelisted)
  635. return 0;
  636. buffer = dell_smbios_get_buffer();
  637. dell_smbios_send_request(17, 11);
  638. ret = buffer->output[0];
  639. status = buffer->output[1];
  640. dell_smbios_release_buffer();
  641. /* dell wireless info smbios call is not supported */
  642. if (ret != 0)
  643. return 0;
  644. /* rfkill is only tested on laptops with a hwswitch */
  645. if (!(status & BIT(0)) && !force_rfkill)
  646. return 0;
  647. if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
  648. wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
  649. RFKILL_TYPE_WLAN,
  650. &dell_rfkill_ops, (void *) 1);
  651. if (!wifi_rfkill) {
  652. ret = -ENOMEM;
  653. goto err_wifi;
  654. }
  655. ret = rfkill_register(wifi_rfkill);
  656. if (ret)
  657. goto err_wifi;
  658. }
  659. if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
  660. bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
  661. &platform_device->dev,
  662. RFKILL_TYPE_BLUETOOTH,
  663. &dell_rfkill_ops, (void *) 2);
  664. if (!bluetooth_rfkill) {
  665. ret = -ENOMEM;
  666. goto err_bluetooth;
  667. }
  668. ret = rfkill_register(bluetooth_rfkill);
  669. if (ret)
  670. goto err_bluetooth;
  671. }
  672. if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
  673. wwan_rfkill = rfkill_alloc("dell-wwan",
  674. &platform_device->dev,
  675. RFKILL_TYPE_WWAN,
  676. &dell_rfkill_ops, (void *) 3);
  677. if (!wwan_rfkill) {
  678. ret = -ENOMEM;
  679. goto err_wwan;
  680. }
  681. ret = rfkill_register(wwan_rfkill);
  682. if (ret)
  683. goto err_wwan;
  684. }
  685. /*
  686. * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
  687. * which can receive events from HW slider switch.
  688. *
  689. * Dell SMBIOS on whitelisted models supports controlling radio devices
  690. * but does not support receiving HW button switch events. We can use
  691. * i8042 filter hook function to receive keyboard data and handle
  692. * keycode for HW button.
  693. *
  694. * So if it is possible we will use Dell Airplane Mode Switch ACPI
  695. * driver for receiving HW events and Dell SMBIOS for setting rfkill
  696. * states. If ACPI driver or device is not available we will fallback to
  697. * i8042 filter hook function.
  698. *
  699. * To prevent duplicate rfkill devices which control and do same thing,
  700. * dell-rbtn driver will automatically remove its own rfkill devices
  701. * once function dell_rbtn_notifier_register() is called.
  702. */
  703. dell_rbtn_notifier_register_func =
  704. symbol_request(dell_rbtn_notifier_register);
  705. if (dell_rbtn_notifier_register_func) {
  706. dell_rbtn_notifier_unregister_func =
  707. symbol_request(dell_rbtn_notifier_unregister);
  708. if (!dell_rbtn_notifier_unregister_func) {
  709. symbol_put(dell_rbtn_notifier_register);
  710. dell_rbtn_notifier_register_func = NULL;
  711. }
  712. }
  713. if (dell_rbtn_notifier_register_func) {
  714. ret = dell_rbtn_notifier_register_func(
  715. &dell_laptop_rbtn_notifier);
  716. symbol_put(dell_rbtn_notifier_register);
  717. dell_rbtn_notifier_register_func = NULL;
  718. if (ret != 0) {
  719. symbol_put(dell_rbtn_notifier_unregister);
  720. dell_rbtn_notifier_unregister_func = NULL;
  721. }
  722. } else {
  723. pr_info("Symbols from dell-rbtn acpi driver are not available\n");
  724. ret = -ENODEV;
  725. }
  726. if (ret == 0) {
  727. pr_info("Using dell-rbtn acpi driver for receiving events\n");
  728. } else if (ret != -ENODEV) {
  729. pr_warn("Unable to register dell rbtn notifier\n");
  730. goto err_filter;
  731. } else {
  732. ret = i8042_install_filter(dell_laptop_i8042_filter);
  733. if (ret) {
  734. pr_warn("Unable to install key filter\n");
  735. goto err_filter;
  736. }
  737. pr_info("Using i8042 filter function for receiving events\n");
  738. }
  739. return 0;
  740. err_filter:
  741. if (wwan_rfkill)
  742. rfkill_unregister(wwan_rfkill);
  743. err_wwan:
  744. rfkill_destroy(wwan_rfkill);
  745. if (bluetooth_rfkill)
  746. rfkill_unregister(bluetooth_rfkill);
  747. err_bluetooth:
  748. rfkill_destroy(bluetooth_rfkill);
  749. if (wifi_rfkill)
  750. rfkill_unregister(wifi_rfkill);
  751. err_wifi:
  752. rfkill_destroy(wifi_rfkill);
  753. return ret;
  754. }
  755. static void dell_cleanup_rfkill(void)
  756. {
  757. if (dell_rbtn_notifier_unregister_func) {
  758. dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
  759. symbol_put(dell_rbtn_notifier_unregister);
  760. dell_rbtn_notifier_unregister_func = NULL;
  761. } else {
  762. i8042_remove_filter(dell_laptop_i8042_filter);
  763. }
  764. cancel_delayed_work_sync(&dell_rfkill_work);
  765. if (wifi_rfkill) {
  766. rfkill_unregister(wifi_rfkill);
  767. rfkill_destroy(wifi_rfkill);
  768. }
  769. if (bluetooth_rfkill) {
  770. rfkill_unregister(bluetooth_rfkill);
  771. rfkill_destroy(bluetooth_rfkill);
  772. }
  773. if (wwan_rfkill) {
  774. rfkill_unregister(wwan_rfkill);
  775. rfkill_destroy(wwan_rfkill);
  776. }
  777. }
  778. static int dell_send_intensity(struct backlight_device *bd)
  779. {
  780. struct calling_interface_buffer *buffer;
  781. struct calling_interface_token *token;
  782. int ret;
  783. token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
  784. if (!token)
  785. return -ENODEV;
  786. buffer = dell_smbios_get_buffer();
  787. buffer->input[0] = token->location;
  788. buffer->input[1] = bd->props.brightness;
  789. if (power_supply_is_system_supplied() > 0)
  790. dell_smbios_send_request(1, 2);
  791. else
  792. dell_smbios_send_request(1, 1);
  793. ret = dell_smbios_error(buffer->output[0]);
  794. dell_smbios_release_buffer();
  795. return ret;
  796. }
  797. static int dell_get_intensity(struct backlight_device *bd)
  798. {
  799. struct calling_interface_buffer *buffer;
  800. struct calling_interface_token *token;
  801. int ret;
  802. token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
  803. if (!token)
  804. return -ENODEV;
  805. buffer = dell_smbios_get_buffer();
  806. buffer->input[0] = token->location;
  807. if (power_supply_is_system_supplied() > 0)
  808. dell_smbios_send_request(0, 2);
  809. else
  810. dell_smbios_send_request(0, 1);
  811. if (buffer->output[0])
  812. ret = dell_smbios_error(buffer->output[0]);
  813. else
  814. ret = buffer->output[1];
  815. dell_smbios_release_buffer();
  816. return ret;
  817. }
  818. static const struct backlight_ops dell_ops = {
  819. .get_brightness = dell_get_intensity,
  820. .update_status = dell_send_intensity,
  821. };
  822. static void touchpad_led_on(void)
  823. {
  824. int command = 0x97;
  825. char data = 1;
  826. i8042_command(&data, command | 1 << 12);
  827. }
  828. static void touchpad_led_off(void)
  829. {
  830. int command = 0x97;
  831. char data = 2;
  832. i8042_command(&data, command | 1 << 12);
  833. }
  834. static void touchpad_led_set(struct led_classdev *led_cdev,
  835. enum led_brightness value)
  836. {
  837. if (value > 0)
  838. touchpad_led_on();
  839. else
  840. touchpad_led_off();
  841. }
  842. static struct led_classdev touchpad_led = {
  843. .name = "dell-laptop::touchpad",
  844. .brightness_set = touchpad_led_set,
  845. .flags = LED_CORE_SUSPENDRESUME,
  846. };
  847. static int __init touchpad_led_init(struct device *dev)
  848. {
  849. return led_classdev_register(dev, &touchpad_led);
  850. }
  851. static void touchpad_led_exit(void)
  852. {
  853. led_classdev_unregister(&touchpad_led);
  854. }
  855. /*
  856. * Derived from information in smbios-keyboard-ctl:
  857. *
  858. * cbClass 4
  859. * cbSelect 11
  860. * Keyboard illumination
  861. * cbArg1 determines the function to be performed
  862. *
  863. * cbArg1 0x0 = Get Feature Information
  864. * cbRES1 Standard return codes (0, -1, -2)
  865. * cbRES2, word0 Bitmap of user-selectable modes
  866. * bit 0 Always off (All systems)
  867. * bit 1 Always on (Travis ATG, Siberia)
  868. * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
  869. * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
  870. * bit 4 Auto: Input-activity-based On; input-activity based Off
  871. * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
  872. * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
  873. * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
  874. * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
  875. * bits 9-15 Reserved for future use
  876. * cbRES2, byte2 Reserved for future use
  877. * cbRES2, byte3 Keyboard illumination type
  878. * 0 Reserved
  879. * 1 Tasklight
  880. * 2 Backlight
  881. * 3-255 Reserved for future use
  882. * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
  883. * bit 0 Any keystroke
  884. * bit 1 Touchpad activity
  885. * bit 2 Pointing stick
  886. * bit 3 Any mouse
  887. * bits 4-7 Reserved for future use
  888. * cbRES3, byte1 Supported timeout unit bitmap
  889. * bit 0 Seconds
  890. * bit 1 Minutes
  891. * bit 2 Hours
  892. * bit 3 Days
  893. * bits 4-7 Reserved for future use
  894. * cbRES3, byte2 Number of keyboard light brightness levels
  895. * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
  896. * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
  897. * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
  898. * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
  899. *
  900. * cbArg1 0x1 = Get Current State
  901. * cbRES1 Standard return codes (0, -1, -2)
  902. * cbRES2, word0 Bitmap of current mode state
  903. * bit 0 Always off (All systems)
  904. * bit 1 Always on (Travis ATG, Siberia)
  905. * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
  906. * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
  907. * bit 4 Auto: Input-activity-based On; input-activity based Off
  908. * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
  909. * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
  910. * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
  911. * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
  912. * bits 9-15 Reserved for future use
  913. * Note: Only One bit can be set
  914. * cbRES2, byte2 Currently active auto keyboard illumination triggers.
  915. * bit 0 Any keystroke
  916. * bit 1 Touchpad activity
  917. * bit 2 Pointing stick
  918. * bit 3 Any mouse
  919. * bits 4-7 Reserved for future use
  920. * cbRES2, byte3 Current Timeout
  921. * bits 7:6 Timeout units indicator:
  922. * 00b Seconds
  923. * 01b Minutes
  924. * 10b Hours
  925. * 11b Days
  926. * bits 5:0 Timeout value (0-63) in sec/min/hr/day
  927. * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
  928. * are set upon return from the [Get feature information] call.
  929. * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
  930. * cbRES3, byte1 Current ALS reading
  931. * cbRES3, byte2 Current keyboard light level.
  932. *
  933. * cbArg1 0x2 = Set New State
  934. * cbRES1 Standard return codes (0, -1, -2)
  935. * cbArg2, word0 Bitmap of current mode state
  936. * bit 0 Always off (All systems)
  937. * bit 1 Always on (Travis ATG, Siberia)
  938. * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
  939. * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
  940. * bit 4 Auto: Input-activity-based On; input-activity based Off
  941. * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
  942. * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
  943. * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
  944. * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
  945. * bits 9-15 Reserved for future use
  946. * Note: Only One bit can be set
  947. * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
  948. * keyboard to turn off automatically.
  949. * bit 0 Any keystroke
  950. * bit 1 Touchpad activity
  951. * bit 2 Pointing stick
  952. * bit 3 Any mouse
  953. * bits 4-7 Reserved for future use
  954. * cbArg2, byte3 Desired Timeout
  955. * bits 7:6 Timeout units indicator:
  956. * 00b Seconds
  957. * 01b Minutes
  958. * 10b Hours
  959. * 11b Days
  960. * bits 5:0 Timeout value (0-63) in sec/min/hr/day
  961. * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
  962. * cbArg3, byte2 Desired keyboard light level.
  963. */
  964. enum kbd_timeout_unit {
  965. KBD_TIMEOUT_SECONDS = 0,
  966. KBD_TIMEOUT_MINUTES,
  967. KBD_TIMEOUT_HOURS,
  968. KBD_TIMEOUT_DAYS,
  969. };
  970. enum kbd_mode_bit {
  971. KBD_MODE_BIT_OFF = 0,
  972. KBD_MODE_BIT_ON,
  973. KBD_MODE_BIT_ALS,
  974. KBD_MODE_BIT_TRIGGER_ALS,
  975. KBD_MODE_BIT_TRIGGER,
  976. KBD_MODE_BIT_TRIGGER_25,
  977. KBD_MODE_BIT_TRIGGER_50,
  978. KBD_MODE_BIT_TRIGGER_75,
  979. KBD_MODE_BIT_TRIGGER_100,
  980. };
  981. #define kbd_is_als_mode_bit(bit) \
  982. ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
  983. #define kbd_is_trigger_mode_bit(bit) \
  984. ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
  985. #define kbd_is_level_mode_bit(bit) \
  986. ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
  987. struct kbd_info {
  988. u16 modes;
  989. u8 type;
  990. u8 triggers;
  991. u8 levels;
  992. u8 seconds;
  993. u8 minutes;
  994. u8 hours;
  995. u8 days;
  996. };
  997. struct kbd_state {
  998. u8 mode_bit;
  999. u8 triggers;
  1000. u8 timeout_value;
  1001. u8 timeout_unit;
  1002. u8 als_setting;
  1003. u8 als_value;
  1004. u8 level;
  1005. };
  1006. static const int kbd_tokens[] = {
  1007. KBD_LED_OFF_TOKEN,
  1008. KBD_LED_AUTO_25_TOKEN,
  1009. KBD_LED_AUTO_50_TOKEN,
  1010. KBD_LED_AUTO_75_TOKEN,
  1011. KBD_LED_AUTO_100_TOKEN,
  1012. KBD_LED_ON_TOKEN,
  1013. };
  1014. static u16 kbd_token_bits;
  1015. static struct kbd_info kbd_info;
  1016. static bool kbd_als_supported;
  1017. static bool kbd_triggers_supported;
  1018. static u8 kbd_mode_levels[16];
  1019. static int kbd_mode_levels_count;
  1020. static u8 kbd_previous_level;
  1021. static u8 kbd_previous_mode_bit;
  1022. static bool kbd_led_present;
  1023. /*
  1024. * NOTE: there are three ways to set the keyboard backlight level.
  1025. * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
  1026. * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
  1027. * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
  1028. *
  1029. * There are laptops which support only one of these methods. If we want to
  1030. * support as many machines as possible we need to implement all three methods.
  1031. * The first two methods use the kbd_state structure. The third uses SMBIOS
  1032. * tokens. If kbd_info.levels == 0, the machine does not support setting the
  1033. * keyboard backlight level via kbd_state.level.
  1034. */
  1035. static int kbd_get_info(struct kbd_info *info)
  1036. {
  1037. struct calling_interface_buffer *buffer;
  1038. u8 units;
  1039. int ret;
  1040. buffer = dell_smbios_get_buffer();
  1041. buffer->input[0] = 0x0;
  1042. dell_smbios_send_request(4, 11);
  1043. ret = buffer->output[0];
  1044. if (ret) {
  1045. ret = dell_smbios_error(ret);
  1046. goto out;
  1047. }
  1048. info->modes = buffer->output[1] & 0xFFFF;
  1049. info->type = (buffer->output[1] >> 24) & 0xFF;
  1050. info->triggers = buffer->output[2] & 0xFF;
  1051. units = (buffer->output[2] >> 8) & 0xFF;
  1052. info->levels = (buffer->output[2] >> 16) & 0xFF;
  1053. if (units & BIT(0))
  1054. info->seconds = (buffer->output[3] >> 0) & 0xFF;
  1055. if (units & BIT(1))
  1056. info->minutes = (buffer->output[3] >> 8) & 0xFF;
  1057. if (units & BIT(2))
  1058. info->hours = (buffer->output[3] >> 16) & 0xFF;
  1059. if (units & BIT(3))
  1060. info->days = (buffer->output[3] >> 24) & 0xFF;
  1061. out:
  1062. dell_smbios_release_buffer();
  1063. return ret;
  1064. }
  1065. static unsigned int kbd_get_max_level(void)
  1066. {
  1067. if (kbd_info.levels != 0)
  1068. return kbd_info.levels;
  1069. if (kbd_mode_levels_count > 0)
  1070. return kbd_mode_levels_count - 1;
  1071. return 0;
  1072. }
  1073. static int kbd_get_level(struct kbd_state *state)
  1074. {
  1075. int i;
  1076. if (kbd_info.levels != 0)
  1077. return state->level;
  1078. if (kbd_mode_levels_count > 0) {
  1079. for (i = 0; i < kbd_mode_levels_count; ++i)
  1080. if (kbd_mode_levels[i] == state->mode_bit)
  1081. return i;
  1082. return 0;
  1083. }
  1084. return -EINVAL;
  1085. }
  1086. static int kbd_set_level(struct kbd_state *state, u8 level)
  1087. {
  1088. if (kbd_info.levels != 0) {
  1089. if (level != 0)
  1090. kbd_previous_level = level;
  1091. if (state->level == level)
  1092. return 0;
  1093. state->level = level;
  1094. if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
  1095. state->mode_bit = kbd_previous_mode_bit;
  1096. else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
  1097. kbd_previous_mode_bit = state->mode_bit;
  1098. state->mode_bit = KBD_MODE_BIT_OFF;
  1099. }
  1100. return 0;
  1101. }
  1102. if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
  1103. if (level != 0)
  1104. kbd_previous_level = level;
  1105. state->mode_bit = kbd_mode_levels[level];
  1106. return 0;
  1107. }
  1108. return -EINVAL;
  1109. }
  1110. static int kbd_get_state(struct kbd_state *state)
  1111. {
  1112. struct calling_interface_buffer *buffer;
  1113. int ret;
  1114. buffer = dell_smbios_get_buffer();
  1115. buffer->input[0] = 0x1;
  1116. dell_smbios_send_request(4, 11);
  1117. ret = buffer->output[0];
  1118. if (ret) {
  1119. ret = dell_smbios_error(ret);
  1120. goto out;
  1121. }
  1122. state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
  1123. if (state->mode_bit != 0)
  1124. state->mode_bit--;
  1125. state->triggers = (buffer->output[1] >> 16) & 0xFF;
  1126. state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
  1127. state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
  1128. state->als_setting = buffer->output[2] & 0xFF;
  1129. state->als_value = (buffer->output[2] >> 8) & 0xFF;
  1130. state->level = (buffer->output[2] >> 16) & 0xFF;
  1131. out:
  1132. dell_smbios_release_buffer();
  1133. return ret;
  1134. }
  1135. static int kbd_set_state(struct kbd_state *state)
  1136. {
  1137. struct calling_interface_buffer *buffer;
  1138. int ret;
  1139. buffer = dell_smbios_get_buffer();
  1140. buffer->input[0] = 0x2;
  1141. buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
  1142. buffer->input[1] |= (state->triggers & 0xFF) << 16;
  1143. buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
  1144. buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
  1145. buffer->input[2] = state->als_setting & 0xFF;
  1146. buffer->input[2] |= (state->level & 0xFF) << 16;
  1147. dell_smbios_send_request(4, 11);
  1148. ret = buffer->output[0];
  1149. dell_smbios_release_buffer();
  1150. return dell_smbios_error(ret);
  1151. }
  1152. static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
  1153. {
  1154. int ret;
  1155. ret = kbd_set_state(state);
  1156. if (ret == 0)
  1157. return 0;
  1158. /*
  1159. * When setting the new state fails,try to restore the previous one.
  1160. * This is needed on some machines where BIOS sets a default state when
  1161. * setting a new state fails. This default state could be all off.
  1162. */
  1163. if (kbd_set_state(old))
  1164. pr_err("Setting old previous keyboard state failed\n");
  1165. return ret;
  1166. }
  1167. static int kbd_set_token_bit(u8 bit)
  1168. {
  1169. struct calling_interface_buffer *buffer;
  1170. struct calling_interface_token *token;
  1171. int ret;
  1172. if (bit >= ARRAY_SIZE(kbd_tokens))
  1173. return -EINVAL;
  1174. token = dell_smbios_find_token(kbd_tokens[bit]);
  1175. if (!token)
  1176. return -EINVAL;
  1177. buffer = dell_smbios_get_buffer();
  1178. buffer->input[0] = token->location;
  1179. buffer->input[1] = token->value;
  1180. dell_smbios_send_request(1, 0);
  1181. ret = buffer->output[0];
  1182. dell_smbios_release_buffer();
  1183. return dell_smbios_error(ret);
  1184. }
  1185. static int kbd_get_token_bit(u8 bit)
  1186. {
  1187. struct calling_interface_buffer *buffer;
  1188. struct calling_interface_token *token;
  1189. int ret;
  1190. int val;
  1191. if (bit >= ARRAY_SIZE(kbd_tokens))
  1192. return -EINVAL;
  1193. token = dell_smbios_find_token(kbd_tokens[bit]);
  1194. if (!token)
  1195. return -EINVAL;
  1196. buffer = dell_smbios_get_buffer();
  1197. buffer->input[0] = token->location;
  1198. dell_smbios_send_request(0, 0);
  1199. ret = buffer->output[0];
  1200. val = buffer->output[1];
  1201. dell_smbios_release_buffer();
  1202. if (ret)
  1203. return dell_smbios_error(ret);
  1204. return (val == token->value);
  1205. }
  1206. static int kbd_get_first_active_token_bit(void)
  1207. {
  1208. int i;
  1209. int ret;
  1210. for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
  1211. ret = kbd_get_token_bit(i);
  1212. if (ret == 1)
  1213. return i;
  1214. }
  1215. return ret;
  1216. }
  1217. static int kbd_get_valid_token_counts(void)
  1218. {
  1219. return hweight16(kbd_token_bits);
  1220. }
  1221. static inline int kbd_init_info(void)
  1222. {
  1223. struct kbd_state state;
  1224. int ret;
  1225. int i;
  1226. ret = kbd_get_info(&kbd_info);
  1227. if (ret)
  1228. return ret;
  1229. kbd_get_state(&state);
  1230. /* NOTE: timeout value is stored in 6 bits so max value is 63 */
  1231. if (kbd_info.seconds > 63)
  1232. kbd_info.seconds = 63;
  1233. if (kbd_info.minutes > 63)
  1234. kbd_info.minutes = 63;
  1235. if (kbd_info.hours > 63)
  1236. kbd_info.hours = 63;
  1237. if (kbd_info.days > 63)
  1238. kbd_info.days = 63;
  1239. /* NOTE: On tested machines ON mode did not work and caused
  1240. * problems (turned backlight off) so do not use it
  1241. */
  1242. kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
  1243. kbd_previous_level = kbd_get_level(&state);
  1244. kbd_previous_mode_bit = state.mode_bit;
  1245. if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
  1246. kbd_previous_level = 1;
  1247. if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
  1248. kbd_previous_mode_bit =
  1249. ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
  1250. if (kbd_previous_mode_bit != 0)
  1251. kbd_previous_mode_bit--;
  1252. }
  1253. if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
  1254. BIT(KBD_MODE_BIT_TRIGGER_ALS)))
  1255. kbd_als_supported = true;
  1256. if (kbd_info.modes & (
  1257. BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
  1258. BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
  1259. BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
  1260. ))
  1261. kbd_triggers_supported = true;
  1262. /* kbd_mode_levels[0] is reserved, see below */
  1263. for (i = 0; i < 16; ++i)
  1264. if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
  1265. kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
  1266. /*
  1267. * Find the first supported mode and assign to kbd_mode_levels[0].
  1268. * This should be 0 (off), but we cannot depend on the BIOS to
  1269. * support 0.
  1270. */
  1271. if (kbd_mode_levels_count > 0) {
  1272. for (i = 0; i < 16; ++i) {
  1273. if (BIT(i) & kbd_info.modes) {
  1274. kbd_mode_levels[0] = i;
  1275. break;
  1276. }
  1277. }
  1278. kbd_mode_levels_count++;
  1279. }
  1280. return 0;
  1281. }
  1282. static inline void kbd_init_tokens(void)
  1283. {
  1284. int i;
  1285. for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
  1286. if (dell_smbios_find_token(kbd_tokens[i]))
  1287. kbd_token_bits |= BIT(i);
  1288. }
  1289. static void kbd_init(void)
  1290. {
  1291. int ret;
  1292. ret = kbd_init_info();
  1293. kbd_init_tokens();
  1294. if (kbd_token_bits != 0 || ret == 0)
  1295. kbd_led_present = true;
  1296. }
  1297. static ssize_t kbd_led_timeout_store(struct device *dev,
  1298. struct device_attribute *attr,
  1299. const char *buf, size_t count)
  1300. {
  1301. struct kbd_state new_state;
  1302. struct kbd_state state;
  1303. bool convert;
  1304. int value;
  1305. int ret;
  1306. char ch;
  1307. u8 unit;
  1308. int i;
  1309. ret = sscanf(buf, "%d %c", &value, &ch);
  1310. if (ret < 1)
  1311. return -EINVAL;
  1312. else if (ret == 1)
  1313. ch = 's';
  1314. if (value < 0)
  1315. return -EINVAL;
  1316. convert = false;
  1317. switch (ch) {
  1318. case 's':
  1319. if (value > kbd_info.seconds)
  1320. convert = true;
  1321. unit = KBD_TIMEOUT_SECONDS;
  1322. break;
  1323. case 'm':
  1324. if (value > kbd_info.minutes)
  1325. convert = true;
  1326. unit = KBD_TIMEOUT_MINUTES;
  1327. break;
  1328. case 'h':
  1329. if (value > kbd_info.hours)
  1330. convert = true;
  1331. unit = KBD_TIMEOUT_HOURS;
  1332. break;
  1333. case 'd':
  1334. if (value > kbd_info.days)
  1335. convert = true;
  1336. unit = KBD_TIMEOUT_DAYS;
  1337. break;
  1338. default:
  1339. return -EINVAL;
  1340. }
  1341. if (quirks && quirks->needs_kbd_timeouts)
  1342. convert = true;
  1343. if (convert) {
  1344. /* Convert value from current units to seconds */
  1345. switch (unit) {
  1346. case KBD_TIMEOUT_DAYS:
  1347. value *= 24;
  1348. case KBD_TIMEOUT_HOURS:
  1349. value *= 60;
  1350. case KBD_TIMEOUT_MINUTES:
  1351. value *= 60;
  1352. unit = KBD_TIMEOUT_SECONDS;
  1353. }
  1354. if (quirks && quirks->needs_kbd_timeouts) {
  1355. for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
  1356. if (value <= quirks->kbd_timeouts[i]) {
  1357. value = quirks->kbd_timeouts[i];
  1358. break;
  1359. }
  1360. }
  1361. }
  1362. if (value <= kbd_info.seconds && kbd_info.seconds) {
  1363. unit = KBD_TIMEOUT_SECONDS;
  1364. } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
  1365. value /= 60;
  1366. unit = KBD_TIMEOUT_MINUTES;
  1367. } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
  1368. value /= (60 * 60);
  1369. unit = KBD_TIMEOUT_HOURS;
  1370. } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
  1371. value /= (60 * 60 * 24);
  1372. unit = KBD_TIMEOUT_DAYS;
  1373. } else {
  1374. return -EINVAL;
  1375. }
  1376. }
  1377. ret = kbd_get_state(&state);
  1378. if (ret)
  1379. return ret;
  1380. new_state = state;
  1381. new_state.timeout_value = value;
  1382. new_state.timeout_unit = unit;
  1383. ret = kbd_set_state_safe(&new_state, &state);
  1384. if (ret)
  1385. return ret;
  1386. return count;
  1387. }
  1388. static ssize_t kbd_led_timeout_show(struct device *dev,
  1389. struct device_attribute *attr, char *buf)
  1390. {
  1391. struct kbd_state state;
  1392. int ret;
  1393. int len;
  1394. ret = kbd_get_state(&state);
  1395. if (ret)
  1396. return ret;
  1397. len = sprintf(buf, "%d", state.timeout_value);
  1398. switch (state.timeout_unit) {
  1399. case KBD_TIMEOUT_SECONDS:
  1400. return len + sprintf(buf+len, "s\n");
  1401. case KBD_TIMEOUT_MINUTES:
  1402. return len + sprintf(buf+len, "m\n");
  1403. case KBD_TIMEOUT_HOURS:
  1404. return len + sprintf(buf+len, "h\n");
  1405. case KBD_TIMEOUT_DAYS:
  1406. return len + sprintf(buf+len, "d\n");
  1407. default:
  1408. return -EINVAL;
  1409. }
  1410. return len;
  1411. }
  1412. static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
  1413. kbd_led_timeout_show, kbd_led_timeout_store);
  1414. static const char * const kbd_led_triggers[] = {
  1415. "keyboard",
  1416. "touchpad",
  1417. /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
  1418. "mouse",
  1419. };
  1420. static ssize_t kbd_led_triggers_store(struct device *dev,
  1421. struct device_attribute *attr,
  1422. const char *buf, size_t count)
  1423. {
  1424. struct kbd_state new_state;
  1425. struct kbd_state state;
  1426. bool triggers_enabled = false;
  1427. int trigger_bit = -1;
  1428. char trigger[21];
  1429. int i, ret;
  1430. ret = sscanf(buf, "%20s", trigger);
  1431. if (ret != 1)
  1432. return -EINVAL;
  1433. if (trigger[0] != '+' && trigger[0] != '-')
  1434. return -EINVAL;
  1435. ret = kbd_get_state(&state);
  1436. if (ret)
  1437. return ret;
  1438. if (kbd_triggers_supported)
  1439. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1440. if (kbd_triggers_supported) {
  1441. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
  1442. if (!(kbd_info.triggers & BIT(i)))
  1443. continue;
  1444. if (!kbd_led_triggers[i])
  1445. continue;
  1446. if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
  1447. continue;
  1448. if (trigger[0] == '+' &&
  1449. triggers_enabled && (state.triggers & BIT(i)))
  1450. return count;
  1451. if (trigger[0] == '-' &&
  1452. (!triggers_enabled || !(state.triggers & BIT(i))))
  1453. return count;
  1454. trigger_bit = i;
  1455. break;
  1456. }
  1457. }
  1458. if (trigger_bit != -1) {
  1459. new_state = state;
  1460. if (trigger[0] == '+')
  1461. new_state.triggers |= BIT(trigger_bit);
  1462. else {
  1463. new_state.triggers &= ~BIT(trigger_bit);
  1464. /* NOTE: trackstick bit (2) must be disabled when
  1465. * disabling touchpad bit (1), otherwise touchpad
  1466. * bit (1) will not be disabled */
  1467. if (trigger_bit == 1)
  1468. new_state.triggers &= ~BIT(2);
  1469. }
  1470. if ((kbd_info.triggers & new_state.triggers) !=
  1471. new_state.triggers)
  1472. return -EINVAL;
  1473. if (new_state.triggers && !triggers_enabled) {
  1474. new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
  1475. kbd_set_level(&new_state, kbd_previous_level);
  1476. } else if (new_state.triggers == 0) {
  1477. kbd_set_level(&new_state, 0);
  1478. }
  1479. if (!(kbd_info.modes & BIT(new_state.mode_bit)))
  1480. return -EINVAL;
  1481. ret = kbd_set_state_safe(&new_state, &state);
  1482. if (ret)
  1483. return ret;
  1484. if (new_state.mode_bit != KBD_MODE_BIT_OFF)
  1485. kbd_previous_mode_bit = new_state.mode_bit;
  1486. return count;
  1487. }
  1488. return -EINVAL;
  1489. }
  1490. static ssize_t kbd_led_triggers_show(struct device *dev,
  1491. struct device_attribute *attr, char *buf)
  1492. {
  1493. struct kbd_state state;
  1494. bool triggers_enabled;
  1495. int level, i, ret;
  1496. int len = 0;
  1497. ret = kbd_get_state(&state);
  1498. if (ret)
  1499. return ret;
  1500. len = 0;
  1501. if (kbd_triggers_supported) {
  1502. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1503. level = kbd_get_level(&state);
  1504. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
  1505. if (!(kbd_info.triggers & BIT(i)))
  1506. continue;
  1507. if (!kbd_led_triggers[i])
  1508. continue;
  1509. if ((triggers_enabled || level <= 0) &&
  1510. (state.triggers & BIT(i)))
  1511. buf[len++] = '+';
  1512. else
  1513. buf[len++] = '-';
  1514. len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
  1515. }
  1516. }
  1517. if (len)
  1518. buf[len - 1] = '\n';
  1519. return len;
  1520. }
  1521. static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
  1522. kbd_led_triggers_show, kbd_led_triggers_store);
  1523. static ssize_t kbd_led_als_enabled_store(struct device *dev,
  1524. struct device_attribute *attr,
  1525. const char *buf, size_t count)
  1526. {
  1527. struct kbd_state new_state;
  1528. struct kbd_state state;
  1529. bool triggers_enabled = false;
  1530. int enable;
  1531. int ret;
  1532. ret = kstrtoint(buf, 0, &enable);
  1533. if (ret)
  1534. return ret;
  1535. ret = kbd_get_state(&state);
  1536. if (ret)
  1537. return ret;
  1538. if (enable == kbd_is_als_mode_bit(state.mode_bit))
  1539. return count;
  1540. new_state = state;
  1541. if (kbd_triggers_supported)
  1542. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1543. if (enable) {
  1544. if (triggers_enabled)
  1545. new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
  1546. else
  1547. new_state.mode_bit = KBD_MODE_BIT_ALS;
  1548. } else {
  1549. if (triggers_enabled) {
  1550. new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
  1551. kbd_set_level(&new_state, kbd_previous_level);
  1552. } else {
  1553. new_state.mode_bit = KBD_MODE_BIT_ON;
  1554. }
  1555. }
  1556. if (!(kbd_info.modes & BIT(new_state.mode_bit)))
  1557. return -EINVAL;
  1558. ret = kbd_set_state_safe(&new_state, &state);
  1559. if (ret)
  1560. return ret;
  1561. kbd_previous_mode_bit = new_state.mode_bit;
  1562. return count;
  1563. }
  1564. static ssize_t kbd_led_als_enabled_show(struct device *dev,
  1565. struct device_attribute *attr,
  1566. char *buf)
  1567. {
  1568. struct kbd_state state;
  1569. bool enabled = false;
  1570. int ret;
  1571. ret = kbd_get_state(&state);
  1572. if (ret)
  1573. return ret;
  1574. enabled = kbd_is_als_mode_bit(state.mode_bit);
  1575. return sprintf(buf, "%d\n", enabled ? 1 : 0);
  1576. }
  1577. static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
  1578. kbd_led_als_enabled_show, kbd_led_als_enabled_store);
  1579. static ssize_t kbd_led_als_setting_store(struct device *dev,
  1580. struct device_attribute *attr,
  1581. const char *buf, size_t count)
  1582. {
  1583. struct kbd_state state;
  1584. struct kbd_state new_state;
  1585. u8 setting;
  1586. int ret;
  1587. ret = kstrtou8(buf, 10, &setting);
  1588. if (ret)
  1589. return ret;
  1590. ret = kbd_get_state(&state);
  1591. if (ret)
  1592. return ret;
  1593. new_state = state;
  1594. new_state.als_setting = setting;
  1595. ret = kbd_set_state_safe(&new_state, &state);
  1596. if (ret)
  1597. return ret;
  1598. return count;
  1599. }
  1600. static ssize_t kbd_led_als_setting_show(struct device *dev,
  1601. struct device_attribute *attr,
  1602. char *buf)
  1603. {
  1604. struct kbd_state state;
  1605. int ret;
  1606. ret = kbd_get_state(&state);
  1607. if (ret)
  1608. return ret;
  1609. return sprintf(buf, "%d\n", state.als_setting);
  1610. }
  1611. static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
  1612. kbd_led_als_setting_show, kbd_led_als_setting_store);
  1613. static struct attribute *kbd_led_attrs[] = {
  1614. &dev_attr_stop_timeout.attr,
  1615. &dev_attr_start_triggers.attr,
  1616. NULL,
  1617. };
  1618. static const struct attribute_group kbd_led_group = {
  1619. .attrs = kbd_led_attrs,
  1620. };
  1621. static struct attribute *kbd_led_als_attrs[] = {
  1622. &dev_attr_als_enabled.attr,
  1623. &dev_attr_als_setting.attr,
  1624. NULL,
  1625. };
  1626. static const struct attribute_group kbd_led_als_group = {
  1627. .attrs = kbd_led_als_attrs,
  1628. };
  1629. static const struct attribute_group *kbd_led_groups[] = {
  1630. &kbd_led_group,
  1631. &kbd_led_als_group,
  1632. NULL,
  1633. };
  1634. static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
  1635. {
  1636. int ret;
  1637. u16 num;
  1638. struct kbd_state state;
  1639. if (kbd_get_max_level()) {
  1640. ret = kbd_get_state(&state);
  1641. if (ret)
  1642. return 0;
  1643. ret = kbd_get_level(&state);
  1644. if (ret < 0)
  1645. return 0;
  1646. return ret;
  1647. }
  1648. if (kbd_get_valid_token_counts()) {
  1649. ret = kbd_get_first_active_token_bit();
  1650. if (ret < 0)
  1651. return 0;
  1652. for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
  1653. num &= num - 1; /* clear the first bit set */
  1654. if (num == 0)
  1655. return 0;
  1656. return ffs(num) - 1;
  1657. }
  1658. pr_warn("Keyboard brightness level control not supported\n");
  1659. return 0;
  1660. }
  1661. static void kbd_led_level_set(struct led_classdev *led_cdev,
  1662. enum led_brightness value)
  1663. {
  1664. struct kbd_state state;
  1665. struct kbd_state new_state;
  1666. u16 num;
  1667. if (kbd_get_max_level()) {
  1668. if (kbd_get_state(&state))
  1669. return;
  1670. new_state = state;
  1671. if (kbd_set_level(&new_state, value))
  1672. return;
  1673. kbd_set_state_safe(&new_state, &state);
  1674. return;
  1675. }
  1676. if (kbd_get_valid_token_counts()) {
  1677. for (num = kbd_token_bits; num != 0 && value > 0; --value)
  1678. num &= num - 1; /* clear the first bit set */
  1679. if (num == 0)
  1680. return;
  1681. kbd_set_token_bit(ffs(num) - 1);
  1682. return;
  1683. }
  1684. pr_warn("Keyboard brightness level control not supported\n");
  1685. }
  1686. static struct led_classdev kbd_led = {
  1687. .name = "dell::kbd_backlight",
  1688. .brightness_set = kbd_led_level_set,
  1689. .brightness_get = kbd_led_level_get,
  1690. .groups = kbd_led_groups,
  1691. };
  1692. static int __init kbd_led_init(struct device *dev)
  1693. {
  1694. kbd_init();
  1695. if (!kbd_led_present)
  1696. return -ENODEV;
  1697. if (!kbd_als_supported)
  1698. kbd_led_groups[1] = NULL;
  1699. kbd_led.max_brightness = kbd_get_max_level();
  1700. if (!kbd_led.max_brightness) {
  1701. kbd_led.max_brightness = kbd_get_valid_token_counts();
  1702. if (kbd_led.max_brightness)
  1703. kbd_led.max_brightness--;
  1704. }
  1705. return led_classdev_register(dev, &kbd_led);
  1706. }
  1707. static void brightness_set_exit(struct led_classdev *led_cdev,
  1708. enum led_brightness value)
  1709. {
  1710. /* Don't change backlight level on exit */
  1711. };
  1712. static void kbd_led_exit(void)
  1713. {
  1714. if (!kbd_led_present)
  1715. return;
  1716. kbd_led.brightness_set = brightness_set_exit;
  1717. led_classdev_unregister(&kbd_led);
  1718. }
  1719. static int __init dell_init(void)
  1720. {
  1721. struct calling_interface_buffer *buffer;
  1722. struct calling_interface_token *token;
  1723. int max_intensity = 0;
  1724. int ret;
  1725. if (!dmi_check_system(dell_device_table))
  1726. return -ENODEV;
  1727. quirks = NULL;
  1728. /* find if this machine support other functions */
  1729. dmi_check_system(dell_quirks);
  1730. ret = platform_driver_register(&platform_driver);
  1731. if (ret)
  1732. goto fail_platform_driver;
  1733. platform_device = platform_device_alloc("dell-laptop", -1);
  1734. if (!platform_device) {
  1735. ret = -ENOMEM;
  1736. goto fail_platform_device1;
  1737. }
  1738. ret = platform_device_add(platform_device);
  1739. if (ret)
  1740. goto fail_platform_device2;
  1741. ret = dell_setup_rfkill();
  1742. if (ret) {
  1743. pr_warn("Unable to setup rfkill\n");
  1744. goto fail_rfkill;
  1745. }
  1746. if (quirks && quirks->touchpad_led)
  1747. touchpad_led_init(&platform_device->dev);
  1748. kbd_led_init(&platform_device->dev);
  1749. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  1750. if (dell_laptop_dir != NULL)
  1751. debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
  1752. &dell_debugfs_fops);
  1753. if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
  1754. return 0;
  1755. token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
  1756. if (token) {
  1757. buffer = dell_smbios_get_buffer();
  1758. buffer->input[0] = token->location;
  1759. dell_smbios_send_request(0, 2);
  1760. if (buffer->output[0] == 0)
  1761. max_intensity = buffer->output[3];
  1762. dell_smbios_release_buffer();
  1763. }
  1764. if (max_intensity) {
  1765. struct backlight_properties props;
  1766. memset(&props, 0, sizeof(struct backlight_properties));
  1767. props.type = BACKLIGHT_PLATFORM;
  1768. props.max_brightness = max_intensity;
  1769. dell_backlight_device = backlight_device_register("dell_backlight",
  1770. &platform_device->dev,
  1771. NULL,
  1772. &dell_ops,
  1773. &props);
  1774. if (IS_ERR(dell_backlight_device)) {
  1775. ret = PTR_ERR(dell_backlight_device);
  1776. dell_backlight_device = NULL;
  1777. goto fail_backlight;
  1778. }
  1779. dell_backlight_device->props.brightness =
  1780. dell_get_intensity(dell_backlight_device);
  1781. backlight_update_status(dell_backlight_device);
  1782. }
  1783. return 0;
  1784. fail_backlight:
  1785. dell_cleanup_rfkill();
  1786. fail_rfkill:
  1787. platform_device_del(platform_device);
  1788. fail_platform_device2:
  1789. platform_device_put(platform_device);
  1790. fail_platform_device1:
  1791. platform_driver_unregister(&platform_driver);
  1792. fail_platform_driver:
  1793. return ret;
  1794. }
  1795. static void __exit dell_exit(void)
  1796. {
  1797. debugfs_remove_recursive(dell_laptop_dir);
  1798. if (quirks && quirks->touchpad_led)
  1799. touchpad_led_exit();
  1800. kbd_led_exit();
  1801. backlight_device_unregister(dell_backlight_device);
  1802. dell_cleanup_rfkill();
  1803. if (platform_device) {
  1804. platform_device_unregister(platform_device);
  1805. platform_driver_unregister(&platform_driver);
  1806. }
  1807. }
  1808. /* dell-rbtn.c driver export functions which will not work correctly (and could
  1809. * cause kernel crash) if they are called before dell-rbtn.c init code. This is
  1810. * not problem when dell-rbtn.c is compiled as external module. When both files
  1811. * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
  1812. * need to ensure that dell_init() will be called after initializing dell-rbtn.
  1813. * This can be achieved by late_initcall() instead module_init().
  1814. */
  1815. late_initcall(dell_init);
  1816. module_exit(dell_exit);
  1817. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  1818. MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
  1819. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  1820. MODULE_DESCRIPTION("Dell laptop driver");
  1821. MODULE_LICENSE("GPL");