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