dell-laptop.c 14 KB

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