ohci-mem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  6. *
  7. * This file is licenced under the GPL.
  8. */
  9. /*-------------------------------------------------------------------------*/
  10. /*
  11. * There's basically three types of memory:
  12. * - data used only by the HCD ... kmalloc is fine
  13. * - async and periodic schedules, shared by HC and HCD ... these
  14. * need to use dma_pool or dma_alloc_coherent
  15. * - driver buffers, read/written by HC ... the hcd glue or the
  16. * device driver provides us with dma addresses
  17. *
  18. * There's also PCI "register" data, which is memory mapped.
  19. * No memory seen by this driver is pagable.
  20. */
  21. /*-------------------------------------------------------------------------*/
  22. static void ohci_hcd_init (struct ohci_hcd *ohci)
  23. {
  24. ohci->next_statechange = jiffies;
  25. spin_lock_init (&ohci->lock);
  26. INIT_LIST_HEAD (&ohci->pending);
  27. INIT_WORK (&ohci->rh_resume, ohci_rh_resume, ohci_to_hcd(ohci));
  28. ohci->reboot_notifier.notifier_call = ohci_reboot;
  29. }
  30. /*-------------------------------------------------------------------------*/
  31. static int ohci_mem_init (struct ohci_hcd *ohci)
  32. {
  33. ohci->td_cache = dma_pool_create ("ohci_td",
  34. ohci_to_hcd(ohci)->self.controller,
  35. sizeof (struct td),
  36. 32 /* byte alignment */,
  37. 0 /* no page-crossing issues */);
  38. if (!ohci->td_cache)
  39. return -ENOMEM;
  40. ohci->ed_cache = dma_pool_create ("ohci_ed",
  41. ohci_to_hcd(ohci)->self.controller,
  42. sizeof (struct ed),
  43. 16 /* byte alignment */,
  44. 0 /* no page-crossing issues */);
  45. if (!ohci->ed_cache) {
  46. dma_pool_destroy (ohci->td_cache);
  47. return -ENOMEM;
  48. }
  49. return 0;
  50. }
  51. static void ohci_mem_cleanup (struct ohci_hcd *ohci)
  52. {
  53. if (ohci->td_cache) {
  54. dma_pool_destroy (ohci->td_cache);
  55. ohci->td_cache = NULL;
  56. }
  57. if (ohci->ed_cache) {
  58. dma_pool_destroy (ohci->ed_cache);
  59. ohci->ed_cache = NULL;
  60. }
  61. }
  62. /*-------------------------------------------------------------------------*/
  63. /* ohci "done list" processing needs this mapping */
  64. static inline struct td *
  65. dma_to_td (struct ohci_hcd *hc, dma_addr_t td_dma)
  66. {
  67. struct td *td;
  68. td_dma &= TD_MASK;
  69. td = hc->td_hash [TD_HASH_FUNC(td_dma)];
  70. while (td && td->td_dma != td_dma)
  71. td = td->td_hash;
  72. return td;
  73. }
  74. /* TDs ... */
  75. static struct td *
  76. td_alloc (struct ohci_hcd *hc, unsigned mem_flags)
  77. {
  78. dma_addr_t dma;
  79. struct td *td;
  80. td = dma_pool_alloc (hc->td_cache, mem_flags, &dma);
  81. if (td) {
  82. /* in case hc fetches it, make it look dead */
  83. memset (td, 0, sizeof *td);
  84. td->hwNextTD = cpu_to_hc32 (hc, dma);
  85. td->td_dma = dma;
  86. /* hashed in td_fill */
  87. }
  88. return td;
  89. }
  90. static void
  91. td_free (struct ohci_hcd *hc, struct td *td)
  92. {
  93. struct td **prev = &hc->td_hash [TD_HASH_FUNC (td->td_dma)];
  94. while (*prev && *prev != td)
  95. prev = &(*prev)->td_hash;
  96. if (*prev)
  97. *prev = td->td_hash;
  98. else if ((td->hwINFO & cpu_to_hc32(hc, TD_DONE)) != 0)
  99. ohci_dbg (hc, "no hash for td %p\n", td);
  100. dma_pool_free (hc->td_cache, td, td->td_dma);
  101. }
  102. /*-------------------------------------------------------------------------*/
  103. /* EDs ... */
  104. static struct ed *
  105. ed_alloc (struct ohci_hcd *hc, unsigned mem_flags)
  106. {
  107. dma_addr_t dma;
  108. struct ed *ed;
  109. ed = dma_pool_alloc (hc->ed_cache, mem_flags, &dma);
  110. if (ed) {
  111. memset (ed, 0, sizeof (*ed));
  112. INIT_LIST_HEAD (&ed->td_list);
  113. ed->dma = dma;
  114. }
  115. return ed;
  116. }
  117. static void
  118. ed_free (struct ohci_hcd *hc, struct ed *ed)
  119. {
  120. dma_pool_free (hc->ed_cache, ed, ed->dma);
  121. }