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