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