dell-laptop.c 53 KB

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