qset.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  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/slab.h>
  21. #include <linux/uwb/umc.h>
  22. #include <linux/usb.h>
  23. #include "../../wusbcore/wusbhc.h"
  24. #include "whcd.h"
  25. struct whc_qset *qset_alloc(struct whc *whc, gfp_t mem_flags)
  26. {
  27. struct whc_qset *qset;
  28. dma_addr_t dma;
  29. qset = dma_pool_zalloc(whc->qset_pool, mem_flags, &dma);
  30. if (qset == NULL)
  31. return NULL;
  32. qset->qset_dma = dma;
  33. qset->whc = whc;
  34. INIT_LIST_HEAD(&qset->list_node);
  35. INIT_LIST_HEAD(&qset->stds);
  36. return qset;
  37. }
  38. /**
  39. * qset_fill_qh - fill the static endpoint state in a qset's QHead
  40. * @qset: the qset whose QH needs initializing with static endpoint
  41. * state
  42. * @urb: an urb for a transfer to this endpoint
  43. */
  44. static void qset_fill_qh(struct whc *whc, struct whc_qset *qset, struct urb *urb)
  45. {
  46. struct usb_device *usb_dev = urb->dev;
  47. struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
  48. struct usb_wireless_ep_comp_descriptor *epcd;
  49. bool is_out;
  50. uint8_t phy_rate;
  51. is_out = usb_pipeout(urb->pipe);
  52. qset->max_packet = le16_to_cpu(urb->ep->desc.wMaxPacketSize);
  53. epcd = (struct usb_wireless_ep_comp_descriptor *)qset->ep->extra;
  54. if (epcd) {
  55. qset->max_seq = epcd->bMaxSequence;
  56. qset->max_burst = epcd->bMaxBurst;
  57. } else {
  58. qset->max_seq = 2;
  59. qset->max_burst = 1;
  60. }
  61. /*
  62. * Initial PHY rate is 53.3 Mbit/s for control endpoints or
  63. * the maximum supported by the device for other endpoints
  64. * (unless limited by the user).
  65. */
  66. if (usb_pipecontrol(urb->pipe))
  67. phy_rate = UWB_PHY_RATE_53;
  68. else {
  69. uint16_t phy_rates;
  70. phy_rates = le16_to_cpu(wusb_dev->wusb_cap_descr->wPHYRates);
  71. phy_rate = fls(phy_rates) - 1;
  72. if (phy_rate > whc->wusbhc.phy_rate)
  73. phy_rate = whc->wusbhc.phy_rate;
  74. }
  75. qset->qh.info1 = cpu_to_le32(
  76. QH_INFO1_EP(usb_pipeendpoint(urb->pipe))
  77. | (is_out ? QH_INFO1_DIR_OUT : QH_INFO1_DIR_IN)
  78. | usb_pipe_to_qh_type(urb->pipe)
  79. | QH_INFO1_DEV_INFO_IDX(wusb_port_no_to_idx(usb_dev->portnum))
  80. | QH_INFO1_MAX_PKT_LEN(qset->max_packet)
  81. );
  82. qset->qh.info2 = cpu_to_le32(
  83. QH_INFO2_BURST(qset->max_burst)
  84. | QH_INFO2_DBP(0)
  85. | QH_INFO2_MAX_COUNT(3)
  86. | QH_INFO2_MAX_RETRY(3)
  87. | QH_INFO2_MAX_SEQ(qset->max_seq - 1)
  88. );
  89. /* FIXME: where can we obtain these Tx parameters from? Why
  90. * doesn't the chip know what Tx power to use? It knows the Rx
  91. * strength and can presumably guess the Tx power required
  92. * from that? */
  93. qset->qh.info3 = cpu_to_le32(
  94. QH_INFO3_TX_RATE(phy_rate)
  95. | QH_INFO3_TX_PWR(0) /* 0 == max power */
  96. );
  97. qset->qh.cur_window = cpu_to_le32((1 << qset->max_burst) - 1);
  98. }
  99. /**
  100. * qset_clear - clear fields in a qset so it may be reinserted into a
  101. * schedule.
  102. *
  103. * The sequence number and current window are not cleared (see
  104. * qset_reset()).
  105. */
  106. void qset_clear(struct whc *whc, struct whc_qset *qset)
  107. {
  108. qset->td_start = qset->td_end = qset->ntds = 0;
  109. qset->qh.link = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
  110. qset->qh.status = qset->qh.status & QH_STATUS_SEQ_MASK;
  111. qset->qh.err_count = 0;
  112. qset->qh.scratch[0] = 0;
  113. qset->qh.scratch[1] = 0;
  114. qset->qh.scratch[2] = 0;
  115. memset(&qset->qh.overlay, 0, sizeof(qset->qh.overlay));
  116. init_completion(&qset->remove_complete);
  117. }
  118. /**
  119. * qset_reset - reset endpoint state in a qset.
  120. *
  121. * Clears the sequence number and current window. This qset must not
  122. * be in the ASL or PZL.
  123. */
  124. void qset_reset(struct whc *whc, struct whc_qset *qset)
  125. {
  126. qset->reset = 0;
  127. qset->qh.status &= ~QH_STATUS_SEQ_MASK;
  128. qset->qh.cur_window = cpu_to_le32((1 << qset->max_burst) - 1);
  129. }
  130. /**
  131. * get_qset - get the qset for an async endpoint
  132. *
  133. * A new qset is created if one does not already exist.
  134. */
  135. struct whc_qset *get_qset(struct whc *whc, struct urb *urb,
  136. gfp_t mem_flags)
  137. {
  138. struct whc_qset *qset;
  139. qset = urb->ep->hcpriv;
  140. if (qset == NULL) {
  141. qset = qset_alloc(whc, mem_flags);
  142. if (qset == NULL)
  143. return NULL;
  144. qset->ep = urb->ep;
  145. urb->ep->hcpriv = qset;
  146. qset_fill_qh(whc, qset, urb);
  147. }
  148. return qset;
  149. }
  150. void qset_remove_complete(struct whc *whc, struct whc_qset *qset)
  151. {
  152. qset->remove = 0;
  153. list_del_init(&qset->list_node);
  154. complete(&qset->remove_complete);
  155. }
  156. /**
  157. * qset_add_qtds - add qTDs for an URB to a qset
  158. *
  159. * Returns true if the list (ASL/PZL) must be updated because (for a
  160. * WHCI 0.95 controller) an activated qTD was pointed to be iCur.
  161. */
  162. enum whc_update qset_add_qtds(struct whc *whc, struct whc_qset *qset)
  163. {
  164. struct whc_std *std;
  165. enum whc_update update = 0;
  166. list_for_each_entry(std, &qset->stds, list_node) {
  167. struct whc_qtd *qtd;
  168. uint32_t status;
  169. if (qset->ntds >= WHCI_QSET_TD_MAX
  170. || (qset->pause_after_urb && std->urb != qset->pause_after_urb))
  171. break;
  172. if (std->qtd)
  173. continue; /* already has a qTD */
  174. qtd = std->qtd = &qset->qtd[qset->td_end];
  175. /* Fill in setup bytes for control transfers. */
  176. if (usb_pipecontrol(std->urb->pipe))
  177. memcpy(qtd->setup, std->urb->setup_packet, 8);
  178. status = QTD_STS_ACTIVE | QTD_STS_LEN(std->len);
  179. if (whc_std_last(std) && usb_pipeout(std->urb->pipe))
  180. status |= QTD_STS_LAST_PKT;
  181. /*
  182. * For an IN transfer the iAlt field should be set so
  183. * the h/w will automatically advance to the next
  184. * transfer. However, if there are 8 or more TDs
  185. * remaining in this transfer then iAlt cannot be set
  186. * as it could point to somewhere in this transfer.
  187. */
  188. if (std->ntds_remaining < WHCI_QSET_TD_MAX) {
  189. int ialt;
  190. ialt = (qset->td_end + std->ntds_remaining) % WHCI_QSET_TD_MAX;
  191. status |= QTD_STS_IALT(ialt);
  192. } else if (usb_pipein(std->urb->pipe))
  193. qset->pause_after_urb = std->urb;
  194. if (std->num_pointers)
  195. qtd->options = cpu_to_le32(QTD_OPT_IOC);
  196. else
  197. qtd->options = cpu_to_le32(QTD_OPT_IOC | QTD_OPT_SMALL);
  198. qtd->page_list_ptr = cpu_to_le64(std->dma_addr);
  199. qtd->status = cpu_to_le32(status);
  200. if (QH_STATUS_TO_ICUR(qset->qh.status) == qset->td_end)
  201. update = WHC_UPDATE_UPDATED;
  202. if (++qset->td_end >= WHCI_QSET_TD_MAX)
  203. qset->td_end = 0;
  204. qset->ntds++;
  205. }
  206. return update;
  207. }
  208. /**
  209. * qset_remove_qtd - remove the first qTD from a qset.
  210. *
  211. * The qTD might be still active (if it's part of a IN URB that
  212. * resulted in a short read) so ensure it's deactivated.
  213. */
  214. static void qset_remove_qtd(struct whc *whc, struct whc_qset *qset)
  215. {
  216. qset->qtd[qset->td_start].status = 0;
  217. if (++qset->td_start >= WHCI_QSET_TD_MAX)
  218. qset->td_start = 0;
  219. qset->ntds--;
  220. }
  221. static void qset_copy_bounce_to_sg(struct whc *whc, struct whc_std *std)
  222. {
  223. struct scatterlist *sg;
  224. void *bounce;
  225. size_t remaining, offset;
  226. bounce = std->bounce_buf;
  227. remaining = std->len;
  228. sg = std->bounce_sg;
  229. offset = std->bounce_offset;
  230. while (remaining) {
  231. size_t len;
  232. len = min(sg->length - offset, remaining);
  233. memcpy(sg_virt(sg) + offset, bounce, len);
  234. bounce += len;
  235. remaining -= len;
  236. offset += len;
  237. if (offset >= sg->length) {
  238. sg = sg_next(sg);
  239. offset = 0;
  240. }
  241. }
  242. }
  243. /**
  244. * qset_free_std - remove an sTD and free it.
  245. * @whc: the WHCI host controller
  246. * @std: the sTD to remove and free.
  247. */
  248. void qset_free_std(struct whc *whc, struct whc_std *std)
  249. {
  250. list_del(&std->list_node);
  251. if (std->bounce_buf) {
  252. bool is_out = usb_pipeout(std->urb->pipe);
  253. dma_addr_t dma_addr;
  254. if (std->num_pointers)
  255. dma_addr = le64_to_cpu(std->pl_virt[0].buf_ptr);
  256. else
  257. dma_addr = std->dma_addr;
  258. dma_unmap_single(whc->wusbhc.dev, dma_addr,
  259. std->len, is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  260. if (!is_out)
  261. qset_copy_bounce_to_sg(whc, std);
  262. kfree(std->bounce_buf);
  263. }
  264. if (std->pl_virt) {
  265. if (!dma_mapping_error(whc->wusbhc.dev, std->dma_addr))
  266. dma_unmap_single(whc->wusbhc.dev, std->dma_addr,
  267. std->num_pointers * sizeof(struct whc_page_list_entry),
  268. DMA_TO_DEVICE);
  269. kfree(std->pl_virt);
  270. std->pl_virt = NULL;
  271. }
  272. kfree(std);
  273. }
  274. /**
  275. * qset_remove_qtds - remove an URB's qTDs (and sTDs).
  276. */
  277. static void qset_remove_qtds(struct whc *whc, struct whc_qset *qset,
  278. struct urb *urb)
  279. {
  280. struct whc_std *std, *t;
  281. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  282. if (std->urb != urb)
  283. break;
  284. if (std->qtd != NULL)
  285. qset_remove_qtd(whc, qset);
  286. qset_free_std(whc, std);
  287. }
  288. }
  289. /**
  290. * qset_free_stds - free any remaining sTDs for an URB.
  291. */
  292. static void qset_free_stds(struct whc_qset *qset, struct urb *urb)
  293. {
  294. struct whc_std *std, *t;
  295. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  296. if (std->urb == urb)
  297. qset_free_std(qset->whc, std);
  298. }
  299. }
  300. static int qset_fill_page_list(struct whc *whc, struct whc_std *std, gfp_t mem_flags)
  301. {
  302. dma_addr_t dma_addr = std->dma_addr;
  303. dma_addr_t sp, ep;
  304. size_t pl_len;
  305. int p;
  306. /* Short buffers don't need a page list. */
  307. if (std->len <= WHCI_PAGE_SIZE) {
  308. std->num_pointers = 0;
  309. return 0;
  310. }
  311. sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
  312. ep = dma_addr + std->len;
  313. std->num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
  314. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  315. std->pl_virt = kmalloc(pl_len, mem_flags);
  316. if (std->pl_virt == NULL)
  317. return -ENOMEM;
  318. std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt, pl_len, DMA_TO_DEVICE);
  319. if (dma_mapping_error(whc->wusbhc.dev, std->dma_addr)) {
  320. kfree(std->pl_virt);
  321. return -EFAULT;
  322. }
  323. for (p = 0; p < std->num_pointers; p++) {
  324. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  325. dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
  326. }
  327. return 0;
  328. }
  329. /**
  330. * urb_dequeue_work - executes asl/pzl update and gives back the urb to the system.
  331. */
  332. static void urb_dequeue_work(struct work_struct *work)
  333. {
  334. struct whc_urb *wurb = container_of(work, struct whc_urb, dequeue_work);
  335. struct whc_qset *qset = wurb->qset;
  336. struct whc *whc = qset->whc;
  337. unsigned long flags;
  338. if (wurb->is_async)
  339. asl_update(whc, WUSBCMD_ASYNC_UPDATED
  340. | WUSBCMD_ASYNC_SYNCED_DB
  341. | WUSBCMD_ASYNC_QSET_RM);
  342. else
  343. pzl_update(whc, WUSBCMD_PERIODIC_UPDATED
  344. | WUSBCMD_PERIODIC_SYNCED_DB
  345. | WUSBCMD_PERIODIC_QSET_RM);
  346. spin_lock_irqsave(&whc->lock, flags);
  347. qset_remove_urb(whc, qset, wurb->urb, wurb->status);
  348. spin_unlock_irqrestore(&whc->lock, flags);
  349. }
  350. static struct whc_std *qset_new_std(struct whc *whc, struct whc_qset *qset,
  351. struct urb *urb, gfp_t mem_flags)
  352. {
  353. struct whc_std *std;
  354. std = kzalloc(sizeof(struct whc_std), mem_flags);
  355. if (std == NULL)
  356. return NULL;
  357. std->urb = urb;
  358. std->qtd = NULL;
  359. INIT_LIST_HEAD(&std->list_node);
  360. list_add_tail(&std->list_node, &qset->stds);
  361. return std;
  362. }
  363. static int qset_add_urb_sg(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  364. gfp_t mem_flags)
  365. {
  366. size_t remaining;
  367. struct scatterlist *sg;
  368. int i;
  369. int ntds = 0;
  370. struct whc_std *std = NULL;
  371. struct whc_page_list_entry *new_pl_virt;
  372. dma_addr_t prev_end = 0;
  373. size_t pl_len;
  374. int p = 0;
  375. remaining = urb->transfer_buffer_length;
  376. for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
  377. dma_addr_t dma_addr;
  378. size_t dma_remaining;
  379. dma_addr_t sp, ep;
  380. int num_pointers;
  381. if (remaining == 0) {
  382. break;
  383. }
  384. dma_addr = sg_dma_address(sg);
  385. dma_remaining = min_t(size_t, sg_dma_len(sg), remaining);
  386. while (dma_remaining) {
  387. size_t dma_len;
  388. /*
  389. * We can use the previous std (if it exists) provided that:
  390. * - the previous one ended on a page boundary.
  391. * - the current one begins on a page boundary.
  392. * - the previous one isn't full.
  393. *
  394. * If a new std is needed but the previous one
  395. * was not a whole number of packets then this
  396. * sg list cannot be mapped onto multiple
  397. * qTDs. Return an error and let the caller
  398. * sort it out.
  399. */
  400. if (!std
  401. || (prev_end & (WHCI_PAGE_SIZE-1))
  402. || (dma_addr & (WHCI_PAGE_SIZE-1))
  403. || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) {
  404. if (std && std->len % qset->max_packet != 0)
  405. return -EINVAL;
  406. std = qset_new_std(whc, qset, urb, mem_flags);
  407. if (std == NULL) {
  408. return -ENOMEM;
  409. }
  410. ntds++;
  411. p = 0;
  412. }
  413. dma_len = dma_remaining;
  414. /*
  415. * If the remainder of this element doesn't
  416. * fit in a single qTD, limit the qTD to a
  417. * whole number of packets. This allows the
  418. * remainder to go into the next qTD.
  419. */
  420. if (std->len + dma_len > QTD_MAX_XFER_SIZE) {
  421. dma_len = (QTD_MAX_XFER_SIZE / qset->max_packet)
  422. * qset->max_packet - std->len;
  423. }
  424. std->len += dma_len;
  425. std->ntds_remaining = -1; /* filled in later */
  426. sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
  427. ep = dma_addr + dma_len;
  428. num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
  429. std->num_pointers += num_pointers;
  430. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  431. new_pl_virt = krealloc(std->pl_virt, pl_len, mem_flags);
  432. if (new_pl_virt == NULL) {
  433. kfree(std->pl_virt);
  434. std->pl_virt = NULL;
  435. return -ENOMEM;
  436. }
  437. std->pl_virt = new_pl_virt;
  438. for (;p < std->num_pointers; p++) {
  439. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  440. dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
  441. }
  442. prev_end = dma_addr = ep;
  443. dma_remaining -= dma_len;
  444. remaining -= dma_len;
  445. }
  446. }
  447. /* Now the number of stds is know, go back and fill in
  448. std->ntds_remaining. */
  449. list_for_each_entry(std, &qset->stds, list_node) {
  450. if (std->ntds_remaining == -1) {
  451. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  452. std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt,
  453. pl_len, DMA_TO_DEVICE);
  454. if (dma_mapping_error(whc->wusbhc.dev, std->dma_addr))
  455. return -EFAULT;
  456. std->ntds_remaining = ntds--;
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. * qset_add_urb_sg_linearize - add an urb with sg list, copying the data
  463. *
  464. * If the URB contains an sg list whose elements cannot be directly
  465. * mapped to qTDs then the data must be transferred via bounce
  466. * buffers.
  467. */
  468. static int qset_add_urb_sg_linearize(struct whc *whc, struct whc_qset *qset,
  469. struct urb *urb, gfp_t mem_flags)
  470. {
  471. bool is_out = usb_pipeout(urb->pipe);
  472. size_t max_std_len;
  473. size_t remaining;
  474. int ntds = 0;
  475. struct whc_std *std = NULL;
  476. void *bounce = NULL;
  477. struct scatterlist *sg;
  478. int i;
  479. /* limit maximum bounce buffer to 16 * 3.5 KiB ~= 28 k */
  480. max_std_len = qset->max_burst * qset->max_packet;
  481. remaining = urb->transfer_buffer_length;
  482. for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
  483. size_t len;
  484. size_t sg_remaining;
  485. void *orig;
  486. if (remaining == 0) {
  487. break;
  488. }
  489. sg_remaining = min_t(size_t, remaining, sg->length);
  490. orig = sg_virt(sg);
  491. while (sg_remaining) {
  492. if (!std || std->len == max_std_len) {
  493. std = qset_new_std(whc, qset, urb, mem_flags);
  494. if (std == NULL)
  495. return -ENOMEM;
  496. std->bounce_buf = kmalloc(max_std_len, mem_flags);
  497. if (std->bounce_buf == NULL)
  498. return -ENOMEM;
  499. std->bounce_sg = sg;
  500. std->bounce_offset = orig - sg_virt(sg);
  501. bounce = std->bounce_buf;
  502. ntds++;
  503. }
  504. len = min(sg_remaining, max_std_len - std->len);
  505. if (is_out)
  506. memcpy(bounce, orig, len);
  507. std->len += len;
  508. std->ntds_remaining = -1; /* filled in later */
  509. bounce += len;
  510. orig += len;
  511. sg_remaining -= len;
  512. remaining -= len;
  513. }
  514. }
  515. /*
  516. * For each of the new sTDs, map the bounce buffers, create
  517. * page lists (if necessary), and fill in std->ntds_remaining.
  518. */
  519. list_for_each_entry(std, &qset->stds, list_node) {
  520. if (std->ntds_remaining != -1)
  521. continue;
  522. std->dma_addr = dma_map_single(&whc->umc->dev, std->bounce_buf, std->len,
  523. is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  524. if (dma_mapping_error(&whc->umc->dev, std->dma_addr))
  525. return -EFAULT;
  526. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  527. return -ENOMEM;
  528. std->ntds_remaining = ntds--;
  529. }
  530. return 0;
  531. }
  532. /**
  533. * qset_add_urb - add an urb to the qset's queue.
  534. *
  535. * The URB is chopped into sTDs, one for each qTD that will required.
  536. * At least one qTD (and sTD) is required even if the transfer has no
  537. * data (e.g., for some control transfers).
  538. */
  539. int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  540. gfp_t mem_flags)
  541. {
  542. struct whc_urb *wurb;
  543. int remaining = urb->transfer_buffer_length;
  544. u64 transfer_dma = urb->transfer_dma;
  545. int ntds_remaining;
  546. int ret;
  547. wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
  548. if (wurb == NULL)
  549. goto err_no_mem;
  550. urb->hcpriv = wurb;
  551. wurb->qset = qset;
  552. wurb->urb = urb;
  553. INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
  554. if (urb->num_sgs) {
  555. ret = qset_add_urb_sg(whc, qset, urb, mem_flags);
  556. if (ret == -EINVAL) {
  557. qset_free_stds(qset, urb);
  558. ret = qset_add_urb_sg_linearize(whc, qset, urb, mem_flags);
  559. }
  560. if (ret < 0)
  561. goto err_no_mem;
  562. return 0;
  563. }
  564. ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
  565. if (ntds_remaining == 0)
  566. ntds_remaining = 1;
  567. while (ntds_remaining) {
  568. struct whc_std *std;
  569. size_t std_len;
  570. std_len = remaining;
  571. if (std_len > QTD_MAX_XFER_SIZE)
  572. std_len = QTD_MAX_XFER_SIZE;
  573. std = qset_new_std(whc, qset, urb, mem_flags);
  574. if (std == NULL)
  575. goto err_no_mem;
  576. std->dma_addr = transfer_dma;
  577. std->len = std_len;
  578. std->ntds_remaining = ntds_remaining;
  579. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  580. goto err_no_mem;
  581. ntds_remaining--;
  582. remaining -= std_len;
  583. transfer_dma += std_len;
  584. }
  585. return 0;
  586. err_no_mem:
  587. qset_free_stds(qset, urb);
  588. return -ENOMEM;
  589. }
  590. /**
  591. * qset_remove_urb - remove an URB from the urb queue.
  592. *
  593. * The URB is returned to the USB subsystem.
  594. */
  595. void qset_remove_urb(struct whc *whc, struct whc_qset *qset,
  596. struct urb *urb, int status)
  597. {
  598. struct wusbhc *wusbhc = &whc->wusbhc;
  599. struct whc_urb *wurb = urb->hcpriv;
  600. usb_hcd_unlink_urb_from_ep(&wusbhc->usb_hcd, urb);
  601. /* Drop the lock as urb->complete() may enqueue another urb. */
  602. spin_unlock(&whc->lock);
  603. wusbhc_giveback_urb(wusbhc, urb, status);
  604. spin_lock(&whc->lock);
  605. kfree(wurb);
  606. }
  607. /**
  608. * get_urb_status_from_qtd - get the completed urb status from qTD status
  609. * @urb: completed urb
  610. * @status: qTD status
  611. */
  612. static int get_urb_status_from_qtd(struct urb *urb, u32 status)
  613. {
  614. if (status & QTD_STS_HALTED) {
  615. if (status & QTD_STS_DBE)
  616. return usb_pipein(urb->pipe) ? -ENOSR : -ECOMM;
  617. else if (status & QTD_STS_BABBLE)
  618. return -EOVERFLOW;
  619. else if (status & QTD_STS_RCE)
  620. return -ETIME;
  621. return -EPIPE;
  622. }
  623. if (usb_pipein(urb->pipe)
  624. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  625. && urb->actual_length < urb->transfer_buffer_length)
  626. return -EREMOTEIO;
  627. return 0;
  628. }
  629. /**
  630. * process_inactive_qtd - process an inactive (but not halted) qTD.
  631. *
  632. * Update the urb with the transfer bytes from the qTD, if the urb is
  633. * completely transferred or (in the case of an IN only) the LPF is
  634. * set, then the transfer is complete and the urb should be returned
  635. * to the system.
  636. */
  637. void process_inactive_qtd(struct whc *whc, struct whc_qset *qset,
  638. struct whc_qtd *qtd)
  639. {
  640. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  641. struct urb *urb = std->urb;
  642. uint32_t status;
  643. bool complete;
  644. status = le32_to_cpu(qtd->status);
  645. urb->actual_length += std->len - QTD_STS_TO_LEN(status);
  646. if (usb_pipein(urb->pipe) && (status & QTD_STS_LAST_PKT))
  647. complete = true;
  648. else
  649. complete = whc_std_last(std);
  650. qset_remove_qtd(whc, qset);
  651. qset_free_std(whc, std);
  652. /*
  653. * Transfers for this URB are complete? Then return it to the
  654. * USB subsystem.
  655. */
  656. if (complete) {
  657. qset_remove_qtds(whc, qset, urb);
  658. qset_remove_urb(whc, qset, urb, get_urb_status_from_qtd(urb, status));
  659. /*
  660. * If iAlt isn't valid then the hardware didn't
  661. * advance iCur. Adjust the start and end pointers to
  662. * match iCur.
  663. */
  664. if (!(status & QTD_STS_IALT_VALID))
  665. qset->td_start = qset->td_end
  666. = QH_STATUS_TO_ICUR(le16_to_cpu(qset->qh.status));
  667. qset->pause_after_urb = NULL;
  668. }
  669. }
  670. /**
  671. * process_halted_qtd - process a qset with a halted qtd
  672. *
  673. * Remove all the qTDs for the failed URB and return the failed URB to
  674. * the USB subsystem. Then remove all other qTDs so the qset can be
  675. * removed.
  676. *
  677. * FIXME: this is the point where rate adaptation can be done. If a
  678. * transfer failed because it exceeded the maximum number of retries
  679. * then it could be reactivated with a slower rate without having to
  680. * remove the qset.
  681. */
  682. void process_halted_qtd(struct whc *whc, struct whc_qset *qset,
  683. struct whc_qtd *qtd)
  684. {
  685. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  686. struct urb *urb = std->urb;
  687. int urb_status;
  688. urb_status = get_urb_status_from_qtd(urb, le32_to_cpu(qtd->status));
  689. qset_remove_qtds(whc, qset, urb);
  690. qset_remove_urb(whc, qset, urb, urb_status);
  691. list_for_each_entry(std, &qset->stds, list_node) {
  692. if (qset->ntds == 0)
  693. break;
  694. qset_remove_qtd(whc, qset);
  695. std->qtd = NULL;
  696. }
  697. qset->remove = 1;
  698. }
  699. void qset_free(struct whc *whc, struct whc_qset *qset)
  700. {
  701. dma_pool_free(whc->qset_pool, qset, qset->qset_dma);
  702. }
  703. /**
  704. * qset_delete - wait for a qset to be unused, then free it.
  705. */
  706. void qset_delete(struct whc *whc, struct whc_qset *qset)
  707. {
  708. wait_for_completion(&qset->remove_complete);
  709. qset_free(whc, qset);
  710. }