fujitsu-laptop.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
  4. Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
  5. Based on earlier work:
  6. Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
  7. Adrian Yee <brewt-fujitsu@brewt.org>
  8. Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
  9. by its respective authors.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful, but
  15. WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. 02110-1301, USA.
  22. */
  23. /*
  24. * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
  25. * features made available on a range of Fujitsu laptops including the
  26. * P2xxx/P5xxx/S6xxx/S7xxx series.
  27. *
  28. * This driver exports a few files in /sys/devices/platform/fujitsu-laptop/;
  29. * others may be added at a later date.
  30. *
  31. * lcd_level - Screen brightness: contains a single integer in the
  32. * range 0..7. (rw)
  33. *
  34. * In addition to these platform device attributes the driver
  35. * registers itself in the Linux backlight control subsystem and is
  36. * available to userspace under /sys/class/backlight/fujitsu-laptop/.
  37. *
  38. * Hotkeys present on certain Fujitsu laptops (eg: the S6xxx series) are
  39. * also supported by this driver.
  40. *
  41. * This driver has been tested on a Fujitsu Lifebook S6410 and S7020. It
  42. * should work on most P-series and S-series Lifebooks, but YMMV.
  43. *
  44. * The module parameter use_alt_lcd_levels switches between different ACPI
  45. * brightness controls which are used by different Fujitsu laptops. In most
  46. * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
  47. * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
  48. *
  49. */
  50. #include <linux/module.h>
  51. #include <linux/kernel.h>
  52. #include <linux/init.h>
  53. #include <linux/acpi.h>
  54. #include <linux/dmi.h>
  55. #include <linux/backlight.h>
  56. #include <linux/input.h>
  57. #include <linux/kfifo.h>
  58. #include <linux/video_output.h>
  59. #include <linux/platform_device.h>
  60. #define FUJITSU_DRIVER_VERSION "0.4.2"
  61. #define FUJITSU_LCD_N_LEVELS 8
  62. #define ACPI_FUJITSU_CLASS "fujitsu"
  63. #define ACPI_FUJITSU_HID "FUJ02B1"
  64. #define ACPI_FUJITSU_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI brightness driver"
  65. #define ACPI_FUJITSU_DEVICE_NAME "Fujitsu FUJ02B1"
  66. #define ACPI_FUJITSU_HOTKEY_HID "FUJ02E3"
  67. #define ACPI_FUJITSU_HOTKEY_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
  68. #define ACPI_FUJITSU_HOTKEY_DEVICE_NAME "Fujitsu FUJ02E3"
  69. #define ACPI_FUJITSU_NOTIFY_CODE1 0x80
  70. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  71. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  72. /* Hotkey details */
  73. #define LOCK_KEY 0x410 /* codes for the keys in the GIRB register */
  74. #define DISPLAY_KEY 0x411 /* keys are mapped to KEY_SCREENLOCK (the key with the key symbol) */
  75. #define ENERGY_KEY 0x412 /* KEY_MEDIA (the key with the laptop symbol, KEY_EMAIL (E key)) */
  76. #define REST_KEY 0x413 /* KEY_SUSPEND (R key) */
  77. #define MAX_HOTKEY_RINGBUFFER_SIZE 100
  78. #define RINGBUFFERSIZE 40
  79. /* Debugging */
  80. #define FUJLAPTOP_LOG ACPI_FUJITSU_HID ": "
  81. #define FUJLAPTOP_ERR KERN_ERR FUJLAPTOP_LOG
  82. #define FUJLAPTOP_NOTICE KERN_NOTICE FUJLAPTOP_LOG
  83. #define FUJLAPTOP_INFO KERN_INFO FUJLAPTOP_LOG
  84. #define FUJLAPTOP_DEBUG KERN_DEBUG FUJLAPTOP_LOG
  85. #define FUJLAPTOP_DBG_ALL 0xffff
  86. #define FUJLAPTOP_DBG_ERROR 0x0001
  87. #define FUJLAPTOP_DBG_WARN 0x0002
  88. #define FUJLAPTOP_DBG_INFO 0x0004
  89. #define FUJLAPTOP_DBG_TRACE 0x0008
  90. #define dbg_printk(a_dbg_level, format, arg...) \
  91. do { if (dbg_level & a_dbg_level) \
  92. printk(FUJLAPTOP_DEBUG "%s: " format, __func__ , ## arg); \
  93. } while (0)
  94. #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
  95. #define vdbg_printk(a_dbg_level, format, arg...) \
  96. dbg_printk(a_dbg_level, format, ## arg)
  97. #else
  98. #define vdbg_printk(a_dbg_level, format, arg...)
  99. #endif
  100. /* Device controlling the backlight and associated keys */
  101. struct fujitsu_t {
  102. acpi_handle acpi_handle;
  103. struct acpi_device *dev;
  104. struct input_dev *input;
  105. char phys[32];
  106. struct backlight_device *bl_device;
  107. struct platform_device *pf_device;
  108. unsigned int max_brightness;
  109. unsigned int brightness_changed;
  110. unsigned int brightness_level;
  111. };
  112. static struct fujitsu_t *fujitsu;
  113. static int use_alt_lcd_levels = -1;
  114. static int disable_brightness_keys = -1;
  115. static int disable_brightness_adjust = -1;
  116. /* Device used to access other hotkeys on the laptop */
  117. struct fujitsu_hotkey_t {
  118. acpi_handle acpi_handle;
  119. struct acpi_device *dev;
  120. struct input_dev *input;
  121. char phys[32];
  122. struct platform_device *pf_device;
  123. struct kfifo *fifo;
  124. spinlock_t fifo_lock;
  125. unsigned int irb; /* info about the pressed buttons */
  126. };
  127. static struct fujitsu_hotkey_t *fujitsu_hotkey;
  128. static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
  129. void *data);
  130. #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
  131. static u32 dbg_level = 0x03;
  132. #endif
  133. static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data);
  134. /* Hardware access for LCD brightness control */
  135. static int set_lcd_level(int level)
  136. {
  137. acpi_status status = AE_OK;
  138. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  139. struct acpi_object_list arg_list = { 1, &arg0 };
  140. acpi_handle handle = NULL;
  141. vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBLL [%d]\n",
  142. level);
  143. if (level < 0 || level >= fujitsu->max_brightness)
  144. return -EINVAL;
  145. if (!fujitsu)
  146. return -EINVAL;
  147. status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle);
  148. if (ACPI_FAILURE(status)) {
  149. vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBLL not present\n");
  150. return -ENODEV;
  151. }
  152. arg0.integer.value = level;
  153. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  154. if (ACPI_FAILURE(status))
  155. return -ENODEV;
  156. return 0;
  157. }
  158. static int set_lcd_level_alt(int level)
  159. {
  160. acpi_status status = AE_OK;
  161. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  162. struct acpi_object_list arg_list = { 1, &arg0 };
  163. acpi_handle handle = NULL;
  164. vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBL2 [%d]\n",
  165. level);
  166. if (level < 0 || level >= fujitsu->max_brightness)
  167. return -EINVAL;
  168. if (!fujitsu)
  169. return -EINVAL;
  170. status = acpi_get_handle(fujitsu->acpi_handle, "SBL2", &handle);
  171. if (ACPI_FAILURE(status)) {
  172. vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBL2 not present\n");
  173. return -ENODEV;
  174. }
  175. arg0.integer.value = level;
  176. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  177. if (ACPI_FAILURE(status))
  178. return -ENODEV;
  179. return 0;
  180. }
  181. static int get_lcd_level(void)
  182. {
  183. unsigned long state = 0;
  184. acpi_status status = AE_OK;
  185. vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
  186. status =
  187. acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
  188. if (status < 0)
  189. return status;
  190. fujitsu->brightness_level = state & 0x0fffffff;
  191. if (state & 0x80000000)
  192. fujitsu->brightness_changed = 1;
  193. else
  194. fujitsu->brightness_changed = 0;
  195. return fujitsu->brightness_level;
  196. }
  197. static int get_max_brightness(void)
  198. {
  199. unsigned long state = 0;
  200. acpi_status status = AE_OK;
  201. vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
  202. status =
  203. acpi_evaluate_integer(fujitsu->acpi_handle, "RBLL", NULL, &state);
  204. if (status < 0)
  205. return status;
  206. fujitsu->max_brightness = state;
  207. return fujitsu->max_brightness;
  208. }
  209. static int get_lcd_level_alt(void)
  210. {
  211. unsigned long state = 0;
  212. acpi_status status = AE_OK;
  213. vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLS\n");
  214. status =
  215. acpi_evaluate_integer(fujitsu->acpi_handle, "GBLS", NULL, &state);
  216. if (status < 0)
  217. return status;
  218. fujitsu->brightness_level = state & 0x0fffffff;
  219. if (state & 0x80000000)
  220. fujitsu->brightness_changed = 1;
  221. else
  222. fujitsu->brightness_changed = 0;
  223. return fujitsu->brightness_level;
  224. }
  225. /* Backlight device stuff */
  226. static int bl_get_brightness(struct backlight_device *b)
  227. {
  228. if (use_alt_lcd_levels)
  229. return get_lcd_level_alt();
  230. else
  231. return get_lcd_level();
  232. }
  233. static int bl_update_status(struct backlight_device *b)
  234. {
  235. if (use_alt_lcd_levels)
  236. return set_lcd_level_alt(b->props.brightness);
  237. else
  238. return set_lcd_level(b->props.brightness);
  239. }
  240. static struct backlight_ops fujitsubl_ops = {
  241. .get_brightness = bl_get_brightness,
  242. .update_status = bl_update_status,
  243. };
  244. /* Platform LCD brightness device */
  245. static ssize_t
  246. show_max_brightness(struct device *dev,
  247. struct device_attribute *attr, char *buf)
  248. {
  249. int ret;
  250. ret = get_max_brightness();
  251. if (ret < 0)
  252. return ret;
  253. return sprintf(buf, "%i\n", ret);
  254. }
  255. static ssize_t
  256. show_brightness_changed(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. int ret;
  260. ret = fujitsu->brightness_changed;
  261. if (ret < 0)
  262. return ret;
  263. return sprintf(buf, "%i\n", ret);
  264. }
  265. static ssize_t show_lcd_level(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. int ret;
  269. if (use_alt_lcd_levels)
  270. ret = get_lcd_level_alt();
  271. else
  272. ret = get_lcd_level();
  273. if (ret < 0)
  274. return ret;
  275. return sprintf(buf, "%i\n", ret);
  276. }
  277. static ssize_t store_lcd_level(struct device *dev,
  278. struct device_attribute *attr, const char *buf,
  279. size_t count)
  280. {
  281. int level, ret;
  282. if (sscanf(buf, "%i", &level) != 1
  283. || (level < 0 || level >= fujitsu->max_brightness))
  284. return -EINVAL;
  285. if (use_alt_lcd_levels)
  286. ret = set_lcd_level_alt(level);
  287. else
  288. ret = set_lcd_level(level);
  289. if (ret < 0)
  290. return ret;
  291. if (use_alt_lcd_levels)
  292. ret = get_lcd_level_alt();
  293. else
  294. ret = get_lcd_level();
  295. if (ret < 0)
  296. return ret;
  297. return count;
  298. }
  299. /* Hardware access for hotkey device */
  300. static int get_irb(void)
  301. {
  302. unsigned long state = 0;
  303. acpi_status status = AE_OK;
  304. vdbg_printk(FUJLAPTOP_DBG_TRACE, "Get irb\n");
  305. status =
  306. acpi_evaluate_integer(fujitsu_hotkey->acpi_handle, "GIRB", NULL,
  307. &state);
  308. if (status < 0)
  309. return status;
  310. fujitsu_hotkey->irb = state;
  311. return fujitsu_hotkey->irb;
  312. }
  313. static ssize_t
  314. ignore_store(struct device *dev,
  315. struct device_attribute *attr, const char *buf, size_t count)
  316. {
  317. return count;
  318. }
  319. static DEVICE_ATTR(max_brightness, 0444, show_max_brightness, ignore_store);
  320. static DEVICE_ATTR(brightness_changed, 0444, show_brightness_changed,
  321. ignore_store);
  322. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  323. static struct attribute *fujitsupf_attributes[] = {
  324. &dev_attr_brightness_changed.attr,
  325. &dev_attr_max_brightness.attr,
  326. &dev_attr_lcd_level.attr,
  327. NULL
  328. };
  329. static struct attribute_group fujitsupf_attribute_group = {
  330. .attrs = fujitsupf_attributes
  331. };
  332. static struct platform_driver fujitsupf_driver = {
  333. .driver = {
  334. .name = "fujitsu-laptop",
  335. .owner = THIS_MODULE,
  336. }
  337. };
  338. static int dmi_check_cb_s6410(const struct dmi_system_id *id)
  339. {
  340. acpi_handle handle;
  341. int have_blnf;
  342. printk(KERN_INFO "fujitsu-laptop: Identified laptop model '%s'.\n",
  343. id->ident);
  344. have_blnf = ACPI_SUCCESS
  345. (acpi_get_handle(NULL, "\\_SB.PCI0.GFX0.LCD.BLNF", &handle));
  346. if (use_alt_lcd_levels == -1) {
  347. vdbg_printk(FUJLAPTOP_DBG_TRACE, "auto-detecting usealt\n");
  348. use_alt_lcd_levels = 1;
  349. }
  350. if (disable_brightness_keys == -1) {
  351. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  352. "auto-detecting disable_keys\n");
  353. disable_brightness_keys = have_blnf ? 1 : 0;
  354. }
  355. if (disable_brightness_adjust == -1) {
  356. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  357. "auto-detecting disable_adjust\n");
  358. disable_brightness_adjust = have_blnf ? 0 : 1;
  359. }
  360. return 0;
  361. }
  362. static struct dmi_system_id __initdata fujitsu_dmi_table[] = {
  363. {
  364. .ident = "Fujitsu Siemens",
  365. .matches = {
  366. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  367. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
  368. },
  369. .callback = dmi_check_cb_s6410},
  370. {
  371. .ident = "FUJITSU LifeBook P8010",
  372. .matches = {
  373. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  374. DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
  375. },
  376. .callback = dmi_check_cb_s6410},
  377. {}
  378. };
  379. /* ACPI device for LCD brightness control */
  380. static int acpi_fujitsu_add(struct acpi_device *device)
  381. {
  382. acpi_status status;
  383. acpi_handle handle;
  384. int result = 0;
  385. int state = 0;
  386. struct input_dev *input;
  387. int error;
  388. if (!device)
  389. return -EINVAL;
  390. fujitsu->acpi_handle = device->handle;
  391. sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
  392. sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
  393. acpi_driver_data(device) = fujitsu;
  394. status = acpi_install_notify_handler(device->handle,
  395. ACPI_DEVICE_NOTIFY,
  396. acpi_fujitsu_notify, fujitsu);
  397. if (ACPI_FAILURE(status)) {
  398. printk(KERN_ERR "Error installing notify handler\n");
  399. error = -ENODEV;
  400. goto err_stop;
  401. }
  402. fujitsu->input = input = input_allocate_device();
  403. if (!input) {
  404. error = -ENOMEM;
  405. goto err_uninstall_notify;
  406. }
  407. snprintf(fujitsu->phys, sizeof(fujitsu->phys),
  408. "%s/video/input0", acpi_device_hid(device));
  409. input->name = acpi_device_name(device);
  410. input->phys = fujitsu->phys;
  411. input->id.bustype = BUS_HOST;
  412. input->id.product = 0x06;
  413. input->dev.parent = &device->dev;
  414. input->evbit[0] = BIT(EV_KEY);
  415. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  416. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  417. set_bit(KEY_UNKNOWN, input->keybit);
  418. error = input_register_device(input);
  419. if (error)
  420. goto err_free_input_dev;
  421. result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
  422. if (result) {
  423. printk(KERN_ERR "Error reading power state\n");
  424. goto end;
  425. }
  426. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  427. acpi_device_name(device), acpi_device_bid(device),
  428. !device->power.state ? "on" : "off");
  429. fujitsu->dev = device;
  430. if (ACPI_SUCCESS
  431. (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
  432. vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
  433. if (ACPI_FAILURE
  434. (acpi_evaluate_object
  435. (device->handle, METHOD_NAME__INI, NULL, NULL)))
  436. printk(KERN_ERR "_INI Method failed\n");
  437. }
  438. /* do config (detect defaults) */
  439. dmi_check_system(fujitsu_dmi_table);
  440. use_alt_lcd_levels = use_alt_lcd_levels == 1 ? 1 : 0;
  441. disable_brightness_keys = disable_brightness_keys == 1 ? 1 : 0;
  442. disable_brightness_adjust = disable_brightness_adjust == 1 ? 1 : 0;
  443. vdbg_printk(FUJLAPTOP_DBG_INFO,
  444. "config: [alt interface: %d], [key disable: %d], [adjust disable: %d]\n",
  445. use_alt_lcd_levels, disable_brightness_keys,
  446. disable_brightness_adjust);
  447. if (get_max_brightness() <= 0)
  448. fujitsu->max_brightness = FUJITSU_LCD_N_LEVELS;
  449. if (use_alt_lcd_levels)
  450. get_lcd_level_alt();
  451. else
  452. get_lcd_level();
  453. return result;
  454. end:
  455. err_free_input_dev:
  456. input_free_device(input);
  457. err_uninstall_notify:
  458. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  459. acpi_fujitsu_notify);
  460. err_stop:
  461. return result;
  462. }
  463. static int acpi_fujitsu_remove(struct acpi_device *device, int type)
  464. {
  465. acpi_status status;
  466. struct fujitsu_t *fujitsu = NULL;
  467. if (!device || !acpi_driver_data(device))
  468. return -EINVAL;
  469. fujitsu = acpi_driver_data(device);
  470. status = acpi_remove_notify_handler(fujitsu->acpi_handle,
  471. ACPI_DEVICE_NOTIFY,
  472. acpi_fujitsu_notify);
  473. if (!device || !acpi_driver_data(device))
  474. return -EINVAL;
  475. fujitsu->acpi_handle = NULL;
  476. return 0;
  477. }
  478. /* Brightness notify */
  479. static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data)
  480. {
  481. struct input_dev *input;
  482. int keycode;
  483. int oldb, newb;
  484. input = fujitsu->input;
  485. switch (event) {
  486. case ACPI_FUJITSU_NOTIFY_CODE1:
  487. keycode = 0;
  488. oldb = fujitsu->brightness_level;
  489. get_lcd_level(); /* the alt version always yields changed */
  490. newb = fujitsu->brightness_level;
  491. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  492. "brightness button event [%i -> %i (%i)]\n",
  493. oldb, newb, fujitsu->brightness_changed);
  494. if (oldb == newb && fujitsu->brightness_changed) {
  495. keycode = 0;
  496. if (disable_brightness_keys != 1) {
  497. if (oldb == 0) {
  498. acpi_bus_generate_proc_event(fujitsu->
  499. dev,
  500. ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
  501. 0);
  502. keycode = KEY_BRIGHTNESSDOWN;
  503. } else if (oldb ==
  504. (fujitsu->max_brightness) - 1) {
  505. acpi_bus_generate_proc_event(fujitsu->
  506. dev,
  507. ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
  508. 0);
  509. keycode = KEY_BRIGHTNESSUP;
  510. }
  511. }
  512. } else if (oldb < newb) {
  513. if (disable_brightness_adjust != 1) {
  514. if (use_alt_lcd_levels)
  515. set_lcd_level_alt(newb);
  516. else
  517. set_lcd_level(newb);
  518. }
  519. if (disable_brightness_keys != 1) {
  520. acpi_bus_generate_proc_event(fujitsu->dev,
  521. ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
  522. 0);
  523. keycode = KEY_BRIGHTNESSUP;
  524. }
  525. } else if (oldb > newb) {
  526. if (disable_brightness_adjust != 1) {
  527. if (use_alt_lcd_levels)
  528. set_lcd_level_alt(newb);
  529. else
  530. set_lcd_level(newb);
  531. }
  532. if (disable_brightness_keys != 1) {
  533. acpi_bus_generate_proc_event(fujitsu->dev,
  534. ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
  535. 0);
  536. keycode = KEY_BRIGHTNESSDOWN;
  537. }
  538. } else {
  539. keycode = KEY_UNKNOWN;
  540. }
  541. break;
  542. default:
  543. keycode = KEY_UNKNOWN;
  544. vdbg_printk(FUJLAPTOP_DBG_WARN,
  545. "unsupported event [0x%x]\n", event);
  546. break;
  547. }
  548. if (keycode != 0) {
  549. input_report_key(input, keycode, 1);
  550. input_sync(input);
  551. input_report_key(input, keycode, 0);
  552. input_sync(input);
  553. }
  554. return;
  555. }
  556. /* ACPI device for hotkey handling */
  557. static int acpi_fujitsu_hotkey_add(struct acpi_device *device)
  558. {
  559. acpi_status status;
  560. acpi_handle handle;
  561. int result = 0;
  562. int state = 0;
  563. struct input_dev *input;
  564. int error;
  565. int i;
  566. if (!device)
  567. return -EINVAL;
  568. fujitsu_hotkey->acpi_handle = device->handle;
  569. sprintf(acpi_device_name(device), "%s",
  570. ACPI_FUJITSU_HOTKEY_DEVICE_NAME);
  571. sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
  572. acpi_driver_data(device) = fujitsu_hotkey;
  573. status = acpi_install_notify_handler(device->handle,
  574. ACPI_DEVICE_NOTIFY,
  575. acpi_fujitsu_hotkey_notify,
  576. fujitsu_hotkey);
  577. if (ACPI_FAILURE(status)) {
  578. printk(KERN_ERR "Error installing notify handler\n");
  579. error = -ENODEV;
  580. goto err_stop;
  581. }
  582. /* kfifo */
  583. spin_lock_init(&fujitsu_hotkey->fifo_lock);
  584. fujitsu_hotkey->fifo =
  585. kfifo_alloc(RINGBUFFERSIZE * sizeof(int), GFP_KERNEL,
  586. &fujitsu_hotkey->fifo_lock);
  587. if (IS_ERR(fujitsu_hotkey->fifo)) {
  588. printk(KERN_ERR "kfifo_alloc failed\n");
  589. error = PTR_ERR(fujitsu_hotkey->fifo);
  590. goto err_stop;
  591. }
  592. fujitsu_hotkey->input = input = input_allocate_device();
  593. if (!input) {
  594. error = -ENOMEM;
  595. goto err_uninstall_notify;
  596. }
  597. snprintf(fujitsu_hotkey->phys, sizeof(fujitsu_hotkey->phys),
  598. "%s/video/input0", acpi_device_hid(device));
  599. input->name = acpi_device_name(device);
  600. input->phys = fujitsu_hotkey->phys;
  601. input->id.bustype = BUS_HOST;
  602. input->id.product = 0x06;
  603. input->dev.parent = &device->dev;
  604. input->evbit[0] = BIT(EV_KEY);
  605. set_bit(KEY_SCREENLOCK, input->keybit);
  606. set_bit(KEY_MEDIA, input->keybit);
  607. set_bit(KEY_EMAIL, input->keybit);
  608. set_bit(KEY_SUSPEND, input->keybit);
  609. set_bit(KEY_UNKNOWN, input->keybit);
  610. error = input_register_device(input);
  611. if (error)
  612. goto err_free_input_dev;
  613. result = acpi_bus_get_power(fujitsu_hotkey->acpi_handle, &state);
  614. if (result) {
  615. printk(KERN_ERR "Error reading power state\n");
  616. goto end;
  617. }
  618. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  619. acpi_device_name(device), acpi_device_bid(device),
  620. !device->power.state ? "on" : "off");
  621. fujitsu_hotkey->dev = device;
  622. if (ACPI_SUCCESS
  623. (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
  624. vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
  625. if (ACPI_FAILURE
  626. (acpi_evaluate_object
  627. (device->handle, METHOD_NAME__INI, NULL, NULL)))
  628. printk(KERN_ERR "_INI Method failed\n");
  629. }
  630. i = 0; /* Discard hotkey ringbuffer */
  631. while (get_irb() != 0 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) ;
  632. vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
  633. return result;
  634. end:
  635. err_free_input_dev:
  636. input_free_device(input);
  637. err_uninstall_notify:
  638. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  639. acpi_fujitsu_hotkey_notify);
  640. kfifo_free(fujitsu_hotkey->fifo);
  641. err_stop:
  642. return result;
  643. }
  644. static int acpi_fujitsu_hotkey_remove(struct acpi_device *device, int type)
  645. {
  646. acpi_status status;
  647. struct fujitsu_hotkey_t *fujitsu_hotkey = NULL;
  648. if (!device || !acpi_driver_data(device))
  649. return -EINVAL;
  650. fujitsu_hotkey = acpi_driver_data(device);
  651. status = acpi_remove_notify_handler(fujitsu_hotkey->acpi_handle,
  652. ACPI_DEVICE_NOTIFY,
  653. acpi_fujitsu_hotkey_notify);
  654. fujitsu_hotkey->acpi_handle = NULL;
  655. kfifo_free(fujitsu_hotkey->fifo);
  656. return 0;
  657. }
  658. static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
  659. void *data)
  660. {
  661. struct input_dev *input;
  662. int keycode, keycode_r;
  663. unsigned int irb = 1;
  664. int i, status;
  665. input = fujitsu_hotkey->input;
  666. vdbg_printk(FUJLAPTOP_DBG_TRACE, "Hotkey event\n");
  667. switch (event) {
  668. case ACPI_FUJITSU_NOTIFY_CODE1:
  669. i = 0;
  670. while ((irb = get_irb()) != 0
  671. && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) {
  672. vdbg_printk(FUJLAPTOP_DBG_TRACE, "GIRB result [%x]\n",
  673. irb);
  674. switch (irb & 0x4ff) {
  675. case LOCK_KEY:
  676. keycode = KEY_SCREENLOCK;
  677. break;
  678. case DISPLAY_KEY:
  679. keycode = KEY_MEDIA;
  680. break;
  681. case ENERGY_KEY:
  682. keycode = KEY_EMAIL;
  683. break;
  684. case REST_KEY:
  685. keycode = KEY_SUSPEND;
  686. break;
  687. case 0:
  688. keycode = 0;
  689. break;
  690. default:
  691. vdbg_printk(FUJLAPTOP_DBG_WARN,
  692. "Unknown GIRB result [%x]\n", irb);
  693. keycode = -1;
  694. break;
  695. }
  696. if (keycode > 0) {
  697. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  698. "Push keycode into ringbuffer [%d]\n",
  699. keycode);
  700. status = kfifo_put(fujitsu_hotkey->fifo,
  701. (unsigned char *)&keycode,
  702. sizeof(keycode));
  703. if (status != sizeof(keycode)) {
  704. vdbg_printk(FUJLAPTOP_DBG_WARN,
  705. "Could not push keycode [0x%x]\n",
  706. keycode);
  707. } else {
  708. input_report_key(input, keycode, 1);
  709. input_sync(input);
  710. }
  711. } else if (keycode == 0) {
  712. while ((status =
  713. kfifo_get
  714. (fujitsu_hotkey->fifo, (unsigned char *)
  715. &keycode_r,
  716. sizeof
  717. (keycode_r))) == sizeof(keycode_r)) {
  718. input_report_key(input, keycode_r, 0);
  719. input_sync(input);
  720. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  721. "Pop keycode from ringbuffer [%d]\n",
  722. keycode_r);
  723. }
  724. }
  725. }
  726. break;
  727. default:
  728. keycode = KEY_UNKNOWN;
  729. vdbg_printk(FUJLAPTOP_DBG_WARN,
  730. "Unsupported event [0x%x]\n", event);
  731. input_report_key(input, keycode, 1);
  732. input_sync(input);
  733. input_report_key(input, keycode, 0);
  734. input_sync(input);
  735. break;
  736. }
  737. return;
  738. }
  739. /* Initialization */
  740. static const struct acpi_device_id fujitsu_device_ids[] = {
  741. {ACPI_FUJITSU_HID, 0},
  742. {"", 0},
  743. };
  744. static struct acpi_driver acpi_fujitsu_driver = {
  745. .name = ACPI_FUJITSU_DRIVER_NAME,
  746. .class = ACPI_FUJITSU_CLASS,
  747. .ids = fujitsu_device_ids,
  748. .ops = {
  749. .add = acpi_fujitsu_add,
  750. .remove = acpi_fujitsu_remove,
  751. },
  752. };
  753. static const struct acpi_device_id fujitsu_hotkey_device_ids[] = {
  754. {ACPI_FUJITSU_HOTKEY_HID, 0},
  755. {"", 0},
  756. };
  757. static struct acpi_driver acpi_fujitsu_hotkey_driver = {
  758. .name = ACPI_FUJITSU_HOTKEY_DRIVER_NAME,
  759. .class = ACPI_FUJITSU_CLASS,
  760. .ids = fujitsu_hotkey_device_ids,
  761. .ops = {
  762. .add = acpi_fujitsu_hotkey_add,
  763. .remove = acpi_fujitsu_hotkey_remove,
  764. },
  765. };
  766. static int __init fujitsu_init(void)
  767. {
  768. int ret, result, max_brightness;
  769. if (acpi_disabled)
  770. return -ENODEV;
  771. fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
  772. if (!fujitsu)
  773. return -ENOMEM;
  774. memset(fujitsu, 0, sizeof(struct fujitsu_t));
  775. result = acpi_bus_register_driver(&acpi_fujitsu_driver);
  776. if (result < 0) {
  777. ret = -ENODEV;
  778. goto fail_acpi;
  779. }
  780. /* Register platform stuff */
  781. fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
  782. if (!fujitsu->pf_device) {
  783. ret = -ENOMEM;
  784. goto fail_platform_driver;
  785. }
  786. ret = platform_device_add(fujitsu->pf_device);
  787. if (ret)
  788. goto fail_platform_device1;
  789. ret =
  790. sysfs_create_group(&fujitsu->pf_device->dev.kobj,
  791. &fujitsupf_attribute_group);
  792. if (ret)
  793. goto fail_platform_device2;
  794. /* Register backlight stuff */
  795. fujitsu->bl_device =
  796. backlight_device_register("fujitsu-laptop", NULL, NULL,
  797. &fujitsubl_ops);
  798. if (IS_ERR(fujitsu->bl_device))
  799. return PTR_ERR(fujitsu->bl_device);
  800. max_brightness = fujitsu->max_brightness;
  801. fujitsu->bl_device->props.max_brightness = max_brightness - 1;
  802. fujitsu->bl_device->props.brightness = fujitsu->brightness_level;
  803. ret = platform_driver_register(&fujitsupf_driver);
  804. if (ret)
  805. goto fail_backlight;
  806. /* Register hotkey driver */
  807. fujitsu_hotkey = kmalloc(sizeof(struct fujitsu_hotkey_t), GFP_KERNEL);
  808. if (!fujitsu_hotkey) {
  809. ret = -ENOMEM;
  810. goto fail_hotkey;
  811. }
  812. memset(fujitsu_hotkey, 0, sizeof(struct fujitsu_hotkey_t));
  813. result = acpi_bus_register_driver(&acpi_fujitsu_hotkey_driver);
  814. if (result < 0) {
  815. ret = -ENODEV;
  816. goto fail_hotkey1;
  817. }
  818. printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
  819. " successfully loaded.\n");
  820. return 0;
  821. fail_hotkey1:
  822. kfree(fujitsu_hotkey);
  823. fail_hotkey:
  824. platform_driver_unregister(&fujitsupf_driver);
  825. fail_backlight:
  826. backlight_device_unregister(fujitsu->bl_device);
  827. fail_platform_device2:
  828. platform_device_del(fujitsu->pf_device);
  829. fail_platform_device1:
  830. platform_device_put(fujitsu->pf_device);
  831. fail_platform_driver:
  832. acpi_bus_unregister_driver(&acpi_fujitsu_driver);
  833. fail_acpi:
  834. kfree(fujitsu);
  835. return ret;
  836. }
  837. static void __exit fujitsu_cleanup(void)
  838. {
  839. sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
  840. &fujitsupf_attribute_group);
  841. platform_device_unregister(fujitsu->pf_device);
  842. platform_driver_unregister(&fujitsupf_driver);
  843. backlight_device_unregister(fujitsu->bl_device);
  844. acpi_bus_unregister_driver(&acpi_fujitsu_driver);
  845. kfree(fujitsu);
  846. acpi_bus_unregister_driver(&acpi_fujitsu_hotkey_driver);
  847. kfree(fujitsu_hotkey);
  848. printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
  849. }
  850. module_init(fujitsu_init);
  851. module_exit(fujitsu_cleanup);
  852. module_param(use_alt_lcd_levels, uint, 0644);
  853. MODULE_PARM_DESC(use_alt_lcd_levels,
  854. "Use alternative interface for lcd_levels (needed for Lifebook s6410).");
  855. module_param(disable_brightness_keys, uint, 0644);
  856. MODULE_PARM_DESC(disable_brightness_keys,
  857. "Disable brightness keys (eg. if they are already handled by the generic ACPI_VIDEO device).");
  858. module_param(disable_brightness_adjust, uint, 0644);
  859. MODULE_PARM_DESC(disable_brightness_adjust, "Disable brightness adjustment .");
  860. #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
  861. module_param_named(debug, dbg_level, uint, 0644);
  862. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  863. #endif
  864. MODULE_AUTHOR("Jonathan Woithe, Peter Gruber");
  865. MODULE_DESCRIPTION("Fujitsu laptop extras support");
  866. MODULE_VERSION(FUJITSU_DRIVER_VERSION);
  867. MODULE_LICENSE("GPL");
  868. MODULE_ALIAS
  869. ("dmi:*:svnFUJITSUSIEMENS:*:pvr:rvnFUJITSU:rnFJNB1D3:*:cvrS6410:*");
  870. MODULE_ALIAS
  871. ("dmi:*:svnFUJITSU:*:pvr:rvnFUJITSU:rnFJNB19C:*:cvrS7020:*");
  872. static struct pnp_device_id pnp_ids[] = {
  873. { .id = "FUJ02bf" },
  874. { .id = "FUJ02B1" },
  875. { .id = "FUJ02E3" },
  876. { .id = "" }
  877. };
  878. MODULE_DEVICE_TABLE(pnp, pnp_ids);