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