1 /* $NetBSD: i2c.c,v 1.92 2025/01/27 17:01:53 riastradh 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.92 2025/01/27 17:01:53 riastradh 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_fill_compat(struct i2c_attach_args*, const char*, 123 size_t, char **); 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_ncompat ? " (" : "", 134 ia->ia_ncompat ? ia->ia_compat[0] : "", 135 ia->ia_ncompat ? ")" : "", 136 pnp, ia->ia_addr); 137 else 138 aprint_normal(" addr 0x%02x", ia->ia_addr); 139 140 return UNCONF; 141 } 142 143 static int 144 iic_print(void *aux, const char *pnp) 145 { 146 struct i2c_attach_args *ia = aux; 147 148 if (ia->ia_addr != (i2c_addr_t)IICCF_ADDR_DEFAULT) 149 aprint_normal(" addr 0x%x", ia->ia_addr); 150 151 return UNCONF; 152 } 153 154 static bool 155 iic_is_special_address(i2c_addr_t addr) 156 { 157 158 /* 159 * See: https://www.i2c-bus.org/addressing/ 160 */ 161 162 /* General Call (read) / Start Byte (write) */ 163 if (addr == 0x00) 164 return (true); 165 166 /* CBUS Addresses */ 167 if (addr == 0x01) 168 return (true); 169 170 /* Reserved for Different Bus Formats */ 171 if (addr == 0x02) 172 return (true); 173 174 /* Reserved for future purposes */ 175 if (addr == 0x03) 176 return (true); 177 178 /* High Speed Master Code */ 179 if ((addr & 0x7c) == 0x04) 180 return (true); 181 182 /* 10-bit Slave Addressing prefix */ 183 if ((addr & 0x7c) == 0x78) 184 return (true); 185 186 /* Reserved for future purposes */ 187 if ((addr & 0x7c) == 0x7c) 188 return (true); 189 190 return (false); 191 } 192 193 static int 194 iic_probe_none(struct iic_softc *sc, 195 const struct i2c_attach_args *ia, int flags) 196 { 197 198 return (0); 199 } 200 201 static int 202 iic_probe_smbus_quick_write(struct iic_softc *sc, 203 const struct i2c_attach_args *ia, int flags) 204 { 205 int error; 206 207 if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) { 208 error = iic_smbus_quick_write(ia->ia_tag, ia->ia_addr, flags); 209 } 210 (void) iic_release_bus(ia->ia_tag, flags); 211 212 return (error); 213 } 214 215 static int 216 iic_probe_smbus_receive_byte(struct iic_softc *sc, 217 const struct i2c_attach_args *ia, int flags) 218 { 219 int error; 220 221 if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) { 222 uint8_t dummy; 223 224 error = iic_smbus_receive_byte(ia->ia_tag, ia->ia_addr, 225 &dummy, flags); 226 } 227 (void) iic_release_bus(ia->ia_tag, flags); 228 229 return (error); 230 } 231 232 static bool 233 iic_indirect_driver_is_permitted(struct iic_softc *sc, cfdata_t cf) 234 { 235 prop_object_iterator_t iter; 236 prop_array_t permitlist; 237 prop_string_t pstr; 238 prop_type_t ptype; 239 bool rv = false; 240 241 permitlist = prop_dictionary_get(device_properties(sc->sc_dev), 242 I2C_PROP_INDIRECT_DEVICE_PERMITLIST); 243 if (permitlist == NULL) { 244 /* No permitlist -> everything allowed */ 245 return (true); 246 } 247 248 if ((ptype = prop_object_type(permitlist)) != PROP_TYPE_ARRAY) { 249 aprint_error_dev(sc->sc_dev, 250 "invalid property type (%d) for '%s'; must be array (%d)\n", 251 ptype, I2C_PROP_INDIRECT_DEVICE_PERMITLIST, 252 PROP_TYPE_ARRAY); 253 return (false); 254 } 255 256 iter = prop_array_iterator(permitlist); 257 while ((pstr = prop_object_iterator_next(iter)) != NULL) { 258 if (prop_string_equals_string(pstr, cf->cf_name)) { 259 rv = true; 260 break; 261 } 262 } 263 prop_object_iterator_release(iter); 264 265 return (rv); 266 } 267 268 static int 269 iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 270 { 271 struct iic_softc *sc = device_private(parent); 272 struct i2c_attach_args ia; 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 ia.ia_tag = sc->sc_tag; 311 312 ia.ia_name = NULL; 313 ia.ia_ncompat = 0; 314 ia.ia_compat = NULL; 315 ia.ia_prop = NULL; 316 317 if (cf->cf_loc[IICCF_ADDR] == IICCF_ADDR_DEFAULT) { 318 /* 319 * This particular config directive has 320 * wildcarded the address, so we will 321 * scan the entire bus for it. 322 */ 323 first_addr = 0; 324 last_addr = I2C_MAX_ADDR; 325 } else { 326 /* 327 * This config directive hard-wires the i2c 328 * bus address for the device, so there is 329 * no need to go poking around at any other 330 * addresses. 331 */ 332 if (cf->cf_loc[IICCF_ADDR] < 0 || 333 cf->cf_loc[IICCF_ADDR] > I2C_MAX_ADDR) { 334 /* Invalid config directive! */ 335 return (0); 336 } 337 first_addr = last_addr = cf->cf_loc[IICCF_ADDR]; 338 } 339 340 for (ia.ia_addr = first_addr; ia.ia_addr <= last_addr; ia.ia_addr++) { 341 int error, match_result; 342 343 /* 344 * Skip I2C addresses that are reserved for 345 * special purposes. 346 */ 347 if (iic_is_special_address(ia.ia_addr)) 348 continue; 349 350 /* 351 * Skip addresses where a device is already attached. 352 */ 353 if (sc->sc_devices[ia.ia_addr] != NULL) 354 continue; 355 356 /* 357 * Call the "match" routine for the device. If that 358 * returns success, then call the probe strategy 359 * function. 360 * 361 * We do it in this order because i2c devices tend 362 * to be found at a small number of possible addresses 363 * (e.g. read-time clocks that are only ever found at 364 * 0x68). This gives the driver a chance to skip any 365 * address that are not valid for the device, saving 366 * us from having to poke at the bus to see if anything 367 * is there. 368 */ 369 match_result = config_probe(parent, cf, &ia);/*XXX*/ 370 if (match_result <= 0) 371 continue; 372 373 /* 374 * If the quality of the match by the driver was low 375 * (i.e. matched on being a valid address only, didn't 376 * perform any hardware probe), invoke our probe routine 377 * to see if it looks like something is really there. 378 */ 379 if (match_result == I2C_MATCH_ADDRESS_ONLY && 380 (error = (*probe_func)(sc, &ia, 0)) != 0) 381 continue; 382 383 sc->sc_devices[ia.ia_addr] = 384 config_attach(parent, cf, &ia, iic_print, CFARGS_NONE); 385 } 386 387 return 0; 388 } 389 390 static void 391 iic_child_detach(device_t parent, device_t child) 392 { 393 struct iic_softc *sc = device_private(parent); 394 int i; 395 396 for (i = 0; i <= I2C_MAX_ADDR; i++) 397 if (sc->sc_devices[i] == child) { 398 sc->sc_devices[i] = NULL; 399 break; 400 } 401 } 402 403 static int 404 iic_rescan(device_t self, const char *ifattr, const int *locators) 405 { 406 config_search(self, NULL, 407 CFARGS(.search = iic_search, 408 .locators = locators)); 409 return 0; 410 } 411 412 static int 413 iic_match(device_t parent, cfdata_t cf, void *aux) 414 { 415 416 return 1; 417 } 418 419 static void 420 iic_attach(device_t parent, device_t self, void *aux) 421 { 422 struct iic_softc *sc = device_private(self); 423 struct i2cbus_attach_args *iba = aux; 424 prop_array_t child_devices; 425 prop_dictionary_t props; 426 char *buf; 427 i2c_tag_t ic; 428 bool no_indirect_config = false; 429 430 aprint_naive("\n"); 431 aprint_normal(": I2C bus\n"); 432 433 sc->sc_dev = self; 434 sc->sc_tag = iba->iba_tag; 435 ic = sc->sc_tag; 436 437 if (!pmf_device_register(self, NULL, NULL)) 438 aprint_error_dev(self, "couldn't establish power handler\n"); 439 440 if (iba->iba_child_devices) { 441 child_devices = iba->iba_child_devices; 442 no_indirect_config = true; 443 } else { 444 props = device_properties(parent); 445 prop_dictionary_get_bool(props, "i2c-no-indirect-config", 446 &no_indirect_config); 447 child_devices = prop_dictionary_get(props, "i2c-child-devices"); 448 } 449 450 if (child_devices) { 451 unsigned int i, count; 452 prop_dictionary_t dev; 453 prop_data_t cdata; 454 uint32_t addr; 455 uint64_t cookie; 456 uint32_t cookietype; 457 const char *name; 458 struct i2c_attach_args ia; 459 devhandle_t devhandle; 460 int loc[IICCF_NLOCS]; 461 462 memset(loc, 0, sizeof loc); 463 count = prop_array_count(child_devices); 464 for (i = 0; i < count; i++) { 465 dev = prop_array_get(child_devices, i); 466 if (!dev) continue; 467 if (!prop_dictionary_get_string( 468 dev, "name", &name)) { 469 /* "name" property is optional. */ 470 name = NULL; 471 } 472 if (!prop_dictionary_get_uint32(dev, "addr", &addr)) 473 continue; 474 if (!prop_dictionary_get_uint64(dev, "cookie", &cookie)) 475 cookie = 0; 476 if (!prop_dictionary_get_uint32(dev, "cookietype", 477 &cookietype)) 478 cookietype = I2C_COOKIE_NONE; 479 loc[IICCF_ADDR] = addr; 480 481 memset(&ia, 0, sizeof ia); 482 ia.ia_addr = addr; 483 ia.ia_tag = ic; 484 ia.ia_name = name; 485 ia.ia_cookie = cookie; 486 ia.ia_cookietype = cookietype; 487 ia.ia_prop = dev; 488 489 devhandle = devhandle_invalid(); 490 #ifdef I2C_USE_FDT 491 if (cookietype == I2C_COOKIE_OF) { 492 devhandle = devhandle_from_of(devhandle, 493 (int)cookie); 494 } 495 #endif /* I2C_USE_FDT */ 496 #ifdef I2C_USE_ACPI 497 if (cookietype == I2C_COOKIE_ACPI) { 498 devhandle = 499 devhandle_from_acpi(devhandle, 500 (ACPI_HANDLE)cookie); 501 } 502 #endif /* I2C_USE_ACPI */ 503 504 buf = NULL; 505 cdata = prop_dictionary_get(dev, "compatible"); 506 if (cdata) 507 iic_fill_compat(&ia, 508 prop_data_value(cdata), 509 prop_data_size(cdata), &buf); 510 511 if (name == NULL && cdata == NULL) { 512 aprint_error_dev(self, 513 "WARNING: ignoring bad child device entry " 514 "for address 0x%02x\n", addr); 515 } else { 516 if (addr > I2C_MAX_ADDR) { 517 aprint_error_dev(self, 518 "WARNING: ignoring bad device " 519 "address @ 0x%02x\n", addr); 520 } else if (sc->sc_devices[addr] == NULL) { 521 sc->sc_devices[addr] = 522 config_found(self, &ia, 523 iic_print_direct, 524 CFARGS(.locators = loc, 525 .devhandle = devhandle)); 526 } 527 } 528 529 if (ia.ia_compat) 530 free(ia.ia_compat, M_TEMP); 531 if (buf) 532 free(buf, M_TEMP); 533 } 534 } else if (!no_indirect_config) { 535 /* 536 * Attach all i2c devices described in the kernel 537 * configuration file. 538 */ 539 iic_rescan(self, "iic", NULL); 540 } 541 } 542 543 static int 544 iic_detach(device_t self, int flags) 545 { 546 int error; 547 548 error = config_detach_children(self, flags); 549 if (error) 550 return error; 551 552 pmf_device_deregister(self); 553 554 return 0; 555 } 556 557 static void 558 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len, 559 char **buffer) 560 { 561 int count, i; 562 const char *c, *start, **ptr; 563 564 *buffer = NULL; 565 for (i = count = 0, c = compat; i < len; i++, c++) 566 if (*c == 0) 567 count++; 568 count += 2; 569 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK); 570 if (!ptr) return; 571 572 for (i = count = 0, start = c = compat; i < len; i++, c++) { 573 if (*c == 0) { 574 ptr[count++] = start; 575 start = c+1; 576 } 577 } 578 if (start < compat+len) { 579 /* last string not 0 terminated */ 580 size_t l = c-start; 581 *buffer = malloc(l+1, M_TEMP, M_WAITOK); 582 memcpy(*buffer, start, l); 583 (*buffer)[l] = 0; 584 ptr[count++] = *buffer; 585 } 586 ptr[count] = NULL; 587 588 ia->ia_compat = ptr; 589 ia->ia_ncompat = count; 590 } 591 592 /* 593 * iic_compatible_match -- 594 * Match a device's "compatible" property against the list 595 * of compatible strings provided by the driver. 596 */ 597 int 598 iic_compatible_match(const struct i2c_attach_args *ia, 599 const struct device_compatible_entry *compats) 600 { 601 int match_result; 602 603 match_result = device_compatible_match(ia->ia_compat, ia->ia_ncompat, 604 compats); 605 if (match_result) { 606 match_result = 607 MIN(I2C_MATCH_DIRECT_COMPATIBLE + match_result - 1, 608 I2C_MATCH_DIRECT_COMPATIBLE_MAX); 609 } 610 611 return match_result; 612 } 613 614 /* 615 * iic_compatible_lookup -- 616 * Look the compatible entry that matches one of the driver's 617 * "compatible" strings. The first match is returned. 618 */ 619 const struct device_compatible_entry * 620 iic_compatible_lookup(const struct i2c_attach_args *ia, 621 const struct device_compatible_entry *compats) 622 { 623 return device_compatible_lookup(ia->ia_compat, ia->ia_ncompat, 624 compats); 625 } 626 627 /* 628 * iic_use_direct_match -- 629 * Helper for direct-config of i2c. Returns true if this is 630 * a direct-config situation, along with match result. 631 * Returns false if the driver should use indirect-config 632 * matching logic. 633 */ 634 bool 635 iic_use_direct_match(const struct i2c_attach_args *ia, const cfdata_t cf, 636 const struct device_compatible_entry *compats, 637 int *match_resultp) 638 { 639 KASSERT(match_resultp != NULL); 640 641 if (ia->ia_name != NULL && 642 strcmp(ia->ia_name, cf->cf_name) == 0) { 643 *match_resultp = I2C_MATCH_DIRECT_SPECIFIC; 644 return true; 645 } 646 647 if (ia->ia_ncompat > 0 && ia->ia_compat != NULL) { 648 *match_resultp = iic_compatible_match(ia, compats); 649 return true; 650 } 651 652 return false; 653 } 654 655 static int 656 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 657 { 658 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 659 660 mutex_enter(&iic_mtx); 661 if (sc == NULL) { 662 mutex_exit(&iic_mtx); 663 return ENXIO; 664 } 665 iic_refcnt++; 666 mutex_exit(&iic_mtx); 667 668 return 0; 669 } 670 671 static int 672 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 673 { 674 675 mutex_enter(&iic_mtx); 676 iic_refcnt--; 677 mutex_exit(&iic_mtx); 678 679 return 0; 680 } 681 682 static int 683 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 684 { 685 i2c_tag_t ic = sc->sc_tag; 686 uint8_t *buf = NULL; 687 void *cmd = NULL; 688 int error = 0; 689 690 /* Validate parameters */ 691 if (iie->iie_addr > I2C_MAX_ADDR) 692 return EINVAL; 693 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 694 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 695 return EINVAL; 696 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 697 return EINVAL; 698 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 699 return EINVAL; 700 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 701 return EBADF; 702 703 #if 0 704 /* Disallow userspace access to devices that have drivers attached. */ 705 if (sc->sc_devices[iie->iie_addr] != NULL) 706 return EBUSY; 707 #endif 708 709 if (iie->iie_cmd != NULL) { 710 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 711 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 712 if (error) 713 goto out; 714 } 715 716 if (iie->iie_buf != NULL) { 717 buf = kmem_alloc(iie->iie_buflen, KM_SLEEP); 718 if (I2C_OP_WRITE_P(iie->iie_op)) { 719 error = copyin(iie->iie_buf, buf, iie->iie_buflen); 720 if (error) 721 goto out; 722 } 723 } 724 725 iic_acquire_bus(ic, 0); 726 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 727 buf, iie->iie_buflen, 0); 728 iic_release_bus(ic, 0); 729 730 /* 731 * Some drivers return error codes on failure, and others return -1. 732 */ 733 if (error < 0) 734 error = EIO; 735 736 out: 737 if (!error && iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op)) 738 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 739 740 if (buf) 741 kmem_free(buf, iie->iie_buflen); 742 743 if (cmd) 744 kmem_free(cmd, iie->iie_cmdlen); 745 746 return error; 747 } 748 749 static int 750 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 751 { 752 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 753 754 if (sc == NULL) 755 return ENXIO; 756 757 switch (cmd) { 758 case I2C_IOCTL_EXEC: 759 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 760 default: 761 return ENODEV; 762 } 763 } 764 765 766 CFATTACH_DECL3_NEW(iic, sizeof(struct iic_softc), 767 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach, 768 DVF_DETACH_SHUTDOWN); 769 770 MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec,i2c_bitbang,i2c_subr"); 771 772 #ifdef _MODULE 773 #include "ioconf.c" 774 #endif 775 776 int 777 iic_init(void) 778 { 779 780 mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE); 781 iic_refcnt = 0; 782 return 0; 783 } 784 785 static int 786 iic_modcmd(modcmd_t cmd, void *opaque) 787 { 788 #ifdef _MODULE 789 int bmajor, cmajor; 790 #endif 791 int error; 792 793 error = 0; 794 switch (cmd) { 795 case MODULE_CMD_INIT: 796 RUN_ONCE(&iic_once, iic_init); 797 798 #ifdef _MODULE 799 mutex_enter(&iic_mtx); 800 bmajor = cmajor = -1; 801 error = devsw_attach("iic", NULL, &bmajor, 802 &iic_cdevsw, &cmajor); 803 if (error != 0) { 804 mutex_exit(&iic_mtx); 805 break; 806 } 807 error = config_init_component(cfdriver_ioconf_iic, 808 cfattach_ioconf_iic, cfdata_ioconf_iic); 809 if (error) { 810 aprint_error("%s: unable to init component\n", 811 iic_cd.cd_name); 812 devsw_detach(NULL, &iic_cdevsw); 813 } 814 mutex_exit(&iic_mtx); 815 #endif 816 break; 817 case MODULE_CMD_FINI: 818 mutex_enter(&iic_mtx); 819 if (iic_refcnt != 0) { 820 mutex_exit(&iic_mtx); 821 return EBUSY; 822 } 823 #ifdef _MODULE 824 error = config_fini_component(cfdriver_ioconf_iic, 825 cfattach_ioconf_iic, cfdata_ioconf_iic); 826 if (error != 0) { 827 mutex_exit(&iic_mtx); 828 break; 829 } 830 devsw_detach(NULL, &iic_cdevsw); 831 #endif 832 mutex_exit(&iic_mtx); 833 break; 834 default: 835 error = ENOTTY; 836 } 837 return error; 838 } 839