dell-laptop.c 57 KB

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