dell-laptop.c 11 KB

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