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