qset.c 21 KB

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