xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 655dc9d00a7d992bb50579ca0008e2a803ed8d21)
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 
85ed6d949aSAndrew Thompson #include <sys/stdint.h>
86ed6d949aSAndrew Thompson #include <sys/stddef.h>
87ed6d949aSAndrew Thompson #include <sys/param.h>
88ed6d949aSAndrew Thompson #include <sys/queue.h>
89ed6d949aSAndrew Thompson #include <sys/types.h>
90ed6d949aSAndrew Thompson #include <sys/systm.h>
91ed6d949aSAndrew Thompson #include <sys/kernel.h>
92ed6d949aSAndrew Thompson #include <sys/bus.h>
93ed6d949aSAndrew Thompson #include <sys/linker_set.h>
94ed6d949aSAndrew Thompson #include <sys/module.h>
95ed6d949aSAndrew Thompson #include <sys/lock.h>
96ed6d949aSAndrew Thompson #include <sys/mutex.h>
97ed6d949aSAndrew Thompson #include <sys/condvar.h>
98ed6d949aSAndrew Thompson #include <sys/sysctl.h>
99ed6d949aSAndrew Thompson #include <sys/sx.h>
100ed6d949aSAndrew Thompson #include <sys/unistd.h>
101ed6d949aSAndrew Thompson #include <sys/callout.h>
102ed6d949aSAndrew Thompson #include <sys/malloc.h>
103ed6d949aSAndrew Thompson #include <sys/priv.h>
104ed6d949aSAndrew Thompson #include <sys/sbuf.h>
105ed6d949aSAndrew Thompson 
10602ac6454SAndrew Thompson #include <dev/usb/usb.h>
107ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
108ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
10902ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
110ed6d949aSAndrew Thompson #include "usbdevs.h"
11102ac6454SAndrew Thompson 
112a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
11302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
11402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
11702ac6454SAndrew Thompson 
11802ac6454SAndrew Thompson typedef struct ufoma_mobile_acm_descriptor {
11902ac6454SAndrew Thompson 	uint8_t	bFunctionLength;
12002ac6454SAndrew Thompson 	uint8_t	bDescriptorType;
12102ac6454SAndrew Thompson 	uint8_t	bDescriptorSubtype;
12202ac6454SAndrew Thompson 	uint8_t	bType;
12302ac6454SAndrew Thompson 	uint8_t	bMode[1];
124a593f6b8SAndrew Thompson } __packed usb_mcpc_acm_descriptor;
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson #define	UISUBCLASS_MCPC 0x88
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson #define	UDESC_VS_INTERFACE 0x44
12902ac6454SAndrew Thompson #define	UDESCSUB_MCPC_ACM  0x11
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB1 0x1
13202ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB2 0x2
13302ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB5 0x5
13402ac6454SAndrew Thompson #define	UMCPC_ACM_TYPE_AB6 0x6
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_DEACTIVATED 0x0
13702ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_MODEM 0x1
13802ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_ATCOMMAND 0x2
13902ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_OBEX 0x60
14002ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_VENDOR1 0xc0
14102ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_VENDOR2 0xfe
14202ac6454SAndrew Thompson #define	UMCPC_ACM_MODE_UNLINKED 0xff
14302ac6454SAndrew Thompson 
14402ac6454SAndrew Thompson #define	UMCPC_CM_MOBILE_ACM 0x0
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson #define	UMCPC_ACTIVATE_MODE 0x60
14702ac6454SAndrew Thompson #define	UMCPC_GET_MODETABLE 0x61
14802ac6454SAndrew Thompson #define	UMCPC_SET_LINK 0x62
14902ac6454SAndrew Thompson #define	UMCPC_CLEAR_LINK 0x63
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson #define	UMCPC_REQUEST_ACKNOWLEDGE 0x31
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson #define	UFOMA_MAX_TIMEOUT 15		/* standard says 10 seconds */
15402ac6454SAndrew Thompson #define	UFOMA_CMD_BUF_SIZE 64		/* bytes */
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson #define	UFOMA_BULK_BUF_SIZE 1024	/* bytes */
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson enum {
15902ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_INTR,
16002ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_READ,
16102ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_WRITE,
16202ac6454SAndrew Thompson 	UFOMA_CTRL_ENDPT_MAX,
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson enum {
16602ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_WRITE,
16702ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_READ,
16802ac6454SAndrew Thompson 	UFOMA_BULK_ENDPT_MAX,
16902ac6454SAndrew Thompson };
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson struct ufoma_softc {
172760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
173760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
17402ac6454SAndrew Thompson 	struct cv sc_cv;
175deefe583SAndrew Thompson 	struct mtx sc_mtx;
17602ac6454SAndrew Thompson 
177760bc48eSAndrew Thompson 	struct usb_xfer *sc_ctrl_xfer[UFOMA_CTRL_ENDPT_MAX];
178760bc48eSAndrew Thompson 	struct usb_xfer *sc_bulk_xfer[UFOMA_BULK_ENDPT_MAX];
17902ac6454SAndrew Thompson 	uint8_t *sc_modetable;
18002ac6454SAndrew Thompson 	device_t sc_dev;
181760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson 	uint32_t sc_unit;
18402ac6454SAndrew Thompson 
18502ac6454SAndrew Thompson 	uint16_t sc_line;
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson 	uint8_t	sc_num_msg;
18802ac6454SAndrew Thompson 	uint8_t	sc_nobulk;
18902ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_no;
19002ac6454SAndrew Thompson 	uint8_t	sc_ctrl_iface_index;
19102ac6454SAndrew Thompson 	uint8_t	sc_data_iface_no;
19202ac6454SAndrew Thompson 	uint8_t	sc_data_iface_index;
19302ac6454SAndrew Thompson 	uint8_t	sc_cm_cap;
19402ac6454SAndrew Thompson 	uint8_t	sc_acm_cap;
19502ac6454SAndrew Thompson 	uint8_t	sc_lsr;
19602ac6454SAndrew Thompson 	uint8_t	sc_msr;
19702ac6454SAndrew Thompson 	uint8_t	sc_modetoactivate;
19802ac6454SAndrew Thompson 	uint8_t	sc_currentmode;
19902ac6454SAndrew Thompson 	uint8_t	sc_name[16];
20002ac6454SAndrew Thompson };
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson /* prototypes */
20302ac6454SAndrew Thompson 
20402ac6454SAndrew Thompson static device_probe_t ufoma_probe;
20502ac6454SAndrew Thompson static device_attach_t ufoma_attach;
20602ac6454SAndrew Thompson static device_detach_t ufoma_detach;
20702ac6454SAndrew Thompson 
208e0a69b51SAndrew Thompson static usb_callback_t ufoma_ctrl_read_callback;
209e0a69b51SAndrew Thompson static usb_callback_t ufoma_ctrl_write_callback;
210e0a69b51SAndrew Thompson static usb_callback_t ufoma_intr_callback;
211e0a69b51SAndrew Thompson static usb_callback_t ufoma_bulk_write_callback;
212e0a69b51SAndrew Thompson static usb_callback_t ufoma_bulk_read_callback;
21302ac6454SAndrew Thompson 
214760bc48eSAndrew Thompson static void	*ufoma_get_intconf(struct usb_config_descriptor *,
215760bc48eSAndrew Thompson 		    struct usb_interface_descriptor *, uint8_t, uint8_t);
21602ac6454SAndrew Thompson static void	ufoma_cfg_link_state(struct ufoma_softc *);
21702ac6454SAndrew Thompson static void	ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
218760bc48eSAndrew Thompson static void	ufoma_cfg_open(struct ucom_softc *);
219760bc48eSAndrew Thompson static void	ufoma_cfg_close(struct ucom_softc *);
220760bc48eSAndrew Thompson static void	ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
221760bc48eSAndrew Thompson static void	ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
22202ac6454SAndrew Thompson 		    uint8_t *);
223760bc48eSAndrew Thompson static void	ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
224760bc48eSAndrew Thompson static void	ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
225760bc48eSAndrew Thompson static int	ufoma_pre_param(struct ucom_softc *, struct termios *);
226760bc48eSAndrew Thompson static void	ufoma_cfg_param(struct ucom_softc *, struct termios *);
22702ac6454SAndrew Thompson static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
228760bc48eSAndrew Thompson 		    struct usb_attach_arg *);
229760bc48eSAndrew Thompson static void	ufoma_start_read(struct ucom_softc *);
230760bc48eSAndrew Thompson static void	ufoma_stop_read(struct ucom_softc *);
231760bc48eSAndrew Thompson static void	ufoma_start_write(struct ucom_softc *);
232760bc48eSAndrew Thompson static void	ufoma_stop_write(struct ucom_softc *);
233655dc9d0SAndrew Thompson static void	ufoma_poll(struct ucom_softc *ucom);
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson /*sysctl stuff*/
23602ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
23702ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
23802ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
23902ac6454SAndrew Thompson 
240760bc48eSAndrew Thompson static const struct usb_config
24102ac6454SAndrew Thompson 	ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
24202ac6454SAndrew Thompson 
24302ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_INTR] = {
24402ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
24502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
24602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2474eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_cdc_notification),
2494eae601eSAndrew Thompson 		.callback = &ufoma_intr_callback,
25002ac6454SAndrew Thompson 	},
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_READ] = {
25302ac6454SAndrew Thompson 		.type = UE_CONTROL,
25402ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
25502ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
256760bc48eSAndrew Thompson 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
2574eae601eSAndrew Thompson 		.flags = {.short_xfer_ok = 1,},
2584eae601eSAndrew Thompson 		.callback = &ufoma_ctrl_read_callback,
2594eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
26002ac6454SAndrew Thompson 	},
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 	[UFOMA_CTRL_ENDPT_WRITE] = {
26302ac6454SAndrew Thompson 		.type = UE_CONTROL,
26402ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
26502ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
266760bc48eSAndrew Thompson 		.bufsize = (sizeof(struct usb_device_request) + 1),
2674eae601eSAndrew Thompson 		.callback = &ufoma_ctrl_write_callback,
2684eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
26902ac6454SAndrew Thompson 	},
27002ac6454SAndrew Thompson };
27102ac6454SAndrew Thompson 
272760bc48eSAndrew Thompson static const struct usb_config
27302ac6454SAndrew Thompson 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
27402ac6454SAndrew Thompson 
27502ac6454SAndrew Thompson 	[UFOMA_BULK_ENDPT_WRITE] = {
27602ac6454SAndrew Thompson 		.type = UE_BULK,
27702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
27802ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2794eae601eSAndrew Thompson 		.bufsize = UFOMA_BULK_BUF_SIZE,
2804eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2814eae601eSAndrew Thompson 		.callback = &ufoma_bulk_write_callback,
28202ac6454SAndrew Thompson 	},
28302ac6454SAndrew Thompson 
28402ac6454SAndrew Thompson 	[UFOMA_BULK_ENDPT_READ] = {
28502ac6454SAndrew Thompson 		.type = UE_BULK,
28602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
28702ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2884eae601eSAndrew Thompson 		.bufsize = UFOMA_BULK_BUF_SIZE,
2894eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2904eae601eSAndrew Thompson 		.callback = &ufoma_bulk_read_callback,
29102ac6454SAndrew Thompson 	},
29202ac6454SAndrew Thompson };
29302ac6454SAndrew Thompson 
294760bc48eSAndrew Thompson static const struct ucom_callback ufoma_callback = {
295a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &ufoma_cfg_get_status,
296a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
297a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &ufoma_cfg_set_rts,
298a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &ufoma_cfg_set_break,
299a593f6b8SAndrew Thompson 	.ucom_cfg_param = &ufoma_cfg_param,
300a593f6b8SAndrew Thompson 	.ucom_cfg_open = &ufoma_cfg_open,
301a593f6b8SAndrew Thompson 	.ucom_cfg_close = &ufoma_cfg_close,
302a593f6b8SAndrew Thompson 	.ucom_pre_param = &ufoma_pre_param,
303a593f6b8SAndrew Thompson 	.ucom_start_read = &ufoma_start_read,
304a593f6b8SAndrew Thompson 	.ucom_stop_read = &ufoma_stop_read,
305a593f6b8SAndrew Thompson 	.ucom_start_write = &ufoma_start_write,
306a593f6b8SAndrew Thompson 	.ucom_stop_write = &ufoma_stop_write,
307655dc9d0SAndrew Thompson 	.ucom_poll = &ufoma_poll,
30802ac6454SAndrew Thompson };
30902ac6454SAndrew Thompson 
31002ac6454SAndrew Thompson static device_method_t ufoma_methods[] = {
31102ac6454SAndrew Thompson 	/* Device methods */
31202ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ufoma_probe),
31302ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ufoma_attach),
31402ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ufoma_detach),
31502ac6454SAndrew Thompson 	{0, 0}
31602ac6454SAndrew Thompson };
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson static devclass_t ufoma_devclass;
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson static driver_t ufoma_driver = {
32102ac6454SAndrew Thompson 	.name = "ufoma",
32202ac6454SAndrew Thompson 	.methods = ufoma_methods,
32302ac6454SAndrew Thompson 	.size = sizeof(struct ufoma_softc),
32402ac6454SAndrew Thompson };
32502ac6454SAndrew Thompson 
3269aef556dSAndrew Thompson DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
32702ac6454SAndrew Thompson MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
32802ac6454SAndrew Thompson MODULE_DEPEND(ufoma, usb, 1, 1, 1);
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson static int
33102ac6454SAndrew Thompson ufoma_probe(device_t dev)
33202ac6454SAndrew Thompson {
333760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
334760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
335760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
336a593f6b8SAndrew Thompson 	usb_mcpc_acm_descriptor *mad;
33702ac6454SAndrew Thompson 
338f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
33902ac6454SAndrew Thompson 		return (ENXIO);
34002ac6454SAndrew Thompson 	}
341a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
342a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	if ((id == NULL) ||
34502ac6454SAndrew Thompson 	    (cd == NULL) ||
34602ac6454SAndrew Thompson 	    (id->bInterfaceClass != UICLASS_CDC) ||
34702ac6454SAndrew Thompson 	    (id->bInterfaceSubClass != UISUBCLASS_MCPC)) {
34802ac6454SAndrew Thompson 		return (ENXIO);
34902ac6454SAndrew Thompson 	}
35002ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
35102ac6454SAndrew Thompson 	if (mad == NULL) {
35202ac6454SAndrew Thompson 		return (ENXIO);
35302ac6454SAndrew Thompson 	}
35402ac6454SAndrew Thompson #ifndef UFOMA_HANDSFREE
35502ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
35602ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
35702ac6454SAndrew Thompson 		return (ENXIO);
35802ac6454SAndrew Thompson 	}
35902ac6454SAndrew Thompson #endif
36002ac6454SAndrew Thompson 	return (0);
36102ac6454SAndrew Thompson }
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson static int
36402ac6454SAndrew Thompson ufoma_attach(device_t dev)
36502ac6454SAndrew Thompson {
366760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
36702ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
368760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
369760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
37002ac6454SAndrew Thompson 	struct sysctl_ctx_list *sctx;
37102ac6454SAndrew Thompson 	struct sysctl_oid *soid;
37202ac6454SAndrew Thompson 
373a593f6b8SAndrew Thompson 	usb_mcpc_acm_descriptor *mad;
37402ac6454SAndrew Thompson 	uint8_t elements;
37502ac6454SAndrew Thompson 	int32_t error;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
37802ac6454SAndrew Thompson 	sc->sc_dev = dev;
37902ac6454SAndrew Thompson 	sc->sc_unit = device_get_unit(dev);
38002ac6454SAndrew Thompson 
381deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
3828437751dSAndrew Thompson 	cv_init(&sc->sc_cv, "CWAIT");
38302ac6454SAndrew Thompson 
384a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
38702ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	DPRINTF("\n");
39002ac6454SAndrew Thompson 
39102ac6454SAndrew Thompson 	/* setup control transfers */
39202ac6454SAndrew Thompson 
393a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
394a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
39502ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
39602ac6454SAndrew Thompson 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
39702ac6454SAndrew Thompson 
398a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
39902ac6454SAndrew Thompson 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
400deefe583SAndrew Thompson 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 	if (error) {
40302ac6454SAndrew Thompson 		device_printf(dev, "allocating control USB "
40402ac6454SAndrew Thompson 		    "transfers failed!\n");
40502ac6454SAndrew Thompson 		goto detach;
40602ac6454SAndrew Thompson 	}
40702ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
40802ac6454SAndrew Thompson 	if (mad == NULL) {
40902ac6454SAndrew Thompson 		goto detach;
41002ac6454SAndrew Thompson 	}
41102ac6454SAndrew Thompson 	if (mad->bFunctionLength < sizeof(*mad)) {
41202ac6454SAndrew Thompson 		device_printf(dev, "invalid MAD descriptor\n");
41302ac6454SAndrew Thompson 		goto detach;
41402ac6454SAndrew Thompson 	}
41502ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
41602ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
41702ac6454SAndrew Thompson 		sc->sc_nobulk = 1;
41802ac6454SAndrew Thompson 	} else {
41902ac6454SAndrew Thompson 		sc->sc_nobulk = 0;
42002ac6454SAndrew Thompson 		if (ufoma_modem_setup(dev, sc, uaa)) {
42102ac6454SAndrew Thompson 			goto detach;
42202ac6454SAndrew Thompson 		}
42302ac6454SAndrew Thompson 	}
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 	/* initialize mode variables */
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
43002ac6454SAndrew Thompson 
43102ac6454SAndrew Thompson 	if (sc->sc_modetable == NULL) {
43202ac6454SAndrew Thompson 		goto detach;
43302ac6454SAndrew Thompson 	}
43402ac6454SAndrew Thompson 	sc->sc_modetable[0] = (elements + 1);
43502ac6454SAndrew Thompson 	bcopy(mad->bMode, &sc->sc_modetable[1], elements);
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
43802ac6454SAndrew Thompson 	sc->sc_modetoactivate = mad->bMode[0];
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	/* clear stall at first run, if any */
441deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
442ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
443ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
444deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
44502ac6454SAndrew Thompson 
446a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
447deefe583SAndrew Thompson 	    &ufoma_callback, &sc->sc_mtx);
44802ac6454SAndrew Thompson 	if (error) {
449a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
45002ac6454SAndrew Thompson 		goto detach;
45102ac6454SAndrew Thompson 	}
45202ac6454SAndrew Thompson 	/*Sysctls*/
45302ac6454SAndrew Thompson 	sctx = device_get_sysctl_ctx(dev);
45402ac6454SAndrew Thompson 	soid = device_get_sysctl_tree(dev);
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
45702ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_support,
45802ac6454SAndrew Thompson 			"A", "Supporting port role");
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
46102ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_current,
46202ac6454SAndrew Thompson 			"A", "Current port role");
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
46502ac6454SAndrew Thompson 			CTLFLAG_RW|CTLTYPE_STRING, sc, 0, ufoma_sysctl_open,
46602ac6454SAndrew Thompson 			"A", "Mode to transit when port is opened");
46702ac6454SAndrew Thompson 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
46802ac6454SAndrew Thompson 			CTLFLAG_RD, &(sc->sc_ucom.sc_unit), 0,
46902ac6454SAndrew Thompson 			"Unit number as USB serial");
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 	return (0);			/* success */
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson detach:
47402ac6454SAndrew Thompson 	ufoma_detach(dev);
47502ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
47602ac6454SAndrew Thompson }
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson static int
47902ac6454SAndrew Thompson ufoma_detach(device_t dev)
48002ac6454SAndrew Thompson {
48102ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
48202ac6454SAndrew Thompson 
483a593f6b8SAndrew Thompson 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
484a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
485a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	if (sc->sc_modetable) {
48802ac6454SAndrew Thompson 		free(sc->sc_modetable, M_USBDEV);
48902ac6454SAndrew Thompson 	}
490deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
4918437751dSAndrew Thompson 	cv_destroy(&sc->sc_cv);
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	return (0);
49402ac6454SAndrew Thompson }
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson static void *
497760bc48eSAndrew Thompson ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
49802ac6454SAndrew Thompson     uint8_t type, uint8_t subtype)
49902ac6454SAndrew Thompson {
500760bc48eSAndrew Thompson 	struct usb_descriptor *desc = (void *)id;
50102ac6454SAndrew Thompson 
502a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
50302ac6454SAndrew Thompson 
50402ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
50502ac6454SAndrew Thompson 			return (NULL);
50602ac6454SAndrew Thompson 		}
50702ac6454SAndrew Thompson 		if ((desc->bDescriptorType == type) &&
50802ac6454SAndrew Thompson 		    (desc->bDescriptorSubtype == subtype)) {
50902ac6454SAndrew Thompson 			break;
51002ac6454SAndrew Thompson 		}
51102ac6454SAndrew Thompson 	}
51202ac6454SAndrew Thompson 	return (desc);
51302ac6454SAndrew Thompson }
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson static void
51602ac6454SAndrew Thompson ufoma_cfg_link_state(struct ufoma_softc *sc)
51702ac6454SAndrew Thompson {
518760bc48eSAndrew Thompson 	struct usb_device_request req;
51902ac6454SAndrew Thompson 	int32_t error;
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
52202ac6454SAndrew Thompson 	req.bRequest = UMCPC_SET_LINK;
52302ac6454SAndrew Thompson 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
52402ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
52502ac6454SAndrew Thompson 	USETW(req.wLength, sc->sc_modetable[0]);
52602ac6454SAndrew Thompson 
527a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
52802ac6454SAndrew Thompson 	    &req, sc->sc_modetable, 0, 1000);
52902ac6454SAndrew Thompson 
5308437751dSAndrew Thompson 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	if (error) {
53302ac6454SAndrew Thompson 		DPRINTF("NO response\n");
53402ac6454SAndrew Thompson 	}
53502ac6454SAndrew Thompson }
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson static void
53802ac6454SAndrew Thompson ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
53902ac6454SAndrew Thompson {
540760bc48eSAndrew Thompson 	struct usb_device_request req;
54102ac6454SAndrew Thompson 	int32_t error;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
54402ac6454SAndrew Thompson 	req.bRequest = UMCPC_ACTIVATE_MODE;
54502ac6454SAndrew Thompson 	USETW(req.wValue, state);
54602ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
54702ac6454SAndrew Thompson 	USETW(req.wLength, 0);
54802ac6454SAndrew Thompson 
549a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
55002ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
55102ac6454SAndrew Thompson 
5528437751dSAndrew Thompson 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
55302ac6454SAndrew Thompson 	    (UFOMA_MAX_TIMEOUT * hz));
55402ac6454SAndrew Thompson 	if (error) {
55502ac6454SAndrew Thompson 		DPRINTF("No response\n");
55602ac6454SAndrew Thompson 	}
55702ac6454SAndrew Thompson }
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson static void
560ed6d949aSAndrew Thompson ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
56102ac6454SAndrew Thompson {
562ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
563760bc48eSAndrew Thompson 	struct usb_device_request req;
564ed6d949aSAndrew Thompson 	struct usb_page_cache *pc0, *pc1;
565ed6d949aSAndrew Thompson 	int len, aframes, nframes;
566ed6d949aSAndrew Thompson 
567ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
57002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
57102ac6454SAndrew Thompson tr_transferred:
572ed6d949aSAndrew Thompson 		if (aframes != nframes)
57302ac6454SAndrew Thompson 			goto tr_setup;
574ed6d949aSAndrew Thompson 		pc1 = usbd_xfer_get_frame(xfer, 1);
5758f9e0ef9SAndrew Thompson 		len = usbd_xfer_frame_len(xfer, 1);
576ed6d949aSAndrew Thompson 		if (len > 0)
577ed6d949aSAndrew Thompson 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
578ed6d949aSAndrew Thompson 		/* FALLTHROUGH */
57902ac6454SAndrew Thompson 	case USB_ST_SETUP:
58002ac6454SAndrew Thompson tr_setup:
58102ac6454SAndrew Thompson 		if (sc->sc_num_msg) {
58202ac6454SAndrew Thompson 			sc->sc_num_msg--;
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
58502ac6454SAndrew Thompson 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
58602ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
58702ac6454SAndrew Thompson 			USETW(req.wValue, 0);
58802ac6454SAndrew Thompson 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
58902ac6454SAndrew Thompson 
590ed6d949aSAndrew Thompson 			pc0 = usbd_xfer_get_frame(xfer, 0);
591ed6d949aSAndrew Thompson 			usbd_copy_in(pc0, 0, &req, sizeof(req));
59202ac6454SAndrew Thompson 
593ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
594ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
595ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 2);
596a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
59702ac6454SAndrew Thompson 		}
59802ac6454SAndrew Thompson 		return;
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	default:			/* Error */
60102ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
602ed6d949aSAndrew Thompson 		    usbd_errstr(error));
60302ac6454SAndrew Thompson 
604ed6d949aSAndrew Thompson 		if (error == USB_ERR_CANCELLED) {
60502ac6454SAndrew Thompson 			return;
60602ac6454SAndrew Thompson 		} else {
60702ac6454SAndrew Thompson 			goto tr_setup;
60802ac6454SAndrew Thompson 		}
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 		goto tr_transferred;
61102ac6454SAndrew Thompson 	}
61202ac6454SAndrew Thompson }
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson static void
615ed6d949aSAndrew Thompson ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
61602ac6454SAndrew Thompson {
617ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
618760bc48eSAndrew Thompson 	struct usb_device_request req;
619ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
62002ac6454SAndrew Thompson 	uint32_t actlen;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
62302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
62402ac6454SAndrew Thompson tr_transferred:
62502ac6454SAndrew Thompson 	case USB_ST_SETUP:
62602ac6454SAndrew Thompson tr_setup:
627ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
628ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
63102ac6454SAndrew Thompson 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
63202ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
63302ac6454SAndrew Thompson 			USETW(req.wValue, 0);
63402ac6454SAndrew Thompson 			USETW(req.wLength, 1);
63502ac6454SAndrew Thompson 
636ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
637ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
63802ac6454SAndrew Thompson 
639ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
640ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, 1);
641ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 2);
64202ac6454SAndrew Thompson 
643a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
64402ac6454SAndrew Thompson 		}
64502ac6454SAndrew Thompson 		return;
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	default:			/* Error */
648ed6d949aSAndrew Thompson 		DPRINTF("error = %s\n", usbd_errstr(error));
64902ac6454SAndrew Thompson 
650ed6d949aSAndrew Thompson 		if (error == USB_ERR_CANCELLED) {
65102ac6454SAndrew Thompson 			return;
65202ac6454SAndrew Thompson 		} else {
65302ac6454SAndrew Thompson 			goto tr_setup;
65402ac6454SAndrew Thompson 		}
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 		goto tr_transferred;
65702ac6454SAndrew Thompson 	}
65802ac6454SAndrew Thompson }
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson static void
661ed6d949aSAndrew Thompson ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
66202ac6454SAndrew Thompson {
663ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
664760bc48eSAndrew Thompson 	struct usb_cdc_notification pkt;
665ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
66602ac6454SAndrew Thompson 	uint16_t wLen;
66702ac6454SAndrew Thompson 	uint16_t temp;
66802ac6454SAndrew Thompson 	uint8_t mstatus;
669ed6d949aSAndrew Thompson 	int actlen;
670ed6d949aSAndrew Thompson 
671ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
67402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
675ed6d949aSAndrew Thompson 		if (actlen < 8) {
67602ac6454SAndrew Thompson 			DPRINTF("too short message\n");
67702ac6454SAndrew Thompson 			goto tr_setup;
67802ac6454SAndrew Thompson 		}
679ed6d949aSAndrew Thompson 		if (actlen > sizeof(pkt)) {
68002ac6454SAndrew Thompson 			DPRINTF("truncating message\n");
681ed6d949aSAndrew Thompson 			actlen = sizeof(pkt);
68202ac6454SAndrew Thompson 		}
683ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
684ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &pkt, actlen);
68502ac6454SAndrew Thompson 
686ed6d949aSAndrew Thompson 		actlen -= 8;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 		wLen = UGETW(pkt.wLength);
689ed6d949aSAndrew Thompson 		if (actlen > wLen) {
690ed6d949aSAndrew Thompson 			actlen = wLen;
69102ac6454SAndrew Thompson 		}
69202ac6454SAndrew Thompson 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
69302ac6454SAndrew Thompson 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
69402ac6454SAndrew Thompson 			temp = UGETW(pkt.wValue);
69502ac6454SAndrew Thompson 			sc->sc_currentmode = (temp >> 8);
69602ac6454SAndrew Thompson 			if (!(temp & 0xff)) {
69702ac6454SAndrew Thompson 				DPRINTF("Mode change failed!\n");
69802ac6454SAndrew Thompson 			}
6998437751dSAndrew Thompson 			cv_signal(&sc->sc_cv);
70002ac6454SAndrew Thompson 		}
70102ac6454SAndrew Thompson 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
70202ac6454SAndrew Thompson 			goto tr_setup;
70302ac6454SAndrew Thompson 		}
70402ac6454SAndrew Thompson 		switch (pkt.bNotification) {
70502ac6454SAndrew Thompson 		case UCDC_N_RESPONSE_AVAILABLE:
70602ac6454SAndrew Thompson 			if (!(sc->sc_nobulk)) {
70702ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
70802ac6454SAndrew Thompson 				break;
70902ac6454SAndrew Thompson 			}
71002ac6454SAndrew Thompson 			if (sc->sc_num_msg != 0xFF) {
71102ac6454SAndrew Thompson 				sc->sc_num_msg++;
71202ac6454SAndrew Thompson 			}
713a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
71402ac6454SAndrew Thompson 			break;
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 		case UCDC_N_SERIAL_STATE:
71702ac6454SAndrew Thompson 			if (sc->sc_nobulk) {
71802ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
71902ac6454SAndrew Thompson 				break;
72002ac6454SAndrew Thompson 			}
72102ac6454SAndrew Thompson 			/*
72202ac6454SAndrew Thompson 		         * Set the serial state in ucom driver based on
72302ac6454SAndrew Thompson 		         * the bits from the notify message
72402ac6454SAndrew Thompson 		         */
725ed6d949aSAndrew Thompson 			if (actlen < 2) {
72602ac6454SAndrew Thompson 				DPRINTF("invalid notification "
727ed6d949aSAndrew Thompson 				    "length, %d bytes!\n", actlen);
72802ac6454SAndrew Thompson 				break;
72902ac6454SAndrew Thompson 			}
73002ac6454SAndrew Thompson 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
73102ac6454SAndrew Thompson 			    pkt.data[0], pkt.data[1]);
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson 			/* currently, lsr is always zero. */
73402ac6454SAndrew Thompson 			sc->sc_lsr = 0;
73502ac6454SAndrew Thompson 			sc->sc_msr = 0;
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 			mstatus = pkt.data[0];
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_RI) {
74002ac6454SAndrew Thompson 				sc->sc_msr |= SER_RI;
74102ac6454SAndrew Thompson 			}
74202ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DSR) {
74302ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
74402ac6454SAndrew Thompson 			}
74502ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DCD) {
74602ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
74702ac6454SAndrew Thompson 			}
748a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
74902ac6454SAndrew Thompson 			break;
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 		default:
75202ac6454SAndrew Thompson 			break;
75302ac6454SAndrew Thompson 		}
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 	case USB_ST_SETUP:
75602ac6454SAndrew Thompson tr_setup:
757ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
758a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
75902ac6454SAndrew Thompson 		return;
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 	default:			/* Error */
762ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
76302ac6454SAndrew Thompson 			/* try to clear stall first */
764ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
76502ac6454SAndrew Thompson 			goto tr_setup;
76602ac6454SAndrew Thompson 		}
76702ac6454SAndrew Thompson 		return;
76802ac6454SAndrew Thompson 	}
76902ac6454SAndrew Thompson }
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson static void
772ed6d949aSAndrew Thompson ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
77302ac6454SAndrew Thompson {
774ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
775ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
77602ac6454SAndrew Thompson 	uint32_t actlen;
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
77902ac6454SAndrew Thompson 	case USB_ST_SETUP:
78002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
78102ac6454SAndrew Thompson tr_setup:
782ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
783ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
78402ac6454SAndrew Thompson 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
785ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
786a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
78702ac6454SAndrew Thompson 		}
78802ac6454SAndrew Thompson 		return;
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	default:			/* Error */
791ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
79202ac6454SAndrew Thompson 			/* try to clear stall first */
793ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79402ac6454SAndrew Thompson 			goto tr_setup;
79502ac6454SAndrew Thompson 		}
79602ac6454SAndrew Thompson 		return;
79702ac6454SAndrew Thompson 	}
79802ac6454SAndrew Thompson }
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson static void
801ed6d949aSAndrew Thompson ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
80202ac6454SAndrew Thompson {
803ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
804ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
805ed6d949aSAndrew Thompson 	int actlen;
806ed6d949aSAndrew Thompson 
807ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
81002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
811ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
812ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
81302ac6454SAndrew Thompson 
81402ac6454SAndrew Thompson 	case USB_ST_SETUP:
81502ac6454SAndrew Thompson tr_setup:
816ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
817a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
81802ac6454SAndrew Thompson 		return;
81902ac6454SAndrew Thompson 
82002ac6454SAndrew Thompson 	default:			/* Error */
821ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
82202ac6454SAndrew Thompson 			/* try to clear stall first */
823ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
82402ac6454SAndrew Thompson 			goto tr_setup;
82502ac6454SAndrew Thompson 		}
82602ac6454SAndrew Thompson 		return;
82702ac6454SAndrew Thompson 	}
82802ac6454SAndrew Thompson }
82902ac6454SAndrew Thompson 
83002ac6454SAndrew Thompson static void
831760bc48eSAndrew Thompson ufoma_cfg_open(struct ucom_softc *ucom)
83202ac6454SAndrew Thompson {
83302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 	/* empty input queue */
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 	if (sc->sc_num_msg != 0xFF) {
83802ac6454SAndrew Thompson 		sc->sc_num_msg++;
83902ac6454SAndrew Thompson 	}
84002ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
84102ac6454SAndrew Thompson 		ufoma_cfg_link_state(sc);
84202ac6454SAndrew Thompson 	}
84302ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
84402ac6454SAndrew Thompson 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
84502ac6454SAndrew Thompson 	}
84602ac6454SAndrew Thompson }
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson static void
849760bc48eSAndrew Thompson ufoma_cfg_close(struct ucom_softc *ucom)
85002ac6454SAndrew Thompson {
85102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
85402ac6454SAndrew Thompson }
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson static void
857760bc48eSAndrew Thompson ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
85802ac6454SAndrew Thompson {
85902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
860760bc48eSAndrew Thompson 	struct usb_device_request req;
86102ac6454SAndrew Thompson 	uint16_t wValue;
86202ac6454SAndrew Thompson 
86302ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
86402ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
86502ac6454SAndrew Thompson 		return;
86602ac6454SAndrew Thompson 	}
86702ac6454SAndrew Thompson 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
86802ac6454SAndrew Thompson 		return;
86902ac6454SAndrew Thompson 	}
87002ac6454SAndrew Thompson 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
87302ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
87402ac6454SAndrew Thompson 	USETW(req.wValue, wValue);
87502ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
87602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
87702ac6454SAndrew Thompson 	USETW(req.wLength, 0);
87802ac6454SAndrew Thompson 
879a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
88002ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
88102ac6454SAndrew Thompson }
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson static void
884760bc48eSAndrew Thompson ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
88502ac6454SAndrew Thompson {
88602ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
88902ac6454SAndrew Thompson 	*msr = sc->sc_msr;
89002ac6454SAndrew Thompson }
89102ac6454SAndrew Thompson 
89202ac6454SAndrew Thompson static void
89302ac6454SAndrew Thompson ufoma_cfg_set_line_state(struct ufoma_softc *sc)
89402ac6454SAndrew Thompson {
895760bc48eSAndrew Thompson 	struct usb_device_request req;
89602ac6454SAndrew Thompson 
89702ac6454SAndrew Thompson 	/* Don't send line state emulation request for OBEX port */
89802ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
89902ac6454SAndrew Thompson 		return;
90002ac6454SAndrew Thompson 	}
90102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
90202ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
90302ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
90402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
90502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
90602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
90702ac6454SAndrew Thompson 
908a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
90902ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
91002ac6454SAndrew Thompson }
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson static void
913760bc48eSAndrew Thompson ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
91402ac6454SAndrew Thompson {
91502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
91602ac6454SAndrew Thompson 
91702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
91802ac6454SAndrew Thompson 		return;
91902ac6454SAndrew Thompson 	}
92002ac6454SAndrew Thompson 	if (onoff)
92102ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
92202ac6454SAndrew Thompson 	else
92302ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
92602ac6454SAndrew Thompson }
92702ac6454SAndrew Thompson 
92802ac6454SAndrew Thompson static void
929760bc48eSAndrew Thompson ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
93002ac6454SAndrew Thompson {
93102ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
93402ac6454SAndrew Thompson 		return;
93502ac6454SAndrew Thompson 	}
93602ac6454SAndrew Thompson 	if (onoff)
93702ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
93802ac6454SAndrew Thompson 	else
93902ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
94202ac6454SAndrew Thompson }
94302ac6454SAndrew Thompson 
94402ac6454SAndrew Thompson static int
945760bc48eSAndrew Thompson ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
94602ac6454SAndrew Thompson {
94702ac6454SAndrew Thompson 	return (0);			/* we accept anything */
94802ac6454SAndrew Thompson }
94902ac6454SAndrew Thompson 
95002ac6454SAndrew Thompson static void
951760bc48eSAndrew Thompson ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
95202ac6454SAndrew Thompson {
95302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
954760bc48eSAndrew Thompson 	struct usb_device_request req;
955760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
95602ac6454SAndrew Thompson 
95702ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
95802ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
95902ac6454SAndrew Thompson 		return;
96002ac6454SAndrew Thompson 	}
96102ac6454SAndrew Thompson 	DPRINTF("\n");
96202ac6454SAndrew Thompson 
96302ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
96402ac6454SAndrew Thompson 
96502ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
96802ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
96902ac6454SAndrew Thompson 	} else {
97002ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
97102ac6454SAndrew Thompson 	}
97202ac6454SAndrew Thompson 
97302ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
97402ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
97502ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
97602ac6454SAndrew Thompson 		} else {
97702ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
97802ac6454SAndrew Thompson 		}
97902ac6454SAndrew Thompson 	} else {
98002ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
98102ac6454SAndrew Thompson 	}
98202ac6454SAndrew Thompson 
98302ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
98402ac6454SAndrew Thompson 	case CS5:
98502ac6454SAndrew Thompson 		ls.bDataBits = 5;
98602ac6454SAndrew Thompson 		break;
98702ac6454SAndrew Thompson 	case CS6:
98802ac6454SAndrew Thompson 		ls.bDataBits = 6;
98902ac6454SAndrew Thompson 		break;
99002ac6454SAndrew Thompson 	case CS7:
99102ac6454SAndrew Thompson 		ls.bDataBits = 7;
99202ac6454SAndrew Thompson 		break;
99302ac6454SAndrew Thompson 	case CS8:
99402ac6454SAndrew Thompson 		ls.bDataBits = 8;
99502ac6454SAndrew Thompson 		break;
99602ac6454SAndrew Thompson 	}
99702ac6454SAndrew Thompson 
99802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
99902ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
100002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
100102ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
100202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
100302ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
100402ac6454SAndrew Thompson 
1005a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
100602ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
100702ac6454SAndrew Thompson }
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson static int
101002ac6454SAndrew Thompson ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1011760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
101202ac6454SAndrew Thompson {
1013760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
1014760bc48eSAndrew Thompson 	struct usb_cdc_acm_descriptor *acm;
1015760bc48eSAndrew Thompson 	struct usb_cdc_cm_descriptor *cmd;
1016760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
1017760bc48eSAndrew Thompson 	struct usb_interface *iface;
101802ac6454SAndrew Thompson 	uint8_t i;
101902ac6454SAndrew Thompson 	int32_t error;
102002ac6454SAndrew Thompson 
1021a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
1022a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
102302ac6454SAndrew Thompson 
102402ac6454SAndrew Thompson 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
102502ac6454SAndrew Thompson 
102602ac6454SAndrew Thompson 	if ((cmd == NULL) ||
102702ac6454SAndrew Thompson 	    (cmd->bLength < sizeof(*cmd))) {
102802ac6454SAndrew Thompson 		return (EINVAL);
102902ac6454SAndrew Thompson 	}
103002ac6454SAndrew Thompson 	sc->sc_cm_cap = cmd->bmCapabilities;
103102ac6454SAndrew Thompson 	sc->sc_data_iface_no = cmd->bDataInterface;
103202ac6454SAndrew Thompson 
103302ac6454SAndrew Thompson 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson 	if ((acm == NULL) ||
103602ac6454SAndrew Thompson 	    (acm->bLength < sizeof(*acm))) {
103702ac6454SAndrew Thompson 		return (EINVAL);
103802ac6454SAndrew Thompson 	}
103902ac6454SAndrew Thompson 	sc->sc_acm_cap = acm->bmCapabilities;
104002ac6454SAndrew Thompson 
104102ac6454SAndrew Thompson 	device_printf(dev, "data interface %d, has %sCM over data, "
104202ac6454SAndrew Thompson 	    "has %sbreak\n",
104302ac6454SAndrew Thompson 	    sc->sc_data_iface_no,
104402ac6454SAndrew Thompson 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
104502ac6454SAndrew Thompson 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 	/* get the data interface too */
104802ac6454SAndrew Thompson 
104902ac6454SAndrew Thompson 	for (i = 0;; i++) {
105002ac6454SAndrew Thompson 
1051a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson 		if (iface) {
105402ac6454SAndrew Thompson 
1055a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
105802ac6454SAndrew Thompson 				sc->sc_data_iface_index = i;
1059a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
106002ac6454SAndrew Thompson 				break;
106102ac6454SAndrew Thompson 			}
106202ac6454SAndrew Thompson 		} else {
106302ac6454SAndrew Thompson 			device_printf(dev, "no data interface!\n");
106402ac6454SAndrew Thompson 			return (EINVAL);
106502ac6454SAndrew Thompson 		}
106602ac6454SAndrew Thompson 	}
106702ac6454SAndrew Thompson 
1068a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
106902ac6454SAndrew Thompson 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1070deefe583SAndrew Thompson 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
107102ac6454SAndrew Thompson 
107202ac6454SAndrew Thompson 	if (error) {
107302ac6454SAndrew Thompson 		device_printf(dev, "allocating BULK USB "
107402ac6454SAndrew Thompson 		    "transfers failed!\n");
107502ac6454SAndrew Thompson 		return (EINVAL);
107602ac6454SAndrew Thompson 	}
107702ac6454SAndrew Thompson 	return (0);
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson static void
1081760bc48eSAndrew Thompson ufoma_start_read(struct ucom_softc *ucom)
108202ac6454SAndrew Thompson {
108302ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
108402ac6454SAndrew Thompson 
108502ac6454SAndrew Thompson 	/* start interrupt transfer */
1086a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	/* start data transfer */
108902ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1090a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
109102ac6454SAndrew Thompson 	} else {
1092a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
109302ac6454SAndrew Thompson 	}
109402ac6454SAndrew Thompson }
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson static void
1097760bc48eSAndrew Thompson ufoma_stop_read(struct ucom_softc *ucom)
109802ac6454SAndrew Thompson {
109902ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 	/* stop interrupt transfer */
1102a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 	/* stop data transfer */
110502ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1106a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
110702ac6454SAndrew Thompson 	} else {
1108a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
110902ac6454SAndrew Thompson 	}
111002ac6454SAndrew Thompson }
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson static void
1113760bc48eSAndrew Thompson ufoma_start_write(struct ucom_softc *ucom)
111402ac6454SAndrew Thompson {
111502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
111602ac6454SAndrew Thompson 
111702ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1118a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
111902ac6454SAndrew Thompson 	} else {
1120a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
112102ac6454SAndrew Thompson 	}
112202ac6454SAndrew Thompson }
112302ac6454SAndrew Thompson 
112402ac6454SAndrew Thompson static void
1125760bc48eSAndrew Thompson ufoma_stop_write(struct ucom_softc *ucom)
112602ac6454SAndrew Thompson {
112702ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
112802ac6454SAndrew Thompson 
112902ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1130a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
113102ac6454SAndrew Thompson 	} else {
1132a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
113302ac6454SAndrew Thompson 	}
113402ac6454SAndrew Thompson }
113502ac6454SAndrew Thompson 
1136a8675caeSAndrew Thompson static struct umcpc_modetostr_tab{
113702ac6454SAndrew Thompson 	int mode;
113802ac6454SAndrew Thompson 	char *str;
113902ac6454SAndrew Thompson }umcpc_modetostr_tab[]={
114002ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
114102ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_MODEM, "modem"},
114202ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
114302ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_OBEX, "obex"},
114402ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
114502ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
114602ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
114702ac6454SAndrew Thompson 	{0, NULL}
114802ac6454SAndrew Thompson };
114902ac6454SAndrew Thompson 
115002ac6454SAndrew Thompson static char *ufoma_mode_to_str(int mode)
115102ac6454SAndrew Thompson {
115202ac6454SAndrew Thompson 	int i;
115302ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
115402ac6454SAndrew Thompson 		if(umcpc_modetostr_tab[i].mode == mode){
115502ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].str;
115602ac6454SAndrew Thompson 		}
115702ac6454SAndrew Thompson 	}
115802ac6454SAndrew Thompson 	return NULL;
115902ac6454SAndrew Thompson }
116002ac6454SAndrew Thompson 
116102ac6454SAndrew Thompson static int ufoma_str_to_mode(char *str)
116202ac6454SAndrew Thompson {
116302ac6454SAndrew Thompson 	int i;
116402ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
116502ac6454SAndrew Thompson 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
116602ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].mode;
116702ac6454SAndrew Thompson 		}
116802ac6454SAndrew Thompson 	}
116902ac6454SAndrew Thompson 	return -1;
117002ac6454SAndrew Thompson }
117102ac6454SAndrew Thompson 
117202ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
117302ac6454SAndrew Thompson {
117402ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
117502ac6454SAndrew Thompson 	struct sbuf sb;
117602ac6454SAndrew Thompson 	int i;
117702ac6454SAndrew Thompson 	char *mode;
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
118002ac6454SAndrew Thompson 	for(i = 1; i < sc->sc_modetable[0]; i++){
118102ac6454SAndrew Thompson 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
118202ac6454SAndrew Thompson 		if(mode !=NULL){
118302ac6454SAndrew Thompson 			sbuf_cat(&sb, mode);
118402ac6454SAndrew Thompson 		}else{
118502ac6454SAndrew Thompson 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
118602ac6454SAndrew Thompson 		}
118702ac6454SAndrew Thompson 		if(i < (sc->sc_modetable[0]-1))
118802ac6454SAndrew Thompson 			sbuf_cat(&sb, ",");
118902ac6454SAndrew Thompson 	}
119002ac6454SAndrew Thompson 	sbuf_trim(&sb);
119102ac6454SAndrew Thompson 	sbuf_finish(&sb);
119202ac6454SAndrew Thompson 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
119302ac6454SAndrew Thompson 	sbuf_delete(&sb);
119402ac6454SAndrew Thompson 
119502ac6454SAndrew Thompson 	return 0;
119602ac6454SAndrew Thompson }
119702ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
119802ac6454SAndrew Thompson {
119902ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
120002ac6454SAndrew Thompson 	char *mode;
120102ac6454SAndrew Thompson 	char subbuf[]="(XXX)";
120202ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_currentmode);
120302ac6454SAndrew Thompson 	if(!mode){
120402ac6454SAndrew Thompson 		mode = subbuf;
120502ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
120602ac6454SAndrew Thompson 	}
120702ac6454SAndrew Thompson 	sysctl_handle_string(oidp, mode, strlen(mode), req);
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	return 0;
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson }
121202ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
121302ac6454SAndrew Thompson {
121402ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
121502ac6454SAndrew Thompson 	char *mode;
121602ac6454SAndrew Thompson 	char subbuf[40];
121702ac6454SAndrew Thompson 	int newmode;
121802ac6454SAndrew Thompson 	int error;
121902ac6454SAndrew Thompson 	int i;
122002ac6454SAndrew Thompson 
122102ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
122202ac6454SAndrew Thompson 	if(mode){
122302ac6454SAndrew Thompson 		strncpy(subbuf, mode, sizeof(subbuf));
122402ac6454SAndrew Thompson 	}else{
122502ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
122602ac6454SAndrew Thompson 	}
122702ac6454SAndrew Thompson 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
122802ac6454SAndrew Thompson 	if(error != 0 || req->newptr == NULL){
122902ac6454SAndrew Thompson 		return error;
123002ac6454SAndrew Thompson 	}
123102ac6454SAndrew Thompson 
123202ac6454SAndrew Thompson 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
123302ac6454SAndrew Thompson 		return EINVAL;
123402ac6454SAndrew Thompson 	}
123502ac6454SAndrew Thompson 
123602ac6454SAndrew Thompson 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
123702ac6454SAndrew Thompson 		if(sc->sc_modetable[i] == newmode){
123802ac6454SAndrew Thompson 			sc->sc_modetoactivate = newmode;
123902ac6454SAndrew Thompson 			return 0;
124002ac6454SAndrew Thompson 		}
124102ac6454SAndrew Thompson 	}
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson 	return EINVAL;
124402ac6454SAndrew Thompson }
1245655dc9d0SAndrew Thompson 
1246655dc9d0SAndrew Thompson static void
1247655dc9d0SAndrew Thompson ufoma_poll(struct ucom_softc *ucom)
1248655dc9d0SAndrew Thompson {
1249655dc9d0SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
1250655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1251655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1252655dc9d0SAndrew Thompson }
1253