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