]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uplcom.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / usb / uplcom.c
1 /*      $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $        */
2
3 /*-
4  * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*-
33  * Copyright (c) 2001 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Ichiro FUKUHARA (ichiro@ichiro.org).
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *        This product includes software developed by the NetBSD
50  *        Foundation, Inc. and its contributors.
51  * 4. Neither the name of The NetBSD Foundation nor the names of its
52  *    contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67
68 /*
69  * This driver supports several USB-to-RS232 serial adapters driven by
70  * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
71  * bridge chip.  The adapters are sold under many different brand
72  * names.
73  *
74  * Datasheets are available at Prolific www site at
75  * http://www.prolific.com.tw.  The datasheets don't contain full
76  * programming information for the chip.
77  *
78  * PL-2303HX is probably programmed the same as PL-2303X.
79  *
80  * There are several differences between PL-2303 and PL-2303(H)X.
81  * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
82  * different command for controlling CRTSCTS and needs special
83  * sequence of commands for initialization which aren't also
84  * documented in the datasheet.
85  */
86
87 #include "opt_uplcom.h"
88
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/kernel.h>
92 #include <sys/module.h>
93 #include <sys/malloc.h>
94 #include <sys/bus.h>
95 #include <sys/ioccom.h>
96 #include <sys/fcntl.h>
97 #include <sys/conf.h>
98 #include <sys/serial.h>
99 #include <sys/tty.h>
100 #include <sys/file.h>
101 #include <sys/selinfo.h>
102 #include <sys/proc.h>
103 #include <sys/poll.h>
104 #include <sys/sysctl.h>
105 #include <sys/uio.h>
106 #include <sys/taskqueue.h>
107
108 #include <machine/bus.h>
109
110 #include <dev/usb/usb.h>
111 #include <dev/usb/usbcdc.h>
112
113 #include <dev/usb/usbdi.h>
114 #include <dev/usb/usbdivar.h>
115 #include <dev/usb/usbdi_util.h>
116 #include "usbdevs.h"
117 #include <dev/usb/usb_quirks.h>
118
119 #include <dev/usb/ucomvar.h>
120
121 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
122 #ifdef USB_DEBUG
123 static int      uplcomdebug = 0;
124 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
125            &uplcomdebug, 0, "uplcom debug level");
126
127 #define DPRINTFN(n, x)  do { \
128                                 if (uplcomdebug > (n)) \
129                                         printf x; \
130                         } while (0)
131 #else
132 #define DPRINTFN(n, x)
133 #endif
134 #define DPRINTF(x) DPRINTFN(0, x)
135
136 #define UPLCOM_MODVER                   1       /* module version */
137
138 #define UPLCOM_CONFIG_INDEX             0
139 #define UPLCOM_IFACE_INDEX              0
140 #define UPLCOM_SECOND_IFACE_INDEX       1
141
142 #ifndef UPLCOM_INTR_INTERVAL
143 #define UPLCOM_INTR_INTERVAL            100     /* ms */
144 #endif
145
146 #define UPLCOM_SET_REQUEST              0x01
147 #define UPLCOM_SET_CRTSCTS              0x41
148 #define UPLCOM_SET_CRTSCTS_PL2303X      0x61
149 #define RSAQ_STATUS_CTS                 0x80
150 #define RSAQ_STATUS_DSR                 0x02
151 #define RSAQ_STATUS_DCD                 0x01
152
153 #define TYPE_PL2303                     0
154 #define TYPE_PL2303X                    1
155
156 struct  uplcom_softc {
157         struct ucom_softc       sc_ucom;
158
159         int                     sc_iface_number;        /* interface number */
160
161         usbd_interface_handle   sc_intr_iface;  /* interrupt interface */
162         int                     sc_intr_number; /* interrupt number */
163         usbd_pipe_handle        sc_intr_pipe;   /* interrupt pipe */
164         u_char                  *sc_intr_buf;   /* interrupt buffer */
165         int                     sc_isize;
166
167         usb_cdc_line_state_t    sc_line_state;  /* current line state */
168         u_char                  sc_dtr;         /* current DTR state */
169         u_char                  sc_rts;         /* current RTS state */
170         u_char                  sc_status;
171
172         u_char                  sc_lsr;         /* Local status register */
173         u_char                  sc_msr;         /* uplcom status register */
174
175         int                     sc_chiptype;    /* Type of chip */
176
177         struct task             sc_task;
178 };
179
180 /*
181  * These are the maximum number of bytes transferred per frame.
182  * The output buffer size cannot be increased due to the size encoding.
183  */
184 #define UPLCOMIBUFSIZE 256
185 #define UPLCOMOBUFSIZE 256
186
187 static  usbd_status uplcom_reset(struct uplcom_softc *);
188 static  usbd_status uplcom_set_line_coding(struct uplcom_softc *,
189                                            usb_cdc_line_state_t *);
190 static  usbd_status uplcom_set_crtscts(struct uplcom_softc *);
191 static  void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
192
193 static  void uplcom_set(void *, int, int, int);
194 static  void uplcom_dtr(struct uplcom_softc *, int);
195 static  void uplcom_rts(struct uplcom_softc *, int);
196 static  void uplcom_break(struct uplcom_softc *, int);
197 static  void uplcom_set_line_state(struct uplcom_softc *);
198 static  void uplcom_get_status(void *, int, u_char *, u_char *);
199 static  int  uplcom_param(void *, int, struct termios *);
200 static  int  uplcom_open(void *, int);
201 static  void uplcom_close(void *, int);
202 static  void uplcom_notify(void *, int);
203
204 struct ucom_callback uplcom_callback = {
205         uplcom_get_status,
206         uplcom_set,
207         uplcom_param,
208         NULL,
209         uplcom_open,
210         uplcom_close,
211         NULL,
212         NULL
213 };
214
215 static const struct uplcom_product {
216         uint16_t        vendor;
217         uint16_t        product;
218         int32_t         release;         /* release is a 16bit entity,
219                                           * if -1 is specified we "don't care"
220                                           * This is a floor value.  The table
221                                           * must have newer revs before older
222                                           * revs (and -1 last).
223                                           */
224         char            chiptype;
225 } uplcom_products [] = {
226         { USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, -1, TYPE_PL2303 },
227
228         /* I/O DATA USB-RSAQ */
229         { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, -1, TYPE_PL2303 },
230         /* Prolific Pharos */
231         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, -1, TYPE_PL2303 },
232         /* I/O DATA USB-RSAQ2 */
233         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, -1, TYPE_PL2303 },
234         /* I/O DATA USB-RSAQ3 */
235         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, -1, TYPE_PL2303X },
236         /* Willcom W-SIM*/
237         { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, -1, TYPE_PL2303X},
238         /* PLANEX USB-RS232 URS-03 */
239         { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, -1, TYPE_PL2303 },
240         /* TrendNet TU-S9 */
241         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303,
242           0x400, TYPE_PL2303X },
243         /* ST Lab USB-SERIAL-4 */
244         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303,
245           0x300, TYPE_PL2303X },
246         /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
247         { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, -1, TYPE_PL2303 },
248         /* TDK USB-PHS Adapter UHA6400 */
249         { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, -1, TYPE_PL2303 },
250         /* RATOC REX-USB60 */
251         { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, -1, TYPE_PL2303 },
252         /* ELECOM UC-SGT */
253         { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, -1, TYPE_PL2303 },
254         { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, -1, TYPE_PL2303 },
255         /* Sagem USB-Serial Controller */
256         { USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, -1, TYPE_PL2303X },
257         /* Sony Ericsson USB Cable */
258         { USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10,
259           -1,TYPE_PL2303 },
260         /* SOURCENEXT KeikaiDenwa 8 */
261         { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8,
262           -1, TYPE_PL2303 },
263         /* SOURCENEXT KeikaiDenwa 8 with charger */
264         { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG,
265           -1, TYPE_PL2303 },
266         /* HAL Corporation Crossam2+USB */
267         { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, -1, TYPE_PL2303 },
268         /* Sitecom USB to Serial */
269         { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, -1, TYPE_PL2303 },
270         /* Tripp-Lite U209-000-R */
271         { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, -1, TYPE_PL2303X },
272         /* Belkin F5U257 */
273         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, -1, TYPE_PL2303X },
274         { 0, 0 }
275 };
276
277 static device_probe_t uplcom_match;
278 static device_attach_t uplcom_attach;
279 static device_detach_t uplcom_detach;
280
281 static device_method_t uplcom_methods[] = {
282         /* Device interface */
283         DEVMETHOD(device_probe, uplcom_match),
284         DEVMETHOD(device_attach, uplcom_attach),
285         DEVMETHOD(device_detach, uplcom_detach),
286         { 0, 0 }
287 };
288
289 static driver_t uplcom_driver = {
290         "ucom",
291         uplcom_methods,
292         sizeof (struct uplcom_softc)
293 };
294
295 DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
296 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
297 MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
298 MODULE_VERSION(uplcom, UPLCOM_MODVER);
299
300 static int      uplcominterval = UPLCOM_INTR_INTERVAL;
301
302 static int
303 sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
304 {
305         int err, val;
306
307         val = uplcominterval;
308         err = sysctl_handle_int(oidp, &val, 0, req);
309         if (err != 0 || req->newptr == NULL)
310                 return (err);
311         if (0 < val && val <= 1000)
312                 uplcominterval = val;
313         else
314                 err = EINVAL;
315
316         return (err);
317 }
318
319 SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
320             0, sizeof(int), sysctl_hw_usb_uplcom_interval,
321             "I", "uplcom interrupt pipe interval");
322
323 static int
324 uplcom_match(device_t self)
325 {
326         struct usb_attach_arg *uaa = device_get_ivars(self);
327         int i;
328
329         if (uaa->iface != NULL)
330                 return (UMATCH_NONE);
331
332         for (i = 0; uplcom_products[i].vendor != 0; i++) {
333                 if (uplcom_products[i].vendor == uaa->vendor &&
334                     uplcom_products[i].product == uaa->product &&
335                     (uplcom_products[i].release <= uaa->release ||
336                      uplcom_products[i].release == -1)) {
337                         return (UMATCH_VENDOR_PRODUCT);
338                 }
339         }
340         return (UMATCH_NONE);
341 }
342
343 static int
344 uplcom_attach(device_t self)
345 {
346         struct uplcom_softc *sc = device_get_softc(self);
347         struct usb_attach_arg *uaa = device_get_ivars(self);
348         usbd_device_handle dev = uaa->device;
349         struct ucom_softc *ucom;
350         usb_config_descriptor_t *cdesc;
351         usb_interface_descriptor_t *id;
352         usb_endpoint_descriptor_t *ed;
353         const char *devname;
354         usbd_status err;
355         int i;
356
357         ucom = &sc->sc_ucom;
358         ucom->sc_dev = self;
359         ucom->sc_udev = dev;
360         ucom->sc_iface = uaa->iface;
361
362         devname = device_get_nameunit(ucom->sc_dev);
363
364         DPRINTF(("uplcom attach: sc = %p\n", sc));
365
366         /* determine chip type */
367         for (i = 0; uplcom_products[i].vendor != 0; i++) {
368                 if (uplcom_products[i].vendor == uaa->vendor &&
369                     uplcom_products[i].product == uaa->product &&
370                     (uplcom_products[i].release == uaa->release ||
371                      uplcom_products[i].release == -1)) {
372                         sc->sc_chiptype = uplcom_products[i].chiptype;
373                         break;
374                 }
375         }
376
377         /*
378          * check we found the device - attach should have ensured we
379          * don't get here without matching device
380          */
381         if (uplcom_products[i].vendor == 0) {
382                 printf("%s: didn't match\n", devname);
383                 ucom->sc_dying = 1;
384                 goto error;
385         }
386
387 #ifdef USB_DEBUG
388         /* print the chip type */
389         if (sc->sc_chiptype == TYPE_PL2303X) {
390                 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
391         } else {
392                 DPRINTF(("uplcom_attach: chiptype 2303\n"));
393         }
394 #endif
395
396         /* initialize endpoints */
397         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
398         sc->sc_intr_number = -1;
399         sc->sc_intr_pipe = NULL;
400
401         /* Move the device into the configured state. */
402         err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
403         if (err) {
404                 printf("%s: failed to set configuration: %s\n",
405                         devname, usbd_errstr(err));
406                 ucom->sc_dying = 1;
407                 goto error;
408         }
409
410         /* get the config descriptor */
411         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
412
413         if (cdesc == NULL) {
414                 printf("%s: failed to get configuration descriptor\n",
415                         device_get_nameunit(ucom->sc_dev));
416                 ucom->sc_dying = 1;
417                 goto error;
418         }
419
420         /* get the (first/common) interface */
421         err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
422                                            &ucom->sc_iface);
423         if (err) {
424                 printf("%s: failed to get interface: %s\n",
425                         devname, usbd_errstr(err));
426                 ucom->sc_dying = 1;
427                 goto error;
428         }
429
430         /* Find the interrupt endpoints */
431
432         id = usbd_get_interface_descriptor(ucom->sc_iface);
433         sc->sc_iface_number = id->bInterfaceNumber;
434
435         for (i = 0; i < id->bNumEndpoints; i++) {
436                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
437                 if (ed == NULL) {
438                         printf("%s: no endpoint descriptor for %d\n",
439                                 device_get_nameunit(ucom->sc_dev), i);
440                         ucom->sc_dying = 1;
441                         goto error;
442                 }
443
444                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
445                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
446                         sc->sc_intr_number = ed->bEndpointAddress;
447                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
448                 }
449         }
450
451         if (sc->sc_intr_number == -1) {
452                 printf("%s: Could not find interrupt in\n",
453                         device_get_nameunit(ucom->sc_dev));
454                 ucom->sc_dying = 1;
455                 goto error;
456         }
457
458         /* keep interface for interrupt */
459         sc->sc_intr_iface = ucom->sc_iface;
460
461         /*
462          * USB-RSAQ1 has two interface
463          *
464          *  USB-RSAQ1       | USB-RSAQ2
465          * -----------------+-----------------
466          * Interface 0      |Interface 0
467          *  Interrupt(0x81) | Interrupt(0x81)
468          * -----------------+ BulkIN(0x02)
469          * Interface 1      | BulkOUT(0x83)
470          *   BulkIN(0x02)   |
471          *   BulkOUT(0x83)  |
472          */
473         if (cdesc->bNumInterface == 2) {
474                 err = usbd_device2interface_handle(dev,
475                                                    UPLCOM_SECOND_IFACE_INDEX,
476                                                    &ucom->sc_iface);
477                 if (err) {
478                         printf("%s: failed to get second interface: %s\n",
479                                 devname, usbd_errstr(err));
480                         ucom->sc_dying = 1;
481                         goto error;
482                 }
483         }
484
485         /* Find the bulk{in,out} endpoints */
486
487         id = usbd_get_interface_descriptor(ucom->sc_iface);
488         sc->sc_iface_number = id->bInterfaceNumber;
489
490         for (i = 0; i < id->bNumEndpoints; i++) {
491                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
492                 if (ed == NULL) {
493                         printf("%s: no endpoint descriptor for %d\n",
494                                 device_get_nameunit(ucom->sc_dev), i);
495                         ucom->sc_dying = 1;
496                         goto error;
497                 }
498
499                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
500                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
501                         ucom->sc_bulkin_no = ed->bEndpointAddress;
502                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
503                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
504                         ucom->sc_bulkout_no = ed->bEndpointAddress;
505                 }
506         }
507
508         if (ucom->sc_bulkin_no == -1) {
509                 printf("%s: Could not find data bulk in\n",
510                         device_get_nameunit(ucom->sc_dev));
511                 ucom->sc_dying = 1;
512                 goto error;
513         }
514
515         if (ucom->sc_bulkout_no == -1) {
516                 printf("%s: Could not find data bulk out\n",
517                         device_get_nameunit(ucom->sc_dev));
518                 ucom->sc_dying = 1;
519                 goto error;
520         }
521
522         sc->sc_dtr = sc->sc_rts = -1;
523         ucom->sc_parent = sc;
524         ucom->sc_portno = UCOM_UNK_PORTNO;
525         /* bulkin, bulkout set above */
526         ucom->sc_ibufsize = UPLCOMIBUFSIZE;
527         ucom->sc_obufsize = UPLCOMOBUFSIZE;
528         ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
529         ucom->sc_opkthdrlen = 0;
530         ucom->sc_callback = &uplcom_callback;
531
532         err = uplcom_reset(sc);
533
534         if (err) {
535                 printf("%s: reset failed: %s\n",
536                        device_get_nameunit(ucom->sc_dev), usbd_errstr(err));
537                 ucom->sc_dying = 1;
538                 goto error;
539         }
540
541         DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
542                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
543
544         TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
545         ucom_attach(&sc->sc_ucom);
546         return 0;
547
548 error:
549         return ENXIO;
550 }
551
552 static int
553 uplcom_detach(device_t self)
554 {
555         struct uplcom_softc *sc = device_get_softc(self);
556         int rv = 0;
557
558         DPRINTF(("uplcom_detach: sc = %p\n", sc));
559
560         if (sc->sc_intr_pipe != NULL) {
561                 usbd_abort_pipe(sc->sc_intr_pipe);
562                 usbd_close_pipe(sc->sc_intr_pipe);
563                 free(sc->sc_intr_buf, M_USBDEV);
564                 sc->sc_intr_pipe = NULL;
565         }
566
567         sc->sc_ucom.sc_dying = 1;
568
569         rv = ucom_detach(&sc->sc_ucom);
570
571         return (rv);
572 }
573
574 static usbd_status
575 uplcom_reset(struct uplcom_softc *sc)
576 {
577         usb_device_request_t req;
578         usbd_status err;
579
580         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
581         req.bRequest = UPLCOM_SET_REQUEST;
582         USETW(req.wValue, 0);
583         USETW(req.wIndex, sc->sc_iface_number);
584         USETW(req.wLength, 0);
585
586         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
587         if (err) {
588                 printf("%s: uplcom_reset: %s\n",
589                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
590                 return (EIO);
591         }
592
593         return (0);
594 }
595
596 struct pl2303x_init {
597         uint8_t         req_type;
598         uint8_t         request;
599         uint16_t        value;
600         uint16_t        index;
601         uint16_t        length;
602 };
603
604 static const struct pl2303x_init pl2303x[] = {
605         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
606         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0, 0 },
607         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
608         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
609         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
610         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1, 0 },
611         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
612         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
613         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1, 0 },
614         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0, 0 },
615         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44, 0 }
616 };
617 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
618
619 static usbd_status
620 uplcom_pl2303x_init(struct uplcom_softc *sc)
621 {
622         usb_device_request_t req;
623         usbd_status err;
624         int i;
625
626         for (i = 0; i < N_PL2302X_INIT; i++) {
627                 req.bmRequestType = pl2303x[i].req_type;
628                 req.bRequest = pl2303x[i].request;
629                 USETW(req.wValue, pl2303x[i].value);
630                 USETW(req.wIndex, pl2303x[i].index);
631                 USETW(req.wLength, pl2303x[i].length);
632
633                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
634                 if (err) {
635                         printf("%s: uplcom_pl2303x_init: %d: %s\n",
636                                 device_get_nameunit(sc->sc_ucom.sc_dev), i,
637                                 usbd_errstr(err));
638                         return (EIO);
639                 }
640         }
641
642         return (0);
643 }
644
645 static void
646 uplcom_set_line_state(struct uplcom_softc *sc)
647 {
648         usb_device_request_t req;
649         int ls;
650         usbd_status err;
651
652         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
653                 (sc->sc_rts ? UCDC_LINE_RTS : 0);
654         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
655         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
656         USETW(req.wValue, ls);
657         USETW(req.wIndex, sc->sc_iface_number);
658         USETW(req.wLength, 0);
659
660         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
661         if (err)
662                 printf("%s: uplcom_set_line_status: %s\n",
663                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
664 }
665
666 static void
667 uplcom_set(void *addr, int portno, int reg, int onoff)
668 {
669         struct uplcom_softc *sc = addr;
670
671         switch (reg) {
672         case UCOM_SET_DTR:
673                 uplcom_dtr(sc, onoff);
674                 break;
675         case UCOM_SET_RTS:
676                 uplcom_rts(sc, onoff);
677                 break;
678         case UCOM_SET_BREAK:
679                 uplcom_break(sc, onoff);
680                 break;
681         default:
682                 break;
683         }
684 }
685
686 static void
687 uplcom_dtr(struct uplcom_softc *sc, int onoff)
688 {
689         DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
690
691         if (sc->sc_dtr == onoff)
692                 return;
693         sc->sc_dtr = onoff;
694
695         uplcom_set_line_state(sc);
696 }
697
698 static void
699 uplcom_rts(struct uplcom_softc *sc, int onoff)
700 {
701         DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
702
703         if (sc->sc_rts == onoff)
704                 return;
705         sc->sc_rts = onoff;
706
707         uplcom_set_line_state(sc);
708 }
709
710 static void
711 uplcom_break(struct uplcom_softc *sc, int onoff)
712 {
713         usb_device_request_t req;
714         usbd_status err;
715
716         DPRINTF(("uplcom_break: onoff = %d\n", onoff));
717
718         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
719         req.bRequest = UCDC_SEND_BREAK;
720         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
721         USETW(req.wIndex, sc->sc_iface_number);
722         USETW(req.wLength, 0);
723
724         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
725         if (err)
726                 printf("%s: uplcom_break: %s\n",
727                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
728 }
729
730 static usbd_status
731 uplcom_set_crtscts(struct uplcom_softc *sc)
732 {
733         usb_device_request_t req;
734         usbd_status err;
735
736         DPRINTF(("uplcom_set_crtscts: on\n"));
737
738         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
739         req.bRequest = UPLCOM_SET_REQUEST;
740         USETW(req.wValue, 0);
741         if (sc->sc_chiptype == TYPE_PL2303X)
742                 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
743         else
744                 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
745         USETW(req.wLength, 0);
746
747         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
748         if (err) {
749                 printf("%s: uplcom_set_crtscts: %s\n",
750                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
751                 return (err);
752         }
753
754         return (USBD_NORMAL_COMPLETION);
755 }
756
757 static usbd_status
758 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
759 {
760         usb_device_request_t req;
761         usbd_status err;
762
763         DPRINTF((
764 "uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
765                  UGETDW(state->dwDTERate), state->bCharFormat,
766                  state->bParityType, state->bDataBits));
767
768         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
769                 DPRINTF(("uplcom_set_line_coding: already set\n"));
770                 return (USBD_NORMAL_COMPLETION);
771         }
772
773         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
774         req.bRequest = UCDC_SET_LINE_CODING;
775         USETW(req.wValue, 0);
776         USETW(req.wIndex, sc->sc_iface_number);
777         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
778
779         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
780         if (err) {
781                 printf("%s: uplcom_set_line_coding: %s\n",
782                        device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
783                 return (err);
784         }
785
786         sc->sc_line_state = *state;
787
788         return (USBD_NORMAL_COMPLETION);
789 }
790
791 static const int uplcom_rates[] = {
792         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
793         19200, 28800, 38400, 57600, 115200,
794         /*
795          * Higher speeds are probably possible. PL2303X supports up to
796          * 6Mb and can set any rate
797          */
798         230400, 460800, 614400, 921600, 1228800
799 };
800 #define N_UPLCOM_RATES  (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
801
802 static int
803 uplcom_param(void *addr, int portno, struct termios *t)
804 {
805         struct uplcom_softc *sc = addr;
806         usbd_status err;
807         usb_cdc_line_state_t ls;
808         int i;
809
810         DPRINTF(("uplcom_param: sc = %p\n", sc));
811
812         /* Check requested baud rate */
813         for (i = 0; i < N_UPLCOM_RATES; i++)
814                 if (uplcom_rates[i] == t->c_ospeed)
815                         break;
816         if (i == N_UPLCOM_RATES) {
817                 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
818                 return (EIO);
819         }
820
821         USETDW(ls.dwDTERate, t->c_ospeed);
822         if (ISSET(t->c_cflag, CSTOPB))
823                 ls.bCharFormat = UCDC_STOP_BIT_2;
824         else
825                 ls.bCharFormat = UCDC_STOP_BIT_1;
826         if (ISSET(t->c_cflag, PARENB)) {
827                 if (ISSET(t->c_cflag, PARODD))
828                         ls.bParityType = UCDC_PARITY_ODD;
829                 else
830                         ls.bParityType = UCDC_PARITY_EVEN;
831         } else
832                 ls.bParityType = UCDC_PARITY_NONE;
833         switch (ISSET(t->c_cflag, CSIZE)) {
834         case CS5:
835                 ls.bDataBits = 5;
836                 break;
837         case CS6:
838                 ls.bDataBits = 6;
839                 break;
840         case CS7:
841                 ls.bDataBits = 7;
842                 break;
843         case CS8:
844                 ls.bDataBits = 8;
845                 break;
846         }
847
848         err = uplcom_set_line_coding(sc, &ls);
849         if (err)
850                 return (EIO);
851
852         if (ISSET(t->c_cflag, CRTSCTS)) {
853                 err = uplcom_set_crtscts(sc);
854                 if (err)
855                         return (EIO);
856         }
857
858         return (0);
859 }
860
861 static int
862 uplcom_open(void *addr, int portno)
863 {
864         struct uplcom_softc *sc = addr;
865         int err;
866
867         if (sc->sc_ucom.sc_dying)
868                 return (ENXIO);
869
870         DPRINTF(("uplcom_open: sc = %p\n", sc));
871
872         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
873                 sc->sc_status = 0; /* clear status bit */
874                 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
875                 err = usbd_open_pipe_intr(sc->sc_intr_iface,
876                                           sc->sc_intr_number,
877                                           USBD_SHORT_XFER_OK,
878                                           &sc->sc_intr_pipe,
879                                           sc,
880                                           sc->sc_intr_buf,
881                                           sc->sc_isize,
882                                           uplcom_intr,
883                                           uplcominterval);
884                 if (err) {
885                         printf("%s: cannot open interrupt pipe (addr %d)\n",
886                                device_get_nameunit(sc->sc_ucom.sc_dev),
887                                sc->sc_intr_number);
888                         return (EIO);
889                 }
890         }
891
892         if (sc->sc_chiptype == TYPE_PL2303X)
893                 return (uplcom_pl2303x_init(sc));
894
895         return (0);
896 }
897
898 static void
899 uplcom_close(void *addr, int portno)
900 {
901         struct uplcom_softc *sc = addr;
902         int err;
903
904         if (sc->sc_ucom.sc_dying)
905                 return;
906
907         DPRINTF(("uplcom_close: close\n"));
908
909         if (sc->sc_intr_pipe != NULL) {
910                 err = usbd_abort_pipe(sc->sc_intr_pipe);
911                 if (err)
912                         printf("%s: abort interrupt pipe failed: %s\n",
913                                device_get_nameunit(sc->sc_ucom.sc_dev),
914                                usbd_errstr(err));
915                 err = usbd_close_pipe(sc->sc_intr_pipe);
916                 if (err)
917                         printf("%s: close interrupt pipe failed: %s\n",
918                                device_get_nameunit(sc->sc_ucom.sc_dev),
919                                usbd_errstr(err));
920                 free(sc->sc_intr_buf, M_USBDEV);
921                 sc->sc_intr_pipe = NULL;
922         }
923 }
924
925 static void
926 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
927 {
928         struct uplcom_softc *sc = priv;
929         u_char *buf = sc->sc_intr_buf;
930         u_char pstatus;
931
932         if (sc->sc_ucom.sc_dying)
933                 return;
934
935         if (status != USBD_NORMAL_COMPLETION) {
936                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
937                         return;
938
939                 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
940                         device_get_nameunit(sc->sc_ucom.sc_dev),
941                         usbd_errstr(status)));
942                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
943                 return;
944         }
945
946         DPRINTF(("%s: uplcom status = %02x\n",
947                  device_get_nameunit(sc->sc_ucom.sc_dev), buf[8]));
948
949         sc->sc_lsr = sc->sc_msr = 0;
950         pstatus = buf[8];
951         if (ISSET(pstatus, RSAQ_STATUS_CTS))
952                 sc->sc_msr |= SER_CTS;
953         else
954                 sc->sc_msr &= ~SER_CTS;
955         if (ISSET(pstatus, RSAQ_STATUS_DSR))
956                 sc->sc_msr |= SER_DSR;
957         else
958                 sc->sc_msr &= ~SER_DSR;
959         if (ISSET(pstatus, RSAQ_STATUS_DCD))
960                 sc->sc_msr |= SER_DCD;
961         else
962                 sc->sc_msr &= ~SER_DCD;
963
964         /* Deferred notifying to the ucom layer */
965         taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
966 }
967
968 static void
969 uplcom_notify(void *arg, int count)
970 {
971         struct uplcom_softc *sc;
972
973         sc = (struct uplcom_softc *)arg;
974         if (sc->sc_ucom.sc_dying)
975                 return;
976         ucom_status_change(&sc->sc_ucom);
977 }
978
979 static void
980 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
981 {
982         struct uplcom_softc *sc = addr;
983
984         DPRINTF(("uplcom_get_status:\n"));
985
986         if (lsr != NULL)
987                 *lsr = sc->sc_lsr;
988         if (msr != NULL)
989                 *msr = sc->sc_msr;
990 }