eeepc-laptop.c 37 KB

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