dell-laptop.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * Driver for Dell laptop extras
  3. *
  4. * Copyright (c) Red Hat <mjg@redhat.com>
  5. *
  6. * Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
  7. * Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/backlight.h>
  19. #include <linux/err.h>
  20. #include <linux/dmi.h>
  21. #include <linux/io.h>
  22. #include <linux/rfkill.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/acpi.h>
  25. #include <linux/mm.h>
  26. #include <linux/i8042.h>
  27. #include <linux/slab.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/seq_file.h>
  30. #include "../../firmware/dcdbas.h"
  31. #define BRIGHTNESS_TOKEN 0x7d
  32. /* This structure will be modified by the firmware when we enter
  33. * system management mode, hence the volatiles */
  34. struct calling_interface_buffer {
  35. u16 class;
  36. u16 select;
  37. volatile u32 input[4];
  38. volatile u32 output[4];
  39. } __packed;
  40. struct calling_interface_token {
  41. u16 tokenID;
  42. u16 location;
  43. union {
  44. u16 value;
  45. u16 stringlength;
  46. };
  47. };
  48. struct calling_interface_structure {
  49. struct dmi_header header;
  50. u16 cmdIOAddress;
  51. u8 cmdIOCode;
  52. u32 supportedCmds;
  53. struct calling_interface_token tokens[];
  54. } __packed;
  55. struct quirk_entry {
  56. u8 touchpad_led;
  57. };
  58. static struct quirk_entry *quirks;
  59. static struct quirk_entry quirk_dell_vostro_v130 = {
  60. .touchpad_led = 1,
  61. };
  62. static int __init dmi_matched(const struct dmi_system_id *dmi)
  63. {
  64. quirks = dmi->driver_data;
  65. return 1;
  66. }
  67. static int da_command_address;
  68. static int da_command_code;
  69. static int da_num_tokens;
  70. static struct calling_interface_token *da_tokens;
  71. static struct platform_driver platform_driver = {
  72. .driver = {
  73. .name = "dell-laptop",
  74. }
  75. };
  76. static struct platform_device *platform_device;
  77. static struct backlight_device *dell_backlight_device;
  78. static struct rfkill *wifi_rfkill;
  79. static struct rfkill *bluetooth_rfkill;
  80. static struct rfkill *wwan_rfkill;
  81. static bool force_rfkill;
  82. module_param(force_rfkill, bool, 0444);
  83. MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
  84. static const struct dmi_system_id dell_device_table[] __initconst = {
  85. {
  86. .ident = "Dell laptop",
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  89. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  90. },
  91. },
  92. {
  93. .matches = {
  94. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  95. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
  96. },
  97. },
  98. {
  99. .ident = "Dell Computer Corporation",
  100. .matches = {
  101. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  102. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  103. },
  104. },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(dmi, dell_device_table);
  108. static const struct dmi_system_id dell_quirks[] __initconst = {
  109. {
  110. .callback = dmi_matched,
  111. .ident = "Dell Vostro V130",
  112. .matches = {
  113. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  114. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
  115. },
  116. .driver_data = &quirk_dell_vostro_v130,
  117. },
  118. {
  119. .callback = dmi_matched,
  120. .ident = "Dell Vostro V131",
  121. .matches = {
  122. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  123. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
  124. },
  125. .driver_data = &quirk_dell_vostro_v130,
  126. },
  127. {
  128. .callback = dmi_matched,
  129. .ident = "Dell Vostro 3350",
  130. .matches = {
  131. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  132. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
  133. },
  134. .driver_data = &quirk_dell_vostro_v130,
  135. },
  136. {
  137. .callback = dmi_matched,
  138. .ident = "Dell Vostro 3555",
  139. .matches = {
  140. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  141. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
  142. },
  143. .driver_data = &quirk_dell_vostro_v130,
  144. },
  145. {
  146. .callback = dmi_matched,
  147. .ident = "Dell Inspiron N311z",
  148. .matches = {
  149. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  150. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
  151. },
  152. .driver_data = &quirk_dell_vostro_v130,
  153. },
  154. {
  155. .callback = dmi_matched,
  156. .ident = "Dell Inspiron M5110",
  157. .matches = {
  158. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  159. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
  160. },
  161. .driver_data = &quirk_dell_vostro_v130,
  162. },
  163. {
  164. .callback = dmi_matched,
  165. .ident = "Dell Vostro 3360",
  166. .matches = {
  167. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  168. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
  169. },
  170. .driver_data = &quirk_dell_vostro_v130,
  171. },
  172. {
  173. .callback = dmi_matched,
  174. .ident = "Dell Vostro 3460",
  175. .matches = {
  176. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  177. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
  178. },
  179. .driver_data = &quirk_dell_vostro_v130,
  180. },
  181. {
  182. .callback = dmi_matched,
  183. .ident = "Dell Vostro 3560",
  184. .matches = {
  185. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  186. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
  187. },
  188. .driver_data = &quirk_dell_vostro_v130,
  189. },
  190. {
  191. .callback = dmi_matched,
  192. .ident = "Dell Vostro 3450",
  193. .matches = {
  194. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  195. DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
  196. },
  197. .driver_data = &quirk_dell_vostro_v130,
  198. },
  199. {
  200. .callback = dmi_matched,
  201. .ident = "Dell Inspiron 5420",
  202. .matches = {
  203. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  204. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
  205. },
  206. .driver_data = &quirk_dell_vostro_v130,
  207. },
  208. {
  209. .callback = dmi_matched,
  210. .ident = "Dell Inspiron 5520",
  211. .matches = {
  212. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  213. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
  214. },
  215. .driver_data = &quirk_dell_vostro_v130,
  216. },
  217. {
  218. .callback = dmi_matched,
  219. .ident = "Dell Inspiron 5720",
  220. .matches = {
  221. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  222. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
  223. },
  224. .driver_data = &quirk_dell_vostro_v130,
  225. },
  226. {
  227. .callback = dmi_matched,
  228. .ident = "Dell Inspiron 7420",
  229. .matches = {
  230. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  231. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
  232. },
  233. .driver_data = &quirk_dell_vostro_v130,
  234. },
  235. {
  236. .callback = dmi_matched,
  237. .ident = "Dell Inspiron 7520",
  238. .matches = {
  239. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  240. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
  241. },
  242. .driver_data = &quirk_dell_vostro_v130,
  243. },
  244. {
  245. .callback = dmi_matched,
  246. .ident = "Dell Inspiron 7720",
  247. .matches = {
  248. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  249. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
  250. },
  251. .driver_data = &quirk_dell_vostro_v130,
  252. },
  253. { }
  254. };
  255. static struct calling_interface_buffer *buffer;
  256. static struct page *bufferpage;
  257. static DEFINE_MUTEX(buffer_mutex);
  258. static int hwswitch_state;
  259. static void get_buffer(void)
  260. {
  261. mutex_lock(&buffer_mutex);
  262. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  263. }
  264. static void release_buffer(void)
  265. {
  266. mutex_unlock(&buffer_mutex);
  267. }
  268. static void __init parse_da_table(const struct dmi_header *dm)
  269. {
  270. /* Final token is a terminator, so we don't want to copy it */
  271. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  272. struct calling_interface_token *new_da_tokens;
  273. struct calling_interface_structure *table =
  274. container_of(dm, struct calling_interface_structure, header);
  275. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  276. 6 bytes of entry */
  277. if (dm->length < 17)
  278. return;
  279. da_command_address = table->cmdIOAddress;
  280. da_command_code = table->cmdIOCode;
  281. new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  282. sizeof(struct calling_interface_token),
  283. GFP_KERNEL);
  284. if (!new_da_tokens)
  285. return;
  286. da_tokens = new_da_tokens;
  287. memcpy(da_tokens+da_num_tokens, table->tokens,
  288. sizeof(struct calling_interface_token) * tokens);
  289. da_num_tokens += tokens;
  290. }
  291. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  292. {
  293. switch (dm->type) {
  294. case 0xd4: /* Indexed IO */
  295. case 0xd5: /* Protected Area Type 1 */
  296. case 0xd6: /* Protected Area Type 2 */
  297. break;
  298. case 0xda: /* Calling interface */
  299. parse_da_table(dm);
  300. break;
  301. }
  302. }
  303. static int find_token_location(int tokenid)
  304. {
  305. int i;
  306. for (i = 0; i < da_num_tokens; i++) {
  307. if (da_tokens[i].tokenID == tokenid)
  308. return da_tokens[i].location;
  309. }
  310. return -1;
  311. }
  312. static struct calling_interface_buffer *
  313. dell_send_request(struct calling_interface_buffer *buffer, int class,
  314. int select)
  315. {
  316. struct smi_cmd command;
  317. command.magic = SMI_CMD_MAGIC;
  318. command.command_address = da_command_address;
  319. command.command_code = da_command_code;
  320. command.ebx = virt_to_phys(buffer);
  321. command.ecx = 0x42534931;
  322. buffer->class = class;
  323. buffer->select = select;
  324. dcdbas_smi_request(&command);
  325. return buffer;
  326. }
  327. /* Derived from information in DellWirelessCtl.cpp:
  328. Class 17, select 11 is radio control. It returns an array of 32-bit values.
  329. Input byte 0 = 0: Wireless information
  330. result[0]: return code
  331. result[1]:
  332. Bit 0: Hardware switch supported
  333. Bit 1: Wifi locator supported
  334. Bit 2: Wifi is supported
  335. Bit 3: Bluetooth is supported
  336. Bit 4: WWAN is supported
  337. Bit 5: Wireless keyboard supported
  338. Bits 6-7: Reserved
  339. Bit 8: Wifi is installed
  340. Bit 9: Bluetooth is installed
  341. Bit 10: WWAN is installed
  342. Bits 11-15: Reserved
  343. Bit 16: Hardware switch is on
  344. Bit 17: Wifi is blocked
  345. Bit 18: Bluetooth is blocked
  346. Bit 19: WWAN is blocked
  347. Bits 20-31: Reserved
  348. result[2]: NVRAM size in bytes
  349. result[3]: NVRAM format version number
  350. Input byte 0 = 2: Wireless switch configuration
  351. result[0]: return code
  352. result[1]:
  353. Bit 0: Wifi controlled by switch
  354. Bit 1: Bluetooth controlled by switch
  355. Bit 2: WWAN controlled by switch
  356. Bits 3-6: Reserved
  357. Bit 7: Wireless switch config locked
  358. Bit 8: Wifi locator enabled
  359. Bits 9-14: Reserved
  360. Bit 15: Wifi locator setting locked
  361. Bits 16-31: Reserved
  362. */
  363. static int dell_rfkill_set(void *data, bool blocked)
  364. {
  365. int disable = blocked ? 1 : 0;
  366. unsigned long radio = (unsigned long)data;
  367. int hwswitch_bit = (unsigned long)data - 1;
  368. get_buffer();
  369. dell_send_request(buffer, 17, 11);
  370. /* If the hardware switch controls this radio, and the hardware
  371. switch is disabled, always disable the radio */
  372. if ((hwswitch_state & BIT(hwswitch_bit)) &&
  373. !(buffer->output[1] & BIT(16)))
  374. disable = 1;
  375. buffer->input[0] = (1 | (radio<<8) | (disable << 16));
  376. dell_send_request(buffer, 17, 11);
  377. release_buffer();
  378. return 0;
  379. }
  380. /* Must be called with the buffer held */
  381. static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
  382. int status)
  383. {
  384. if (status & BIT(0)) {
  385. /* Has hw-switch, sync sw_state to BIOS */
  386. int block = rfkill_blocked(rfkill);
  387. buffer->input[0] = (1 | (radio << 8) | (block << 16));
  388. dell_send_request(buffer, 17, 11);
  389. } else {
  390. /* No hw-switch, sync BIOS state to sw_state */
  391. rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
  392. }
  393. }
  394. static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
  395. int status)
  396. {
  397. if (hwswitch_state & (BIT(radio - 1)))
  398. rfkill_set_hw_state(rfkill, !(status & BIT(16)));
  399. }
  400. static void dell_rfkill_query(struct rfkill *rfkill, void *data)
  401. {
  402. int status;
  403. get_buffer();
  404. dell_send_request(buffer, 17, 11);
  405. status = buffer->output[1];
  406. dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
  407. release_buffer();
  408. }
  409. static const struct rfkill_ops dell_rfkill_ops = {
  410. .set_block = dell_rfkill_set,
  411. .query = dell_rfkill_query,
  412. };
  413. static struct dentry *dell_laptop_dir;
  414. static int dell_debugfs_show(struct seq_file *s, void *data)
  415. {
  416. int status;
  417. get_buffer();
  418. dell_send_request(buffer, 17, 11);
  419. status = buffer->output[1];
  420. release_buffer();
  421. seq_printf(s, "status:\t0x%X\n", status);
  422. seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
  423. status & BIT(0));
  424. seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
  425. (status & BIT(1)) >> 1);
  426. seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
  427. (status & BIT(2)) >> 2);
  428. seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
  429. (status & BIT(3)) >> 3);
  430. seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
  431. (status & BIT(4)) >> 4);
  432. seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
  433. (status & BIT(5)) >> 5);
  434. seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
  435. (status & BIT(8)) >> 8);
  436. seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
  437. (status & BIT(9)) >> 9);
  438. seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
  439. (status & BIT(10)) >> 10);
  440. seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
  441. (status & BIT(16)) >> 16);
  442. seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
  443. (status & BIT(17)) >> 17);
  444. seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
  445. (status & BIT(18)) >> 18);
  446. seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
  447. (status & BIT(19)) >> 19);
  448. seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
  449. seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
  450. hwswitch_state & BIT(0));
  451. seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
  452. (hwswitch_state & BIT(1)) >> 1);
  453. seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
  454. (hwswitch_state & BIT(2)) >> 2);
  455. seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
  456. (hwswitch_state & BIT(7)) >> 7);
  457. seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
  458. (hwswitch_state & BIT(8)) >> 8);
  459. seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
  460. (hwswitch_state & BIT(15)) >> 15);
  461. return 0;
  462. }
  463. static int dell_debugfs_open(struct inode *inode, struct file *file)
  464. {
  465. return single_open(file, dell_debugfs_show, inode->i_private);
  466. }
  467. static const struct file_operations dell_debugfs_fops = {
  468. .owner = THIS_MODULE,
  469. .open = dell_debugfs_open,
  470. .read = seq_read,
  471. .llseek = seq_lseek,
  472. .release = single_release,
  473. };
  474. static void dell_update_rfkill(struct work_struct *ignored)
  475. {
  476. int status;
  477. get_buffer();
  478. dell_send_request(buffer, 17, 11);
  479. status = buffer->output[1];
  480. if (wifi_rfkill) {
  481. dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
  482. dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
  483. }
  484. if (bluetooth_rfkill) {
  485. dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
  486. dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
  487. }
  488. if (wwan_rfkill) {
  489. dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
  490. dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
  491. }
  492. release_buffer();
  493. }
  494. static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
  495. static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
  496. struct serio *port)
  497. {
  498. static bool extended;
  499. if (str & I8042_STR_AUXDATA)
  500. return false;
  501. if (unlikely(data == 0xe0)) {
  502. extended = true;
  503. return false;
  504. } else if (unlikely(extended)) {
  505. switch (data) {
  506. case 0x8:
  507. schedule_delayed_work(&dell_rfkill_work,
  508. round_jiffies_relative(HZ / 4));
  509. break;
  510. }
  511. extended = false;
  512. }
  513. return false;
  514. }
  515. static int __init dell_setup_rfkill(void)
  516. {
  517. int status, ret, whitelisted;
  518. const char *product;
  519. /*
  520. * rfkill support causes trouble on various models, mostly Inspirons.
  521. * So we whitelist certain series, and don't support rfkill on others.
  522. */
  523. whitelisted = 0;
  524. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  525. if (product && (strncmp(product, "Latitude", 8) == 0 ||
  526. strncmp(product, "Precision", 9) == 0))
  527. whitelisted = 1;
  528. if (!force_rfkill && !whitelisted)
  529. return 0;
  530. get_buffer();
  531. dell_send_request(buffer, 17, 11);
  532. status = buffer->output[1];
  533. buffer->input[0] = 0x2;
  534. dell_send_request(buffer, 17, 11);
  535. hwswitch_state = buffer->output[1];
  536. release_buffer();
  537. if (!(status & BIT(0))) {
  538. if (force_rfkill) {
  539. /* No hwsitch, clear all hw-controlled bits */
  540. hwswitch_state &= ~7;
  541. } else {
  542. /* rfkill is only tested on laptops with a hwswitch */
  543. return 0;
  544. }
  545. }
  546. if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
  547. wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
  548. RFKILL_TYPE_WLAN,
  549. &dell_rfkill_ops, (void *) 1);
  550. if (!wifi_rfkill) {
  551. ret = -ENOMEM;
  552. goto err_wifi;
  553. }
  554. ret = rfkill_register(wifi_rfkill);
  555. if (ret)
  556. goto err_wifi;
  557. }
  558. if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
  559. bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
  560. &platform_device->dev,
  561. RFKILL_TYPE_BLUETOOTH,
  562. &dell_rfkill_ops, (void *) 2);
  563. if (!bluetooth_rfkill) {
  564. ret = -ENOMEM;
  565. goto err_bluetooth;
  566. }
  567. ret = rfkill_register(bluetooth_rfkill);
  568. if (ret)
  569. goto err_bluetooth;
  570. }
  571. if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
  572. wwan_rfkill = rfkill_alloc("dell-wwan",
  573. &platform_device->dev,
  574. RFKILL_TYPE_WWAN,
  575. &dell_rfkill_ops, (void *) 3);
  576. if (!wwan_rfkill) {
  577. ret = -ENOMEM;
  578. goto err_wwan;
  579. }
  580. ret = rfkill_register(wwan_rfkill);
  581. if (ret)
  582. goto err_wwan;
  583. }
  584. ret = i8042_install_filter(dell_laptop_i8042_filter);
  585. if (ret) {
  586. pr_warn("Unable to install key filter\n");
  587. goto err_filter;
  588. }
  589. return 0;
  590. err_filter:
  591. if (wwan_rfkill)
  592. rfkill_unregister(wwan_rfkill);
  593. err_wwan:
  594. rfkill_destroy(wwan_rfkill);
  595. if (bluetooth_rfkill)
  596. rfkill_unregister(bluetooth_rfkill);
  597. err_bluetooth:
  598. rfkill_destroy(bluetooth_rfkill);
  599. if (wifi_rfkill)
  600. rfkill_unregister(wifi_rfkill);
  601. err_wifi:
  602. rfkill_destroy(wifi_rfkill);
  603. return ret;
  604. }
  605. static void dell_cleanup_rfkill(void)
  606. {
  607. if (wifi_rfkill) {
  608. rfkill_unregister(wifi_rfkill);
  609. rfkill_destroy(wifi_rfkill);
  610. }
  611. if (bluetooth_rfkill) {
  612. rfkill_unregister(bluetooth_rfkill);
  613. rfkill_destroy(bluetooth_rfkill);
  614. }
  615. if (wwan_rfkill) {
  616. rfkill_unregister(wwan_rfkill);
  617. rfkill_destroy(wwan_rfkill);
  618. }
  619. }
  620. static int dell_send_intensity(struct backlight_device *bd)
  621. {
  622. int ret = 0;
  623. get_buffer();
  624. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  625. buffer->input[1] = bd->props.brightness;
  626. if (buffer->input[0] == -1) {
  627. ret = -ENODEV;
  628. goto out;
  629. }
  630. if (power_supply_is_system_supplied() > 0)
  631. dell_send_request(buffer, 1, 2);
  632. else
  633. dell_send_request(buffer, 1, 1);
  634. out:
  635. release_buffer();
  636. return ret;
  637. }
  638. static int dell_get_intensity(struct backlight_device *bd)
  639. {
  640. int ret = 0;
  641. get_buffer();
  642. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  643. if (buffer->input[0] == -1) {
  644. ret = -ENODEV;
  645. goto out;
  646. }
  647. if (power_supply_is_system_supplied() > 0)
  648. dell_send_request(buffer, 0, 2);
  649. else
  650. dell_send_request(buffer, 0, 1);
  651. ret = buffer->output[1];
  652. out:
  653. release_buffer();
  654. return ret;
  655. }
  656. static const struct backlight_ops dell_ops = {
  657. .get_brightness = dell_get_intensity,
  658. .update_status = dell_send_intensity,
  659. };
  660. static void touchpad_led_on(void)
  661. {
  662. int command = 0x97;
  663. char data = 1;
  664. i8042_command(&data, command | 1 << 12);
  665. }
  666. static void touchpad_led_off(void)
  667. {
  668. int command = 0x97;
  669. char data = 2;
  670. i8042_command(&data, command | 1 << 12);
  671. }
  672. static void touchpad_led_set(struct led_classdev *led_cdev,
  673. enum led_brightness value)
  674. {
  675. if (value > 0)
  676. touchpad_led_on();
  677. else
  678. touchpad_led_off();
  679. }
  680. static struct led_classdev touchpad_led = {
  681. .name = "dell-laptop::touchpad",
  682. .brightness_set = touchpad_led_set,
  683. .flags = LED_CORE_SUSPENDRESUME,
  684. };
  685. static int __init touchpad_led_init(struct device *dev)
  686. {
  687. return led_classdev_register(dev, &touchpad_led);
  688. }
  689. static void touchpad_led_exit(void)
  690. {
  691. led_classdev_unregister(&touchpad_led);
  692. }
  693. static int __init dell_init(void)
  694. {
  695. int max_intensity = 0;
  696. int ret;
  697. if (!dmi_check_system(dell_device_table))
  698. return -ENODEV;
  699. quirks = NULL;
  700. /* find if this machine support other functions */
  701. dmi_check_system(dell_quirks);
  702. dmi_walk(find_tokens, NULL);
  703. if (!da_tokens) {
  704. pr_info("Unable to find dmi tokens\n");
  705. return -ENODEV;
  706. }
  707. ret = platform_driver_register(&platform_driver);
  708. if (ret)
  709. goto fail_platform_driver;
  710. platform_device = platform_device_alloc("dell-laptop", -1);
  711. if (!platform_device) {
  712. ret = -ENOMEM;
  713. goto fail_platform_device1;
  714. }
  715. ret = platform_device_add(platform_device);
  716. if (ret)
  717. goto fail_platform_device2;
  718. /*
  719. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  720. * is passed to SMI handler.
  721. */
  722. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  723. if (!bufferpage) {
  724. ret = -ENOMEM;
  725. goto fail_buffer;
  726. }
  727. buffer = page_address(bufferpage);
  728. ret = dell_setup_rfkill();
  729. if (ret) {
  730. pr_warn("Unable to setup rfkill\n");
  731. goto fail_rfkill;
  732. }
  733. if (quirks && quirks->touchpad_led)
  734. touchpad_led_init(&platform_device->dev);
  735. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  736. if (dell_laptop_dir != NULL)
  737. debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
  738. &dell_debugfs_fops);
  739. #ifdef CONFIG_ACPI
  740. /* In the event of an ACPI backlight being available, don't
  741. * register the platform controller.
  742. */
  743. if (acpi_video_backlight_support())
  744. return 0;
  745. #endif
  746. get_buffer();
  747. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  748. if (buffer->input[0] != -1) {
  749. dell_send_request(buffer, 0, 2);
  750. max_intensity = buffer->output[3];
  751. }
  752. release_buffer();
  753. if (max_intensity) {
  754. struct backlight_properties props;
  755. memset(&props, 0, sizeof(struct backlight_properties));
  756. props.type = BACKLIGHT_PLATFORM;
  757. props.max_brightness = max_intensity;
  758. dell_backlight_device = backlight_device_register("dell_backlight",
  759. &platform_device->dev,
  760. NULL,
  761. &dell_ops,
  762. &props);
  763. if (IS_ERR(dell_backlight_device)) {
  764. ret = PTR_ERR(dell_backlight_device);
  765. dell_backlight_device = NULL;
  766. goto fail_backlight;
  767. }
  768. dell_backlight_device->props.brightness =
  769. dell_get_intensity(dell_backlight_device);
  770. backlight_update_status(dell_backlight_device);
  771. }
  772. return 0;
  773. fail_backlight:
  774. i8042_remove_filter(dell_laptop_i8042_filter);
  775. cancel_delayed_work_sync(&dell_rfkill_work);
  776. dell_cleanup_rfkill();
  777. fail_rfkill:
  778. free_page((unsigned long)bufferpage);
  779. fail_buffer:
  780. platform_device_del(platform_device);
  781. fail_platform_device2:
  782. platform_device_put(platform_device);
  783. fail_platform_device1:
  784. platform_driver_unregister(&platform_driver);
  785. fail_platform_driver:
  786. kfree(da_tokens);
  787. return ret;
  788. }
  789. static void __exit dell_exit(void)
  790. {
  791. debugfs_remove_recursive(dell_laptop_dir);
  792. if (quirks && quirks->touchpad_led)
  793. touchpad_led_exit();
  794. i8042_remove_filter(dell_laptop_i8042_filter);
  795. cancel_delayed_work_sync(&dell_rfkill_work);
  796. backlight_device_unregister(dell_backlight_device);
  797. dell_cleanup_rfkill();
  798. if (platform_device) {
  799. platform_device_unregister(platform_device);
  800. platform_driver_unregister(&platform_driver);
  801. }
  802. kfree(da_tokens);
  803. free_page((unsigned long)buffer);
  804. }
  805. module_init(dell_init);
  806. module_exit(dell_exit);
  807. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  808. MODULE_DESCRIPTION("Dell laptop driver");
  809. MODULE_LICENSE("GPL");