xref: /freebsd/sys/dev/usb/misc/cp2112.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) Andriy Gapon
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * Hardware information links:
31  * - CP2112 Datasheet
32  *   https://www.silabs.com/documents/public/data-sheets/cp2112-datasheet.pdf
33  * - AN495: CP2112 Interface Specification
34  *   https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
35  * - CP2112 Errata
36  *   https://www.silabs.com/documents/public/errata/cp2112-errata.pdf
37  */
38 
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/condvar.h>
43 #include <sys/bus.h>
44 #include <sys/gpio.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/sdt.h>
50 #include <sys/sx.h>
51 
52 #include <dev/gpio/gpiobusvar.h>
53 
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
56 #include "iicbus_if.h"
57 
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbhid.h>
62 #include "usbdevs.h"
63 
64 #define	USB_DEBUG_VAR usb_debug
65 #include <dev/usb/usb_debug.h>
66 
67 #define	SIZEOF_FIELD(_s, _f)	sizeof(((struct _s *)NULL)->_f)
68 
69 #define	CP2112GPIO_LOCK(sc)	sx_xlock(&sc->gpio_lock)
70 #define	CP2112GPIO_UNLOCK(sc)	sx_xunlock(&sc->gpio_lock)
71 #define	CP2112GPIO_LOCKED(sc)	sx_assert(&sc->gpio_lock, SX_XLOCKED)
72 
73 #define	CP2112_PART_NUM			0x0c
74 #define	CP2112_GPIO_COUNT		8
75 #define	CP2112_REPORT_SIZE		64
76 
77 #define	CP2112_REQ_RESET		0x1
78 #define	CP2112_REQ_GPIO_CFG		0x2
79 #define	CP2112_REQ_GPIO_GET		0x3
80 #define	CP2112_REQ_GPIO_SET		0x4
81 #define	CP2112_REQ_VERSION		0x5
82 #define	CP2112_REQ_SMB_CFG		0x6
83 
84 #define	CP2112_REQ_SMB_READ		0x10
85 #define	CP2112_REQ_SMB_WRITE_READ	0x11
86 #define	CP2112_REQ_SMB_READ_FORCE_SEND	0x12
87 #define	CP2112_REQ_SMB_READ_RESPONSE	0x13
88 #define	CP2112_REQ_SMB_WRITE		0x14
89 #define	CP2112_REQ_SMB_XFER_STATUS_REQ	0x15
90 #define	CP2112_REQ_SMB_XFER_STATUS_RESP	0x16
91 #define	CP2112_REQ_SMB_CANCEL		0x17
92 
93 #define	CP2112_REQ_LOCK			0x20
94 #define	CP2112_REQ_USB_CFG		0x21
95 
96 #define	CP2112_IIC_MAX_READ_LEN		512
97 #define	CP2112_IIC_REPSTART_VER		2	/* Erratum CP2112_E10. */
98 
99 #define	CP2112_GPIO_SPEC_CLK7		1	/* Pin 7 is clock output. */
100 #define	CP2112_GPIO_SPEC_TX0		2	/* Pin 0 pulses on USB TX. */
101 #define	CP2112_GPIO_SPEC_RX1		4	/* Pin 1 pulses on USB RX. */
102 
103 #define	CP2112_IIC_STATUS0_IDLE		0
104 #define	CP2112_IIC_STATUS0_BUSY		1
105 #define	CP2112_IIC_STATUS0_CMP		2
106 #define	CP2112_IIC_STATUS0_ERROR	3
107 
108 #define	CP2112_IIC_STATUS1_TIMEOUT_NACK	0
109 #define	CP2112_IIC_STATUS1_TIMEOUT_BUS	1
110 #define	CP2112_IIC_STATUS1_ARB_LOST	2
111 
112 /* CP2112_REQ_VERSION */
113 struct version_request {
114 	uint8_t id;
115 	uint8_t part_num;
116 	uint8_t version;
117 } __packed;
118 
119 /* CP2112_REQ_GPIO_GET */
120 struct gpio_get_req {
121 	uint8_t id;
122 	uint8_t state;
123 } __packed;
124 
125 /* CP2112_REQ_GPIO_SET */
126 struct gpio_set_req {
127 	uint8_t id;
128 	uint8_t state;
129 	uint8_t mask;
130 } __packed;
131 
132 /* CP2112_REQ_GPIO_CFG */
133 struct gpio_config_req {
134 	uint8_t id;
135 	uint8_t output;
136 	uint8_t pushpull;
137 	uint8_t special;
138 	uint8_t divider;
139 } __packed;
140 
141 /* CP2112_REQ_SMB_XFER_STATUS_REQ */
142 struct i2c_xfer_status_req {
143 	uint8_t id;
144 	uint8_t request;
145 } __packed;
146 
147 /* CP2112_REQ_SMB_XFER_STATUS_RESP */
148 struct i2c_xfer_status_resp {
149 	uint8_t id;
150 	uint8_t status0;
151 	uint8_t status1;
152 	uint16_t status2;
153 	uint16_t status3;
154 } __packed;
155 
156 /* CP2112_REQ_SMB_READ_FORCE_SEND */
157 struct i2c_data_read_force_send_req {
158 	uint8_t id;
159 	uint16_t len;
160 } __packed;
161 
162 /* CP2112_REQ_SMB_READ_RESPONSE */
163 struct i2c_data_read_resp {
164 	uint8_t id;
165 	uint8_t status;
166 	uint8_t len;
167 	uint8_t data[61];
168 } __packed;
169 
170 /* CP2112_REQ_SMB_READ */
171 struct i2c_write_read_req {
172 	uint8_t id;
173 	uint8_t slave;
174 	uint16_t rlen;
175 	uint8_t wlen;
176 	uint8_t wdata[16];
177 } __packed;
178 
179 /* CP2112_REQ_SMB_WRITE */
180 struct i2c_read_req {
181 	uint8_t id;
182 	uint8_t slave;
183 	uint16_t len;
184 } __packed;
185 
186 /* CP2112_REQ_SMB_WRITE_READ */
187 struct i2c_write_req {
188 	uint8_t id;
189 	uint8_t slave;
190 	uint8_t len;
191 	uint8_t data[61];
192 } __packed;
193 
194 /* CP2112_REQ_SMB_CFG */
195 struct i2c_cfg_req {
196 	uint8_t		id;
197 	uint32_t	speed;		/* Hz */
198 	uint8_t		slave_addr;	/* ACK only */
199 	uint8_t		auto_send_read;	/* boolean */
200 	uint16_t	write_timeout;	/* 0-1000 ms, 0 ~ no timeout */
201 	uint16_t	read_timeout;	/* 0-1000 ms, 0 ~ no timeout */
202 	uint8_t		scl_low_timeout;/* boolean */
203 	uint16_t	retry_count;	/* 1-1000, 0 ~ forever */
204 } __packed;
205 
206 enum cp2112_out_mode {
207 	OUT_OD,
208 	OUT_PP,
209 	OUT_KEEP
210 };
211 
212 enum {
213 	CP2112_INTR_OUT = 0,
214 	CP2112_INTR_IN,
215 	CP2112_N_TRANSFER,
216 };
217 
218 struct cp2112_softc {
219 	device_t		sc_gpio_dev;
220 	device_t		sc_iic_dev;
221 	struct usb_device	*sc_udev;
222 	uint8_t			sc_iface_index;
223 	uint8_t			sc_version;
224 };
225 
226 struct cp2112gpio_softc {
227 	struct sx		gpio_lock;
228 	device_t		busdev;
229 	int			gpio_caps;
230 	struct gpio_pin		pins[CP2112_GPIO_COUNT];
231 };
232 
233 struct cp2112iic_softc {
234 	device_t	dev;
235 	device_t	iicbus_dev;
236 	struct usb_xfer	*xfers[CP2112_N_TRANSFER];
237 	u_char		own_addr;
238 	struct {
239 		struct mtx	lock;
240 		struct cv	cv;
241 		struct {
242 			uint8_t		*data;
243 			int		len;
244 			int		done;
245 			int		error;
246 		}		in;
247 		struct {
248 			const uint8_t	*data;
249 			int		len;
250 			int		done;
251 			int		error;
252 		}		out;
253 	}		io;
254 };
255 
256 static int cp2112_detach(device_t dev);
257 static int cp2112gpio_detach(device_t dev);
258 static int cp2112iic_detach(device_t dev);
259 
260 static const STRUCT_USB_HOST_ID cp2112_devs[] = {
261 	{ USB_VP(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2112) },
262 	{ USB_VP(0x1009, USB_PRODUCT_SILABS_CP2112) }, /* XXX */
263 };
264 
265 static int
266 cp2112_get_report(device_t dev, uint8_t id, void *data, uint16_t len)
267 {
268 	struct cp2112_softc *sc;
269 	int err;
270 
271 	sc = device_get_softc(dev);
272 	err = usbd_req_get_report(sc->sc_udev, NULL, data,
273 	    len, sc->sc_iface_index, UHID_FEATURE_REPORT, id);
274 	return (err);
275 }
276 
277 static int
278 cp2112_set_report(device_t dev, uint8_t id, void *data, uint16_t len)
279 {
280 	struct cp2112_softc *sc;
281 	int err;
282 
283 	sc = device_get_softc(dev);
284 	*(uint8_t *)data = id;
285 	err = usbd_req_set_report(sc->sc_udev, NULL, data,
286 	    len, sc->sc_iface_index, UHID_FEATURE_REPORT, id);
287 	return (err);
288 }
289 
290 static int
291 cp2112_probe(device_t dev)
292 {
293 	struct usb_attach_arg *uaa;
294 
295 	uaa = device_get_ivars(dev);
296 	if (uaa->usb_mode != USB_MODE_HOST)
297 		return (ENXIO);
298 	if (uaa->info.bInterfaceClass != UICLASS_HID)
299 		return (ENXIO);
300 
301 	if (usbd_lookup_id_by_uaa(cp2112_devs, sizeof(cp2112_devs), uaa) == 0)
302 		return (BUS_PROBE_DEFAULT);
303 	return (ENXIO);
304 }
305 
306 static int
307 cp2112_attach(device_t dev)
308 {
309 	struct version_request vdata;
310 	struct usb_attach_arg *uaa;
311 	struct cp2112_softc *sc;
312 	int err;
313 
314 	uaa = device_get_ivars(dev);
315 	sc = device_get_softc(dev);
316 
317 	device_set_usb_desc(dev);
318 
319 	sc->sc_udev = uaa->device;
320 	sc->sc_iface_index = uaa->info.bIfaceIndex;
321 
322 	err = cp2112_get_report(dev, CP2112_REQ_VERSION, &vdata, sizeof(vdata));
323 	if (err != 0)
324 		goto detach;
325 	device_printf(dev, "part number 0x%02x, version 0x%02x\n",
326 	    vdata.part_num, vdata.version);
327 	if (vdata.part_num != CP2112_PART_NUM) {
328 		device_printf(dev, "unsupported part number\n");
329 		goto detach;
330 	}
331 	sc->sc_version = vdata.version;
332 	sc->sc_gpio_dev = device_add_child(dev, "gpio", -1);
333 	if (sc->sc_gpio_dev != NULL) {
334 		err = device_probe_and_attach(sc->sc_gpio_dev);
335 		if (err != 0) {
336 			device_printf(dev, "failed to attach gpio child\n");
337 		}
338 	} else {
339 		device_printf(dev, "failed to create gpio child\n");
340 	}
341 
342 	sc->sc_iic_dev = device_add_child(dev, "iichb", -1);
343 	if (sc->sc_iic_dev != NULL) {
344 		err = device_probe_and_attach(sc->sc_iic_dev);
345 		if (err != 0) {
346 			device_printf(dev, "failed to attach iic child\n");
347 		}
348 	} else {
349 		device_printf(dev, "failed to create iic child\n");
350 	}
351 
352 	return (0);
353 
354 detach:
355 	cp2112_detach(dev);
356 	return (ENXIO);
357 }
358 
359 static int
360 cp2112_detach(device_t dev)
361 {
362 	int err;
363 
364 	err = bus_generic_detach(dev);
365 	if (err != 0)
366 		return (err);
367 	device_delete_children(dev);
368 	return (0);
369 }
370 
371 static int
372 cp2112_gpio_read_pin(device_t dev, uint32_t pin_num, bool *on)
373 {
374 	struct gpio_get_req data;
375 	struct cp2112gpio_softc *sc __diagused;
376 	int err;
377 
378 	sc = device_get_softc(dev);
379 	CP2112GPIO_LOCKED(sc);
380 
381 	err = cp2112_get_report(device_get_parent(dev),
382 	    CP2112_REQ_GPIO_GET, &data, sizeof(data));
383 	if (err != 0)
384 		return (err);
385 	*on = (data.state & ((uint8_t)1 << pin_num)) != 0;
386 	return (0);
387 
388 }
389 
390 static int
391 cp2112_gpio_write_pin(device_t dev, uint32_t pin_num, bool on)
392 {
393 	struct gpio_set_req data;
394 	struct cp2112gpio_softc *sc __diagused;
395 	int err;
396 	bool actual;
397 
398 	sc = device_get_softc(dev);
399 	CP2112GPIO_LOCKED(sc);
400 
401 	data.state = (uint8_t)on << pin_num;
402 	data.mask = (uint8_t)1 << pin_num;
403 	err = cp2112_set_report(device_get_parent(dev),
404 	    CP2112_REQ_GPIO_SET, &data, sizeof(data));
405 	if (err != 0)
406 		return (err);
407 	err = cp2112_gpio_read_pin(dev, pin_num, &actual);
408 	if (err != 0)
409 		return (err);
410 	if (actual != on)
411 		return (EIO);
412 	return (0);
413 }
414 
415 static int
416 cp2112_gpio_configure_write_pin(device_t dev, uint32_t pin_num,
417     bool output, enum cp2112_out_mode *mode)
418 {
419 	struct gpio_config_req data;
420 	struct cp2112gpio_softc *sc __diagused;
421 	int err;
422 	uint8_t mask;
423 
424 	sc = device_get_softc(dev);
425 	CP2112GPIO_LOCKED(sc);
426 
427 	err = cp2112_get_report(device_get_parent(dev),
428 	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
429 	if (err != 0)
430 		return (err);
431 
432 	mask = (uint8_t)1 << pin_num;
433 	if (output) {
434 		data.output |= mask;
435 		switch (*mode) {
436 		case OUT_PP:
437 			data.pushpull |= mask;
438 			break;
439 		case OUT_OD:
440 			data.pushpull &= ~mask;
441 			break;
442 		default:
443 			break;
444 		}
445 	} else {
446 		data.output &= ~mask;
447 	}
448 
449 	err = cp2112_set_report(device_get_parent(dev),
450 	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
451 	if (err != 0)
452 		return (err);
453 
454 	/* Read back and verify. */
455 	err = cp2112_get_report(device_get_parent(dev),
456 	    CP2112_REQ_GPIO_CFG, &data, sizeof(data));
457 	if (err != 0)
458 		return (err);
459 
460 	if (((data.output & mask) != 0) != output)
461 		return (EIO);
462 	if (output) {
463 		switch (*mode) {
464 		case OUT_PP:
465 			if ((data.pushpull & mask) == 0)
466 				return (EIO);
467 			break;
468 		case OUT_OD:
469 			if ((data.pushpull & mask) != 0)
470 				return (EIO);
471 			break;
472 		default:
473 			*mode = (data.pushpull & mask) != 0 ?
474 			    OUT_PP : OUT_OD;
475 			break;
476 		}
477 	}
478 	return (0);
479 }
480 
481 static device_t
482 cp2112_gpio_get_bus(device_t dev)
483 {
484 	struct cp2112gpio_softc *sc;
485 
486 	sc = device_get_softc(dev);
487 	return (sc->busdev);
488 }
489 
490 static int
491 cp2112_gpio_pin_max(device_t dev, int *maxpin)
492 {
493 
494 	*maxpin = CP2112_GPIO_COUNT - 1;
495 	return (0);
496 }
497 
498 static int
499 cp2112_gpio_pin_set(device_t dev, uint32_t pin_num, uint32_t pin_value)
500 {
501 	struct cp2112gpio_softc *sc;
502 	int err;
503 
504 	if (pin_num >= CP2112_GPIO_COUNT)
505 		return (EINVAL);
506 
507 	sc = device_get_softc(dev);
508 	CP2112GPIO_LOCK(sc);
509 	err = cp2112_gpio_write_pin(dev, pin_num, pin_value != 0);
510 	CP2112GPIO_UNLOCK(sc);
511 
512 	return (err);
513 }
514 
515 static int
516 cp2112_gpio_pin_get(device_t dev, uint32_t pin_num, uint32_t *pin_value)
517 {
518 	struct cp2112gpio_softc *sc;
519 	int err;
520 	bool on;
521 
522 	if (pin_num >= CP2112_GPIO_COUNT)
523 		return (EINVAL);
524 
525 	sc = device_get_softc(dev);
526 	CP2112GPIO_LOCK(sc);
527 	err = cp2112_gpio_read_pin(dev, pin_num, &on);
528 	CP2112GPIO_UNLOCK(sc);
529 
530 	if (err == 0)
531 		*pin_value = on;
532 	return (err);
533 }
534 
535 static int
536 cp2112_gpio_pin_toggle(device_t dev, uint32_t pin_num)
537 {
538 	struct cp2112gpio_softc *sc;
539 	int err;
540 	bool on;
541 
542 	if (pin_num >= CP2112_GPIO_COUNT)
543 		return (EINVAL);
544 
545 	sc = device_get_softc(dev);
546 	CP2112GPIO_LOCK(sc);
547 	err = cp2112_gpio_read_pin(dev, pin_num, &on);
548 	if (err == 0)
549 		err = cp2112_gpio_write_pin(dev, pin_num, !on);
550 	CP2112GPIO_UNLOCK(sc);
551 
552 	return (err);
553 }
554 
555 static int
556 cp2112_gpio_pin_getcaps(device_t dev, uint32_t pin_num, uint32_t *caps)
557 {
558 	struct cp2112gpio_softc *sc;
559 
560 	if (pin_num >= CP2112_GPIO_COUNT)
561 		return (EINVAL);
562 
563 	sc = device_get_softc(dev);
564 	CP2112GPIO_LOCK(sc);
565 	*caps = sc->gpio_caps;
566 	CP2112GPIO_UNLOCK(sc);
567 
568 	return (0);
569 }
570 
571 static int
572 cp2112_gpio_pin_getflags(device_t dev, uint32_t pin_num, uint32_t *flags)
573 {
574 	struct cp2112gpio_softc *sc;
575 
576 	if (pin_num >= CP2112_GPIO_COUNT)
577 		return (EINVAL);
578 
579 	sc = device_get_softc(dev);
580 	CP2112GPIO_LOCK(sc);
581 	*flags = sc->pins[pin_num].gp_flags;
582 	CP2112GPIO_UNLOCK(sc);
583 
584 	return (0);
585 }
586 
587 static int
588 cp2112_gpio_pin_getname(device_t dev, uint32_t pin_num, char *name)
589 {
590 	struct cp2112gpio_softc *sc;
591 
592 	if (pin_num >= CP2112_GPIO_COUNT)
593 		return (EINVAL);
594 
595 	sc = device_get_softc(dev);
596 	CP2112GPIO_LOCK(sc);
597 	memcpy(name, sc->pins[pin_num].gp_name, GPIOMAXNAME);
598 	CP2112GPIO_UNLOCK(sc);
599 
600 	return (0);
601 }
602 
603 static int
604 cp2112_gpio_pin_setflags(device_t dev, uint32_t pin_num, uint32_t flags)
605 {
606 	struct cp2112gpio_softc *sc;
607 	struct gpio_pin *pin;
608 	enum cp2112_out_mode out_mode;
609 	int err;
610 
611 	if (pin_num >= CP2112_GPIO_COUNT)
612 		return (EINVAL);
613 
614 	sc = device_get_softc(dev);
615 	if ((flags & sc->gpio_caps) != flags)
616 		return (EINVAL);
617 
618 	if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) == 0)
619 			return (EINVAL);
620 	if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
621 		(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
622 			return (EINVAL);
623 	}
624 	if ((flags & GPIO_PIN_INPUT) != 0) {
625 		if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) != 0)
626 			return (EINVAL);
627 	} else {
628 		if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) ==
629 		    (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL))
630 			return (EINVAL);
631 	}
632 
633 	/*
634 	 * If neither push-pull or open-drain is explicitly requested, then
635 	 * preserve the current state.
636 	 */
637 	out_mode = OUT_KEEP;
638 	if ((flags & GPIO_PIN_OUTPUT) != 0) {
639 		if ((flags & GPIO_PIN_OPENDRAIN) != 0)
640 			out_mode = OUT_OD;
641 		if ((flags & GPIO_PIN_PUSHPULL) != 0)
642 			out_mode = OUT_PP;
643 	}
644 
645 	CP2112GPIO_LOCK(sc);
646 	pin = &sc->pins[pin_num];
647 	err = cp2112_gpio_configure_write_pin(dev, pin_num,
648 	    (flags & GPIO_PIN_OUTPUT) != 0, &out_mode);
649 	if (err == 0) {
650 		/*
651 		 * If neither open-drain or push-pull was requested, then see
652 		 * what hardware actually had.  Otherwise, it has been
653 		 * reconfigured as requested.
654 		 */
655 		if ((flags & GPIO_PIN_OUTPUT) != 0 &&
656 		    (flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) == 0) {
657 			KASSERT(out_mode != OUT_KEEP,
658 			    ("impossible current output mode"));
659 			if (out_mode == OUT_OD)
660 				flags |= GPIO_PIN_OPENDRAIN;
661 			else
662 				flags |= GPIO_PIN_PUSHPULL;
663 		}
664 		pin->gp_flags = flags;
665 	}
666 	CP2112GPIO_UNLOCK(sc);
667 
668 	return (err);
669 }
670 
671 static int
672 cp2112gpio_probe(device_t dev)
673 {
674 	device_set_desc(dev, "CP2112 GPIO interface");
675 	return (BUS_PROBE_SPECIFIC);
676 }
677 
678 static int
679 cp2112gpio_attach(device_t dev)
680 {
681 	struct gpio_config_req data;
682 	struct cp2112gpio_softc *sc;
683 	device_t cp2112;
684 	int err;
685 	int i;
686 	uint8_t mask;
687 
688 	cp2112 = device_get_parent(dev);
689 	sc = device_get_softc(dev);
690 	sx_init(&sc->gpio_lock, "cp2112 lock");
691 
692 	sc->gpio_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
693 	    GPIO_PIN_PUSHPULL;
694 
695 	err = cp2112_get_report(cp2112, CP2112_REQ_GPIO_CFG,
696 	    &data, sizeof(data));
697 	if (err != 0)
698 		goto detach;
699 
700 	for (i = 0; i < CP2112_GPIO_COUNT; i++) {
701 		struct gpio_pin *pin;
702 
703 		mask = (uint8_t)1 << i;
704 		pin = &sc->pins[i];
705 		pin->gp_flags = 0;
706 
707 		snprintf(pin->gp_name, GPIOMAXNAME, "GPIO%u", i);
708 		pin->gp_name[GPIOMAXNAME - 1] = '\0';
709 
710 		if ((i == 0 && (data.special & CP2112_GPIO_SPEC_TX0) != 0) ||
711 		    (i == 1 && (data.special & CP2112_GPIO_SPEC_RX1) != 0) ||
712 		    (i == 7 && (data.special & CP2112_GPIO_SPEC_CLK7) != 0)) {
713 			/* Special mode means that a pin is not for GPIO. */
714 		} else if ((data.output & mask) != 0) {
715 			pin->gp_flags |= GPIO_PIN_OUTPUT;
716 			if ((data.pushpull & mask) != 0)
717 				pin->gp_flags |= GPIO_PIN_PUSHPULL;
718 			else
719 				pin->gp_flags |= GPIO_PIN_OPENDRAIN;
720 		} else {
721 			pin->gp_flags |= GPIO_PIN_INPUT;
722 		}
723 	}
724 
725 	sc->busdev = gpiobus_attach_bus(dev);
726 	if (sc->busdev == NULL) {
727 		device_printf(dev, "gpiobus_attach_bus failed\n");
728 		goto detach;
729 	}
730 	return (0);
731 
732 detach:
733 	cp2112gpio_detach(dev);
734 	return (ENXIO);
735 }
736 
737 static int
738 cp2112gpio_detach(device_t dev)
739 {
740 	struct cp2112gpio_softc *sc;
741 
742 	sc = device_get_softc(dev);
743 	if (sc->busdev != NULL)
744 		gpiobus_detach_bus(dev);
745 	sx_destroy(&sc->gpio_lock);
746 	return (0);
747 }
748 
749 static void
750 cp2112iic_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
751 {
752 	struct cp2112iic_softc *sc;
753 	struct usb_page_cache *pc;
754 
755 	sc = usbd_xfer_softc(xfer);
756 
757 	mtx_assert(&sc->io.lock, MA_OWNED);
758 
759 	switch (USB_GET_STATE(xfer)) {
760 	case USB_ST_SETUP:
761 		pc = usbd_xfer_get_frame(xfer, 0);
762 		usbd_copy_in(pc, 0, sc->io.out.data, sc->io.out.len);
763 		usbd_xfer_set_frame_len(xfer, 0, sc->io.out.len);
764 		usbd_xfer_set_frames(xfer, 1);
765 		usbd_transfer_submit(xfer);
766 		break;
767 	case USB_ST_TRANSFERRED:
768 		sc->io.out.error = 0;
769 		sc->io.out.done = 1;
770 		cv_signal(&sc->io.cv);
771 		break;
772 	default:			/* Error */
773 		device_printf(sc->dev, "write intr state %d error %d\n",
774 		    USB_GET_STATE(xfer), error);
775 		sc->io.out.error = IIC_EBUSERR;
776 		cv_signal(&sc->io.cv);
777 		if (error != USB_ERR_CANCELLED) {
778 			/* try to clear stall first */
779 			usbd_xfer_set_stall(xfer);
780 		}
781 		break;
782 	}
783 }
784 
785 static void
786 cp2112iic_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
787 {
788 	struct cp2112iic_softc *sc = usbd_xfer_softc(xfer);
789 	struct usb_page_cache *pc;
790 	int act_len, len;
791 
792 	mtx_assert(&sc->io.lock, MA_OWNED);
793 	usbd_xfer_status(xfer, &act_len, NULL, NULL, NULL);
794 
795 	switch (USB_GET_STATE(xfer)) {
796 	case USB_ST_TRANSFERRED:
797 		if (sc->io.in.done) {
798 			device_printf(sc->dev,
799 			    "interrupt while previous is pending, ignored\n");
800 		} else if (sc->io.in.len == 0) {
801 			uint8_t buf[8];
802 
803 			/*
804 			 * There is a spurious Transfer Status Response and
805 			 * zero-length Read Response during hardware
806 			 * configuration.  Possibly they carry some information
807 			 * about the initial bus state.
808 			 */
809 			if (device_is_attached(sc->dev)) {
810 				device_printf(sc->dev,
811 				    "unsolicited interrupt, ignored\n");
812 				if (bootverbose) {
813 					pc = usbd_xfer_get_frame(xfer, 0);
814 					len = MIN(sizeof(buf), act_len);
815 					usbd_copy_out(pc, 0, buf, len);
816 					device_printf(sc->dev, "data: %*D\n",
817 					    len, buf, " ");
818 				}
819 			} else {
820 				pc = usbd_xfer_get_frame(xfer, 0);
821 				len = MIN(sizeof(buf), act_len);
822 				usbd_copy_out(pc, 0, buf, len);
823 				if (buf[0] == CP2112_REQ_SMB_XFER_STATUS_RESP) {
824 					device_printf(sc->dev,
825 					    "initial bus status0 = 0x%02x, "
826 					    "status1 = 0x%02x\n",
827 					    buf[1], buf[2]);
828 				}
829 			}
830 		} else if (act_len == CP2112_REPORT_SIZE) {
831 			pc = usbd_xfer_get_frame(xfer, 0);
832 			usbd_copy_out(pc, 0, sc->io.in.data, sc->io.in.len);
833 			sc->io.in.error = 0;
834 			sc->io.in.done = 1;
835 		} else {
836 			device_printf(sc->dev,
837 			    "unexpected input report length %u\n", act_len);
838 			sc->io.in.error = IIC_EBUSERR;
839 			sc->io.in.done = 1;
840 		}
841 		cv_signal(&sc->io.cv);
842 	case USB_ST_SETUP:
843 tr_setup:
844 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
845 		usbd_transfer_submit(xfer);
846 		break;
847 
848 	default:			/* Error */
849 		device_printf(sc->dev, "read intr state %d error %d\n",
850 		    USB_GET_STATE(xfer), error);
851 
852 		sc->io.in.error = IIC_EBUSERR;
853 		sc->io.in.done = 1;
854 		cv_signal(&sc->io.cv);
855 		if (error != USB_ERR_CANCELLED) {
856 			/* try to clear stall first */
857 			usbd_xfer_set_stall(xfer);
858 			goto tr_setup;
859 		}
860 		break;
861 	}
862 }
863 
864 static const struct usb_config cp2112iic_config[CP2112_N_TRANSFER] = {
865 	[CP2112_INTR_OUT] = {
866 		.type = UE_INTERRUPT,
867 		.endpoint = UE_ADDR_ANY,
868 		.direction = UE_DIR_OUT,
869 		.flags = { .pipe_bof = 1, .no_pipe_ok = 1, },
870 		.bufsize = 0,	/* use wMaxPacketSize */
871 		.callback = &cp2112iic_intr_write_callback,
872 	},
873 	[CP2112_INTR_IN] = {
874 		.type = UE_INTERRUPT,
875 		.endpoint = UE_ADDR_ANY,
876 		.direction = UE_DIR_IN,
877 		.flags = { .pipe_bof = 1, .short_xfer_ok = 1, },
878 		.bufsize = 0,	/* use wMaxPacketSize */
879 		.callback = &cp2112iic_intr_read_callback,
880 	},
881 };
882 
883 static int
884 cp2112iic_send_req(struct cp2112iic_softc *sc, const void *data,
885     uint16_t len)
886 {
887 	int err;
888 
889 	mtx_assert(&sc->io.lock, MA_OWNED);
890 	KASSERT(sc->io.out.done == 0, ("%s: conflicting request", __func__));
891 
892 	sc->io.out.data = data;
893 	sc->io.out.len = len;
894 
895 	DTRACE_PROBE1(send__req, uint8_t, *(const uint8_t *)data);
896 
897 	usbd_transfer_start(sc->xfers[CP2112_INTR_OUT]);
898 
899 	while (!sc->io.out.done)
900 		cv_wait(&sc->io.cv, &sc->io.lock);
901 
902 	usbd_transfer_stop(sc->xfers[CP2112_INTR_OUT]);
903 
904 	sc->io.out.done = 0;
905 	sc->io.out.data = NULL;
906 	sc->io.out.len = 0;
907 	err = sc->io.out.error;
908 	if (err != 0) {
909 		device_printf(sc->dev, "output report 0x%02x failed: %d\n",
910 		    *(const uint8_t*)data, err);
911 	}
912 	return (err);
913 }
914 
915 static int
916 cp2112iic_req_resp(struct cp2112iic_softc *sc, const void *req_data,
917     uint16_t req_len, void *resp_data, uint16_t resp_len)
918 {
919 	int err;
920 
921 	mtx_assert(&sc->io.lock, MA_OWNED);
922 
923 	/*
924 	 * Prepare to receive a response interrupt even before the
925 	 * request transfer is confirmed (USB_ST_TRANSFERED).
926 	 */
927 	KASSERT(sc->io.in.done == 0, ("%s: conflicting request", __func__));
928 	sc->io.in.len = resp_len;
929 	sc->io.in.data = resp_data;
930 
931 	err = cp2112iic_send_req(sc, req_data, req_len);
932 	if (err != 0) {
933 		sc->io.in.len = 0;
934 		sc->io.in.data = NULL;
935 		return (err);
936 	}
937 
938 	while (!sc->io.in.done)
939 		cv_wait(&sc->io.cv, &sc->io.lock);
940 
941 	err = sc->io.in.error;
942 	sc->io.in.done = 0;
943 	sc->io.in.error = 0;
944 	sc->io.in.len = 0;
945 	sc->io.in.data = NULL;
946 	return (err);
947 }
948 
949 static int
950 cp2112iic_check_req_status(struct cp2112iic_softc *sc)
951 {
952 	struct i2c_xfer_status_req xfer_status_req;
953 	struct i2c_xfer_status_resp xfer_status_resp;
954 	int err;
955 
956 	mtx_assert(&sc->io.lock, MA_OWNED);
957 
958 	do {
959 		xfer_status_req.id = CP2112_REQ_SMB_XFER_STATUS_REQ;
960 		xfer_status_req.request = 1;
961 		err = cp2112iic_req_resp(sc,
962 		    &xfer_status_req, sizeof(xfer_status_req),
963 		    &xfer_status_resp, sizeof(xfer_status_resp));
964 
965 		if (xfer_status_resp.id != CP2112_REQ_SMB_XFER_STATUS_RESP) {
966 			device_printf(sc->dev,
967 			    "unexpected response 0x%02x to status request\n",
968 			    xfer_status_resp.id);
969 			err = IIC_EBUSERR;
970 			goto out;
971 		}
972 
973 		DTRACE_PROBE4(xfer__status, uint8_t, xfer_status_resp.status0,
974 		    uint8_t, xfer_status_resp.status1,
975 		    uint16_t, be16toh(xfer_status_resp.status2),
976 		    uint16_t, be16toh(xfer_status_resp.status3));
977 
978 		switch (xfer_status_resp.status0) {
979 		case CP2112_IIC_STATUS0_IDLE:
980 			err = IIC_ESTATUS;
981 			break;
982 		case CP2112_IIC_STATUS0_BUSY:
983 			err = ERESTART;	/* non-I2C, special handling */
984 			break;
985 		case CP2112_IIC_STATUS0_CMP:
986 			err = IIC_NOERR;
987 			break;
988 		case CP2112_IIC_STATUS0_ERROR:
989 			switch (xfer_status_resp.status1) {
990 			case CP2112_IIC_STATUS1_TIMEOUT_NACK:
991 				err = IIC_ENOACK;
992 				break;
993 			case CP2112_IIC_STATUS1_TIMEOUT_BUS:
994 				err = IIC_ETIMEOUT;
995 				break;
996 			case CP2112_IIC_STATUS1_ARB_LOST:
997 				err = IIC_EBUSBSY;
998 				break;
999 			default:
1000 				device_printf(sc->dev,
1001 				    "i2c error, status = 0x%02x\n",
1002 				    xfer_status_resp.status1);
1003 				err = IIC_ESTATUS;
1004 				break;
1005 			}
1006 			break;
1007 		default:
1008 			device_printf(sc->dev,
1009 			    "unknown i2c xfer status0 0x%02x\n",
1010 			    xfer_status_resp.status0);
1011 			err = IIC_EBUSERR;
1012 			break;
1013 		}
1014 
1015 	} while (err == ERESTART);
1016 out:
1017 	return (err);
1018 }
1019 
1020 static int
1021 cp2112iic_read_data(struct cp2112iic_softc *sc, void *data, uint16_t in_len,
1022     uint16_t *out_len)
1023 {
1024 	struct i2c_data_read_force_send_req data_read_force_send;
1025 	struct i2c_data_read_resp data_read_resp;
1026 	int err;
1027 
1028 	mtx_assert(&sc->io.lock, MA_OWNED);
1029 
1030 	/*
1031 	 * Prepare to receive a response interrupt even before the request
1032 	 * transfer is confirmed (USB_ST_TRANSFERED).
1033 	 */
1034 
1035 	if (in_len > sizeof(data_read_resp.data))
1036 		in_len = sizeof(data_read_resp.data);
1037 	data_read_force_send.id = CP2112_REQ_SMB_READ_FORCE_SEND;
1038 	data_read_force_send.len = htobe16(in_len);
1039 	err = cp2112iic_req_resp(sc,
1040 	    &data_read_force_send, sizeof(data_read_force_send),
1041 	    &data_read_resp, sizeof(data_read_resp));
1042 	if (err != 0)
1043 		goto out;
1044 
1045 	if (data_read_resp.id != CP2112_REQ_SMB_READ_RESPONSE) {
1046 		device_printf(sc->dev,
1047 		    "unexpected response 0x%02x to data read request\n",
1048 		    data_read_resp.id);
1049 		err = IIC_EBUSERR;
1050 		goto out;
1051 	}
1052 
1053 	DTRACE_PROBE2(read__response, uint8_t, data_read_resp.status,
1054 	    uint8_t, data_read_resp.len);
1055 
1056 	/*
1057 	 * We expect either the request completed status or, more typical for
1058 	 * this driver, the bus idle status because of the preceding
1059 	 * Force Read Status command (which is not an I2C request).
1060 	 */
1061 	if (data_read_resp.status != CP2112_IIC_STATUS0_CMP &&
1062 	    data_read_resp.status != CP2112_IIC_STATUS0_IDLE) {
1063 		err = IIC_EBUSERR;
1064 		goto out;
1065 	}
1066 	if (data_read_resp.len > in_len) {
1067 		device_printf(sc->dev, "device returns more data than asked\n");
1068 		err = IIC_EOVERFLOW;
1069 		goto out;
1070 	}
1071 
1072 	*out_len = data_read_resp.len;
1073 	if (*out_len > 0)
1074 		memcpy(data, data_read_resp.data, *out_len);
1075 out:
1076 	return (err);
1077 }
1078 
1079 static int
1080 cp2112iic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1081 {
1082 	struct cp2112iic_softc *sc = device_get_softc(dev);
1083 	struct cp2112_softc *psc = device_get_softc(device_get_parent(dev));
1084 	const char *reason = NULL;
1085 	uint32_t i;
1086 	uint16_t read_off, to_read;
1087 	int err;
1088 
1089 	/*
1090 	 * The hardware interface imposes limits on allowed I2C messages.
1091 	 * It is not possible to explicitly send a start or stop.
1092 	 * It is not possible to do a zero length transfer.
1093 	 * For this reason it's impossible to send a message with no data
1094 	 * at all (like an SMBus quick message).
1095 	 * Each read or write transfer beginning with the start condition
1096 	 * and ends with the stop condition.  The only exception is that
1097 	 * it is possible to have a write transfer followed by a read
1098 	 * transfer to the same slave with the repeated start condition
1099 	 * between them.
1100 	 */
1101 	for (i = 0; i < nmsgs; i++) {
1102 		if (i == 0 && (msgs[i].flags & IIC_M_NOSTART) != 0) {
1103 			reason = "first message without start";
1104 			break;
1105 		}
1106 		if (i == nmsgs - 1 && (msgs[i].flags & IIC_M_NOSTOP) != 0) {
1107 			reason = "last message without stop";
1108 			break;
1109 		}
1110 		if (msgs[i].len == 0) {
1111 			reason = "message with no data";
1112 			break;
1113 		}
1114 		if ((msgs[i].flags & IIC_M_RD) != 0 &&
1115 		    msgs[i].len > CP2112_IIC_MAX_READ_LEN) {
1116 			reason = "too long read";
1117 			break;
1118 		}
1119 		if ((msgs[i].flags & IIC_M_RD) == 0 &&
1120 		    msgs[i].len > SIZEOF_FIELD(i2c_write_req, data)) {
1121 			reason = "too long write";
1122 			break;
1123 		}
1124 		if ((msgs[i].flags & IIC_M_NOSTART) != 0) {
1125 			reason = "message without start or repeated start";
1126 			break;
1127 		}
1128 		if ((msgs[i].flags & IIC_M_NOSTOP) != 0 &&
1129 		    (msgs[i].flags & IIC_M_RD) != 0) {
1130 			reason = "read without stop";
1131 			break;
1132 		}
1133 		if ((msgs[i].flags & IIC_M_NOSTOP) != 0 &&
1134 		    psc->sc_version < CP2112_IIC_REPSTART_VER) {
1135 			reason = "write without stop";
1136 			break;
1137 		}
1138 		if ((msgs[i].flags & IIC_M_NOSTOP) != 0 &&
1139 		    msgs[i].len > SIZEOF_FIELD(i2c_write_read_req, wdata)) {
1140 			reason = "too long write without stop";
1141 			break;
1142 		}
1143 		if (i > 0) {
1144 			if ((msgs[i - 1].flags & IIC_M_NOSTOP) != 0 &&
1145 			    msgs[i].slave != msgs[i - 1].slave) {
1146 				reason = "change of slave without stop";
1147 				break;
1148 			}
1149 			if ((msgs[i - 1].flags & IIC_M_NOSTOP) != 0 &&
1150 			    (msgs[i].flags & IIC_M_RD) == 0) {
1151 				reason = "write after repeated start";
1152 				break;
1153 			}
1154 		}
1155 	}
1156 	if (reason != NULL) {
1157 		if (bootverbose)
1158 			device_printf(dev, "unsupported i2c message: %s\n",
1159 			    reason);
1160 		return (IIC_ENOTSUPP);
1161 	}
1162 
1163 	mtx_lock(&sc->io.lock);
1164 
1165 	for (i = 0; i < nmsgs; i++) {
1166 		if (i + 1 < nmsgs && (msgs[i].flags & IIC_M_NOSTOP) != 0) {
1167 			/*
1168 			 * Combine <write><repeated start><read> into a single
1169 			 * CP2112 operation.
1170 			 */
1171 			struct i2c_write_read_req req;
1172 
1173 			KASSERT((msgs[i].flags & IIC_M_RD) == 0,
1174 			    ("read without stop"));
1175 			KASSERT((msgs[i + 1].flags & IIC_M_RD) != 0,
1176 			    ("write after write without stop"));
1177 			req.id = CP2112_REQ_SMB_WRITE_READ;
1178 			req.slave = msgs[i].slave & ~LSB;
1179 			to_read = msgs[i + 1].len;
1180 			req.rlen = htobe16(to_read);
1181 			req.wlen = msgs[i].len;
1182 			memcpy(req.wdata, msgs[i].buf, msgs[i].len);
1183 			err = cp2112iic_send_req(sc, &req, msgs[i].len + 5);
1184 
1185 			/*
1186 			 * The next message is already handled.
1187 			 * Also needed for read data to go into the right msg.
1188 			 */
1189 			i++;
1190 		} else if ((msgs[i].flags & IIC_M_RD) != 0) {
1191 			struct i2c_read_req req;
1192 
1193 			req.id = CP2112_REQ_SMB_READ;
1194 			req.slave = msgs[i].slave & ~LSB;
1195 			to_read = msgs[i].len;
1196 			req.len = htobe16(to_read);
1197 			err = cp2112iic_send_req(sc, &req, sizeof(req));
1198 		} else {
1199 			struct i2c_write_req req;
1200 
1201 			req.id = CP2112_REQ_SMB_WRITE;
1202 			req.slave = msgs[i].slave & ~LSB;
1203 			req.len = msgs[i].len;
1204 			memcpy(req.data, msgs[i].buf, msgs[i].len);
1205 			to_read = 0;
1206 			err = cp2112iic_send_req(sc, &req, msgs[i].len + 3);
1207 		}
1208 		if (err != 0)
1209 			break;
1210 
1211 		err = cp2112iic_check_req_status(sc);
1212 		if (err != 0)
1213 			break;
1214 
1215 		read_off = 0;
1216 		while (to_read > 0) {
1217 			uint16_t act_read;
1218 
1219 			err = cp2112iic_read_data(sc, msgs[i].buf + read_off,
1220 			    to_read, &act_read);
1221 			if (err != 0)
1222 				break;
1223 			KASSERT(act_read <= to_read, ("cp2112iic_read_data "
1224 			    "returned more data than asked"));
1225 			read_off += act_read;
1226 			to_read -= act_read;
1227 		}
1228 		if (err != 0)
1229 			break;
1230 	}
1231 
1232 	mtx_unlock(&sc->io.lock);
1233 	return (err);
1234 }
1235 
1236 static int
1237 cp2112iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
1238 {
1239 	struct i2c_cfg_req i2c_cfg;
1240 	struct cp2112iic_softc *sc;
1241 	device_t cp2112;
1242 	u_int busfreq;
1243 	int err;
1244 
1245 	sc = device_get_softc(dev);
1246 	cp2112 = device_get_parent(dev);
1247 	if (sc->iicbus_dev == NULL)
1248 		busfreq = 100000;
1249 	else
1250 		busfreq = IICBUS_GET_FREQUENCY(sc->iicbus_dev, speed);
1251 
1252 	err = cp2112_get_report(cp2112, CP2112_REQ_SMB_CFG,
1253 	    &i2c_cfg, sizeof(i2c_cfg));
1254 	if (err != 0) {
1255 		device_printf(dev, "failed to get CP2112_REQ_SMB_CFG report\n");
1256 		return (err);
1257 	}
1258 
1259 	if (oldaddr != NULL)
1260 		*oldaddr = i2c_cfg.slave_addr;
1261 	/*
1262 	 * For simplicity we do not enable Auto Send Read
1263 	 * because of erratum CP2112_E101 (fixed in version 3).
1264 	 *
1265 	 * TODO: set I2C parameters based on configuration preferences:
1266 	 * - read and write timeouts (no timeout by default),
1267 	 * - SCL low timeout (disabled by default),
1268 	 * etc.
1269 	 *
1270 	 * TODO: should the device reset request (0x01) be sent?
1271 	 * If the device disconnects as a result, then no.
1272 	 */
1273 	i2c_cfg.speed = htobe32(busfreq);
1274 	if (addr != 0)
1275 		i2c_cfg.slave_addr = addr;
1276 	i2c_cfg.auto_send_read = 0;
1277 	i2c_cfg.retry_count = htobe16(1);
1278 	i2c_cfg.scl_low_timeout = 0;
1279 	if (bootverbose) {
1280 		device_printf(dev, "speed %d Hz\n", be32toh(i2c_cfg.speed));
1281 		device_printf(dev, "slave addr 0x%02x\n", i2c_cfg.slave_addr);
1282 		device_printf(dev, "auto send read %s\n",
1283 		    i2c_cfg.auto_send_read ? "on" : "off");
1284 		device_printf(dev, "write timeout %d ms (0 - disabled)\n",
1285 		    be16toh(i2c_cfg.write_timeout));
1286 		device_printf(dev, "read timeout %d ms (0 - disabled)\n",
1287 		    be16toh(i2c_cfg.read_timeout));
1288 		device_printf(dev, "scl low timeout %s\n",
1289 		    i2c_cfg.scl_low_timeout ? "on" : "off");
1290 		device_printf(dev, "retry count %d (0 - no limit)\n",
1291 		    be16toh(i2c_cfg.retry_count));
1292 	}
1293 	err = cp2112_set_report(cp2112, CP2112_REQ_SMB_CFG,
1294 	    &i2c_cfg, sizeof(i2c_cfg));
1295 	if (err != 0) {
1296 		device_printf(dev, "failed to set CP2112_REQ_SMB_CFG report\n");
1297 		return (err);
1298 	}
1299 	return (0);
1300 }
1301 
1302 static int
1303 cp2112iic_probe(device_t dev)
1304 {
1305 	device_set_desc(dev, "CP2112 I2C interface");
1306 	return (BUS_PROBE_SPECIFIC);
1307 }
1308 
1309 static int
1310 cp2112iic_attach(device_t dev)
1311 {
1312 	struct cp2112iic_softc *sc;
1313 	struct cp2112_softc *psc;
1314 	device_t cp2112;
1315 	int err;
1316 
1317 	sc = device_get_softc(dev);
1318 	sc->dev = dev;
1319 	cp2112 = device_get_parent(dev);
1320 	psc = device_get_softc(cp2112);
1321 
1322 	mtx_init(&sc->io.lock, "cp2112iic lock", NULL, MTX_DEF | MTX_RECURSE);
1323 	cv_init(&sc->io.cv, "cp2112iic cv");
1324 
1325 	err = usbd_transfer_setup(psc->sc_udev,
1326 	    &psc->sc_iface_index, sc->xfers, cp2112iic_config,
1327 	    nitems(cp2112iic_config), sc, &sc->io.lock);
1328 	if (err != 0) {
1329 		device_printf(dev, "usbd_transfer_setup failed %d\n", err);
1330 		goto detach;
1331 	}
1332 
1333 	/* Prepare to receive interrupts. */
1334 	mtx_lock(&sc->io.lock);
1335 	usbd_transfer_start(sc->xfers[CP2112_INTR_IN]);
1336 	mtx_unlock(&sc->io.lock);
1337 
1338 	sc->iicbus_dev = device_add_child(dev, "iicbus", -1);
1339 	if (sc->iicbus_dev == NULL) {
1340 		device_printf(dev, "iicbus creation failed\n");
1341 		err = ENXIO;
1342 		goto detach;
1343 	}
1344 	bus_generic_attach(dev);
1345 	return (0);
1346 
1347 detach:
1348 	cp2112iic_detach(dev);
1349 	return (err);
1350 }
1351 
1352 static int
1353 cp2112iic_detach(device_t dev)
1354 {
1355 	struct cp2112iic_softc *sc;
1356 	int err;
1357 
1358 	sc = device_get_softc(dev);
1359 	err = bus_generic_detach(dev);
1360 	if (err != 0)
1361 		return (err);
1362 	device_delete_children(dev);
1363 
1364 	mtx_lock(&sc->io.lock);
1365 	usbd_transfer_stop(sc->xfers[CP2112_INTR_IN]);
1366 	mtx_unlock(&sc->io.lock);
1367 	usbd_transfer_unsetup(sc->xfers, nitems(cp2112iic_config));
1368 
1369 	cv_destroy(&sc->io.cv);
1370 	mtx_destroy(&sc->io.lock);
1371 
1372 	return (0);
1373 }
1374 
1375 static device_method_t cp2112hid_methods[] = {
1376 	DEVMETHOD(device_probe,		cp2112_probe),
1377 	DEVMETHOD(device_attach,	cp2112_attach),
1378 	DEVMETHOD(device_detach,	cp2112_detach),
1379 
1380 	DEVMETHOD_END
1381 };
1382 
1383 static driver_t cp2112hid_driver = {
1384 	.name = "cp2112hid",
1385 	.methods = cp2112hid_methods,
1386 	.size = sizeof(struct cp2112_softc),
1387 };
1388 
1389 DRIVER_MODULE(cp2112hid, uhub, cp2112hid_driver, NULL, NULL);
1390 MODULE_DEPEND(cp2112hid, usb, 1, 1, 1);
1391 MODULE_VERSION(cp2112hid, 1);
1392 USB_PNP_HOST_INFO(cp2112_devs);
1393 
1394 static device_method_t cp2112gpio_methods[] = {
1395 	/* Device */
1396 	DEVMETHOD(device_probe,		cp2112gpio_probe),
1397 	DEVMETHOD(device_attach,	cp2112gpio_attach),
1398 	DEVMETHOD(device_detach,	cp2112gpio_detach),
1399 
1400 	/* GPIO */
1401 	DEVMETHOD(gpio_get_bus,		cp2112_gpio_get_bus),
1402 	DEVMETHOD(gpio_pin_max,		cp2112_gpio_pin_max),
1403 	DEVMETHOD(gpio_pin_get,		cp2112_gpio_pin_get),
1404 	DEVMETHOD(gpio_pin_set,		cp2112_gpio_pin_set),
1405 	DEVMETHOD(gpio_pin_toggle,	cp2112_gpio_pin_toggle),
1406 	DEVMETHOD(gpio_pin_getname,	cp2112_gpio_pin_getname),
1407 	DEVMETHOD(gpio_pin_getcaps,	cp2112_gpio_pin_getcaps),
1408 	DEVMETHOD(gpio_pin_getflags,	cp2112_gpio_pin_getflags),
1409 	DEVMETHOD(gpio_pin_setflags,	cp2112_gpio_pin_setflags),
1410 
1411 	DEVMETHOD_END
1412 };
1413 
1414 static driver_t cp2112gpio_driver = {
1415 	.name = "gpio",
1416 	.methods = cp2112gpio_methods,
1417 	.size = sizeof(struct cp2112gpio_softc),
1418 };
1419 
1420 DRIVER_MODULE(cp2112gpio, cp2112hid, cp2112gpio_driver, NULL, NULL);
1421 MODULE_DEPEND(cp2112gpio, cp2112hid, 1, 1, 1);
1422 MODULE_DEPEND(cp2112gpio, gpiobus, 1, 1, 1);
1423 MODULE_VERSION(cp2112gpio, 1);
1424 
1425 static device_method_t cp2112iic_methods[] = {
1426 	/* Device interface */
1427 	DEVMETHOD(device_probe, cp2112iic_probe),
1428 	DEVMETHOD(device_attach, cp2112iic_attach),
1429 	DEVMETHOD(device_detach, cp2112iic_detach),
1430 
1431 	/* I2C methods */
1432 	DEVMETHOD(iicbus_transfer, cp2112iic_transfer),
1433 	DEVMETHOD(iicbus_reset, cp2112iic_reset),
1434 	DEVMETHOD(iicbus_callback, iicbus_null_callback),
1435 
1436 	DEVMETHOD_END
1437 };
1438 
1439 static driver_t cp2112iic_driver = {
1440 	"iichb",
1441 	cp2112iic_methods,
1442 	sizeof(struct cp2112iic_softc)
1443 };
1444 
1445 DRIVER_MODULE(cp2112iic, cp2112hid, cp2112iic_driver, NULL, NULL);
1446 MODULE_DEPEND(cp2112iic, cp2112hid, 1, 1, 1);
1447 MODULE_DEPEND(cp2112iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1448 MODULE_VERSION(cp2112iic, 1);
1449