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