1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 /* 35 * ATMEL_MXT - Atmel MXT touchscreen driver 36 * 37 * PRELIMINARY DRIVER PRELIMINARY DRIVER PRELIMINARY DRIVERE 38 * 39 * (everything is pretty much hardwired and we assume the device is already 40 * operational, which it appears to be. ONLY TESTED ON ACER C720). 41 * 42 * This driver attaches to Acer TouchScreen MXT chipsets and currently 43 * emulates the ELO touchscreen serial protocol "elographics" for X: 44 * 45 * Section "InputDevice" 46 * Identifier "TouchScreen0" 47 * Driver "elographics" 48 * Option "Device" "/dev/atmel1-4a" 49 * EndSection 50 * 51 * The MXT chipsets typically attach on haswell chromebooks on one of the I2C 52 * busses at address 0x4A. On my Acer C720 it attaches to the ig4 driver's 53 * I2C bus #1 at 0x4A. kldload ig4; kldload atmel_mxt. 54 * 55 * The kernel driver and test code is written from scratch, but some code has 56 * been snarfed (in separate files) from linux development as referenced 57 * here: 58 * 59 * www.atmel.com/products/touchsolutions/touchscreens/unlimited_touch.aspx 60 * git://github.com/atmel-maxtouch/obp-utils.git 61 * 62 * The linux driver was also consulted, but not used. Note that the linux 63 * driver appears to be GPL'd but uses code from obp-utils.git on github 64 * which is (c)Copyright by Atmel and uses more of a BSD-like license. The 65 * obp-* source files contain the snarfed code and include this license. 66 * 67 * The ELO touchscreen serial protocol uses 10-byte fixed-length packets: 68 * 69 * Byte 0: ELO_SYNC_BYTE ('U') 70 * Byte 1-8: Packet data 71 * Byte 9: checksum of bytes 0 to 8 72 * 73 * Our interface reads and writes only whole packets and does not do any 74 * buffer stitching. It is compatible with Xorg. 75 * 76 * Control Commands sent from Userland (only an Ack packet is returned) 77 * 78 * Byte 0: ELO_SYNC_BYTE ('U') 79 * Byte 1: ELO_MODE ('m') 80 * Byte 2: Flags 81 * 0x80 - 82 * 0x40 Tracking mode 83 * 0x20 - 84 * 0x10 - 85 * 0x08 Scaling mode 86 * 0x04 Untouch mode 87 * 0x02 Streaming mode 88 * 0x01 Touch mode 89 * 90 * Byte 0: ELO_SYNC_BYTE ('U') 91 * Byte 1: ELO_REPORT ('r') 92 * 93 * Query Command sent from Userland: (expect response packet and Ack packet) 94 * 95 * Byte 0: ELO_SYNC_BYTE ('U') Request ID 96 * Byte 1: ELO_ID ('i') 97 * 98 * Byte 0: ELO_SYNC_BYTE ('U')` Request Owner 99 * Byte 1: ELO_OWNER ('o') 100 * 101 * Streaming packets sent from the driver to userland 102 * 103 * Byte 0: ELO_SYNC_BYTE ('U') 104 * Byte 1: ELO_TOUCH ('T') 105 * Byte 2: Packet type 106 * Bit 2 : Pen Up (Release) 0x04 107 * Bit 1 : Position (Stream) 0x02 108 * Bit 0 : Pen Down (Press) 0x01 109 * Byte 3: X coordinate lsb 110 * Byte 4: X coordinate msb 111 * Byte 5: Y coordinate lsb 112 * Byte 6: Y coordinate msb 113 * Byte 7: Z coordinate lsb 114 * Byte 8: Z coordinate msb 115 * 116 * Responses to commands: (one or two packets returned) 117 * 118 * Byte 0: ELO_SYNC_BYTE ('U') (if in response to query) 119 * Byte 1: toupper(command_byte) (control commands have no 120 * Byte 2-8: ... depends .... response) 121 * 122 * Byte 0: ELO_SYNC_BYTE ('U') (unconditional ack) 123 * Byte 1: 'A'ck 124 * Byte 2-8: ... depends .... 125 * 126 * NOTE! For the most part we ignore commands other than doing the handwaving 127 * to send a dummied-up response and ack as assumed by the X driver. 128 * Also, the read() and write() support only reads and writes whole 129 * packets. There is NO byte-partial buffering. The X driver appears 130 * to be compatible with these restrictions. 131 * 132 * figure out the bootstrapping and commands. 133 * 134 * Unable to locate any datasheet for the device. 135 * 136 * FEATURES 137 * 138 * Currently no features. Only one finger is supported by this attachment 139 * for now and I haven't written any de-jitter and finger-transfer code. 140 * This is good enough for moving and resizing windows (given big enough 141 * widgets), and hitting browser buttons and hotlinks. 142 * 143 * Currently no scrolling control or other features. We would need to 144 * basically implement either the linux general input even infrastructure 145 * which is a LOT of code, or a mouse emulator to handle scrolling emulation. 146 */ 147 #include <sys/kernel.h> 148 #include <sys/param.h> 149 #include <sys/systm.h> 150 #include <sys/device.h> 151 #include <sys/malloc.h> 152 #include <sys/module.h> 153 #include <sys/bus.h> 154 #include <sys/conf.h> 155 #include <sys/uio.h> 156 #include <sys/fcntl.h> 157 #include <sys/vnode.h> 158 #include <sys/sysctl.h> 159 #include <sys/event.h> 160 #include <sys/devfs.h> 161 162 #include <bus/smbus/smbconf.h> 163 #include <bus/smbus/smbus.h> 164 #include "atmel_mxt.h" 165 166 #include "smbus_if.h" 167 168 struct elopacket { 169 uint8_t sync; 170 uint8_t cmd; 171 uint8_t byte2; 172 uint8_t byte3; 173 uint8_t byte4; 174 uint8_t byte5; 175 uint8_t byte6; 176 uint8_t byte7; 177 uint8_t byte8; 178 uint8_t csum; 179 } __packed; 180 181 typedef struct elopacket elopacket_t; 182 183 typedef struct atmel_track { 184 uint16_t x; 185 uint16_t y; 186 uint16_t pressure; 187 int status; 188 int report; /* what we have to report */ 189 } atmel_track_t; 190 191 #define ATMEL_TRACK_RELEASED 0 192 #define ATMEL_TRACK_PRESSED 1 193 194 #define ATMEL_REPORT_PRESS 0x0001 195 #define ATMEL_REPORT_MOVE 0x0002 196 #define ATMEL_REPORT_RELEASE 0x0004 197 198 #define ATMEL_MAXTRACK 10 199 200 struct atmel_mxt_softc { 201 device_t dev; 202 int count; /* >0 if device opened */ 203 int unit; 204 int addr; 205 cdev_t devnode; 206 struct kqinfo kqinfo; 207 struct lock lk; 208 209 int poll_flags; 210 thread_t poll_td; 211 212 /* 213 * Hardware state 214 */ 215 struct mxt_rollup core; 216 struct mxt_object *msgprocobj; 217 struct mxt_object *cmdprocobj; 218 219 /* 220 * Capabilities 221 */ 222 short cap_resx; 223 short cap_resy; 224 225 /* 226 * Emulation 227 */ 228 atmel_track_t track[ATMEL_MAXTRACK]; 229 int tracking; 230 int track_fingers; 231 232 elopacket_t pend_rep; /* pending reply to command */ 233 int pend_ack; /* pending reply mode */ 234 235 int last_active_tick; 236 int last_calibrate_tick; 237 int data_signal; /* something ready to read */ 238 int blocked; /* someone is blocking */ 239 int reporting_mode; 240 int sample_rate; /* samples/sec */ 241 int poll_ticks; 242 }; 243 244 typedef struct atmel_mxt_softc atmel_mxt_softc_t; 245 246 #define ATMEL_POLL_SHUTDOWN 0x0001 247 248 #define PEND_ACK_NONE 0 /* no reply to command pending */ 249 #define PEND_ACK_RESPOND 1 /* reply w/response and ack */ 250 #define PEND_ACK_ACK 2 /* reply w/ack only */ 251 252 #define REPORT_NONE 0x0000 253 #define REPORT_ALL 0x0001 254 255 /* 256 * Async debug variable commands are executed by the poller and will 257 * auto-clear. 258 */ 259 static int atmel_mxt_idle_freq = 1; 260 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_idle_freq, CTLFLAG_RW, 261 &atmel_mxt_idle_freq, 0, ""); 262 static int atmel_mxt_slow_freq = 20; 263 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_slow_freq, CTLFLAG_RW, 264 &atmel_mxt_slow_freq, 0, ""); 265 static int atmel_mxt_norm_freq = 100; 266 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_norm_freq, CTLFLAG_RW, 267 &atmel_mxt_norm_freq, 0, ""); 268 static int atmel_mxt_minpressure = 16; 269 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_minpressure, CTLFLAG_RW, 270 &atmel_mxt_minpressure, 0, ""); 271 static int atmel_mxt_reset = 0; 272 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_reset, CTLFLAG_RW, 273 &atmel_mxt_reset, 0, ""); 274 275 /* 276 * Run a calibration command every N seconds only when idle. 0 to disable. 277 * Default every 30 seconds. 278 */ 279 static int atmel_mxt_autocalibrate = 30; 280 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_autocalibrate, CTLFLAG_RW, 281 &atmel_mxt_autocalibrate, 0, ""); 282 283 /* 284 * run a calibration on module startup. 285 */ 286 static int atmel_mxt_debug = 0; 287 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_debug, CTLFLAG_RW, 288 &atmel_mxt_debug, 0, ""); 289 290 static void atmel_mxt_poll_thread(void *arg); 291 static void atmel_find_active_state(atmel_mxt_softc_t *sc); 292 static int atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg); 293 static struct mxt_object *mxt_findobject(struct mxt_rollup *core, int type); 294 static int mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, 295 void *rbuf, int bytes); 296 static int mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, 297 void *xbuf, int bytes); 298 static int mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val); 299 static int mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj, 300 void *rbuf, int rbytes); 301 static int mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj, 302 int offset, uint8_t val); 303 static void atmel_reset_device(atmel_mxt_softc_t *sc); 304 305 static 306 const char * 307 msgflagsstr(uint8_t flags) 308 { 309 static char buf[9]; 310 311 buf[0] = (flags & MXT_MSGF_DETECT) ? 'D' : '.'; 312 buf[1] = (flags & MXT_MSGF_PRESS) ? 'P' : '.'; 313 buf[2] = (flags & MXT_MSGF_RELEASE) ? 'R' : '.'; 314 buf[3] = (flags & MXT_MSGF_MOVE) ? 'M' : '.'; 315 buf[4] = (flags & MXT_MSGF_VECTOR) ? 'V' : '.'; 316 buf[5] = (flags & MXT_MSGF_AMP) ? 'A' : '.'; 317 buf[6] = (flags & MXT_MSGF_SUPPRESS) ? 'S' : '.'; 318 buf[7] = (flags & MXT_MSGF_UNGRIP) ? 'U' : '.'; 319 320 return buf; 321 } 322 323 static 324 void 325 atmel_mxt_lock(atmel_mxt_softc_t *sc) 326 { 327 lockmgr(&sc->lk, LK_EXCLUSIVE); 328 } 329 330 static 331 void 332 atmel_mxt_unlock(atmel_mxt_softc_t *sc) 333 { 334 lockmgr(&sc->lk, LK_RELEASE); 335 } 336 337 /* 338 * Notify if possible receive data ready. Must be called 339 * without the lock held to avoid deadlocking in kqueue. 340 */ 341 static 342 void 343 atmel_mxt_notify(atmel_mxt_softc_t *sc) 344 { 345 if (sc->data_signal) { 346 KNOTE(&sc->kqinfo.ki_note, 0); 347 atmel_mxt_lock(sc); 348 if (sc->blocked) { 349 sc->blocked = 0; 350 wakeup(&sc->blocked); 351 } 352 atmel_mxt_unlock(sc); 353 } 354 } 355 356 /* 357 * Initialize the device 358 */ 359 static 360 int 361 init_device(atmel_mxt_softc_t *sc, int probe) 362 { 363 int blksize; 364 int totsize; 365 uint32_t crc; 366 367 if (mxt_read_reg(sc, 0, &sc->core.info, sizeof(sc->core.info))) { 368 device_printf(sc->dev, "init_device read-reg failed\n"); 369 return ENXIO; 370 } 371 sc->core.nobjs = sc->core.info.num_objects; 372 if (!probe) { 373 device_printf(sc->dev, 374 "%d configuration objects\n", 375 sc->core.info.num_objects); 376 } 377 if (sc->core.nobjs < 0 || sc->core.nobjs > 1024) { 378 device_printf(sc->dev, 379 "init_device nobjs (%d) out of bounds\n", 380 sc->core.nobjs); 381 return ENXIO; 382 } 383 blksize = sizeof(sc->core.info) + 384 sc->core.nobjs * sizeof(struct mxt_object); 385 totsize = blksize + sizeof(struct mxt_raw_crc); 386 387 sc->core.buf = kmalloc(totsize, M_DEVBUF, M_WAITOK | M_ZERO); 388 if (mxt_read_reg(sc, 0, sc->core.buf, totsize)) { 389 device_printf(sc->dev, 390 "init_device cannot read configuration space\n"); 391 goto done; 392 } 393 kprintf("COREBUF %p %d\n", sc->core.buf, blksize); 394 crc = obp_convert_crc((void *)((uint8_t *)sc->core.buf + blksize)); 395 if (obp_crc24(sc->core.buf, blksize) != crc) { 396 device_printf(sc->dev, 397 "init_device: configuration space " 398 "crc mismatch %08x/%08x\n", 399 crc, obp_crc24(sc->core.buf, blksize)); 400 /*goto done;*/ 401 } 402 { 403 int i; 404 405 kprintf("info: "); 406 for (i = 0; i < sizeof(sc->core.info); ++i) 407 kprintf(" %02x", sc->core.buf[i]); 408 kprintf("\nconfig: "); 409 while (i < blksize) { 410 kprintf(" %02x", sc->core.buf[i]); 411 ++i; 412 } 413 kprintf("\n"); 414 } 415 sc->core.objs = (void *)((uint8_t *)sc->core.buf + 416 sizeof(sc->core.info)); 417 sc->msgprocobj = mxt_findobject(&sc->core, MXT_GEN_MESSAGEPROCESSOR); 418 sc->cmdprocobj = mxt_findobject(&sc->core, MXT_GEN_COMMANDPROCESSOR); 419 if (sc->msgprocobj == NULL) { 420 device_printf(sc->dev, 421 "init_device: cannot find msgproc config\n"); 422 goto done; 423 } 424 425 done: 426 if (sc->msgprocobj == NULL) { 427 if (sc->core.buf) { 428 kfree(sc->core.buf, M_DEVBUF); 429 sc->core.buf = NULL; 430 } 431 return ENXIO; 432 } else { 433 if (probe) { 434 kfree(sc->core.buf, M_DEVBUF); 435 sc->core.buf = NULL; 436 } else { 437 atmel_reset_device(sc); 438 } 439 return 0; 440 } 441 } 442 443 /* 444 * Device infrastructure 445 */ 446 #define ATMEL_SOFTC(unit) \ 447 ((atmel_mxt_softc_t *)devclass_get_softc(atmel_mxt_devclass, (unit))) 448 449 static void atmel_mxt_identify(driver_t *driver, device_t parent); 450 static int atmel_mxt_probe(device_t); 451 static int atmel_mxt_attach(device_t); 452 static int atmel_mxt_detach(device_t); 453 454 static devclass_t atmel_mxt_devclass; 455 456 static device_method_t atmel_mxt_methods[] = { 457 /* device interface */ 458 DEVMETHOD(device_identify, atmel_mxt_identify), 459 DEVMETHOD(device_probe, atmel_mxt_probe), 460 DEVMETHOD(device_attach, atmel_mxt_attach), 461 DEVMETHOD(device_detach, atmel_mxt_detach), 462 463 #if 0 464 /* smbus interface */ 465 DEVMETHOD(smbus_intr, smbus_generic_intr), 466 #endif 467 468 DEVMETHOD_END 469 }; 470 471 static driver_t atmel_mxt_driver = { 472 "atmel_mxt", 473 atmel_mxt_methods, 474 sizeof(atmel_mxt_softc_t), 475 }; 476 477 static d_open_t atmel_mxtopen; 478 static d_close_t atmel_mxtclose; 479 static d_ioctl_t atmel_mxtioctl; 480 static d_read_t atmel_mxtread; 481 static d_write_t atmel_mxtwrite; 482 static d_kqfilter_t atmel_mxtkqfilter; 483 484 static struct dev_ops atmel_mxt_ops = { 485 { "atmel_mxt", 0, 0 }, 486 .d_open = atmel_mxtopen, 487 .d_close = atmel_mxtclose, 488 .d_ioctl = atmel_mxtioctl, 489 .d_read = atmel_mxtread, 490 .d_write = atmel_mxtwrite, 491 .d_kqfilter = atmel_mxtkqfilter, 492 }; 493 494 static void 495 atmel_mxt_identify(driver_t *driver, device_t parent) 496 { 497 if (device_find_child(parent, "atmel_mxt", -1) == NULL) 498 BUS_ADD_CHILD(parent, parent, 0, "atmel_mxt", -1); 499 } 500 501 static int 502 atmel_mxt_probe(device_t dev) 503 { 504 atmel_mxt_softc_t sc; 505 int error; 506 507 bzero(&sc, sizeof(sc)); 508 sc.dev = dev; 509 sc.unit = device_get_unit(dev); 510 511 /* 512 * Only match against specific addresses to avoid blowing up 513 * other I2C devices (?). At least for now. 514 * 515 * 0x400 (from smbus) - means specific device address probe, 516 * rather than generic. 517 * 518 * 0x4A - cypress trackpad on the acer c720. 519 */ 520 if ((sc.unit & 0x04FF) != (0x0400 | 0x04A)) 521 return ENXIO; 522 sc.addr = sc.unit & 0x3FF; 523 error = init_device(&sc, 1); 524 if (error) 525 return ENXIO; 526 527 device_set_desc(dev, "Atmel MXT TouchScreen"); 528 529 return (BUS_PROBE_VENDOR); 530 } 531 532 static int 533 atmel_mxt_attach(device_t dev) 534 { 535 atmel_mxt_softc_t *sc; 536 537 sc = (atmel_mxt_softc_t *)device_get_softc(dev); 538 if (!sc) 539 return ENOMEM; 540 541 bzero(sc, sizeof(*sc)); 542 543 lockinit(&sc->lk, "atmel_mxt", 0, 0); 544 sc->reporting_mode = 1; 545 546 sc->dev = dev; 547 sc->unit = device_get_unit(dev); 548 if ((sc->unit & 0x04FF) != (0x0400 | 0x04A)) 549 return ENXIO; 550 sc->addr = sc->unit & 0x3FF; 551 sc->last_active_tick = ticks; 552 sc->last_calibrate_tick = ticks - atmel_mxt_autocalibrate * hz; 553 554 if (init_device(sc, 0)) 555 return ENXIO; 556 557 if (sc->unit & 0x0400) { 558 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit, 559 UID_ROOT, GID_WHEEL, 0600, 560 "atmel%d-%02x", 561 sc->unit >> 11, sc->unit & 1023); 562 } else { 563 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit, 564 UID_ROOT, GID_WHEEL, 0600, "atmel%d", sc->unit); 565 } 566 device_printf(dev, "atmel mxt touchscreen driver attached\n"); 567 /* device_printf(dev, "...."); */ 568 569 /* 570 * Start the polling thread. 571 */ 572 lwkt_create(atmel_mxt_poll_thread, sc, 573 &sc->poll_td, NULL, 0, -1, "atmel_mxt-poll"); 574 575 return (0); 576 } 577 578 static int 579 atmel_mxt_detach(device_t dev) 580 { 581 atmel_mxt_softc_t *sc; 582 583 sc = (atmel_mxt_softc_t *)device_get_softc(dev); 584 585 /* 586 * Cleanup our poller thread 587 */ 588 atomic_set_int(&sc->poll_flags, ATMEL_POLL_SHUTDOWN); 589 while (sc->poll_td) { 590 wakeup(&sc->poll_flags); 591 tsleep(&sc->poll_td, 0, "atmel_mxtdet", hz); 592 } 593 594 if (sc->devnode) 595 dev_ops_remove_minor(&atmel_mxt_ops, device_get_unit(dev)); 596 if (sc->devnode) 597 devfs_assume_knotes(sc->devnode, &sc->kqinfo); 598 if (sc->core.buf) { 599 kfree(sc->core.buf, M_DEVBUF); 600 sc->core.buf = NULL; 601 } 602 sc->msgprocobj = NULL; 603 sc->cmdprocobj = NULL; 604 605 return (0); 606 } 607 608 /* 609 * USER DEVICE I/O FUNCTIONS 610 */ 611 static int 612 atmel_mxtopen (struct dev_open_args *ap) 613 { 614 cdev_t dev = ap->a_head.a_dev; 615 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 616 617 if (sc == NULL) 618 return (ENXIO); 619 620 if (sc->count != 0) 621 return (EBUSY); 622 623 sc->count++; 624 625 return (0); 626 } 627 628 static int 629 atmel_mxtclose(struct dev_close_args *ap) 630 { 631 cdev_t dev = ap->a_head.a_dev; 632 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 633 634 if (sc == NULL) 635 return (ENXIO); 636 637 if (sc->count == 0) { 638 /* This is not supposed to happen. */ 639 return (0); 640 } 641 642 if (sc->count-- == 0) { 643 sc->reporting_mode = 0; 644 } 645 646 return (0); 647 } 648 649 static int 650 atmel_mxtread(struct dev_read_args *ap) 651 { 652 cdev_t dev = ap->a_head.a_dev; 653 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 654 int error; 655 struct uio *uio = ap->a_uio; 656 int ioflag = ap->a_ioflag; 657 size_t n; 658 elopacket_t pkt; 659 atmel_track_t *track; 660 661 /* 662 * Load next ready event, block if necessary. 663 */ 664 atmel_mxt_lock(sc); 665 for (;;) { 666 error = 0; 667 668 switch(sc->pend_ack) { 669 case PEND_ACK_NONE: 670 if (sc->tracking && sc->track[sc->tracking].report) { 671 /* 672 * Report ready 673 */ 674 track = &sc->track[sc->tracking]; 675 bzero(&pkt, sizeof(pkt)); 676 pkt.cmd = 'T'; 677 if (track->report & ATMEL_REPORT_PRESS) { 678 pkt.byte2 |= 0x01; 679 track->report &= ~ATMEL_REPORT_PRESS; 680 } else if (track->report & ATMEL_REPORT_MOVE) { 681 pkt.byte2 |= 0x02; 682 track->report &= ~ATMEL_REPORT_MOVE; 683 } else if (track->report & 684 ATMEL_REPORT_RELEASE) { 685 pkt.byte2 |= 0x04; 686 track->report &= ~ATMEL_REPORT_RELEASE; 687 } 688 pkt.byte3 = track->x & 0xFF; 689 pkt.byte4 = track->x >> 8; 690 pkt.byte5 = track->y & 0xFF; 691 pkt.byte6 = track->y >> 8; 692 pkt.byte7 = track->pressure & 0xFF; 693 pkt.byte8 = track->pressure >> 8; 694 } else if (ioflag & IO_NDELAY) { 695 /* 696 * Non-blocking, nothing ready 697 */ 698 error = EWOULDBLOCK; 699 } else { 700 /* 701 * Blocking, nothing ready 702 */ 703 sc->data_signal = 0; 704 sc->blocked = 1; 705 error = lksleep(&sc->blocked, &sc->lk, 706 PCATCH, "atmelw", 0); 707 if (error == 0) 708 continue; 709 } 710 break; 711 case PEND_ACK_RESPOND: 712 pkt = sc->pend_rep; 713 sc->pend_ack = PEND_ACK_ACK; 714 break; 715 case PEND_ACK_ACK: 716 bzero(&pkt, sizeof(pkt)); 717 pkt.cmd = 'A'; 718 sc->pend_ack = PEND_ACK_NONE; 719 break; 720 } 721 atmel_find_active_state(sc); 722 break; 723 } 724 atmel_mxt_unlock(sc); 725 726 /* 727 * If no error we can return the event loaded into pkt. 728 */ 729 if (error == 0) { 730 uint8_t csum = 0xAA; 731 int i; 732 733 pkt.sync = 'U'; 734 for (i = 0; i < sizeof(pkt) - 1; ++i) 735 csum += ((uint8_t *)&pkt)[i]; 736 pkt.csum = csum; 737 n = uio->uio_resid; 738 if (n > sizeof(pkt)) 739 n = sizeof(pkt); 740 error = uiomove((void *)&pkt, n, uio); 741 } 742 743 return error; 744 } 745 746 static int 747 atmel_mxtwrite(struct dev_write_args *ap) 748 { 749 cdev_t dev = ap->a_head.a_dev; 750 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 751 struct uio *uio = ap->a_uio; 752 elopacket_t pkt; 753 int error; 754 size_t n; 755 756 error = 0; 757 758 while (uio->uio_resid) { 759 bzero(&pkt, sizeof(pkt)); 760 n = uio->uio_resid; 761 if (n > sizeof(pkt)) 762 n = sizeof(pkt); 763 error = uiomove((void *)&pkt, n, uio); 764 if (error) 765 break; 766 atmel_mxt_lock(sc); 767 switch(pkt.cmd) { 768 case 'i': 769 /* 770 * ELO_ID request id 771 */ 772 bzero(&sc->pend_rep, sizeof(sc->pend_rep)); 773 sc->pend_rep.cmd = 'I'; 774 sc->pend_ack = PEND_ACK_RESPOND; 775 break; 776 case 'o': 777 /* 778 * ELO_OWNER request owner 779 */ 780 bzero(&sc->pend_rep, sizeof(sc->pend_rep)); 781 sc->pend_rep.cmd = 'O'; 782 sc->pend_ack = PEND_ACK_RESPOND; 783 break; 784 case 'm': 785 /* 786 * ELO_MODE control packet 787 */ 788 sc->pend_ack = PEND_ACK_ACK; 789 break; 790 case 'r': 791 /* 792 * ELO_REPORT control packet 793 */ 794 sc->pend_ack = PEND_ACK_ACK; 795 break; 796 } 797 atmel_mxt_unlock(sc); 798 } 799 return error; 800 } 801 802 static void atmel_mxt_filt_detach(struct knote *); 803 static int atmel_mxt_filt(struct knote *, long); 804 805 static struct filterops atmel_mxt_filtops = 806 { FILTEROP_ISFD, NULL, atmel_mxt_filt_detach, atmel_mxt_filt }; 807 808 static int 809 atmel_mxtkqfilter(struct dev_kqfilter_args *ap) 810 { 811 cdev_t dev = ap->a_head.a_dev; 812 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 813 struct knote *kn = ap->a_kn; 814 struct klist *klist; 815 816 switch(kn->kn_filter) { 817 case EVFILT_READ: 818 kn->kn_fop = &atmel_mxt_filtops; 819 kn->kn_hook = (void *)sc; 820 ap->a_result = 0; 821 break; 822 default: 823 ap->a_result = EOPNOTSUPP; 824 return (0); 825 } 826 klist = &sc->kqinfo.ki_note; 827 knote_insert(klist, kn); 828 829 return (0); 830 } 831 832 static void 833 atmel_mxt_filt_detach(struct knote *kn) 834 { 835 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook; 836 struct klist *klist; 837 838 klist = &sc->kqinfo.ki_note; 839 knote_remove(klist, kn); 840 } 841 842 static int 843 atmel_mxt_filt(struct knote *kn, long hint) 844 { 845 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook; 846 int ready; 847 848 atmel_mxt_lock(sc); 849 if (sc->data_signal) 850 ready = 1; 851 else 852 ready = 0; 853 atmel_mxt_unlock(sc); 854 855 return (ready); 856 } 857 858 static int 859 atmel_mxtioctl(struct dev_ioctl_args *ap) 860 { 861 cdev_t dev = ap->a_head.a_dev; 862 device_t bus; /* smbbus */ 863 /*struct atmel_mxtcmd *s = (struct atmel_mxtcmd *)ap->a_data;*/ 864 void *s = NULL; 865 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 866 int error; 867 868 if (sc == NULL) 869 return (ENXIO); 870 if (s == NULL) 871 return (EINVAL); 872 873 /* 874 * NOTE: smbus_*() functions automatically recurse the parent to 875 * get to the actual device driver. 876 */ 877 bus = device_get_parent(sc->dev); /* smbus */ 878 879 /* Allocate the bus. */ 880 if ((error = smbus_request_bus(bus, sc->dev, 881 (ap->a_fflag & O_NONBLOCK) ? 882 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR)))) 883 return (error); 884 885 switch (ap->a_cmd) { 886 default: 887 #if 0 888 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data); 889 #endif 890 error = ENOTTY; 891 break; 892 } 893 894 smbus_release_bus(bus, sc->dev); 895 896 return (error); 897 } 898 899 /* 900 * MAJOR SUPPORT FUNCTIONS 901 */ 902 static 903 void 904 atmel_mxt_poll_thread(void *arg) 905 { 906 atmel_mxt_softc_t *sc = arg; 907 int error; 908 int freq = atmel_mxt_norm_freq; 909 int isidle = 0; 910 911 while ((sc->poll_flags & ATMEL_POLL_SHUTDOWN) == 0) { 912 if (sc->msgprocobj) 913 error = 0; 914 else 915 error = ENXIO; 916 if (error == 0) { 917 mxt_message_t msg; 918 919 error = mxt_read_object(sc, sc->msgprocobj, 920 &msg, sizeof(msg)); 921 if (error == 0) 922 isidle = atmel_mxt_raw_input(sc, &msg); 923 else 924 isidle = 1; 925 } 926 927 /* 928 * don't let the last_active_tick or last_calibrate_tick 929 * delta calculation overflow. 930 */ 931 if ((ticks - sc->last_active_tick) > 1000 * hz) 932 sc->last_active_tick = ticks - 1000 * hz; 933 if ((ticks - sc->last_calibrate_tick) > 1000 * hz) 934 sc->last_calibrate_tick = ticks - 1000 * hz; 935 936 /* 937 * Reset if requested 938 */ 939 if (atmel_mxt_reset) { 940 atmel_mxt_reset = 0; 941 atmel_reset_device(sc); 942 } 943 944 /* 945 * Automatically calibrate when the touchpad has been 946 * idle atmel_mxt_autocalibrate seconds, and recalibrate 947 * on the same interval while it remains idle. 948 * 949 * If we don't do this the touchscreen can get really out 950 * of whack over time and basically stop functioning properly. 951 * It's unclear why the device does not do this automatically. 952 * 953 * Response occurs in the message stream (which we just 954 * ignore). 955 */ 956 if (sc->cmdprocobj && atmel_mxt_autocalibrate && 957 ((ticks - sc->last_calibrate_tick) > 958 atmel_mxt_autocalibrate * hz) && 959 ((ticks - sc->last_active_tick) > 960 atmel_mxt_autocalibrate * hz)) { 961 sc->last_calibrate_tick = ticks; 962 mxt_write_object_off(sc, sc->cmdprocobj, 963 MXT_CMDPROC_CALIBRATE_OFF, 1); 964 } 965 tsleep(&sc->poll_flags, 0, "atmpol", (hz + freq - 1) / freq); 966 ++sc->poll_ticks; 967 if (sc->count == 0) 968 freq = atmel_mxt_idle_freq; 969 else if (isidle) 970 freq = atmel_mxt_slow_freq; 971 else 972 freq = atmel_mxt_norm_freq; 973 } 974 sc->poll_td = NULL; 975 wakeup(&sc->poll_td); 976 } 977 978 static 979 void 980 atmel_reset_device(atmel_mxt_softc_t *sc) 981 { 982 int dummy; 983 mxt_write_object_off(sc, sc->cmdprocobj, MXT_CMDPROC_RESET_OFF, 1); 984 tsleep(&dummy, 0, "atmel_reset", hz * 2); 985 } 986 987 /* 988 * Calculate currently active state, if any 989 */ 990 static 991 void 992 atmel_find_active_state(atmel_mxt_softc_t *sc) 993 { 994 atmel_track_t *track; 995 int i; 996 997 track = &sc->track[sc->tracking]; 998 if (track->report == 0) { 999 for (i = 0; i < ATMEL_MAXTRACK; ++i) { 1000 track = &sc->track[i]; 1001 if (track->report) { 1002 sc->tracking = i; 1003 break; 1004 } 1005 } 1006 } 1007 if (track->report == 0 && sc->pend_ack == PEND_ACK_NONE) { 1008 sc->data_signal = 0; 1009 } else { 1010 sc->data_signal = 1; 1011 } 1012 } 1013 1014 /* 1015 * Return non-zero if we are idle 1016 */ 1017 static 1018 int 1019 atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg) 1020 { 1021 atmel_track_t *track; 1022 int donotify = 0; 1023 1024 if (atmel_mxt_debug) { 1025 kprintf("track=%02x f=%s x=%-4d y=%-4d p=%d amp=%d\n", 1026 msg->any.reportid, 1027 msgflagsstr(msg->touch.flags), 1028 (msg->touch.pos[0] << 4) | 1029 ((msg->touch.pos[2] >> 4) & 0x0F), 1030 (msg->touch.pos[1] << 4) | 1031 ((msg->touch.pos[2]) & 0x0F), 1032 msg->touch.area, 1033 msg->touch.amplitude); 1034 } 1035 atmel_mxt_lock(sc); 1036 1037 /* 1038 * If message buffer is empty and no fingers are currently pressed 1039 * return idle, else we are not idle. 1040 */ 1041 if (msg->any.reportid == 0xFF) 1042 goto done; 1043 1044 /* 1045 * Process message buffer. For now ignore any messages with 1046 * reportids that we do not understand. 1047 * 1048 * note: reportid==1 typicallk acknowledges calibrations (?) 1049 */ 1050 if (msg->any.reportid < 3 || msg->any.reportid >= ATMEL_MAXTRACK) 1051 goto done; 1052 1053 sc->last_active_tick = ticks; 1054 1055 track = &sc->track[msg->any.reportid]; 1056 track->x = (msg->touch.pos[0] << 4) | 1057 ((msg->touch.pos[2] >> 4) & 0x0F); 1058 track->y = (msg->touch.pos[1] << 4) | 1059 (msg->touch.pos[2] & 0x0F); 1060 track->pressure = msg->touch.amplitude; 1061 1062 track->x = track->x * 3000 / 1361; 1063 track->y = track->y * 3000 / 3064; 1064 1065 if (msg->touch.flags & MXT_MSGF_DETECT) { 1066 track->status = ATMEL_TRACK_PRESSED; 1067 if (msg->touch.flags & MXT_MSGF_PRESS) { 1068 track->report |= ATMEL_REPORT_PRESS; 1069 } 1070 if (msg->touch.flags & MXT_MSGF_MOVE) { 1071 track->report |= ATMEL_REPORT_MOVE; 1072 } 1073 } else { 1074 track->status = ATMEL_TRACK_RELEASED; 1075 track->report |= ATMEL_REPORT_RELEASE; 1076 } 1077 atmel_find_active_state(sc); 1078 donotify = 1; 1079 done: 1080 atmel_mxt_unlock(sc); 1081 if (donotify) 1082 atmel_mxt_notify(sc); 1083 if (sc->track_fingers) 1084 return 0; 1085 else 1086 return 1; 1087 } 1088 1089 /* 1090 * Support functions 1091 */ 1092 static 1093 struct mxt_object * 1094 mxt_findobject(struct mxt_rollup *core, int type) 1095 { 1096 int i; 1097 1098 for (i = 0; i < core->nobjs; ++i) { 1099 if (core->objs[i].type == type) 1100 return(&core->objs[i]); 1101 } 1102 return NULL; 1103 } 1104 1105 static int 1106 mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, void *rbuf, int bytes) 1107 { 1108 uint8_t wreg[2]; 1109 device_t bus; 1110 int error; 1111 int rbytes; 1112 1113 wreg[0] = reg & 255; 1114 wreg[1] = reg >> 8; 1115 1116 bus = device_get_parent(sc->dev); 1117 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0) 1118 return error; 1119 error = smbus_trans(bus, sc->addr, 0, 1120 SMB_TRANS_NOCNT | 1121 SMB_TRANS_NOCMD | 1122 SMB_TRANS_7BIT, 1123 wreg, 2, 1124 rbuf, bytes, &rbytes); 1125 smbus_release_bus(bus, sc->dev); 1126 1127 if (bytes != rbytes) { 1128 device_printf(sc->dev, 1129 "smbus_trans reg %d short read %d/%d\n", 1130 reg, bytes, rbytes); 1131 error = EINVAL; 1132 } 1133 1134 return error; 1135 } 1136 1137 static int 1138 mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, void *xbuf, int bytes) 1139 { 1140 uint8_t wbuf[256]; 1141 device_t bus; 1142 int error; 1143 1144 KKASSERT(bytes < sizeof(wbuf) - 2); 1145 wbuf[0] = reg & 255; 1146 wbuf[1] = reg >> 8; 1147 bcopy(xbuf, wbuf + 2, bytes); 1148 1149 bus = device_get_parent(sc->dev); 1150 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0) 1151 return error; 1152 error = smbus_trans(bus, sc->addr, 0, 1153 SMB_TRANS_NOCNT | 1154 SMB_TRANS_NOCMD | 1155 SMB_TRANS_7BIT, 1156 wbuf, bytes + 2, 1157 NULL, 0, NULL); 1158 smbus_release_bus(bus, sc->dev); 1159 return error; 1160 } 1161 1162 static int 1163 mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val) 1164 { 1165 return mxt_write_reg_buf(sc, reg, &val, 1); 1166 } 1167 1168 static int 1169 mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj, 1170 void *rbuf, int rbytes) 1171 { 1172 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8); 1173 int bytes = obj->size_minus_one + 1; 1174 1175 if (bytes > rbytes) 1176 bytes = rbytes; 1177 return mxt_read_reg(sc, reg, rbuf, bytes); 1178 } 1179 1180 static int 1181 mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj, 1182 int offset, uint8_t val) 1183 { 1184 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8); 1185 1186 reg += offset; 1187 return mxt_write_reg(sc, reg, val); 1188 } 1189 1190 DRIVER_MODULE(atmel_mxt, smbus, atmel_mxt_driver, atmel_mxt_devclass, 1191 NULL, NULL); 1192 MODULE_DEPEND(atmel_mxt, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); 1193 MODULE_VERSION(atmel_mxt, 1); 1194