dell-laptop.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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(int radio, enum rfkill_state state)
  150. {
  151. struct calling_interface_buffer buffer;
  152. int disable = (state == RFKILL_STATE_UNBLOCKED) ? 0 : 1;
  153. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  154. buffer.input[0] = (1 | (radio<<8) | (disable << 16));
  155. dell_send_request(&buffer, 17, 11);
  156. return 0;
  157. }
  158. static int dell_wifi_set(void *data, enum rfkill_state state)
  159. {
  160. return dell_rfkill_set(1, state);
  161. }
  162. static int dell_bluetooth_set(void *data, enum rfkill_state state)
  163. {
  164. return dell_rfkill_set(2, state);
  165. }
  166. static int dell_wwan_set(void *data, enum rfkill_state state)
  167. {
  168. return dell_rfkill_set(3, state);
  169. }
  170. static int dell_rfkill_get(int bit, enum rfkill_state *state)
  171. {
  172. struct calling_interface_buffer buffer;
  173. int status;
  174. int new_state = RFKILL_STATE_HARD_BLOCKED;
  175. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  176. dell_send_request(&buffer, 17, 11);
  177. status = buffer.output[1];
  178. if (status & (1<<16))
  179. new_state = RFKILL_STATE_SOFT_BLOCKED;
  180. if (status & (1<<bit))
  181. *state = new_state;
  182. else
  183. *state = RFKILL_STATE_UNBLOCKED;
  184. return 0;
  185. }
  186. static int dell_wifi_get(void *data, enum rfkill_state *state)
  187. {
  188. return dell_rfkill_get(17, state);
  189. }
  190. static int dell_bluetooth_get(void *data, enum rfkill_state *state)
  191. {
  192. return dell_rfkill_get(18, state);
  193. }
  194. static int dell_wwan_get(void *data, enum rfkill_state *state)
  195. {
  196. return dell_rfkill_get(19, state);
  197. }
  198. static int dell_setup_rfkill(void)
  199. {
  200. struct calling_interface_buffer buffer;
  201. int status;
  202. int ret;
  203. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  204. dell_send_request(&buffer, 17, 11);
  205. status = buffer.output[1];
  206. if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
  207. wifi_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_WLAN);
  208. if (!wifi_rfkill)
  209. goto err_wifi;
  210. wifi_rfkill->name = "dell-wifi";
  211. wifi_rfkill->toggle_radio = dell_wifi_set;
  212. wifi_rfkill->get_state = dell_wifi_get;
  213. ret = rfkill_register(wifi_rfkill);
  214. if (ret)
  215. goto err_wifi;
  216. }
  217. if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
  218. bluetooth_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_BLUETOOTH);
  219. if (!bluetooth_rfkill)
  220. goto err_bluetooth;
  221. bluetooth_rfkill->name = "dell-bluetooth";
  222. bluetooth_rfkill->toggle_radio = dell_bluetooth_set;
  223. bluetooth_rfkill->get_state = dell_bluetooth_get;
  224. ret = rfkill_register(bluetooth_rfkill);
  225. if (ret)
  226. goto err_bluetooth;
  227. }
  228. if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
  229. wwan_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_WWAN);
  230. if (!wwan_rfkill)
  231. goto err_wwan;
  232. wwan_rfkill->name = "dell-wwan";
  233. wwan_rfkill->toggle_radio = dell_wwan_set;
  234. wwan_rfkill->get_state = dell_wwan_get;
  235. ret = rfkill_register(wwan_rfkill);
  236. if (ret)
  237. goto err_wwan;
  238. }
  239. return 0;
  240. err_wwan:
  241. if (wwan_rfkill)
  242. rfkill_free(wwan_rfkill);
  243. if (bluetooth_rfkill) {
  244. rfkill_unregister(bluetooth_rfkill);
  245. bluetooth_rfkill = NULL;
  246. }
  247. err_bluetooth:
  248. if (bluetooth_rfkill)
  249. rfkill_free(bluetooth_rfkill);
  250. if (wifi_rfkill) {
  251. rfkill_unregister(wifi_rfkill);
  252. wifi_rfkill = NULL;
  253. }
  254. err_wifi:
  255. if (wifi_rfkill)
  256. rfkill_free(wifi_rfkill);
  257. return ret;
  258. }
  259. static int dell_send_intensity(struct backlight_device *bd)
  260. {
  261. struct calling_interface_buffer buffer;
  262. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  263. buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
  264. buffer.input[1] = bd->props.brightness;
  265. if (buffer.input[0] == -1)
  266. return -ENODEV;
  267. if (power_supply_is_system_supplied() > 0)
  268. dell_send_request(&buffer, 1, 2);
  269. else
  270. dell_send_request(&buffer, 1, 1);
  271. return 0;
  272. }
  273. static int dell_get_intensity(struct backlight_device *bd)
  274. {
  275. struct calling_interface_buffer buffer;
  276. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  277. buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
  278. if (buffer.input[0] == -1)
  279. return -ENODEV;
  280. if (power_supply_is_system_supplied() > 0)
  281. dell_send_request(&buffer, 0, 2);
  282. else
  283. dell_send_request(&buffer, 0, 1);
  284. return buffer.output[1];
  285. }
  286. static struct backlight_ops dell_ops = {
  287. .get_brightness = dell_get_intensity,
  288. .update_status = dell_send_intensity,
  289. };
  290. static int __init dell_init(void)
  291. {
  292. struct calling_interface_buffer buffer;
  293. int max_intensity = 0;
  294. int ret;
  295. if (!dmi_check_system(dell_device_table))
  296. return -ENODEV;
  297. dmi_walk(find_tokens, NULL);
  298. if (!da_tokens) {
  299. printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
  300. return -ENODEV;
  301. }
  302. ret = dell_setup_rfkill();
  303. if (ret) {
  304. printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
  305. goto out;
  306. }
  307. #ifdef CONFIG_ACPI
  308. /* In the event of an ACPI backlight being available, don't
  309. * register the platform controller.
  310. */
  311. if (acpi_video_backlight_support())
  312. return 0;
  313. #endif
  314. memset(&buffer, 0, sizeof(struct calling_interface_buffer));
  315. buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
  316. if (buffer.input[0] != -1) {
  317. dell_send_request(&buffer, 0, 2);
  318. max_intensity = buffer.output[3];
  319. }
  320. if (max_intensity) {
  321. dell_backlight_device = backlight_device_register(
  322. "dell_backlight",
  323. NULL, NULL,
  324. &dell_ops);
  325. if (IS_ERR(dell_backlight_device)) {
  326. ret = PTR_ERR(dell_backlight_device);
  327. dell_backlight_device = NULL;
  328. goto out;
  329. }
  330. dell_backlight_device->props.max_brightness = max_intensity;
  331. dell_backlight_device->props.brightness =
  332. dell_get_intensity(dell_backlight_device);
  333. backlight_update_status(dell_backlight_device);
  334. }
  335. return 0;
  336. out:
  337. if (wifi_rfkill)
  338. rfkill_unregister(wifi_rfkill);
  339. if (bluetooth_rfkill)
  340. rfkill_unregister(bluetooth_rfkill);
  341. if (wwan_rfkill)
  342. rfkill_unregister(wwan_rfkill);
  343. kfree(da_tokens);
  344. return ret;
  345. }
  346. static void __exit dell_exit(void)
  347. {
  348. backlight_device_unregister(dell_backlight_device);
  349. if (wifi_rfkill)
  350. rfkill_unregister(wifi_rfkill);
  351. if (bluetooth_rfkill)
  352. rfkill_unregister(bluetooth_rfkill);
  353. if (wwan_rfkill)
  354. rfkill_unregister(wwan_rfkill);
  355. }
  356. module_init(dell_init);
  357. module_exit(dell_exit);
  358. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  359. MODULE_DESCRIPTION("Dell laptop driver");
  360. MODULE_LICENSE("GPL");
  361. MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");