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