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