dell-laptop.c 14 KB

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