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