dell-laptop.c 56 KB

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