eeepc-laptop.c 37 KB

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