wacom_w8001.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Wacom W8001 penabled serial touchscreen driver
  3. *
  4. * Copyright (c) 2008 Jaya Kumar
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. *
  12. * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/input/mt.h>
  19. #include <linux/serio.h>
  20. #include <linux/ctype.h>
  21. #include <linux/delay.h>
  22. #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
  23. MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
  24. MODULE_DESCRIPTION(DRIVER_DESC);
  25. MODULE_LICENSE("GPL");
  26. #define W8001_MAX_LENGTH 13
  27. #define W8001_LEAD_MASK 0x80
  28. #define W8001_LEAD_BYTE 0x80
  29. #define W8001_TAB_MASK 0x40
  30. #define W8001_TAB_BYTE 0x40
  31. /* set in first byte of touch data packets */
  32. #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
  33. #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
  34. #define W8001_QUERY_PACKET 0x20
  35. #define W8001_CMD_STOP '0'
  36. #define W8001_CMD_START '1'
  37. #define W8001_CMD_QUERY '*'
  38. #define W8001_CMD_TOUCHQUERY '%'
  39. /* length of data packets in bytes, depends on device. */
  40. #define W8001_PKTLEN_TOUCH93 5
  41. #define W8001_PKTLEN_TOUCH9A 7
  42. #define W8001_PKTLEN_TPCPEN 9
  43. #define W8001_PKTLEN_TPCCTL 11 /* control packet */
  44. #define W8001_PKTLEN_TOUCH2FG 13
  45. /* resolution in points/mm */
  46. #define W8001_PEN_RESOLUTION 100
  47. #define W8001_TOUCH_RESOLUTION 10
  48. struct w8001_coord {
  49. u8 rdy;
  50. u8 tsw;
  51. u8 f1;
  52. u8 f2;
  53. u16 x;
  54. u16 y;
  55. u16 pen_pressure;
  56. u8 tilt_x;
  57. u8 tilt_y;
  58. };
  59. /* touch query reply packet */
  60. struct w8001_touch_query {
  61. u16 x;
  62. u16 y;
  63. u8 panel_res;
  64. u8 capacity_res;
  65. u8 sensor_id;
  66. };
  67. /*
  68. * Per-touchscreen data.
  69. */
  70. struct w8001 {
  71. struct input_dev *pen_dev;
  72. struct input_dev *touch_dev;
  73. struct serio *serio;
  74. struct completion cmd_done;
  75. int id;
  76. int idx;
  77. unsigned char response_type;
  78. unsigned char response[W8001_MAX_LENGTH];
  79. unsigned char data[W8001_MAX_LENGTH];
  80. char phys[32];
  81. int type;
  82. unsigned int pktlen;
  83. u16 max_touch_x;
  84. u16 max_touch_y;
  85. u16 max_pen_x;
  86. u16 max_pen_y;
  87. char pen_name[64];
  88. char touch_name[64];
  89. int open_count;
  90. struct mutex mutex;
  91. };
  92. static void parse_pen_data(u8 *data, struct w8001_coord *coord)
  93. {
  94. memset(coord, 0, sizeof(*coord));
  95. coord->rdy = data[0] & 0x20;
  96. coord->tsw = data[0] & 0x01;
  97. coord->f1 = data[0] & 0x02;
  98. coord->f2 = data[0] & 0x04;
  99. coord->x = (data[1] & 0x7F) << 9;
  100. coord->x |= (data[2] & 0x7F) << 2;
  101. coord->x |= (data[6] & 0x60) >> 5;
  102. coord->y = (data[3] & 0x7F) << 9;
  103. coord->y |= (data[4] & 0x7F) << 2;
  104. coord->y |= (data[6] & 0x18) >> 3;
  105. coord->pen_pressure = data[5] & 0x7F;
  106. coord->pen_pressure |= (data[6] & 0x07) << 7 ;
  107. coord->tilt_x = data[7] & 0x7F;
  108. coord->tilt_y = data[8] & 0x7F;
  109. }
  110. static void parse_single_touch(u8 *data, struct w8001_coord *coord)
  111. {
  112. coord->x = (data[1] << 7) | data[2];
  113. coord->y = (data[3] << 7) | data[4];
  114. coord->tsw = data[0] & 0x01;
  115. }
  116. static void scale_touch_coordinates(struct w8001 *w8001,
  117. unsigned int *x, unsigned int *y)
  118. {
  119. if (w8001->max_pen_x && w8001->max_touch_x)
  120. *x = *x * w8001->max_pen_x / w8001->max_touch_x;
  121. if (w8001->max_pen_y && w8001->max_touch_y)
  122. *y = *y * w8001->max_pen_y / w8001->max_touch_y;
  123. }
  124. static void parse_multi_touch(struct w8001 *w8001)
  125. {
  126. struct input_dev *dev = w8001->touch_dev;
  127. unsigned char *data = w8001->data;
  128. unsigned int x, y;
  129. int i;
  130. int count = 0;
  131. for (i = 0; i < 2; i++) {
  132. bool touch = data[0] & (1 << i);
  133. input_mt_slot(dev, i);
  134. input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
  135. if (touch) {
  136. x = (data[6 * i + 1] << 7) | data[6 * i + 2];
  137. y = (data[6 * i + 3] << 7) | data[6 * i + 4];
  138. /* data[5,6] and [11,12] is finger capacity */
  139. /* scale to pen maximum */
  140. scale_touch_coordinates(w8001, &x, &y);
  141. input_report_abs(dev, ABS_MT_POSITION_X, x);
  142. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  143. count++;
  144. }
  145. }
  146. /* emulate single touch events when stylus is out of proximity.
  147. * This is to make single touch backward support consistent
  148. * across all Wacom single touch devices.
  149. */
  150. if (w8001->type != BTN_TOOL_PEN &&
  151. w8001->type != BTN_TOOL_RUBBER) {
  152. w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED;
  153. input_mt_report_pointer_emulation(dev, true);
  154. }
  155. input_sync(dev);
  156. }
  157. static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
  158. {
  159. memset(query, 0, sizeof(*query));
  160. query->panel_res = data[1];
  161. query->sensor_id = data[2] & 0x7;
  162. query->capacity_res = data[7];
  163. query->x = data[3] << 9;
  164. query->x |= data[4] << 2;
  165. query->x |= (data[2] >> 5) & 0x3;
  166. query->y = data[5] << 9;
  167. query->y |= data[6] << 2;
  168. query->y |= (data[2] >> 3) & 0x3;
  169. /* Early days' single-finger touch models need the following defaults */
  170. if (!query->x && !query->y) {
  171. query->x = 1024;
  172. query->y = 1024;
  173. if (query->panel_res)
  174. query->x = query->y = (1 << query->panel_res);
  175. query->panel_res = W8001_TOUCH_RESOLUTION;
  176. }
  177. }
  178. static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
  179. {
  180. struct input_dev *dev = w8001->pen_dev;
  181. /*
  182. * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
  183. * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
  184. * or eraser. Assume:
  185. * - if dev is already in proximity and f2 is toggled → pen + side2
  186. * - if dev comes into proximity with f2 set → eraser
  187. * If f2 disappears after assuming eraser, fake proximity out for
  188. * eraser and in for pen.
  189. */
  190. switch (w8001->type) {
  191. case BTN_TOOL_RUBBER:
  192. if (!coord->f2) {
  193. input_report_abs(dev, ABS_PRESSURE, 0);
  194. input_report_key(dev, BTN_TOUCH, 0);
  195. input_report_key(dev, BTN_STYLUS, 0);
  196. input_report_key(dev, BTN_STYLUS2, 0);
  197. input_report_key(dev, BTN_TOOL_RUBBER, 0);
  198. input_sync(dev);
  199. w8001->type = BTN_TOOL_PEN;
  200. }
  201. break;
  202. case BTN_TOOL_FINGER:
  203. case KEY_RESERVED:
  204. w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  205. break;
  206. default:
  207. input_report_key(dev, BTN_STYLUS2, coord->f2);
  208. break;
  209. }
  210. input_report_abs(dev, ABS_X, coord->x);
  211. input_report_abs(dev, ABS_Y, coord->y);
  212. input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
  213. input_report_key(dev, BTN_TOUCH, coord->tsw);
  214. input_report_key(dev, BTN_STYLUS, coord->f1);
  215. input_report_key(dev, w8001->type, coord->rdy);
  216. input_sync(dev);
  217. if (!coord->rdy)
  218. w8001->type = KEY_RESERVED;
  219. }
  220. static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
  221. {
  222. struct input_dev *dev = w8001->touch_dev;
  223. unsigned int x = coord->x;
  224. unsigned int y = coord->y;
  225. /* scale to pen maximum */
  226. scale_touch_coordinates(w8001, &x, &y);
  227. input_report_abs(dev, ABS_X, x);
  228. input_report_abs(dev, ABS_Y, y);
  229. input_report_key(dev, BTN_TOUCH, coord->tsw);
  230. input_sync(dev);
  231. w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
  232. }
  233. static irqreturn_t w8001_interrupt(struct serio *serio,
  234. unsigned char data, unsigned int flags)
  235. {
  236. struct w8001 *w8001 = serio_get_drvdata(serio);
  237. struct w8001_coord coord;
  238. unsigned char tmp;
  239. w8001->data[w8001->idx] = data;
  240. switch (w8001->idx++) {
  241. case 0:
  242. if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
  243. pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
  244. w8001->idx = 0;
  245. }
  246. break;
  247. case W8001_PKTLEN_TOUCH93 - 1:
  248. case W8001_PKTLEN_TOUCH9A - 1:
  249. tmp = w8001->data[0] & W8001_TOUCH_BYTE;
  250. if (tmp != W8001_TOUCH_BYTE)
  251. break;
  252. if (w8001->pktlen == w8001->idx) {
  253. w8001->idx = 0;
  254. if (w8001->type != BTN_TOOL_PEN &&
  255. w8001->type != BTN_TOOL_RUBBER) {
  256. parse_single_touch(w8001->data, &coord);
  257. report_single_touch(w8001, &coord);
  258. }
  259. }
  260. break;
  261. /* Pen coordinates packet */
  262. case W8001_PKTLEN_TPCPEN - 1:
  263. tmp = w8001->data[0] & W8001_TAB_MASK;
  264. if (unlikely(tmp == W8001_TAB_BYTE))
  265. break;
  266. tmp = w8001->data[0] & W8001_TOUCH_BYTE;
  267. if (tmp == W8001_TOUCH_BYTE)
  268. break;
  269. w8001->idx = 0;
  270. parse_pen_data(w8001->data, &coord);
  271. report_pen_events(w8001, &coord);
  272. break;
  273. /* control packet */
  274. case W8001_PKTLEN_TPCCTL - 1:
  275. tmp = w8001->data[0] & W8001_TOUCH_MASK;
  276. if (tmp == W8001_TOUCH_BYTE)
  277. break;
  278. w8001->idx = 0;
  279. memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
  280. w8001->response_type = W8001_QUERY_PACKET;
  281. complete(&w8001->cmd_done);
  282. break;
  283. /* 2 finger touch packet */
  284. case W8001_PKTLEN_TOUCH2FG - 1:
  285. w8001->idx = 0;
  286. parse_multi_touch(w8001);
  287. break;
  288. default:
  289. /*
  290. * ThinkPad X60 Tablet PC (pen only device) sometimes
  291. * sends invalid data packets that are larger than
  292. * W8001_PKTLEN_TPCPEN. Let's start over again.
  293. */
  294. if (!w8001->touch_dev && w8001->idx > W8001_PKTLEN_TPCPEN - 1)
  295. w8001->idx = 0;
  296. }
  297. return IRQ_HANDLED;
  298. }
  299. static int w8001_command(struct w8001 *w8001, unsigned char command,
  300. bool wait_response)
  301. {
  302. int rc;
  303. w8001->response_type = 0;
  304. init_completion(&w8001->cmd_done);
  305. rc = serio_write(w8001->serio, command);
  306. if (rc == 0 && wait_response) {
  307. wait_for_completion_timeout(&w8001->cmd_done, HZ);
  308. if (w8001->response_type != W8001_QUERY_PACKET)
  309. rc = -EIO;
  310. }
  311. return rc;
  312. }
  313. static int w8001_open(struct input_dev *dev)
  314. {
  315. struct w8001 *w8001 = input_get_drvdata(dev);
  316. int err;
  317. err = mutex_lock_interruptible(&w8001->mutex);
  318. if (err)
  319. return err;
  320. if (w8001->open_count++ == 0) {
  321. err = w8001_command(w8001, W8001_CMD_START, false);
  322. if (err)
  323. w8001->open_count--;
  324. }
  325. mutex_unlock(&w8001->mutex);
  326. return err;
  327. }
  328. static void w8001_close(struct input_dev *dev)
  329. {
  330. struct w8001 *w8001 = input_get_drvdata(dev);
  331. mutex_lock(&w8001->mutex);
  332. if (--w8001->open_count == 0)
  333. w8001_command(w8001, W8001_CMD_STOP, false);
  334. mutex_unlock(&w8001->mutex);
  335. }
  336. static int w8001_detect(struct w8001 *w8001)
  337. {
  338. int error;
  339. error = w8001_command(w8001, W8001_CMD_STOP, false);
  340. if (error)
  341. return error;
  342. msleep(250); /* wait 250ms before querying the device */
  343. return 0;
  344. }
  345. static int w8001_setup_pen(struct w8001 *w8001, char *basename,
  346. size_t basename_sz)
  347. {
  348. struct input_dev *dev = w8001->pen_dev;
  349. struct w8001_coord coord;
  350. int error;
  351. /* penabled? */
  352. error = w8001_command(w8001, W8001_CMD_QUERY, true);
  353. if (error)
  354. return error;
  355. __set_bit(EV_KEY, dev->evbit);
  356. __set_bit(EV_ABS, dev->evbit);
  357. __set_bit(BTN_TOUCH, dev->keybit);
  358. __set_bit(BTN_TOOL_PEN, dev->keybit);
  359. __set_bit(BTN_TOOL_RUBBER, dev->keybit);
  360. __set_bit(BTN_STYLUS, dev->keybit);
  361. __set_bit(BTN_STYLUS2, dev->keybit);
  362. __set_bit(INPUT_PROP_DIRECT, dev->propbit);
  363. parse_pen_data(w8001->response, &coord);
  364. w8001->max_pen_x = coord.x;
  365. w8001->max_pen_y = coord.y;
  366. input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
  367. input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
  368. input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
  369. input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
  370. input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
  371. if (coord.tilt_x && coord.tilt_y) {
  372. input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
  373. input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
  374. }
  375. w8001->id = 0x90;
  376. strlcat(basename, " Penabled", basename_sz);
  377. return 0;
  378. }
  379. static int w8001_setup_touch(struct w8001 *w8001, char *basename,
  380. size_t basename_sz)
  381. {
  382. struct input_dev *dev = w8001->touch_dev;
  383. struct w8001_touch_query touch;
  384. int error;
  385. /* Touch enabled? */
  386. error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
  387. if (error)
  388. return error;
  389. /*
  390. * Some non-touch devices may reply to the touch query. But their
  391. * second byte is empty, which indicates touch is not supported.
  392. */
  393. if (!w8001->response[1])
  394. return -ENXIO;
  395. __set_bit(EV_KEY, dev->evbit);
  396. __set_bit(EV_ABS, dev->evbit);
  397. __set_bit(BTN_TOUCH, dev->keybit);
  398. __set_bit(INPUT_PROP_DIRECT, dev->propbit);
  399. parse_touchquery(w8001->response, &touch);
  400. w8001->max_touch_x = touch.x;
  401. w8001->max_touch_y = touch.y;
  402. if (w8001->max_pen_x && w8001->max_pen_y) {
  403. /* if pen is supported scale to pen maximum */
  404. touch.x = w8001->max_pen_x;
  405. touch.y = w8001->max_pen_y;
  406. touch.panel_res = W8001_PEN_RESOLUTION;
  407. }
  408. input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
  409. input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
  410. input_abs_set_res(dev, ABS_X, touch.panel_res);
  411. input_abs_set_res(dev, ABS_Y, touch.panel_res);
  412. switch (touch.sensor_id) {
  413. case 0:
  414. case 2:
  415. w8001->pktlen = W8001_PKTLEN_TOUCH93;
  416. w8001->id = 0x93;
  417. strlcat(basename, " 1FG", basename_sz);
  418. break;
  419. case 1:
  420. case 3:
  421. case 4:
  422. w8001->pktlen = W8001_PKTLEN_TOUCH9A;
  423. strlcat(basename, " 1FG", basename_sz);
  424. w8001->id = 0x9a;
  425. break;
  426. case 5:
  427. w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
  428. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  429. error = input_mt_init_slots(dev, 2, 0);
  430. if (error) {
  431. dev_err(&w8001->serio->dev,
  432. "failed to initialize MT slots: %d\n", error);
  433. return error;
  434. }
  435. input_set_abs_params(dev, ABS_MT_POSITION_X,
  436. 0, touch.x, 0, 0);
  437. input_set_abs_params(dev, ABS_MT_POSITION_Y,
  438. 0, touch.y, 0, 0);
  439. input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
  440. 0, MT_TOOL_MAX, 0, 0);
  441. input_abs_set_res(dev, ABS_MT_POSITION_X, touch.panel_res);
  442. input_abs_set_res(dev, ABS_MT_POSITION_Y, touch.panel_res);
  443. strlcat(basename, " 2FG", basename_sz);
  444. if (w8001->max_pen_x && w8001->max_pen_y)
  445. w8001->id = 0xE3;
  446. else
  447. w8001->id = 0xE2;
  448. break;
  449. }
  450. strlcat(basename, " Touchscreen", basename_sz);
  451. return 0;
  452. }
  453. static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001,
  454. struct serio *serio)
  455. {
  456. dev->phys = w8001->phys;
  457. dev->id.bustype = BUS_RS232;
  458. dev->id.product = w8001->id;
  459. dev->id.vendor = 0x056a;
  460. dev->id.version = 0x0100;
  461. dev->open = w8001_open;
  462. dev->close = w8001_close;
  463. dev->dev.parent = &serio->dev;
  464. input_set_drvdata(dev, w8001);
  465. }
  466. /*
  467. * w8001_disconnect() is the opposite of w8001_connect()
  468. */
  469. static void w8001_disconnect(struct serio *serio)
  470. {
  471. struct w8001 *w8001 = serio_get_drvdata(serio);
  472. serio_close(serio);
  473. if (w8001->pen_dev)
  474. input_unregister_device(w8001->pen_dev);
  475. if (w8001->touch_dev)
  476. input_unregister_device(w8001->touch_dev);
  477. kfree(w8001);
  478. serio_set_drvdata(serio, NULL);
  479. }
  480. /*
  481. * w8001_connect() is the routine that is called when someone adds a
  482. * new serio device that supports the w8001 protocol and registers it as
  483. * an input device.
  484. */
  485. static int w8001_connect(struct serio *serio, struct serio_driver *drv)
  486. {
  487. struct w8001 *w8001;
  488. struct input_dev *input_dev_pen;
  489. struct input_dev *input_dev_touch;
  490. char basename[64];
  491. int err, err_pen, err_touch;
  492. w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
  493. input_dev_pen = input_allocate_device();
  494. input_dev_touch = input_allocate_device();
  495. if (!w8001 || !input_dev_pen || !input_dev_touch) {
  496. err = -ENOMEM;
  497. goto fail1;
  498. }
  499. w8001->serio = serio;
  500. w8001->pen_dev = input_dev_pen;
  501. w8001->touch_dev = input_dev_touch;
  502. mutex_init(&w8001->mutex);
  503. init_completion(&w8001->cmd_done);
  504. snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
  505. serio_set_drvdata(serio, w8001);
  506. err = serio_open(serio, drv);
  507. if (err)
  508. goto fail2;
  509. err = w8001_detect(w8001);
  510. if (err)
  511. goto fail3;
  512. /* For backwards-compatibility we compose the basename based on
  513. * capabilities and then just append the tool type
  514. */
  515. strlcpy(basename, "Wacom Serial", sizeof(basename));
  516. err_pen = w8001_setup_pen(w8001, basename, sizeof(basename));
  517. err_touch = w8001_setup_touch(w8001, basename, sizeof(basename));
  518. if (err_pen && err_touch) {
  519. err = -ENXIO;
  520. goto fail3;
  521. }
  522. if (!err_pen) {
  523. strlcpy(w8001->pen_name, basename, sizeof(w8001->pen_name));
  524. strlcat(w8001->pen_name, " Pen", sizeof(w8001->pen_name));
  525. input_dev_pen->name = w8001->pen_name;
  526. w8001_set_devdata(input_dev_pen, w8001, serio);
  527. err = input_register_device(w8001->pen_dev);
  528. if (err)
  529. goto fail3;
  530. } else {
  531. input_free_device(input_dev_pen);
  532. input_dev_pen = NULL;
  533. w8001->pen_dev = NULL;
  534. }
  535. if (!err_touch) {
  536. strlcpy(w8001->touch_name, basename, sizeof(w8001->touch_name));
  537. strlcat(w8001->touch_name, " Finger",
  538. sizeof(w8001->touch_name));
  539. input_dev_touch->name = w8001->touch_name;
  540. w8001_set_devdata(input_dev_touch, w8001, serio);
  541. err = input_register_device(w8001->touch_dev);
  542. if (err)
  543. goto fail4;
  544. } else {
  545. input_free_device(input_dev_touch);
  546. input_dev_touch = NULL;
  547. w8001->touch_dev = NULL;
  548. }
  549. return 0;
  550. fail4:
  551. if (w8001->pen_dev)
  552. input_unregister_device(w8001->pen_dev);
  553. fail3:
  554. serio_close(serio);
  555. fail2:
  556. serio_set_drvdata(serio, NULL);
  557. fail1:
  558. input_free_device(input_dev_pen);
  559. input_free_device(input_dev_touch);
  560. kfree(w8001);
  561. return err;
  562. }
  563. static struct serio_device_id w8001_serio_ids[] = {
  564. {
  565. .type = SERIO_RS232,
  566. .proto = SERIO_W8001,
  567. .id = SERIO_ANY,
  568. .extra = SERIO_ANY,
  569. },
  570. { 0 }
  571. };
  572. MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
  573. static struct serio_driver w8001_drv = {
  574. .driver = {
  575. .name = "w8001",
  576. },
  577. .description = DRIVER_DESC,
  578. .id_table = w8001_serio_ids,
  579. .interrupt = w8001_interrupt,
  580. .connect = w8001_connect,
  581. .disconnect = w8001_disconnect,
  582. };
  583. module_serio_driver(w8001_drv);