Home | History | Annotate | Line # | Download | only in usb
stuirda.c revision 1.2.4.1
      1  1.2.4.1  itohy /*	$NetBSD: stuirda.c,v 1.2.4.1 2007/06/21 15:14:18 itohy 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  * 3. All advertising materials mentioning features or use of this software
     19      1.1     is  *    must display the following acknowledgement:
     20      1.1     is  *        This product includes software developed by the NetBSD
     21      1.1     is  *        Foundation, Inc. and its contributors.
     22      1.1     is  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1     is  *    contributors may be used to endorse or promote products derived
     24      1.1     is  *    from this software without specific prior written permission.
     25      1.1     is  *
     26      1.1     is  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1     is  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1     is  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1     is  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1     is  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1     is  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1     is  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1     is  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1     is  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1     is  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1     is  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1     is  */
     38      1.1     is #include <sys/cdefs.h>
     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/lock.h>
     46      1.1     is #include <sys/ioctl.h>
     47      1.1     is #include <sys/conf.h>
     48      1.1     is #include <sys/file.h>
     49      1.1     is #include <sys/poll.h>
     50      1.1     is #include <sys/select.h>
     51      1.1     is #include <sys/proc.h>
     52      1.1     is 
     53      1.1     is 
     54      1.1     is #include <dev/firmload.h>
     55      1.1     is #include <dev/usb/usb.h>
     56      1.1     is #include <dev/usb/usbdi.h>
     57      1.1     is #include <dev/usb/usbdi_util.h>
     58      1.1     is #include <dev/usb/usbdevs.h>
     59      1.1     is #include <dev/usb/usb_port.h>
     60      1.1     is 
     61      1.1     is #include <dev/ir/ir.h>
     62      1.1     is #include <dev/ir/irdaio.h>
     63      1.1     is #include <dev/ir/irframevar.h>
     64      1.1     is 
     65      1.1     is #include <dev/usb/uirdavar.h>
     66      1.1     is 
     67      1.1     is #ifdef UIRDA_DEBUG
     68      1.1     is #define DPRINTF(x)	if (stuirdadebug) logprintf x
     69      1.1     is #define DPRINTFN(n,x)	if (stuirdadebug>(n)) logprintf x
     70      1.1     is int	stuirdadebug = 1;
     71      1.1     is #else
     72      1.1     is #define DPRINTF(x)
     73      1.1     is #define DPRINTFN(n,x)
     74      1.1     is #endif
     75      1.1     is 
     76      1.1     is struct stuirda_softc {
     77      1.1     is 	struct uirda_softc sc_uirda;
     78      1.1     is };
     79      1.1     is 
     80      1.1     is int stuirda_fwload(struct uirda_softc *sc);
     81      1.1     is 
     82      1.1     is /*
     83      1.1     is  * These devices need firmware download.
     84      1.1     is  */
     85      1.1     is Static const struct usb_devno stuirda_devs[] = {
     86      1.1     is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_SIR4116 },
     87      1.1     is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_FIR4210 },
     88      1.1     is 	{ USB_VENDOR_SIGMATEL, USB_PRODUCT_SIGMATEL_VFIR4220 },
     89      1.1     is };
     90      1.1     is #define stuirda_lookup(v, p) (usb_lookup(stuirda_devs, v, p))
     91      1.1     is 
     92      1.1     is int stuirda_write(void *h, struct uio *uio, int flag);
     93      1.1     is 
     94      1.1     is struct irframe_methods stuirda_methods = {
     95      1.1     is 	uirda_open, uirda_close, uirda_read, stuirda_write, uirda_poll,
     96      1.1     is 	uirda_kqfilter, uirda_set_params, uirda_get_speeds,
     97      1.1     is 	uirda_get_turnarounds
     98      1.1     is };
     99      1.1     is 
    100      1.1     is #define STUIRDA_HEADER_SIZE 3
    101      1.1     is 
    102      1.1     is #define stuirda_activate uirda_activate
    103      1.1     is #define stuirda_detach uirda_detach
    104      1.1     is 
    105      1.1     is USB_DECLARE_DRIVER(stuirda);
    106      1.1     is 
    107      1.1     is USB_MATCH(stuirda)
    108      1.1     is {
    109  1.2.4.1  itohy #ifndef USB_USE_IFATTACH
    110  1.2.4.1  itohy 	USB_MATCH_START(stuirda, uaa);
    111  1.2.4.1  itohy #else
    112      1.2     is 	USB_IFMATCH_START(stuirda, uaa);
    113  1.2.4.1  itohy #endif /* USB_USE_IFATTACH */
    114      1.1     is 
    115      1.1     is 	DPRINTFN(50,("stuirda_match\n"));
    116      1.1     is 
    117  1.2.4.1  itohy #ifndef USB_USE_IFATTACH
    118  1.2.4.1  itohy 	if (uaa->iface == NULL)
    119  1.2.4.1  itohy 		return (UMATCH_NONE);
    120  1.2.4.1  itohy #endif /* USB_USE_IFATTACH */
    121  1.2.4.1  itohy 
    122      1.1     is 	if (stuirda_lookup(uaa->vendor, uaa->product) != NULL)
    123      1.1     is 		return (UMATCH_VENDOR_PRODUCT);
    124      1.1     is 
    125      1.1     is 	return (UMATCH_NONE);
    126      1.1     is }
    127      1.1     is 
    128      1.1     is void uirda_attach(struct device *,struct device *,void *);
    129      1.1     is 
    130      1.1     is USB_ATTACH(stuirda)
    131      1.1     is {
    132  1.2.4.1  itohy #ifndef USB_USE_IFATTACH
    133  1.2.4.1  itohy 	USB_ATTACH_START(stuirda, sc, uaa);
    134  1.2.4.1  itohy #else
    135      1.2     is 	USB_IFATTACH_START(stuirda, sc, uaa);
    136  1.2.4.1  itohy #endif /* USB_USE_IFATTACH */
    137      1.1     is 
    138      1.1     is 	(void)uaa;
    139      1.1     is 
    140      1.1     is 	sc->sc_uirda.sc_loadfw = stuirda_fwload;
    141      1.1     is 	sc->sc_uirda.sc_irm = &stuirda_methods;
    142      1.1     is 	sc->sc_uirda.sc_hdszi = STUIRDA_HEADER_SIZE;
    143      1.1     is 
    144      1.1     is 	uirda_attach(parent,self,aux);
    145      1.1     is }
    146      1.1     is 
    147      1.1     is int
    148      1.1     is stuirda_fwload(struct uirda_softc *sc) {
    149      1.1     is 
    150      1.1     is 
    151      1.1     is 	int rc;
    152      1.1     is 	firmware_handle_t fh;
    153      1.1     is 	off_t fwsize;
    154      1.1     is 	usb_device_descriptor_t usbddsc;
    155      1.1     is 	usbd_xfer_handle	fwxfer;
    156      1.1     is 	usbd_pipe_handle	fwpipe;
    157      1.1     is 	usbd_status status;
    158      1.1     is 	usb_device_request_t req;
    159      1.1     is 	char *buffer;
    160      1.1     is 	char *p;
    161      1.1     is 	char fwname[12];
    162      1.1     is 	int n;
    163      1.1     is 	/* size_t bsize; */
    164      1.1     is 
    165      1.1     is 	printf("%s: needing to download firmware\n",
    166      1.1     is 		USBDEVNAME(sc->sc_dev));
    167      1.1     is 
    168      1.1     is 	status = usbd_get_device_desc(sc->sc_udev, &usbddsc);
    169      1.1     is 	if (status) {
    170      1.1     is 		printf("%s: can't get device descriptor, status %d\n",
    171      1.1     is 		    USBDEVNAME(sc->sc_dev), status);
    172      1.1     is 		return status;
    173      1.1     is 	}
    174      1.1     is 
    175      1.1     is 	rc = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
    176      1.1     is 		USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
    177      1.1     is 	printf("error %d reading class desc\n", rc);
    178      1.1     is 
    179      1.1     is 	sprintf(fwname, "4210%02x%02x.sb",
    180      1.1     is 		usbddsc.bcdDevice[1],
    181      1.1     is 		usbddsc.bcdDevice[0]);
    182      1.1     is 
    183      1.1     is 	printf("%s: Attempting to load firmware %s\n",
    184      1.1     is 		USBDEVNAME(sc->sc_dev), fwname);
    185      1.1     is 
    186      1.1     is 	rc = firmware_open("uirda", fwname, &fh);
    187      1.1     is 
    188      1.1     is 	if (rc) {
    189      1.1     is 		printf("%s: Cannot load firmware\n",
    190      1.1     is 			USBDEVNAME(sc->sc_dev));
    191      1.1     is 		return 0;
    192      1.1     is 		return rc;
    193      1.1     is 	}
    194      1.1     is 	fwsize = firmware_get_size(fh);
    195      1.1     is 
    196      1.1     is 	printf("%s: Firmware size %lld\n",
    197      1.1     is 		USBDEVNAME(sc->sc_dev), (long long)fwsize);
    198      1.1     is 
    199      1.1     is 	buffer = firmware_malloc(fwsize);
    200      1.1     is 	if (buffer == NULL) {
    201      1.1     is 		printf("%s: Cannot load firmware: out of memory\n",
    202      1.1     is 			USBDEVNAME(sc->sc_dev));
    203      1.1     is 		goto giveup2;
    204      1.1     is 	}
    205      1.1     is 
    206      1.1     is 	rc = firmware_read(fh, 0, buffer, (size_t)fwsize);
    207      1.1     is 
    208      1.1     is 	if (rc) {
    209      1.1     is 		printf("%s: Cannot read firmware\n", USBDEVNAME(sc->sc_dev));
    210      1.1     is 		goto giveup3;
    211      1.1     is 	}
    212      1.1     is 
    213      1.1     is 	for (p = buffer + sizeof("Product Version:");
    214      1.1     is 	    p < buffer + fwsize - 5; p++) {
    215      1.1     is 
    216      1.1     is 		if (0x1A == *p)
    217      1.1     is 			break;
    218      1.1     is 	}
    219      1.1     is 	if (0x1a != *p || memcmp(p+1, "STMP", 4) != 0) {
    220      1.1     is 		/* firmware bad */
    221      1.1     is 		printf("%s: Bad firmware\n", USBDEVNAME(sc->sc_dev));
    222      1.1     is 		goto giveup3;
    223      1.1     is 	}
    224      1.1     is 
    225      1.1     is 	p += 5;
    226      1.1     is 
    227      1.1     is 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    228      1.1     is 	req.bRequest = 2 /* XXX magic */;
    229      1.1     is 	USETW(req.wValue, 0);
    230      1.1     is 	USETW(req.wIndex, 0);
    231      1.1     is 	USETW(req.wLength, 0);
    232      1.1     is 	rc = usbd_do_request(sc->sc_udev, &req, 0);
    233      1.1     is 	if (rc) {
    234      1.1     is 		printf("%s: Cannot switch to f/w d/l mode, error %d\n",
    235      1.1     is 			USBDEVNAME(sc->sc_dev), rc);
    236      1.1     is 		goto giveup4;
    237      1.1     is 	}
    238      1.1     is 
    239      1.1     is 	delay(100000);
    240      1.1     is 
    241      1.1     is 	rc = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &fwpipe);
    242      1.1     is 	if (rc) {
    243      1.1     is 		printf("%s: Cannot open pipe, rc=%d\n",
    244      1.1     is 		    USBDEVNAME(sc->sc_dev), rc);
    245      1.1     is 		goto giveup3;
    246      1.1     is 	}
    247  1.2.4.1  itohy 	fwxfer = usbd_alloc_xfer(sc->sc_udev, fwpipe);
    248      1.1     is 	if (fwxfer == NULL) {
    249      1.1     is 		printf("%s: Cannot alloc xfer\n", USBDEVNAME(sc->sc_dev));
    250      1.1     is 		goto giveup4;
    251      1.1     is 	}
    252      1.1     is 	n = (buffer + fwsize - p);
    253      1.1     is 	while (n > 0) {
    254      1.1     is 		if (n > 1023)
    255      1.1     is 			n = 1023;
    256      1.1     is 		rc = usbd_bulk_transfer(fwxfer, fwpipe,
    257  1.2.4.1  itohy 		    USBD_NO_COPY|USBD_SYNCHRONOUS|USBD_FORCE_SHORT_XFER,
    258  1.2.4.1  itohy 		    5000, p, &n, "uirda-fw-wr");
    259      1.1     is 		printf("%s: write: rc=%d, %d left\n",
    260      1.1     is 		    USBDEVNAME(sc->sc_dev), rc, n);
    261      1.1     is 		if (rc) {
    262      1.1     is 			printf("%s: write: rc=%d, %d bytes written\n",
    263      1.1     is 			    USBDEVNAME(sc->sc_dev), rc, n);
    264      1.1     is 			goto giveup4;
    265      1.1     is 		}
    266      1.1     is 		printf("%s: written %d\n", USBDEVNAME(sc->sc_dev), n);
    267      1.1     is 		p += n;
    268      1.1     is 		n = (buffer + fwsize - p);
    269      1.1     is 	}
    270      1.1     is 	delay(100000);
    271      1.1     is 	/* TODO: more code here */
    272      1.1     is 	rc = 0;
    273      1.1     is 
    274  1.2.4.1  itohy 	usbd_free_xfer(fwxfer);
    275      1.1     is 	giveup4: usbd_close_pipe(fwpipe);
    276      1.1     is 	giveup3: firmware_free(buffer, fwsize);
    277      1.1     is 	giveup2: firmware_close(fh);
    278      1.1     is 
    279      1.1     is 	return rc;
    280      1.1     is 
    281      1.1     is }
    282      1.1     is 
    283      1.1     is int
    284      1.1     is stuirda_write(void *h, struct uio *uio, int flag)
    285      1.1     is {
    286      1.1     is 	struct uirda_softc *sc = h;
    287      1.1     is 	usbd_status err;
    288      1.1     is 	u_int32_t n;
    289      1.1     is 	int error = 0;
    290      1.1     is 
    291      1.1     is 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    292      1.1     is 
    293      1.1     is 	if (sc->sc_dying)
    294      1.1     is 		return (EIO);
    295      1.1     is 
    296      1.1     is #ifdef DIAGNOSTIC
    297      1.1     is 	if (sc->sc_wr_buf == NULL)
    298      1.1     is 		return (EINVAL);
    299      1.1     is #endif
    300      1.1     is 
    301      1.1     is 	n = uio->uio_resid;
    302      1.1     is 	if (n > sc->sc_params.maxsize)
    303      1.1     is 		return (EINVAL);
    304      1.1     is 
    305      1.1     is 	sc->sc_refcnt++;
    306      1.1     is 	lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
    307      1.1     is 
    308      1.1     is 	sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
    309      1.1     is 
    310      1.1     is 	sc->sc_wr_buf[1] = 0;
    311      1.1     is 	sc->sc_wr_buf[2] = 7; /* XXX turnaround - maximum for now */
    312      1.1     is 	if ((n > 0 && (n % 128) == 0 && (n % 512) != 0)) {
    313      1.1     is 		sc->sc_wr_buf[1] = 1;
    314      1.1     is 	}
    315      1.1     is 
    316      1.1     is 	error = uiomove(sc->sc_wr_buf + STUIRDA_HEADER_SIZE, n, uio);
    317      1.1     is 	if (!error) {
    318      1.1     is 		DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
    319      1.1     is 
    320      1.1     is 		n += STUIRDA_HEADER_SIZE + sc->sc_wr_buf[1];
    321      1.1     is 		err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
    322      1.1     is 			  USBD_FORCE_SHORT_XFER|USBD_NO_COPY,
    323      1.1     is 			  UIRDA_WR_TIMEOUT,
    324      1.1     is 			  sc->sc_wr_buf, &n, "uirdawr");
    325      1.1     is 		DPRINTFN(2, ("uirdawrite: err=%d\n", err));
    326      1.1     is 		if (err) {
    327      1.1     is 			if (err == USBD_INTERRUPTED)
    328      1.1     is 				error = EINTR;
    329      1.1     is 			else if (err == USBD_TIMEOUT)
    330      1.1     is 				error = ETIMEDOUT;
    331      1.1     is 			else
    332      1.1     is 				error = EIO;
    333      1.1     is 		}
    334      1.1     is 	}
    335      1.1     is 
    336      1.1     is 	lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
    337      1.1     is 	if (--sc->sc_refcnt < 0)
    338      1.1     is 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    339      1.1     is 
    340      1.1     is 	DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
    341      1.1     is 	return (error);
    342      1.1     is }
    343