1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include "i2400m-usb.h"
72
73
74#define D_SUBMODULE tx
75#include "usb-debug-levels.h"
76
77
78
79
80
81
82
83
84
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->endpoint_cfg.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, 200);
109 usb_mark_last_busy(i2400mu->usb_dev);
110 switch (result) {
111 case 0:
112 if (sent_size != tx_msg_size) {
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 -EPIPE:
119
120
121
122
123
124
125
126
127
128
129 if (edc_inc(&i2400mu->urb_edc,
130 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
131 dev_err(dev, "BM-CMD: too many stalls in "
132 "URB; resetting device\n");
133 usb_queue_reset_device(i2400mu->usb_iface);
134
135 } else {
136 usb_clear_halt(i2400mu->usb_dev, usb_pipe);
137 msleep(10);
138 goto retry;
139 }
140 case -EINVAL:
141 case -ENODEV:
142 case -ENOENT:
143 case -ESHUTDOWN:
144 case -ECONNRESET:
145 result = -ESHUTDOWN;
146 break;
147 default:
148 if (edc_inc(&i2400mu->urb_edc,
149 EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
150 dev_err(dev, "TX: maximum errors in URB "
151 "exceeded; resetting device\n");
152 usb_queue_reset_device(i2400mu->usb_iface);
153 } else {
154 dev_err(dev, "TX: cannot send URB; retrying. "
155 "tx_msg @%zu %zu B [%d sent]: %d\n",
156 (void *) tx_msg - i2400m->tx_buf,
157 tx_msg_size, sent_size, result);
158 goto retry;
159 }
160 }
161 if (do_autopm)
162 usb_autopm_put_interface(i2400mu->usb_iface);
163 d_fnend(4, dev, "(i2400mu %p) = result\n", i2400mu);
164 return result;
165}
166
167
168
169
170
171
172
173
174
175
176
177static
178int i2400mu_txd(void *_i2400mu)
179{
180 struct i2400mu *i2400mu = _i2400mu;
181 struct i2400m *i2400m = &i2400mu->i2400m;
182 struct device *dev = &i2400mu->usb_iface->dev;
183 struct i2400m_msg_hdr *tx_msg;
184 size_t tx_msg_size;
185 unsigned long flags;
186
187 d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
188
189 spin_lock_irqsave(&i2400m->tx_lock, flags);
190 BUG_ON(i2400mu->tx_kthread != NULL);
191 i2400mu->tx_kthread = current;
192 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
193
194 while (1) {
195 d_printf(2, dev, "TX: waiting for messages\n");
196 tx_msg = NULL;
197 wait_event_interruptible(
198 i2400mu->tx_wq,
199 (kthread_should_stop()
200 || (tx_msg = i2400m_tx_msg_get(i2400m, &tx_msg_size)))
201 );
202 if (kthread_should_stop())
203 break;
204 WARN_ON(tx_msg == NULL);
205 d_printf(2, dev, "TX: submitting %zu bytes\n", tx_msg_size);
206 d_dump(5, dev, tx_msg, tx_msg_size);
207
208 i2400mu_tx(i2400mu, tx_msg, tx_msg_size);
209 i2400m_tx_msg_sent(i2400m);
210 }
211
212 spin_lock_irqsave(&i2400m->tx_lock, flags);
213 i2400mu->tx_kthread = NULL;
214 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
215
216 d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
217 return 0;
218}
219
220
221
222
223
224
225
226
227
228
229void i2400mu_bus_tx_kick(struct i2400m *i2400m)
230{
231 struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
232 struct device *dev = &i2400mu->usb_iface->dev;
233
234 d_fnstart(3, dev, "(i2400m %p) = void\n", i2400m);
235 wake_up_all(&i2400mu->tx_wq);
236 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
237}
238
239
240int i2400mu_tx_setup(struct i2400mu *i2400mu)
241{
242 int result = 0;
243 struct i2400m *i2400m = &i2400mu->i2400m;
244 struct device *dev = &i2400mu->usb_iface->dev;
245 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
246 struct task_struct *kthread;
247
248 kthread = kthread_run(i2400mu_txd, i2400mu, "%s-tx",
249 wimax_dev->name);
250
251 if (IS_ERR(kthread)) {
252 result = PTR_ERR(kthread);
253 dev_err(dev, "TX: cannot start thread: %d\n", result);
254 }
255 return result;
256}
257
258void i2400mu_tx_release(struct i2400mu *i2400mu)
259{
260 unsigned long flags;
261 struct i2400m *i2400m = &i2400mu->i2400m;
262 struct device *dev = i2400m_dev(i2400m);
263 struct task_struct *kthread;
264
265 spin_lock_irqsave(&i2400m->tx_lock, flags);
266 kthread = i2400mu->tx_kthread;
267 i2400mu->tx_kthread = NULL;
268 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
269 if (kthread)
270 kthread_stop(kthread);
271 else
272 d_printf(1, dev, "TX: kthread had already exited\n");
273}
274