qset.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 *entry;
  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. std->pl_virt = krealloc(std->pl_virt, pl_len, mem_flags);
  429. if (std->pl_virt == NULL) {
  430. return -ENOMEM;
  431. }
  432. for (;p < std->num_pointers; p++, entry++) {
  433. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  434. dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
  435. }
  436. prev_end = dma_addr = ep;
  437. dma_remaining -= dma_len;
  438. remaining -= dma_len;
  439. }
  440. }
  441. /* Now the number of stds is know, go back and fill in
  442. std->ntds_remaining. */
  443. list_for_each_entry(std, &qset->stds, list_node) {
  444. if (std->ntds_remaining == -1) {
  445. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  446. std->ntds_remaining = ntds--;
  447. std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt,
  448. pl_len, DMA_TO_DEVICE);
  449. }
  450. }
  451. return 0;
  452. }
  453. /**
  454. * qset_add_urb_sg_linearize - add an urb with sg list, copying the data
  455. *
  456. * If the URB contains an sg list whose elements cannot be directly
  457. * mapped to qTDs then the data must be transferred via bounce
  458. * buffers.
  459. */
  460. static int qset_add_urb_sg_linearize(struct whc *whc, struct whc_qset *qset,
  461. struct urb *urb, gfp_t mem_flags)
  462. {
  463. bool is_out = usb_pipeout(urb->pipe);
  464. size_t max_std_len;
  465. size_t remaining;
  466. int ntds = 0;
  467. struct whc_std *std = NULL;
  468. void *bounce = NULL;
  469. struct scatterlist *sg;
  470. int i;
  471. /* limit maximum bounce buffer to 16 * 3.5 KiB ~= 28 k */
  472. max_std_len = qset->max_burst * qset->max_packet;
  473. remaining = urb->transfer_buffer_length;
  474. for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
  475. size_t len;
  476. size_t sg_remaining;
  477. void *orig;
  478. if (remaining == 0) {
  479. break;
  480. }
  481. sg_remaining = min_t(size_t, remaining, sg->length);
  482. orig = sg_virt(sg);
  483. while (sg_remaining) {
  484. if (!std || std->len == max_std_len) {
  485. std = qset_new_std(whc, qset, urb, mem_flags);
  486. if (std == NULL)
  487. return -ENOMEM;
  488. std->bounce_buf = kmalloc(max_std_len, mem_flags);
  489. if (std->bounce_buf == NULL)
  490. return -ENOMEM;
  491. std->bounce_sg = sg;
  492. std->bounce_offset = orig - sg_virt(sg);
  493. bounce = std->bounce_buf;
  494. ntds++;
  495. }
  496. len = min(sg_remaining, max_std_len - std->len);
  497. if (is_out)
  498. memcpy(bounce, orig, len);
  499. std->len += len;
  500. std->ntds_remaining = -1; /* filled in later */
  501. bounce += len;
  502. orig += len;
  503. sg_remaining -= len;
  504. remaining -= len;
  505. }
  506. }
  507. /*
  508. * For each of the new sTDs, map the bounce buffers, create
  509. * page lists (if necessary), and fill in std->ntds_remaining.
  510. */
  511. list_for_each_entry(std, &qset->stds, list_node) {
  512. if (std->ntds_remaining != -1)
  513. continue;
  514. std->dma_addr = dma_map_single(&whc->umc->dev, std->bounce_buf, std->len,
  515. is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  516. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  517. return -ENOMEM;
  518. std->ntds_remaining = ntds--;
  519. }
  520. return 0;
  521. }
  522. /**
  523. * qset_add_urb - add an urb to the qset's queue.
  524. *
  525. * The URB is chopped into sTDs, one for each qTD that will required.
  526. * At least one qTD (and sTD) is required even if the transfer has no
  527. * data (e.g., for some control transfers).
  528. */
  529. int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  530. gfp_t mem_flags)
  531. {
  532. struct whc_urb *wurb;
  533. int remaining = urb->transfer_buffer_length;
  534. u64 transfer_dma = urb->transfer_dma;
  535. int ntds_remaining;
  536. int ret;
  537. wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
  538. if (wurb == NULL)
  539. goto err_no_mem;
  540. urb->hcpriv = wurb;
  541. wurb->qset = qset;
  542. wurb->urb = urb;
  543. INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
  544. if (urb->num_sgs) {
  545. ret = qset_add_urb_sg(whc, qset, urb, mem_flags);
  546. if (ret == -EINVAL) {
  547. qset_free_stds(qset, urb);
  548. ret = qset_add_urb_sg_linearize(whc, qset, urb, mem_flags);
  549. }
  550. if (ret < 0)
  551. goto err_no_mem;
  552. return 0;
  553. }
  554. ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
  555. if (ntds_remaining == 0)
  556. ntds_remaining = 1;
  557. while (ntds_remaining) {
  558. struct whc_std *std;
  559. size_t std_len;
  560. std_len = remaining;
  561. if (std_len > QTD_MAX_XFER_SIZE)
  562. std_len = QTD_MAX_XFER_SIZE;
  563. std = qset_new_std(whc, qset, urb, mem_flags);
  564. if (std == NULL)
  565. goto err_no_mem;
  566. std->dma_addr = transfer_dma;
  567. std->len = std_len;
  568. std->ntds_remaining = ntds_remaining;
  569. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  570. goto err_no_mem;
  571. ntds_remaining--;
  572. remaining -= std_len;
  573. transfer_dma += std_len;
  574. }
  575. return 0;
  576. err_no_mem:
  577. qset_free_stds(qset, urb);
  578. return -ENOMEM;
  579. }
  580. /**
  581. * qset_remove_urb - remove an URB from the urb queue.
  582. *
  583. * The URB is returned to the USB subsystem.
  584. */
  585. void qset_remove_urb(struct whc *whc, struct whc_qset *qset,
  586. struct urb *urb, int status)
  587. {
  588. struct wusbhc *wusbhc = &whc->wusbhc;
  589. struct whc_urb *wurb = urb->hcpriv;
  590. usb_hcd_unlink_urb_from_ep(&wusbhc->usb_hcd, urb);
  591. /* Drop the lock as urb->complete() may enqueue another urb. */
  592. spin_unlock(&whc->lock);
  593. wusbhc_giveback_urb(wusbhc, urb, status);
  594. spin_lock(&whc->lock);
  595. kfree(wurb);
  596. }
  597. /**
  598. * get_urb_status_from_qtd - get the completed urb status from qTD status
  599. * @urb: completed urb
  600. * @status: qTD status
  601. */
  602. static int get_urb_status_from_qtd(struct urb *urb, u32 status)
  603. {
  604. if (status & QTD_STS_HALTED) {
  605. if (status & QTD_STS_DBE)
  606. return usb_pipein(urb->pipe) ? -ENOSR : -ECOMM;
  607. else if (status & QTD_STS_BABBLE)
  608. return -EOVERFLOW;
  609. else if (status & QTD_STS_RCE)
  610. return -ETIME;
  611. return -EPIPE;
  612. }
  613. if (usb_pipein(urb->pipe)
  614. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  615. && urb->actual_length < urb->transfer_buffer_length)
  616. return -EREMOTEIO;
  617. return 0;
  618. }
  619. /**
  620. * process_inactive_qtd - process an inactive (but not halted) qTD.
  621. *
  622. * Update the urb with the transfer bytes from the qTD, if the urb is
  623. * completely transferred or (in the case of an IN only) the LPF is
  624. * set, then the transfer is complete and the urb should be returned
  625. * to the system.
  626. */
  627. void process_inactive_qtd(struct whc *whc, struct whc_qset *qset,
  628. struct whc_qtd *qtd)
  629. {
  630. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  631. struct urb *urb = std->urb;
  632. uint32_t status;
  633. bool complete;
  634. status = le32_to_cpu(qtd->status);
  635. urb->actual_length += std->len - QTD_STS_TO_LEN(status);
  636. if (usb_pipein(urb->pipe) && (status & QTD_STS_LAST_PKT))
  637. complete = true;
  638. else
  639. complete = whc_std_last(std);
  640. qset_remove_qtd(whc, qset);
  641. qset_free_std(whc, std);
  642. /*
  643. * Transfers for this URB are complete? Then return it to the
  644. * USB subsystem.
  645. */
  646. if (complete) {
  647. qset_remove_qtds(whc, qset, urb);
  648. qset_remove_urb(whc, qset, urb, get_urb_status_from_qtd(urb, status));
  649. /*
  650. * If iAlt isn't valid then the hardware didn't
  651. * advance iCur. Adjust the start and end pointers to
  652. * match iCur.
  653. */
  654. if (!(status & QTD_STS_IALT_VALID))
  655. qset->td_start = qset->td_end
  656. = QH_STATUS_TO_ICUR(le16_to_cpu(qset->qh.status));
  657. qset->pause_after_urb = NULL;
  658. }
  659. }
  660. /**
  661. * process_halted_qtd - process a qset with a halted qtd
  662. *
  663. * Remove all the qTDs for the failed URB and return the failed URB to
  664. * the USB subsystem. Then remove all other qTDs so the qset can be
  665. * removed.
  666. *
  667. * FIXME: this is the point where rate adaptation can be done. If a
  668. * transfer failed because it exceeded the maximum number of retries
  669. * then it could be reactivated with a slower rate without having to
  670. * remove the qset.
  671. */
  672. void process_halted_qtd(struct whc *whc, struct whc_qset *qset,
  673. struct whc_qtd *qtd)
  674. {
  675. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  676. struct urb *urb = std->urb;
  677. int urb_status;
  678. urb_status = get_urb_status_from_qtd(urb, le32_to_cpu(qtd->status));
  679. qset_remove_qtds(whc, qset, urb);
  680. qset_remove_urb(whc, qset, urb, urb_status);
  681. list_for_each_entry(std, &qset->stds, list_node) {
  682. if (qset->ntds == 0)
  683. break;
  684. qset_remove_qtd(whc, qset);
  685. std->qtd = NULL;
  686. }
  687. qset->remove = 1;
  688. }
  689. void qset_free(struct whc *whc, struct whc_qset *qset)
  690. {
  691. dma_pool_free(whc->qset_pool, qset, qset->qset_dma);
  692. }
  693. /**
  694. * qset_delete - wait for a qset to be unused, then free it.
  695. */
  696. void qset_delete(struct whc *whc, struct whc_qset *qset)
  697. {
  698. wait_for_completion(&qset->remove_complete);
  699. qset_free(whc, qset);
  700. }