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