eeepc-laptop.c 37 KB

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