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/module.h> 152 #include <sys/bus.h> 153 #include <sys/conf.h> 154 #include <sys/uio.h> 155 #include <sys/fcntl.h> 156 #include <sys/vnode.h> 157 #include <sys/sysctl.h> 158 #include <sys/event.h> 159 #include <sys/devfs.h> 160 161 #include <bus/smbus/smbconf.h> 162 #include <bus/smbus/smbus.h> 163 #include "atmel_mxt.h" 164 165 #include "smbus_if.h" 166 167 struct elopacket { 168 uint8_t sync; 169 uint8_t cmd; 170 uint8_t byte2; 171 uint8_t byte3; 172 uint8_t byte4; 173 uint8_t byte5; 174 uint8_t byte6; 175 uint8_t byte7; 176 uint8_t byte8; 177 uint8_t csum; 178 } __packed; 179 180 typedef struct elopacket elopacket_t; 181 182 typedef struct atmel_track { 183 uint16_t x; 184 uint16_t y; 185 uint16_t pressure; 186 int status; 187 int report; /* what we have to report */ 188 } atmel_track_t; 189 190 #define ATMEL_TRACK_RELEASED 0 191 #define ATMEL_TRACK_PRESSED 1 192 193 #define ATMEL_REPORT_PRESS 0x0001 194 #define ATMEL_REPORT_MOVE 0x0002 195 #define ATMEL_REPORT_RELEASE 0x0004 196 197 #define ATMEL_MAXTRACK 10 198 199 struct atmel_mxt_softc { 200 device_t dev; 201 int count; /* >0 if device opened */ 202 int unit; 203 int addr; 204 cdev_t devnode; 205 struct kqinfo kqinfo; 206 struct lock lk; 207 208 int poll_flags; 209 thread_t poll_td; 210 211 /* 212 * Hardware state 213 */ 214 struct mxt_rollup core; 215 struct mxt_object *msgprocobj; 216 struct mxt_object *cmdprocobj; 217 218 /* 219 * Capabilities 220 */ 221 short cap_resx; 222 short cap_resy; 223 224 /* 225 * Emulation 226 */ 227 atmel_track_t track[ATMEL_MAXTRACK]; 228 int tracking; 229 int track_fingers; 230 231 elopacket_t pend_rep; /* pending reply to command */ 232 int pend_ack; /* pending reply mode */ 233 234 int last_active_tick; 235 int last_calibrate_tick; 236 int data_signal; /* something ready to read */ 237 int blocked; /* someone is blocking */ 238 int reporting_mode; 239 int sample_rate; /* samples/sec */ 240 int poll_ticks; 241 }; 242 243 typedef struct atmel_mxt_softc atmel_mxt_softc_t; 244 245 #define ATMEL_POLL_SHUTDOWN 0x0001 246 247 #define PEND_ACK_NONE 0 /* no reply to command pending */ 248 #define PEND_ACK_RESPOND 1 /* reply w/response and ack */ 249 #define PEND_ACK_ACK 2 /* reply w/ack only */ 250 251 #define REPORT_NONE 0x0000 252 #define REPORT_ALL 0x0001 253 254 /* 255 * Async debug variable commands are executed by the poller and will 256 * auto-clear. 257 */ 258 static int atmel_mxt_idle_freq = 1; 259 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_idle_freq, CTLFLAG_RW, 260 &atmel_mxt_idle_freq, 0, ""); 261 static int atmel_mxt_slow_freq = 20; 262 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_slow_freq, CTLFLAG_RW, 263 &atmel_mxt_slow_freq, 0, ""); 264 static int atmel_mxt_norm_freq = 100; 265 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_norm_freq, CTLFLAG_RW, 266 &atmel_mxt_norm_freq, 0, ""); 267 static int atmel_mxt_minpressure = 16; 268 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_minpressure, CTLFLAG_RW, 269 &atmel_mxt_minpressure, 0, ""); 270 static int atmel_mxt_reset = 0; 271 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_reset, CTLFLAG_RW, 272 &atmel_mxt_reset, 0, ""); 273 274 /* 275 * Run a calibration command every N seconds only when idle. 0 to disable. 276 * Default every 30 seconds. 277 */ 278 static int atmel_mxt_autocalibrate = 30; 279 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_autocalibrate, CTLFLAG_RW, 280 &atmel_mxt_autocalibrate, 0, ""); 281 282 /* 283 * run a calibration on module startup. 284 */ 285 static int atmel_mxt_debug = 0; 286 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_debug, CTLFLAG_RW, 287 &atmel_mxt_debug, 0, ""); 288 289 static void atmel_mxt_poll_thread(void *arg); 290 static void atmel_find_active_state(atmel_mxt_softc_t *sc); 291 static int atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg); 292 static struct mxt_object *mxt_findobject(struct mxt_rollup *core, int type); 293 static int mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, 294 void *rbuf, int bytes); 295 static int mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, 296 void *xbuf, int bytes); 297 static int mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val); 298 static int mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj, 299 void *rbuf, int rbytes); 300 static int mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj, 301 int offset, uint8_t val); 302 static void atmel_reset_device(atmel_mxt_softc_t *sc); 303 304 static 305 const char * 306 msgflagsstr(uint8_t flags) 307 { 308 static char buf[9]; 309 310 buf[0] = (flags & MXT_MSGF_DETECT) ? 'D' : '.'; 311 buf[1] = (flags & MXT_MSGF_PRESS) ? 'P' : '.'; 312 buf[2] = (flags & MXT_MSGF_RELEASE) ? 'R' : '.'; 313 buf[3] = (flags & MXT_MSGF_MOVE) ? 'M' : '.'; 314 buf[4] = (flags & MXT_MSGF_VECTOR) ? 'V' : '.'; 315 buf[5] = (flags & MXT_MSGF_AMP) ? 'A' : '.'; 316 buf[6] = (flags & MXT_MSGF_SUPPRESS) ? 'S' : '.'; 317 buf[7] = (flags & MXT_MSGF_UNGRIP) ? 'U' : '.'; 318 319 return buf; 320 } 321 322 static 323 void 324 atmel_mxt_lock(atmel_mxt_softc_t *sc) 325 { 326 lockmgr(&sc->lk, LK_EXCLUSIVE); 327 } 328 329 static 330 void 331 atmel_mxt_unlock(atmel_mxt_softc_t *sc) 332 { 333 lockmgr(&sc->lk, LK_RELEASE); 334 } 335 336 /* 337 * Notify if possible receive data ready. Must be called 338 * without the lock held to avoid deadlocking in kqueue. 339 */ 340 static 341 void 342 atmel_mxt_notify(atmel_mxt_softc_t *sc) 343 { 344 if (sc->data_signal) { 345 KNOTE(&sc->kqinfo.ki_note, 0); 346 atmel_mxt_lock(sc); 347 if (sc->blocked) { 348 sc->blocked = 0; 349 wakeup(&sc->blocked); 350 } 351 atmel_mxt_unlock(sc); 352 } 353 } 354 355 /* 356 * Initialize the device 357 */ 358 static 359 int 360 init_device(atmel_mxt_softc_t *sc, int probe) 361 { 362 int blksize; 363 int totsize; 364 uint32_t crc; 365 366 if (mxt_read_reg(sc, 0, &sc->core.info, sizeof(sc->core.info))) { 367 device_printf(sc->dev, "init_device read-reg failed\n"); 368 return ENXIO; 369 } 370 sc->core.nobjs = sc->core.info.num_objects; 371 if (!probe) { 372 device_printf(sc->dev, 373 "%d configuration objects\n", 374 sc->core.info.num_objects); 375 } 376 if (sc->core.nobjs < 0 || sc->core.nobjs > 1024) { 377 device_printf(sc->dev, 378 "init_device nobjs (%d) out of bounds\n", 379 sc->core.nobjs); 380 return ENXIO; 381 } 382 blksize = sizeof(sc->core.info) + 383 sc->core.nobjs * sizeof(struct mxt_object); 384 totsize = blksize + sizeof(struct mxt_raw_crc); 385 386 sc->core.buf = kmalloc(totsize, M_DEVBUF, M_WAITOK | M_ZERO); 387 if (mxt_read_reg(sc, 0, sc->core.buf, totsize)) { 388 device_printf(sc->dev, 389 "init_device cannot read configuration space\n"); 390 goto done; 391 } 392 kprintf("COREBUF %p %d\n", sc->core.buf, blksize); 393 crc = obp_convert_crc((void *)((uint8_t *)sc->core.buf + blksize)); 394 if (obp_crc24(sc->core.buf, blksize) != crc) { 395 device_printf(sc->dev, 396 "init_device: configuration space " 397 "crc mismatch %08x/%08x\n", 398 crc, obp_crc24(sc->core.buf, blksize)); 399 /*goto done;*/ 400 } 401 { 402 int i; 403 404 kprintf("info: "); 405 for (i = 0; i < sizeof(sc->core.info); ++i) 406 kprintf(" %02x", sc->core.buf[i]); 407 kprintf("\nconfig: "); 408 while (i < blksize) { 409 kprintf(" %02x", sc->core.buf[i]); 410 ++i; 411 } 412 kprintf("\n"); 413 } 414 sc->core.objs = (void *)((uint8_t *)sc->core.buf + 415 sizeof(sc->core.info)); 416 sc->msgprocobj = mxt_findobject(&sc->core, MXT_GEN_MESSAGEPROCESSOR); 417 sc->cmdprocobj = mxt_findobject(&sc->core, MXT_GEN_COMMANDPROCESSOR); 418 if (sc->msgprocobj == NULL) { 419 device_printf(sc->dev, 420 "init_device: cannot find msgproc config\n"); 421 goto done; 422 } 423 424 done: 425 if (sc->msgprocobj == NULL) { 426 if (sc->core.buf) { 427 kfree(sc->core.buf, M_DEVBUF); 428 sc->core.buf = NULL; 429 } 430 return ENXIO; 431 } else { 432 if (probe) { 433 kfree(sc->core.buf, M_DEVBUF); 434 sc->core.buf = NULL; 435 } else { 436 atmel_reset_device(sc); 437 } 438 return 0; 439 } 440 } 441 442 /* 443 * Device infrastructure 444 */ 445 #define ATMEL_SOFTC(unit) \ 446 ((atmel_mxt_softc_t *)devclass_get_softc(atmel_mxt_devclass, (unit))) 447 448 static void atmel_mxt_identify(driver_t *driver, device_t parent); 449 static int atmel_mxt_probe(device_t); 450 static int atmel_mxt_attach(device_t); 451 static int atmel_mxt_detach(device_t); 452 453 static devclass_t atmel_mxt_devclass; 454 455 static device_method_t atmel_mxt_methods[] = { 456 /* device interface */ 457 DEVMETHOD(device_identify, atmel_mxt_identify), 458 DEVMETHOD(device_probe, atmel_mxt_probe), 459 DEVMETHOD(device_attach, atmel_mxt_attach), 460 DEVMETHOD(device_detach, atmel_mxt_detach), 461 462 #if 0 463 /* smbus interface */ 464 DEVMETHOD(smbus_intr, smbus_generic_intr), 465 #endif 466 467 DEVMETHOD_END 468 }; 469 470 static driver_t atmel_mxt_driver = { 471 "atmel_mxt", 472 atmel_mxt_methods, 473 sizeof(atmel_mxt_softc_t), 474 }; 475 476 static d_open_t atmel_mxtopen; 477 static d_close_t atmel_mxtclose; 478 static d_ioctl_t atmel_mxtioctl; 479 static d_read_t atmel_mxtread; 480 static d_write_t atmel_mxtwrite; 481 static d_kqfilter_t atmel_mxtkqfilter; 482 483 static struct dev_ops atmel_mxt_ops = { 484 { "atmel_mxt", 0, 0 }, 485 .d_open = atmel_mxtopen, 486 .d_close = atmel_mxtclose, 487 .d_ioctl = atmel_mxtioctl, 488 .d_read = atmel_mxtread, 489 .d_write = atmel_mxtwrite, 490 .d_kqfilter = atmel_mxtkqfilter, 491 }; 492 493 static void 494 atmel_mxt_identify(driver_t *driver, device_t parent) 495 { 496 if (device_find_child(parent, "atmel_mxt", -1) == NULL) 497 BUS_ADD_CHILD(parent, parent, 0, "atmel_mxt", -1); 498 } 499 500 static int 501 atmel_mxt_probe(device_t dev) 502 { 503 atmel_mxt_softc_t sc; 504 int error; 505 506 bzero(&sc, sizeof(sc)); 507 sc.dev = dev; 508 sc.unit = device_get_unit(dev); 509 510 /* 511 * Only match against specific addresses to avoid blowing up 512 * other I2C devices (?). At least for now. 513 * 514 * 0x400 (from smbus) - means specific device address probe, 515 * rather than generic. 516 * 517 * 0x4A - cypress trackpad on the acer c720. 518 */ 519 if ((sc.unit & 0x04FF) != (0x0400 | 0x04A)) 520 return ENXIO; 521 sc.addr = sc.unit & 0x3FF; 522 error = init_device(&sc, 1); 523 if (error) 524 return ENXIO; 525 526 device_set_desc(dev, "Atmel MXT TouchScreen"); 527 528 return (BUS_PROBE_VENDOR); 529 } 530 531 static int 532 atmel_mxt_attach(device_t dev) 533 { 534 atmel_mxt_softc_t *sc; 535 536 sc = (atmel_mxt_softc_t *)device_get_softc(dev); 537 if (!sc) 538 return ENOMEM; 539 540 bzero(sc, sizeof(*sc)); 541 542 lockinit(&sc->lk, "atmel_mxt", 0, 0); 543 sc->reporting_mode = 1; 544 545 sc->dev = dev; 546 sc->unit = device_get_unit(dev); 547 if ((sc->unit & 0x04FF) != (0x0400 | 0x04A)) 548 return ENXIO; 549 sc->addr = sc->unit & 0x3FF; 550 sc->last_active_tick = ticks; 551 sc->last_calibrate_tick = ticks - atmel_mxt_autocalibrate * hz; 552 553 if (init_device(sc, 0)) 554 return ENXIO; 555 556 if (sc->unit & 0x0400) { 557 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit, 558 UID_ROOT, GID_WHEEL, 0600, 559 "atmel%d-%02x", 560 sc->unit >> 11, sc->unit & 1023); 561 } else { 562 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit, 563 UID_ROOT, GID_WHEEL, 0600, "atmel%d", sc->unit); 564 } 565 device_printf(dev, "atmel mxt touchscreen driver attached\n"); 566 /* device_printf(dev, "...."); */ 567 568 /* 569 * Start the polling thread. 570 */ 571 lwkt_create(atmel_mxt_poll_thread, sc, 572 &sc->poll_td, NULL, 0, -1, "atmel_mxt-poll"); 573 574 return (0); 575 } 576 577 static int 578 atmel_mxt_detach(device_t dev) 579 { 580 atmel_mxt_softc_t *sc; 581 582 sc = (atmel_mxt_softc_t *)device_get_softc(dev); 583 584 /* 585 * Cleanup our poller thread 586 */ 587 atomic_set_int(&sc->poll_flags, ATMEL_POLL_SHUTDOWN); 588 while (sc->poll_td) { 589 wakeup(&sc->poll_flags); 590 tsleep(&sc->poll_td, 0, "atmel_mxtdet", hz); 591 } 592 593 if (sc->devnode) 594 dev_ops_remove_minor(&atmel_mxt_ops, device_get_unit(dev)); 595 if (sc->devnode) 596 devfs_assume_knotes(sc->devnode, &sc->kqinfo); 597 if (sc->core.buf) { 598 kfree(sc->core.buf, M_DEVBUF); 599 sc->core.buf = NULL; 600 } 601 sc->msgprocobj = NULL; 602 sc->cmdprocobj = NULL; 603 604 return (0); 605 } 606 607 /* 608 * USER DEVICE I/O FUNCTIONS 609 */ 610 static int 611 atmel_mxtopen (struct dev_open_args *ap) 612 { 613 cdev_t dev = ap->a_head.a_dev; 614 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 615 616 if (sc == NULL) 617 return (ENXIO); 618 619 if (sc->count != 0) 620 return (EBUSY); 621 622 sc->count++; 623 624 return (0); 625 } 626 627 static int 628 atmel_mxtclose(struct dev_close_args *ap) 629 { 630 cdev_t dev = ap->a_head.a_dev; 631 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 632 633 if (sc == NULL) 634 return (ENXIO); 635 636 if (sc->count == 0) { 637 /* This is not supposed to happen. */ 638 return (0); 639 } 640 641 if (sc->count-- == 0) { 642 sc->reporting_mode = 0; 643 } 644 645 return (0); 646 } 647 648 static int 649 atmel_mxtread(struct dev_read_args *ap) 650 { 651 cdev_t dev = ap->a_head.a_dev; 652 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 653 int error; 654 struct uio *uio = ap->a_uio; 655 int ioflag = ap->a_ioflag; 656 size_t n; 657 elopacket_t pkt; 658 atmel_track_t *track; 659 660 /* 661 * Load next ready event, block if necessary. 662 */ 663 atmel_mxt_lock(sc); 664 for (;;) { 665 error = 0; 666 667 switch(sc->pend_ack) { 668 case PEND_ACK_NONE: 669 if (sc->tracking && sc->track[sc->tracking].report) { 670 /* 671 * Report ready 672 */ 673 track = &sc->track[sc->tracking]; 674 bzero(&pkt, sizeof(pkt)); 675 pkt.cmd = 'T'; 676 if (track->report & ATMEL_REPORT_PRESS) { 677 pkt.byte2 |= 0x01; 678 track->report &= ~ATMEL_REPORT_PRESS; 679 } else if (track->report & ATMEL_REPORT_MOVE) { 680 pkt.byte2 |= 0x02; 681 track->report &= ~ATMEL_REPORT_MOVE; 682 } else if (track->report & 683 ATMEL_REPORT_RELEASE) { 684 pkt.byte2 |= 0x04; 685 track->report &= ~ATMEL_REPORT_RELEASE; 686 } 687 pkt.byte3 = track->x & 0xFF; 688 pkt.byte4 = track->x >> 8; 689 pkt.byte5 = track->y & 0xFF; 690 pkt.byte6 = track->y >> 8; 691 pkt.byte7 = track->pressure & 0xFF; 692 pkt.byte8 = track->pressure >> 8; 693 } else if (ioflag & IO_NDELAY) { 694 /* 695 * Non-blocking, nothing ready 696 */ 697 error = EWOULDBLOCK; 698 } else { 699 /* 700 * Blocking, nothing ready 701 */ 702 sc->data_signal = 0; 703 sc->blocked = 1; 704 error = lksleep(&sc->blocked, &sc->lk, 705 PCATCH, "atmelw", 0); 706 if (error == 0) 707 continue; 708 } 709 break; 710 case PEND_ACK_RESPOND: 711 pkt = sc->pend_rep; 712 sc->pend_ack = PEND_ACK_ACK; 713 break; 714 case PEND_ACK_ACK: 715 bzero(&pkt, sizeof(pkt)); 716 pkt.cmd = 'A'; 717 sc->pend_ack = PEND_ACK_NONE; 718 break; 719 } 720 atmel_find_active_state(sc); 721 break; 722 } 723 atmel_mxt_unlock(sc); 724 725 /* 726 * If no error we can return the event loaded into pkt. 727 */ 728 if (error == 0) { 729 uint8_t csum = 0xAA; 730 int i; 731 732 pkt.sync = 'U'; 733 for (i = 0; i < sizeof(pkt) - 1; ++i) 734 csum += ((uint8_t *)&pkt)[i]; 735 pkt.csum = csum; 736 n = uio->uio_resid; 737 if (n > sizeof(pkt)) 738 n = sizeof(pkt); 739 error = uiomove((void *)&pkt, n, uio); 740 } 741 742 return error; 743 } 744 745 static int 746 atmel_mxtwrite(struct dev_write_args *ap) 747 { 748 cdev_t dev = ap->a_head.a_dev; 749 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 750 struct uio *uio = ap->a_uio; 751 elopacket_t pkt; 752 int error; 753 size_t n; 754 755 error = 0; 756 757 while (uio->uio_resid) { 758 bzero(&pkt, sizeof(pkt)); 759 n = uio->uio_resid; 760 if (n > sizeof(pkt)) 761 n = sizeof(pkt); 762 error = uiomove((void *)&pkt, n, uio); 763 if (error) 764 break; 765 atmel_mxt_lock(sc); 766 switch(pkt.cmd) { 767 case 'i': 768 /* 769 * ELO_ID request id 770 */ 771 bzero(&sc->pend_rep, sizeof(sc->pend_rep)); 772 sc->pend_rep.cmd = 'I'; 773 sc->pend_ack = PEND_ACK_RESPOND; 774 break; 775 case 'o': 776 /* 777 * ELO_OWNER request owner 778 */ 779 bzero(&sc->pend_rep, sizeof(sc->pend_rep)); 780 sc->pend_rep.cmd = 'O'; 781 sc->pend_ack = PEND_ACK_RESPOND; 782 break; 783 case 'm': 784 /* 785 * ELO_MODE control packet 786 */ 787 sc->pend_ack = PEND_ACK_ACK; 788 break; 789 case 'r': 790 /* 791 * ELO_REPORT control packet 792 */ 793 sc->pend_ack = PEND_ACK_ACK; 794 break; 795 } 796 atmel_mxt_unlock(sc); 797 } 798 return error; 799 } 800 801 static void atmel_mxt_filt_detach(struct knote *); 802 static int atmel_mxt_filt(struct knote *, long); 803 804 static struct filterops atmel_mxt_filtops = 805 { FILTEROP_ISFD, NULL, atmel_mxt_filt_detach, atmel_mxt_filt }; 806 807 static int 808 atmel_mxtkqfilter(struct dev_kqfilter_args *ap) 809 { 810 cdev_t dev = ap->a_head.a_dev; 811 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 812 struct knote *kn = ap->a_kn; 813 struct klist *klist; 814 815 switch(kn->kn_filter) { 816 case EVFILT_READ: 817 kn->kn_fop = &atmel_mxt_filtops; 818 kn->kn_hook = (void *)sc; 819 ap->a_result = 0; 820 break; 821 default: 822 ap->a_result = EOPNOTSUPP; 823 return (0); 824 } 825 klist = &sc->kqinfo.ki_note; 826 knote_insert(klist, kn); 827 828 return (0); 829 } 830 831 static void 832 atmel_mxt_filt_detach(struct knote *kn) 833 { 834 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook; 835 struct klist *klist; 836 837 klist = &sc->kqinfo.ki_note; 838 knote_remove(klist, kn); 839 } 840 841 static int 842 atmel_mxt_filt(struct knote *kn, long hint) 843 { 844 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook; 845 int ready; 846 847 atmel_mxt_lock(sc); 848 if (sc->data_signal) 849 ready = 1; 850 else 851 ready = 0; 852 atmel_mxt_unlock(sc); 853 854 return (ready); 855 } 856 857 static int 858 atmel_mxtioctl(struct dev_ioctl_args *ap) 859 { 860 cdev_t dev = ap->a_head.a_dev; 861 device_t bus; /* smbbus */ 862 /*struct atmel_mxtcmd *s = (struct atmel_mxtcmd *)ap->a_data;*/ 863 void *s = NULL; 864 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev)); 865 int error; 866 867 if (sc == NULL) 868 return (ENXIO); 869 if (s == NULL) 870 return (EINVAL); 871 872 /* 873 * NOTE: smbus_*() functions automatically recurse the parent to 874 * get to the actual device driver. 875 */ 876 bus = device_get_parent(sc->dev); /* smbus */ 877 878 /* Allocate the bus. */ 879 if ((error = smbus_request_bus(bus, sc->dev, 880 (ap->a_fflag & O_NONBLOCK) ? 881 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR)))) 882 return (error); 883 884 switch (ap->a_cmd) { 885 default: 886 #if 0 887 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data); 888 #endif 889 error = ENOTTY; 890 break; 891 } 892 893 smbus_release_bus(bus, sc->dev); 894 895 return (error); 896 } 897 898 /* 899 * MAJOR SUPPORT FUNCTIONS 900 */ 901 static 902 void 903 atmel_mxt_poll_thread(void *arg) 904 { 905 atmel_mxt_softc_t *sc = arg; 906 int error; 907 int freq = atmel_mxt_norm_freq; 908 int isidle = 0; 909 910 while ((sc->poll_flags & ATMEL_POLL_SHUTDOWN) == 0) { 911 if (sc->msgprocobj) 912 error = 0; 913 else 914 error = ENXIO; 915 if (error == 0) { 916 mxt_message_t msg; 917 918 error = mxt_read_object(sc, sc->msgprocobj, 919 &msg, sizeof(msg)); 920 if (error == 0) 921 isidle = atmel_mxt_raw_input(sc, &msg); 922 else 923 isidle = 1; 924 } 925 926 /* 927 * don't let the last_active_tick or last_calibrate_tick 928 * delta calculation overflow. 929 */ 930 if ((ticks - sc->last_active_tick) > 1000 * hz) 931 sc->last_active_tick = ticks - 1000 * hz; 932 if ((ticks - sc->last_calibrate_tick) > 1000 * hz) 933 sc->last_calibrate_tick = ticks - 1000 * hz; 934 935 /* 936 * Reset if requested 937 */ 938 if (atmel_mxt_reset) { 939 atmel_mxt_reset = 0; 940 atmel_reset_device(sc); 941 } 942 943 /* 944 * Automatically calibrate when the touchpad has been 945 * idle atmel_mxt_autocalibrate seconds, and recalibrate 946 * on the same interval while it remains idle. 947 * 948 * If we don't do this the touchscreen can get really out 949 * of whack over time and basically stop functioning properly. 950 * It's unclear why the device does not do this automatically. 951 * 952 * Response occurs in the message stream (which we just 953 * ignore). 954 */ 955 if (sc->cmdprocobj && atmel_mxt_autocalibrate && 956 ((ticks - sc->last_calibrate_tick) > 957 atmel_mxt_autocalibrate * hz) && 958 ((ticks - sc->last_active_tick) > 959 atmel_mxt_autocalibrate * hz)) { 960 sc->last_calibrate_tick = ticks; 961 mxt_write_object_off(sc, sc->cmdprocobj, 962 MXT_CMDPROC_CALIBRATE_OFF, 1); 963 } 964 tsleep(&sc->poll_flags, 0, "atmpol", (hz + freq - 1) / freq); 965 ++sc->poll_ticks; 966 if (sc->count == 0) 967 freq = atmel_mxt_idle_freq; 968 else if (isidle) 969 freq = atmel_mxt_slow_freq; 970 else 971 freq = atmel_mxt_norm_freq; 972 } 973 sc->poll_td = NULL; 974 wakeup(&sc->poll_td); 975 } 976 977 static 978 void 979 atmel_reset_device(atmel_mxt_softc_t *sc) 980 { 981 int dummy; 982 mxt_write_object_off(sc, sc->cmdprocobj, MXT_CMDPROC_RESET_OFF, 1); 983 tsleep(&dummy, 0, "atmel_reset", hz * 2); 984 } 985 986 /* 987 * Calculate currently active state, if any 988 */ 989 static 990 void 991 atmel_find_active_state(atmel_mxt_softc_t *sc) 992 { 993 atmel_track_t *track; 994 int i; 995 996 track = &sc->track[sc->tracking]; 997 if (track->report == 0) { 998 for (i = 0; i < ATMEL_MAXTRACK; ++i) { 999 track = &sc->track[i]; 1000 if (track->report) { 1001 sc->tracking = i; 1002 break; 1003 } 1004 } 1005 } 1006 if (track->report == 0 && sc->pend_ack == PEND_ACK_NONE) { 1007 sc->data_signal = 0; 1008 } else { 1009 sc->data_signal = 1; 1010 } 1011 } 1012 1013 /* 1014 * Return non-zero if we are idle 1015 */ 1016 static 1017 int 1018 atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg) 1019 { 1020 atmel_track_t *track; 1021 int donotify = 0; 1022 1023 if (atmel_mxt_debug) { 1024 kprintf("track=%02x f=%s x=%-4d y=%-4d p=%d amp=%d\n", 1025 msg->any.reportid, 1026 msgflagsstr(msg->touch.flags), 1027 (msg->touch.pos[0] << 4) | 1028 ((msg->touch.pos[2] >> 4) & 0x0F), 1029 (msg->touch.pos[1] << 4) | 1030 ((msg->touch.pos[2]) & 0x0F), 1031 msg->touch.area, 1032 msg->touch.amplitude); 1033 } 1034 atmel_mxt_lock(sc); 1035 1036 /* 1037 * If message buffer is empty and no fingers are currently pressed 1038 * return idle, else we are not idle. 1039 */ 1040 if (msg->any.reportid == 0xFF) 1041 goto done; 1042 1043 /* 1044 * Process message buffer. For now ignore any messages with 1045 * reportids that we do not understand. 1046 * 1047 * note: reportid==1 typicallk acknowledges calibrations (?) 1048 */ 1049 if (msg->any.reportid < 3 || msg->any.reportid >= ATMEL_MAXTRACK) 1050 goto done; 1051 1052 sc->last_active_tick = ticks; 1053 1054 track = &sc->track[msg->any.reportid]; 1055 track->x = (msg->touch.pos[0] << 4) | 1056 ((msg->touch.pos[2] >> 4) & 0x0F); 1057 track->y = (msg->touch.pos[1] << 4) | 1058 (msg->touch.pos[2] & 0x0F); 1059 track->pressure = msg->touch.amplitude; 1060 1061 track->x = track->x * 3000 / 1361; 1062 track->y = track->y * 3000 / 3064; 1063 1064 if (msg->touch.flags & MXT_MSGF_DETECT) { 1065 track->status = ATMEL_TRACK_PRESSED; 1066 if (msg->touch.flags & MXT_MSGF_PRESS) { 1067 track->report |= ATMEL_REPORT_PRESS; 1068 } 1069 if (msg->touch.flags & MXT_MSGF_MOVE) { 1070 track->report |= ATMEL_REPORT_MOVE; 1071 } 1072 } else { 1073 track->status = ATMEL_TRACK_RELEASED; 1074 track->report |= ATMEL_REPORT_RELEASE; 1075 } 1076 atmel_find_active_state(sc); 1077 donotify = 1; 1078 done: 1079 atmel_mxt_unlock(sc); 1080 if (donotify) 1081 atmel_mxt_notify(sc); 1082 if (sc->track_fingers) 1083 return 0; 1084 else 1085 return 1; 1086 } 1087 1088 /* 1089 * Support functions 1090 */ 1091 static 1092 struct mxt_object * 1093 mxt_findobject(struct mxt_rollup *core, int type) 1094 { 1095 int i; 1096 1097 for (i = 0; i < core->nobjs; ++i) { 1098 if (core->objs[i].type == type) 1099 return(&core->objs[i]); 1100 } 1101 return NULL; 1102 } 1103 1104 static int 1105 mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, void *rbuf, int bytes) 1106 { 1107 uint8_t wreg[2]; 1108 device_t bus; 1109 int error; 1110 int rbytes; 1111 1112 wreg[0] = reg & 255; 1113 wreg[1] = reg >> 8; 1114 1115 bus = device_get_parent(sc->dev); 1116 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0) 1117 return error; 1118 error = smbus_trans(bus, sc->addr, 0, 1119 SMB_TRANS_NOCNT | 1120 SMB_TRANS_NOCMD | 1121 SMB_TRANS_7BIT, 1122 wreg, 2, 1123 rbuf, bytes, &rbytes); 1124 smbus_release_bus(bus, sc->dev); 1125 1126 if (bytes != rbytes) { 1127 device_printf(sc->dev, 1128 "smbus_trans reg %d short read %d/%d\n", 1129 reg, bytes, rbytes); 1130 error = EINVAL; 1131 } 1132 1133 return error; 1134 } 1135 1136 static int 1137 mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, void *xbuf, int bytes) 1138 { 1139 uint8_t wbuf[256]; 1140 device_t bus; 1141 int error; 1142 1143 KKASSERT(bytes < sizeof(wbuf) - 2); 1144 wbuf[0] = reg & 255; 1145 wbuf[1] = reg >> 8; 1146 bcopy(xbuf, wbuf + 2, bytes); 1147 1148 bus = device_get_parent(sc->dev); 1149 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0) 1150 return error; 1151 error = smbus_trans(bus, sc->addr, 0, 1152 SMB_TRANS_NOCNT | 1153 SMB_TRANS_NOCMD | 1154 SMB_TRANS_7BIT, 1155 wbuf, bytes + 2, 1156 NULL, 0, NULL); 1157 smbus_release_bus(bus, sc->dev); 1158 return error; 1159 } 1160 1161 static int 1162 mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val) 1163 { 1164 return mxt_write_reg_buf(sc, reg, &val, 1); 1165 } 1166 1167 static int 1168 mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj, 1169 void *rbuf, int rbytes) 1170 { 1171 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8); 1172 int bytes = obj->size_minus_one + 1; 1173 1174 if (bytes > rbytes) 1175 bytes = rbytes; 1176 return mxt_read_reg(sc, reg, rbuf, bytes); 1177 } 1178 1179 static int 1180 mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj, 1181 int offset, uint8_t val) 1182 { 1183 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8); 1184 1185 reg += offset; 1186 return mxt_write_reg(sc, reg, val); 1187 } 1188 1189 DRIVER_MODULE(atmel_mxt, smbus, atmel_mxt_driver, atmel_mxt_devclass, 1190 NULL, NULL); 1191 MODULE_DEPEND(atmel_mxt, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); 1192 MODULE_VERSION(atmel_mxt, 1); 1193