dell-laptop.c 55 KB

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