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