dell-laptop.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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. int trigger_bit = -1;
  1314. char trigger[21];
  1315. int i, ret;
  1316. ret = sscanf(buf, "%20s", trigger);
  1317. if (ret != 1)
  1318. return -EINVAL;
  1319. if (trigger[0] != '+' && trigger[0] != '-')
  1320. return -EINVAL;
  1321. ret = kbd_get_state(&state);
  1322. if (ret)
  1323. return ret;
  1324. if (kbd_triggers_supported)
  1325. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1326. if (kbd_triggers_supported) {
  1327. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
  1328. if (!(kbd_info.triggers & BIT(i)))
  1329. continue;
  1330. if (!kbd_led_triggers[i])
  1331. continue;
  1332. if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
  1333. continue;
  1334. if (trigger[0] == '+' &&
  1335. triggers_enabled && (state.triggers & BIT(i)))
  1336. return count;
  1337. if (trigger[0] == '-' &&
  1338. (!triggers_enabled || !(state.triggers & BIT(i))))
  1339. return count;
  1340. trigger_bit = i;
  1341. break;
  1342. }
  1343. }
  1344. if (trigger_bit != -1) {
  1345. new_state = state;
  1346. if (trigger[0] == '+')
  1347. new_state.triggers |= BIT(trigger_bit);
  1348. else {
  1349. new_state.triggers &= ~BIT(trigger_bit);
  1350. /* NOTE: trackstick bit (2) must be disabled when
  1351. * disabling touchpad bit (1), otherwise touchpad
  1352. * bit (1) will not be disabled */
  1353. if (trigger_bit == 1)
  1354. new_state.triggers &= ~BIT(2);
  1355. }
  1356. if ((kbd_info.triggers & new_state.triggers) !=
  1357. new_state.triggers)
  1358. return -EINVAL;
  1359. if (new_state.triggers && !triggers_enabled) {
  1360. new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
  1361. kbd_set_level(&new_state, kbd_previous_level);
  1362. } else if (new_state.triggers == 0) {
  1363. kbd_set_level(&new_state, 0);
  1364. }
  1365. if (!(kbd_info.modes & BIT(new_state.mode_bit)))
  1366. return -EINVAL;
  1367. ret = kbd_set_state_safe(&new_state, &state);
  1368. if (ret)
  1369. return ret;
  1370. if (new_state.mode_bit != KBD_MODE_BIT_OFF)
  1371. kbd_previous_mode_bit = new_state.mode_bit;
  1372. return count;
  1373. }
  1374. return -EINVAL;
  1375. }
  1376. static ssize_t kbd_led_triggers_show(struct device *dev,
  1377. struct device_attribute *attr, char *buf)
  1378. {
  1379. struct kbd_state state;
  1380. bool triggers_enabled;
  1381. int level, i, ret;
  1382. int len = 0;
  1383. ret = kbd_get_state(&state);
  1384. if (ret)
  1385. return ret;
  1386. len = 0;
  1387. if (kbd_triggers_supported) {
  1388. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1389. level = kbd_get_level(&state);
  1390. for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
  1391. if (!(kbd_info.triggers & BIT(i)))
  1392. continue;
  1393. if (!kbd_led_triggers[i])
  1394. continue;
  1395. if ((triggers_enabled || level <= 0) &&
  1396. (state.triggers & BIT(i)))
  1397. buf[len++] = '+';
  1398. else
  1399. buf[len++] = '-';
  1400. len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
  1401. }
  1402. }
  1403. if (len)
  1404. buf[len - 1] = '\n';
  1405. return len;
  1406. }
  1407. static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
  1408. kbd_led_triggers_show, kbd_led_triggers_store);
  1409. static ssize_t kbd_led_als_enabled_store(struct device *dev,
  1410. struct device_attribute *attr,
  1411. const char *buf, size_t count)
  1412. {
  1413. struct kbd_state new_state;
  1414. struct kbd_state state;
  1415. bool triggers_enabled = false;
  1416. int enable;
  1417. int ret;
  1418. ret = kstrtoint(buf, 0, &enable);
  1419. if (ret)
  1420. return ret;
  1421. ret = kbd_get_state(&state);
  1422. if (ret)
  1423. return ret;
  1424. if (enable == kbd_is_als_mode_bit(state.mode_bit))
  1425. return count;
  1426. new_state = state;
  1427. if (kbd_triggers_supported)
  1428. triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
  1429. if (enable) {
  1430. if (triggers_enabled)
  1431. new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
  1432. else
  1433. new_state.mode_bit = KBD_MODE_BIT_ALS;
  1434. } else {
  1435. if (triggers_enabled) {
  1436. new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
  1437. kbd_set_level(&new_state, kbd_previous_level);
  1438. } else {
  1439. new_state.mode_bit = KBD_MODE_BIT_ON;
  1440. }
  1441. }
  1442. if (!(kbd_info.modes & BIT(new_state.mode_bit)))
  1443. return -EINVAL;
  1444. ret = kbd_set_state_safe(&new_state, &state);
  1445. if (ret)
  1446. return ret;
  1447. kbd_previous_mode_bit = new_state.mode_bit;
  1448. return count;
  1449. }
  1450. static ssize_t kbd_led_als_enabled_show(struct device *dev,
  1451. struct device_attribute *attr,
  1452. char *buf)
  1453. {
  1454. struct kbd_state state;
  1455. bool enabled = false;
  1456. int ret;
  1457. ret = kbd_get_state(&state);
  1458. if (ret)
  1459. return ret;
  1460. enabled = kbd_is_als_mode_bit(state.mode_bit);
  1461. return sprintf(buf, "%d\n", enabled ? 1 : 0);
  1462. }
  1463. static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
  1464. kbd_led_als_enabled_show, kbd_led_als_enabled_store);
  1465. static ssize_t kbd_led_als_setting_store(struct device *dev,
  1466. struct device_attribute *attr,
  1467. const char *buf, size_t count)
  1468. {
  1469. struct kbd_state state;
  1470. struct kbd_state new_state;
  1471. u8 setting;
  1472. int ret;
  1473. ret = kstrtou8(buf, 10, &setting);
  1474. if (ret)
  1475. return ret;
  1476. ret = kbd_get_state(&state);
  1477. if (ret)
  1478. return ret;
  1479. new_state = state;
  1480. new_state.als_setting = setting;
  1481. ret = kbd_set_state_safe(&new_state, &state);
  1482. if (ret)
  1483. return ret;
  1484. return count;
  1485. }
  1486. static ssize_t kbd_led_als_setting_show(struct device *dev,
  1487. struct device_attribute *attr,
  1488. char *buf)
  1489. {
  1490. struct kbd_state state;
  1491. int ret;
  1492. ret = kbd_get_state(&state);
  1493. if (ret)
  1494. return ret;
  1495. return sprintf(buf, "%d\n", state.als_setting);
  1496. }
  1497. static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
  1498. kbd_led_als_setting_show, kbd_led_als_setting_store);
  1499. static struct attribute *kbd_led_attrs[] = {
  1500. &dev_attr_stop_timeout.attr,
  1501. &dev_attr_start_triggers.attr,
  1502. NULL,
  1503. };
  1504. static const struct attribute_group kbd_led_group = {
  1505. .attrs = kbd_led_attrs,
  1506. };
  1507. static struct attribute *kbd_led_als_attrs[] = {
  1508. &dev_attr_als_enabled.attr,
  1509. &dev_attr_als_setting.attr,
  1510. NULL,
  1511. };
  1512. static const struct attribute_group kbd_led_als_group = {
  1513. .attrs = kbd_led_als_attrs,
  1514. };
  1515. static const struct attribute_group *kbd_led_groups[] = {
  1516. &kbd_led_group,
  1517. &kbd_led_als_group,
  1518. NULL,
  1519. };
  1520. static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
  1521. {
  1522. int ret;
  1523. u16 num;
  1524. struct kbd_state state;
  1525. if (kbd_get_max_level()) {
  1526. ret = kbd_get_state(&state);
  1527. if (ret)
  1528. return 0;
  1529. ret = kbd_get_level(&state);
  1530. if (ret < 0)
  1531. return 0;
  1532. return ret;
  1533. }
  1534. if (kbd_get_valid_token_counts()) {
  1535. ret = kbd_get_first_active_token_bit();
  1536. if (ret < 0)
  1537. return 0;
  1538. for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
  1539. num &= num - 1; /* clear the first bit set */
  1540. if (num == 0)
  1541. return 0;
  1542. return ffs(num) - 1;
  1543. }
  1544. pr_warn("Keyboard brightness level control not supported\n");
  1545. return 0;
  1546. }
  1547. static void kbd_led_level_set(struct led_classdev *led_cdev,
  1548. enum led_brightness value)
  1549. {
  1550. struct kbd_state state;
  1551. struct kbd_state new_state;
  1552. u16 num;
  1553. if (kbd_get_max_level()) {
  1554. if (kbd_get_state(&state))
  1555. return;
  1556. new_state = state;
  1557. if (kbd_set_level(&new_state, value))
  1558. return;
  1559. kbd_set_state_safe(&new_state, &state);
  1560. return;
  1561. }
  1562. if (kbd_get_valid_token_counts()) {
  1563. for (num = kbd_token_bits; num != 0 && value > 0; --value)
  1564. num &= num - 1; /* clear the first bit set */
  1565. if (num == 0)
  1566. return;
  1567. kbd_set_token_bit(ffs(num) - 1);
  1568. return;
  1569. }
  1570. pr_warn("Keyboard brightness level control not supported\n");
  1571. }
  1572. static struct led_classdev kbd_led = {
  1573. .name = "dell::kbd_backlight",
  1574. .brightness_set = kbd_led_level_set,
  1575. .brightness_get = kbd_led_level_get,
  1576. .groups = kbd_led_groups,
  1577. };
  1578. static int __init kbd_led_init(struct device *dev)
  1579. {
  1580. kbd_init();
  1581. if (!kbd_led_present)
  1582. return -ENODEV;
  1583. if (!kbd_als_supported)
  1584. kbd_led_groups[1] = NULL;
  1585. kbd_led.max_brightness = kbd_get_max_level();
  1586. if (!kbd_led.max_brightness) {
  1587. kbd_led.max_brightness = kbd_get_valid_token_counts();
  1588. if (kbd_led.max_brightness)
  1589. kbd_led.max_brightness--;
  1590. }
  1591. return led_classdev_register(dev, &kbd_led);
  1592. }
  1593. static void brightness_set_exit(struct led_classdev *led_cdev,
  1594. enum led_brightness value)
  1595. {
  1596. /* Don't change backlight level on exit */
  1597. };
  1598. static void kbd_led_exit(void)
  1599. {
  1600. if (!kbd_led_present)
  1601. return;
  1602. kbd_led.brightness_set = brightness_set_exit;
  1603. led_classdev_unregister(&kbd_led);
  1604. }
  1605. static int __init dell_init(void)
  1606. {
  1607. int max_intensity = 0;
  1608. int ret;
  1609. if (!dmi_check_system(dell_device_table))
  1610. return -ENODEV;
  1611. quirks = NULL;
  1612. /* find if this machine support other functions */
  1613. dmi_check_system(dell_quirks);
  1614. dmi_walk(find_tokens, NULL);
  1615. if (!da_tokens) {
  1616. pr_info("Unable to find dmi tokens\n");
  1617. return -ENODEV;
  1618. }
  1619. ret = platform_driver_register(&platform_driver);
  1620. if (ret)
  1621. goto fail_platform_driver;
  1622. platform_device = platform_device_alloc("dell-laptop", -1);
  1623. if (!platform_device) {
  1624. ret = -ENOMEM;
  1625. goto fail_platform_device1;
  1626. }
  1627. ret = platform_device_add(platform_device);
  1628. if (ret)
  1629. goto fail_platform_device2;
  1630. /*
  1631. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  1632. * is passed to SMI handler.
  1633. */
  1634. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  1635. if (!bufferpage) {
  1636. ret = -ENOMEM;
  1637. goto fail_buffer;
  1638. }
  1639. buffer = page_address(bufferpage);
  1640. ret = dell_setup_rfkill();
  1641. if (ret) {
  1642. pr_warn("Unable to setup rfkill\n");
  1643. goto fail_rfkill;
  1644. }
  1645. if (quirks && quirks->touchpad_led)
  1646. touchpad_led_init(&platform_device->dev);
  1647. kbd_led_init(&platform_device->dev);
  1648. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  1649. if (dell_laptop_dir != NULL)
  1650. debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
  1651. &dell_debugfs_fops);
  1652. #ifdef CONFIG_ACPI
  1653. /* In the event of an ACPI backlight being available, don't
  1654. * register the platform controller.
  1655. */
  1656. if (acpi_video_backlight_support())
  1657. return 0;
  1658. #endif
  1659. get_buffer();
  1660. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  1661. if (buffer->input[0] != -1) {
  1662. dell_send_request(buffer, 0, 2);
  1663. max_intensity = buffer->output[3];
  1664. }
  1665. release_buffer();
  1666. if (max_intensity) {
  1667. struct backlight_properties props;
  1668. memset(&props, 0, sizeof(struct backlight_properties));
  1669. props.type = BACKLIGHT_PLATFORM;
  1670. props.max_brightness = max_intensity;
  1671. dell_backlight_device = backlight_device_register("dell_backlight",
  1672. &platform_device->dev,
  1673. NULL,
  1674. &dell_ops,
  1675. &props);
  1676. if (IS_ERR(dell_backlight_device)) {
  1677. ret = PTR_ERR(dell_backlight_device);
  1678. dell_backlight_device = NULL;
  1679. goto fail_backlight;
  1680. }
  1681. dell_backlight_device->props.brightness =
  1682. dell_get_intensity(dell_backlight_device);
  1683. backlight_update_status(dell_backlight_device);
  1684. }
  1685. return 0;
  1686. fail_backlight:
  1687. i8042_remove_filter(dell_laptop_i8042_filter);
  1688. cancel_delayed_work_sync(&dell_rfkill_work);
  1689. dell_cleanup_rfkill();
  1690. fail_rfkill:
  1691. free_page((unsigned long)bufferpage);
  1692. fail_buffer:
  1693. platform_device_del(platform_device);
  1694. fail_platform_device2:
  1695. platform_device_put(platform_device);
  1696. fail_platform_device1:
  1697. platform_driver_unregister(&platform_driver);
  1698. fail_platform_driver:
  1699. kfree(da_tokens);
  1700. return ret;
  1701. }
  1702. static void __exit dell_exit(void)
  1703. {
  1704. debugfs_remove_recursive(dell_laptop_dir);
  1705. if (quirks && quirks->touchpad_led)
  1706. touchpad_led_exit();
  1707. kbd_led_exit();
  1708. i8042_remove_filter(dell_laptop_i8042_filter);
  1709. cancel_delayed_work_sync(&dell_rfkill_work);
  1710. backlight_device_unregister(dell_backlight_device);
  1711. dell_cleanup_rfkill();
  1712. if (platform_device) {
  1713. platform_device_unregister(platform_device);
  1714. platform_driver_unregister(&platform_driver);
  1715. }
  1716. kfree(da_tokens);
  1717. free_page((unsigned long)buffer);
  1718. }
  1719. module_init(dell_init);
  1720. module_exit(dell_exit);
  1721. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  1722. MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
  1723. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  1724. MODULE_DESCRIPTION("Dell laptop driver");
  1725. MODULE_LICENSE("GPL");