1 1.9 maxv /* $NetBSD: uipad.c,v 1.9 2020/01/07 06:42:26 maxv Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2011 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 * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos 32 1.1 christos #include <sys/cdefs.h> 33 1.9 maxv __KERNEL_RCSID(0, "$NetBSD: uipad.c,v 1.9 2020/01/07 06:42:26 maxv Exp $"); 34 1.3 skrll 35 1.3 skrll #ifdef _KERNEL_OPT 36 1.3 skrll #include "opt_usb.h" 37 1.3 skrll #endif 38 1.1 christos 39 1.1 christos #include <sys/param.h> 40 1.1 christos #include <sys/systm.h> 41 1.1 christos #include <sys/kernel.h> 42 1.1 christos #include <sys/device.h> 43 1.1 christos #include <sys/ioctl.h> 44 1.1 christos #include <sys/conf.h> 45 1.1 christos #include <sys/file.h> 46 1.1 christos #include <sys/select.h> 47 1.1 christos #include <sys/proc.h> 48 1.1 christos #include <sys/vnode.h> 49 1.1 christos #include <sys/poll.h> 50 1.1 christos #include <sys/bus.h> 51 1.1 christos 52 1.1 christos #include <dev/usb/usb.h> 53 1.1 christos #include <dev/usb/usbdi.h> 54 1.1 christos #include <dev/usb/usbdi_util.h> 55 1.1 christos #include <dev/usb/usbdivar.h> 56 1.1 christos 57 1.1 christos #include <dev/usb/usbdevs.h> 58 1.1 christos 59 1.1 christos #ifdef UIPAD_DEBUG 60 1.1 christos #define DPRINTF(x) if (uipaddebug) printf x 61 1.1 christos #define DPRINTFN(n, x) if (uipaddebug > n) printf x 62 1.1 christos int uipaddebug = 0; 63 1.1 christos #else 64 1.1 christos #define DPRINTF(x) 65 1.1 christos #define DPRINTFN(n, x) 66 1.1 christos #endif 67 1.1 christos 68 1.1 christos struct uipad_softc { 69 1.4 skrll device_t sc_dev; 70 1.2 skrll struct usbd_device * sc_udev; 71 1.1 christos }; 72 1.1 christos 73 1.1 christos static const struct usb_devno uipad_devs[] = { 74 1.1 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD }, 75 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_2 }, 76 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_3 }, 77 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_MINI }, 78 1.5 christos #if 0 79 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE }, 80 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G }, 81 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS }, 82 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4 }, 83 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4_VZW }, 84 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S }, 85 1.5 christos { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_5 }, 86 1.5 christos #endif 87 1.1 christos }; 88 1.1 christos 89 1.1 christos #define uipad_lookup(v, p) usb_lookup(uipad_devs, v, p) 90 1.1 christos 91 1.9 maxv static int uipad_match(device_t, cfdata_t, void *); 92 1.9 maxv static void uipad_attach(device_t, device_t, void *); 93 1.9 maxv static int uipad_detach(device_t, int); 94 1.7 mrg 95 1.1 christos CFATTACH_DECL_NEW(uipad, sizeof(struct uipad_softc), uipad_match, 96 1.1 christos uipad_attach, uipad_detach, NULL); 97 1.1 christos 98 1.1 christos static void 99 1.1 christos uipad_cmd(struct uipad_softc *sc, uint8_t requestType, uint8_t reqno, 100 1.1 christos uint16_t value, uint16_t index) 101 1.1 christos { 102 1.1 christos usb_device_request_t req; 103 1.1 christos usbd_status err; 104 1.4 skrll 105 1.1 christos DPRINTF(("ipad cmd type=%x, number=%x, value=%d, index=%d\n", 106 1.1 christos requestType, reqno, value, index)); 107 1.1 christos req.bmRequestType = requestType; 108 1.1 christos req.bRequest = reqno; 109 1.4 skrll USETW(req.wValue, value); 110 1.1 christos USETW(req.wIndex, index); 111 1.1 christos USETW(req.wLength, 0); 112 1.4 skrll 113 1.1 christos if ((err = usbd_do_request(sc->sc_udev, &req, NULL)) != 0) 114 1.1 christos aprint_error_dev(sc->sc_dev, "sending command failed %d\n", 115 1.1 christos err); 116 1.1 christos } 117 1.1 christos 118 1.1 christos static void 119 1.1 christos uipad_charge(struct uipad_softc *sc) 120 1.1 christos { 121 1.2 skrll if (sc->sc_udev->ud_power != USB_MAX_POWER) 122 1.1 christos uipad_cmd(sc, UT_VENDOR | UT_WRITE, 0x40, 0x6400, 0x6400); 123 1.1 christos } 124 1.1 christos 125 1.9 maxv static int 126 1.1 christos uipad_match(device_t parent, cfdata_t match, void *aux) 127 1.1 christos { 128 1.1 christos struct usb_attach_arg *uaa = aux; 129 1.1 christos 130 1.1 christos DPRINTFN(50, ("uipad_match\n")); 131 1.2 skrll return uipad_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 132 1.1 christos UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 133 1.1 christos } 134 1.1 christos 135 1.9 maxv static void 136 1.1 christos uipad_attach(device_t parent, device_t self, void *aux) 137 1.1 christos { 138 1.1 christos struct uipad_softc *sc = device_private(self); 139 1.1 christos struct usb_attach_arg *uaa = aux; 140 1.2 skrll struct usbd_device * dev = uaa->uaa_device; 141 1.1 christos char *devinfop; 142 1.1 christos 143 1.1 christos DPRINTFN(10,("uipad_attach: sc=%p\n", sc)); 144 1.1 christos 145 1.1 christos sc->sc_dev = self; 146 1.1 christos sc->sc_udev = dev; 147 1.1 christos 148 1.1 christos aprint_naive("\n"); 149 1.1 christos aprint_normal("\n"); 150 1.1 christos 151 1.1 christos devinfop = usbd_devinfo_alloc(dev, 0); 152 1.1 christos aprint_normal_dev(self, "%s\n", devinfop); 153 1.1 christos usbd_devinfo_free(devinfop); 154 1.1 christos 155 1.1 christos uipad_charge(sc); 156 1.1 christos 157 1.1 christos DPRINTFN(10, ("uipad_attach: %p\n", sc->sc_udev)); 158 1.1 christos 159 1.1 christos if (!pmf_device_register(self, NULL, NULL)) 160 1.1 christos aprint_error_dev(self, "couldn't establish power handler\n"); 161 1.1 christos 162 1.1 christos usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 163 1.1 christos return; 164 1.1 christos } 165 1.1 christos 166 1.9 maxv static int 167 1.1 christos uipad_detach(device_t self, int flags) 168 1.1 christos { 169 1.1 christos struct uipad_softc *sc = device_private(self); 170 1.1 christos DPRINTF(("uipad_detach: sc=%p flags=%d\n", sc, flags)); 171 1.1 christos 172 1.1 christos pmf_device_deregister(self); 173 1.1 christos 174 1.1 christos usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 175 1.1 christos 176 1.1 christos return 0; 177 1.1 christos } 178