ehci-mem.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2001 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * There's basically three types of memory:
  22. * - data used only by the HCD ... kmalloc is fine
  23. * - async and periodic schedules, shared by HC and HCD ... these
  24. * need to use dma_pool or dma_alloc_coherent
  25. * - driver buffers, read/written by HC ... single shot DMA mapped
  26. *
  27. * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
  28. * No memory seen by this driver is pageable.
  29. */
  30. /*-------------------------------------------------------------------------*/
  31. /* Allocate the key transfer structures from the previously allocated pool */
  32. static inline void ehci_qtd_init(struct ehci_hcd *ehci, struct ehci_qtd *qtd,
  33. dma_addr_t dma)
  34. {
  35. memset(qtd, 0, sizeof *qtd);
  36. qtd->qtd_dma = dma;
  37. qtd->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  38. qtd->hw_next = EHCI_LIST_END(ehci);
  39. qtd->hw_alt_next = EHCI_LIST_END(ehci);
  40. INIT_LIST_HEAD(&qtd->qtd_list);
  41. }
  42. static struct ehci_qtd *ehci_qtd_alloc (struct ehci_hcd *ehci, gfp_t flags)
  43. {
  44. struct ehci_qtd *qtd;
  45. dma_addr_t dma;
  46. //qtd = dma_pool_alloc (ehci->qtd_pool, flags, &dma);
  47. qtd = usb_dma_malloc(sizeof(struct ehci_qtd), &dma);
  48. if (qtd != NULL) {
  49. EHCI_DEBUG_PRINTF("qtd virt = %p, phys = 0x%lx", qtd, dma);
  50. ehci_qtd_init(ehci, qtd, dma);
  51. }
  52. return qtd;
  53. }
  54. static inline void ehci_qtd_free (struct ehci_hcd *ehci, struct ehci_qtd *qtd)
  55. {
  56. //dma_pool_free (ehci->qtd_pool, qtd, qtd->qtd_dma);
  57. usb_dma_free(qtd, qtd->qtd_dma);
  58. }
  59. static void qh_destroy(struct ehci_hcd *ehci, struct ehci_qh *qh)
  60. {
  61. /* clean qtds first, and know this is not linked */
  62. if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
  63. ehci_dbg ("unused qh not empty!\n");
  64. }
  65. if (qh->dummy)
  66. ehci_qtd_free (ehci, qh->dummy);
  67. //dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
  68. usb_dma_free((void *)qh->hw, qh->qh_dma);
  69. hal_free(qh);
  70. }
  71. static struct ehci_qh *ehci_qh_alloc (struct ehci_hcd *ehci)
  72. {
  73. struct ehci_qh *qh;
  74. dma_addr_t dma;
  75. qh = hal_malloc(sizeof *qh);
  76. if (!qh)
  77. goto done;
  78. //qh->hw = (struct ehci_qh_hw *)
  79. // dma_pool_alloc(ehci->qh_pool, flags, &dma);
  80. qh->hw = (struct ehci_qh_hw *)
  81. usb_dma_malloc(sizeof(struct ehci_qh_hw), &dma);
  82. if (!qh->hw)
  83. goto fail;
  84. memset(qh->hw, 0, sizeof *qh->hw);
  85. qh->qh_dma = dma;
  86. INIT_LIST_HEAD (&qh->qtd_list);
  87. INIT_LIST_HEAD(&qh->unlink_node);
  88. /* dummy td enables safe urb queuing */
  89. qh->dummy = ehci_qtd_alloc (ehci, 0);
  90. if (qh->dummy == NULL) {
  91. ehci_dbg ("no dummy td\n");
  92. goto fail1;
  93. }
  94. done:
  95. return qh;
  96. fail1:
  97. //dma_pool_free(ehci->qh_pool, qh->hw, qh->qh_dma);
  98. usb_dma_free(qh->hw, qh->qh_dma);
  99. fail:
  100. //kfree(qh);
  101. hal_free(qh);
  102. return NULL;
  103. }
  104. /*-------------------------------------------------------------------------*/
  105. /* The queue heads and transfer descriptors are managed from pools tied
  106. * to each of the "per device" structures.
  107. * This is the initialisation and cleanup code.
  108. */
  109. static void ehci_mem_cleanup (struct ehci_hcd *ehci)
  110. {
  111. if (ehci->async)
  112. qh_destroy(ehci, ehci->async);
  113. ehci->async = NULL;
  114. //if (ehci->dummy)
  115. // qh_destroy(ehci, ehci->dummy);
  116. //ehci->dummy = NULL;
  117. /* DMA consistent memory and pools */
  118. //dma_pool_destroy(ehci->qtd_pool);
  119. //ehci->qtd_pool = NULL;
  120. //dma_pool_destroy(ehci->qh_pool);
  121. //ehci->qh_pool = NULL;
  122. //dma_pool_destroy(ehci->itd_pool);
  123. //ehci->itd_pool = NULL;
  124. //dma_pool_destroy(ehci->sitd_pool);
  125. //ehci->sitd_pool = NULL;
  126. if (ehci->periodic)
  127. usb_dma_free(ehci->periodic, ehci->periodic_dma);
  128. //dma_free_coherent (ehci_to_hcd(ehci)->self.controller,
  129. // ehci->periodic_size * sizeof (u32),
  130. // ehci->periodic, ehci->periodic_dma);
  131. ehci->periodic = NULL;
  132. /* shadow periodic table */
  133. hal_free(ehci->pshadow);
  134. ehci->pshadow = NULL;
  135. }
  136. /* remember to add cleanup code (above) if you add anything here */
  137. static int ehci_mem_init (struct ehci_hcd *ehci)
  138. {
  139. int i;
  140. /* QTDs for control/bulk/intr transfers */
  141. //ehci->qtd_pool = dma_pool_create ("ehci_qtd",
  142. // ehci_to_hcd(ehci)->self.controller,
  143. // sizeof (struct ehci_qtd),
  144. // 32 /* byte alignment (for hw parts) */,
  145. // 4096 /* can't cross 4K */);
  146. //if (!ehci->qtd_pool) {
  147. // goto fail;
  148. //}
  149. ///* QHs for control/bulk/intr transfers */
  150. //ehci->qh_pool = dma_pool_create ("ehci_qh",
  151. // ehci_to_hcd(ehci)->self.controller,
  152. // sizeof(struct ehci_qh_hw),
  153. // 32 /* byte alignment (for hw parts) */,
  154. // 4096 /* can't cross 4K */);
  155. //if (!ehci->qh_pool) {
  156. // goto fail;
  157. //}
  158. ehci->async = ehci_qh_alloc (ehci);
  159. if (!ehci->async) {
  160. goto fail;
  161. }
  162. /* ITD for high speed ISO transfers */
  163. //ehci->itd_pool = dma_pool_create ("ehci_itd",
  164. // ehci_to_hcd(ehci)->self.controller,
  165. // sizeof (struct ehci_itd),
  166. // 32 /* byte alignment (for hw parts) */,
  167. // 4096 /* can't cross 4K */);
  168. //if (!ehci->itd_pool) {
  169. // goto fail;
  170. //}
  171. ///* SITD for full/low speed split ISO transfers */
  172. //ehci->sitd_pool = dma_pool_create ("ehci_sitd",
  173. // ehci_to_hcd(ehci)->self.controller,
  174. // sizeof (struct ehci_sitd),
  175. // 32 /* byte alignment (for hw parts) */,
  176. // 4096 /* can't cross 4K */);
  177. //if (!ehci->sitd_pool) {
  178. // goto fail;
  179. //}
  180. /* Hardware periodic table */
  181. //ehci->periodic = (__le32 *)
  182. // dma_alloc_coherent (ehci_to_hcd(ehci)->self.controller,
  183. // ehci->periodic_size * sizeof(__le32),
  184. // &ehci->periodic_dma, flags);
  185. ehci->periodic = usb_dma_malloc(ehci->periodic_size * sizeof(uint32_t),
  186. (dma_addr_t *)&ehci->periodic_dma);
  187. if (ehci->periodic == NULL) {
  188. goto fail;
  189. }
  190. //if (ehci->use_dummy_qh) {
  191. // struct ehci_qh_hw *hw;
  192. // ehci->dummy = ehci_qh_alloc(ehci, flags);
  193. // if (!ehci->dummy)
  194. // goto fail;
  195. // hw = ehci->dummy->hw;
  196. // hw->hw_next = EHCI_LIST_END(ehci);
  197. // hw->hw_qtd_next = EHCI_LIST_END(ehci);
  198. // hw->hw_alt_next = EHCI_LIST_END(ehci);
  199. // ehci->dummy->hw = hw;
  200. // for (i = 0; i < ehci->periodic_size; i++)
  201. // ehci->periodic[i] = cpu_to_hc32(ehci,
  202. // ehci->dummy->qh_dma);
  203. //} else {
  204. for (i = 0; i < ehci->periodic_size; i++)
  205. ehci->periodic[i] = EHCI_LIST_END(ehci);
  206. //}
  207. /* software shadow of hardware table */
  208. //ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
  209. ehci->pshadow = hal_malloc((ehci->periodic_size) * (sizeof(void *)));
  210. memset(ehci->pshadow, 0, (ehci->periodic_size) * (sizeof(void *)));
  211. if (ehci->pshadow != NULL)
  212. return 0;
  213. fail:
  214. ehci_dbg ("couldn't init memory\n");
  215. ehci_mem_cleanup (ehci);
  216. return -ENOMEM;
  217. }