dell-laptop.c 13 KB

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