1 /* $NetBSD: i2c.c,v 1.88 2022/07/23 03:05:27 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #ifdef _KERNEL_OPT 39 #include "opt_i2c.h" 40 41 #include "opt_fdt.h" 42 #ifdef FDT 43 #define I2C_USE_FDT 44 #endif /* FDT */ 45 46 #if defined(__aarch64__) || defined(__amd64__) 47 #include "acpica.h" 48 #if NACPICA > 0 49 #define I2C_USE_ACPI 50 #endif /* NACPICA > 0 */ 51 #endif /* __aarch64__ || __amd64__ */ 52 53 #endif /* _KERNEL_OPT */ 54 55 #include <sys/cdefs.h> 56 __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.88 2022/07/23 03:05:27 thorpej Exp $"); 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/device.h> 61 #include <sys/event.h> 62 #include <sys/conf.h> 63 #include <sys/malloc.h> 64 #include <sys/kmem.h> 65 #include <sys/kthread.h> 66 #include <sys/proc.h> 67 #include <sys/kernel.h> 68 #include <sys/fcntl.h> 69 #include <sys/module.h> 70 #include <sys/once.h> 71 #include <sys/mutex.h> 72 73 #ifdef I2C_USE_ACPI 74 #include <dev/acpi/acpivar.h> 75 #endif /* I2C_USE_ACPI */ 76 77 #ifdef I2C_USE_FDT 78 #include <dev/fdt/fdtvar.h> 79 #endif /* I2C_USE_FDT */ 80 81 #include <dev/i2c/i2cvar.h> 82 83 #include "ioconf.h" 84 #include "locators.h" 85 86 #ifndef I2C_MAX_ADDR 87 #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */ 88 #endif 89 90 struct iic_softc { 91 device_t sc_dev; 92 i2c_tag_t sc_tag; 93 device_t sc_devices[I2C_MAX_ADDR + 1]; 94 }; 95 96 static dev_type_open(iic_open); 97 static dev_type_close(iic_close); 98 static dev_type_ioctl(iic_ioctl); 99 100 int iic_init(void); 101 102 kmutex_t iic_mtx; 103 int iic_refcnt; 104 105 ONCE_DECL(iic_once); 106 107 const struct cdevsw iic_cdevsw = { 108 .d_open = iic_open, 109 .d_close = iic_close, 110 .d_read = noread, 111 .d_write = nowrite, 112 .d_ioctl = iic_ioctl, 113 .d_stop = nostop, 114 .d_tty = notty, 115 .d_poll = nopoll, 116 .d_mmap = nommap, 117 .d_kqfilter = nokqfilter, 118 .d_discard = nodiscard, 119 .d_flag = D_OTHER 120 }; 121 122 static void iic_smbus_intr_thread(void *); 123 static void iic_fill_compat(struct i2c_attach_args*, const char*, 124 size_t, char **); 125 126 static int 127 iic_print_direct(void *aux, const char *pnp) 128 { 129 struct i2c_attach_args *ia = aux; 130 131 if (pnp != NULL) 132 aprint_normal("%s%s%s%s at %s addr 0x%02x", 133 ia->ia_name ? ia->ia_name : "(unknown)", 134 ia->ia_ncompat ? " (" : "", 135 ia->ia_ncompat ? ia->ia_compat[0] : "", 136 ia->ia_ncompat ? ")" : "", 137 pnp, ia->ia_addr); 138 else 139 aprint_normal(" addr 0x%02x", ia->ia_addr); 140 141 return UNCONF; 142 } 143 144 static int 145 iic_print(void *aux, const char *pnp) 146 { 147 struct i2c_attach_args *ia = aux; 148 149 if (ia->ia_addr != (i2c_addr_t)IICCF_ADDR_DEFAULT) 150 aprint_normal(" addr 0x%x", ia->ia_addr); 151 152 return UNCONF; 153 } 154 155 static bool 156 iic_is_special_address(i2c_addr_t addr) 157 { 158 159 /* 160 * See: https://www.i2c-bus.org/addressing/ 161 */ 162 163 /* General Call (read) / Start Byte (write) */ 164 if (addr == 0x00) 165 return (true); 166 167 /* CBUS Addresses */ 168 if (addr == 0x01) 169 return (true); 170 171 /* Reserved for Different Bus Formats */ 172 if (addr == 0x02) 173 return (true); 174 175 /* Reserved for future purposes */ 176 if (addr == 0x03) 177 return (true); 178 179 /* High Speed Master Code */ 180 if ((addr & 0x7c) == 0x04) 181 return (true); 182 183 /* 10-bit Slave Addressing prefix */ 184 if ((addr & 0x7c) == 0x78) 185 return (true); 186 187 /* Reserved for future purposes */ 188 if ((addr & 0x7c) == 0x7c) 189 return (true); 190 191 return (false); 192 } 193 194 static int 195 iic_probe_none(struct iic_softc *sc, 196 const struct i2c_attach_args *ia, int flags) 197 { 198 199 return (0); 200 } 201 202 static int 203 iic_probe_smbus_quick_write(struct iic_softc *sc, 204 const struct i2c_attach_args *ia, int flags) 205 { 206 int error; 207 208 if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) { 209 error = iic_smbus_quick_write(ia->ia_tag, ia->ia_addr, flags); 210 } 211 (void) iic_release_bus(ia->ia_tag, flags); 212 213 return (error); 214 } 215 216 static int 217 iic_probe_smbus_receive_byte(struct iic_softc *sc, 218 const struct i2c_attach_args *ia, int flags) 219 { 220 int error; 221 222 if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) { 223 uint8_t dummy; 224 225 error = iic_smbus_receive_byte(ia->ia_tag, ia->ia_addr, 226 &dummy, flags); 227 } 228 (void) iic_release_bus(ia->ia_tag, flags); 229 230 return (error); 231 } 232 233 static bool 234 iic_indirect_driver_is_permitted(struct iic_softc *sc, cfdata_t cf) 235 { 236 prop_object_iterator_t iter; 237 prop_array_t permitlist; 238 prop_string_t pstr; 239 prop_type_t ptype; 240 bool rv = false; 241 242 permitlist = prop_dictionary_get(device_properties(sc->sc_dev), 243 I2C_PROP_INDIRECT_DEVICE_PERMITLIST); 244 if (permitlist == NULL) { 245 /* No permitlist -> everything allowed */ 246 return (true); 247 } 248 249 if ((ptype = prop_object_type(permitlist)) != PROP_TYPE_ARRAY) { 250 aprint_error_dev(sc->sc_dev, 251 "invalid property type (%d) for '%s'; must be array (%d)\n", 252 ptype, I2C_PROP_INDIRECT_DEVICE_PERMITLIST, 253 PROP_TYPE_ARRAY); 254 return (false); 255 } 256 257 iter = prop_array_iterator(permitlist); 258 while ((pstr = prop_object_iterator_next(iter)) != NULL) { 259 if (prop_string_equals_string(pstr, cf->cf_name)) { 260 rv = true; 261 break; 262 } 263 } 264 prop_object_iterator_release(iter); 265 266 return (rv); 267 } 268 269 static int 270 iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 271 { 272 struct iic_softc *sc = device_private(parent); 273 struct i2c_attach_args ia; 274 int (*probe_func)(struct iic_softc *, 275 const struct i2c_attach_args *, int); 276 prop_string_t pstr; 277 i2c_addr_t first_addr, last_addr; 278 279 /* 280 * Before we do any more work, consult the allowed-driver 281 * permit-list for this bus (if any). 282 */ 283 if (iic_indirect_driver_is_permitted(sc, cf) == false) 284 return (0); 285 286 /* default to "quick write". */ 287 probe_func = iic_probe_smbus_quick_write; 288 289 pstr = prop_dictionary_get(device_properties(sc->sc_dev), 290 I2C_PROP_INDIRECT_PROBE_STRATEGY); 291 if (pstr == NULL) { 292 /* Use the default. */ 293 } else if (prop_string_equals_string(pstr, 294 I2C_PROBE_STRATEGY_QUICK_WRITE)) { 295 probe_func = iic_probe_smbus_quick_write; 296 } else if (prop_string_equals_string(pstr, 297 I2C_PROBE_STRATEGY_RECEIVE_BYTE)) { 298 probe_func = iic_probe_smbus_receive_byte; 299 } else if (prop_string_equals_string(pstr, 300 I2C_PROBE_STRATEGY_NONE)) { 301 probe_func = iic_probe_none; 302 } else { 303 aprint_error_dev(sc->sc_dev, 304 "unknown probe strategy '%s'; defaulting to '%s'\n", 305 prop_string_value(pstr), 306 I2C_PROBE_STRATEGY_QUICK_WRITE); 307 308 /* Use the default. */ 309 } 310 311 ia.ia_tag = sc->sc_tag; 312 313 ia.ia_name = NULL; 314 ia.ia_ncompat = 0; 315 ia.ia_compat = NULL; 316 ia.ia_prop = NULL; 317 318 if (cf->cf_loc[IICCF_ADDR] == IICCF_ADDR_DEFAULT) { 319 /* 320 * This particular config directive has 321 * wildcarded the address, so we will 322 * scan the entire bus for it. 323 */ 324 first_addr = 0; 325 last_addr = I2C_MAX_ADDR; 326 } else { 327 /* 328 * This config directive hard-wires the i2c 329 * bus address for the device, so there is 330 * no need to go poking around at any other 331 * addresses. 332 */ 333 if (cf->cf_loc[IICCF_ADDR] < 0 || 334 cf->cf_loc[IICCF_ADDR] > I2C_MAX_ADDR) { 335 /* Invalid config directive! */ 336 return (0); 337 } 338 first_addr = last_addr = cf->cf_loc[IICCF_ADDR]; 339 } 340 341 for (ia.ia_addr = first_addr; ia.ia_addr <= last_addr; ia.ia_addr++) { 342 int error, match_result; 343 344 /* 345 * Skip I2C addresses that are reserved for 346 * special purposes. 347 */ 348 if (iic_is_special_address(ia.ia_addr)) 349 continue; 350 351 /* 352 * Skip addresses where a device is already attached. 353 */ 354 if (sc->sc_devices[ia.ia_addr] != NULL) 355 continue; 356 357 /* 358 * Call the "match" routine for the device. If that 359 * returns success, then call the probe strategy 360 * function. 361 * 362 * We do it in this order because i2c devices tend 363 * to be found at a small number of possible addresses 364 * (e.g. read-time clocks that are only ever found at 365 * 0x68). This gives the driver a chance to skip any 366 * address that are not valid for the device, saving 367 * us from having to poke at the bus to see if anything 368 * is there. 369 */ 370 match_result = config_probe(parent, cf, &ia);/*XXX*/ 371 if (match_result <= 0) 372 continue; 373 374 /* 375 * If the quality of the match by the driver was low 376 * (i.e. matched on being a valid address only, didn't 377 * perform any hardware probe), invoke our probe routine 378 * to see if it looks like something is really there. 379 */ 380 if (match_result == I2C_MATCH_ADDRESS_ONLY && 381 (error = (*probe_func)(sc, &ia, 0)) != 0) 382 continue; 383 384 sc->sc_devices[ia.ia_addr] = 385 config_attach(parent, cf, &ia, iic_print, CFARGS_NONE); 386 } 387 388 return 0; 389 } 390 391 static void 392 iic_child_detach(device_t parent, device_t child) 393 { 394 struct iic_softc *sc = device_private(parent); 395 int i; 396 397 for (i = 0; i <= I2C_MAX_ADDR; i++) 398 if (sc->sc_devices[i] == child) { 399 sc->sc_devices[i] = NULL; 400 break; 401 } 402 } 403 404 static int 405 iic_rescan(device_t self, const char *ifattr, const int *locators) 406 { 407 config_search(self, NULL, 408 CFARGS(.search = iic_search, 409 .locators = locators)); 410 return 0; 411 } 412 413 static int 414 iic_match(device_t parent, cfdata_t cf, void *aux) 415 { 416 417 return 1; 418 } 419 420 static void 421 iic_attach(device_t parent, device_t self, void *aux) 422 { 423 struct iic_softc *sc = device_private(self); 424 struct i2cbus_attach_args *iba = aux; 425 prop_array_t child_devices; 426 prop_dictionary_t props; 427 char *buf; 428 i2c_tag_t ic; 429 int rv; 430 bool no_indirect_config = false; 431 432 aprint_naive("\n"); 433 aprint_normal(": I2C bus\n"); 434 435 sc->sc_dev = self; 436 sc->sc_tag = iba->iba_tag; 437 ic = sc->sc_tag; 438 ic->ic_devname = device_xname(self); 439 440 LIST_INIT(&(sc->sc_tag->ic_list)); 441 LIST_INIT(&(sc->sc_tag->ic_proc_list)); 442 443 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 444 iic_smbus_intr_thread, ic, &ic->ic_intr_thread, 445 "%s", ic->ic_devname); 446 if (rv) 447 aprint_error_dev(self, "unable to create intr thread\n"); 448 449 if (!pmf_device_register(self, NULL, NULL)) 450 aprint_error_dev(self, "couldn't establish power handler\n"); 451 452 if (iba->iba_child_devices) { 453 child_devices = iba->iba_child_devices; 454 no_indirect_config = true; 455 } else { 456 props = device_properties(parent); 457 prop_dictionary_get_bool(props, "i2c-no-indirect-config", 458 &no_indirect_config); 459 child_devices = prop_dictionary_get(props, "i2c-child-devices"); 460 } 461 462 if (child_devices) { 463 unsigned int i, count; 464 prop_dictionary_t dev; 465 prop_data_t cdata; 466 uint32_t addr; 467 uint64_t cookie; 468 uint32_t cookietype; 469 const char *name; 470 struct i2c_attach_args ia; 471 devhandle_t devhandle; 472 int loc[IICCF_NLOCS]; 473 474 memset(loc, 0, sizeof loc); 475 count = prop_array_count(child_devices); 476 for (i = 0; i < count; i++) { 477 dev = prop_array_get(child_devices, i); 478 if (!dev) continue; 479 if (!prop_dictionary_get_string( 480 dev, "name", &name)) { 481 /* "name" property is optional. */ 482 name = NULL; 483 } 484 if (!prop_dictionary_get_uint32(dev, "addr", &addr)) 485 continue; 486 if (!prop_dictionary_get_uint64(dev, "cookie", &cookie)) 487 cookie = 0; 488 if (!prop_dictionary_get_uint32(dev, "cookietype", 489 &cookietype)) 490 cookietype = I2C_COOKIE_NONE; 491 loc[IICCF_ADDR] = addr; 492 493 memset(&ia, 0, sizeof ia); 494 ia.ia_addr = addr; 495 ia.ia_tag = ic; 496 ia.ia_name = name; 497 ia.ia_cookie = cookie; 498 ia.ia_cookietype = cookietype; 499 ia.ia_prop = dev; 500 501 devhandle = devhandle_invalid(); 502 #ifdef I2C_USE_FDT 503 if (cookietype == I2C_COOKIE_OF) { 504 devhandle = devhandle_from_of(devhandle, 505 (int)cookie); 506 } 507 #endif /* I2C_USE_FDT */ 508 #ifdef I2C_USE_ACPI 509 if (cookietype == I2C_COOKIE_ACPI) { 510 devhandle = 511 devhandle_from_acpi(devhandle, 512 (ACPI_HANDLE)cookie); 513 } 514 #endif /* I2C_USE_ACPI */ 515 516 buf = NULL; 517 cdata = prop_dictionary_get(dev, "compatible"); 518 if (cdata) 519 iic_fill_compat(&ia, 520 prop_data_value(cdata), 521 prop_data_size(cdata), &buf); 522 523 if (name == NULL && cdata == NULL) { 524 aprint_error_dev(self, 525 "WARNING: ignoring bad child device entry " 526 "for address 0x%02x\n", addr); 527 } else { 528 if (addr > I2C_MAX_ADDR) { 529 aprint_error_dev(self, 530 "WARNING: ignoring bad device " 531 "address @ 0x%02x\n", addr); 532 } else if (sc->sc_devices[addr] == NULL) { 533 sc->sc_devices[addr] = 534 config_found(self, &ia, 535 iic_print_direct, 536 CFARGS(.locators = loc, 537 .devhandle = devhandle)); 538 } 539 } 540 541 if (ia.ia_compat) 542 free(ia.ia_compat, M_TEMP); 543 if (buf) 544 free(buf, M_TEMP); 545 } 546 } else if (!no_indirect_config) { 547 /* 548 * Attach all i2c devices described in the kernel 549 * configuration file. 550 */ 551 iic_rescan(self, "iic", NULL); 552 } 553 } 554 555 static int 556 iic_detach(device_t self, int flags) 557 { 558 struct iic_softc *sc = device_private(self); 559 i2c_tag_t ic = sc->sc_tag; 560 int i, error; 561 void *hdl; 562 563 for (i = 0; i <= I2C_MAX_ADDR; i++) { 564 if (sc->sc_devices[i]) { 565 error = config_detach(sc->sc_devices[i], flags); 566 if (error) 567 return error; 568 } 569 } 570 571 if (ic->ic_running) { 572 ic->ic_running = 0; 573 wakeup(ic); 574 kthread_join(ic->ic_intr_thread); 575 } 576 577 if (!LIST_EMPTY(&ic->ic_list)) { 578 device_printf(self, "WARNING: intr handler list not empty\n"); 579 while (!LIST_EMPTY(&ic->ic_list)) { 580 hdl = LIST_FIRST(&ic->ic_list); 581 iic_smbus_intr_disestablish(ic, hdl); 582 } 583 } 584 if (!LIST_EMPTY(&ic->ic_proc_list)) { 585 device_printf(self, "WARNING: proc handler list not empty\n"); 586 while (!LIST_EMPTY(&ic->ic_proc_list)) { 587 hdl = LIST_FIRST(&ic->ic_proc_list); 588 iic_smbus_intr_disestablish_proc(ic, hdl); 589 } 590 } 591 592 pmf_device_deregister(self); 593 594 return 0; 595 } 596 597 static void 598 iic_smbus_intr_thread(void *aux) 599 { 600 i2c_tag_t ic; 601 struct ic_intr_list *il; 602 603 ic = (i2c_tag_t)aux; 604 ic->ic_running = 1; 605 ic->ic_pending = 0; 606 607 while (ic->ic_running) { 608 if (ic->ic_pending == 0) 609 tsleep(ic, PZERO, "iicintr", hz); 610 if (ic->ic_pending > 0) { 611 LIST_FOREACH(il, &(ic->ic_proc_list), il_next) { 612 (*il->il_intr)(il->il_intrarg); 613 } 614 ic->ic_pending--; 615 } 616 } 617 618 kthread_exit(0); 619 } 620 621 void * 622 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 623 { 624 struct ic_intr_list *il; 625 626 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 627 if (il == NULL) 628 return NULL; 629 630 il->il_intr = intr; 631 il->il_intrarg = intrarg; 632 633 LIST_INSERT_HEAD(&(ic->ic_list), il, il_next); 634 635 return il; 636 } 637 638 void 639 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl) 640 { 641 struct ic_intr_list *il; 642 643 il = (struct ic_intr_list *)hdl; 644 645 LIST_REMOVE(il, il_next); 646 free(il, M_DEVBUF); 647 648 return; 649 } 650 651 void * 652 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 653 { 654 struct ic_intr_list *il; 655 656 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 657 if (il == NULL) 658 return NULL; 659 660 il->il_intr = intr; 661 il->il_intrarg = intrarg; 662 663 LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next); 664 665 return il; 666 } 667 668 void 669 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl) 670 { 671 struct ic_intr_list *il; 672 673 il = (struct ic_intr_list *)hdl; 674 675 LIST_REMOVE(il, il_next); 676 free(il, M_DEVBUF); 677 678 return; 679 } 680 681 int 682 iic_smbus_intr(i2c_tag_t ic) 683 { 684 struct ic_intr_list *il; 685 686 LIST_FOREACH(il, &(ic->ic_list), il_next) { 687 (*il->il_intr)(il->il_intrarg); 688 } 689 690 ic->ic_pending++; 691 wakeup(ic); 692 693 return 1; 694 } 695 696 static void 697 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len, 698 char **buffer) 699 { 700 int count, i; 701 const char *c, *start, **ptr; 702 703 *buffer = NULL; 704 for (i = count = 0, c = compat; i < len; i++, c++) 705 if (*c == 0) 706 count++; 707 count += 2; 708 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK); 709 if (!ptr) return; 710 711 for (i = count = 0, start = c = compat; i < len; i++, c++) { 712 if (*c == 0) { 713 ptr[count++] = start; 714 start = c+1; 715 } 716 } 717 if (start < compat+len) { 718 /* last string not 0 terminated */ 719 size_t l = c-start; 720 *buffer = malloc(l+1, M_TEMP, M_WAITOK); 721 memcpy(*buffer, start, l); 722 (*buffer)[l] = 0; 723 ptr[count++] = *buffer; 724 } 725 ptr[count] = NULL; 726 727 ia->ia_compat = ptr; 728 ia->ia_ncompat = count; 729 } 730 731 /* 732 * iic_compatible_match -- 733 * Match a device's "compatible" property against the list 734 * of compatible strings provided by the driver. 735 */ 736 int 737 iic_compatible_match(const struct i2c_attach_args *ia, 738 const struct device_compatible_entry *compats) 739 { 740 int match_result; 741 742 match_result = device_compatible_match(ia->ia_compat, ia->ia_ncompat, 743 compats); 744 if (match_result) { 745 match_result = 746 MIN(I2C_MATCH_DIRECT_COMPATIBLE + match_result - 1, 747 I2C_MATCH_DIRECT_COMPATIBLE_MAX); 748 } 749 750 return match_result; 751 } 752 753 /* 754 * iic_compatible_lookup -- 755 * Look the compatible entry that matches one of the driver's 756 * "compatible" strings. The first match is returned. 757 */ 758 const struct device_compatible_entry * 759 iic_compatible_lookup(const struct i2c_attach_args *ia, 760 const struct device_compatible_entry *compats) 761 { 762 return device_compatible_lookup(ia->ia_compat, ia->ia_ncompat, 763 compats); 764 } 765 766 /* 767 * iic_use_direct_match -- 768 * Helper for direct-config of i2c. Returns true if this is 769 * a direct-config situation, along with match result. 770 * Returns false if the driver should use indirect-config 771 * matching logic. 772 */ 773 bool 774 iic_use_direct_match(const struct i2c_attach_args *ia, const cfdata_t cf, 775 const struct device_compatible_entry *compats, 776 int *match_resultp) 777 { 778 KASSERT(match_resultp != NULL); 779 780 if (ia->ia_name != NULL && 781 strcmp(ia->ia_name, cf->cf_name) == 0) { 782 *match_resultp = I2C_MATCH_DIRECT_SPECIFIC; 783 return true; 784 } 785 786 if (ia->ia_ncompat > 0 && ia->ia_compat != NULL) { 787 *match_resultp = iic_compatible_match(ia, compats); 788 return true; 789 } 790 791 return false; 792 } 793 794 static int 795 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 796 { 797 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 798 799 mutex_enter(&iic_mtx); 800 if (sc == NULL) { 801 mutex_exit(&iic_mtx); 802 return ENXIO; 803 } 804 iic_refcnt++; 805 mutex_exit(&iic_mtx); 806 807 return 0; 808 } 809 810 static int 811 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 812 { 813 814 mutex_enter(&iic_mtx); 815 iic_refcnt--; 816 mutex_exit(&iic_mtx); 817 818 return 0; 819 } 820 821 static int 822 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 823 { 824 i2c_tag_t ic = sc->sc_tag; 825 uint8_t *buf = NULL; 826 void *cmd = NULL; 827 int error; 828 829 /* Validate parameters */ 830 if (iie->iie_addr > I2C_MAX_ADDR) 831 return EINVAL; 832 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 833 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 834 return EINVAL; 835 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 836 return EINVAL; 837 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 838 return EINVAL; 839 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 840 return EBADF; 841 842 #if 0 843 /* Disallow userspace access to devices that have drivers attached. */ 844 if (sc->sc_devices[iie->iie_addr] != NULL) 845 return EBUSY; 846 #endif 847 848 if (iie->iie_cmd != NULL) { 849 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 850 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 851 if (error) 852 goto out; 853 } 854 855 if (iie->iie_buf != NULL) { 856 buf = kmem_alloc(iie->iie_buflen, KM_SLEEP); 857 if (I2C_OP_WRITE_P(iie->iie_op)) { 858 error = copyin(iie->iie_buf, buf, iie->iie_buflen); 859 if (error) 860 goto out; 861 } 862 } 863 864 iic_acquire_bus(ic, 0); 865 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 866 buf, iie->iie_buflen, 0); 867 iic_release_bus(ic, 0); 868 869 /* 870 * Some drivers return error codes on failure, and others return -1. 871 */ 872 if (error < 0) 873 error = EIO; 874 875 out: 876 if (iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op)) 877 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 878 879 if (buf) 880 kmem_free(buf, iie->iie_buflen); 881 882 if (cmd) 883 kmem_free(cmd, iie->iie_cmdlen); 884 885 if (error) 886 return error; 887 888 return error; 889 } 890 891 static int 892 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 893 { 894 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 895 896 if (sc == NULL) 897 return ENXIO; 898 899 switch (cmd) { 900 case I2C_IOCTL_EXEC: 901 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 902 default: 903 return ENODEV; 904 } 905 } 906 907 908 CFATTACH_DECL3_NEW(iic, sizeof(struct iic_softc), 909 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach, 910 DVF_DETACH_SHUTDOWN); 911 912 MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec,i2c_bitbang,i2c_subr"); 913 914 #ifdef _MODULE 915 #include "ioconf.c" 916 #endif 917 918 int 919 iic_init(void) 920 { 921 922 mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE); 923 iic_refcnt = 0; 924 return 0; 925 } 926 927 static int 928 iic_modcmd(modcmd_t cmd, void *opaque) 929 { 930 #ifdef _MODULE 931 int bmajor, cmajor; 932 #endif 933 int error; 934 935 error = 0; 936 switch (cmd) { 937 case MODULE_CMD_INIT: 938 RUN_ONCE(&iic_once, iic_init); 939 940 #ifdef _MODULE 941 mutex_enter(&iic_mtx); 942 bmajor = cmajor = -1; 943 error = devsw_attach("iic", NULL, &bmajor, 944 &iic_cdevsw, &cmajor); 945 if (error != 0) { 946 mutex_exit(&iic_mtx); 947 break; 948 } 949 error = config_init_component(cfdriver_ioconf_iic, 950 cfattach_ioconf_iic, cfdata_ioconf_iic); 951 if (error) { 952 aprint_error("%s: unable to init component\n", 953 iic_cd.cd_name); 954 devsw_detach(NULL, &iic_cdevsw); 955 } 956 mutex_exit(&iic_mtx); 957 #endif 958 break; 959 case MODULE_CMD_FINI: 960 mutex_enter(&iic_mtx); 961 if (iic_refcnt != 0) { 962 mutex_exit(&iic_mtx); 963 return EBUSY; 964 } 965 #ifdef _MODULE 966 error = config_fini_component(cfdriver_ioconf_iic, 967 cfattach_ioconf_iic, cfdata_ioconf_iic); 968 if (error != 0) { 969 mutex_exit(&iic_mtx); 970 break; 971 } 972 devsw_detach(NULL, &iic_cdevsw); 973 #endif 974 mutex_exit(&iic_mtx); 975 break; 976 default: 977 error = ENOTTY; 978 } 979 return error; 980 } 981