dell-laptop.c 58 KB

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