dell-laptop.c 53 KB

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