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