dell-laptop.c 59 KB

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