eeepc-laptop.c 36 KB

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