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