1 /* $NetBSD: ubsa.c,v 1.23 2008/05/24 16:40:58 cube Exp $ */ 2 /*- 3 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 /* 28 * Copyright (c) 2001 The NetBSD Foundation, Inc. 29 * All rights reserved. 30 * 31 * This code is derived from software contributed to The NetBSD Foundation 32 * by Ichiro FUKUHARA (ichiro@ichiro.org). 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53 * POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 #include <sys/cdefs.h> 57 __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.23 2008/05/24 16:40:58 cube Exp $"); 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/kernel.h> 62 #include <sys/malloc.h> 63 #ifdef __FreeBSD__ 64 #include <sys/bus.h> 65 #endif 66 #include <sys/ioccom.h> 67 #include <sys/fcntl.h> 68 #include <sys/conf.h> 69 #include <sys/tty.h> 70 #include <sys/file.h> 71 #if __FreeBSD_version >= 500014 72 #include <sys/selinfo.h> 73 #else 74 #include <sys/select.h> 75 #endif 76 #include <sys/proc.h> 77 #include <sys/device.h> 78 #include <sys/poll.h> 79 #include <sys/sysctl.h> 80 81 #include <dev/usb/usb.h> 82 #include <dev/usb/usbcdc.h> 83 84 #include <dev/usb/usbdi.h> 85 #include <dev/usb/usbdi_util.h> 86 #include <dev/usb/usbdevs.h> 87 #include <dev/usb/usb_quirks.h> 88 89 #include <dev/usb/ucomvar.h> 90 #include <dev/usb/ubsavar.h> 91 92 #ifdef UBSA_DEBUG 93 Static int ubsadebug = 0; 94 #ifdef __FreeBSD__ 95 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa"); 96 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW, 97 &ubsadebug, 0, "ubsa debug level"); 98 #endif 99 100 #define DPRINTFN(n, x) do { \ 101 if (ubsadebug > (n)) \ 102 logprintf x; \ 103 } while (0) 104 #else 105 #define DPRINTFN(n, x) 106 #endif 107 #define DPRINTF(x) DPRINTFN(0, x) 108 109 struct ucom_methods ubsa_methods = { 110 ubsa_get_status, 111 ubsa_set, 112 ubsa_param, 113 NULL, 114 ubsa_open, 115 ubsa_close, 116 NULL, 117 NULL 118 }; 119 120 Static const struct usb_devno ubsa_devs[] = { 121 /* BELKIN F5U103 */ 122 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 }, 123 /* BELKIN F5U120 */ 124 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 }, 125 /* GoHubs GO-COM232 */ 126 { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM }, 127 /* GoHubs GO-COM232 */ 128 { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 }, 129 /* Peracom */ 130 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 }, 131 /* Option N.V. */ 132 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G }, 133 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 }, 134 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS }, 135 /* AnyDATA ADU-E100H */ 136 { USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H }, 137 }; 138 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p) 139 140 int ubsa_match(device_t, cfdata_t, void *); 141 void ubsa_attach(device_t, device_t, void *); 142 void ubsa_childdet(device_t, device_t); 143 int ubsa_detach(device_t, int); 144 int ubsa_activate(device_t, enum devact); 145 146 extern struct cfdriver ubsa_cd; 147 148 CFATTACH_DECL2_NEW(ubsa, sizeof(struct ubsa_softc), 149 ubsa_match, ubsa_attach, ubsa_detach, ubsa_activate, NULL, ubsa_childdet); 150 151 USB_MATCH(ubsa) 152 { 153 USB_MATCH_START(ubsa, uaa); 154 155 return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ? 156 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 157 } 158 159 USB_ATTACH(ubsa) 160 { 161 USB_ATTACH_START(ubsa, sc, uaa); 162 usbd_device_handle dev = uaa->device; 163 usb_config_descriptor_t *cdesc; 164 usb_interface_descriptor_t *id; 165 usb_endpoint_descriptor_t *ed; 166 char *devinfop; 167 usbd_status err; 168 struct ucom_attach_args uca; 169 int i; 170 171 sc->sc_dev = self; 172 devinfop = usbd_devinfo_alloc(dev, 0); 173 USB_ATTACH_SETUP; 174 aprint_normal_dev(self, "%s\n", devinfop); 175 usbd_devinfo_free(devinfop); 176 177 sc->sc_udev = dev; 178 sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX; 179 sc->sc_numif = 1; /* default device has one interface */ 180 181 /* 182 * initialize rts, dtr variables to something 183 * different from boolean 0, 1 184 */ 185 sc->sc_dtr = -1; 186 sc->sc_rts = -1; 187 188 /* 189 * Quad UMTS cards use different requests to 190 * control com settings and only some. 191 */ 192 sc->sc_quadumts = 0; 193 if (uaa->vendor == USB_VENDOR_OPTIONNV) { 194 switch (uaa->product) { 195 case USB_PRODUCT_OPTIONNV_QUADUMTS: 196 case USB_PRODUCT_OPTIONNV_QUADUMTS2: 197 sc->sc_quadumts = 1; 198 break; 199 } 200 } 201 202 DPRINTF(("ubsa attach: sc = %p\n", sc)); 203 204 /* Move the device into the configured state. */ 205 err = usbd_set_config_index(dev, sc->sc_config_index, 1); 206 if (err) { 207 aprint_error_dev(self, 208 "failed to set configuration: %s\n", 209 usbd_errstr(err)); 210 sc->sc_dying = 1; 211 goto error; 212 } 213 214 /* get the config descriptor */ 215 cdesc = usbd_get_config_descriptor(sc->sc_udev); 216 217 if (cdesc == NULL) { 218 aprint_error_dev(self, 219 "failed to get configuration descriptor\n"); 220 sc->sc_dying = 1; 221 goto error; 222 } 223 224 sc->sc_intr_number = -1; 225 sc->sc_intr_pipe = NULL; 226 227 /* get the interfaces */ 228 err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET, 229 &sc->sc_iface[0]); 230 if (err) { 231 /* can not get main interface */ 232 sc->sc_dying = 1; 233 goto error; 234 } 235 236 /* Find the endpoints */ 237 id = usbd_get_interface_descriptor(sc->sc_iface[0]); 238 sc->sc_iface_number[0] = id->bInterfaceNumber; 239 240 /* initialize endpoints */ 241 uca.bulkin = uca.bulkout = -1; 242 243 for (i = 0; i < id->bNumEndpoints; i++) { 244 ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i); 245 if (ed == NULL) { 246 aprint_error_dev(self, 247 "no endpoint descriptor for %d\n", i); 248 break; 249 } 250 251 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 252 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 253 sc->sc_intr_number = ed->bEndpointAddress; 254 sc->sc_isize = UGETW(ed->wMaxPacketSize); 255 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 256 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 257 uca.bulkin = ed->bEndpointAddress; 258 uca.ibufsize = UGETW(ed->wMaxPacketSize); 259 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 260 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 261 uca.bulkout = ed->bEndpointAddress; 262 uca.obufsize = UGETW(ed->wMaxPacketSize); 263 } 264 } /* end of Endpoint loop */ 265 266 if (sc->sc_intr_number == -1) { 267 aprint_error_dev(self, "Could not find interrupt in\n"); 268 sc->sc_dying = 1; 269 goto error; 270 } 271 272 if (uca.bulkin == -1) { 273 aprint_error_dev(self, "Could not find data bulk in\n"); 274 sc->sc_dying = 1; 275 goto error; 276 } 277 278 if (uca.bulkout == -1) { 279 aprint_error_dev(self, "Could not find data bulk out\n"); 280 sc->sc_dying = 1; 281 goto error; 282 } 283 284 uca.portno = 0; 285 /* bulkin, bulkout set above */ 286 uca.ibufsizepad = uca.ibufsize; 287 uca.opkthdrlen = 0; 288 uca.device = dev; 289 uca.iface = sc->sc_iface[0]; 290 uca.methods = &ubsa_methods; 291 uca.arg = sc; 292 uca.info = NULL; 293 DPRINTF(("ubsa: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n", 294 i, uca.bulkin, uca.bulkout, sc->sc_intr_number)); 295 sc->sc_subdevs[0] = config_found_sm_loc(self, "ucombus", NULL, &uca, 296 ucomprint, ucomsubmatch); 297 298 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 299 USBDEV(sc->sc_dev)); 300 301 USB_ATTACH_SUCCESS_RETURN; 302 303 error: 304 USB_ATTACH_ERROR_RETURN; 305 } 306 307 308 void 309 ubsa_childdet(device_t self, device_t child) 310 { 311 int i; 312 struct ubsa_softc *sc = device_private(self); 313 314 for (i = 0; i < sc->sc_numif; i++) { 315 if (sc->sc_subdevs[i] == child) 316 break; 317 } 318 KASSERT(i < sc->sc_numif); 319 sc->sc_subdevs[i] = NULL; 320 } 321 322 USB_DETACH(ubsa) 323 { 324 USB_DETACH_START(ubsa, sc); 325 int i; 326 int rv = 0; 327 328 329 DPRINTF(("ubsa_detach: sc = %p\n", sc)); 330 331 if (sc->sc_intr_pipe != NULL) { 332 usbd_abort_pipe(sc->sc_intr_pipe); 333 usbd_close_pipe(sc->sc_intr_pipe); 334 free(sc->sc_intr_buf, M_USBDEV); 335 sc->sc_intr_pipe = NULL; 336 } 337 338 sc->sc_dying = 1; 339 for (i = 0; i < sc->sc_numif; i++) { 340 if (sc->sc_subdevs[i] != NULL) 341 rv |= config_detach(sc->sc_subdevs[i], flags); 342 } 343 344 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 345 USBDEV(sc->sc_dev)); 346 347 return (rv); 348 } 349 350 int 351 ubsa_activate(device_ptr_t self, enum devact act) 352 { 353 struct ubsa_softc *sc = device_private(self); 354 int rv = 0; 355 int i; 356 357 switch (act) { 358 case DVACT_ACTIVATE: 359 return (EOPNOTSUPP); 360 361 case DVACT_DEACTIVATE: 362 for (i = 0; i < sc->sc_numif; i++) { 363 if (sc->sc_subdevs[i] != NULL) 364 rv |= config_deactivate(sc->sc_subdevs[i]); 365 } 366 sc->sc_dying = 1; 367 break; 368 } 369 return (rv); 370 } 371 372