Home | History | Annotate | Line # | Download | only in usb
uberry.c revision 1.1
      1 /*	$NetBSD: uberry.c,v 1.1 2008/05/26 00:23:05 christos 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific pberryr written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: uberry.c,v 1.1 2008/05/26 00:23:05 christos Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/malloc.h>
     46 #if defined(__NetBSD__) || defined(__OpenBSD__)
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #elif defined(__FreeBSD__)
     50 #include <sys/module.h>
     51 #include <sys/bus.h>
     52 #include <sys/ioccom.h>
     53 #include <sys/conf.h>
     54 #include <sys/fcntl.h>
     55 #include <sys/filio.h>
     56 #endif
     57 #include <sys/conf.h>
     58 #include <sys/file.h>
     59 #include <sys/select.h>
     60 #include <sys/proc.h>
     61 #include <sys/vnode.h>
     62 #include <sys/poll.h>
     63 
     64 #include <dev/usb/usb.h>
     65 #include <dev/usb/usbdi.h>
     66 #include <dev/usb/usbdi_util.h>
     67 
     68 #include <dev/usb/usbdevs.h>
     69 
     70 #ifdef UBERRY_DEBUG
     71 #define DPRINTF(x)	if (uberrydebug) logprintf x
     72 #define DPRINTFN(n, x)	if (uberrydebug > n) logprintf x
     73 int	uberrydebug = 0;
     74 #else
     75 #define DPRINTF(x)
     76 #define DPRINTFN(n, x)
     77 #endif
     78 
     79 struct uberry_softc {
     80  	USBBASEDEVICE		sc_dev;
     81 	usbd_device_handle	sc_udev;
     82 };
     83 
     84 static const struct usb_devno uberry_devs[] = {
     85 	{ USB_VENDOR_RIM, USB_PRODUCT_RIM_BLACKBERRY },
     86 	{ USB_VENDOR_RIM, USB_PRODUCT_RIM_BLACKBERRY_PEARL },
     87 	{ USB_VENDOR_RIM, USB_PRODUCT_RIM_BLACKBERRY_PEARL_DUAL },
     88 };
     89 
     90 #define uberry_lookup(v, p) usb_lookup(uberry_devs, v, p)
     91 #define UBERRY_CONFIG_NO 1
     92 
     93 USB_DECLARE_DRIVER(uberry);
     94 
     95 static void
     96 uberry_cmd(struct uberry_softc *sc, uint8_t requestType, uint8_t reqno,
     97     uint8_t value, uint8_t index, void *data, uint8_t length)
     98 {
     99 	usb_device_request_t req;
    100 	usbd_status err;
    101 
    102 	DPRINTF(("berry cmd type=%x, number=%x, value=%d, index=%d, len=%d\n",
    103 	    requestType, reqno, value, index, length));
    104         req.bmRequestType = requestType;
    105         req.bRequest = reqno;
    106         USETW(req.wValue, value);
    107         USETW(req.wIndex, index);
    108         USETW(req.wLength, length);
    109 
    110         if ((err = usbd_do_request(sc->sc_udev, &req, data)) != 0)
    111 		aprint_error_dev(sc->sc_dev, "sending command failed %d\n",
    112 		    err);
    113 }
    114 
    115 static void
    116 uberry_charge(struct uberry_softc *sc)
    117 {
    118 	char dummy[2];
    119 
    120 #ifdef notyet
    121 	if (sc->sc_udev->power == 250) {
    122 		DPRINTF(("Power is already %d\n", sc->sc_udev->power));
    123 		return;
    124 	}
    125 #endif
    126 
    127 	uberry_cmd(sc, UT_READ | UT_VENDOR, 0xa5, 0, 1, dummy, 2);
    128 	uberry_cmd(sc, UT_WRITE | UT_VENDOR, 0xa2, 0, 1, dummy, 0);
    129 }
    130 
    131 /*
    132  * Expose both the USB mass storage interface and the database access one
    133  */
    134 static void
    135 uberry_dual_mode(struct uberry_softc *sc)
    136 {
    137 	char dummy[2];
    138 
    139 	uberry_cmd(sc, UT_READ | UT_VENDOR, 0xa9, 1, 1, dummy, 2);
    140 }
    141 
    142 
    143 USB_MATCH(uberry)
    144 {
    145 	USB_MATCH_START(uberry, uaa);
    146 
    147 	DPRINTFN(50,("uberry_match\n"));
    148 
    149 	return (uberry_lookup(uaa->vendor, uaa->product) != NULL ?
    150 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    151 }
    152 
    153 USB_ATTACH(uberry)
    154 {
    155 	USB_ATTACH_START(uberry, sc, uaa);
    156 	usbd_device_handle	dev = uaa->device;
    157 	char			*devinfop;
    158 #ifdef notyet
    159 	usbd_status		err;
    160 #endif
    161 
    162 	DPRINTFN(10,("uberry_attach: sc=%p\n", sc));
    163 
    164 	sc->sc_dev = self;
    165 	sc->sc_udev = dev;
    166 
    167 	devinfop = usbd_devinfo_alloc(dev, 0);
    168 	USB_ATTACH_SETUP;
    169 	aprint_normal_dev(self, "%s\n", devinfop);
    170 	usbd_devinfo_free(devinfop);
    171 
    172 	uberry_charge(sc);
    173 	if (uaa->product == USB_PRODUCT_RIM_BLACKBERRY_PEARL)
    174 		uberry_dual_mode(sc);
    175 #ifdef notyet
    176 	err = usbd_set_config_no(dev, UBERRY_CONFIG_NO, 1);
    177 	if (err) {
    178 		aprint_error_dev(self, "setting config no failed\n");
    179 		USB_ATTACH_ERROR_RETURN;
    180 	}
    181 #endif
    182 
    183 	DPRINTFN(10, ("uberry_attach: %p\n", sc->sc_udev));
    184 
    185 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    186 	   USBDEV(sc->sc_dev));
    187 
    188 	USB_ATTACH_SUCCESS_RETURN;
    189 }
    190 
    191 USB_DETACH(uberry)
    192 {
    193 	USB_DETACH_START(uberry, sc);
    194 #if defined(__NetBSD__) || defined(__OpenBSD__)
    195 	DPRINTF(("uberry_detach: sc=%p flags=%d\n", sc, flags));
    196 #elif defined(__FreeBSD__)
    197 	DPRINTF(("uberry_detach: sc=%p\n", sc));
    198 #endif
    199 
    200 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    201 	    USBDEV(sc->sc_dev));
    202 
    203 	return (0);
    204 }
    205 
    206 #if defined(__NetBSD__) || defined(__OpenBSD__)
    207 int
    208 uberry_activate(device_ptr_t self, enum devact act)
    209 {
    210 	switch (act) {
    211 	case DVACT_ACTIVATE:
    212 		return (EOPNOTSUPP);
    213 		break;
    214 
    215 	case DVACT_DEACTIVATE:
    216 		break;
    217 	}
    218 	return (0);
    219 }
    220 #endif
    221 
    222 #if defined(__FreeBSD__)
    223 DRIVER_MODULE(uberry, uhub, uberry_driver, uberry_devclass, usbd_driver_load, 0);
    224 #endif /* defined(__FreeBSD__) */
    225