Module Name: src Committed By: pooka Date: Sun Mar 7 23:28:14 UTC 2010
Modified Files: src/share/examples/rump: Makefile Added Files: src/share/examples/rump/ttyserv: Makefile ttyserv.c Log Message: Add a simple tty server which attaches to /dev/ttyU in the rump kernel and provides the same device on the host with pud(4). I can succesfully talk to my u3g device using tip(1) against the server's devices. Some characters are lost here and there, though (probably a problem with interrupt pipes in ugenhc). Once that issues is solved, there is no reason why e.g. pppd (chat) could not be used against the server. Now, why would anyone want to do this instead of using the kernel driver directly? Well, for one, on my laptop I run netbsd-5 which doesn't have a u3g driver which supports the u3g hardware I have. As anyone with half an eye can see, this is quite copypasted from umserv and there is plenty of opportunity for uncopypasting for the eager vi-wielder. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/share/examples/rump/Makefile cvs rdiff -u -r0 -r1.1 src/share/examples/rump/ttyserv/Makefile \ src/share/examples/rump/ttyserv/ttyserv.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/share/examples/rump/Makefile diff -u src/share/examples/rump/Makefile:1.2 src/share/examples/rump/Makefile:1.3 --- src/share/examples/rump/Makefile:1.2 Mon Jan 11 02:20:06 2010 +++ src/share/examples/rump/Makefile Sun Mar 7 23:28:14 2010 @@ -1,6 +1,7 @@ -# $NetBSD: Makefile,v 1.2 2010/01/11 02:20:06 pooka Exp $ +# $NetBSD: Makefile,v 1.3 2010/03/07 23:28:14 pooka Exp $ # -SUBDIR= img2cgd sdread tipsy ulptprint umserv ukbd_read ums_draw wirelessconf +SUBDIR= img2cgd sdread swwdog_arm tipsy ttyserv ulptprint umserv \ + ukbd_read ums_draw wirelessconf .include <bsd.subdir.mk> Added files: Index: src/share/examples/rump/ttyserv/Makefile diff -u /dev/null src/share/examples/rump/ttyserv/Makefile:1.1 --- /dev/null Sun Mar 7 23:28:14 2010 +++ src/share/examples/rump/ttyserv/Makefile Sun Mar 7 23:28:14 2010 @@ -0,0 +1,10 @@ +PROG=ttyserv +NOMAN= +LDADD+=-lrumpdev_ucom -lrumpdev_ugenhc -lrumpdev_usb -lrumpdev +LDADD+=-lrumpvfs +LDADD+=-lrump -lrumpuser -lpthread + +WARNS=4 +DBG=-g + +.include <bsd.prog.mk> Index: src/share/examples/rump/ttyserv/ttyserv.c diff -u /dev/null src/share/examples/rump/ttyserv/ttyserv.c:1.1 --- /dev/null Sun Mar 7 23:28:14 2010 +++ src/share/examples/rump/ttyserv/ttyserv.c Sun Mar 7 23:28:14 2010 @@ -0,0 +1,223 @@ +/* $NetBSD: ttyserv.c,v 1.1 2010/03/07 23:28:14 pooka Exp $ */ + +/* + * Copyright (c) 2009, 2010 Antti Kantee. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * This is a [quick, simple & dirty] userspace tty/ucom server. + * We probe USB devices using rump and attach them to the host kernel + * using pud(4). + */ + +#include <sys/types.h> +#include <sys/syslimits.h> + +#include <dev/pud/pud_msgif.h> + +#include <rump/rump.h> + +#include <assert.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <paths.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +/* + * No devfs? No problem. We just hack a bit & wait for the dust to settle. + */ +#define MYMAJOR 412 +static int +makenodes(void) +{ + struct stat sb; + int rv; + + if (stat("rumpttyU0", &sb) != 0) + rv = mknod("rumpttyU0", S_IFCHR | 0666, makedev(MYMAJOR, 0)); + if (rv != 0 && !(rv == -1 && errno == EEXIST)) + return rv; + + if (stat("rumpttyU1", &sb) != 0) + rv = mknod("rumpttyU1", S_IFCHR | 0666, makedev(MYMAJOR, 1)); + if (rv != 0 && !(rv == -1 && errno == EEXIST)) + return rv; + + return 0; +} + +static struct vnode *devvps[2]; +static kauth_cred_t rootcred; +static int fd; + +static void * +handlereq(void *arg) +{ + struct vnode *devvp; + struct pud_creq_open *pr_open; + struct pud_creq_close *pr_close; + struct pud_req_readwrite *pr_rw; + struct pud_req_ioctl *pr_ioctl; + struct uio *uio; + size_t reslen; + struct pud_req *pdr = arg; + int minordev, rv; + ssize_t n; + + minordev = minor(pdr->pdr_dev); + if (minordev < 0 || minordev >= 8) { + rv = ENXIO; + goto sendresponse; + } + devvp = devvps[minordev]; + + switch (pdr->pdr_reqtype) { + case PUD_CDEV_OPEN: + pr_open = (void *)pdr; + RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE); + rv = RUMP_VOP_OPEN(devvp, pr_open->pm_fmt, rootcred); + RUMP_VOP_UNLOCK(devvp, 0); + break; + + case PUD_CDEV_CLOSE: + pr_close = (void *)pdr; + RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE); + rv = RUMP_VOP_CLOSE(devvp, pr_close->pm_fmt, rootcred); + RUMP_VOP_UNLOCK(devvp, 0); + break; + + case PUD_CDEV_IOCTL: + pr_ioctl = (void *)pdr; + rv = RUMP_VOP_IOCTL(devvp, pr_ioctl->pm_iocmd, + pr_ioctl->pm_data, pr_ioctl->pm_flag, rootcred); + break; + + case PUD_CDEV_READ: + pr_rw = (void *)pdr; + assert(pr_rw->pm_resid <= 64*1024); + uio = rump_pub_uio_setup(&pr_rw->pm_data[0], + pr_rw->pm_resid, pr_rw->pm_offset, RUMPUIO_READ); + RUMP_VOP_LOCK(devvp, RUMP_LK_SHARED); + rv = RUMP_VOP_READ(devvp, uio, 0, rootcred); + RUMP_VOP_UNLOCK(devvp, 0); + reslen = rump_pub_uio_free(uio); + pdr->pdr_pth.pth_framelen -= reslen; + pr_rw->pm_resid = reslen; + break; + + case PUD_CDEV_WRITE: + pr_rw = (void *)pdr; + uio = rump_pub_uio_setup(&pr_rw->pm_data[0], + pr_rw->pm_resid, pr_rw->pm_offset, RUMPUIO_WRITE); + RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE); + rv = RUMP_VOP_WRITE(devvp, uio, 0, rootcred); + RUMP_VOP_UNLOCK(devvp, 0); + reslen = rump_pub_uio_free(uio); + pr_rw->pm_resid = reslen; + pdr->pdr_pth.pth_framelen=sizeof(struct pud_creq_write); + rv = 0; + break; + default: + printf("unknown request %d\n", pdr->pdr_reqtype); + abort(); + } + +sendresponse: + printf("result %d\n", rv); + pdr->pdr_rv = rv; + n = write(fd, pdr, pdr->pdr_pth.pth_framelen); + assert(n == (ssize_t)pdr->pdr_pth.pth_framelen); + + pthread_exit(NULL); +} + +int +main(int argc, char *argv[]) +{ + struct pud_conf_reg pcr; + struct pud_req *pdr; + ssize_t n; + int rv; + + if (makenodes() == -1) + err(1, "makenodes"); + + fd = open(_PATH_PUD, O_RDWR); + if (fd == -1) + err(1, "open"); + + memset(&pcr, 0, sizeof(pcr)); + pcr.pm_pdr.pdr_pth.pth_framelen = sizeof(struct pud_conf_reg); + pcr.pm_version = PUD_DEVELVERSION | PUD_VERSION; + pcr.pm_pdr.pdr_reqclass = PUD_REQ_CONF; + pcr.pm_pdr.pdr_reqtype = PUD_CONF_REG; + + pcr.pm_regdev = makedev(MYMAJOR, 0); + pcr.pm_flags = PUD_CONFFLAG_BDEV; + strlcpy(pcr.pm_devname, "youmass", sizeof(pcr.pm_devname)); + + n = write(fd, &pcr, pcr.pm_pdr.pdr_pth.pth_framelen); + if (n == -1) + err(1, "configure"); /* XXX: doubles as protocol error */ + + rump_boot_sethowto(RUMP_AB_VERBOSE); + rump_init(); + + if ((rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, 0, "/dev/ttyU0", + NULL, &devvps[0], NULL)) != 0) + errx(1, "raw device 0 lookup failed %d", rv); + if ((rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, 0, "/dev/ttyU1", + NULL, &devvps[1], NULL)) != 0) + errx(1, "raw device 1 lookup failed %d", rv); + + rootcred = rump_pub_cred_create(0, 0, 0, NULL); + + /* process requests ad infinitum */ + for (;;) { + pthread_t pt; + +#define PDRSIZE (64*1024+1024) + pdr = malloc(PDRSIZE); + if (pdr == NULL) + err(1, "malloc"); + + n = read(fd, pdr, PDRSIZE); + if (n == -1) + err(1, "read"); + + /* + * tip & cu fork and read/write at the same time. hence, + * we need a multithreaded server as otherwise read requests + * will block eternally since no writes can be done. + * + * XXX: do this properly (detached threads, or pool) + */ + pthread_create(&pt, NULL, handlereq, pdr); + } +}