dell-laptop.c 58 KB

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