xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 02ac6454880b59bbc5f3f74dffaffa90b30adc8b)
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
802ac6454SAndrew Thompson  * 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 {
16002ac6454SAndrew Thompson 	struct usb2_com_super_softc sc_super_ucom;
16102ac6454SAndrew Thompson 	struct usb2_com_softc sc_ucom;
16202ac6454SAndrew Thompson 	struct cv sc_cv;
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	struct usb2_xfer *sc_ctrl_xfer[UFOMA_CTRL_ENDPT_MAX];
16502ac6454SAndrew Thompson 	struct usb2_xfer *sc_bulk_xfer[UFOMA_BULK_ENDPT_MAX];
16602ac6454SAndrew Thompson 	uint8_t *sc_modetable;
16702ac6454SAndrew Thompson 	device_t sc_dev;
16802ac6454SAndrew Thompson 	struct usb2_device *sc_udev;
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	uint32_t sc_unit;
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson 	uint16_t sc_line;
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson 	uint8_t	sc_num_msg;
17502ac6454SAndrew Thompson 	uint8_t	sc_nobulk;
17602ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
17702ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_index;
17802ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
17902ac6454SAndrew Thompson 	uint8_t	sc_data_iface_index;
18002ac6454SAndrew Thompson 	uint8_t	sc_cm_cap;
18102ac6454SAndrew Thompson 	uint8_t	sc_acm_cap;
18202ac6454SAndrew Thompson 	uint8_t	sc_lsr;
18302ac6454SAndrew Thompson 	uint8_t	sc_msr;
18402ac6454SAndrew Thompson 	uint8_t	sc_modetoactivate;
18502ac6454SAndrew Thompson 	uint8_t	sc_currentmode;
18602ac6454SAndrew Thompson 	uint8_t	sc_name[16];
18702ac6454SAndrew Thompson };
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson /* prototypes */
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson static device_probe_t ufoma_probe;
19202ac6454SAndrew Thompson static device_attach_t ufoma_attach;
19302ac6454SAndrew Thompson static device_detach_t ufoma_detach;
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson static usb2_callback_t ufoma_ctrl_read_callback;
19602ac6454SAndrew Thompson static usb2_callback_t ufoma_ctrl_write_callback;
19702ac6454SAndrew Thompson static usb2_callback_t ufoma_intr_callback;
19802ac6454SAndrew Thompson static usb2_callback_t ufoma_bulk_write_callback;
19902ac6454SAndrew Thompson static usb2_callback_t ufoma_bulk_read_callback;
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson static void	*ufoma_get_intconf(struct usb2_config_descriptor *,
20202ac6454SAndrew Thompson 		    struct usb2_interface_descriptor *, uint8_t, uint8_t);
20302ac6454SAndrew Thompson static void	ufoma_cfg_link_state(struct ufoma_softc *);
20402ac6454SAndrew Thompson static void	ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
20502ac6454SAndrew Thompson static void	ufoma_cfg_open(struct usb2_com_softc *);
20602ac6454SAndrew Thompson static void	ufoma_cfg_close(struct usb2_com_softc *);
20702ac6454SAndrew Thompson static void	ufoma_cfg_set_break(struct usb2_com_softc *, uint8_t);
20802ac6454SAndrew Thompson static void	ufoma_cfg_get_status(struct usb2_com_softc *, uint8_t *,
20902ac6454SAndrew Thompson 		    uint8_t *);
21002ac6454SAndrew Thompson static void	ufoma_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
21102ac6454SAndrew Thompson static void	ufoma_cfg_set_rts(struct usb2_com_softc *, uint8_t);
21202ac6454SAndrew Thompson static int	ufoma_pre_param(struct usb2_com_softc *, struct termios *);
21302ac6454SAndrew Thompson static void	ufoma_cfg_param(struct usb2_com_softc *, struct termios *);
21402ac6454SAndrew Thompson static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
21502ac6454SAndrew Thompson 		    struct usb2_attach_arg *);
21602ac6454SAndrew Thompson static void	ufoma_start_read(struct usb2_com_softc *);
21702ac6454SAndrew Thompson static void	ufoma_stop_read(struct usb2_com_softc *);
21802ac6454SAndrew Thompson static void	ufoma_start_write(struct usb2_com_softc *);
21902ac6454SAndrew Thompson static void	ufoma_stop_write(struct usb2_com_softc *);
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson /*sysctl stuff*/
22202ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
22302ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
22402ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson static const struct usb2_config
22802ac6454SAndrew Thompson 	ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_INTR] = {
23102ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
23202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
23402ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
23502ac6454SAndrew Thompson 		.mh.bufsize = sizeof(struct usb2_cdc_notification),
23602ac6454SAndrew Thompson 		.mh.callback = &ufoma_intr_callback,
23702ac6454SAndrew Thompson 	},
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_READ] = {
24002ac6454SAndrew Thompson 		.type = UE_CONTROL,
24102ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
24202ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
24302ac6454SAndrew Thompson 		.mh.bufsize = (sizeof(struct usb2_device_request) + UFOMA_CMD_BUF_SIZE),
24402ac6454SAndrew Thompson 		.mh.flags = {.short_xfer_ok = 1,},
24502ac6454SAndrew Thompson 		.mh.callback = &ufoma_ctrl_read_callback,
24602ac6454SAndrew Thompson 		.mh.timeout = 1000,	/* 1 second */
24702ac6454SAndrew Thompson 	},
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_WRITE] = {
25002ac6454SAndrew Thompson 		.type = UE_CONTROL,
25102ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
25202ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
25302ac6454SAndrew Thompson 		.mh.bufsize = (sizeof(struct usb2_device_request) + 1),
25402ac6454SAndrew Thompson 		.mh.flags = {},
25502ac6454SAndrew Thompson 		.mh.callback = &ufoma_ctrl_write_callback,
25602ac6454SAndrew Thompson 		.mh.timeout = 1000,	/* 1 second */
25702ac6454SAndrew Thompson 	},
25802ac6454SAndrew Thompson };
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson static const struct usb2_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,
26702ac6454SAndrew Thompson 		.mh.bufsize = UFOMA_BULK_BUF_SIZE,
26802ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
26902ac6454SAndrew Thompson 		.mh.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,
27602ac6454SAndrew Thompson 		.mh.bufsize = UFOMA_BULK_BUF_SIZE,
27702ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
27802ac6454SAndrew Thompson 		.mh.callback = &ufoma_bulk_read_callback,
27902ac6454SAndrew Thompson 	},
28002ac6454SAndrew Thompson };
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson static const struct usb2_com_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 
31302ac6454SAndrew Thompson DRIVER_MODULE(ufoma, ushub, 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 {
32002ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
32102ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
32202ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
32302ac6454SAndrew Thompson 	usb2_mcpc_acm_descriptor *mad;
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	if (uaa->usb2_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 {
35302ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
35402ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
35502ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
35602ac6454SAndrew Thompson 	struct usb2_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 
36802ac6454SAndrew Thompson 	usb2_cv_init(&sc->sc_cv, "CWAIT");
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
37302ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	DPRINTF("\n");
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	/* setup control transfers */
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(uaa->device);
38002ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
38102ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
38202ac6454SAndrew Thompson 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
38502ac6454SAndrew Thompson 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
38602ac6454SAndrew Thompson 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &Giant);
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 	if (error) {
38902ac6454SAndrew Thompson 		device_printf(dev, "allocating control USB "
39002ac6454SAndrew Thompson 		    "transfers failed!\n");
39102ac6454SAndrew Thompson 		goto detach;
39202ac6454SAndrew Thompson 	}
39302ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
39402ac6454SAndrew Thompson 	if (mad == NULL) {
39502ac6454SAndrew Thompson 		goto detach;
39602ac6454SAndrew Thompson 	}
39702ac6454SAndrew Thompson 	if (mad->bFunctionLength < sizeof(*mad)) {
39802ac6454SAndrew Thompson 		device_printf(dev, "invalid MAD descriptor\n");
39902ac6454SAndrew Thompson 		goto detach;
40002ac6454SAndrew Thompson 	}
40102ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
40202ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
40302ac6454SAndrew Thompson 		sc->sc_nobulk = 1;
40402ac6454SAndrew Thompson 	} else {
40502ac6454SAndrew Thompson 		sc->sc_nobulk = 0;
40602ac6454SAndrew Thompson 		if (ufoma_modem_setup(dev, sc, uaa)) {
40702ac6454SAndrew Thompson 			goto detach;
40802ac6454SAndrew Thompson 		}
40902ac6454SAndrew Thompson 	}
41002ac6454SAndrew Thompson 
41102ac6454SAndrew Thompson 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson 	/* initialize mode variables */
41402ac6454SAndrew Thompson 
41502ac6454SAndrew Thompson 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	if (sc->sc_modetable == NULL) {
41802ac6454SAndrew Thompson 		goto detach;
41902ac6454SAndrew Thompson 	}
42002ac6454SAndrew Thompson 	sc->sc_modetable[0] = (elements + 1);
42102ac6454SAndrew Thompson 	bcopy(mad->bMode, &sc->sc_modetable[1], elements);
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
42402ac6454SAndrew Thompson 	sc->sc_modetoactivate = mad->bMode[0];
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 	/* clear stall at first run, if any */
42702ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
42802ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
43102ac6454SAndrew Thompson 	    &ufoma_callback, &Giant);
43202ac6454SAndrew Thompson 	if (error) {
43302ac6454SAndrew Thompson 		DPRINTF("usb2_com_attach failed\n");
43402ac6454SAndrew Thompson 		goto detach;
43502ac6454SAndrew Thompson 	}
43602ac6454SAndrew Thompson 	/*Sysctls*/
43702ac6454SAndrew Thompson 	sctx = device_get_sysctl_ctx(dev);
43802ac6454SAndrew Thompson 	soid = device_get_sysctl_tree(dev);
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
44102ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_support,
44202ac6454SAndrew Thompson 			"A", "Supporting port role");
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
44502ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_current,
44602ac6454SAndrew Thompson 			"A", "Current port role");
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
44902ac6454SAndrew Thompson 			CTLFLAG_RW|CTLTYPE_STRING, sc, 0, ufoma_sysctl_open,
45002ac6454SAndrew Thompson 			"A", "Mode to transit when port is opened");
45102ac6454SAndrew Thompson 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
45202ac6454SAndrew Thompson 			CTLFLAG_RD, &(sc->sc_ucom.sc_unit), 0,
45302ac6454SAndrew Thompson 			"Unit number as USB serial");
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 	return (0);			/* success */
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson detach:
45802ac6454SAndrew Thompson 	ufoma_detach(dev);
45902ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
46002ac6454SAndrew Thompson }
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson static int
46302ac6454SAndrew Thompson ufoma_detach(device_t dev)
46402ac6454SAndrew Thompson {
46502ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	if (sc->sc_modetable) {
47402ac6454SAndrew Thompson 		free(sc->sc_modetable, M_USBDEV);
47502ac6454SAndrew Thompson 	}
47602ac6454SAndrew Thompson 	usb2_cv_destroy(&sc->sc_cv);
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson 	return (0);
47902ac6454SAndrew Thompson }
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson static void *
48202ac6454SAndrew Thompson ufoma_get_intconf(struct usb2_config_descriptor *cd, struct usb2_interface_descriptor *id,
48302ac6454SAndrew Thompson     uint8_t type, uint8_t subtype)
48402ac6454SAndrew Thompson {
48502ac6454SAndrew Thompson 	struct usb2_descriptor *desc = (void *)id;
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	while ((desc = usb2_desc_foreach(cd, desc))) {
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
49002ac6454SAndrew Thompson 			return (NULL);
49102ac6454SAndrew Thompson 		}
49202ac6454SAndrew Thompson 		if ((desc->bDescriptorType == type) &&
49302ac6454SAndrew Thompson 		    (desc->bDescriptorSubtype == subtype)) {
49402ac6454SAndrew Thompson 			break;
49502ac6454SAndrew Thompson 		}
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 	return (desc);
49802ac6454SAndrew Thompson }
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson static void
50102ac6454SAndrew Thompson ufoma_cfg_link_state(struct ufoma_softc *sc)
50202ac6454SAndrew Thompson {
50302ac6454SAndrew Thompson 	struct usb2_device_request req;
50402ac6454SAndrew Thompson 	int32_t error;
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
50702ac6454SAndrew Thompson 	req.bRequest = UMCPC_SET_LINK;
50802ac6454SAndrew Thompson 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
50902ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
51002ac6454SAndrew Thompson 	USETW(req.wLength, sc->sc_modetable[0]);
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
51302ac6454SAndrew Thompson 	    &req, sc->sc_modetable, 0, 1000);
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	error = usb2_cv_timedwait(&sc->sc_cv, &Giant, hz);
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	if (error) {
51802ac6454SAndrew Thompson 		DPRINTF("NO response\n");
51902ac6454SAndrew Thompson 	}
52002ac6454SAndrew Thompson }
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson static void
52302ac6454SAndrew Thompson ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
52402ac6454SAndrew Thompson {
52502ac6454SAndrew Thompson 	struct usb2_device_request req;
52602ac6454SAndrew Thompson 	int32_t error;
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
52902ac6454SAndrew Thompson 	req.bRequest = UMCPC_ACTIVATE_MODE;
53002ac6454SAndrew Thompson 	USETW(req.wValue, state);
53102ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
53202ac6454SAndrew Thompson 	USETW(req.wLength, 0);
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
53502ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	error = usb2_cv_timedwait(&sc->sc_cv, &Giant,
53802ac6454SAndrew Thompson 	    (UFOMA_MAX_TIMEOUT * hz));
53902ac6454SAndrew Thompson 	if (error) {
54002ac6454SAndrew Thompson 		DPRINTF("No response\n");
54102ac6454SAndrew Thompson 	}
54202ac6454SAndrew Thompson }
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson static void
54502ac6454SAndrew Thompson ufoma_ctrl_read_callback(struct usb2_xfer *xfer)
54602ac6454SAndrew Thompson {
54702ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
54802ac6454SAndrew Thompson 	struct usb2_device_request req;
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
55102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
55202ac6454SAndrew Thompson tr_transferred:
55302ac6454SAndrew Thompson 		if (xfer->aframes != xfer->nframes) {
55402ac6454SAndrew Thompson 			goto tr_setup;
55502ac6454SAndrew Thompson 		}
55602ac6454SAndrew Thompson 		if (xfer->frlengths[1] > 0) {
55702ac6454SAndrew Thompson 			usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers + 1,
55802ac6454SAndrew Thompson 			    0, xfer->frlengths[1]);
55902ac6454SAndrew Thompson 		}
56002ac6454SAndrew Thompson 	case USB_ST_SETUP:
56102ac6454SAndrew Thompson tr_setup:
56202ac6454SAndrew Thompson 		if (sc->sc_num_msg) {
56302ac6454SAndrew Thompson 			sc->sc_num_msg--;
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
56602ac6454SAndrew Thompson 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
56702ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
56802ac6454SAndrew Thompson 			USETW(req.wValue, 0);
56902ac6454SAndrew Thompson 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
57402ac6454SAndrew Thompson 			xfer->frlengths[1] = UFOMA_CMD_BUF_SIZE;
57502ac6454SAndrew Thompson 			xfer->nframes = 2;
57602ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
57702ac6454SAndrew Thompson 		}
57802ac6454SAndrew Thompson 		return;
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson 	default:			/* Error */
58102ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
58202ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
58502ac6454SAndrew Thompson 			return;
58602ac6454SAndrew Thompson 		} else {
58702ac6454SAndrew Thompson 			goto tr_setup;
58802ac6454SAndrew Thompson 		}
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 		goto tr_transferred;
59102ac6454SAndrew Thompson 	}
59202ac6454SAndrew Thompson }
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson static void
59502ac6454SAndrew Thompson ufoma_ctrl_write_callback(struct usb2_xfer *xfer)
59602ac6454SAndrew Thompson {
59702ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
59802ac6454SAndrew Thompson 	struct usb2_device_request req;
59902ac6454SAndrew Thompson 	uint32_t actlen;
60002ac6454SAndrew Thompson 
60102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
60202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
60302ac6454SAndrew Thompson tr_transferred:
60402ac6454SAndrew Thompson 	case USB_ST_SETUP:
60502ac6454SAndrew Thompson tr_setup:
60602ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers + 1,
60702ac6454SAndrew Thompson 		    0, 1, &actlen)) {
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
61002ac6454SAndrew Thompson 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
61102ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
61202ac6454SAndrew Thompson 			USETW(req.wValue, 0);
61302ac6454SAndrew Thompson 			USETW(req.wLength, 1);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
61802ac6454SAndrew Thompson 			xfer->frlengths[1] = 1;
61902ac6454SAndrew Thompson 			xfer->nframes = 2;
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
62202ac6454SAndrew Thompson 		}
62302ac6454SAndrew Thompson 		return;
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 	default:			/* Error */
62602ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
62702ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
63002ac6454SAndrew Thompson 			return;
63102ac6454SAndrew Thompson 		} else {
63202ac6454SAndrew Thompson 			goto tr_setup;
63302ac6454SAndrew Thompson 		}
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 		goto tr_transferred;
63602ac6454SAndrew Thompson 	}
63702ac6454SAndrew Thompson }
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson static void
64002ac6454SAndrew Thompson ufoma_intr_callback(struct usb2_xfer *xfer)
64102ac6454SAndrew Thompson {
64202ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
64302ac6454SAndrew Thompson 	struct usb2_cdc_notification pkt;
64402ac6454SAndrew Thompson 	uint16_t wLen;
64502ac6454SAndrew Thompson 	uint16_t temp;
64602ac6454SAndrew Thompson 	uint8_t mstatus;
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
64902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
65002ac6454SAndrew Thompson 		if (xfer->actlen < 8) {
65102ac6454SAndrew Thompson 			DPRINTF("too short message\n");
65202ac6454SAndrew Thompson 			goto tr_setup;
65302ac6454SAndrew Thompson 		}
65402ac6454SAndrew Thompson 		if (xfer->actlen > sizeof(pkt)) {
65502ac6454SAndrew Thompson 			DPRINTF("truncating message\n");
65602ac6454SAndrew Thompson 			xfer->actlen = sizeof(pkt);
65702ac6454SAndrew Thompson 		}
65802ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, &pkt, xfer->actlen);
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson 		xfer->actlen -= 8;
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 		wLen = UGETW(pkt.wLength);
66302ac6454SAndrew Thompson 		if (xfer->actlen > wLen) {
66402ac6454SAndrew Thompson 			xfer->actlen = wLen;
66502ac6454SAndrew Thompson 		}
66602ac6454SAndrew Thompson 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
66702ac6454SAndrew Thompson 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
66802ac6454SAndrew Thompson 			temp = UGETW(pkt.wValue);
66902ac6454SAndrew Thompson 			sc->sc_currentmode = (temp >> 8);
67002ac6454SAndrew Thompson 			if (!(temp & 0xff)) {
67102ac6454SAndrew Thompson 				DPRINTF("Mode change failed!\n");
67202ac6454SAndrew Thompson 			}
67302ac6454SAndrew Thompson 			usb2_cv_signal(&sc->sc_cv);
67402ac6454SAndrew Thompson 		}
67502ac6454SAndrew Thompson 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
67602ac6454SAndrew Thompson 			goto tr_setup;
67702ac6454SAndrew Thompson 		}
67802ac6454SAndrew Thompson 		switch (pkt.bNotification) {
67902ac6454SAndrew Thompson 		case UCDC_N_RESPONSE_AVAILABLE:
68002ac6454SAndrew Thompson 			if (!(sc->sc_nobulk)) {
68102ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
68202ac6454SAndrew Thompson 				break;
68302ac6454SAndrew Thompson 			}
68402ac6454SAndrew Thompson 			if (sc->sc_num_msg != 0xFF) {
68502ac6454SAndrew Thompson 				sc->sc_num_msg++;
68602ac6454SAndrew Thompson 			}
68702ac6454SAndrew Thompson 			usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
68802ac6454SAndrew Thompson 			break;
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 		case UCDC_N_SERIAL_STATE:
69102ac6454SAndrew Thompson 			if (sc->sc_nobulk) {
69202ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
69302ac6454SAndrew Thompson 				break;
69402ac6454SAndrew Thompson 			}
69502ac6454SAndrew Thompson 			/*
69602ac6454SAndrew Thompson 		         * Set the serial state in ucom driver based on
69702ac6454SAndrew Thompson 		         * the bits from the notify message
69802ac6454SAndrew Thompson 		         */
69902ac6454SAndrew Thompson 			if (xfer->actlen < 2) {
70002ac6454SAndrew Thompson 				DPRINTF("invalid notification "
70102ac6454SAndrew Thompson 				    "length, %d bytes!\n", xfer->actlen);
70202ac6454SAndrew Thompson 				break;
70302ac6454SAndrew Thompson 			}
70402ac6454SAndrew Thompson 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
70502ac6454SAndrew Thompson 			    pkt.data[0], pkt.data[1]);
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 			/* currently, lsr is always zero. */
70802ac6454SAndrew Thompson 			sc->sc_lsr = 0;
70902ac6454SAndrew Thompson 			sc->sc_msr = 0;
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 			mstatus = pkt.data[0];
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_RI) {
71402ac6454SAndrew Thompson 				sc->sc_msr |= SER_RI;
71502ac6454SAndrew Thompson 			}
71602ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DSR) {
71702ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
71802ac6454SAndrew Thompson 			}
71902ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DCD) {
72002ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
72102ac6454SAndrew Thompson 			}
72202ac6454SAndrew Thompson 			usb2_com_status_change(&sc->sc_ucom);
72302ac6454SAndrew Thompson 			break;
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 		default:
72602ac6454SAndrew Thompson 			break;
72702ac6454SAndrew Thompson 		}
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 	case USB_ST_SETUP:
73002ac6454SAndrew Thompson tr_setup:
73102ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
73202ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
73302ac6454SAndrew Thompson 		return;
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	default:			/* Error */
73602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
73702ac6454SAndrew Thompson 			/* try to clear stall first */
73802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
73902ac6454SAndrew Thompson 			goto tr_setup;
74002ac6454SAndrew Thompson 		}
74102ac6454SAndrew Thompson 		return;
74202ac6454SAndrew Thompson 	}
74302ac6454SAndrew Thompson }
74402ac6454SAndrew Thompson 
74502ac6454SAndrew Thompson static void
74602ac6454SAndrew Thompson ufoma_bulk_write_callback(struct usb2_xfer *xfer)
74702ac6454SAndrew Thompson {
74802ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
74902ac6454SAndrew Thompson 	uint32_t actlen;
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
75202ac6454SAndrew Thompson 	case USB_ST_SETUP:
75302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
75402ac6454SAndrew Thompson tr_setup:
75502ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
75602ac6454SAndrew Thompson 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
75702ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
75802ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
75902ac6454SAndrew Thompson 		}
76002ac6454SAndrew Thompson 		return;
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 	default:			/* Error */
76302ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
76402ac6454SAndrew Thompson 			/* try to clear stall first */
76502ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
76602ac6454SAndrew Thompson 			goto tr_setup;
76702ac6454SAndrew Thompson 		}
76802ac6454SAndrew Thompson 		return;
76902ac6454SAndrew Thompson 	}
77002ac6454SAndrew Thompson }
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson static void
77302ac6454SAndrew Thompson ufoma_bulk_read_callback(struct usb2_xfer *xfer)
77402ac6454SAndrew Thompson {
77502ac6454SAndrew Thompson 	struct ufoma_softc *sc = xfer->priv_sc;
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
77802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
77902ac6454SAndrew Thompson 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0,
78002ac6454SAndrew Thompson 		    xfer->actlen);
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 	case USB_ST_SETUP:
78302ac6454SAndrew Thompson tr_setup:
78402ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
78502ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
78602ac6454SAndrew Thompson 		return;
78702ac6454SAndrew Thompson 
78802ac6454SAndrew Thompson 	default:			/* Error */
78902ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
79002ac6454SAndrew Thompson 			/* try to clear stall first */
79102ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
79202ac6454SAndrew Thompson 			goto tr_setup;
79302ac6454SAndrew Thompson 		}
79402ac6454SAndrew Thompson 		return;
79502ac6454SAndrew Thompson 	}
79602ac6454SAndrew Thompson }
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson static void
79902ac6454SAndrew Thompson ufoma_cfg_open(struct usb2_com_softc *ucom)
80002ac6454SAndrew Thompson {
80102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 	/* empty input queue */
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 	if (sc->sc_num_msg != 0xFF) {
80602ac6454SAndrew Thompson 		sc->sc_num_msg++;
80702ac6454SAndrew Thompson 	}
80802ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
80902ac6454SAndrew Thompson 		ufoma_cfg_link_state(sc);
81002ac6454SAndrew Thompson 	}
81102ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
81202ac6454SAndrew Thompson 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
81302ac6454SAndrew Thompson 	}
81402ac6454SAndrew Thompson }
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson static void
81702ac6454SAndrew Thompson ufoma_cfg_close(struct usb2_com_softc *ucom)
81802ac6454SAndrew Thompson {
81902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
82202ac6454SAndrew Thompson }
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson static void
82502ac6454SAndrew Thompson ufoma_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
82602ac6454SAndrew Thompson {
82702ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
82802ac6454SAndrew Thompson 	struct usb2_device_request req;
82902ac6454SAndrew Thompson 	uint16_t wValue;
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
83202ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
83302ac6454SAndrew Thompson 		return;
83402ac6454SAndrew Thompson 	}
83502ac6454SAndrew Thompson 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
83602ac6454SAndrew Thompson 		return;
83702ac6454SAndrew Thompson 	}
83802ac6454SAndrew Thompson 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
84102ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
84202ac6454SAndrew Thompson 	USETW(req.wValue, wValue);
84302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
84402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
84502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
84602ac6454SAndrew Thompson 
84702ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
84802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
84902ac6454SAndrew Thompson }
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson static void
85202ac6454SAndrew Thompson ufoma_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
85302ac6454SAndrew Thompson {
85402ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
85702ac6454SAndrew Thompson 	*msr = sc->sc_msr;
85802ac6454SAndrew Thompson }
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson static void
86102ac6454SAndrew Thompson ufoma_cfg_set_line_state(struct ufoma_softc *sc)
86202ac6454SAndrew Thompson {
86302ac6454SAndrew Thompson 	struct usb2_device_request req;
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson 	/* Don't send line state emulation request for OBEX port */
86602ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
86702ac6454SAndrew Thompson 		return;
86802ac6454SAndrew Thompson 	}
86902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
87002ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
87102ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
87202ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
87302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
87402ac6454SAndrew Thompson 	USETW(req.wLength, 0);
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
87702ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
87802ac6454SAndrew Thompson }
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson static void
88102ac6454SAndrew Thompson ufoma_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
88202ac6454SAndrew Thompson {
88302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
88602ac6454SAndrew Thompson 		return;
88702ac6454SAndrew Thompson 	}
88802ac6454SAndrew Thompson 	if (onoff)
88902ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
89002ac6454SAndrew Thompson 	else
89102ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
89202ac6454SAndrew Thompson 
89302ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
89402ac6454SAndrew Thompson }
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson static void
89702ac6454SAndrew Thompson ufoma_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
89802ac6454SAndrew Thompson {
89902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
90202ac6454SAndrew Thompson 		return;
90302ac6454SAndrew Thompson 	}
90402ac6454SAndrew Thompson 	if (onoff)
90502ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
90602ac6454SAndrew Thompson 	else
90702ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
91002ac6454SAndrew Thompson }
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson static int
91302ac6454SAndrew Thompson ufoma_pre_param(struct usb2_com_softc *ucom, struct termios *t)
91402ac6454SAndrew Thompson {
91502ac6454SAndrew Thompson 	return (0);			/* we accept anything */
91602ac6454SAndrew Thompson }
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson static void
91902ac6454SAndrew Thompson ufoma_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
92002ac6454SAndrew Thompson {
92102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
92202ac6454SAndrew Thompson 	struct usb2_device_request req;
92302ac6454SAndrew Thompson 	struct usb2_cdc_line_state ls;
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
92602ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
92702ac6454SAndrew Thompson 		return;
92802ac6454SAndrew Thompson 	}
92902ac6454SAndrew Thompson 	DPRINTF("\n");
93002ac6454SAndrew Thompson 
93102ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
93602ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
93702ac6454SAndrew Thompson 	} else {
93802ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
93902ac6454SAndrew Thompson 	}
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
94202ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
94302ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
94402ac6454SAndrew Thompson 		} else {
94502ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
94602ac6454SAndrew Thompson 		}
94702ac6454SAndrew Thompson 	} else {
94802ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
94902ac6454SAndrew Thompson 	}
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
95202ac6454SAndrew Thompson 	case CS5:
95302ac6454SAndrew Thompson 		ls.bDataBits = 5;
95402ac6454SAndrew Thompson 		break;
95502ac6454SAndrew Thompson 	case CS6:
95602ac6454SAndrew Thompson 		ls.bDataBits = 6;
95702ac6454SAndrew Thompson 		break;
95802ac6454SAndrew Thompson 	case CS7:
95902ac6454SAndrew Thompson 		ls.bDataBits = 7;
96002ac6454SAndrew Thompson 		break;
96102ac6454SAndrew Thompson 	case CS8:
96202ac6454SAndrew Thompson 		ls.bDataBits = 8;
96302ac6454SAndrew Thompson 		break;
96402ac6454SAndrew Thompson 	}
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
96702ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
96802ac6454SAndrew Thompson 	USETW(req.wValue, 0);
96902ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
97002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
97102ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
97202ac6454SAndrew Thompson 
97302ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
97402ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
97502ac6454SAndrew Thompson }
97602ac6454SAndrew Thompson 
97702ac6454SAndrew Thompson static int
97802ac6454SAndrew Thompson ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
97902ac6454SAndrew Thompson     struct usb2_attach_arg *uaa)
98002ac6454SAndrew Thompson {
98102ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
98202ac6454SAndrew Thompson 	struct usb2_cdc_acm_descriptor *acm;
98302ac6454SAndrew Thompson 	struct usb2_cdc_cm_descriptor *cmd;
98402ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
98502ac6454SAndrew Thompson 	struct usb2_interface *iface;
98602ac6454SAndrew Thompson 	uint8_t i;
98702ac6454SAndrew Thompson 	int32_t error;
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(uaa->device);
99002ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
99302ac6454SAndrew Thompson 
99402ac6454SAndrew Thompson 	if ((cmd == NULL) ||
99502ac6454SAndrew Thompson 	    (cmd->bLength < sizeof(*cmd))) {
99602ac6454SAndrew Thompson 		return (EINVAL);
99702ac6454SAndrew Thompson 	}
99802ac6454SAndrew Thompson 	sc->sc_cm_cap = cmd->bmCapabilities;
99902ac6454SAndrew Thompson 	sc->sc_data_iface_no = cmd->bDataInterface;
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
100202ac6454SAndrew Thompson 
100302ac6454SAndrew Thompson 	if ((acm == NULL) ||
100402ac6454SAndrew Thompson 	    (acm->bLength < sizeof(*acm))) {
100502ac6454SAndrew Thompson 		return (EINVAL);
100602ac6454SAndrew Thompson 	}
100702ac6454SAndrew Thompson 	sc->sc_acm_cap = acm->bmCapabilities;
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson 	device_printf(dev, "data interface %d, has %sCM over data, "
101002ac6454SAndrew Thompson 	    "has %sbreak\n",
101102ac6454SAndrew Thompson 	    sc->sc_data_iface_no,
101202ac6454SAndrew Thompson 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
101302ac6454SAndrew Thompson 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	/* get the data interface too */
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	for (i = 0;; i++) {
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 		iface = usb2_get_iface(uaa->device, i);
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 		if (iface) {
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 			id = usb2_get_interface_descriptor(iface);
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
102602ac6454SAndrew Thompson 				sc->sc_data_iface_index = i;
102702ac6454SAndrew Thompson 				usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
102802ac6454SAndrew Thompson 				break;
102902ac6454SAndrew Thompson 			}
103002ac6454SAndrew Thompson 		} else {
103102ac6454SAndrew Thompson 			device_printf(dev, "no data interface!\n");
103202ac6454SAndrew Thompson 			return (EINVAL);
103302ac6454SAndrew Thompson 		}
103402ac6454SAndrew Thompson 	}
103502ac6454SAndrew Thompson 
103602ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
103702ac6454SAndrew Thompson 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
103802ac6454SAndrew Thompson 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &Giant);
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	if (error) {
104102ac6454SAndrew Thompson 		device_printf(dev, "allocating BULK USB "
104202ac6454SAndrew Thompson 		    "transfers failed!\n");
104302ac6454SAndrew Thompson 		return (EINVAL);
104402ac6454SAndrew Thompson 	}
104502ac6454SAndrew Thompson 	return (0);
104602ac6454SAndrew Thompson }
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson static void
104902ac6454SAndrew Thompson ufoma_start_read(struct usb2_com_softc *ucom)
105002ac6454SAndrew Thompson {
105102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson 	/* start interrupt transfer */
105402ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 	/* start data transfer */
105702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
105802ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
105902ac6454SAndrew Thompson 	} else {
106002ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
106102ac6454SAndrew Thompson 	}
106202ac6454SAndrew Thompson }
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson static void
106502ac6454SAndrew Thompson ufoma_stop_read(struct usb2_com_softc *ucom)
106602ac6454SAndrew Thompson {
106702ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
106802ac6454SAndrew Thompson 
106902ac6454SAndrew Thompson 	/* stop interrupt transfer */
107002ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
107102ac6454SAndrew Thompson 
107202ac6454SAndrew Thompson 	/* stop data transfer */
107302ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
107402ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
107502ac6454SAndrew Thompson 	} else {
107602ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
107702ac6454SAndrew Thompson 	}
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson static void
108102ac6454SAndrew Thompson ufoma_start_write(struct usb2_com_softc *ucom)
108202ac6454SAndrew Thompson {
108302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
108402ac6454SAndrew Thompson 
108502ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
108602ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
108702ac6454SAndrew Thompson 	} else {
108802ac6454SAndrew Thompson 		usb2_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
108902ac6454SAndrew Thompson 	}
109002ac6454SAndrew Thompson }
109102ac6454SAndrew Thompson 
109202ac6454SAndrew Thompson static void
109302ac6454SAndrew Thompson ufoma_stop_write(struct usb2_com_softc *ucom)
109402ac6454SAndrew Thompson {
109502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
109602ac6454SAndrew Thompson 
109702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
109802ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
109902ac6454SAndrew Thompson 	} else {
110002ac6454SAndrew Thompson 		usb2_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
110102ac6454SAndrew Thompson 	}
110202ac6454SAndrew Thompson }
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson struct umcpc_modetostr_tab{
110502ac6454SAndrew Thompson 	int mode;
110602ac6454SAndrew Thompson 	char *str;
110702ac6454SAndrew Thompson }umcpc_modetostr_tab[]={
110802ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
110902ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_MODEM, "modem"},
111002ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
111102ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_OBEX, "obex"},
111202ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
111302ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
111402ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
111502ac6454SAndrew Thompson 	{0, NULL}
111602ac6454SAndrew Thompson };
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson static char *ufoma_mode_to_str(int mode)
111902ac6454SAndrew Thompson {
112002ac6454SAndrew Thompson 	int i;
112102ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
112202ac6454SAndrew Thompson 		if(umcpc_modetostr_tab[i].mode == mode){
112302ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].str;
112402ac6454SAndrew Thompson 		}
112502ac6454SAndrew Thompson 	}
112602ac6454SAndrew Thompson 	return NULL;
112702ac6454SAndrew Thompson }
112802ac6454SAndrew Thompson 
112902ac6454SAndrew Thompson static int ufoma_str_to_mode(char *str)
113002ac6454SAndrew Thompson {
113102ac6454SAndrew Thompson 	int i;
113202ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
113302ac6454SAndrew Thompson 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
113402ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].mode;
113502ac6454SAndrew Thompson 		}
113602ac6454SAndrew Thompson 	}
113702ac6454SAndrew Thompson 	return -1;
113802ac6454SAndrew Thompson }
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
114102ac6454SAndrew Thompson {
114202ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
114302ac6454SAndrew Thompson 	struct sbuf sb;
114402ac6454SAndrew Thompson 	int i;
114502ac6454SAndrew Thompson 	char *mode;
114602ac6454SAndrew Thompson 
114702ac6454SAndrew Thompson 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
114802ac6454SAndrew Thompson 	for(i = 1; i < sc->sc_modetable[0]; i++){
114902ac6454SAndrew Thompson 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
115002ac6454SAndrew Thompson 		if(mode !=NULL){
115102ac6454SAndrew Thompson 			sbuf_cat(&sb, mode);
115202ac6454SAndrew Thompson 		}else{
115302ac6454SAndrew Thompson 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
115402ac6454SAndrew Thompson 		}
115502ac6454SAndrew Thompson 		if(i < (sc->sc_modetable[0]-1))
115602ac6454SAndrew Thompson 			sbuf_cat(&sb, ",");
115702ac6454SAndrew Thompson 	}
115802ac6454SAndrew Thompson 	sbuf_trim(&sb);
115902ac6454SAndrew Thompson 	sbuf_finish(&sb);
116002ac6454SAndrew Thompson 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
116102ac6454SAndrew Thompson 	sbuf_delete(&sb);
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 	return 0;
116402ac6454SAndrew Thompson }
116502ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
116602ac6454SAndrew Thompson {
116702ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
116802ac6454SAndrew Thompson 	char *mode;
116902ac6454SAndrew Thompson 	char subbuf[]="(XXX)";
117002ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_currentmode);
117102ac6454SAndrew Thompson 	if(!mode){
117202ac6454SAndrew Thompson 		mode = subbuf;
117302ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
117402ac6454SAndrew Thompson 	}
117502ac6454SAndrew Thompson 	sysctl_handle_string(oidp, mode, strlen(mode), req);
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson 	return 0;
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson }
118002ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
118102ac6454SAndrew Thompson {
118202ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
118302ac6454SAndrew Thompson 	char *mode;
118402ac6454SAndrew Thompson 	char subbuf[40];
118502ac6454SAndrew Thompson 	int newmode;
118602ac6454SAndrew Thompson 	int error;
118702ac6454SAndrew Thompson 	int i;
118802ac6454SAndrew Thompson 
118902ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
119002ac6454SAndrew Thompson 	if(mode){
119102ac6454SAndrew Thompson 		strncpy(subbuf, mode, sizeof(subbuf));
119202ac6454SAndrew Thompson 	}else{
119302ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
119402ac6454SAndrew Thompson 	}
119502ac6454SAndrew Thompson 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
119602ac6454SAndrew Thompson 	if(error != 0 || req->newptr == NULL){
119702ac6454SAndrew Thompson 		return error;
119802ac6454SAndrew Thompson 	}
119902ac6454SAndrew Thompson 
120002ac6454SAndrew Thompson 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
120102ac6454SAndrew Thompson 		return EINVAL;
120202ac6454SAndrew Thompson 	}
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
120502ac6454SAndrew Thompson 		if(sc->sc_modetable[i] == newmode){
120602ac6454SAndrew Thompson 			sc->sc_modetoactivate = newmode;
120702ac6454SAndrew Thompson 			return 0;
120802ac6454SAndrew Thompson 		}
120902ac6454SAndrew Thompson 	}
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson 	return EINVAL;
121202ac6454SAndrew Thompson }
1213