dell-laptop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/power_supply.h>
  23. #include <linux/acpi.h>
  24. #include <linux/mm.h>
  25. #include <linux/i8042.h>
  26. #include <linux/slab.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/seq_file.h>
  29. #include "../../firmware/dcdbas.h"
  30. #define BRIGHTNESS_TOKEN 0x7d
  31. /* This structure will be modified by the firmware when we enter
  32. * system management mode, hence the volatiles */
  33. struct calling_interface_buffer {
  34. u16 class;
  35. u16 select;
  36. volatile u32 input[4];
  37. volatile u32 output[4];
  38. } __packed;
  39. struct calling_interface_token {
  40. u16 tokenID;
  41. u16 location;
  42. union {
  43. u16 value;
  44. u16 stringlength;
  45. };
  46. };
  47. struct calling_interface_structure {
  48. struct dmi_header header;
  49. u16 cmdIOAddress;
  50. u8 cmdIOCode;
  51. u32 supportedCmds;
  52. struct calling_interface_token tokens[];
  53. } __packed;
  54. struct quirk_entry {
  55. u8 touchpad_led;
  56. };
  57. static struct quirk_entry *quirks;
  58. static struct quirk_entry quirk_dell_vostro_v130 = {
  59. .touchpad_led = 1,
  60. };
  61. static int dmi_matched(const struct dmi_system_id *dmi)
  62. {
  63. quirks = dmi->driver_data;
  64. return 1;
  65. }
  66. static int da_command_address;
  67. static int da_command_code;
  68. static int da_num_tokens;
  69. static struct calling_interface_token *da_tokens;
  70. static struct platform_driver platform_driver = {
  71. .driver = {
  72. .name = "dell-laptop",
  73. .owner = THIS_MODULE,
  74. }
  75. };
  76. static struct platform_device *platform_device;
  77. static struct backlight_device *dell_backlight_device;
  78. static const struct dmi_system_id dell_device_table[] __initconst = {
  79. {
  80. .ident = "Dell laptop",
  81. .matches = {
  82. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  83. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  84. },
  85. },
  86. {
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  89. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
  90. },
  91. },
  92. {
  93. .ident = "Dell Computer Corporation",
  94. .matches = {
  95. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  96. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  97. },
  98. },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(dmi, dell_device_table);
  102. static struct dmi_system_id dell_quirks[] = {
  103. {
  104. .callback = dmi_matched,
  105. .ident = "Dell Vostro V130",
  106. .matches = {
  107. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  108. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
  109. },
  110. .driver_data = &quirk_dell_vostro_v130,
  111. },
  112. {
  113. .callback = dmi_matched,
  114. .ident = "Dell Vostro V131",
  115. .matches = {
  116. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  117. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
  118. },
  119. .driver_data = &quirk_dell_vostro_v130,
  120. },
  121. {
  122. .callback = dmi_matched,
  123. .ident = "Dell Vostro 3350",
  124. .matches = {
  125. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  126. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
  127. },
  128. .driver_data = &quirk_dell_vostro_v130,
  129. },
  130. {
  131. .callback = dmi_matched,
  132. .ident = "Dell Vostro 3555",
  133. .matches = {
  134. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  135. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
  136. },
  137. .driver_data = &quirk_dell_vostro_v130,
  138. },
  139. {
  140. .callback = dmi_matched,
  141. .ident = "Dell Inspiron N311z",
  142. .matches = {
  143. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  144. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
  145. },
  146. .driver_data = &quirk_dell_vostro_v130,
  147. },
  148. {
  149. .callback = dmi_matched,
  150. .ident = "Dell Inspiron M5110",
  151. .matches = {
  152. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  153. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
  154. },
  155. .driver_data = &quirk_dell_vostro_v130,
  156. },
  157. {
  158. .callback = dmi_matched,
  159. .ident = "Dell Vostro 3360",
  160. .matches = {
  161. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  162. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
  163. },
  164. .driver_data = &quirk_dell_vostro_v130,
  165. },
  166. {
  167. .callback = dmi_matched,
  168. .ident = "Dell Vostro 3460",
  169. .matches = {
  170. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  171. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
  172. },
  173. .driver_data = &quirk_dell_vostro_v130,
  174. },
  175. {
  176. .callback = dmi_matched,
  177. .ident = "Dell Vostro 3560",
  178. .matches = {
  179. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  180. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
  181. },
  182. .driver_data = &quirk_dell_vostro_v130,
  183. },
  184. {
  185. .callback = dmi_matched,
  186. .ident = "Dell Vostro 3450",
  187. .matches = {
  188. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  189. DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
  190. },
  191. .driver_data = &quirk_dell_vostro_v130,
  192. },
  193. {
  194. .callback = dmi_matched,
  195. .ident = "Dell Inspiron 5420",
  196. .matches = {
  197. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  198. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
  199. },
  200. .driver_data = &quirk_dell_vostro_v130,
  201. },
  202. {
  203. .callback = dmi_matched,
  204. .ident = "Dell Inspiron 5520",
  205. .matches = {
  206. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  207. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
  208. },
  209. .driver_data = &quirk_dell_vostro_v130,
  210. },
  211. {
  212. .callback = dmi_matched,
  213. .ident = "Dell Inspiron 5720",
  214. .matches = {
  215. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  216. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
  217. },
  218. .driver_data = &quirk_dell_vostro_v130,
  219. },
  220. {
  221. .callback = dmi_matched,
  222. .ident = "Dell Inspiron 7420",
  223. .matches = {
  224. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  225. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
  226. },
  227. .driver_data = &quirk_dell_vostro_v130,
  228. },
  229. {
  230. .callback = dmi_matched,
  231. .ident = "Dell Inspiron 7520",
  232. .matches = {
  233. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  234. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
  235. },
  236. .driver_data = &quirk_dell_vostro_v130,
  237. },
  238. {
  239. .callback = dmi_matched,
  240. .ident = "Dell Inspiron 7720",
  241. .matches = {
  242. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  243. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
  244. },
  245. .driver_data = &quirk_dell_vostro_v130,
  246. },
  247. { }
  248. };
  249. static struct calling_interface_buffer *buffer;
  250. static struct page *bufferpage;
  251. static DEFINE_MUTEX(buffer_mutex);
  252. static int hwswitch_state;
  253. static void get_buffer(void)
  254. {
  255. mutex_lock(&buffer_mutex);
  256. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  257. }
  258. static void release_buffer(void)
  259. {
  260. mutex_unlock(&buffer_mutex);
  261. }
  262. static void __init parse_da_table(const struct dmi_header *dm)
  263. {
  264. /* Final token is a terminator, so we don't want to copy it */
  265. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  266. struct calling_interface_token *new_da_tokens;
  267. struct calling_interface_structure *table =
  268. container_of(dm, struct calling_interface_structure, header);
  269. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  270. 6 bytes of entry */
  271. if (dm->length < 17)
  272. return;
  273. da_command_address = table->cmdIOAddress;
  274. da_command_code = table->cmdIOCode;
  275. new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  276. sizeof(struct calling_interface_token),
  277. GFP_KERNEL);
  278. if (!new_da_tokens)
  279. return;
  280. da_tokens = new_da_tokens;
  281. memcpy(da_tokens+da_num_tokens, table->tokens,
  282. sizeof(struct calling_interface_token) * tokens);
  283. da_num_tokens += tokens;
  284. }
  285. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  286. {
  287. switch (dm->type) {
  288. case 0xd4: /* Indexed IO */
  289. case 0xd5: /* Protected Area Type 1 */
  290. case 0xd6: /* Protected Area Type 2 */
  291. break;
  292. case 0xda: /* Calling interface */
  293. parse_da_table(dm);
  294. break;
  295. }
  296. }
  297. static int find_token_location(int tokenid)
  298. {
  299. int i;
  300. for (i = 0; i < da_num_tokens; i++) {
  301. if (da_tokens[i].tokenID == tokenid)
  302. return da_tokens[i].location;
  303. }
  304. return -1;
  305. }
  306. static struct calling_interface_buffer *
  307. dell_send_request(struct calling_interface_buffer *buffer, int class,
  308. int select)
  309. {
  310. struct smi_cmd command;
  311. command.magic = SMI_CMD_MAGIC;
  312. command.command_address = da_command_address;
  313. command.command_code = da_command_code;
  314. command.ebx = virt_to_phys(buffer);
  315. command.ecx = 0x42534931;
  316. buffer->class = class;
  317. buffer->select = select;
  318. dcdbas_smi_request(&command);
  319. return buffer;
  320. }
  321. static struct dentry *dell_laptop_dir;
  322. static int dell_debugfs_show(struct seq_file *s, void *data)
  323. {
  324. int status;
  325. get_buffer();
  326. dell_send_request(buffer, 17, 11);
  327. status = buffer->output[1];
  328. release_buffer();
  329. seq_printf(s, "status:\t0x%X\n", status);
  330. seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
  331. status & BIT(0));
  332. seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
  333. (status & BIT(1)) >> 1);
  334. seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
  335. (status & BIT(2)) >> 2);
  336. seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
  337. (status & BIT(3)) >> 3);
  338. seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
  339. (status & BIT(4)) >> 4);
  340. seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
  341. (status & BIT(5)) >> 5);
  342. seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
  343. (status & BIT(8)) >> 8);
  344. seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
  345. (status & BIT(9)) >> 9);
  346. seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
  347. (status & BIT(10)) >> 10);
  348. seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
  349. (status & BIT(16)) >> 16);
  350. seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
  351. (status & BIT(17)) >> 17);
  352. seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
  353. (status & BIT(18)) >> 18);
  354. seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
  355. (status & BIT(19)) >> 19);
  356. seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
  357. seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
  358. hwswitch_state & BIT(0));
  359. seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
  360. (hwswitch_state & BIT(1)) >> 1);
  361. seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
  362. (hwswitch_state & BIT(2)) >> 2);
  363. seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
  364. (hwswitch_state & BIT(7)) >> 7);
  365. seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
  366. (hwswitch_state & BIT(8)) >> 8);
  367. seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
  368. (hwswitch_state & BIT(15)) >> 15);
  369. return 0;
  370. }
  371. static int dell_debugfs_open(struct inode *inode, struct file *file)
  372. {
  373. return single_open(file, dell_debugfs_show, inode->i_private);
  374. }
  375. static const struct file_operations dell_debugfs_fops = {
  376. .owner = THIS_MODULE,
  377. .open = dell_debugfs_open,
  378. .read = seq_read,
  379. .llseek = seq_lseek,
  380. .release = single_release,
  381. };
  382. static int dell_send_intensity(struct backlight_device *bd)
  383. {
  384. int ret = 0;
  385. get_buffer();
  386. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  387. buffer->input[1] = bd->props.brightness;
  388. if (buffer->input[0] == -1) {
  389. ret = -ENODEV;
  390. goto out;
  391. }
  392. if (power_supply_is_system_supplied() > 0)
  393. dell_send_request(buffer, 1, 2);
  394. else
  395. dell_send_request(buffer, 1, 1);
  396. out:
  397. release_buffer();
  398. return 0;
  399. }
  400. static int dell_get_intensity(struct backlight_device *bd)
  401. {
  402. int ret = 0;
  403. get_buffer();
  404. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  405. if (buffer->input[0] == -1) {
  406. ret = -ENODEV;
  407. goto out;
  408. }
  409. if (power_supply_is_system_supplied() > 0)
  410. dell_send_request(buffer, 0, 2);
  411. else
  412. dell_send_request(buffer, 0, 1);
  413. ret = buffer->output[1];
  414. out:
  415. release_buffer();
  416. return ret;
  417. }
  418. static const struct backlight_ops dell_ops = {
  419. .get_brightness = dell_get_intensity,
  420. .update_status = dell_send_intensity,
  421. };
  422. static void touchpad_led_on(void)
  423. {
  424. int command = 0x97;
  425. char data = 1;
  426. i8042_command(&data, command | 1 << 12);
  427. }
  428. static void touchpad_led_off(void)
  429. {
  430. int command = 0x97;
  431. char data = 2;
  432. i8042_command(&data, command | 1 << 12);
  433. }
  434. static void touchpad_led_set(struct led_classdev *led_cdev,
  435. enum led_brightness value)
  436. {
  437. if (value > 0)
  438. touchpad_led_on();
  439. else
  440. touchpad_led_off();
  441. }
  442. static struct led_classdev touchpad_led = {
  443. .name = "dell-laptop::touchpad",
  444. .brightness_set = touchpad_led_set,
  445. .flags = LED_CORE_SUSPENDRESUME,
  446. };
  447. static int touchpad_led_init(struct device *dev)
  448. {
  449. return led_classdev_register(dev, &touchpad_led);
  450. }
  451. static void touchpad_led_exit(void)
  452. {
  453. led_classdev_unregister(&touchpad_led);
  454. }
  455. static int __init dell_init(void)
  456. {
  457. int max_intensity = 0;
  458. int ret;
  459. if (!dmi_check_system(dell_device_table))
  460. return -ENODEV;
  461. quirks = NULL;
  462. /* find if this machine support other functions */
  463. dmi_check_system(dell_quirks);
  464. dmi_walk(find_tokens, NULL);
  465. if (!da_tokens) {
  466. pr_info("Unable to find dmi tokens\n");
  467. return -ENODEV;
  468. }
  469. ret = platform_driver_register(&platform_driver);
  470. if (ret)
  471. goto fail_platform_driver;
  472. platform_device = platform_device_alloc("dell-laptop", -1);
  473. if (!platform_device) {
  474. ret = -ENOMEM;
  475. goto fail_platform_device1;
  476. }
  477. ret = platform_device_add(platform_device);
  478. if (ret)
  479. goto fail_platform_device2;
  480. /*
  481. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  482. * is passed to SMI handler.
  483. */
  484. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  485. if (!bufferpage) {
  486. ret = -ENOMEM;
  487. goto fail_buffer;
  488. }
  489. buffer = page_address(bufferpage);
  490. if (quirks && quirks->touchpad_led)
  491. touchpad_led_init(&platform_device->dev);
  492. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  493. #ifdef CONFIG_ACPI
  494. /* In the event of an ACPI backlight being available, don't
  495. * register the platform controller.
  496. */
  497. if (acpi_video_backlight_support())
  498. return 0;
  499. #endif
  500. get_buffer();
  501. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  502. if (buffer->input[0] != -1) {
  503. dell_send_request(buffer, 0, 2);
  504. max_intensity = buffer->output[3];
  505. }
  506. release_buffer();
  507. if (max_intensity) {
  508. struct backlight_properties props;
  509. memset(&props, 0, sizeof(struct backlight_properties));
  510. props.type = BACKLIGHT_PLATFORM;
  511. props.max_brightness = max_intensity;
  512. dell_backlight_device = backlight_device_register("dell_backlight",
  513. &platform_device->dev,
  514. NULL,
  515. &dell_ops,
  516. &props);
  517. if (IS_ERR(dell_backlight_device)) {
  518. ret = PTR_ERR(dell_backlight_device);
  519. dell_backlight_device = NULL;
  520. goto fail_backlight;
  521. }
  522. dell_backlight_device->props.brightness =
  523. dell_get_intensity(dell_backlight_device);
  524. backlight_update_status(dell_backlight_device);
  525. }
  526. return 0;
  527. fail_backlight:
  528. free_page((unsigned long)bufferpage);
  529. fail_buffer:
  530. platform_device_del(platform_device);
  531. fail_platform_device2:
  532. platform_device_put(platform_device);
  533. fail_platform_device1:
  534. platform_driver_unregister(&platform_driver);
  535. fail_platform_driver:
  536. kfree(da_tokens);
  537. return ret;
  538. }
  539. static void __exit dell_exit(void)
  540. {
  541. debugfs_remove_recursive(dell_laptop_dir);
  542. if (quirks && quirks->touchpad_led)
  543. touchpad_led_exit();
  544. backlight_device_unregister(dell_backlight_device);
  545. if (platform_device) {
  546. platform_device_unregister(platform_device);
  547. platform_driver_unregister(&platform_driver);
  548. }
  549. kfree(da_tokens);
  550. free_page((unsigned long)buffer);
  551. }
  552. module_init(dell_init);
  553. module_exit(dell_exit);
  554. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  555. MODULE_DESCRIPTION("Dell laptop driver");
  556. MODULE_LICENSE("GPL");