xref: /OK3568_Linux_fs/kernel/drivers/net/wimax/i2400m/usb-notif.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Intel Wireless WiMAX Connection 2400m over USB
3*4882a593Smuzhiyun  * Notification handling
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun  * are met:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *   * Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun  *   * Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer in
16*4882a593Smuzhiyun  *     the documentation and/or other materials provided with the
17*4882a593Smuzhiyun  *     distribution.
18*4882a593Smuzhiyun  *   * Neither the name of Intel Corporation nor the names of its
19*4882a593Smuzhiyun  *     contributors may be used to endorse or promote products derived
20*4882a593Smuzhiyun  *     from this software without specific prior written permission.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Intel Corporation <linux-wimax@intel.com>
36*4882a593Smuzhiyun  * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37*4882a593Smuzhiyun  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
38*4882a593Smuzhiyun  *  - Initial implementation
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * The notification endpoint is active when the device is not in boot
42*4882a593Smuzhiyun  * mode; in here we just read and get notifications; based on those,
43*4882a593Smuzhiyun  * we act to either reinitialize the device after a reboot or to
44*4882a593Smuzhiyun  * submit a RX request.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * ROADMAP
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * i2400mu_usb_notification_setup()
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * i2400mu_usb_notification_release()
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * i2400mu_usb_notification_cb()	Called when a URB is ready
53*4882a593Smuzhiyun  *   i2400mu_notif_grok()
54*4882a593Smuzhiyun  *     i2400m_is_boot_barker()
55*4882a593Smuzhiyun  *     i2400m_dev_reset_handle()
56*4882a593Smuzhiyun  *     i2400mu_rx_kick()
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun #include <linux/usb.h>
59*4882a593Smuzhiyun #include <linux/slab.h>
60*4882a593Smuzhiyun #include "i2400m-usb.h"
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define D_SUBMODULE notif
64*4882a593Smuzhiyun #include "usb-debug-levels.h"
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static const
68*4882a593Smuzhiyun __le32 i2400m_ZERO_BARKER[4] = { 0, 0, 0, 0 };
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * Process a received notification
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * In normal operation mode, we can only receive two types of payloads
75*4882a593Smuzhiyun  * on the notification endpoint:
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  *   - a reboot barker, we do a bootstrap (the device has reseted).
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  *   - a block of zeroes: there is pending data in the IN endpoint
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun static
i2400mu_notification_grok(struct i2400mu * i2400mu,const void * buf,size_t buf_len)82*4882a593Smuzhiyun int i2400mu_notification_grok(struct i2400mu *i2400mu, const void *buf,
83*4882a593Smuzhiyun 				 size_t buf_len)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	int ret;
86*4882a593Smuzhiyun 	struct device *dev = &i2400mu->usb_iface->dev;
87*4882a593Smuzhiyun 	struct i2400m *i2400m = &i2400mu->i2400m;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	d_fnstart(4, dev, "(i2400m %p buf %p buf_len %zu)\n",
90*4882a593Smuzhiyun 		  i2400mu, buf, buf_len);
91*4882a593Smuzhiyun 	ret = -EIO;
92*4882a593Smuzhiyun 	if (buf_len < sizeof(i2400m_ZERO_BARKER))
93*4882a593Smuzhiyun 		/* Not a bug, just ignore */
94*4882a593Smuzhiyun 		goto error_bad_size;
95*4882a593Smuzhiyun 	ret = 0;
96*4882a593Smuzhiyun 	if (!memcmp(i2400m_ZERO_BARKER, buf, sizeof(i2400m_ZERO_BARKER))) {
97*4882a593Smuzhiyun 		i2400mu_rx_kick(i2400mu);
98*4882a593Smuzhiyun 		goto out;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	ret = i2400m_is_boot_barker(i2400m, buf, buf_len);
101*4882a593Smuzhiyun 	if (unlikely(ret >= 0))
102*4882a593Smuzhiyun 		ret = i2400m_dev_reset_handle(i2400m, "device rebooted");
103*4882a593Smuzhiyun 	else	/* Unknown or unexpected data in the notif message */
104*4882a593Smuzhiyun 		i2400m_unknown_barker(i2400m, buf, buf_len);
105*4882a593Smuzhiyun error_bad_size:
106*4882a593Smuzhiyun out:
107*4882a593Smuzhiyun 	d_fnend(4, dev, "(i2400m %p buf %p buf_len %zu) = %d\n",
108*4882a593Smuzhiyun 		i2400mu, buf, buf_len, ret);
109*4882a593Smuzhiyun 	return ret;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun  * URB callback for the notification endpoint
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * @urb: the urb received from the notification endpoint
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * This function will just process the USB side of the transaction,
119*4882a593Smuzhiyun  * checking everything is fine, pass the processing to
120*4882a593Smuzhiyun  * i2400m_notification_grok() and resubmit the URB.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun static
i2400mu_notification_cb(struct urb * urb)123*4882a593Smuzhiyun void i2400mu_notification_cb(struct urb *urb)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	int ret;
126*4882a593Smuzhiyun 	struct i2400mu *i2400mu = urb->context;
127*4882a593Smuzhiyun 	struct device *dev = &i2400mu->usb_iface->dev;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	d_fnstart(4, dev, "(urb %p status %d actual_length %d)\n",
130*4882a593Smuzhiyun 		  urb, urb->status, urb->actual_length);
131*4882a593Smuzhiyun 	ret = urb->status;
132*4882a593Smuzhiyun 	switch (ret) {
133*4882a593Smuzhiyun 	case 0:
134*4882a593Smuzhiyun 		ret = i2400mu_notification_grok(i2400mu, urb->transfer_buffer,
135*4882a593Smuzhiyun 						urb->actual_length);
136*4882a593Smuzhiyun 		if (ret == -EIO && edc_inc(&i2400mu->urb_edc, EDC_MAX_ERRORS,
137*4882a593Smuzhiyun 					   EDC_ERROR_TIMEFRAME))
138*4882a593Smuzhiyun 			goto error_exceeded;
139*4882a593Smuzhiyun 		if (ret == -ENOMEM)	/* uff...power cycle? shutdown? */
140*4882a593Smuzhiyun 			goto error_exceeded;
141*4882a593Smuzhiyun 		break;
142*4882a593Smuzhiyun 	case -EINVAL:			/* while removing driver */
143*4882a593Smuzhiyun 	case -ENODEV:			/* dev disconnect ... */
144*4882a593Smuzhiyun 	case -ENOENT:			/* ditto */
145*4882a593Smuzhiyun 	case -ESHUTDOWN:		/* URB killed */
146*4882a593Smuzhiyun 	case -ECONNRESET:		/* disconnection */
147*4882a593Smuzhiyun 		goto out;		/* Notify around */
148*4882a593Smuzhiyun 	default:			/* Some error? */
149*4882a593Smuzhiyun 		if (edc_inc(&i2400mu->urb_edc,
150*4882a593Smuzhiyun 			    EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
151*4882a593Smuzhiyun 			goto error_exceeded;
152*4882a593Smuzhiyun 		dev_err(dev, "notification: URB error %d, retrying\n",
153*4882a593Smuzhiyun 			urb->status);
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 	usb_mark_last_busy(i2400mu->usb_dev);
156*4882a593Smuzhiyun 	ret = usb_submit_urb(i2400mu->notif_urb, GFP_ATOMIC);
157*4882a593Smuzhiyun 	switch (ret) {
158*4882a593Smuzhiyun 	case 0:
159*4882a593Smuzhiyun 	case -EINVAL:			/* while removing driver */
160*4882a593Smuzhiyun 	case -ENODEV:			/* dev disconnect ... */
161*4882a593Smuzhiyun 	case -ENOENT:			/* ditto */
162*4882a593Smuzhiyun 	case -ESHUTDOWN:		/* URB killed */
163*4882a593Smuzhiyun 	case -ECONNRESET:		/* disconnection */
164*4882a593Smuzhiyun 		break;			/* just ignore */
165*4882a593Smuzhiyun 	default:			/* Some error? */
166*4882a593Smuzhiyun 		dev_err(dev, "notification: cannot submit URB: %d\n", ret);
167*4882a593Smuzhiyun 		goto error_submit;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
170*4882a593Smuzhiyun 		urb, urb->status, urb->actual_length);
171*4882a593Smuzhiyun 	return;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun error_exceeded:
174*4882a593Smuzhiyun 	dev_err(dev, "maximum errors in notification URB exceeded; "
175*4882a593Smuzhiyun 		"resetting device\n");
176*4882a593Smuzhiyun error_submit:
177*4882a593Smuzhiyun 	usb_queue_reset_device(i2400mu->usb_iface);
178*4882a593Smuzhiyun out:
179*4882a593Smuzhiyun 	d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
180*4882a593Smuzhiyun 		urb, urb->status, urb->actual_length);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun  * setup the notification endpoint
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * @i2400m: device descriptor
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * This procedure prepares the notification urb and handler for receiving
190*4882a593Smuzhiyun  * unsolicited barkers from the device.
191*4882a593Smuzhiyun  */
i2400mu_notification_setup(struct i2400mu * i2400mu)192*4882a593Smuzhiyun int i2400mu_notification_setup(struct i2400mu *i2400mu)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	struct device *dev = &i2400mu->usb_iface->dev;
195*4882a593Smuzhiyun 	int usb_pipe, ret = 0;
196*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *epd;
197*4882a593Smuzhiyun 	char *buf;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	d_fnstart(4, dev, "(i2400m %p)\n", i2400mu);
200*4882a593Smuzhiyun 	buf = kmalloc(I2400MU_MAX_NOTIFICATION_LEN, GFP_KERNEL | GFP_DMA);
201*4882a593Smuzhiyun 	if (buf == NULL) {
202*4882a593Smuzhiyun 		ret = -ENOMEM;
203*4882a593Smuzhiyun 		goto error_buf_alloc;
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	i2400mu->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
207*4882a593Smuzhiyun 	if (!i2400mu->notif_urb) {
208*4882a593Smuzhiyun 		ret = -ENOMEM;
209*4882a593Smuzhiyun 		goto error_alloc_urb;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	epd = usb_get_epd(i2400mu->usb_iface,
212*4882a593Smuzhiyun 			  i2400mu->endpoint_cfg.notification);
213*4882a593Smuzhiyun 	usb_pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
214*4882a593Smuzhiyun 	usb_fill_int_urb(i2400mu->notif_urb, i2400mu->usb_dev, usb_pipe,
215*4882a593Smuzhiyun 			 buf, I2400MU_MAX_NOTIFICATION_LEN,
216*4882a593Smuzhiyun 			 i2400mu_notification_cb, i2400mu, epd->bInterval);
217*4882a593Smuzhiyun 	ret = usb_submit_urb(i2400mu->notif_urb, GFP_KERNEL);
218*4882a593Smuzhiyun 	if (ret != 0) {
219*4882a593Smuzhiyun 		dev_err(dev, "notification: cannot submit URB: %d\n", ret);
220*4882a593Smuzhiyun 		goto error_submit;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 	d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
223*4882a593Smuzhiyun 	return ret;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun error_submit:
226*4882a593Smuzhiyun 	usb_free_urb(i2400mu->notif_urb);
227*4882a593Smuzhiyun error_alloc_urb:
228*4882a593Smuzhiyun 	kfree(buf);
229*4882a593Smuzhiyun error_buf_alloc:
230*4882a593Smuzhiyun 	d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
231*4882a593Smuzhiyun 	return ret;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun  * Tear down of the notification mechanism
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * @i2400m: device descriptor
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Kill the interrupt endpoint urb, free any allocated resources.
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  * We need to check if we have done it before as for example,
243*4882a593Smuzhiyun  * _suspend() call this; if after a suspend() we get a _disconnect()
244*4882a593Smuzhiyun  * (as the case is when hibernating), nothing bad happens.
245*4882a593Smuzhiyun  */
i2400mu_notification_release(struct i2400mu * i2400mu)246*4882a593Smuzhiyun void i2400mu_notification_release(struct i2400mu *i2400mu)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct device *dev = &i2400mu->usb_iface->dev;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
251*4882a593Smuzhiyun 	if (i2400mu->notif_urb != NULL) {
252*4882a593Smuzhiyun 		usb_kill_urb(i2400mu->notif_urb);
253*4882a593Smuzhiyun 		kfree(i2400mu->notif_urb->transfer_buffer);
254*4882a593Smuzhiyun 		usb_free_urb(i2400mu->notif_urb);
255*4882a593Smuzhiyun 		i2400mu->notif_urb = NULL;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 	d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
258*4882a593Smuzhiyun }
259