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