wacom_w8001.c 17 KB

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