]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/uslcom.c
Merge ^/vendor/libc++/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / dev / usb / serial / uslcom.c
1 /*      $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
2
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5
6 /*
7  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21
22 /*
23  * Driver for Silicon Laboratories CP2101/CP2102/CP2103/CP2104/CP2105
24  * USB-Serial adapters.  Based on datasheet AN571, publicly available from
25  * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN571.pdf
26  */
27
28 #include <sys/stdint.h>
29 #include <sys/stddef.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/sx.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/priv.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 #include <dev/usb/usb_ioctl.h>
51 #include "usbdevs.h"
52
53 #define USB_DEBUG_VAR uslcom_debug
54 #include <dev/usb/usb_debug.h>
55 #include <dev/usb/usb_process.h>
56
57 #include <dev/usb/serial/usb_serial.h>
58
59 #ifdef USB_DEBUG
60 static int uslcom_debug = 0;
61
62 static SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
63 SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RWTUN,
64     &uslcom_debug, 0, "Debug level");
65 #endif
66
67 #define USLCOM_BULK_BUF_SIZE    1024
68 #define USLCOM_CONFIG_INDEX     0
69
70 /* Request types */
71 #define USLCOM_WRITE            0x41
72 #define USLCOM_READ             0xc1
73
74 /* Request codes */
75 #define USLCOM_IFC_ENABLE       0x00
76 #define USLCOM_SET_BAUDDIV      0x01
77 #define USLCOM_SET_LINE_CTL     0x03
78 #define USLCOM_SET_BREAK        0x05
79 #define USLCOM_SET_MHS          0x07
80 #define USLCOM_GET_MDMSTS       0x08
81 #define USLCOM_SET_FLOW         0x13
82 #define USLCOM_SET_BAUDRATE     0x1e
83 #define USLCOM_VENDOR_SPECIFIC  0xff
84
85 /* USLCOM_IFC_ENABLE values */
86 #define USLCOM_IFC_ENABLE_DIS   0x00
87 #define USLCOM_IFC_ENABLE_EN    0x01
88
89 /* USLCOM_SET_MHS/USLCOM_GET_MDMSTS values */
90 #define USLCOM_MHS_DTR_ON       0x0001
91 #define USLCOM_MHS_DTR_SET      0x0100
92 #define USLCOM_MHS_RTS_ON       0x0002
93 #define USLCOM_MHS_RTS_SET      0x0200
94 #define USLCOM_MHS_CTS          0x0010
95 #define USLCOM_MHS_DSR          0x0020
96 #define USLCOM_MHS_RI           0x0040
97 #define USLCOM_MHS_DCD          0x0080
98
99 /* USLCOM_SET_BAUDDIV values */
100 #define USLCOM_BAUDDIV_REF      3686400 /* 3.6864 MHz */
101
102 /* USLCOM_SET_LINE_CTL values */
103 #define USLCOM_STOP_BITS_1      0x00
104 #define USLCOM_STOP_BITS_2      0x02
105 #define USLCOM_PARITY_NONE      0x00
106 #define USLCOM_PARITY_ODD       0x10
107 #define USLCOM_PARITY_EVEN      0x20
108 #define USLCOM_SET_DATA_BITS(x) ((x) << 8)
109
110 /* USLCOM_SET_BREAK values */
111 #define USLCOM_SET_BREAK_OFF    0x00
112 #define USLCOM_SET_BREAK_ON     0x01
113
114 /* USLCOM_SET_FLOW values - 1st word */
115 #define USLCOM_FLOW_DTR_ON      0x00000001 /* DTR static active */
116 #define USLCOM_FLOW_CTS_HS      0x00000008 /* CTS handshake */
117 /* USLCOM_SET_FLOW values - 2nd word */
118 #define USLCOM_FLOW_RTS_ON      0x00000040 /* RTS static active */
119 #define USLCOM_FLOW_RTS_HS      0x00000080 /* RTS handshake */
120
121 /* USLCOM_VENDOR_SPECIFIC values */
122 #define USLCOM_GET_PARTNUM      0x370B
123 #define USLCOM_WRITE_LATCH      0x37E1
124 #define USLCOM_READ_LATCH       0x00C2
125
126 /* USLCOM_GET_PARTNUM values from hardware */
127 #define USLCOM_PARTNUM_CP2101   1
128 #define USLCOM_PARTNUM_CP2102   2
129 #define USLCOM_PARTNUM_CP2103   3
130 #define USLCOM_PARTNUM_CP2104   4
131 #define USLCOM_PARTNUM_CP2105   5
132
133 enum {
134         USLCOM_BULK_DT_WR,
135         USLCOM_BULK_DT_RD,
136         USLCOM_CTRL_DT_RD,
137         USLCOM_N_TRANSFER,
138 };
139
140 struct uslcom_softc {
141         struct ucom_super_softc sc_super_ucom;
142         struct ucom_softc sc_ucom;
143         struct usb_callout sc_watchdog;
144
145         struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
146         struct usb_device *sc_udev;
147         struct mtx sc_mtx;
148
149         uint8_t sc_msr;
150         uint8_t sc_lsr;
151         uint8_t sc_iface_no;
152         uint8_t sc_partnum;
153 };
154
155 static device_probe_t uslcom_probe;
156 static device_attach_t uslcom_attach;
157 static device_detach_t uslcom_detach;
158 static void uslcom_free_softc(struct uslcom_softc *);
159
160 static usb_callback_t uslcom_write_callback;
161 static usb_callback_t uslcom_read_callback;
162 static usb_callback_t uslcom_control_callback;
163
164 static void uslcom_free(struct ucom_softc *);
165 static uint8_t uslcom_get_partnum(struct uslcom_softc *);
166 static void uslcom_cfg_open(struct ucom_softc *);
167 static void uslcom_cfg_close(struct ucom_softc *);
168 static void uslcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
169 static void uslcom_cfg_set_rts(struct ucom_softc *, uint8_t);
170 static void uslcom_cfg_set_break(struct ucom_softc *, uint8_t);
171 static void uslcom_cfg_param(struct ucom_softc *, struct termios *);
172 static void uslcom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
173 static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
174     struct thread *);
175 static int uslcom_pre_param(struct ucom_softc *, struct termios *);
176 static void uslcom_start_read(struct ucom_softc *);
177 static void uslcom_stop_read(struct ucom_softc *);
178 static void uslcom_start_write(struct ucom_softc *);
179 static void uslcom_stop_write(struct ucom_softc *);
180 static void uslcom_poll(struct ucom_softc *ucom);
181
182 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
183         [USLCOM_BULK_DT_WR] = {
184                 .type = UE_BULK,
185                 .endpoint = UE_ADDR_ANY,
186                 .direction = UE_DIR_OUT,
187                 .bufsize = USLCOM_BULK_BUF_SIZE,
188                .flags = {.pipe_bof = 1,},
189                 .callback = &uslcom_write_callback,
190         },
191
192         [USLCOM_BULK_DT_RD] = {
193                 .type = UE_BULK,
194                 .endpoint = UE_ADDR_ANY,
195                 .direction = UE_DIR_IN,
196                 .bufsize = USLCOM_BULK_BUF_SIZE,
197                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
198                 .callback = &uslcom_read_callback,
199         },
200         [USLCOM_CTRL_DT_RD] = {
201                 .type = UE_CONTROL,
202                 .endpoint = 0x00,
203                 .direction = UE_DIR_ANY,
204                 .bufsize = sizeof(struct usb_device_request) + 8,
205                 .flags = {.pipe_bof = 1,},
206                 .callback = &uslcom_control_callback,
207                 .timeout = 1000,        /* 1 second timeout */
208         },
209 };
210
211 static struct ucom_callback uslcom_callback = {
212         .ucom_cfg_get_status = &uslcom_cfg_get_status,
213         .ucom_cfg_set_dtr = &uslcom_cfg_set_dtr,
214         .ucom_cfg_set_rts = &uslcom_cfg_set_rts,
215         .ucom_cfg_set_break = &uslcom_cfg_set_break,
216         .ucom_cfg_open = &uslcom_cfg_open,
217         .ucom_cfg_close = &uslcom_cfg_close,
218         .ucom_cfg_param = &uslcom_cfg_param,
219         .ucom_pre_param = &uslcom_pre_param,
220         .ucom_ioctl = &uslcom_ioctl,
221         .ucom_start_read = &uslcom_start_read,
222         .ucom_stop_read = &uslcom_stop_read,
223         .ucom_start_write = &uslcom_start_write,
224         .ucom_stop_write = &uslcom_stop_write,
225         .ucom_poll = &uslcom_poll,
226         .ucom_free = &uslcom_free,
227 };
228
229 static const STRUCT_USB_HOST_ID uslcom_devs[] = {
230 #define USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
231     USLCOM_DEV(BALTECH, CARDREADER),
232     USLCOM_DEV(CLIPSAL, 5000CT2),
233     USLCOM_DEV(CLIPSAL, 5500PACA),
234     USLCOM_DEV(CLIPSAL, 5500PCU),
235     USLCOM_DEV(CLIPSAL, 560884),
236     USLCOM_DEV(CLIPSAL, 5800PC),
237     USLCOM_DEV(CLIPSAL, C5000CT2),
238     USLCOM_DEV(CLIPSAL, L51xx),
239     USLCOM_DEV(DATAAPEX, MULTICOM),
240     USLCOM_DEV(DELL, DW700),
241     USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
242     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
243     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
244     USLCOM_DEV(DYNASTREAM, ANT2USB),
245     USLCOM_DEV(ELV, USBI2C),
246     USLCOM_DEV(FESTO, CMSP),
247     USLCOM_DEV(FESTO, CPX_USB),
248     USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
249     USLCOM_DEV(FOXCONN, TCOM_TC_300),
250     USLCOM_DEV(GEMALTO, PROXPU),
251     USLCOM_DEV(JABLOTRON, PC60B),
252     USLCOM_DEV(KAMSTRUP, OPTICALEYE),
253     USLCOM_DEV(KAMSTRUP, MBUS_250D),
254     USLCOM_DEV(LAKESHORE, 121),
255     USLCOM_DEV(LAKESHORE, 218A),
256     USLCOM_DEV(LAKESHORE, 219),
257     USLCOM_DEV(LAKESHORE, 233),
258     USLCOM_DEV(LAKESHORE, 235),
259     USLCOM_DEV(LAKESHORE, 335),
260     USLCOM_DEV(LAKESHORE, 336),
261     USLCOM_DEV(LAKESHORE, 350),
262     USLCOM_DEV(LAKESHORE, 371),
263     USLCOM_DEV(LAKESHORE, 411),
264     USLCOM_DEV(LAKESHORE, 425),
265     USLCOM_DEV(LAKESHORE, 455A),
266     USLCOM_DEV(LAKESHORE, 465),
267     USLCOM_DEV(LAKESHORE, 475A),
268     USLCOM_DEV(LAKESHORE, 625A),
269     USLCOM_DEV(LAKESHORE, 642A),
270     USLCOM_DEV(LAKESHORE, 648),
271     USLCOM_DEV(LAKESHORE, 737),
272     USLCOM_DEV(LAKESHORE, 776),
273     USLCOM_DEV(LINKINSTRUMENTS, MSO19),
274     USLCOM_DEV(LINKINSTRUMENTS, MSO28),
275     USLCOM_DEV(LINKINSTRUMENTS, MSO28_2),
276     USLCOM_DEV(MEI, CASHFLOW_SC),
277     USLCOM_DEV(MEI, S2000),
278     USLCOM_DEV(NETGEAR, M4100),
279     USLCOM_DEV(OWEN, AC4),
280     USLCOM_DEV(OWL, CM_160),
281     USLCOM_DEV(PHILIPS, ACE1001),
282     USLCOM_DEV(PLX, CA42),
283     USLCOM_DEV(RENESAS, RX610),
284     USLCOM_DEV(SEL, C662),
285     USLCOM_DEV(SILABS, AC_SERV_CAN),
286     USLCOM_DEV(SILABS, AC_SERV_CIS),
287     USLCOM_DEV(SILABS, AC_SERV_IBUS),
288     USLCOM_DEV(SILABS, AC_SERV_OBD),
289     USLCOM_DEV(SILABS, AEROCOMM),
290     USLCOM_DEV(SILABS, AMBER_AMB2560),
291     USLCOM_DEV(SILABS, ARGUSISP),
292     USLCOM_DEV(SILABS, ARKHAM_DS101_A),
293     USLCOM_DEV(SILABS, ARKHAM_DS101_M),
294     USLCOM_DEV(SILABS, ARYGON_MIFARE),
295     USLCOM_DEV(SILABS, AVIT_USB_TTL),
296     USLCOM_DEV(SILABS, B_G_H3000),
297     USLCOM_DEV(SILABS, BALLUFF_RFID),
298     USLCOM_DEV(SILABS, BEI_VCP),
299     USLCOM_DEV(SILABS, BSM7DUSB),
300     USLCOM_DEV(SILABS, BURNSIDE),
301     USLCOM_DEV(SILABS, C2_EDGE_MODEM),
302     USLCOM_DEV(SILABS, CP2102),
303     USLCOM_DEV(SILABS, CP210X_2),
304     USLCOM_DEV(SILABS, CP210X_3),
305     USLCOM_DEV(SILABS, CP210X_4),
306     USLCOM_DEV(SILABS, CRUMB128),
307     USLCOM_DEV(SILABS, CYGNAL),
308     USLCOM_DEV(SILABS, CYGNAL_DEBUG),
309     USLCOM_DEV(SILABS, CYGNAL_GPS),
310     USLCOM_DEV(SILABS, DEGREE),
311     USLCOM_DEV(SILABS, DEKTEK_DTAPLUS),
312     USLCOM_DEV(SILABS, EMS_C1007),
313     USLCOM_DEV(SILABS, HAMLINKUSB),
314     USLCOM_DEV(SILABS, HELICOM),
315     USLCOM_DEV(SILABS, HUBZ),
316     USLCOM_DEV(SILABS, IMS_USB_RS422),
317     USLCOM_DEV(SILABS, INFINITY_MIC),
318     USLCOM_DEV(SILABS, INGENI_ZIGBEE),
319     USLCOM_DEV(SILABS, INSYS_MODEM),
320     USLCOM_DEV(SILABS, IRZ_SG10),
321     USLCOM_DEV(SILABS, KYOCERA_GPS),
322     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
323     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
324     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
325     USLCOM_DEV(SILABS, MC35PU),
326     USLCOM_DEV(SILABS, MMB_ZIGBEE),
327     USLCOM_DEV(SILABS, MJS_TOSLINK),
328     USLCOM_DEV(SILABS, MSD_DASHHAWK),
329     USLCOM_DEV(SILABS, MULTIPLEX_RC),
330     USLCOM_DEV(SILABS, OPTRIS_MSPRO),
331     USLCOM_DEV(SILABS, POLOLU),
332     USLCOM_DEV(SILABS, PROCYON_AVS),
333     USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
334     USLCOM_DEV(SILABS, SUUNTO),
335     USLCOM_DEV(SILABS, TAMSMASTER),
336     USLCOM_DEV(SILABS, TELEGESIS_ETRX2),
337     USLCOM_DEV(SILABS, TRACIENT),
338     USLCOM_DEV(SILABS, TRAQMATE),
339     USLCOM_DEV(SILABS, USBCOUNT50),
340     USLCOM_DEV(SILABS, USBPULSE100),
341     USLCOM_DEV(SILABS, USBSCOPE50),
342     USLCOM_DEV(SILABS, USBWAVE12),
343     USLCOM_DEV(SILABS, V_PREON32),
344     USLCOM_DEV(SILABS, VSTABI),
345     USLCOM_DEV(SILABS, WAVIT),
346     USLCOM_DEV(SILABS, WMRBATT),
347     USLCOM_DEV(SILABS, WMRRIGBLASTER),
348     USLCOM_DEV(SILABS, WMRRIGTALK),
349     USLCOM_DEV(SILABS, ZEPHYR_BIO),
350     USLCOM_DEV(SILABS2, DCU11CLONE),
351     USLCOM_DEV(SILABS3, GPRS_MODEM),
352     USLCOM_DEV(SILABS4, 100EU_MODEM),
353     USLCOM_DEV(SYNTECH, CYPHERLAB100),
354     USLCOM_DEV(USI, MC60),
355     USLCOM_DEV(VAISALA, CABLE),
356     USLCOM_DEV(WAGO, SERVICECABLE),
357     USLCOM_DEV(WAVESENSE, JAZZ),
358     USLCOM_DEV(WESTMOUNTAIN, RIGBLASTER_ADVANTAGE),
359     USLCOM_DEV(WIENERPLEINBAUS, PL512),
360     USLCOM_DEV(WIENERPLEINBAUS, RCM),
361     USLCOM_DEV(WIENERPLEINBAUS, MPOD),
362     USLCOM_DEV(WIENERPLEINBAUS, CML),
363 #undef USLCOM_DEV
364 };
365
366 static device_method_t uslcom_methods[] = {
367         DEVMETHOD(device_probe, uslcom_probe),
368         DEVMETHOD(device_attach, uslcom_attach),
369         DEVMETHOD(device_detach, uslcom_detach),
370         DEVMETHOD_END
371 };
372
373 static devclass_t uslcom_devclass;
374
375 static driver_t uslcom_driver = {
376         .name = "uslcom",
377         .methods = uslcom_methods,
378         .size = sizeof(struct uslcom_softc),
379 };
380
381 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
382 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
383 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
384 MODULE_VERSION(uslcom, 1);
385 USB_PNP_HOST_INFO(uslcom_devs);
386
387 static void
388 uslcom_watchdog(void *arg)
389 {
390         struct uslcom_softc *sc = arg;
391
392         mtx_assert(&sc->sc_mtx, MA_OWNED);
393
394         usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]);
395
396         usb_callout_reset(&sc->sc_watchdog,
397             hz / 4, &uslcom_watchdog, sc);
398 }
399
400 static int
401 uslcom_probe(device_t dev)
402 {
403         struct usb_attach_arg *uaa = device_get_ivars(dev);
404
405         DPRINTFN(11, "\n");
406
407         if (uaa->usb_mode != USB_MODE_HOST) {
408                 return (ENXIO);
409         }
410         if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
411                 return (ENXIO);
412         }
413         return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
414 }
415
416 static int
417 uslcom_attach(device_t dev)
418 {
419         struct usb_attach_arg *uaa = device_get_ivars(dev);
420         struct uslcom_softc *sc = device_get_softc(dev);
421         int error;
422
423         DPRINTFN(11, "\n");
424
425         device_set_usb_desc(dev);
426         mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
427         ucom_ref(&sc->sc_super_ucom);
428         usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
429
430         sc->sc_udev = uaa->device;
431         /* use the interface number from the USB interface descriptor */
432         sc->sc_iface_no = uaa->info.bIfaceNum;
433
434         error = usbd_transfer_setup(uaa->device,
435             &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
436             USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
437         if (error) {
438                 DPRINTF("one or more missing USB endpoints, "
439                     "error=%s\n", usbd_errstr(error));
440                 goto detach;
441         }
442         /* clear stall at first run */
443         mtx_lock(&sc->sc_mtx);
444         usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
445         usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
446         mtx_unlock(&sc->sc_mtx);
447
448         sc->sc_partnum = uslcom_get_partnum(sc);
449
450         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
451             &uslcom_callback, &sc->sc_mtx);
452         if (error) {
453                 goto detach;
454         }
455         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
456
457         return (0);
458
459 detach:
460         uslcom_detach(dev);
461         return (ENXIO);
462 }
463
464 static int
465 uslcom_detach(device_t dev)
466 {
467         struct uslcom_softc *sc = device_get_softc(dev);
468
469         DPRINTF("sc=%p\n", sc);
470
471         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
472         usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
473
474         usb_callout_drain(&sc->sc_watchdog);
475
476         device_claim_softc(dev);
477
478         uslcom_free_softc(sc);
479
480         return (0);
481 }
482
483 UCOM_UNLOAD_DRAIN(uslcom);
484
485 static void
486 uslcom_free_softc(struct uslcom_softc *sc)
487 {
488         if (ucom_unref(&sc->sc_super_ucom)) {
489                 mtx_destroy(&sc->sc_mtx);
490                 device_free_softc(sc);
491         }
492 }
493
494 static void
495 uslcom_free(struct ucom_softc *ucom)
496 {
497         uslcom_free_softc(ucom->sc_parent);
498 }
499
500 static void
501 uslcom_cfg_open(struct ucom_softc *ucom)
502 {
503         struct uslcom_softc *sc = ucom->sc_parent;
504         struct usb_device_request req;
505
506         req.bmRequestType = USLCOM_WRITE;
507         req.bRequest = USLCOM_IFC_ENABLE;
508         USETW(req.wValue, USLCOM_IFC_ENABLE_EN);
509         USETW(req.wIndex, sc->sc_iface_no);
510         USETW(req.wLength, 0);
511
512         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
513             &req, NULL, 0, 1000)) {
514                 DPRINTF("UART enable failed (ignored)\n");
515         }
516
517         /* start polling status */
518         uslcom_watchdog(sc);
519 }
520
521 static void
522 uslcom_cfg_close(struct ucom_softc *ucom)
523 {
524         struct uslcom_softc *sc = ucom->sc_parent;
525         struct usb_device_request req;
526
527         /* stop polling status */
528         usb_callout_stop(&sc->sc_watchdog);
529
530         req.bmRequestType = USLCOM_WRITE;
531         req.bRequest = USLCOM_IFC_ENABLE;
532         USETW(req.wValue, USLCOM_IFC_ENABLE_DIS);
533         USETW(req.wIndex, sc->sc_iface_no);
534         USETW(req.wLength, 0);
535
536         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
537             &req, NULL, 0, 1000)) {
538                 DPRINTF("UART disable failed (ignored)\n");
539         }
540 }
541
542 static uint8_t
543 uslcom_get_partnum(struct uslcom_softc *sc)
544 {
545         struct usb_device_request req;
546         uint8_t partnum;
547
548         /* Find specific chip type */
549         partnum = 0;
550         req.bmRequestType = USLCOM_READ;
551         req.bRequest = USLCOM_VENDOR_SPECIFIC;
552         USETW(req.wValue, USLCOM_GET_PARTNUM);
553         USETW(req.wIndex, sc->sc_iface_no);
554         USETW(req.wLength, sizeof(partnum));
555
556         if (usbd_do_request_flags(sc->sc_udev, NULL,
557             &req, &partnum, 0, NULL, 1000)) {
558                 DPRINTF("GET_PARTNUM failed\n");
559         }
560
561         return(partnum);
562 }
563
564 static void
565 uslcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
566 {
567         struct uslcom_softc *sc = ucom->sc_parent;
568         struct usb_device_request req;
569         uint16_t ctl;
570
571         DPRINTF("onoff = %d\n", onoff);
572
573         ctl = onoff ? USLCOM_MHS_DTR_ON : 0;
574         ctl |= USLCOM_MHS_DTR_SET;
575
576         req.bmRequestType = USLCOM_WRITE;
577         req.bRequest = USLCOM_SET_MHS;
578         USETW(req.wValue, ctl);
579         USETW(req.wIndex, sc->sc_iface_no);
580         USETW(req.wLength, 0);
581
582         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
583             &req, NULL, 0, 1000)) {
584                 DPRINTF("Setting DTR failed (ignored)\n");
585         }
586 }
587
588 static void
589 uslcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
590 {
591         struct uslcom_softc *sc = ucom->sc_parent;
592         struct usb_device_request req;
593         uint16_t ctl;
594
595         DPRINTF("onoff = %d\n", onoff);
596
597         ctl = onoff ? USLCOM_MHS_RTS_ON : 0;
598         ctl |= USLCOM_MHS_RTS_SET;
599
600         req.bmRequestType = USLCOM_WRITE;
601         req.bRequest = USLCOM_SET_MHS;
602         USETW(req.wValue, ctl);
603         USETW(req.wIndex, sc->sc_iface_no);
604         USETW(req.wLength, 0);
605
606         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
607             &req, NULL, 0, 1000)) {
608                 DPRINTF("Setting DTR failed (ignored)\n");
609         }
610 }
611
612 static int
613 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
614 {
615         struct uslcom_softc *sc = ucom->sc_parent;
616         uint32_t maxspeed;
617
618         switch (sc->sc_partnum) {
619         case USLCOM_PARTNUM_CP2104:
620         case USLCOM_PARTNUM_CP2105:
621                 maxspeed = 2000000;
622                 break;
623         case USLCOM_PARTNUM_CP2101:
624         case USLCOM_PARTNUM_CP2102:
625         case USLCOM_PARTNUM_CP2103:
626         default:
627                 /*
628                  * Datasheet for cp2102 says 921600 max.  Testing shows that
629                  * 1228800 and 1843200 work fine.
630                  */
631                 maxspeed = 1843200;
632                 break;
633         }
634         if (t->c_ospeed <= 0 || t->c_ospeed > maxspeed)
635                 return (EINVAL);
636         return (0);
637 }
638
639 static void
640 uslcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
641 {
642         struct uslcom_softc *sc = ucom->sc_parent;
643         struct usb_device_request req;
644         uint32_t baudrate, flowctrl[4];
645         uint16_t data;
646
647         DPRINTF("\n");
648
649         baudrate = t->c_ospeed;
650         req.bmRequestType = USLCOM_WRITE;
651         req.bRequest = USLCOM_SET_BAUDRATE;
652         USETW(req.wValue, 0);
653         USETW(req.wIndex, sc->sc_iface_no);
654         USETW(req.wLength, sizeof(baudrate));
655
656         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
657             &req, &baudrate, 0, 1000)) {
658                 printf("Set baudrate failed (ignored)\n");
659         }
660
661         if (t->c_cflag & CSTOPB)
662                 data = USLCOM_STOP_BITS_2;
663         else
664                 data = USLCOM_STOP_BITS_1;
665         if (t->c_cflag & PARENB) {
666                 if (t->c_cflag & PARODD)
667                         data |= USLCOM_PARITY_ODD;
668                 else
669                         data |= USLCOM_PARITY_EVEN;
670         } else
671                 data |= USLCOM_PARITY_NONE;
672         switch (t->c_cflag & CSIZE) {
673         case CS5:
674                 data |= USLCOM_SET_DATA_BITS(5);
675                 break;
676         case CS6:
677                 data |= USLCOM_SET_DATA_BITS(6);
678                 break;
679         case CS7:
680                 data |= USLCOM_SET_DATA_BITS(7);
681                 break;
682         case CS8:
683                 data |= USLCOM_SET_DATA_BITS(8);
684                 break;
685         }
686
687         req.bmRequestType = USLCOM_WRITE;
688         req.bRequest = USLCOM_SET_LINE_CTL;
689         USETW(req.wValue, data);
690         USETW(req.wIndex, sc->sc_iface_no);
691         USETW(req.wLength, 0);
692
693         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
694             &req, NULL, 0, 1000)) {
695                 DPRINTF("Set format failed (ignored)\n");
696         }
697
698         if (t->c_cflag & CRTSCTS) {
699                 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS);
700                 flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS);
701         } else {
702                 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON);
703                 flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON);
704         }
705         flowctrl[2] = 0;
706         flowctrl[3] = 0;
707         req.bmRequestType = USLCOM_WRITE;
708         req.bRequest = USLCOM_SET_FLOW;
709         USETW(req.wValue, 0);
710         USETW(req.wIndex, sc->sc_iface_no);
711         USETW(req.wLength, sizeof(flowctrl));
712
713         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
714             &req, flowctrl, 0, 1000)) {
715                 DPRINTF("Set flowcontrol failed (ignored)\n");
716         }
717 }
718
719 static void
720 uslcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
721 {
722         struct uslcom_softc *sc = ucom->sc_parent;
723
724         DPRINTF("\n");
725
726         /* XXX Note: sc_lsr is always zero */
727         *lsr = sc->sc_lsr;
728         *msr = sc->sc_msr;
729 }
730
731 static void
732 uslcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
733 {
734         struct uslcom_softc *sc = ucom->sc_parent;
735         struct usb_device_request req;
736         uint16_t brk = onoff ? USLCOM_SET_BREAK_ON : USLCOM_SET_BREAK_OFF;
737
738         req.bmRequestType = USLCOM_WRITE;
739         req.bRequest = USLCOM_SET_BREAK;
740         USETW(req.wValue, brk);
741         USETW(req.wIndex, sc->sc_iface_no);
742         USETW(req.wLength, 0);
743
744         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
745             &req, NULL, 0, 1000)) {
746                 DPRINTF("Set BREAK failed (ignored)\n");
747         }
748 }
749
750 static int
751 uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
752     int flag, struct thread *td)
753 {
754         struct uslcom_softc *sc = ucom->sc_parent;
755         struct usb_device_request req;
756         int error = 0;
757         uint8_t latch;
758
759         DPRINTF("cmd=0x%08x\n", cmd);
760
761         switch (cmd) {
762         case USB_GET_GPIO:
763                 if (sc->sc_partnum < USLCOM_PARTNUM_CP2103) {
764                         error = ENODEV;
765                         break;
766                 }
767                 req.bmRequestType = USLCOM_READ;
768                 req.bRequest = USLCOM_VENDOR_SPECIFIC;
769                 USETW(req.wValue, USLCOM_READ_LATCH);
770                 USETW(req.wIndex, sc->sc_iface_no);
771                 USETW(req.wLength, sizeof(latch));
772
773                 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
774                     &req, &latch, 0, 1000)) {
775                         DPRINTF("Get LATCH failed\n");
776                         error = EIO;
777                 }
778                 *(int *)data = latch;
779                 break;
780
781         case USB_SET_GPIO:
782                 if (sc->sc_partnum < USLCOM_PARTNUM_CP2103)
783                         error = ENODEV;
784                 else if ((sc->sc_partnum == USLCOM_PARTNUM_CP2103) ||
785                     (sc->sc_partnum == USLCOM_PARTNUM_CP2104)) {
786                         req.bmRequestType = USLCOM_WRITE;
787                         req.bRequest = USLCOM_VENDOR_SPECIFIC;
788                         USETW(req.wValue, USLCOM_WRITE_LATCH);
789                         USETW(req.wIndex, (*(int *)data));
790                         USETW(req.wLength, 0);
791
792                         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
793                             &req, NULL, 0, 1000)) {
794                                 DPRINTF("Set LATCH failed\n");
795                                 error = EIO;
796                         }
797                 } else
798                         error = ENODEV; /* Not yet */
799                 break;
800
801         default:
802                 DPRINTF("Unknown IOCTL\n");
803                 error = ENOIOCTL;
804                 break;
805         }
806         return (error);
807 }
808
809 static void
810 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
811 {
812         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
813         struct usb_page_cache *pc;
814         uint32_t actlen;
815
816         switch (USB_GET_STATE(xfer)) {
817         case USB_ST_SETUP:
818         case USB_ST_TRANSFERRED:
819 tr_setup:
820                 pc = usbd_xfer_get_frame(xfer, 0);
821                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
822                     USLCOM_BULK_BUF_SIZE, &actlen)) {
823
824                         DPRINTF("actlen = %d\n", actlen);
825
826                         usbd_xfer_set_frame_len(xfer, 0, actlen);
827                         usbd_transfer_submit(xfer);
828                 }
829                 return;
830
831         default:                        /* Error */
832                 if (error != USB_ERR_CANCELLED) {
833                         /* try to clear stall first */
834                         usbd_xfer_set_stall(xfer);
835                         goto tr_setup;
836                 }
837                 return;
838         }
839 }
840
841 static void
842 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
843 {
844         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
845         struct usb_page_cache *pc;
846         int actlen;
847
848         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
849
850         switch (USB_GET_STATE(xfer)) {
851         case USB_ST_TRANSFERRED:
852                 pc = usbd_xfer_get_frame(xfer, 0);
853                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
854
855         case USB_ST_SETUP:
856 tr_setup:
857                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
858                 usbd_transfer_submit(xfer);
859                 return;
860
861         default:                        /* Error */
862                 if (error != USB_ERR_CANCELLED) {
863                         /* try to clear stall first */
864                         usbd_xfer_set_stall(xfer);
865                         goto tr_setup;
866                 }
867                 return;
868         }
869 }
870
871 static void
872 uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error)
873 {
874         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
875         struct usb_page_cache *pc;
876         struct usb_device_request req;
877         uint8_t msr = 0;
878         uint8_t buf;
879
880         switch (USB_GET_STATE(xfer)) {
881         case USB_ST_TRANSFERRED:
882                 pc = usbd_xfer_get_frame(xfer, 1);
883                 usbd_copy_out(pc, 0, &buf, sizeof(buf));
884                 if (buf & USLCOM_MHS_CTS)
885                         msr |= SER_CTS;
886                 if (buf & USLCOM_MHS_DSR)
887                         msr |= SER_DSR;
888                 if (buf & USLCOM_MHS_RI)
889                         msr |= SER_RI;
890                 if (buf & USLCOM_MHS_DCD)
891                         msr |= SER_DCD;
892
893                 if (msr != sc->sc_msr) {
894                         DPRINTF("status change msr=0x%02x "
895                             "(was 0x%02x)\n", msr, sc->sc_msr);
896                         sc->sc_msr = msr;
897                         ucom_status_change(&sc->sc_ucom);
898                 }
899                 break;
900
901         case USB_ST_SETUP:
902                 req.bmRequestType = USLCOM_READ;
903                 req.bRequest = USLCOM_GET_MDMSTS;
904                 USETW(req.wValue, 0);
905                 USETW(req.wIndex, sc->sc_iface_no);
906                 USETW(req.wLength, sizeof(buf));
907
908                 usbd_xfer_set_frames(xfer, 2);
909                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
910                 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
911
912                 pc = usbd_xfer_get_frame(xfer, 0);
913                 usbd_copy_in(pc, 0, &req, sizeof(req));
914                 usbd_transfer_submit(xfer);
915                 break;
916
917         default:                /* error */
918                 if (error != USB_ERR_CANCELLED)
919                         DPRINTF("error=%s\n", usbd_errstr(error));
920                 break;
921         }
922 }
923
924 static void
925 uslcom_start_read(struct ucom_softc *ucom)
926 {
927         struct uslcom_softc *sc = ucom->sc_parent;
928
929         /* start read endpoint */
930         usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
931 }
932
933 static void
934 uslcom_stop_read(struct ucom_softc *ucom)
935 {
936         struct uslcom_softc *sc = ucom->sc_parent;
937
938         /* stop read endpoint */
939         usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
940 }
941
942 static void
943 uslcom_start_write(struct ucom_softc *ucom)
944 {
945         struct uslcom_softc *sc = ucom->sc_parent;
946
947         usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
948 }
949
950 static void
951 uslcom_stop_write(struct ucom_softc *ucom)
952 {
953         struct uslcom_softc *sc = ucom->sc_parent;
954
955         usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
956 }
957
958 static void
959 uslcom_poll(struct ucom_softc *ucom)
960 {
961         struct uslcom_softc *sc = ucom->sc_parent;
962         usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
963 }