Merge branch 'vendor/XZ' into HEAD
[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.26 2008/01/13 16:04:02 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/fcntl.h>
93 #include <sys/conf.h>
94 #include <sys/tty.h>
95 #include <sys/file.h>
96 #include <sys/select.h>
97 #include <sys/proc.h>
98 #include <sys/poll.h>
99 #include <sys/sysctl.h>
100 #include <sys/taskqueue.h>
101
102 #include <bus/usb/usb.h>
103 #include <bus/usb/usbcdc.h>
104
105 #include <bus/usb/usbdi.h>
106 #include <bus/usb/usbdi_util.h>
107 #include <bus/usb/usb_quirks.h>
108
109 #include "../ucom/ucomvar.h"
110
111 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
112 #ifdef USB_DEBUG
113 static int      uplcomdebug = 0;
114 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
115            &uplcomdebug, 0, "uplcom debug level");
116
117 #define DPRINTFN(n, x)  do { \
118                                 if (uplcomdebug > (n)) \
119                                         kprintf x; \
120                         } while (0)
121 #else
122 #define DPRINTFN(n, x)
123 #endif
124 #define DPRINTF(x) DPRINTFN(0, x)
125
126 #define UPLCOM_MODVER                   1       /* module version */
127
128 #define UPLCOM_CONFIG_INDEX             0
129 #define UPLCOM_IFACE_INDEX              0
130 #define UPLCOM_SECOND_IFACE_INDEX       1
131
132 #ifndef UPLCOM_INTR_INTERVAL
133 #define UPLCOM_INTR_INTERVAL            100     /* ms */
134 #endif
135
136 #define UPLCOM_SET_REQUEST              0x01
137 #define UPLCOM_SET_CRTSCTS              0x41
138 #define UPLCOM_SET_CRTSCTS_PL2303X      0x61
139 #define RSAQ_STATUS_CTS                 0x80
140 #define RSAQ_STATUS_DSR                 0x02
141 #define RSAQ_STATUS_DCD                 0x01
142
143 #define TYPE_PL2303                     0
144 #define TYPE_PL2303X                    1
145
146 struct  uplcom_softc {
147         struct ucom_softc       sc_ucom;
148
149         int                     sc_iface_number;        /* interface number */
150
151         usbd_interface_handle   sc_intr_iface;  /* interrupt interface */
152         int                     sc_intr_number; /* interrupt number */
153         usbd_pipe_handle        sc_intr_pipe;   /* interrupt pipe */
154         u_char                  *sc_intr_buf;   /* interrupt buffer */
155         int                     sc_isize;
156
157         usb_cdc_line_state_t    sc_line_state;  /* current line state */
158         u_char                  sc_dtr;         /* current DTR state */
159         u_char                  sc_rts;         /* current RTS state */
160         u_char                  sc_status;
161
162         u_char                  sc_lsr;         /* Local status register */
163         u_char                  sc_msr;         /* uplcom status register */
164
165         int                     sc_chiptype;    /* Type of chip */
166
167         struct task             sc_task;
168 };
169
170 /*
171  * These are the maximum number of bytes transferred per frame.
172  * The output buffer size cannot be increased due to the size encoding.
173  */
174 #define UPLCOMIBUFSIZE 256
175 #define UPLCOMOBUFSIZE 256
176
177 static  usbd_status uplcom_reset(struct uplcom_softc *);
178 static  usbd_status uplcom_set_line_coding(struct uplcom_softc *,
179                                            usb_cdc_line_state_t *);
180 static  usbd_status uplcom_set_crtscts(struct uplcom_softc *);
181 static  void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
182
183 static  void uplcom_set(void *, int, int, int);
184 static  void uplcom_dtr(struct uplcom_softc *, int);
185 static  void uplcom_rts(struct uplcom_softc *, int);
186 static  void uplcom_break(struct uplcom_softc *, int);
187 static  void uplcom_set_line_state(struct uplcom_softc *);
188 static  void uplcom_get_status(void *, int, u_char *, u_char *);
189 #if 0 /* TODO */
190 static  int  uplcom_ioctl(void *, int, u_long, caddr_t, int, struct thread *);
191 #endif
192 static  int  uplcom_param(void *, int, struct termios *);
193 static  int  uplcom_open(void *, int);
194 static  void uplcom_close(void *, int);
195 static  void uplcom_notify(void *, int);
196
197 struct ucom_callback uplcom_callback = {
198         uplcom_get_status,
199         uplcom_set,
200         uplcom_param,
201         NULL, /* uplcom_ioctl, TODO */
202         uplcom_open,
203         uplcom_close,
204         NULL,
205         NULL
206 };
207
208 static const struct usb_devno uplcom_devs[] = {
209         { USB_DEVICE(0x0413, 0x2101) }, /* Leadtek 9531 GPS */
210         { USB_DEVICE(0x0421, 0x1234) }, /* Nokia CA-42 USB data cable clones*/
211         { USB_DEVICE(0x04bb, 0x0a03) }, /* I/O DATA USB-RSAQ USB to serial */
212         { USB_DEVICE(0x04bb, 0x0a0e) }, /* I/O DATA USB-RSAQ5 USB to serial */
213         { USB_DEVICE(0x04bf, 0x0115) }, /* TDK USB-PDC adapter UPA9664 */
214         { USB_DEVICE(0x04bf, 0x0117) }, /* TDK USB-PHS adapter UHA6400 */
215         { USB_DEVICE(0x04da, 0x3900) }, /* Panasonic 50" touch panel */
216         { USB_DEVICE(0x04e8, 0x8001) }, /* Samsung I330 smartphone cradle */
217         { USB_DEVICE(0x050d, 0x0257) }, /* Belkin F5U257 USB to serial */
218         { USB_DEVICE(0x0547, 0x2008) }, /* Anchor serial */
219         { USB_DEVICE(0x0557, 0x2008) }, /* Aten UC232A USB to serial */
220         { USB_DEVICE(0x056e, 0x5003) }, /* ELECOM UC-SGT USB to serial */
221         { USB_DEVICE(0x056e, 0x5004) }, /* ELECOM UC-SGT USB to serial */
222         { USB_DEVICE(0x0584, 0xb000) }, /* RATOC REX-USB60 USB to serial */
223         { USB_DEVICE(0x058f, 0x9720) }, /* Alcor AU9720 USB to serial */
224         { USB_DEVICE(0x067b, 0x04bb) }, /* I/O DATA USB-RSAQ2 USB to serial */
225         { USB_DEVICE(0x067b, 0x1234) }, /* Unbranded DCU-11 clone */
226         { USB_DEVICE(0x067b, 0x2303) }, /* IOGEAR/ATEN UC-232A, ST Lab
227                                            USB-SERIAL-X etc */
228         { USB_DEVICE(0x067b, 0xaaa0) }, /* Microsoft OEM Pharos 360 GPS */
229         { USB_DEVICE(0x067b, 0xaaa2) }, /* I/O DATA USB-RSAQ3 USB to serial */
230         { USB_DEVICE(0x0731, 0x0528) }, /* Sony-Ericsson DCU-10/DCU-11 */
231         { USB_DEVICE(0x0731, 0x2003) }, /* Susteen Datapilot Universal-2 Phone
232                                            Cable */
233         { USB_DEVICE(0x0745, 0x0001) }, /* Syntech CPT-8001C barcode scanner
234                                            USB cradle */
235         { USB_DEVICE(0x079b, 0x0027) }, /* Sagem USB data cables */
236         { USB_DEVICE(0x0833, 0x012e) }, /* SOURCENEXT KeikaiDenwa 8 with
237                                            charger */
238         { USB_DEVICE(0x0833, 0x039f) }, /* SOURCENEXT KeikaiDenwa 8 */
239         { USB_DEVICE(0x0b41, 0x0011) }, /* HAL Corporation Crossam2+USB */
240         { USB_DEVICE(0x0b8c, 0x2303) }, /* Smart Technologies USB to serial */
241         { USB_DEVICE(0x0df7, 0x0620) }, /* Mobile Action MA-620 IrDA */
242         { USB_DEVICE(0x0e55, 0x110b) }, /* Speed Dragon MS3303H */
243         { USB_DEVICE(0x0eba, 0x1080) }, /* Itegno GSM/GPRS modem */
244         { USB_DEVICE(0x0eba, 0x2080) }, /* Itegno CDMA 1x card */
245         { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 USB data cable clone */
246         { USB_DEVICE(0x11f5, 0x0001) }, /* Siemens SX1 cellphone */
247         { USB_DEVICE(0x11f5, 0x0003) }, /* Siemens x65 series cellphones */
248         { USB_DEVICE(0x11f5, 0x0004) }, /* Siemens x75 series cellphones */
249         { USB_DEVICE(0x11f6, 0x2001) }, /* Willcom W-SIM */
250         { USB_DEVICE(0x11f7, 0x02df) }, /* Alcatel One Touch 535/735 phones */
251         { USB_DEVICE(0x1453, 0x4026) }, /* RADIOSHACK USB cable */
252         { USB_DEVICE(0x2478, 0x2008) }, /* Tripp-Lite U209-000-R */
253         { USB_DEVICE(0x6189, 0x2068) }, /* Sitecom USB to serial cable */
254 };
255
256 static device_probe_t uplcom_match;
257 static device_attach_t uplcom_attach;
258 static device_detach_t uplcom_detach;
259
260 static device_method_t uplcom_methods[] = {
261         /* Device interface */
262         DEVMETHOD(device_probe, uplcom_match),
263         DEVMETHOD(device_attach, uplcom_attach),
264         DEVMETHOD(device_detach, uplcom_detach),
265         { 0, 0 }
266 };
267
268 static driver_t uplcom_driver = {
269         "ucom",
270         uplcom_methods,
271         sizeof (struct uplcom_softc)
272 };
273
274 DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
275 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
276 MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
277 MODULE_VERSION(uplcom, UPLCOM_MODVER);
278
279 static int      uplcominterval = UPLCOM_INTR_INTERVAL;
280
281 static int
282 sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
283 {
284         int err, val;
285
286         val = uplcominterval;
287         err = sysctl_handle_int(oidp, &val, sizeof(val), req);
288         if (err != 0 || req->newptr == NULL)
289                 return (err);
290         if (0 < val && val <= 1000)
291                 uplcominterval = val;
292         else
293                 err = EINVAL;
294
295         return (err);
296 }
297
298 SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
299             0, sizeof(int), sysctl_hw_usb_uplcom_interval,
300             "I", "uplcom interrupt pipe interval");
301
302 static int
303 uplcom_match(device_t self)
304 {
305         struct usb_attach_arg *uaa = device_get_ivars(self);
306
307         if (uaa->iface != NULL)
308                 return (UMATCH_NONE);
309
310         return (usb_lookup(uplcom_devs, uaa->vendor, uaa->product) != NULL ?
311                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
312 }
313
314 static int
315 uplcom_attach(device_t self)
316 {
317         struct uplcom_softc *sc = device_get_softc(self);
318         struct usb_attach_arg *uaa = device_get_ivars(self);
319         usbd_device_handle dev = uaa->device;
320         usb_device_descriptor_t *dd;
321         struct ucom_softc *ucom;
322         usb_config_descriptor_t *cdesc;
323         usb_interface_descriptor_t *id;
324         usb_endpoint_descriptor_t *ed;
325         usbd_status err;
326         int i;
327
328         ucom = &sc->sc_ucom;
329         bzero(sc, sizeof (struct uplcom_softc));
330
331         ucom->sc_dev = self;
332         ucom->sc_udev = dev;
333         ucom->sc_iface = uaa->iface;
334
335         DPRINTF(("uplcom attach: sc = %p\n", sc));
336
337         dd = usbd_get_device_descriptor(uaa->device);
338
339         if (!dd)
340                 goto error;
341
342         /*
343          * Determine chip type with algorithm sequence taken from the
344          * Linux driver as I'm not aware of any better method. Device
345          * release number in chip could be (and in fact is in many cases)
346          * replaced by the contents of external EEPROM etc.
347          */
348         else if (dd->bDeviceClass == 0x02)
349                 sc->sc_chiptype = TYPE_PL2303;
350         else if (dd->bMaxPacketSize == 0x40)
351                 sc->sc_chiptype = TYPE_PL2303X;
352         else
353                 sc->sc_chiptype = TYPE_PL2303;
354
355 #ifdef USB_DEBUG
356         /* print the chip type */
357         if (sc->sc_chiptype == TYPE_PL2303X) {
358                 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
359         } else {
360                 DPRINTF(("uplcom_attach: chiptype 2303\n"));
361         }
362 #endif
363
364         /* initialize endpoints */
365         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
366         sc->sc_intr_number = -1;
367         sc->sc_intr_pipe = NULL;
368
369         /* Move the device into the configured state. */
370         err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
371         if (err) {
372                 device_printf(ucom->sc_dev, "failed to set configuration: %s\n",
373                               usbd_errstr(err));
374                 ucom->sc_dying = 1;
375                 goto error;
376         }
377
378         /* get the config descriptor */
379         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
380
381         if (cdesc == NULL) {
382                 device_printf(ucom->sc_dev, "failed to get configuration "
383                               "descriptor\n");
384                 ucom->sc_dying = 1;
385                 goto error;
386         }
387
388         /* get the (first/common) interface */
389         err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
390                                            &ucom->sc_iface);
391         if (err) {
392                 device_printf(ucom->sc_dev, "failed to get interface: %s\n",
393                               usbd_errstr(err));
394                 ucom->sc_dying = 1;
395                 goto error;
396         }
397
398         /* Find the interrupt endpoints */
399
400         id = usbd_get_interface_descriptor(ucom->sc_iface);
401         sc->sc_iface_number = id->bInterfaceNumber;
402
403         for (i = 0; i < id->bNumEndpoints; i++) {
404                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
405                 if (ed == NULL) {
406                         device_printf(ucom->sc_dev, "no endpoint descriptor "
407                                       "for %d\n", i);
408                         ucom->sc_dying = 1;
409                         goto error;
410                 }
411
412                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
413                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
414                         sc->sc_intr_number = ed->bEndpointAddress;
415                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
416                 }
417         }
418
419         if (sc->sc_intr_number == -1) {
420                 device_printf(ucom->sc_dev, "could not find interrupt in\n");
421                 ucom->sc_dying = 1;
422                 goto error;
423         }
424
425         /* keep interface for interrupt */
426         sc->sc_intr_iface = ucom->sc_iface;
427
428         /*
429          * USB-RSAQ1 has two interface
430          *
431          *  USB-RSAQ1       | USB-RSAQ2
432          * -----------------+-----------------
433          * Interface 0      |Interface 0
434          *  Interrupt(0x81) | Interrupt(0x81)
435          * -----------------+ BulkIN(0x02)
436          * Interface 1      | BulkOUT(0x83)
437          *   BulkIN(0x02)   |
438          *   BulkOUT(0x83)  |
439          */
440         if (cdesc->bNumInterface == 2) {
441                 err = usbd_device2interface_handle(dev,
442                                                    UPLCOM_SECOND_IFACE_INDEX,
443                                                    &ucom->sc_iface);
444                 if (err) {
445                         device_printf(ucom->sc_dev, "failed to get second "
446                                       "interface: %s\n", usbd_errstr(err));
447                         ucom->sc_dying = 1;
448                         goto error;
449                 }
450         }
451
452         /* Find the bulk{in,out} endpoints */
453
454         id = usbd_get_interface_descriptor(ucom->sc_iface);
455         sc->sc_iface_number = id->bInterfaceNumber;
456
457         for (i = 0; i < id->bNumEndpoints; i++) {
458                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
459                 if (ed == NULL) {
460                         device_printf(ucom->sc_dev, "no endpoint descriptor "
461                                       "for %d\n", i);
462                         ucom->sc_dying = 1;
463                         goto error;
464                 }
465
466                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
467                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
468                         ucom->sc_bulkin_no = ed->bEndpointAddress;
469                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
470                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
471                         ucom->sc_bulkout_no = ed->bEndpointAddress;
472                 }
473         }
474
475         if (ucom->sc_bulkin_no == -1) {
476                 device_printf(ucom->sc_dev, "could not find data bulk in\n");
477                 ucom->sc_dying = 1;
478                 goto error;
479         }
480
481         if (ucom->sc_bulkout_no == -1) {
482                 device_printf(ucom->sc_dev, "could not find data bulk out\n");
483                 ucom->sc_dying = 1;
484                 goto error;
485         }
486
487         sc->sc_dtr = sc->sc_rts = -1;
488         ucom->sc_parent = sc;
489         ucom->sc_portno = UCOM_UNK_PORTNO;
490         /* bulkin, bulkout set above */
491         ucom->sc_ibufsize = UPLCOMIBUFSIZE;
492         ucom->sc_obufsize = UPLCOMOBUFSIZE;
493         ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
494         ucom->sc_opkthdrlen = 0;
495         ucom->sc_callback = &uplcom_callback;
496
497         err = uplcom_reset(sc);
498
499         if (err) {
500                 device_printf(ucom->sc_dev, "reset failed: %s\n",
501                               usbd_errstr(err));
502                 ucom->sc_dying = 1;
503                 goto error;
504         }
505
506         DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
507                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
508
509         TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
510         ucom_attach(&sc->sc_ucom);
511
512         return 0;
513
514 error:
515         return ENXIO;
516 }
517
518 static int
519 uplcom_detach(device_t self)
520 {
521         struct uplcom_softc *sc = device_get_softc(self);
522         int rv = 0;
523
524         DPRINTF(("uplcom_detach: sc = %p\n", sc));
525
526         if (sc->sc_intr_pipe != NULL) {
527                 usbd_abort_pipe(sc->sc_intr_pipe);
528                 usbd_close_pipe(sc->sc_intr_pipe);
529                 kfree(sc->sc_intr_buf, M_USBDEV);
530                 sc->sc_intr_pipe = NULL;
531         }
532
533         sc->sc_ucom.sc_dying = 1;
534
535         rv = ucom_detach(&sc->sc_ucom);
536
537         return (rv);
538 }
539
540 static usbd_status
541 uplcom_reset(struct uplcom_softc *sc)
542 {
543         usb_device_request_t req;
544         usbd_status err;
545
546         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
547         req.bRequest = UPLCOM_SET_REQUEST;
548         USETW(req.wValue, 0);
549         USETW(req.wIndex, sc->sc_iface_number);
550         USETW(req.wLength, 0);
551
552         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
553         if (err) {
554                 device_printf(sc->sc_ucom.sc_dev, "uplcom_reset: %s\n",
555                               usbd_errstr(err));
556                 return (EIO);
557         }
558
559         return (0);
560 }
561
562 struct pl2303x_init {
563         uint8_t         req_type;
564         uint8_t         request;
565         uint16_t        value;
566         uint16_t        index;
567         uint16_t        length;
568 };
569
570 static const struct pl2303x_init pl2303x[] = {
571         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
572         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0, 0 },
573         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
574         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
575         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
576         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1, 0 },
577         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
578         { UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
579         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1, 0 },
580         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0, 0 },
581         { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44, 0 }
582 };
583 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
584
585 static usbd_status
586 uplcom_pl2303x_init(struct uplcom_softc *sc)
587 {
588         usb_device_request_t req;
589         usbd_status err;
590         int i;
591
592         for (i = 0; i < N_PL2302X_INIT; i++) {
593                 req.bmRequestType = pl2303x[i].req_type;
594                 req.bRequest = pl2303x[i].request;
595                 USETW(req.wValue, pl2303x[i].value);
596                 USETW(req.wIndex, pl2303x[i].index);
597                 USETW(req.wLength, pl2303x[i].length);
598
599                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
600                 if (err) {
601                         device_printf(sc->sc_ucom.sc_dev,
602                                       "uplcom_pl2303x_init: %d: %s\n", i,
603                                       usbd_errstr(err));
604                         return (EIO);
605                 }
606         }
607
608         return (0);
609 }
610
611 static void
612 uplcom_set_line_state(struct uplcom_softc *sc)
613 {
614         usb_device_request_t req;
615         int ls;
616         usbd_status err;
617
618         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
619                 (sc->sc_rts ? UCDC_LINE_RTS : 0);
620         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
621         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
622         USETW(req.wValue, ls);
623         USETW(req.wIndex, sc->sc_iface_number);
624         USETW(req.wLength, 0);
625
626         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
627         if (err)
628                 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_status: "
629                               "%s\n", usbd_errstr(err));
630 }
631
632 static void
633 uplcom_set(void *addr, int portno, int reg, int onoff)
634 {
635         struct uplcom_softc *sc = addr;
636
637         switch (reg) {
638         case UCOM_SET_DTR:
639                 uplcom_dtr(sc, onoff);
640                 break;
641         case UCOM_SET_RTS:
642                 uplcom_rts(sc, onoff);
643                 break;
644         case UCOM_SET_BREAK:
645                 uplcom_break(sc, onoff);
646                 break;
647         default:
648                 break;
649         }
650 }
651
652 static void
653 uplcom_dtr(struct uplcom_softc *sc, int onoff)
654 {
655         DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
656
657         if (sc->sc_dtr == onoff)
658                 return;
659         sc->sc_dtr = onoff;
660
661         uplcom_set_line_state(sc);
662 }
663
664 static void
665 uplcom_rts(struct uplcom_softc *sc, int onoff)
666 {
667         DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
668
669         if (sc->sc_rts == onoff)
670                 return;
671         sc->sc_rts = onoff;
672
673         uplcom_set_line_state(sc);
674 }
675
676 static void
677 uplcom_break(struct uplcom_softc *sc, int onoff)
678 {
679         usb_device_request_t req;
680         usbd_status err;
681
682         DPRINTF(("uplcom_break: onoff = %d\n", onoff));
683
684         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
685         req.bRequest = UCDC_SEND_BREAK;
686         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
687         USETW(req.wIndex, sc->sc_iface_number);
688         USETW(req.wLength, 0);
689
690         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
691         if (err)
692                 device_printf(sc->sc_ucom.sc_dev, "uplcom_break: %s\n",
693                               usbd_errstr(err));
694 }
695
696 static usbd_status
697 uplcom_set_crtscts(struct uplcom_softc *sc)
698 {
699         usb_device_request_t req;
700         usbd_status err;
701
702         DPRINTF(("uplcom_set_crtscts: on\n"));
703
704         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
705         req.bRequest = UPLCOM_SET_REQUEST;
706         USETW(req.wValue, 0);
707         if (sc->sc_chiptype == TYPE_PL2303X)
708                 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
709         else
710                 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
711         USETW(req.wLength, 0);
712
713         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
714         if (err) {
715                 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_crtscts: %s\n",
716                               usbd_errstr(err));
717                 return (err);
718         }
719
720         return (USBD_NORMAL_COMPLETION);
721 }
722
723 static usbd_status
724 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
725 {
726         usb_device_request_t req;
727         usbd_status err;
728
729         DPRINTF((
730 "uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
731                  UGETDW(state->dwDTERate), state->bCharFormat,
732                  state->bParityType, state->bDataBits));
733
734         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
735                 DPRINTF(("uplcom_set_line_coding: already set\n"));
736                 return (USBD_NORMAL_COMPLETION);
737         }
738
739         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
740         req.bRequest = UCDC_SET_LINE_CODING;
741         USETW(req.wValue, 0);
742         USETW(req.wIndex, sc->sc_iface_number);
743         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
744
745         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
746         if (err) {
747                 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_coding: "
748                               "%s\n", usbd_errstr(err));
749                 return (err);
750         }
751
752         sc->sc_line_state = *state;
753
754         return (USBD_NORMAL_COMPLETION);
755 }
756
757 static const int uplcom_rates[] = {
758         75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
759         19200, 28800, 38400, 57600, 115200,
760         /*
761          * Higher speeds are probably possible. PL2303X supports up to
762          * 6Mb and can set any rate
763          */
764         230400, 460800, 614400, 921600, 1228800
765 };
766 #define N_UPLCOM_RATES  (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
767
768 static int
769 uplcom_param(void *addr, int portno, struct termios *t)
770 {
771         struct uplcom_softc *sc = addr;
772         usbd_status err;
773         usb_cdc_line_state_t ls;
774         int i;
775
776         DPRINTF(("uplcom_param: sc = %p\n", sc));
777
778         /* Check requested baud rate */
779         for (i = 0; i < N_UPLCOM_RATES; i++)
780                 if (uplcom_rates[i] == t->c_ospeed)
781                         break;
782         if (i == N_UPLCOM_RATES) {
783                 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
784                 return (EIO);
785         }
786
787         USETDW(ls.dwDTERate, t->c_ospeed);
788         if (ISSET(t->c_cflag, CSTOPB))
789                 ls.bCharFormat = UCDC_STOP_BIT_2;
790         else
791                 ls.bCharFormat = UCDC_STOP_BIT_1;
792         if (ISSET(t->c_cflag, PARENB)) {
793                 if (ISSET(t->c_cflag, PARODD))
794                         ls.bParityType = UCDC_PARITY_ODD;
795                 else
796                         ls.bParityType = UCDC_PARITY_EVEN;
797         } else
798                 ls.bParityType = UCDC_PARITY_NONE;
799         switch (ISSET(t->c_cflag, CSIZE)) {
800         case CS5:
801                 ls.bDataBits = 5;
802                 break;
803         case CS6:
804                 ls.bDataBits = 6;
805                 break;
806         case CS7:
807                 ls.bDataBits = 7;
808                 break;
809         case CS8:
810                 ls.bDataBits = 8;
811                 break;
812         }
813
814         err = uplcom_set_line_coding(sc, &ls);
815         if (err)
816                 return (EIO);
817
818         if (ISSET(t->c_cflag, CRTSCTS)) {
819                 err = uplcom_set_crtscts(sc);
820                 if (err)
821                         return (EIO);
822         }
823
824         return (0);
825 }
826
827 static int
828 uplcom_open(void *addr, int portno)
829 {
830         struct uplcom_softc *sc = addr;
831         int err;
832
833         if (sc->sc_ucom.sc_dying)
834                 return (ENXIO);
835
836         DPRINTF(("uplcom_open: sc = %p\n", sc));
837
838         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
839                 sc->sc_status = 0; /* clear status bit */
840                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
841                 err = usbd_open_pipe_intr(sc->sc_intr_iface,
842                                           sc->sc_intr_number,
843                                           USBD_SHORT_XFER_OK,
844                                           &sc->sc_intr_pipe,
845                                           sc,
846                                           sc->sc_intr_buf,
847                                           sc->sc_isize,
848                                           uplcom_intr,
849                                           uplcominterval);
850                 if (err) {
851                         device_printf(sc->sc_ucom.sc_dev, "cannot open "
852                                       "interrupt pipe (addr %d)\n",
853                                       sc->sc_intr_number);
854                         return (EIO);
855                 }
856         }
857
858         if (sc->sc_chiptype == TYPE_PL2303X)
859                 return (uplcom_pl2303x_init(sc));
860
861         return (0);
862 }
863
864 static void
865 uplcom_close(void *addr, int portno)
866 {
867         struct uplcom_softc *sc = addr;
868         int err;
869
870         if (sc->sc_ucom.sc_dying)
871                 return;
872
873         DPRINTF(("uplcom_close: close\n"));
874
875         if (sc->sc_intr_pipe != NULL) {
876                 err = usbd_abort_pipe(sc->sc_intr_pipe);
877                 if (err)
878                         device_printf(sc->sc_ucom.sc_dev, "abort interrupt "
879                                       "pipe failed: %s\n", usbd_errstr(err));
880                 err = usbd_close_pipe(sc->sc_intr_pipe);
881                 if (err)
882                         device_printf(sc->sc_ucom.sc_dev, "close interrupt "
883                                       "pipe failed: %s\n", usbd_errstr(err));
884                 kfree(sc->sc_intr_buf, M_USBDEV);
885                 sc->sc_intr_pipe = NULL;
886         }
887 }
888
889 static void
890 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
891 {
892         struct uplcom_softc *sc = priv;
893         u_char *buf = sc->sc_intr_buf;
894         u_char pstatus;
895
896         if (sc->sc_ucom.sc_dying)
897                 return;
898
899         if (status != USBD_NORMAL_COMPLETION) {
900                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
901                         return;
902
903                 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
904                         device_get_nameunit(sc->sc_ucom.sc_dev),
905                         usbd_errstr(status)));
906                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
907                 return;
908         }
909
910         DPRINTF(("%s: uplcom status = %02x\n",
911                  device_get_nameunit(sc->sc_ucom.sc_dev), buf[8]));
912
913         sc->sc_lsr = sc->sc_msr = 0;
914         pstatus = buf[8];
915         if (ISSET(pstatus, RSAQ_STATUS_CTS))
916                 sc->sc_msr |= UMSR_CTS;
917         else
918                 sc->sc_msr &= ~UMSR_CTS;
919         if (ISSET(pstatus, RSAQ_STATUS_DSR))
920                 sc->sc_msr |= UMSR_DSR;
921         else
922                 sc->sc_msr &= ~UMSR_DSR;
923         if (ISSET(pstatus, RSAQ_STATUS_DCD))
924                 sc->sc_msr |= UMSR_DCD;
925         else
926                 sc->sc_msr &= ~UMSR_DCD;
927
928         /* Deferred notifying to the ucom layer */
929         taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
930 }
931
932 static void
933 uplcom_notify(void *arg, int count)
934 {
935         struct uplcom_softc *sc;
936
937         sc = (struct uplcom_softc *)arg;
938         if (sc->sc_ucom.sc_dying)
939                 return;
940         ucom_status_change(&sc->sc_ucom);
941 }
942
943 static void
944 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
945 {
946         struct uplcom_softc *sc = addr;
947
948         DPRINTF(("uplcom_get_status:\n"));
949
950         if (lsr != NULL)
951                 *lsr = sc->sc_lsr;
952         if (msr != NULL)
953                 *msr = sc->sc_msr;
954 }
955
956 #if 0 /* TODO */
957 static int
958 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
959              struct thread *p)
960 {
961         struct uplcom_softc *sc = addr;
962         int error = 0;
963
964         if (sc->sc_ucom.sc_dying)
965                 return (EIO);
966
967         DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
968
969         switch (cmd) {
970         case TIOCNOTTY:
971         case TIOCMGET:
972         case TIOCMSET:
973         case USB_GET_CM_OVER_DATA:
974         case USB_SET_CM_OVER_DATA:
975                 break;
976
977         default:
978                 DPRINTF(("uplcom_ioctl: unknown\n"));
979                 error = ENOTTY;
980                 break;
981         }
982
983         return (error);
984 }
985 #endif