Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb Add KingSun/DonShine IRDA dongle.



details:   https://anonhg.NetBSD.org/src/rev/6912a4c8d67e
branches:  trunk
changeset: 786989:6912a4c8d67e
user:      kiyohara <kiyohara%NetBSD.org@localhost>
date:      Tue May 28 12:03:26 2013 +0000

description:
Add KingSun/DonShine IRDA dongle.

diffstat:

 sys/dev/usb/files.usb |     7 +-
 sys/dev/usb/udsir.c   |  1056 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 1062 insertions(+), 1 deletions(-)

diffs (truncated from 1078 to 300 lines):

diff -r 1e333b138d75 -r 6912a4c8d67e sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Tue May 28 11:04:04 2013 +0000
+++ b/sys/dev/usb/files.usb     Tue May 28 12:03:26 2013 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.128 2013/03/30 02:37:18 christos Exp $
+#      $NetBSD: files.usb,v 1.129 2013/05/28 12:03:26 kiyohara Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -453,3 +453,8 @@
 device rsu: arp, ether, firmload, ifnet, wlan
 attach rsu at usbdevif
 file   dev/usb/if_rsu.c                rsu
+
+# KingSun/DonShine IrDA adaptors
+device udsir: irbus, irdasir
+attach udsir at usbifif
+file   dev/usb/udsir.c                 udsir
diff -r 1e333b138d75 -r 6912a4c8d67e sys/dev/usb/udsir.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/usb/udsir.c       Tue May 28 12:03:26 2013 +0000
@@ -0,0 +1,1056 @@
+/*     $NetBSD: udsir.c,v 1.1 2013/05/28 12:03:26 kiyohara Exp $       */
+
+/*
+ * Copyright (c) 2001 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by David Sainty <David.Sainty%dtsp.co.nz@localhost>
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: udsir.c,v 1.1 2013/05/28 12:03:26 kiyohara Exp $");
+
+#include <sys/param.h>
+#include <sys/device.h>
+#include <sys/errno.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/conf.h>
+#include <sys/file.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+#include <sys/proc.h>
+#include <sys/kthread.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+
+#include <dev/ir/ir.h>
+#include <dev/ir/irdaio.h>
+#include <dev/ir/irframevar.h>
+#include <dev/ir/sir.h>
+
+#ifdef UDSIR_DEBUG
+#define DPRINTFN(n,x)  if (udsirdebug > (n)) printf x
+int    udsirdebug = 0;
+#else
+#define DPRINTFN(n,x)
+#endif
+
+/* Max size with framing. */
+#define MAX_UDSIR_OUTPUT_FRAME (2 * IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4)
+
+struct udsir_softc {
+       device_t                sc_dev;
+       usbd_device_handle      sc_udev;
+       usbd_interface_handle   sc_iface;
+
+       uint8_t                 *sc_ur_buf; /* Unencapsulated frame */
+       u_int                   sc_ur_framelen;
+
+       uint8_t                 *sc_rd_buf; /* Raw incoming data stream */
+       int                     sc_rd_maxpsz;
+       size_t                  sc_rd_index;
+       int                     sc_rd_addr;
+       usbd_pipe_handle        sc_rd_pipe;
+       usbd_xfer_handle        sc_rd_xfer;
+       u_int                   sc_rd_count;
+       int                     sc_rd_readinprogress;
+       int                     sc_rd_expectdataticks;
+       u_char                  sc_rd_err;
+       struct framestate       sc_framestate;
+       struct lwp              *sc_thread;
+       struct selinfo          sc_rd_sel;
+
+       uint8_t                 *sc_wr_buf;
+       int                     sc_wr_maxpsz;
+       int                     sc_wr_addr;
+       int                     sc_wr_stalewrite;
+       usbd_xfer_handle        sc_wr_xfer;
+       usbd_pipe_handle        sc_wr_pipe;
+       struct selinfo          sc_wr_sel;
+
+       enum {
+               udir_input, /* Receiving data */
+               udir_output, /* Transmitting data */
+               udir_stalled, /* Error preventing data flow */
+               udir_idle /* Neither receiving nor transmitting */
+       } sc_direction;
+
+       device_t                sc_child;
+       struct irda_params      sc_params;
+
+       int                     sc_refcnt;
+       char                    sc_closing;
+       char                    sc_dying;
+};
+
+/* True if we cannot safely read data from the device */
+#define UDSIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
+
+#define UDSIR_WR_TIMEOUT 200
+
+static int udsir_match(device_t, cfdata_t, void *);
+static void udsir_attach(device_t, device_t, void *);
+static int udsir_detach(device_t, int);
+static void udsir_childdet(device_t, device_t);
+static int udsir_activate(device_t, enum devact);
+
+static int udsir_open(void *, int, int, struct lwp *);
+static int udsir_close(void *, int, int, struct lwp *);
+static int udsir_read(void *, struct uio *, int);
+static int udsir_write(void *, struct uio *, int);
+static int udsir_poll(void *, int, struct lwp *);
+static int udsir_kqfilter(void *, struct knote *);
+static int udsir_set_params(void *, struct irda_params *);
+static int udsir_get_speeds(void *, int *);
+static int udsir_get_turnarounds(void *, int *);
+
+static void filt_udsirrdetach(struct knote *);
+static int filt_udsirread(struct knote *, long);
+static void filt_udsirwdetach(struct knote *);
+static int filt_udsirwrite(struct knote *, long);
+
+static void udsir_thread(void *);
+
+#ifdef UDSIR_DEBUG
+static void udsir_dumpdata(uint8_t const *, size_t, char const *);
+#endif
+static int deframe_rd_ur(struct udsir_softc *);
+static void udsir_periodic(struct udsir_softc *);
+static void udsir_rd_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
+static usbd_status udsir_start_read(struct udsir_softc *);
+
+CFATTACH_DECL2_NEW(udsir, sizeof(struct udsir_softc),
+    udsir_match, udsir_attach, udsir_detach,
+    udsir_activate, NULL, udsir_childdet);
+
+static struct irframe_methods const udsir_methods = {
+    udsir_open, udsir_close, udsir_read, udsir_write, udsir_poll,
+    udsir_kqfilter, udsir_set_params, udsir_get_speeds, udsir_get_turnarounds,
+};
+
+static int
+udsir_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct usbif_attach_arg *uaa = aux;
+
+       DPRINTFN(50, ("udsir_match\n"));
+
+       if (uaa->vendor == USB_VENDOR_KINGSUN &&
+           uaa->product == USB_PRODUCT_KINGSUN_IRDA)
+               return UMATCH_VENDOR_PRODUCT;
+
+       return UMATCH_NONE;
+}
+
+static void
+udsir_attach(device_t parent, device_t self, void *aux)
+{
+       struct udsir_softc *sc = device_private(self);
+       struct usbif_attach_arg *uaa = aux;
+       usbd_device_handle dev = uaa->device;
+       usbd_interface_handle iface = uaa->iface;
+       char *devinfop;
+       usb_endpoint_descriptor_t *ed;
+       uint8_t epcount;
+       int i;
+       struct ir_attach_args ia;
+
+       DPRINTFN(10, ("udsir_attach: sc=%p\n", sc));
+
+       sc->sc_dev = self;
+
+       aprint_naive("\n");
+       aprint_normal("\n");
+
+       devinfop = usbd_devinfo_alloc(dev, 0);
+       aprint_normal_dev(self, "%s\n", devinfop);
+       usbd_devinfo_free(devinfop);
+
+       sc->sc_udev = dev;
+       sc->sc_iface = iface;
+
+       epcount = 0;
+       (void)usbd_endpoint_count(iface, &epcount);
+
+       sc->sc_rd_addr = -1;
+       sc->sc_wr_addr = -1;
+       for (i = 0; i < epcount; i++) {
+               ed = usbd_interface2endpoint_descriptor(iface, i);
+               if (ed == NULL) {
+                       aprint_error_dev(self, "couldn't get ep %d\n", i);
+                       return;
+               }
+               if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+                       sc->sc_rd_addr = ed->bEndpointAddress;
+                       sc->sc_rd_maxpsz = UGETW(ed->wMaxPacketSize);
+               } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+                          UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+                       sc->sc_wr_addr = ed->bEndpointAddress;
+                       sc->sc_wr_maxpsz = UGETW(ed->wMaxPacketSize);
+               }
+       }
+       if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
+               aprint_error_dev(self, "missing endpoint\n");
+               return;
+       }
+
+       DPRINTFN(10, ("udsir_attach: %p\n", sc->sc_udev));
+
+       usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
+                          sc->sc_dev);
+
+       ia.ia_type = IR_TYPE_IRFRAME;
+       ia.ia_methods = &udsir_methods;
+       ia.ia_handle = sc;
+
+       sc->sc_child = config_found(self, &ia, ir_print);
+       selinit(&sc->sc_rd_sel);
+       selinit(&sc->sc_wr_sel);
+
+       return;
+}
+
+static int
+udsir_detach(device_t self, int flags)
+{
+       struct udsir_softc *sc = device_private(self);
+       int s;
+       int rv = 0;
+
+       DPRINTFN(0, ("udsir_detach: sc=%p flags=%d\n", sc, flags));
+
+       sc->sc_closing = sc->sc_dying = 1;
+
+       wakeup(&sc->sc_thread);
+
+       while (sc->sc_thread != NULL)
+               tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
+
+       /* Abort all pipes.  Causes processes waiting for transfer to wake. */
+       if (sc->sc_rd_pipe != NULL) {
+               usbd_abort_pipe(sc->sc_rd_pipe);
+               usbd_close_pipe(sc->sc_rd_pipe);
+               sc->sc_rd_pipe = NULL;
+       }
+       if (sc->sc_wr_pipe != NULL) {
+               usbd_abort_pipe(sc->sc_wr_pipe);
+               usbd_close_pipe(sc->sc_wr_pipe);
+               sc->sc_wr_pipe = NULL;
+       }
+       wakeup(&sc->sc_ur_framelen);
+       wakeup(&sc->sc_wr_buf);
+
+       s = splusb();
+       if (--sc->sc_refcnt >= 0) {
+               /* Wait for processes to go away. */
+               usb_detach_waitold(sc->sc_dev);
+       }
+       splx(s);
+
+       if (sc->sc_child != NULL)



Home | Main Index | Thread Index | Old Index