1 /* $OpenBSD: moscom.c,v 1.11 2007/10/11 18:33:14 deraadt 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 usbd_device_handle sc_udev; 138 usbd_interface_handle 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 u_char sc_dying; 146 }; 147 148 void moscom_get_status(void *, int, u_char *, u_char *); 149 void moscom_set(void *, int, int, int); 150 int moscom_param(void *, int, struct termios *); 151 int moscom_open(void *, int); 152 int moscom_cmd(struct moscom_softc *, int, int); 153 154 struct ucom_methods moscom_methods = { 155 NULL, 156 moscom_set, 157 moscom_param, 158 NULL, 159 moscom_open, 160 NULL, 161 NULL, 162 NULL, 163 }; 164 165 static const struct usb_devno moscom_devs[] = { 166 { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703 } 167 }; 168 169 int moscom_match(struct device *, void *, void *); 170 void moscom_attach(struct device *, struct device *, void *); 171 int moscom_detach(struct device *, int); 172 int moscom_activate(struct device *, enum devact); 173 174 struct cfdriver moscom_cd = { 175 NULL, "moscom", DV_DULL 176 }; 177 178 const struct cfattach moscom_ca = { 179 sizeof(struct moscom_softc), 180 moscom_match, 181 moscom_attach, 182 moscom_detach, 183 moscom_activate, 184 }; 185 186 int 187 moscom_match(struct device *parent, void *match, void *aux) 188 { 189 struct usb_attach_arg *uaa = aux; 190 191 if (uaa->iface != NULL) 192 return UMATCH_NONE; 193 194 return (usb_lookup(moscom_devs, uaa->vendor, uaa->product) != NULL) ? 195 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 196 } 197 198 void 199 moscom_attach(struct device *parent, struct device *self, void *aux) 200 { 201 struct moscom_softc *sc = (struct moscom_softc *)self; 202 struct usb_attach_arg *uaa = aux; 203 struct ucom_attach_args uca; 204 usb_interface_descriptor_t *id; 205 usb_endpoint_descriptor_t *ed; 206 usbd_status error; 207 int i; 208 209 bzero(&uca, sizeof(uca)); 210 sc->sc_udev = uaa->device; 211 212 if (usbd_set_config_index(sc->sc_udev, MOSCOM_CONFIG_NO, 1) != 0) { 213 printf("%s: could not set configuration no\n", 214 sc->sc_dev.dv_xname); 215 sc->sc_dying = 1; 216 return; 217 } 218 219 /* get the first interface handle */ 220 error = usbd_device2interface_handle(sc->sc_udev, MOSCOM_IFACE_NO, 221 &sc->sc_iface); 222 if (error != 0) { 223 printf("%s: could not get interface handle\n", 224 sc->sc_dev.dv_xname); 225 sc->sc_dying = 1; 226 return; 227 } 228 229 id = usbd_get_interface_descriptor(sc->sc_iface); 230 231 uca.bulkin = uca.bulkout = -1; 232 for (i = 0; i < id->bNumEndpoints; i++) { 233 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 234 if (ed == NULL) { 235 printf("%s: no endpoint descriptor found for %d\n", 236 sc->sc_dev.dv_xname, i); 237 sc->sc_dying = 1; 238 return; 239 } 240 241 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 242 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 243 uca.bulkin = ed->bEndpointAddress; 244 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 245 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 246 uca.bulkout = ed->bEndpointAddress; 247 } 248 249 if (uca.bulkin == -1 || uca.bulkout == -1) { 250 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 251 sc->sc_dying = 1; 252 return; 253 } 254 255 uca.ibufsize = MOSCOMBUFSZ; 256 uca.obufsize = MOSCOMBUFSZ; 257 uca.ibufsizepad = MOSCOMBUFSZ; 258 uca.opkthdrlen = 0; 259 uca.device = sc->sc_udev; 260 uca.iface = sc->sc_iface; 261 uca.methods = &moscom_methods; 262 uca.arg = sc; 263 uca.info = NULL; 264 265 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 266 &sc->sc_dev); 267 268 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 269 } 270 271 int 272 moscom_detach(struct device *self, int flags) 273 { 274 struct moscom_softc *sc = (struct moscom_softc *)self; 275 int rv = 0; 276 277 sc->sc_dying = 1; 278 if (sc->sc_subdev != NULL) { 279 rv = config_detach(sc->sc_subdev, flags); 280 sc->sc_subdev = NULL; 281 } 282 283 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 284 &sc->sc_dev); 285 286 return (rv); 287 } 288 289 int 290 moscom_activate(struct device *self, enum devact act) 291 { 292 struct moscom_softc *sc = (struct moscom_softc *)self; 293 int rv = 0; 294 295 switch (act) { 296 case DVACT_ACTIVATE: 297 break; 298 299 case DVACT_DEACTIVATE: 300 if (sc->sc_subdev != NULL) 301 rv = config_deactivate(sc->sc_subdev); 302 sc->sc_dying = 1; 303 break; 304 } 305 return (rv); 306 } 307 308 int 309 moscom_open(void *vsc, int portno) 310 { 311 struct moscom_softc *sc = vsc; 312 usb_device_request_t req; 313 314 if (sc->sc_dying) 315 return (EIO); 316 317 /* Purge FIFOs or odd things happen */ 318 if (moscom_cmd(sc, MOSCOM_FIFO, 0x00) != 0) 319 return (EIO); 320 321 if (moscom_cmd(sc, MOSCOM_FIFO, MOSCOM_FIFO_EN | 322 MOSCOM_FIFO_RXCLR | MOSCOM_FIFO_TXCLR | 323 MOSCOM_FIFO_DMA_BLK | MOSCOM_FIFO_RXLVL_MASK) != 0) 324 return (EIO); 325 326 /* Magic tell device we're ready for data command */ 327 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 328 req.bRequest = MOSCOM_WRITE; 329 USETW(req.wValue, 0x08); 330 USETW(req.wIndex, MOSCOM_INT); 331 USETW(req.wLength, 0); 332 if (usbd_do_request(sc->sc_udev, &req, NULL) != 0) 333 return (EIO); 334 335 return (0); 336 } 337 338 void 339 moscom_set(void *vsc, int portno, int reg, int onoff) 340 { 341 struct moscom_softc *sc = vsc; 342 int val; 343 344 switch (reg) { 345 case UCOM_SET_DTR: 346 val = onoff ? MOSCOM_MCR_DTR : 0; 347 break; 348 case UCOM_SET_RTS: 349 val = onoff ? MOSCOM_MCR_RTS : 0; 350 break; 351 case UCOM_SET_BREAK: 352 val = sc->sc_lcr; 353 if (onoff) 354 val |= MOSCOM_LCR_BREAK; 355 moscom_cmd(sc, MOSCOM_LCR, val); 356 return; 357 default: 358 return; 359 } 360 361 moscom_cmd(sc, MOSCOM_MCR, val); 362 } 363 364 int 365 moscom_param(void *vsc, int portno, struct termios *t) 366 { 367 struct moscom_softc *sc = (struct moscom_softc *)vsc; 368 int data; 369 370 if (t->c_ospeed <= 0 || t->c_ospeed > 115200) 371 return (EINVAL); 372 373 data = MOSCOM_BAUD_REF / t->c_ospeed; 374 375 if (data == 0 || data > 0xffff) 376 return (EINVAL); 377 378 moscom_cmd(sc, MOSCOM_LCR, MOSCOM_LCR_DIVLATCH_EN); 379 moscom_cmd(sc, MOSCOM_BAUDLO, data & 0xFF); 380 moscom_cmd(sc, MOSCOM_BAUDHI, (data >> 8) & 0xFF); 381 382 if (ISSET(t->c_cflag, CSTOPB)) 383 data = MOSCOM_LCR_STOP_BITS_2; 384 else 385 data = MOSCOM_LCR_STOP_BITS_1; 386 if (ISSET(t->c_cflag, PARENB)) { 387 if (ISSET(t->c_cflag, PARODD)) 388 data |= MOSCOM_LCR_PARITY_ODD; 389 else 390 data |= MOSCOM_LCR_PARITY_EVEN; 391 } else 392 data |= MOSCOM_LCR_PARITY_NONE; 393 switch (ISSET(t->c_cflag, CSIZE)) { 394 case CS5: 395 data |= MOSCOM_LCR_DBITS(5); 396 break; 397 case CS6: 398 data |= MOSCOM_LCR_DBITS(6); 399 break; 400 case CS7: 401 data |= MOSCOM_LCR_DBITS(7); 402 break; 403 case CS8: 404 data |= MOSCOM_LCR_DBITS(8); 405 break; 406 } 407 408 sc->sc_lcr = data; 409 moscom_cmd(sc, MOSCOM_LCR, sc->sc_lcr); 410 411 #if 0 412 /* XXX flow control */ 413 if (ISSET(t->c_cflag, CRTSCTS)) 414 /* rts/cts flow ctl */ 415 } else if (ISSET(t->c_iflag, IXON|IXOFF)) { 416 /* xon/xoff flow ctl */ 417 } else { 418 /* disable flow ctl */ 419 } 420 #endif 421 422 return (0); 423 } 424 425 void 426 moscom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 427 { 428 struct moscom_softc *sc = vsc; 429 430 if (msr != NULL) 431 *msr = sc->sc_msr; 432 if (lsr != NULL) 433 *lsr = sc->sc_lsr; 434 } 435 436 int 437 moscom_cmd(struct moscom_softc *sc, int reg, int val) 438 { 439 usb_device_request_t req; 440 usbd_status err; 441 442 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 443 req.bRequest = MOSCOM_WRITE; 444 USETW(req.wValue, val + MOSCOM_UART_REG); 445 USETW(req.wIndex, reg); 446 USETW(req.wLength, 0); 447 err = usbd_do_request(sc->sc_udev, &req, NULL); 448 if (err) 449 return (EIO); 450 else 451 return (0); 452 } 453