fhci-mem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale QUICC Engine USB Host Controller Driver
  4. *
  5. * Copyright (c) Freescale Semicondutor, Inc. 2006.
  6. * Shlomi Gridish <gridish@freescale.com>
  7. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  8. * Copyright (c) Logic Product Development, Inc. 2007
  9. * Peter Barada <peterb@logicpd.com>
  10. * Copyright (c) MontaVista Software, Inc. 2008.
  11. * Anton Vorontsov <avorontsov@ru.mvista.com>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/hcd.h>
  20. #include "fhci.h"
  21. static void init_td(struct td *td)
  22. {
  23. memset(td, 0, sizeof(*td));
  24. INIT_LIST_HEAD(&td->node);
  25. INIT_LIST_HEAD(&td->frame_lh);
  26. }
  27. static void init_ed(struct ed *ed)
  28. {
  29. memset(ed, 0, sizeof(*ed));
  30. INIT_LIST_HEAD(&ed->td_list);
  31. INIT_LIST_HEAD(&ed->node);
  32. }
  33. static struct td *get_empty_td(struct fhci_hcd *fhci)
  34. {
  35. struct td *td;
  36. if (!list_empty(&fhci->empty_tds)) {
  37. td = list_entry(fhci->empty_tds.next, struct td, node);
  38. list_del(fhci->empty_tds.next);
  39. } else {
  40. td = kmalloc(sizeof(*td), GFP_ATOMIC);
  41. if (!td)
  42. fhci_err(fhci, "No memory to allocate to TD\n");
  43. else
  44. init_td(td);
  45. }
  46. return td;
  47. }
  48. void fhci_recycle_empty_td(struct fhci_hcd *fhci, struct td *td)
  49. {
  50. init_td(td);
  51. list_add(&td->node, &fhci->empty_tds);
  52. }
  53. struct ed *fhci_get_empty_ed(struct fhci_hcd *fhci)
  54. {
  55. struct ed *ed;
  56. if (!list_empty(&fhci->empty_eds)) {
  57. ed = list_entry(fhci->empty_eds.next, struct ed, node);
  58. list_del(fhci->empty_eds.next);
  59. } else {
  60. ed = kmalloc(sizeof(*ed), GFP_ATOMIC);
  61. if (!ed)
  62. fhci_err(fhci, "No memory to allocate to ED\n");
  63. else
  64. init_ed(ed);
  65. }
  66. return ed;
  67. }
  68. void fhci_recycle_empty_ed(struct fhci_hcd *fhci, struct ed *ed)
  69. {
  70. init_ed(ed);
  71. list_add(&ed->node, &fhci->empty_eds);
  72. }
  73. struct td *fhci_td_fill(struct fhci_hcd *fhci, struct urb *urb,
  74. struct urb_priv *urb_priv, struct ed *ed, u16 index,
  75. enum fhci_ta_type type, int toggle, u8 *data, u32 len,
  76. u16 interval, u16 start_frame, bool ioc)
  77. {
  78. struct td *td = get_empty_td(fhci);
  79. if (!td)
  80. return NULL;
  81. td->urb = urb;
  82. td->ed = ed;
  83. td->type = type;
  84. td->toggle = toggle;
  85. td->data = data;
  86. td->len = len;
  87. td->iso_index = index;
  88. td->interval = interval;
  89. td->start_frame = start_frame;
  90. td->ioc = ioc;
  91. td->status = USB_TD_OK;
  92. urb_priv->tds[index] = td;
  93. return td;
  94. }