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