]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/ubser.c
Initial import from vendor-sys branch of openzfs
[FreeBSD/FreeBSD.git] / sys / dev / usb / serial / ubser.c
1 /*-
2  * Copyright (c) 2004 Bernd Walter <ticso@FreeBSD.org>
3  *
4  * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5  * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6  * $Author: ticso $
7  * $Rev: 1127 $
8  */
9
10 /*-
11  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
12  *
13  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 /*-
39  * Copyright (c) 2000 The NetBSD Foundation, Inc.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to The NetBSD Foundation
43  * by Lennart Augustsson (lennart@augustsson.net).
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69
70 /*
71  * BWCT serial adapter driver
72  */
73
74 #include <sys/stdint.h>
75 #include <sys/stddef.h>
76 #include <sys/param.h>
77 #include <sys/queue.h>
78 #include <sys/types.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/module.h>
83 #include <sys/lock.h>
84 #include <sys/mutex.h>
85 #include <sys/condvar.h>
86 #include <sys/sysctl.h>
87 #include <sys/sx.h>
88 #include <sys/unistd.h>
89 #include <sys/callout.h>
90 #include <sys/malloc.h>
91 #include <sys/priv.h>
92
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdi_util.h>
96 #include "usbdevs.h"
97
98 #define USB_DEBUG_VAR ubser_debug
99 #include <dev/usb/usb_debug.h>
100 #include <dev/usb/usb_process.h>
101
102 #include <dev/usb/serial/usb_serial.h>
103
104 #define UBSER_UNIT_MAX  32
105
106 /* Vendor Interface Requests */
107 #define VENDOR_GET_NUMSER               0x01
108 #define VENDOR_SET_BREAK                0x02
109 #define VENDOR_CLEAR_BREAK              0x03
110
111 #ifdef USB_DEBUG
112 static int ubser_debug = 0;
113
114 static SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
115     "USB ubser");
116 SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RWTUN,
117     &ubser_debug, 0, "ubser debug level");
118 #endif
119
120 enum {
121         UBSER_BULK_DT_WR,
122         UBSER_BULK_DT_RD,
123         UBSER_N_TRANSFER,
124 };
125
126 struct ubser_softc {
127         struct ucom_super_softc sc_super_ucom;
128         struct ucom_softc sc_ucom[UBSER_UNIT_MAX];
129
130         struct usb_xfer *sc_xfer[UBSER_N_TRANSFER];
131         struct usb_device *sc_udev;
132         struct mtx sc_mtx;
133
134         uint16_t sc_tx_size;
135
136         uint8_t sc_numser;
137         uint8_t sc_iface_no;
138         uint8_t sc_iface_index;
139         uint8_t sc_curr_tx_unit;
140 };
141
142 /* prototypes */
143
144 static device_probe_t ubser_probe;
145 static device_attach_t ubser_attach;
146 static device_detach_t ubser_detach;
147 static void ubser_free_softc(struct ubser_softc *);
148
149 static usb_callback_t ubser_write_callback;
150 static usb_callback_t ubser_read_callback;
151
152 static void     ubser_free(struct ucom_softc *);
153 static int      ubser_pre_param(struct ucom_softc *, struct termios *);
154 static void     ubser_cfg_set_break(struct ucom_softc *, uint8_t);
155 static void     ubser_cfg_get_status(struct ucom_softc *, uint8_t *,
156                     uint8_t *);
157 static void     ubser_start_read(struct ucom_softc *);
158 static void     ubser_stop_read(struct ucom_softc *);
159 static void     ubser_start_write(struct ucom_softc *);
160 static void     ubser_stop_write(struct ucom_softc *);
161 static void     ubser_poll(struct ucom_softc *ucom);
162
163 static const struct usb_config ubser_config[UBSER_N_TRANSFER] = {
164
165         [UBSER_BULK_DT_WR] = {
166                 .type = UE_BULK,
167                 .endpoint = UE_ADDR_ANY,
168                 .direction = UE_DIR_OUT,
169                 .bufsize = 0,   /* use wMaxPacketSize */
170                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
171                 .callback = &ubser_write_callback,
172         },
173
174         [UBSER_BULK_DT_RD] = {
175                 .type = UE_BULK,
176                 .endpoint = UE_ADDR_ANY,
177                 .direction = UE_DIR_IN,
178                 .bufsize = 0,   /* use wMaxPacketSize */
179                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
180                 .callback = &ubser_read_callback,
181         },
182 };
183
184 static const struct ucom_callback ubser_callback = {
185         .ucom_cfg_set_break = &ubser_cfg_set_break,
186         .ucom_cfg_get_status = &ubser_cfg_get_status,
187         .ucom_pre_param = &ubser_pre_param,
188         .ucom_start_read = &ubser_start_read,
189         .ucom_stop_read = &ubser_stop_read,
190         .ucom_start_write = &ubser_start_write,
191         .ucom_stop_write = &ubser_stop_write,
192         .ucom_poll = &ubser_poll,
193         .ucom_free = &ubser_free,
194 };
195
196 static device_method_t ubser_methods[] = {
197         DEVMETHOD(device_probe, ubser_probe),
198         DEVMETHOD(device_attach, ubser_attach),
199         DEVMETHOD(device_detach, ubser_detach),
200         DEVMETHOD_END
201 };
202
203 static devclass_t ubser_devclass;
204
205 static driver_t ubser_driver = {
206         .name = "ubser",
207         .methods = ubser_methods,
208         .size = sizeof(struct ubser_softc),
209 };
210
211 DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
212 MODULE_DEPEND(ubser, ucom, 1, 1, 1);
213 MODULE_DEPEND(ubser, usb, 1, 1, 1);
214 MODULE_VERSION(ubser, 1);
215
216 static int
217 ubser_probe(device_t dev)
218 {
219         struct usb_attach_arg *uaa = device_get_ivars(dev);
220
221         if (uaa->usb_mode != USB_MODE_HOST) {
222                 return (ENXIO);
223         }
224         /* check if this is a BWCT vendor specific ubser interface */
225         if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) &&
226             (uaa->info.bInterfaceClass == 0xff) &&
227             (uaa->info.bInterfaceSubClass == 0x00))
228                 return (0);
229
230         return (ENXIO);
231 }
232
233 static int
234 ubser_attach(device_t dev)
235 {
236         struct usb_attach_arg *uaa = device_get_ivars(dev);
237         struct ubser_softc *sc = device_get_softc(dev);
238         struct usb_device_request req;
239         uint8_t n;
240         int error;
241
242         device_set_usb_desc(dev);
243         mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
244         ucom_ref(&sc->sc_super_ucom);
245
246         sc->sc_iface_no = uaa->info.bIfaceNum;
247         sc->sc_iface_index = uaa->info.bIfaceIndex;
248         sc->sc_udev = uaa->device;
249
250         /* get number of serials */
251         req.bmRequestType = UT_READ_VENDOR_INTERFACE;
252         req.bRequest = VENDOR_GET_NUMSER;
253         USETW(req.wValue, 0);
254         req.wIndex[0] = sc->sc_iface_no;
255         req.wIndex[1] = 0;
256         USETW(req.wLength, 1);
257         error = usbd_do_request_flags(uaa->device, NULL,
258             &req, &sc->sc_numser,
259             0, NULL, USB_DEFAULT_TIMEOUT);
260
261         if (error || (sc->sc_numser == 0)) {
262                 device_printf(dev, "failed to get number "
263                     "of serial ports: %s\n",
264                     usbd_errstr(error));
265                 goto detach;
266         }
267         if (sc->sc_numser > UBSER_UNIT_MAX)
268                 sc->sc_numser = UBSER_UNIT_MAX;
269
270         device_printf(dev, "found %i serials\n", sc->sc_numser);
271
272         error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
273             sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
274         if (error) {
275                 goto detach;
276         }
277         sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]);
278
279         if (sc->sc_tx_size == 0) {
280                 DPRINTFN(0, "invalid tx_size\n");
281                 goto detach;
282         }
283         /* initialize port numbers */
284
285         for (n = 0; n < sc->sc_numser; n++) {
286                 sc->sc_ucom[n].sc_portno = n;
287         }
288
289         error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
290             sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
291         if (error) {
292                 goto detach;
293         }
294         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
295
296         mtx_lock(&sc->sc_mtx);
297         usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
298         usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
299         usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
300         mtx_unlock(&sc->sc_mtx);
301
302         return (0);                     /* success */
303
304 detach:
305         ubser_detach(dev);
306         return (ENXIO);                 /* failure */
307 }
308
309 static int
310 ubser_detach(device_t dev)
311 {
312         struct ubser_softc *sc = device_get_softc(dev);
313
314         DPRINTF("\n");
315
316         ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
317         usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
318
319         device_claim_softc(dev);
320
321         ubser_free_softc(sc);
322
323         return (0);
324 }
325
326 UCOM_UNLOAD_DRAIN(ubser);
327
328 static void
329 ubser_free_softc(struct ubser_softc *sc)
330 {
331         if (ucom_unref(&sc->sc_super_ucom)) {
332                 mtx_destroy(&sc->sc_mtx);
333                 device_free_softc(sc);
334         }
335 }
336
337 static void
338 ubser_free(struct ucom_softc *ucom)
339 {
340         ubser_free_softc(ucom->sc_parent);
341 }
342
343 static int
344 ubser_pre_param(struct ucom_softc *ucom, struct termios *t)
345 {
346         DPRINTF("\n");
347
348         /*
349          * The firmware on our devices can only do 8n1@9600bps
350          * without handshake.
351          * We refuse to accept other configurations.
352          */
353
354         /* ensure 9600bps */
355         switch (t->c_ospeed) {
356         case 9600:
357                 break;
358         default:
359                 return (EINVAL);
360         }
361
362         /* 2 stop bits not possible */
363         if (t->c_cflag & CSTOPB)
364                 return (EINVAL);
365
366         /* XXX parity handling not possible with current firmware */
367         if (t->c_cflag & PARENB)
368                 return (EINVAL);
369
370         /* we can only do 8 data bits */
371         switch (t->c_cflag & CSIZE) {
372         case CS8:
373                 break;
374         default:
375                 return (EINVAL);
376         }
377
378         /* we can't do any kind of hardware handshaking */
379         if ((t->c_cflag &
380             (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
381                 return (EINVAL);
382
383         /*
384          * XXX xon/xoff not supported by the firmware!
385          * This is handled within FreeBSD only and may overflow buffers
386          * because of delayed reaction due to device buffering.
387          */
388
389         return (0);
390 }
391
392 static __inline void
393 ubser_inc_tx_unit(struct ubser_softc *sc)
394 {
395         sc->sc_curr_tx_unit++;
396         if (sc->sc_curr_tx_unit >= sc->sc_numser) {
397                 sc->sc_curr_tx_unit = 0;
398         }
399 }
400
401 static void
402 ubser_write_callback(struct usb_xfer *xfer, usb_error_t error)
403 {
404         struct ubser_softc *sc = usbd_xfer_softc(xfer);
405         struct usb_page_cache *pc;
406         uint8_t buf[1];
407         uint8_t first_unit = sc->sc_curr_tx_unit;
408         uint32_t actlen;
409
410         switch (USB_GET_STATE(xfer)) {
411         case USB_ST_SETUP:
412         case USB_ST_TRANSFERRED:
413 tr_setup:
414                 pc = usbd_xfer_get_frame(xfer, 0);
415                 do {
416                         if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
417                             pc, 1, sc->sc_tx_size - 1,
418                             &actlen)) {
419
420                                 buf[0] = sc->sc_curr_tx_unit;
421
422                                 usbd_copy_in(pc, 0, buf, 1);
423
424                                 usbd_xfer_set_frame_len(xfer, 0, actlen + 1);
425                                 usbd_transfer_submit(xfer);
426
427                                 ubser_inc_tx_unit(sc);  /* round robin */
428
429                                 break;
430                         }
431                         ubser_inc_tx_unit(sc);
432
433                 } while (sc->sc_curr_tx_unit != first_unit);
434
435                 return;
436
437         default:                        /* Error */
438                 if (error != USB_ERR_CANCELLED) {
439                         /* try to clear stall first */
440                         usbd_xfer_set_stall(xfer);
441                         goto tr_setup;
442                 }
443                 return;
444
445         }
446 }
447
448 static void
449 ubser_read_callback(struct usb_xfer *xfer, usb_error_t error)
450 {
451         struct ubser_softc *sc = usbd_xfer_softc(xfer);
452         struct usb_page_cache *pc;
453         uint8_t buf[1];
454         int actlen;
455
456         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
457
458         switch (USB_GET_STATE(xfer)) {
459         case USB_ST_TRANSFERRED:
460                 if (actlen < 1) {
461                         DPRINTF("invalid actlen=0!\n");
462                         goto tr_setup;
463                 }
464                 pc = usbd_xfer_get_frame(xfer, 0);
465                 usbd_copy_out(pc, 0, buf, 1);
466
467                 if (buf[0] >= sc->sc_numser) {
468                         DPRINTF("invalid serial number!\n");
469                         goto tr_setup;
470                 }
471                 ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1);
472
473         case USB_ST_SETUP:
474 tr_setup:
475                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
476                 usbd_transfer_submit(xfer);
477                 return;
478
479         default:                        /* Error */
480                 if (error != USB_ERR_CANCELLED) {
481                         /* try to clear stall first */
482                         usbd_xfer_set_stall(xfer);
483                         goto tr_setup;
484                 }
485                 return;
486
487         }
488 }
489
490 static void
491 ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
492 {
493         struct ubser_softc *sc = ucom->sc_parent;
494         uint8_t x = ucom->sc_portno;
495         struct usb_device_request req;
496         usb_error_t err;
497
498         if (onoff) {
499
500                 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
501                 req.bRequest = VENDOR_SET_BREAK;
502                 req.wValue[0] = x;
503                 req.wValue[1] = 0;
504                 req.wIndex[0] = sc->sc_iface_no;
505                 req.wIndex[1] = 0;
506                 USETW(req.wLength, 0);
507
508                 err = ucom_cfg_do_request(sc->sc_udev, ucom, 
509                     &req, NULL, 0, 1000);
510                 if (err) {
511                         DPRINTFN(0, "send break failed, error=%s\n",
512                             usbd_errstr(err));
513                 }
514         }
515 }
516
517 static void
518 ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
519 {
520         /* fake status bits */
521         *lsr = 0;
522         *msr = SER_DCD;
523 }
524
525 static void
526 ubser_start_read(struct ucom_softc *ucom)
527 {
528         struct ubser_softc *sc = ucom->sc_parent;
529
530         usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
531 }
532
533 static void
534 ubser_stop_read(struct ucom_softc *ucom)
535 {
536         struct ubser_softc *sc = ucom->sc_parent;
537
538         usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
539 }
540
541 static void
542 ubser_start_write(struct ucom_softc *ucom)
543 {
544         struct ubser_softc *sc = ucom->sc_parent;
545
546         usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
547 }
548
549 static void
550 ubser_stop_write(struct ucom_softc *ucom)
551 {
552         struct ubser_softc *sc = ucom->sc_parent;
553
554         usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
555 }
556
557 static void
558 ubser_poll(struct ucom_softc *ucom)
559 {
560         struct ubser_softc *sc = ucom->sc_parent;
561         usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER);
562 }