qset.c 21 KB

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