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