dell-laptop.c 10 KB

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