eeepc-laptop.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * eeepc-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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/backlight.h>
  25. #include <linux/fb.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <acpi/acpi_drivers.h>
  29. #include <acpi/acpi_bus.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/input.h>
  32. #include <linux/rfkill.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci_hotplug.h>
  35. #include <linux/leds.h>
  36. #define EEEPC_LAPTOP_VERSION "0.1"
  37. #define EEEPC_LAPTOP_NAME "Eee PC Hotkey Driver"
  38. #define EEEPC_LAPTOP_FILE "eeepc"
  39. #define EEEPC_ACPI_CLASS "hotkey"
  40. #define EEEPC_ACPI_DEVICE_NAME "Hotkey"
  41. #define EEEPC_ACPI_HID "ASUS010"
  42. MODULE_AUTHOR("Corentin Chary, Eric Cooper");
  43. MODULE_DESCRIPTION(EEEPC_LAPTOP_NAME);
  44. MODULE_LICENSE("GPL");
  45. /*
  46. * Definitions for Asus EeePC
  47. */
  48. #define NOTIFY_BRN_MIN 0x20
  49. #define NOTIFY_BRN_MAX 0x2f
  50. enum {
  51. DISABLE_ASL_WLAN = 0x0001,
  52. DISABLE_ASL_BLUETOOTH = 0x0002,
  53. DISABLE_ASL_IRDA = 0x0004,
  54. DISABLE_ASL_CAMERA = 0x0008,
  55. DISABLE_ASL_TV = 0x0010,
  56. DISABLE_ASL_GPS = 0x0020,
  57. DISABLE_ASL_DISPLAYSWITCH = 0x0040,
  58. DISABLE_ASL_MODEM = 0x0080,
  59. DISABLE_ASL_CARDREADER = 0x0100,
  60. DISABLE_ASL_3G = 0x0200,
  61. DISABLE_ASL_WIMAX = 0x0400,
  62. DISABLE_ASL_HWCF = 0x0800
  63. };
  64. enum {
  65. CM_ASL_WLAN = 0,
  66. CM_ASL_BLUETOOTH,
  67. CM_ASL_IRDA,
  68. CM_ASL_1394,
  69. CM_ASL_CAMERA,
  70. CM_ASL_TV,
  71. CM_ASL_GPS,
  72. CM_ASL_DVDROM,
  73. CM_ASL_DISPLAYSWITCH,
  74. CM_ASL_PANELBRIGHT,
  75. CM_ASL_BIOSFLASH,
  76. CM_ASL_ACPIFLASH,
  77. CM_ASL_CPUFV,
  78. CM_ASL_CPUTEMPERATURE,
  79. CM_ASL_FANCPU,
  80. CM_ASL_FANCHASSIS,
  81. CM_ASL_USBPORT1,
  82. CM_ASL_USBPORT2,
  83. CM_ASL_USBPORT3,
  84. CM_ASL_MODEM,
  85. CM_ASL_CARDREADER,
  86. CM_ASL_3G,
  87. CM_ASL_WIMAX,
  88. CM_ASL_HWCF,
  89. CM_ASL_LID,
  90. CM_ASL_TYPE,
  91. CM_ASL_PANELPOWER, /*P901*/
  92. CM_ASL_TPD
  93. };
  94. static const char *cm_getv[] = {
  95. "WLDG", "BTHG", NULL, NULL,
  96. "CAMG", NULL, NULL, NULL,
  97. NULL, "PBLG", NULL, NULL,
  98. "CFVG", NULL, NULL, NULL,
  99. "USBG", NULL, NULL, "MODG",
  100. "CRDG", "M3GG", "WIMG", "HWCF",
  101. "LIDG", "TYPE", "PBPG", "TPDG"
  102. };
  103. static const char *cm_setv[] = {
  104. "WLDS", "BTHS", NULL, NULL,
  105. "CAMS", NULL, NULL, NULL,
  106. "SDSP", "PBLS", "HDPS", NULL,
  107. "CFVS", NULL, NULL, NULL,
  108. "USBG", NULL, NULL, "MODS",
  109. "CRDS", "M3GS", "WIMS", NULL,
  110. NULL, NULL, "PBPS", "TPDS"
  111. };
  112. struct key_entry {
  113. char type;
  114. u8 code;
  115. u16 keycode;
  116. };
  117. enum { KE_KEY, KE_END };
  118. static const struct key_entry eeepc_keymap[] = {
  119. /* Sleep already handled via generic ACPI code */
  120. {KE_KEY, 0x10, KEY_WLAN },
  121. {KE_KEY, 0x11, KEY_WLAN },
  122. {KE_KEY, 0x12, KEY_PROG1 },
  123. {KE_KEY, 0x13, KEY_MUTE },
  124. {KE_KEY, 0x14, KEY_VOLUMEDOWN },
  125. {KE_KEY, 0x15, KEY_VOLUMEUP },
  126. {KE_KEY, 0x16, KEY_DISPLAY_OFF },
  127. {KE_KEY, 0x1a, KEY_COFFEE },
  128. {KE_KEY, 0x1b, KEY_ZOOM },
  129. {KE_KEY, 0x1c, KEY_PROG2 },
  130. {KE_KEY, 0x1d, KEY_PROG3 },
  131. {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
  132. {KE_KEY, NOTIFY_BRN_MAX, KEY_BRIGHTNESSUP },
  133. {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
  134. {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
  135. {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
  136. {KE_KEY, 0x37, KEY_F13 }, /* Disable Touchpad */
  137. {KE_KEY, 0x38, KEY_F14 },
  138. {KE_END, 0},
  139. };
  140. /*
  141. * This is the main structure, we can use it to store useful information
  142. */
  143. struct eeepc_laptop {
  144. acpi_handle handle; /* the handle of the acpi device */
  145. u32 cm_supported; /* the control methods supported
  146. by this BIOS */
  147. u16 event_count[128]; /* count for each event */
  148. struct platform_device *platform_device;
  149. struct device *hwmon_device;
  150. struct backlight_device *backlight_device;
  151. struct input_dev *inputdev;
  152. struct key_entry *keymap;
  153. struct rfkill *wlan_rfkill;
  154. struct rfkill *bluetooth_rfkill;
  155. struct rfkill *wwan3g_rfkill;
  156. struct rfkill *wimax_rfkill;
  157. struct hotplug_slot *hotplug_slot;
  158. struct mutex hotplug_lock;
  159. struct led_classdev tpd_led;
  160. int tpd_led_wk;
  161. struct workqueue_struct *led_workqueue;
  162. struct work_struct tpd_led_work;
  163. };
  164. /*
  165. * ACPI Helpers
  166. */
  167. static int write_acpi_int(acpi_handle handle, const char *method, int val)
  168. {
  169. struct acpi_object_list params;
  170. union acpi_object in_obj;
  171. acpi_status status;
  172. params.count = 1;
  173. params.pointer = &in_obj;
  174. in_obj.type = ACPI_TYPE_INTEGER;
  175. in_obj.integer.value = val;
  176. status = acpi_evaluate_object(handle, (char *)method, &params, NULL);
  177. return (status == AE_OK ? 0 : -1);
  178. }
  179. static int read_acpi_int(acpi_handle handle, const char *method, int *val)
  180. {
  181. acpi_status status;
  182. unsigned long long result;
  183. status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
  184. if (ACPI_FAILURE(status)) {
  185. *val = -1;
  186. return -1;
  187. } else {
  188. *val = result;
  189. return 0;
  190. }
  191. }
  192. static int set_acpi(struct eeepc_laptop *eeepc, int cm, int value)
  193. {
  194. const char *method = cm_setv[cm];
  195. if (method == NULL)
  196. return -ENODEV;
  197. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  198. return -ENODEV;
  199. if (write_acpi_int(eeepc->handle, method, value))
  200. pr_warning("Error writing %s\n", method);
  201. return 0;
  202. }
  203. static int get_acpi(struct eeepc_laptop *eeepc, int cm)
  204. {
  205. const char *method = cm_getv[cm];
  206. int value;
  207. if (method == NULL)
  208. return -ENODEV;
  209. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  210. return -ENODEV;
  211. if (read_acpi_int(eeepc->handle, method, &value))
  212. pr_warning("Error reading %s\n", method);
  213. return value;
  214. }
  215. static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
  216. acpi_handle *handle)
  217. {
  218. const char *method = cm_setv[cm];
  219. acpi_status status;
  220. if (method == NULL)
  221. return -ENODEV;
  222. if ((eeepc->cm_supported & (0x1 << cm)) == 0)
  223. return -ENODEV;
  224. status = acpi_get_handle(eeepc->handle, (char *)method,
  225. handle);
  226. if (status != AE_OK) {
  227. pr_warning("Error finding %s\n", method);
  228. return -ENODEV;
  229. }
  230. return 0;
  231. }
  232. /*
  233. * Sys helpers
  234. */
  235. static int parse_arg(const char *buf, unsigned long count, int *val)
  236. {
  237. if (!count)
  238. return 0;
  239. if (sscanf(buf, "%i", val) != 1)
  240. return -EINVAL;
  241. return count;
  242. }
  243. static ssize_t store_sys_acpi(struct device *dev, int cm,
  244. const char *buf, size_t count)
  245. {
  246. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  247. int rv, value;
  248. rv = parse_arg(buf, count, &value);
  249. if (rv > 0)
  250. value = set_acpi(eeepc, cm, value);
  251. if (value < 0)
  252. return -EIO;
  253. return rv;
  254. }
  255. static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
  256. {
  257. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  258. int value = get_acpi(eeepc, cm);
  259. if (value < 0)
  260. return -EIO;
  261. return sprintf(buf, "%d\n", value);
  262. }
  263. #define EEEPC_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  264. static ssize_t show_##_name(struct device *dev, \
  265. struct device_attribute *attr, \
  266. char *buf) \
  267. { \
  268. return show_sys_acpi(dev, _cm, buf); \
  269. } \
  270. static ssize_t store_##_name(struct device *dev, \
  271. struct device_attribute *attr, \
  272. const char *buf, size_t count) \
  273. { \
  274. return store_sys_acpi(dev, _cm, buf, count); \
  275. } \
  276. static struct device_attribute dev_attr_##_name = { \
  277. .attr = { \
  278. .name = __stringify(_name), \
  279. .mode = _mode }, \
  280. .show = show_##_name, \
  281. .store = store_##_name, \
  282. }
  283. EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
  284. EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
  285. EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
  286. struct eeepc_cpufv {
  287. int num;
  288. int cur;
  289. };
  290. static int get_cpufv(struct eeepc_laptop *eeepc, struct eeepc_cpufv *c)
  291. {
  292. c->cur = get_acpi(eeepc, CM_ASL_CPUFV);
  293. c->num = (c->cur >> 8) & 0xff;
  294. c->cur &= 0xff;
  295. if (c->cur < 0 || c->num <= 0 || c->num > 12)
  296. return -ENODEV;
  297. return 0;
  298. }
  299. static ssize_t show_available_cpufv(struct device *dev,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  304. struct eeepc_cpufv c;
  305. int i;
  306. ssize_t len = 0;
  307. if (get_cpufv(eeepc, &c))
  308. return -ENODEV;
  309. for (i = 0; i < c.num; i++)
  310. len += sprintf(buf + len, "%d ", i);
  311. len += sprintf(buf + len, "\n");
  312. return len;
  313. }
  314. static ssize_t show_cpufv(struct device *dev,
  315. struct device_attribute *attr,
  316. char *buf)
  317. {
  318. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  319. struct eeepc_cpufv c;
  320. if (get_cpufv(eeepc, &c))
  321. return -ENODEV;
  322. return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
  323. }
  324. static ssize_t store_cpufv(struct device *dev,
  325. struct device_attribute *attr,
  326. const char *buf, size_t count)
  327. {
  328. struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
  329. struct eeepc_cpufv c;
  330. int rv, value;
  331. if (get_cpufv(eeepc, &c))
  332. return -ENODEV;
  333. rv = parse_arg(buf, count, &value);
  334. if (rv < 0)
  335. return rv;
  336. if (!rv || value < 0 || value >= c.num)
  337. return -EINVAL;
  338. set_acpi(eeepc, CM_ASL_CPUFV, value);
  339. return rv;
  340. }
  341. static struct device_attribute dev_attr_cpufv = {
  342. .attr = {
  343. .name = "cpufv",
  344. .mode = 0644 },
  345. .show = show_cpufv,
  346. .store = store_cpufv
  347. };
  348. static struct device_attribute dev_attr_available_cpufv = {
  349. .attr = {
  350. .name = "available_cpufv",
  351. .mode = 0444 },
  352. .show = show_available_cpufv
  353. };
  354. static struct attribute *platform_attributes[] = {
  355. &dev_attr_camera.attr,
  356. &dev_attr_cardr.attr,
  357. &dev_attr_disp.attr,
  358. &dev_attr_cpufv.attr,
  359. &dev_attr_available_cpufv.attr,
  360. NULL
  361. };
  362. static struct attribute_group platform_attribute_group = {
  363. .attrs = platform_attributes
  364. };
  365. static int eeepc_platform_init(struct eeepc_laptop *eeepc)
  366. {
  367. int result;
  368. eeepc->platform_device = platform_device_alloc(EEEPC_LAPTOP_FILE, -1);
  369. if (!eeepc->platform_device)
  370. return -ENOMEM;
  371. platform_set_drvdata(eeepc->platform_device, eeepc);
  372. result = platform_device_add(eeepc->platform_device);
  373. if (result)
  374. goto fail_platform_device;
  375. result = sysfs_create_group(&eeepc->platform_device->dev.kobj,
  376. &platform_attribute_group);
  377. if (result)
  378. goto fail_sysfs;
  379. return 0;
  380. fail_sysfs:
  381. platform_device_del(eeepc->platform_device);
  382. fail_platform_device:
  383. platform_device_put(eeepc->platform_device);
  384. return result;
  385. }
  386. static void eeepc_platform_exit(struct eeepc_laptop *eeepc)
  387. {
  388. sysfs_remove_group(&eeepc->platform_device->dev.kobj,
  389. &platform_attribute_group);
  390. platform_device_unregister(eeepc->platform_device);
  391. }
  392. /*
  393. * LEDs
  394. */
  395. /*
  396. * These functions actually update the LED's, and are called from a
  397. * workqueue. By doing this as separate work rather than when the LED
  398. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  399. * potentially bad time, such as a timer interrupt.
  400. */
  401. static void tpd_led_update(struct work_struct *work)
  402. {
  403. struct eeepc_laptop *eeepc;
  404. eeepc = container_of(work, struct eeepc_laptop, tpd_led_work);
  405. set_acpi(eeepc, CM_ASL_TPD, eeepc->tpd_led_wk);
  406. }
  407. static void tpd_led_set(struct led_classdev *led_cdev,
  408. enum led_brightness value)
  409. {
  410. struct eeepc_laptop *eeepc;
  411. eeepc = container_of(led_cdev, struct eeepc_laptop, tpd_led);
  412. eeepc->tpd_led_wk = (value > 0) ? 1 : 0;
  413. queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
  414. }
  415. static int eeepc_led_init(struct eeepc_laptop *eeepc)
  416. {
  417. int rv;
  418. if (get_acpi(eeepc, CM_ASL_TPD) == -ENODEV)
  419. return 0;
  420. eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
  421. if (!eeepc->led_workqueue)
  422. return -ENOMEM;
  423. INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
  424. eeepc->tpd_led.name = "eeepc::touchpad";
  425. eeepc->tpd_led.brightness_set = tpd_led_set;
  426. eeepc->tpd_led.max_brightness = 1;
  427. rv = led_classdev_register(&eeepc->platform_device->dev,
  428. &eeepc->tpd_led);
  429. if (rv) {
  430. destroy_workqueue(eeepc->led_workqueue);
  431. return rv;
  432. }
  433. return 0;
  434. }
  435. static void eeepc_led_exit(struct eeepc_laptop *eeepc)
  436. {
  437. if (eeepc->tpd_led.dev)
  438. led_classdev_unregister(&eeepc->tpd_led);
  439. if (eeepc->led_workqueue)
  440. destroy_workqueue(eeepc->led_workqueue);
  441. }
  442. /*
  443. * PCI hotplug (for wlan rfkill)
  444. */
  445. static bool eeepc_wlan_rfkill_blocked(struct eeepc_laptop *eeepc)
  446. {
  447. if (get_acpi(eeepc, CM_ASL_WLAN) == 1)
  448. return false;
  449. return true;
  450. }
  451. static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc)
  452. {
  453. struct pci_dev *dev;
  454. struct pci_bus *bus;
  455. bool blocked = eeepc_wlan_rfkill_blocked(eeepc);
  456. if (eeepc->wlan_rfkill)
  457. rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
  458. mutex_lock(&eeepc->hotplug_lock);
  459. if (eeepc->hotplug_slot) {
  460. bus = pci_find_bus(0, 1);
  461. if (!bus) {
  462. pr_warning("Unable to find PCI bus 1?\n");
  463. goto out_unlock;
  464. }
  465. if (!blocked) {
  466. dev = pci_get_slot(bus, 0);
  467. if (dev) {
  468. /* Device already present */
  469. pci_dev_put(dev);
  470. goto out_unlock;
  471. }
  472. dev = pci_scan_single_device(bus, 0);
  473. if (dev) {
  474. pci_bus_assign_resources(bus);
  475. if (pci_bus_add_device(dev))
  476. pr_err("Unable to hotplug wifi\n");
  477. }
  478. } else {
  479. dev = pci_get_slot(bus, 0);
  480. if (dev) {
  481. pci_remove_bus_device(dev);
  482. pci_dev_put(dev);
  483. }
  484. }
  485. }
  486. out_unlock:
  487. mutex_unlock(&eeepc->hotplug_lock);
  488. }
  489. static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
  490. {
  491. struct eeepc_laptop *eeepc = data;
  492. if (event != ACPI_NOTIFY_BUS_CHECK)
  493. return;
  494. eeepc_rfkill_hotplug(eeepc);
  495. }
  496. static int eeepc_register_rfkill_notifier(struct eeepc_laptop *eeepc,
  497. char *node)
  498. {
  499. acpi_status status;
  500. acpi_handle handle;
  501. status = acpi_get_handle(NULL, node, &handle);
  502. if (ACPI_SUCCESS(status)) {
  503. status = acpi_install_notify_handler(handle,
  504. ACPI_SYSTEM_NOTIFY,
  505. eeepc_rfkill_notify,
  506. eeepc);
  507. if (ACPI_FAILURE(status))
  508. pr_warning("Failed to register notify on %s\n", node);
  509. } else
  510. return -ENODEV;
  511. return 0;
  512. }
  513. static void eeepc_unregister_rfkill_notifier(struct eeepc_laptop *eeepc,
  514. char *node)
  515. {
  516. acpi_status status = AE_OK;
  517. acpi_handle handle;
  518. status = acpi_get_handle(NULL, node, &handle);
  519. if (ACPI_SUCCESS(status)) {
  520. status = acpi_remove_notify_handler(handle,
  521. ACPI_SYSTEM_NOTIFY,
  522. eeepc_rfkill_notify);
  523. if (ACPI_FAILURE(status))
  524. pr_err("Error removing rfkill notify handler %s\n",
  525. node);
  526. }
  527. }
  528. static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
  529. u8 *value)
  530. {
  531. struct eeepc_laptop *eeepc = hotplug_slot->private;
  532. int val = get_acpi(eeepc, CM_ASL_WLAN);
  533. if (val == 1 || val == 0)
  534. *value = val;
  535. else
  536. return -EINVAL;
  537. return 0;
  538. }
  539. static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
  540. {
  541. kfree(hotplug_slot->info);
  542. kfree(hotplug_slot);
  543. }
  544. static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
  545. .owner = THIS_MODULE,
  546. .get_adapter_status = eeepc_get_adapter_status,
  547. .get_power_status = eeepc_get_adapter_status,
  548. };
  549. static int eeepc_setup_pci_hotplug(struct eeepc_laptop *eeepc)
  550. {
  551. int ret = -ENOMEM;
  552. struct pci_bus *bus = pci_find_bus(0, 1);
  553. if (!bus) {
  554. pr_err("Unable to find wifi PCI bus\n");
  555. return -ENODEV;
  556. }
  557. eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  558. if (!eeepc->hotplug_slot)
  559. goto error_slot;
  560. eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  561. GFP_KERNEL);
  562. if (!eeepc->hotplug_slot->info)
  563. goto error_info;
  564. eeepc->hotplug_slot->private = eeepc;
  565. eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
  566. eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
  567. eeepc_get_adapter_status(eeepc->hotplug_slot,
  568. &eeepc->hotplug_slot->info->adapter_status);
  569. ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
  570. if (ret) {
  571. pr_err("Unable to register hotplug slot - %d\n", ret);
  572. goto error_register;
  573. }
  574. return 0;
  575. error_register:
  576. kfree(eeepc->hotplug_slot->info);
  577. error_info:
  578. kfree(eeepc->hotplug_slot);
  579. eeepc->hotplug_slot = NULL;
  580. error_slot:
  581. return ret;
  582. }
  583. /*
  584. * Rfkill devices
  585. */
  586. static int eeepc_rfkill_set(void *data, bool blocked)
  587. {
  588. acpi_handle handle = data;
  589. return write_acpi_int(handle, NULL, !blocked);
  590. }
  591. static const struct rfkill_ops eeepc_rfkill_ops = {
  592. .set_block = eeepc_rfkill_set,
  593. };
  594. static int eeepc_new_rfkill(struct eeepc_laptop *eeepc,
  595. struct rfkill **rfkill,
  596. const char *name,
  597. enum rfkill_type type, int cm)
  598. {
  599. acpi_handle handle;
  600. int result;
  601. result = acpi_setter_handle(eeepc, cm, &handle);
  602. if (result < 0)
  603. return result;
  604. *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
  605. &eeepc_rfkill_ops, handle);
  606. if (!*rfkill)
  607. return -EINVAL;
  608. rfkill_init_sw_state(*rfkill, get_acpi(eeepc, cm) != 1);
  609. result = rfkill_register(*rfkill);
  610. if (result) {
  611. rfkill_destroy(*rfkill);
  612. *rfkill = NULL;
  613. return result;
  614. }
  615. return 0;
  616. }
  617. static void eeepc_rfkill_exit(struct eeepc_laptop *eeepc)
  618. {
  619. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  620. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  621. eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  622. if (eeepc->wlan_rfkill) {
  623. rfkill_unregister(eeepc->wlan_rfkill);
  624. rfkill_destroy(eeepc->wlan_rfkill);
  625. eeepc->wlan_rfkill = NULL;
  626. }
  627. /*
  628. * Refresh pci hotplug in case the rfkill state was changed after
  629. * eeepc_unregister_rfkill_notifier()
  630. */
  631. eeepc_rfkill_hotplug(eeepc);
  632. if (eeepc->hotplug_slot)
  633. pci_hp_deregister(eeepc->hotplug_slot);
  634. if (eeepc->bluetooth_rfkill) {
  635. rfkill_unregister(eeepc->bluetooth_rfkill);
  636. rfkill_destroy(eeepc->bluetooth_rfkill);
  637. eeepc->bluetooth_rfkill = NULL;
  638. }
  639. if (eeepc->wwan3g_rfkill) {
  640. rfkill_unregister(eeepc->wwan3g_rfkill);
  641. rfkill_destroy(eeepc->wwan3g_rfkill);
  642. eeepc->wwan3g_rfkill = NULL;
  643. }
  644. if (eeepc->wimax_rfkill) {
  645. rfkill_unregister(eeepc->wimax_rfkill);
  646. rfkill_destroy(eeepc->wimax_rfkill);
  647. eeepc->wimax_rfkill = NULL;
  648. }
  649. }
  650. static int eeepc_rfkill_init(struct eeepc_laptop *eeepc)
  651. {
  652. int result = 0;
  653. mutex_init(&eeepc->hotplug_lock);
  654. result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
  655. "eeepc-wlan", RFKILL_TYPE_WLAN,
  656. CM_ASL_WLAN);
  657. if (result && result != -ENODEV)
  658. goto exit;
  659. result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
  660. "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
  661. CM_ASL_BLUETOOTH);
  662. if (result && result != -ENODEV)
  663. goto exit;
  664. result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
  665. "eeepc-wwan3g", RFKILL_TYPE_WWAN,
  666. CM_ASL_3G);
  667. if (result && result != -ENODEV)
  668. goto exit;
  669. result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
  670. "eeepc-wimax", RFKILL_TYPE_WIMAX,
  671. CM_ASL_WIMAX);
  672. if (result && result != -ENODEV)
  673. goto exit;
  674. result = eeepc_setup_pci_hotplug(eeepc);
  675. /*
  676. * If we get -EBUSY then something else is handling the PCI hotplug -
  677. * don't fail in this case
  678. */
  679. if (result == -EBUSY)
  680. result = 0;
  681. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
  682. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
  683. eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
  684. /*
  685. * Refresh pci hotplug in case the rfkill state was changed during
  686. * setup.
  687. */
  688. eeepc_rfkill_hotplug(eeepc);
  689. exit:
  690. if (result && result != -ENODEV)
  691. eeepc_rfkill_exit(eeepc);
  692. return result;
  693. }
  694. /*
  695. * Platform driver - hibernate/resume callbacks
  696. */
  697. static int eeepc_hotk_thaw(struct device *device)
  698. {
  699. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  700. if (eeepc->wlan_rfkill) {
  701. bool wlan;
  702. /*
  703. * Work around bios bug - acpi _PTS turns off the wireless led
  704. * during suspend. Normally it restores it on resume, but
  705. * we should kick it ourselves in case hibernation is aborted.
  706. */
  707. wlan = get_acpi(eeepc, CM_ASL_WLAN);
  708. set_acpi(eeepc, CM_ASL_WLAN, wlan);
  709. }
  710. return 0;
  711. }
  712. static int eeepc_hotk_restore(struct device *device)
  713. {
  714. struct eeepc_laptop *eeepc = dev_get_drvdata(device);
  715. /* Refresh both wlan rfkill state and pci hotplug */
  716. if (eeepc->wlan_rfkill)
  717. eeepc_rfkill_hotplug(eeepc);
  718. if (eeepc->bluetooth_rfkill)
  719. rfkill_set_sw_state(eeepc->bluetooth_rfkill,
  720. get_acpi(eeepc, CM_ASL_BLUETOOTH) != 1);
  721. if (eeepc->wwan3g_rfkill)
  722. rfkill_set_sw_state(eeepc->wwan3g_rfkill,
  723. get_acpi(eeepc, CM_ASL_3G) != 1);
  724. if (eeepc->wimax_rfkill)
  725. rfkill_set_sw_state(eeepc->wimax_rfkill,
  726. get_acpi(eeepc, CM_ASL_WIMAX) != 1);
  727. return 0;
  728. }
  729. static struct dev_pm_ops eeepc_pm_ops = {
  730. .thaw = eeepc_hotk_thaw,
  731. .restore = eeepc_hotk_restore,
  732. };
  733. static struct platform_driver platform_driver = {
  734. .driver = {
  735. .name = EEEPC_LAPTOP_FILE,
  736. .owner = THIS_MODULE,
  737. .pm = &eeepc_pm_ops,
  738. }
  739. };
  740. /*
  741. * Hwmon device
  742. */
  743. #define EEEPC_EC_SC00 0x61
  744. #define EEEPC_EC_FAN_PWM (EEEPC_EC_SC00 + 2) /* Fan PWM duty cycle (%) */
  745. #define EEEPC_EC_FAN_HRPM (EEEPC_EC_SC00 + 5) /* High byte, fan speed (RPM) */
  746. #define EEEPC_EC_FAN_LRPM (EEEPC_EC_SC00 + 6) /* Low byte, fan speed (RPM) */
  747. #define EEEPC_EC_SFB0 0xD0
  748. #define EEEPC_EC_FAN_CTRL (EEEPC_EC_SFB0 + 3) /* Byte containing SF25 */
  749. static int eeepc_get_fan_pwm(void)
  750. {
  751. u8 value = 0;
  752. ec_read(EEEPC_EC_FAN_PWM, &value);
  753. return value * 255 / 100;
  754. }
  755. static void eeepc_set_fan_pwm(int value)
  756. {
  757. value = SENSORS_LIMIT(value, 0, 255);
  758. value = value * 100 / 255;
  759. ec_write(EEEPC_EC_FAN_PWM, value);
  760. }
  761. static int eeepc_get_fan_rpm(void)
  762. {
  763. u8 high = 0;
  764. u8 low = 0;
  765. ec_read(EEEPC_EC_FAN_HRPM, &high);
  766. ec_read(EEEPC_EC_FAN_LRPM, &low);
  767. return high << 8 | low;
  768. }
  769. static int eeepc_get_fan_ctrl(void)
  770. {
  771. u8 value = 0;
  772. ec_read(EEEPC_EC_FAN_CTRL, &value);
  773. if (value & 0x02)
  774. return 1; /* manual */
  775. else
  776. return 2; /* automatic */
  777. }
  778. static void eeepc_set_fan_ctrl(int manual)
  779. {
  780. u8 value = 0;
  781. ec_read(EEEPC_EC_FAN_CTRL, &value);
  782. if (manual == 1)
  783. value |= 0x02;
  784. else
  785. value &= ~0x02;
  786. ec_write(EEEPC_EC_FAN_CTRL, value);
  787. }
  788. static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
  789. {
  790. int rv, value;
  791. rv = parse_arg(buf, count, &value);
  792. if (rv > 0)
  793. set(value);
  794. return rv;
  795. }
  796. static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
  797. {
  798. return sprintf(buf, "%d\n", get());
  799. }
  800. #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
  801. static ssize_t show_##_name(struct device *dev, \
  802. struct device_attribute *attr, \
  803. char *buf) \
  804. { \
  805. return show_sys_hwmon(_set, buf); \
  806. } \
  807. static ssize_t store_##_name(struct device *dev, \
  808. struct device_attribute *attr, \
  809. const char *buf, size_t count) \
  810. { \
  811. return store_sys_hwmon(_get, buf, count); \
  812. } \
  813. static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
  814. EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
  815. EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
  816. eeepc_get_fan_pwm, eeepc_set_fan_pwm);
  817. EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  818. eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
  819. static ssize_t
  820. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  821. {
  822. return sprintf(buf, "eeepc\n");
  823. }
  824. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  825. static struct attribute *hwmon_attributes[] = {
  826. &sensor_dev_attr_pwm1.dev_attr.attr,
  827. &sensor_dev_attr_fan1_input.dev_attr.attr,
  828. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  829. &sensor_dev_attr_name.dev_attr.attr,
  830. NULL
  831. };
  832. static struct attribute_group hwmon_attribute_group = {
  833. .attrs = hwmon_attributes
  834. };
  835. static void eeepc_hwmon_exit(struct eeepc_laptop *eeepc)
  836. {
  837. struct device *hwmon;
  838. hwmon = eeepc->hwmon_device;
  839. if (!hwmon)
  840. return;
  841. sysfs_remove_group(&hwmon->kobj,
  842. &hwmon_attribute_group);
  843. hwmon_device_unregister(hwmon);
  844. eeepc->hwmon_device = NULL;
  845. }
  846. static int eeepc_hwmon_init(struct eeepc_laptop *eeepc)
  847. {
  848. struct device *hwmon;
  849. int result;
  850. hwmon = hwmon_device_register(&eeepc->platform_device->dev);
  851. if (IS_ERR(hwmon)) {
  852. pr_err("Could not register eeepc hwmon device\n");
  853. eeepc->hwmon_device = NULL;
  854. return PTR_ERR(hwmon);
  855. }
  856. eeepc->hwmon_device = hwmon;
  857. result = sysfs_create_group(&hwmon->kobj,
  858. &hwmon_attribute_group);
  859. if (result)
  860. eeepc_hwmon_exit(eeepc);
  861. return result;
  862. }
  863. /*
  864. * Backlight device
  865. */
  866. static int read_brightness(struct backlight_device *bd)
  867. {
  868. struct eeepc_laptop *eeepc = bl_get_data(bd);
  869. return get_acpi(eeepc, CM_ASL_PANELBRIGHT);
  870. }
  871. static int set_brightness(struct backlight_device *bd, int value)
  872. {
  873. struct eeepc_laptop *eeepc = bl_get_data(bd);
  874. return set_acpi(eeepc, CM_ASL_PANELBRIGHT, value);
  875. }
  876. static int update_bl_status(struct backlight_device *bd)
  877. {
  878. return set_brightness(bd, bd->props.brightness);
  879. }
  880. static struct backlight_ops eeepcbl_ops = {
  881. .get_brightness = read_brightness,
  882. .update_status = update_bl_status,
  883. };
  884. static int eeepc_backlight_notify(struct eeepc_laptop *eeepc)
  885. {
  886. struct backlight_device *bd = eeepc->backlight_device;
  887. int old = bd->props.brightness;
  888. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  889. return old;
  890. }
  891. static int eeepc_backlight_init(struct eeepc_laptop *eeepc)
  892. {
  893. struct backlight_device *bd;
  894. bd = backlight_device_register(EEEPC_LAPTOP_FILE,
  895. &eeepc->platform_device->dev,
  896. eeepc, &eeepcbl_ops);
  897. if (IS_ERR(bd)) {
  898. pr_err("Could not register eeepc backlight device\n");
  899. eeepc->backlight_device = NULL;
  900. return PTR_ERR(bd);
  901. }
  902. eeepc->backlight_device = bd;
  903. bd->props.max_brightness = 15;
  904. bd->props.brightness = read_brightness(bd);
  905. bd->props.power = FB_BLANK_UNBLANK;
  906. backlight_update_status(bd);
  907. return 0;
  908. }
  909. static void eeepc_backlight_exit(struct eeepc_laptop *eeepc)
  910. {
  911. if (eeepc->backlight_device)
  912. backlight_device_unregister(eeepc->backlight_device);
  913. eeepc->backlight_device = NULL;
  914. }
  915. /*
  916. * Input device (i.e. hotkeys)
  917. */
  918. static struct key_entry *eeepc_get_entry_by_scancode(
  919. struct eeepc_laptop *eeepc,
  920. int code)
  921. {
  922. struct key_entry *key;
  923. for (key = eeepc->keymap; key->type != KE_END; key++)
  924. if (code == key->code)
  925. return key;
  926. return NULL;
  927. }
  928. static void eeepc_input_notify(struct eeepc_laptop *eeepc, int event)
  929. {
  930. static struct key_entry *key;
  931. key = eeepc_get_entry_by_scancode(eeepc, event);
  932. if (key) {
  933. switch (key->type) {
  934. case KE_KEY:
  935. input_report_key(eeepc->inputdev, key->keycode,
  936. 1);
  937. input_sync(eeepc->inputdev);
  938. input_report_key(eeepc->inputdev, key->keycode,
  939. 0);
  940. input_sync(eeepc->inputdev);
  941. break;
  942. }
  943. }
  944. }
  945. static struct key_entry *eeepc_get_entry_by_keycode(
  946. struct eeepc_laptop *eeepc, int code)
  947. {
  948. struct key_entry *key;
  949. for (key = eeepc->keymap; key->type != KE_END; key++)
  950. if (code == key->keycode && key->type == KE_KEY)
  951. return key;
  952. return NULL;
  953. }
  954. static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  955. {
  956. struct eeepc_laptop *eeepc = input_get_drvdata(dev);
  957. struct key_entry *key = eeepc_get_entry_by_scancode(eeepc, scancode);
  958. if (key && key->type == KE_KEY) {
  959. *keycode = key->keycode;
  960. return 0;
  961. }
  962. return -EINVAL;
  963. }
  964. static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  965. {
  966. struct eeepc_laptop *eeepc = input_get_drvdata(dev);
  967. struct key_entry *key;
  968. int old_keycode;
  969. if (keycode < 0 || keycode > KEY_MAX)
  970. return -EINVAL;
  971. key = eeepc_get_entry_by_scancode(eeepc, scancode);
  972. if (key && key->type == KE_KEY) {
  973. old_keycode = key->keycode;
  974. key->keycode = keycode;
  975. set_bit(keycode, dev->keybit);
  976. if (!eeepc_get_entry_by_keycode(eeepc, old_keycode))
  977. clear_bit(old_keycode, dev->keybit);
  978. return 0;
  979. }
  980. return -EINVAL;
  981. }
  982. static int eeepc_input_init(struct eeepc_laptop *eeepc)
  983. {
  984. const struct key_entry *key;
  985. int result;
  986. eeepc->inputdev = input_allocate_device();
  987. if (!eeepc->inputdev) {
  988. pr_info("Unable to allocate input device\n");
  989. return -ENOMEM;
  990. }
  991. eeepc->inputdev->name = "Asus EeePC extra buttons";
  992. eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
  993. eeepc->inputdev->phys = EEEPC_LAPTOP_FILE "/input0";
  994. eeepc->inputdev->id.bustype = BUS_HOST;
  995. eeepc->inputdev->getkeycode = eeepc_getkeycode;
  996. eeepc->inputdev->setkeycode = eeepc_setkeycode;
  997. input_set_drvdata(eeepc->inputdev, eeepc);
  998. eeepc->keymap = kmemdup(eeepc_keymap, sizeof(eeepc_keymap),
  999. GFP_KERNEL);
  1000. for (key = eeepc_keymap; key->type != KE_END; key++) {
  1001. switch (key->type) {
  1002. case KE_KEY:
  1003. set_bit(EV_KEY, eeepc->inputdev->evbit);
  1004. set_bit(key->keycode, eeepc->inputdev->keybit);
  1005. break;
  1006. }
  1007. }
  1008. result = input_register_device(eeepc->inputdev);
  1009. if (result) {
  1010. pr_info("Unable to register input device\n");
  1011. input_free_device(eeepc->inputdev);
  1012. return result;
  1013. }
  1014. return 0;
  1015. }
  1016. static void eeepc_input_exit(struct eeepc_laptop *eeepc)
  1017. {
  1018. if (eeepc->inputdev) {
  1019. input_unregister_device(eeepc->inputdev);
  1020. kfree(eeepc->keymap);
  1021. }
  1022. }
  1023. /*
  1024. * ACPI driver
  1025. */
  1026. static void eeepc_acpi_notify(struct acpi_device *device, u32 event)
  1027. {
  1028. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1029. u16 count;
  1030. if (event > ACPI_MAX_SYS_NOTIFY)
  1031. return;
  1032. count = eeepc->event_count[event % 128]++;
  1033. acpi_bus_generate_proc_event(device, event, count);
  1034. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1035. dev_name(&device->dev), event,
  1036. count);
  1037. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX) {
  1038. int old_brightness, new_brightness;
  1039. /* Update backlight device. */
  1040. old_brightness = eeepc_backlight_notify(eeepc);
  1041. /* Convert brightness event to keypress (obsolescent hack). */
  1042. new_brightness = event - NOTIFY_BRN_MIN;
  1043. if (new_brightness < old_brightness) {
  1044. event = NOTIFY_BRN_MIN; /* brightness down */
  1045. } else if (new_brightness > old_brightness) {
  1046. event = NOTIFY_BRN_MAX; /* brightness up */
  1047. } else {
  1048. /*
  1049. * no change in brightness - already at min/max,
  1050. * event will be desired value (or else ignored).
  1051. */
  1052. }
  1053. }
  1054. eeepc_input_notify(eeepc, event);
  1055. }
  1056. static void cmsg_quirk(struct eeepc_laptop *eeepc, int cm, const char *name)
  1057. {
  1058. int dummy;
  1059. /* Some BIOSes do not report cm although it is avaliable.
  1060. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  1061. if (!(eeepc->cm_supported & (1 << cm))
  1062. && !read_acpi_int(eeepc->handle, cm_getv[cm], &dummy)) {
  1063. pr_info("%s (%x) not reported by BIOS,"
  1064. " enabling anyway\n", name, 1 << cm);
  1065. eeepc->cm_supported |= 1 << cm;
  1066. }
  1067. }
  1068. static void cmsg_quirks(struct eeepc_laptop *eeepc)
  1069. {
  1070. cmsg_quirk(eeepc, CM_ASL_LID, "LID");
  1071. cmsg_quirk(eeepc, CM_ASL_TYPE, "TYPE");
  1072. cmsg_quirk(eeepc, CM_ASL_PANELPOWER, "PANELPOWER");
  1073. cmsg_quirk(eeepc, CM_ASL_TPD, "TPD");
  1074. }
  1075. static int eeepc_acpi_init(struct eeepc_laptop *eeepc,
  1076. struct acpi_device *device)
  1077. {
  1078. unsigned int init_flags;
  1079. int result;
  1080. result = acpi_bus_get_status(device);
  1081. if (result)
  1082. return result;
  1083. if (!device->status.present) {
  1084. pr_err("Hotkey device not present, aborting\n");
  1085. return -ENODEV;
  1086. }
  1087. init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  1088. pr_notice("Hotkey init flags 0x%x\n", init_flags);
  1089. if (write_acpi_int(eeepc->handle, "INIT", init_flags)) {
  1090. pr_err("Hotkey initialization failed\n");
  1091. return -ENODEV;
  1092. }
  1093. /* get control methods supported */
  1094. if (read_acpi_int(eeepc->handle, "CMSG", &eeepc->cm_supported)) {
  1095. pr_err("Get control methods supported failed\n");
  1096. return -ENODEV;
  1097. }
  1098. cmsg_quirks(eeepc);
  1099. pr_info("Get control methods supported: 0x%x\n", eeepc->cm_supported);
  1100. return 0;
  1101. }
  1102. static void __devinit eeepc_enable_camera(struct eeepc_laptop *eeepc)
  1103. {
  1104. /*
  1105. * If the following call to set_acpi() fails, it's because there's no
  1106. * camera so we can ignore the error.
  1107. */
  1108. if (get_acpi(eeepc, CM_ASL_CAMERA) == 0)
  1109. set_acpi(eeepc, CM_ASL_CAMERA, 1);
  1110. }
  1111. static bool eeepc_device_present;
  1112. static int __devinit eeepc_acpi_add(struct acpi_device *device)
  1113. {
  1114. struct eeepc_laptop *eeepc;
  1115. int result;
  1116. pr_notice(EEEPC_LAPTOP_NAME "\n");
  1117. eeepc = kzalloc(sizeof(struct eeepc_laptop), GFP_KERNEL);
  1118. if (!eeepc)
  1119. return -ENOMEM;
  1120. eeepc->handle = device->handle;
  1121. strcpy(acpi_device_name(device), EEEPC_ACPI_DEVICE_NAME);
  1122. strcpy(acpi_device_class(device), EEEPC_ACPI_CLASS);
  1123. device->driver_data = eeepc;
  1124. result = eeepc_acpi_init(eeepc, device);
  1125. if (result)
  1126. goto fail_platform;
  1127. eeepc_enable_camera(eeepc);
  1128. /*
  1129. * Register the platform device first. It is used as a parent for the
  1130. * sub-devices below.
  1131. *
  1132. * Note that if there are multiple instances of this ACPI device it
  1133. * will bail out, because the platform device is registered with a
  1134. * fixed name. Of course it doesn't make sense to have more than one,
  1135. * and machine-specific scripts find the fixed name convenient. But
  1136. * It's also good for us to exclude multiple instances because both
  1137. * our hwmon and our wlan rfkill subdevice use global ACPI objects
  1138. * (the EC and the wlan PCI slot respectively).
  1139. */
  1140. result = eeepc_platform_init(eeepc);
  1141. if (result)
  1142. goto fail_platform;
  1143. if (!acpi_video_backlight_support()) {
  1144. result = eeepc_backlight_init(eeepc);
  1145. if (result)
  1146. goto fail_backlight;
  1147. } else
  1148. pr_info("Backlight controlled by ACPI video driver\n");
  1149. result = eeepc_input_init(eeepc);
  1150. if (result)
  1151. goto fail_input;
  1152. result = eeepc_hwmon_init(eeepc);
  1153. if (result)
  1154. goto fail_hwmon;
  1155. result = eeepc_led_init(eeepc);
  1156. if (result)
  1157. goto fail_led;
  1158. result = eeepc_rfkill_init(eeepc);
  1159. if (result)
  1160. goto fail_rfkill;
  1161. eeepc_device_present = true;
  1162. return 0;
  1163. fail_rfkill:
  1164. eeepc_led_exit(eeepc);
  1165. fail_led:
  1166. eeepc_hwmon_exit(eeepc);
  1167. fail_hwmon:
  1168. eeepc_input_exit(eeepc);
  1169. fail_input:
  1170. eeepc_backlight_exit(eeepc);
  1171. fail_backlight:
  1172. eeepc_platform_exit(eeepc);
  1173. fail_platform:
  1174. kfree(eeepc);
  1175. return result;
  1176. }
  1177. static int eeepc_acpi_remove(struct acpi_device *device, int type)
  1178. {
  1179. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1180. eeepc_backlight_exit(eeepc);
  1181. eeepc_rfkill_exit(eeepc);
  1182. eeepc_input_exit(eeepc);
  1183. eeepc_hwmon_exit(eeepc);
  1184. eeepc_led_exit(eeepc);
  1185. eeepc_platform_exit(eeepc);
  1186. kfree(eeepc);
  1187. return 0;
  1188. }
  1189. static const struct acpi_device_id eeepc_device_ids[] = {
  1190. {EEEPC_ACPI_HID, 0},
  1191. {"", 0},
  1192. };
  1193. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  1194. static struct acpi_driver eeepc_acpi_driver = {
  1195. .name = EEEPC_LAPTOP_NAME,
  1196. .class = EEEPC_ACPI_CLASS,
  1197. .owner = THIS_MODULE,
  1198. .ids = eeepc_device_ids,
  1199. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1200. .ops = {
  1201. .add = eeepc_acpi_add,
  1202. .remove = eeepc_acpi_remove,
  1203. .notify = eeepc_acpi_notify,
  1204. },
  1205. };
  1206. static int __init eeepc_laptop_init(void)
  1207. {
  1208. int result;
  1209. result = platform_driver_register(&platform_driver);
  1210. if (result < 0)
  1211. return result;
  1212. result = acpi_bus_register_driver(&eeepc_acpi_driver);
  1213. if (result < 0)
  1214. goto fail_acpi_driver;
  1215. if (!eeepc_device_present) {
  1216. result = -ENODEV;
  1217. goto fail_no_device;
  1218. }
  1219. return 0;
  1220. fail_no_device:
  1221. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1222. fail_acpi_driver:
  1223. platform_driver_unregister(&platform_driver);
  1224. return result;
  1225. }
  1226. static void __exit eeepc_laptop_exit(void)
  1227. {
  1228. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1229. platform_driver_unregister(&platform_driver);
  1230. }
  1231. module_init(eeepc_laptop_init);
  1232. module_exit(eeepc_laptop_exit);