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