aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wimax/i2400m
diff options
context:
space:
mode:
authorInaky Perez-Gonzalez <inaky@linux.intel.com>2008-12-20 19:57:53 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2009-01-07 13:00:21 -0500
commita8ebf98f541463107bb9544a1b611981dc2477e7 (patch)
treeaae6235a1c14ecc2a212f020c9168507d630a23a /drivers/net/wimax/i2400m
parent795038107b0078ee5ad3ad33327fe1c3520f6bf2 (diff)
i2400m/USB: TX and RX path backends
Implements the backend so that the generic driver can TX/RX to/from the USB device. TX is implemented with a kthread sitting in a never-ending loop that when kicked by the generic driver's TX code will pull data from the TX FIFO and send it to the device until it drains it. Then it goes back sleep, waiting for another kick. RX is implemented in a similar fashion, but reads are kicked in by the device notifying in the interrupt endpoint that data is ready. Device reset notifications are also sent via the notification endpoint. We need a thread contexts to run USB autopm functions (blocking) and to process the received data (can get to be heavy in processing time). Signed-off-by: Inaky Perez-Gonzalez <inaky@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/net/wimax/i2400m')
-rw-r--r--drivers/net/wimax/i2400m/usb-notif.c269
-rw-r--r--drivers/net/wimax/i2400m/usb-rx.c417
-rw-r--r--drivers/net/wimax/i2400m/usb-tx.c229
3 files changed, 915 insertions, 0 deletions
diff --git a/drivers/net/wimax/i2400m/usb-notif.c b/drivers/net/wimax/i2400m/usb-notif.c
new file mode 100644
index 000000000000..9702c22b2497
--- /dev/null
+++ b/drivers/net/wimax/i2400m/usb-notif.c
@@ -0,0 +1,269 @@
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_dev_reset_handle()
55 * i2400mu_rx_kick()
56 */
57#include <linux/usb.h>
58#include "i2400m-usb.h"
59
60
61#define D_SUBMODULE notif
62#include "usb-debug-levels.h"
63
64
65static const
66__le32 i2400m_ZERO_BARKER[4] = { 0, 0, 0, 0 };
67
68
69/*
70 * Process a received notification
71 *
72 * In normal operation mode, we can only receive two types of payloads
73 * on the notification endpoint:
74 *
75 * - a reboot barker, we do a bootstrap (the device has reseted).
76 *
77 * - a block of zeroes: there is pending data in the IN endpoint
78 */
79static
80int i2400mu_notification_grok(struct i2400mu *i2400mu, const void *buf,
81 size_t buf_len)
82{
83 int ret;
84 struct device *dev = &i2400mu->usb_iface->dev;
85 struct i2400m *i2400m = &i2400mu->i2400m;
86
87 d_fnstart(4, dev, "(i2400m %p buf %p buf_len %zu)\n",
88 i2400mu, buf, buf_len);
89 ret = -EIO;
90 if (buf_len < sizeof(i2400m_NBOOT_BARKER))
91 /* Not a bug, just ignore */
92 goto error_bad_size;
93 if (!memcmp(i2400m_NBOOT_BARKER, buf, sizeof(i2400m_NBOOT_BARKER))
94 || !memcmp(i2400m_SBOOT_BARKER, buf, sizeof(i2400m_SBOOT_BARKER)))
95 ret = i2400m_dev_reset_handle(i2400m);
96 else if (!memcmp(i2400m_ZERO_BARKER, buf, sizeof(i2400m_ZERO_BARKER))) {
97 i2400mu_rx_kick(i2400mu);
98 ret = 0;
99 } else { /* Unknown or unexpected data in the notif message */
100 char prefix[64];
101 ret = -EIO;
102 dev_err(dev, "HW BUG? Unknown/unexpected data in notification "
103 "message (%zu bytes)\n", buf_len);
104 snprintf(prefix, sizeof(prefix), "%s %s: ",
105 dev_driver_string(dev) , dev->bus_id);
106 if (buf_len > 64) {
107 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
108 8, 4, buf, 64, 0);
109 printk(KERN_ERR "%s... (only first 64 bytes "
110 "dumped)\n", prefix);
111 } else
112 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
113 8, 4, buf, buf_len, 0);
114 }
115error_bad_size:
116 d_fnend(4, dev, "(i2400m %p buf %p buf_len %zu) = %d\n",
117 i2400mu, buf, buf_len, ret);
118 return ret;
119}
120
121
122/*
123 * URB callback for the notification endpoint
124 *
125 * @urb: the urb received from the notification endpoint
126 *
127 * This function will just process the USB side of the transaction,
128 * checking everything is fine, pass the processing to
129 * i2400m_notification_grok() and resubmit the URB.
130 */
131static
132void i2400mu_notification_cb(struct urb *urb)
133{
134 int ret;
135 struct i2400mu *i2400mu = urb->context;
136 struct device *dev = &i2400mu->usb_iface->dev;
137
138 d_fnstart(4, dev, "(urb %p status %d actual_length %d)\n",
139 urb, urb->status, urb->actual_length);
140 ret = urb->status;
141 switch (ret) {
142 case 0:
143 ret = i2400mu_notification_grok(i2400mu, urb->transfer_buffer,
144 urb->actual_length);
145 if (ret == -EIO && edc_inc(&i2400mu->urb_edc, EDC_MAX_ERRORS,
146 EDC_ERROR_TIMEFRAME))
147 goto error_exceeded;
148 if (ret == -ENOMEM) /* uff...power cycle? shutdown? */
149 goto error_exceeded;
150 break;
151 case -EINVAL: /* while removing driver */
152 case -ENODEV: /* dev disconnect ... */
153 case -ENOENT: /* ditto */
154 case -ESHUTDOWN: /* URB killed */
155 case -ECONNRESET: /* disconnection */
156 goto out; /* Notify around */
157 default: /* Some error? */
158 if (edc_inc(&i2400mu->urb_edc,
159 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
160 goto error_exceeded;
161 dev_err(dev, "notification: URB error %d, retrying\n",
162 urb->status);
163 }
164 usb_mark_last_busy(i2400mu->usb_dev);
165 ret = usb_submit_urb(i2400mu->notif_urb, GFP_ATOMIC);
166 switch (ret) {
167 case 0:
168 case -EINVAL: /* while removing driver */
169 case -ENODEV: /* dev disconnect ... */
170 case -ENOENT: /* ditto */
171 case -ESHUTDOWN: /* URB killed */
172 case -ECONNRESET: /* disconnection */
173 break; /* just ignore */
174 default: /* Some error? */
175 dev_err(dev, "notification: cannot submit URB: %d\n", ret);
176 goto error_submit;
177 }
178 d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
179 urb, urb->status, urb->actual_length);
180 return;
181
182error_exceeded:
183 dev_err(dev, "maximum errors in notification URB exceeded; "
184 "resetting device\n");
185error_submit:
186 usb_queue_reset_device(i2400mu->usb_iface);
187out:
188 d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
189 urb, urb->status, urb->actual_length);
190 return;
191}
192
193
194/*
195 * setup the notification endpoint
196 *
197 * @i2400m: device descriptor
198 *
199 * This procedure prepares the notification urb and handler for receiving
200 * unsolicited barkers from the device.
201 */
202int i2400mu_notification_setup(struct i2400mu *i2400mu)
203{
204 struct device *dev = &i2400mu->usb_iface->dev;
205 int usb_pipe, ret = 0;
206 struct usb_endpoint_descriptor *epd;
207 char *buf;
208
209 d_fnstart(4, dev, "(i2400m %p)\n", i2400mu);
210 buf = kmalloc(I2400MU_MAX_NOTIFICATION_LEN, GFP_KERNEL | GFP_DMA);
211 if (buf == NULL) {
212 dev_err(dev, "notification: buffer allocation failed\n");
213 ret = -ENOMEM;
214 goto error_buf_alloc;
215 }
216
217 i2400mu->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
218 if (!i2400mu->notif_urb) {
219 ret = -ENOMEM;
220 dev_err(dev, "notification: cannot allocate URB\n");
221 goto error_alloc_urb;
222 }
223 epd = usb_get_epd(i2400mu->usb_iface, I2400MU_EP_NOTIFICATION);
224 usb_pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
225 usb_fill_int_urb(i2400mu->notif_urb, i2400mu->usb_dev, usb_pipe,
226 buf, I2400MU_MAX_NOTIFICATION_LEN,
227 i2400mu_notification_cb, i2400mu, epd->bInterval);
228 ret = usb_submit_urb(i2400mu->notif_urb, GFP_KERNEL);
229 if (ret != 0) {
230 dev_err(dev, "notification: cannot submit URB: %d\n", ret);
231 goto error_submit;
232 }
233 d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
234 return ret;
235
236error_submit:
237 usb_free_urb(i2400mu->notif_urb);
238error_alloc_urb:
239 kfree(buf);
240error_buf_alloc:
241 d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
242 return ret;
243}
244
245
246/*
247 * Tear down of the notification mechanism
248 *
249 * @i2400m: device descriptor
250 *
251 * Kill the interrupt endpoint urb, free any allocated resources.
252 *
253 * We need to check if we have done it before as for example,
254 * _suspend() call this; if after a suspend() we get a _disconnect()
255 * (as the case is when hibernating), nothing bad happens.
256 */
257void i2400mu_notification_release(struct i2400mu *i2400mu)
258{
259 struct device *dev = &i2400mu->usb_iface->dev;
260
261 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
262 if (i2400mu->notif_urb != NULL) {
263 usb_kill_urb(i2400mu->notif_urb);
264 kfree(i2400mu->notif_urb->transfer_buffer);
265 usb_free_urb(i2400mu->notif_urb);
266 i2400mu->notif_urb = NULL;
267 }
268 d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
269}
diff --git a/drivers/net/wimax/i2400m/usb-rx.c b/drivers/net/wimax/i2400m/usb-rx.c
new file mode 100644
index 000000000000..074cc1f89853
--- /dev/null
+++ b/drivers/net/wimax/i2400m/usb-rx.c
@@ -0,0 +1,417 @@
1/*
2 * Intel Wireless WiMAX Connection 2400m
3 * USB RX 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 * - Initial implementation
38 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
39 * - Use skb_clone(), break up processing in chunks
40 * - Split transport/device specific
41 * - Make buffer size dynamic to exert less memory pressure
42 *
43 *
44 * This handles the RX path on USB.
45 *
46 * When a notification is received that says 'there is RX data ready',
47 * we call i2400mu_rx_kick(); that wakes up the RX kthread, which
48 * reads a buffer from USB and passes it to i2400m_rx() in the generic
49 * handling code. The RX buffer has an specific format that is
50 * described in rx.c.
51 *
52 * We use a kernel thread in a loop because:
53 *
54 * - we want to be able to call the USB power management get/put
55 * functions (blocking) before each transaction.
56 *
57 * - We might get a lot of notifications and we don't want to submit
58 * a zillion reads; by serializing, we are throttling.
59 *
60 * - RX data processing can get heavy enough so that it is not
61 * appropiate for doing it in the USB callback; thus we run it in a
62 * process context.
63 *
64 * We provide a read buffer of an arbitrary size (short of a page); if
65 * the callback reports -EOVERFLOW, it means it was too small, so we
66 * just double the size and retry (being careful to append, as
67 * sometimes the device provided some data). Every now and then we
68 * check if the average packet size is smaller than the current packet
69 * size and if so, we halve it. At the end, the size of the
70 * preallocated buffer should be following the average received
71 * transaction size, adapting dynamically to it.
72 *
73 * ROADMAP
74 *
75 * i2400mu_rx_kick() Called from notif.c when we get a
76 * 'data ready' notification
77 * i2400mu_rxd() Kernel RX daemon
78 * i2400mu_rx() Receive USB data
79 * i2400m_rx() Send data to generic i2400m RX handling
80 *
81 * i2400mu_rx_setup() called from i2400mu_bus_dev_start()
82 *
83 * i2400mu_rx_release() called from i2400mu_bus_dev_stop()
84 */
85#include <linux/workqueue.h>
86#include <linux/usb.h>
87#include "i2400m-usb.h"
88
89
90#define D_SUBMODULE rx
91#include "usb-debug-levels.h"
92
93/*
94 * Dynamic RX size
95 *
96 * We can't let the rx_size be a multiple of 512 bytes (the RX
97 * endpoint's max packet size). On some USB host controllers (we
98 * haven't been able to fully characterize which), if the device is
99 * about to send (for example) X bytes and we only post a buffer to
100 * receive n*512, it will fail to mark that as babble (so that
101 * i2400mu_rx() [case -EOVERFLOW] can resize the buffer and get the
102 * rest).
103 *
104 * So on growing or shrinking, if it is a multiple of the
105 * maxpacketsize, we remove some (instead of incresing some, so in a
106 * buddy allocator we try to waste less space).
107 *
108 * Note we also need a hook for this on i2400mu_rx() -- when we do the
109 * first read, we are sure we won't hit this spot because
110 * i240mm->rx_size has been set properly. However, if we have to
111 * double because of -EOVERFLOW, when we launch the read to get the
112 * rest of the data, we *have* to make sure that also is not a
113 * multiple of the max_pkt_size.
114 */
115
116static
117size_t i2400mu_rx_size_grow(struct i2400mu *i2400mu)
118{
119 struct device *dev = &i2400mu->usb_iface->dev;
120 size_t rx_size;
121 const size_t max_pkt_size = 512;
122
123 rx_size = 2 * i2400mu->rx_size;
124 if (rx_size % max_pkt_size == 0) {
125 rx_size -= 8;
126 d_printf(1, dev,
127 "RX: expected size grew to %zu [adjusted -8] "
128 "from %zu\n",
129 rx_size, i2400mu->rx_size);
130 } else
131 d_printf(1, dev,
132 "RX: expected size grew to %zu from %zu\n",
133 rx_size, i2400mu->rx_size);
134 return rx_size;
135}
136
137
138static
139void i2400mu_rx_size_maybe_shrink(struct i2400mu *i2400mu)
140{
141 const size_t max_pkt_size = 512;
142 struct device *dev = &i2400mu->usb_iface->dev;
143
144 if (unlikely(i2400mu->rx_size_cnt >= 100
145 && i2400mu->rx_size_auto_shrink)) {
146 size_t avg_rx_size =
147 i2400mu->rx_size_acc / i2400mu->rx_size_cnt;
148 size_t new_rx_size = i2400mu->rx_size / 2;
149 if (avg_rx_size < new_rx_size) {
150 if (new_rx_size % max_pkt_size == 0) {
151 new_rx_size -= 8;
152 d_printf(1, dev,
153 "RX: expected size shrank to %zu "
154 "[adjusted -8] from %zu\n",
155 new_rx_size, i2400mu->rx_size);
156 } else
157 d_printf(1, dev,
158 "RX: expected size shrank to %zu "
159 "from %zu\n",
160 new_rx_size, i2400mu->rx_size);
161 i2400mu->rx_size = new_rx_size;
162 i2400mu->rx_size_cnt = 0;
163 i2400mu->rx_size_acc = i2400mu->rx_size;
164 }
165 }
166}
167
168/*
169 * Receive a message with payloads from the USB bus into an skb
170 *
171 * @i2400mu: USB device descriptor
172 * @rx_skb: skb where to place the received message
173 *
174 * Deals with all the USB-specifics of receiving, dynamically
175 * increasing the buffer size if so needed. Returns the payload in the
176 * skb, ready to process. On a zero-length packet, we retry.
177 *
178 * On soft USB errors, we retry (until they become too frequent and
179 * then are promoted to hard); on hard USB errors, we reset the
180 * device. On other errors (skb realloacation, we just drop it and
181 * hope for the next invocation to solve it).
182 *
183 * Returns: pointer to the skb if ok, ERR_PTR on error.
184 * NOTE: this function might realloc the skb (if it is too small),
185 * so always update with the one returned.
186 * ERR_PTR() is < 0 on error.
187 */
188static
189struct sk_buff *i2400mu_rx(struct i2400mu *i2400mu, struct sk_buff *rx_skb)
190{
191 int result = 0;
192 struct device *dev = &i2400mu->usb_iface->dev;
193 int usb_pipe, read_size, rx_size, do_autopm;
194 struct usb_endpoint_descriptor *epd;
195 const size_t max_pkt_size = 512;
196
197 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
198 do_autopm = atomic_read(&i2400mu->do_autopm);
199 result = do_autopm ?
200 usb_autopm_get_interface(i2400mu->usb_iface) : 0;
201 if (result < 0) {
202 dev_err(dev, "RX: can't get autopm: %d\n", result);
203 do_autopm = 0;
204 }
205 epd = usb_get_epd(i2400mu->usb_iface, I2400MU_EP_BULK_IN);
206 usb_pipe = usb_rcvbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
207retry:
208 rx_size = skb_end_pointer(rx_skb) - rx_skb->data - rx_skb->len;
209 if (unlikely(rx_size % max_pkt_size == 0)) {
210 rx_size -= 8;
211 d_printf(1, dev, "RX: rx_size adapted to %d [-8]\n", rx_size);
212 }
213 result = usb_bulk_msg(
214 i2400mu->usb_dev, usb_pipe, rx_skb->data + rx_skb->len,
215 rx_size, &read_size, HZ);
216 usb_mark_last_busy(i2400mu->usb_dev);
217 switch (result) {
218 case 0:
219 if (read_size == 0)
220 goto retry; /* ZLP, just resubmit */
221 skb_put(rx_skb, read_size);
222 break;
223 case -EINVAL: /* while removing driver */
224 case -ENODEV: /* dev disconnect ... */
225 case -ENOENT: /* just ignore it */
226 case -ESHUTDOWN:
227 case -ECONNRESET:
228 break;
229 case -EOVERFLOW: { /* too small, reallocate */
230 struct sk_buff *new_skb;
231 rx_size = i2400mu_rx_size_grow(i2400mu);
232 if (rx_size <= (1 << 16)) /* cap it */
233 i2400mu->rx_size = rx_size;
234 else if (printk_ratelimit()) {
235 dev_err(dev, "BUG? rx_size up to %d\n", rx_size);
236 result = -EINVAL;
237 goto out;
238 }
239 skb_put(rx_skb, read_size);
240 new_skb = skb_copy_expand(rx_skb, 0, rx_size - rx_skb->len,
241 GFP_KERNEL);
242 if (new_skb == NULL) {
243 if (printk_ratelimit())
244 dev_err(dev, "RX: Can't reallocate skb to %d; "
245 "RX dropped\n", rx_size);
246 kfree(rx_skb);
247 result = 0;
248 goto out; /* drop it...*/
249 }
250 kfree_skb(rx_skb);
251 rx_skb = new_skb;
252 i2400mu->rx_size_cnt = 0;
253 i2400mu->rx_size_acc = i2400mu->rx_size;
254 d_printf(1, dev, "RX: size changed to %d, received %d, "
255 "copied %d, capacity %ld\n",
256 rx_size, read_size, rx_skb->len,
257 (long) (skb_end_pointer(new_skb) - new_skb->head));
258 goto retry;
259 }
260 /* In most cases, it happens due to the hardware scheduling a
261 * read when there was no data - unfortunately, we have no way
262 * to tell this timeout from a USB timeout. So we just ignore
263 * it. */
264 case -ETIMEDOUT:
265 dev_err(dev, "RX: timeout: %d\n", result);
266 result = 0;
267 break;
268 default: /* Any error */
269 if (edc_inc(&i2400mu->urb_edc,
270 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
271 goto error_reset;
272 dev_err(dev, "RX: error receiving URB: %d, retrying\n", result);
273 goto retry;
274 }
275out:
276 if (do_autopm)
277 usb_autopm_put_interface(i2400mu->usb_iface);
278 d_fnend(4, dev, "(i2400mu %p) = %p\n", i2400mu, rx_skb);
279 return rx_skb;
280
281error_reset:
282 dev_err(dev, "RX: maximum errors in URB exceeded; "
283 "resetting device\n");
284 usb_queue_reset_device(i2400mu->usb_iface);
285 rx_skb = ERR_PTR(result);
286 goto out;
287}
288
289
290/*
291 * Kernel thread for USB reception of data
292 *
293 * This thread waits for a kick; once kicked, it will allocate an skb
294 * and receive a single message to it from USB (using
295 * i2400mu_rx()). Once received, it is passed to the generic i2400m RX
296 * code for processing.
297 *
298 * When done processing, it runs some dirty statistics to verify if
299 * the last 100 messages received were smaller than half of the
300 * current RX buffer size. In that case, the RX buffer size is
301 * halved. This will helps lowering the pressure on the memory
302 * allocator.
303 *
304 * Hard errors force the thread to exit.
305 */
306static
307int i2400mu_rxd(void *_i2400mu)
308{
309 int result = 0;
310 struct i2400mu *i2400mu = _i2400mu;
311 struct i2400m *i2400m = &i2400mu->i2400m;
312 struct device *dev = &i2400mu->usb_iface->dev;
313 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
314 size_t pending;
315 int rx_size;
316 struct sk_buff *rx_skb;
317
318 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
319 while (1) {
320 d_printf(2, dev, "TX: waiting for messages\n");
321 pending = 0;
322 wait_event_interruptible(
323 i2400mu->rx_wq,
324 (kthread_should_stop() /* check this first! */
325 || (pending = atomic_read(&i2400mu->rx_pending_count)))
326 );
327 if (kthread_should_stop())
328 break;
329 if (pending == 0)
330 continue;
331 rx_size = i2400mu->rx_size;
332 d_printf(2, dev, "RX: reading up to %d bytes\n", rx_size);
333 rx_skb = __netdev_alloc_skb(net_dev, rx_size, GFP_KERNEL);
334 if (rx_skb == NULL) {
335 dev_err(dev, "RX: can't allocate skb [%d bytes]\n",
336 rx_size);
337 msleep(50); /* give it some time? */
338 continue;
339 }
340
341 /* Receive the message with the payloads */
342 rx_skb = i2400mu_rx(i2400mu, rx_skb);
343 result = PTR_ERR(rx_skb);
344 if (IS_ERR(rx_skb))
345 goto out;
346 atomic_dec(&i2400mu->rx_pending_count);
347 if (rx_skb->len == 0) { /* some ignorable condition */
348 kfree_skb(rx_skb);
349 continue;
350 }
351
352 /* Deliver the message to the generic i2400m code */
353 i2400mu->rx_size_cnt++;
354 i2400mu->rx_size_acc += rx_skb->len;
355 result = i2400m_rx(i2400m, rx_skb);
356 if (result == -EIO
357 && edc_inc(&i2400mu->urb_edc,
358 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
359 goto error_reset;
360 }
361
362 /* Maybe adjust RX buffer size */
363 i2400mu_rx_size_maybe_shrink(i2400mu);
364 }
365 result = 0;
366out:
367 d_fnend(4, dev, "(i2400mu %p) = %d\n", i2400mu, result);
368 return result;
369
370error_reset:
371 dev_err(dev, "RX: maximum errors in received buffer exceeded; "
372 "resetting device\n");
373 usb_queue_reset_device(i2400mu->usb_iface);
374 goto out;
375}
376
377
378/*
379 * Start reading from the device
380 *
381 * @i2400m: device instance
382 *
383 * Notify the RX thread that there is data pending.
384 */
385void i2400mu_rx_kick(struct i2400mu *i2400mu)
386{
387 struct i2400m *i2400m = &i2400mu->i2400m;
388 struct device *dev = &i2400mu->usb_iface->dev;
389
390 d_fnstart(3, dev, "(i2400mu %p)\n", i2400m);
391 atomic_inc(&i2400mu->rx_pending_count);
392 wake_up_all(&i2400mu->rx_wq);
393 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
394}
395
396
397int i2400mu_rx_setup(struct i2400mu *i2400mu)
398{
399 int result = 0;
400 struct i2400m *i2400m = &i2400mu->i2400m;
401 struct device *dev = &i2400mu->usb_iface->dev;
402 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
403
404 i2400mu->rx_kthread = kthread_run(i2400mu_rxd, i2400mu, "%s-rx",
405 wimax_dev->name);
406 if (IS_ERR(i2400mu->rx_kthread)) {
407 result = PTR_ERR(i2400mu->rx_kthread);
408 dev_err(dev, "RX: cannot start thread: %d\n", result);
409 }
410 return result;
411}
412
413void i2400mu_rx_release(struct i2400mu *i2400mu)
414{
415 kthread_stop(i2400mu->rx_kthread);
416}
417
diff --git a/drivers/net/wimax/i2400m/usb-tx.c b/drivers/net/wimax/i2400m/usb-tx.c
new file mode 100644
index 000000000000..dfd893356f49
--- /dev/null
+++ b/drivers/net/wimax/i2400m/usb-tx.c
@@ -0,0 +1,229 @@
1/*
2 * Intel Wireless WiMAX Connection 2400m
3 * USB specific TX 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 * - Initial implementation
38 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
39 * - Split transport/device specific
40 *
41 *
42 * Takes the TX messages in the i2400m's driver TX FIFO and sends them
43 * to the device until there are no more.
44 *
45 * If we fail sending the message, we just drop it. There isn't much
46 * we can do at this point. We could also retry, but the USB stack has
47 * already retried and still failed, so there is not much of a
48 * point. As well, most of the traffic is network, which has recovery
49 * methods for dropped packets.
50 *
51 * For sending we just obtain a FIFO buffer to send, send it to the
52 * USB bulk out, tell the TX FIFO code we have sent it; query for
53 * another one, etc... until done.
54 *
55 * We use a thread so we can call usb_autopm_enable() and
56 * usb_autopm_disable() for each transaction; this way when the device
57 * goes idle, it will suspend. It also has less overhead than a
58 * dedicated workqueue, as it is being used for a single task.
59 *
60 * ROADMAP
61 *
62 * i2400mu_tx_setup()
63 * i2400mu_tx_release()
64 *
65 * i2400mu_bus_tx_kick() - Called by the tx.c code when there
66 * is new data in the FIFO.
67 * i2400mu_txd()
68 * i2400m_tx_msg_get()
69 * i2400m_tx_msg_sent()
70 */
71#include "i2400m-usb.h"
72
73
74#define D_SUBMODULE tx
75#include "usb-debug-levels.h"
76
77
78/*
79 * Get the next TX message in the TX FIFO and send it to the device
80 *
81 * Note that any iteration consumes a message to be sent, no matter if
82 * it succeeds or fails (we have no real way to retry or complain).
83 *
84 * Return: 0 if ok, < 0 errno code on hard error.
85 */
86static
87int i2400mu_tx(struct i2400mu *i2400mu, struct i2400m_msg_hdr *tx_msg,
88 size_t tx_msg_size)
89{
90 int result = 0;
91 struct i2400m *i2400m = &i2400mu->i2400m;
92 struct device *dev = &i2400mu->usb_iface->dev;
93 int usb_pipe, sent_size, do_autopm;
94 struct usb_endpoint_descriptor *epd;
95
96 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
97 do_autopm = atomic_read(&i2400mu->do_autopm);
98 result = do_autopm ?
99 usb_autopm_get_interface(i2400mu->usb_iface) : 0;
100 if (result < 0) {
101 dev_err(dev, "TX: can't get autopm: %d\n", result);
102 do_autopm = 0;
103 }
104 epd = usb_get_epd(i2400mu->usb_iface, I2400MU_EP_BULK_OUT);
105 usb_pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
106retry:
107 result = usb_bulk_msg(i2400mu->usb_dev, usb_pipe,
108 tx_msg, tx_msg_size, &sent_size, HZ);
109 usb_mark_last_busy(i2400mu->usb_dev);
110 switch (result) {
111 case 0:
112 if (sent_size != tx_msg_size) { /* Too short? drop it */
113 dev_err(dev, "TX: short write (%d B vs %zu "
114 "expected)\n", sent_size, tx_msg_size);
115 result = -EIO;
116 }
117 break;
118 case -EINVAL: /* while removing driver */
119 case -ENODEV: /* dev disconnect ... */
120 case -ENOENT: /* just ignore it */
121 case -ESHUTDOWN: /* and exit */
122 case -ECONNRESET:
123 result = -ESHUTDOWN;
124 break;
125 default: /* Some error? */
126 if (edc_inc(&i2400mu->urb_edc,
127 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
128 dev_err(dev, "TX: maximum errors in URB "
129 "exceeded; resetting device\n");
130 usb_queue_reset_device(i2400mu->usb_iface);
131 } else {
132 dev_err(dev, "TX: cannot send URB; retrying. "
133 "tx_msg @%zu %zu B [%d sent]: %d\n",
134 (void *) tx_msg - i2400m->tx_buf,
135 tx_msg_size, sent_size, result);
136 goto retry;
137 }
138 }
139 if (do_autopm)
140 usb_autopm_put_interface(i2400mu->usb_iface);
141 d_fnend(4, dev, "(i2400mu %p) = result\n", i2400mu);
142 return result;
143}
144
145
146/*
147 * Get the next TX message in the TX FIFO and send it to the device
148 *
149 * Note we exit the loop if i2400mu_tx() fails; that funtion only
150 * fails on hard error (failing to tx a buffer not being one of them,
151 * see its doc).
152 *
153 * Return: 0
154 */
155static
156int i2400mu_txd(void *_i2400mu)
157{
158 int result = 0;
159 struct i2400mu *i2400mu = _i2400mu;
160 struct i2400m *i2400m = &i2400mu->i2400m;
161 struct device *dev = &i2400mu->usb_iface->dev;
162 struct i2400m_msg_hdr *tx_msg;
163 size_t tx_msg_size;
164
165 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
166
167 while (1) {
168 d_printf(2, dev, "TX: waiting for messages\n");
169 tx_msg = NULL;
170 wait_event_interruptible(
171 i2400mu->tx_wq,
172 (kthread_should_stop() /* check this first! */
173 || (tx_msg = i2400m_tx_msg_get(i2400m, &tx_msg_size)))
174 );
175 if (kthread_should_stop())
176 break;
177 WARN_ON(tx_msg == NULL); /* should not happen...*/
178 d_printf(2, dev, "TX: submitting %zu bytes\n", tx_msg_size);
179 d_dump(5, dev, tx_msg, tx_msg_size);
180 /* Yeah, we ignore errors ... not much we can do */
181 i2400mu_tx(i2400mu, tx_msg, tx_msg_size);
182 i2400m_tx_msg_sent(i2400m); /* ack it, advance the FIFO */
183 if (result < 0)
184 break;
185 }
186 d_fnend(4, dev, "(i2400mu %p) = %d\n", i2400mu, result);
187 return result;
188}
189
190
191/*
192 * i2400m TX engine notifies us that there is data in the FIFO ready
193 * for TX
194 *
195 * If there is a URB in flight, don't do anything; when it finishes,
196 * it will see there is data in the FIFO and send it. Else, just
197 * submit a write.
198 */
199void i2400mu_bus_tx_kick(struct i2400m *i2400m)
200{
201 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
202 struct device *dev = &i2400mu->usb_iface->dev;
203
204 d_fnstart(3, dev, "(i2400m %p) = void\n", i2400m);
205 wake_up_all(&i2400mu->tx_wq);
206 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
207}
208
209
210int i2400mu_tx_setup(struct i2400mu *i2400mu)
211{
212 int result = 0;
213 struct i2400m *i2400m = &i2400mu->i2400m;
214 struct device *dev = &i2400mu->usb_iface->dev;
215 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
216
217 i2400mu->tx_kthread = kthread_run(i2400mu_txd, i2400mu, "%s-tx",
218 wimax_dev->name);
219 if (IS_ERR(i2400mu->tx_kthread)) {
220 result = PTR_ERR(i2400mu->tx_kthread);
221 dev_err(dev, "TX: cannot start thread: %d\n", result);
222 }
223 return result;
224}
225
226void i2400mu_tx_release(struct i2400mu *i2400mu)
227{
228 kthread_stop(i2400mu->tx_kthread);
229}