dell-laptop.c 9.3 KB

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