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