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