]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/uslcom.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[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 void uslcom_open(struct ucom_softc *);
166 static void uslcom_close(struct ucom_softc *);
167 static uint8_t uslcom_get_partnum(struct uslcom_softc *);
168 static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
169 static void uslcom_set_rts(struct ucom_softc *, uint8_t);
170 static void uslcom_set_break(struct ucom_softc *, uint8_t);
171 static int uslcom_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
172                 struct thread *);
173 static int uslcom_pre_param(struct ucom_softc *, struct termios *);
174 static void uslcom_param(struct ucom_softc *, struct termios *);
175 static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
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
184         [USLCOM_BULK_DT_WR] = {
185                 .type = UE_BULK,
186                 .endpoint = UE_ADDR_ANY,
187                 .direction = UE_DIR_OUT,
188                 .bufsize = USLCOM_BULK_BUF_SIZE,
189                .flags = {.pipe_bof = 1,},
190                 .callback = &uslcom_write_callback,
191         },
192
193         [USLCOM_BULK_DT_RD] = {
194                 .type = UE_BULK,
195                 .endpoint = UE_ADDR_ANY,
196                 .direction = UE_DIR_IN,
197                 .bufsize = USLCOM_BULK_BUF_SIZE,
198                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
199                 .callback = &uslcom_read_callback,
200         },
201         [USLCOM_CTRL_DT_RD] = {
202                 .type = UE_CONTROL,
203                 .endpoint = 0x00,
204                 .direction = UE_DIR_ANY,
205                 .bufsize = sizeof(struct usb_device_request) + 8,
206                 .flags = {.pipe_bof = 1,},
207                 .callback = &uslcom_control_callback,
208                 .timeout = 1000,        /* 1 second timeout */
209         },
210 };
211
212 static struct ucom_callback uslcom_callback = {
213         .ucom_cfg_open = &uslcom_open,
214         .ucom_cfg_close = &uslcom_close,
215         .ucom_cfg_get_status = &uslcom_get_status,
216         .ucom_cfg_set_dtr = &uslcom_set_dtr,
217         .ucom_cfg_set_rts = &uslcom_set_rts,
218         .ucom_cfg_set_break = &uslcom_set_break,
219         .ucom_ioctl = &uslcom_ioctl,
220         .ucom_cfg_param = &uslcom_param,
221         .ucom_pre_param = &uslcom_pre_param,
222         .ucom_start_read = &uslcom_start_read,
223         .ucom_stop_read = &uslcom_stop_read,
224         .ucom_start_write = &uslcom_start_write,
225         .ucom_stop_write = &uslcom_stop_write,
226         .ucom_poll = &uslcom_poll,
227         .ucom_free = &uslcom_free,
228 };
229
230 static const STRUCT_USB_HOST_ID uslcom_devs[] = {
231 #define USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
232     USLCOM_DEV(BALTECH, CARDREADER),
233     USLCOM_DEV(CLIPSAL, 5000CT2),
234     USLCOM_DEV(CLIPSAL, 5500PACA),
235     USLCOM_DEV(CLIPSAL, 5500PCU),
236     USLCOM_DEV(CLIPSAL, 560884),
237     USLCOM_DEV(CLIPSAL, 5800PC),
238     USLCOM_DEV(CLIPSAL, C5000CT2),
239     USLCOM_DEV(CLIPSAL, L51xx),
240     USLCOM_DEV(DATAAPEX, MULTICOM),
241     USLCOM_DEV(DELL, DW700),
242     USLCOM_DEV(DIGIANSWER, ZIGBEE802154),
243     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
244     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD2),
245     USLCOM_DEV(DYNASTREAM, ANT2USB),
246     USLCOM_DEV(ELV, USBI2C),
247     USLCOM_DEV(FESTO, CMSP),
248     USLCOM_DEV(FESTO, CPX_USB),
249     USLCOM_DEV(FOXCONN, PIRELLI_DP_L10),
250     USLCOM_DEV(FOXCONN, TCOM_TC_300),
251     USLCOM_DEV(GEMALTO, PROXPU),
252     USLCOM_DEV(JABLOTRON, PC60B),
253     USLCOM_DEV(KAMSTRUP, OPTICALEYE),
254     USLCOM_DEV(KAMSTRUP, MBUS_250D),
255     USLCOM_DEV(LAKESHORE, 121),
256     USLCOM_DEV(LAKESHORE, 218A),
257     USLCOM_DEV(LAKESHORE, 219),
258     USLCOM_DEV(LAKESHORE, 233),
259     USLCOM_DEV(LAKESHORE, 235),
260     USLCOM_DEV(LAKESHORE, 335),
261     USLCOM_DEV(LAKESHORE, 336),
262     USLCOM_DEV(LAKESHORE, 350),
263     USLCOM_DEV(LAKESHORE, 371),
264     USLCOM_DEV(LAKESHORE, 411),
265     USLCOM_DEV(LAKESHORE, 425),
266     USLCOM_DEV(LAKESHORE, 455A),
267     USLCOM_DEV(LAKESHORE, 465),
268     USLCOM_DEV(LAKESHORE, 475A),
269     USLCOM_DEV(LAKESHORE, 625A),
270     USLCOM_DEV(LAKESHORE, 642A),
271     USLCOM_DEV(LAKESHORE, 648),
272     USLCOM_DEV(LAKESHORE, 737),
273     USLCOM_DEV(LAKESHORE, 776),
274     USLCOM_DEV(LINKINSTRUMENTS, MSO19),
275     USLCOM_DEV(LINKINSTRUMENTS, MSO28),
276     USLCOM_DEV(LINKINSTRUMENTS, MSO28_2),
277     USLCOM_DEV(MEI, CASHFLOW_SC),
278     USLCOM_DEV(MEI, S2000),
279     USLCOM_DEV(NETGEAR, M4100),
280     USLCOM_DEV(OWEN, AC4),
281     USLCOM_DEV(OWL, CM_160),
282     USLCOM_DEV(PHILIPS, ACE1001),
283     USLCOM_DEV(PLX, CA42),
284     USLCOM_DEV(RENESAS, RX610),
285     USLCOM_DEV(SEL, C662),
286     USLCOM_DEV(SILABS, AC_SERV_CAN),
287     USLCOM_DEV(SILABS, AC_SERV_CIS),
288     USLCOM_DEV(SILABS, AC_SERV_IBUS),
289     USLCOM_DEV(SILABS, AC_SERV_OBD),
290     USLCOM_DEV(SILABS, AEROCOMM),
291     USLCOM_DEV(SILABS, AMBER_AMB2560),
292     USLCOM_DEV(SILABS, ARGUSISP),
293     USLCOM_DEV(SILABS, ARKHAM_DS101_A),
294     USLCOM_DEV(SILABS, ARKHAM_DS101_M),
295     USLCOM_DEV(SILABS, ARYGON_MIFARE),
296     USLCOM_DEV(SILABS, AVIT_USB_TTL),
297     USLCOM_DEV(SILABS, B_G_H3000),
298     USLCOM_DEV(SILABS, BALLUFF_RFID),
299     USLCOM_DEV(SILABS, BEI_VCP),
300     USLCOM_DEV(SILABS, BSM7DUSB),
301     USLCOM_DEV(SILABS, BURNSIDE),
302     USLCOM_DEV(SILABS, C2_EDGE_MODEM),
303     USLCOM_DEV(SILABS, CP2102),
304     USLCOM_DEV(SILABS, CP210X_2),
305     USLCOM_DEV(SILABS, CP210X_3),
306     USLCOM_DEV(SILABS, CP210X_4),
307     USLCOM_DEV(SILABS, CRUMB128),
308     USLCOM_DEV(SILABS, CYGNAL),
309     USLCOM_DEV(SILABS, CYGNAL_DEBUG),
310     USLCOM_DEV(SILABS, CYGNAL_GPS),
311     USLCOM_DEV(SILABS, DEGREE),
312     USLCOM_DEV(SILABS, DEKTEK_DTAPLUS),
313     USLCOM_DEV(SILABS, EMS_C1007),
314     USLCOM_DEV(SILABS, HAMLINKUSB),
315     USLCOM_DEV(SILABS, HELICOM),
316     USLCOM_DEV(SILABS, HUBZ),
317     USLCOM_DEV(SILABS, IMS_USB_RS422),
318     USLCOM_DEV(SILABS, INFINITY_MIC),
319     USLCOM_DEV(SILABS, INGENI_ZIGBEE),
320     USLCOM_DEV(SILABS, INSYS_MODEM),
321     USLCOM_DEV(SILABS, IRZ_SG10),
322     USLCOM_DEV(SILABS, KYOCERA_GPS),
323     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
324     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
325     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
326     USLCOM_DEV(SILABS, MC35PU),
327     USLCOM_DEV(SILABS, MMB_ZIGBEE),
328     USLCOM_DEV(SILABS, MJS_TOSLINK),
329     USLCOM_DEV(SILABS, MSD_DASHHAWK),
330     USLCOM_DEV(SILABS, MULTIPLEX_RC),
331     USLCOM_DEV(SILABS, OPTRIS_MSPRO),
332     USLCOM_DEV(SILABS, POLOLU),
333     USLCOM_DEV(SILABS, PROCYON_AVS),
334     USLCOM_DEV(SILABS, SB_PARAMOUNT_ME),
335     USLCOM_DEV(SILABS, SUUNTO),
336     USLCOM_DEV(SILABS, TAMSMASTER),
337     USLCOM_DEV(SILABS, TELEGESIS_ETRX2),
338     USLCOM_DEV(SILABS, TRACIENT),
339     USLCOM_DEV(SILABS, TRAQMATE),
340     USLCOM_DEV(SILABS, USBCOUNT50),
341     USLCOM_DEV(SILABS, USBPULSE100),
342     USLCOM_DEV(SILABS, USBSCOPE50),
343     USLCOM_DEV(SILABS, USBWAVE12),
344     USLCOM_DEV(SILABS, V_PREON32),
345     USLCOM_DEV(SILABS, VSTABI),
346     USLCOM_DEV(SILABS, WAVIT),
347     USLCOM_DEV(SILABS, WMRBATT),
348     USLCOM_DEV(SILABS, WMRRIGBLASTER),
349     USLCOM_DEV(SILABS, WMRRIGTALK),
350     USLCOM_DEV(SILABS, ZEPHYR_BIO),
351     USLCOM_DEV(SILABS2, DCU11CLONE),
352     USLCOM_DEV(SILABS3, GPRS_MODEM),
353     USLCOM_DEV(SILABS4, 100EU_MODEM),
354     USLCOM_DEV(SYNTECH, CYPHERLAB100),
355     USLCOM_DEV(USI, MC60),
356     USLCOM_DEV(VAISALA, CABLE),
357     USLCOM_DEV(WAGO, SERVICECABLE),
358     USLCOM_DEV(WAVESENSE, JAZZ),
359     USLCOM_DEV(WESTMOUNTAIN, RIGBLASTER_ADVANTAGE),
360     USLCOM_DEV(WIENERPLEINBAUS, PL512),
361     USLCOM_DEV(WIENERPLEINBAUS, RCM),
362     USLCOM_DEV(WIENERPLEINBAUS, MPOD),
363     USLCOM_DEV(WIENERPLEINBAUS, CML),
364 #undef USLCOM_DEV
365 };
366
367 static device_method_t uslcom_methods[] = {
368         DEVMETHOD(device_probe, uslcom_probe),
369         DEVMETHOD(device_attach, uslcom_attach),
370         DEVMETHOD(device_detach, uslcom_detach),
371         DEVMETHOD_END
372 };
373
374 static devclass_t uslcom_devclass;
375
376 static driver_t uslcom_driver = {
377         .name = "uslcom",
378         .methods = uslcom_methods,
379         .size = sizeof(struct uslcom_softc),
380 };
381
382 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
383 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
384 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
385 MODULE_VERSION(uslcom, 1);
386 USB_PNP_HOST_INFO(uslcom_devs);
387
388 static void
389 uslcom_watchdog(void *arg)
390 {
391         struct uslcom_softc *sc = arg;
392
393         mtx_assert(&sc->sc_mtx, MA_OWNED);
394
395         usbd_transfer_start(sc->sc_xfer[USLCOM_CTRL_DT_RD]);
396
397         usb_callout_reset(&sc->sc_watchdog,
398             hz / 4, &uslcom_watchdog, sc);
399 }
400
401 static int
402 uslcom_probe(device_t dev)
403 {
404         struct usb_attach_arg *uaa = device_get_ivars(dev);
405
406         DPRINTFN(11, "\n");
407
408         if (uaa->usb_mode != USB_MODE_HOST) {
409                 return (ENXIO);
410         }
411         if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
412                 return (ENXIO);
413         }
414         return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
415 }
416
417 static int
418 uslcom_attach(device_t dev)
419 {
420         struct usb_attach_arg *uaa = device_get_ivars(dev);
421         struct uslcom_softc *sc = device_get_softc(dev);
422         int error;
423
424         DPRINTFN(11, "\n");
425
426         device_set_usb_desc(dev);
427         mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
428         ucom_ref(&sc->sc_super_ucom);
429         usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
430
431         sc->sc_udev = uaa->device;
432         /* use the interface number from the USB interface descriptor */
433         sc->sc_iface_no = uaa->info.bIfaceNum;
434
435         error = usbd_transfer_setup(uaa->device,
436             &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
437             USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
438         if (error) {
439                 DPRINTF("one or more missing USB endpoints, "
440                     "error=%s\n", usbd_errstr(error));
441                 goto detach;
442         }
443         /* clear stall at first run */
444         mtx_lock(&sc->sc_mtx);
445         usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
446         usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
447         mtx_unlock(&sc->sc_mtx);
448
449         sc->sc_partnum = uslcom_get_partnum(sc);
450
451         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
452             &uslcom_callback, &sc->sc_mtx);
453         if (error) {
454                 goto detach;
455         }
456         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
457
458         return (0);
459
460 detach:
461         uslcom_detach(dev);
462         return (ENXIO);
463 }
464
465 static int
466 uslcom_detach(device_t dev)
467 {
468         struct uslcom_softc *sc = device_get_softc(dev);
469
470         DPRINTF("sc=%p\n", sc);
471
472         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
473         usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
474
475         usb_callout_drain(&sc->sc_watchdog);
476
477         device_claim_softc(dev);
478
479         uslcom_free_softc(sc);
480
481         return (0);
482 }
483
484 UCOM_UNLOAD_DRAIN(uslcom);
485
486 static void
487 uslcom_free_softc(struct uslcom_softc *sc)
488 {
489         if (ucom_unref(&sc->sc_super_ucom)) {
490                 mtx_destroy(&sc->sc_mtx);
491                 device_free_softc(sc);
492         }
493 }
494
495 static void
496 uslcom_free(struct ucom_softc *ucom)
497 {
498         uslcom_free_softc(ucom->sc_parent);
499 }
500
501 static void
502 uslcom_open(struct ucom_softc *ucom)
503 {
504         struct uslcom_softc *sc = ucom->sc_parent;
505         struct usb_device_request req;
506
507         req.bmRequestType = USLCOM_WRITE;
508         req.bRequest = USLCOM_IFC_ENABLE;
509         USETW(req.wValue, USLCOM_IFC_ENABLE_EN);
510         USETW(req.wIndex, sc->sc_iface_no);
511         USETW(req.wLength, 0);
512
513         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
514             &req, NULL, 0, 1000)) {
515                 DPRINTF("UART enable failed (ignored)\n");
516         }
517
518         /* start polling status */
519         uslcom_watchdog(sc);
520 }
521
522 static void
523 uslcom_close(struct ucom_softc *ucom)
524 {
525         struct uslcom_softc *sc = ucom->sc_parent;
526         struct usb_device_request req;
527
528         /* stop polling status */
529         usb_callout_stop(&sc->sc_watchdog);
530
531         req.bmRequestType = USLCOM_WRITE;
532         req.bRequest = USLCOM_IFC_ENABLE;
533         USETW(req.wValue, USLCOM_IFC_ENABLE_DIS);
534         USETW(req.wIndex, sc->sc_iface_no);
535         USETW(req.wLength, 0);
536
537         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
538             &req, NULL, 0, 1000)) {
539                 DPRINTF("UART disable failed (ignored)\n");
540         }
541 }
542
543 static uint8_t
544 uslcom_get_partnum(struct uslcom_softc *sc)
545 {
546         struct usb_device_request req;
547         uint8_t partnum;
548
549         /* Find specific chip type */
550         partnum = 0;
551         req.bmRequestType = USLCOM_READ;
552         req.bRequest = USLCOM_VENDOR_SPECIFIC;
553         USETW(req.wValue, USLCOM_GET_PARTNUM);
554         USETW(req.wIndex, sc->sc_iface_no);
555         USETW(req.wLength, sizeof(partnum));
556
557         if (usbd_do_request_flags(sc->sc_udev, NULL,
558             &req, &partnum, 0, NULL, 1000)) {
559                 DPRINTF("GET_PARTNUM failed\n");
560         }
561
562         return(partnum);
563 }
564
565 static void
566 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
567 {
568         struct uslcom_softc *sc = ucom->sc_parent;
569         struct usb_device_request req;
570         uint16_t ctl;
571
572         DPRINTF("onoff = %d\n", onoff);
573
574         ctl = onoff ? USLCOM_MHS_DTR_ON : 0;
575         ctl |= USLCOM_MHS_DTR_SET;
576
577         req.bmRequestType = USLCOM_WRITE;
578         req.bRequest = USLCOM_SET_MHS;
579         USETW(req.wValue, ctl);
580         USETW(req.wIndex, sc->sc_iface_no);
581         USETW(req.wLength, 0);
582
583         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
584             &req, NULL, 0, 1000)) {
585                 DPRINTF("Setting DTR failed (ignored)\n");
586         }
587 }
588
589 static void
590 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
591 {
592         struct uslcom_softc *sc = ucom->sc_parent;
593         struct usb_device_request req;
594         uint16_t ctl;
595
596         DPRINTF("onoff = %d\n", onoff);
597
598         ctl = onoff ? USLCOM_MHS_RTS_ON : 0;
599         ctl |= USLCOM_MHS_RTS_SET;
600
601         req.bmRequestType = USLCOM_WRITE;
602         req.bRequest = USLCOM_SET_MHS;
603         USETW(req.wValue, ctl);
604         USETW(req.wIndex, sc->sc_iface_no);
605         USETW(req.wLength, 0);
606
607         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
608             &req, NULL, 0, 1000)) {
609                 DPRINTF("Setting DTR failed (ignored)\n");
610         }
611 }
612
613 static int
614 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
615 {
616         if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
617                 return (EINVAL);
618         return (0);
619 }
620
621 static void
622 uslcom_param(struct ucom_softc *ucom, struct termios *t)
623 {
624         struct uslcom_softc *sc = ucom->sc_parent;
625         struct usb_device_request req;
626         uint32_t baudrate, flowctrl[4];
627         uint16_t data;
628
629         DPRINTF("\n");
630
631         baudrate = t->c_ospeed;
632         req.bmRequestType = USLCOM_WRITE;
633         req.bRequest = USLCOM_SET_BAUDRATE;
634         USETW(req.wValue, 0);
635         USETW(req.wIndex, sc->sc_iface_no);
636         USETW(req.wLength, sizeof(baudrate));
637
638         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
639             &req, &baudrate, 0, 1000)) {
640                 DPRINTF("Set baudrate failed (ignored)\n");
641         }
642
643         if (t->c_cflag & CSTOPB)
644                 data = USLCOM_STOP_BITS_2;
645         else
646                 data = USLCOM_STOP_BITS_1;
647         if (t->c_cflag & PARENB) {
648                 if (t->c_cflag & PARODD)
649                         data |= USLCOM_PARITY_ODD;
650                 else
651                         data |= USLCOM_PARITY_EVEN;
652         } else
653                 data |= USLCOM_PARITY_NONE;
654         switch (t->c_cflag & CSIZE) {
655         case CS5:
656                 data |= USLCOM_SET_DATA_BITS(5);
657                 break;
658         case CS6:
659                 data |= USLCOM_SET_DATA_BITS(6);
660                 break;
661         case CS7:
662                 data |= USLCOM_SET_DATA_BITS(7);
663                 break;
664         case CS8:
665                 data |= USLCOM_SET_DATA_BITS(8);
666                 break;
667         }
668
669         req.bmRequestType = USLCOM_WRITE;
670         req.bRequest = USLCOM_SET_LINE_CTL;
671         USETW(req.wValue, data);
672         USETW(req.wIndex, sc->sc_iface_no);
673         USETW(req.wLength, 0);
674
675         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
676             &req, NULL, 0, 1000)) {
677                 DPRINTF("Set format failed (ignored)\n");
678         }
679        
680         if (t->c_cflag & CRTSCTS) {
681                 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON | USLCOM_FLOW_CTS_HS);
682                 flowctrl[1] = htole32(USLCOM_FLOW_RTS_HS);
683         } else {
684                 flowctrl[0] = htole32(USLCOM_FLOW_DTR_ON);
685                 flowctrl[1] = htole32(USLCOM_FLOW_RTS_ON);
686         }
687         flowctrl[2] = 0;
688         flowctrl[3] = 0;
689         req.bmRequestType = USLCOM_WRITE;
690         req.bRequest = USLCOM_SET_FLOW;
691         USETW(req.wValue, 0);
692         USETW(req.wIndex, sc->sc_iface_no);
693         USETW(req.wLength, sizeof(flowctrl));
694
695         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
696             &req, flowctrl, 0, 1000)) {
697                 DPRINTF("Set flowcontrol failed (ignored)\n");
698         }
699 }
700
701 static void
702 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
703 {
704         struct uslcom_softc *sc = ucom->sc_parent;
705
706         DPRINTF("\n");
707
708         /* XXX Note: sc_lsr is always zero */
709         *lsr = sc->sc_lsr;
710         *msr = sc->sc_msr;
711 }
712
713 static void
714 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
715 {
716         struct uslcom_softc *sc = ucom->sc_parent;
717         struct usb_device_request req;
718         uint16_t brk = onoff ? USLCOM_SET_BREAK_ON : USLCOM_SET_BREAK_OFF;
719
720         req.bmRequestType = USLCOM_WRITE;
721         req.bRequest = USLCOM_SET_BREAK;
722         USETW(req.wValue, brk);
723         USETW(req.wIndex, sc->sc_iface_no);
724         USETW(req.wLength, 0);
725
726         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
727             &req, NULL, 0, 1000)) {
728                 DPRINTF("Set BREAK failed (ignored)\n");
729         }
730 }
731
732 static int
733 uslcom_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
734     int flag, struct thread *td)
735 {
736         struct uslcom_softc *sc = ucom->sc_parent;
737         struct usb_device_request req;
738         int error = 0;
739         uint8_t latch;
740
741         DPRINTF("cmd=0x%08x\n", cmd);
742
743         switch (cmd) {
744         case USB_GET_GPIO:
745                 if (sc->sc_partnum < USLCOM_PARTNUM_CP2103) {
746                         error = ENODEV;
747                         break;
748                 }
749                 req.bmRequestType = USLCOM_READ;
750                 req.bRequest = USLCOM_VENDOR_SPECIFIC;
751                 USETW(req.wValue, USLCOM_READ_LATCH);
752                 USETW(req.wIndex, sc->sc_iface_no);
753                 USETW(req.wLength, sizeof(latch));
754
755                 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
756                     &req, &latch, 0, 1000)) {
757                         DPRINTF("Get LATCH failed\n");
758                         error = EIO;
759                 }
760                 *(int *)data = latch;
761                 break;
762
763         case USB_SET_GPIO:
764                 if (sc->sc_partnum < USLCOM_PARTNUM_CP2103)
765                         error = ENODEV;
766                 else if ((sc->sc_partnum == USLCOM_PARTNUM_CP2103) ||
767                     (sc->sc_partnum == USLCOM_PARTNUM_CP2104)) {
768                         req.bmRequestType = USLCOM_WRITE;
769                         req.bRequest = USLCOM_VENDOR_SPECIFIC;
770                         USETW(req.wValue, USLCOM_WRITE_LATCH);
771                         USETW(req.wIndex, (*(int *)data));
772                         USETW(req.wLength, 0);
773
774                         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
775                             &req, NULL, 0, 1000)) {
776                                 DPRINTF("Set LATCH failed\n");
777                                 error = EIO;
778                         }
779                 } else
780                         error = ENODEV; /* Not yet */
781                 break;
782
783         default:
784                 DPRINTF("Unknown IOCTL\n");
785                 error = ENOIOCTL;
786                 break;
787         }
788         return (error);
789 }
790
791 static void
792 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
793 {
794         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
795         struct usb_page_cache *pc;
796         uint32_t actlen;
797
798         switch (USB_GET_STATE(xfer)) {
799         case USB_ST_SETUP:
800         case USB_ST_TRANSFERRED:
801 tr_setup:
802                 pc = usbd_xfer_get_frame(xfer, 0);
803                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
804                     USLCOM_BULK_BUF_SIZE, &actlen)) {
805
806                         DPRINTF("actlen = %d\n", actlen);
807
808                         usbd_xfer_set_frame_len(xfer, 0, actlen);
809                         usbd_transfer_submit(xfer);
810                 }
811                 return;
812
813         default:                        /* Error */
814                 if (error != USB_ERR_CANCELLED) {
815                         /* try to clear stall first */
816                         usbd_xfer_set_stall(xfer);
817                         goto tr_setup;
818                 }
819                 return;
820         }
821 }
822
823 static void
824 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
825 {
826         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
827         struct usb_page_cache *pc;
828         int actlen;
829
830         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
831
832         switch (USB_GET_STATE(xfer)) {
833         case USB_ST_TRANSFERRED:
834                 pc = usbd_xfer_get_frame(xfer, 0);
835                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
836
837         case USB_ST_SETUP:
838 tr_setup:
839                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
840                 usbd_transfer_submit(xfer);
841                 return;
842
843         default:                        /* Error */
844                 if (error != USB_ERR_CANCELLED) {
845                         /* try to clear stall first */
846                         usbd_xfer_set_stall(xfer);
847                         goto tr_setup;
848                 }
849                 return;
850         }
851 }
852
853 static void
854 uslcom_control_callback(struct usb_xfer *xfer, usb_error_t error)
855 {
856         struct uslcom_softc *sc = usbd_xfer_softc(xfer);
857         struct usb_page_cache *pc;
858         struct usb_device_request req;
859         uint8_t msr = 0;
860         uint8_t buf;
861
862         switch (USB_GET_STATE(xfer)) {
863         case USB_ST_TRANSFERRED:
864                 pc = usbd_xfer_get_frame(xfer, 1);
865                 usbd_copy_out(pc, 0, &buf, sizeof(buf));
866                 if (buf & USLCOM_MHS_CTS)
867                         msr |= SER_CTS;
868                 if (buf & USLCOM_MHS_DSR)
869                         msr |= SER_DSR;
870                 if (buf & USLCOM_MHS_RI)
871                         msr |= SER_RI;
872                 if (buf & USLCOM_MHS_DCD)
873                         msr |= SER_DCD;
874
875                 if (msr != sc->sc_msr) {
876                         DPRINTF("status change msr=0x%02x "
877                             "(was 0x%02x)\n", msr, sc->sc_msr);
878                         sc->sc_msr = msr;
879                         ucom_status_change(&sc->sc_ucom);
880                 }
881                 break;
882
883         case USB_ST_SETUP:
884                 req.bmRequestType = USLCOM_READ;
885                 req.bRequest = USLCOM_GET_MDMSTS;
886                 USETW(req.wValue, 0);
887                 USETW(req.wIndex, sc->sc_iface_no);
888                 USETW(req.wLength, sizeof(buf));
889                
890                 usbd_xfer_set_frames(xfer, 2);
891                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
892                 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
893
894                 pc = usbd_xfer_get_frame(xfer, 0);
895                 usbd_copy_in(pc, 0, &req, sizeof(req));
896                 usbd_transfer_submit(xfer);
897                 break;
898
899         default:                /* error */
900                 if (error != USB_ERR_CANCELLED)
901                         DPRINTF("error=%s\n", usbd_errstr(error));
902                 break;
903         }
904 }
905
906 static void
907 uslcom_start_read(struct ucom_softc *ucom)
908 {
909         struct uslcom_softc *sc = ucom->sc_parent;
910
911         /* start read endpoint */
912         usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
913 }
914
915 static void
916 uslcom_stop_read(struct ucom_softc *ucom)
917 {
918         struct uslcom_softc *sc = ucom->sc_parent;
919
920         /* stop read endpoint */
921         usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
922 }
923
924 static void
925 uslcom_start_write(struct ucom_softc *ucom)
926 {
927         struct uslcom_softc *sc = ucom->sc_parent;
928
929         usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
930 }
931
932 static void
933 uslcom_stop_write(struct ucom_softc *ucom)
934 {
935         struct uslcom_softc *sc = ucom->sc_parent;
936
937         usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
938 }
939
940 static void
941 uslcom_poll(struct ucom_softc *ucom)
942 {
943         struct uslcom_softc *sc = ucom->sc_parent;
944         usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
945 }