hil_ptr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Generic linux-input device driver for axis-bearing devices
  3. *
  4. * Copyright (c) 2001 Brian S. Julin
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * Alternatively, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL").
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. *
  29. * References:
  30. * HP-HIL Technical Reference Manual. Hewlett Packard Product No. 45918A
  31. *
  32. */
  33. #include <linux/hil.h>
  34. #include <linux/input.h>
  35. #include <linux/serio.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/pci_ids.h>
  41. #define PREFIX "HIL PTR: "
  42. #define HIL_GENERIC_NAME "HIL pointer device"
  43. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  44. MODULE_DESCRIPTION(HIL_GENERIC_NAME " driver");
  45. MODULE_LICENSE("Dual BSD/GPL");
  46. #define TABLET_SIMULATES_MOUSE /* allow tablet to be used as mouse */
  47. #undef TABLET_AUTOADJUST /* auto-adjust valid tablet ranges */
  48. #define HIL_PTR_MAX_LENGTH 16
  49. struct hil_ptr {
  50. struct input_dev *dev;
  51. struct serio *serio;
  52. /* Input buffer and index for packets from HIL bus. */
  53. hil_packet data[HIL_PTR_MAX_LENGTH];
  54. int idx4; /* four counts per packet */
  55. /* Raw device info records from HIL bus, see hil.h for fields. */
  56. char idd[HIL_PTR_MAX_LENGTH]; /* DID byte and IDD record */
  57. char rsc[HIL_PTR_MAX_LENGTH]; /* RSC record */
  58. char exd[HIL_PTR_MAX_LENGTH]; /* EXD record */
  59. char rnm[HIL_PTR_MAX_LENGTH + 1]; /* RNM record + NULL term. */
  60. /* Extra device details not contained in struct input_dev. */
  61. unsigned int nbtn, naxes;
  62. unsigned int btnmap[7];
  63. /* Something to sleep around with. */
  64. struct semaphore sem;
  65. };
  66. /* Process a complete packet after transfer from the HIL */
  67. static void hil_ptr_process_record(struct hil_ptr *ptr)
  68. {
  69. struct input_dev *dev = ptr->dev;
  70. hil_packet *data = ptr->data;
  71. hil_packet p;
  72. int idx, i, cnt, laxis;
  73. int ax16, absdev;
  74. idx = ptr->idx4/4;
  75. p = data[idx - 1];
  76. if ((p & ~HIL_CMDCT_POL) ==
  77. (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
  78. goto report;
  79. if ((p & ~HIL_CMDCT_RPL) ==
  80. (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_RPL))
  81. goto report;
  82. /* Not a poll response. See if we are loading config records. */
  83. switch (p & HIL_PKT_DATA_MASK) {
  84. case HIL_CMD_IDD:
  85. for (i = 0; i < idx; i++)
  86. ptr->idd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
  87. for (; i < HIL_PTR_MAX_LENGTH; i++)
  88. ptr->idd[i] = 0;
  89. break;
  90. case HIL_CMD_RSC:
  91. for (i = 0; i < idx; i++)
  92. ptr->rsc[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
  93. for (; i < HIL_PTR_MAX_LENGTH; i++)
  94. ptr->rsc[i] = 0;
  95. break;
  96. case HIL_CMD_EXD:
  97. for (i = 0; i < idx; i++)
  98. ptr->exd[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
  99. for (; i < HIL_PTR_MAX_LENGTH; i++)
  100. ptr->exd[i] = 0;
  101. break;
  102. case HIL_CMD_RNM:
  103. for (i = 0; i < idx; i++)
  104. ptr->rnm[i] = ptr->data[i] & HIL_PKT_DATA_MASK;
  105. for (; i < HIL_PTR_MAX_LENGTH + 1; i++)
  106. ptr->rnm[i] = 0;
  107. break;
  108. default:
  109. /* These occur when device isn't present */
  110. if (p == (HIL_ERR_INT | HIL_PKT_CMD))
  111. break;
  112. /* Anything else we'd like to know about. */
  113. printk(KERN_WARNING PREFIX "Device sent unknown record %x\n", p);
  114. break;
  115. }
  116. goto out;
  117. report:
  118. if ((p & HIL_CMDCT_POL) != idx - 1) {
  119. printk(KERN_WARNING PREFIX
  120. "Malformed poll packet %x (idx = %i)\n", p, idx);
  121. goto out;
  122. }
  123. i = (ptr->data[0] & HIL_POL_AXIS_ALT) ? 3 : 0;
  124. laxis = ptr->data[0] & HIL_POL_NUM_AXES_MASK;
  125. laxis += i;
  126. ax16 = ptr->idd[1] & HIL_IDD_HEADER_16BIT; /* 8 or 16bit resolution */
  127. absdev = ptr->idd[1] & HIL_IDD_HEADER_ABS;
  128. for (cnt = 1; i < laxis; i++) {
  129. unsigned int lo,hi,val;
  130. lo = ptr->data[cnt++] & HIL_PKT_DATA_MASK;
  131. hi = ax16 ? (ptr->data[cnt++] & HIL_PKT_DATA_MASK) : 0;
  132. if (absdev) {
  133. val = lo + (hi<<8);
  134. #ifdef TABLET_AUTOADJUST
  135. if (val < dev->absmin[ABS_X + i])
  136. dev->absmin[ABS_X + i] = val;
  137. if (val > dev->absmax[ABS_X + i])
  138. dev->absmax[ABS_X + i] = val;
  139. #endif
  140. if (i%3) val = dev->absmax[ABS_X + i] - val;
  141. input_report_abs(dev, ABS_X + i, val);
  142. } else {
  143. val = (int) (((int8_t)lo) | ((int8_t)hi<<8));
  144. if (i%3)
  145. val *= -1;
  146. input_report_rel(dev, REL_X + i, val);
  147. }
  148. }
  149. while (cnt < idx - 1) {
  150. unsigned int btn;
  151. int up;
  152. btn = ptr->data[cnt++];
  153. up = btn & 1;
  154. btn &= 0xfe;
  155. if (btn == 0x8e)
  156. continue; /* TODO: proximity == touch? */
  157. else
  158. if ((btn > 0x8c) || (btn < 0x80))
  159. continue;
  160. btn = (btn - 0x80) >> 1;
  161. btn = ptr->btnmap[btn];
  162. input_report_key(dev, btn, !up);
  163. }
  164. input_sync(dev);
  165. out:
  166. ptr->idx4 = 0;
  167. up(&ptr->sem);
  168. }
  169. static void hil_ptr_process_err(struct hil_ptr *ptr)
  170. {
  171. printk(KERN_WARNING PREFIX "errored HIL packet\n");
  172. ptr->idx4 = 0;
  173. up(&ptr->sem);
  174. }
  175. static irqreturn_t hil_ptr_interrupt(struct serio *serio,
  176. unsigned char data, unsigned int flags)
  177. {
  178. struct hil_ptr *ptr;
  179. hil_packet packet;
  180. int idx;
  181. ptr = serio_get_drvdata(serio);
  182. BUG_ON(ptr == NULL);
  183. if (ptr->idx4 >= (HIL_PTR_MAX_LENGTH * sizeof(hil_packet))) {
  184. hil_ptr_process_err(ptr);
  185. return IRQ_HANDLED;
  186. }
  187. idx = ptr->idx4/4;
  188. if (!(ptr->idx4 % 4))
  189. ptr->data[idx] = 0;
  190. packet = ptr->data[idx];
  191. packet |= ((hil_packet)data) << ((3 - (ptr->idx4 % 4)) * 8);
  192. ptr->data[idx] = packet;
  193. /* Records of N 4-byte hil_packets must terminate with a command. */
  194. if ((++(ptr->idx4)) % 4)
  195. return IRQ_HANDLED;
  196. if ((packet & 0xffff0000) != HIL_ERR_INT) {
  197. hil_ptr_process_err(ptr);
  198. return IRQ_HANDLED;
  199. }
  200. if (packet & HIL_PKT_CMD)
  201. hil_ptr_process_record(ptr);
  202. return IRQ_HANDLED;
  203. }
  204. static void hil_ptr_disconnect(struct serio *serio)
  205. {
  206. struct hil_ptr *ptr;
  207. ptr = serio_get_drvdata(serio);
  208. BUG_ON(ptr == NULL);
  209. serio_close(serio);
  210. input_unregister_device(ptr->dev);
  211. kfree(ptr);
  212. }
  213. static int hil_ptr_connect(struct serio *serio, struct serio_driver *driver)
  214. {
  215. struct hil_ptr *ptr;
  216. const char *txt;
  217. unsigned int i, naxsets, btntype;
  218. uint8_t did, *idd;
  219. if (!(ptr = kzalloc(sizeof(struct hil_ptr), GFP_KERNEL)))
  220. return -ENOMEM;
  221. ptr->dev = input_allocate_device();
  222. if (!ptr->dev)
  223. goto bail0;
  224. if (serio_open(serio, driver))
  225. goto bail1;
  226. serio_set_drvdata(serio, ptr);
  227. ptr->serio = serio;
  228. init_MUTEX_LOCKED(&ptr->sem);
  229. /* Get device info. MLC driver supplies devid/status/etc. */
  230. serio->write(serio, 0);
  231. serio->write(serio, 0);
  232. serio->write(serio, HIL_PKT_CMD >> 8);
  233. serio->write(serio, HIL_CMD_IDD);
  234. down(&ptr->sem);
  235. serio->write(serio, 0);
  236. serio->write(serio, 0);
  237. serio->write(serio, HIL_PKT_CMD >> 8);
  238. serio->write(serio, HIL_CMD_RSC);
  239. down(&ptr->sem);
  240. serio->write(serio, 0);
  241. serio->write(serio, 0);
  242. serio->write(serio, HIL_PKT_CMD >> 8);
  243. serio->write(serio, HIL_CMD_RNM);
  244. down(&ptr->sem);
  245. serio->write(serio, 0);
  246. serio->write(serio, 0);
  247. serio->write(serio, HIL_PKT_CMD >> 8);
  248. serio->write(serio, HIL_CMD_EXD);
  249. down(&ptr->sem);
  250. up(&ptr->sem);
  251. did = ptr->idd[0];
  252. idd = ptr->idd + 1;
  253. txt = "unknown";
  254. if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
  255. ptr->dev->evbit[0] = BIT(EV_REL);
  256. txt = "relative";
  257. }
  258. if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_ABS) {
  259. ptr->dev->evbit[0] = BIT(EV_ABS);
  260. txt = "absolute";
  261. }
  262. if (!ptr->dev->evbit[0])
  263. goto bail2;
  264. ptr->nbtn = HIL_IDD_NUM_BUTTONS(idd);
  265. if (ptr->nbtn)
  266. ptr->dev->evbit[0] |= BIT(EV_KEY);
  267. naxsets = HIL_IDD_NUM_AXSETS(*idd);
  268. ptr->naxes = HIL_IDD_NUM_AXES_PER_SET(*idd);
  269. printk(KERN_INFO PREFIX "HIL pointer device found (did: 0x%02x, axis: %s)\n",
  270. did, txt);
  271. printk(KERN_INFO PREFIX "HIL pointer has %i buttons and %i sets of %i axes\n",
  272. ptr->nbtn, naxsets, ptr->naxes);
  273. btntype = BTN_MISC;
  274. if ((did & HIL_IDD_DID_ABS_TABLET_MASK) == HIL_IDD_DID_ABS_TABLET)
  275. #ifdef TABLET_SIMULATES_MOUSE
  276. btntype = BTN_TOUCH;
  277. #else
  278. btntype = BTN_DIGI;
  279. #endif
  280. if ((did & HIL_IDD_DID_ABS_TSCREEN_MASK) == HIL_IDD_DID_ABS_TSCREEN)
  281. btntype = BTN_TOUCH;
  282. if ((did & HIL_IDD_DID_REL_MOUSE_MASK) == HIL_IDD_DID_REL_MOUSE)
  283. btntype = BTN_MOUSE;
  284. for (i = 0; i < ptr->nbtn; i++) {
  285. set_bit(btntype | i, ptr->dev->keybit);
  286. ptr->btnmap[i] = btntype | i;
  287. }
  288. if (btntype == BTN_MOUSE) {
  289. /* Swap buttons 2 and 3 */
  290. ptr->btnmap[1] = BTN_MIDDLE;
  291. ptr->btnmap[2] = BTN_RIGHT;
  292. }
  293. if ((did & HIL_IDD_DID_TYPE_MASK) == HIL_IDD_DID_TYPE_REL) {
  294. for (i = 0; i < ptr->naxes; i++)
  295. set_bit(REL_X + i, ptr->dev->relbit);
  296. for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++)
  297. set_bit(REL_X + i, ptr->dev->relbit);
  298. } else {
  299. for (i = 0; i < ptr->naxes; i++) {
  300. set_bit(ABS_X + i, ptr->dev->absbit);
  301. ptr->dev->absmin[ABS_X + i] = 0;
  302. ptr->dev->absmax[ABS_X + i] =
  303. HIL_IDD_AXIS_MAX((ptr->idd + 1), i);
  304. }
  305. for (i = 3; (i < ptr->naxes + 3) && (naxsets > 1); i++) {
  306. set_bit(ABS_X + i, ptr->dev->absbit);
  307. ptr->dev->absmin[ABS_X + i] = 0;
  308. ptr->dev->absmax[ABS_X + i] =
  309. HIL_IDD_AXIS_MAX((ptr->idd + 1), (i - 3));
  310. }
  311. #ifdef TABLET_AUTOADJUST
  312. for (i = 0; i < ABS_MAX; i++) {
  313. int diff = ptr->dev->absmax[ABS_X + i] / 10;
  314. ptr->dev->absmin[ABS_X + i] += diff;
  315. ptr->dev->absmax[ABS_X + i] -= diff;
  316. }
  317. #endif
  318. }
  319. ptr->dev->name = strlen(ptr->rnm) ? ptr->rnm : HIL_GENERIC_NAME;
  320. ptr->dev->id.bustype = BUS_HIL;
  321. ptr->dev->id.vendor = PCI_VENDOR_ID_HP;
  322. ptr->dev->id.product = 0x0001; /* TODO: get from ptr->rsc */
  323. ptr->dev->id.version = 0x0100; /* TODO: get from ptr->rsc */
  324. ptr->dev->dev.parent = &serio->dev;
  325. input_register_device(ptr->dev);
  326. printk(KERN_INFO "input: %s (%s), ID: %d\n",
  327. ptr->dev->name,
  328. (btntype == BTN_MOUSE) ? "HIL mouse":"HIL tablet or touchpad",
  329. did);
  330. return 0;
  331. bail2:
  332. serio_close(serio);
  333. bail1:
  334. input_free_device(ptr->dev);
  335. bail0:
  336. kfree(ptr);
  337. serio_set_drvdata(serio, NULL);
  338. return -ENODEV;
  339. }
  340. static struct serio_device_id hil_ptr_ids[] = {
  341. {
  342. .type = SERIO_HIL_MLC,
  343. .proto = SERIO_HIL,
  344. .id = SERIO_ANY,
  345. .extra = SERIO_ANY,
  346. },
  347. { 0 }
  348. };
  349. static struct serio_driver hil_ptr_serio_driver = {
  350. .driver = {
  351. .name = "hil_ptr",
  352. },
  353. .description = "HP HIL mouse/tablet driver",
  354. .id_table = hil_ptr_ids,
  355. .connect = hil_ptr_connect,
  356. .disconnect = hil_ptr_disconnect,
  357. .interrupt = hil_ptr_interrupt
  358. };
  359. static int __init hil_ptr_init(void)
  360. {
  361. return serio_register_driver(&hil_ptr_serio_driver);
  362. }
  363. static void __exit hil_ptr_exit(void)
  364. {
  365. serio_unregister_driver(&hil_ptr_serio_driver);
  366. }
  367. module_init(hil_ptr_init);
  368. module_exit(hil_ptr_exit);