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