qset.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. for (p = 0; p < std->num_pointers; p++) {
  321. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  322. dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
  323. }
  324. return 0;
  325. }
  326. /**
  327. * urb_dequeue_work - executes asl/pzl update and gives back the urb to the system.
  328. */
  329. static void urb_dequeue_work(struct work_struct *work)
  330. {
  331. struct whc_urb *wurb = container_of(work, struct whc_urb, dequeue_work);
  332. struct whc_qset *qset = wurb->qset;
  333. struct whc *whc = qset->whc;
  334. unsigned long flags;
  335. if (wurb->is_async == true)
  336. asl_update(whc, WUSBCMD_ASYNC_UPDATED
  337. | WUSBCMD_ASYNC_SYNCED_DB
  338. | WUSBCMD_ASYNC_QSET_RM);
  339. else
  340. pzl_update(whc, WUSBCMD_PERIODIC_UPDATED
  341. | WUSBCMD_PERIODIC_SYNCED_DB
  342. | WUSBCMD_PERIODIC_QSET_RM);
  343. spin_lock_irqsave(&whc->lock, flags);
  344. qset_remove_urb(whc, qset, wurb->urb, wurb->status);
  345. spin_unlock_irqrestore(&whc->lock, flags);
  346. }
  347. static struct whc_std *qset_new_std(struct whc *whc, struct whc_qset *qset,
  348. struct urb *urb, gfp_t mem_flags)
  349. {
  350. struct whc_std *std;
  351. std = kzalloc(sizeof(struct whc_std), mem_flags);
  352. if (std == NULL)
  353. return NULL;
  354. std->urb = urb;
  355. std->qtd = NULL;
  356. INIT_LIST_HEAD(&std->list_node);
  357. list_add_tail(&std->list_node, &qset->stds);
  358. return std;
  359. }
  360. static int qset_add_urb_sg(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  361. gfp_t mem_flags)
  362. {
  363. size_t remaining;
  364. struct scatterlist *sg;
  365. int i;
  366. int ntds = 0;
  367. struct whc_std *std = NULL;
  368. struct whc_page_list_entry *new_pl_virt;
  369. dma_addr_t prev_end = 0;
  370. size_t pl_len;
  371. int p = 0;
  372. remaining = urb->transfer_buffer_length;
  373. for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
  374. dma_addr_t dma_addr;
  375. size_t dma_remaining;
  376. dma_addr_t sp, ep;
  377. int num_pointers;
  378. if (remaining == 0) {
  379. break;
  380. }
  381. dma_addr = sg_dma_address(sg);
  382. dma_remaining = min_t(size_t, sg_dma_len(sg), remaining);
  383. while (dma_remaining) {
  384. size_t dma_len;
  385. /*
  386. * We can use the previous std (if it exists) provided that:
  387. * - the previous one ended on a page boundary.
  388. * - the current one begins on a page boundary.
  389. * - the previous one isn't full.
  390. *
  391. * If a new std is needed but the previous one
  392. * was not a whole number of packets then this
  393. * sg list cannot be mapped onto multiple
  394. * qTDs. Return an error and let the caller
  395. * sort it out.
  396. */
  397. if (!std
  398. || (prev_end & (WHCI_PAGE_SIZE-1))
  399. || (dma_addr & (WHCI_PAGE_SIZE-1))
  400. || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) {
  401. if (std && std->len % qset->max_packet != 0)
  402. return -EINVAL;
  403. std = qset_new_std(whc, qset, urb, mem_flags);
  404. if (std == NULL) {
  405. return -ENOMEM;
  406. }
  407. ntds++;
  408. p = 0;
  409. }
  410. dma_len = dma_remaining;
  411. /*
  412. * If the remainder of this element doesn't
  413. * fit in a single qTD, limit the qTD to a
  414. * whole number of packets. This allows the
  415. * remainder to go into the next qTD.
  416. */
  417. if (std->len + dma_len > QTD_MAX_XFER_SIZE) {
  418. dma_len = (QTD_MAX_XFER_SIZE / qset->max_packet)
  419. * qset->max_packet - std->len;
  420. }
  421. std->len += dma_len;
  422. std->ntds_remaining = -1; /* filled in later */
  423. sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
  424. ep = dma_addr + dma_len;
  425. num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
  426. std->num_pointers += num_pointers;
  427. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  428. new_pl_virt = krealloc(std->pl_virt, pl_len, mem_flags);
  429. if (new_pl_virt == NULL) {
  430. kfree(std->pl_virt);
  431. std->pl_virt = NULL;
  432. return -ENOMEM;
  433. }
  434. std->pl_virt = new_pl_virt;
  435. for (;p < std->num_pointers; p++) {
  436. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  437. dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
  438. }
  439. prev_end = dma_addr = ep;
  440. dma_remaining -= dma_len;
  441. remaining -= dma_len;
  442. }
  443. }
  444. /* Now the number of stds is know, go back and fill in
  445. std->ntds_remaining. */
  446. list_for_each_entry(std, &qset->stds, list_node) {
  447. if (std->ntds_remaining == -1) {
  448. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  449. std->ntds_remaining = ntds--;
  450. std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt,
  451. pl_len, DMA_TO_DEVICE);
  452. }
  453. }
  454. return 0;
  455. }
  456. /**
  457. * qset_add_urb_sg_linearize - add an urb with sg list, copying the data
  458. *
  459. * If the URB contains an sg list whose elements cannot be directly
  460. * mapped to qTDs then the data must be transferred via bounce
  461. * buffers.
  462. */
  463. static int qset_add_urb_sg_linearize(struct whc *whc, struct whc_qset *qset,
  464. struct urb *urb, gfp_t mem_flags)
  465. {
  466. bool is_out = usb_pipeout(urb->pipe);
  467. size_t max_std_len;
  468. size_t remaining;
  469. int ntds = 0;
  470. struct whc_std *std = NULL;
  471. void *bounce = NULL;
  472. struct scatterlist *sg;
  473. int i;
  474. /* limit maximum bounce buffer to 16 * 3.5 KiB ~= 28 k */
  475. max_std_len = qset->max_burst * qset->max_packet;
  476. remaining = urb->transfer_buffer_length;
  477. for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
  478. size_t len;
  479. size_t sg_remaining;
  480. void *orig;
  481. if (remaining == 0) {
  482. break;
  483. }
  484. sg_remaining = min_t(size_t, remaining, sg->length);
  485. orig = sg_virt(sg);
  486. while (sg_remaining) {
  487. if (!std || std->len == max_std_len) {
  488. std = qset_new_std(whc, qset, urb, mem_flags);
  489. if (std == NULL)
  490. return -ENOMEM;
  491. std->bounce_buf = kmalloc(max_std_len, mem_flags);
  492. if (std->bounce_buf == NULL)
  493. return -ENOMEM;
  494. std->bounce_sg = sg;
  495. std->bounce_offset = orig - sg_virt(sg);
  496. bounce = std->bounce_buf;
  497. ntds++;
  498. }
  499. len = min(sg_remaining, max_std_len - std->len);
  500. if (is_out)
  501. memcpy(bounce, orig, len);
  502. std->len += len;
  503. std->ntds_remaining = -1; /* filled in later */
  504. bounce += len;
  505. orig += len;
  506. sg_remaining -= len;
  507. remaining -= len;
  508. }
  509. }
  510. /*
  511. * For each of the new sTDs, map the bounce buffers, create
  512. * page lists (if necessary), and fill in std->ntds_remaining.
  513. */
  514. list_for_each_entry(std, &qset->stds, list_node) {
  515. if (std->ntds_remaining != -1)
  516. continue;
  517. std->dma_addr = dma_map_single(&whc->umc->dev, std->bounce_buf, std->len,
  518. is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  519. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  520. return -ENOMEM;
  521. std->ntds_remaining = ntds--;
  522. }
  523. return 0;
  524. }
  525. /**
  526. * qset_add_urb - add an urb to the qset's queue.
  527. *
  528. * The URB is chopped into sTDs, one for each qTD that will required.
  529. * At least one qTD (and sTD) is required even if the transfer has no
  530. * data (e.g., for some control transfers).
  531. */
  532. int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  533. gfp_t mem_flags)
  534. {
  535. struct whc_urb *wurb;
  536. int remaining = urb->transfer_buffer_length;
  537. u64 transfer_dma = urb->transfer_dma;
  538. int ntds_remaining;
  539. int ret;
  540. wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
  541. if (wurb == NULL)
  542. goto err_no_mem;
  543. urb->hcpriv = wurb;
  544. wurb->qset = qset;
  545. wurb->urb = urb;
  546. INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
  547. if (urb->num_sgs) {
  548. ret = qset_add_urb_sg(whc, qset, urb, mem_flags);
  549. if (ret == -EINVAL) {
  550. qset_free_stds(qset, urb);
  551. ret = qset_add_urb_sg_linearize(whc, qset, urb, mem_flags);
  552. }
  553. if (ret < 0)
  554. goto err_no_mem;
  555. return 0;
  556. }
  557. ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
  558. if (ntds_remaining == 0)
  559. ntds_remaining = 1;
  560. while (ntds_remaining) {
  561. struct whc_std *std;
  562. size_t std_len;
  563. std_len = remaining;
  564. if (std_len > QTD_MAX_XFER_SIZE)
  565. std_len = QTD_MAX_XFER_SIZE;
  566. std = qset_new_std(whc, qset, urb, mem_flags);
  567. if (std == NULL)
  568. goto err_no_mem;
  569. std->dma_addr = transfer_dma;
  570. std->len = std_len;
  571. std->ntds_remaining = ntds_remaining;
  572. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  573. goto err_no_mem;
  574. ntds_remaining--;
  575. remaining -= std_len;
  576. transfer_dma += std_len;
  577. }
  578. return 0;
  579. err_no_mem:
  580. qset_free_stds(qset, urb);
  581. return -ENOMEM;
  582. }
  583. /**
  584. * qset_remove_urb - remove an URB from the urb queue.
  585. *
  586. * The URB is returned to the USB subsystem.
  587. */
  588. void qset_remove_urb(struct whc *whc, struct whc_qset *qset,
  589. struct urb *urb, int status)
  590. {
  591. struct wusbhc *wusbhc = &whc->wusbhc;
  592. struct whc_urb *wurb = urb->hcpriv;
  593. usb_hcd_unlink_urb_from_ep(&wusbhc->usb_hcd, urb);
  594. /* Drop the lock as urb->complete() may enqueue another urb. */
  595. spin_unlock(&whc->lock);
  596. wusbhc_giveback_urb(wusbhc, urb, status);
  597. spin_lock(&whc->lock);
  598. kfree(wurb);
  599. }
  600. /**
  601. * get_urb_status_from_qtd - get the completed urb status from qTD status
  602. * @urb: completed urb
  603. * @status: qTD status
  604. */
  605. static int get_urb_status_from_qtd(struct urb *urb, u32 status)
  606. {
  607. if (status & QTD_STS_HALTED) {
  608. if (status & QTD_STS_DBE)
  609. return usb_pipein(urb->pipe) ? -ENOSR : -ECOMM;
  610. else if (status & QTD_STS_BABBLE)
  611. return -EOVERFLOW;
  612. else if (status & QTD_STS_RCE)
  613. return -ETIME;
  614. return -EPIPE;
  615. }
  616. if (usb_pipein(urb->pipe)
  617. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  618. && urb->actual_length < urb->transfer_buffer_length)
  619. return -EREMOTEIO;
  620. return 0;
  621. }
  622. /**
  623. * process_inactive_qtd - process an inactive (but not halted) qTD.
  624. *
  625. * Update the urb with the transfer bytes from the qTD, if the urb is
  626. * completely transferred or (in the case of an IN only) the LPF is
  627. * set, then the transfer is complete and the urb should be returned
  628. * to the system.
  629. */
  630. void process_inactive_qtd(struct whc *whc, struct whc_qset *qset,
  631. struct whc_qtd *qtd)
  632. {
  633. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  634. struct urb *urb = std->urb;
  635. uint32_t status;
  636. bool complete;
  637. status = le32_to_cpu(qtd->status);
  638. urb->actual_length += std->len - QTD_STS_TO_LEN(status);
  639. if (usb_pipein(urb->pipe) && (status & QTD_STS_LAST_PKT))
  640. complete = true;
  641. else
  642. complete = whc_std_last(std);
  643. qset_remove_qtd(whc, qset);
  644. qset_free_std(whc, std);
  645. /*
  646. * Transfers for this URB are complete? Then return it to the
  647. * USB subsystem.
  648. */
  649. if (complete) {
  650. qset_remove_qtds(whc, qset, urb);
  651. qset_remove_urb(whc, qset, urb, get_urb_status_from_qtd(urb, status));
  652. /*
  653. * If iAlt isn't valid then the hardware didn't
  654. * advance iCur. Adjust the start and end pointers to
  655. * match iCur.
  656. */
  657. if (!(status & QTD_STS_IALT_VALID))
  658. qset->td_start = qset->td_end
  659. = QH_STATUS_TO_ICUR(le16_to_cpu(qset->qh.status));
  660. qset->pause_after_urb = NULL;
  661. }
  662. }
  663. /**
  664. * process_halted_qtd - process a qset with a halted qtd
  665. *
  666. * Remove all the qTDs for the failed URB and return the failed URB to
  667. * the USB subsystem. Then remove all other qTDs so the qset can be
  668. * removed.
  669. *
  670. * FIXME: this is the point where rate adaptation can be done. If a
  671. * transfer failed because it exceeded the maximum number of retries
  672. * then it could be reactivated with a slower rate without having to
  673. * remove the qset.
  674. */
  675. void process_halted_qtd(struct whc *whc, struct whc_qset *qset,
  676. struct whc_qtd *qtd)
  677. {
  678. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  679. struct urb *urb = std->urb;
  680. int urb_status;
  681. urb_status = get_urb_status_from_qtd(urb, le32_to_cpu(qtd->status));
  682. qset_remove_qtds(whc, qset, urb);
  683. qset_remove_urb(whc, qset, urb, urb_status);
  684. list_for_each_entry(std, &qset->stds, list_node) {
  685. if (qset->ntds == 0)
  686. break;
  687. qset_remove_qtd(whc, qset);
  688. std->qtd = NULL;
  689. }
  690. qset->remove = 1;
  691. }
  692. void qset_free(struct whc *whc, struct whc_qset *qset)
  693. {
  694. dma_pool_free(whc->qset_pool, qset, qset->qset_dma);
  695. }
  696. /**
  697. * qset_delete - wait for a qset to be unused, then free it.
  698. */
  699. void qset_delete(struct whc *whc, struct whc_qset *qset)
  700. {
  701. wait_for_completion(&qset->remove_complete);
  702. qset_free(whc, qset);
  703. }