blob: 39e855a55c6384732e30c67b726e3564b98218f2 [file] [log] [blame]
David Vrabel7e6133a2008-09-17 16:34:28 +01001/*
2 * Wireless Host Controller (WHC) qset management.
3 *
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/kernel.h>
19#include <linux/dma-mapping.h>
20#include <linux/uwb/umc.h>
21#include <linux/usb.h>
22
23#include "../../wusbcore/wusbhc.h"
24
25#include "whcd.h"
26
David Vrabel7e6133a2008-09-17 16:34:28 +010027struct whc_qset *qset_alloc(struct whc *whc, gfp_t mem_flags)
28{
29 struct whc_qset *qset;
30 dma_addr_t dma;
31
32 qset = dma_pool_alloc(whc->qset_pool, mem_flags, &dma);
33 if (qset == NULL)
34 return NULL;
35 memset(qset, 0, sizeof(struct whc_qset));
36
37 qset->qset_dma = dma;
38 qset->whc = whc;
39
40 INIT_LIST_HEAD(&qset->list_node);
41 INIT_LIST_HEAD(&qset->stds);
42
43 return qset;
44}
45
46/**
47 * qset_fill_qh - fill the static endpoint state in a qset's QHead
48 * @qset: the qset whose QH needs initializing with static endpoint
49 * state
50 * @urb: an urb for a transfer to this endpoint
51 */
David Vrabelc3f22d92009-10-12 15:45:18 +000052static void qset_fill_qh(struct whc *whc, struct whc_qset *qset, struct urb *urb)
David Vrabel7e6133a2008-09-17 16:34:28 +010053{
54 struct usb_device *usb_dev = urb->dev;
David Vrabelc3f22d92009-10-12 15:45:18 +000055 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
David Vrabel7e6133a2008-09-17 16:34:28 +010056 struct usb_wireless_ep_comp_descriptor *epcd;
57 bool is_out;
David Vrabelc3f22d92009-10-12 15:45:18 +000058 uint8_t phy_rate;
David Vrabel7e6133a2008-09-17 16:34:28 +010059
60 is_out = usb_pipeout(urb->pipe);
61
David Vrabel294a39e2009-08-24 15:02:27 +010062 qset->max_packet = le16_to_cpu(urb->ep->desc.wMaxPacketSize);
David Vrabel7e6133a2008-09-17 16:34:28 +010063
David Vrabel294a39e2009-08-24 15:02:27 +010064 epcd = (struct usb_wireless_ep_comp_descriptor *)qset->ep->extra;
David Vrabel7e6133a2008-09-17 16:34:28 +010065 if (epcd) {
66 qset->max_seq = epcd->bMaxSequence;
67 qset->max_burst = epcd->bMaxBurst;
68 } else {
69 qset->max_seq = 2;
70 qset->max_burst = 1;
71 }
72
David Vrabelc3f22d92009-10-12 15:45:18 +000073 /*
74 * Initial PHY rate is 53.3 Mbit/s for control endpoints or
75 * the maximum supported by the device for other endpoints
76 * (unless limited by the user).
77 */
78 if (usb_pipecontrol(urb->pipe))
79 phy_rate = UWB_PHY_RATE_53;
80 else {
81 uint16_t phy_rates;
82
83 phy_rates = le16_to_cpu(wusb_dev->wusb_cap_descr->wPHYRates);
84 phy_rate = fls(phy_rates) - 1;
85 if (phy_rate > whc->wusbhc.phy_rate)
86 phy_rate = whc->wusbhc.phy_rate;
87 }
88
David Vrabel7e6133a2008-09-17 16:34:28 +010089 qset->qh.info1 = cpu_to_le32(
90 QH_INFO1_EP(usb_pipeendpoint(urb->pipe))
91 | (is_out ? QH_INFO1_DIR_OUT : QH_INFO1_DIR_IN)
92 | usb_pipe_to_qh_type(urb->pipe)
93 | QH_INFO1_DEV_INFO_IDX(wusb_port_no_to_idx(usb_dev->portnum))
David Vrabel294a39e2009-08-24 15:02:27 +010094 | QH_INFO1_MAX_PKT_LEN(qset->max_packet)
David Vrabel7e6133a2008-09-17 16:34:28 +010095 );
96 qset->qh.info2 = cpu_to_le32(
97 QH_INFO2_BURST(qset->max_burst)
98 | QH_INFO2_DBP(0)
99 | QH_INFO2_MAX_COUNT(3)
100 | QH_INFO2_MAX_RETRY(3)
101 | QH_INFO2_MAX_SEQ(qset->max_seq - 1)
102 );
103 /* FIXME: where can we obtain these Tx parameters from? Why
104 * doesn't the chip know what Tx power to use? It knows the Rx
105 * strength and can presumably guess the Tx power required
106 * from that? */
107 qset->qh.info3 = cpu_to_le32(
David Vrabelc3f22d92009-10-12 15:45:18 +0000108 QH_INFO3_TX_RATE(phy_rate)
David Vrabel7e6133a2008-09-17 16:34:28 +0100109 | QH_INFO3_TX_PWR(0) /* 0 == max power */
110 );
David Vrabel7f0406d2009-04-08 17:36:29 +0000111
112 qset->qh.cur_window = cpu_to_le32((1 << qset->max_burst) - 1);
David Vrabel7e6133a2008-09-17 16:34:28 +0100113}
114
115/**
116 * qset_clear - clear fields in a qset so it may be reinserted into a
David Vrabel7f0406d2009-04-08 17:36:29 +0000117 * schedule.
118 *
119 * The sequence number and current window are not cleared (see
120 * qset_reset()).
David Vrabel7e6133a2008-09-17 16:34:28 +0100121 */
122void qset_clear(struct whc *whc, struct whc_qset *qset)
123{
124 qset->td_start = qset->td_end = qset->ntds = 0;
David Vrabel7e6133a2008-09-17 16:34:28 +0100125
126 qset->qh.link = cpu_to_le32(QH_LINK_NTDS(8) | QH_LINK_T);
David Vrabel7f0406d2009-04-08 17:36:29 +0000127 qset->qh.status = qset->qh.status & QH_STATUS_SEQ_MASK;
David Vrabel7e6133a2008-09-17 16:34:28 +0100128 qset->qh.err_count = 0;
David Vrabel7e6133a2008-09-17 16:34:28 +0100129 qset->qh.scratch[0] = 0;
130 qset->qh.scratch[1] = 0;
131 qset->qh.scratch[2] = 0;
132
133 memset(&qset->qh.overlay, 0, sizeof(qset->qh.overlay));
134
135 init_completion(&qset->remove_complete);
136}
137
138/**
David Vrabel7f0406d2009-04-08 17:36:29 +0000139 * qset_reset - reset endpoint state in a qset.
140 *
141 * Clears the sequence number and current window. This qset must not
142 * be in the ASL or PZL.
143 */
144void qset_reset(struct whc *whc, struct whc_qset *qset)
145{
David Vrabel831baa42009-06-24 18:26:40 +0100146 qset->reset = 0;
David Vrabel7f0406d2009-04-08 17:36:29 +0000147
148 qset->qh.status &= ~QH_STATUS_SEQ_MASK;
149 qset->qh.cur_window = cpu_to_le32((1 << qset->max_burst) - 1);
150}
151
152/**
David Vrabel7e6133a2008-09-17 16:34:28 +0100153 * get_qset - get the qset for an async endpoint
154 *
155 * A new qset is created if one does not already exist.
156 */
157struct whc_qset *get_qset(struct whc *whc, struct urb *urb,
158 gfp_t mem_flags)
159{
160 struct whc_qset *qset;
161
162 qset = urb->ep->hcpriv;
163 if (qset == NULL) {
164 qset = qset_alloc(whc, mem_flags);
165 if (qset == NULL)
166 return NULL;
167
168 qset->ep = urb->ep;
169 urb->ep->hcpriv = qset;
David Vrabelc3f22d92009-10-12 15:45:18 +0000170 qset_fill_qh(whc, qset, urb);
David Vrabel7e6133a2008-09-17 16:34:28 +0100171 }
172 return qset;
173}
174
175void qset_remove_complete(struct whc *whc, struct whc_qset *qset)
176{
David Vrabel831baa42009-06-24 18:26:40 +0100177 qset->remove = 0;
David Vrabel7e6133a2008-09-17 16:34:28 +0100178 list_del_init(&qset->list_node);
179 complete(&qset->remove_complete);
180}
181
182/**
183 * qset_add_qtds - add qTDs for an URB to a qset
184 *
185 * Returns true if the list (ASL/PZL) must be updated because (for a
186 * WHCI 0.95 controller) an activated qTD was pointed to be iCur.
187 */
188enum whc_update qset_add_qtds(struct whc *whc, struct whc_qset *qset)
189{
190 struct whc_std *std;
191 enum whc_update update = 0;
192
193 list_for_each_entry(std, &qset->stds, list_node) {
194 struct whc_qtd *qtd;
195 uint32_t status;
196
197 if (qset->ntds >= WHCI_QSET_TD_MAX
198 || (qset->pause_after_urb && std->urb != qset->pause_after_urb))
199 break;
200
201 if (std->qtd)
202 continue; /* already has a qTD */
203
204 qtd = std->qtd = &qset->qtd[qset->td_end];
205
206 /* Fill in setup bytes for control transfers. */
207 if (usb_pipecontrol(std->urb->pipe))
208 memcpy(qtd->setup, std->urb->setup_packet, 8);
209
210 status = QTD_STS_ACTIVE | QTD_STS_LEN(std->len);
211
212 if (whc_std_last(std) && usb_pipeout(std->urb->pipe))
213 status |= QTD_STS_LAST_PKT;
214
215 /*
216 * For an IN transfer the iAlt field should be set so
217 * the h/w will automatically advance to the next
218 * transfer. However, if there are 8 or more TDs
219 * remaining in this transfer then iAlt cannot be set
220 * as it could point to somewhere in this transfer.
221 */
222 if (std->ntds_remaining < WHCI_QSET_TD_MAX) {
223 int ialt;
224 ialt = (qset->td_end + std->ntds_remaining) % WHCI_QSET_TD_MAX;
225 status |= QTD_STS_IALT(ialt);
226 } else if (usb_pipein(std->urb->pipe))
227 qset->pause_after_urb = std->urb;
228
229 if (std->num_pointers)
230 qtd->options = cpu_to_le32(QTD_OPT_IOC);
231 else
232 qtd->options = cpu_to_le32(QTD_OPT_IOC | QTD_OPT_SMALL);
233 qtd->page_list_ptr = cpu_to_le64(std->dma_addr);
234
235 qtd->status = cpu_to_le32(status);
236
237 if (QH_STATUS_TO_ICUR(qset->qh.status) == qset->td_end)
238 update = WHC_UPDATE_UPDATED;
239
240 if (++qset->td_end >= WHCI_QSET_TD_MAX)
241 qset->td_end = 0;
242 qset->ntds++;
243 }
244
245 return update;
246}
247
248/**
249 * qset_remove_qtd - remove the first qTD from a qset.
250 *
251 * The qTD might be still active (if it's part of a IN URB that
252 * resulted in a short read) so ensure it's deactivated.
253 */
254static void qset_remove_qtd(struct whc *whc, struct whc_qset *qset)
255{
256 qset->qtd[qset->td_start].status = 0;
257
258 if (++qset->td_start >= WHCI_QSET_TD_MAX)
259 qset->td_start = 0;
260 qset->ntds--;
261}
262
David Vrabel294a39e2009-08-24 15:02:27 +0100263static void qset_copy_bounce_to_sg(struct whc *whc, struct whc_std *std)
264{
265 struct scatterlist *sg;
266 void *bounce;
267 size_t remaining, offset;
268
269 bounce = std->bounce_buf;
270 remaining = std->len;
271
272 sg = std->bounce_sg;
273 offset = std->bounce_offset;
274
275 while (remaining) {
276 size_t len;
277
278 len = min(sg->length - offset, remaining);
279 memcpy(sg_virt(sg) + offset, bounce, len);
280
281 bounce += len;
282 remaining -= len;
283
284 offset += len;
285 if (offset >= sg->length) {
286 sg = sg_next(sg);
287 offset = 0;
288 }
289 }
290
291}
292
David Vrabel7e6133a2008-09-17 16:34:28 +0100293/**
294 * qset_free_std - remove an sTD and free it.
295 * @whc: the WHCI host controller
296 * @std: the sTD to remove and free.
297 */
298void qset_free_std(struct whc *whc, struct whc_std *std)
299{
300 list_del(&std->list_node);
David Vrabel294a39e2009-08-24 15:02:27 +0100301 if (std->bounce_buf) {
302 bool is_out = usb_pipeout(std->urb->pipe);
303 dma_addr_t dma_addr;
David Vrabel7e6133a2008-09-17 16:34:28 +0100304
David Vrabel294a39e2009-08-24 15:02:27 +0100305 if (std->num_pointers)
306 dma_addr = le64_to_cpu(std->pl_virt[0].buf_ptr);
307 else
308 dma_addr = std->dma_addr;
309
310 dma_unmap_single(whc->wusbhc.dev, dma_addr,
311 std->len, is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
312 if (!is_out)
313 qset_copy_bounce_to_sg(whc, std);
314 kfree(std->bounce_buf);
315 }
316 if (std->pl_virt) {
317 if (std->dma_addr)
318 dma_unmap_single(whc->wusbhc.dev, std->dma_addr,
319 std->num_pointers * sizeof(struct whc_page_list_entry),
320 DMA_TO_DEVICE);
321 kfree(std->pl_virt);
322 std->pl_virt = NULL;
323 }
David Vrabel7e6133a2008-09-17 16:34:28 +0100324 kfree(std);
325}
326
327/**
328 * qset_remove_qtds - remove an URB's qTDs (and sTDs).
329 */
330static void qset_remove_qtds(struct whc *whc, struct whc_qset *qset,
331 struct urb *urb)
332{
333 struct whc_std *std, *t;
334
335 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
336 if (std->urb != urb)
337 break;
338 if (std->qtd != NULL)
339 qset_remove_qtd(whc, qset);
340 qset_free_std(whc, std);
341 }
342}
343
344/**
345 * qset_free_stds - free any remaining sTDs for an URB.
346 */
347static void qset_free_stds(struct whc_qset *qset, struct urb *urb)
348{
349 struct whc_std *std, *t;
350
351 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
352 if (std->urb == urb)
353 qset_free_std(qset->whc, std);
354 }
355}
356
357static int qset_fill_page_list(struct whc *whc, struct whc_std *std, gfp_t mem_flags)
358{
359 dma_addr_t dma_addr = std->dma_addr;
360 dma_addr_t sp, ep;
David Vrabel7e6133a2008-09-17 16:34:28 +0100361 size_t pl_len;
362 int p;
363
David Vrabel294a39e2009-08-24 15:02:27 +0100364 /* Short buffers don't need a page list. */
365 if (std->len <= WHCI_PAGE_SIZE) {
366 std->num_pointers = 0;
367 return 0;
368 }
369
370 sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
371 ep = dma_addr + std->len;
David Vrabel7e6133a2008-09-17 16:34:28 +0100372 std->num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
373
374 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
375 std->pl_virt = kmalloc(pl_len, mem_flags);
376 if (std->pl_virt == NULL)
377 return -ENOMEM;
378 std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt, pl_len, DMA_TO_DEVICE);
379
380 for (p = 0; p < std->num_pointers; p++) {
381 std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
David Vrabel294a39e2009-08-24 15:02:27 +0100382 dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
David Vrabel7e6133a2008-09-17 16:34:28 +0100383 }
384
385 return 0;
386}
387
388/**
389 * urb_dequeue_work - executes asl/pzl update and gives back the urb to the system.
390 */
391static void urb_dequeue_work(struct work_struct *work)
392{
393 struct whc_urb *wurb = container_of(work, struct whc_urb, dequeue_work);
394 struct whc_qset *qset = wurb->qset;
395 struct whc *whc = qset->whc;
396 unsigned long flags;
397
398 if (wurb->is_async == true)
399 asl_update(whc, WUSBCMD_ASYNC_UPDATED
400 | WUSBCMD_ASYNC_SYNCED_DB
401 | WUSBCMD_ASYNC_QSET_RM);
402 else
403 pzl_update(whc, WUSBCMD_PERIODIC_UPDATED
404 | WUSBCMD_PERIODIC_SYNCED_DB
405 | WUSBCMD_PERIODIC_QSET_RM);
406
407 spin_lock_irqsave(&whc->lock, flags);
408 qset_remove_urb(whc, qset, wurb->urb, wurb->status);
409 spin_unlock_irqrestore(&whc->lock, flags);
410}
411
David Vrabel294a39e2009-08-24 15:02:27 +0100412static struct whc_std *qset_new_std(struct whc *whc, struct whc_qset *qset,
413 struct urb *urb, gfp_t mem_flags)
414{
415 struct whc_std *std;
416
417 std = kzalloc(sizeof(struct whc_std), mem_flags);
418 if (std == NULL)
419 return NULL;
420
421 std->urb = urb;
422 std->qtd = NULL;
423
424 INIT_LIST_HEAD(&std->list_node);
425 list_add_tail(&std->list_node, &qset->stds);
426
427 return std;
428}
429
430static int qset_add_urb_sg(struct whc *whc, struct whc_qset *qset, struct urb *urb,
431 gfp_t mem_flags)
432{
433 size_t remaining;
434 struct scatterlist *sg;
435 int i;
436 int ntds = 0;
437 struct whc_std *std = NULL;
438 struct whc_page_list_entry *entry;
439 dma_addr_t prev_end = 0;
440 size_t pl_len;
441 int p = 0;
442
David Vrabel294a39e2009-08-24 15:02:27 +0100443 remaining = urb->transfer_buffer_length;
444
445 for_each_sg(urb->sg->sg, sg, urb->num_sgs, i) {
446 dma_addr_t dma_addr;
447 size_t dma_remaining;
448 dma_addr_t sp, ep;
449 int num_pointers;
450
451 if (remaining == 0) {
452 break;
453 }
454
455 dma_addr = sg_dma_address(sg);
David Vrabelf0ad0732009-10-14 13:23:55 +0000456 dma_remaining = min_t(size_t, sg_dma_len(sg), remaining);
David Vrabel294a39e2009-08-24 15:02:27 +0100457
458 while (dma_remaining) {
459 size_t dma_len;
460
461 /*
462 * We can use the previous std (if it exists) provided that:
463 * - the previous one ended on a page boundary.
464 * - the current one begins on a page boundary.
465 * - the previous one isn't full.
466 *
467 * If a new std is needed but the previous one
468 * did not end on a wMaxPacketSize boundary
469 * then this sg list cannot be mapped onto
470 * multiple qTDs. Return an error and let the
471 * caller sort it out.
472 */
473 if (!std
474 || (prev_end & (WHCI_PAGE_SIZE-1))
475 || (dma_addr & (WHCI_PAGE_SIZE-1))
476 || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) {
477 if (prev_end % qset->max_packet != 0)
478 return -EINVAL;
David Vrabel294a39e2009-08-24 15:02:27 +0100479 std = qset_new_std(whc, qset, urb, mem_flags);
480 if (std == NULL) {
481 return -ENOMEM;
482 }
483 ntds++;
484 p = 0;
485 }
486
487 dma_len = dma_remaining;
488
489 /*
490 * If the remainder in this element doesn't
491 * fit in a single qTD, end the qTD on a
492 * wMaxPacketSize boundary.
493 */
494 if (std->len + dma_len > QTD_MAX_XFER_SIZE) {
495 dma_len = QTD_MAX_XFER_SIZE - std->len;
496 ep = ((dma_addr + dma_len) / qset->max_packet) * qset->max_packet;
497 dma_len = ep - dma_addr;
498 }
499
David Vrabel294a39e2009-08-24 15:02:27 +0100500 std->len += dma_len;
501 std->ntds_remaining = -1; /* filled in later */
502
503 sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
504 ep = dma_addr + dma_len;
505 num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
506 std->num_pointers += num_pointers;
507
David Vrabel294a39e2009-08-24 15:02:27 +0100508 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
509
510 std->pl_virt = krealloc(std->pl_virt, pl_len, mem_flags);
511 if (std->pl_virt == NULL) {
512 return -ENOMEM;
513 }
514
515 for (;p < std->num_pointers; p++, entry++) {
David Vrabel294a39e2009-08-24 15:02:27 +0100516 std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
517 dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
518 }
519
520 prev_end = dma_addr = ep;
521 dma_remaining -= dma_len;
522 remaining -= dma_len;
523 }
524 }
525
David Vrabel294a39e2009-08-24 15:02:27 +0100526 /* Now the number of stds is know, go back and fill in
527 std->ntds_remaining. */
528 list_for_each_entry(std, &qset->stds, list_node) {
529 if (std->ntds_remaining == -1) {
530 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
531 std->ntds_remaining = ntds--;
532 std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt,
533 pl_len, DMA_TO_DEVICE);
534 }
535 }
536 return 0;
537}
538
539/**
540 * qset_add_urb_sg_linearize - add an urb with sg list, copying the data
541 *
542 * If the URB contains an sg list whose elements cannot be directly
543 * mapped to qTDs then the data must be transferred via bounce
544 * buffers.
545 */
546static int qset_add_urb_sg_linearize(struct whc *whc, struct whc_qset *qset,
547 struct urb *urb, gfp_t mem_flags)
548{
549 bool is_out = usb_pipeout(urb->pipe);
550 size_t max_std_len;
551 size_t remaining;
552 int ntds = 0;
553 struct whc_std *std = NULL;
554 void *bounce = NULL;
555 struct scatterlist *sg;
556 int i;
557
558 /* limit maximum bounce buffer to 16 * 3.5 KiB ~= 28 k */
559 max_std_len = qset->max_burst * qset->max_packet;
560
561 remaining = urb->transfer_buffer_length;
562
563 for_each_sg(urb->sg->sg, sg, urb->sg->nents, i) {
564 size_t len;
565 size_t sg_remaining;
566 void *orig;
567
568 if (remaining == 0) {
569 break;
570 }
571
David Vrabelf0ad0732009-10-14 13:23:55 +0000572 sg_remaining = min_t(size_t, remaining, sg->length);
David Vrabel294a39e2009-08-24 15:02:27 +0100573 orig = sg_virt(sg);
574
David Vrabel294a39e2009-08-24 15:02:27 +0100575 while (sg_remaining) {
576 if (!std || std->len == max_std_len) {
David Vrabel294a39e2009-08-24 15:02:27 +0100577 std = qset_new_std(whc, qset, urb, mem_flags);
578 if (std == NULL)
579 return -ENOMEM;
580 std->bounce_buf = kmalloc(max_std_len, mem_flags);
581 if (std->bounce_buf == NULL)
582 return -ENOMEM;
583 std->bounce_sg = sg;
584 std->bounce_offset = orig - sg_virt(sg);
585 bounce = std->bounce_buf;
586 ntds++;
587 }
588
589 len = min(sg_remaining, max_std_len - std->len);
590
David Vrabel294a39e2009-08-24 15:02:27 +0100591 if (is_out)
592 memcpy(bounce, orig, len);
593
594 std->len += len;
595 std->ntds_remaining = -1; /* filled in later */
596
597 bounce += len;
598 orig += len;
599 sg_remaining -= len;
600 remaining -= len;
601 }
602 }
603
604 /*
605 * For each of the new sTDs, map the bounce buffers, create
606 * page lists (if necessary), and fill in std->ntds_remaining.
607 */
608 list_for_each_entry(std, &qset->stds, list_node) {
609 if (std->ntds_remaining != -1)
610 continue;
611
612 std->dma_addr = dma_map_single(&whc->umc->dev, std->bounce_buf, std->len,
613 is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
614
615 if (qset_fill_page_list(whc, std, mem_flags) < 0)
616 return -ENOMEM;
617
618 std->ntds_remaining = ntds--;
619 }
620
621 return 0;
622}
623
David Vrabel7e6133a2008-09-17 16:34:28 +0100624/**
625 * qset_add_urb - add an urb to the qset's queue.
626 *
627 * The URB is chopped into sTDs, one for each qTD that will required.
628 * At least one qTD (and sTD) is required even if the transfer has no
629 * data (e.g., for some control transfers).
630 */
631int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
632 gfp_t mem_flags)
633{
634 struct whc_urb *wurb;
635 int remaining = urb->transfer_buffer_length;
636 u64 transfer_dma = urb->transfer_dma;
637 int ntds_remaining;
David Vrabel294a39e2009-08-24 15:02:27 +0100638 int ret;
David Vrabel7e6133a2008-09-17 16:34:28 +0100639
640 wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
641 if (wurb == NULL)
642 goto err_no_mem;
643 urb->hcpriv = wurb;
644 wurb->qset = qset;
645 wurb->urb = urb;
646 INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
647
David Vrabel294a39e2009-08-24 15:02:27 +0100648 if (urb->sg) {
649 ret = qset_add_urb_sg(whc, qset, urb, mem_flags);
650 if (ret == -EINVAL) {
David Vrabel294a39e2009-08-24 15:02:27 +0100651 qset_free_stds(qset, urb);
652 ret = qset_add_urb_sg_linearize(whc, qset, urb, mem_flags);
653 }
654 if (ret < 0)
655 goto err_no_mem;
656 return 0;
657 }
658
659 ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
660 if (ntds_remaining == 0)
661 ntds_remaining = 1;
662
David Vrabel7e6133a2008-09-17 16:34:28 +0100663 while (ntds_remaining) {
664 struct whc_std *std;
665 size_t std_len;
666
David Vrabel7e6133a2008-09-17 16:34:28 +0100667 std_len = remaining;
668 if (std_len > QTD_MAX_XFER_SIZE)
669 std_len = QTD_MAX_XFER_SIZE;
670
David Vrabel294a39e2009-08-24 15:02:27 +0100671 std = qset_new_std(whc, qset, urb, mem_flags);
672 if (std == NULL)
673 goto err_no_mem;
674
David Vrabel7e6133a2008-09-17 16:34:28 +0100675 std->dma_addr = transfer_dma;
676 std->len = std_len;
677 std->ntds_remaining = ntds_remaining;
David Vrabel7e6133a2008-09-17 16:34:28 +0100678
David Vrabel294a39e2009-08-24 15:02:27 +0100679 if (qset_fill_page_list(whc, std, mem_flags) < 0)
680 goto err_no_mem;
David Vrabel7e6133a2008-09-17 16:34:28 +0100681
682 ntds_remaining--;
683 remaining -= std_len;
684 transfer_dma += std_len;
685 }
686
687 return 0;
688
689err_no_mem:
690 qset_free_stds(qset, urb);
691 return -ENOMEM;
692}
693
694/**
695 * qset_remove_urb - remove an URB from the urb queue.
696 *
697 * The URB is returned to the USB subsystem.
698 */
699void qset_remove_urb(struct whc *whc, struct whc_qset *qset,
700 struct urb *urb, int status)
701{
702 struct wusbhc *wusbhc = &whc->wusbhc;
703 struct whc_urb *wurb = urb->hcpriv;
704
705 usb_hcd_unlink_urb_from_ep(&wusbhc->usb_hcd, urb);
706 /* Drop the lock as urb->complete() may enqueue another urb. */
707 spin_unlock(&whc->lock);
708 wusbhc_giveback_urb(wusbhc, urb, status);
709 spin_lock(&whc->lock);
710
711 kfree(wurb);
712}
713
714/**
715 * get_urb_status_from_qtd - get the completed urb status from qTD status
716 * @urb: completed urb
717 * @status: qTD status
718 */
719static int get_urb_status_from_qtd(struct urb *urb, u32 status)
720{
721 if (status & QTD_STS_HALTED) {
722 if (status & QTD_STS_DBE)
723 return usb_pipein(urb->pipe) ? -ENOSR : -ECOMM;
724 else if (status & QTD_STS_BABBLE)
725 return -EOVERFLOW;
726 else if (status & QTD_STS_RCE)
727 return -ETIME;
728 return -EPIPE;
729 }
730 if (usb_pipein(urb->pipe)
731 && (urb->transfer_flags & URB_SHORT_NOT_OK)
732 && urb->actual_length < urb->transfer_buffer_length)
733 return -EREMOTEIO;
734 return 0;
735}
736
737/**
738 * process_inactive_qtd - process an inactive (but not halted) qTD.
739 *
740 * Update the urb with the transfer bytes from the qTD, if the urb is
741 * completely transfered or (in the case of an IN only) the LPF is
742 * set, then the transfer is complete and the urb should be returned
743 * to the system.
744 */
745void process_inactive_qtd(struct whc *whc, struct whc_qset *qset,
746 struct whc_qtd *qtd)
747{
748 struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
749 struct urb *urb = std->urb;
750 uint32_t status;
751 bool complete;
752
753 status = le32_to_cpu(qtd->status);
754
755 urb->actual_length += std->len - QTD_STS_TO_LEN(status);
756
757 if (usb_pipein(urb->pipe) && (status & QTD_STS_LAST_PKT))
758 complete = true;
759 else
760 complete = whc_std_last(std);
761
762 qset_remove_qtd(whc, qset);
763 qset_free_std(whc, std);
764
765 /*
766 * Transfers for this URB are complete? Then return it to the
767 * USB subsystem.
768 */
769 if (complete) {
770 qset_remove_qtds(whc, qset, urb);
771 qset_remove_urb(whc, qset, urb, get_urb_status_from_qtd(urb, status));
772
773 /*
774 * If iAlt isn't valid then the hardware didn't
775 * advance iCur. Adjust the start and end pointers to
776 * match iCur.
777 */
778 if (!(status & QTD_STS_IALT_VALID))
779 qset->td_start = qset->td_end
780 = QH_STATUS_TO_ICUR(le16_to_cpu(qset->qh.status));
781 qset->pause_after_urb = NULL;
782 }
783}
784
785/**
786 * process_halted_qtd - process a qset with a halted qtd
787 *
788 * Remove all the qTDs for the failed URB and return the failed URB to
789 * the USB subsystem. Then remove all other qTDs so the qset can be
790 * removed.
791 *
792 * FIXME: this is the point where rate adaptation can be done. If a
793 * transfer failed because it exceeded the maximum number of retries
794 * then it could be reactivated with a slower rate without having to
795 * remove the qset.
796 */
797void process_halted_qtd(struct whc *whc, struct whc_qset *qset,
798 struct whc_qtd *qtd)
799{
800 struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
801 struct urb *urb = std->urb;
802 int urb_status;
803
804 urb_status = get_urb_status_from_qtd(urb, le32_to_cpu(qtd->status));
805
806 qset_remove_qtds(whc, qset, urb);
807 qset_remove_urb(whc, qset, urb, urb_status);
808
809 list_for_each_entry(std, &qset->stds, list_node) {
810 if (qset->ntds == 0)
811 break;
812 qset_remove_qtd(whc, qset);
813 std->qtd = NULL;
814 }
815
816 qset->remove = 1;
817}
818
819void qset_free(struct whc *whc, struct whc_qset *qset)
820{
821 dma_pool_free(whc->qset_pool, qset, qset->qset_dma);
822}
823
824/**
825 * qset_delete - wait for a qset to be unused, then free it.
826 */
827void qset_delete(struct whc *whc, struct whc_qset *qset)
828{
829 wait_for_completion(&qset->remove_complete);
830 qset_free(whc, qset);
831}