Reomve unnecessary sys/vnode.h include.
[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.16 2007/08/07 10:42:41 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         /* SUNTAC U-Cable type D2 */
203         { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
204         /* SUNTAC Ir-Trinity */
205         { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
206         /* SUNTAC U-Cable type P1 */
207         { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
208         /* SUNTAC Slipper U */
209         { USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
210 };
211 #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p)
212
213 static device_probe_t uvscom_match;
214 static device_attach_t uvscom_attach;
215 static device_detach_t uvscom_detach;
216
217 static device_method_t uvscom_methods[] = {
218         /* Device interface */
219         DEVMETHOD(device_probe, uvscom_match),
220         DEVMETHOD(device_attach, uvscom_attach),
221         DEVMETHOD(device_detach, uvscom_detach),
222         { 0, 0 }
223 };
224
225 static driver_t uvscom_driver = {
226         "ucom",
227         uvscom_methods,
228         sizeof (struct uvscom_softc)
229 };
230
231 DRIVER_MODULE(uvscom, uhub, uvscom_driver, ucom_devclass, usbd_driver_load, 0);
232 MODULE_DEPEND(uvscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
233 MODULE_VERSION(uvscom, UVSCOM_MODVER);
234
235 static int
236 uvscom_match(device_t self)
237 {
238         struct usb_attach_arg *uaa = device_get_ivars(self);
239
240         if (uaa->iface != NULL)
241                 return (UMATCH_NONE);
242
243         return (uvscom_lookup(uaa->vendor, uaa->product) != NULL ?
244                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
245 }
246
247 static int
248 uvscom_attach(device_t self)
249 {
250         struct uvscom_softc *sc = device_get_softc(self);
251         struct usb_attach_arg *uaa = device_get_ivars(self);
252         usbd_device_handle dev = uaa->device;
253         struct ucom_softc *ucom;
254         usb_config_descriptor_t *cdesc;
255         usb_interface_descriptor_t *id;
256         usb_endpoint_descriptor_t *ed;
257         char *devinfo;
258         const char *devname;
259         usbd_status err;
260         int i;
261
262         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
263         ucom = &sc->sc_ucom;
264
265         bzero(sc, sizeof (struct uvscom_softc));
266
267         usbd_devinfo(dev, 0, devinfo);
268         ucom->sc_dev = self;
269         device_set_desc_copy(self, devinfo);
270
271         ucom->sc_udev = dev;
272         ucom->sc_iface = uaa->iface;
273
274         devname = device_get_nameunit(ucom->sc_dev);
275         kprintf("%s: %s\n", devname, devinfo);
276
277         DPRINTF(("uvscom attach: sc = %p\n", sc));
278
279         /* initialize endpoints */
280         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
281         sc->sc_intr_number = -1;
282         sc->sc_intr_pipe = NULL;
283
284         /* Move the device into the configured state. */
285         err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
286         if (err) {
287                 kprintf("%s: failed to set configuration, err=%s\n",
288                         devname, usbd_errstr(err));
289                 goto error;
290         }
291
292         /* get the config descriptor */
293         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
294
295         if (cdesc == NULL) {
296                 kprintf("%s: failed to get configuration descriptor\n",
297                         device_get_nameunit(ucom->sc_dev));
298                 goto error;
299         }
300
301         /* get the common interface */
302         err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
303                                            &ucom->sc_iface);
304         if (err) {
305                 kprintf("%s: failed to get interface, err=%s\n",
306                         devname, usbd_errstr(err));
307                 goto error;
308         }
309
310         id = usbd_get_interface_descriptor(ucom->sc_iface);
311         sc->sc_iface_number = id->bInterfaceNumber;
312
313         /* Find endpoints */
314         for (i = 0; i < id->bNumEndpoints; i++) {
315                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
316                 if (ed == NULL) {
317                         kprintf("%s: no endpoint descriptor for %d\n",
318                                 device_get_nameunit(ucom->sc_dev), i);
319                         goto error;
320                 }
321
322                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
323                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
324                         ucom->sc_bulkin_no = ed->bEndpointAddress;
325                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
326                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
327                         ucom->sc_bulkout_no = ed->bEndpointAddress;
328                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
329                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
330                         sc->sc_intr_number = ed->bEndpointAddress;
331                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
332                 }
333         }
334
335         if (ucom->sc_bulkin_no == -1) {
336                 kprintf("%s: Could not find data bulk in\n",
337                         device_get_nameunit(ucom->sc_dev));
338                 goto error;
339         }
340         if (ucom->sc_bulkout_no == -1) {
341                 kprintf("%s: Could not find data bulk out\n",
342                         device_get_nameunit(ucom->sc_dev));
343                 goto error;
344         }
345         if (sc->sc_intr_number == -1) {
346                 kprintf("%s: Could not find interrupt in\n",
347                         device_get_nameunit(ucom->sc_dev));
348                 goto error;
349         }
350
351         sc->sc_dtr = sc->sc_rts = 0;
352         sc->sc_lcr = UVSCOM_LINE_INIT;
353
354         ucom->sc_parent = sc;
355         ucom->sc_portno = UCOM_UNK_PORTNO;
356         /* bulkin, bulkout set above */
357         ucom->sc_ibufsize = UVSCOMIBUFSIZE;
358         ucom->sc_obufsize = UVSCOMOBUFSIZE;
359         ucom->sc_ibufsizepad = UVSCOMIBUFSIZE;
360         ucom->sc_opkthdrlen = 0;
361         ucom->sc_callback = &uvscom_callback;
362
363         err = uvscom_reset(sc);
364
365         if (err) {
366                 kprintf("%s: reset failed, %s\n", device_get_nameunit(ucom->sc_dev),
367                         usbd_errstr(err));
368                 goto error;
369         }
370
371         DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
372                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
373
374         ucom_attach(&sc->sc_ucom);
375
376         kfree(devinfo, M_USBDEV);
377         return 0;
378
379 error:
380         ucom->sc_dying = 1;
381         kfree(devinfo, M_USBDEV);
382         return ENXIO;
383 }
384
385 static int
386 uvscom_detach(device_t self)
387 {
388         struct uvscom_softc *sc = device_get_softc(self);
389         int rv = 0;
390
391         DPRINTF(("uvscom_detach: sc = %p\n", sc));
392
393         sc->sc_ucom.sc_dying = 1;
394
395         if (sc->sc_intr_pipe != NULL) {
396                 usbd_abort_pipe(sc->sc_intr_pipe);
397                 usbd_close_pipe(sc->sc_intr_pipe);
398                 kfree(sc->sc_intr_buf, M_USBDEV);
399                 sc->sc_intr_pipe = NULL;
400         }
401
402         rv = ucom_detach(&sc->sc_ucom);
403
404         return (rv);
405 }
406
407 static usbd_status
408 uvscom_readstat(struct uvscom_softc *sc)
409 {
410         usb_device_request_t req;
411         usbd_status err;
412         uint16_t r;
413
414         DPRINTF(("%s: send readstat\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
415
416         req.bmRequestType = UT_READ_VENDOR_DEVICE;
417         req.bRequest = UVSCOM_READ_STATUS;
418         USETW(req.wValue, 0);
419         USETW(req.wIndex, 0);
420         USETW(req.wLength, 2);
421
422         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &r);
423         if (err) {
424                 kprintf("%s: uvscom_readstat: %s\n",
425                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
426                 return (err);
427         }
428
429         DPRINTF(("%s: uvscom_readstat: r = %d\n",
430                  device_get_nameunit(sc->sc_ucom.sc_dev), r));
431
432         return (USBD_NORMAL_COMPLETION);
433 }
434
435 static usbd_status
436 uvscom_shutdown(struct uvscom_softc *sc)
437 {
438         usb_device_request_t req;
439         usbd_status err;
440
441         DPRINTF(("%s: send shutdown\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
442
443         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
444         req.bRequest = UVSCOM_SHUTDOWN;
445         USETW(req.wValue, 0);
446         USETW(req.wIndex, 0);
447         USETW(req.wLength, 0);
448
449         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
450         if (err) {
451                 kprintf("%s: uvscom_shutdown: %s\n",
452                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
453                 return (err);
454         }
455
456         return (USBD_NORMAL_COMPLETION);
457 }
458
459 static usbd_status
460 uvscom_reset(struct uvscom_softc *sc)
461 {
462         DPRINTF(("%s: uvscom_reset\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
463
464         return (USBD_NORMAL_COMPLETION);
465 }
466
467 static usbd_status
468 uvscom_set_crtscts(struct uvscom_softc *sc)
469 {
470         DPRINTF(("%s: uvscom_set_crtscts\n", device_get_nameunit(sc->sc_ucom.sc_dev)));
471
472         return (USBD_NORMAL_COMPLETION);
473 }
474
475 static usbd_status
476 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
477 {
478         usb_device_request_t req;
479         usbd_status err;
480
481         DPRINTF(("%s: uvscom_set_line: %04x\n",
482                  device_get_nameunit(sc->sc_ucom.sc_dev), line));
483
484         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
485         req.bRequest = UVSCOM_LINE_CTL;
486         USETW(req.wValue, line);
487         USETW(req.wIndex, 0);
488         USETW(req.wLength, 0);
489
490         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
491         if (err) {
492                 kprintf("%s: uvscom_set_line: %s\n",
493                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
494                 return (err);
495         }
496
497         return (USBD_NORMAL_COMPLETION);
498 }
499
500 static usbd_status
501 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
502 {
503         usb_device_request_t req;
504         usbd_status err;
505
506         DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
507                  device_get_nameunit(sc->sc_ucom.sc_dev), lsp, ls));
508
509         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
510         req.bRequest = UVSCOM_SET_SPEED;
511         USETW(req.wValue, lsp);
512         USETW(req.wIndex, 0);
513         USETW(req.wLength, 0);
514
515         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
516         if (err) {
517                 kprintf("%s: uvscom_set_line_coding: %s\n",
518                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
519                 return (err);
520         }
521
522         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
523         req.bRequest = UVSCOM_SET_PARAM;
524         USETW(req.wValue, ls);
525         USETW(req.wIndex, 0);
526         USETW(req.wLength, 0);
527
528         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
529         if (err) {
530                 kprintf("%s: uvscom_set_line_coding: %s\n",
531                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
532                 return (err);
533         }
534
535         return (USBD_NORMAL_COMPLETION);
536 }
537
538 static void
539 uvscom_dtr(struct uvscom_softc *sc, int onoff)
540 {
541         DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
542                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
543
544         if (sc->sc_dtr == onoff)
545                 return;                 /* no change */
546
547         sc->sc_dtr = onoff;
548
549         if (onoff)
550                 SET(sc->sc_lcr, UVSCOM_DTR);
551         else
552                 CLR(sc->sc_lcr, UVSCOM_DTR);
553
554         uvscom_set_line(sc, sc->sc_lcr);
555 }
556
557 static void
558 uvscom_rts(struct uvscom_softc *sc, int onoff)
559 {
560         DPRINTF(("%s: uvscom_rts: onoff = %d\n",
561                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
562
563         if (sc->sc_rts == onoff)
564                 return;                 /* no change */
565
566         sc->sc_rts = onoff;
567
568         if (onoff)
569                 SET(sc->sc_lcr, UVSCOM_RTS);
570         else
571                 CLR(sc->sc_lcr, UVSCOM_RTS);
572
573         uvscom_set_line(sc, sc->sc_lcr);
574 }
575
576 static void
577 uvscom_break(struct uvscom_softc *sc, int onoff)
578 {
579         DPRINTF(("%s: uvscom_break: onoff = %d\n",
580                  device_get_nameunit(sc->sc_ucom.sc_dev), onoff));
581
582         if (onoff)
583                 uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
584 }
585
586 static void
587 uvscom_set(void *addr, int portno, int reg, int onoff)
588 {
589         struct uvscom_softc *sc = addr;
590
591         switch (reg) {
592         case UCOM_SET_DTR:
593                 uvscom_dtr(sc, onoff);
594                 break;
595         case UCOM_SET_RTS:
596                 uvscom_rts(sc, onoff);
597                 break;
598         case UCOM_SET_BREAK:
599                 uvscom_break(sc, onoff);
600                 break;
601         default:
602                 break;
603         }
604 }
605
606 static int
607 uvscom_param(void *addr, int portno, struct termios *t)
608 {
609         struct uvscom_softc *sc = addr;
610         usbd_status err;
611         uint16_t lsp;
612         uint16_t ls;
613
614         DPRINTF(("%s: uvscom_param: sc = %p\n",
615                  device_get_nameunit(sc->sc_ucom.sc_dev), sc));
616
617         ls = 0;
618
619         switch (t->c_ospeed) {
620         case B150:
621                 lsp = UVSCOM_SPEED_150BPS;
622                 break;
623         case B300:
624                 lsp = UVSCOM_SPEED_300BPS;
625                 break;
626         case B600:
627                 lsp = UVSCOM_SPEED_600BPS;
628                 break;
629         case B1200:
630                 lsp = UVSCOM_SPEED_1200BPS;
631                 break;
632         case B2400:
633                 lsp = UVSCOM_SPEED_2400BPS;
634                 break;
635         case B4800:
636                 lsp = UVSCOM_SPEED_4800BPS;
637                 break;
638         case B9600:
639                 lsp = UVSCOM_SPEED_9600BPS;
640                 break;
641         case B19200:
642                 lsp = UVSCOM_SPEED_19200BPS;
643                 break;
644         case B38400:
645                 lsp = UVSCOM_SPEED_38400BPS;
646                 break;
647         case B57600:
648                 lsp = UVSCOM_SPEED_57600BPS;
649                 break;
650         case B115200:
651                 lsp = UVSCOM_SPEED_115200BPS;
652                 break;
653         default:
654                 return (EIO);
655         }
656
657         if (ISSET(t->c_cflag, CSTOPB))
658                 SET(ls, UVSCOM_STOP_BIT_2);
659         else
660                 SET(ls, UVSCOM_STOP_BIT_1);
661
662         if (ISSET(t->c_cflag, PARENB)) {
663                 if (ISSET(t->c_cflag, PARODD))
664                         SET(ls, UVSCOM_PARITY_ODD);
665                 else
666                         SET(ls, UVSCOM_PARITY_EVEN);
667         } else
668                 SET(ls, UVSCOM_PARITY_NONE);
669
670         switch (ISSET(t->c_cflag, CSIZE)) {
671         case CS5:
672                 SET(ls, UVSCOM_DATA_BIT_5);
673                 break;
674         case CS6:
675                 SET(ls, UVSCOM_DATA_BIT_6);
676                 break;
677         case CS7:
678                 SET(ls, UVSCOM_DATA_BIT_7);
679                 break;
680         case CS8:
681                 SET(ls, UVSCOM_DATA_BIT_8);
682                 break;
683         default:
684                 return (EIO);
685         }
686
687         err = uvscom_set_line_coding(sc, lsp, ls);
688         if (err)
689                 return (EIO);
690
691         if (ISSET(t->c_cflag, CRTSCTS)) {
692                 err = uvscom_set_crtscts(sc);
693                 if (err)
694                         return (EIO);
695         }
696
697         return (0);
698 }
699
700 static int
701 uvscom_open(void *addr, int portno)
702 {
703         struct uvscom_softc *sc = addr;
704         int err;
705         int i;
706
707         if (sc->sc_ucom.sc_dying)
708                 return (ENXIO);
709
710         DPRINTF(("uvscom_open: sc = %p\n", sc));
711
712         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
713                 DPRINTF(("uvscom_open: open interrupt pipe.\n"));
714
715                 sc->sc_usr = 0;         /* clear unit status */
716
717                 err = uvscom_readstat(sc);
718                 if (err) {
719                         DPRINTF(("%s: uvscom_open: readstat faild\n",
720                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
721                         return (ENXIO);
722                 }
723
724                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
725                 err = usbd_open_pipe_intr(sc->sc_ucom.sc_iface,
726                                           sc->sc_intr_number,
727                                           USBD_SHORT_XFER_OK,
728                                           &sc->sc_intr_pipe,
729                                           sc,
730                                           sc->sc_intr_buf,
731                                           sc->sc_isize,
732                                           uvscom_intr,
733                                           UVSCOM_INTR_INTERVAL);
734                 if (err) {
735                         kprintf("%s: cannot open interrupt pipe (addr %d)\n",
736                                  device_get_nameunit(sc->sc_ucom.sc_dev),
737                                  sc->sc_intr_number);
738                         return (ENXIO);
739                 }
740         } else {
741                 DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
742         }
743
744         if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
745                 /* unit is not ready */
746
747                 for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
748                         tsleep(&err, 0, "uvsop", hz);   /* XXX */
749                         if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
750                                 break;
751                 }
752                 if (i == 0) {
753                         DPRINTF(("%s: unit is not ready\n",
754                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
755                         return (ENXIO);
756                 }
757
758                 /* check PC card was inserted */
759                 if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
760                         DPRINTF(("%s: no card\n",
761                                  device_get_nameunit(sc->sc_ucom.sc_dev)));
762                         return (ENXIO);
763                 }
764         }
765
766         return (0);
767 }
768
769 static void
770 uvscom_close(void *addr, int portno)
771 {
772         struct uvscom_softc *sc = addr;
773         int err;
774
775         if (sc->sc_ucom.sc_dying)
776                 return;
777
778         DPRINTF(("uvscom_close: close\n"));
779
780         uvscom_shutdown(sc);
781
782         if (sc->sc_intr_pipe != NULL) {
783                 err = usbd_abort_pipe(sc->sc_intr_pipe);
784                 if (err)
785                         kprintf("%s: abort interrupt pipe failed: %s\n",
786                                 device_get_nameunit(sc->sc_ucom.sc_dev),
787                                            usbd_errstr(err));
788                 err = usbd_close_pipe(sc->sc_intr_pipe);
789                 if (err)
790                         kprintf("%s: close interrupt pipe failed: %s\n",
791                                 device_get_nameunit(sc->sc_ucom.sc_dev),
792                                            usbd_errstr(err));
793                 kfree(sc->sc_intr_buf, M_USBDEV);
794                 sc->sc_intr_pipe = NULL;
795         }
796 }
797
798 static void
799 uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
800 {
801         struct uvscom_softc *sc = priv;
802         u_char *buf = sc->sc_intr_buf;
803         u_char pstatus;
804
805         if (sc->sc_ucom.sc_dying)
806                 return;
807
808         if (status != USBD_NORMAL_COMPLETION) {
809                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
810                         return;
811
812                 kprintf("%s: uvscom_intr: abnormal status: %s\n",
813                         device_get_nameunit(sc->sc_ucom.sc_dev),
814                         usbd_errstr(status));
815                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
816                 return;
817         }
818
819         DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
820                  device_get_nameunit(sc->sc_ucom.sc_dev), buf[0], buf[1]));
821
822         sc->sc_lsr = sc->sc_msr = 0;
823         sc->sc_usr = buf[1];
824
825         pstatus = buf[0];
826         if (ISSET(pstatus, UVSCOM_TXRDY))
827                 SET(sc->sc_lsr, ULSR_TXRDY);
828         if (ISSET(pstatus, UVSCOM_RXRDY))
829                 SET(sc->sc_lsr, ULSR_RXRDY);
830
831         pstatus = buf[1];
832         if (ISSET(pstatus, UVSCOM_CTS))
833                 SET(sc->sc_msr, UMSR_CTS);
834         if (ISSET(pstatus, UVSCOM_DSR))
835                 SET(sc->sc_msr, UMSR_DSR);
836         if (ISSET(pstatus, UVSCOM_DCD))
837                 SET(sc->sc_msr, UMSR_DCD);
838
839         ucom_status_change(&sc->sc_ucom);
840 }
841
842 static void
843 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
844 {
845         struct uvscom_softc *sc = addr;
846
847         if (lsr != NULL)
848                 *lsr = sc->sc_lsr;
849         if (msr != NULL)
850                 *msr = sc->sc_msr;
851 }
852
853 #if TODO
854 static int
855 uvscom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
856              struct thread *p)
857 {
858         struct uvscom_softc *sc = addr;
859         int error = 0;
860
861         if (sc->sc_ucom.sc_dying)
862                 return (EIO);
863
864         DPRINTF(("uvscom_ioctl: cmd = 0x%08lx\n", cmd));
865
866         switch (cmd) {
867         case TIOCNOTTY:
868         case TIOCMGET:
869         case TIOCMSET:
870                 break;
871
872         default:
873                 DPRINTF(("uvscom_ioctl: unknown\n"));
874                 error = ENOTTY;
875                 break;
876         }
877
878         return (error);
879 }
880 #endif