eeepc-laptop.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. /* Brightness events are special */
  1038. if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX) {
  1039. /* Ignore them completely if the acpi video driver is used */
  1040. if (eeepc->backlight_device != NULL) {
  1041. int old_brightness, new_brightness;
  1042. /* Update the backlight device. */
  1043. old_brightness = eeepc_backlight_notify(eeepc);
  1044. /* Convert event to keypress (obsolescent hack) */
  1045. new_brightness = event - NOTIFY_BRN_MIN;
  1046. if (new_brightness < old_brightness) {
  1047. event = NOTIFY_BRN_MIN; /* brightness down */
  1048. } else if (new_brightness > old_brightness) {
  1049. event = NOTIFY_BRN_MAX; /* brightness up */
  1050. } else {
  1051. /*
  1052. * no change in brightness - already at min/max,
  1053. * event will be desired value (or else ignored)
  1054. */
  1055. }
  1056. eeepc_input_notify(eeepc, event);
  1057. }
  1058. } else {
  1059. /* Everything else is a bona-fide keypress event */
  1060. eeepc_input_notify(eeepc, event);
  1061. }
  1062. }
  1063. static void cmsg_quirk(struct eeepc_laptop *eeepc, int cm, const char *name)
  1064. {
  1065. int dummy;
  1066. /* Some BIOSes do not report cm although it is avaliable.
  1067. Check if cm_getv[cm] works and, if yes, assume cm should be set. */
  1068. if (!(eeepc->cm_supported & (1 << cm))
  1069. && !read_acpi_int(eeepc->handle, cm_getv[cm], &dummy)) {
  1070. pr_info("%s (%x) not reported by BIOS,"
  1071. " enabling anyway\n", name, 1 << cm);
  1072. eeepc->cm_supported |= 1 << cm;
  1073. }
  1074. }
  1075. static void cmsg_quirks(struct eeepc_laptop *eeepc)
  1076. {
  1077. cmsg_quirk(eeepc, CM_ASL_LID, "LID");
  1078. cmsg_quirk(eeepc, CM_ASL_TYPE, "TYPE");
  1079. cmsg_quirk(eeepc, CM_ASL_PANELPOWER, "PANELPOWER");
  1080. cmsg_quirk(eeepc, CM_ASL_TPD, "TPD");
  1081. }
  1082. static int eeepc_acpi_init(struct eeepc_laptop *eeepc,
  1083. struct acpi_device *device)
  1084. {
  1085. unsigned int init_flags;
  1086. int result;
  1087. result = acpi_bus_get_status(device);
  1088. if (result)
  1089. return result;
  1090. if (!device->status.present) {
  1091. pr_err("Hotkey device not present, aborting\n");
  1092. return -ENODEV;
  1093. }
  1094. init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
  1095. pr_notice("Hotkey init flags 0x%x\n", init_flags);
  1096. if (write_acpi_int(eeepc->handle, "INIT", init_flags)) {
  1097. pr_err("Hotkey initialization failed\n");
  1098. return -ENODEV;
  1099. }
  1100. /* get control methods supported */
  1101. if (read_acpi_int(eeepc->handle, "CMSG", &eeepc->cm_supported)) {
  1102. pr_err("Get control methods supported failed\n");
  1103. return -ENODEV;
  1104. }
  1105. cmsg_quirks(eeepc);
  1106. pr_info("Get control methods supported: 0x%x\n", eeepc->cm_supported);
  1107. return 0;
  1108. }
  1109. static void __devinit eeepc_enable_camera(struct eeepc_laptop *eeepc)
  1110. {
  1111. /*
  1112. * If the following call to set_acpi() fails, it's because there's no
  1113. * camera so we can ignore the error.
  1114. */
  1115. if (get_acpi(eeepc, CM_ASL_CAMERA) == 0)
  1116. set_acpi(eeepc, CM_ASL_CAMERA, 1);
  1117. }
  1118. static bool eeepc_device_present;
  1119. static int __devinit eeepc_acpi_add(struct acpi_device *device)
  1120. {
  1121. struct eeepc_laptop *eeepc;
  1122. int result;
  1123. pr_notice(EEEPC_LAPTOP_NAME "\n");
  1124. eeepc = kzalloc(sizeof(struct eeepc_laptop), GFP_KERNEL);
  1125. if (!eeepc)
  1126. return -ENOMEM;
  1127. eeepc->handle = device->handle;
  1128. strcpy(acpi_device_name(device), EEEPC_ACPI_DEVICE_NAME);
  1129. strcpy(acpi_device_class(device), EEEPC_ACPI_CLASS);
  1130. device->driver_data = eeepc;
  1131. result = eeepc_acpi_init(eeepc, device);
  1132. if (result)
  1133. goto fail_platform;
  1134. eeepc_enable_camera(eeepc);
  1135. /*
  1136. * Register the platform device first. It is used as a parent for the
  1137. * sub-devices below.
  1138. *
  1139. * Note that if there are multiple instances of this ACPI device it
  1140. * will bail out, because the platform device is registered with a
  1141. * fixed name. Of course it doesn't make sense to have more than one,
  1142. * and machine-specific scripts find the fixed name convenient. But
  1143. * It's also good for us to exclude multiple instances because both
  1144. * our hwmon and our wlan rfkill subdevice use global ACPI objects
  1145. * (the EC and the wlan PCI slot respectively).
  1146. */
  1147. result = eeepc_platform_init(eeepc);
  1148. if (result)
  1149. goto fail_platform;
  1150. if (!acpi_video_backlight_support()) {
  1151. result = eeepc_backlight_init(eeepc);
  1152. if (result)
  1153. goto fail_backlight;
  1154. } else
  1155. pr_info("Backlight controlled by ACPI video driver\n");
  1156. result = eeepc_input_init(eeepc);
  1157. if (result)
  1158. goto fail_input;
  1159. result = eeepc_hwmon_init(eeepc);
  1160. if (result)
  1161. goto fail_hwmon;
  1162. result = eeepc_led_init(eeepc);
  1163. if (result)
  1164. goto fail_led;
  1165. result = eeepc_rfkill_init(eeepc);
  1166. if (result)
  1167. goto fail_rfkill;
  1168. eeepc_device_present = true;
  1169. return 0;
  1170. fail_rfkill:
  1171. eeepc_led_exit(eeepc);
  1172. fail_led:
  1173. eeepc_hwmon_exit(eeepc);
  1174. fail_hwmon:
  1175. eeepc_input_exit(eeepc);
  1176. fail_input:
  1177. eeepc_backlight_exit(eeepc);
  1178. fail_backlight:
  1179. eeepc_platform_exit(eeepc);
  1180. fail_platform:
  1181. kfree(eeepc);
  1182. return result;
  1183. }
  1184. static int eeepc_acpi_remove(struct acpi_device *device, int type)
  1185. {
  1186. struct eeepc_laptop *eeepc = acpi_driver_data(device);
  1187. eeepc_backlight_exit(eeepc);
  1188. eeepc_rfkill_exit(eeepc);
  1189. eeepc_input_exit(eeepc);
  1190. eeepc_hwmon_exit(eeepc);
  1191. eeepc_led_exit(eeepc);
  1192. eeepc_platform_exit(eeepc);
  1193. kfree(eeepc);
  1194. return 0;
  1195. }
  1196. static const struct acpi_device_id eeepc_device_ids[] = {
  1197. {EEEPC_ACPI_HID, 0},
  1198. {"", 0},
  1199. };
  1200. MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
  1201. static struct acpi_driver eeepc_acpi_driver = {
  1202. .name = EEEPC_LAPTOP_NAME,
  1203. .class = EEEPC_ACPI_CLASS,
  1204. .owner = THIS_MODULE,
  1205. .ids = eeepc_device_ids,
  1206. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1207. .ops = {
  1208. .add = eeepc_acpi_add,
  1209. .remove = eeepc_acpi_remove,
  1210. .notify = eeepc_acpi_notify,
  1211. },
  1212. };
  1213. static int __init eeepc_laptop_init(void)
  1214. {
  1215. int result;
  1216. result = platform_driver_register(&platform_driver);
  1217. if (result < 0)
  1218. return result;
  1219. result = acpi_bus_register_driver(&eeepc_acpi_driver);
  1220. if (result < 0)
  1221. goto fail_acpi_driver;
  1222. if (!eeepc_device_present) {
  1223. result = -ENODEV;
  1224. goto fail_no_device;
  1225. }
  1226. return 0;
  1227. fail_no_device:
  1228. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1229. fail_acpi_driver:
  1230. platform_driver_unregister(&platform_driver);
  1231. return result;
  1232. }
  1233. static void __exit eeepc_laptop_exit(void)
  1234. {
  1235. acpi_bus_unregister_driver(&eeepc_acpi_driver);
  1236. platform_driver_unregister(&platform_driver);
  1237. }
  1238. module_init(eeepc_laptop_init);
  1239. module_exit(eeepc_laptop_exit);