xref: /netbsd-src/sys/dev/usb/uberry.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: uberry.c,v 1.12 2016/12/04 10:12:35 skrll 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.12 2016/12/04 10:12:35 skrll 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 UBERRY_DEBUG
67 #define DPRINTF(x)	if (uberrydebug) printf x
68 #define DPRINTFN(n, x)	if (uberrydebug > n) printf x
69 int	uberrydebug = 0;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n, x)
73 #endif
74 
75 struct uberry_softc {
76 	device_t		sc_dev;
77 	struct usbd_device *	sc_udev;
78 };
79 
80 /*
81  * Note that we do not attach to USB_PRODUCT_RIM_BLACKBERRY_PEARL_DUAL
82  * as we let umass claim the device instead.
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 };
88 
89 #define uberry_lookup(v, p) usb_lookup(uberry_devs, v, p)
90 #define UBERRY_CONFIG_NO 1
91 
92 int	uberry_match(device_t, cfdata_t, void *);
93 void	uberry_attach(device_t, device_t, void *);
94 int	uberry_detach(device_t, int);
95 int	uberry_activate(device_t, enum devact);
96 extern struct cfdriver uberry_cd;
97 CFATTACH_DECL_NEW(uberry, sizeof(struct uberry_softc), uberry_match,
98     uberry_attach, uberry_detach, NULL);
99 
100 static void
101 uberry_cmd(struct uberry_softc *sc, uint8_t requestType, uint8_t reqno,
102     uint8_t value, uint8_t index, void *data, uint8_t length)
103 {
104 	usb_device_request_t req;
105 	usbd_status err;
106 
107 	DPRINTF(("berry cmd type=%x, number=%x, value=%d, index=%d, len=%d\n",
108 	    requestType, reqno, value, index, length));
109         req.bmRequestType = requestType;
110         req.bRequest = reqno;
111         USETW(req.wValue, value);
112         USETW(req.wIndex, index);
113         USETW(req.wLength, length);
114 
115         if ((err = usbd_do_request(sc->sc_udev, &req, data)) != 0)
116 		aprint_error_dev(sc->sc_dev, "sending command failed %d\n",
117 		    err);
118 }
119 
120 static void
121 uberry_charge(struct uberry_softc *sc)
122 {
123 	char dummy[2];
124 	usbd_status err;
125 
126 	if (sc->sc_udev->ud_power != USB_MAX_POWER) {
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 	err = usbd_set_config_no(sc->sc_udev, UBERRY_CONFIG_NO, 1);
132 	if (err) {
133 		aprint_error_dev(sc->sc_dev, "failed to set configuration"
134 		    ", err=%s\n", usbd_errstr(err));
135 		return;
136 	}
137 }
138 
139 /*
140  * Expose both the USB mass storage interface and the database access one
141  */
142 static void
143 uberry_dual_mode(struct uberry_softc *sc)
144 {
145 	char dummy[2];
146 	usbd_status err;
147 
148 	uberry_cmd(sc, UT_READ | UT_VENDOR, 0xa9, 1, 1, dummy, 2);
149 
150 	err = usbd_set_config_no(sc->sc_udev, UBERRY_CONFIG_NO, 1);
151 	if (err) {
152 		aprint_error_dev(sc->sc_dev, "failed to set configuration"
153 		    ", err=%s\n", usbd_errstr(err));
154 		return;
155 	}
156 }
157 
158 
159 int
160 uberry_match(device_t parent, cfdata_t match, void *aux)
161 {
162 	struct usb_attach_arg *uaa = aux;
163 
164 	DPRINTFN(50, ("uberry_match\n"));
165 	return (uberry_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
166 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
167 }
168 
169 void
170 uberry_attach(device_t parent, device_t self, void *aux)
171 {
172 	struct uberry_softc *sc = device_private(self);
173 	struct usb_attach_arg *uaa = aux;
174 	struct usbd_device *	dev = uaa->uaa_device;
175 	char			*devinfop;
176 
177 	DPRINTFN(10,("uberry_attach: sc=%p\n", sc));
178 
179 	sc->sc_dev = self;
180 	sc->sc_udev = dev;
181 
182 	aprint_naive("\n");
183 	aprint_normal("\n");
184 
185 	devinfop = usbd_devinfo_alloc(dev, 0);
186 	aprint_normal_dev(self, "%s\n", devinfop);
187 	usbd_devinfo_free(devinfop);
188 
189 	uberry_charge(sc);
190 	if (uaa->uaa_product == USB_PRODUCT_RIM_BLACKBERRY_PEARL)
191 		uberry_dual_mode(sc);
192 
193 	DPRINTFN(10, ("uberry_attach: %p\n", sc->sc_udev));
194 
195 	if (!pmf_device_register(self, NULL, NULL))
196 		aprint_error_dev(self, "couldn't establish power handler\n");
197 
198 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
199 	return;
200 }
201 
202 int
203 uberry_detach(device_t self, int flags)
204 {
205 	struct uberry_softc *sc = device_private(self);
206 	DPRINTF(("uberry_detach: sc=%p flags=%d\n", sc, flags));
207 
208 	pmf_device_deregister(self);
209 
210 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
211 
212 	return (0);
213 }
214