1 /* $NetBSD: uark.c,v 1.15 2019/05/09 02:43:35 mrg Exp $ */ 2 /* $OpenBSD: uark.c,v 1.13 2009/10/13 19:33:17 pirofti Exp $ */ 3 4 /* 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 #include <sys/cdefs.h> 21 __KERNEL_RCSID(0, "$NetBSD: uark.c,v 1.15 2019/05/09 02:43:35 mrg Exp $"); 22 23 #ifdef _KERNEL_OPT 24 #include "opt_usb.h" 25 #endif 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/kernel.h> 30 #include <sys/conf.h> 31 #include <sys/tty.h> 32 #include <sys/device.h> 33 34 #include <dev/usb/usb.h> 35 #include <dev/usb/usbdi.h> 36 #include <dev/usb/usbdi_util.h> 37 #include <dev/usb/usbdevs.h> 38 39 #include <dev/usb/ucomvar.h> 40 41 #ifdef UARK_DEBUG 42 #define DPRINTFN(n, x) do { if (uarkdebug > (n)) printf x; } while (0) 43 int uarkebug = 0; 44 #else 45 #define DPRINTFN(n, x) 46 #endif 47 #define DPRINTF(x) DPRINTFN(0, x) 48 49 #define UARKBUFSZ 256 50 #define UARK_CONFIG_NO 0 51 #define UARK_IFACE_NO 0 52 53 #define UARK_SET_DATA_BITS(x) (x - 5) 54 55 #define UARK_PARITY_NONE 0x00 56 #define UARK_PARITY_ODD 0x08 57 #define UARK_PARITY_EVEN 0x18 58 59 #define UARK_STOP_BITS_1 0x00 60 #define UARK_STOP_BITS_2 0x04 61 62 #define UARK_BAUD_REF 3000000 63 64 #define UARK_WRITE 0x40 65 #define UARK_READ 0xc0 66 67 #define UARK_REQUEST 0xfe 68 69 struct uark_softc { 70 device_t sc_dev; 71 struct usbd_device * sc_udev; 72 struct usbd_interface * sc_iface; 73 device_t sc_subdev; 74 75 u_char sc_msr; 76 u_char sc_lsr; 77 78 bool sc_dying; 79 }; 80 81 static void uark_get_status(void *, int portno, u_char *lsr, u_char *msr); 82 static void uark_set(void *, int, int, int); 83 static int uark_param(void *, int, struct termios *); 84 static int uark_open(void *, int); 85 static void uark_break(void *, int, int); 86 static int uark_cmd(struct uark_softc *, uint16_t, uint16_t); 87 88 struct ucom_methods uark_methods = { 89 .ucom_get_status = uark_get_status, 90 .ucom_set = uark_set, 91 .ucom_param = uark_param, 92 .ucom_open = uark_open, 93 }; 94 95 static const struct usb_devno uark_devs[] = { 96 { USB_VENDOR_ARKMICROCHIPS, USB_PRODUCT_ARKMICROCHIPS_USBSERIAL }, 97 }; 98 99 static int uark_match(device_t, cfdata_t, void *); 100 static void uark_attach(device_t, device_t, void *); 101 static int uark_detach(device_t, int); 102 103 CFATTACH_DECL_NEW(uark, sizeof(struct uark_softc), uark_match, uark_attach, 104 uark_detach, NULL); 105 106 static int 107 uark_match(device_t parent, cfdata_t match, void *aux) 108 { 109 struct usb_attach_arg *uaa = aux; 110 111 return (usb_lookup(uark_devs, uaa->uaa_vendor, uaa->uaa_product) 112 != NULL) ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 113 } 114 115 static void 116 uark_attach(device_t parent, device_t self, void *aux) 117 { 118 struct uark_softc *sc = device_private(self); 119 struct usb_attach_arg *uaa = aux; 120 struct usbd_device *dev = uaa->uaa_device; 121 char *devinfop; 122 struct ucom_attach_args ucaa; 123 usb_interface_descriptor_t *id; 124 usb_endpoint_descriptor_t *ed; 125 usbd_status error; 126 int i; 127 128 memset(&ucaa, 0, sizeof(ucaa)); 129 sc->sc_dev = self; 130 131 devinfop = usbd_devinfo_alloc(dev, 0); 132 aprint_naive("\n"); 133 aprint_normal("\n"); 134 aprint_normal_dev(self, "%s\n", devinfop); 135 usbd_devinfo_free(devinfop); 136 137 sc->sc_udev = dev; 138 sc->sc_dying = false; 139 140 if (usbd_set_config_index(sc->sc_udev, UARK_CONFIG_NO, 1) != 0) { 141 aprint_error_dev(self, "could not set configuration no\n"); 142 sc->sc_dying = true; 143 return; 144 } 145 146 /* get the first interface handle */ 147 error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO, 148 &sc->sc_iface); 149 if (error != 0) { 150 aprint_error_dev(self, "could not get interface handle\n"); 151 sc->sc_dying = true; 152 return; 153 } 154 155 id = usbd_get_interface_descriptor(sc->sc_iface); 156 157 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 158 for (i = 0; i < id->bNumEndpoints; i++) { 159 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 160 if (ed == NULL) { 161 aprint_error_dev(self, 162 "no endpoint descriptor found for %d\n", i); 163 sc->sc_dying = true; 164 return; 165 } 166 167 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 168 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 169 ucaa.ucaa_bulkin = ed->bEndpointAddress; 170 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 171 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 172 ucaa.ucaa_bulkout = ed->bEndpointAddress; 173 } 174 175 if (ucaa.ucaa_bulkin == -1 || ucaa.ucaa_bulkout == -1) { 176 aprint_error_dev(self, "missing endpoint\n"); 177 sc->sc_dying = true; 178 return; 179 } 180 181 ucaa.ucaa_ibufsize = UARKBUFSZ; 182 ucaa.ucaa_obufsize = UARKBUFSZ; 183 ucaa.ucaa_ibufsizepad = UARKBUFSZ; 184 ucaa.ucaa_opkthdrlen = 0; 185 ucaa.ucaa_device = sc->sc_udev; 186 ucaa.ucaa_iface = sc->sc_iface; 187 ucaa.ucaa_methods = &uark_methods; 188 ucaa.ucaa_arg = sc; 189 ucaa.ucaa_info = NULL; 190 191 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 192 193 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa, 194 ucomprint, ucomsubmatch); 195 196 return; 197 } 198 199 static int 200 uark_detach(device_t self, int flags) 201 { 202 struct uark_softc *sc = device_private(self); 203 int rv = 0; 204 205 sc->sc_dying = true; 206 207 if (sc->sc_subdev != NULL) { 208 rv = config_detach(sc->sc_subdev, flags); 209 sc->sc_subdev = NULL; 210 } 211 212 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 213 214 return rv; 215 } 216 217 static void 218 uark_set(void *vsc, int portno, int reg, int onoff) 219 { 220 struct uark_softc *sc = vsc; 221 222 if (sc->sc_dying) 223 return; 224 225 switch (reg) { 226 case UCOM_SET_BREAK: 227 uark_break(sc, portno, onoff); 228 return; 229 case UCOM_SET_DTR: 230 case UCOM_SET_RTS: 231 default: 232 return; 233 } 234 } 235 236 static int 237 uark_param(void *vsc, int portno, struct termios *t) 238 { 239 struct uark_softc *sc = (struct uark_softc *)vsc; 240 int data; 241 242 if (sc->sc_dying) 243 return EIO; 244 245 switch (t->c_ospeed) { 246 case 300: 247 case 600: 248 case 1200: 249 case 1800: 250 case 2400: 251 case 4800: 252 case 9600: 253 case 19200: 254 case 38400: 255 case 57600: 256 case 115200: 257 uark_cmd(sc, 3, 0x83); 258 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF); 259 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8); 260 uark_cmd(sc, 3, 0x03); 261 break; 262 default: 263 return EINVAL; 264 } 265 266 if (ISSET(t->c_cflag, CSTOPB)) 267 data = UARK_STOP_BITS_2; 268 else 269 data = UARK_STOP_BITS_1; 270 271 if (ISSET(t->c_cflag, PARENB)) { 272 if (ISSET(t->c_cflag, PARODD)) 273 data |= UARK_PARITY_ODD; 274 else 275 data |= UARK_PARITY_EVEN; 276 } else 277 data |= UARK_PARITY_NONE; 278 279 switch (ISSET(t->c_cflag, CSIZE)) { 280 case CS5: 281 data |= UARK_SET_DATA_BITS(5); 282 break; 283 case CS6: 284 data |= UARK_SET_DATA_BITS(6); 285 break; 286 case CS7: 287 data |= UARK_SET_DATA_BITS(7); 288 break; 289 case CS8: 290 data |= UARK_SET_DATA_BITS(8); 291 break; 292 } 293 294 uark_cmd(sc, 3, 0x00); 295 uark_cmd(sc, 3, data); 296 297 #if 0 298 /* XXX flow control */ 299 if (ISSET(t->c_cflag, CRTSCTS)) { 300 /* rts/cts flow ctl */ 301 } else if (ISSET(t->c_iflag, IXON|IXOFF)) { 302 /* xon/xoff flow ctl */ 303 } else { 304 /* disable flow ctl */ 305 } 306 #endif 307 308 return 0; 309 } 310 311 static int 312 uark_open(void *arg, int portno) 313 { 314 struct uark_softc *sc = arg; 315 316 if (sc->sc_dying) 317 return EIO; 318 319 return 0; 320 } 321 322 static void 323 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 324 { 325 struct uark_softc *sc = vsc; 326 327 if (sc->sc_dying) 328 return; 329 330 *msr = sc->sc_msr; 331 *lsr = sc->sc_lsr; 332 } 333 334 static void 335 uark_break(void *vsc, int portno, int onoff) 336 { 337 #if 0 338 struct uark_softc *sc = vsc; 339 340 if (sc->sc_dying) 341 return; 342 343 #ifdef UARK_DEBUG 344 aprint_normal_dev(sc->sc_dev, "break %s!\n", onoff ? "on" : "off"); 345 #endif 346 347 if (onoff) 348 /* break on */ 349 uark_cmd(sc, 4, 0x01); 350 else 351 uark_cmd(sc, 4, 0x00); 352 #endif 353 } 354 355 static int 356 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value) 357 { 358 usb_device_request_t req; 359 usbd_status err; 360 361 req.bmRequestType = UARK_WRITE; 362 req.bRequest = UARK_REQUEST; 363 USETW(req.wValue, value); 364 USETW(req.wIndex, index); 365 USETW(req.wLength, 0); 366 err = usbd_do_request(sc->sc_udev, &req, NULL); 367 368 if (err) 369 return EIO; 370 371 return 0; 372 } 373