Home | History | Annotate | Line # | Download | only in usb
stuirda.c revision 1.19
      1  1.19    skrll /*	$NetBSD: stuirda.c,v 1.19 2016/11/25 12:56:29 skrll Exp $	*/
      2   1.1       is 
      3   1.1       is /*
      4   1.1       is  * Copyright (c) 2001,2007 The NetBSD Foundation, Inc.
      5   1.1       is  * All rights reserved.
      6   1.1       is  *
      7   1.1       is  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       is  * by Lennart Augustsson (lennart (at) augustsson.net).
      9   1.1       is  *
     10   1.1       is  * Redistribution and use in source and binary forms, with or without
     11   1.1       is  * modification, are permitted provided that the following conditions
     12   1.1       is  * are met:
     13   1.1       is  * 1. Redistributions of source code must retain the above copyright
     14   1.1       is  *    notice, this list of conditions and the following disclaimer.
     15   1.1       is  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       is  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       is  *    documentation and/or other materials provided with the distribution.
     18   1.1       is  *
     19   1.1       is  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       is  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       is  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       is  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       is  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       is  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       is  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       is  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       is  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       is  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       is  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       is  */
     31   1.4    lukem 
     32   1.1       is #include <sys/cdefs.h>
     33  1.19    skrll __KERNEL_RCSID(0, "$NetBSD: stuirda.c,v 1.19 2016/11/25 12:56:29 skrll Exp $");
     34  1.19    skrll 
     35  1.19    skrll #ifdef _KERNEL_OPT
     36  1.19    skrll #include "opt_usb.h"
     37  1.19    skrll #endif
     38   1.4    lukem 
     39   1.1       is #include <sys/param.h>
     40   1.1       is 
     41   1.1       is #include <sys/device.h>
     42   1.1       is #include <sys/systm.h>
     43   1.1       is #include <sys/kernel.h>
     44   1.1       is #include <sys/device.h>
     45   1.1       is #include <sys/ioctl.h>
     46   1.1       is #include <sys/conf.h>
     47   1.1       is #include <sys/file.h>
     48   1.1       is #include <sys/poll.h>
     49   1.1       is #include <sys/select.h>
     50   1.1       is #include <sys/proc.h>
     51   1.1       is 
     52   1.1       is 
     53   1.1       is #include <dev/firmload.h>
     54   1.1       is #include <dev/usb/usb.h>
     55   1.1       is #include <dev/usb/usbdi.h>
     56   1.1       is #include <dev/usb/usbdi_util.h>
     57   1.1       is #include <dev/usb/usbdevs.h>
     58   1.1       is 
     59   1.1       is #include <dev/ir/ir.h>
     60   1.1       is #include <dev/ir/irdaio.h>
     61   1.1       is #include <dev/ir/irframevar.h>
     62   1.1       is 
     63   1.1       is #include <dev/usb/uirdavar.h>
     64   1.1       is 
     65   1.1       is #ifdef UIRDA_DEBUG
     66  1.11   dyoung #define DPRINTF(x)	if (stuirdadebug) printf x
     67  1.11   dyoung #define DPRINTFN(n,x)	if (stuirdadebug>(n)) printf x
     68   1.1       is int	stuirdadebug = 1;
     69   1.1       is #else
     70   1.1       is #define DPRINTF(x)
     71   1.1       is #define DPRINTFN(n,x)
     72   1.1       is #endif
     73   1.1       is 
     74   1.1       is struct stuirda_softc {
     75   1.1       is 	struct uirda_softc sc_uirda;
     76   1.1       is };
     77   1.1       is 
     78   1.1       is int stuirda_fwload(struct uirda_softc *sc);
     79   1.1       is 
     80  1.17    skrll /*
     81   1.1       is  * These devices need firmware download.
     82   1.1       is  */
     83   1.1       is Static const struct usb_devno stuirda_devs[] = {
     84   1.1       is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_SIR4116 },
     85   1.1       is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_FIR4210 },
     86   1.1       is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4220 },
     87   1.1       is };
     88   1.1       is #define stuirda_lookup(v, p) (usb_lookup(stuirda_devs, v, p))
     89   1.1       is 
     90   1.1       is int stuirda_write(void *h, struct uio *uio, int flag);
     91   1.1       is 
     92   1.1       is struct irframe_methods stuirda_methods = {
     93   1.1       is 	uirda_open, uirda_close, uirda_read, stuirda_write, uirda_poll,
     94   1.1       is 	uirda_kqfilter, uirda_set_params, uirda_get_speeds,
     95   1.1       is 	uirda_get_turnarounds
     96   1.1       is };
     97   1.1       is 
     98   1.1       is #define STUIRDA_HEADER_SIZE 3
     99   1.1       is 
    100   1.1       is #define stuirda_activate uirda_activate
    101   1.1       is #define stuirda_detach uirda_detach
    102   1.1       is 
    103  1.18  msaitoh int	stuirda_match(device_t, cfdata_t, void *);
    104  1.18  msaitoh void	stuirda_attach(device_t, device_t, void *);
    105  1.18  msaitoh int	stuirda_detach(device_t, int);
    106  1.18  msaitoh int	stuirda_activate(device_t, enum devact);
    107  1.11   dyoung extern struct cfdriver stuirda_cd;
    108  1.18  msaitoh CFATTACH_DECL_NEW(stuirda, sizeof(struct stuirda_softc), stuirda_match,
    109  1.18  msaitoh     stuirda_attach, stuirda_detach, stuirda_activate);
    110   1.1       is 
    111  1.17    skrll int
    112  1.11   dyoung stuirda_match(device_t parent, cfdata_t match, void *aux)
    113   1.1       is {
    114  1.17    skrll 	struct usbif_attach_arg *uiaa = aux;
    115   1.1       is 
    116   1.1       is 	DPRINTFN(50,("stuirda_match\n"));
    117   1.1       is 
    118  1.17    skrll 	if (stuirda_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
    119  1.17    skrll 		return UMATCH_VENDOR_PRODUCT;
    120   1.1       is 
    121  1.17    skrll 	return UMATCH_NONE;
    122   1.1       is }
    123   1.1       is 
    124   1.8     cube void uirda_attach(device_t, device_t, void *);
    125   1.1       is 
    126  1.17    skrll void
    127  1.11   dyoung stuirda_attach(device_t parent, device_t self, void *aux)
    128   1.1       is {
    129  1.11   dyoung 	struct stuirda_softc *sc = device_private(self);
    130   1.1       is 
    131   1.1       is 	sc->sc_uirda.sc_loadfw = stuirda_fwload;
    132   1.1       is 	sc->sc_uirda.sc_irm = &stuirda_methods;
    133   1.1       is 	sc->sc_uirda.sc_hdszi = STUIRDA_HEADER_SIZE;
    134   1.1       is 
    135   1.1       is 	uirda_attach(parent,self,aux);
    136   1.1       is }
    137   1.1       is 
    138   1.1       is int
    139  1.18  msaitoh stuirda_fwload(struct uirda_softc *sc)
    140  1.18  msaitoh {
    141   1.1       is 
    142   1.1       is 
    143   1.1       is 	int rc;
    144   1.1       is 	firmware_handle_t fh;
    145   1.1       is 	off_t fwsize;
    146   1.1       is 	usb_device_descriptor_t usbddsc;
    147  1.17    skrll 	struct usbd_xfer *	fwxfer;
    148  1.17    skrll 	struct usbd_pipe *	fwpipe;
    149   1.1       is 	usbd_status status;
    150   1.1       is 	usb_device_request_t req;
    151   1.1       is 	char *buffer;
    152   1.1       is 	char *p;
    153   1.1       is 	char fwname[12];
    154   1.1       is 	int n;
    155  1.17    skrll 	uint8_t *usbbuf;
    156   1.1       is 	/* size_t bsize; */
    157   1.1       is 
    158   1.1       is 	printf("%s: needing to download firmware\n",
    159  1.11   dyoung 		device_xname(sc->sc_dev));
    160   1.1       is 
    161   1.1       is 	status = usbd_get_device_desc(sc->sc_udev, &usbddsc);
    162   1.1       is 	if (status) {
    163   1.1       is 		printf("%s: can't get device descriptor, status %d\n",
    164  1.11   dyoung 		    device_xname(sc->sc_dev), status);
    165   1.1       is 		return status;
    166   1.1       is 	}
    167   1.1       is 
    168   1.1       is 	rc = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
    169   1.1       is 		USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
    170   1.1       is 	printf("error %d reading class desc\n", rc);
    171   1.1       is 
    172   1.6   cegger 	snprintf(fwname, sizeof(fwname), "4210%02x%02x.sb",
    173   1.1       is 		usbddsc.bcdDevice[1],
    174   1.1       is 		usbddsc.bcdDevice[0]);
    175   1.1       is 
    176   1.1       is 	printf("%s: Attempting to load firmware %s\n",
    177  1.11   dyoung 		device_xname(sc->sc_dev), fwname);
    178  1.17    skrll 
    179   1.9       is 	rc = firmware_open("stuirda", fwname, &fh);
    180   1.1       is 
    181   1.1       is 	if (rc) {
    182   1.1       is 		printf("%s: Cannot load firmware\n",
    183  1.11   dyoung 			device_xname(sc->sc_dev));
    184   1.1       is 		return rc;
    185   1.1       is 	}
    186   1.1       is 	fwsize = firmware_get_size(fh);
    187   1.1       is 
    188   1.1       is 	printf("%s: Firmware size %lld\n",
    189  1.11   dyoung 		device_xname(sc->sc_dev), (long long)fwsize);
    190   1.1       is 
    191   1.1       is 	buffer = firmware_malloc(fwsize);
    192   1.1       is 	if (buffer == NULL) {
    193   1.1       is 		printf("%s: Cannot load firmware: out of memory\n",
    194  1.11   dyoung 			device_xname(sc->sc_dev));
    195   1.1       is 		goto giveup2;
    196   1.1       is 	}
    197   1.1       is 
    198   1.1       is 	rc = firmware_read(fh, 0, buffer, (size_t)fwsize);
    199   1.1       is 
    200   1.1       is 	if (rc) {
    201  1.11   dyoung 		printf("%s: Cannot read firmware\n", device_xname(sc->sc_dev));
    202   1.1       is 		goto giveup3;
    203   1.1       is 	}
    204   1.1       is 
    205   1.1       is 	for (p = buffer + sizeof("Product Version:");
    206   1.1       is 	    p < buffer + fwsize - 5; p++) {
    207   1.1       is 
    208   1.1       is 		if (0x1A == *p)
    209   1.1       is 			break;
    210   1.1       is 	}
    211   1.1       is 	if (0x1a != *p || memcmp(p+1, "STMP", 4) != 0) {
    212   1.1       is 		/* firmware bad */
    213  1.11   dyoung 		printf("%s: Bad firmware\n", device_xname(sc->sc_dev));
    214   1.1       is 		goto giveup3;
    215   1.1       is 	}
    216   1.1       is 
    217   1.1       is 	p += 5;
    218   1.1       is 
    219   1.1       is 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    220   1.1       is 	req.bRequest = 2 /* XXX magic */;
    221   1.1       is 	USETW(req.wValue, 0);
    222   1.1       is 	USETW(req.wIndex, 0);
    223   1.1       is 	USETW(req.wLength, 0);
    224   1.1       is 	rc = usbd_do_request(sc->sc_udev, &req, 0);
    225   1.1       is 	if (rc) {
    226   1.1       is 		printf("%s: Cannot switch to f/w d/l mode, error %d\n",
    227  1.11   dyoung 			device_xname(sc->sc_dev), rc);
    228  1.15    joerg 		goto giveup3;
    229   1.1       is 	}
    230   1.1       is 
    231   1.1       is 	delay(100000);
    232   1.1       is 
    233   1.1       is 	rc = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &fwpipe);
    234   1.1       is 	if (rc) {
    235   1.1       is 		printf("%s: Cannot open pipe, rc=%d\n",
    236  1.11   dyoung 		    device_xname(sc->sc_dev), rc);
    237   1.1       is 		goto giveup3;
    238   1.1       is 	}
    239  1.17    skrll 
    240  1.17    skrll 	int err = usbd_create_xfer(fwpipe, 1024, USBD_FORCE_SHORT_XFER, 0,
    241  1.17    skrll 	    &fwxfer);
    242  1.17    skrll 	if (err) {
    243  1.11   dyoung 		printf("%s: Cannot alloc xfer\n", device_xname(sc->sc_dev));
    244   1.1       is 		goto giveup4;
    245   1.1       is 	}
    246  1.17    skrll 	usbbuf = usbd_get_buffer(fwxfer);
    247   1.1       is 	n = (buffer + fwsize - p);
    248   1.1       is 	while (n > 0) {
    249   1.1       is 		if (n > 1023)
    250   1.1       is 			n = 1023;
    251   1.1       is 		memcpy(usbbuf, p, n);
    252  1.17    skrll 		rc = usbd_bulk_transfer(fwxfer, fwpipe, USBD_FORCE_SHORT_XFER,
    253  1.17    skrll 		    5000, usbbuf, &n);
    254   1.1       is 		printf("%s: write: rc=%d, %d left\n",
    255  1.11   dyoung 		    device_xname(sc->sc_dev), rc, n);
    256   1.1       is 		if (rc) {
    257   1.1       is 			printf("%s: write: rc=%d, %d bytes written\n",
    258  1.11   dyoung 			    device_xname(sc->sc_dev), rc, n);
    259   1.1       is 			goto giveup4;
    260   1.1       is 		}
    261  1.11   dyoung 		printf("%s: written %d\n", device_xname(sc->sc_dev), n);
    262  1.17    skrll 		p += n;
    263   1.1       is 		n = (buffer + fwsize - p);
    264   1.1       is 	}
    265   1.1       is 	delay(100000);
    266   1.1       is 	/* TODO: more code here */
    267   1.1       is 	rc = 0;
    268  1.17    skrll 	usbd_destroy_xfer(fwxfer);
    269   1.1       is 
    270   1.1       is 	giveup4: usbd_close_pipe(fwpipe);
    271   1.1       is 	giveup3: firmware_free(buffer, fwsize);
    272   1.1       is 	giveup2: firmware_close(fh);
    273   1.1       is 
    274   1.1       is 	return rc;
    275  1.17    skrll 
    276   1.1       is }
    277   1.1       is 
    278   1.1       is int
    279   1.1       is stuirda_write(void *h, struct uio *uio, int flag)
    280   1.1       is {
    281   1.1       is 	struct uirda_softc *sc = h;
    282   1.1       is 	usbd_status err;
    283  1.17    skrll 	uint32_t n;
    284   1.1       is 	int error = 0;
    285   1.1       is 
    286   1.1       is 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    287   1.1       is 
    288   1.1       is 	if (sc->sc_dying)
    289  1.17    skrll 		return EIO;
    290   1.1       is 
    291   1.1       is #ifdef DIAGNOSTIC
    292   1.1       is 	if (sc->sc_wr_buf == NULL)
    293  1.17    skrll 		return EINVAL;
    294   1.1       is #endif
    295   1.1       is 
    296   1.1       is 	n = uio->uio_resid;
    297   1.1       is 	if (n > sc->sc_params.maxsize)
    298  1.17    skrll 		return EINVAL;
    299   1.1       is 
    300   1.1       is 	sc->sc_refcnt++;
    301   1.3       ad 	mutex_enter(&sc->sc_wr_buf_lk);
    302   1.1       is 
    303   1.1       is 	sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
    304   1.1       is 
    305   1.1       is 	sc->sc_wr_buf[1] = 0;
    306   1.1       is 	sc->sc_wr_buf[2] = 7; /* XXX turnaround - maximum for now */
    307   1.1       is 	if ((n > 0 && (n % 128) == 0 && (n % 512) != 0)) {
    308   1.1       is 		sc->sc_wr_buf[1] = 1;
    309   1.1       is 	}
    310   1.1       is 
    311   1.1       is 	error = uiomove(sc->sc_wr_buf + STUIRDA_HEADER_SIZE, n, uio);
    312  1.17    skrll 	if (error)
    313  1.17    skrll 		goto done;
    314  1.17    skrll 
    315  1.17    skrll 	DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
    316  1.17    skrll 
    317  1.17    skrll 	n += STUIRDA_HEADER_SIZE + sc->sc_wr_buf[1];
    318   1.1       is 
    319  1.17    skrll 	err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    320  1.17    skrll 	    USBD_FORCE_SHORT_XFER, UIRDA_WR_TIMEOUT, sc->sc_wr_buf, &n);
    321  1.17    skrll 	DPRINTFN(2, ("uirdawrite: err=%d\n", err));
    322  1.17    skrll 	if (err) {
    323  1.17    skrll 		if (err == USBD_INTERRUPTED)
    324  1.17    skrll 			error = EINTR;
    325  1.17    skrll 		else if (err == USBD_TIMEOUT)
    326  1.17    skrll 			error = ETIMEDOUT;
    327  1.17    skrll 		else
    328  1.17    skrll 			error = EIO;
    329   1.1       is 	}
    330  1.17    skrll done:
    331   1.3       ad 	mutex_exit(&sc->sc_wr_buf_lk);
    332   1.1       is 	if (--sc->sc_refcnt < 0)
    333  1.14      mrg 		usb_detach_wakeupold(sc->sc_dev);
    334   1.1       is 
    335   1.1       is 	DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
    336  1.17    skrll 	return error;
    337   1.1       is }
    338