xref: /openbmc/linux/drivers/usb/host/fhci-mem.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
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  * This program is free software; you can redistribute  it and/or modify it
14  * under  the terms of  the GNU General  Public License as published by the
15  * Free Software Foundation;  either version 2 of the  License, or (at your
16  * option) any later version.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/list.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26 #include "fhci.h"
27 
28 static void init_td(struct td *td)
29 {
30 	memset(td, 0, sizeof(*td));
31 	INIT_LIST_HEAD(&td->node);
32 	INIT_LIST_HEAD(&td->frame_lh);
33 }
34 
35 static void init_ed(struct ed *ed)
36 {
37 	memset(ed, 0, sizeof(*ed));
38 	INIT_LIST_HEAD(&ed->td_list);
39 	INIT_LIST_HEAD(&ed->node);
40 }
41 
42 static struct td *get_empty_td(struct fhci_hcd *fhci)
43 {
44 	struct td *td;
45 
46 	if (!list_empty(&fhci->empty_tds)) {
47 		td = list_entry(fhci->empty_tds.next, struct td, node);
48 		list_del(fhci->empty_tds.next);
49 	} else {
50 		td = kmalloc(sizeof(*td), GFP_ATOMIC);
51 		if (!td)
52 			fhci_err(fhci, "No memory to allocate to TD\n");
53 		else
54 			init_td(td);
55 	}
56 
57 	return td;
58 }
59 
60 void fhci_recycle_empty_td(struct fhci_hcd *fhci, struct td *td)
61 {
62 	init_td(td);
63 	list_add(&td->node, &fhci->empty_tds);
64 }
65 
66 struct ed *fhci_get_empty_ed(struct fhci_hcd *fhci)
67 {
68 	struct ed *ed;
69 
70 	if (!list_empty(&fhci->empty_eds)) {
71 		ed = list_entry(fhci->empty_eds.next, struct ed, node);
72 		list_del(fhci->empty_eds.next);
73 	} else {
74 		ed = kmalloc(sizeof(*ed), GFP_ATOMIC);
75 		if (!ed)
76 			fhci_err(fhci, "No memory to allocate to ED\n");
77 		else
78 			init_ed(ed);
79 	}
80 
81 	return ed;
82 }
83 
84 void fhci_recycle_empty_ed(struct fhci_hcd *fhci, struct ed *ed)
85 {
86 	init_ed(ed);
87 	list_add(&ed->node, &fhci->empty_eds);
88 }
89 
90 struct td *fhci_td_fill(struct fhci_hcd *fhci, struct urb *urb,
91 			struct urb_priv *urb_priv, struct ed *ed, u16 index,
92 			enum fhci_ta_type type, int toggle, u8 *data, u32 len,
93 			u16 interval, u16 start_frame, bool ioc)
94 {
95 	struct td *td = get_empty_td(fhci);
96 
97 	if (!td)
98 		return NULL;
99 
100 	td->urb = urb;
101 	td->ed = ed;
102 	td->type = type;
103 	td->toggle = toggle;
104 	td->data = data;
105 	td->len = len;
106 	td->iso_index = index;
107 	td->interval = interval;
108 	td->start_frame = start_frame;
109 	td->ioc = ioc;
110 	td->status = USB_TD_OK;
111 
112 	urb_priv->tds[index] = td;
113 
114 	return td;
115 }
116