dell-laptop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 __devinitdata 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. };
  195. static struct calling_interface_buffer *buffer;
  196. static struct page *bufferpage;
  197. static DEFINE_MUTEX(buffer_mutex);
  198. static int hwswitch_state;
  199. static void get_buffer(void)
  200. {
  201. mutex_lock(&buffer_mutex);
  202. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  203. }
  204. static void release_buffer(void)
  205. {
  206. mutex_unlock(&buffer_mutex);
  207. }
  208. static void __init parse_da_table(const struct dmi_header *dm)
  209. {
  210. /* Final token is a terminator, so we don't want to copy it */
  211. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  212. struct calling_interface_structure *table =
  213. container_of(dm, struct calling_interface_structure, header);
  214. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  215. 6 bytes of entry */
  216. if (dm->length < 17)
  217. return;
  218. da_command_address = table->cmdIOAddress;
  219. da_command_code = table->cmdIOCode;
  220. da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  221. sizeof(struct calling_interface_token),
  222. GFP_KERNEL);
  223. if (!da_tokens)
  224. return;
  225. memcpy(da_tokens+da_num_tokens, table->tokens,
  226. sizeof(struct calling_interface_token) * tokens);
  227. da_num_tokens += tokens;
  228. }
  229. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  230. {
  231. switch (dm->type) {
  232. case 0xd4: /* Indexed IO */
  233. case 0xd5: /* Protected Area Type 1 */
  234. case 0xd6: /* Protected Area Type 2 */
  235. break;
  236. case 0xda: /* Calling interface */
  237. parse_da_table(dm);
  238. break;
  239. }
  240. }
  241. static int find_token_location(int tokenid)
  242. {
  243. int i;
  244. for (i = 0; i < da_num_tokens; i++) {
  245. if (da_tokens[i].tokenID == tokenid)
  246. return da_tokens[i].location;
  247. }
  248. return -1;
  249. }
  250. static struct calling_interface_buffer *
  251. dell_send_request(struct calling_interface_buffer *buffer, int class,
  252. int select)
  253. {
  254. struct smi_cmd command;
  255. command.magic = SMI_CMD_MAGIC;
  256. command.command_address = da_command_address;
  257. command.command_code = da_command_code;
  258. command.ebx = virt_to_phys(buffer);
  259. command.ecx = 0x42534931;
  260. buffer->class = class;
  261. buffer->select = select;
  262. dcdbas_smi_request(&command);
  263. return buffer;
  264. }
  265. static struct dentry *dell_laptop_dir;
  266. static int dell_debugfs_show(struct seq_file *s, void *data)
  267. {
  268. int status;
  269. get_buffer();
  270. dell_send_request(buffer, 17, 11);
  271. status = buffer->output[1];
  272. release_buffer();
  273. seq_printf(s, "status:\t0x%X\n", status);
  274. seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
  275. status & BIT(0));
  276. seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
  277. (status & BIT(1)) >> 1);
  278. seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
  279. (status & BIT(2)) >> 2);
  280. seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
  281. (status & BIT(3)) >> 3);
  282. seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
  283. (status & BIT(4)) >> 4);
  284. seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
  285. (status & BIT(5)) >> 5);
  286. seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
  287. (status & BIT(8)) >> 8);
  288. seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
  289. (status & BIT(9)) >> 9);
  290. seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
  291. (status & BIT(10)) >> 10);
  292. seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
  293. (status & BIT(16)) >> 16);
  294. seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
  295. (status & BIT(17)) >> 17);
  296. seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
  297. (status & BIT(18)) >> 18);
  298. seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
  299. (status & BIT(19)) >> 19);
  300. seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
  301. seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
  302. hwswitch_state & BIT(0));
  303. seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
  304. (hwswitch_state & BIT(1)) >> 1);
  305. seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
  306. (hwswitch_state & BIT(2)) >> 2);
  307. seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
  308. (hwswitch_state & BIT(7)) >> 7);
  309. seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
  310. (hwswitch_state & BIT(8)) >> 8);
  311. seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
  312. (hwswitch_state & BIT(15)) >> 15);
  313. return 0;
  314. }
  315. static int dell_debugfs_open(struct inode *inode, struct file *file)
  316. {
  317. return single_open(file, dell_debugfs_show, inode->i_private);
  318. }
  319. static const struct file_operations dell_debugfs_fops = {
  320. .owner = THIS_MODULE,
  321. .open = dell_debugfs_open,
  322. .read = seq_read,
  323. .llseek = seq_lseek,
  324. .release = single_release,
  325. };
  326. static int dell_send_intensity(struct backlight_device *bd)
  327. {
  328. int ret = 0;
  329. get_buffer();
  330. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  331. buffer->input[1] = bd->props.brightness;
  332. if (buffer->input[0] == -1) {
  333. ret = -ENODEV;
  334. goto out;
  335. }
  336. if (power_supply_is_system_supplied() > 0)
  337. dell_send_request(buffer, 1, 2);
  338. else
  339. dell_send_request(buffer, 1, 1);
  340. out:
  341. release_buffer();
  342. return 0;
  343. }
  344. static int dell_get_intensity(struct backlight_device *bd)
  345. {
  346. int ret = 0;
  347. get_buffer();
  348. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  349. if (buffer->input[0] == -1) {
  350. ret = -ENODEV;
  351. goto out;
  352. }
  353. if (power_supply_is_system_supplied() > 0)
  354. dell_send_request(buffer, 0, 2);
  355. else
  356. dell_send_request(buffer, 0, 1);
  357. ret = buffer->output[1];
  358. out:
  359. release_buffer();
  360. return ret;
  361. }
  362. static const struct backlight_ops dell_ops = {
  363. .get_brightness = dell_get_intensity,
  364. .update_status = dell_send_intensity,
  365. };
  366. static void touchpad_led_on(void)
  367. {
  368. int command = 0x97;
  369. char data = 1;
  370. i8042_command(&data, command | 1 << 12);
  371. }
  372. static void touchpad_led_off(void)
  373. {
  374. int command = 0x97;
  375. char data = 2;
  376. i8042_command(&data, command | 1 << 12);
  377. }
  378. static void touchpad_led_set(struct led_classdev *led_cdev,
  379. enum led_brightness value)
  380. {
  381. if (value > 0)
  382. touchpad_led_on();
  383. else
  384. touchpad_led_off();
  385. }
  386. static struct led_classdev touchpad_led = {
  387. .name = "dell-laptop::touchpad",
  388. .brightness_set = touchpad_led_set,
  389. .flags = LED_CORE_SUSPENDRESUME,
  390. };
  391. static int __devinit touchpad_led_init(struct device *dev)
  392. {
  393. return led_classdev_register(dev, &touchpad_led);
  394. }
  395. static void touchpad_led_exit(void)
  396. {
  397. led_classdev_unregister(&touchpad_led);
  398. }
  399. static int __init dell_init(void)
  400. {
  401. int max_intensity = 0;
  402. int ret;
  403. if (!dmi_check_system(dell_device_table))
  404. return -ENODEV;
  405. quirks = NULL;
  406. /* find if this machine support other functions */
  407. dmi_check_system(dell_quirks);
  408. dmi_walk(find_tokens, NULL);
  409. if (!da_tokens) {
  410. pr_info("Unable to find dmi tokens\n");
  411. return -ENODEV;
  412. }
  413. ret = platform_driver_register(&platform_driver);
  414. if (ret)
  415. goto fail_platform_driver;
  416. platform_device = platform_device_alloc("dell-laptop", -1);
  417. if (!platform_device) {
  418. ret = -ENOMEM;
  419. goto fail_platform_device1;
  420. }
  421. ret = platform_device_add(platform_device);
  422. if (ret)
  423. goto fail_platform_device2;
  424. /*
  425. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  426. * is passed to SMI handler.
  427. */
  428. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  429. if (!bufferpage)
  430. goto fail_buffer;
  431. buffer = page_address(bufferpage);
  432. if (quirks && quirks->touchpad_led)
  433. touchpad_led_init(&platform_device->dev);
  434. dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
  435. #ifdef CONFIG_ACPI
  436. /* In the event of an ACPI backlight being available, don't
  437. * register the platform controller.
  438. */
  439. if (acpi_video_backlight_support())
  440. return 0;
  441. #endif
  442. get_buffer();
  443. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  444. if (buffer->input[0] != -1) {
  445. dell_send_request(buffer, 0, 2);
  446. max_intensity = buffer->output[3];
  447. }
  448. release_buffer();
  449. if (max_intensity) {
  450. struct backlight_properties props;
  451. memset(&props, 0, sizeof(struct backlight_properties));
  452. props.type = BACKLIGHT_PLATFORM;
  453. props.max_brightness = max_intensity;
  454. dell_backlight_device = backlight_device_register("dell_backlight",
  455. &platform_device->dev,
  456. NULL,
  457. &dell_ops,
  458. &props);
  459. if (IS_ERR(dell_backlight_device)) {
  460. ret = PTR_ERR(dell_backlight_device);
  461. dell_backlight_device = NULL;
  462. goto fail_backlight;
  463. }
  464. dell_backlight_device->props.brightness =
  465. dell_get_intensity(dell_backlight_device);
  466. backlight_update_status(dell_backlight_device);
  467. }
  468. return 0;
  469. fail_backlight:
  470. free_page((unsigned long)bufferpage);
  471. fail_buffer:
  472. platform_device_del(platform_device);
  473. fail_platform_device2:
  474. platform_device_put(platform_device);
  475. fail_platform_device1:
  476. platform_driver_unregister(&platform_driver);
  477. fail_platform_driver:
  478. kfree(da_tokens);
  479. return ret;
  480. }
  481. static void __exit dell_exit(void)
  482. {
  483. debugfs_remove_recursive(dell_laptop_dir);
  484. if (quirks && quirks->touchpad_led)
  485. touchpad_led_exit();
  486. backlight_device_unregister(dell_backlight_device);
  487. if (platform_device) {
  488. platform_device_unregister(platform_device);
  489. platform_driver_unregister(&platform_driver);
  490. }
  491. kfree(da_tokens);
  492. free_page((unsigned long)buffer);
  493. }
  494. module_init(dell_init);
  495. module_exit(dell_exit);
  496. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  497. MODULE_DESCRIPTION("Dell laptop driver");
  498. MODULE_LICENSE("GPL");