1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2236dd4d1SAnton Vorontsov /*
3236dd4d1SAnton Vorontsov * Freescale QUICC Engine USB Host Controller Driver
4236dd4d1SAnton Vorontsov *
5236dd4d1SAnton Vorontsov * Copyright (c) Freescale Semicondutor, Inc. 2006.
6236dd4d1SAnton Vorontsov * Shlomi Gridish <gridish@freescale.com>
7236dd4d1SAnton Vorontsov * Jerry Huang <Chang-Ming.Huang@freescale.com>
8236dd4d1SAnton Vorontsov * Copyright (c) Logic Product Development, Inc. 2007
9236dd4d1SAnton Vorontsov * Peter Barada <peterb@logicpd.com>
10236dd4d1SAnton Vorontsov * Copyright (c) MontaVista Software, Inc. 2008.
11236dd4d1SAnton Vorontsov * Anton Vorontsov <avorontsov@ru.mvista.com>
12236dd4d1SAnton Vorontsov */
13236dd4d1SAnton Vorontsov
14236dd4d1SAnton Vorontsov #include <linux/kernel.h>
15236dd4d1SAnton Vorontsov #include <linux/types.h>
16236dd4d1SAnton Vorontsov #include <linux/delay.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18236dd4d1SAnton Vorontsov #include <linux/list.h>
19236dd4d1SAnton Vorontsov #include <linux/usb.h>
2027729aadSEric Lescouet #include <linux/usb/hcd.h>
21236dd4d1SAnton Vorontsov #include "fhci.h"
22236dd4d1SAnton Vorontsov
init_td(struct td * td)23236dd4d1SAnton Vorontsov static void init_td(struct td *td)
24236dd4d1SAnton Vorontsov {
25236dd4d1SAnton Vorontsov memset(td, 0, sizeof(*td));
26236dd4d1SAnton Vorontsov INIT_LIST_HEAD(&td->node);
27236dd4d1SAnton Vorontsov INIT_LIST_HEAD(&td->frame_lh);
28236dd4d1SAnton Vorontsov }
29236dd4d1SAnton Vorontsov
init_ed(struct ed * ed)30236dd4d1SAnton Vorontsov static void init_ed(struct ed *ed)
31236dd4d1SAnton Vorontsov {
32236dd4d1SAnton Vorontsov memset(ed, 0, sizeof(*ed));
33236dd4d1SAnton Vorontsov INIT_LIST_HEAD(&ed->td_list);
34236dd4d1SAnton Vorontsov INIT_LIST_HEAD(&ed->node);
35236dd4d1SAnton Vorontsov }
36236dd4d1SAnton Vorontsov
get_empty_td(struct fhci_hcd * fhci)37236dd4d1SAnton Vorontsov static struct td *get_empty_td(struct fhci_hcd *fhci)
38236dd4d1SAnton Vorontsov {
39236dd4d1SAnton Vorontsov struct td *td;
40236dd4d1SAnton Vorontsov
41236dd4d1SAnton Vorontsov if (!list_empty(&fhci->empty_tds)) {
42236dd4d1SAnton Vorontsov td = list_entry(fhci->empty_tds.next, struct td, node);
43236dd4d1SAnton Vorontsov list_del(fhci->empty_tds.next);
44236dd4d1SAnton Vorontsov } else {
45236dd4d1SAnton Vorontsov td = kmalloc(sizeof(*td), GFP_ATOMIC);
46236dd4d1SAnton Vorontsov if (!td)
47236dd4d1SAnton Vorontsov fhci_err(fhci, "No memory to allocate to TD\n");
48236dd4d1SAnton Vorontsov else
49236dd4d1SAnton Vorontsov init_td(td);
50236dd4d1SAnton Vorontsov }
51236dd4d1SAnton Vorontsov
52236dd4d1SAnton Vorontsov return td;
53236dd4d1SAnton Vorontsov }
54236dd4d1SAnton Vorontsov
fhci_recycle_empty_td(struct fhci_hcd * fhci,struct td * td)55236dd4d1SAnton Vorontsov void fhci_recycle_empty_td(struct fhci_hcd *fhci, struct td *td)
56236dd4d1SAnton Vorontsov {
57236dd4d1SAnton Vorontsov init_td(td);
58236dd4d1SAnton Vorontsov list_add(&td->node, &fhci->empty_tds);
59236dd4d1SAnton Vorontsov }
60236dd4d1SAnton Vorontsov
fhci_get_empty_ed(struct fhci_hcd * fhci)61236dd4d1SAnton Vorontsov struct ed *fhci_get_empty_ed(struct fhci_hcd *fhci)
62236dd4d1SAnton Vorontsov {
63236dd4d1SAnton Vorontsov struct ed *ed;
64236dd4d1SAnton Vorontsov
65236dd4d1SAnton Vorontsov if (!list_empty(&fhci->empty_eds)) {
66236dd4d1SAnton Vorontsov ed = list_entry(fhci->empty_eds.next, struct ed, node);
67236dd4d1SAnton Vorontsov list_del(fhci->empty_eds.next);
68236dd4d1SAnton Vorontsov } else {
69236dd4d1SAnton Vorontsov ed = kmalloc(sizeof(*ed), GFP_ATOMIC);
70236dd4d1SAnton Vorontsov if (!ed)
71236dd4d1SAnton Vorontsov fhci_err(fhci, "No memory to allocate to ED\n");
72236dd4d1SAnton Vorontsov else
73236dd4d1SAnton Vorontsov init_ed(ed);
74236dd4d1SAnton Vorontsov }
75236dd4d1SAnton Vorontsov
76236dd4d1SAnton Vorontsov return ed;
77236dd4d1SAnton Vorontsov }
78236dd4d1SAnton Vorontsov
fhci_recycle_empty_ed(struct fhci_hcd * fhci,struct ed * ed)79236dd4d1SAnton Vorontsov void fhci_recycle_empty_ed(struct fhci_hcd *fhci, struct ed *ed)
80236dd4d1SAnton Vorontsov {
81236dd4d1SAnton Vorontsov init_ed(ed);
82236dd4d1SAnton Vorontsov list_add(&ed->node, &fhci->empty_eds);
83236dd4d1SAnton Vorontsov }
84236dd4d1SAnton Vorontsov
fhci_td_fill(struct fhci_hcd * fhci,struct urb * urb,struct urb_priv * urb_priv,struct ed * ed,u16 index,enum fhci_ta_type type,int toggle,u8 * data,u32 len,u16 interval,u16 start_frame,bool ioc)85236dd4d1SAnton Vorontsov struct td *fhci_td_fill(struct fhci_hcd *fhci, struct urb *urb,
86236dd4d1SAnton Vorontsov struct urb_priv *urb_priv, struct ed *ed, u16 index,
87236dd4d1SAnton Vorontsov enum fhci_ta_type type, int toggle, u8 *data, u32 len,
88236dd4d1SAnton Vorontsov u16 interval, u16 start_frame, bool ioc)
89236dd4d1SAnton Vorontsov {
90236dd4d1SAnton Vorontsov struct td *td = get_empty_td(fhci);
91236dd4d1SAnton Vorontsov
92236dd4d1SAnton Vorontsov if (!td)
93236dd4d1SAnton Vorontsov return NULL;
94236dd4d1SAnton Vorontsov
95236dd4d1SAnton Vorontsov td->urb = urb;
96236dd4d1SAnton Vorontsov td->ed = ed;
97236dd4d1SAnton Vorontsov td->type = type;
98236dd4d1SAnton Vorontsov td->toggle = toggle;
99236dd4d1SAnton Vorontsov td->data = data;
100236dd4d1SAnton Vorontsov td->len = len;
101236dd4d1SAnton Vorontsov td->iso_index = index;
102236dd4d1SAnton Vorontsov td->interval = interval;
103236dd4d1SAnton Vorontsov td->start_frame = start_frame;
104236dd4d1SAnton Vorontsov td->ioc = ioc;
105236dd4d1SAnton Vorontsov td->status = USB_TD_OK;
106236dd4d1SAnton Vorontsov
107236dd4d1SAnton Vorontsov urb_priv->tds[index] = td;
108236dd4d1SAnton Vorontsov
109236dd4d1SAnton Vorontsov return td;
110236dd4d1SAnton Vorontsov }
111