1 /* $NetBSD: usb/uvscom.c,v 1.1 2002/03/19 15:08:42 augustss Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /*
33 * uvscom: SUNTAC Slipper U VS-10U driver.
34 * Slipper U is a PC Card to USB converter for data communication card
35 * adapter. It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
36 * P-in m@ater and various data communication card adapters.
37 */
38
39 #include <sys/stdint.h>
40 #include <sys/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/bus.h>
47 #include <sys/module.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sx.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include "usbdevs.h"
62
63 #define USB_DEBUG_VAR uvscom_debug
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_process.h>
66
67 #include <dev/usb/serial/usb_serial.h>
68
69 #ifdef USB_DEBUG
70 static int uvscom_debug = 0;
71
72 static SYSCTL_NODE(_hw_usb, OID_AUTO, uvscom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
73 "USB uvscom");
74 SYSCTL_INT(_hw_usb_uvscom, OID_AUTO, debug, CTLFLAG_RWTUN,
75 &uvscom_debug, 0, "Debug level");
76 #endif
77
78 #define UVSCOM_MODVER 1 /* module version */
79
80 #define UVSCOM_CONFIG_INDEX 0
81 #define UVSCOM_IFACE_INDEX 0
82
83 /* Request */
84 #define UVSCOM_SET_SPEED 0x10
85 #define UVSCOM_LINE_CTL 0x11
86 #define UVSCOM_SET_PARAM 0x12
87 #define UVSCOM_READ_STATUS 0xd0
88 #define UVSCOM_SHUTDOWN 0xe0
89
90 /* UVSCOM_SET_SPEED parameters */
91 #define UVSCOM_SPEED_150BPS 0x00
92 #define UVSCOM_SPEED_300BPS 0x01
93 #define UVSCOM_SPEED_600BPS 0x02
94 #define UVSCOM_SPEED_1200BPS 0x03
95 #define UVSCOM_SPEED_2400BPS 0x04
96 #define UVSCOM_SPEED_4800BPS 0x05
97 #define UVSCOM_SPEED_9600BPS 0x06
98 #define UVSCOM_SPEED_19200BPS 0x07
99 #define UVSCOM_SPEED_38400BPS 0x08
100 #define UVSCOM_SPEED_57600BPS 0x09
101 #define UVSCOM_SPEED_115200BPS 0x0a
102
103 /* UVSCOM_LINE_CTL parameters */
104 #define UVSCOM_BREAK 0x40
105 #define UVSCOM_RTS 0x02
106 #define UVSCOM_DTR 0x01
107 #define UVSCOM_LINE_INIT 0x08
108
109 /* UVSCOM_SET_PARAM parameters */
110 #define UVSCOM_DATA_MASK 0x03
111 #define UVSCOM_DATA_BIT_8 0x03
112 #define UVSCOM_DATA_BIT_7 0x02
113 #define UVSCOM_DATA_BIT_6 0x01
114 #define UVSCOM_DATA_BIT_5 0x00
115
116 #define UVSCOM_STOP_MASK 0x04
117 #define UVSCOM_STOP_BIT_2 0x04
118 #define UVSCOM_STOP_BIT_1 0x00
119
120 #define UVSCOM_PARITY_MASK 0x18
121 #define UVSCOM_PARITY_EVEN 0x18
122 #define UVSCOM_PARITY_ODD 0x08
123 #define UVSCOM_PARITY_NONE 0x00
124
125 /* Status bits */
126 #define UVSCOM_TXRDY 0x04
127 #define UVSCOM_RXRDY 0x01
128
129 #define UVSCOM_DCD 0x08
130 #define UVSCOM_NOCARD 0x04
131 #define UVSCOM_DSR 0x02
132 #define UVSCOM_CTS 0x01
133 #define UVSCOM_USTAT_MASK (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
134
135 #define UVSCOM_BULK_BUF_SIZE 1024 /* bytes */
136
137 enum {
138 UVSCOM_BULK_DT_WR,
139 UVSCOM_BULK_DT_RD,
140 UVSCOM_INTR_DT_RD,
141 UVSCOM_N_TRANSFER,
142 };
143
144 struct uvscom_softc {
145 struct ucom_super_softc sc_super_ucom;
146 struct ucom_softc sc_ucom;
147
148 struct usb_xfer *sc_xfer[UVSCOM_N_TRANSFER];
149 struct usb_device *sc_udev;
150 struct mtx sc_mtx;
151
152 uint16_t sc_line; /* line control register */
153
154 uint8_t sc_iface_no; /* interface number */
155 uint8_t sc_iface_index; /* interface index */
156 uint8_t sc_lsr; /* local status register */
157 uint8_t sc_msr; /* uvscom status register */
158 uint8_t sc_unit_status; /* unit status */
159 };
160
161 /* prototypes */
162
163 static device_probe_t uvscom_probe;
164 static device_attach_t uvscom_attach;
165 static device_detach_t uvscom_detach;
166 static void uvscom_free_softc(struct uvscom_softc *);
167
168 static usb_callback_t uvscom_write_callback;
169 static usb_callback_t uvscom_read_callback;
170 static usb_callback_t uvscom_intr_callback;
171
172 static void uvscom_free(struct ucom_softc *);
173 static void uvscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
174 static void uvscom_cfg_set_rts(struct ucom_softc *, uint8_t);
175 static void uvscom_cfg_set_break(struct ucom_softc *, uint8_t);
176 static int uvscom_pre_param(struct ucom_softc *, struct termios *);
177 static void uvscom_cfg_param(struct ucom_softc *, struct termios *);
178 static int uvscom_pre_open(struct ucom_softc *);
179 static void uvscom_cfg_open(struct ucom_softc *);
180 static void uvscom_cfg_close(struct ucom_softc *);
181 static void uvscom_start_read(struct ucom_softc *);
182 static void uvscom_stop_read(struct ucom_softc *);
183 static void uvscom_start_write(struct ucom_softc *);
184 static void uvscom_stop_write(struct ucom_softc *);
185 static void uvscom_cfg_get_status(struct ucom_softc *, uint8_t *,
186 uint8_t *);
187 static void uvscom_cfg_write(struct uvscom_softc *, uint8_t, uint16_t);
188 static uint16_t uvscom_cfg_read_status(struct uvscom_softc *);
189 static void uvscom_poll(struct ucom_softc *ucom);
190
191 static const struct usb_config uvscom_config[UVSCOM_N_TRANSFER] = {
192 [UVSCOM_BULK_DT_WR] = {
193 .type = UE_BULK,
194 .endpoint = UE_ADDR_ANY,
195 .direction = UE_DIR_OUT,
196 .bufsize = UVSCOM_BULK_BUF_SIZE,
197 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
198 .callback = &uvscom_write_callback,
199 },
200
201 [UVSCOM_BULK_DT_RD] = {
202 .type = UE_BULK,
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_IN,
205 .bufsize = UVSCOM_BULK_BUF_SIZE,
206 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
207 .callback = &uvscom_read_callback,
208 },
209
210 [UVSCOM_INTR_DT_RD] = {
211 .type = UE_INTERRUPT,
212 .endpoint = UE_ADDR_ANY,
213 .direction = UE_DIR_IN,
214 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
215 .bufsize = 0, /* use wMaxPacketSize */
216 .callback = &uvscom_intr_callback,
217 },
218 };
219
220 static const struct ucom_callback uvscom_callback = {
221 .ucom_cfg_get_status = &uvscom_cfg_get_status,
222 .ucom_cfg_set_dtr = &uvscom_cfg_set_dtr,
223 .ucom_cfg_set_rts = &uvscom_cfg_set_rts,
224 .ucom_cfg_set_break = &uvscom_cfg_set_break,
225 .ucom_cfg_param = &uvscom_cfg_param,
226 .ucom_cfg_open = &uvscom_cfg_open,
227 .ucom_cfg_close = &uvscom_cfg_close,
228 .ucom_pre_open = &uvscom_pre_open,
229 .ucom_pre_param = &uvscom_pre_param,
230 .ucom_start_read = &uvscom_start_read,
231 .ucom_stop_read = &uvscom_stop_read,
232 .ucom_start_write = &uvscom_start_write,
233 .ucom_stop_write = &uvscom_stop_write,
234 .ucom_poll = &uvscom_poll,
235 .ucom_free = &uvscom_free,
236 };
237
238 static const STRUCT_USB_HOST_ID uvscom_devs[] = {
239 /* SUNTAC U-Cable type A4 */
240 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
241 /* SUNTAC U-Cable type D2 */
242 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
243 /* SUNTAC Ir-Trinity */
244 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
245 /* SUNTAC U-Cable type P1 */
246 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
247 /* SUNTAC Slipper U */
248 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
249 };
250
251 static device_method_t uvscom_methods[] = {
252 DEVMETHOD(device_probe, uvscom_probe),
253 DEVMETHOD(device_attach, uvscom_attach),
254 DEVMETHOD(device_detach, uvscom_detach),
255 DEVMETHOD_END
256 };
257
258 static driver_t uvscom_driver = {
259 .name = "uvscom",
260 .methods = uvscom_methods,
261 .size = sizeof(struct uvscom_softc),
262 };
263
264 DRIVER_MODULE(uvscom, uhub, uvscom_driver, NULL, NULL);
265 MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
266 MODULE_DEPEND(uvscom, usb, 1, 1, 1);
267 MODULE_VERSION(uvscom, UVSCOM_MODVER);
268 USB_PNP_HOST_INFO(uvscom_devs);
269
270 static int
uvscom_probe(device_t dev)271 uvscom_probe(device_t dev)
272 {
273 struct usb_attach_arg *uaa = device_get_ivars(dev);
274
275 if (uaa->usb_mode != USB_MODE_HOST) {
276 return (ENXIO);
277 }
278 if (uaa->info.bConfigIndex != UVSCOM_CONFIG_INDEX) {
279 return (ENXIO);
280 }
281 if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
282 return (ENXIO);
283 }
284 return (usbd_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
285 }
286
287 static int
uvscom_attach(device_t dev)288 uvscom_attach(device_t dev)
289 {
290 struct usb_attach_arg *uaa = device_get_ivars(dev);
291 struct uvscom_softc *sc = device_get_softc(dev);
292 int error;
293
294 device_set_usb_desc(dev);
295 mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
296 ucom_ref(&sc->sc_super_ucom);
297
298 sc->sc_udev = uaa->device;
299
300 DPRINTF("sc=%p\n", sc);
301
302 sc->sc_iface_no = uaa->info.bIfaceNum;
303 sc->sc_iface_index = UVSCOM_IFACE_INDEX;
304
305 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
306 sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
307
308 if (error) {
309 DPRINTF("could not allocate all USB transfers!\n");
310 goto detach;
311 }
312 sc->sc_line = UVSCOM_LINE_INIT;
313
314 /* clear stall at first run */
315 mtx_lock(&sc->sc_mtx);
316 usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
317 usbd_xfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
318 mtx_unlock(&sc->sc_mtx);
319
320 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
321 &uvscom_callback, &sc->sc_mtx);
322 if (error) {
323 goto detach;
324 }
325 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
326
327 /* start interrupt pipe */
328 mtx_lock(&sc->sc_mtx);
329 usbd_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
330 mtx_unlock(&sc->sc_mtx);
331
332 return (0);
333
334 detach:
335 uvscom_detach(dev);
336 return (ENXIO);
337 }
338
339 static int
uvscom_detach(device_t dev)340 uvscom_detach(device_t dev)
341 {
342 struct uvscom_softc *sc = device_get_softc(dev);
343
344 DPRINTF("sc=%p\n", sc);
345
346 /* stop interrupt pipe */
347
348 if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
349 usbd_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
350
351 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
352 usbd_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
353
354 device_claim_softc(dev);
355
356 uvscom_free_softc(sc);
357
358 return (0);
359 }
360
361 UCOM_UNLOAD_DRAIN(uvscom);
362
363 static void
uvscom_free_softc(struct uvscom_softc * sc)364 uvscom_free_softc(struct uvscom_softc *sc)
365 {
366 if (ucom_unref(&sc->sc_super_ucom)) {
367 mtx_destroy(&sc->sc_mtx);
368 device_free_softc(sc);
369 }
370 }
371
372 static void
uvscom_free(struct ucom_softc * ucom)373 uvscom_free(struct ucom_softc *ucom)
374 {
375 uvscom_free_softc(ucom->sc_parent);
376 }
377
378 static void
uvscom_write_callback(struct usb_xfer * xfer,usb_error_t error)379 uvscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
380 {
381 struct uvscom_softc *sc = usbd_xfer_softc(xfer);
382 struct usb_page_cache *pc;
383 uint32_t actlen;
384
385 switch (USB_GET_STATE(xfer)) {
386 case USB_ST_SETUP:
387 case USB_ST_TRANSFERRED:
388 tr_setup:
389 pc = usbd_xfer_get_frame(xfer, 0);
390 if (ucom_get_data(&sc->sc_ucom, pc, 0,
391 UVSCOM_BULK_BUF_SIZE, &actlen)) {
392 usbd_xfer_set_frame_len(xfer, 0, actlen);
393 usbd_transfer_submit(xfer);
394 }
395 return;
396
397 default: /* Error */
398 if (error != USB_ERR_CANCELLED) {
399 /* try to clear stall first */
400 usbd_xfer_set_stall(xfer);
401 goto tr_setup;
402 }
403 return;
404 }
405 }
406
407 static void
uvscom_read_callback(struct usb_xfer * xfer,usb_error_t error)408 uvscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
409 {
410 struct uvscom_softc *sc = usbd_xfer_softc(xfer);
411 struct usb_page_cache *pc;
412 int actlen;
413
414 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
415
416 switch (USB_GET_STATE(xfer)) {
417 case USB_ST_TRANSFERRED:
418 pc = usbd_xfer_get_frame(xfer, 0);
419 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
420
421 case USB_ST_SETUP:
422 tr_setup:
423 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
424 usbd_transfer_submit(xfer);
425 return;
426
427 default: /* Error */
428 if (error != USB_ERR_CANCELLED) {
429 /* try to clear stall first */
430 usbd_xfer_set_stall(xfer);
431 goto tr_setup;
432 }
433 return;
434 }
435 }
436
437 static void
uvscom_intr_callback(struct usb_xfer * xfer,usb_error_t error)438 uvscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
439 {
440 struct uvscom_softc *sc = usbd_xfer_softc(xfer);
441 struct usb_page_cache *pc;
442 uint8_t buf[2];
443 int actlen;
444
445 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
446
447 switch (USB_GET_STATE(xfer)) {
448 case USB_ST_TRANSFERRED:
449 if (actlen >= 2) {
450 pc = usbd_xfer_get_frame(xfer, 0);
451 usbd_copy_out(pc, 0, buf, sizeof(buf));
452
453 sc->sc_lsr = 0;
454 sc->sc_msr = 0;
455 sc->sc_unit_status = buf[1];
456
457 if (buf[0] & UVSCOM_TXRDY) {
458 sc->sc_lsr |= ULSR_TXRDY;
459 }
460 if (buf[0] & UVSCOM_RXRDY) {
461 sc->sc_lsr |= ULSR_RXRDY;
462 }
463 if (buf[1] & UVSCOM_CTS) {
464 sc->sc_msr |= SER_CTS;
465 }
466 if (buf[1] & UVSCOM_DSR) {
467 sc->sc_msr |= SER_DSR;
468 }
469 if (buf[1] & UVSCOM_DCD) {
470 sc->sc_msr |= SER_DCD;
471 }
472 /*
473 * the UCOM layer will ignore this call if the TTY
474 * device is closed!
475 */
476 ucom_status_change(&sc->sc_ucom);
477 }
478 case USB_ST_SETUP:
479 tr_setup:
480 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
481 usbd_transfer_submit(xfer);
482 return;
483
484 default: /* Error */
485 if (error != USB_ERR_CANCELLED) {
486 /* try to clear stall first */
487 usbd_xfer_set_stall(xfer);
488 goto tr_setup;
489 }
490 return;
491 }
492 }
493
494 static void
uvscom_cfg_set_dtr(struct ucom_softc * ucom,uint8_t onoff)495 uvscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
496 {
497 struct uvscom_softc *sc = ucom->sc_parent;
498
499 DPRINTF("onoff = %d\n", onoff);
500
501 if (onoff)
502 sc->sc_line |= UVSCOM_DTR;
503 else
504 sc->sc_line &= ~UVSCOM_DTR;
505
506 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
507 }
508
509 static void
uvscom_cfg_set_rts(struct ucom_softc * ucom,uint8_t onoff)510 uvscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
511 {
512 struct uvscom_softc *sc = ucom->sc_parent;
513
514 DPRINTF("onoff = %d\n", onoff);
515
516 if (onoff)
517 sc->sc_line |= UVSCOM_RTS;
518 else
519 sc->sc_line &= ~UVSCOM_RTS;
520
521 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
522 }
523
524 static void
uvscom_cfg_set_break(struct ucom_softc * ucom,uint8_t onoff)525 uvscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
526 {
527 struct uvscom_softc *sc = ucom->sc_parent;
528
529 DPRINTF("onoff = %d\n", onoff);
530
531 if (onoff)
532 sc->sc_line |= UVSCOM_BREAK;
533 else
534 sc->sc_line &= ~UVSCOM_BREAK;
535
536 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
537 }
538
539 static int
uvscom_pre_param(struct ucom_softc * ucom,struct termios * t)540 uvscom_pre_param(struct ucom_softc *ucom, struct termios *t)
541 {
542 switch (t->c_ospeed) {
543 case B150:
544 case B300:
545 case B600:
546 case B1200:
547 case B2400:
548 case B4800:
549 case B9600:
550 case B19200:
551 case B38400:
552 case B57600:
553 case B115200:
554 default:
555 return (EINVAL);
556 }
557 return (0);
558 }
559
560 static void
uvscom_cfg_param(struct ucom_softc * ucom,struct termios * t)561 uvscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
562 {
563 struct uvscom_softc *sc = ucom->sc_parent;
564 uint16_t value;
565
566 DPRINTF("\n");
567
568 switch (t->c_ospeed) {
569 case B150:
570 value = UVSCOM_SPEED_150BPS;
571 break;
572 case B300:
573 value = UVSCOM_SPEED_300BPS;
574 break;
575 case B600:
576 value = UVSCOM_SPEED_600BPS;
577 break;
578 case B1200:
579 value = UVSCOM_SPEED_1200BPS;
580 break;
581 case B2400:
582 value = UVSCOM_SPEED_2400BPS;
583 break;
584 case B4800:
585 value = UVSCOM_SPEED_4800BPS;
586 break;
587 case B9600:
588 value = UVSCOM_SPEED_9600BPS;
589 break;
590 case B19200:
591 value = UVSCOM_SPEED_19200BPS;
592 break;
593 case B38400:
594 value = UVSCOM_SPEED_38400BPS;
595 break;
596 case B57600:
597 value = UVSCOM_SPEED_57600BPS;
598 break;
599 case B115200:
600 value = UVSCOM_SPEED_115200BPS;
601 break;
602 default:
603 return;
604 }
605
606 uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
607
608 value = 0;
609
610 if (t->c_cflag & CSTOPB) {
611 value |= UVSCOM_STOP_BIT_2;
612 }
613 if (t->c_cflag & PARENB) {
614 if (t->c_cflag & PARODD) {
615 value |= UVSCOM_PARITY_ODD;
616 } else {
617 value |= UVSCOM_PARITY_EVEN;
618 }
619 } else {
620 value |= UVSCOM_PARITY_NONE;
621 }
622
623 switch (t->c_cflag & CSIZE) {
624 case CS5:
625 value |= UVSCOM_DATA_BIT_5;
626 break;
627 case CS6:
628 value |= UVSCOM_DATA_BIT_6;
629 break;
630 case CS7:
631 value |= UVSCOM_DATA_BIT_7;
632 break;
633 default:
634 case CS8:
635 value |= UVSCOM_DATA_BIT_8;
636 break;
637 }
638
639 uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
640 }
641
642 static int
uvscom_pre_open(struct ucom_softc * ucom)643 uvscom_pre_open(struct ucom_softc *ucom)
644 {
645 struct uvscom_softc *sc = ucom->sc_parent;
646
647 DPRINTF("sc = %p\n", sc);
648
649 /* check if PC card was inserted */
650
651 if (sc->sc_unit_status & UVSCOM_NOCARD) {
652 DPRINTF("no PC card!\n");
653 return (ENXIO);
654 }
655 return (0);
656 }
657
658 static void
uvscom_cfg_open(struct ucom_softc * ucom)659 uvscom_cfg_open(struct ucom_softc *ucom)
660 {
661 struct uvscom_softc *sc = ucom->sc_parent;
662
663 DPRINTF("sc = %p\n", sc);
664
665 uvscom_cfg_read_status(sc);
666 }
667
668 static void
uvscom_cfg_close(struct ucom_softc * ucom)669 uvscom_cfg_close(struct ucom_softc *ucom)
670 {
671 struct uvscom_softc *sc = ucom->sc_parent;
672
673 DPRINTF("sc=%p\n", sc);
674
675 uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
676 }
677
678 static void
uvscom_start_read(struct ucom_softc * ucom)679 uvscom_start_read(struct ucom_softc *ucom)
680 {
681 struct uvscom_softc *sc = ucom->sc_parent;
682
683 usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
684 }
685
686 static void
uvscom_stop_read(struct ucom_softc * ucom)687 uvscom_stop_read(struct ucom_softc *ucom)
688 {
689 struct uvscom_softc *sc = ucom->sc_parent;
690
691 usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
692 }
693
694 static void
uvscom_start_write(struct ucom_softc * ucom)695 uvscom_start_write(struct ucom_softc *ucom)
696 {
697 struct uvscom_softc *sc = ucom->sc_parent;
698
699 usbd_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
700 }
701
702 static void
uvscom_stop_write(struct ucom_softc * ucom)703 uvscom_stop_write(struct ucom_softc *ucom)
704 {
705 struct uvscom_softc *sc = ucom->sc_parent;
706
707 usbd_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
708 }
709
710 static void
uvscom_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)711 uvscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
712 {
713 struct uvscom_softc *sc = ucom->sc_parent;
714
715 *lsr = sc->sc_lsr;
716 *msr = sc->sc_msr;
717 }
718
719 static void
uvscom_cfg_write(struct uvscom_softc * sc,uint8_t index,uint16_t value)720 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
721 {
722 struct usb_device_request req;
723 usb_error_t err;
724
725 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
726 req.bRequest = index;
727 USETW(req.wValue, value);
728 USETW(req.wIndex, 0);
729 USETW(req.wLength, 0);
730
731 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
732 &req, NULL, 0, 1000);
733 if (err) {
734 DPRINTFN(0, "device request failed, err=%s "
735 "(ignored)\n", usbd_errstr(err));
736 }
737 }
738
739 static uint16_t
uvscom_cfg_read_status(struct uvscom_softc * sc)740 uvscom_cfg_read_status(struct uvscom_softc *sc)
741 {
742 struct usb_device_request req;
743 usb_error_t err;
744 uint8_t data[2];
745
746 req.bmRequestType = UT_READ_VENDOR_DEVICE;
747 req.bRequest = UVSCOM_READ_STATUS;
748 USETW(req.wValue, 0);
749 USETW(req.wIndex, 0);
750 USETW(req.wLength, 2);
751
752 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
753 &req, data, 0, 1000);
754 if (err) {
755 DPRINTFN(0, "device request failed, err=%s "
756 "(ignored)\n", usbd_errstr(err));
757 }
758 return (data[0] | (data[1] << 8));
759 }
760
761 static void
uvscom_poll(struct ucom_softc * ucom)762 uvscom_poll(struct ucom_softc *ucom)
763 {
764 struct uvscom_softc *sc = ucom->sc_parent;
765 usbd_transfer_poll(sc->sc_xfer, UVSCOM_N_TRANSFER);
766 }
767