classmate-laptop.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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/slab.h>
  21. #include <linux/workqueue.h>
  22. #include <acpi/acpi_drivers.h>
  23. #include <linux/backlight.h>
  24. #include <linux/input.h>
  25. #include <linux/rfkill.h>
  26. MODULE_LICENSE("GPL");
  27. struct cmpc_accel {
  28. int sensitivity;
  29. int g_select;
  30. int inputdev_state;
  31. };
  32. #define CMPC_ACCEL_DEV_STATE_CLOSED 0
  33. #define CMPC_ACCEL_DEV_STATE_OPEN 1
  34. #define CMPC_ACCEL_SENSITIVITY_DEFAULT 5
  35. #define CMPC_ACCEL_G_SELECT_DEFAULT 0
  36. #define CMPC_ACCEL_HID "ACCE0000"
  37. #define CMPC_ACCEL_HID_V4 "ACCE0001"
  38. #define CMPC_TABLET_HID "TBLT0000"
  39. #define CMPC_IPML_HID "IPML200"
  40. #define CMPC_KEYS_HID "FNBT0000"
  41. /*
  42. * Generic input device code.
  43. */
  44. typedef void (*input_device_init)(struct input_dev *dev);
  45. static int cmpc_add_acpi_notify_device(struct acpi_device *acpi, char *name,
  46. input_device_init idev_init)
  47. {
  48. struct input_dev *inputdev;
  49. int error;
  50. inputdev = input_allocate_device();
  51. if (!inputdev)
  52. return -ENOMEM;
  53. inputdev->name = name;
  54. inputdev->dev.parent = &acpi->dev;
  55. idev_init(inputdev);
  56. error = input_register_device(inputdev);
  57. if (error) {
  58. input_free_device(inputdev);
  59. return error;
  60. }
  61. dev_set_drvdata(&acpi->dev, inputdev);
  62. return 0;
  63. }
  64. static int cmpc_remove_acpi_notify_device(struct acpi_device *acpi)
  65. {
  66. struct input_dev *inputdev = dev_get_drvdata(&acpi->dev);
  67. input_unregister_device(inputdev);
  68. return 0;
  69. }
  70. /*
  71. * Accelerometer code for Classmate V4
  72. */
  73. static acpi_status cmpc_start_accel_v4(acpi_handle handle)
  74. {
  75. union acpi_object param[4];
  76. struct acpi_object_list input;
  77. acpi_status status;
  78. param[0].type = ACPI_TYPE_INTEGER;
  79. param[0].integer.value = 0x3;
  80. param[1].type = ACPI_TYPE_INTEGER;
  81. param[1].integer.value = 0;
  82. param[2].type = ACPI_TYPE_INTEGER;
  83. param[2].integer.value = 0;
  84. param[3].type = ACPI_TYPE_INTEGER;
  85. param[3].integer.value = 0;
  86. input.count = 4;
  87. input.pointer = param;
  88. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  89. return status;
  90. }
  91. static acpi_status cmpc_stop_accel_v4(acpi_handle handle)
  92. {
  93. union acpi_object param[4];
  94. struct acpi_object_list input;
  95. acpi_status status;
  96. param[0].type = ACPI_TYPE_INTEGER;
  97. param[0].integer.value = 0x4;
  98. param[1].type = ACPI_TYPE_INTEGER;
  99. param[1].integer.value = 0;
  100. param[2].type = ACPI_TYPE_INTEGER;
  101. param[2].integer.value = 0;
  102. param[3].type = ACPI_TYPE_INTEGER;
  103. param[3].integer.value = 0;
  104. input.count = 4;
  105. input.pointer = param;
  106. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  107. return status;
  108. }
  109. static acpi_status cmpc_accel_set_sensitivity_v4(acpi_handle handle, int val)
  110. {
  111. union acpi_object param[4];
  112. struct acpi_object_list input;
  113. param[0].type = ACPI_TYPE_INTEGER;
  114. param[0].integer.value = 0x02;
  115. param[1].type = ACPI_TYPE_INTEGER;
  116. param[1].integer.value = val;
  117. param[2].type = ACPI_TYPE_INTEGER;
  118. param[2].integer.value = 0;
  119. param[3].type = ACPI_TYPE_INTEGER;
  120. param[3].integer.value = 0;
  121. input.count = 4;
  122. input.pointer = param;
  123. return acpi_evaluate_object(handle, "ACMD", &input, NULL);
  124. }
  125. static acpi_status cmpc_accel_set_g_select_v4(acpi_handle handle, int val)
  126. {
  127. union acpi_object param[4];
  128. struct acpi_object_list input;
  129. param[0].type = ACPI_TYPE_INTEGER;
  130. param[0].integer.value = 0x05;
  131. param[1].type = ACPI_TYPE_INTEGER;
  132. param[1].integer.value = val;
  133. param[2].type = ACPI_TYPE_INTEGER;
  134. param[2].integer.value = 0;
  135. param[3].type = ACPI_TYPE_INTEGER;
  136. param[3].integer.value = 0;
  137. input.count = 4;
  138. input.pointer = param;
  139. return acpi_evaluate_object(handle, "ACMD", &input, NULL);
  140. }
  141. static acpi_status cmpc_get_accel_v4(acpi_handle handle,
  142. int16_t *x,
  143. int16_t *y,
  144. int16_t *z)
  145. {
  146. union acpi_object param[4];
  147. struct acpi_object_list input;
  148. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  149. int16_t *locs;
  150. acpi_status status;
  151. param[0].type = ACPI_TYPE_INTEGER;
  152. param[0].integer.value = 0x01;
  153. param[1].type = ACPI_TYPE_INTEGER;
  154. param[1].integer.value = 0;
  155. param[2].type = ACPI_TYPE_INTEGER;
  156. param[2].integer.value = 0;
  157. param[3].type = ACPI_TYPE_INTEGER;
  158. param[3].integer.value = 0;
  159. input.count = 4;
  160. input.pointer = param;
  161. status = acpi_evaluate_object(handle, "ACMD", &input, &output);
  162. if (ACPI_SUCCESS(status)) {
  163. union acpi_object *obj;
  164. obj = output.pointer;
  165. locs = (int16_t *) obj->buffer.pointer;
  166. *x = locs[0];
  167. *y = locs[1];
  168. *z = locs[2];
  169. kfree(output.pointer);
  170. }
  171. return status;
  172. }
  173. static void cmpc_accel_handler_v4(struct acpi_device *dev, u32 event)
  174. {
  175. if (event == 0x81) {
  176. int16_t x, y, z;
  177. acpi_status status;
  178. status = cmpc_get_accel_v4(dev->handle, &x, &y, &z);
  179. if (ACPI_SUCCESS(status)) {
  180. struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
  181. input_report_abs(inputdev, ABS_X, x);
  182. input_report_abs(inputdev, ABS_Y, y);
  183. input_report_abs(inputdev, ABS_Z, z);
  184. input_sync(inputdev);
  185. }
  186. }
  187. }
  188. static ssize_t cmpc_accel_sensitivity_show_v4(struct device *dev,
  189. struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct acpi_device *acpi;
  193. struct input_dev *inputdev;
  194. struct cmpc_accel *accel;
  195. acpi = to_acpi_device(dev);
  196. inputdev = dev_get_drvdata(&acpi->dev);
  197. accel = dev_get_drvdata(&inputdev->dev);
  198. return sprintf(buf, "%d\n", accel->sensitivity);
  199. }
  200. static ssize_t cmpc_accel_sensitivity_store_v4(struct device *dev,
  201. struct device_attribute *attr,
  202. const char *buf, size_t count)
  203. {
  204. struct acpi_device *acpi;
  205. struct input_dev *inputdev;
  206. struct cmpc_accel *accel;
  207. unsigned long sensitivity;
  208. int r;
  209. acpi = to_acpi_device(dev);
  210. inputdev = dev_get_drvdata(&acpi->dev);
  211. accel = dev_get_drvdata(&inputdev->dev);
  212. r = kstrtoul(buf, 0, &sensitivity);
  213. if (r)
  214. return r;
  215. /* sensitivity must be between 1 and 127 */
  216. if (sensitivity < 1 || sensitivity > 127)
  217. return -EINVAL;
  218. accel->sensitivity = sensitivity;
  219. cmpc_accel_set_sensitivity_v4(acpi->handle, sensitivity);
  220. return strnlen(buf, count);
  221. }
  222. static struct device_attribute cmpc_accel_sensitivity_attr_v4 = {
  223. .attr = { .name = "sensitivity", .mode = 0660 },
  224. .show = cmpc_accel_sensitivity_show_v4,
  225. .store = cmpc_accel_sensitivity_store_v4
  226. };
  227. static ssize_t cmpc_accel_g_select_show_v4(struct device *dev,
  228. struct device_attribute *attr,
  229. char *buf)
  230. {
  231. struct acpi_device *acpi;
  232. struct input_dev *inputdev;
  233. struct cmpc_accel *accel;
  234. acpi = to_acpi_device(dev);
  235. inputdev = dev_get_drvdata(&acpi->dev);
  236. accel = dev_get_drvdata(&inputdev->dev);
  237. return sprintf(buf, "%d\n", accel->g_select);
  238. }
  239. static ssize_t cmpc_accel_g_select_store_v4(struct device *dev,
  240. struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. struct acpi_device *acpi;
  244. struct input_dev *inputdev;
  245. struct cmpc_accel *accel;
  246. unsigned long g_select;
  247. int r;
  248. acpi = to_acpi_device(dev);
  249. inputdev = dev_get_drvdata(&acpi->dev);
  250. accel = dev_get_drvdata(&inputdev->dev);
  251. r = kstrtoul(buf, 0, &g_select);
  252. if (r)
  253. return r;
  254. /* 0 means 1.5g, 1 means 6g, everything else is wrong */
  255. if (g_select != 0 && g_select != 1)
  256. return -EINVAL;
  257. accel->g_select = g_select;
  258. cmpc_accel_set_g_select_v4(acpi->handle, g_select);
  259. return strnlen(buf, count);
  260. }
  261. static struct device_attribute cmpc_accel_g_select_attr_v4 = {
  262. .attr = { .name = "g_select", .mode = 0660 },
  263. .show = cmpc_accel_g_select_show_v4,
  264. .store = cmpc_accel_g_select_store_v4
  265. };
  266. static int cmpc_accel_open_v4(struct input_dev *input)
  267. {
  268. struct acpi_device *acpi;
  269. struct cmpc_accel *accel;
  270. acpi = to_acpi_device(input->dev.parent);
  271. accel = dev_get_drvdata(&input->dev);
  272. cmpc_accel_set_sensitivity_v4(acpi->handle, accel->sensitivity);
  273. cmpc_accel_set_g_select_v4(acpi->handle, accel->g_select);
  274. if (ACPI_SUCCESS(cmpc_start_accel_v4(acpi->handle))) {
  275. accel->inputdev_state = CMPC_ACCEL_DEV_STATE_OPEN;
  276. return 0;
  277. }
  278. return -EIO;
  279. }
  280. static void cmpc_accel_close_v4(struct input_dev *input)
  281. {
  282. struct acpi_device *acpi;
  283. struct cmpc_accel *accel;
  284. acpi = to_acpi_device(input->dev.parent);
  285. accel = dev_get_drvdata(&input->dev);
  286. cmpc_stop_accel_v4(acpi->handle);
  287. accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
  288. }
  289. static void cmpc_accel_idev_init_v4(struct input_dev *inputdev)
  290. {
  291. set_bit(EV_ABS, inputdev->evbit);
  292. input_set_abs_params(inputdev, ABS_X, -255, 255, 16, 0);
  293. input_set_abs_params(inputdev, ABS_Y, -255, 255, 16, 0);
  294. input_set_abs_params(inputdev, ABS_Z, -255, 255, 16, 0);
  295. inputdev->open = cmpc_accel_open_v4;
  296. inputdev->close = cmpc_accel_close_v4;
  297. }
  298. static int cmpc_accel_suspend_v4(struct device *dev)
  299. {
  300. struct input_dev *inputdev;
  301. struct cmpc_accel *accel;
  302. inputdev = dev_get_drvdata(dev);
  303. accel = dev_get_drvdata(&inputdev->dev);
  304. if (accel->inputdev_state == CMPC_ACCEL_DEV_STATE_OPEN)
  305. return cmpc_stop_accel_v4(to_acpi_device(dev)->handle);
  306. return 0;
  307. }
  308. static int cmpc_accel_resume_v4(struct device *dev)
  309. {
  310. struct input_dev *inputdev;
  311. struct cmpc_accel *accel;
  312. inputdev = dev_get_drvdata(dev);
  313. accel = dev_get_drvdata(&inputdev->dev);
  314. if (accel->inputdev_state == CMPC_ACCEL_DEV_STATE_OPEN) {
  315. cmpc_accel_set_sensitivity_v4(to_acpi_device(dev)->handle,
  316. accel->sensitivity);
  317. cmpc_accel_set_g_select_v4(to_acpi_device(dev)->handle,
  318. accel->g_select);
  319. if (ACPI_FAILURE(cmpc_start_accel_v4(to_acpi_device(dev)->handle)))
  320. return -EIO;
  321. }
  322. return 0;
  323. }
  324. static int cmpc_accel_add_v4(struct acpi_device *acpi)
  325. {
  326. int error;
  327. struct input_dev *inputdev;
  328. struct cmpc_accel *accel;
  329. accel = kmalloc(sizeof(*accel), GFP_KERNEL);
  330. if (!accel)
  331. return -ENOMEM;
  332. accel->inputdev_state = CMPC_ACCEL_DEV_STATE_CLOSED;
  333. accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
  334. cmpc_accel_set_sensitivity_v4(acpi->handle, accel->sensitivity);
  335. error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
  336. if (error)
  337. goto failed_sensitivity;
  338. accel->g_select = CMPC_ACCEL_G_SELECT_DEFAULT;
  339. cmpc_accel_set_g_select_v4(acpi->handle, accel->g_select);
  340. error = device_create_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
  341. if (error)
  342. goto failed_g_select;
  343. error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel_v4",
  344. cmpc_accel_idev_init_v4);
  345. if (error)
  346. goto failed_input;
  347. inputdev = dev_get_drvdata(&acpi->dev);
  348. dev_set_drvdata(&inputdev->dev, accel);
  349. return 0;
  350. failed_input:
  351. device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
  352. failed_g_select:
  353. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
  354. failed_sensitivity:
  355. kfree(accel);
  356. return error;
  357. }
  358. static int cmpc_accel_remove_v4(struct acpi_device *acpi, int type)
  359. {
  360. struct input_dev *inputdev;
  361. struct cmpc_accel *accel;
  362. inputdev = dev_get_drvdata(&acpi->dev);
  363. accel = dev_get_drvdata(&inputdev->dev);
  364. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr_v4);
  365. device_remove_file(&acpi->dev, &cmpc_accel_g_select_attr_v4);
  366. return cmpc_remove_acpi_notify_device(acpi);
  367. }
  368. static SIMPLE_DEV_PM_OPS(cmpc_accel_pm, cmpc_accel_suspend_v4,
  369. cmpc_accel_resume_v4);
  370. static const struct acpi_device_id cmpc_accel_device_ids_v4[] = {
  371. {CMPC_ACCEL_HID_V4, 0},
  372. {"", 0}
  373. };
  374. static struct acpi_driver cmpc_accel_acpi_driver_v4 = {
  375. .owner = THIS_MODULE,
  376. .name = "cmpc_accel_v4",
  377. .class = "cmpc_accel_v4",
  378. .ids = cmpc_accel_device_ids_v4,
  379. .ops = {
  380. .add = cmpc_accel_add_v4,
  381. .remove = cmpc_accel_remove_v4,
  382. .notify = cmpc_accel_handler_v4,
  383. },
  384. .drv.pm = &cmpc_accel_pm,
  385. };
  386. /*
  387. * Accelerometer code for Classmate versions prior to V4
  388. */
  389. static acpi_status cmpc_start_accel(acpi_handle handle)
  390. {
  391. union acpi_object param[2];
  392. struct acpi_object_list input;
  393. acpi_status status;
  394. param[0].type = ACPI_TYPE_INTEGER;
  395. param[0].integer.value = 0x3;
  396. param[1].type = ACPI_TYPE_INTEGER;
  397. input.count = 2;
  398. input.pointer = param;
  399. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  400. return status;
  401. }
  402. static acpi_status cmpc_stop_accel(acpi_handle handle)
  403. {
  404. union acpi_object param[2];
  405. struct acpi_object_list input;
  406. acpi_status status;
  407. param[0].type = ACPI_TYPE_INTEGER;
  408. param[0].integer.value = 0x4;
  409. param[1].type = ACPI_TYPE_INTEGER;
  410. input.count = 2;
  411. input.pointer = param;
  412. status = acpi_evaluate_object(handle, "ACMD", &input, NULL);
  413. return status;
  414. }
  415. static acpi_status cmpc_accel_set_sensitivity(acpi_handle handle, int val)
  416. {
  417. union acpi_object param[2];
  418. struct acpi_object_list input;
  419. param[0].type = ACPI_TYPE_INTEGER;
  420. param[0].integer.value = 0x02;
  421. param[1].type = ACPI_TYPE_INTEGER;
  422. param[1].integer.value = val;
  423. input.count = 2;
  424. input.pointer = param;
  425. return acpi_evaluate_object(handle, "ACMD", &input, NULL);
  426. }
  427. static acpi_status cmpc_get_accel(acpi_handle handle,
  428. unsigned char *x,
  429. unsigned char *y,
  430. unsigned char *z)
  431. {
  432. union acpi_object param[2];
  433. struct acpi_object_list input;
  434. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, 0 };
  435. unsigned char *locs;
  436. acpi_status status;
  437. param[0].type = ACPI_TYPE_INTEGER;
  438. param[0].integer.value = 0x01;
  439. param[1].type = ACPI_TYPE_INTEGER;
  440. input.count = 2;
  441. input.pointer = param;
  442. status = acpi_evaluate_object(handle, "ACMD", &input, &output);
  443. if (ACPI_SUCCESS(status)) {
  444. union acpi_object *obj;
  445. obj = output.pointer;
  446. locs = obj->buffer.pointer;
  447. *x = locs[0];
  448. *y = locs[1];
  449. *z = locs[2];
  450. kfree(output.pointer);
  451. }
  452. return status;
  453. }
  454. static void cmpc_accel_handler(struct acpi_device *dev, u32 event)
  455. {
  456. if (event == 0x81) {
  457. unsigned char x, y, z;
  458. acpi_status status;
  459. status = cmpc_get_accel(dev->handle, &x, &y, &z);
  460. if (ACPI_SUCCESS(status)) {
  461. struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
  462. input_report_abs(inputdev, ABS_X, x);
  463. input_report_abs(inputdev, ABS_Y, y);
  464. input_report_abs(inputdev, ABS_Z, z);
  465. input_sync(inputdev);
  466. }
  467. }
  468. }
  469. static ssize_t cmpc_accel_sensitivity_show(struct device *dev,
  470. struct device_attribute *attr,
  471. char *buf)
  472. {
  473. struct acpi_device *acpi;
  474. struct input_dev *inputdev;
  475. struct cmpc_accel *accel;
  476. acpi = to_acpi_device(dev);
  477. inputdev = dev_get_drvdata(&acpi->dev);
  478. accel = dev_get_drvdata(&inputdev->dev);
  479. return sprintf(buf, "%d\n", accel->sensitivity);
  480. }
  481. static ssize_t cmpc_accel_sensitivity_store(struct device *dev,
  482. struct device_attribute *attr,
  483. const char *buf, size_t count)
  484. {
  485. struct acpi_device *acpi;
  486. struct input_dev *inputdev;
  487. struct cmpc_accel *accel;
  488. unsigned long sensitivity;
  489. int r;
  490. acpi = to_acpi_device(dev);
  491. inputdev = dev_get_drvdata(&acpi->dev);
  492. accel = dev_get_drvdata(&inputdev->dev);
  493. r = strict_strtoul(buf, 0, &sensitivity);
  494. if (r)
  495. return r;
  496. accel->sensitivity = sensitivity;
  497. cmpc_accel_set_sensitivity(acpi->handle, sensitivity);
  498. return strnlen(buf, count);
  499. }
  500. static struct device_attribute cmpc_accel_sensitivity_attr = {
  501. .attr = { .name = "sensitivity", .mode = 0660 },
  502. .show = cmpc_accel_sensitivity_show,
  503. .store = cmpc_accel_sensitivity_store
  504. };
  505. static int cmpc_accel_open(struct input_dev *input)
  506. {
  507. struct acpi_device *acpi;
  508. acpi = to_acpi_device(input->dev.parent);
  509. if (ACPI_SUCCESS(cmpc_start_accel(acpi->handle)))
  510. return 0;
  511. return -EIO;
  512. }
  513. static void cmpc_accel_close(struct input_dev *input)
  514. {
  515. struct acpi_device *acpi;
  516. acpi = to_acpi_device(input->dev.parent);
  517. cmpc_stop_accel(acpi->handle);
  518. }
  519. static void cmpc_accel_idev_init(struct input_dev *inputdev)
  520. {
  521. set_bit(EV_ABS, inputdev->evbit);
  522. input_set_abs_params(inputdev, ABS_X, 0, 255, 8, 0);
  523. input_set_abs_params(inputdev, ABS_Y, 0, 255, 8, 0);
  524. input_set_abs_params(inputdev, ABS_Z, 0, 255, 8, 0);
  525. inputdev->open = cmpc_accel_open;
  526. inputdev->close = cmpc_accel_close;
  527. }
  528. static int cmpc_accel_add(struct acpi_device *acpi)
  529. {
  530. int error;
  531. struct input_dev *inputdev;
  532. struct cmpc_accel *accel;
  533. accel = kmalloc(sizeof(*accel), GFP_KERNEL);
  534. if (!accel)
  535. return -ENOMEM;
  536. accel->sensitivity = CMPC_ACCEL_SENSITIVITY_DEFAULT;
  537. cmpc_accel_set_sensitivity(acpi->handle, accel->sensitivity);
  538. error = device_create_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  539. if (error)
  540. goto failed_file;
  541. error = cmpc_add_acpi_notify_device(acpi, "cmpc_accel",
  542. cmpc_accel_idev_init);
  543. if (error)
  544. goto failed_input;
  545. inputdev = dev_get_drvdata(&acpi->dev);
  546. dev_set_drvdata(&inputdev->dev, accel);
  547. return 0;
  548. failed_input:
  549. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  550. failed_file:
  551. kfree(accel);
  552. return error;
  553. }
  554. static int cmpc_accel_remove(struct acpi_device *acpi, int type)
  555. {
  556. struct input_dev *inputdev;
  557. struct cmpc_accel *accel;
  558. inputdev = dev_get_drvdata(&acpi->dev);
  559. accel = dev_get_drvdata(&inputdev->dev);
  560. device_remove_file(&acpi->dev, &cmpc_accel_sensitivity_attr);
  561. return cmpc_remove_acpi_notify_device(acpi);
  562. }
  563. static const struct acpi_device_id cmpc_accel_device_ids[] = {
  564. {CMPC_ACCEL_HID, 0},
  565. {"", 0}
  566. };
  567. static struct acpi_driver cmpc_accel_acpi_driver = {
  568. .owner = THIS_MODULE,
  569. .name = "cmpc_accel",
  570. .class = "cmpc_accel",
  571. .ids = cmpc_accel_device_ids,
  572. .ops = {
  573. .add = cmpc_accel_add,
  574. .remove = cmpc_accel_remove,
  575. .notify = cmpc_accel_handler,
  576. }
  577. };
  578. /*
  579. * Tablet mode code.
  580. */
  581. static acpi_status cmpc_get_tablet(acpi_handle handle,
  582. unsigned long long *value)
  583. {
  584. union acpi_object param;
  585. struct acpi_object_list input;
  586. unsigned long long output;
  587. acpi_status status;
  588. param.type = ACPI_TYPE_INTEGER;
  589. param.integer.value = 0x01;
  590. input.count = 1;
  591. input.pointer = &param;
  592. status = acpi_evaluate_integer(handle, "TCMD", &input, &output);
  593. if (ACPI_SUCCESS(status))
  594. *value = output;
  595. return status;
  596. }
  597. static void cmpc_tablet_handler(struct acpi_device *dev, u32 event)
  598. {
  599. unsigned long long val = 0;
  600. struct input_dev *inputdev = dev_get_drvdata(&dev->dev);
  601. if (event == 0x81) {
  602. if (ACPI_SUCCESS(cmpc_get_tablet(dev->handle, &val)))
  603. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  604. }
  605. }
  606. static void cmpc_tablet_idev_init(struct input_dev *inputdev)
  607. {
  608. unsigned long long val = 0;
  609. struct acpi_device *acpi;
  610. set_bit(EV_SW, inputdev->evbit);
  611. set_bit(SW_TABLET_MODE, inputdev->swbit);
  612. acpi = to_acpi_device(inputdev->dev.parent);
  613. if (ACPI_SUCCESS(cmpc_get_tablet(acpi->handle, &val)))
  614. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  615. }
  616. static int cmpc_tablet_add(struct acpi_device *acpi)
  617. {
  618. return cmpc_add_acpi_notify_device(acpi, "cmpc_tablet",
  619. cmpc_tablet_idev_init);
  620. }
  621. static int cmpc_tablet_remove(struct acpi_device *acpi, int type)
  622. {
  623. return cmpc_remove_acpi_notify_device(acpi);
  624. }
  625. static int cmpc_tablet_resume(struct device *dev)
  626. {
  627. struct input_dev *inputdev = dev_get_drvdata(dev);
  628. unsigned long long val = 0;
  629. if (ACPI_SUCCESS(cmpc_get_tablet(to_acpi_device(dev)->handle, &val)))
  630. input_report_switch(inputdev, SW_TABLET_MODE, !val);
  631. return 0;
  632. }
  633. static SIMPLE_DEV_PM_OPS(cmpc_tablet_pm, NULL, cmpc_tablet_resume);
  634. static const struct acpi_device_id cmpc_tablet_device_ids[] = {
  635. {CMPC_TABLET_HID, 0},
  636. {"", 0}
  637. };
  638. static struct acpi_driver cmpc_tablet_acpi_driver = {
  639. .owner = THIS_MODULE,
  640. .name = "cmpc_tablet",
  641. .class = "cmpc_tablet",
  642. .ids = cmpc_tablet_device_ids,
  643. .ops = {
  644. .add = cmpc_tablet_add,
  645. .remove = cmpc_tablet_remove,
  646. .notify = cmpc_tablet_handler,
  647. },
  648. .drv.pm = &cmpc_tablet_pm,
  649. };
  650. /*
  651. * Backlight code.
  652. */
  653. static acpi_status cmpc_get_brightness(acpi_handle handle,
  654. unsigned long long *value)
  655. {
  656. union acpi_object param;
  657. struct acpi_object_list input;
  658. unsigned long long output;
  659. acpi_status status;
  660. param.type = ACPI_TYPE_INTEGER;
  661. param.integer.value = 0xC0;
  662. input.count = 1;
  663. input.pointer = &param;
  664. status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
  665. if (ACPI_SUCCESS(status))
  666. *value = output;
  667. return status;
  668. }
  669. static acpi_status cmpc_set_brightness(acpi_handle handle,
  670. unsigned long long value)
  671. {
  672. union acpi_object param[2];
  673. struct acpi_object_list input;
  674. acpi_status status;
  675. unsigned long long output;
  676. param[0].type = ACPI_TYPE_INTEGER;
  677. param[0].integer.value = 0xC0;
  678. param[1].type = ACPI_TYPE_INTEGER;
  679. param[1].integer.value = value;
  680. input.count = 2;
  681. input.pointer = param;
  682. status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
  683. return status;
  684. }
  685. static int cmpc_bl_get_brightness(struct backlight_device *bd)
  686. {
  687. acpi_status status;
  688. acpi_handle handle;
  689. unsigned long long brightness;
  690. handle = bl_get_data(bd);
  691. status = cmpc_get_brightness(handle, &brightness);
  692. if (ACPI_SUCCESS(status))
  693. return brightness;
  694. else
  695. return -1;
  696. }
  697. static int cmpc_bl_update_status(struct backlight_device *bd)
  698. {
  699. acpi_status status;
  700. acpi_handle handle;
  701. handle = bl_get_data(bd);
  702. status = cmpc_set_brightness(handle, bd->props.brightness);
  703. if (ACPI_SUCCESS(status))
  704. return 0;
  705. else
  706. return -1;
  707. }
  708. static const struct backlight_ops cmpc_bl_ops = {
  709. .get_brightness = cmpc_bl_get_brightness,
  710. .update_status = cmpc_bl_update_status
  711. };
  712. /*
  713. * RFKILL code.
  714. */
  715. static acpi_status cmpc_get_rfkill_wlan(acpi_handle handle,
  716. unsigned long long *value)
  717. {
  718. union acpi_object param;
  719. struct acpi_object_list input;
  720. unsigned long long output;
  721. acpi_status status;
  722. param.type = ACPI_TYPE_INTEGER;
  723. param.integer.value = 0xC1;
  724. input.count = 1;
  725. input.pointer = &param;
  726. status = acpi_evaluate_integer(handle, "GRDI", &input, &output);
  727. if (ACPI_SUCCESS(status))
  728. *value = output;
  729. return status;
  730. }
  731. static acpi_status cmpc_set_rfkill_wlan(acpi_handle handle,
  732. unsigned long long value)
  733. {
  734. union acpi_object param[2];
  735. struct acpi_object_list input;
  736. acpi_status status;
  737. unsigned long long output;
  738. param[0].type = ACPI_TYPE_INTEGER;
  739. param[0].integer.value = 0xC1;
  740. param[1].type = ACPI_TYPE_INTEGER;
  741. param[1].integer.value = value;
  742. input.count = 2;
  743. input.pointer = param;
  744. status = acpi_evaluate_integer(handle, "GWRI", &input, &output);
  745. return status;
  746. }
  747. static void cmpc_rfkill_query(struct rfkill *rfkill, void *data)
  748. {
  749. acpi_status status;
  750. acpi_handle handle;
  751. unsigned long long state;
  752. bool blocked;
  753. handle = data;
  754. status = cmpc_get_rfkill_wlan(handle, &state);
  755. if (ACPI_SUCCESS(status)) {
  756. blocked = state & 1 ? false : true;
  757. rfkill_set_sw_state(rfkill, blocked);
  758. }
  759. }
  760. static int cmpc_rfkill_block(void *data, bool blocked)
  761. {
  762. acpi_status status;
  763. acpi_handle handle;
  764. unsigned long long state;
  765. bool is_blocked;
  766. handle = data;
  767. status = cmpc_get_rfkill_wlan(handle, &state);
  768. if (ACPI_FAILURE(status))
  769. return -ENODEV;
  770. /* Check if we really need to call cmpc_set_rfkill_wlan */
  771. is_blocked = state & 1 ? false : true;
  772. if (is_blocked != blocked) {
  773. state = blocked ? 0 : 1;
  774. status = cmpc_set_rfkill_wlan(handle, state);
  775. if (ACPI_FAILURE(status))
  776. return -ENODEV;
  777. }
  778. return 0;
  779. }
  780. static const struct rfkill_ops cmpc_rfkill_ops = {
  781. .query = cmpc_rfkill_query,
  782. .set_block = cmpc_rfkill_block,
  783. };
  784. /*
  785. * Common backlight and rfkill code.
  786. */
  787. struct ipml200_dev {
  788. struct backlight_device *bd;
  789. struct rfkill *rf;
  790. };
  791. static int cmpc_ipml_add(struct acpi_device *acpi)
  792. {
  793. int retval;
  794. struct ipml200_dev *ipml;
  795. struct backlight_properties props;
  796. ipml = kmalloc(sizeof(*ipml), GFP_KERNEL);
  797. if (ipml == NULL)
  798. return -ENOMEM;
  799. memset(&props, 0, sizeof(struct backlight_properties));
  800. props.type = BACKLIGHT_PLATFORM;
  801. props.max_brightness = 7;
  802. ipml->bd = backlight_device_register("cmpc_bl", &acpi->dev,
  803. acpi->handle, &cmpc_bl_ops,
  804. &props);
  805. if (IS_ERR(ipml->bd)) {
  806. retval = PTR_ERR(ipml->bd);
  807. goto out_bd;
  808. }
  809. ipml->rf = rfkill_alloc("cmpc_rfkill", &acpi->dev, RFKILL_TYPE_WLAN,
  810. &cmpc_rfkill_ops, acpi->handle);
  811. /*
  812. * If RFKILL is disabled, rfkill_alloc will return ERR_PTR(-ENODEV).
  813. * This is OK, however, since all other uses of the device will not
  814. * derefence it.
  815. */
  816. if (ipml->rf) {
  817. retval = rfkill_register(ipml->rf);
  818. if (retval) {
  819. rfkill_destroy(ipml->rf);
  820. ipml->rf = NULL;
  821. }
  822. }
  823. dev_set_drvdata(&acpi->dev, ipml);
  824. return 0;
  825. out_bd:
  826. kfree(ipml);
  827. return retval;
  828. }
  829. static int cmpc_ipml_remove(struct acpi_device *acpi, int type)
  830. {
  831. struct ipml200_dev *ipml;
  832. ipml = dev_get_drvdata(&acpi->dev);
  833. backlight_device_unregister(ipml->bd);
  834. if (ipml->rf) {
  835. rfkill_unregister(ipml->rf);
  836. rfkill_destroy(ipml->rf);
  837. }
  838. kfree(ipml);
  839. return 0;
  840. }
  841. static const struct acpi_device_id cmpc_ipml_device_ids[] = {
  842. {CMPC_IPML_HID, 0},
  843. {"", 0}
  844. };
  845. static struct acpi_driver cmpc_ipml_acpi_driver = {
  846. .owner = THIS_MODULE,
  847. .name = "cmpc",
  848. .class = "cmpc",
  849. .ids = cmpc_ipml_device_ids,
  850. .ops = {
  851. .add = cmpc_ipml_add,
  852. .remove = cmpc_ipml_remove
  853. }
  854. };
  855. /*
  856. * Extra keys code.
  857. */
  858. static int cmpc_keys_codes[] = {
  859. KEY_UNKNOWN,
  860. KEY_WLAN,
  861. KEY_SWITCHVIDEOMODE,
  862. KEY_BRIGHTNESSDOWN,
  863. KEY_BRIGHTNESSUP,
  864. KEY_VENDOR,
  865. KEY_UNKNOWN,
  866. KEY_CAMERA,
  867. KEY_BACK,
  868. KEY_FORWARD,
  869. KEY_MAX
  870. };
  871. static void cmpc_keys_handler(struct acpi_device *dev, u32 event)
  872. {
  873. struct input_dev *inputdev;
  874. int code = KEY_MAX;
  875. if ((event & 0x0F) < ARRAY_SIZE(cmpc_keys_codes))
  876. code = cmpc_keys_codes[event & 0x0F];
  877. inputdev = dev_get_drvdata(&dev->dev);
  878. input_report_key(inputdev, code, !(event & 0x10));
  879. input_sync(inputdev);
  880. }
  881. static void cmpc_keys_idev_init(struct input_dev *inputdev)
  882. {
  883. int i;
  884. set_bit(EV_KEY, inputdev->evbit);
  885. for (i = 0; cmpc_keys_codes[i] != KEY_MAX; i++)
  886. set_bit(cmpc_keys_codes[i], inputdev->keybit);
  887. }
  888. static int cmpc_keys_add(struct acpi_device *acpi)
  889. {
  890. return cmpc_add_acpi_notify_device(acpi, "cmpc_keys",
  891. cmpc_keys_idev_init);
  892. }
  893. static int cmpc_keys_remove(struct acpi_device *acpi, int type)
  894. {
  895. return cmpc_remove_acpi_notify_device(acpi);
  896. }
  897. static const struct acpi_device_id cmpc_keys_device_ids[] = {
  898. {CMPC_KEYS_HID, 0},
  899. {"", 0}
  900. };
  901. static struct acpi_driver cmpc_keys_acpi_driver = {
  902. .owner = THIS_MODULE,
  903. .name = "cmpc_keys",
  904. .class = "cmpc_keys",
  905. .ids = cmpc_keys_device_ids,
  906. .ops = {
  907. .add = cmpc_keys_add,
  908. .remove = cmpc_keys_remove,
  909. .notify = cmpc_keys_handler,
  910. }
  911. };
  912. /*
  913. * General init/exit code.
  914. */
  915. static int cmpc_init(void)
  916. {
  917. int r;
  918. r = acpi_bus_register_driver(&cmpc_keys_acpi_driver);
  919. if (r)
  920. goto failed_keys;
  921. r = acpi_bus_register_driver(&cmpc_ipml_acpi_driver);
  922. if (r)
  923. goto failed_bl;
  924. r = acpi_bus_register_driver(&cmpc_tablet_acpi_driver);
  925. if (r)
  926. goto failed_tablet;
  927. r = acpi_bus_register_driver(&cmpc_accel_acpi_driver);
  928. if (r)
  929. goto failed_accel;
  930. r = acpi_bus_register_driver(&cmpc_accel_acpi_driver_v4);
  931. if (r)
  932. goto failed_accel_v4;
  933. return r;
  934. failed_accel_v4:
  935. acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
  936. failed_accel:
  937. acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
  938. failed_tablet:
  939. acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
  940. failed_bl:
  941. acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
  942. failed_keys:
  943. return r;
  944. }
  945. static void cmpc_exit(void)
  946. {
  947. acpi_bus_unregister_driver(&cmpc_accel_acpi_driver_v4);
  948. acpi_bus_unregister_driver(&cmpc_accel_acpi_driver);
  949. acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver);
  950. acpi_bus_unregister_driver(&cmpc_ipml_acpi_driver);
  951. acpi_bus_unregister_driver(&cmpc_keys_acpi_driver);
  952. }
  953. module_init(cmpc_init);
  954. module_exit(cmpc_exit);
  955. static const struct acpi_device_id cmpc_device_ids[] = {
  956. {CMPC_ACCEL_HID, 0},
  957. {CMPC_ACCEL_HID_V4, 0},
  958. {CMPC_TABLET_HID, 0},
  959. {CMPC_IPML_HID, 0},
  960. {CMPC_KEYS_HID, 0},
  961. {"", 0}
  962. };
  963. MODULE_DEVICE_TABLE(acpi, cmpc_device_ids);