dell-laptop.c 57 KB

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