dell-laptop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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_structure *table =
  267. container_of(dm, struct calling_interface_structure, header);
  268. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  269. 6 bytes of entry */
  270. if (dm->length < 17)
  271. return;
  272. da_command_address = table->cmdIOAddress;
  273. da_command_code = table->cmdIOCode;
  274. da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  275. sizeof(struct calling_interface_token),
  276. GFP_KERNEL);
  277. if (!da_tokens)
  278. return;
  279. memcpy(da_tokens+da_num_tokens, table->tokens,
  280. sizeof(struct calling_interface_token) * tokens);
  281. da_num_tokens += tokens;
  282. }
  283. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  284. {
  285. switch (dm->type) {
  286. case 0xd4: /* Indexed IO */
  287. case 0xd5: /* Protected Area Type 1 */
  288. case 0xd6: /* Protected Area Type 2 */
  289. break;
  290. case 0xda: /* Calling interface */
  291. parse_da_table(dm);
  292. break;
  293. }
  294. }
  295. static int find_token_location(int tokenid)
  296. {
  297. int i;
  298. for (i = 0; i < da_num_tokens; i++) {
  299. if (da_tokens[i].tokenID == tokenid)
  300. return da_tokens[i].location;
  301. }
  302. return -1;
  303. }
  304. static struct calling_interface_buffer *
  305. dell_send_request(struct calling_interface_buffer *buffer, int class,
  306. int select)
  307. {
  308. struct smi_cmd command;
  309. command.magic = SMI_CMD_MAGIC;
  310. command.command_address = da_command_address;
  311. command.command_code = da_command_code;
  312. command.ebx = virt_to_phys(buffer);
  313. command.ecx = 0x42534931;
  314. buffer->class = class;
  315. buffer->select = select;
  316. dcdbas_smi_request(&command);
  317. return buffer;
  318. }
  319. static struct dentry *dell_laptop_dir;
  320. static int dell_debugfs_show(struct seq_file *s, void *data)
  321. {
  322. int status;
  323. get_buffer();
  324. dell_send_request(buffer, 17, 11);
  325. status = buffer->output[1];
  326. release_buffer();
  327. seq_printf(s, "status:\t0x%X\n", status);
  328. seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
  329. status & BIT(0));
  330. seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
  331. (status & BIT(1)) >> 1);
  332. seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
  333. (status & BIT(2)) >> 2);
  334. seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
  335. (status & BIT(3)) >> 3);
  336. seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
  337. (status & BIT(4)) >> 4);
  338. seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
  339. (status & BIT(5)) >> 5);
  340. seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
  341. (status & BIT(8)) >> 8);
  342. seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
  343. (status & BIT(9)) >> 9);
  344. seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
  345. (status & BIT(10)) >> 10);
  346. seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
  347. (status & BIT(16)) >> 16);
  348. seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
  349. (status & BIT(17)) >> 17);
  350. seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
  351. (status & BIT(18)) >> 18);
  352. seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
  353. (status & BIT(19)) >> 19);
  354. seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
  355. seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
  356. hwswitch_state & BIT(0));
  357. seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
  358. (hwswitch_state & BIT(1)) >> 1);
  359. seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
  360. (hwswitch_state & BIT(2)) >> 2);
  361. seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
  362. (hwswitch_state & BIT(7)) >> 7);
  363. seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
  364. (hwswitch_state & BIT(8)) >> 8);
  365. seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
  366. (hwswitch_state & BIT(15)) >> 15);
  367. return 0;
  368. }
  369. static int dell_debugfs_open(struct inode *inode, struct file *file)
  370. {
  371. return single_open(file, dell_debugfs_show, inode->i_private);
  372. }
  373. static const struct file_operations dell_debugfs_fops = {
  374. .owner = THIS_MODULE,
  375. .open = dell_debugfs_open,
  376. .read = seq_read,
  377. .llseek = seq_lseek,
  378. .release = single_release,
  379. };
  380. static int dell_send_intensity(struct backlight_device *bd)
  381. {
  382. int ret = 0;
  383. get_buffer();
  384. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  385. buffer->input[1] = bd->props.brightness;
  386. if (buffer->input[0] == -1) {
  387. ret = -ENODEV;
  388. goto out;
  389. }
  390. if (power_supply_is_system_supplied() > 0)
  391. dell_send_request(buffer, 1, 2);
  392. else
  393. dell_send_request(buffer, 1, 1);
  394. out:
  395. release_buffer();
  396. return 0;
  397. }
  398. static int dell_get_intensity(struct backlight_device *bd)
  399. {
  400. int ret = 0;
  401. get_buffer();
  402. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  403. if (buffer->input[0] == -1) {
  404. ret = -ENODEV;
  405. goto out;
  406. }
  407. if (power_supply_is_system_supplied() > 0)
  408. dell_send_request(buffer, 0, 2);
  409. else
  410. dell_send_request(buffer, 0, 1);
  411. ret = buffer->output[1];
  412. out:
  413. release_buffer();
  414. return ret;
  415. }
  416. static const struct backlight_ops dell_ops = {
  417. .get_brightness = dell_get_intensity,
  418. .update_status = dell_send_intensity,
  419. };
  420. static void touchpad_led_on(void)
  421. {
  422. int command = 0x97;
  423. char data = 1;
  424. i8042_command(&data, command | 1 << 12);
  425. }
  426. static void touchpad_led_off(void)
  427. {
  428. int command = 0x97;
  429. char data = 2;
  430. i8042_command(&data, command | 1 << 12);
  431. }
  432. static void touchpad_led_set(struct led_classdev *led_cdev,
  433. enum led_brightness value)
  434. {
  435. if (value > 0)
  436. touchpad_led_on();
  437. else
  438. touchpad_led_off();
  439. }
  440. static struct led_classdev touchpad_led = {
  441. .name = "dell-laptop::touchpad",
  442. .brightness_set = touchpad_led_set,
  443. .flags = LED_CORE_SUSPENDRESUME,
  444. };
  445. static int touchpad_led_init(struct device *dev)
  446. {
  447. return led_classdev_register(dev, &touchpad_led);
  448. }
  449. static void touchpad_led_exit(void)
  450. {
  451. led_classdev_unregister(&touchpad_led);
  452. }
  453. static int __init dell_init(void)
  454. {
  455. int max_intensity = 0;
  456. int ret;
  457. if (!dmi_check_system(dell_device_table))
  458. return -ENODEV;
  459. quirks = NULL;
  460. /* find if this machine support other functions */
  461. dmi_check_system(dell_quirks);
  462. dmi_walk(find_tokens, NULL);
  463. if (!da_tokens) {
  464. pr_info("Unable to find dmi tokens\n");
  465. return -ENODEV;
  466. }
  467. ret = platform_driver_register(&platform_driver);
  468. if (ret)
  469. goto fail_platform_driver;
  470. platform_device = platform_device_alloc("dell-laptop", -1);
  471. if (!platform_device) {
  472. ret = -ENOMEM;
  473. goto fail_platform_device1;
  474. }
  475. ret = platform_device_add(platform_device);
  476. if (ret)
  477. goto fail_platform_device2;
  478. /*
  479. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  480. * is passed to SMI handler.
  481. */
  482. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  483. if (!bufferpage)
  484. goto fail_buffer;
  485. buffer = page_address(bufferpage);
  486. if (quirks && quirks->touchpad_led)
  487. touchpad_led_init(&platform_device->dev);
  488. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  489. #ifdef CONFIG_ACPI
  490. /* In the event of an ACPI backlight being available, don't
  491. * register the platform controller.
  492. */
  493. if (acpi_video_backlight_support())
  494. return 0;
  495. #endif
  496. get_buffer();
  497. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  498. if (buffer->input[0] != -1) {
  499. dell_send_request(buffer, 0, 2);
  500. max_intensity = buffer->output[3];
  501. }
  502. release_buffer();
  503. if (max_intensity) {
  504. struct backlight_properties props;
  505. memset(&props, 0, sizeof(struct backlight_properties));
  506. props.type = BACKLIGHT_PLATFORM;
  507. props.max_brightness = max_intensity;
  508. dell_backlight_device = backlight_device_register("dell_backlight",
  509. &platform_device->dev,
  510. NULL,
  511. &dell_ops,
  512. &props);
  513. if (IS_ERR(dell_backlight_device)) {
  514. ret = PTR_ERR(dell_backlight_device);
  515. dell_backlight_device = NULL;
  516. goto fail_backlight;
  517. }
  518. dell_backlight_device->props.brightness =
  519. dell_get_intensity(dell_backlight_device);
  520. backlight_update_status(dell_backlight_device);
  521. }
  522. return 0;
  523. fail_backlight:
  524. free_page((unsigned long)bufferpage);
  525. fail_buffer:
  526. platform_device_del(platform_device);
  527. fail_platform_device2:
  528. platform_device_put(platform_device);
  529. fail_platform_device1:
  530. platform_driver_unregister(&platform_driver);
  531. fail_platform_driver:
  532. kfree(da_tokens);
  533. return ret;
  534. }
  535. static void __exit dell_exit(void)
  536. {
  537. debugfs_remove_recursive(dell_laptop_dir);
  538. if (quirks && quirks->touchpad_led)
  539. touchpad_led_exit();
  540. backlight_device_unregister(dell_backlight_device);
  541. if (platform_device) {
  542. platform_device_unregister(platform_device);
  543. platform_driver_unregister(&platform_driver);
  544. }
  545. kfree(da_tokens);
  546. free_page((unsigned long)buffer);
  547. }
  548. module_init(dell_init);
  549. module_exit(dell_exit);
  550. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  551. MODULE_DESCRIPTION("Dell laptop driver");
  552. MODULE_LICENSE("GPL");