xref: /openbsd-src/sys/dev/usb/umsm.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: umsm.c,v 1.118 2020/07/31 10:49:33 mglocker Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Yojiro UO <yuo@nui.org>
5  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Driver for Qualcomm MSM EVDO and UMTS communication devices */
21 
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/kernel.h>
25 #include <sys/malloc.h>
26 #include <sys/device.h>
27 #include <sys/conf.h>
28 #include <sys/tty.h>
29 
30 #include <dev/usb/usb.h>
31 #include <dev/usb/usbdi.h>
32 #include <dev/usb/usbdevs.h>
33 #include <dev/usb/ucomvar.h>
34 #include <dev/usb/usbcdc.h>
35 #include <dev/usb/umassvar.h>
36 #undef DPRINTF	/* undef DPRINTF for umass */
37 
38 #ifdef UMSM_DEBUG
39 int     umsmdebug = 0;
40 #define DPRINTFN(n, x)  do { if (umsmdebug > (n)) printf x; } while (0)
41 #else
42 #define DPRINTFN(n, x)
43 #endif
44 
45 #define DPRINTF(x) DPRINTFN(0, x)
46 
47 #define UMSMBUFSZ	4096
48 #define	UMSM_INTR_INTERVAL	100	/* ms */
49 #define E220_MODE_CHANGE_REQUEST 0x2
50 #define TRUINSTALL_CHANGEMODE_REQUEST 0x0b
51 
52 int umsm_match(struct device *, void *, void *);
53 void umsm_attach(struct device *, struct device *, void *);
54 int umsm_detach(struct device *, int);
55 
56 int umsm_open(void *, int);
57 void umsm_close(void *, int);
58 void umsm_intr(struct usbd_xfer *, void *, usbd_status);
59 void umsm_get_status(void *, int, u_char *, u_char *);
60 void umsm_set(void *, int, int, int);
61 
62 struct umsm_softc {
63 	struct device		 sc_dev;
64 	struct usbd_device	*sc_udev;
65 	struct usbd_interface	*sc_iface;
66 	int			 sc_iface_no;
67 	struct device		*sc_subdev;
68 	uint16_t		 sc_flag;
69 
70 	/* interrupt ep */
71 	int			 sc_intr_number;
72 	struct usbd_pipe	*sc_intr_pipe;
73 	u_char			*sc_intr_buf;
74 	int			 sc_isize;
75 
76 	u_char			 sc_lsr;	/* Local status register */
77 	u_char			 sc_msr;	/* status register */
78 	u_char			 sc_dtr;	/* current DTR state */
79 	u_char			 sc_rts;	/* current RTS state */
80 };
81 
82 usbd_status umsm_huawei_changemode(struct usbd_device *);
83 usbd_status umsm_truinstall_changemode(struct usbd_device *);
84 usbd_status umsm_umass_changemode(struct umsm_softc *);
85 
86 struct ucom_methods umsm_methods = {
87 	umsm_get_status,
88 	umsm_set,
89 	NULL,
90 	NULL,
91 	umsm_open,
92 	umsm_close,
93 	NULL,
94 	NULL,
95 };
96 
97 struct umsm_type {
98 	struct usb_devno	umsm_dev;
99 	uint16_t		umsm_flag;
100 /* device type */
101 #define	DEV_NORMAL	0x0000
102 #define	DEV_HUAWEI	0x0001
103 #define	DEV_TRUINSTALL	0x0002
104 #define	DEV_UMASS1	0x0010
105 #define	DEV_UMASS2	0x0020
106 #define	DEV_UMASS3	0x0040
107 #define	DEV_UMASS4	0x0080
108 #define	DEV_UMASS5	0x0100
109 #define	DEV_UMASS6	0x0200
110 #define	DEV_UMASS7	0x0400
111 #define	DEV_UMASS8	0x1000
112 #define DEV_UMASS	(DEV_UMASS1 | DEV_UMASS2 | DEV_UMASS3 | DEV_UMASS4 | \
113     DEV_UMASS5 | DEV_UMASS6 | DEV_UMASS7 | DEV_UMASS8)
114 };
115 
116 static const struct umsm_type umsm_devs[] = {
117 	{{ USB_VENDOR_AIRPRIME,	USB_PRODUCT_AIRPRIME_PC5220 }, 0},
118 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_AIRCARD_313U }, 0},
119 
120 	{{ USB_VENDOR_ANYDATA,	USB_PRODUCT_ANYDATA_A2502 }, 0},
121 	{{ USB_VENDOR_ANYDATA,	USB_PRODUCT_ANYDATA_ADU_500A }, 0},
122 	{{ USB_VENDOR_ANYDATA,  USB_PRODUCT_ANYDATA_ADU_E100H }, 0},
123 
124 	{{ USB_VENDOR_DELL,	USB_PRODUCT_DELL_EU870D }, 0},
125 	{{ USB_VENDOR_DELL,	USB_PRODUCT_DELL_U740 }, 0},
126 	{{ USB_VENDOR_DELL,	USB_PRODUCT_DELL_W5500 }, 0},
127 
128 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E161 }, DEV_UMASS5},
129 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E173S_INIT }, DEV_UMASS5},
130 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E173S }, 0},
131 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E180 }, DEV_HUAWEI},
132 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E181 }, DEV_HUAWEI},
133 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E182 }, DEV_UMASS5},
134 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E1820 }, DEV_UMASS5},
135 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E220 }, DEV_HUAWEI},
136 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E303 }, DEV_UMASS5},
137 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E353_INIT }, DEV_UMASS5},
138 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E510 }, DEV_HUAWEI},
139 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E618 }, DEV_HUAWEI},
140 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E392_INIT }, DEV_UMASS5},
141 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_EM770W }, 0},
142 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_MOBILE }, DEV_HUAWEI},
143 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K3765_INIT }, DEV_UMASS5},
144 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K3765 }, 0},
145 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K3772_INIT }, DEV_UMASS5},
146 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K3772 }, 0},
147 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_MU609 }, DEV_TRUINSTALL},
148 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K4510 }, DEV_UMASS5},
149 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_K4511 }, DEV_UMASS5},
150 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E1750 }, DEV_UMASS5},
151 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E1752 }, 0},
152 	{{ USB_VENDOR_HUAWEI,	USB_PRODUCT_HUAWEI_E3372 }, DEV_UMASS5},
153 
154 	{{ USB_VENDOR_HYUNDAI,	USB_PRODUCT_HYUNDAI_UM175 }, 0},
155 
156 	{{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_D21LCMASS }, DEV_UMASS3},
157 	{{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_D21LC }, 0},
158 	{{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_510FUMASS }, DEV_UMASS3},
159 	{{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_510FU }, 0},
160 
161 	{{ USB_VENDOR_KYOCERA2,	USB_PRODUCT_KYOCERA2_KPC650 }, 0},
162 
163 	{{ USB_VENDOR_MEDIATEK, USB_PRODUCT_MEDIATEK_UMASS }, DEV_UMASS8},
164 	{{ USB_VENDOR_MEDIATEK, USB_PRODUCT_MEDIATEK_DC_4COM }, 0},
165 
166 	/* XXX Some qualcomm devices are missing */
167 	{{ USB_VENDOR_QUALCOMM,	USB_PRODUCT_QUALCOMM_MSM_DRIVER }, DEV_UMASS1},
168 	{{ USB_VENDOR_QUALCOMM,	USB_PRODUCT_QUALCOMM_MSM_DRIVER2 }, DEV_UMASS4},
169 	{{ USB_VENDOR_QUALCOMM,	USB_PRODUCT_QUALCOMM_MSM_HSDPA }, 0},
170 	{{ USB_VENDOR_QUALCOMM,	USB_PRODUCT_QUALCOMM_MSM_HSDPA2 }, 0},
171 	{{ USB_VENDOR_QUALCOMM,	USB_PRODUCT_QUALCOMM_MSM_HSDPA3 }, 0},
172 
173 	{{ USB_VENDOR_QUANTA2, USB_PRODUCT_QUANTA2_UMASS }, DEV_UMASS4},
174 	{{ USB_VENDOR_QUANTA2, USB_PRODUCT_QUANTA2_Q101 }, 0},
175 
176 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC2746 }, 0},
177 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_UMASS_INSTALLER }, DEV_UMASS4},
178 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_UMASS_INSTALLER2 }, DEV_UMASS6},
179 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_UMASS_INSTALLER3 }, DEV_UMASS7},
180 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_UMASS_INSTALLER4 }, DEV_UMASS4},
181 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_K3565Z }, 0},
182 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF112 }, DEV_UMASS4},
183 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF633 }, 0},
184 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF637 }, 0},
185 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MSA110UP }, 0},
186 
187 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_EXPRESSCARD }, 0},
188 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINV620 }, 0},
189 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINV740 }, 0},
190 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V720 }, 0},
191 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINU740 }, 0},
192 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINU740_2 }, 0},
193 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U870 }, 0},
194 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_XU870 }, 0},
195 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_EU870D }, 0},
196 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_X950D }, 0},
197 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ES620 }, 0},
198 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U720 }, 0},
199 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U727 }, DEV_UMASS1},
200 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC950D }, 0},
201 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MERLINX950D }, DEV_UMASS4},
202 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ZEROCD2 }, DEV_UMASS4},
203 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U760 }, DEV_UMASS4},
204 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC760 }, 0},
205 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC760CD }, DEV_UMASS4},
206 
207 	{{ USB_VENDOR_NOVATEL1,	USB_PRODUCT_NOVATEL1_FLEXPACKGPS }, 0},
208 
209 	{{ USB_VENDOR_NOKIA2, USB_PRODUCT_NOKIA2_CS15UMASS }, DEV_UMASS4},
210 	{{ USB_VENDOR_NOKIA2, USB_PRODUCT_NOKIA2_CS15 }, 0},
211 
212 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GFUSION }, 0},
213 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS }, 0},
214 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD }, 0},
215 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUADPLUS }, 0},
216 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GSICON72 }, DEV_UMASS1},
217 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTHSUPA380E }, 0},
218 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36 }, 0},
219 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_ICON225 }, DEV_UMASS2},
220 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_ICON505 }, DEV_UMASS1},
221 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_SCORPION }, 0},
222 	{{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G }, 0},
223 
224 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, 0},
225 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, 0},
226 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD_595 }, 0},
227 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, 0},
228 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E }, 0},
229 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 }, 0},
230 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U }, 0},
231 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD_580 }, 0},
232 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 }, 0},
233 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725_2 }, 0},
234 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, 0},
235 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, 0},
236 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, 0},
237 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775 }, 0},
238 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, 0},
239 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 }, 0},
240 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD_875 }, 0},
241 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 }, 0},
242 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 }, 0},
243 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8790 }, 0},
244 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 }, 0},
245 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 }, 0},
246 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E }, 0},
247 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E }, 0},
248 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U }, 0},
249 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U }, 0},
250 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC885U }, 0},
251 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C01SW }, 0},
252 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305}, 0},
253 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC7304}, 0},
254 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_TRUINSTALL }, DEV_TRUINSTALL},
255 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8355}, 0},
256 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD_340U}, 0},
257 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD_770S}, 0},
258 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC7455}, 0},
259 
260 	{{ USB_VENDOR_SIMCOM, USB_PRODUCT_SIMCOM_SIM7600E}, 0},
261 
262 	{{ USB_VENDOR_TCTMOBILE, USB_PRODUCT_TCTMOBILE_UMASS }, DEV_UMASS3},
263 	{{ USB_VENDOR_TCTMOBILE, USB_PRODUCT_TCTMOBILE_UMASS_2 }, DEV_UMASS3},
264 	{{ USB_VENDOR_TCTMOBILE, USB_PRODUCT_TCTMOBILE_UMSM }, 0},
265 	{{ USB_VENDOR_TCTMOBILE, USB_PRODUCT_TCTMOBILE_UMSM_2 }, 0},
266 	{{ USB_VENDOR_TCTMOBILE, USB_PRODUCT_TCTMOBILE_UMSM_3 }, 0},
267 
268 	{{ USB_VENDOR_TOSHIBA, USB_PRODUCT_TOSHIBA_HSDPA }, 0},
269 
270 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_HS2300 }, 0},
271 
272 	{{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CNU510 }, 0}, /* ??? */
273 	{{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CCU550 }, 0}, /* ??? */
274 	{{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CGU628 }, DEV_UMASS1},
275 	{{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CGU628_DISK }, 0},
276 	{{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CNU680 }, DEV_UMASS1},
277 };
278 
279 #define umsm_lookup(v, p) ((const struct umsm_type *)usb_lookup(umsm_devs, v, p))
280 
281 struct cfdriver umsm_cd = {
282 	NULL, "umsm", DV_DULL
283 };
284 
285 const struct cfattach umsm_ca = {
286 	sizeof(struct umsm_softc), umsm_match, umsm_attach, umsm_detach
287 };
288 
289 int
290 umsm_match(struct device *parent, void *match, void *aux)
291 {
292 	struct usb_attach_arg *uaa = aux;
293 	usb_interface_descriptor_t *id;
294 	uint16_t flag;
295 
296 	if (uaa->iface == NULL)
297 		return UMATCH_NONE;
298 
299 	/*
300 	 * Some devices (eg Huawei E220) have multiple interfaces and some
301 	 * of them are of class umass. Don't claim ownership in such case.
302 	 */
303 	if (umsm_lookup(uaa->vendor, uaa->product) != NULL) {
304 		id = usbd_get_interface_descriptor(uaa->iface);
305 		flag = umsm_lookup(uaa->vendor, uaa->product)->umsm_flag;
306 
307 		if (id == NULL || id->bInterfaceClass == UICLASS_MASS) {
308 			/*
309 			 * Some high-speed modems require special care.
310 			 */
311 			if (flag & DEV_HUAWEI) {
312 				if (uaa->ifaceno != 2)
313 					return UMATCH_VENDOR_IFACESUBCLASS;
314 				else
315 					return UMATCH_NONE;
316 			} else if (flag & DEV_UMASS) {
317 				return UMATCH_VENDOR_IFACESUBCLASS;
318 			} else if (flag & DEV_TRUINSTALL) {
319 				return UMATCH_VENDOR_IFACESUBCLASS;
320 			} else
321 				return UMATCH_NONE;
322 		/*
323 		 * Some devices have interfaces which fail to attach but in
324 		 * addition seem to make the remaining interfaces unusable. Only
325 		 * attach whitelisted interfaces in this case.
326 		 */
327 		} else if ((uaa->vendor == USB_VENDOR_MEDIATEK &&
328 			    uaa->product == USB_PRODUCT_MEDIATEK_DC_4COM) &&
329 			   !(id->bInterfaceClass == UICLASS_VENDOR &&
330 			    ((id->bInterfaceSubClass == 0x02 &&
331 			      id->bInterfaceProtocol == 0x01) ||
332 			     (id->bInterfaceSubClass == 0x00 &&
333 			      id->bInterfaceProtocol == 0x00)))) {
334 			return UMATCH_NONE;
335 		} else
336 			return UMATCH_VENDOR_IFACESUBCLASS;
337 	}
338 
339 	return UMATCH_NONE;
340 }
341 
342 void
343 umsm_attach(struct device *parent, struct device *self, void *aux)
344 {
345 	struct umsm_softc *sc = (struct umsm_softc *)self;
346 	struct usb_attach_arg *uaa = aux;
347 	struct ucom_attach_args uca;
348 	usb_interface_descriptor_t *id;
349 	usb_endpoint_descriptor_t *ed;
350 	int i;
351 
352 	bzero(&uca, sizeof(uca));
353 	sc->sc_udev = uaa->device;
354 	sc->sc_iface = uaa->iface;
355 	sc->sc_flag  = umsm_lookup(uaa->vendor, uaa->product)->umsm_flag;
356 
357 	id = usbd_get_interface_descriptor(sc->sc_iface);
358 
359 	/*
360 	 * Some 3G modems have multiple interfaces and some of them
361 	 * are umass class. Don't claim ownership in such case.
362 	 */
363 	if (id == NULL || id->bInterfaceClass == UICLASS_MASS) {
364 		/*
365 		 * Some 3G modems require a special request to
366 		 * enable their modem function.
367 		 */
368 		if ((sc->sc_flag & DEV_HUAWEI) && uaa->ifaceno == 0) {
369                         umsm_huawei_changemode(uaa->device);
370 			printf("%s: umass only mode. need to reattach\n",
371 				sc->sc_dev.dv_xname);
372 		} else if (sc->sc_flag & DEV_TRUINSTALL) {
373 			umsm_truinstall_changemode(uaa->device);
374 			printf("%s: truinstall mode. need to reattach\n",
375 				sc->sc_dev.dv_xname);
376 		} else if ((sc->sc_flag & DEV_UMASS) && uaa->ifaceno == 0) {
377 			umsm_umass_changemode(sc);
378 		}
379 
380 		/*
381 		 * The device will reset its own bus from the device side
382 		 * when its mode was changed, so just return.
383 		 */
384 		return;
385 	}
386 
387 	sc->sc_iface_no = id->bInterfaceNumber;
388 	uca.bulkin = uca.bulkout = -1;
389 	sc->sc_intr_number = sc->sc_isize = -1;
390 	for (i = 0; i < id->bNumEndpoints; i++) {
391 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
392 		if (ed == NULL) {
393 			printf("%s: no endpoint descriptor found for %d\n",
394 			    sc->sc_dev.dv_xname, i);
395 			usbd_deactivate(sc->sc_udev);
396 			return;
397 		}
398 
399 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
400 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
401 			sc->sc_intr_number = ed->bEndpointAddress;
402 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
403 			DPRINTF(("%s: find interrupt endpoint for %s\n",
404 				__func__, sc->sc_dev.dv_xname));
405 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
406 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
407 			uca.bulkin = ed->bEndpointAddress;
408 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
409 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
410 			uca.bulkout = ed->bEndpointAddress;
411 	}
412 	if (uca.bulkin == -1 || uca.bulkout == -1) {
413 		printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
414 		usbd_deactivate(sc->sc_udev);
415 		return;
416 	}
417 
418 	sc->sc_dtr = sc->sc_rts = -1;
419 
420 	/* We need to force size as some devices lie */
421 	uca.ibufsize = UMSMBUFSZ;
422 	uca.obufsize = UMSMBUFSZ;
423 	uca.ibufsizepad = UMSMBUFSZ;
424 	uca.opkthdrlen = 0;
425 	uca.device = sc->sc_udev;
426 	uca.iface = sc->sc_iface;
427 	uca.methods = &umsm_methods;
428 	uca.arg = sc;
429 	uca.info = NULL;
430 	uca.portno = UCOM_UNK_PORTNO;
431 
432 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
433 }
434 
435 int
436 umsm_detach(struct device *self, int flags)
437 {
438 	struct umsm_softc *sc = (struct umsm_softc *)self;
439 	int rv = 0;
440 
441 	/* close the interrupt endpoint if that is opened */
442 	if (sc->sc_intr_pipe != NULL) {
443 		usbd_close_pipe(sc->sc_intr_pipe);
444 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
445 		sc->sc_intr_pipe = NULL;
446 	}
447 
448 	usbd_deactivate(sc->sc_udev);
449 	if (sc->sc_subdev != NULL) {
450 		rv = config_detach(sc->sc_subdev, flags);
451 		sc->sc_subdev = NULL;
452 	}
453 
454 	return (rv);
455 }
456 
457 int
458 umsm_open(void *addr, int portno)
459 {
460 	struct umsm_softc *sc = addr;
461 	int err;
462 
463 	if (usbd_is_dying(sc->sc_udev))
464 		return (ENXIO);
465 
466 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
467 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
468 		err = usbd_open_pipe_intr(sc->sc_iface,
469 		    sc->sc_intr_number,
470 		    USBD_SHORT_XFER_OK,
471 		    &sc->sc_intr_pipe,
472 		    sc,
473 		    sc->sc_intr_buf,
474 		    sc->sc_isize,
475 		    umsm_intr,
476 		    UMSM_INTR_INTERVAL);
477 		if (err) {
478 			printf("%s: cannot open interrupt pipe (addr %d)\n",
479 			    sc->sc_dev.dv_xname,
480 			    sc->sc_intr_number);
481 			return (EIO);
482 		}
483 	}
484 
485 	return (0);
486 }
487 
488 void
489 umsm_close(void *addr, int portno)
490 {
491 	struct umsm_softc *sc = addr;
492 	int err;
493 
494 	if (usbd_is_dying(sc->sc_udev))
495 		return;
496 
497 	if (sc->sc_intr_pipe != NULL) {
498 		err = usbd_close_pipe(sc->sc_intr_pipe);
499 		if (err)
500 			printf("%s: close interrupt pipe failed: %s\n",
501 			    sc->sc_dev.dv_xname,
502 			    usbd_errstr(err));
503 		free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
504 		sc->sc_intr_pipe = NULL;
505 	}
506 }
507 
508 void
509 umsm_intr(struct usbd_xfer *xfer, void *priv,
510 	usbd_status status)
511 {
512 	struct umsm_softc *sc = priv;
513 	struct usb_cdc_notification *buf;
514 	u_char mstatus;
515 
516 	buf = (struct usb_cdc_notification *)sc->sc_intr_buf;
517 	if (usbd_is_dying(sc->sc_udev))
518 		return;
519 
520 	if (status != USBD_NORMAL_COMPLETION) {
521 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
522 			return;
523 
524 		DPRINTF(("%s: umsm_intr: abnormal status: %s\n",
525 		    sc->sc_dev.dv_xname, usbd_errstr(status)));
526 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
527 		return;
528 	}
529 
530 	if (buf->bmRequestType != UCDC_NOTIFICATION) {
531 #if 1 /* test */
532 		printf("%s: this device is not using CDC notify message in intr pipe.\n"
533 		    "Please send your dmesg to <bugs@openbsd.org>, thanks.\n",
534 		    sc->sc_dev.dv_xname);
535 		printf("%s: intr buffer 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
536 		    sc->sc_dev.dv_xname,
537 		    sc->sc_intr_buf[0], sc->sc_intr_buf[1],
538 		    sc->sc_intr_buf[2], sc->sc_intr_buf[3],
539 		    sc->sc_intr_buf[4], sc->sc_intr_buf[5],
540 		    sc->sc_intr_buf[6]);
541 #else
542 		DPRINTF(("%s: umsm_intr: unknown message type(0x%02x)\n",
543 		    sc->sc_dev.dv_xname, buf->bmRequestType));
544 #endif
545 		return;
546 	}
547 
548 	if (buf->bNotification == UCDC_N_SERIAL_STATE) {
549 		/* invalid message length, discard it */
550 		if (UGETW(buf->wLength) != 2)
551 			return;
552 		/* XXX: sc_lsr is always 0 */
553 		sc->sc_lsr = sc->sc_msr = 0;
554 		mstatus = buf->data[0];
555 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
556 			sc->sc_msr |= UMSR_RI;
557 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
558 			sc->sc_msr |= UMSR_DSR;
559 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
560 			sc->sc_msr |= UMSR_DCD;
561 	} else if (buf->bNotification != UCDC_N_CONNECTION_SPEED_CHANGE) {
562 		DPRINTF(("%s: umsm_intr: unknown notify message (0x%02x)\n",
563 		    sc->sc_dev.dv_xname, buf->bNotification));
564 		return;
565 	}
566 
567 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
568 }
569 
570 void
571 umsm_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
572 {
573 	struct umsm_softc *sc = addr;
574 
575 	if (lsr != NULL)
576 		*lsr = sc->sc_lsr;
577 	if (msr != NULL)
578 		*msr = sc->sc_msr;
579 }
580 
581 void
582 umsm_set(void *addr, int portno, int reg, int onoff)
583 {
584 	struct umsm_softc *sc = addr;
585 	usb_device_request_t req;
586 	int ls;
587 
588 	switch (reg) {
589 	case UCOM_SET_DTR:
590 		if (sc->sc_dtr == onoff)
591 			return;
592 		sc->sc_dtr = onoff;
593 		break;
594 	case UCOM_SET_RTS:
595 		if (sc->sc_rts == onoff)
596 			return;
597 		sc->sc_rts = onoff;
598 		break;
599 	default:
600 		return;
601 	}
602 
603 	/* build a usb request */
604 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
605 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
606 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
607 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
608 	USETW(req.wValue, ls);
609 	USETW(req.wIndex, sc->sc_iface_no);
610 	USETW(req.wLength, 0);
611 
612 	(void)usbd_do_request(sc->sc_udev, &req, 0);
613 }
614 
615 usbd_status
616 umsm_huawei_changemode(struct usbd_device *dev)
617 {
618 	usb_device_request_t req;
619 	usbd_status err;
620 
621 	req.bmRequestType = UT_WRITE_DEVICE;
622 	req.bRequest = UR_SET_FEATURE;
623 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
624 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
625 	USETW(req.wLength, 0);
626 
627 	err = usbd_do_request(dev, &req, 0);
628 	if (err)
629 		return (EIO);
630 
631 	return (0);
632 }
633 
634 usbd_status
635 umsm_truinstall_changemode(struct usbd_device *dev)
636 {
637 	usb_device_request_t req;
638 	usbd_status err;
639 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
640 	req.bRequest = TRUINSTALL_CHANGEMODE_REQUEST;
641 	USETW(req.wValue, 0x1);
642 	USETW(req.wIndex, 0);
643 	USETW(req.wLength, 0);
644 
645 	err = usbd_do_request(dev, &req, 0);
646 	if (err)
647 		return (EIO);
648 
649 	return (0);
650 }
651 
652 usbd_status
653 umsm_umass_changemode(struct umsm_softc *sc)
654 {
655 #define UMASS_CMD_REZERO_UNIT		0x01
656 #define UMASS_CMD_START_STOP		0x1b
657 #define UMASS_CMDPARAM_EJECT		0x02
658 #define UMASS_SERVICE_ACTION_OUT	0x9f
659 	usb_interface_descriptor_t *id;
660 	usb_endpoint_descriptor_t *ed;
661 	struct usbd_xfer *xfer;
662 	struct usbd_pipe *cmdpipe;
663 	usbd_status err;
664 	u_int32_t n;
665 	void *bufp;
666 	int target_ep, i;
667 
668 	struct umass_bbb_cbw cbw;
669 	static int dCBWTag = 0x12345678;
670 
671 	USETDW(cbw.dCBWSignature, CBWSIGNATURE);
672 	USETDW(cbw.dCBWTag, dCBWTag);
673 	cbw.bCBWLUN   = 0;
674 	cbw.bCDBLength= 6;
675 	bzero(cbw.CBWCDB, sizeof(cbw.CBWCDB));
676 
677 	switch (sc->sc_flag) {
678 	case DEV_UMASS1:
679 		USETDW(cbw.dCBWDataTransferLength, 0x0);
680 		cbw.bCBWFlags = CBWFLAGS_OUT;
681 		cbw.CBWCDB[0] = UMASS_CMD_REZERO_UNIT;
682 		cbw.CBWCDB[1] = 0x0;	/* target LUN: 0 */
683 		break;
684 	case DEV_UMASS2:
685 		USETDW(cbw.dCBWDataTransferLength, 0x1);
686 		cbw.bCBWFlags = CBWFLAGS_IN;
687 		cbw.CBWCDB[0] = UMASS_CMD_REZERO_UNIT;
688 		cbw.CBWCDB[1] = 0x0;	/* target LUN: 0 */
689 		break;
690 	case DEV_UMASS3: /* longcheer */
691 		USETDW(cbw.dCBWDataTransferLength, 0x80);
692 		cbw.bCBWFlags = CBWFLAGS_IN;
693 		cbw.CBWCDB[0] = 0x06;
694 		cbw.CBWCDB[1] = 0xf5;
695 		cbw.CBWCDB[2] = 0x04;
696 		cbw.CBWCDB[3] = 0x02;
697 		cbw.CBWCDB[4] = 0x52;
698 		cbw.CBWCDB[5] = 0x70;
699 		break;
700 	case DEV_UMASS4:
701 		USETDW(cbw.dCBWDataTransferLength, 0x0);
702 		cbw.bCBWFlags = CBWFLAGS_OUT;
703 		cbw.CBWCDB[0] = UMASS_CMD_START_STOP;
704 		cbw.CBWCDB[1] = 0x00;	/* target LUN: 0 */
705 		cbw.CBWCDB[4] = UMASS_CMDPARAM_EJECT;
706 		break;
707 	case DEV_UMASS5:
708 		cbw.bCBWFlags = CBWFLAGS_OUT;
709 		cbw.CBWCDB[0] = 0x11;
710 		cbw.CBWCDB[1] = 0x06;
711 		break;
712 	case DEV_UMASS6:	/* ZTE */
713 		USETDW(cbw.dCBWDataTransferLength, 0x20);
714 		cbw.bCBWFlags = CBWFLAGS_IN;
715 		cbw.bCDBLength= 12;
716 		cbw.CBWCDB[0] = 0x85;
717 		cbw.CBWCDB[1] = 0x01;
718 		cbw.CBWCDB[2] = 0x01;
719 		cbw.CBWCDB[3] = 0x01;
720 		cbw.CBWCDB[4] = 0x18;
721 		cbw.CBWCDB[5] = 0x01;
722 		cbw.CBWCDB[6] = 0x01;
723 		cbw.CBWCDB[7] = 0x01;
724 		cbw.CBWCDB[8] = 0x01;
725 		cbw.CBWCDB[9] = 0x01;
726 		break;
727 	case DEV_UMASS7:	/* ZTE */
728 		USETDW(cbw.dCBWDataTransferLength, 0xc0);
729 		cbw.bCBWFlags = CBWFLAGS_IN;
730 		cbw.CBWCDB[0] = UMASS_SERVICE_ACTION_OUT;
731 		cbw.CBWCDB[1] = 0x03;
732 		break;
733 	case DEV_UMASS8:
734 		USETDW(cbw.dCBWDataTransferLength, 0x0);
735 		cbw.bCBWFlags = CBWFLAGS_OUT;
736 		cbw.CBWCDB[0] = 0xf0;
737 		cbw.CBWCDB[1] = 0x01;
738 		cbw.CBWCDB[2] = 0x03;
739 		break;
740 	default:
741 		DPRINTF(("%s: unknown device type.\n", sc->sc_dev.dv_xname));
742 		break;
743 	}
744 
745 	/* get command endpoint address */
746 	id = usbd_get_interface_descriptor(sc->sc_iface);
747 	for (i = 0; i < id->bNumEndpoints; i++) {
748 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
749 		if (ed == NULL) {
750 			return (USBD_IOERROR);
751 		}
752 
753 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
754 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
755 			target_ep = ed->bEndpointAddress;
756 	}
757 
758 	/* open command endppoint */
759 	err = usbd_open_pipe(sc->sc_iface, target_ep,
760 		USBD_EXCLUSIVE_USE, &cmdpipe);
761 	if (err) {
762 		DPRINTF(("%s: open pipe for modem change cmd failed: %s\n",
763 		    sc->sc_dev.dv_xname, usbd_errstr(err)));
764 		return (err);
765 	}
766 
767 	xfer = usbd_alloc_xfer(sc->sc_udev);
768 	if (xfer == NULL) {
769 		usbd_close_pipe(cmdpipe);
770 		return (USBD_NOMEM);
771 	} else {
772 		bufp = usbd_alloc_buffer(xfer, UMASS_BBB_CBW_SIZE);
773 		if (bufp == NULL)
774 			err = USBD_NOMEM;
775 		else {
776 			n = UMASS_BBB_CBW_SIZE;
777 			memcpy(bufp, &cbw, UMASS_BBB_CBW_SIZE);
778 			usbd_setup_xfer(xfer, cmdpipe, 0, bufp, n,
779 			    USBD_NO_COPY | USBD_SYNCHRONOUS, 0, NULL);
780 			err = usbd_transfer(xfer);
781 			if (err) {
782 				usbd_clear_endpoint_stall(cmdpipe);
783 				DPRINTF(("%s: send error:%s", __func__,
784 				    usbd_errstr(err)));
785 			}
786 		}
787 		usbd_close_pipe(cmdpipe);
788 		usbd_free_buffer(xfer);
789 		usbd_free_xfer(xfer);
790 	}
791 
792 	return (err);
793 }
794