eeepc-laptop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * eepc-laptop.c - Asus Eee PC extras
  3. *
  4. * Based on asus_acpi.c as patched for the Eee PC by Asus:
  5. * ftp://ftp.asus.com/pub/ASUS/EeePC/701/ASUS_ACPI_071126.rar
  6. * Based on eee.c from eeepc-linux
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/types.h>
  22. #include <linux/platform_device.h>
  23. #include <acpi/acpi_drivers.h>
  24. #include <acpi/acpi_bus.h>
  25. #include <linux/uaccess.h>
  26. #define EEEPC_LAPTOP_VERSION "0.1"
  27. #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
  28. #define EEEPC_HOTK_FILE "eeepc"
  29. #define EEEPC_HOTK_CLASS "hotkey"
  30. #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
  31. #define EEEPC_HOTK_HID "ASUS010"
  32. #define EEEPC_LOG EEEPC_HOTK_FILE ": "
  33. #define EEEPC_ERR KERN_ERR EEEPC_LOG
  34. #define EEEPC_WARNING KERN_WARNING EEEPC_LOG
  35. #define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
  36. #define EEEPC_INFO KERN_INFO EEEPC_LOG
  37. /*
  38. * Definitions for Asus EeePC
  39. */
  40. #define NOTIFY_WLAN_ON 0x10
  41. enum {
  42. DISABLE_ASL_WLAN = 0x0001,
  43. DISABLE_ASL_BLUETOOTH = 0x0002,
  44. DISABLE_ASL_IRDA = 0x0004,
  45. DISABLE_ASL_CAMERA = 0x0008,
  46. DISABLE_ASL_TV = 0x0010,
  47. DISABLE_ASL_GPS = 0x0020,
  48. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  49. DISABLE_ASL_MODEM = 0x0080,
  50. DISABLE_ASL_CARDREADER = 0x0100
  51. };
  52. enum {
  53. CM_ASL_WLAN = 0,
  54. CM_ASL_BLUETOOTH,
  55. CM_ASL_IRDA,
  56. CM_ASL_1394,
  57. CM_ASL_CAMERA,
  58. CM_ASL_TV,
  59. CM_ASL_GPS,
  60. CM_ASL_DVDROM,
  61. CM_ASL_DISPLAYSWITCH,
  62. CM_ASL_PANELBRIGHT,
  63. CM_ASL_BIOSFLASH,
  64. CM_ASL_ACPIFLASH,
  65. CM_ASL_CPUFV,
  66. CM_ASL_CPUTEMPERATURE,
  67. CM_ASL_FANCPU,
  68. CM_ASL_FANCHASSIS,
  69. CM_ASL_USBPORT1,
  70. CM_ASL_USBPORT2,
  71. CM_ASL_USBPORT3,
  72. CM_ASL_MODEM,
  73. CM_ASL_CARDREADER,
  74. CM_ASL_LID
  75. };
  76. const char *cm_getv[] = {
  77. "WLDG", NULL, NULL, NULL,
  78. "CAMG", NULL, NULL, NULL,
  79. NULL, "PBLG", NULL, NULL,
  80. "CFVG", NULL, NULL, NULL,
  81. "USBG", NULL, NULL, "MODG",
  82. "CRDG", "LIDG"
  83. };
  84. const char *cm_setv[] = {
  85. "WLDS", NULL, NULL, NULL,
  86. "CAMS", NULL, NULL, NULL,
  87. "SDSP", "PBLS", "HDPS", NULL,
  88. "CFVS", NULL, NULL, NULL,
  89. "USBG", NULL, NULL, "MODS",
  90. "CRDS", NULL
  91. };
  92. /*
  93. * This is the main structure, we can use it to store useful information
  94. * about the hotk device
  95. */
  96. struct eeepc_hotk {
  97. struct acpi_device *device; /* the device we are in */
  98. acpi_handle handle; /* the handle of the hotk device */
  99. u32 cm_supported; /* the control methods supported
  100. by this BIOS */
  101. uint init_flag; /* Init flags */
  102. u16 event_count[128]; /* count for each event */
  103. };
  104. /* The actual device the driver binds to */
  105. static struct eeepc_hotk *ehotk;
  106. /* Platform device/driver */
  107. static struct platform_driver platform_driver = {
  108. .driver = {
  109. .name = EEEPC_HOTK_FILE,
  110. .owner = THIS_MODULE,
  111. }
  112. };
  113. static struct platform_device *platform_device;
  114. /*
  115. * The hotkey driver declaration
  116. */
  117. static int eeepc_hotk_add(struct acpi_device *device);
  118. static int eeepc_hotk_remove(struct acpi_device *device, int type);
  119. static const struct acpi_device_id eeepc_device_ids[] = {
  120. {EEEPC_HOTK_HID, 0},
  121. {"", 0},
  122. };
  123. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  124. static struct acpi_driver eeepc_hotk_driver = {
  125. .name = EEEPC_HOTK_NAME,
  126. .class = EEEPC_HOTK_CLASS,
  127. .ids = eeepc_device_ids,
  128. .ops = {
  129. .add = eeepc_hotk_add,
  130. .remove = eeepc_hotk_remove,
  131. },
  132. };
  133. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  134. MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
  135. MODULE_LICENSE("GPL");
  136. /*
  137. * ACPI Helpers
  138. */
  139. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  140. struct acpi_buffer *output)
  141. {
  142. struct acpi_object_list params;
  143. union acpi_object in_obj;
  144. acpi_status status;
  145. params.count = 1;
  146. params.pointer = &in_obj;
  147. in_obj.type = ACPI_TYPE_INTEGER;
  148. in_obj.integer.value = val;
  149. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  150. return (status == AE_OK ? 0 : -1);
  151. }
  152. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  153. {
  154. acpi_status status;
  155. ulong result;
  156. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  157. if (ACPI_FAILURE(status)) {
  158. *val = -1;
  159. return -1;
  160. } else {
  161. *val = result;
  162. return 0;
  163. }
  164. }
  165. static int set_acpi(int cm, int value)
  166. {
  167. if (ehotk->cm_supported & (0x1 << cm)) {
  168. const char *method = cm_setv[cm];
  169. if (method == NULL)
  170. return -ENODEV;
  171. if (write_acpi_int(ehotk->handle, method, value, NULL))
  172. printk(EEEPC_WARNING "Error writing %s\n", method);
  173. }
  174. return 0;
  175. }
  176. static int get_acpi(int cm)
  177. {
  178. int value = -1;
  179. if ((ehotk->cm_supported & (0x1 << cm))) {
  180. const char *method = cm_getv[cm];
  181. if (method == NULL)
  182. return -ENODEV;
  183. if (read_acpi_int(ehotk->handle, method, &value))
  184. printk(EEEPC_WARNING "Error reading %s\n", method);
  185. }
  186. return value;
  187. }
  188. /*
  189. * Sys helpers
  190. */
  191. static int parse_arg(const char *buf, unsigned long count, int *val)
  192. {
  193. if (!count)
  194. return 0;
  195. if (sscanf(buf, "%i", val) != 1)
  196. return -EINVAL;
  197. return count;
  198. }
  199. static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
  200. {
  201. int rv, value;
  202. rv = parse_arg(buf, count, &value);
  203. if (rv > 0)
  204. set_acpi(cm, value);
  205. return rv;
  206. }
  207. static ssize_t show_sys_acpi(int cm, char *buf)
  208. {
  209. return sprintf(buf, "%d\n", get_acpi(cm));
  210. }
  211. #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
  212. static ssize_t show_##_name(struct device *dev, \
  213. struct device_attribute *attr, \
  214. char *buf) \
  215. { \
  216. return show_sys_acpi(_cm, buf); \
  217. } \
  218. static ssize_t store_##_name(struct device *dev, \
  219. struct device_attribute *attr, \
  220. const char *buf, size_t count) \
  221. { \
  222. return store_sys_acpi(_cm, buf, count); \
  223. } \
  224. static struct device_attribute dev_attr_##_name = { \
  225. .attr = { \
  226. .name = __stringify(_name), \
  227. .mode = 0644 }, \
  228. .show = show_##_name, \
  229. .store = store_##_name, \
  230. }
  231. EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
  232. EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
  233. EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
  234. EEEPC_CREATE_DEVICE_ATTR(wlan, CM_ASL_WLAN);
  235. static struct attribute *platform_attributes[] = {
  236. &dev_attr_camera.attr,
  237. &dev_attr_cardr.attr,
  238. &dev_attr_disp.attr,
  239. &dev_attr_wlan.attr,
  240. NULL
  241. };
  242. static struct attribute_group platform_attribute_group = {
  243. .attrs = platform_attributes
  244. };
  245. /*
  246. * Hotkey functions
  247. */
  248. static int eeepc_hotk_check(void)
  249. {
  250. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  251. int result;
  252. result = acpi_bus_get_status(ehotk->device);
  253. if (result)
  254. return result;
  255. if (ehotk->device->status.present) {
  256. if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
  257. &buffer)) {
  258. printk(EEEPC_ERR "Hotkey initialization failed\n");
  259. return -ENODEV;
  260. } else {
  261. printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
  262. ehotk->init_flag);
  263. }
  264. /* get control methods supported */
  265. if (read_acpi_int(ehotk->handle, "CMSG"
  266. , &ehotk->cm_supported)) {
  267. printk(EEEPC_ERR
  268. "Get control methods supported failed\n");
  269. return -ENODEV;
  270. } else {
  271. printk(EEEPC_INFO
  272. "Get control methods supported: 0x%x\n",
  273. ehotk->cm_supported);
  274. }
  275. } else {
  276. printk(EEEPC_ERR "Hotkey device not present, aborting\n");
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. static void notify_wlan(u32 *event)
  282. {
  283. /* if DISABLE_ASL_WLAN is set, the notify code for fn+f2
  284. will always be 0x10 */
  285. if (ehotk->cm_supported & (0x1 << CM_ASL_WLAN)) {
  286. const char *method = cm_getv[CM_ASL_WLAN];
  287. int value;
  288. if (read_acpi_int(ehotk->handle, method, &value))
  289. printk(EEEPC_WARNING "Error reading %s\n",
  290. method);
  291. else if (value == 1)
  292. *event = 0x11;
  293. }
  294. }
  295. static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
  296. {
  297. if (!ehotk)
  298. return;
  299. if (event == NOTIFY_WLAN_ON && (DISABLE_ASL_WLAN & ehotk->init_flag))
  300. notify_wlan(&event);
  301. acpi_bus_generate_proc_event(ehotk->device, event,
  302. ehotk->event_count[event % 128]++);
  303. }
  304. static int eeepc_hotk_add(struct acpi_device *device)
  305. {
  306. acpi_status status = AE_OK;
  307. int result;
  308. if (!device)
  309. return -EINVAL;
  310. printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
  311. ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
  312. if (!ehotk)
  313. return -ENOMEM;
  314. ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  315. ehotk->handle = device->handle;
  316. strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
  317. strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
  318. acpi_driver_data(device) = ehotk;
  319. ehotk->device = device;
  320. result = eeepc_hotk_check();
  321. if (result)
  322. goto end;
  323. status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
  324. eeepc_hotk_notify, ehotk);
  325. if (ACPI_FAILURE(status))
  326. printk(EEEPC_ERR "Error installing notify handler\n");
  327. end:
  328. if (result) {
  329. kfree(ehotk);
  330. ehotk = NULL;
  331. }
  332. return result;
  333. }
  334. static int eeepc_hotk_remove(struct acpi_device *device, int type)
  335. {
  336. acpi_status status = 0;
  337. if (!device || !acpi_driver_data(device))
  338. return -EINVAL;
  339. status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
  340. eeepc_hotk_notify);
  341. if (ACPI_FAILURE(status))
  342. printk(EEEPC_ERR "Error removing notify handler\n");
  343. kfree(ehotk);
  344. return 0;
  345. }
  346. /*
  347. * exit/init
  348. */
  349. static void __exit eeepc_laptop_exit(void)
  350. {
  351. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  352. sysfs_remove_group(&platform_device->dev.kobj,
  353. &platform_attribute_group);
  354. platform_device_unregister(platform_device);
  355. platform_driver_unregister(&platform_driver);
  356. }
  357. static int __init eeepc_laptop_init(void)
  358. {
  359. struct device *dev;
  360. int result;
  361. if (acpi_disabled)
  362. return -ENODEV;
  363. result = acpi_bus_register_driver(&eeepc_hotk_driver);
  364. if (result < 0)
  365. return result;
  366. if (!ehotk) {
  367. acpi_bus_unregister_driver(&eeepc_hotk_driver);
  368. return -ENODEV;
  369. }
  370. dev = acpi_get_physical_device(ehotk->device->handle);
  371. /* Register platform stuff */
  372. result = platform_driver_register(&platform_driver);
  373. if (result)
  374. goto fail_platform_driver;
  375. platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
  376. if (!platform_device) {
  377. result = -ENOMEM;
  378. goto fail_platform_device1;
  379. }
  380. result = platform_device_add(platform_device);
  381. if (result)
  382. goto fail_platform_device2;
  383. result = sysfs_create_group(&platform_device->dev.kobj,
  384. &platform_attribute_group);
  385. if (result)
  386. goto fail_sysfs;
  387. return 0;
  388. fail_sysfs:
  389. platform_device_del(platform_device);
  390. fail_platform_device2:
  391. platform_device_put(platform_device);
  392. fail_platform_device1:
  393. platform_driver_unregister(&platform_driver);
  394. fail_platform_driver:
  395. return result;
  396. }
  397. module_init(eeepc_laptop_init);
  398. module_exit(eeepc_laptop_exit);