panasonic-laptop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Panasonic HotKey and LCD brightness control driver
  3. * (C) 2004 Hiroshi Miura <miura@da-cha.org>
  4. * (C) 2004 NTT DATA Intellilink Co. http://www.intellilink.co.jp/
  5. * (C) YOKOTA Hiroshi <yokota (at) netlab. is. tsukuba. ac. jp>
  6. * (C) 2004 David Bronaugh <dbronaugh>
  7. * (C) 2006-2008 Harald Welte <laforge@gnumonks.org>
  8. *
  9. * derived from toshiba_acpi.c, Copyright (C) 2002-2004 John Belmonte
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * publicshed by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. *---------------------------------------------------------------------------
  25. *
  26. * ChangeLog:
  27. * Sep.23, 2008 Harald Welte <laforge@gnumonks.org>
  28. * -v0.95 rename driver from drivers/acpi/pcc_acpi.c to
  29. * drivers/misc/panasonic-laptop.c
  30. *
  31. * Jul.04, 2008 Harald Welte <laforge@gnumonks.org>
  32. * -v0.94 replace /proc interface with device attributes
  33. * support {set,get}keycode on th input device
  34. *
  35. * Jun.27, 2008 Harald Welte <laforge@gnumonks.org>
  36. * -v0.92 merge with 2.6.26-rc6 input API changes
  37. * remove broken <= 2.6.15 kernel support
  38. * resolve all compiler warnings
  39. * various coding style fixes (checkpatch.pl)
  40. * add support for backlight api
  41. * major code restructuring
  42. *
  43. * Dac.28, 2007 Harald Welte <laforge@gnumonks.org>
  44. * -v0.91 merge with 2.6.24-rc6 ACPI changes
  45. *
  46. * Nov.04, 2006 Hiroshi Miura <miura@da-cha.org>
  47. * -v0.9 remove warning about section reference.
  48. * remove acpi_os_free
  49. * add /proc/acpi/pcc/brightness interface for HAL access
  50. * merge dbronaugh's enhancement
  51. * Aug.17, 2004 David Bronaugh (dbronaugh)
  52. * - Added screen brightness setting interface
  53. * Thanks to FreeBSD crew (acpi_panasonic.c)
  54. * for the ideas I needed to accomplish it
  55. *
  56. * May.29, 2006 Hiroshi Miura <miura@da-cha.org>
  57. * -v0.8.4 follow to change keyinput structure
  58. * thanks Fabian Yamaguchi <fabs@cs.tu-berlin.de>,
  59. * Jacob Bower <jacob.bower@ic.ac.uk> and
  60. * Hiroshi Yokota for providing solutions.
  61. *
  62. * Oct.02, 2004 Hiroshi Miura <miura@da-cha.org>
  63. * -v0.8.2 merge code of YOKOTA Hiroshi
  64. * <yokota@netlab.is.tsukuba.ac.jp>.
  65. * Add sticky key mode interface.
  66. * Refactoring acpi_pcc_generate_keyinput().
  67. *
  68. * Sep.15, 2004 Hiroshi Miura <miura@da-cha.org>
  69. * -v0.8 Generate key input event on input subsystem.
  70. * This is based on yet another driver written by
  71. * Ryuta Nakanishi.
  72. *
  73. * Sep.10, 2004 Hiroshi Miura <miura@da-cha.org>
  74. * -v0.7 Change proc interface functions using seq_file
  75. * facility as same as other ACPI drivers.
  76. *
  77. * Aug.28, 2004 Hiroshi Miura <miura@da-cha.org>
  78. * -v0.6.4 Fix a silly error with status checking
  79. *
  80. * Aug.25, 2004 Hiroshi Miura <miura@da-cha.org>
  81. * -v0.6.3 replace read_acpi_int by standard function
  82. * acpi_evaluate_integer
  83. * some clean up and make smart copyright notice.
  84. * fix return value of pcc_acpi_get_key()
  85. * fix checking return value of acpi_bus_register_driver()
  86. *
  87. * Aug.22, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
  88. * -v0.6.2 Add check on ACPI data (num_sifr)
  89. * Coding style cleanups, better error messages/handling
  90. * Fixed an off-by-one error in memory allocation
  91. *
  92. * Aug.21, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
  93. * -v0.6.1 Fix a silly error with status checking
  94. *
  95. * Aug.20, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
  96. * - v0.6 Correct brightness controls to reflect reality
  97. * based on information gleaned by Hiroshi Miura
  98. * and discussions with Hiroshi Miura
  99. *
  100. * Aug.10, 2004 Hiroshi Miura <miura@da-cha.org>
  101. * - v0.5 support LCD brightness control
  102. * based on the disclosed information by MEI.
  103. *
  104. * Jul.25, 2004 Hiroshi Miura <miura@da-cha.org>
  105. * - v0.4 first post version
  106. * add function to retrive SIFR
  107. *
  108. * Jul.24, 2004 Hiroshi Miura <miura@da-cha.org>
  109. * - v0.3 get proper status of hotkey
  110. *
  111. * Jul.22, 2004 Hiroshi Miura <miura@da-cha.org>
  112. * - v0.2 add HotKey handler
  113. *
  114. * Jul.17, 2004 Hiroshi Miura <miura@da-cha.org>
  115. * - v0.1 start from toshiba_acpi driver written by John Belmonte
  116. *
  117. */
  118. #include <linux/kernel.h>
  119. #include <linux/module.h>
  120. #include <linux/init.h>
  121. #include <linux/types.h>
  122. #include <linux/backlight.h>
  123. #include <linux/ctype.h>
  124. #include <linux/seq_file.h>
  125. #include <linux/uaccess.h>
  126. #include <acpi/acpi_bus.h>
  127. #include <acpi/acpi_drivers.h>
  128. #include <linux/input.h>
  129. #ifndef ACPI_HOTKEY_COMPONENT
  130. #define ACPI_HOTKEY_COMPONENT 0x10000000
  131. #endif
  132. #define _COMPONENT ACPI_HOTKEY_COMPONENT
  133. MODULE_AUTHOR("Hiroshi Miura, David Bronaugh and Harald Welte");
  134. MODULE_DESCRIPTION("ACPI HotKey driver for Panasonic Let's Note laptops");
  135. MODULE_LICENSE("GPL");
  136. #define LOGPREFIX "pcc_acpi: "
  137. /* Define ACPI PATHs */
  138. /* Lets note hotkeys */
  139. #define METHOD_HKEY_QUERY "HINF"
  140. #define METHOD_HKEY_SQTY "SQTY"
  141. #define METHOD_HKEY_SINF "SINF"
  142. #define METHOD_HKEY_SSET "SSET"
  143. #define HKEY_NOTIFY 0x80
  144. #define ACPI_PCC_DRIVER_NAME "Panasonic Laptop Support"
  145. #define ACPI_PCC_DEVICE_NAME "Hotkey"
  146. #define ACPI_PCC_CLASS "pcc"
  147. #define ACPI_PCC_INPUT_PHYS "panasonic/hkey0"
  148. /* LCD_TYPEs: 0 = Normal, 1 = Semi-transparent
  149. ENV_STATEs: Normal temp=0x01, High temp=0x81, N/A=0x00
  150. */
  151. enum SINF_BITS { SINF_NUM_BATTERIES = 0,
  152. SINF_LCD_TYPE,
  153. SINF_AC_MAX_BRIGHT,
  154. SINF_AC_MIN_BRIGHT,
  155. SINF_AC_CUR_BRIGHT,
  156. SINF_DC_MAX_BRIGHT,
  157. SINF_DC_MIN_BRIGHT,
  158. SINF_DC_CUR_BRIGHT,
  159. SINF_MUTE,
  160. SINF_RESERVED,
  161. SINF_ENV_STATE,
  162. SINF_STICKY_KEY = 0x80,
  163. };
  164. /* R1 handles SINF_AC_CUR_BRIGHT as SINF_CUR_BRIGHT, doesn't know AC state */
  165. static int acpi_pcc_hotkey_add(struct acpi_device *device);
  166. static int acpi_pcc_hotkey_remove(struct acpi_device *device, int type);
  167. static int acpi_pcc_hotkey_resume(struct acpi_device *device);
  168. static const struct acpi_device_id pcc_device_ids[] = {
  169. { "MAT0012", 0},
  170. { "MAT0013", 0},
  171. { "MAT0018", 0},
  172. { "MAT0019", 0},
  173. { "", 0},
  174. };
  175. MODULE_DEVICE_TABLE(acpi, pcc_device_ids);
  176. static struct acpi_driver acpi_pcc_driver = {
  177. .name = ACPI_PCC_DRIVER_NAME,
  178. .class = ACPI_PCC_CLASS,
  179. .ids = pcc_device_ids,
  180. .ops = {
  181. .add = acpi_pcc_hotkey_add,
  182. .remove = acpi_pcc_hotkey_remove,
  183. .resume = acpi_pcc_hotkey_resume,
  184. },
  185. };
  186. #define KEYMAP_SIZE 11
  187. static const int initial_keymap[KEYMAP_SIZE] = {
  188. /* 0 */ KEY_RESERVED,
  189. /* 1 */ KEY_BRIGHTNESSDOWN,
  190. /* 2 */ KEY_BRIGHTNESSUP,
  191. /* 3 */ KEY_DISPLAYTOGGLE,
  192. /* 4 */ KEY_MUTE,
  193. /* 5 */ KEY_VOLUMEDOWN,
  194. /* 6 */ KEY_VOLUMEUP,
  195. /* 7 */ KEY_SLEEP,
  196. /* 8 */ KEY_PROG1, /* Change CPU boost */
  197. /* 9 */ KEY_BATTERY,
  198. /* 10 */ KEY_SUSPEND,
  199. };
  200. struct pcc_acpi {
  201. acpi_handle handle;
  202. unsigned long num_sifr;
  203. int sticky_mode;
  204. u32 *sinf;
  205. struct acpi_device *device;
  206. struct input_dev *input_dev;
  207. struct backlight_device *backlight;
  208. int keymap[KEYMAP_SIZE];
  209. };
  210. struct pcc_keyinput {
  211. struct acpi_hotkey *hotkey;
  212. };
  213. /* method access functions */
  214. static int acpi_pcc_write_sset(struct pcc_acpi *pcc, int func, int val)
  215. {
  216. union acpi_object in_objs[] = {
  217. { .integer.type = ACPI_TYPE_INTEGER,
  218. .integer.value = func, },
  219. { .integer.type = ACPI_TYPE_INTEGER,
  220. .integer.value = val, },
  221. };
  222. struct acpi_object_list params = {
  223. .count = ARRAY_SIZE(in_objs),
  224. .pointer = in_objs,
  225. };
  226. acpi_status status = AE_OK;
  227. status = acpi_evaluate_object(pcc->handle, METHOD_HKEY_SSET,
  228. &params, NULL);
  229. return status == AE_OK;
  230. }
  231. static inline int acpi_pcc_get_sqty(struct acpi_device *device)
  232. {
  233. unsigned long long s;
  234. acpi_status status;
  235. status = acpi_evaluate_integer(device->handle, METHOD_HKEY_SQTY,
  236. NULL, &s);
  237. if (ACPI_SUCCESS(status))
  238. return s;
  239. else {
  240. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  241. "evaluation error HKEY.SQTY\n"));
  242. return -EINVAL;
  243. }
  244. }
  245. static int acpi_pcc_retrieve_biosdata(struct pcc_acpi *pcc, u32 *sinf)
  246. {
  247. acpi_status status;
  248. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  249. union acpi_object *hkey = NULL;
  250. int i;
  251. status = acpi_evaluate_object(pcc->handle, METHOD_HKEY_SINF, 0,
  252. &buffer);
  253. if (ACPI_FAILURE(status)) {
  254. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  255. "evaluation error HKEY.SINF\n"));
  256. return 0;
  257. }
  258. hkey = buffer.pointer;
  259. if (!hkey || (hkey->type != ACPI_TYPE_PACKAGE)) {
  260. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid HKEY.SINF\n"));
  261. goto end;
  262. }
  263. if (pcc->num_sifr < hkey->package.count) {
  264. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  265. "SQTY reports bad SINF length\n"));
  266. status = AE_ERROR;
  267. goto end;
  268. }
  269. for (i = 0; i < hkey->package.count; i++) {
  270. union acpi_object *element = &(hkey->package.elements[i]);
  271. if (likely(element->type == ACPI_TYPE_INTEGER)) {
  272. sinf[i] = element->integer.value;
  273. } else
  274. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  275. "Invalid HKEY.SINF data\n"));
  276. }
  277. sinf[hkey->package.count] = -1;
  278. end:
  279. kfree(buffer.pointer);
  280. return status == AE_OK;
  281. }
  282. /* backlight API interface functions */
  283. /* This driver currently treats AC and DC brightness identical,
  284. * since we don't need to invent an interface to the core ACPI
  285. * logic to receive events in case a power supply is plugged in
  286. * or removed */
  287. static int bl_get(struct backlight_device *bd)
  288. {
  289. struct pcc_acpi *pcc = bl_get_data(bd);
  290. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  291. return -EIO;
  292. return pcc->sinf[SINF_AC_CUR_BRIGHT];
  293. }
  294. static int bl_set_status(struct backlight_device *bd)
  295. {
  296. struct pcc_acpi *pcc = bl_get_data(bd);
  297. int bright = bd->props.brightness;
  298. int rc;
  299. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  300. return -EIO;
  301. if (bright < pcc->sinf[SINF_AC_MIN_BRIGHT])
  302. bright = pcc->sinf[SINF_AC_MIN_BRIGHT];
  303. if (bright < pcc->sinf[SINF_DC_MIN_BRIGHT])
  304. bright = pcc->sinf[SINF_DC_MIN_BRIGHT];
  305. if (bright < pcc->sinf[SINF_AC_MIN_BRIGHT] ||
  306. bright > pcc->sinf[SINF_AC_MAX_BRIGHT])
  307. return -EINVAL;
  308. rc = acpi_pcc_write_sset(pcc, SINF_AC_CUR_BRIGHT, bright);
  309. if (rc < 0)
  310. return rc;
  311. return acpi_pcc_write_sset(pcc, SINF_DC_CUR_BRIGHT, bright);
  312. }
  313. static struct backlight_ops pcc_backlight_ops = {
  314. .get_brightness = bl_get,
  315. .update_status = bl_set_status,
  316. };
  317. /* sysfs user interface functions */
  318. static ssize_t show_numbatt(struct device *dev, struct device_attribute *attr,
  319. char *buf)
  320. {
  321. struct acpi_device *acpi = to_acpi_device(dev);
  322. struct pcc_acpi *pcc = acpi_driver_data(acpi);
  323. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  324. return -EIO;
  325. return snprintf(buf, PAGE_SIZE, "%u\n", pcc->sinf[SINF_NUM_BATTERIES]);
  326. }
  327. static ssize_t show_lcdtype(struct device *dev, struct device_attribute *attr,
  328. char *buf)
  329. {
  330. struct acpi_device *acpi = to_acpi_device(dev);
  331. struct pcc_acpi *pcc = acpi_driver_data(acpi);
  332. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  333. return -EIO;
  334. return snprintf(buf, PAGE_SIZE, "%u\n", pcc->sinf[SINF_LCD_TYPE]);
  335. }
  336. static ssize_t show_mute(struct device *dev, struct device_attribute *attr,
  337. char *buf)
  338. {
  339. struct acpi_device *acpi = to_acpi_device(dev);
  340. struct pcc_acpi *pcc = acpi_driver_data(acpi);
  341. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  342. return -EIO;
  343. return snprintf(buf, PAGE_SIZE, "%u\n", pcc->sinf[SINF_MUTE]);
  344. }
  345. static ssize_t show_sticky(struct device *dev, struct device_attribute *attr,
  346. char *buf)
  347. {
  348. struct acpi_device *acpi = to_acpi_device(dev);
  349. struct pcc_acpi *pcc = acpi_driver_data(acpi);
  350. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf))
  351. return -EIO;
  352. return snprintf(buf, PAGE_SIZE, "%u\n", pcc->sinf[SINF_STICKY_KEY]);
  353. }
  354. static ssize_t set_sticky(struct device *dev, struct device_attribute *attr,
  355. const char *buf, size_t count)
  356. {
  357. struct acpi_device *acpi = to_acpi_device(dev);
  358. struct pcc_acpi *pcc = acpi_driver_data(acpi);
  359. int val;
  360. if (count && sscanf(buf, "%i", &val) == 1 &&
  361. (val == 0 || val == 1)) {
  362. acpi_pcc_write_sset(pcc, SINF_STICKY_KEY, val);
  363. pcc->sticky_mode = val;
  364. }
  365. return count;
  366. }
  367. static DEVICE_ATTR(numbatt, S_IRUGO, show_numbatt, NULL);
  368. static DEVICE_ATTR(lcdtype, S_IRUGO, show_lcdtype, NULL);
  369. static DEVICE_ATTR(mute, S_IRUGO, show_mute, NULL);
  370. static DEVICE_ATTR(sticky_key, S_IRUGO | S_IWUSR, show_sticky, set_sticky);
  371. static struct attribute *pcc_sysfs_entries[] = {
  372. &dev_attr_numbatt.attr,
  373. &dev_attr_lcdtype.attr,
  374. &dev_attr_mute.attr,
  375. &dev_attr_sticky_key.attr,
  376. NULL,
  377. };
  378. static struct attribute_group pcc_attr_group = {
  379. .name = NULL, /* put in device directory */
  380. .attrs = pcc_sysfs_entries,
  381. };
  382. /* hotkey input device driver */
  383. static int pcc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  384. {
  385. struct pcc_acpi *pcc = input_get_drvdata(dev);
  386. if (scancode >= ARRAY_SIZE(pcc->keymap))
  387. return -EINVAL;
  388. *keycode = pcc->keymap[scancode];
  389. return 0;
  390. }
  391. static int keymap_get_by_keycode(struct pcc_acpi *pcc, int keycode)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(pcc->keymap); i++) {
  395. if (pcc->keymap[i] == keycode)
  396. return i+1;
  397. }
  398. return 0;
  399. }
  400. static int pcc_setkeycode(struct input_dev *dev, int scancode, int keycode)
  401. {
  402. struct pcc_acpi *pcc = input_get_drvdata(dev);
  403. int oldkeycode;
  404. if (scancode >= ARRAY_SIZE(pcc->keymap))
  405. return -EINVAL;
  406. if (keycode < 0 || keycode > KEY_MAX)
  407. return -EINVAL;
  408. oldkeycode = pcc->keymap[scancode];
  409. pcc->keymap[scancode] = keycode;
  410. set_bit(keycode, dev->keybit);
  411. if (!keymap_get_by_keycode(pcc, oldkeycode))
  412. clear_bit(oldkeycode, dev->keybit);
  413. return 0;
  414. }
  415. static void acpi_pcc_generate_keyinput(struct pcc_acpi *pcc)
  416. {
  417. struct input_dev *hotk_input_dev = pcc->input_dev;
  418. int rc;
  419. int key_code, hkey_num;
  420. unsigned long long result;
  421. rc = acpi_evaluate_integer(pcc->handle, METHOD_HKEY_QUERY,
  422. NULL, &result);
  423. if (!ACPI_SUCCESS(rc)) {
  424. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  425. "error getting hotkey status\n"));
  426. return;
  427. }
  428. acpi_bus_generate_proc_event(pcc->device, HKEY_NOTIFY, result);
  429. hkey_num = result & 0xf;
  430. if (hkey_num < 0 || hkey_num >= ARRAY_SIZE(pcc->keymap)) {
  431. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  432. "hotkey number out of range: %d\n",
  433. hkey_num));
  434. return;
  435. }
  436. key_code = pcc->keymap[hkey_num];
  437. if (key_code != KEY_RESERVED) {
  438. int pushed = (result & 0x80) ? TRUE : FALSE;
  439. input_report_key(hotk_input_dev, key_code, pushed);
  440. input_sync(hotk_input_dev);
  441. }
  442. return;
  443. }
  444. static void acpi_pcc_hotkey_notify(acpi_handle handle, u32 event, void *data)
  445. {
  446. struct pcc_acpi *pcc = (struct pcc_acpi *) data;
  447. switch (event) {
  448. case HKEY_NOTIFY:
  449. acpi_pcc_generate_keyinput(pcc);
  450. break;
  451. default:
  452. /* nothing to do */
  453. break;
  454. }
  455. }
  456. static int acpi_pcc_init_input(struct pcc_acpi *pcc)
  457. {
  458. int i, rc;
  459. pcc->input_dev = input_allocate_device();
  460. if (!pcc->input_dev) {
  461. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  462. "Couldn't allocate input device for hotkey"));
  463. return -ENOMEM;
  464. }
  465. pcc->input_dev->evbit[0] = BIT(EV_KEY);
  466. pcc->input_dev->name = ACPI_PCC_DRIVER_NAME;
  467. pcc->input_dev->phys = ACPI_PCC_INPUT_PHYS;
  468. pcc->input_dev->id.bustype = BUS_HOST;
  469. pcc->input_dev->id.vendor = 0x0001;
  470. pcc->input_dev->id.product = 0x0001;
  471. pcc->input_dev->id.version = 0x0100;
  472. pcc->input_dev->getkeycode = pcc_getkeycode;
  473. pcc->input_dev->setkeycode = pcc_setkeycode;
  474. /* load initial keymap */
  475. memcpy(pcc->keymap, initial_keymap, sizeof(pcc->keymap));
  476. for (i = 0; i < ARRAY_SIZE(pcc->keymap); i++)
  477. __set_bit(pcc->keymap[i], pcc->input_dev->keybit);
  478. __clear_bit(KEY_RESERVED, pcc->input_dev->keybit);
  479. input_set_drvdata(pcc->input_dev, pcc);
  480. rc = input_register_device(pcc->input_dev);
  481. if (rc < 0)
  482. input_free_device(pcc->input_dev);
  483. return rc;
  484. }
  485. /* kernel module interface */
  486. static int acpi_pcc_hotkey_resume(struct acpi_device *device)
  487. {
  488. struct pcc_acpi *pcc = acpi_driver_data(device);
  489. acpi_status status = AE_OK;
  490. if (device == NULL || pcc == NULL)
  491. return -EINVAL;
  492. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Sticky mode restore: %d\n",
  493. pcc->sticky_mode));
  494. status = acpi_pcc_write_sset(pcc, SINF_STICKY_KEY, pcc->sticky_mode);
  495. return status == AE_OK ? 0 : -EINVAL;
  496. }
  497. static int acpi_pcc_hotkey_add(struct acpi_device *device)
  498. {
  499. acpi_status status;
  500. struct pcc_acpi *pcc;
  501. int num_sifr, result;
  502. if (!device)
  503. return -EINVAL;
  504. num_sifr = acpi_pcc_get_sqty(device);
  505. if (num_sifr > 255) {
  506. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "num_sifr too large"));
  507. return -ENODEV;
  508. }
  509. pcc = kzalloc(sizeof(struct pcc_acpi), GFP_KERNEL);
  510. if (!pcc) {
  511. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  512. "Couldn't allocate mem for pcc"));
  513. return -ENOMEM;
  514. }
  515. pcc->sinf = kzalloc(sizeof(u32) * (num_sifr + 1), GFP_KERNEL);
  516. if (!pcc->sinf) {
  517. result = -ENOMEM;
  518. goto out_hotkey;
  519. }
  520. pcc->device = device;
  521. pcc->handle = device->handle;
  522. pcc->num_sifr = num_sifr;
  523. device->driver_data = pcc;
  524. strcpy(acpi_device_name(device), ACPI_PCC_DEVICE_NAME);
  525. strcpy(acpi_device_class(device), ACPI_PCC_CLASS);
  526. result = acpi_pcc_init_input(pcc);
  527. if (result) {
  528. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  529. "Error installing keyinput handler\n"));
  530. goto out_sinf;
  531. }
  532. /* initialize hotkey input device */
  533. status = acpi_install_notify_handler(pcc->handle, ACPI_DEVICE_NOTIFY,
  534. acpi_pcc_hotkey_notify, pcc);
  535. if (ACPI_FAILURE(status)) {
  536. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  537. "Error installing notify handler\n"));
  538. result = -ENODEV;
  539. goto out_input;
  540. }
  541. /* initialize backlight */
  542. pcc->backlight = backlight_device_register("panasonic", NULL, pcc,
  543. &pcc_backlight_ops);
  544. if (IS_ERR(pcc->backlight))
  545. goto out_notify;
  546. if (!acpi_pcc_retrieve_biosdata(pcc, pcc->sinf)) {
  547. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  548. "Couldn't retrieve BIOS data\n"));
  549. goto out_backlight;
  550. }
  551. /* read the initial brightness setting from the hardware */
  552. pcc->backlight->props.max_brightness =
  553. pcc->sinf[SINF_AC_MAX_BRIGHT];
  554. pcc->backlight->props.brightness = pcc->sinf[SINF_AC_CUR_BRIGHT];
  555. /* read the initial sticky key mode from the hardware */
  556. pcc->sticky_mode = pcc->sinf[SINF_STICKY_KEY];
  557. /* add sysfs attributes */
  558. result = sysfs_create_group(&device->dev.kobj, &pcc_attr_group);
  559. if (result)
  560. goto out_backlight;
  561. return 0;
  562. out_backlight:
  563. backlight_device_unregister(pcc->backlight);
  564. out_notify:
  565. acpi_remove_notify_handler(pcc->handle, ACPI_DEVICE_NOTIFY,
  566. acpi_pcc_hotkey_notify);
  567. out_input:
  568. input_unregister_device(pcc->input_dev);
  569. /* no need to input_free_device() since core input API refcount and
  570. * free()s the device */
  571. out_sinf:
  572. kfree(pcc->sinf);
  573. out_hotkey:
  574. kfree(pcc);
  575. return result;
  576. }
  577. static int __init acpi_pcc_init(void)
  578. {
  579. int result = 0;
  580. if (acpi_disabled)
  581. return -ENODEV;
  582. result = acpi_bus_register_driver(&acpi_pcc_driver);
  583. if (result < 0) {
  584. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  585. "Error registering hotkey driver\n"));
  586. return -ENODEV;
  587. }
  588. return 0;
  589. }
  590. static int acpi_pcc_hotkey_remove(struct acpi_device *device, int type)
  591. {
  592. struct pcc_acpi *pcc = acpi_driver_data(device);
  593. if (!device || !pcc)
  594. return -EINVAL;
  595. sysfs_remove_group(&device->dev.kobj, &pcc_attr_group);
  596. backlight_device_unregister(pcc->backlight);
  597. acpi_remove_notify_handler(pcc->handle, ACPI_DEVICE_NOTIFY,
  598. acpi_pcc_hotkey_notify);
  599. input_unregister_device(pcc->input_dev);
  600. /* no need to input_free_device() since core input API refcount and
  601. * free()s the device */
  602. kfree(pcc->sinf);
  603. kfree(pcc);
  604. return 0;
  605. }
  606. static void __exit acpi_pcc_exit(void)
  607. {
  608. acpi_bus_unregister_driver(&acpi_pcc_driver);
  609. }
  610. module_init(acpi_pcc_init);
  611. module_exit(acpi_pcc_exit);