hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/usb/core/buffer.c
....@@ -16,6 +16,7 @@
1616 #include <linux/io.h>
1717 #include <linux/dma-mapping.h>
1818 #include <linux/dmapool.h>
19
+#include <linux/genalloc.h>
1920 #include <linux/usb.h>
2021 #include <linux/usb/hcd.h>
2122
....@@ -65,9 +66,7 @@
6566 char name[16];
6667 int i, size;
6768
68
- if (!IS_ENABLED(CONFIG_HAS_DMA) ||
69
- (!is_device_dma_capable(hcd->self.sysdev) &&
70
- !(hcd->driver->flags & HCD_LOCAL_MEM)))
69
+ if (hcd->localmem_pool || !hcd_uses_dma(hcd))
7170 return 0;
7271
7372 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
....@@ -101,12 +100,8 @@
101100 return;
102101
103102 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
104
- struct dma_pool *pool = hcd->pool[i];
105
-
106
- if (pool) {
107
- dma_pool_destroy(pool);
108
- hcd->pool[i] = NULL;
109
- }
103
+ dma_pool_destroy(hcd->pool[i]);
104
+ hcd->pool[i] = NULL;
110105 }
111106 }
112107
....@@ -128,10 +123,11 @@
128123 if (size == 0)
129124 return NULL;
130125
126
+ if (hcd->localmem_pool)
127
+ return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
128
+
131129 /* some USB hosts just use PIO */
132
- if (!IS_ENABLED(CONFIG_HAS_DMA) ||
133
- (!is_device_dma_capable(bus->sysdev) &&
134
- !(hcd->driver->flags & HCD_LOCAL_MEM))) {
130
+ if (!hcd_uses_dma(hcd)) {
135131 *dma = ~(dma_addr_t) 0;
136132 return kmalloc(size, mem_flags);
137133 }
....@@ -156,9 +152,12 @@
156152 if (!addr)
157153 return;
158154
159
- if (!IS_ENABLED(CONFIG_HAS_DMA) ||
160
- (!is_device_dma_capable(bus->sysdev) &&
161
- !(hcd->driver->flags & HCD_LOCAL_MEM))) {
155
+ if (hcd->localmem_pool) {
156
+ gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
157
+ return;
158
+ }
159
+
160
+ if (!hcd_uses_dma(hcd)) {
162161 kfree(addr);
163162 return;
164163 }
....@@ -171,3 +170,44 @@
171170 }
172171 dma_free_coherent(hcd->self.sysdev, size, addr, dma);
173172 }
173
+
174
+void *hcd_buffer_alloc_pages(struct usb_hcd *hcd,
175
+ size_t size, gfp_t mem_flags, dma_addr_t *dma)
176
+{
177
+ if (size == 0)
178
+ return NULL;
179
+
180
+ if (hcd->localmem_pool)
181
+ return gen_pool_dma_alloc_align(hcd->localmem_pool,
182
+ size, dma, PAGE_SIZE);
183
+
184
+ /* some USB hosts just use PIO */
185
+ if (!hcd_uses_dma(hcd)) {
186
+ *dma = DMA_MAPPING_ERROR;
187
+ return (void *)__get_free_pages(mem_flags,
188
+ get_order(size));
189
+ }
190
+
191
+ return dma_alloc_coherent(hcd->self.sysdev,
192
+ size, dma, mem_flags);
193
+}
194
+
195
+void hcd_buffer_free_pages(struct usb_hcd *hcd,
196
+ size_t size, void *addr, dma_addr_t dma)
197
+{
198
+ if (!addr)
199
+ return;
200
+
201
+ if (hcd->localmem_pool) {
202
+ gen_pool_free(hcd->localmem_pool,
203
+ (unsigned long)addr, size);
204
+ return;
205
+ }
206
+
207
+ if (!hcd_uses_dma(hcd)) {
208
+ free_pages((unsigned long)addr, get_order(size));
209
+ return;
210
+ }
211
+
212
+ dma_free_coherent(hcd->self.sysdev, size, addr, dma);
213
+}