1 /* $NetBSD: i2c.c,v 1.101 2025/09/21 17:54:16 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.101 2025/09/21 17:54:16 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 #include <dev/acpi/acpi_i2c.h> 76 #endif /* I2C_USE_ACPI */ 77 78 #ifdef I2C_USE_FDT 79 #include <dev/fdt/fdtvar.h> 80 #include <dev/fdt/fdt_i2c.h> 81 #endif /* I2C_USE_FDT */ 82 83 #include <dev/i2c/i2cvar.h> 84 #include <dev/i2c/i2c_calls.h> 85 86 #include "ioconf.h" 87 #include "locators.h" 88 89 #ifndef I2C_MAX_ADDR 90 #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */ 91 #endif 92 93 struct iic_softc { 94 device_t sc_dev; 95 i2c_tag_t sc_tag; 96 device_t sc_devices[I2C_MAX_ADDR + 1]; 97 }; 98 99 static dev_type_open(iic_open); 100 static dev_type_close(iic_close); 101 static dev_type_ioctl(iic_ioctl); 102 103 int iic_init(void); 104 105 kmutex_t iic_mtx; 106 int iic_refcnt; 107 108 ONCE_DECL(iic_once); 109 110 const struct cdevsw iic_cdevsw = { 111 .d_open = iic_open, 112 .d_close = iic_close, 113 .d_read = noread, 114 .d_write = nowrite, 115 .d_ioctl = iic_ioctl, 116 .d_stop = nostop, 117 .d_tty = notty, 118 .d_poll = nopoll, 119 .d_mmap = nommap, 120 .d_kqfilter = nokqfilter, 121 .d_discard = nodiscard, 122 .d_flag = D_OTHER 123 }; 124 125 static int 126 iic_print_direct(void *aux, const char *pnp) 127 { 128 struct i2c_attach_args *ia = aux; 129 130 if (pnp != NULL) { 131 aprint_normal("%s%s%s%s at %s addr 0x%02x", 132 ia->ia_name ? ia->ia_name : "(unknown)", 133 ia->ia_clist ? " (" : "", 134 ia->ia_clist ? ia->ia_clist : "", 135 ia->ia_clist ? ")" : "", 136 pnp, ia->ia_addr); 137 } else { 138 aprint_normal(" addr 0x%02x", ia->ia_addr); 139 } 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%02x", 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 int (*probe_func)(struct iic_softc *, 274 const struct i2c_attach_args *, int); 275 prop_string_t pstr; 276 i2c_addr_t first_addr, last_addr; 277 278 /* 279 * Before we do any more work, consult the allowed-driver 280 * permit-list for this bus (if any). 281 */ 282 if (iic_indirect_driver_is_permitted(sc, cf) == false) 283 return (0); 284 285 /* default to "quick write". */ 286 probe_func = iic_probe_smbus_quick_write; 287 288 pstr = prop_dictionary_get(device_properties(sc->sc_dev), 289 I2C_PROP_INDIRECT_PROBE_STRATEGY); 290 if (pstr == NULL) { 291 /* Use the default. */ 292 } else if (prop_string_equals_string(pstr, 293 I2C_PROBE_STRATEGY_QUICK_WRITE)) { 294 probe_func = iic_probe_smbus_quick_write; 295 } else if (prop_string_equals_string(pstr, 296 I2C_PROBE_STRATEGY_RECEIVE_BYTE)) { 297 probe_func = iic_probe_smbus_receive_byte; 298 } else if (prop_string_equals_string(pstr, 299 I2C_PROBE_STRATEGY_NONE)) { 300 probe_func = iic_probe_none; 301 } else { 302 aprint_error_dev(sc->sc_dev, 303 "unknown probe strategy '%s'; defaulting to '%s'\n", 304 prop_string_value(pstr), 305 I2C_PROBE_STRATEGY_QUICK_WRITE); 306 307 /* Use the default. */ 308 } 309 310 struct i2c_attach_args ia = { 311 .ia_tag = sc->sc_tag, 312 }; 313 314 if (cf->cf_loc[IICCF_ADDR] == IICCF_ADDR_DEFAULT) { 315 /* 316 * This particular config directive has 317 * wildcarded the address, so we will 318 * scan the entire bus for it. 319 */ 320 first_addr = 0; 321 last_addr = I2C_MAX_ADDR; 322 } else { 323 /* 324 * This config directive hard-wires the i2c 325 * bus address for the device, so there is 326 * no need to go poking around at any other 327 * addresses. 328 */ 329 if (cf->cf_loc[IICCF_ADDR] < 0 || 330 cf->cf_loc[IICCF_ADDR] > I2C_MAX_ADDR) { 331 /* Invalid config directive! */ 332 return (0); 333 } 334 first_addr = last_addr = cf->cf_loc[IICCF_ADDR]; 335 } 336 337 for (ia.ia_addr = first_addr; ia.ia_addr <= last_addr; ia.ia_addr++) { 338 int error, match_result; 339 340 /* 341 * Skip I2C addresses that are reserved for 342 * special purposes. 343 */ 344 if (iic_is_special_address(ia.ia_addr)) 345 continue; 346 347 /* 348 * Skip addresses where a device is already attached. 349 */ 350 if (sc->sc_devices[ia.ia_addr] != NULL) 351 continue; 352 353 /* 354 * Call the "match" routine for the device. If that 355 * returns success, then call the probe strategy 356 * function. 357 * 358 * We do it in this order because i2c devices tend 359 * to be found at a small number of possible addresses 360 * (e.g. read-time clocks that are only ever found at 361 * 0x68). This gives the driver a chance to skip any 362 * address that are not valid for the device, saving 363 * us from having to poke at the bus to see if anything 364 * is there. 365 */ 366 match_result = config_probe(parent, cf, &ia);/*XXX*/ 367 if (match_result <= 0) 368 continue; 369 370 /* 371 * If the quality of the match by the driver was low 372 * (i.e. matched on being a valid address only, didn't 373 * perform any hardware probe), invoke our probe routine 374 * to see if it looks like something is really there. 375 */ 376 if (match_result == I2C_MATCH_ADDRESS_ONLY && 377 (error = (*probe_func)(sc, &ia, 0)) != 0) 378 continue; 379 380 sc->sc_devices[ia.ia_addr] = 381 config_attach(parent, cf, &ia, iic_print, CFARGS_NONE); 382 } 383 384 return 0; 385 } 386 387 static void 388 iic_child_detach(device_t parent, device_t child) 389 { 390 struct iic_softc *sc = device_private(parent); 391 int i; 392 393 for (i = 0; i <= I2C_MAX_ADDR; i++) 394 if (sc->sc_devices[i] == child) { 395 sc->sc_devices[i] = NULL; 396 break; 397 } 398 } 399 400 static int 401 iic_rescan(device_t self, const char *ifattr, const int *locators) 402 { 403 config_search(self, NULL, 404 CFARGS(.search = iic_search, 405 .locators = locators)); 406 return 0; 407 } 408 409 static int 410 iic_match(device_t parent, cfdata_t cf, void *aux) 411 { 412 413 return 1; 414 } 415 416 static void 417 iic_attach_child_direct(struct iic_softc *sc, struct i2c_attach_args *ia) 418 { 419 device_t newdev; 420 int loc[IICCF_NLOCS] = { 421 [IICCF_ADDR] = ia->ia_addr, 422 }; 423 424 if (ia->ia_addr > I2C_MAX_ADDR) { 425 aprint_error_dev(sc->sc_dev, 426 "WARNING: ignoring bad device address @ 0x%x\n", 427 ia->ia_addr); 428 return; 429 } 430 431 if (sc->sc_devices[ia->ia_addr] != NULL) { 432 return; 433 } 434 435 newdev = config_found(sc->sc_dev, ia, iic_print_direct, 436 CFARGS(.submatch = config_stdsubmatch, 437 .locators = loc, 438 .devhandle = ia->ia_devhandle)); 439 440 sc->sc_devices[ia->ia_addr] = newdev; 441 } 442 443 static bool 444 i2c_enumerate_devices_callback(device_t self, 445 struct i2c_enumerate_devices_args *args) 446 { 447 struct iic_softc *sc = device_private(self); 448 449 iic_attach_child_direct(sc, args->ia); 450 451 return true; /* keep enumerating */ 452 } 453 454 static bool 455 iic_attach_children_direct(struct iic_softc *sc) 456 { 457 device_t parent = device_parent(sc->sc_dev); 458 devhandle_t devhandle = device_handle(sc->sc_dev); 459 prop_array_t child_devices; 460 bool no_indirect_config; 461 462 child_devices = prop_dictionary_get(device_properties(parent), 463 "i2c-child-devices"); 464 if (!prop_dictionary_get_bool(device_properties(parent), 465 "i2c-no-indirect-config", 466 &no_indirect_config)) { 467 no_indirect_config = false; 468 } 469 470 if (child_devices == NULL) { 471 switch (devhandle_type(devhandle)) { 472 #ifdef I2C_USE_ACPI 473 case DEVHANDLE_TYPE_ACPI: 474 child_devices = acpi_copy_i2c_devs(sc->sc_dev); 475 no_indirect_config = true; 476 break; 477 #endif 478 #ifdef I2C_USE_FDT 479 case DEVHANDLE_TYPE_OF: 480 child_devices = fdtbus_copy_i2c_devs(sc->sc_dev); 481 no_indirect_config = true; 482 break; 483 #endif 484 default: 485 break; 486 } 487 } else { 488 prop_object_retain(child_devices); 489 no_indirect_config = true; 490 } 491 492 /* 493 * If no explicit child device array is provided, then attempt 494 * to enumerate i2c devices using the platform device tree. 495 */ 496 struct i2c_attach_args ia = { 497 .ia_tag = sc->sc_tag, 498 }; 499 if (child_devices == NULL) { 500 struct i2c_enumerate_devices_args enumargs = { 501 .ia = &ia, 502 .callback = i2c_enumerate_devices_callback, 503 }; 504 if (device_call(sc->sc_dev, 505 I2C_ENUMERATE_DEVICES(&enumargs)) == 0) { 506 no_indirect_config = true; 507 } 508 goto done; 509 } 510 511 /* 512 * We have an explicit child device array to enumerate. 513 */ 514 prop_object_iterator_t iter = prop_array_iterator(child_devices); 515 prop_dictionary_t dev; 516 517 while ((dev = prop_object_iterator_next(iter)) != NULL) { 518 const void *vptr; 519 size_t vsize; 520 521 if (!prop_dictionary_get_uint16(dev, "addr", &ia.ia_addr)) { 522 continue; 523 } 524 525 if (!prop_dictionary_get_string(dev, "name", &ia.ia_name)) { 526 /* "name" property is optional. */ 527 ia.ia_name = NULL; 528 } 529 530 if (!prop_dictionary_get_data(dev, "compatible", 531 (const void **)&ia.ia_clist, 532 &ia.ia_clist_size)) { 533 ia.ia_clist = NULL; 534 ia.ia_clist_size = 0; 535 } 536 537 if (!prop_dictionary_get_data(dev, "devhandle", 538 &vptr, &vsize)) { 539 vptr = NULL; 540 } else if (vsize != sizeof(ia.ia_devhandle)) { 541 vptr = NULL; 542 } 543 if (vptr != NULL) { 544 memcpy(&ia.ia_devhandle, vptr, 545 sizeof(ia.ia_devhandle)); 546 } else { 547 ia.ia_devhandle = devhandle_invalid(); 548 } 549 550 if ((ia.ia_name == NULL && ia.ia_clist == NULL) || 551 ia.ia_addr > I2C_MAX_ADDR) { 552 aprint_error_dev(sc->sc_dev, 553 "WARNING: ignoring bad child device entry " 554 "for address 0x%x\n", ia.ia_addr); 555 continue; 556 } 557 558 iic_attach_child_direct(sc, &ia); 559 } 560 prop_object_iterator_release(iter); 561 prop_object_release(child_devices); 562 563 done: 564 /* 565 * We return "true" if we want to let indirect configuration 566 * proceed. 567 */ 568 return !no_indirect_config; 569 } 570 571 static void 572 iic_attach(device_t parent, device_t self, void *aux) 573 { 574 struct iic_softc *sc = device_private(self); 575 devhandle_t devhandle = device_handle(self); 576 struct i2cbus_attach_args *iba = aux; 577 578 aprint_naive("\n"); 579 aprint_normal(": I2C bus\n"); 580 581 sc->sc_dev = self; 582 sc->sc_tag = iba->iba_tag; 583 584 if (!pmf_device_register(self, NULL, NULL)) 585 aprint_error_dev(self, "couldn't establish power handler\n"); 586 587 /* XXX There ought to be a generic way to do this. */ 588 switch (devhandle_type(devhandle)) { 589 #ifdef I2C_USE_ACPI 590 case DEVHANDLE_TYPE_ACPI: 591 acpi_i2c_register(self, sc->sc_tag); 592 break; 593 #endif 594 #ifdef I2C_USE_FDT 595 case DEVHANDLE_TYPE_OF: 596 fdtbus_register_i2c_controller(self, sc->sc_tag); 597 break; 598 #endif 599 default: 600 break; 601 } 602 603 if (iic_attach_children_direct(sc)) { 604 /* 605 * Attach all i2c devices described in the kernel 606 * configuration file. 607 */ 608 iic_rescan(self, "iic", NULL); 609 } 610 } 611 612 static int 613 iic_detach(device_t self, int flags) 614 { 615 int error; 616 617 error = config_detach_children(self, flags); 618 if (error) 619 return error; 620 621 pmf_device_deregister(self); 622 623 return 0; 624 } 625 626 /* 627 * iic_compatible_match -- 628 * Match a device's "compatible" property against the list 629 * of compatible strings provided by the driver. 630 */ 631 int 632 iic_compatible_match(const struct i2c_attach_args *ia, 633 const struct device_compatible_entry *compats) 634 { 635 int match_result; 636 637 match_result = device_compatible_match_strlist(ia->ia_clist, 638 ia->ia_clist_size, compats); 639 if (match_result) { 640 match_result = 641 MIN(I2C_MATCH_DIRECT_COMPATIBLE + match_result - 1, 642 I2C_MATCH_DIRECT_COMPATIBLE_MAX); 643 } 644 645 return match_result; 646 } 647 648 /* 649 * iic_compatible_lookup -- 650 * Look the compatible entry that matches one of the driver's 651 * "compatible" strings. The first match is returned. 652 */ 653 const struct device_compatible_entry * 654 iic_compatible_lookup(const struct i2c_attach_args *ia, 655 const struct device_compatible_entry *compats) 656 { 657 return device_compatible_lookup_strlist(ia->ia_clist, 658 ia->ia_clist_size, compats); 659 } 660 661 /* 662 * iic_use_direct_match -- 663 * Helper for direct-config of i2c. Returns true if this is 664 * a direct-config situation, along with match result. 665 * Returns false if the driver should use indirect-config 666 * matching logic. 667 */ 668 bool 669 iic_use_direct_match(const struct i2c_attach_args *ia, const cfdata_t cf, 670 const struct device_compatible_entry *compats, 671 int *match_resultp) 672 { 673 KASSERT(match_resultp != NULL); 674 675 if (ia->ia_name != NULL && 676 strcmp(ia->ia_name, cf->cf_name) == 0) { 677 *match_resultp = I2C_MATCH_DIRECT_SPECIFIC; 678 return true; 679 } 680 681 if (ia->ia_clist != NULL && ia->ia_clist_size != 0) { 682 *match_resultp = iic_compatible_match(ia, compats); 683 return true; 684 } 685 686 return false; 687 } 688 689 static int 690 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 691 { 692 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 693 694 mutex_enter(&iic_mtx); 695 if (sc == NULL) { 696 mutex_exit(&iic_mtx); 697 return ENXIO; 698 } 699 iic_refcnt++; 700 mutex_exit(&iic_mtx); 701 702 return 0; 703 } 704 705 static int 706 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 707 { 708 709 mutex_enter(&iic_mtx); 710 iic_refcnt--; 711 mutex_exit(&iic_mtx); 712 713 return 0; 714 } 715 716 static int 717 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 718 { 719 i2c_tag_t ic = sc->sc_tag; 720 uint8_t *buf = NULL; 721 void *cmd = NULL; 722 int error = 0; 723 724 /* Validate parameters */ 725 if (iie->iie_addr > I2C_MAX_ADDR) 726 return EINVAL; 727 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 728 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 729 return EINVAL; 730 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 731 return EINVAL; 732 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 733 return EINVAL; 734 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 735 return EBADF; 736 737 #if 0 738 /* Disallow userspace access to devices that have drivers attached. */ 739 if (sc->sc_devices[iie->iie_addr] != NULL) 740 return EBUSY; 741 #endif 742 743 if (iie->iie_cmd != NULL) { 744 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 745 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 746 if (error) 747 goto out; 748 } 749 750 if (iie->iie_buf != NULL) { 751 buf = kmem_alloc(iie->iie_buflen, KM_SLEEP); 752 if (I2C_OP_WRITE_P(iie->iie_op)) { 753 error = copyin(iie->iie_buf, buf, iie->iie_buflen); 754 if (error) 755 goto out; 756 } 757 } 758 759 iic_acquire_bus(ic, 0); 760 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 761 buf, iie->iie_buflen, 0); 762 iic_release_bus(ic, 0); 763 764 /* 765 * Some drivers return error codes on failure, and others return -1. 766 */ 767 if (error < 0) 768 error = EIO; 769 770 out: 771 if (!error && iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op)) 772 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 773 774 if (buf) 775 kmem_free(buf, iie->iie_buflen); 776 777 if (cmd) 778 kmem_free(cmd, iie->iie_cmdlen); 779 780 return error; 781 } 782 783 static int 784 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 785 { 786 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 787 788 if (sc == NULL) 789 return ENXIO; 790 791 switch (cmd) { 792 case I2C_IOCTL_EXEC: 793 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 794 default: 795 return ENODEV; 796 } 797 } 798 799 800 CFATTACH_DECL3_NEW(iic, sizeof(struct iic_softc), 801 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach, 802 DVF_DETACH_SHUTDOWN); 803 804 MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec,i2c_bitbang,i2c_subr"); 805 806 #ifdef _MODULE 807 #include "ioconf.c" 808 #endif 809 810 int 811 iic_init(void) 812 { 813 814 mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE); 815 iic_refcnt = 0; 816 return 0; 817 } 818 819 static int 820 iic_modcmd(modcmd_t cmd, void *opaque) 821 { 822 #ifdef _MODULE 823 int bmajor, cmajor; 824 #endif 825 int error; 826 827 error = 0; 828 switch (cmd) { 829 case MODULE_CMD_INIT: 830 RUN_ONCE(&iic_once, iic_init); 831 832 #ifdef _MODULE 833 mutex_enter(&iic_mtx); 834 bmajor = cmajor = -1; 835 error = devsw_attach("iic", NULL, &bmajor, 836 &iic_cdevsw, &cmajor); 837 if (error != 0) { 838 mutex_exit(&iic_mtx); 839 break; 840 } 841 error = config_init_component(cfdriver_ioconf_iic, 842 cfattach_ioconf_iic, cfdata_ioconf_iic); 843 if (error) { 844 aprint_error("%s: unable to init component\n", 845 iic_cd.cd_name); 846 devsw_detach(NULL, &iic_cdevsw); 847 } 848 mutex_exit(&iic_mtx); 849 #endif 850 break; 851 case MODULE_CMD_FINI: 852 mutex_enter(&iic_mtx); 853 if (iic_refcnt != 0) { 854 mutex_exit(&iic_mtx); 855 return EBUSY; 856 } 857 #ifdef _MODULE 858 error = config_fini_component(cfdriver_ioconf_iic, 859 cfattach_ioconf_iic, cfdata_ioconf_iic); 860 if (error != 0) { 861 mutex_exit(&iic_mtx); 862 break; 863 } 864 devsw_detach(NULL, &iic_cdevsw); 865 #endif 866 mutex_exit(&iic_mtx); 867 break; 868 default: 869 error = ENOTTY; 870 } 871 return error; 872 } 873