1 /* $OpenBSD: moscom.c,v 1.23 2016/01/07 12:53:37 mpi Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/tty.h> 23 #include <sys/device.h> 24 25 #include <dev/usb/usb.h> 26 #include <dev/usb/usbdi.h> 27 #include <dev/usb/usbdi_util.h> 28 #include <dev/usb/usbdevs.h> 29 30 #include <dev/usb/ucomvar.h> 31 32 #define MOSCOMBUFSZ 256 33 #define MOSCOM_CONFIG_INDEX 0 34 #define MOSCOM_IFACE_NO 0 35 36 #define MOSCOM_READ 0x0d 37 #define MOSCOM_WRITE 0x0e 38 #define MOSCOM_UART_REG 0x0300 39 #define MOSCOM_VEND_REG 0x0000 40 41 #define MOSCOM_TXBUF 0x00 /* Write */ 42 #define MOSCOM_RXBUF 0x00 /* Read */ 43 #define MOSCOM_INT 0x01 44 #define MOSCOM_FIFO 0x02 /* Write */ 45 #define MOSCOM_ISR 0x02 /* Read */ 46 #define MOSCOM_LCR 0x03 47 #define MOSCOM_MCR 0x04 48 #define MOSCOM_LSR 0x05 49 #define MOSCOM_MSR 0x06 50 #define MOSCOM_SCRATCH 0x07 51 #define MOSCOM_DIV_LO 0x08 52 #define MOSCOM_DIV_HI 0x09 53 #define MOSCOM_EFR 0x0a 54 #define MOSCOM_XON1 0x0b 55 #define MOSCOM_XON2 0x0c 56 #define MOSCOM_XOFF1 0x0d 57 #define MOSCOM_XOFF2 0x0e 58 59 #define MOSCOM_BAUDLO 0x00 60 #define MOSCOM_BAUDHI 0x01 61 62 #define MOSCOM_INT_RXEN 0x01 63 #define MOSCOM_INT_TXEN 0x02 64 #define MOSCOM_INT_RSEN 0x04 65 #define MOSCOM_INT_MDMEM 0x08 66 #define MOSCOM_INT_SLEEP 0x10 67 #define MOSCOM_INT_XOFF 0x20 68 #define MOSCOM_INT_RTS 0x40 69 70 #define MOSCOM_FIFO_EN 0x01 71 #define MOSCOM_FIFO_RXCLR 0x02 72 #define MOSCOM_FIFO_TXCLR 0x04 73 #define MOSCOM_FIFO_DMA_BLK 0x08 74 #define MOSCOM_FIFO_TXLVL_MASK 0x30 75 #define MOSCOM_FIFO_TXLVL_8 0x00 76 #define MOSCOM_FIFO_TXLVL_16 0x10 77 #define MOSCOM_FIFO_TXLVL_32 0x20 78 #define MOSCOM_FIFO_TXLVL_56 0x30 79 #define MOSCOM_FIFO_RXLVL_MASK 0xc0 80 #define MOSCOM_FIFO_RXLVL_8 0x00 81 #define MOSCOM_FIFO_RXLVL_16 0x40 82 #define MOSCOM_FIFO_RXLVL_56 0x80 83 #define MOSCOM_FIFO_RXLVL_80 0xc0 84 85 #define MOSCOM_ISR_MDM 0x00 86 #define MOSCOM_ISR_NONE 0x01 87 #define MOSCOM_ISR_TX 0x02 88 #define MOSCOM_ISR_RX 0x04 89 #define MOSCOM_ISR_LINE 0x06 90 #define MOSCOM_ISR_RXTIMEOUT 0x0c 91 #define MOSCOM_ISR_RX_XOFF 0x10 92 #define MOSCOM_ISR_RTSCTS 0x20 93 #define MOSCOM_ISR_FIFOEN 0xc0 94 95 #define MOSCOM_LCR_DBITS(x) (x - 5) 96 #define MOSCOM_LCR_STOP_BITS_1 0x00 97 #define MOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */ 98 #define MOSCOM_LCR_PARITY_NONE 0x00 99 #define MOSCOM_LCR_PARITY_ODD 0x08 100 #define MOSCOM_LCR_PARITY_EVEN 0x18 101 #define MOSCOM_LCR_BREAK 0x40 102 #define MOSCOM_LCR_DIVLATCH_EN 0x80 103 104 #define MOSCOM_MCR_DTR 0x01 105 #define MOSCOM_MCR_RTS 0x02 106 #define MOSCOM_MCR_LOOP 0x04 107 #define MOSCOM_MCR_INTEN 0x08 108 #define MOSCOM_MCR_LOOPBACK 0x10 109 #define MOSCOM_MCR_XONANY 0x20 110 #define MOSCOM_MCR_IRDA_EN 0x40 111 #define MOSCOM_MCR_BAUD_DIV4 0x80 112 113 #define MOSCOM_LSR_RXDATA 0x01 114 #define MOSCOM_LSR_RXOVER 0x02 115 #define MOSCOM_LSR_RXPAR_ERR 0x04 116 #define MOSCOM_LSR_RXFRM_ERR 0x08 117 #define MOSCOM_LSR_RXBREAK 0x10 118 #define MOSCOM_LSR_TXEMPTY 0x20 119 #define MOSCOM_LSR_TXALLEMPTY 0x40 120 #define MOSCOM_LSR_TXFIFO_ERR 0x80 121 122 #define MOSCOM_MSR_CTS_CHG 0x01 123 #define MOSCOM_MSR_DSR_CHG 0x02 124 #define MOSCOM_MSR_RI_CHG 0x04 125 #define MOSCOM_MSR_CD_CHG 0x08 126 #define MOSCOM_MSR_CTS 0x10 127 #define MOSCOM_MSR_RTS 0x20 128 #define MOSCOM_MSR_RI 0x40 129 #define MOSCOM_MSR_CD 0x80 130 131 #define MOSCOM_BAUD_REF 115200 132 133 struct moscom_softc { 134 struct device sc_dev; 135 struct usbd_device *sc_udev; 136 struct usbd_interface *sc_iface; 137 struct device *sc_subdev; 138 139 u_char sc_msr; 140 u_char sc_lsr; 141 u_char sc_lcr; 142 }; 143 144 void moscom_set(void *, int, int, int); 145 int moscom_param(void *, int, struct termios *); 146 int moscom_open(void *, int); 147 int moscom_cmd(struct moscom_softc *, int, int); 148 149 struct ucom_methods moscom_methods = { 150 NULL, 151 moscom_set, 152 moscom_param, 153 NULL, 154 moscom_open, 155 NULL, 156 NULL, 157 NULL, 158 }; 159 160 static const struct usb_devno moscom_devs[] = { 161 { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703 } 162 }; 163 164 int moscom_match(struct device *, void *, void *); 165 void moscom_attach(struct device *, struct device *, void *); 166 int moscom_detach(struct device *, int); 167 168 struct cfdriver moscom_cd = { 169 NULL, "moscom", DV_DULL 170 }; 171 172 const struct cfattach moscom_ca = { 173 sizeof(struct moscom_softc), moscom_match, moscom_attach, moscom_detach 174 }; 175 176 int 177 moscom_match(struct device *parent, void *match, void *aux) 178 { 179 struct usb_attach_arg *uaa = aux; 180 181 if (uaa->iface != NULL) 182 return UMATCH_NONE; 183 184 return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ? 185 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 186 } 187 188 void 189 moscom_attach(struct device *parent, struct device *self, void *aux) 190 { 191 struct moscom_softc *sc = (struct moscom_softc *)self; 192 struct usb_attach_arg *uaa = aux; 193 struct ucom_attach_args uca; 194 usb_interface_descriptor_t *id; 195 usb_endpoint_descriptor_t *ed; 196 usbd_status error; 197 int i; 198 199 bzero(&uca, sizeof(uca)); 200 sc->sc_udev = uaa->device; 201 202 if (usbd_set_config_index(sc->sc_udev, MOSCOM_CONFIG_INDEX, 1) != 0) { 203 printf("%s: could not set configuration no\n", 204 sc->sc_dev.dv_xname); 205 usbd_deactivate(sc->sc_udev); 206 return; 207 } 208 209 /* get the first interface handle */ 210 error = usbd_device2interface_handle(sc->sc_udev, MOSCOM_IFACE_NO, 211 &sc->sc_iface); 212 if (error != 0) { 213 printf("%s: could not get interface handle\n", 214 sc->sc_dev.dv_xname); 215 usbd_deactivate(sc->sc_udev); 216 return; 217 } 218 219 id = usbd_get_interface_descriptor(sc->sc_iface); 220 221 uca.bulkin = uca.bulkout = -1; 222 for (i = 0; i < id->bNumEndpoints; i++) { 223 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 224 if (ed == NULL) { 225 printf("%s: no endpoint descriptor found for %d\n", 226 sc->sc_dev.dv_xname, i); 227 usbd_deactivate(sc->sc_udev); 228 return; 229 } 230 231 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 232 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 233 uca.bulkin = ed->bEndpointAddress; 234 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 235 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 236 uca.bulkout = ed->bEndpointAddress; 237 } 238 239 if (uca.bulkin == -1 || uca.bulkout == -1) { 240 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 241 usbd_deactivate(sc->sc_udev); 242 return; 243 } 244 245 uca.ibufsize = MOSCOMBUFSZ; 246 uca.obufsize = MOSCOMBUFSZ; 247 uca.ibufsizepad = MOSCOMBUFSZ; 248 uca.opkthdrlen = 0; 249 uca.device = sc->sc_udev; 250 uca.iface = sc->sc_iface; 251 uca.methods = &moscom_methods; 252 uca.arg = sc; 253 uca.info = NULL; 254 255 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 256 } 257 258 int 259 moscom_detach(struct device *self, int flags) 260 { 261 struct moscom_softc *sc = (struct moscom_softc *)self; 262 int rv = 0; 263 264 if (sc->sc_subdev != NULL) { 265 rv = config_detach(sc->sc_subdev, flags); 266 sc->sc_subdev = NULL; 267 } 268 269 return (rv); 270 } 271 272 int 273 moscom_open(void *vsc, int portno) 274 { 275 struct moscom_softc *sc = vsc; 276 usb_device_request_t req; 277 278 if (usbd_is_dying(sc->sc_udev)) 279 return (EIO); 280 281 /* Purge FIFOs or odd things happen */ 282 if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0) 283 return (EIO); 284 285 if (moscom_cmd(sc, MOSCOM_FIFO, MOSCOM_FIFO_EN | 286 MOSCOM_FIFO_RXCLR | MOSCOM_FIFO_TXCLR | 287 MOSCOM_FIFO_DMA_BLK | MOSCOM_FIFO_RXLVL_MASK) != 0) 288 return (EIO); 289 290 /* Magic tell device we're ready for data command */ 291 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 292 req.bRequest = MOSCOM_WRITE; 293 USETW(req.wValue, 0x08); 294 USETW(req.wIndex, MOSCOM_INT); 295 USETW(req.wLength, 0); 296 if (usbd_do_request(sc->sc_udev, &req, NULL) != 0) 297 return (EIO); 298 299 return (0); 300 } 301 302 void 303 moscom_set(void *vsc, int portno, int reg, int onoff) 304 { 305 struct moscom_softc *sc = vsc; 306 int val; 307 308 switch (reg) { 309 case UCOM_SET_DTR: 310 val = onoff ? MOSCOM_MCR_DTR : 0; 311 break; 312 case UCOM_SET_RTS: 313 val = onoff ? MOSCOM_MCR_RTS : 0; 314 break; 315 case UCOM_SET_BREAK: 316 val = sc->sc_lcr; 317 if (onoff) 318 val |= MOSCOM_LCR_BREAK; 319 moscom_cmd(sc, MOSCOM_LCR, val); 320 return; 321 default: 322 return; 323 } 324 325 moscom_cmd(sc, MOSCOM_MCR, val); 326 } 327 328 int 329 moscom_param(void *vsc, int portno, struct termios *t) 330 { 331 struct moscom_softc *sc = (struct moscom_softc *)vsc; 332 int data; 333 334 if (t->c_ospeed <= 0 || t->c_ospeed > 115200) 335 return (EINVAL); 336 337 data = MOSCOM_BAUD_REF / t->c_ospeed; 338 339 if (data == 0 || data > 0xffff) 340 return (EINVAL); 341 342 moscom_cmd(sc, MOSCOM_LCR, MOSCOM_LCR_DIVLATCH_EN); 343 moscom_cmd(sc, MOSCOM_BAUDLO, data & 0xFF); 344 moscom_cmd(sc, MOSCOM_BAUDHI, (data >> 8) & 0xFF); 345 346 if (ISSET(t->c_cflag, CSTOPB)) 347 data = MOSCOM_LCR_STOP_BITS_2; 348 else 349 data = MOSCOM_LCR_STOP_BITS_1; 350 if (ISSET(t->c_cflag, PARENB)) { 351 if (ISSET(t->c_cflag, PARODD)) 352 data |= MOSCOM_LCR_PARITY_ODD; 353 else 354 data |= MOSCOM_LCR_PARITY_EVEN; 355 } else 356 data |= MOSCOM_LCR_PARITY_NONE; 357 switch (ISSET(t->c_cflag, CSIZE)) { 358 case CS5: 359 data |= MOSCOM_LCR_DBITS(5); 360 break; 361 case CS6: 362 data |= MOSCOM_LCR_DBITS(6); 363 break; 364 case CS7: 365 data |= MOSCOM_LCR_DBITS(7); 366 break; 367 case CS8: 368 data |= MOSCOM_LCR_DBITS(8); 369 break; 370 } 371 372 sc->sc_lcr = data; 373 moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr); 374 375 #if 0 376 /* XXX flow control */ 377 if (ISSET(t->c_cflag, CRTSCTS)) 378 /* rts/cts flow ctl */ 379 } else if (ISSET(t->c_iflag, IXON|IXOFF)) { 380 /* xon/xoff flow ctl */ 381 } else { 382 /* disable flow ctl */ 383 } 384 #endif 385 386 return (0); 387 } 388 389 int 390 moscom_cmd(struct moscom_softc *sc, int reg, int val) 391 { 392 usb_device_request_t req; 393 usbd_status err; 394 395 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 396 req.bRequest = MOSCOM_WRITE; 397 USETW(req.wValue, val + MOSCOM_UART_REG); 398 USETW(req.wIndex, reg); 399 USETW(req.wLength, 0); 400 err = usbd_do_request(sc->sc_udev, &req, NULL); 401 if (err) 402 return (EIO); 403 else 404 return (0); 405 } 406