1 /* $NetBSD: i2c.c,v 1.39 2013/02/03 16:28:51 jdc 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 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.39 2013/02/03 16:28:51 jdc Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/device.h> 44 #include <sys/event.h> 45 #include <sys/conf.h> 46 #include <sys/malloc.h> 47 #include <sys/kmem.h> 48 #include <sys/kthread.h> 49 #include <sys/proc.h> 50 #include <sys/kernel.h> 51 #include <sys/fcntl.h> 52 #include <sys/module.h> 53 54 #include <dev/i2c/i2cvar.h> 55 56 #include "locators.h" 57 58 #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */ 59 60 struct iic_softc { 61 i2c_tag_t sc_tag; 62 int sc_type; 63 device_t sc_devices[I2C_MAX_ADDR + 1]; 64 }; 65 66 static dev_type_open(iic_open); 67 static dev_type_close(iic_close); 68 static dev_type_ioctl(iic_ioctl); 69 70 const struct cdevsw iic_cdevsw = { 71 iic_open, iic_close, noread, nowrite, iic_ioctl, 72 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER 73 }; 74 75 extern struct cfdriver iic_cd; 76 77 static void iic_smbus_intr_thread(void *); 78 static void iic_fill_compat(struct i2c_attach_args*, const char*, 79 size_t, char **); 80 81 static int 82 iic_print_direct(void *aux, const char *pnp) 83 { 84 struct i2c_attach_args *ia = aux; 85 86 if (pnp != NULL) 87 aprint_normal("%s at %s addr 0x%02x", ia->ia_name, pnp, 88 ia->ia_addr); 89 else 90 aprint_normal(" addr 0x%02x", ia->ia_addr); 91 92 return UNCONF; 93 } 94 95 static int 96 iic_print(void *aux, const char *pnp) 97 { 98 struct i2c_attach_args *ia = aux; 99 100 if (ia->ia_addr != (i2c_addr_t)-1) 101 aprint_normal(" addr 0x%x", ia->ia_addr); 102 103 return UNCONF; 104 } 105 106 static int 107 iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 108 { 109 struct iic_softc *sc = device_private(parent); 110 struct i2c_attach_args ia; 111 112 ia.ia_tag = sc->sc_tag; 113 ia.ia_addr = cf->cf_loc[IICCF_ADDR]; 114 ia.ia_size = cf->cf_loc[IICCF_SIZE]; 115 ia.ia_type = sc->sc_type; 116 117 ia.ia_name = NULL; 118 ia.ia_ncompat = 0; 119 ia.ia_compat = NULL; 120 121 if (ia.ia_addr != (i2c_addr_t)-1 && 122 ia.ia_addr <= I2C_MAX_ADDR && 123 !sc->sc_devices[ia.ia_addr]) 124 if (config_match(parent, cf, &ia) > 0) { 125 sc->sc_devices[ia.ia_addr] = 126 config_attach(parent, cf, &ia, iic_print); 127 } 128 return 0; 129 } 130 131 static void 132 iic_child_detach(device_t parent, device_t child) 133 { 134 struct iic_softc *sc = device_private(parent); 135 int i; 136 137 for (i = 0; i <= I2C_MAX_ADDR; i++) 138 if (sc->sc_devices[i] == child) { 139 sc->sc_devices[i] = NULL; 140 break; 141 } 142 } 143 144 static int 145 iic_rescan(device_t self, const char *ifattr, const int *locators) 146 { 147 config_search_ia(iic_search, self, ifattr, NULL); 148 return 0; 149 } 150 151 static int 152 iic_match(device_t parent, cfdata_t cf, void *aux) 153 { 154 155 return 1; 156 } 157 158 static void 159 iic_attach(device_t parent, device_t self, void *aux) 160 { 161 struct iic_softc *sc = device_private(self); 162 struct i2cbus_attach_args *iba = aux; 163 prop_array_t child_devices; 164 char *buf; 165 i2c_tag_t ic; 166 int rv; 167 168 aprint_naive("\n"); 169 aprint_normal(": I2C bus\n"); 170 171 sc->sc_tag = iba->iba_tag; 172 sc->sc_type = iba->iba_type; 173 ic = sc->sc_tag; 174 ic->ic_devname = device_xname(self); 175 176 LIST_INIT(&(sc->sc_tag->ic_list)); 177 LIST_INIT(&(sc->sc_tag->ic_proc_list)); 178 179 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 180 iic_smbus_intr_thread, ic, &ic->ic_intr_thread, 181 "%s", ic->ic_devname); 182 if (rv) 183 aprint_error_dev(self, "unable to create intr thread\n"); 184 185 if (!pmf_device_register(self, NULL, NULL)) 186 aprint_error_dev(self, "couldn't establish power handler\n"); 187 188 child_devices = prop_dictionary_get(device_properties(parent), 189 "i2c-child-devices"); 190 if (child_devices) { 191 unsigned int i, count; 192 prop_dictionary_t dev; 193 prop_data_t cdata; 194 uint32_t addr, size; 195 uint64_t cookie; 196 const char *name; 197 struct i2c_attach_args ia; 198 int loc[2]; 199 200 memset(loc, 0, sizeof loc); 201 count = prop_array_count(child_devices); 202 for (i = 0; i < count; i++) { 203 dev = prop_array_get(child_devices, i); 204 if (!dev) continue; 205 if (!prop_dictionary_get_cstring_nocopy( 206 dev, "name", &name)) 207 continue; 208 if (!prop_dictionary_get_uint32(dev, "addr", &addr)) 209 continue; 210 if (!prop_dictionary_get_uint64(dev, "cookie", &cookie)) 211 cookie = 0; 212 loc[0] = addr; 213 if (prop_dictionary_get_uint32(dev, "size", &size)) 214 loc[1] = size; 215 else 216 loc[1] = -1; 217 218 memset(&ia, 0, sizeof ia); 219 ia.ia_addr = addr; 220 ia.ia_type = sc->sc_type; 221 ia.ia_tag = ic; 222 ia.ia_name = name; 223 ia.ia_cookie = cookie; 224 ia.ia_size = size; 225 226 buf = NULL; 227 cdata = prop_dictionary_get(dev, "compatible"); 228 if (cdata) 229 iic_fill_compat(&ia, 230 prop_data_data_nocopy(cdata), 231 prop_data_size(cdata), &buf); 232 233 if (addr > I2C_MAX_ADDR) { 234 aprint_error_dev(self, 235 "WARNING: ignoring bad device address " 236 "@ 0x%02x\n", addr); 237 } else if (sc->sc_devices[addr] == NULL) { 238 sc->sc_devices[addr] = 239 config_found_sm_loc(self, "iic", loc, &ia, 240 iic_print_direct, NULL); 241 } 242 243 if (ia.ia_compat) 244 free(ia.ia_compat, M_TEMP); 245 if (buf) 246 free(buf, M_TEMP); 247 } 248 } else { 249 /* 250 * Attach all i2c devices described in the kernel 251 * configuration file. 252 */ 253 iic_rescan(self, "iic", NULL); 254 } 255 } 256 257 static int 258 iic_detach(device_t self, int flags) 259 { 260 struct iic_softc *sc = device_private(self); 261 i2c_tag_t ic = sc->sc_tag; 262 int i, error; 263 void *hdl; 264 265 for (i = 0; i <= I2C_MAX_ADDR; i++) { 266 if (sc->sc_devices[i]) { 267 error = config_detach(sc->sc_devices[i], flags); 268 if (error) 269 return error; 270 } 271 } 272 273 if (ic->ic_running) { 274 ic->ic_running = 0; 275 wakeup(ic); 276 kthread_join(ic->ic_intr_thread); 277 } 278 279 if (!LIST_EMPTY(&ic->ic_list)) { 280 device_printf(self, "WARNING: intr handler list not empty\n"); 281 while (!LIST_EMPTY(&ic->ic_list)) { 282 hdl = LIST_FIRST(&ic->ic_list); 283 iic_smbus_intr_disestablish(ic, hdl); 284 } 285 } 286 if (!LIST_EMPTY(&ic->ic_proc_list)) { 287 device_printf(self, "WARNING: proc handler list not empty\n"); 288 while (!LIST_EMPTY(&ic->ic_proc_list)) { 289 hdl = LIST_FIRST(&ic->ic_proc_list); 290 iic_smbus_intr_disestablish_proc(ic, hdl); 291 } 292 } 293 294 pmf_device_deregister(self); 295 296 return 0; 297 } 298 299 static void 300 iic_smbus_intr_thread(void *aux) 301 { 302 i2c_tag_t ic; 303 struct ic_intr_list *il; 304 int rv; 305 306 ic = (i2c_tag_t)aux; 307 ic->ic_running = 1; 308 ic->ic_pending = 0; 309 310 while (ic->ic_running) { 311 if (ic->ic_pending == 0) 312 rv = tsleep(ic, PZERO, "iicintr", hz); 313 if (ic->ic_pending > 0) { 314 LIST_FOREACH(il, &(ic->ic_proc_list), il_next) { 315 (*il->il_intr)(il->il_intrarg); 316 } 317 ic->ic_pending--; 318 } 319 } 320 321 kthread_exit(0); 322 } 323 324 void * 325 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 326 { 327 struct ic_intr_list *il; 328 329 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 330 if (il == NULL) 331 return NULL; 332 333 il->il_intr = intr; 334 il->il_intrarg = intrarg; 335 336 LIST_INSERT_HEAD(&(ic->ic_list), il, il_next); 337 338 return il; 339 } 340 341 void 342 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl) 343 { 344 struct ic_intr_list *il; 345 346 il = (struct ic_intr_list *)hdl; 347 348 LIST_REMOVE(il, il_next); 349 free(il, M_DEVBUF); 350 351 return; 352 } 353 354 void * 355 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 356 { 357 struct ic_intr_list *il; 358 359 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 360 if (il == NULL) 361 return NULL; 362 363 il->il_intr = intr; 364 il->il_intrarg = intrarg; 365 366 LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next); 367 368 return il; 369 } 370 371 void 372 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl) 373 { 374 struct ic_intr_list *il; 375 376 il = (struct ic_intr_list *)hdl; 377 378 LIST_REMOVE(il, il_next); 379 free(il, M_DEVBUF); 380 381 return; 382 } 383 384 int 385 iic_smbus_intr(i2c_tag_t ic) 386 { 387 struct ic_intr_list *il; 388 389 LIST_FOREACH(il, &(ic->ic_list), il_next) { 390 (*il->il_intr)(il->il_intrarg); 391 } 392 393 ic->ic_pending++; 394 wakeup(ic); 395 396 return 1; 397 } 398 399 static void 400 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len, 401 char **buffer) 402 { 403 int count, i; 404 const char *c, *start, **ptr; 405 406 *buffer = NULL; 407 for (i = count = 0, c = compat; i < len; i++, c++) 408 if (*c == 0) 409 count++; 410 count += 2; 411 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK); 412 if (!ptr) return; 413 414 for (i = count = 0, start = c = compat; i < len; i++, c++) { 415 if (*c == 0) { 416 ptr[count++] = start; 417 start = c+1; 418 } 419 } 420 if (start < compat+len) { 421 /* last string not 0 terminated */ 422 size_t l = c-start; 423 *buffer = malloc(l+1, M_TEMP, M_WAITOK); 424 memcpy(*buffer, start, l); 425 (*buffer)[l] = 0; 426 ptr[count++] = *buffer; 427 } 428 ptr[count] = NULL; 429 430 ia->ia_compat = ptr; 431 ia->ia_ncompat = count; 432 } 433 434 int 435 iic_compat_match(struct i2c_attach_args *ia, const char ** compats) 436 { 437 int i; 438 439 for (; compats && *compats; compats++) { 440 for (i = 0; i < ia->ia_ncompat; i++) { 441 if (strcmp(*compats, ia->ia_compat[i]) == 0) 442 return 1; 443 } 444 } 445 return 0; 446 } 447 448 static int 449 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 450 { 451 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 452 453 if (sc == NULL) 454 return ENXIO; 455 456 return 0; 457 } 458 459 static int 460 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 461 { 462 return 0; 463 } 464 465 static int 466 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 467 { 468 i2c_tag_t ic = sc->sc_tag; 469 uint8_t buf[I2C_EXEC_MAX_BUFLEN]; 470 void *cmd = NULL; 471 int error; 472 473 /* Validate parameters */ 474 if (iie->iie_addr > I2C_MAX_ADDR) 475 return EINVAL; 476 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 477 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 478 return EINVAL; 479 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 480 return EINVAL; 481 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 482 return EINVAL; 483 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 484 return EBADF; 485 486 #if 0 487 /* Disallow userspace access to devices that have drivers attached. */ 488 if (sc->sc_devices[iie->iie_addr] != NULL) 489 return EBUSY; 490 #endif 491 492 if (iie->iie_cmd != NULL) { 493 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 494 if (cmd == NULL) 495 return ENOMEM; 496 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 497 if (error) { 498 kmem_free(cmd, iie->iie_cmdlen); 499 return error; 500 } 501 } 502 503 iic_acquire_bus(ic, 0); 504 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 505 buf, iie->iie_buflen, 0); 506 iic_release_bus(ic, 0); 507 508 /* 509 * Some drivers return error codes on failure, and others return -1. 510 */ 511 if (error < 0) 512 error = EIO; 513 514 if (cmd) 515 kmem_free(cmd, iie->iie_cmdlen); 516 517 if (error) 518 return error; 519 520 if (iie->iie_buf) 521 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 522 523 return error; 524 } 525 526 static int 527 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 528 { 529 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 530 531 if (sc == NULL) 532 return ENXIO; 533 534 switch (cmd) { 535 case I2C_IOCTL_EXEC: 536 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 537 default: 538 return ENODEV; 539 } 540 } 541 542 543 CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc), 544 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach); 545 546 MODULE(MODULE_CLASS_DRIVER, iic, NULL); 547 548 #ifdef _MODULE 549 #include "ioconf.c" 550 #endif 551 552 static int 553 iic_modcmd(modcmd_t cmd, void *opaque) 554 { 555 int error; 556 557 error = 0; 558 switch (cmd) { 559 case MODULE_CMD_INIT: 560 #ifdef _MODULE 561 error = config_init_component(cfdriver_ioconf_iic, 562 cfattach_ioconf_iic, cfdata_ioconf_iic); 563 if (error) 564 aprint_error("%s: unable to init component\n", 565 iic_cd.cd_name); 566 #endif 567 break; 568 case MODULE_CMD_FINI: 569 #ifdef _MODULE 570 config_fini_component(cfdriver_ioconf_iic, 571 cfattach_ioconf_iic, cfdata_ioconf_iic); 572 #endif 573 break; 574 default: 575 error = ENOTTY; 576 } 577 return error; 578 } 579