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