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