xref: /netbsd-src/sys/dev/usb/uipad.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: uipad.c,v 1.6 2017/09/30 13:01:50 sevan Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 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 pipadr 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: uipad.c,v 1.6 2017/09/30 13:01:50 sevan Exp $");
41 
42 #ifdef _KERNEL_OPT
43 #include "opt_usb.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52 #include <sys/file.h>
53 #include <sys/select.h>
54 #include <sys/proc.h>
55 #include <sys/vnode.h>
56 #include <sys/poll.h>
57 #include <sys/bus.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdivar.h>
63 
64 #include <dev/usb/usbdevs.h>
65 
66 #ifdef UIPAD_DEBUG
67 #define DPRINTF(x)	if (uipaddebug) printf x
68 #define DPRINTFN(n, x)	if (uipaddebug > n) printf x
69 int	uipaddebug = 0;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n, x)
73 #endif
74 
75 struct uipad_softc {
76 	device_t		sc_dev;
77 	struct usbd_device *	sc_udev;
78 };
79 
80 static const struct usb_devno uipad_devs[] = {
81 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD },
82 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_2 },
83 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_3 },
84 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPAD_MINI },
85 #if 0
86 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE },
87 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G },
88 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3GS },
89 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4 },
90 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4_VZW },
91 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_4S },
92 	{ USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_5 },
93 #endif
94 };
95 
96 #define uipad_lookup(v, p) usb_lookup(uipad_devs, v, p)
97 
98 int	uipad_match(device_t, cfdata_t, void *);
99 void	uipad_attach(device_t, device_t, void *);
100 int	uipad_detach(device_t, int);
101 int	uipad_activate(device_t, enum devact);
102 
103 extern struct cfdriver uipad_cd;
104 CFATTACH_DECL_NEW(uipad, sizeof(struct uipad_softc), uipad_match,
105     uipad_attach, uipad_detach, NULL);
106 
107 static void
108 uipad_cmd(struct uipad_softc *sc, uint8_t requestType, uint8_t reqno,
109     uint16_t value, uint16_t index)
110 {
111 	usb_device_request_t req;
112 	usbd_status err;
113 
114 	DPRINTF(("ipad cmd type=%x, number=%x, value=%d, index=%d\n",
115 	    requestType, reqno, value, index));
116         req.bmRequestType = requestType;
117         req.bRequest = reqno;
118         USETW(req.wValue, value);
119         USETW(req.wIndex, index);
120         USETW(req.wLength, 0);
121 
122         if ((err = usbd_do_request(sc->sc_udev, &req, NULL)) != 0)
123 		aprint_error_dev(sc->sc_dev, "sending command failed %d\n",
124 		    err);
125 }
126 
127 static void
128 uipad_charge(struct uipad_softc *sc)
129 {
130 	if (sc->sc_udev->ud_power != USB_MAX_POWER)
131 		uipad_cmd(sc, UT_VENDOR | UT_WRITE, 0x40, 0x6400, 0x6400);
132 }
133 
134 int
135 uipad_match(device_t parent, cfdata_t match, void *aux)
136 {
137 	struct usb_attach_arg *uaa = aux;
138 
139 	DPRINTFN(50, ("uipad_match\n"));
140 	return uipad_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
141 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
142 }
143 
144 void
145 uipad_attach(device_t parent, device_t self, void *aux)
146 {
147 	struct uipad_softc *sc = device_private(self);
148 	struct usb_attach_arg *uaa = aux;
149 	struct usbd_device *	dev = uaa->uaa_device;
150 	char			*devinfop;
151 
152 	DPRINTFN(10,("uipad_attach: sc=%p\n", sc));
153 
154 	sc->sc_dev = self;
155 	sc->sc_udev = dev;
156 
157 	aprint_naive("\n");
158 	aprint_normal("\n");
159 
160 	devinfop = usbd_devinfo_alloc(dev, 0);
161 	aprint_normal_dev(self, "%s\n", devinfop);
162 	usbd_devinfo_free(devinfop);
163 
164 	uipad_charge(sc);
165 
166 	DPRINTFN(10, ("uipad_attach: %p\n", sc->sc_udev));
167 
168 	if (!pmf_device_register(self, NULL, NULL))
169 		aprint_error_dev(self, "couldn't establish power handler\n");
170 
171 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
172 	return;
173 }
174 
175 int
176 uipad_detach(device_t self, int flags)
177 {
178 	struct uipad_softc *sc = device_private(self);
179 	DPRINTF(("uipad_detach: sc=%p flags=%d\n", sc, flags));
180 
181 	pmf_device_deregister(self);
182 
183 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
184 
185 	return 0;
186 }
187