xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision a8675cae2b38ae9c7abb981e4a11e21a757680e6)
102ac6454SAndrew Thompson /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson #include <sys/cdefs.h>
402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
502ac6454SAndrew Thompson #define UFOMA_HANDSFREE
602ac6454SAndrew Thompson /*-
702ac6454SAndrew Thompson  * Copyright (c) 2005, Takanori Watanabe
85d38a4d4SEd Schouten  * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
902ac6454SAndrew Thompson  * All rights reserved.
1002ac6454SAndrew Thompson  *
1102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1302ac6454SAndrew Thompson  * are met:
1402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2102ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2202ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2302ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2402ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2502ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2602ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2702ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2802ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2902ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3002ac6454SAndrew Thompson  * SUCH DAMAGE.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson /*-
3402ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3502ac6454SAndrew Thompson  * All rights reserved.
3602ac6454SAndrew Thompson  *
3702ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3802ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
3902ac6454SAndrew Thompson  * Carlstedt Research & Technology.
4002ac6454SAndrew Thompson  *
4102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
4202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
4302ac6454SAndrew Thompson  * are met:
4402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4902ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
5002ac6454SAndrew Thompson  *    must display the following acknowledgement:
5102ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
5202ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
5302ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
5402ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
5502ac6454SAndrew Thompson  *    from this software without specific prior written permission.
5602ac6454SAndrew Thompson  *
5702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5802ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5902ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
6002ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
6102ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6202ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6302ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6402ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6502ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6602ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6702ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
6802ac6454SAndrew Thompson  */
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson /*
7102ac6454SAndrew Thompson  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
7202ac6454SAndrew Thompson  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
7302ac6454SAndrew Thompson  */
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson /*
7602ac6454SAndrew Thompson  * TODO:
7702ac6454SAndrew Thompson  * - Implement a Call Device for modems without multiplexed commands.
7802ac6454SAndrew Thompson  */
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson /*
8102ac6454SAndrew Thompson  * NOTE: all function names beginning like "ufoma_cfg_" can only
8202ac6454SAndrew Thompson  * be called from within the config thread function !
8302ac6454SAndrew Thompson  */
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #include "usbdevs.h"
8602ac6454SAndrew Thompson #include <dev/usb/usb.h>
8702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
8802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
8902ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_debug
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
9402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
9502ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
9602ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
9702ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
9802ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
9902ac6454SAndrew Thompson #include <dev/usb/usb_parse.h>
10002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
10302ac6454SAndrew Thompson #include <sys/sysctl.h>
10402ac6454SAndrew Thompson #include <sys/sbuf.h>
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson typedef struct ufoma_mobile_acm_descriptor {
10702ac6454SAndrew Thompson 	uint8_t	bFunctionLength;
10802ac6454SAndrew Thompson 	uint8_t	bDescriptorType;
10902ac6454SAndrew Thompson 	uint8_t	bDescriptorSubtype;
11002ac6454SAndrew Thompson 	uint8_t	bType;
11102ac6454SAndrew Thompson 	uint8_t	bMode[1];
11202ac6454SAndrew Thompson } __packed usb2_mcpc_acm_descriptor;
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson #define	UISUBCLASS_MCPC 0x88
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson #define	UDESC_VS_INTERFACE 0x44
11702ac6454SAndrew Thompson #define	UDESCSUB_MCPC_ACM  0x11
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB1 0x1
12002ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB2 0x2
12102ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB5 0x5
12202ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB6 0x6
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_DEACTIVATED 0x0
12502ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_MODEM 0x1
12602ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_ATCOMMAND 0x2
12702ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_OBEX 0x60
12802ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_VENDOR1 0xc0
12902ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_VENDOR2 0xfe
13002ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_UNLINKED 0xff
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson #define	UMCPC_CM_MOBILE_ACM 0x0
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson #define	UMCPC_ACTIVATE_MODE 0x60
13502ac6454SAndrew Thompson #define	UMCPC_GET_MODETABLE 0x61
13602ac6454SAndrew Thompson #define	UMCPC_SET_LINK 0x62
13702ac6454SAndrew Thompson #define	UMCPC_CLEAR_LINK 0x63
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson #define	UMCPC_REQUEST_ACKNOWLEDGE 0x31
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson #define	UFOMA_MAX_TIMEOUT 15		/* standard says 10 seconds */
14202ac6454SAndrew Thompson #define	UFOMA_CMD_BUF_SIZE 64		/* bytes */
14302ac6454SAndrew Thompson 
14402ac6454SAndrew Thompson #define	UFOMA_BULK_BUF_SIZE 1024	/* bytes */
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson enum {
14702ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_INTR,
14802ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_READ,
14902ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_WRITE,
15002ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_MAX,
15102ac6454SAndrew Thompson };
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson enum {
15402ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_WRITE,
15502ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_READ,
15602ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_MAX,
15702ac6454SAndrew Thompson };
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson struct ufoma_softc {
160760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
161760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16202ac6454SAndrew Thompson 	struct cv sc_cv;
163deefe583SAndrew Thompson 	struct mtx sc_mtx;
16402ac6454SAndrew Thompson 
165760bc48eSAndrew Thompson 	struct usb_xfer *sc_ctrl_xfer[UFOMA_CTRL_ENDPT_MAX];
166760bc48eSAndrew Thompson 	struct usb_xfer *sc_bulk_xfer[UFOMA_BULK_ENDPT_MAX];
16702ac6454SAndrew Thompson 	uint8_t *sc_modetable;
16802ac6454SAndrew Thompson 	device_t sc_dev;
169760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	uint32_t sc_unit;
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	uint16_t sc_line;
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson 	uint8_t	sc_num_msg;
17602ac6454SAndrew Thompson 	uint8_t	sc_nobulk;
17702ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
17802ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_index;
17902ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
18002ac6454SAndrew Thompson 	uint8_t	sc_data_iface_index;
18102ac6454SAndrew Thompson 	uint8_t	sc_cm_cap;
18202ac6454SAndrew Thompson 	uint8_t	sc_acm_cap;
18302ac6454SAndrew Thompson 	uint8_t	sc_lsr;
18402ac6454SAndrew Thompson 	uint8_t	sc_msr;
18502ac6454SAndrew Thompson 	uint8_t	sc_modetoactivate;
18602ac6454SAndrew Thompson 	uint8_t	sc_currentmode;
18702ac6454SAndrew Thompson 	uint8_t	sc_name[16];
18802ac6454SAndrew Thompson };
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson /* prototypes */
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson static device_probe_t ufoma_probe;
19302ac6454SAndrew Thompson static device_attach_t ufoma_attach;
19402ac6454SAndrew Thompson static device_detach_t ufoma_detach;
19502ac6454SAndrew Thompson 
196e0a69b51SAndrew Thompson static usb_callback_t ufoma_ctrl_read_callback;
197e0a69b51SAndrew Thompson static usb_callback_t ufoma_ctrl_write_callback;
198e0a69b51SAndrew Thompson static usb_callback_t ufoma_intr_callback;
199e0a69b51SAndrew Thompson static usb_callback_t ufoma_bulk_write_callback;
200e0a69b51SAndrew Thompson static usb_callback_t ufoma_bulk_read_callback;
20102ac6454SAndrew Thompson 
202760bc48eSAndrew Thompson static void	*ufoma_get_intconf(struct usb_config_descriptor *,
203760bc48eSAndrew Thompson 		    struct usb_interface_descriptor *, uint8_t, uint8_t);
20402ac6454SAndrew Thompson static void	ufoma_cfg_link_state(struct ufoma_softc *);
20502ac6454SAndrew Thompson static void	ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
206760bc48eSAndrew Thompson static void	ufoma_cfg_open(struct ucom_softc *);
207760bc48eSAndrew Thompson static void	ufoma_cfg_close(struct ucom_softc *);
208760bc48eSAndrew Thompson static void	ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
209760bc48eSAndrew Thompson static void	ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
21002ac6454SAndrew Thompson 		    uint8_t *);
211760bc48eSAndrew Thompson static void	ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
212760bc48eSAndrew Thompson static void	ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
213760bc48eSAndrew Thompson static int	ufoma_pre_param(struct ucom_softc *, struct termios *);
214760bc48eSAndrew Thompson static void	ufoma_cfg_param(struct ucom_softc *, struct termios *);
21502ac6454SAndrew Thompson static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
216760bc48eSAndrew Thompson 		    struct usb_attach_arg *);
217760bc48eSAndrew Thompson static void	ufoma_start_read(struct ucom_softc *);
218760bc48eSAndrew Thompson static void	ufoma_stop_read(struct ucom_softc *);
219760bc48eSAndrew Thompson static void	ufoma_start_write(struct ucom_softc *);
220760bc48eSAndrew Thompson static void	ufoma_stop_write(struct ucom_softc *);
22102ac6454SAndrew Thompson 
22202ac6454SAndrew Thompson /*sysctl stuff*/
22302ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
22402ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
22502ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 
228760bc48eSAndrew Thompson static const struct usb_config
22902ac6454SAndrew Thompson 	ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_INTR] = {
23202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
23302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2354eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
236760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_cdc_notification),
2374eae601eSAndrew Thompson 		.callback = &ufoma_intr_callback,
23802ac6454SAndrew Thompson 	},
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_READ] = {
24102ac6454SAndrew Thompson 		.type = UE_CONTROL,
24202ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
24302ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
244760bc48eSAndrew Thompson 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
2454eae601eSAndrew Thompson 		.flags = {.short_xfer_ok = 1,},
2464eae601eSAndrew Thompson 		.callback = &ufoma_ctrl_read_callback,
2474eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
24802ac6454SAndrew Thompson 	},
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_WRITE] = {
25102ac6454SAndrew Thompson 		.type = UE_CONTROL,
25202ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
25302ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
254760bc48eSAndrew Thompson 		.bufsize = (sizeof(struct usb_device_request) + 1),
2554eae601eSAndrew Thompson 		.callback = &ufoma_ctrl_write_callback,
2564eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
25702ac6454SAndrew Thompson 	},
25802ac6454SAndrew Thompson };
25902ac6454SAndrew Thompson 
260760bc48eSAndrew Thompson static const struct usb_config
26102ac6454SAndrew Thompson 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 	[UFOMA_BULK_ENDPT_WRITE] = {
26402ac6454SAndrew Thompson 		.type = UE_BULK,
26502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26602ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2674eae601eSAndrew Thompson 		.bufsize = UFOMA_BULK_BUF_SIZE,
2684eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2694eae601eSAndrew Thompson 		.callback = &ufoma_bulk_write_callback,
27002ac6454SAndrew Thompson 	},
27102ac6454SAndrew Thompson 
27202ac6454SAndrew Thompson 	[UFOMA_BULK_ENDPT_READ] = {
27302ac6454SAndrew Thompson 		.type = UE_BULK,
27402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
27502ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2764eae601eSAndrew Thompson 		.bufsize = UFOMA_BULK_BUF_SIZE,
2774eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2784eae601eSAndrew Thompson 		.callback = &ufoma_bulk_read_callback,
27902ac6454SAndrew Thompson 	},
28002ac6454SAndrew Thompson };
28102ac6454SAndrew Thompson 
282760bc48eSAndrew Thompson static const struct ucom_callback ufoma_callback = {
28302ac6454SAndrew Thompson 	.usb2_com_cfg_get_status = &ufoma_cfg_get_status,
28402ac6454SAndrew Thompson 	.usb2_com_cfg_set_dtr = &ufoma_cfg_set_dtr,
28502ac6454SAndrew Thompson 	.usb2_com_cfg_set_rts = &ufoma_cfg_set_rts,
28602ac6454SAndrew Thompson 	.usb2_com_cfg_set_break = &ufoma_cfg_set_break,
28702ac6454SAndrew Thompson 	.usb2_com_cfg_param = &ufoma_cfg_param,
28802ac6454SAndrew Thompson 	.usb2_com_cfg_open = &ufoma_cfg_open,
28902ac6454SAndrew Thompson 	.usb2_com_cfg_close = &ufoma_cfg_close,
29002ac6454SAndrew Thompson 	.usb2_com_pre_param = &ufoma_pre_param,
29102ac6454SAndrew Thompson 	.usb2_com_start_read = &ufoma_start_read,
29202ac6454SAndrew Thompson 	.usb2_com_stop_read = &ufoma_stop_read,
29302ac6454SAndrew Thompson 	.usb2_com_start_write = &ufoma_start_write,
29402ac6454SAndrew Thompson 	.usb2_com_stop_write = &ufoma_stop_write,
29502ac6454SAndrew Thompson };
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson static device_method_t ufoma_methods[] = {
29802ac6454SAndrew Thompson 	/* Device methods */
29902ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ufoma_probe),
30002ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ufoma_attach),
30102ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ufoma_detach),
30202ac6454SAndrew Thompson 	{0, 0}
30302ac6454SAndrew Thompson };
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson static devclass_t ufoma_devclass;
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson static driver_t ufoma_driver = {
30802ac6454SAndrew Thompson 	.name = "ufoma",
30902ac6454SAndrew Thompson 	.methods = ufoma_methods,
31002ac6454SAndrew Thompson 	.size = sizeof(struct ufoma_softc),
31102ac6454SAndrew Thompson };
31202ac6454SAndrew Thompson 
3139aef556dSAndrew Thompson DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
31402ac6454SAndrew Thompson MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
31502ac6454SAndrew Thompson MODULE_DEPEND(ufoma, usb, 1, 1, 1);
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson static int
31802ac6454SAndrew Thompson ufoma_probe(device_t dev)
31902ac6454SAndrew Thompson {
320760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
321760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
322760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
32302ac6454SAndrew Thompson 	usb2_mcpc_acm_descriptor *mad;
32402ac6454SAndrew Thompson 
325f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
32602ac6454SAndrew Thompson 		return (ENXIO);
32702ac6454SAndrew Thompson 	}
32802ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
32902ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(uaa->device);
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	if ((id == NULL) ||
33202ac6454SAndrew Thompson 	    (cd == NULL) ||
33302ac6454SAndrew Thompson 	    (id->bInterfaceClass != UICLASS_CDC) ||
33402ac6454SAndrew Thompson 	    (id->bInterfaceSubClass != UISUBCLASS_MCPC)) {
33502ac6454SAndrew Thompson 		return (ENXIO);
33602ac6454SAndrew Thompson 	}
33702ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
33802ac6454SAndrew Thompson 	if (mad == NULL) {
33902ac6454SAndrew Thompson 		return (ENXIO);
34002ac6454SAndrew Thompson 	}
34102ac6454SAndrew Thompson #ifndef UFOMA_HANDSFREE
34202ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
34302ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
34402ac6454SAndrew Thompson 		return (ENXIO);
34502ac6454SAndrew Thompson 	}
34602ac6454SAndrew Thompson #endif
34702ac6454SAndrew Thompson 	return (0);
34802ac6454SAndrew Thompson }
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson static int
35102ac6454SAndrew Thompson ufoma_attach(device_t dev)
35202ac6454SAndrew Thompson {
353760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
35402ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
355760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
356760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
35702ac6454SAndrew Thompson 	struct sysctl_ctx_list *sctx;
35802ac6454SAndrew Thompson 	struct sysctl_oid *soid;
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	usb2_mcpc_acm_descriptor *mad;
36102ac6454SAndrew Thompson 	uint8_t elements;
36202ac6454SAndrew Thompson 	int32_t error;
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
36502ac6454SAndrew Thompson 	sc->sc_dev = dev;
36602ac6454SAndrew Thompson 	sc->sc_unit = device_get_unit(dev);
36702ac6454SAndrew Thompson 
368deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
36902ac6454SAndrew Thompson 	usb2_cv_init(&sc->sc_cv, "CWAIT");
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
37402ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	DPRINTF("\n");
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	/* setup control transfers */
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(uaa->device);
38102ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
38202ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
38302ac6454SAndrew Thompson 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
38602ac6454SAndrew Thompson 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
387deefe583SAndrew Thompson 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	if (error) {
39002ac6454SAndrew Thompson 		device_printf(dev, "allocating control USB "
39102ac6454SAndrew Thompson 		    "transfers failed!\n");
39202ac6454SAndrew Thompson 		goto detach;
39302ac6454SAndrew Thompson 	}
39402ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
39502ac6454SAndrew Thompson 	if (mad == NULL) {
39602ac6454SAndrew Thompson 		goto detach;
39702ac6454SAndrew Thompson 	}
39802ac6454SAndrew Thompson 	if (mad->bFunctionLength < sizeof(*mad)) {
39902ac6454SAndrew Thompson 		device_printf(dev, "invalid MAD descriptor\n");
40002ac6454SAndrew Thompson 		goto detach;
40102ac6454SAndrew Thompson 	}
40202ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
40302ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
40402ac6454SAndrew Thompson 		sc->sc_nobulk = 1;
40502ac6454SAndrew Thompson 	} else {
40602ac6454SAndrew Thompson 		sc->sc_nobulk = 0;
40702ac6454SAndrew Thompson 		if (ufoma_modem_setup(dev, sc, uaa)) {
40802ac6454SAndrew Thompson 			goto detach;
40902ac6454SAndrew Thompson 		}
41002ac6454SAndrew Thompson 	}
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson 	/* initialize mode variables */
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson 	if (sc->sc_modetable == NULL) {
41902ac6454SAndrew Thompson 		goto detach;
42002ac6454SAndrew Thompson 	}
42102ac6454SAndrew Thompson 	sc->sc_modetable[0] = (elements + 1);
42202ac6454SAndrew Thompson 	bcopy(mad->bMode, &sc->sc_modetable[1], elements);
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
42502ac6454SAndrew Thompson 	sc->sc_modetoactivate = mad->bMode[0];
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 	/* clear stall at first run, if any */
428deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
42902ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
43002ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
431deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
434deefe583SAndrew Thompson 	    &ufoma_callback, &sc->sc_mtx);
43502ac6454SAndrew Thompson 	if (error) {
43602ac6454SAndrew Thompson 		DPRINTF("usb2_com_attach failed\n");
43702ac6454SAndrew Thompson 		goto detach;
43802ac6454SAndrew Thompson 	}
43902ac6454SAndrew Thompson 	/*Sysctls*/
44002ac6454SAndrew Thompson 	sctx = device_get_sysctl_ctx(dev);
44102ac6454SAndrew Thompson 	soid = device_get_sysctl_tree(dev);
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
44402ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_support,
44502ac6454SAndrew Thompson 			"A", "Supporting port role");
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
44802ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_current,
44902ac6454SAndrew Thompson 			"A", "Current port role");
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
45202ac6454SAndrew Thompson 			CTLFLAG_RW|CTLTYPE_STRING, sc, 0, ufoma_sysctl_open,
45302ac6454SAndrew Thompson 			"A", "Mode to transit when port is opened");
45402ac6454SAndrew Thompson 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
45502ac6454SAndrew Thompson 			CTLFLAG_RD, &(sc->sc_ucom.sc_unit), 0,
45602ac6454SAndrew Thompson 			"Unit number as USB serial");
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 	return (0);			/* success */
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson detach:
46102ac6454SAndrew Thompson 	ufoma_detach(dev);
46202ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
46302ac6454SAndrew Thompson }
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson static int
46602ac6454SAndrew Thompson ufoma_detach(device_t dev)
46702ac6454SAndrew Thompson {
46802ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
47102ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
47202ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 	if (sc->sc_modetable) {
47502ac6454SAndrew Thompson 		free(sc->sc_modetable, M_USBDEV);
47602ac6454SAndrew Thompson 	}
477deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
47802ac6454SAndrew Thompson 	usb2_cv_destroy(&sc->sc_cv);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	return (0);
48102ac6454SAndrew Thompson }
48202ac6454SAndrew Thompson 
48302ac6454SAndrew Thompson static void *
484760bc48eSAndrew Thompson ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
48502ac6454SAndrew Thompson     uint8_t type, uint8_t subtype)
48602ac6454SAndrew Thompson {
487760bc48eSAndrew Thompson 	struct usb_descriptor *desc = (void *)id;
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 	while ((desc = usb2_desc_foreach(cd, desc))) {
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
49202ac6454SAndrew Thompson 			return (NULL);
49302ac6454SAndrew Thompson 		}
49402ac6454SAndrew Thompson 		if ((desc->bDescriptorType == type) &&
49502ac6454SAndrew Thompson 		    (desc->bDescriptorSubtype == subtype)) {
49602ac6454SAndrew Thompson 			break;
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson 	}
49902ac6454SAndrew Thompson 	return (desc);
50002ac6454SAndrew Thompson }
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson static void
50302ac6454SAndrew Thompson ufoma_cfg_link_state(struct ufoma_softc *sc)
50402ac6454SAndrew Thompson {
505760bc48eSAndrew Thompson 	struct usb_device_request req;
50602ac6454SAndrew Thompson 	int32_t error;
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
50902ac6454SAndrew Thompson 	req.bRequest = UMCPC_SET_LINK;
51002ac6454SAndrew Thompson 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
51102ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
51202ac6454SAndrew Thompson 	USETW(req.wLength, sc->sc_modetable[0]);
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
51502ac6454SAndrew Thompson 	    &req, sc->sc_modetable, 0, 1000);
51602ac6454SAndrew Thompson 
517deefe583SAndrew Thompson 	error = usb2_cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 	if (error) {
52002ac6454SAndrew Thompson 		DPRINTF("NO response\n");
52102ac6454SAndrew Thompson 	}
52202ac6454SAndrew Thompson }
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson static void
52502ac6454SAndrew Thompson ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
52602ac6454SAndrew Thompson {
527760bc48eSAndrew Thompson 	struct usb_device_request req;
52802ac6454SAndrew Thompson 	int32_t error;
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
53102ac6454SAndrew Thompson 	req.bRequest = UMCPC_ACTIVATE_MODE;
53202ac6454SAndrew Thompson 	USETW(req.wValue, state);
53302ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
53402ac6454SAndrew Thompson 	USETW(req.wLength, 0);
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
53702ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
53802ac6454SAndrew Thompson 
539deefe583SAndrew Thompson 	error = usb2_cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
54002ac6454SAndrew Thompson 	    (UFOMA_MAX_TIMEOUT * hz));
54102ac6454SAndrew Thompson 	if (error) {
54202ac6454SAndrew Thompson 		DPRINTF("No response\n");
54302ac6454SAndrew Thompson 	}
54402ac6454SAndrew Thompson }
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson static void
547760bc48eSAndrew Thompson ufoma_ctrl_read_callback(struct usb_xfer *xfer)
54802ac6454SAndrew Thompson {
54902ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
550760bc48eSAndrew Thompson 	struct usb_device_request req;
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
55302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
55402ac6454SAndrew Thompson tr_transferred:
55502ac6454SAndrew Thompson 		if (xfer->aframes != xfer->nframes) {
55602ac6454SAndrew Thompson 			goto tr_setup;
55702ac6454SAndrew Thompson 		}
55802ac6454SAndrew Thompson 		if (xfer->frlengths[1] > 0) {
55902ac6454SAndrew Thompson 			usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers + 1,
56002ac6454SAndrew Thompson 			    0, xfer->frlengths[1]);
56102ac6454SAndrew Thompson 		}
56202ac6454SAndrew Thompson 	case USB_ST_SETUP:
56302ac6454SAndrew Thompson tr_setup:
56402ac6454SAndrew Thompson 		if (sc->sc_num_msg) {
56502ac6454SAndrew Thompson 			sc->sc_num_msg--;
56602ac6454SAndrew Thompson 
56702ac6454SAndrew Thompson 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
56802ac6454SAndrew Thompson 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
56902ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
57002ac6454SAndrew Thompson 			USETW(req.wValue, 0);
57102ac6454SAndrew Thompson 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
57602ac6454SAndrew Thompson 			xfer->frlengths[1] = UFOMA_CMD_BUF_SIZE;
57702ac6454SAndrew Thompson 			xfer->nframes = 2;
57802ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
57902ac6454SAndrew Thompson 		}
58002ac6454SAndrew Thompson 		return;
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	default:			/* Error */
58302ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
58402ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
58702ac6454SAndrew Thompson 			return;
58802ac6454SAndrew Thompson 		} else {
58902ac6454SAndrew Thompson 			goto tr_setup;
59002ac6454SAndrew Thompson 		}
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 		goto tr_transferred;
59302ac6454SAndrew Thompson 	}
59402ac6454SAndrew Thompson }
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson static void
597760bc48eSAndrew Thompson ufoma_ctrl_write_callback(struct usb_xfer *xfer)
59802ac6454SAndrew Thompson {
59902ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
600760bc48eSAndrew Thompson 	struct usb_device_request req;
60102ac6454SAndrew Thompson 	uint32_t actlen;
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
60402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
60502ac6454SAndrew Thompson tr_transferred:
60602ac6454SAndrew Thompson 	case USB_ST_SETUP:
60702ac6454SAndrew Thompson tr_setup:
60802ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers + 1,
60902ac6454SAndrew Thompson 		    0, 1, &actlen)) {
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
61202ac6454SAndrew Thompson 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
61302ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
61402ac6454SAndrew Thompson 			USETW(req.wValue, 0);
61502ac6454SAndrew Thompson 			USETW(req.wLength, 1);
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
61802ac6454SAndrew Thompson 
61902ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
62002ac6454SAndrew Thompson 			xfer->frlengths[1] = 1;
62102ac6454SAndrew Thompson 			xfer->nframes = 2;
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
62402ac6454SAndrew Thompson 		}
62502ac6454SAndrew Thompson 		return;
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson 	default:			/* Error */
62802ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
62902ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
63202ac6454SAndrew Thompson 			return;
63302ac6454SAndrew Thompson 		} else {
63402ac6454SAndrew Thompson 			goto tr_setup;
63502ac6454SAndrew Thompson 		}
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 		goto tr_transferred;
63802ac6454SAndrew Thompson 	}
63902ac6454SAndrew Thompson }
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson static void
642760bc48eSAndrew Thompson ufoma_intr_callback(struct usb_xfer *xfer)
64302ac6454SAndrew Thompson {
64402ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
645760bc48eSAndrew Thompson 	struct usb_cdc_notification pkt;
64602ac6454SAndrew Thompson 	uint16_t wLen;
64702ac6454SAndrew Thompson 	uint16_t temp;
64802ac6454SAndrew Thompson 	uint8_t mstatus;
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
65102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
65202ac6454SAndrew Thompson 		if (xfer->actlen < 8) {
65302ac6454SAndrew Thompson 			DPRINTF("too short message\n");
65402ac6454SAndrew Thompson 			goto tr_setup;
65502ac6454SAndrew Thompson 		}
65602ac6454SAndrew Thompson 		if (xfer->actlen > sizeof(pkt)) {
65702ac6454SAndrew Thompson 			DPRINTF("truncating message\n");
65802ac6454SAndrew Thompson 			xfer->actlen = sizeof(pkt);
65902ac6454SAndrew Thompson 		}
66002ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, &pkt, xfer->actlen);
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 		xfer->actlen -= 8;
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 		wLen = UGETW(pkt.wLength);
66502ac6454SAndrew Thompson 		if (xfer->actlen > wLen) {
66602ac6454SAndrew Thompson 			xfer->actlen = wLen;
66702ac6454SAndrew Thompson 		}
66802ac6454SAndrew Thompson 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
66902ac6454SAndrew Thompson 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
67002ac6454SAndrew Thompson 			temp = UGETW(pkt.wValue);
67102ac6454SAndrew Thompson 			sc->sc_currentmode = (temp >> 8);
67202ac6454SAndrew Thompson 			if (!(temp & 0xff)) {
67302ac6454SAndrew Thompson 				DPRINTF("Mode change failed!\n");
67402ac6454SAndrew Thompson 			}
67502ac6454SAndrew Thompson 			usb2_cv_signal(&sc->sc_cv);
67602ac6454SAndrew Thompson 		}
67702ac6454SAndrew Thompson 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
67802ac6454SAndrew Thompson 			goto tr_setup;
67902ac6454SAndrew Thompson 		}
68002ac6454SAndrew Thompson 		switch (pkt.bNotification) {
68102ac6454SAndrew Thompson 		case UCDC_N_RESPONSE_AVAILABLE:
68202ac6454SAndrew Thompson 			if (!(sc->sc_nobulk)) {
68302ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
68402ac6454SAndrew Thompson 				break;
68502ac6454SAndrew Thompson 			}
68602ac6454SAndrew Thompson 			if (sc->sc_num_msg != 0xFF) {
68702ac6454SAndrew Thompson 				sc->sc_num_msg++;
68802ac6454SAndrew Thompson 			}
68902ac6454SAndrew Thompson 			usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
69002ac6454SAndrew Thompson 			break;
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 		case UCDC_N_SERIAL_STATE:
69302ac6454SAndrew Thompson 			if (sc->sc_nobulk) {
69402ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
69502ac6454SAndrew Thompson 				break;
69602ac6454SAndrew Thompson 			}
69702ac6454SAndrew Thompson 			/*
69802ac6454SAndrew Thompson 		         * Set the serial state in ucom driver based on
69902ac6454SAndrew Thompson 		         * the bits from the notify message
70002ac6454SAndrew Thompson 		         */
70102ac6454SAndrew Thompson 			if (xfer->actlen < 2) {
70202ac6454SAndrew Thompson 				DPRINTF("invalid notification "
70302ac6454SAndrew Thompson 				    "length, %d bytes!\n", xfer->actlen);
70402ac6454SAndrew Thompson 				break;
70502ac6454SAndrew Thompson 			}
70602ac6454SAndrew Thompson 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
70702ac6454SAndrew Thompson 			    pkt.data[0], pkt.data[1]);
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 			/* currently, lsr is always zero. */
71002ac6454SAndrew Thompson 			sc->sc_lsr = 0;
71102ac6454SAndrew Thompson 			sc->sc_msr = 0;
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 			mstatus = pkt.data[0];
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_RI) {
71602ac6454SAndrew Thompson 				sc->sc_msr |= SER_RI;
71702ac6454SAndrew Thompson 			}
71802ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DSR) {
71902ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
72002ac6454SAndrew Thompson 			}
72102ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DCD) {
72202ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
72302ac6454SAndrew Thompson 			}
72402ac6454SAndrew Thompson 			usb2_com_status_change(&sc->sc_ucom);
72502ac6454SAndrew Thompson 			break;
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson 		default:
72802ac6454SAndrew Thompson 			break;
72902ac6454SAndrew Thompson 		}
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson 	case USB_ST_SETUP:
73202ac6454SAndrew Thompson tr_setup:
73302ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
73402ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
73502ac6454SAndrew Thompson 		return;
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 	default:			/* Error */
73802ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
73902ac6454SAndrew Thompson 			/* try to clear stall first */
74002ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
74102ac6454SAndrew Thompson 			goto tr_setup;
74202ac6454SAndrew Thompson 		}
74302ac6454SAndrew Thompson 		return;
74402ac6454SAndrew Thompson 	}
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
748760bc48eSAndrew Thompson ufoma_bulk_write_callback(struct usb_xfer *xfer)
74902ac6454SAndrew Thompson {
75002ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
75102ac6454SAndrew Thompson 	uint32_t actlen;
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
75402ac6454SAndrew Thompson 	case USB_ST_SETUP:
75502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
75602ac6454SAndrew Thompson tr_setup:
75702ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
75802ac6454SAndrew Thompson 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
75902ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
76002ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
76102ac6454SAndrew Thompson 		}
76202ac6454SAndrew Thompson 		return;
76302ac6454SAndrew Thompson 
76402ac6454SAndrew Thompson 	default:			/* Error */
76502ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
76602ac6454SAndrew Thompson 			/* try to clear stall first */
76702ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
76802ac6454SAndrew Thompson 			goto tr_setup;
76902ac6454SAndrew Thompson 		}
77002ac6454SAndrew Thompson 		return;
77102ac6454SAndrew Thompson 	}
77202ac6454SAndrew Thompson }
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson static void
775760bc48eSAndrew Thompson ufoma_bulk_read_callback(struct usb_xfer *xfer)
77602ac6454SAndrew Thompson {
77702ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
77802ac6454SAndrew Thompson 
77902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
78002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
78102ac6454SAndrew Thompson 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0,
78202ac6454SAndrew Thompson 		    xfer->actlen);
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	case USB_ST_SETUP:
78502ac6454SAndrew Thompson tr_setup:
78602ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
78702ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
78802ac6454SAndrew Thompson 		return;
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	default:			/* Error */
79102ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
79202ac6454SAndrew Thompson 			/* try to clear stall first */
79302ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
79402ac6454SAndrew Thompson 			goto tr_setup;
79502ac6454SAndrew Thompson 		}
79602ac6454SAndrew Thompson 		return;
79702ac6454SAndrew Thompson 	}
79802ac6454SAndrew Thompson }
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson static void
801760bc48eSAndrew Thompson ufoma_cfg_open(struct ucom_softc *ucom)
80202ac6454SAndrew Thompson {
80302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 	/* empty input queue */
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	if (sc->sc_num_msg != 0xFF) {
80802ac6454SAndrew Thompson 		sc->sc_num_msg++;
80902ac6454SAndrew Thompson 	}
81002ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
81102ac6454SAndrew Thompson 		ufoma_cfg_link_state(sc);
81202ac6454SAndrew Thompson 	}
81302ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
81402ac6454SAndrew Thompson 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
81502ac6454SAndrew Thompson 	}
81602ac6454SAndrew Thompson }
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson static void
819760bc48eSAndrew Thompson ufoma_cfg_close(struct ucom_softc *ucom)
82002ac6454SAndrew Thompson {
82102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
82402ac6454SAndrew Thompson }
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson static void
827760bc48eSAndrew Thompson ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
82802ac6454SAndrew Thompson {
82902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
830760bc48eSAndrew Thompson 	struct usb_device_request req;
83102ac6454SAndrew Thompson 	uint16_t wValue;
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
83402ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
83502ac6454SAndrew Thompson 		return;
83602ac6454SAndrew Thompson 	}
83702ac6454SAndrew Thompson 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
83802ac6454SAndrew Thompson 		return;
83902ac6454SAndrew Thompson 	}
84002ac6454SAndrew Thompson 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
84302ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
84402ac6454SAndrew Thompson 	USETW(req.wValue, wValue);
84502ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
84602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
84702ac6454SAndrew Thompson 	USETW(req.wLength, 0);
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
85002ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
85102ac6454SAndrew Thompson }
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson static void
854760bc48eSAndrew Thompson ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
85502ac6454SAndrew Thompson {
85602ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
85902ac6454SAndrew Thompson 	*msr = sc->sc_msr;
86002ac6454SAndrew Thompson }
86102ac6454SAndrew Thompson 
86202ac6454SAndrew Thompson static void
86302ac6454SAndrew Thompson ufoma_cfg_set_line_state(struct ufoma_softc *sc)
86402ac6454SAndrew Thompson {
865760bc48eSAndrew Thompson 	struct usb_device_request req;
86602ac6454SAndrew Thompson 
86702ac6454SAndrew Thompson 	/* Don't send line state emulation request for OBEX port */
86802ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
86902ac6454SAndrew Thompson 		return;
87002ac6454SAndrew Thompson 	}
87102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
87202ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
87302ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
87402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
87502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
87602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
87702ac6454SAndrew Thompson 
87802ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
87902ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
88002ac6454SAndrew Thompson }
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson static void
883760bc48eSAndrew Thompson ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
88402ac6454SAndrew Thompson {
88502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
88802ac6454SAndrew Thompson 		return;
88902ac6454SAndrew Thompson 	}
89002ac6454SAndrew Thompson 	if (onoff)
89102ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
89202ac6454SAndrew Thompson 	else
89302ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
89402ac6454SAndrew Thompson 
89502ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
89602ac6454SAndrew Thompson }
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson static void
899760bc48eSAndrew Thompson ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
90002ac6454SAndrew Thompson {
90102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
90402ac6454SAndrew Thompson 		return;
90502ac6454SAndrew Thompson 	}
90602ac6454SAndrew Thompson 	if (onoff)
90702ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
90802ac6454SAndrew Thompson 	else
90902ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
91202ac6454SAndrew Thompson }
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson static int
915760bc48eSAndrew Thompson ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
91602ac6454SAndrew Thompson {
91702ac6454SAndrew Thompson 	return (0);			/* we accept anything */
91802ac6454SAndrew Thompson }
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson static void
921760bc48eSAndrew Thompson ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
92202ac6454SAndrew Thompson {
92302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
924760bc48eSAndrew Thompson 	struct usb_device_request req;
925760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
92802ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
92902ac6454SAndrew Thompson 		return;
93002ac6454SAndrew Thompson 	}
93102ac6454SAndrew Thompson 	DPRINTF("\n");
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
93802ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
93902ac6454SAndrew Thompson 	} else {
94002ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
94102ac6454SAndrew Thompson 	}
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
94402ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
94502ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
94602ac6454SAndrew Thompson 		} else {
94702ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
94802ac6454SAndrew Thompson 		}
94902ac6454SAndrew Thompson 	} else {
95002ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
95102ac6454SAndrew Thompson 	}
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
95402ac6454SAndrew Thompson 	case CS5:
95502ac6454SAndrew Thompson 		ls.bDataBits = 5;
95602ac6454SAndrew Thompson 		break;
95702ac6454SAndrew Thompson 	case CS6:
95802ac6454SAndrew Thompson 		ls.bDataBits = 6;
95902ac6454SAndrew Thompson 		break;
96002ac6454SAndrew Thompson 	case CS7:
96102ac6454SAndrew Thompson 		ls.bDataBits = 7;
96202ac6454SAndrew Thompson 		break;
96302ac6454SAndrew Thompson 	case CS8:
96402ac6454SAndrew Thompson 		ls.bDataBits = 8;
96502ac6454SAndrew Thompson 		break;
96602ac6454SAndrew Thompson 	}
96702ac6454SAndrew Thompson 
96802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
96902ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
97002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
97102ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
97202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
97302ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
97602ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
97702ac6454SAndrew Thompson }
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson static int
98002ac6454SAndrew Thompson ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
981760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
98202ac6454SAndrew Thompson {
983760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
984760bc48eSAndrew Thompson 	struct usb_cdc_acm_descriptor *acm;
985760bc48eSAndrew Thompson 	struct usb_cdc_cm_descriptor *cmd;
986760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
987760bc48eSAndrew Thompson 	struct usb_interface *iface;
98802ac6454SAndrew Thompson 	uint8_t i;
98902ac6454SAndrew Thompson 	int32_t error;
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(uaa->device);
99202ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 	if ((cmd == NULL) ||
99702ac6454SAndrew Thompson 	    (cmd->bLength < sizeof(*cmd))) {
99802ac6454SAndrew Thompson 		return (EINVAL);
99902ac6454SAndrew Thompson 	}
100002ac6454SAndrew Thompson 	sc->sc_cm_cap = cmd->bmCapabilities;
100102ac6454SAndrew Thompson 	sc->sc_data_iface_no = cmd->bDataInterface;
100202ac6454SAndrew Thompson 
100302ac6454SAndrew Thompson 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson 	if ((acm == NULL) ||
100602ac6454SAndrew Thompson 	    (acm->bLength < sizeof(*acm))) {
100702ac6454SAndrew Thompson 		return (EINVAL);
100802ac6454SAndrew Thompson 	}
100902ac6454SAndrew Thompson 	sc->sc_acm_cap = acm->bmCapabilities;
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson 	device_printf(dev, "data interface %d, has %sCM over data, "
101202ac6454SAndrew Thompson 	    "has %sbreak\n",
101302ac6454SAndrew Thompson 	    sc->sc_data_iface_no,
101402ac6454SAndrew Thompson 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
101502ac6454SAndrew Thompson 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	/* get the data interface too */
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	for (i = 0;; i++) {
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 		iface = usb2_get_iface(uaa->device, i);
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 		if (iface) {
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 			id = usb2_get_interface_descriptor(iface);
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
102802ac6454SAndrew Thompson 				sc->sc_data_iface_index = i;
102902ac6454SAndrew Thompson 				usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
103002ac6454SAndrew Thompson 				break;
103102ac6454SAndrew Thompson 			}
103202ac6454SAndrew Thompson 		} else {
103302ac6454SAndrew Thompson 			device_printf(dev, "no data interface!\n");
103402ac6454SAndrew Thompson 			return (EINVAL);
103502ac6454SAndrew Thompson 		}
103602ac6454SAndrew Thompson 	}
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
103902ac6454SAndrew Thompson 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1040deefe583SAndrew Thompson 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 	if (error) {
104302ac6454SAndrew Thompson 		device_printf(dev, "allocating BULK USB "
104402ac6454SAndrew Thompson 		    "transfers failed!\n");
104502ac6454SAndrew Thompson 		return (EINVAL);
104602ac6454SAndrew Thompson 	}
104702ac6454SAndrew Thompson 	return (0);
104802ac6454SAndrew Thompson }
104902ac6454SAndrew Thompson 
105002ac6454SAndrew Thompson static void
1051760bc48eSAndrew Thompson ufoma_start_read(struct ucom_softc *ucom)
105202ac6454SAndrew Thompson {
105302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	/* start interrupt transfer */
105602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
105702ac6454SAndrew Thompson 
105802ac6454SAndrew Thompson 	/* start data transfer */
105902ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
106002ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
106102ac6454SAndrew Thompson 	} else {
106202ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
106302ac6454SAndrew Thompson 	}
106402ac6454SAndrew Thompson }
106502ac6454SAndrew Thompson 
106602ac6454SAndrew Thompson static void
1067760bc48eSAndrew Thompson ufoma_stop_read(struct ucom_softc *ucom)
106802ac6454SAndrew Thompson {
106902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	/* stop interrupt transfer */
107202ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
107302ac6454SAndrew Thompson 
107402ac6454SAndrew Thompson 	/* stop data transfer */
107502ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
107602ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
107702ac6454SAndrew Thompson 	} else {
107802ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
107902ac6454SAndrew Thompson 	}
108002ac6454SAndrew Thompson }
108102ac6454SAndrew Thompson 
108202ac6454SAndrew Thompson static void
1083760bc48eSAndrew Thompson ufoma_start_write(struct ucom_softc *ucom)
108402ac6454SAndrew Thompson {
108502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
108802ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
108902ac6454SAndrew Thompson 	} else {
109002ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
109102ac6454SAndrew Thompson 	}
109202ac6454SAndrew Thompson }
109302ac6454SAndrew Thompson 
109402ac6454SAndrew Thompson static void
1095760bc48eSAndrew Thompson ufoma_stop_write(struct ucom_softc *ucom)
109602ac6454SAndrew Thompson {
109702ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
110002ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
110102ac6454SAndrew Thompson 	} else {
110202ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
110302ac6454SAndrew Thompson 	}
110402ac6454SAndrew Thompson }
110502ac6454SAndrew Thompson 
1106a8675caeSAndrew Thompson static struct umcpc_modetostr_tab{
110702ac6454SAndrew Thompson 	int mode;
110802ac6454SAndrew Thompson 	char *str;
110902ac6454SAndrew Thompson }umcpc_modetostr_tab[]={
111002ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
111102ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_MODEM, "modem"},
111202ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
111302ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_OBEX, "obex"},
111402ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
111502ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
111602ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
111702ac6454SAndrew Thompson 	{0, NULL}
111802ac6454SAndrew Thompson };
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson static char *ufoma_mode_to_str(int mode)
112102ac6454SAndrew Thompson {
112202ac6454SAndrew Thompson 	int i;
112302ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
112402ac6454SAndrew Thompson 		if(umcpc_modetostr_tab[i].mode == mode){
112502ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].str;
112602ac6454SAndrew Thompson 		}
112702ac6454SAndrew Thompson 	}
112802ac6454SAndrew Thompson 	return NULL;
112902ac6454SAndrew Thompson }
113002ac6454SAndrew Thompson 
113102ac6454SAndrew Thompson static int ufoma_str_to_mode(char *str)
113202ac6454SAndrew Thompson {
113302ac6454SAndrew Thompson 	int i;
113402ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
113502ac6454SAndrew Thompson 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
113602ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].mode;
113702ac6454SAndrew Thompson 		}
113802ac6454SAndrew Thompson 	}
113902ac6454SAndrew Thompson 	return -1;
114002ac6454SAndrew Thompson }
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
114302ac6454SAndrew Thompson {
114402ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
114502ac6454SAndrew Thompson 	struct sbuf sb;
114602ac6454SAndrew Thompson 	int i;
114702ac6454SAndrew Thompson 	char *mode;
114802ac6454SAndrew Thompson 
114902ac6454SAndrew Thompson 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
115002ac6454SAndrew Thompson 	for(i = 1; i < sc->sc_modetable[0]; i++){
115102ac6454SAndrew Thompson 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
115202ac6454SAndrew Thompson 		if(mode !=NULL){
115302ac6454SAndrew Thompson 			sbuf_cat(&sb, mode);
115402ac6454SAndrew Thompson 		}else{
115502ac6454SAndrew Thompson 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
115602ac6454SAndrew Thompson 		}
115702ac6454SAndrew Thompson 		if(i < (sc->sc_modetable[0]-1))
115802ac6454SAndrew Thompson 			sbuf_cat(&sb, ",");
115902ac6454SAndrew Thompson 	}
116002ac6454SAndrew Thompson 	sbuf_trim(&sb);
116102ac6454SAndrew Thompson 	sbuf_finish(&sb);
116202ac6454SAndrew Thompson 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
116302ac6454SAndrew Thompson 	sbuf_delete(&sb);
116402ac6454SAndrew Thompson 
116502ac6454SAndrew Thompson 	return 0;
116602ac6454SAndrew Thompson }
116702ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
116802ac6454SAndrew Thompson {
116902ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
117002ac6454SAndrew Thompson 	char *mode;
117102ac6454SAndrew Thompson 	char subbuf[]="(XXX)";
117202ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_currentmode);
117302ac6454SAndrew Thompson 	if(!mode){
117402ac6454SAndrew Thompson 		mode = subbuf;
117502ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
117602ac6454SAndrew Thompson 	}
117702ac6454SAndrew Thompson 	sysctl_handle_string(oidp, mode, strlen(mode), req);
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson 	return 0;
118002ac6454SAndrew Thompson 
118102ac6454SAndrew Thompson }
118202ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
118302ac6454SAndrew Thompson {
118402ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
118502ac6454SAndrew Thompson 	char *mode;
118602ac6454SAndrew Thompson 	char subbuf[40];
118702ac6454SAndrew Thompson 	int newmode;
118802ac6454SAndrew Thompson 	int error;
118902ac6454SAndrew Thompson 	int i;
119002ac6454SAndrew Thompson 
119102ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
119202ac6454SAndrew Thompson 	if(mode){
119302ac6454SAndrew Thompson 		strncpy(subbuf, mode, sizeof(subbuf));
119402ac6454SAndrew Thompson 	}else{
119502ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
119602ac6454SAndrew Thompson 	}
119702ac6454SAndrew Thompson 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
119802ac6454SAndrew Thompson 	if(error != 0 || req->newptr == NULL){
119902ac6454SAndrew Thompson 		return error;
120002ac6454SAndrew Thompson 	}
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
120302ac6454SAndrew Thompson 		return EINVAL;
120402ac6454SAndrew Thompson 	}
120502ac6454SAndrew Thompson 
120602ac6454SAndrew Thompson 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
120702ac6454SAndrew Thompson 		if(sc->sc_modetable[i] == newmode){
120802ac6454SAndrew Thompson 			sc->sc_modetoactivate = newmode;
120902ac6454SAndrew Thompson 			return 0;
121002ac6454SAndrew Thompson 		}
121102ac6454SAndrew Thompson 	}
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson 	return EINVAL;
121402ac6454SAndrew Thompson }
1215