xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 8f9e0ef947d29b8728c62a941c13e127120701fc)
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 *);
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson /*sysctl stuff*/
23502ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
23602ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
23702ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
23802ac6454SAndrew Thompson 
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,
30702ac6454SAndrew Thompson };
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson static device_method_t ufoma_methods[] = {
31002ac6454SAndrew Thompson 	/* Device methods */
31102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ufoma_probe),
31202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ufoma_attach),
31302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ufoma_detach),
31402ac6454SAndrew Thompson 	{0, 0}
31502ac6454SAndrew Thompson };
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson static devclass_t ufoma_devclass;
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson static driver_t ufoma_driver = {
32002ac6454SAndrew Thompson 	.name = "ufoma",
32102ac6454SAndrew Thompson 	.methods = ufoma_methods,
32202ac6454SAndrew Thompson 	.size = sizeof(struct ufoma_softc),
32302ac6454SAndrew Thompson };
32402ac6454SAndrew Thompson 
3259aef556dSAndrew Thompson DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
32602ac6454SAndrew Thompson MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
32702ac6454SAndrew Thompson MODULE_DEPEND(ufoma, usb, 1, 1, 1);
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson static int
33002ac6454SAndrew Thompson ufoma_probe(device_t dev)
33102ac6454SAndrew Thompson {
332760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
333760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
334760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
335a593f6b8SAndrew Thompson 	usb_mcpc_acm_descriptor *mad;
33602ac6454SAndrew Thompson 
337f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
33802ac6454SAndrew Thompson 		return (ENXIO);
33902ac6454SAndrew Thompson 	}
340a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
341a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	if ((id == NULL) ||
34402ac6454SAndrew Thompson 	    (cd == NULL) ||
34502ac6454SAndrew Thompson 	    (id->bInterfaceClass != UICLASS_CDC) ||
34602ac6454SAndrew Thompson 	    (id->bInterfaceSubClass != UISUBCLASS_MCPC)) {
34702ac6454SAndrew Thompson 		return (ENXIO);
34802ac6454SAndrew Thompson 	}
34902ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
35002ac6454SAndrew Thompson 	if (mad == NULL) {
35102ac6454SAndrew Thompson 		return (ENXIO);
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson #ifndef UFOMA_HANDSFREE
35402ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
35502ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
35602ac6454SAndrew Thompson 		return (ENXIO);
35702ac6454SAndrew Thompson 	}
35802ac6454SAndrew Thompson #endif
35902ac6454SAndrew Thompson 	return (0);
36002ac6454SAndrew Thompson }
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson static int
36302ac6454SAndrew Thompson ufoma_attach(device_t dev)
36402ac6454SAndrew Thompson {
365760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
36602ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
367760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
368760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
36902ac6454SAndrew Thompson 	struct sysctl_ctx_list *sctx;
37002ac6454SAndrew Thompson 	struct sysctl_oid *soid;
37102ac6454SAndrew Thompson 
372a593f6b8SAndrew Thompson 	usb_mcpc_acm_descriptor *mad;
37302ac6454SAndrew Thompson 	uint8_t elements;
37402ac6454SAndrew Thompson 	int32_t error;
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
37702ac6454SAndrew Thompson 	sc->sc_dev = dev;
37802ac6454SAndrew Thompson 	sc->sc_unit = device_get_unit(dev);
37902ac6454SAndrew Thompson 
380deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
3818437751dSAndrew Thompson 	cv_init(&sc->sc_cv, "CWAIT");
38202ac6454SAndrew Thompson 
383a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
38602ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 	DPRINTF("\n");
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	/* setup control transfers */
39102ac6454SAndrew Thompson 
392a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
393a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
39402ac6454SAndrew Thompson 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
39502ac6454SAndrew Thompson 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
39602ac6454SAndrew Thompson 
397a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
39802ac6454SAndrew Thompson 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
399deefe583SAndrew Thompson 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 	if (error) {
40202ac6454SAndrew Thompson 		device_printf(dev, "allocating control USB "
40302ac6454SAndrew Thompson 		    "transfers failed!\n");
40402ac6454SAndrew Thompson 		goto detach;
40502ac6454SAndrew Thompson 	}
40602ac6454SAndrew Thompson 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
40702ac6454SAndrew Thompson 	if (mad == NULL) {
40802ac6454SAndrew Thompson 		goto detach;
40902ac6454SAndrew Thompson 	}
41002ac6454SAndrew Thompson 	if (mad->bFunctionLength < sizeof(*mad)) {
41102ac6454SAndrew Thompson 		device_printf(dev, "invalid MAD descriptor\n");
41202ac6454SAndrew Thompson 		goto detach;
41302ac6454SAndrew Thompson 	}
41402ac6454SAndrew Thompson 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
41502ac6454SAndrew Thompson 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
41602ac6454SAndrew Thompson 		sc->sc_nobulk = 1;
41702ac6454SAndrew Thompson 	} else {
41802ac6454SAndrew Thompson 		sc->sc_nobulk = 0;
41902ac6454SAndrew Thompson 		if (ufoma_modem_setup(dev, sc, uaa)) {
42002ac6454SAndrew Thompson 			goto detach;
42102ac6454SAndrew Thompson 		}
42202ac6454SAndrew Thompson 	}
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson 	/* initialize mode variables */
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 	if (sc->sc_modetable == NULL) {
43102ac6454SAndrew Thompson 		goto detach;
43202ac6454SAndrew Thompson 	}
43302ac6454SAndrew Thompson 	sc->sc_modetable[0] = (elements + 1);
43402ac6454SAndrew Thompson 	bcopy(mad->bMode, &sc->sc_modetable[1], elements);
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
43702ac6454SAndrew Thompson 	sc->sc_modetoactivate = mad->bMode[0];
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson 	/* clear stall at first run, if any */
440deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
441ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
442ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
443deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
44402ac6454SAndrew Thompson 
445a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
446deefe583SAndrew Thompson 	    &ufoma_callback, &sc->sc_mtx);
44702ac6454SAndrew Thompson 	if (error) {
448a593f6b8SAndrew Thompson 		DPRINTF("ucom_attach failed\n");
44902ac6454SAndrew Thompson 		goto detach;
45002ac6454SAndrew Thompson 	}
45102ac6454SAndrew Thompson 	/*Sysctls*/
45202ac6454SAndrew Thompson 	sctx = device_get_sysctl_ctx(dev);
45302ac6454SAndrew Thompson 	soid = device_get_sysctl_tree(dev);
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
45602ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_support,
45702ac6454SAndrew Thompson 			"A", "Supporting port role");
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
46002ac6454SAndrew Thompson 			CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_current,
46102ac6454SAndrew Thompson 			"A", "Current port role");
46202ac6454SAndrew Thompson 
46302ac6454SAndrew Thompson 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
46402ac6454SAndrew Thompson 			CTLFLAG_RW|CTLTYPE_STRING, sc, 0, ufoma_sysctl_open,
46502ac6454SAndrew Thompson 			"A", "Mode to transit when port is opened");
46602ac6454SAndrew Thompson 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
46702ac6454SAndrew Thompson 			CTLFLAG_RD, &(sc->sc_ucom.sc_unit), 0,
46802ac6454SAndrew Thompson 			"Unit number as USB serial");
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 	return (0);			/* success */
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson detach:
47302ac6454SAndrew Thompson 	ufoma_detach(dev);
47402ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
47502ac6454SAndrew Thompson }
47602ac6454SAndrew Thompson 
47702ac6454SAndrew Thompson static int
47802ac6454SAndrew Thompson ufoma_detach(device_t dev)
47902ac6454SAndrew Thompson {
48002ac6454SAndrew Thompson 	struct ufoma_softc *sc = device_get_softc(dev);
48102ac6454SAndrew Thompson 
482a593f6b8SAndrew Thompson 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
483a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
484a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 	if (sc->sc_modetable) {
48702ac6454SAndrew Thompson 		free(sc->sc_modetable, M_USBDEV);
48802ac6454SAndrew Thompson 	}
489deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
4908437751dSAndrew Thompson 	cv_destroy(&sc->sc_cv);
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 	return (0);
49302ac6454SAndrew Thompson }
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson static void *
496760bc48eSAndrew Thompson ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
49702ac6454SAndrew Thompson     uint8_t type, uint8_t subtype)
49802ac6454SAndrew Thompson {
499760bc48eSAndrew Thompson 	struct usb_descriptor *desc = (void *)id;
50002ac6454SAndrew Thompson 
501a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
50402ac6454SAndrew Thompson 			return (NULL);
50502ac6454SAndrew Thompson 		}
50602ac6454SAndrew Thompson 		if ((desc->bDescriptorType == type) &&
50702ac6454SAndrew Thompson 		    (desc->bDescriptorSubtype == subtype)) {
50802ac6454SAndrew Thompson 			break;
50902ac6454SAndrew Thompson 		}
51002ac6454SAndrew Thompson 	}
51102ac6454SAndrew Thompson 	return (desc);
51202ac6454SAndrew Thompson }
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson static void
51502ac6454SAndrew Thompson ufoma_cfg_link_state(struct ufoma_softc *sc)
51602ac6454SAndrew Thompson {
517760bc48eSAndrew Thompson 	struct usb_device_request req;
51802ac6454SAndrew Thompson 	int32_t error;
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
52102ac6454SAndrew Thompson 	req.bRequest = UMCPC_SET_LINK;
52202ac6454SAndrew Thompson 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
52302ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
52402ac6454SAndrew Thompson 	USETW(req.wLength, sc->sc_modetable[0]);
52502ac6454SAndrew Thompson 
526a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
52702ac6454SAndrew Thompson 	    &req, sc->sc_modetable, 0, 1000);
52802ac6454SAndrew Thompson 
5298437751dSAndrew Thompson 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 	if (error) {
53202ac6454SAndrew Thompson 		DPRINTF("NO response\n");
53302ac6454SAndrew Thompson 	}
53402ac6454SAndrew Thompson }
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson static void
53702ac6454SAndrew Thompson ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
53802ac6454SAndrew Thompson {
539760bc48eSAndrew Thompson 	struct usb_device_request req;
54002ac6454SAndrew Thompson 	int32_t error;
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
54302ac6454SAndrew Thompson 	req.bRequest = UMCPC_ACTIVATE_MODE;
54402ac6454SAndrew Thompson 	USETW(req.wValue, state);
54502ac6454SAndrew Thompson 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
54602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
54702ac6454SAndrew Thompson 
548a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
54902ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
55002ac6454SAndrew Thompson 
5518437751dSAndrew Thompson 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
55202ac6454SAndrew Thompson 	    (UFOMA_MAX_TIMEOUT * hz));
55302ac6454SAndrew Thompson 	if (error) {
55402ac6454SAndrew Thompson 		DPRINTF("No response\n");
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson }
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson static void
559ed6d949aSAndrew Thompson ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
56002ac6454SAndrew Thompson {
561ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
562760bc48eSAndrew Thompson 	struct usb_device_request req;
563ed6d949aSAndrew Thompson 	struct usb_page_cache *pc0, *pc1;
564ed6d949aSAndrew Thompson 	int len, aframes, nframes;
565ed6d949aSAndrew Thompson 
566ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
56902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
57002ac6454SAndrew Thompson tr_transferred:
571ed6d949aSAndrew Thompson 		if (aframes != nframes)
57202ac6454SAndrew Thompson 			goto tr_setup;
573ed6d949aSAndrew Thompson 		pc1 = usbd_xfer_get_frame(xfer, 1);
5748f9e0ef9SAndrew Thompson 		len = usbd_xfer_frame_len(xfer, 1);
575ed6d949aSAndrew Thompson 		if (len > 0)
576ed6d949aSAndrew Thompson 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
577ed6d949aSAndrew Thompson 		/* FALLTHROUGH */
57802ac6454SAndrew Thompson 	case USB_ST_SETUP:
57902ac6454SAndrew Thompson tr_setup:
58002ac6454SAndrew Thompson 		if (sc->sc_num_msg) {
58102ac6454SAndrew Thompson 			sc->sc_num_msg--;
58202ac6454SAndrew Thompson 
58302ac6454SAndrew Thompson 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
58402ac6454SAndrew Thompson 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
58502ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
58602ac6454SAndrew Thompson 			USETW(req.wValue, 0);
58702ac6454SAndrew Thompson 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
58802ac6454SAndrew Thompson 
589ed6d949aSAndrew Thompson 			pc0 = usbd_xfer_get_frame(xfer, 0);
590ed6d949aSAndrew Thompson 			usbd_copy_in(pc0, 0, &req, sizeof(req));
59102ac6454SAndrew Thompson 
592ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
593ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
594ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 2);
595a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
59602ac6454SAndrew Thompson 		}
59702ac6454SAndrew Thompson 		return;
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	default:			/* Error */
60002ac6454SAndrew Thompson 		DPRINTF("error = %s\n",
601ed6d949aSAndrew Thompson 		    usbd_errstr(error));
60202ac6454SAndrew Thompson 
603ed6d949aSAndrew Thompson 		if (error == USB_ERR_CANCELLED) {
60402ac6454SAndrew Thompson 			return;
60502ac6454SAndrew Thompson 		} else {
60602ac6454SAndrew Thompson 			goto tr_setup;
60702ac6454SAndrew Thompson 		}
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson 		goto tr_transferred;
61002ac6454SAndrew Thompson 	}
61102ac6454SAndrew Thompson }
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson static void
614ed6d949aSAndrew Thompson ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
61502ac6454SAndrew Thompson {
616ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
617760bc48eSAndrew Thompson 	struct usb_device_request req;
618ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
61902ac6454SAndrew Thompson 	uint32_t actlen;
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
62202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
62302ac6454SAndrew Thompson tr_transferred:
62402ac6454SAndrew Thompson 	case USB_ST_SETUP:
62502ac6454SAndrew Thompson tr_setup:
626ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
627ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
63002ac6454SAndrew Thompson 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
63102ac6454SAndrew Thompson 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
63202ac6454SAndrew Thompson 			USETW(req.wValue, 0);
63302ac6454SAndrew Thompson 			USETW(req.wLength, 1);
63402ac6454SAndrew Thompson 
635ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
636ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
63702ac6454SAndrew Thompson 
638ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
639ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, 1);
640ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, 2);
64102ac6454SAndrew Thompson 
642a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
64302ac6454SAndrew Thompson 		}
64402ac6454SAndrew Thompson 		return;
64502ac6454SAndrew Thompson 
64602ac6454SAndrew Thompson 	default:			/* Error */
647ed6d949aSAndrew Thompson 		DPRINTF("error = %s\n", usbd_errstr(error));
64802ac6454SAndrew Thompson 
649ed6d949aSAndrew Thompson 		if (error == USB_ERR_CANCELLED) {
65002ac6454SAndrew Thompson 			return;
65102ac6454SAndrew Thompson 		} else {
65202ac6454SAndrew Thompson 			goto tr_setup;
65302ac6454SAndrew Thompson 		}
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 		goto tr_transferred;
65602ac6454SAndrew Thompson 	}
65702ac6454SAndrew Thompson }
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson static void
660ed6d949aSAndrew Thompson ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
66102ac6454SAndrew Thompson {
662ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
663760bc48eSAndrew Thompson 	struct usb_cdc_notification pkt;
664ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
66502ac6454SAndrew Thompson 	uint16_t wLen;
66602ac6454SAndrew Thompson 	uint16_t temp;
66702ac6454SAndrew Thompson 	uint8_t mstatus;
668ed6d949aSAndrew Thompson 	int actlen;
669ed6d949aSAndrew Thompson 
670ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
67302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
674ed6d949aSAndrew Thompson 		if (actlen < 8) {
67502ac6454SAndrew Thompson 			DPRINTF("too short message\n");
67602ac6454SAndrew Thompson 			goto tr_setup;
67702ac6454SAndrew Thompson 		}
678ed6d949aSAndrew Thompson 		if (actlen > sizeof(pkt)) {
67902ac6454SAndrew Thompson 			DPRINTF("truncating message\n");
680ed6d949aSAndrew Thompson 			actlen = sizeof(pkt);
68102ac6454SAndrew Thompson 		}
682ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
683ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &pkt, actlen);
68402ac6454SAndrew Thompson 
685ed6d949aSAndrew Thompson 		actlen -= 8;
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 		wLen = UGETW(pkt.wLength);
688ed6d949aSAndrew Thompson 		if (actlen > wLen) {
689ed6d949aSAndrew Thompson 			actlen = wLen;
69002ac6454SAndrew Thompson 		}
69102ac6454SAndrew Thompson 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
69202ac6454SAndrew Thompson 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
69302ac6454SAndrew Thompson 			temp = UGETW(pkt.wValue);
69402ac6454SAndrew Thompson 			sc->sc_currentmode = (temp >> 8);
69502ac6454SAndrew Thompson 			if (!(temp & 0xff)) {
69602ac6454SAndrew Thompson 				DPRINTF("Mode change failed!\n");
69702ac6454SAndrew Thompson 			}
6988437751dSAndrew Thompson 			cv_signal(&sc->sc_cv);
69902ac6454SAndrew Thompson 		}
70002ac6454SAndrew Thompson 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
70102ac6454SAndrew Thompson 			goto tr_setup;
70202ac6454SAndrew Thompson 		}
70302ac6454SAndrew Thompson 		switch (pkt.bNotification) {
70402ac6454SAndrew Thompson 		case UCDC_N_RESPONSE_AVAILABLE:
70502ac6454SAndrew Thompson 			if (!(sc->sc_nobulk)) {
70602ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
70702ac6454SAndrew Thompson 				break;
70802ac6454SAndrew Thompson 			}
70902ac6454SAndrew Thompson 			if (sc->sc_num_msg != 0xFF) {
71002ac6454SAndrew Thompson 				sc->sc_num_msg++;
71102ac6454SAndrew Thompson 			}
712a593f6b8SAndrew Thompson 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
71302ac6454SAndrew Thompson 			break;
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 		case UCDC_N_SERIAL_STATE:
71602ac6454SAndrew Thompson 			if (sc->sc_nobulk) {
71702ac6454SAndrew Thompson 				DPRINTF("Wrong serial state!\n");
71802ac6454SAndrew Thompson 				break;
71902ac6454SAndrew Thompson 			}
72002ac6454SAndrew Thompson 			/*
72102ac6454SAndrew Thompson 		         * Set the serial state in ucom driver based on
72202ac6454SAndrew Thompson 		         * the bits from the notify message
72302ac6454SAndrew Thompson 		         */
724ed6d949aSAndrew Thompson 			if (actlen < 2) {
72502ac6454SAndrew Thompson 				DPRINTF("invalid notification "
726ed6d949aSAndrew Thompson 				    "length, %d bytes!\n", actlen);
72702ac6454SAndrew Thompson 				break;
72802ac6454SAndrew Thompson 			}
72902ac6454SAndrew Thompson 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
73002ac6454SAndrew Thompson 			    pkt.data[0], pkt.data[1]);
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 			/* currently, lsr is always zero. */
73302ac6454SAndrew Thompson 			sc->sc_lsr = 0;
73402ac6454SAndrew Thompson 			sc->sc_msr = 0;
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 			mstatus = pkt.data[0];
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_RI) {
73902ac6454SAndrew Thompson 				sc->sc_msr |= SER_RI;
74002ac6454SAndrew Thompson 			}
74102ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DSR) {
74202ac6454SAndrew Thompson 				sc->sc_msr |= SER_DSR;
74302ac6454SAndrew Thompson 			}
74402ac6454SAndrew Thompson 			if (mstatus & UCDC_N_SERIAL_DCD) {
74502ac6454SAndrew Thompson 				sc->sc_msr |= SER_DCD;
74602ac6454SAndrew Thompson 			}
747a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
74802ac6454SAndrew Thompson 			break;
74902ac6454SAndrew Thompson 
75002ac6454SAndrew Thompson 		default:
75102ac6454SAndrew Thompson 			break;
75202ac6454SAndrew Thompson 		}
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson 	case USB_ST_SETUP:
75502ac6454SAndrew Thompson tr_setup:
756ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
757a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
75802ac6454SAndrew Thompson 		return;
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 	default:			/* Error */
761ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
76202ac6454SAndrew Thompson 			/* try to clear stall first */
763ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
76402ac6454SAndrew Thompson 			goto tr_setup;
76502ac6454SAndrew Thompson 		}
76602ac6454SAndrew Thompson 		return;
76702ac6454SAndrew Thompson 	}
76802ac6454SAndrew Thompson }
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson static void
771ed6d949aSAndrew Thompson ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
77202ac6454SAndrew Thompson {
773ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
774ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
77502ac6454SAndrew Thompson 	uint32_t actlen;
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
77802ac6454SAndrew Thompson 	case USB_ST_SETUP:
77902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
78002ac6454SAndrew Thompson tr_setup:
781ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
782ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
78302ac6454SAndrew Thompson 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
784ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
785a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
78602ac6454SAndrew Thompson 		}
78702ac6454SAndrew Thompson 		return;
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 	default:			/* Error */
790ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
79102ac6454SAndrew Thompson 			/* try to clear stall first */
792ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79302ac6454SAndrew Thompson 			goto tr_setup;
79402ac6454SAndrew Thompson 		}
79502ac6454SAndrew Thompson 		return;
79602ac6454SAndrew Thompson 	}
79702ac6454SAndrew Thompson }
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson static void
800ed6d949aSAndrew Thompson ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
80102ac6454SAndrew Thompson {
802ed6d949aSAndrew Thompson 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
803ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
804ed6d949aSAndrew Thompson 	int actlen;
805ed6d949aSAndrew Thompson 
806ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
80702ac6454SAndrew Thompson 
80802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
80902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
810ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
811ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson 	case USB_ST_SETUP:
81402ac6454SAndrew Thompson tr_setup:
815ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
816a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
81702ac6454SAndrew Thompson 		return;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	default:			/* Error */
820ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
82102ac6454SAndrew Thompson 			/* try to clear stall first */
822ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
82302ac6454SAndrew Thompson 			goto tr_setup;
82402ac6454SAndrew Thompson 		}
82502ac6454SAndrew Thompson 		return;
82602ac6454SAndrew Thompson 	}
82702ac6454SAndrew Thompson }
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson static void
830760bc48eSAndrew Thompson ufoma_cfg_open(struct ucom_softc *ucom)
83102ac6454SAndrew Thompson {
83202ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
83302ac6454SAndrew Thompson 
83402ac6454SAndrew Thompson 	/* empty input queue */
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 	if (sc->sc_num_msg != 0xFF) {
83702ac6454SAndrew Thompson 		sc->sc_num_msg++;
83802ac6454SAndrew Thompson 	}
83902ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
84002ac6454SAndrew Thompson 		ufoma_cfg_link_state(sc);
84102ac6454SAndrew Thompson 	}
84202ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
84302ac6454SAndrew Thompson 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
84402ac6454SAndrew Thompson 	}
84502ac6454SAndrew Thompson }
84602ac6454SAndrew Thompson 
84702ac6454SAndrew Thompson static void
848760bc48eSAndrew Thompson ufoma_cfg_close(struct ucom_softc *ucom)
84902ac6454SAndrew Thompson {
85002ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
85302ac6454SAndrew Thompson }
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson static void
856760bc48eSAndrew Thompson ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
85702ac6454SAndrew Thompson {
85802ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
859760bc48eSAndrew Thompson 	struct usb_device_request req;
86002ac6454SAndrew Thompson 	uint16_t wValue;
86102ac6454SAndrew Thompson 
86202ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
86302ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
86402ac6454SAndrew Thompson 		return;
86502ac6454SAndrew Thompson 	}
86602ac6454SAndrew Thompson 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
86702ac6454SAndrew Thompson 		return;
86802ac6454SAndrew Thompson 	}
86902ac6454SAndrew Thompson 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
87202ac6454SAndrew Thompson 	req.bRequest = UCDC_SEND_BREAK;
87302ac6454SAndrew Thompson 	USETW(req.wValue, wValue);
87402ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
87502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
87602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
87702ac6454SAndrew Thompson 
878a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
87902ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
88002ac6454SAndrew Thompson }
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson static void
883760bc48eSAndrew Thompson ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
88402ac6454SAndrew Thompson {
88502ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
88802ac6454SAndrew Thompson 	*msr = sc->sc_msr;
88902ac6454SAndrew Thompson }
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson static void
89202ac6454SAndrew Thompson ufoma_cfg_set_line_state(struct ufoma_softc *sc)
89302ac6454SAndrew Thompson {
894760bc48eSAndrew Thompson 	struct usb_device_request req;
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 	/* Don't send line state emulation request for OBEX port */
89702ac6454SAndrew Thompson 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
89802ac6454SAndrew Thompson 		return;
89902ac6454SAndrew Thompson 	}
90002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
90102ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
90202ac6454SAndrew Thompson 	USETW(req.wValue, sc->sc_line);
90302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
90402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
90502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
90602ac6454SAndrew Thompson 
907a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
90802ac6454SAndrew Thompson 	    &req, NULL, 0, 1000);
90902ac6454SAndrew Thompson }
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson static void
912760bc48eSAndrew Thompson ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
91302ac6454SAndrew Thompson {
91402ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
91502ac6454SAndrew Thompson 
91602ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
91702ac6454SAndrew Thompson 		return;
91802ac6454SAndrew Thompson 	}
91902ac6454SAndrew Thompson 	if (onoff)
92002ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_DTR;
92102ac6454SAndrew Thompson 	else
92202ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_DTR;
92302ac6454SAndrew Thompson 
92402ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
92502ac6454SAndrew Thompson }
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson static void
928760bc48eSAndrew Thompson ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
92902ac6454SAndrew Thompson {
93002ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
93302ac6454SAndrew Thompson 		return;
93402ac6454SAndrew Thompson 	}
93502ac6454SAndrew Thompson 	if (onoff)
93602ac6454SAndrew Thompson 		sc->sc_line |= UCDC_LINE_RTS;
93702ac6454SAndrew Thompson 	else
93802ac6454SAndrew Thompson 		sc->sc_line &= ~UCDC_LINE_RTS;
93902ac6454SAndrew Thompson 
94002ac6454SAndrew Thompson 	ufoma_cfg_set_line_state(sc);
94102ac6454SAndrew Thompson }
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson static int
944760bc48eSAndrew Thompson ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
94502ac6454SAndrew Thompson {
94602ac6454SAndrew Thompson 	return (0);			/* we accept anything */
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson static void
950760bc48eSAndrew Thompson ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
95102ac6454SAndrew Thompson {
95202ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
953760bc48eSAndrew Thompson 	struct usb_device_request req;
954760bc48eSAndrew Thompson 	struct usb_cdc_line_state ls;
95502ac6454SAndrew Thompson 
95602ac6454SAndrew Thompson 	if (sc->sc_nobulk ||
95702ac6454SAndrew Thompson 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
95802ac6454SAndrew Thompson 		return;
95902ac6454SAndrew Thompson 	}
96002ac6454SAndrew Thompson 	DPRINTF("\n");
96102ac6454SAndrew Thompson 
96202ac6454SAndrew Thompson 	bzero(&ls, sizeof(ls));
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson 	USETDW(ls.dwDTERate, t->c_ospeed);
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson 	if (t->c_cflag & CSTOPB) {
96702ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_2;
96802ac6454SAndrew Thompson 	} else {
96902ac6454SAndrew Thompson 		ls.bCharFormat = UCDC_STOP_BIT_1;
97002ac6454SAndrew Thompson 	}
97102ac6454SAndrew Thompson 
97202ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
97302ac6454SAndrew Thompson 		if (t->c_cflag & PARODD) {
97402ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_ODD;
97502ac6454SAndrew Thompson 		} else {
97602ac6454SAndrew Thompson 			ls.bParityType = UCDC_PARITY_EVEN;
97702ac6454SAndrew Thompson 		}
97802ac6454SAndrew Thompson 	} else {
97902ac6454SAndrew Thompson 		ls.bParityType = UCDC_PARITY_NONE;
98002ac6454SAndrew Thompson 	}
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
98302ac6454SAndrew Thompson 	case CS5:
98402ac6454SAndrew Thompson 		ls.bDataBits = 5;
98502ac6454SAndrew Thompson 		break;
98602ac6454SAndrew Thompson 	case CS6:
98702ac6454SAndrew Thompson 		ls.bDataBits = 6;
98802ac6454SAndrew Thompson 		break;
98902ac6454SAndrew Thompson 	case CS7:
99002ac6454SAndrew Thompson 		ls.bDataBits = 7;
99102ac6454SAndrew Thompson 		break;
99202ac6454SAndrew Thompson 	case CS8:
99302ac6454SAndrew Thompson 		ls.bDataBits = 8;
99402ac6454SAndrew Thompson 		break;
99502ac6454SAndrew Thompson 	}
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
99802ac6454SAndrew Thompson 	req.bRequest = UCDC_SET_LINE_CODING;
99902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
100002ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_ctrl_iface_no;
100102ac6454SAndrew Thompson 	req.wIndex[1] = 0;
100202ac6454SAndrew Thompson 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
100302ac6454SAndrew Thompson 
1004a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
100502ac6454SAndrew Thompson 	    &req, &ls, 0, 1000);
100602ac6454SAndrew Thompson }
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson static int
100902ac6454SAndrew Thompson ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1010760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
101102ac6454SAndrew Thompson {
1012760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
1013760bc48eSAndrew Thompson 	struct usb_cdc_acm_descriptor *acm;
1014760bc48eSAndrew Thompson 	struct usb_cdc_cm_descriptor *cmd;
1015760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
1016760bc48eSAndrew Thompson 	struct usb_interface *iface;
101702ac6454SAndrew Thompson 	uint8_t i;
101802ac6454SAndrew Thompson 	int32_t error;
101902ac6454SAndrew Thompson 
1020a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(uaa->device);
1021a593f6b8SAndrew Thompson 	id = usbd_get_interface_descriptor(uaa->iface);
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 	if ((cmd == NULL) ||
102602ac6454SAndrew Thompson 	    (cmd->bLength < sizeof(*cmd))) {
102702ac6454SAndrew Thompson 		return (EINVAL);
102802ac6454SAndrew Thompson 	}
102902ac6454SAndrew Thompson 	sc->sc_cm_cap = cmd->bmCapabilities;
103002ac6454SAndrew Thompson 	sc->sc_data_iface_no = cmd->bDataInterface;
103102ac6454SAndrew Thompson 
103202ac6454SAndrew Thompson 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
103302ac6454SAndrew Thompson 
103402ac6454SAndrew Thompson 	if ((acm == NULL) ||
103502ac6454SAndrew Thompson 	    (acm->bLength < sizeof(*acm))) {
103602ac6454SAndrew Thompson 		return (EINVAL);
103702ac6454SAndrew Thompson 	}
103802ac6454SAndrew Thompson 	sc->sc_acm_cap = acm->bmCapabilities;
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	device_printf(dev, "data interface %d, has %sCM over data, "
104102ac6454SAndrew Thompson 	    "has %sbreak\n",
104202ac6454SAndrew Thompson 	    sc->sc_data_iface_no,
104302ac6454SAndrew Thompson 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
104402ac6454SAndrew Thompson 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 	/* get the data interface too */
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson 	for (i = 0;; i++) {
104902ac6454SAndrew Thompson 
1050a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, i);
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 		if (iface) {
105302ac6454SAndrew Thompson 
1054a593f6b8SAndrew Thompson 			id = usbd_get_interface_descriptor(iface);
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
105702ac6454SAndrew Thompson 				sc->sc_data_iface_index = i;
1058a593f6b8SAndrew Thompson 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
105902ac6454SAndrew Thompson 				break;
106002ac6454SAndrew Thompson 			}
106102ac6454SAndrew Thompson 		} else {
106202ac6454SAndrew Thompson 			device_printf(dev, "no data interface!\n");
106302ac6454SAndrew Thompson 			return (EINVAL);
106402ac6454SAndrew Thompson 		}
106502ac6454SAndrew Thompson 	}
106602ac6454SAndrew Thompson 
1067a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
106802ac6454SAndrew Thompson 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1069deefe583SAndrew Thompson 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	if (error) {
107202ac6454SAndrew Thompson 		device_printf(dev, "allocating BULK USB "
107302ac6454SAndrew Thompson 		    "transfers failed!\n");
107402ac6454SAndrew Thompson 		return (EINVAL);
107502ac6454SAndrew Thompson 	}
107602ac6454SAndrew Thompson 	return (0);
107702ac6454SAndrew Thompson }
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson static void
1080760bc48eSAndrew Thompson ufoma_start_read(struct ucom_softc *ucom)
108102ac6454SAndrew Thompson {
108202ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson 	/* start interrupt transfer */
1085a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson 	/* start data transfer */
108802ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1089a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
109002ac6454SAndrew Thompson 	} else {
1091a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
109202ac6454SAndrew Thompson 	}
109302ac6454SAndrew Thompson }
109402ac6454SAndrew Thompson 
109502ac6454SAndrew Thompson static void
1096760bc48eSAndrew Thompson ufoma_stop_read(struct ucom_softc *ucom)
109702ac6454SAndrew Thompson {
109802ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
109902ac6454SAndrew Thompson 
110002ac6454SAndrew Thompson 	/* stop interrupt transfer */
1101a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
110202ac6454SAndrew Thompson 
110302ac6454SAndrew Thompson 	/* stop data transfer */
110402ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1105a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
110602ac6454SAndrew Thompson 	} else {
1107a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
110802ac6454SAndrew Thompson 	}
110902ac6454SAndrew Thompson }
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson static void
1112760bc48eSAndrew Thompson ufoma_start_write(struct ucom_softc *ucom)
111302ac6454SAndrew Thompson {
111402ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
111502ac6454SAndrew Thompson 
111602ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1117a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
111802ac6454SAndrew Thompson 	} else {
1119a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
112002ac6454SAndrew Thompson 	}
112102ac6454SAndrew Thompson }
112202ac6454SAndrew Thompson 
112302ac6454SAndrew Thompson static void
1124760bc48eSAndrew Thompson ufoma_stop_write(struct ucom_softc *ucom)
112502ac6454SAndrew Thompson {
112602ac6454SAndrew Thompson 	struct ufoma_softc *sc = ucom->sc_parent;
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson 	if (sc->sc_nobulk) {
1129a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
113002ac6454SAndrew Thompson 	} else {
1131a593f6b8SAndrew Thompson 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
113202ac6454SAndrew Thompson 	}
113302ac6454SAndrew Thompson }
113402ac6454SAndrew Thompson 
1135a8675caeSAndrew Thompson static struct umcpc_modetostr_tab{
113602ac6454SAndrew Thompson 	int mode;
113702ac6454SAndrew Thompson 	char *str;
113802ac6454SAndrew Thompson }umcpc_modetostr_tab[]={
113902ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
114002ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_MODEM, "modem"},
114102ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
114202ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_OBEX, "obex"},
114302ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
114402ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
114502ac6454SAndrew Thompson 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
114602ac6454SAndrew Thompson 	{0, NULL}
114702ac6454SAndrew Thompson };
114802ac6454SAndrew Thompson 
114902ac6454SAndrew Thompson static char *ufoma_mode_to_str(int mode)
115002ac6454SAndrew Thompson {
115102ac6454SAndrew Thompson 	int i;
115202ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
115302ac6454SAndrew Thompson 		if(umcpc_modetostr_tab[i].mode == mode){
115402ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].str;
115502ac6454SAndrew Thompson 		}
115602ac6454SAndrew Thompson 	}
115702ac6454SAndrew Thompson 	return NULL;
115802ac6454SAndrew Thompson }
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson static int ufoma_str_to_mode(char *str)
116102ac6454SAndrew Thompson {
116202ac6454SAndrew Thompson 	int i;
116302ac6454SAndrew Thompson 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
116402ac6454SAndrew Thompson 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
116502ac6454SAndrew Thompson 			return umcpc_modetostr_tab[i].mode;
116602ac6454SAndrew Thompson 		}
116702ac6454SAndrew Thompson 	}
116802ac6454SAndrew Thompson 	return -1;
116902ac6454SAndrew Thompson }
117002ac6454SAndrew Thompson 
117102ac6454SAndrew Thompson static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
117202ac6454SAndrew Thompson {
117302ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
117402ac6454SAndrew Thompson 	struct sbuf sb;
117502ac6454SAndrew Thompson 	int i;
117602ac6454SAndrew Thompson 	char *mode;
117702ac6454SAndrew Thompson 
117802ac6454SAndrew Thompson 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
117902ac6454SAndrew Thompson 	for(i = 1; i < sc->sc_modetable[0]; i++){
118002ac6454SAndrew Thompson 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
118102ac6454SAndrew Thompson 		if(mode !=NULL){
118202ac6454SAndrew Thompson 			sbuf_cat(&sb, mode);
118302ac6454SAndrew Thompson 		}else{
118402ac6454SAndrew Thompson 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
118502ac6454SAndrew Thompson 		}
118602ac6454SAndrew Thompson 		if(i < (sc->sc_modetable[0]-1))
118702ac6454SAndrew Thompson 			sbuf_cat(&sb, ",");
118802ac6454SAndrew Thompson 	}
118902ac6454SAndrew Thompson 	sbuf_trim(&sb);
119002ac6454SAndrew Thompson 	sbuf_finish(&sb);
119102ac6454SAndrew Thompson 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
119202ac6454SAndrew Thompson 	sbuf_delete(&sb);
119302ac6454SAndrew Thompson 
119402ac6454SAndrew Thompson 	return 0;
119502ac6454SAndrew Thompson }
119602ac6454SAndrew Thompson static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
119702ac6454SAndrew Thompson {
119802ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
119902ac6454SAndrew Thompson 	char *mode;
120002ac6454SAndrew Thompson 	char subbuf[]="(XXX)";
120102ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_currentmode);
120202ac6454SAndrew Thompson 	if(!mode){
120302ac6454SAndrew Thompson 		mode = subbuf;
120402ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
120502ac6454SAndrew Thompson 	}
120602ac6454SAndrew Thompson 	sysctl_handle_string(oidp, mode, strlen(mode), req);
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 	return 0;
120902ac6454SAndrew Thompson 
121002ac6454SAndrew Thompson }
121102ac6454SAndrew Thompson static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
121202ac6454SAndrew Thompson {
121302ac6454SAndrew Thompson 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
121402ac6454SAndrew Thompson 	char *mode;
121502ac6454SAndrew Thompson 	char subbuf[40];
121602ac6454SAndrew Thompson 	int newmode;
121702ac6454SAndrew Thompson 	int error;
121802ac6454SAndrew Thompson 	int i;
121902ac6454SAndrew Thompson 
122002ac6454SAndrew Thompson 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
122102ac6454SAndrew Thompson 	if(mode){
122202ac6454SAndrew Thompson 		strncpy(subbuf, mode, sizeof(subbuf));
122302ac6454SAndrew Thompson 	}else{
122402ac6454SAndrew Thompson 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
122502ac6454SAndrew Thompson 	}
122602ac6454SAndrew Thompson 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
122702ac6454SAndrew Thompson 	if(error != 0 || req->newptr == NULL){
122802ac6454SAndrew Thompson 		return error;
122902ac6454SAndrew Thompson 	}
123002ac6454SAndrew Thompson 
123102ac6454SAndrew Thompson 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
123202ac6454SAndrew Thompson 		return EINVAL;
123302ac6454SAndrew Thompson 	}
123402ac6454SAndrew Thompson 
123502ac6454SAndrew Thompson 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
123602ac6454SAndrew Thompson 		if(sc->sc_modetable[i] == newmode){
123702ac6454SAndrew Thompson 			sc->sc_modetoactivate = newmode;
123802ac6454SAndrew Thompson 			return 0;
123902ac6454SAndrew Thompson 		}
124002ac6454SAndrew Thompson 	}
124102ac6454SAndrew Thompson 
124202ac6454SAndrew Thompson 	return EINVAL;
124302ac6454SAndrew Thompson }
1244