18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Intel Wireless WiMAX Connection 2400m 38c2ecf20Sopenharmony_ci * USB RX handling 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Intel Corporation. All rights reserved. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 108c2ecf20Sopenharmony_ci * are met: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * * Redistributions of source code must retain the above copyright 138c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 148c2ecf20Sopenharmony_ci * * Redistributions in binary form must reproduce the above copyright 158c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in 168c2ecf20Sopenharmony_ci * the documentation and/or other materials provided with the 178c2ecf20Sopenharmony_ci * distribution. 188c2ecf20Sopenharmony_ci * * Neither the name of Intel Corporation nor the names of its 198c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived 208c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 238c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 248c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 258c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 268c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 278c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 288c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 298c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 308c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 318c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 328c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * Intel Corporation <linux-wimax@intel.com> 368c2ecf20Sopenharmony_ci * Yanir Lubetkin <yanirx.lubetkin@intel.com> 378c2ecf20Sopenharmony_ci * - Initial implementation 388c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 398c2ecf20Sopenharmony_ci * - Use skb_clone(), break up processing in chunks 408c2ecf20Sopenharmony_ci * - Split transport/device specific 418c2ecf20Sopenharmony_ci * - Make buffer size dynamic to exert less memory pressure 428c2ecf20Sopenharmony_ci * 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * This handles the RX path on USB. 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci * When a notification is received that says 'there is RX data ready', 478c2ecf20Sopenharmony_ci * we call i2400mu_rx_kick(); that wakes up the RX kthread, which 488c2ecf20Sopenharmony_ci * reads a buffer from USB and passes it to i2400m_rx() in the generic 498c2ecf20Sopenharmony_ci * handling code. The RX buffer has an specific format that is 508c2ecf20Sopenharmony_ci * described in rx.c. 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * We use a kernel thread in a loop because: 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * - we want to be able to call the USB power management get/put 558c2ecf20Sopenharmony_ci * functions (blocking) before each transaction. 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * - We might get a lot of notifications and we don't want to submit 588c2ecf20Sopenharmony_ci * a zillion reads; by serializing, we are throttling. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * - RX data processing can get heavy enough so that it is not 618c2ecf20Sopenharmony_ci * appropriate for doing it in the USB callback; thus we run it in a 628c2ecf20Sopenharmony_ci * process context. 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * We provide a read buffer of an arbitrary size (short of a page); if 658c2ecf20Sopenharmony_ci * the callback reports -EOVERFLOW, it means it was too small, so we 668c2ecf20Sopenharmony_ci * just double the size and retry (being careful to append, as 678c2ecf20Sopenharmony_ci * sometimes the device provided some data). Every now and then we 688c2ecf20Sopenharmony_ci * check if the average packet size is smaller than the current packet 698c2ecf20Sopenharmony_ci * size and if so, we halve it. At the end, the size of the 708c2ecf20Sopenharmony_ci * preallocated buffer should be following the average received 718c2ecf20Sopenharmony_ci * transaction size, adapting dynamically to it. 728c2ecf20Sopenharmony_ci * 738c2ecf20Sopenharmony_ci * ROADMAP 748c2ecf20Sopenharmony_ci * 758c2ecf20Sopenharmony_ci * i2400mu_rx_kick() Called from notif.c when we get a 768c2ecf20Sopenharmony_ci * 'data ready' notification 778c2ecf20Sopenharmony_ci * i2400mu_rxd() Kernel RX daemon 788c2ecf20Sopenharmony_ci * i2400mu_rx() Receive USB data 798c2ecf20Sopenharmony_ci * i2400m_rx() Send data to generic i2400m RX handling 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * i2400mu_rx_setup() called from i2400mu_bus_dev_start() 828c2ecf20Sopenharmony_ci * 838c2ecf20Sopenharmony_ci * i2400mu_rx_release() called from i2400mu_bus_dev_stop() 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 868c2ecf20Sopenharmony_ci#include <linux/slab.h> 878c2ecf20Sopenharmony_ci#include <linux/usb.h> 888c2ecf20Sopenharmony_ci#include "i2400m-usb.h" 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define D_SUBMODULE rx 928c2ecf20Sopenharmony_ci#include "usb-debug-levels.h" 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* 958c2ecf20Sopenharmony_ci * Dynamic RX size 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * We can't let the rx_size be a multiple of 512 bytes (the RX 988c2ecf20Sopenharmony_ci * endpoint's max packet size). On some USB host controllers (we 998c2ecf20Sopenharmony_ci * haven't been able to fully characterize which), if the device is 1008c2ecf20Sopenharmony_ci * about to send (for example) X bytes and we only post a buffer to 1018c2ecf20Sopenharmony_ci * receive n*512, it will fail to mark that as babble (so that 1028c2ecf20Sopenharmony_ci * i2400mu_rx() [case -EOVERFLOW] can resize the buffer and get the 1038c2ecf20Sopenharmony_ci * rest). 1048c2ecf20Sopenharmony_ci * 1058c2ecf20Sopenharmony_ci * So on growing or shrinking, if it is a multiple of the 1068c2ecf20Sopenharmony_ci * maxpacketsize, we remove some (instead of incresing some, so in a 1078c2ecf20Sopenharmony_ci * buddy allocator we try to waste less space). 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * Note we also need a hook for this on i2400mu_rx() -- when we do the 1108c2ecf20Sopenharmony_ci * first read, we are sure we won't hit this spot because 1118c2ecf20Sopenharmony_ci * i240mm->rx_size has been set properly. However, if we have to 1128c2ecf20Sopenharmony_ci * double because of -EOVERFLOW, when we launch the read to get the 1138c2ecf20Sopenharmony_ci * rest of the data, we *have* to make sure that also is not a 1148c2ecf20Sopenharmony_ci * multiple of the max_pkt_size. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic 1188c2ecf20Sopenharmony_cisize_t i2400mu_rx_size_grow(struct i2400mu *i2400mu) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 1218c2ecf20Sopenharmony_ci size_t rx_size; 1228c2ecf20Sopenharmony_ci const size_t max_pkt_size = 512; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci rx_size = 2 * i2400mu->rx_size; 1258c2ecf20Sopenharmony_ci if (rx_size % max_pkt_size == 0) { 1268c2ecf20Sopenharmony_ci rx_size -= 8; 1278c2ecf20Sopenharmony_ci d_printf(1, dev, 1288c2ecf20Sopenharmony_ci "RX: expected size grew to %zu [adjusted -8] " 1298c2ecf20Sopenharmony_ci "from %zu\n", 1308c2ecf20Sopenharmony_ci rx_size, i2400mu->rx_size); 1318c2ecf20Sopenharmony_ci } else 1328c2ecf20Sopenharmony_ci d_printf(1, dev, 1338c2ecf20Sopenharmony_ci "RX: expected size grew to %zu from %zu\n", 1348c2ecf20Sopenharmony_ci rx_size, i2400mu->rx_size); 1358c2ecf20Sopenharmony_ci return rx_size; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic 1408c2ecf20Sopenharmony_civoid i2400mu_rx_size_maybe_shrink(struct i2400mu *i2400mu) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci const size_t max_pkt_size = 512; 1438c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci if (unlikely(i2400mu->rx_size_cnt >= 100 1468c2ecf20Sopenharmony_ci && i2400mu->rx_size_auto_shrink)) { 1478c2ecf20Sopenharmony_ci size_t avg_rx_size = 1488c2ecf20Sopenharmony_ci i2400mu->rx_size_acc / i2400mu->rx_size_cnt; 1498c2ecf20Sopenharmony_ci size_t new_rx_size = i2400mu->rx_size / 2; 1508c2ecf20Sopenharmony_ci if (avg_rx_size < new_rx_size) { 1518c2ecf20Sopenharmony_ci if (new_rx_size % max_pkt_size == 0) { 1528c2ecf20Sopenharmony_ci new_rx_size -= 8; 1538c2ecf20Sopenharmony_ci d_printf(1, dev, 1548c2ecf20Sopenharmony_ci "RX: expected size shrank to %zu " 1558c2ecf20Sopenharmony_ci "[adjusted -8] from %zu\n", 1568c2ecf20Sopenharmony_ci new_rx_size, i2400mu->rx_size); 1578c2ecf20Sopenharmony_ci } else 1588c2ecf20Sopenharmony_ci d_printf(1, dev, 1598c2ecf20Sopenharmony_ci "RX: expected size shrank to %zu " 1608c2ecf20Sopenharmony_ci "from %zu\n", 1618c2ecf20Sopenharmony_ci new_rx_size, i2400mu->rx_size); 1628c2ecf20Sopenharmony_ci i2400mu->rx_size = new_rx_size; 1638c2ecf20Sopenharmony_ci i2400mu->rx_size_cnt = 0; 1648c2ecf20Sopenharmony_ci i2400mu->rx_size_acc = i2400mu->rx_size; 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci/* 1708c2ecf20Sopenharmony_ci * Receive a message with payloads from the USB bus into an skb 1718c2ecf20Sopenharmony_ci * 1728c2ecf20Sopenharmony_ci * @i2400mu: USB device descriptor 1738c2ecf20Sopenharmony_ci * @rx_skb: skb where to place the received message 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * Deals with all the USB-specifics of receiving, dynamically 1768c2ecf20Sopenharmony_ci * increasing the buffer size if so needed. Returns the payload in the 1778c2ecf20Sopenharmony_ci * skb, ready to process. On a zero-length packet, we retry. 1788c2ecf20Sopenharmony_ci * 1798c2ecf20Sopenharmony_ci * On soft USB errors, we retry (until they become too frequent and 1808c2ecf20Sopenharmony_ci * then are promoted to hard); on hard USB errors, we reset the 1818c2ecf20Sopenharmony_ci * device. On other errors (skb realloacation, we just drop it and 1828c2ecf20Sopenharmony_ci * hope for the next invocation to solve it). 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * Returns: pointer to the skb if ok, ERR_PTR on error. 1858c2ecf20Sopenharmony_ci * NOTE: this function might realloc the skb (if it is too small), 1868c2ecf20Sopenharmony_ci * so always update with the one returned. 1878c2ecf20Sopenharmony_ci * ERR_PTR() is < 0 on error. 1888c2ecf20Sopenharmony_ci * Will return NULL if it cannot reallocate -- this can be 1898c2ecf20Sopenharmony_ci * considered a transient retryable error. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_cistatic 1928c2ecf20Sopenharmony_cistruct sk_buff *i2400mu_rx(struct i2400mu *i2400mu, struct sk_buff *rx_skb) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci int result = 0; 1958c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 1968c2ecf20Sopenharmony_ci int usb_pipe, read_size, rx_size, do_autopm; 1978c2ecf20Sopenharmony_ci struct usb_endpoint_descriptor *epd; 1988c2ecf20Sopenharmony_ci const size_t max_pkt_size = 512; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu); 2018c2ecf20Sopenharmony_ci do_autopm = atomic_read(&i2400mu->do_autopm); 2028c2ecf20Sopenharmony_ci result = do_autopm ? 2038c2ecf20Sopenharmony_ci usb_autopm_get_interface(i2400mu->usb_iface) : 0; 2048c2ecf20Sopenharmony_ci if (result < 0) { 2058c2ecf20Sopenharmony_ci dev_err(dev, "RX: can't get autopm: %d\n", result); 2068c2ecf20Sopenharmony_ci do_autopm = 0; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_in); 2098c2ecf20Sopenharmony_ci usb_pipe = usb_rcvbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress); 2108c2ecf20Sopenharmony_ciretry: 2118c2ecf20Sopenharmony_ci rx_size = skb_end_pointer(rx_skb) - rx_skb->data - rx_skb->len; 2128c2ecf20Sopenharmony_ci if (unlikely(rx_size % max_pkt_size == 0)) { 2138c2ecf20Sopenharmony_ci rx_size -= 8; 2148c2ecf20Sopenharmony_ci d_printf(1, dev, "RX: rx_size adapted to %d [-8]\n", rx_size); 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci result = usb_bulk_msg( 2178c2ecf20Sopenharmony_ci i2400mu->usb_dev, usb_pipe, rx_skb->data + rx_skb->len, 2188c2ecf20Sopenharmony_ci rx_size, &read_size, 200); 2198c2ecf20Sopenharmony_ci usb_mark_last_busy(i2400mu->usb_dev); 2208c2ecf20Sopenharmony_ci switch (result) { 2218c2ecf20Sopenharmony_ci case 0: 2228c2ecf20Sopenharmony_ci if (read_size == 0) 2238c2ecf20Sopenharmony_ci goto retry; /* ZLP, just resubmit */ 2248c2ecf20Sopenharmony_ci skb_put(rx_skb, read_size); 2258c2ecf20Sopenharmony_ci break; 2268c2ecf20Sopenharmony_ci case -EPIPE: 2278c2ecf20Sopenharmony_ci /* 2288c2ecf20Sopenharmony_ci * Stall -- maybe the device is choking with our 2298c2ecf20Sopenharmony_ci * requests. Clear it and give it some time. If they 2308c2ecf20Sopenharmony_ci * happen to often, it might be another symptom, so we 2318c2ecf20Sopenharmony_ci * reset. 2328c2ecf20Sopenharmony_ci * 2338c2ecf20Sopenharmony_ci * No error handling for usb_clear_halt(0; if it 2348c2ecf20Sopenharmony_ci * works, the retry works; if it fails, this switch 2358c2ecf20Sopenharmony_ci * does the error handling for us. 2368c2ecf20Sopenharmony_ci */ 2378c2ecf20Sopenharmony_ci if (edc_inc(&i2400mu->urb_edc, 2388c2ecf20Sopenharmony_ci 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { 2398c2ecf20Sopenharmony_ci dev_err(dev, "BM-CMD: too many stalls in " 2408c2ecf20Sopenharmony_ci "URB; resetting device\n"); 2418c2ecf20Sopenharmony_ci goto do_reset; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci usb_clear_halt(i2400mu->usb_dev, usb_pipe); 2448c2ecf20Sopenharmony_ci msleep(10); /* give the device some time */ 2458c2ecf20Sopenharmony_ci goto retry; 2468c2ecf20Sopenharmony_ci case -EINVAL: /* while removing driver */ 2478c2ecf20Sopenharmony_ci case -ENODEV: /* dev disconnect ... */ 2488c2ecf20Sopenharmony_ci case -ENOENT: /* just ignore it */ 2498c2ecf20Sopenharmony_ci case -ESHUTDOWN: 2508c2ecf20Sopenharmony_ci case -ECONNRESET: 2518c2ecf20Sopenharmony_ci break; 2528c2ecf20Sopenharmony_ci case -EOVERFLOW: { /* too small, reallocate */ 2538c2ecf20Sopenharmony_ci struct sk_buff *new_skb; 2548c2ecf20Sopenharmony_ci rx_size = i2400mu_rx_size_grow(i2400mu); 2558c2ecf20Sopenharmony_ci if (rx_size <= (1 << 16)) /* cap it */ 2568c2ecf20Sopenharmony_ci i2400mu->rx_size = rx_size; 2578c2ecf20Sopenharmony_ci else if (printk_ratelimit()) { 2588c2ecf20Sopenharmony_ci dev_err(dev, "BUG? rx_size up to %d\n", rx_size); 2598c2ecf20Sopenharmony_ci result = -EINVAL; 2608c2ecf20Sopenharmony_ci goto out; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci skb_put(rx_skb, read_size); 2638c2ecf20Sopenharmony_ci new_skb = skb_copy_expand(rx_skb, 0, rx_size - rx_skb->len, 2648c2ecf20Sopenharmony_ci GFP_KERNEL); 2658c2ecf20Sopenharmony_ci if (new_skb == NULL) { 2668c2ecf20Sopenharmony_ci kfree_skb(rx_skb); 2678c2ecf20Sopenharmony_ci rx_skb = NULL; 2688c2ecf20Sopenharmony_ci goto out; /* drop it...*/ 2698c2ecf20Sopenharmony_ci } 2708c2ecf20Sopenharmony_ci kfree_skb(rx_skb); 2718c2ecf20Sopenharmony_ci rx_skb = new_skb; 2728c2ecf20Sopenharmony_ci i2400mu->rx_size_cnt = 0; 2738c2ecf20Sopenharmony_ci i2400mu->rx_size_acc = i2400mu->rx_size; 2748c2ecf20Sopenharmony_ci d_printf(1, dev, "RX: size changed to %d, received %d, " 2758c2ecf20Sopenharmony_ci "copied %d, capacity %ld\n", 2768c2ecf20Sopenharmony_ci rx_size, read_size, rx_skb->len, 2778c2ecf20Sopenharmony_ci (long) skb_end_offset(new_skb)); 2788c2ecf20Sopenharmony_ci goto retry; 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci /* In most cases, it happens due to the hardware scheduling a 2818c2ecf20Sopenharmony_ci * read when there was no data - unfortunately, we have no way 2828c2ecf20Sopenharmony_ci * to tell this timeout from a USB timeout. So we just ignore 2838c2ecf20Sopenharmony_ci * it. */ 2848c2ecf20Sopenharmony_ci case -ETIMEDOUT: 2858c2ecf20Sopenharmony_ci dev_err(dev, "RX: timeout: %d\n", result); 2868c2ecf20Sopenharmony_ci result = 0; 2878c2ecf20Sopenharmony_ci break; 2888c2ecf20Sopenharmony_ci default: /* Any error */ 2898c2ecf20Sopenharmony_ci if (edc_inc(&i2400mu->urb_edc, 2908c2ecf20Sopenharmony_ci EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) 2918c2ecf20Sopenharmony_ci goto error_reset; 2928c2ecf20Sopenharmony_ci dev_err(dev, "RX: error receiving URB: %d, retrying\n", result); 2938c2ecf20Sopenharmony_ci goto retry; 2948c2ecf20Sopenharmony_ci } 2958c2ecf20Sopenharmony_ciout: 2968c2ecf20Sopenharmony_ci if (do_autopm) 2978c2ecf20Sopenharmony_ci usb_autopm_put_interface(i2400mu->usb_iface); 2988c2ecf20Sopenharmony_ci d_fnend(4, dev, "(i2400mu %p) = %p\n", i2400mu, rx_skb); 2998c2ecf20Sopenharmony_ci return rx_skb; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_cierror_reset: 3028c2ecf20Sopenharmony_ci dev_err(dev, "RX: maximum errors in URB exceeded; " 3038c2ecf20Sopenharmony_ci "resetting device\n"); 3048c2ecf20Sopenharmony_cido_reset: 3058c2ecf20Sopenharmony_ci usb_queue_reset_device(i2400mu->usb_iface); 3068c2ecf20Sopenharmony_ci rx_skb = ERR_PTR(result); 3078c2ecf20Sopenharmony_ci goto out; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci/* 3128c2ecf20Sopenharmony_ci * Kernel thread for USB reception of data 3138c2ecf20Sopenharmony_ci * 3148c2ecf20Sopenharmony_ci * This thread waits for a kick; once kicked, it will allocate an skb 3158c2ecf20Sopenharmony_ci * and receive a single message to it from USB (using 3168c2ecf20Sopenharmony_ci * i2400mu_rx()). Once received, it is passed to the generic i2400m RX 3178c2ecf20Sopenharmony_ci * code for processing. 3188c2ecf20Sopenharmony_ci * 3198c2ecf20Sopenharmony_ci * When done processing, it runs some dirty statistics to verify if 3208c2ecf20Sopenharmony_ci * the last 100 messages received were smaller than half of the 3218c2ecf20Sopenharmony_ci * current RX buffer size. In that case, the RX buffer size is 3228c2ecf20Sopenharmony_ci * halved. This will helps lowering the pressure on the memory 3238c2ecf20Sopenharmony_ci * allocator. 3248c2ecf20Sopenharmony_ci * 3258c2ecf20Sopenharmony_ci * Hard errors force the thread to exit. 3268c2ecf20Sopenharmony_ci */ 3278c2ecf20Sopenharmony_cistatic 3288c2ecf20Sopenharmony_ciint i2400mu_rxd(void *_i2400mu) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci int result = 0; 3318c2ecf20Sopenharmony_ci struct i2400mu *i2400mu = _i2400mu; 3328c2ecf20Sopenharmony_ci struct i2400m *i2400m = &i2400mu->i2400m; 3338c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 3348c2ecf20Sopenharmony_ci struct net_device *net_dev = i2400m->wimax_dev.net_dev; 3358c2ecf20Sopenharmony_ci size_t pending; 3368c2ecf20Sopenharmony_ci int rx_size; 3378c2ecf20Sopenharmony_ci struct sk_buff *rx_skb; 3388c2ecf20Sopenharmony_ci unsigned long flags; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu); 3418c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->rx_lock, flags); 3428c2ecf20Sopenharmony_ci BUG_ON(i2400mu->rx_kthread != NULL); 3438c2ecf20Sopenharmony_ci i2400mu->rx_kthread = current; 3448c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->rx_lock, flags); 3458c2ecf20Sopenharmony_ci while (1) { 3468c2ecf20Sopenharmony_ci d_printf(2, dev, "RX: waiting for messages\n"); 3478c2ecf20Sopenharmony_ci pending = 0; 3488c2ecf20Sopenharmony_ci wait_event_interruptible( 3498c2ecf20Sopenharmony_ci i2400mu->rx_wq, 3508c2ecf20Sopenharmony_ci (kthread_should_stop() /* check this first! */ 3518c2ecf20Sopenharmony_ci || (pending = atomic_read(&i2400mu->rx_pending_count))) 3528c2ecf20Sopenharmony_ci ); 3538c2ecf20Sopenharmony_ci if (kthread_should_stop()) 3548c2ecf20Sopenharmony_ci break; 3558c2ecf20Sopenharmony_ci if (pending == 0) 3568c2ecf20Sopenharmony_ci continue; 3578c2ecf20Sopenharmony_ci rx_size = i2400mu->rx_size; 3588c2ecf20Sopenharmony_ci d_printf(2, dev, "RX: reading up to %d bytes\n", rx_size); 3598c2ecf20Sopenharmony_ci rx_skb = __netdev_alloc_skb(net_dev, rx_size, GFP_KERNEL); 3608c2ecf20Sopenharmony_ci if (rx_skb == NULL) { 3618c2ecf20Sopenharmony_ci dev_err(dev, "RX: can't allocate skb [%d bytes]\n", 3628c2ecf20Sopenharmony_ci rx_size); 3638c2ecf20Sopenharmony_ci msleep(50); /* give it some time? */ 3648c2ecf20Sopenharmony_ci continue; 3658c2ecf20Sopenharmony_ci } 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci /* Receive the message with the payloads */ 3688c2ecf20Sopenharmony_ci rx_skb = i2400mu_rx(i2400mu, rx_skb); 3698c2ecf20Sopenharmony_ci result = PTR_ERR(rx_skb); 3708c2ecf20Sopenharmony_ci if (IS_ERR(rx_skb)) 3718c2ecf20Sopenharmony_ci goto out; 3728c2ecf20Sopenharmony_ci atomic_dec(&i2400mu->rx_pending_count); 3738c2ecf20Sopenharmony_ci if (rx_skb == NULL || rx_skb->len == 0) { 3748c2ecf20Sopenharmony_ci /* some "ignorable" condition */ 3758c2ecf20Sopenharmony_ci kfree_skb(rx_skb); 3768c2ecf20Sopenharmony_ci continue; 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci /* Deliver the message to the generic i2400m code */ 3808c2ecf20Sopenharmony_ci i2400mu->rx_size_cnt++; 3818c2ecf20Sopenharmony_ci i2400mu->rx_size_acc += rx_skb->len; 3828c2ecf20Sopenharmony_ci result = i2400m_rx(i2400m, rx_skb); 3838c2ecf20Sopenharmony_ci if (result == -EIO 3848c2ecf20Sopenharmony_ci && edc_inc(&i2400mu->urb_edc, 3858c2ecf20Sopenharmony_ci EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) { 3868c2ecf20Sopenharmony_ci goto error_reset; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci /* Maybe adjust RX buffer size */ 3908c2ecf20Sopenharmony_ci i2400mu_rx_size_maybe_shrink(i2400mu); 3918c2ecf20Sopenharmony_ci } 3928c2ecf20Sopenharmony_ci result = 0; 3938c2ecf20Sopenharmony_ciout: 3948c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->rx_lock, flags); 3958c2ecf20Sopenharmony_ci i2400mu->rx_kthread = NULL; 3968c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->rx_lock, flags); 3978c2ecf20Sopenharmony_ci d_fnend(4, dev, "(i2400mu %p) = %d\n", i2400mu, result); 3988c2ecf20Sopenharmony_ci return result; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cierror_reset: 4018c2ecf20Sopenharmony_ci dev_err(dev, "RX: maximum errors in received buffer exceeded; " 4028c2ecf20Sopenharmony_ci "resetting device\n"); 4038c2ecf20Sopenharmony_ci usb_queue_reset_device(i2400mu->usb_iface); 4048c2ecf20Sopenharmony_ci goto out; 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci/* 4098c2ecf20Sopenharmony_ci * Start reading from the device 4108c2ecf20Sopenharmony_ci * 4118c2ecf20Sopenharmony_ci * @i2400m: device instance 4128c2ecf20Sopenharmony_ci * 4138c2ecf20Sopenharmony_ci * Notify the RX thread that there is data pending. 4148c2ecf20Sopenharmony_ci */ 4158c2ecf20Sopenharmony_civoid i2400mu_rx_kick(struct i2400mu *i2400mu) 4168c2ecf20Sopenharmony_ci{ 4178c2ecf20Sopenharmony_ci struct i2400m *i2400m = &i2400mu->i2400m; 4188c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400mu %p)\n", i2400m); 4218c2ecf20Sopenharmony_ci atomic_inc(&i2400mu->rx_pending_count); 4228c2ecf20Sopenharmony_ci wake_up_all(&i2400mu->rx_wq); 4238c2ecf20Sopenharmony_ci d_fnend(3, dev, "(i2400m %p) = void\n", i2400m); 4248c2ecf20Sopenharmony_ci} 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ciint i2400mu_rx_setup(struct i2400mu *i2400mu) 4288c2ecf20Sopenharmony_ci{ 4298c2ecf20Sopenharmony_ci int result = 0; 4308c2ecf20Sopenharmony_ci struct i2400m *i2400m = &i2400mu->i2400m; 4318c2ecf20Sopenharmony_ci struct device *dev = &i2400mu->usb_iface->dev; 4328c2ecf20Sopenharmony_ci struct wimax_dev *wimax_dev = &i2400m->wimax_dev; 4338c2ecf20Sopenharmony_ci struct task_struct *kthread; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci kthread = kthread_run(i2400mu_rxd, i2400mu, "%s-rx", 4368c2ecf20Sopenharmony_ci wimax_dev->name); 4378c2ecf20Sopenharmony_ci /* the kthread function sets i2400mu->rx_thread */ 4388c2ecf20Sopenharmony_ci if (IS_ERR(kthread)) { 4398c2ecf20Sopenharmony_ci result = PTR_ERR(kthread); 4408c2ecf20Sopenharmony_ci dev_err(dev, "RX: cannot start thread: %d\n", result); 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci return result; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_civoid i2400mu_rx_release(struct i2400mu *i2400mu) 4478c2ecf20Sopenharmony_ci{ 4488c2ecf20Sopenharmony_ci unsigned long flags; 4498c2ecf20Sopenharmony_ci struct i2400m *i2400m = &i2400mu->i2400m; 4508c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 4518c2ecf20Sopenharmony_ci struct task_struct *kthread; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci spin_lock_irqsave(&i2400m->rx_lock, flags); 4548c2ecf20Sopenharmony_ci kthread = i2400mu->rx_kthread; 4558c2ecf20Sopenharmony_ci i2400mu->rx_kthread = NULL; 4568c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&i2400m->rx_lock, flags); 4578c2ecf20Sopenharmony_ci if (kthread) 4588c2ecf20Sopenharmony_ci kthread_stop(kthread); 4598c2ecf20Sopenharmony_ci else 4608c2ecf20Sopenharmony_ci d_printf(1, dev, "RX: kthread had already exited\n"); 4618c2ecf20Sopenharmony_ci} 4628c2ecf20Sopenharmony_ci 463