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