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 driver for SMK eHome Infrared Transceiver



details:   https://anonhg.NetBSD.org/src/rev/a932a8812e5b
branches:  trunk
changeset: 767464:a932a8812e5b
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Tue Jul 19 12:23:04 2011 +0000

description:
add driver for SMK eHome Infrared Transceiver

diffstat:

 sys/dev/usb/files.usb |    7 +-
 sys/dev/usb/irmce.c   |  626 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 632 insertions(+), 1 deletions(-)

diffs (truncated from 651 to 300 lines):

diff -r cdb40c6f87d0 -r a932a8812e5b sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Tue Jul 19 07:29:39 2011 +0000
+++ b/sys/dev/usb/files.usb     Tue Jul 19 12:23:04 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.108 2011/07/11 18:02:04 jmcneill Exp $
+#      $NetBSD: files.usb,v 1.109 2011/07/19 12:23:04 jmcneill Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -128,6 +128,11 @@
 attach ustir at usbdevif
 file   dev/usb/ustir.c                 ustir
 
+# Windows Media Center IR tranceivers
+device irmce: irbus
+attach irmce at usbifif
+file   dev/usb/irmce.c                 irmce
+
 
 # Bluetooth controllers
 device ubt: btbus, bluetooth
diff -r cdb40c6f87d0 -r a932a8812e5b sys/dev/usb/irmce.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/usb/irmce.c       Tue Jul 19 12:23:04 2011 +0000
@@ -0,0 +1,626 @@
+/* $NetBSD: irmce.c,v 1.1 2011/07/19 12:23:04 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * 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 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.
+ */
+
+/*
+ * IR receiver/transceiver for Windows Media Center
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: irmce.c,v 1.1 2011/07/19 12:23:04 jmcneill Exp $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+#include <sys/bus.h>
+#include <sys/select.h>
+#include <sys/module.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+
+#include <dev/ir/ir.h>
+#include <dev/ir/cirio.h>
+#include <dev/ir/cirvar.h>
+
+enum irmce_state {
+       IRMCE_STATE_HEADER,
+       IRMCE_STATE_IRDATA,
+       IRMCE_STATE_CMDHEADER,
+       IRMCE_STATE_CMDDATA,
+};
+
+struct irmce_softc {
+       device_t                sc_dev;
+       device_t                sc_cirdev;
+
+       usbd_device_handle      sc_udev;
+       usbd_interface_handle   sc_iface;
+
+       int                     sc_bulkin_ep;
+       uint16_t                sc_bulkin_maxpktsize;
+       usbd_pipe_handle        sc_bulkin_pipe;
+       usbd_xfer_handle        sc_bulkin_xfer;
+       uint8_t *               sc_bulkin_buffer;
+
+       int                     sc_bulkout_ep;
+       uint16_t                sc_bulkout_maxpktsize;
+       usbd_pipe_handle        sc_bulkout_pipe;
+       usbd_xfer_handle        sc_bulkout_xfer;
+       uint8_t *               sc_bulkout_buffer;
+
+       bool                    sc_raw;
+
+       uint8_t                 sc_ir_buf[16];
+       size_t                  sc_ir_bufused;
+       size_t                  sc_ir_resid;
+       enum irmce_state        sc_ir_state;
+       uint8_t                 sc_ir_header;
+
+       bool                    sc_rc6_hb[256];
+       size_t                  sc_rc6_nhb;
+};
+
+static int     irmce_match(device_t, cfdata_t, void *);
+static void    irmce_attach(device_t, device_t, void *);
+static int     irmce_detach(device_t, int);
+static void    irmce_childdet(device_t, device_t);
+static int     irmce_activate(device_t, enum devact);
+static int     irmce_rescan(device_t, const char *, const int *);
+
+static int     irmce_print(void *, const char *);
+
+static int     irmce_reset(struct irmce_softc *);
+
+static int     irmce_open(void *, int, int, struct proc *);
+static int     irmce_close(void *, int, int, struct proc *);
+static int     irmce_read(void *, struct uio *, int);
+static int     irmce_write(void *, struct uio *, int);
+static int     irmce_setparams(void *, struct cir_params *);
+
+static const struct cir_methods irmce_cir_methods = {
+       .im_open = irmce_open,
+       .im_close = irmce_close,
+       .im_read = irmce_read,
+       .im_write = irmce_write,
+       .im_setparams = irmce_setparams,
+};
+
+static const struct {
+       uint16_t                vendor;
+       uint16_t                product;
+} irmce_devices[] = {
+       { USB_VENDOR_SMK, USB_PRODUCT_SMK_MCE_IR },
+};
+
+CFATTACH_DECL2_NEW(irmce, sizeof(struct irmce_softc),
+    irmce_match, irmce_attach, irmce_detach, irmce_activate,
+    irmce_rescan, irmce_childdet);
+
+static int
+irmce_match(device_t parent, cfdata_t match, void *opaque)
+{
+       struct usbif_attach_arg *uiaa = opaque;
+       unsigned int i;
+
+       for (i = 0; i < __arraycount(irmce_devices); i++) {
+               if (irmce_devices[i].vendor == uiaa->vendor &&
+                   irmce_devices[i].product == uiaa->product)
+                       return UMATCH_VENDOR_PRODUCT;
+       }
+
+       return UMATCH_NONE;
+}
+
+static void
+irmce_attach(device_t parent, device_t self, void *opaque)
+{
+       struct irmce_softc *sc = device_private(self);
+       struct usbif_attach_arg *uiaa = opaque;
+       usb_endpoint_descriptor_t *ed;
+       char *devinfop;
+       unsigned int i;
+       uint8_t nep;
+
+       pmf_device_register(self, NULL, NULL);
+
+       aprint_naive("\n");
+
+       devinfop = usbd_devinfo_alloc(uiaa->device, 0);
+       aprint_normal(": %s\n", devinfop);
+       usbd_devinfo_free(devinfop);
+
+       sc->sc_dev = self;
+       sc->sc_udev = uiaa->device;
+       sc->sc_iface = uiaa->iface;
+
+       nep = 0;
+       usbd_endpoint_count(sc->sc_iface, &nep);
+       sc->sc_bulkin_ep = sc->sc_bulkout_ep = -1;
+       for (i = 0; i < nep; i++) {
+               int dir, type;
+
+               ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
+               if (ed == NULL) {
+                       aprint_error_dev(self,
+                           "couldn't read endpoint descriptor %d\n", i);
+                       continue;
+               }
+
+               dir = UE_GET_DIR(ed->bEndpointAddress);
+               type = UE_GET_XFERTYPE(ed->bmAttributes);
+
+               if (type != UE_BULK)
+                       continue;
+
+               if (dir == UE_DIR_IN && sc->sc_bulkin_ep == -1) {
+                       sc->sc_bulkin_ep = ed->bEndpointAddress;
+                       sc->sc_bulkin_maxpktsize =
+                           UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
+                           (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
+               }
+               if (dir == UE_DIR_OUT && sc->sc_bulkout_ep == -1) {
+                       sc->sc_bulkout_ep = ed->bEndpointAddress;
+                       sc->sc_bulkout_maxpktsize =
+                           UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
+                           (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
+               }
+       }
+
+       aprint_debug_dev(self, "in 0x%02x/%d out 0x%02x/%d\n",
+           sc->sc_bulkin_ep, sc->sc_bulkin_maxpktsize,
+           sc->sc_bulkout_ep, sc->sc_bulkout_maxpktsize);
+
+       if (sc->sc_bulkin_maxpktsize < 16 || sc->sc_bulkout_maxpktsize < 16) {
+               aprint_error_dev(self, "bad maxpktsize\n");
+               return;
+       }
+
+       sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
+       sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
+       if (sc->sc_bulkin_xfer == NULL || sc->sc_bulkout_xfer == NULL) {
+               aprint_error_dev(self, "couldn't alloc xfer\n");
+               return;
+       }
+       sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
+           sc->sc_bulkin_maxpktsize);
+       sc->sc_bulkout_buffer = usbd_alloc_buffer(sc->sc_bulkout_xfer,
+           sc->sc_bulkout_maxpktsize);
+       if (sc->sc_bulkin_buffer == NULL || sc->sc_bulkout_buffer == NULL) {
+               aprint_error_dev(self, "couldn't alloc xfer buffer\n");
+               return;
+       }
+
+       irmce_rescan(self, NULL, NULL);
+}
+
+static int
+irmce_detach(device_t self, int flags)
+{
+       struct irmce_softc *sc = device_private(self);
+       int error;
+
+       if (sc->sc_cirdev) {
+               error = config_detach(sc->sc_cirdev, flags);
+               if (error)
+                       return error;
+       }
+
+       if (sc->sc_bulkin_xfer) {
+               usbd_free_xfer(sc->sc_bulkin_xfer);
+               sc->sc_bulkin_buffer = NULL;
+               sc->sc_bulkin_xfer = NULL;
+       }
+       if (sc->sc_bulkout_xfer) {
+               usbd_free_xfer(sc->sc_bulkout_xfer);
+               sc->sc_bulkout_buffer = NULL;
+               sc->sc_bulkout_xfer = NULL;
+       }
+
+       pmf_device_deregister(self);
+
+       return 0;
+}
+
+static int
+irmce_activate(device_t self, enum devact act)
+{
+       return 0;
+}
+
+static int
+irmce_rescan(device_t self, const char *ifattr, const int *locators)
+{
+       struct irmce_softc *sc = device_private(self);
+       struct ir_attach_args iaa;
+
+       if (sc->sc_cirdev == NULL) {
+               iaa.ia_type = IR_TYPE_CIR;
+               iaa.ia_methods = &irmce_cir_methods;
+               iaa.ia_handle = sc;
+               sc->sc_cirdev = config_found_ia(self, "irbus",
+                   &iaa, irmce_print);
+       }
+
+       return 0;
+}
+



Home | Main Index | Thread Index | Old Index