dell-laptop.c 58 KB

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