dell-laptop.c 48 KB

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