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