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