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