Line data Source code
1 : /* $OpenBSD: ubsa.c,v 1.66 2018/04/27 08:08:06 guenther Exp $ */
2 : /* $NetBSD: ubsa.c,v 1.5 2002/11/25 00:51:33 fvdl Exp $ */
3 : /*-
4 : * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
5 : * All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * 2. Redistributions in binary form must reproduce the above copyright
13 : * notice, this list of conditions and the following disclaimer in the
14 : * documentation and/or other materials provided with the distribution.
15 : *
16 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 : * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 : * SUCH DAMAGE.
27 : */
28 : /*
29 : * Copyright (c) 2001 The NetBSD Foundation, Inc.
30 : * All rights reserved.
31 : *
32 : * This code is derived from software contributed to The NetBSD Foundation
33 : * by Ichiro FUKUHARA (ichiro@ichiro.org).
34 : *
35 : * Redistribution and use in source and binary forms, with or without
36 : * modification, are permitted provided that the following conditions
37 : * are met:
38 : * 1. Redistributions of source code must retain the above copyright
39 : * notice, this list of conditions and the following disclaimer.
40 : * 2. Redistributions in binary form must reproduce the above copyright
41 : * notice, this list of conditions and the following disclaimer in the
42 : * documentation and/or other materials provided with the distribution.
43 : *
44 : * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
45 : * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 : * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
48 : * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 : * POSSIBILITY OF SUCH DAMAGE.
55 : */
56 :
57 : #include <sys/param.h>
58 : #include <sys/systm.h>
59 : #include <sys/kernel.h>
60 : #include <sys/malloc.h>
61 : #include <sys/device.h>
62 : #include <sys/ioccom.h>
63 : #include <sys/fcntl.h>
64 : #include <sys/conf.h>
65 : #include <sys/tty.h>
66 : #include <sys/selinfo.h>
67 : #include <sys/poll.h>
68 :
69 : #include <dev/usb/usb.h>
70 : #include <dev/usb/usbcdc.h>
71 :
72 : #include <dev/usb/usbdi.h>
73 : #include <dev/usb/usbdi_util.h>
74 : #include <dev/usb/usbdevs.h>
75 :
76 : #include <dev/usb/ucomvar.h>
77 :
78 : #ifdef UBSA_DEBUG
79 : int ubsadebug = 0;
80 :
81 : #define DPRINTFN(n, x) do { if (ubsadebug > (n)) printf x; } while (0)
82 : #else
83 : #define DPRINTFN(n, x)
84 : #endif
85 : #define DPRINTF(x) DPRINTFN(0, x)
86 :
87 : #define UBSA_MODVER 1 /* module version */
88 :
89 : #define UBSA_CONFIG_INDEX 1
90 : #define UBSA_IFACE_INDEX 0
91 :
92 : #define UBSA_INTR_INTERVAL 100 /* ms */
93 :
94 : #define UBSA_SET_BAUDRATE 0x00
95 : #define UBSA_SET_STOP_BITS 0x01
96 : #define UBSA_SET_DATA_BITS 0x02
97 : #define UBSA_SET_PARITY 0x03
98 : #define UBSA_SET_DTR 0x0A
99 : #define UBSA_SET_RTS 0x0B
100 : #define UBSA_SET_BREAK 0x0C
101 : #define UBSA_SET_FLOW_CTRL 0x10
102 :
103 : #define UBSA_PARITY_NONE 0x00
104 : #define UBSA_PARITY_EVEN 0x01
105 : #define UBSA_PARITY_ODD 0x02
106 : #define UBSA_PARITY_MARK 0x03
107 : #define UBSA_PARITY_SPACE 0x04
108 :
109 : #define UBSA_FLOW_NONE 0x0000
110 : #define UBSA_FLOW_OCTS 0x0001
111 : #define UBSA_FLOW_ODSR 0x0002
112 : #define UBSA_FLOW_IDSR 0x0004
113 : #define UBSA_FLOW_IDTR 0x0008
114 : #define UBSA_FLOW_IRTS 0x0010
115 : #define UBSA_FLOW_ORTS 0x0020
116 : #define UBSA_FLOW_UNKNOWN 0x0040
117 : #define UBSA_FLOW_OXON 0x0080
118 : #define UBSA_FLOW_IXON 0x0100
119 :
120 : /* line status register */
121 : #define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */
122 : #define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */
123 : #define UBSA_LSR_BI 0x10 /* Break detected */
124 : #define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */
125 : #define UBSA_LSR_PE 0x04 /* Parity error */
126 : #define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */
127 : #define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
128 : #define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */
129 :
130 : /* modem status register */
131 : /* All deltas are from the last read of the MSR. */
132 : #define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */
133 : #define UBSA_MSR_RI 0x40 /* Current Ring Indicator */
134 : #define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */
135 : #define UBSA_MSR_CTS 0x10 /* Current Clear to Send */
136 : #define UBSA_MSR_DDCD 0x08 /* DCD has changed state */
137 : #define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */
138 : #define UBSA_MSR_DDSR 0x02 /* DSR has changed state */
139 : #define UBSA_MSR_DCTS 0x01 /* CTS has changed state */
140 :
141 : struct ubsa_softc {
142 : struct device sc_dev; /* base device */
143 : struct usbd_device *sc_udev; /* USB device */
144 : struct usbd_interface *sc_iface; /* interface */
145 :
146 : int sc_iface_number; /* interface number */
147 :
148 : int sc_intr_number; /* interrupt number */
149 : struct usbd_pipe *sc_intr_pipe; /* interrupt pipe */
150 : u_char *sc_intr_buf; /* interrupt buffer */
151 : int sc_isize;
152 :
153 : u_char sc_dtr; /* current DTR state */
154 : u_char sc_rts; /* current RTS state */
155 :
156 : u_char sc_lsr; /* Local status register */
157 : u_char sc_msr; /* ubsa status register */
158 :
159 : struct device *sc_subdev; /* ucom device */
160 :
161 : };
162 :
163 : void ubsa_intr(struct usbd_xfer *, void *, usbd_status);
164 :
165 : void ubsa_get_status(void *, int, u_char *, u_char *);
166 : void ubsa_set(void *, int, int, int);
167 : int ubsa_param(void *, int, struct termios *);
168 : int ubsa_open(void *, int);
169 : void ubsa_close(void *, int);
170 :
171 : void ubsa_break(struct ubsa_softc *sc, int onoff);
172 : int ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
173 : void ubsa_dtr(struct ubsa_softc *, int);
174 : void ubsa_rts(struct ubsa_softc *, int);
175 : void ubsa_baudrate(struct ubsa_softc *, speed_t);
176 : void ubsa_parity(struct ubsa_softc *, tcflag_t);
177 : void ubsa_databits(struct ubsa_softc *, tcflag_t);
178 : void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
179 : void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
180 :
181 : struct ucom_methods ubsa_methods = {
182 : ubsa_get_status,
183 : ubsa_set,
184 : ubsa_param,
185 : NULL,
186 : ubsa_open,
187 : ubsa_close,
188 : NULL,
189 : NULL
190 : };
191 :
192 : const struct usb_devno ubsa_devs[] = {
193 : /* Axesstel MV100H */
194 : { USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM },
195 : /* BELKIN F5U103 */
196 : { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
197 : /* BELKIN F5U120 */
198 : { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
199 : /* GoHubs GO-COM232 , Belkin F5U003 */
200 : { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
201 : /* GoHubs GO-COM232 */
202 : { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
203 : /* Peracom */
204 : { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
205 : /* ZTE Inc. CMDMA MSM modem */
206 : { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_CDMA_MSM },
207 : /* ZTE Inc. AC8700 */
208 : { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 },
209 : };
210 :
211 : int ubsa_match(struct device *, void *, void *);
212 : void ubsa_attach(struct device *, struct device *, void *);
213 : int ubsa_detach(struct device *, int);
214 :
215 : struct cfdriver ubsa_cd = {
216 : NULL, "ubsa", DV_DULL
217 : };
218 :
219 : const struct cfattach ubsa_ca = {
220 : sizeof(struct ubsa_softc), ubsa_match, ubsa_attach, ubsa_detach
221 : };
222 :
223 : int
224 0 : ubsa_match(struct device *parent, void *match, void *aux)
225 : {
226 0 : struct usb_attach_arg *uaa = aux;
227 :
228 0 : if (uaa->iface != NULL)
229 0 : return (UMATCH_NONE);
230 :
231 0 : return (usb_lookup(ubsa_devs, uaa->vendor, uaa->product) != NULL ?
232 : UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
233 0 : }
234 :
235 : void
236 0 : ubsa_attach(struct device *parent, struct device *self, void *aux)
237 : {
238 0 : struct ubsa_softc *sc = (struct ubsa_softc *)self;
239 0 : struct usb_attach_arg *uaa = aux;
240 0 : struct usbd_device *dev = uaa->device;
241 : usb_config_descriptor_t *cdesc;
242 : usb_interface_descriptor_t *id;
243 : usb_endpoint_descriptor_t *ed;
244 0 : const char *devname = sc->sc_dev.dv_xname;
245 : usbd_status err;
246 0 : struct ucom_attach_args uca;
247 : int i;
248 :
249 0 : sc->sc_udev = dev;
250 :
251 : /*
252 : * initialize rts, dtr variables to something
253 : * different from boolean 0, 1
254 : */
255 0 : sc->sc_dtr = -1;
256 0 : sc->sc_rts = -1;
257 :
258 : DPRINTF(("ubsa attach: sc = %p\n", sc));
259 :
260 : /* initialize endpoints */
261 0 : uca.bulkin = uca.bulkout = -1;
262 0 : sc->sc_intr_number = -1;
263 0 : sc->sc_intr_pipe = NULL;
264 :
265 : /* Move the device into the configured state. */
266 0 : err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
267 0 : if (err) {
268 0 : printf("%s: failed to set configuration: %s\n",
269 0 : devname, usbd_errstr(err));
270 0 : usbd_deactivate(sc->sc_udev);
271 0 : goto error;
272 : }
273 :
274 : /* get the config descriptor */
275 0 : cdesc = usbd_get_config_descriptor(sc->sc_udev);
276 :
277 0 : if (cdesc == NULL) {
278 0 : printf("%s: failed to get configuration descriptor\n",
279 : devname);
280 0 : usbd_deactivate(sc->sc_udev);
281 0 : goto error;
282 : }
283 :
284 : /* get the first interface */
285 0 : err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
286 0 : &sc->sc_iface);
287 0 : if (err) {
288 0 : printf("%s: failed to get interface: %s\n",
289 0 : devname, usbd_errstr(err));
290 0 : usbd_deactivate(sc->sc_udev);
291 0 : goto error;
292 : }
293 :
294 : /* Find the endpoints */
295 :
296 0 : id = usbd_get_interface_descriptor(sc->sc_iface);
297 0 : sc->sc_iface_number = id->bInterfaceNumber;
298 :
299 0 : for (i = 0; i < id->bNumEndpoints; i++) {
300 0 : ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
301 0 : if (ed == NULL) {
302 0 : printf("%s: no endpoint descriptor for %d\n",
303 : sc->sc_dev.dv_xname, i);
304 0 : usbd_deactivate(sc->sc_udev);
305 0 : goto error;
306 : }
307 :
308 0 : if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
309 0 : UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
310 0 : sc->sc_intr_number = ed->bEndpointAddress;
311 0 : sc->sc_isize = UGETW(ed->wMaxPacketSize);
312 0 : } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
313 0 : UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
314 0 : uca.bulkin = ed->bEndpointAddress;
315 0 : uca.ibufsize = UGETW(ed->wMaxPacketSize);
316 0 : } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
317 0 : UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
318 0 : uca.bulkout = ed->bEndpointAddress;
319 0 : uca.obufsize = UGETW(ed->wMaxPacketSize);
320 0 : }
321 : }
322 :
323 0 : if (sc->sc_intr_number == -1) {
324 0 : printf("%s: Could not find interrupt in\n", devname);
325 0 : usbd_deactivate(sc->sc_udev);
326 0 : goto error;
327 : }
328 :
329 0 : if (uca.bulkin == -1) {
330 0 : printf("%s: Could not find data bulk in\n", devname);
331 0 : usbd_deactivate(sc->sc_udev);
332 0 : goto error;
333 : }
334 :
335 0 : if (uca.bulkout == -1) {
336 0 : printf("%s: Could not find data bulk out\n", devname);
337 0 : usbd_deactivate(sc->sc_udev);
338 0 : goto error;
339 : }
340 :
341 0 : uca.portno = UCOM_UNK_PORTNO;
342 : /* bulkin, bulkout set above */
343 0 : uca.ibufsizepad = uca.ibufsize;
344 0 : uca.opkthdrlen = 0;
345 0 : uca.device = dev;
346 0 : uca.iface = sc->sc_iface;
347 0 : uca.methods = &ubsa_methods;
348 0 : uca.arg = sc;
349 0 : uca.info = NULL;
350 :
351 : DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
352 : uca.bulkin, uca.bulkout, sc->sc_intr_number));
353 :
354 0 : sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
355 :
356 : error:
357 : return;
358 0 : }
359 :
360 : int
361 0 : ubsa_detach(struct device *self, int flags)
362 : {
363 0 : struct ubsa_softc *sc = (struct ubsa_softc *)self;
364 : int rv = 0;
365 :
366 :
367 : DPRINTF(("ubsa_detach: sc = %p\n", sc));
368 :
369 0 : if (sc->sc_intr_pipe != NULL) {
370 0 : usbd_abort_pipe(sc->sc_intr_pipe);
371 0 : usbd_close_pipe(sc->sc_intr_pipe);
372 0 : free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
373 0 : sc->sc_intr_pipe = NULL;
374 0 : }
375 :
376 0 : if (sc->sc_subdev != NULL) {
377 0 : rv = config_detach(sc->sc_subdev, flags);
378 0 : sc->sc_subdev = NULL;
379 0 : }
380 :
381 0 : return (rv);
382 : }
383 :
384 : int
385 0 : ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
386 : {
387 0 : usb_device_request_t req;
388 : usbd_status err;
389 :
390 0 : req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
391 0 : req.bRequest = request;
392 0 : USETW(req.wValue, value);
393 0 : USETW(req.wIndex, sc->sc_iface_number);
394 0 : USETW(req.wLength, 0);
395 :
396 0 : err = usbd_do_request(sc->sc_udev, &req, 0);
397 0 : if (err && err != USBD_STALLED)
398 0 : printf("%s: ubsa_request: %s\n",
399 0 : sc->sc_dev.dv_xname, usbd_errstr(err));
400 0 : return (err);
401 0 : }
402 :
403 : void
404 0 : ubsa_dtr(struct ubsa_softc *sc, int onoff)
405 : {
406 :
407 : DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
408 :
409 0 : if (sc->sc_dtr == onoff)
410 : return;
411 0 : sc->sc_dtr = onoff;
412 :
413 0 : ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
414 0 : }
415 :
416 : void
417 0 : ubsa_rts(struct ubsa_softc *sc, int onoff)
418 : {
419 :
420 : DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
421 :
422 0 : if (sc->sc_rts == onoff)
423 : return;
424 0 : sc->sc_rts = onoff;
425 :
426 0 : ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
427 0 : }
428 :
429 : void
430 0 : ubsa_break(struct ubsa_softc *sc, int onoff)
431 : {
432 :
433 : DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
434 :
435 0 : ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
436 0 : }
437 :
438 : void
439 0 : ubsa_set(void *addr, int portno, int reg, int onoff)
440 : {
441 : struct ubsa_softc *sc;
442 :
443 0 : sc = addr;
444 0 : switch (reg) {
445 : case UCOM_SET_DTR:
446 0 : ubsa_dtr(sc, onoff);
447 0 : break;
448 : case UCOM_SET_RTS:
449 0 : ubsa_rts(sc, onoff);
450 0 : break;
451 : case UCOM_SET_BREAK:
452 0 : ubsa_break(sc, onoff);
453 0 : break;
454 : default:
455 : break;
456 : }
457 0 : }
458 :
459 : void
460 0 : ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
461 : {
462 : u_int16_t value = 0;
463 :
464 : DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
465 :
466 0 : switch(speed) {
467 : case B0:
468 : break;
469 : case B300:
470 : case B600:
471 : case B1200:
472 : case B2400:
473 : case B4800:
474 : case B9600:
475 : case B19200:
476 : case B38400:
477 : case B57600:
478 : case B115200:
479 : case B230400:
480 0 : value = B230400 / speed;
481 0 : break;
482 : default:
483 : DPRINTF(("%s: ubsa_param: unsupported baudrate, "
484 : "forcing default of 9600\n",
485 : sc->sc_dev.dv_xname));
486 : value = B230400 / B9600;
487 0 : break;
488 : };
489 :
490 0 : if (speed == B0) {
491 0 : ubsa_flow(sc, 0, 0);
492 0 : ubsa_dtr(sc, 0);
493 0 : ubsa_rts(sc, 0);
494 0 : } else
495 0 : ubsa_request(sc, UBSA_SET_BAUDRATE, value);
496 0 : }
497 :
498 : void
499 0 : ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
500 : {
501 : int value;
502 :
503 : DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
504 :
505 0 : if (cflag & PARENB)
506 0 : value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
507 : else
508 : value = UBSA_PARITY_NONE;
509 :
510 0 : ubsa_request(sc, UBSA_SET_PARITY, value);
511 0 : }
512 :
513 : void
514 0 : ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
515 : {
516 : int value;
517 :
518 : DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
519 :
520 0 : switch (cflag & CSIZE) {
521 0 : case CS5: value = 0; break;
522 0 : case CS6: value = 1; break;
523 0 : case CS7: value = 2; break;
524 0 : case CS8: value = 3; break;
525 : default:
526 : DPRINTF(("%s: ubsa_param: unsupported databits requested, "
527 : "forcing default of 8\n",
528 : sc->sc_dev.dv_xname));
529 : value = 3;
530 : }
531 :
532 0 : ubsa_request(sc, UBSA_SET_DATA_BITS, value);
533 0 : }
534 :
535 : void
536 0 : ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
537 : {
538 : int value;
539 :
540 : DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
541 :
542 0 : value = (cflag & CSTOPB) ? 1 : 0;
543 :
544 0 : ubsa_request(sc, UBSA_SET_STOP_BITS, value);
545 0 : }
546 :
547 : void
548 0 : ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
549 : {
550 : int value;
551 :
552 : DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
553 :
554 : value = 0;
555 0 : if (cflag & CRTSCTS)
556 0 : value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
557 0 : if (iflag & (IXON|IXOFF))
558 0 : value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
559 :
560 0 : ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
561 0 : }
562 :
563 : int
564 0 : ubsa_param(void *addr, int portno, struct termios *ti)
565 : {
566 0 : struct ubsa_softc *sc = addr;
567 :
568 : DPRINTF(("ubsa_param: sc = %p\n", sc));
569 :
570 0 : ubsa_baudrate(sc, ti->c_ospeed);
571 0 : ubsa_parity(sc, ti->c_cflag);
572 0 : ubsa_databits(sc, ti->c_cflag);
573 0 : ubsa_stopbits(sc, ti->c_cflag);
574 0 : ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
575 :
576 0 : return (0);
577 : }
578 :
579 : int
580 0 : ubsa_open(void *addr, int portno)
581 : {
582 0 : struct ubsa_softc *sc = addr;
583 : int err;
584 :
585 0 : if (usbd_is_dying(sc->sc_udev))
586 0 : return (ENXIO);
587 :
588 : DPRINTF(("ubsa_open: sc = %p\n", sc));
589 :
590 0 : if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
591 0 : sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
592 0 : err = usbd_open_pipe_intr(sc->sc_iface,
593 0 : sc->sc_intr_number,
594 : USBD_SHORT_XFER_OK,
595 : &sc->sc_intr_pipe,
596 : sc,
597 : sc->sc_intr_buf,
598 0 : sc->sc_isize,
599 : ubsa_intr,
600 : UBSA_INTR_INTERVAL);
601 0 : if (err) {
602 0 : printf("%s: cannot open interrupt pipe (addr %d)\n",
603 0 : sc->sc_dev.dv_xname,
604 0 : sc->sc_intr_number);
605 0 : return (EIO);
606 : }
607 : }
608 :
609 0 : return (0);
610 0 : }
611 :
612 : void
613 0 : ubsa_close(void *addr, int portno)
614 : {
615 0 : struct ubsa_softc *sc = addr;
616 : int err;
617 :
618 0 : if (usbd_is_dying(sc->sc_udev))
619 0 : return;
620 :
621 : DPRINTF(("ubsa_close: close\n"));
622 :
623 0 : if (sc->sc_intr_pipe != NULL) {
624 0 : usbd_abort_pipe(sc->sc_intr_pipe);
625 0 : err = usbd_close_pipe(sc->sc_intr_pipe);
626 0 : if (err)
627 0 : printf("%s: close interrupt pipe failed: %s\n",
628 0 : sc->sc_dev.dv_xname,
629 0 : usbd_errstr(err));
630 0 : free(sc->sc_intr_buf, M_USBDEV, sc->sc_isize);
631 0 : sc->sc_intr_pipe = NULL;
632 0 : }
633 0 : }
634 :
635 : void
636 0 : ubsa_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
637 : {
638 0 : struct ubsa_softc *sc = priv;
639 : u_char *buf;
640 : struct usb_cdc_notification *cdcbuf;
641 :
642 0 : buf = sc->sc_intr_buf;
643 0 : cdcbuf = (struct usb_cdc_notification *)sc->sc_intr_buf;
644 0 : if (usbd_is_dying(sc->sc_udev))
645 0 : return;
646 :
647 0 : if (status != USBD_NORMAL_COMPLETION) {
648 0 : if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
649 0 : return;
650 :
651 : DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
652 : sc->sc_dev.dv_xname, usbd_errstr(status)));
653 0 : usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
654 0 : return;
655 : }
656 :
657 : #if 1 /* test */
658 0 : if (cdcbuf->bmRequestType == UCDC_NOTIFICATION) {
659 0 : printf("%s: this device is using CDC notify message in"
660 : " intr pipe.\n"
661 : "Please send your dmesg to <bugs@openbsd.org>, thanks.\n",
662 0 : sc->sc_dev.dv_xname);
663 0 : printf("%s: intr buffer 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
664 : sc->sc_dev.dv_xname,
665 0 : buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
666 :
667 : /* check the buffer data */
668 0 : if (cdcbuf->bNotification == UCDC_N_SERIAL_STATE)
669 0 : printf("%s:notify serial state, len=%d, data=0x%02x\n",
670 : sc->sc_dev.dv_xname,
671 0 : UGETW(cdcbuf->wLength), cdcbuf->data[0]);
672 : }
673 : #endif
674 :
675 : /* incidentally, Belkin adapter status bits match UART 16550 bits */
676 0 : sc->sc_lsr = buf[2];
677 0 : sc->sc_msr = buf[3];
678 :
679 : DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
680 : sc->sc_dev.dv_xname, sc->sc_lsr, sc->sc_msr));
681 :
682 0 : ucom_status_change((struct ucom_softc *)sc->sc_subdev);
683 0 : }
684 :
685 : void
686 0 : ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
687 : {
688 0 : struct ubsa_softc *sc = addr;
689 :
690 : DPRINTF(("ubsa_get_status\n"));
691 :
692 0 : if (lsr != NULL)
693 0 : *lsr = sc->sc_lsr;
694 0 : if (msr != NULL)
695 0 : *msr = sc->sc_msr;
696 0 : }
|