classmate-laptop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/workqueue.h>
  21. #include <acpi/acpi_drivers.h>
  22. #include <linux/backlight.h>
  23. #include <linux/input.h>
  24. MODULE_LICENSE("GPL");
  25. struct cmpc_accel {
  26. int sensitivity;
  27. };
  28. #define CMPC_ACCEL_SENSITIVITY_DEFAULT 5
  29. #define CMPC_ACCEL_HID "ACCE0000"
  30. #define CMPC_TABLET_HID "TBLT0000"
  31. #define CMPC_BL_HID "IPML200"
  32. #define CMPC_KEYS_HID "FnBT0000"
  33. /*
  34. * Generic input device code.
  35. */
  36. typedef void (*input_device_init)(struct input_dev *dev);
  37. static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
  38. input_device_init idev_init)
  39. {
  40. struct input_dev *inputdev;
  41. int error;
  42. inputdev = input_allocate_device();
  43. if (!inputdev)
  44. return -ENOMEM;
  45. inputdev->name = name;
  46. inputdev->dev.parent = &acpi->dev;
  47. idev_init(inputdev);
  48. error = input_register_device(inputdev);
  49. if (error) {
  50. input_free_device(inputdev);
  51. return error;
  52. }
  53. dev_set_drvdata(&acpi->dev, inputdev);
  54. return 0;
  55. }
  56. static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi)
  57. {
  58. struct input_dev *inputdev = dev_get_drvdata(&acpi->dev);
  59. input_unregister_device(inputdev);
  60. return 0;
  61. }
  62. /*
  63. * Accelerometer code.
  64. */
  65. static acpi_status cmpc_start_accel(acpi_handle handle)
  66. {
  67. union acpi_object param[2];
  68. struct acpi_object_list input;
  69. acpi_status status;
  70. param[0].type = ACPI_TYPE_INTEGER;
  71. param[0].integer.value = 0x3;
  72. param[1].type = ACPI_TYPE_INTEGER;
  73. input.count = 2;
  74. input.pointer = param;
  75. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  76. return status;
  77. }
  78. static acpi_status cmpc_stop_accel(acpi_handle handle)
  79. {
  80. union acpi_object param[2];
  81. struct acpi_object_list input;
  82. acpi_status status;
  83. param[0].type = ACPI_TYPE_INTEGER;
  84. param[0].integer.value = 0x4;
  85. param[1].type = ACPI_TYPE_INTEGER;
  86. input.count = 2;
  87. input.pointer = param;
  88. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  89. return status;
  90. }
  91. static acpi_status cmpc_accel_set_sensitivity(acpi_handle handle, int val)
  92. {
  93. union acpi_object param[2];
  94. struct acpi_object_list input;
  95. param[0].type = ACPI_TYPE_INTEGER;
  96. param[0].integer.value = 0x02;
  97. param[1].type = ACPI_TYPE_INTEGER;
  98. param[1].integer.value = val;
  99. input.count = 2;
  100. input.pointer = param;
  101. return acpi_evaluate_object(handle, "ACMD", &input, NULL);
  102. }
  103. static acpi_status cmpc_get_accel(acpi_handle handle,
  104. unsigned char *x,
  105. unsigned char *y,
  106. unsigned char *z)
  107. {
  108. union acpi_object param[2];
  109. struct acpi_object_list input;
  110. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
  111. unsigned char *locs;
  112. acpi_status status;
  113. param[0].type = ACPI_TYPE_INTEGER;
  114. param[0].integer.value = 0x01;
  115. param[1].type = ACPI_TYPE_INTEGER;
  116. input.count = 2;
  117. input.pointer = param;
  118. status = acpi_evaluate_object(handle, "ACMD", &input, &output);
  119. if (ACPI_SUCCESS(status)) {
  120. union acpi_object *obj;
  121. obj = output.pointer;
  122. locs = obj->buffer.pointer;
  123. *x = locs[0];
  124. *y = locs[1];
  125. *z = locs[2];
  126. kfree(output.pointer);
  127. }
  128. return status;
  129. }
  130. static void cmpc_accel_handler(struct acpi_device *dev, u32 event)
  131. {
  132. if (event == 0x81) {
  133. unsigned char x, y, z;
  134. acpi_status status;
  135. status = cmpc_get_accel(dev->handle, &x, &y, &z);
  136. if (ACPI_SUCCESS(status)) {
  137. struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
  138. input_report_abs(inputdev, ABS_X, x);
  139. input_report_abs(inputdev, ABS_Y, y);
  140. input_report_abs(inputdev, ABS_Z, z);
  141. input_sync(inputdev);
  142. }
  143. }
  144. }
  145. static ssize_t cmpc_accel_sensitivity_show(struct device *dev,
  146. struct device_attribute *attr,
  147. char *buf)
  148. {
  149. struct acpi_device *acpi;
  150. struct input_dev *inputdev;
  151. struct cmpc_accel *accel;
  152. acpi = to_acpi_device(dev);
  153. inputdev = dev_get_drvdata(&acpi->dev);
  154. accel = dev_get_drvdata(&inputdev->dev);
  155. return sprintf(buf, "%d\n", accel->sensitivity);
  156. }
  157. static ssize_t cmpc_accel_sensitivity_store(struct device *dev,
  158. struct device_attribute *attr,
  159. const char *buf, size_t count)
  160. {
  161. struct acpi_device *acpi;
  162. struct input_dev *inputdev;
  163. struct cmpc_accel *accel;
  164. unsigned long sensitivity;
  165. int r;
  166. acpi = to_acpi_device(dev);
  167. inputdev = dev_get_drvdata(&acpi->dev);
  168. accel = dev_get_drvdata(&inputdev->dev);
  169. r = strict_strtoul(buf, 0, &sensitivity);
  170. if (r)
  171. return r;
  172. accel->sensitivity = sensitivity;
  173. cmpc_accel_set_sensitivity(acpi->handle, sensitivity);
  174. return strnlen(buf, count);
  175. }
  176. struct device_attribute cmpc_accel_sensitivity_attr = {
  177. .attr = { .name = "sensitivity", .mode = 0660 },
  178. .show = cmpc_accel_sensitivity_show,
  179. .store = cmpc_accel_sensitivity_store
  180. };
  181. static int cmpc_accel_open(struct input_dev *input)
  182. {
  183. struct acpi_device *acpi;
  184. acpi = to_acpi_device(input->dev.parent);
  185. if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
  186. return 0;
  187. return -EIO;
  188. }
  189. static void cmpc_accel_close(struct input_dev *input)
  190. {
  191. struct acpi_device *acpi;
  192. acpi = to_acpi_device(input->dev.parent);
  193. cmpc_stop_accel(acpi->handle);
  194. }
  195. static void cmpc_accel_idev_init(struct input_dev *inputdev)
  196. {
  197. set_bit(EV_ABS, inputdev->evbit);
  198. input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
  199. input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
  200. input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
  201. inputdev->open = cmpc_accel_open;
  202. inputdev->close = cmpc_accel_close;
  203. }
  204. static int cmpc_accel_add(struct acpi_device *acpi)
  205. {
  206. int error;
  207. struct input_dev *inputdev;
  208. struct cmpc_accel *accel;
  209. accel = kmalloc(sizeof(*accel), GFP_KERNEL);
  210. if (!accel)
  211. return -ENOMEM;
  212. accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
  213. cmpc_accel_set_sensitivity(acpi->handle, accel->sensitivity);
  214. error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  215. if (error)
  216. goto failed_file;
  217. error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
  218. cmpc_accel_idev_init);
  219. if (error)
  220. goto failed_input;
  221. inputdev = dev_get_drvdata(&acpi->dev);
  222. dev_set_drvdata(&inputdev->dev, accel);
  223. return 0;
  224. failed_input:
  225. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  226. failed_file:
  227. kfree(accel);
  228. return error;
  229. }
  230. static int cmpc_accel_remove(struct acpi_device *acpi, int type)
  231. {
  232. struct input_dev *inputdev;
  233. struct cmpc_accel *accel;
  234. inputdev = dev_get_drvdata(&acpi->dev);
  235. accel = dev_get_drvdata(&inputdev->dev);
  236. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  237. return cmpc_remove_acpi_notify_device(acpi);
  238. }
  239. static const struct acpi_device_id cmpc_accel_device_ids[] = {
  240. {CMPC_ACCEL_HID, 0},
  241. {"", 0}
  242. };
  243. static struct acpi_driver cmpc_accel_acpi_driver = {
  244. .owner = THIS_MODULE,
  245. .name = "cmpc_accel",
  246. .class = "cmpc_accel",
  247. .ids = cmpc_accel_device_ids,
  248. .ops = {
  249. .add = cmpc_accel_add,
  250. .remove = cmpc_accel_remove,
  251. .notify = cmpc_accel_handler,
  252. }
  253. };
  254. /*
  255. * Tablet mode code.
  256. */
  257. static acpi_status cmpc_get_tablet(acpi_handle handle,
  258. unsigned long long *value)
  259. {
  260. union acpi_object param;
  261. struct acpi_object_list input;
  262. unsigned long long output;
  263. acpi_status status;
  264. param.type = ACPI_TYPE_INTEGER;
  265. param.integer.value = 0x01;
  266. input.count = 1;
  267. input.pointer = &param;
  268. status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
  269. if (ACPI_SUCCESS(status))
  270. *value = output;
  271. return status;
  272. }
  273. static void cmpc_tablet_handler(struct acpi_device *dev, u32 event)
  274. {
  275. unsigned long long val = 0;
  276. struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
  277. if (event == 0x81) {
  278. if (ACPI_SUCCESS(cmpc_get_tablet(dev->handle, &val)))
  279. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  280. }
  281. }
  282. static void cmpc_tablet_idev_init(struct input_dev *inputdev)
  283. {
  284. unsigned long long val = 0;
  285. struct acpi_device *acpi;
  286. set_bit(EV_SW, inputdev->evbit);
  287. set_bit(SW_TABLET_MODE, inputdev->swbit);
  288. acpi = to_acpi_device(inputdev->dev.parent);
  289. if (ACPI_SUCCESS(cmpc_get_tablet(acpi->handle, &val)))
  290. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  291. }
  292. static int cmpc_tablet_add(struct acpi_device *acpi)
  293. {
  294. return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
  295. cmpc_tablet_idev_init);
  296. }
  297. static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
  298. {
  299. return cmpc_remove_acpi_notify_device(acpi);
  300. }
  301. static int cmpc_tablet_resume(struct acpi_device *acpi)
  302. {
  303. struct input_dev *inputdev = dev_get_drvdata(&acpi->dev);
  304. unsigned long long val = 0;
  305. if (ACPI_SUCCESS(cmpc_get_tablet(acpi->handle, &val)))
  306. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  307. return 0;
  308. }
  309. static const struct acpi_device_id cmpc_tablet_device_ids[] = {
  310. {CMPC_TABLET_HID, 0},
  311. {"", 0}
  312. };
  313. static struct acpi_driver cmpc_tablet_acpi_driver = {
  314. .owner = THIS_MODULE,
  315. .name = "cmpc_tablet",
  316. .class = "cmpc_tablet",
  317. .ids = cmpc_tablet_device_ids,
  318. .ops = {
  319. .add = cmpc_tablet_add,
  320. .remove = cmpc_tablet_remove,
  321. .resume = cmpc_tablet_resume,
  322. .notify = cmpc_tablet_handler,
  323. }
  324. };
  325. /*
  326. * Backlight code.
  327. */
  328. static acpi_status cmpc_get_brightness(acpi_handle handle,
  329. unsigned long long *value)
  330. {
  331. union acpi_object param;
  332. struct acpi_object_list input;
  333. unsigned long long output;
  334. acpi_status status;
  335. param.type = ACPI_TYPE_INTEGER;
  336. param.integer.value = 0xC0;
  337. input.count = 1;
  338. input.pointer = &param;
  339. status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
  340. if (ACPI_SUCCESS(status))
  341. *value = output;
  342. return status;
  343. }
  344. static acpi_status cmpc_set_brightness(acpi_handle handle,
  345. unsigned long long value)
  346. {
  347. union acpi_object param[2];
  348. struct acpi_object_list input;
  349. acpi_status status;
  350. unsigned long long output;
  351. param[0].type = ACPI_TYPE_INTEGER;
  352. param[0].integer.value = 0xC0;
  353. param[1].type = ACPI_TYPE_INTEGER;
  354. param[1].integer.value = value;
  355. input.count = 2;
  356. input.pointer = param;
  357. status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
  358. return status;
  359. }
  360. static int cmpc_bl_get_brightness(struct backlight_device *bd)
  361. {
  362. acpi_status status;
  363. acpi_handle handle;
  364. unsigned long long brightness;
  365. handle = bl_get_data(bd);
  366. status = cmpc_get_brightness(handle, &brightness);
  367. if (ACPI_SUCCESS(status))
  368. return brightness;
  369. else
  370. return -1;
  371. }
  372. static int cmpc_bl_update_status(struct backlight_device *bd)
  373. {
  374. acpi_status status;
  375. acpi_handle handle;
  376. handle = bl_get_data(bd);
  377. status = cmpc_set_brightness(handle, bd->props.brightness);
  378. if (ACPI_SUCCESS(status))
  379. return 0;
  380. else
  381. return -1;
  382. }
  383. static struct backlight_ops cmpc_bl_ops = {
  384. .get_brightness = cmpc_bl_get_brightness,
  385. .update_status = cmpc_bl_update_status
  386. };
  387. static int cmpc_bl_add(struct acpi_device *acpi)
  388. {
  389. struct backlight_device *bd;
  390. bd = backlight_device_register("cmpc_bl", &acpi->dev,
  391. acpi->handle, &cmpc_bl_ops);
  392. bd->props.max_brightness = 7;
  393. dev_set_drvdata(&acpi->dev, bd);
  394. return 0;
  395. }
  396. static int cmpc_bl_remove(struct acpi_device *acpi, int type)
  397. {
  398. struct backlight_device *bd;
  399. bd = dev_get_drvdata(&acpi->dev);
  400. backlight_device_unregister(bd);
  401. return 0;
  402. }
  403. static const struct acpi_device_id cmpc_bl_device_ids[] = {
  404. {CMPC_BL_HID, 0},
  405. {"", 0}
  406. };
  407. static struct acpi_driver cmpc_bl_acpi_driver = {
  408. .owner = THIS_MODULE,
  409. .name = "cmpc",
  410. .class = "cmpc",
  411. .ids = cmpc_bl_device_ids,
  412. .ops = {
  413. .add = cmpc_bl_add,
  414. .remove = cmpc_bl_remove
  415. }
  416. };
  417. /*
  418. * Extra keys code.
  419. */
  420. static int cmpc_keys_codes[] = {
  421. KEY_UNKNOWN,
  422. KEY_WLAN,
  423. KEY_SWITCHVIDEOMODE,
  424. KEY_BRIGHTNESSDOWN,
  425. KEY_BRIGHTNESSUP,
  426. KEY_VENDOR,
  427. KEY_UNKNOWN,
  428. KEY_CAMERA,
  429. KEY_BACK,
  430. KEY_FORWARD,
  431. KEY_MAX
  432. };
  433. static void cmpc_keys_handler(struct acpi_device *dev, u32 event)
  434. {
  435. struct input_dev *inputdev;
  436. int code = KEY_MAX;
  437. if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
  438. code = cmpc_keys_codes[event & 0x0F];
  439. inputdev = dev_get_drvdata(&dev->dev);;
  440. input_report_key(inputdev, code, !(event & 0x10));
  441. }
  442. static void cmpc_keys_idev_init(struct input_dev *inputdev)
  443. {
  444. int i;
  445. set_bit(EV_KEY, inputdev->evbit);
  446. for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
  447. set_bit(cmpc_keys_codes[i], inputdev->keybit);
  448. }
  449. static int cmpc_keys_add(struct acpi_device *acpi)
  450. {
  451. return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
  452. cmpc_keys_idev_init);
  453. }
  454. static int cmpc_keys_remove(struct acpi_device *acpi, int type)
  455. {
  456. return cmpc_remove_acpi_notify_device(acpi);
  457. }
  458. static const struct acpi_device_id cmpc_keys_device_ids[] = {
  459. {CMPC_KEYS_HID, 0},
  460. {"", 0}
  461. };
  462. static struct acpi_driver cmpc_keys_acpi_driver = {
  463. .owner = THIS_MODULE,
  464. .name = "cmpc_keys",
  465. .class = "cmpc_keys",
  466. .ids = cmpc_keys_device_ids,
  467. .ops = {
  468. .add = cmpc_keys_add,
  469. .remove = cmpc_keys_remove,
  470. .notify = cmpc_keys_handler,
  471. }
  472. };
  473. /*
  474. * General init/exit code.
  475. */
  476. static int cmpc_init(void)
  477. {
  478. int r;
  479. r = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
  480. if (r)
  481. goto failed_keys;
  482. r = acpi_bus_register_driver(&cmpc_bl_acpi_driver);
  483. if (r)
  484. goto failed_bl;
  485. r = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
  486. if (r)
  487. goto failed_tablet;
  488. r = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
  489. if (r)
  490. goto failed_accel;
  491. return r;
  492. failed_accel:
  493. acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
  494. failed_tablet:
  495. acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
  496. failed_bl:
  497. acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
  498. failed_keys:
  499. return r;
  500. }
  501. static void cmpc_exit(void)
  502. {
  503. acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
  504. acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
  505. acpi_bus_unregister_driver(&cmpc_bl_acpi_driver);
  506. acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
  507. }
  508. module_init(cmpc_init);
  509. module_exit(cmpc_exit);
  510. static const struct acpi_device_id cmpc_device_ids[] = {
  511. {CMPC_ACCEL_HID, 0},
  512. {CMPC_TABLET_HID, 0},
  513. {CMPC_BL_HID, 0},
  514. {CMPC_KEYS_HID, 0},
  515. {"", 0}
  516. };
  517. MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);