1 /* $NetBSD: uipaq.c,v 1.21 2016/07/07 06:55:42 msaitoh Exp $ */ 2 /* $OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt Exp $ */ 3 4 /* 5 * Copyright (c) 2000-2005 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * iPAQ driver 36 * 37 * 19 July 2003: Incorporated changes suggested by Sam Lawrance from 38 * the uppc module 39 * 40 * 41 * Contact isis@cs.umd.edu if you have any questions/comments about this driver 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: uipaq.c,v 1.21 2016/07/07 06:55:42 msaitoh Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/device.h> 51 #include <sys/conf.h> 52 #include <sys/tty.h> 53 54 #include <dev/usb/usb.h> 55 56 #include <dev/usb/usbcdc.h> /*UCDC_* stuff */ 57 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdevs.h> 61 62 #include <dev/usb/ucomvar.h> 63 64 #ifdef UIPAQ_DEBUG 65 #define DPRINTF(x) if (uipaqdebug) printf x 66 #define DPRINTFN(n,x) if (uipaqdebug>(n)) printf x 67 int uipaqdebug = 0; 68 #else 69 #define DPRINTF(x) 70 #define DPRINTFN(n,x) 71 #endif 72 73 #define UIPAQ_CONFIG_NO 1 74 #define UIPAQ_IFACE_INDEX 0 75 76 #define UIPAQIBUFSIZE 1024 77 #define UIPAQOBUFSIZE 1024 78 79 struct uipaq_softc { 80 device_t sc_dev; /* base device */ 81 struct usbd_device * sc_udev; /* device */ 82 struct usbd_interface * sc_iface; /* interface */ 83 84 device_t sc_subdev; /* ucom uses that */ 85 uint16_t sc_lcr; /* state for DTR/RTS */ 86 87 uint16_t sc_flags; 88 89 u_char sc_dying; 90 }; 91 92 /* Callback routines */ 93 Static void uipaq_set(void *, int, int, int); 94 95 96 /* Support routines. */ 97 /* based on uppc module by Sam Lawrance */ 98 Static void uipaq_dtr(struct uipaq_softc *, int); 99 Static void uipaq_rts(struct uipaq_softc *, int); 100 Static void uipaq_break(struct uipaq_softc *, int); 101 102 103 struct ucom_methods uipaq_methods = { 104 .ucom_get_status = NULL, 105 .ucom_set = uipaq_set, 106 .ucom_param = NULL, 107 .ucom_ioctl = NULL, 108 .ucom_open = NULL, 109 .ucom_close = NULL, 110 .ucom_read = NULL, 111 .ucom_write = NULL, 112 }; 113 114 struct uipaq_type { 115 struct usb_devno uv_dev; 116 uint16_t uv_flags; 117 }; 118 119 static const struct uipaq_type uipaq_devs[] = { 120 {{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 }, 121 {{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0}, 122 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0}, 123 {{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0}, 124 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0}, 125 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS011SH} , 0} 126 }; 127 128 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p)) 129 130 int uipaq_match(device_t, cfdata_t, void *); 131 void uipaq_attach(device_t, device_t, void *); 132 void uipaq_childdet(device_t, device_t); 133 int uipaq_detach(device_t, int); 134 int uipaq_activate(device_t, enum devact); 135 extern struct cfdriver uipaq_cd; 136 CFATTACH_DECL2_NEW(uipaq, sizeof(struct uipaq_softc), uipaq_match, 137 uipaq_attach, uipaq_detach, uipaq_activate, NULL, uipaq_childdet); 138 139 int 140 uipaq_match(device_t parent, cfdata_t match, void *aux) 141 { 142 struct usb_attach_arg *uaa = aux; 143 144 DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n", 145 uaa->uaa_vendor, uaa->uaa_product)); 146 147 return uipaq_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 148 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 149 } 150 151 void 152 uipaq_attach(device_t parent, device_t self, void *aux) 153 { 154 struct uipaq_softc *sc = device_private(self); 155 struct usb_attach_arg *uaa = aux; 156 struct usbd_device *dev = uaa->uaa_device; 157 struct usbd_interface *iface; 158 usb_interface_descriptor_t *id; 159 usb_endpoint_descriptor_t *ed; 160 char *devinfop; 161 const char *devname = device_xname(self); 162 int i; 163 usbd_status err; 164 struct ucom_attach_args ucaa; 165 166 DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc)); 167 168 sc->sc_dev = self; 169 170 aprint_naive("\n"); 171 aprint_normal("\n"); 172 173 devinfop = usbd_devinfo_alloc(dev, 0); 174 aprint_normal_dev(self, "%s\n", devinfop); 175 usbd_devinfo_free(devinfop); 176 177 /* Move the device into the configured state. */ 178 err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1); 179 if (err) { 180 aprint_error_dev(self, "failed to set configuration, err=%s\n", 181 usbd_errstr(err)); 182 goto bad; 183 } 184 185 err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface); 186 if (err) { 187 aprint_error("\n%s: failed to get interface, err=%s\n", 188 devname, usbd_errstr(err)); 189 goto bad; 190 } 191 192 sc->sc_flags = uipaq_lookup(uaa->uaa_vendor, uaa->uaa_product)->uv_flags; 193 194 id = usbd_get_interface_descriptor(iface); 195 196 sc->sc_udev = dev; 197 sc->sc_iface = iface; 198 199 ucaa.ucaa_ibufsize = UIPAQIBUFSIZE; 200 ucaa.ucaa_obufsize = UIPAQOBUFSIZE; 201 ucaa.ucaa_ibufsizepad = UIPAQIBUFSIZE; 202 ucaa.ucaa_opkthdrlen = 0; 203 ucaa.ucaa_device = dev; 204 ucaa.ucaa_iface = iface; 205 ucaa.ucaa_methods = &uipaq_methods; 206 ucaa.ucaa_arg = sc; 207 ucaa.ucaa_portno = UCOM_UNK_PORTNO; 208 ucaa.ucaa_info = "Generic"; 209 210 /* err = uipaq_init(sc); 211 if (err) { 212 printf("%s: init failed, %s\n", device_xname(sc->sc_dev), 213 usbd_errstr(err)); 214 goto bad; 215 }*/ 216 217 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 218 219 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 220 for (i=0; i<id->bNumEndpoints; i++) { 221 ed = usbd_interface2endpoint_descriptor(iface, i); 222 if (ed == NULL) { 223 aprint_error_dev(self, 224 "no endpoint descriptor for %d\n", i); 225 goto bad; 226 } 227 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 228 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 229 ucaa.ucaa_bulkin = ed->bEndpointAddress; 230 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 231 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 232 ucaa.ucaa_bulkout = ed->bEndpointAddress; 233 } 234 } 235 if (ucaa.ucaa_bulkin == -1 || ucaa.ucaa_bulkout == -1) { 236 aprint_error_dev(self, "no proper endpoints found (%d,%d) \n", 237 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout); 238 return; 239 } 240 241 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa, 242 ucomprint, ucomsubmatch); 243 244 return; 245 246 bad: 247 DPRINTF(("uipaq_attach: ATTACH ERROR\n")); 248 sc->sc_dying = 1; 249 return; 250 } 251 252 253 void 254 uipaq_dtr(struct uipaq_softc* sc, int onoff) 255 { 256 usb_device_request_t req; 257 usbd_status err; 258 int retries = 3; 259 260 DPRINTF(("%s: uipaq_dtr: onoff=%x\n", device_xname(sc->sc_dev), onoff)); 261 262 /* Avoid sending unnecessary requests */ 263 if (onoff && (sc->sc_lcr & UCDC_LINE_DTR)) 264 return; 265 if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR)) 266 return; 267 268 /* Other parameters depend on reg */ 269 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 270 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 271 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR 272 : sc->sc_lcr & ~UCDC_LINE_DTR; 273 USETW(req.wValue, sc->sc_lcr); 274 USETW(req.wIndex, 0x0); 275 USETW(req.wLength, 0); 276 277 /* Fire off the request a few times if necessary */ 278 while (retries) { 279 err = usbd_do_request(sc->sc_udev, &req, NULL); 280 if (!err) 281 break; 282 retries--; 283 } 284 } 285 286 287 void 288 uipaq_rts(struct uipaq_softc* sc, int onoff) 289 { 290 usb_device_request_t req; 291 usbd_status err; 292 int retries = 3; 293 294 DPRINTF(("%s: uipaq_rts: onoff=%x\n", device_xname(sc->sc_dev), onoff)); 295 296 /* Avoid sending unnecessary requests */ 297 if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return; 298 if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return; 299 300 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 301 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 302 sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS 303 : sc->sc_lcr & ~UCDC_LINE_RTS; 304 USETW(req.wValue, sc->sc_lcr); 305 USETW(req.wIndex, 0x0); 306 USETW(req.wLength, 0); 307 308 while (retries) { 309 err = usbd_do_request(sc->sc_udev, &req, NULL); 310 if (!err) 311 break; 312 retries--; 313 } 314 } 315 316 317 void 318 uipaq_break(struct uipaq_softc* sc, int onoff) 319 { 320 usb_device_request_t req; 321 usbd_status err; 322 int retries = 3; 323 324 DPRINTF(("%s: uipaq_break: onoff=%x\n", device_xname(sc->sc_dev), 325 onoff)); 326 327 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 328 req.bRequest = UCDC_SEND_BREAK; 329 330 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 331 USETW(req.wIndex, 0x0); 332 USETW(req.wLength, 0); 333 334 while (retries) { 335 err = usbd_do_request(sc->sc_udev, &req, NULL); 336 if (!err) 337 break; 338 retries--; 339 } 340 } 341 342 343 void 344 uipaq_set(void *addr, int portno, int reg, int onoff) 345 { 346 struct uipaq_softc* sc = addr; 347 348 switch (reg) { 349 case UCOM_SET_DTR: 350 uipaq_dtr(addr, onoff); 351 break; 352 case UCOM_SET_RTS: 353 uipaq_rts(addr, onoff); 354 break; 355 case UCOM_SET_BREAK: 356 uipaq_break(addr, onoff); 357 break; 358 default: 359 aprint_error_dev(sc->sc_dev, 360 "unhandled set request: reg=%x onoff=%x\n", reg, onoff); 361 return; 362 } 363 } 364 365 366 int 367 uipaq_activate(device_t self, enum devact act) 368 { 369 struct uipaq_softc *sc = device_private(self); 370 371 switch (act) { 372 case DVACT_DEACTIVATE: 373 sc->sc_dying = 1; 374 return 0; 375 default: 376 return EOPNOTSUPP; 377 } 378 } 379 380 void 381 uipaq_childdet(device_t self, device_t child) 382 { 383 struct uipaq_softc *sc = device_private(self); 384 385 KASSERT(sc->sc_subdev == child); 386 sc->sc_subdev = NULL; 387 } 388 389 int 390 uipaq_detach(device_t self, int flags) 391 { 392 struct uipaq_softc *sc = device_private(self); 393 int rv = 0; 394 395 DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags)); 396 sc->sc_dying = 1; 397 if (sc->sc_subdev != NULL) 398 rv |= config_detach(sc->sc_subdev, flags); 399 400 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 401 402 return rv; 403 } 404 405