Kill usage of USB_VENDOR_FOO and USB_PRODUCT_BAR defines mostly using two
[dragonfly.git] / sys / dev / usbmisc / uvscom / uvscom.c
1 /*-
2  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.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  *
26  * $NetBSD: usb/uvscom.c,v 1.1 2002/03/19 15:08:42 augustss Exp $
27  * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.19 2003/11/16 12:26:10 akiyama Exp $
28  * $DragonFly: src/sys/dev/usbmisc/uvscom/uvscom.c,v 1.17 2007/11/05 13:32:28 hasso Exp $
29  */
30
31 /*
32  * uvscom: SUNTAC Slipper U VS-10U driver.
33  * Slipper U is a PC card to USB converter for data communication card
34  * adapter.  It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
35  * P-in m@ater and various data communication card adapters.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/fcntl.h>
43 #include <sys/conf.h>
44 #include <sys/tty.h>
45 #include <sys/file.h>
46 #include <sys/bus.h>
47 #include <sys/ioccom.h>
48 #include <sys/select.h>
49 #include <sys/proc.h>
50 #include <sys/poll.h>
51 #include <sys/sysctl.h>
52
53 #include <bus/usb/usb.h>
54 #include <bus/usb/usbcdc.h>
55
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
58 #include <bus/usb/usbdevs.h>
59 #include <bus/usb/usb_quirks.h>
60
61 #include "../ucom/ucomvar.h"
62
63 #ifdef USB_DEBUG
64 static int      uvscomdebug = 0;
65 SYSCTL_NODE(_hw_usb, OID_AUTO, uvscom, CTLFLAG_RW, 0, "USB uvscom");
66 SYSCTL_INT(_hw_usb_uvscom, OID_AUTO, debug, CTLFLAG_RW,
67            &uvscomdebug, 0, "uvscom debug level");
68
69 #define DPRINTFN(n, x) do { \
70                                 if (uvscomdebug > (n)) \
71                                         kprintf x; \
72                         } while (0)
73 #else
74 #define DPRINTFN(n, x)
75 #endif
76 #define DPRINTF(x) DPRINTFN(0, x)
77
78 #define UVSCOM_MODVER           1       /* module version */
79
80 #define UVSCOM_CONFIG_INDEX     0
81 #define UVSCOM_IFACE_INDEX      0
82
83 #define UVSCOM_INTR_INTERVAL    100     /* mS */
84
85 #define UVSCOM_UNIT_WAIT        5
86
87 /* Request */
88 #define UVSCOM_SET_SPEED        0x10
89 #define UVSCOM_LINE_CTL         0x11
90 #define UVSCOM_SET_PARAM        0x12
91 #define UVSCOM_READ_STATUS      0xd0
92 #define UVSCOM_SHUTDOWN         0xe0
93
94 /* UVSCOM_SET_SPEED parameters */
95 #define UVSCOM_SPEED_150BPS     0x00
96 #define UVSCOM_SPEED_300BPS     0x01
97 #define UVSCOM_SPEED_600BPS     0x02
98 #define UVSCOM_SPEED_1200BPS    0x03
99 #define UVSCOM_SPEED_2400BPS    0x04
100 #define UVSCOM_SPEED_4800BPS    0x05
101 #define UVSCOM_SPEED_9600BPS    0x06
102 #define UVSCOM_SPEED_19200BPS   0x07
103 #define UVSCOM_SPEED_38400BPS   0x08
104 #define UVSCOM_SPEED_57600BPS   0x09
105 #define UVSCOM_SPEED_115200BPS  0x0a
106
107 /* UVSCOM_LINE_CTL parameters */
108 #define UVSCOM_BREAK            0x40
109 #define UVSCOM_RTS              0x02
110 #define UVSCOM_DTR              0x01
111 #define UVSCOM_LINE_INIT        0x08
112
113 /* UVSCOM_SET_PARAM parameters */
114 #define UVSCOM_DATA_MASK        0x03
115 #define UVSCOM_DATA_BIT_8       0x03
116 #define UVSCOM_DATA_BIT_7       0x02
117 #define UVSCOM_DATA_BIT_6       0x01
118 #define UVSCOM_DATA_BIT_5       0x00
119
120 #define UVSCOM_STOP_MASK        0x04
121 #define UVSCOM_STOP_BIT_2       0x04
122 #define UVSCOM_STOP_BIT_1       0x00
123
124 #define UVSCOM_PARITY_MASK      0x18
125 #define UVSCOM_PARITY_EVEN      0x18
126 #if 0
127 #define UVSCOM_PARITY_UNK       0x10
128 #endif
129 #define UVSCOM_PARITY_ODD       0x08
130 #define UVSCOM_PARITY_NONE      0x00
131
132 /* Status bits */
133 #define UVSCOM_TXRDY            0x04
134 #define UVSCOM_RXRDY            0x01
135
136 #define UVSCOM_DCD              0x08
137 #define UVSCOM_NOCARD           0x04
138 #define UVSCOM_DSR              0x02
139 #define UVSCOM_CTS              0x01
140 #define UVSCOM_USTAT_MASK       (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
141
142 struct  uvscom_softc {
143         struct ucom_softc       sc_ucom;
144
145         int                     sc_iface_number;/* interface number */
146
147         usbd_interface_handle   sc_intr_iface;  /* interrupt interface */
148         int                     sc_intr_number; /* interrupt number */
149         usbd_pipe_handle        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;         /* uvscom status register */
158
159         uint16_t                sc_lcr;         /* Line control */
160         u_char                  sc_usr;         /* unit status */
161 };
162
163 /*
164  * These are the maximum number of bytes transferred per frame.
165  * The output buffer size cannot be increased due to the size encoding.
166  */
167 #define UVSCOMIBUFSIZE 512
168 #define UVSCOMOBUFSIZE 64
169
170 static  usbd_status uvscom_shutdown(struct uvscom_softc *);
171 static  usbd_status uvscom_reset(struct uvscom_softc *);
172 static  usbd_status uvscom_set_line_coding(struct uvscom_softc *,
173                                            uint16_t, uint16_t);
174 static  usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
175 static  usbd_status uvscom_set_crtscts(struct uvscom_softc *);
176 static  void uvscom_get_status(void *, int, u_char *, u_char *);
177 static  void uvscom_dtr(struct uvscom_softc *, int);
178 static  void uvscom_rts(struct uvscom_softc *, int);
179 static  void uvscom_break(struct uvscom_softc *, int);
180
181 static  void uvscom_set(void *, int, int, int);
182 static  void uvscom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
183 #if TODO
184 static  int  uvscom_ioctl(void *, int, u_long, caddr_t, int, struct thread *);
185 #endif
186 static  int  uvscom_param(void *, int, struct termios *);
187 static  int  uvscom_open(void *, int);
188 static  void uvscom_close(void *, int);
189
190 struct ucom_callback uvscom_callback = {
191         uvscom_get_status,
192         uvscom_set,
193         uvscom_param,
194         NULL, /* uvscom_ioctl, TODO */
195         uvscom_open,
196         uvscom_close,
197         NULL,
198         NULL
199 };
200
201 static const struct usb_devno uvscom_devs [] = {
202         { USB_DEVICE(0x05db, 0x0003) }, /* SUNTAC U-Cable type D2 */
203         { USB_DEVICE(0x05db, 0x0005) }, /* SUNTAC U-Cable type P1 */
204         { USB_DEVICE(0x05db, 0x0009) }, /* SUNTAC Slipper U */
205         { USB_DEVICE(0x05db, 0x000a) }, /* SUNTAC Ir-Trinity */
206 };
207
208 static device_probe_t uvscom_match;
209 static device_attach_t uvscom_attach;
210 static device_detach_t uvscom_detach;
211
212 static device_method_t uvscom_methods[] = {
213         /* Device interface */
214         DEVMETHOD(device_probe, uvscom_match),
215         DEVMETHOD(device_attach, uvscom_attach),
216         DEVMETHOD(device_detach, uvscom_detach),
217         { 0, 0 }
218 };
219
220 static driver_t uvscom_driver = {
221         "ucom",
222         uvscom_methods,
223         sizeof (struct uvscom_softc)
224 };
225
226 DRIVER_MODULE(uvscom, uhub, uvscom_driver, ucom_devclass, usbd_driver_load, 0);
227 MODULE_DEPEND(uvscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
228 MODULE_VERSION(uvscom, UVSCOM_MODVER);
229
230 static int
231 uvscom_match(device_t self)
232 {
233         struct usb_attach_arg *uaa = device_get_ivars(self);
234
235         if (uaa->iface != NULL)
236                 return (UMATCH_NONE);
237
238         return (usb_lookup(uvscom_devs, uaa->vendor, uaa->product) != NULL ?
239                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
240 }
241
242 static int
243 uvscom_attach(device_t self)
244 {
245         struct uvscom_softc *sc = device_get_softc(self);
246         struct usb_attach_arg *uaa = device_get_ivars(self);
247         usbd_device_handle dev = uaa->device;
248         struct ucom_softc *ucom;
249         usb_config_descriptor_t *cdesc;
250         usb_interface_descriptor_t *id;
251         usb_endpoint_descriptor_t *ed;
252         char *devinfo;
253         const char *devname;
254         usbd_status err;
255         int i;
256
257         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
258         ucom = &sc->sc_ucom;
259
260         bzero(sc, sizeof (struct uvscom_softc));
261
262         usbd_devinfo(dev, 0, devinfo);
263         ucom->sc_dev = self;
264         device_set_desc_copy(self, devinfo);
265
266         ucom->sc_udev = dev;
267         ucom->sc_iface = uaa->iface;
268
269         devname = device_get_nameunit(ucom->sc_dev);
270         kprintf("%s: %s\n", devname, devinfo);
271
272         DPRINTF(("uvscom attach: sc = %p\n", sc));
273
274         /* initialize endpoints */
275         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
276         sc->sc_intr_number = -1;
277         sc->sc_intr_pipe = NULL;
278
279         /* Move the device into the configured state. */
280         err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
281         if (err) {
282                 kprintf("%s: failed to set configuration, err=%s\n",
283                         devname, usbd_errstr(err));
284                 goto error;
285         }
286
287         /* get the config descriptor */
288         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
289
290         if (cdesc == NULL) {
291                 kprintf("%s: failed to get configuration descriptor\n",
292                         device_get_nameunit(ucom->sc_dev));
293                 goto error;
294         }
295
296         /* get the common interface */
297         err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
298                                            &ucom->sc_iface);
299         if (err) {
300                 kprintf("%s: failed to get interface, err=%s\n",
301                         devname, usbd_errstr(err));
302                 goto error;
303         }
304
305         id = usbd_get_interface_descriptor(ucom->sc_iface);
306         sc->sc_iface_number = id->bInterfaceNumber;
307
308         /* Find endpoints */
309         for (i = 0; i < id->bNumEndpoints; i++) {
310                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
311                 if (ed == NULL) {
312                         kprintf("%s: no endpoint descriptor for %d\n",
313                                 device_get_nameunit(ucom->sc_dev), i);
314                         goto error;
315                 }
316
317                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
318                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
319                         ucom->sc_bulkin_no = ed->bEndpointAddress;
320                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
321                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
322                         ucom->sc_bulkout_no = ed->bEndpointAddress;
323                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
324                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
325                         sc->sc_intr_number = ed->bEndpointAddress;
326                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
327                 }
328         }
329
330         if (ucom->sc_bulkin_no == -1) {
331                 kprintf("%s: Could not find data bulk in\n",
332                         device_get_nameunit(ucom->sc_dev));
333                 goto error;
334         }
335         if (ucom->sc_bulkout_no == -1) {
336                 kprintf("%s: Could not find data bulk out\n",
337                         device_get_nameunit(ucom->sc_dev));
338                 goto error;
339         }
340         if (sc->sc_intr_number == -1) {
341                 kprintf("%s: Could not find interrupt in\n",
342                         device_get_nameunit(ucom->sc_dev));
343                 goto error;
344         }
345
346         sc->sc_dtr = sc->sc_rts = 0;
347         sc->sc_lcr = UVSCOM_LINE_INIT;
348
349         ucom->sc_parent = sc;
350         ucom->sc_portno = UCOM_UNK_PORTNO;
351         /* bulkin, bulkout set above */
352         ucom->sc_ibufsize = UVSCOMIBUFSIZE;
353         ucom->sc_obufsize = UVSCOMOBUFSIZE;
354         ucom->sc_ibufsizepad = UVSCOMIBUFSIZE;
355         ucom->sc_opkthdrlen = 0;
356         ucom->sc_callback = &uvscom_callback;
357
358         err = uvscom_reset(sc);
359
360         if (err) {
361                 kprintf("%s: reset failed, %s\n", device_get_nameunit(ucom->sc_dev),
362                         usbd_errstr(err));
363                 goto error;
364         }
365
366         DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
367                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
368
369         ucom_attach(&sc->sc_ucom);
370
371         kfree(devinfo, M_USBDEV);
372         return 0;
373
374 error:
375         ucom->sc_dying = 1;
376         kfree(devinfo, M_USBDEV);
377         return ENXIO;
378 }
379
380 static int
381 uvscom_detach(device_t self)
382 {
383         struct uvscom_softc *sc = device_get_softc(self);
384         int rv = 0;
385
386         DPRINTF(("uvscom_detach: sc = %p\n", sc));
387
388         sc->sc_ucom.sc_dying = 1;
389
390         if (sc->sc_intr_pipe != NULL) {
391                 usbd_abort_pipe(sc->sc_intr_pipe);
392                 usbd_close_pipe(sc->sc_intr_pipe);
393                 kfree(sc->sc_intr_buf, M_USBDEV);
394                 sc->sc_intr_pipe = NULL;
395         }
396
397         rv = ucom_detach(&sc->sc_ucom);
398
399         return (rv);
400 }
401
402 static usbd_status
403 uvscom_readstat(struct uvscom_softc *sc)
404 {
405         usb_device_request_t req;
406         usbd_status err;
407         uint16_t r;
408
409         DPRINTF(("%s: send readstat\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
410
411         req.bmRequestType = UT_READ_VENDOR_DEVICE;
412         req.bRequest = UVSCOM_READ_STATUS;
413         USETW(req.wValue, 0);
414         USETW(req.wIndex, 0);
415         USETW(req.wLength, 2);
416
417         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &r);
418         if (err) {
419                 kprintf("%s: uvscom_readstat: %s\n",
420                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
421                 return (err);
422         }
423
424         DPRINTF(("%s: uvscom_readstat: r = %d\n",
425                  device_get_nameunit(sc->sc_ucom.sc_dev), r));
426
427         return (USBD_NORMAL_COMPLETION);
428 }
429
430 static usbd_status
431 uvscom_shutdown(struct uvscom_softc *sc)
432 {
433         usb_device_request_t req;
434         usbd_status err;
435
436         DPRINTF(("%s: send shutdown\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
437
438         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
439         req.bRequest = UVSCOM_SHUTDOWN;
440         USETW(req.wValue, 0);
441         USETW(req.wIndex, 0);
442         USETW(req.wLength, 0);
443
444         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
445         if (err) {
446                 kprintf("%s: uvscom_shutdown: %s\n",
447                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
448                 return (err);
449         }
450
451         return (USBD_NORMAL_COMPLETION);
452 }
453
454 static usbd_status
455 uvscom_reset(struct uvscom_softc *sc)
456 {
457         DPRINTF(("%s: uvscom_reset\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
458
459         return (USBD_NORMAL_COMPLETION);
460 }
461
462 static usbd_status
463 uvscom_set_crtscts(struct uvscom_softc *sc)
464 {
465         DPRINTF(("%s: uvscom_set_crtscts\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
466
467         return (USBD_NORMAL_COMPLETION);
468 }
469
470 static usbd_status
471 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
472 {
473         usb_device_request_t req;
474         usbd_status err;
475
476         DPRINTF(("%s: uvscom_set_line: %04x\n",
477                  device_get_nameunit(sc->sc_ucom.sc_dev), line));
478
479         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
480         req.bRequest = UVSCOM_LINE_CTL;
481         USETW(req.wValue, line);
482         USETW(req.wIndex, 0);
483         USETW(req.wLength, 0);
484
485         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
486         if (err) {
487                 kprintf("%s: uvscom_set_line: %s\n",
488                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
489                 return (err);
490         }
491
492         return (USBD_NORMAL_COMPLETION);
493 }
494
495 static usbd_status
496 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
497 {
498         usb_device_request_t req;
499         usbd_status err;
500
501         DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
502                  device_get_nameunit(sc->sc_ucom.sc_dev), lsp, ls));
503
504         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
505         req.bRequest = UVSCOM_SET_SPEED;
506         USETW(req.wValue, lsp);
507         USETW(req.wIndex, 0);
508         USETW(req.wLength, 0);
509
510         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
511         if (err) {
512                 kprintf("%s: uvscom_set_line_coding: %s\n",
513                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
514                 return (err);
515         }
516
517         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
518         req.bRequest = UVSCOM_SET_PARAM;
519         USETW(req.wValue, ls);
520         USETW(req.wIndex, 0);
521         USETW(req.wLength, 0);
522
523         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
524         if (err) {
525                 kprintf("%s: uvscom_set_line_coding: %s\n",
526                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
527                 return (err);
528         }
529
530         return (USBD_NORMAL_COMPLETION);
531 }
532
533 static void
534 uvscom_dtr(struct uvscom_softc *sc, int onoff)
535 {
536         DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
537                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
538
539         if (sc->sc_dtr == onoff)
540                 return;                 /* no change */
541
542         sc->sc_dtr = onoff;
543
544         if (onoff)
545                 SET(sc->sc_lcr, UVSCOM_DTR);
546         else
547                 CLR(sc->sc_lcr, UVSCOM_DTR);
548
549         uvscom_set_line(sc, sc->sc_lcr);
550 }
551
552 static void
553 uvscom_rts(struct uvscom_softc *sc, int onoff)
554 {
555         DPRINTF(("%s: uvscom_rts: onoff = %d\n",
556                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
557
558         if (sc->sc_rts == onoff)
559                 return;                 /* no change */
560
561         sc->sc_rts = onoff;
562
563         if (onoff)
564                 SET(sc->sc_lcr, UVSCOM_RTS);
565         else
566                 CLR(sc->sc_lcr, UVSCOM_RTS);
567
568         uvscom_set_line(sc, sc->sc_lcr);
569 }
570
571 static void
572 uvscom_break(struct uvscom_softc *sc, int onoff)
573 {
574         DPRINTF(("%s: uvscom_break: onoff = %d\n",
575                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
576
577         if (onoff)
578                 uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
579 }
580
581 static void
582 uvscom_set(void *addr, int portno, int reg, int onoff)
583 {
584         struct uvscom_softc *sc = addr;
585
586         switch (reg) {
587         case UCOM_SET_DTR:
588                 uvscom_dtr(sc, onoff);
589                 break;
590         case UCOM_SET_RTS:
591                 uvscom_rts(sc, onoff);
592                 break;
593         case UCOM_SET_BREAK:
594                 uvscom_break(sc, onoff);
595                 break;
596         default:
597                 break;
598         }
599 }
600
601 static int
602 uvscom_param(void *addr, int portno, struct termios *t)
603 {
604         struct uvscom_softc *sc = addr;
605         usbd_status err;
606         uint16_t lsp;
607         uint16_t ls;
608
609         DPRINTF(("%s: uvscom_param: sc = %p\n",
610                  device_get_nameunit(sc->sc_ucom.sc_dev), sc));
611
612         ls = 0;
613
614         switch (t->c_ospeed) {
615         case B150:
616                 lsp = UVSCOM_SPEED_150BPS;
617                 break;
618         case B300:
619                 lsp = UVSCOM_SPEED_300BPS;
620                 break;
621         case B600:
622                 lsp = UVSCOM_SPEED_600BPS;
623                 break;
624         case B1200:
625                 lsp = UVSCOM_SPEED_1200BPS;
626                 break;
627         case B2400:
628                 lsp = UVSCOM_SPEED_2400BPS;
629                 break;
630         case B4800:
631                 lsp = UVSCOM_SPEED_4800BPS;
632                 break;
633         case B9600:
634                 lsp = UVSCOM_SPEED_9600BPS;
635                 break;
636         case B19200:
637                 lsp = UVSCOM_SPEED_19200BPS;
638                 break;
639         case B38400:
640                 lsp = UVSCOM_SPEED_38400BPS;
641                 break;
642         case B57600:
643                 lsp = UVSCOM_SPEED_57600BPS;
644                 break;
645         case B115200:
646                 lsp = UVSCOM_SPEED_115200BPS;
647                 break;
648         default:
649                 return (EIO);
650         }
651
652         if (ISSET(t->c_cflag, CSTOPB))
653                 SET(ls, UVSCOM_STOP_BIT_2);
654         else
655                 SET(ls, UVSCOM_STOP_BIT_1);
656
657         if (ISSET(t->c_cflag, PARENB)) {
658                 if (ISSET(t->c_cflag, PARODD))
659                         SET(ls, UVSCOM_PARITY_ODD);
660                 else
661                         SET(ls, UVSCOM_PARITY_EVEN);
662         } else
663                 SET(ls, UVSCOM_PARITY_NONE);
664
665         switch (ISSET(t->c_cflag, CSIZE)) {
666         case CS5:
667                 SET(ls, UVSCOM_DATA_BIT_5);
668                 break;
669         case CS6:
670                 SET(ls, UVSCOM_DATA_BIT_6);
671                 break;
672         case CS7:
673                 SET(ls, UVSCOM_DATA_BIT_7);
674                 break;
675         case CS8:
676                 SET(ls, UVSCOM_DATA_BIT_8);
677                 break;
678         default:
679                 return (EIO);
680         }
681
682         err = uvscom_set_line_coding(sc, lsp, ls);
683         if (err)
684                 return (EIO);
685
686         if (ISSET(t->c_cflag, CRTSCTS)) {
687                 err = uvscom_set_crtscts(sc);
688                 if (err)
689                         return (EIO);
690         }
691
692         return (0);
693 }
694
695 static int
696 uvscom_open(void *addr, int portno)
697 {
698         struct uvscom_softc *sc = addr;
699         int err;
700         int i;
701
702         if (sc->sc_ucom.sc_dying)
703                 return (ENXIO);
704
705         DPRINTF(("uvscom_open: sc = %p\n", sc));
706
707         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
708                 DPRINTF(("uvscom_open: open interrupt pipe.\n"));
709
710                 sc->sc_usr = 0;         /* clear unit status */
711
712                 err = uvscom_readstat(sc);
713                 if (err) {
714                         DPRINTF(("%s: uvscom_open: readstat faild\n",
715                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
716                         return (ENXIO);
717                 }
718
719                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
720                 err = usbd_open_pipe_intr(sc->sc_ucom.sc_iface,
721                                           sc->sc_intr_number,
722                                           USBD_SHORT_XFER_OK,
723                                           &sc->sc_intr_pipe,
724                                           sc,
725                                           sc->sc_intr_buf,
726                                           sc->sc_isize,
727                                           uvscom_intr,
728                                           UVSCOM_INTR_INTERVAL);
729                 if (err) {
730                         kprintf("%s: cannot open interrupt pipe (addr %d)\n",
731                                  device_get_nameunit(sc->sc_ucom.sc_dev),
732                                  sc->sc_intr_number);
733                         return (ENXIO);
734                 }
735         } else {
736                 DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
737         }
738
739         if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
740                 /* unit is not ready */
741
742                 for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
743                         tsleep(&err, 0, "uvsop", hz);   /* XXX */
744                         if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
745                                 break;
746                 }
747                 if (i == 0) {
748                         DPRINTF(("%s: unit is not ready\n",
749                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
750                         return (ENXIO);
751                 }
752
753                 /* check PC card was inserted */
754                 if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
755                         DPRINTF(("%s: no card\n",
756                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
757                         return (ENXIO);
758                 }
759         }
760
761         return (0);
762 }
763
764 static void
765 uvscom_close(void *addr, int portno)
766 {
767         struct uvscom_softc *sc = addr;
768         int err;
769
770         if (sc->sc_ucom.sc_dying)
771                 return;
772
773         DPRINTF(("uvscom_close: close\n"));
774
775         uvscom_shutdown(sc);
776
777         if (sc->sc_intr_pipe != NULL) {
778                 err = usbd_abort_pipe(sc->sc_intr_pipe);
779                 if (err)
780                         kprintf("%s: abort interrupt pipe failed: %s\n",
781                                 device_get_nameunit(sc->sc_ucom.sc_dev),
782                                            usbd_errstr(err));
783                 err = usbd_close_pipe(sc->sc_intr_pipe);
784                 if (err)
785                         kprintf("%s: close interrupt pipe failed: %s\n",
786                                 device_get_nameunit(sc->sc_ucom.sc_dev),
787                                            usbd_errstr(err));
788                 kfree(sc->sc_intr_buf, M_USBDEV);
789                 sc->sc_intr_pipe = NULL;
790         }
791 }
792
793 static void
794 uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
795 {
796         struct uvscom_softc *sc = priv;
797         u_char *buf = sc->sc_intr_buf;
798         u_char pstatus;
799
800         if (sc->sc_ucom.sc_dying)
801                 return;
802
803         if (status != USBD_NORMAL_COMPLETION) {
804                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
805                         return;
806
807                 kprintf("%s: uvscom_intr: abnormal status: %s\n",
808                         device_get_nameunit(sc->sc_ucom.sc_dev),
809                         usbd_errstr(status));
810                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
811                 return;
812         }
813
814         DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
815                  device_get_nameunit(sc->sc_ucom.sc_dev), buf[0], buf[1]));
816
817         sc->sc_lsr = sc->sc_msr = 0;
818         sc->sc_usr = buf[1];
819
820         pstatus = buf[0];
821         if (ISSET(pstatus, UVSCOM_TXRDY))
822                 SET(sc->sc_lsr, ULSR_TXRDY);
823         if (ISSET(pstatus, UVSCOM_RXRDY))
824                 SET(sc->sc_lsr, ULSR_RXRDY);
825
826         pstatus = buf[1];
827         if (ISSET(pstatus, UVSCOM_CTS))
828                 SET(sc->sc_msr, UMSR_CTS);
829         if (ISSET(pstatus, UVSCOM_DSR))
830                 SET(sc->sc_msr, UMSR_DSR);
831         if (ISSET(pstatus, UVSCOM_DCD))
832                 SET(sc->sc_msr, UMSR_DCD);
833
834         ucom_status_change(&sc->sc_ucom);
835 }
836
837 static void
838 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
839 {
840         struct uvscom_softc *sc = addr;
841
842         if (lsr != NULL)
843                 *lsr = sc->sc_lsr;
844         if (msr != NULL)
845                 *msr = sc->sc_msr;
846 }
847
848 #if TODO
849 static int
850 uvscom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
851              struct thread *p)
852 {
853         struct uvscom_softc *sc = addr;
854         int error = 0;
855
856         if (sc->sc_ucom.sc_dying)
857                 return (EIO);
858
859         DPRINTF(("uvscom_ioctl: cmd = 0x%08lx\n", cmd));
860
861         switch (cmd) {
862         case TIOCNOTTY:
863         case TIOCMGET:
864         case TIOCMSET:
865                 break;
866
867         default:
868                 DPRINTF(("uvscom_ioctl: unknown\n"));
869                 error = ENOTTY;
870                 break;
871         }
872
873         return (error);
874 }
875 #endif