fujitsu-laptop.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. };
  372. /* ACPI device for LCD brightness control */
  373. static int acpi_fujitsu_add(struct acpi_device *device)
  374. {
  375. acpi_status status;
  376. acpi_handle handle;
  377. int result = 0;
  378. int state = 0;
  379. struct input_dev *input;
  380. int error;
  381. if (!device)
  382. return -EINVAL;
  383. fujitsu->acpi_handle = device->handle;
  384. sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
  385. sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
  386. acpi_driver_data(device) = fujitsu;
  387. status = acpi_install_notify_handler(device->handle,
  388. ACPI_DEVICE_NOTIFY,
  389. acpi_fujitsu_notify, fujitsu);
  390. if (ACPI_FAILURE(status)) {
  391. printk(KERN_ERR "Error installing notify handler\n");
  392. error = -ENODEV;
  393. goto err_stop;
  394. }
  395. fujitsu->input = input = input_allocate_device();
  396. if (!input) {
  397. error = -ENOMEM;
  398. goto err_uninstall_notify;
  399. }
  400. snprintf(fujitsu->phys, sizeof(fujitsu->phys),
  401. "%s/video/input0", acpi_device_hid(device));
  402. input->name = acpi_device_name(device);
  403. input->phys = fujitsu->phys;
  404. input->id.bustype = BUS_HOST;
  405. input->id.product = 0x06;
  406. input->dev.parent = &device->dev;
  407. input->evbit[0] = BIT(EV_KEY);
  408. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  409. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  410. set_bit(KEY_UNKNOWN, input->keybit);
  411. error = input_register_device(input);
  412. if (error)
  413. goto err_free_input_dev;
  414. result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
  415. if (result) {
  416. printk(KERN_ERR "Error reading power state\n");
  417. goto end;
  418. }
  419. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  420. acpi_device_name(device), acpi_device_bid(device),
  421. !device->power.state ? "on" : "off");
  422. fujitsu->dev = device;
  423. if (ACPI_SUCCESS
  424. (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
  425. vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
  426. if (ACPI_FAILURE
  427. (acpi_evaluate_object
  428. (device->handle, METHOD_NAME__INI, NULL, NULL)))
  429. printk(KERN_ERR "_INI Method failed\n");
  430. }
  431. /* do config (detect defaults) */
  432. dmi_check_system(fujitsu_dmi_table);
  433. use_alt_lcd_levels = use_alt_lcd_levels == 1 ? 1 : 0;
  434. disable_brightness_keys = disable_brightness_keys == 1 ? 1 : 0;
  435. disable_brightness_adjust = disable_brightness_adjust == 1 ? 1 : 0;
  436. vdbg_printk(FUJLAPTOP_DBG_INFO,
  437. "config: [alt interface: %d], [key disable: %d], [adjust disable: %d]\n",
  438. use_alt_lcd_levels, disable_brightness_keys,
  439. disable_brightness_adjust);
  440. if (get_max_brightness() <= 0)
  441. fujitsu->max_brightness = FUJITSU_LCD_N_LEVELS;
  442. if (use_alt_lcd_levels)
  443. get_lcd_level_alt();
  444. else
  445. get_lcd_level();
  446. return result;
  447. end:
  448. err_free_input_dev:
  449. input_free_device(input);
  450. err_uninstall_notify:
  451. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  452. acpi_fujitsu_notify);
  453. err_stop:
  454. return result;
  455. }
  456. static int acpi_fujitsu_remove(struct acpi_device *device, int type)
  457. {
  458. acpi_status status;
  459. struct fujitsu_t *fujitsu = NULL;
  460. if (!device || !acpi_driver_data(device))
  461. return -EINVAL;
  462. fujitsu = acpi_driver_data(device);
  463. status = acpi_remove_notify_handler(fujitsu->acpi_handle,
  464. ACPI_DEVICE_NOTIFY,
  465. acpi_fujitsu_notify);
  466. if (!device || !acpi_driver_data(device))
  467. return -EINVAL;
  468. fujitsu->acpi_handle = NULL;
  469. return 0;
  470. }
  471. /* Brightness notify */
  472. static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data)
  473. {
  474. struct input_dev *input;
  475. int keycode;
  476. int oldb, newb;
  477. input = fujitsu->input;
  478. switch (event) {
  479. case ACPI_FUJITSU_NOTIFY_CODE1:
  480. keycode = 0;
  481. oldb = fujitsu->brightness_level;
  482. get_lcd_level(); /* the alt version always yields changed */
  483. newb = fujitsu->brightness_level;
  484. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  485. "brightness button event [%i -> %i (%i)]\n",
  486. oldb, newb, fujitsu->brightness_changed);
  487. if (oldb == newb && fujitsu->brightness_changed) {
  488. keycode = 0;
  489. if (disable_brightness_keys != 1) {
  490. if (oldb == 0) {
  491. acpi_bus_generate_proc_event(fujitsu->
  492. dev,
  493. ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
  494. 0);
  495. keycode = KEY_BRIGHTNESSDOWN;
  496. } else if (oldb ==
  497. (fujitsu->max_brightness) - 1) {
  498. acpi_bus_generate_proc_event(fujitsu->
  499. dev,
  500. ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
  501. 0);
  502. keycode = KEY_BRIGHTNESSUP;
  503. }
  504. }
  505. } else if (oldb < newb) {
  506. if (disable_brightness_adjust != 1) {
  507. if (use_alt_lcd_levels)
  508. set_lcd_level_alt(newb);
  509. else
  510. set_lcd_level(newb);
  511. }
  512. if (disable_brightness_keys != 1) {
  513. acpi_bus_generate_proc_event(fujitsu->dev,
  514. ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
  515. 0);
  516. keycode = KEY_BRIGHTNESSUP;
  517. }
  518. } else if (oldb > newb) {
  519. if (disable_brightness_adjust != 1) {
  520. if (use_alt_lcd_levels)
  521. set_lcd_level_alt(newb);
  522. else
  523. set_lcd_level(newb);
  524. }
  525. if (disable_brightness_keys != 1) {
  526. acpi_bus_generate_proc_event(fujitsu->dev,
  527. ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
  528. 0);
  529. keycode = KEY_BRIGHTNESSDOWN;
  530. }
  531. } else {
  532. keycode = KEY_UNKNOWN;
  533. }
  534. break;
  535. default:
  536. keycode = KEY_UNKNOWN;
  537. vdbg_printk(FUJLAPTOP_DBG_WARN,
  538. "unsupported event [0x%x]\n", event);
  539. break;
  540. }
  541. if (keycode != 0) {
  542. input_report_key(input, keycode, 1);
  543. input_sync(input);
  544. input_report_key(input, keycode, 0);
  545. input_sync(input);
  546. }
  547. return;
  548. }
  549. /* ACPI device for hotkey handling */
  550. static int acpi_fujitsu_hotkey_add(struct acpi_device *device)
  551. {
  552. acpi_status status;
  553. acpi_handle handle;
  554. int result = 0;
  555. int state = 0;
  556. struct input_dev *input;
  557. int error;
  558. int i;
  559. if (!device)
  560. return -EINVAL;
  561. fujitsu_hotkey->acpi_handle = device->handle;
  562. sprintf(acpi_device_name(device), "%s",
  563. ACPI_FUJITSU_HOTKEY_DEVICE_NAME);
  564. sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
  565. acpi_driver_data(device) = fujitsu_hotkey;
  566. status = acpi_install_notify_handler(device->handle,
  567. ACPI_DEVICE_NOTIFY,
  568. acpi_fujitsu_hotkey_notify,
  569. fujitsu_hotkey);
  570. if (ACPI_FAILURE(status)) {
  571. printk(KERN_ERR "Error installing notify handler\n");
  572. error = -ENODEV;
  573. goto err_stop;
  574. }
  575. /* kfifo */
  576. spin_lock_init(&fujitsu_hotkey->fifo_lock);
  577. fujitsu_hotkey->fifo =
  578. kfifo_alloc(RINGBUFFERSIZE * sizeof(int), GFP_KERNEL,
  579. &fujitsu_hotkey->fifo_lock);
  580. if (IS_ERR(fujitsu_hotkey->fifo)) {
  581. printk(KERN_ERR "kfifo_alloc failed\n");
  582. error = PTR_ERR(fujitsu_hotkey->fifo);
  583. goto err_stop;
  584. }
  585. fujitsu_hotkey->input = input = input_allocate_device();
  586. if (!input) {
  587. error = -ENOMEM;
  588. goto err_uninstall_notify;
  589. }
  590. snprintf(fujitsu_hotkey->phys, sizeof(fujitsu_hotkey->phys),
  591. "%s/video/input0", acpi_device_hid(device));
  592. input->name = acpi_device_name(device);
  593. input->phys = fujitsu_hotkey->phys;
  594. input->id.bustype = BUS_HOST;
  595. input->id.product = 0x06;
  596. input->dev.parent = &device->dev;
  597. input->evbit[0] = BIT(EV_KEY);
  598. set_bit(KEY_SCREENLOCK, input->keybit);
  599. set_bit(KEY_MEDIA, input->keybit);
  600. set_bit(KEY_EMAIL, input->keybit);
  601. set_bit(KEY_SUSPEND, input->keybit);
  602. set_bit(KEY_UNKNOWN, input->keybit);
  603. error = input_register_device(input);
  604. if (error)
  605. goto err_free_input_dev;
  606. result = acpi_bus_get_power(fujitsu_hotkey->acpi_handle, &state);
  607. if (result) {
  608. printk(KERN_ERR "Error reading power state\n");
  609. goto end;
  610. }
  611. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  612. acpi_device_name(device), acpi_device_bid(device),
  613. !device->power.state ? "on" : "off");
  614. fujitsu_hotkey->dev = device;
  615. if (ACPI_SUCCESS
  616. (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
  617. vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
  618. if (ACPI_FAILURE
  619. (acpi_evaluate_object
  620. (device->handle, METHOD_NAME__INI, NULL, NULL)))
  621. printk(KERN_ERR "_INI Method failed\n");
  622. }
  623. i = 0; /* Discard hotkey ringbuffer */
  624. while (get_irb() != 0 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) ;
  625. vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
  626. return result;
  627. end:
  628. err_free_input_dev:
  629. input_free_device(input);
  630. err_uninstall_notify:
  631. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  632. acpi_fujitsu_hotkey_notify);
  633. kfifo_free(fujitsu_hotkey->fifo);
  634. err_stop:
  635. return result;
  636. }
  637. static int acpi_fujitsu_hotkey_remove(struct acpi_device *device, int type)
  638. {
  639. acpi_status status;
  640. struct fujitsu_hotkey_t *fujitsu_hotkey = NULL;
  641. if (!device || !acpi_driver_data(device))
  642. return -EINVAL;
  643. fujitsu_hotkey = acpi_driver_data(device);
  644. status = acpi_remove_notify_handler(fujitsu_hotkey->acpi_handle,
  645. ACPI_DEVICE_NOTIFY,
  646. acpi_fujitsu_hotkey_notify);
  647. fujitsu_hotkey->acpi_handle = NULL;
  648. kfifo_free(fujitsu_hotkey->fifo);
  649. return 0;
  650. }
  651. static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
  652. void *data)
  653. {
  654. struct input_dev *input;
  655. int keycode, keycode_r;
  656. unsigned int irb = 1;
  657. int i, status;
  658. input = fujitsu_hotkey->input;
  659. vdbg_printk(FUJLAPTOP_DBG_TRACE, "Hotkey event\n");
  660. switch (event) {
  661. case ACPI_FUJITSU_NOTIFY_CODE1:
  662. i = 0;
  663. while ((irb = get_irb()) != 0
  664. && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) {
  665. vdbg_printk(FUJLAPTOP_DBG_TRACE, "GIRB result [%x]\n",
  666. irb);
  667. switch (irb & 0x4ff) {
  668. case LOCK_KEY:
  669. keycode = KEY_SCREENLOCK;
  670. break;
  671. case DISPLAY_KEY:
  672. keycode = KEY_MEDIA;
  673. break;
  674. case ENERGY_KEY:
  675. keycode = KEY_EMAIL;
  676. break;
  677. case REST_KEY:
  678. keycode = KEY_SUSPEND;
  679. break;
  680. case 0:
  681. keycode = 0;
  682. break;
  683. default:
  684. vdbg_printk(FUJLAPTOP_DBG_WARN,
  685. "Unknown GIRB result [%x]\n", irb);
  686. keycode = -1;
  687. break;
  688. }
  689. if (keycode > 0) {
  690. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  691. "Push keycode into ringbuffer [%d]\n",
  692. keycode);
  693. status = kfifo_put(fujitsu_hotkey->fifo,
  694. (unsigned char *)&keycode,
  695. sizeof(keycode));
  696. if (status != sizeof(keycode)) {
  697. vdbg_printk(FUJLAPTOP_DBG_WARN,
  698. "Could not push keycode [0x%x]\n",
  699. keycode);
  700. } else {
  701. input_report_key(input, keycode, 1);
  702. input_sync(input);
  703. }
  704. } else if (keycode == 0) {
  705. while ((status =
  706. kfifo_get
  707. (fujitsu_hotkey->fifo, (unsigned char *)
  708. &keycode_r,
  709. sizeof
  710. (keycode_r))) == sizeof(keycode_r)) {
  711. input_report_key(input, keycode_r, 0);
  712. input_sync(input);
  713. vdbg_printk(FUJLAPTOP_DBG_TRACE,
  714. "Pop keycode from ringbuffer [%d]\n",
  715. keycode_r);
  716. }
  717. }
  718. }
  719. break;
  720. default:
  721. keycode = KEY_UNKNOWN;
  722. vdbg_printk(FUJLAPTOP_DBG_WARN,
  723. "Unsupported event [0x%x]\n", event);
  724. input_report_key(input, keycode, 1);
  725. input_sync(input);
  726. input_report_key(input, keycode, 0);
  727. input_sync(input);
  728. break;
  729. }
  730. return;
  731. }
  732. /* Initialization */
  733. static const struct acpi_device_id fujitsu_device_ids[] = {
  734. {ACPI_FUJITSU_HID, 0},
  735. {"", 0},
  736. };
  737. static struct acpi_driver acpi_fujitsu_driver = {
  738. .name = ACPI_FUJITSU_DRIVER_NAME,
  739. .class = ACPI_FUJITSU_CLASS,
  740. .ids = fujitsu_device_ids,
  741. .ops = {
  742. .add = acpi_fujitsu_add,
  743. .remove = acpi_fujitsu_remove,
  744. },
  745. };
  746. static const struct acpi_device_id fujitsu_hotkey_device_ids[] = {
  747. {ACPI_FUJITSU_HOTKEY_HID, 0},
  748. {"", 0},
  749. };
  750. static struct acpi_driver acpi_fujitsu_hotkey_driver = {
  751. .name = ACPI_FUJITSU_HOTKEY_DRIVER_NAME,
  752. .class = ACPI_FUJITSU_CLASS,
  753. .ids = fujitsu_hotkey_device_ids,
  754. .ops = {
  755. .add = acpi_fujitsu_hotkey_add,
  756. .remove = acpi_fujitsu_hotkey_remove,
  757. },
  758. };
  759. static int __init fujitsu_init(void)
  760. {
  761. int ret, result, max_brightness;
  762. if (acpi_disabled)
  763. return -ENODEV;
  764. fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
  765. if (!fujitsu)
  766. return -ENOMEM;
  767. memset(fujitsu, 0, sizeof(struct fujitsu_t));
  768. result = acpi_bus_register_driver(&acpi_fujitsu_driver);
  769. if (result < 0) {
  770. ret = -ENODEV;
  771. goto fail_acpi;
  772. }
  773. /* Register platform stuff */
  774. fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
  775. if (!fujitsu->pf_device) {
  776. ret = -ENOMEM;
  777. goto fail_platform_driver;
  778. }
  779. ret = platform_device_add(fujitsu->pf_device);
  780. if (ret)
  781. goto fail_platform_device1;
  782. ret =
  783. sysfs_create_group(&fujitsu->pf_device->dev.kobj,
  784. &fujitsupf_attribute_group);
  785. if (ret)
  786. goto fail_platform_device2;
  787. /* Register backlight stuff */
  788. fujitsu->bl_device =
  789. backlight_device_register("fujitsu-laptop", NULL, NULL,
  790. &fujitsubl_ops);
  791. if (IS_ERR(fujitsu->bl_device))
  792. return PTR_ERR(fujitsu->bl_device);
  793. max_brightness = fujitsu->max_brightness;
  794. fujitsu->bl_device->props.max_brightness = max_brightness - 1;
  795. fujitsu->bl_device->props.brightness = fujitsu->brightness_level;
  796. ret = platform_driver_register(&fujitsupf_driver);
  797. if (ret)
  798. goto fail_backlight;
  799. /* Register hotkey driver */
  800. fujitsu_hotkey = kmalloc(sizeof(struct fujitsu_hotkey_t), GFP_KERNEL);
  801. if (!fujitsu_hotkey) {
  802. ret = -ENOMEM;
  803. goto fail_hotkey;
  804. }
  805. memset(fujitsu_hotkey, 0, sizeof(struct fujitsu_hotkey_t));
  806. result = acpi_bus_register_driver(&acpi_fujitsu_hotkey_driver);
  807. if (result < 0) {
  808. ret = -ENODEV;
  809. goto fail_hotkey1;
  810. }
  811. printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
  812. " successfully loaded.\n");
  813. return 0;
  814. fail_hotkey1:
  815. kfree(fujitsu_hotkey);
  816. fail_hotkey:
  817. platform_driver_unregister(&fujitsupf_driver);
  818. fail_backlight:
  819. backlight_device_unregister(fujitsu->bl_device);
  820. fail_platform_device2:
  821. platform_device_del(fujitsu->pf_device);
  822. fail_platform_device1:
  823. platform_device_put(fujitsu->pf_device);
  824. fail_platform_driver:
  825. acpi_bus_unregister_driver(&acpi_fujitsu_driver);
  826. fail_acpi:
  827. kfree(fujitsu);
  828. return ret;
  829. }
  830. static void __exit fujitsu_cleanup(void)
  831. {
  832. sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
  833. &fujitsupf_attribute_group);
  834. platform_device_unregister(fujitsu->pf_device);
  835. platform_driver_unregister(&fujitsupf_driver);
  836. backlight_device_unregister(fujitsu->bl_device);
  837. acpi_bus_unregister_driver(&acpi_fujitsu_driver);
  838. kfree(fujitsu);
  839. acpi_bus_unregister_driver(&acpi_fujitsu_hotkey_driver);
  840. kfree(fujitsu_hotkey);
  841. printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
  842. }
  843. module_init(fujitsu_init);
  844. module_exit(fujitsu_cleanup);
  845. module_param(use_alt_lcd_levels, uint, 0644);
  846. MODULE_PARM_DESC(use_alt_lcd_levels,
  847. "Use alternative interface for lcd_levels (needed for Lifebook s6410).");
  848. module_param(disable_brightness_keys, uint, 0644);
  849. MODULE_PARM_DESC(disable_brightness_keys,
  850. "Disable brightness keys (eg. if they are already handled by the generic ACPI_VIDEO device).");
  851. module_param(disable_brightness_adjust, uint, 0644);
  852. MODULE_PARM_DESC(disable_brightness_adjust, "Disable brightness adjustment .");
  853. #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
  854. module_param_named(debug, dbg_level, uint, 0644);
  855. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  856. #endif
  857. MODULE_AUTHOR("Jonathan Woithe, Peter Gruber");
  858. MODULE_DESCRIPTION("Fujitsu laptop extras support");
  859. MODULE_VERSION(FUJITSU_DRIVER_VERSION);
  860. MODULE_LICENSE("GPL");
  861. MODULE_ALIAS
  862. ("dmi:*:svnFUJITSUSIEMENS:*:pvr:rvnFUJITSU:rnFJNB1D3:*:cvrS6410:*");
  863. MODULE_ALIAS
  864. ("dmi:*:svnFUJITSU:*:pvr:rvnFUJITSU:rnFJNB19C:*:cvrS7020:*");
  865. static struct pnp_device_id pnp_ids[] = {
  866. { .id = "FUJ02bf" },
  867. { .id = "FUJ02B1" },
  868. { .id = "FUJ02E3" },
  869. { .id = "" }
  870. };
  871. MODULE_DEVICE_TABLE(pnp, pnp_ids);