dell-laptop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/backlight.h>
  18. #include <linux/err.h>
  19. #include <linux/dmi.h>
  20. #include <linux/io.h>
  21. #include <linux/rfkill.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 "../../firmware/dcdbas.h"
  28. #define BRIGHTNESS_TOKEN 0x7d
  29. /* This structure will be modified by the firmware when we enter
  30. * system management mode, hence the volatiles */
  31. struct calling_interface_buffer {
  32. u16 class;
  33. u16 select;
  34. volatile u32 input[4];
  35. volatile u32 output[4];
  36. } __packed;
  37. struct calling_interface_token {
  38. u16 tokenID;
  39. u16 location;
  40. union {
  41. u16 value;
  42. u16 stringlength;
  43. };
  44. };
  45. struct calling_interface_structure {
  46. struct dmi_header header;
  47. u16 cmdIOAddress;
  48. u8 cmdIOCode;
  49. u32 supportedCmds;
  50. struct calling_interface_token tokens[];
  51. } __packed;
  52. static int da_command_address;
  53. static int da_command_code;
  54. static int da_num_tokens;
  55. static struct calling_interface_token *da_tokens;
  56. static struct platform_driver platform_driver = {
  57. .driver = {
  58. .name = "dell-laptop",
  59. .owner = THIS_MODULE,
  60. }
  61. };
  62. static struct platform_device *platform_device;
  63. static struct backlight_device *dell_backlight_device;
  64. static struct rfkill *wifi_rfkill;
  65. static struct rfkill *bluetooth_rfkill;
  66. static struct rfkill *wwan_rfkill;
  67. static const struct dmi_system_id __initdata dell_device_table[] = {
  68. {
  69. .ident = "Dell laptop",
  70. .matches = {
  71. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  72. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  73. },
  74. },
  75. {
  76. .matches = {
  77. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  78. DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
  79. },
  80. },
  81. {
  82. .ident = "Dell Computer Corporation",
  83. .matches = {
  84. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  85. DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
  86. },
  87. },
  88. { }
  89. };
  90. static struct dmi_system_id __devinitdata dell_blacklist[] = {
  91. /* Supported by compal-laptop */
  92. {
  93. .ident = "Dell Mini 9",
  94. .matches = {
  95. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  96. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
  97. },
  98. },
  99. {
  100. .ident = "Dell Mini 10",
  101. .matches = {
  102. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  103. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
  104. },
  105. },
  106. {
  107. .ident = "Dell Mini 10v",
  108. .matches = {
  109. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  110. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
  111. },
  112. },
  113. {
  114. .ident = "Dell Inspiron 11z",
  115. .matches = {
  116. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  117. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
  118. },
  119. },
  120. {
  121. .ident = "Dell Mini 12",
  122. .matches = {
  123. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  124. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
  125. },
  126. },
  127. {}
  128. };
  129. static struct calling_interface_buffer *buffer;
  130. static struct page *bufferpage;
  131. static DEFINE_MUTEX(buffer_mutex);
  132. static int hwswitch_state;
  133. static void get_buffer(void)
  134. {
  135. mutex_lock(&buffer_mutex);
  136. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  137. }
  138. static void release_buffer(void)
  139. {
  140. mutex_unlock(&buffer_mutex);
  141. }
  142. static void __init parse_da_table(const struct dmi_header *dm)
  143. {
  144. /* Final token is a terminator, so we don't want to copy it */
  145. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  146. struct calling_interface_structure *table =
  147. container_of(dm, struct calling_interface_structure, header);
  148. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  149. 6 bytes of entry */
  150. if (dm->length < 17)
  151. return;
  152. da_command_address = table->cmdIOAddress;
  153. da_command_code = table->cmdIOCode;
  154. da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  155. sizeof(struct calling_interface_token),
  156. GFP_KERNEL);
  157. if (!da_tokens)
  158. return;
  159. memcpy(da_tokens+da_num_tokens, table->tokens,
  160. sizeof(struct calling_interface_token) * tokens);
  161. da_num_tokens += tokens;
  162. }
  163. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  164. {
  165. switch (dm->type) {
  166. case 0xd4: /* Indexed IO */
  167. break;
  168. case 0xd5: /* Protected Area Type 1 */
  169. break;
  170. case 0xd6: /* Protected Area Type 2 */
  171. break;
  172. case 0xda: /* Calling interface */
  173. parse_da_table(dm);
  174. break;
  175. }
  176. }
  177. static int find_token_location(int tokenid)
  178. {
  179. int i;
  180. for (i = 0; i < da_num_tokens; i++) {
  181. if (da_tokens[i].tokenID == tokenid)
  182. return da_tokens[i].location;
  183. }
  184. return -1;
  185. }
  186. static struct calling_interface_buffer *
  187. dell_send_request(struct calling_interface_buffer *buffer, int class,
  188. int select)
  189. {
  190. struct smi_cmd command;
  191. command.magic = SMI_CMD_MAGIC;
  192. command.command_address = da_command_address;
  193. command.command_code = da_command_code;
  194. command.ebx = virt_to_phys(buffer);
  195. command.ecx = 0x42534931;
  196. buffer->class = class;
  197. buffer->select = select;
  198. dcdbas_smi_request(&command);
  199. return buffer;
  200. }
  201. /* Derived from information in DellWirelessCtl.cpp:
  202. Class 17, select 11 is radio control. It returns an array of 32-bit values.
  203. Input byte 0 = 0: Wireless information
  204. result[0]: return code
  205. result[1]:
  206. Bit 0: Hardware switch supported
  207. Bit 1: Wifi locator supported
  208. Bit 2: Wifi is supported
  209. Bit 3: Bluetooth is supported
  210. Bit 4: WWAN is supported
  211. Bit 5: Wireless keyboard supported
  212. Bits 6-7: Reserved
  213. Bit 8: Wifi is installed
  214. Bit 9: Bluetooth is installed
  215. Bit 10: WWAN is installed
  216. Bits 11-15: Reserved
  217. Bit 16: Hardware switch is on
  218. Bit 17: Wifi is blocked
  219. Bit 18: Bluetooth is blocked
  220. Bit 19: WWAN is blocked
  221. Bits 20-31: Reserved
  222. result[2]: NVRAM size in bytes
  223. result[3]: NVRAM format version number
  224. Input byte 0 = 2: Wireless switch configuration
  225. result[0]: return code
  226. result[1]:
  227. Bit 0: Wifi controlled by switch
  228. Bit 1: Bluetooth controlled by switch
  229. Bit 2: WWAN controlled by switch
  230. Bits 3-6: Reserved
  231. Bit 7: Wireless switch config locked
  232. Bit 8: Wifi locator enabled
  233. Bits 9-14: Reserved
  234. Bit 15: Wifi locator setting locked
  235. Bits 16-31: Reserved
  236. */
  237. static int dell_rfkill_set(void *data, bool blocked)
  238. {
  239. int disable = blocked ? 1 : 0;
  240. unsigned long radio = (unsigned long)data;
  241. int hwswitch_bit = (unsigned long)data - 1;
  242. int ret = 0;
  243. get_buffer();
  244. dell_send_request(buffer, 17, 11);
  245. /* If the hardware switch controls this radio, and the hardware
  246. switch is disabled, don't allow changing the software state */
  247. if ((hwswitch_state & BIT(hwswitch_bit)) &&
  248. !(buffer->output[1] & BIT(16))) {
  249. ret = -EINVAL;
  250. goto out;
  251. }
  252. buffer->input[0] = (1 | (radio<<8) | (disable << 16));
  253. dell_send_request(buffer, 17, 11);
  254. out:
  255. release_buffer();
  256. return ret;
  257. }
  258. static void dell_rfkill_query(struct rfkill *rfkill, void *data)
  259. {
  260. int status;
  261. int bit = (unsigned long)data + 16;
  262. int hwswitch_bit = (unsigned long)data - 1;
  263. get_buffer();
  264. dell_send_request(buffer, 17, 11);
  265. status = buffer->output[1];
  266. release_buffer();
  267. rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
  268. if (hwswitch_state & (BIT(hwswitch_bit)))
  269. rfkill_set_hw_state(rfkill, !(status & BIT(16)));
  270. }
  271. static const struct rfkill_ops dell_rfkill_ops = {
  272. .set_block = dell_rfkill_set,
  273. .query = dell_rfkill_query,
  274. };
  275. static void dell_update_rfkill(struct work_struct *ignored)
  276. {
  277. if (wifi_rfkill)
  278. dell_rfkill_query(wifi_rfkill, (void *)1);
  279. if (bluetooth_rfkill)
  280. dell_rfkill_query(bluetooth_rfkill, (void *)2);
  281. if (wwan_rfkill)
  282. dell_rfkill_query(wwan_rfkill, (void *)3);
  283. }
  284. static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
  285. static int __init dell_setup_rfkill(void)
  286. {
  287. int status;
  288. int ret;
  289. if (dmi_check_system(dell_blacklist)) {
  290. printk(KERN_INFO "dell-laptop: Blacklisted hardware detected - "
  291. "not enabling rfkill\n");
  292. return 0;
  293. }
  294. get_buffer();
  295. dell_send_request(buffer, 17, 11);
  296. status = buffer->output[1];
  297. buffer->input[0] = 0x2;
  298. dell_send_request(buffer, 17, 11);
  299. hwswitch_state = buffer->output[1];
  300. release_buffer();
  301. if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
  302. wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
  303. RFKILL_TYPE_WLAN,
  304. &dell_rfkill_ops, (void *) 1);
  305. if (!wifi_rfkill) {
  306. ret = -ENOMEM;
  307. goto err_wifi;
  308. }
  309. ret = rfkill_register(wifi_rfkill);
  310. if (ret)
  311. goto err_wifi;
  312. }
  313. if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
  314. bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
  315. &platform_device->dev,
  316. RFKILL_TYPE_BLUETOOTH,
  317. &dell_rfkill_ops, (void *) 2);
  318. if (!bluetooth_rfkill) {
  319. ret = -ENOMEM;
  320. goto err_bluetooth;
  321. }
  322. ret = rfkill_register(bluetooth_rfkill);
  323. if (ret)
  324. goto err_bluetooth;
  325. }
  326. if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
  327. wwan_rfkill = rfkill_alloc("dell-wwan",
  328. &platform_device->dev,
  329. RFKILL_TYPE_WWAN,
  330. &dell_rfkill_ops, (void *) 3);
  331. if (!wwan_rfkill) {
  332. ret = -ENOMEM;
  333. goto err_wwan;
  334. }
  335. ret = rfkill_register(wwan_rfkill);
  336. if (ret)
  337. goto err_wwan;
  338. }
  339. return 0;
  340. err_wwan:
  341. rfkill_destroy(wwan_rfkill);
  342. if (bluetooth_rfkill)
  343. rfkill_unregister(bluetooth_rfkill);
  344. err_bluetooth:
  345. rfkill_destroy(bluetooth_rfkill);
  346. if (wifi_rfkill)
  347. rfkill_unregister(wifi_rfkill);
  348. err_wifi:
  349. rfkill_destroy(wifi_rfkill);
  350. return ret;
  351. }
  352. static void dell_cleanup_rfkill(void)
  353. {
  354. if (wifi_rfkill) {
  355. rfkill_unregister(wifi_rfkill);
  356. rfkill_destroy(wifi_rfkill);
  357. }
  358. if (bluetooth_rfkill) {
  359. rfkill_unregister(bluetooth_rfkill);
  360. rfkill_destroy(bluetooth_rfkill);
  361. }
  362. if (wwan_rfkill) {
  363. rfkill_unregister(wwan_rfkill);
  364. rfkill_destroy(wwan_rfkill);
  365. }
  366. }
  367. static int dell_send_intensity(struct backlight_device *bd)
  368. {
  369. int ret = 0;
  370. get_buffer();
  371. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  372. buffer->input[1] = bd->props.brightness;
  373. if (buffer->input[0] == -1) {
  374. ret = -ENODEV;
  375. goto out;
  376. }
  377. if (power_supply_is_system_supplied() > 0)
  378. dell_send_request(buffer, 1, 2);
  379. else
  380. dell_send_request(buffer, 1, 1);
  381. out:
  382. release_buffer();
  383. return 0;
  384. }
  385. static int dell_get_intensity(struct backlight_device *bd)
  386. {
  387. int ret = 0;
  388. get_buffer();
  389. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  390. if (buffer->input[0] == -1) {
  391. ret = -ENODEV;
  392. goto out;
  393. }
  394. if (power_supply_is_system_supplied() > 0)
  395. dell_send_request(buffer, 0, 2);
  396. else
  397. dell_send_request(buffer, 0, 1);
  398. out:
  399. release_buffer();
  400. if (ret)
  401. return ret;
  402. return buffer->output[1];
  403. }
  404. static struct backlight_ops dell_ops = {
  405. .get_brightness = dell_get_intensity,
  406. .update_status = dell_send_intensity,
  407. };
  408. static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
  409. struct serio *port)
  410. {
  411. static bool extended;
  412. if (str & 0x20)
  413. return false;
  414. if (unlikely(data == 0xe0)) {
  415. extended = true;
  416. return false;
  417. } else if (unlikely(extended)) {
  418. switch (data) {
  419. case 0x8:
  420. schedule_delayed_work(&dell_rfkill_work,
  421. round_jiffies_relative(HZ));
  422. break;
  423. }
  424. extended = false;
  425. }
  426. return false;
  427. }
  428. static int __init dell_init(void)
  429. {
  430. int max_intensity = 0;
  431. int ret;
  432. if (!dmi_check_system(dell_device_table))
  433. return -ENODEV;
  434. dmi_walk(find_tokens, NULL);
  435. if (!da_tokens) {
  436. printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
  437. return -ENODEV;
  438. }
  439. ret = platform_driver_register(&platform_driver);
  440. if (ret)
  441. goto fail_platform_driver;
  442. platform_device = platform_device_alloc("dell-laptop", -1);
  443. if (!platform_device) {
  444. ret = -ENOMEM;
  445. goto fail_platform_device1;
  446. }
  447. ret = platform_device_add(platform_device);
  448. if (ret)
  449. goto fail_platform_device2;
  450. /*
  451. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  452. * is passed to SMI handler.
  453. */
  454. bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
  455. if (!bufferpage)
  456. goto fail_buffer;
  457. buffer = page_address(bufferpage);
  458. mutex_init(&buffer_mutex);
  459. ret = dell_setup_rfkill();
  460. if (ret) {
  461. printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
  462. goto fail_rfkill;
  463. }
  464. ret = i8042_install_filter(dell_laptop_i8042_filter);
  465. if (ret) {
  466. printk(KERN_WARNING
  467. "dell-laptop: Unable to install key filter\n");
  468. goto fail_filter;
  469. }
  470. #ifdef CONFIG_ACPI
  471. /* In the event of an ACPI backlight being available, don't
  472. * register the platform controller.
  473. */
  474. if (acpi_video_backlight_support())
  475. return 0;
  476. #endif
  477. get_buffer();
  478. buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
  479. if (buffer->input[0] != -1) {
  480. dell_send_request(buffer, 0, 2);
  481. max_intensity = buffer->output[3];
  482. }
  483. release_buffer();
  484. if (max_intensity) {
  485. struct backlight_properties props;
  486. memset(&props, 0, sizeof(struct backlight_properties));
  487. props.max_brightness = max_intensity;
  488. dell_backlight_device = backlight_device_register("dell_backlight",
  489. &platform_device->dev,
  490. NULL,
  491. &dell_ops,
  492. &props);
  493. if (IS_ERR(dell_backlight_device)) {
  494. ret = PTR_ERR(dell_backlight_device);
  495. dell_backlight_device = NULL;
  496. goto fail_backlight;
  497. }
  498. dell_backlight_device->props.brightness =
  499. dell_get_intensity(dell_backlight_device);
  500. backlight_update_status(dell_backlight_device);
  501. }
  502. return 0;
  503. fail_backlight:
  504. i8042_remove_filter(dell_laptop_i8042_filter);
  505. cancel_delayed_work_sync(&dell_rfkill_work);
  506. fail_filter:
  507. dell_cleanup_rfkill();
  508. fail_rfkill:
  509. free_page((unsigned long)bufferpage);
  510. fail_buffer:
  511. platform_device_del(platform_device);
  512. fail_platform_device2:
  513. platform_device_put(platform_device);
  514. fail_platform_device1:
  515. platform_driver_unregister(&platform_driver);
  516. fail_platform_driver:
  517. kfree(da_tokens);
  518. return ret;
  519. }
  520. static void __exit dell_exit(void)
  521. {
  522. i8042_remove_filter(dell_laptop_i8042_filter);
  523. cancel_delayed_work_sync(&dell_rfkill_work);
  524. backlight_device_unregister(dell_backlight_device);
  525. dell_cleanup_rfkill();
  526. if (platform_device) {
  527. platform_device_unregister(platform_device);
  528. platform_driver_unregister(&platform_driver);
  529. }
  530. kfree(da_tokens);
  531. free_page((unsigned long)buffer);
  532. }
  533. module_init(dell_init);
  534. module_exit(dell_exit);
  535. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  536. MODULE_DESCRIPTION("Dell laptop driver");
  537. MODULE_LICENSE("GPL");
  538. MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
  539. MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
  540. MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");