eeepc-laptop.c 37 KB

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