Home | History | Annotate | Line # | Download | only in usb
uberry.c revision 1.12.14.1
      1  1.12.14.1  pgoyette /*	$NetBSD: uberry.c,v 1.12.14.1 2019/01/26 22:00:24 pgoyette Exp $	*/
      2        1.1  christos 
      3        1.1  christos /*-
      4        1.1  christos  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5        1.1  christos  * All rights reserved.
      6        1.1  christos  *
      7        1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1  christos  * by Christos Zoulas.
      9        1.1  christos  *
     10        1.1  christos  * Redistribution and use in source and binary forms, with or without
     11        1.1  christos  * modification, are permitted provided that the following conditions
     12        1.1  christos  * are met:
     13        1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14        1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15        1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17        1.1  christos  *    documentation and/or other materials provided with the distribution.
     18        1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19        1.1  christos  *    must display the following acknowledgement:
     20        1.1  christos  *        This product includes software developed by the NetBSD
     21        1.1  christos  *        Foundation, Inc. and its contributors.
     22        1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.1  christos  *    contributors may be used to endorse or promote products derived
     24        1.1  christos  *    from this software without specific pberryr written permission.
     25        1.1  christos  *
     26        1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1  christos  */
     38        1.1  christos 
     39        1.1  christos #include <sys/cdefs.h>
     40  1.12.14.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: uberry.c,v 1.12.14.1 2019/01/26 22:00:24 pgoyette Exp $");
     41       1.11     skrll 
     42       1.11     skrll #ifdef _KERNEL_OPT
     43       1.11     skrll #include "opt_usb.h"
     44       1.11     skrll #endif
     45        1.1  christos 
     46        1.1  christos #include <sys/param.h>
     47        1.1  christos #include <sys/systm.h>
     48        1.1  christos #include <sys/kernel.h>
     49        1.1  christos #include <sys/device.h>
     50        1.1  christos #include <sys/ioctl.h>
     51        1.1  christos #include <sys/conf.h>
     52        1.1  christos #include <sys/file.h>
     53        1.1  christos #include <sys/select.h>
     54        1.1  christos #include <sys/proc.h>
     55        1.1  christos #include <sys/vnode.h>
     56        1.1  christos #include <sys/poll.h>
     57        1.2  jmcneill #include <sys/bus.h>
     58        1.1  christos 
     59        1.1  christos #include <dev/usb/usb.h>
     60        1.1  christos #include <dev/usb/usbdi.h>
     61        1.1  christos #include <dev/usb/usbdi_util.h>
     62        1.2  jmcneill #include <dev/usb/usbdivar.h>
     63        1.1  christos 
     64        1.1  christos #include <dev/usb/usbdevs.h>
     65        1.1  christos 
     66        1.1  christos #ifdef UBERRY_DEBUG
     67        1.6    dyoung #define DPRINTF(x)	if (uberrydebug) printf x
     68        1.6    dyoung #define DPRINTFN(n, x)	if (uberrydebug > n) printf x
     69        1.1  christos int	uberrydebug = 0;
     70        1.1  christos #else
     71        1.1  christos #define DPRINTF(x)
     72        1.1  christos #define DPRINTFN(n, x)
     73        1.1  christos #endif
     74        1.1  christos 
     75        1.1  christos struct uberry_softc {
     76       1.12     skrll 	device_t		sc_dev;
     77       1.10     skrll 	struct usbd_device *	sc_udev;
     78        1.1  christos };
     79        1.1  christos 
     80        1.3  jmcneill /*
     81        1.3  jmcneill  * Note that we do not attach to USB_PRODUCT_RIM_BLACKBERRY_PEARL_DUAL
     82        1.3  jmcneill  * as we let umass claim the device instead.
     83        1.3  jmcneill  */
     84        1.1  christos static const struct usb_devno uberry_devs[] = {
     85        1.1  christos 	{ USB_VENDOR_RIM, USB_PRODUCT_RIM_BLACKBERRY },
     86        1.1  christos 	{ USB_VENDOR_RIM, USB_PRODUCT_RIM_BLACKBERRY_PEARL },
     87        1.1  christos };
     88        1.1  christos 
     89        1.1  christos #define uberry_lookup(v, p) usb_lookup(uberry_devs, v, p)
     90        1.1  christos #define UBERRY_CONFIG_NO 1
     91        1.1  christos 
     92        1.5    dyoung int	uberry_match(device_t, cfdata_t, void *);
     93        1.5    dyoung void	uberry_attach(device_t, device_t, void *);
     94        1.5    dyoung int	uberry_detach(device_t, int);
     95        1.5    dyoung int	uberry_activate(device_t, enum devact);
     96        1.5    dyoung extern struct cfdriver uberry_cd;
     97        1.5    dyoung CFATTACH_DECL_NEW(uberry, sizeof(struct uberry_softc), uberry_match,
     98        1.5    dyoung     uberry_attach, uberry_detach, NULL);
     99        1.1  christos 
    100        1.1  christos static void
    101        1.1  christos uberry_cmd(struct uberry_softc *sc, uint8_t requestType, uint8_t reqno,
    102        1.1  christos     uint8_t value, uint8_t index, void *data, uint8_t length)
    103        1.1  christos {
    104        1.1  christos 	usb_device_request_t req;
    105        1.1  christos 	usbd_status err;
    106       1.12     skrll 
    107        1.1  christos 	DPRINTF(("berry cmd type=%x, number=%x, value=%d, index=%d, len=%d\n",
    108        1.1  christos 	    requestType, reqno, value, index, length));
    109        1.1  christos         req.bmRequestType = requestType;
    110        1.1  christos         req.bRequest = reqno;
    111       1.12     skrll         USETW(req.wValue, value);
    112        1.1  christos         USETW(req.wIndex, index);
    113        1.1  christos         USETW(req.wLength, length);
    114       1.12     skrll 
    115        1.1  christos         if ((err = usbd_do_request(sc->sc_udev, &req, data)) != 0)
    116        1.1  christos 		aprint_error_dev(sc->sc_dev, "sending command failed %d\n",
    117        1.1  christos 		    err);
    118        1.1  christos }
    119        1.1  christos 
    120        1.1  christos static void
    121        1.1  christos uberry_charge(struct uberry_softc *sc)
    122        1.1  christos {
    123        1.1  christos 	char dummy[2];
    124        1.2  jmcneill 	usbd_status err;
    125        1.1  christos 
    126       1.10     skrll 	if (sc->sc_udev->ud_power != USB_MAX_POWER) {
    127        1.3  jmcneill 		uberry_cmd(sc, UT_READ | UT_VENDOR, 0xa5, 0, 1, dummy, 2);
    128        1.3  jmcneill 		uberry_cmd(sc, UT_WRITE | UT_VENDOR, 0xa2, 0, 1, dummy, 0);
    129        1.1  christos 	}
    130        1.1  christos 
    131        1.2  jmcneill 	err = usbd_set_config_no(sc->sc_udev, UBERRY_CONFIG_NO, 1);
    132        1.2  jmcneill 	if (err) {
    133        1.9     skrll 		aprint_error_dev(sc->sc_dev, "failed to set configuration"
    134        1.9     skrll 		    ", err=%s\n", usbd_errstr(err));
    135        1.3  jmcneill 		return;
    136        1.2  jmcneill 	}
    137        1.1  christos }
    138        1.1  christos 
    139        1.1  christos /*
    140        1.1  christos  * Expose both the USB mass storage interface and the database access one
    141        1.1  christos  */
    142        1.1  christos static void
    143        1.1  christos uberry_dual_mode(struct uberry_softc *sc)
    144        1.1  christos {
    145        1.1  christos 	char dummy[2];
    146        1.2  jmcneill 	usbd_status err;
    147        1.1  christos 
    148        1.1  christos 	uberry_cmd(sc, UT_READ | UT_VENDOR, 0xa9, 1, 1, dummy, 2);
    149        1.2  jmcneill 
    150        1.2  jmcneill 	err = usbd_set_config_no(sc->sc_udev, UBERRY_CONFIG_NO, 1);
    151        1.2  jmcneill 	if (err) {
    152        1.9     skrll 		aprint_error_dev(sc->sc_dev, "failed to set configuration"
    153        1.9     skrll 		    ", err=%s\n", usbd_errstr(err));
    154        1.3  jmcneill 		return;
    155        1.2  jmcneill 	}
    156        1.1  christos }
    157        1.1  christos 
    158        1.1  christos 
    159       1.12     skrll int
    160        1.5    dyoung uberry_match(device_t parent, cfdata_t match, void *aux)
    161        1.1  christos {
    162        1.5    dyoung 	struct usb_attach_arg *uaa = aux;
    163        1.1  christos 
    164        1.5    dyoung 	DPRINTFN(50, ("uberry_match\n"));
    165       1.10     skrll 	return (uberry_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    166        1.5    dyoung 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    167        1.1  christos }
    168        1.1  christos 
    169       1.12     skrll void
    170        1.5    dyoung uberry_attach(device_t parent, device_t self, void *aux)
    171        1.1  christos {
    172        1.5    dyoung 	struct uberry_softc *sc = device_private(self);
    173        1.5    dyoung 	struct usb_attach_arg *uaa = aux;
    174       1.10     skrll 	struct usbd_device *	dev = uaa->uaa_device;
    175        1.1  christos 	char			*devinfop;
    176        1.1  christos 
    177        1.1  christos 	DPRINTFN(10,("uberry_attach: sc=%p\n", sc));
    178        1.1  christos 
    179        1.1  christos 	sc->sc_dev = self;
    180        1.1  christos 	sc->sc_udev = dev;
    181        1.1  christos 
    182        1.4    plunky 	aprint_naive("\n");
    183        1.4    plunky 	aprint_normal("\n");
    184        1.4    plunky 
    185        1.1  christos 	devinfop = usbd_devinfo_alloc(dev, 0);
    186        1.1  christos 	aprint_normal_dev(self, "%s\n", devinfop);
    187        1.1  christos 	usbd_devinfo_free(devinfop);
    188        1.1  christos 
    189        1.1  christos 	uberry_charge(sc);
    190       1.10     skrll 	if (uaa->uaa_product == USB_PRODUCT_RIM_BLACKBERRY_PEARL)
    191        1.1  christos 		uberry_dual_mode(sc);
    192        1.1  christos 
    193        1.1  christos 	DPRINTFN(10, ("uberry_attach: %p\n", sc->sc_udev));
    194        1.1  christos 
    195        1.3  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    196        1.3  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    197        1.3  jmcneill 
    198        1.5    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    199        1.5    dyoung 	return;
    200        1.1  christos }
    201        1.1  christos 
    202       1.12     skrll int
    203        1.5    dyoung uberry_detach(device_t self, int flags)
    204        1.1  christos {
    205        1.5    dyoung 	struct uberry_softc *sc = device_private(self);
    206        1.1  christos 	DPRINTF(("uberry_detach: sc=%p flags=%d\n", sc, flags));
    207        1.3  jmcneill 
    208        1.3  jmcneill 	pmf_device_deregister(self);
    209        1.1  christos 
    210        1.5    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    211        1.1  christos 
    212  1.12.14.1  pgoyette 	return 0;
    213        1.1  christos }
    214