History

usb: add a hcd_uses_dma helper

The USB buffer allocation code is the only place in the usb core (and in fact the whole kernel) that uses is_device_dma_capable, while the URB mapping code uses the uses_dma flag in struct usb_bus. Switch the buffer allocation to use the uses_dma flag used by the rest of the USB code, and create a helper in hcd.h that checks this flag as well as the CONFIG_HAS_DMA to simplify the caller a bit.

Christoph Hellwig committed 7 years ago

Commit edfbcb31 parent dd3ecf15 files · +10 −11Browse files

Unified · Split

5 files
  1. drivers/usb/core/buffer.c +3 −7
  2. drivers/usb/core/hcd.c +2 −2
  3. drivers/usb/dwc2/hcd.c +1 −1
  4. include/linux/usb.h +1 −1
  5. include/linux/usb/hcd.h +3 −0
drivers/usb/core/buffer.c+3−7
@@ -66,9 +66,7 @@ int hcd_buffer_create(struct usb_hcd *hcd) · 65 unmodified lines
66 char name[16];66 char name[16];
67 int i, size;67 int i, size;
68 68
69 if (hcd->localmem_pool ||69 if (hcd->localmem_pool || !hcd_uses_dma(hcd))
70 !IS_ENABLED(CONFIG_HAS_DMA) ||
71 !is_device_dma_capable(hcd->self.sysdev))
72 return 0;70 return 0;
73 71
74 for (i = 0; i < HCD_BUFFER_POOLS; i++) {72 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
@@ -129,8 +127,7 @@ void *hcd_buffer_alloc( · 54 unmodified lines
129 return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);127 return gen_pool_dma_alloc(hcd->localmem_pool, size, dma);
130 128
131 /* some USB hosts just use PIO */129 /* some USB hosts just use PIO */
132 if (!IS_ENABLED(CONFIG_HAS_DMA) ||130 if (!hcd_uses_dma(hcd)) {
133 !is_device_dma_capable(bus->sysdev)) {
134 *dma = ~(dma_addr_t) 0;131 *dma = ~(dma_addr_t) 0;
135 return kmalloc(size, mem_flags);132 return kmalloc(size, mem_flags);
136 }133 }
@@ -160,8 +157,7 @@ void hcd_buffer_free( · 23 unmodified lines
160 return;157 return;
161 }158 }
162 159
163 if (!IS_ENABLED(CONFIG_HAS_DMA) ||160 if (!hcd_uses_dma(hcd)) {
164 !is_device_dma_capable(bus->sysdev)) {
165 kfree(addr);161 kfree(addr);
166 return;162 return;
167 }163 }
drivers/usb/core/hcd.c+2−2
@@ -1412,7 +1412,7 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · 1411 unmodified lines
1412 if (usb_endpoint_xfer_control(&urb->ep->desc)) {1412 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1413 if (hcd->self.uses_pio_for_control)1413 if (hcd->self.uses_pio_for_control)
1414 return ret;1414 return ret;
1415 if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {1415 if (hcd_uses_dma(hcd)) {
1416 if (is_vmalloc_addr(urb->setup_packet)) {1416 if (is_vmalloc_addr(urb->setup_packet)) {
1417 WARN_ONCE(1, "setup packet is not dma capable\n");1417 WARN_ONCE(1, "setup packet is not dma capable\n");
1418 return -EAGAIN;1418 return -EAGAIN;
@@ -1446,7 +1446,7 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, · 27 unmodified lines
1446 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;1446 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1447 if (urb->transfer_buffer_length != 01447 if (urb->transfer_buffer_length != 0
1448 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {1448 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1449 if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {1449 if (hcd_uses_dma(hcd)) {
1450 if (urb->num_sgs) {1450 if (urb->num_sgs) {
1451 int n;1451 int n;
1452 1452
drivers/usb/dwc2/hcd.c+1−1
@@ -4608,7 +4608,7 @@ static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, · 4607 unmodified lines
4608 4608
4609 buf = urb->transfer_buffer;4609 buf = urb->transfer_buffer;
4610 4610
4611 if (hcd->self.uses_dma) {4611 if (hcd_uses_dma(hcd)) {
4612 if (!buf && (urb->transfer_dma & 3)) {4612 if (!buf && (urb->transfer_dma & 3)) {
4613 dev_err(hsotg->dev,4613 dev_err(hsotg->dev,
4614 "%s: unaligned transfer with no transfer_buffer",4614 "%s: unaligned transfer with no transfer_buffer",
include/linux/usb.h+1−1
@@ -1457,7 +1457,7 @@ typedef void (*usb_complete_t)(struct urb *); · 1456 unmodified lines
1457 * field rather than determining a dma address themselves.1457 * field rather than determining a dma address themselves.
1458 *1458 *
1459 * Note that transfer_buffer must still be set if the controller1459 * Note that transfer_buffer must still be set if the controller
1460 * does not support DMA (as indicated by bus.uses_dma) and when talking1460 * does not support DMA (as indicated by hcd_uses_dma()) and when talking
1461 * to root hub. If you have to trasfer between highmem zone and the device1461 * to root hub. If you have to trasfer between highmem zone and the device
1462 * on such controller, create a bounce buffer or bail out with an error.1462 * on such controller, create a bounce buffer or bail out with an error.
1463 * If transfer_buffer cannot be set (is in highmem) and the controller is DMA1463 * If transfer_buffer cannot be set (is in highmem) and the controller is DMA
include/linux/usb/hcd.h+3−0
@@ -422,6 +422,9 @@ static inline bool hcd_periodic_completion_in_progress(struct usb_hcd *hcd, · 421 unmodified lines
422 return hcd->high_prio_bh.completing_ep == ep;422 return hcd->high_prio_bh.completing_ep == ep;
423}423}
424 424
425#define hcd_uses_dma(hcd) \
426 (IS_ENABLED(CONFIG_HAS_DMA) && (hcd)->self.uses_dma)
427
425extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb);428extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb);
426extern int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,429extern int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
427 int status);430 int status);