blob: a3c46e99c8040dffe0a2fcc40a8f4308f24d7763 [file] [log] [blame]
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -08001/*
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 }
Dirk Brandewie20935862009-08-12 11:29:46 -0700104 epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_out);
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800105 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;
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900164 unsigned long flags;
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800165
166 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
167
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900168 spin_lock_irqsave(&i2400m->tx_lock, flags);
169 BUG_ON(i2400mu->tx_kthread != NULL);
170 i2400mu->tx_kthread = current;
171 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
172
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800173 while (1) {
174 d_printf(2, dev, "TX: waiting for messages\n");
175 tx_msg = NULL;
176 wait_event_interruptible(
177 i2400mu->tx_wq,
178 (kthread_should_stop() /* check this first! */
179 || (tx_msg = i2400m_tx_msg_get(i2400m, &tx_msg_size)))
180 );
181 if (kthread_should_stop())
182 break;
183 WARN_ON(tx_msg == NULL); /* should not happen...*/
184 d_printf(2, dev, "TX: submitting %zu bytes\n", tx_msg_size);
185 d_dump(5, dev, tx_msg, tx_msg_size);
186 /* Yeah, we ignore errors ... not much we can do */
187 i2400mu_tx(i2400mu, tx_msg, tx_msg_size);
188 i2400m_tx_msg_sent(i2400m); /* ack it, advance the FIFO */
189 if (result < 0)
190 break;
191 }
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900192
193 spin_lock_irqsave(&i2400m->tx_lock, flags);
194 i2400mu->tx_kthread = NULL;
195 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
196
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800197 d_fnend(4, dev, "(i2400mu %p) = %d\n", i2400mu, result);
198 return result;
199}
200
201
202/*
203 * i2400m TX engine notifies us that there is data in the FIFO ready
204 * for TX
205 *
206 * If there is a URB in flight, don't do anything; when it finishes,
207 * it will see there is data in the FIFO and send it. Else, just
208 * submit a write.
209 */
210void i2400mu_bus_tx_kick(struct i2400m *i2400m)
211{
212 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
213 struct device *dev = &i2400mu->usb_iface->dev;
214
215 d_fnstart(3, dev, "(i2400m %p) = void\n", i2400m);
216 wake_up_all(&i2400mu->tx_wq);
217 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
218}
219
220
221int i2400mu_tx_setup(struct i2400mu *i2400mu)
222{
223 int result = 0;
224 struct i2400m *i2400m = &i2400mu->i2400m;
225 struct device *dev = &i2400mu->usb_iface->dev;
226 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900227 struct task_struct *kthread;
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800228
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900229 kthread = kthread_run(i2400mu_txd, i2400mu, "%s-tx",
230 wimax_dev->name);
231 /* the kthread function sets i2400mu->tx_thread */
232 if (IS_ERR(kthread)) {
233 result = PTR_ERR(kthread);
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800234 dev_err(dev, "TX: cannot start thread: %d\n", result);
235 }
236 return result;
237}
238
239void i2400mu_tx_release(struct i2400mu *i2400mu)
240{
Inaky Perez-Gonzalez4a78fd92009-10-08 08:11:38 +0900241 unsigned long flags;
242 struct i2400m *i2400m = &i2400mu->i2400m;
243 struct device *dev = i2400m_dev(i2400m);
244 struct task_struct *kthread;
245
246 spin_lock_irqsave(&i2400m->tx_lock, flags);
247 kthread = i2400mu->tx_kthread;
248 i2400mu->tx_kthread = NULL;
249 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
250 if (kthread)
251 kthread_stop(kthread);
252 else
253 d_printf(1, dev, "TX: kthread had already exited\n");
Inaky Perez-Gonzaleza8ebf982008-12-20 16:57:53 -0800254}