On Tue, Jun 26, 2007 at 03:14:22AM +0200, Marcel Holtmann wrote: > Hi Greg, > > > here is a patch that adds three URB setup helpers that will allocate the > > transfer buffer using kmalloc() and set the new URB_FREE_BUFFER flage to > > make it free together with the URB. Please apply. > > while I was creating this patch, I was thinking about its usefulness. It > will make the drivers more simpler, but still every driver that is gonna > use it has to allocate the URB itself. So why not go one step ahead and > create usb_alloc_{control,bulk,int}_urb helpers that will allocate the > URB, its transfer buffer set the appropriate flags. > > Comments?
You mean something like the following, but also for bulk and int? I like it, it reduces the number of allocations and frees we have to do as the transfer buffer will get freed automatically with the urb, when it is. What do people think? thanks, greg k-h --- 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, ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ linux-usb-devel@lists.sourceforge.net To unsubscribe, use the last form field at: https://lists.sourceforge.net/lists/listinfo/linux-usb-devel