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