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