dell-laptop.c 22 KB

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