USB: Introduce usb_setup_{control,bulk,int}_urb helpers This patch adds usb_setup_{control,bulk,int}_urb helpers that will allocate the transfer buffer and set URB_FREE_BUFFER flag for freeing it when the reference count of an URB goes down to zero. This allows an easy and simple construction of one-shot URBs. Signed-off-by: Marcel Holtmann --- commit 2a3deb451a56722f4943f03e1a2b44a85f3b8db5 tree 21ce3f7992f969dfeb8ee8c9c9adce1c5d9d35d7 parent 189548642c5962e60c3667bdb3a703fe0bed12a6 author Marcel Holtmann Tue, 26 Jun 2007 03:02:33 +0200 committer Marcel Holtmann Tue, 26 Jun 2007 03:02:33 +0200 drivers/usb/core/urb.c | 35 +++++++++++++++++++++++++++++++++++ include/linux/usb.h | 5 +++++ 2 files changed, 40 insertions(+) --- a/drivers/usb/core/urb.c +++ b/drivers/usb/core/urb.c @@ -578,6 +578,41 @@ int usb_wait_anchor_empty_timeout(struct } EXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout); +/** + * usb_alloc_control_urb - function to create and initialize a control urb + * @dev: pointer to the struct usb_device for this urb. + * @pipe: the endpoint pipe + * @setup_packet: pointer to the setup_packet buffer + * @size: requested buffer size + * @mem_flags: affect whether allocation may block + * @complete_fn: pointer to the usb_complete_t function + * @context: what to set the urb context to. + * + * Initializes a control urb and the transfer buffer for the urb, with the + * proper information needed to submit it to a device. A pointer to the urb + * will be returned. If there is an error creating the urb, NULL will be + * returned. + */ +struct urb *usb_alloc_control_urb(struct usb_device *dev, unsigned int pipe, + unsigned char *setup_packet, size_t size, + gfp_t mem_flags, usb_complete_t complete_fn, + void *context) +{ + struct urb *urb; + void *buffer; + + urb = kzalloc(sizeof(*urb) + size, mem_flags); + if (!urb) + return NULL; + buffer = ((unsigned char *)(urb) + size); + + usb_fill_control_urb(urb, dev, pipe, setup_packet, + buffer, size, complete_fn, context); + + return urb; +} +EXPORT_SYMBOL(usb_alloc_control_urb); + EXPORT_SYMBOL(usb_init_urb); EXPORT_SYMBOL(usb_alloc_urb); EXPORT_SYMBOL(usb_free_urb); --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -1334,6 +1334,11 @@ extern void usb_unanchor_urb(struct urb extern int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, unsigned int timeout); +struct urb *usb_alloc_control_urb(struct usb_device *dev, unsigned int pipe, + unsigned char *setup_packet, size_t size, + gfp_t mem_flags, usb_complete_t complete_fn, + void *context); + void *usb_buffer_alloc (struct usb_device *dev, size_t size, gfp_t mem_flags, dma_addr_t *dma); void usb_buffer_free (struct usb_device *dev, size_t size,