dell-laptop.c 14 KB

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