0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/device.h>
0015 #include <linux/mm.h>
0016 #include <linux/io.h>
0017 #include <linux/dma-mapping.h>
0018 #include <linux/dmapool.h>
0019 #include <linux/genalloc.h>
0020 #include <linux/usb.h>
0021 #include <linux/usb/hcd.h>
0022
0023
0024
0025
0026
0027
0028
0029 static size_t pool_max[HCD_BUFFER_POOLS] = {
0030 32, 128, 512, 2048,
0031 };
0032
0033 void __init usb_init_pool_max(void)
0034 {
0035
0036
0037
0038
0039 if (ARCH_KMALLOC_MINALIGN <= 32)
0040 ;
0041 else if (ARCH_KMALLOC_MINALIGN <= 64)
0042 pool_max[0] = 64;
0043 else if (ARCH_KMALLOC_MINALIGN <= 128)
0044 pool_max[0] = 0;
0045 else
0046 BUILD_BUG();
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 int hcd_buffer_create(struct usb_hcd *hcd)
0066 {
0067 char name[16];
0068 int i, size;
0069
0070 if (hcd->localmem_pool || !hcd_uses_dma(hcd))
0071 return 0;
0072
0073 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0074 size = pool_max[i];
0075 if (!size)
0076 continue;
0077 snprintf(name, sizeof(name), "buffer-%d", size);
0078 hcd->pool[i] = dma_pool_create(name, hcd->self.sysdev,
0079 size, size, 0);
0080 if (!hcd->pool[i]) {
0081 hcd_buffer_destroy(hcd);
0082 return -ENOMEM;
0083 }
0084 }
0085 return 0;
0086 }
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 void hcd_buffer_destroy(struct usb_hcd *hcd)
0098 {
0099 int i;
0100
0101 if (!IS_ENABLED(CONFIG_HAS_DMA))
0102 return;
0103
0104 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0105 dma_pool_destroy(hcd->pool[i]);
0106 hcd->pool[i] = NULL;
0107 }
0108 }
0109
0110
0111
0112
0113
0114
0115 void *hcd_buffer_alloc(
0116 struct usb_bus *bus,
0117 size_t size,
0118 gfp_t mem_flags,
0119 dma_addr_t *dma
0120 )
0121 {
0122 struct usb_hcd *hcd = bus_to_hcd(bus);
0123 int i;
0124
0125 if (size == 0)
0126 return NULL;
0127
0128 if (hcd->localmem_pool)
0129 return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
0130
0131
0132 if (!hcd_uses_dma(hcd)) {
0133 *dma = ~(dma_addr_t) 0;
0134 return kmalloc(size, mem_flags);
0135 }
0136
0137 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0138 if (size <= pool_max[i])
0139 return dma_pool_alloc(hcd->pool[i], mem_flags, dma);
0140 }
0141 return dma_alloc_coherent(hcd->self.sysdev, size, dma, mem_flags);
0142 }
0143
0144 void hcd_buffer_free(
0145 struct usb_bus *bus,
0146 size_t size,
0147 void *addr,
0148 dma_addr_t dma
0149 )
0150 {
0151 struct usb_hcd *hcd = bus_to_hcd(bus);
0152 int i;
0153
0154 if (!addr)
0155 return;
0156
0157 if (hcd->localmem_pool) {
0158 gen_pool_free(hcd->localmem_pool, (unsigned long)addr, size);
0159 return;
0160 }
0161
0162 if (!hcd_uses_dma(hcd)) {
0163 kfree(addr);
0164 return;
0165 }
0166
0167 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
0168 if (size <= pool_max[i]) {
0169 dma_pool_free(hcd->pool[i], addr, dma);
0170 return;
0171 }
0172 }
0173 dma_free_coherent(hcd->self.sysdev, size, addr, dma);
0174 }