dell-laptop.c 57 KB

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