1 /* $OpenBSD: uark.c,v 1.20 2014/07/12 20:26: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 #ifdef UARK_DEBUG 35 #define DPRINTFN(n, x) do { if (uarkdebug > (n)) printf x; } while (0) 36 int uarkebug = 0; 37 #else 38 #define DPRINTFN(n, x) 39 #endif 40 #define DPRINTF(x) DPRINTFN(0, x) 41 42 #define UARKBUFSZ 256 43 #define UARK_CONFIG_NO 0 44 #define UARK_IFACE_NO 0 45 46 #define UARK_SET_DATA_BITS(x) (x - 5) 47 48 #define UARK_PARITY_NONE 0x00 49 #define UARK_PARITY_ODD 0x08 50 #define UARK_PARITY_EVEN 0x18 51 52 #define UARK_STOP_BITS_1 0x00 53 #define UARK_STOP_BITS_2 0x04 54 55 #define UARK_BAUD_REF 3000000 56 57 #define UARK_WRITE 0x40 58 #define UARK_READ 0xc0 59 60 #define UARK_REQUEST 0xfe 61 62 struct uark_softc { 63 struct device sc_dev; 64 struct usbd_device *sc_udev; 65 struct usbd_interface *sc_iface; 66 struct device *sc_subdev; 67 68 u_char sc_msr; 69 u_char sc_lsr; 70 }; 71 72 void uark_get_status(void *, int portno, u_char *lsr, u_char *msr); 73 void uark_set(void *, int, int, int); 74 int uark_param(void *, int, struct termios *); 75 void uark_break(void *, int, int); 76 int uark_cmd(struct uark_softc *, uint16_t, uint16_t); 77 78 struct ucom_methods uark_methods = { 79 uark_get_status, 80 uark_set, 81 uark_param, 82 NULL, 83 NULL, 84 NULL, 85 NULL, 86 NULL, 87 }; 88 89 static const struct usb_devno uark_devs[] = { 90 { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 } 91 }; 92 93 int uark_match(struct device *, void *, void *); 94 void uark_attach(struct device *, struct device *, void *); 95 int uark_detach(struct device *, int); 96 97 struct cfdriver uark_cd = { 98 NULL, "uark", DV_DULL 99 }; 100 101 const struct cfattach uark_ca = { 102 sizeof(struct uark_softc), uark_match, uark_attach, uark_detach 103 }; 104 105 int 106 uark_match(struct device *parent, void *match, void *aux) 107 { 108 struct usb_attach_arg *uaa = aux; 109 110 if (uaa->iface != NULL) 111 return UMATCH_NONE; 112 113 return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ? 114 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 115 } 116 117 void 118 uark_attach(struct device *parent, struct device *self, void *aux) 119 { 120 struct uark_softc *sc = (struct uark_softc *)self; 121 struct usb_attach_arg *uaa = aux; 122 struct ucom_attach_args uca; 123 usb_interface_descriptor_t *id; 124 usb_endpoint_descriptor_t *ed; 125 usbd_status error; 126 int i; 127 128 bzero(&uca, sizeof(uca)); 129 sc->sc_udev = uaa->device; 130 131 if (usbd_set_config_index(sc->sc_udev, UARK_CONFIG_NO, 1) != 0) { 132 printf("%s: could not set configuration no\n", 133 sc->sc_dev.dv_xname); 134 usbd_deactivate(sc->sc_udev); 135 return; 136 } 137 138 /* get the first interface handle */ 139 error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO, 140 &sc->sc_iface); 141 if (error != 0) { 142 printf("%s: could not get interface handle\n", 143 sc->sc_dev.dv_xname); 144 usbd_deactivate(sc->sc_udev); 145 return; 146 } 147 148 id = usbd_get_interface_descriptor(sc->sc_iface); 149 150 uca.bulkin = uca.bulkout = -1; 151 for (i = 0; i < id->bNumEndpoints; i++) { 152 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 153 if (ed == NULL) { 154 printf("%s: no endpoint descriptor found for %d\n", 155 sc->sc_dev.dv_xname, i); 156 usbd_deactivate(sc->sc_udev); 157 return; 158 } 159 160 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 161 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 162 uca.bulkin = ed->bEndpointAddress; 163 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 164 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 165 uca.bulkout = ed->bEndpointAddress; 166 } 167 168 if (uca.bulkin == -1 || uca.bulkout == -1) { 169 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 170 usbd_deactivate(sc->sc_udev); 171 return; 172 } 173 174 uca.ibufsize = UARKBUFSZ; 175 uca.obufsize = UARKBUFSZ; 176 uca.ibufsizepad = UARKBUFSZ; 177 uca.opkthdrlen = 0; 178 uca.device = sc->sc_udev; 179 uca.iface = sc->sc_iface; 180 uca.methods = &uark_methods; 181 uca.arg = sc; 182 uca.info = NULL; 183 184 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 185 } 186 187 int 188 uark_detach(struct device *self, int flags) 189 { 190 struct uark_softc *sc = (struct uark_softc *)self; 191 int rv = 0; 192 193 if (sc->sc_subdev != NULL) { 194 rv = config_detach(sc->sc_subdev, flags); 195 sc->sc_subdev = NULL; 196 } 197 198 return (rv); 199 } 200 201 void 202 uark_set(void *vsc, int portno, int reg, int onoff) 203 { 204 struct uark_softc *sc = vsc; 205 206 switch (reg) { 207 case UCOM_SET_BREAK: 208 uark_break(sc, portno, onoff); 209 return; 210 case UCOM_SET_DTR: 211 case UCOM_SET_RTS: 212 default: 213 return; 214 } 215 } 216 217 int 218 uark_param(void *vsc, int portno, struct termios *t) 219 { 220 struct uark_softc *sc = (struct uark_softc *)vsc; 221 int data; 222 223 switch (t->c_ospeed) { 224 case 300: 225 case 600: 226 case 1200: 227 case 1800: 228 case 2400: 229 case 4800: 230 case 9600: 231 case 19200: 232 case 38400: 233 case 57600: 234 case 115200: 235 uark_cmd(sc, 3, 0x83); 236 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF); 237 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8); 238 uark_cmd(sc, 3, 0x03); 239 break; 240 default: 241 return (EINVAL); 242 } 243 244 if (ISSET(t->c_cflag, CSTOPB)) 245 data = UARK_STOP_BITS_2; 246 else 247 data = UARK_STOP_BITS_1; 248 249 if (ISSET(t->c_cflag, PARENB)) { 250 if (ISSET(t->c_cflag, PARODD)) 251 data |= UARK_PARITY_ODD; 252 else 253 data |= UARK_PARITY_EVEN; 254 } else 255 data |= UARK_PARITY_NONE; 256 257 switch (ISSET(t->c_cflag, CSIZE)) { 258 case CS5: 259 data |= UARK_SET_DATA_BITS(5); 260 break; 261 case CS6: 262 data |= UARK_SET_DATA_BITS(6); 263 break; 264 case CS7: 265 data |= UARK_SET_DATA_BITS(7); 266 break; 267 case CS8: 268 data |= UARK_SET_DATA_BITS(8); 269 break; 270 } 271 272 uark_cmd(sc, 3, 0x00); 273 uark_cmd(sc, 3, data); 274 275 #if 0 276 /* XXX flow control */ 277 if (ISSET(t->c_cflag, CRTSCTS)) 278 /* rts/cts flow ctl */ 279 } else if (ISSET(t->c_iflag, IXON|IXOFF)) { 280 /* xon/xoff flow ctl */ 281 } else { 282 /* disable flow ctl */ 283 } 284 #endif 285 286 return (0); 287 } 288 289 void 290 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 291 { 292 struct uark_softc *sc = vsc; 293 294 if (msr != NULL) 295 *msr = sc->sc_msr; 296 if (lsr != NULL) 297 *lsr = sc->sc_lsr; 298 } 299 300 void 301 uark_break(void *vsc, int portno, int onoff) 302 { 303 #ifdef UARK_DEBUG 304 struct uark_softc *sc = vsc; 305 306 printf("%s: break %s!\n", sc->sc_dev.dv_xname, 307 onoff ? "on" : "off"); 308 309 if (onoff) 310 /* break on */ 311 uark_cmd(sc, 4, 0x01); 312 else 313 uark_cmd(sc, 4, 0x00); 314 #endif 315 } 316 317 int 318 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value) 319 { 320 usb_device_request_t req; 321 usbd_status err; 322 323 req.bmRequestType = UARK_WRITE; 324 req.bRequest = UARK_REQUEST; 325 USETW(req.wValue, value); 326 USETW(req.wIndex, index); 327 USETW(req.wLength, 0); 328 err = usbd_do_request(sc->sc_udev, &req, NULL); 329 330 if (err) 331 return (EIO); 332 333 return (0); 334 } 335