dell-laptop.c 14 KB

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