Commit 1efd54ea authored by Andrzej Pietrasiewicz's avatar Andrzej Pietrasiewicz Committed by Felipe Balbi

usb: gadget: factor out alloc_ep_req

alloc_ep_req() is a function repeated in several modules.
Make a common implementation and use it.
Signed-off-by: default avatarAndrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: default avatarKyungmin Park <kyungmin.park@samsung.com>
Acked-by: default avatarMichal Nazarewicz <mina86@mina86.com>
Signed-off-by: default avatarFelipe Balbi <balbi@ti.com>
parent a01091e5
...@@ -7,7 +7,7 @@ ccflags-$(CONFIG_USB_GADGET_VERBOSE) += -DVERBOSE_DEBUG ...@@ -7,7 +7,7 @@ ccflags-$(CONFIG_USB_GADGET_VERBOSE) += -DVERBOSE_DEBUG
obj-$(CONFIG_USB_GADGET) += udc-core.o obj-$(CONFIG_USB_GADGET) += udc-core.o
obj-$(CONFIG_USB_LIBCOMPOSITE) += libcomposite.o obj-$(CONFIG_USB_LIBCOMPOSITE) += libcomposite.o
libcomposite-y := usbstring.o config.o epautoconf.o libcomposite-y := usbstring.o config.o epautoconf.o
libcomposite-y += composite.o functions.o configfs.o libcomposite-y += composite.o functions.o configfs.o u_f.o
obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o
obj-$(CONFIG_USB_NET2272) += net2272.o obj-$(CONFIG_USB_NET2272) += net2272.o
obj-$(CONFIG_USB_NET2280) += net2280.o obj-$(CONFIG_USB_NET2280) += net2280.o
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/usb/g_hid.h> #include <linux/usb/g_hid.h>
#include "u_f.h"
static int major, minors; static int major, minors;
static struct class *hidg_class; static struct class *hidg_class;
...@@ -334,20 +336,10 @@ static int f_hidg_open(struct inode *inode, struct file *fd) ...@@ -334,20 +336,10 @@ static int f_hidg_open(struct inode *inode, struct file *fd)
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/* usb_function */ /* usb_function */
static struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, unsigned length) static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
unsigned length)
{ {
struct usb_request *req; return alloc_ep_req(ep, length, length);
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
if (req) {
req->length = length;
req->buf = kmalloc(length, GFP_ATOMIC);
if (!req->buf) {
usb_ep_free_request(ep, req);
req = NULL;
}
}
return req;
} }
static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <linux/usb/composite.h> #include <linux/usb/composite.h>
#include "g_zero.h" #include "g_zero.h"
#include "u_f.h"
/* /*
* LOOPBACK FUNCTION ... a testing vehicle for USB peripherals, * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
...@@ -293,6 +294,11 @@ static void disable_loopback(struct f_loopback *loop) ...@@ -293,6 +294,11 @@ static void disable_loopback(struct f_loopback *loop)
VDBG(cdev, "%s disabled\n", loop->function.name); VDBG(cdev, "%s disabled\n", loop->function.name);
} }
static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
{
return alloc_ep_req(ep, len, buflen);
}
static int static int
enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop) enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
{ {
...@@ -332,7 +338,7 @@ enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop) ...@@ -332,7 +338,7 @@ enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
* than 'buflen' bytes each. * than 'buflen' bytes each.
*/ */
for (i = 0; i < qlen && result == 0; i++) { for (i = 0; i < qlen && result == 0; i++) {
req = alloc_ep_req(ep, 0); req = lb_alloc_ep_req(ep, 0);
if (req) { if (req) {
req->complete = loopback_complete; req->complete = loopback_complete;
result = usb_ep_queue(ep, req, GFP_ATOMIC); result = usb_ep_queue(ep, req, GFP_ATOMIC);
......
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include <linux/usb/audio.h> #include <linux/usb/audio.h>
#include <linux/usb/midi.h> #include <linux/usb/midi.h>
#include "u_f.h"
MODULE_AUTHOR("Ben Williamson"); MODULE_AUTHOR("Ben Williamson");
MODULE_LICENSE("GPL v2"); MODULE_LICENSE("GPL v2");
...@@ -191,20 +193,10 @@ static struct usb_gadget_strings *midi_strings[] = { ...@@ -191,20 +193,10 @@ static struct usb_gadget_strings *midi_strings[] = {
NULL, NULL,
}; };
static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
unsigned length)
{ {
struct usb_request *req; return alloc_ep_req(ep, length, length);
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
if (req) {
req->length = length;
req->buf = kmalloc(length, GFP_ATOMIC);
if (!req->buf) {
usb_ep_free_request(ep, req);
req = NULL;
}
}
return req;
} }
static void free_ep_req(struct usb_ep *ep, struct usb_request *req) static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
...@@ -365,7 +357,7 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) ...@@ -365,7 +357,7 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
/* allocate a bunch of read buffers and queue them all at once. */ /* allocate a bunch of read buffers and queue them all at once. */
for (i = 0; i < midi->qlen && err == 0; i++) { for (i = 0; i < midi->qlen && err == 0; i++) {
struct usb_request *req = struct usb_request *req =
alloc_ep_req(midi->out_ep, midi->buflen); midi_alloc_ep_req(midi->out_ep, midi->buflen);
if (req == NULL) if (req == NULL)
return -ENOMEM; return -ENOMEM;
...@@ -546,7 +538,7 @@ static void f_midi_transmit(struct f_midi *midi, struct usb_request *req) ...@@ -546,7 +538,7 @@ static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
return; return;
if (!req) if (!req)
req = alloc_ep_req(ep, midi->buflen); req = midi_alloc_ep_req(ep, midi->buflen);
if (!req) { if (!req) {
ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n"); ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "g_zero.h" #include "g_zero.h"
#include "gadget_chips.h" #include "gadget_chips.h"
#include "u_f.h"
/* /*
* SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
...@@ -301,23 +302,9 @@ static struct usb_gadget_strings *sourcesink_strings[] = { ...@@ -301,23 +302,9 @@ static struct usb_gadget_strings *sourcesink_strings[] = {
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
struct usb_request *alloc_ep_req(struct usb_ep *ep, int len) static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
{ {
struct usb_request *req; return alloc_ep_req(ep, len, buflen);
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
if (req) {
if (len)
req->length = len;
else
req->length = buflen;
req->buf = kmalloc(req->length, GFP_ATOMIC);
if (!req->buf) {
usb_ep_free_request(ep, req);
req = NULL;
}
}
return req;
} }
void free_ep_req(struct usb_ep *ep, struct usb_request *req) void free_ep_req(struct usb_ep *ep, struct usb_request *req)
...@@ -628,10 +615,10 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, ...@@ -628,10 +615,10 @@ static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
break; break;
} }
ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
req = alloc_ep_req(ep, size); req = ss_alloc_ep_req(ep, size);
} else { } else {
ep = is_in ? ss->in_ep : ss->out_ep; ep = is_in ? ss->in_ep : ss->out_ep;
req = alloc_ep_req(ep, 0); req = ss_alloc_ep_req(ep, 0);
} }
if (!req) if (!req)
......
...@@ -36,7 +36,6 @@ void lb_modexit(void); ...@@ -36,7 +36,6 @@ void lb_modexit(void);
int lb_modinit(void); int lb_modinit(void);
/* common utilities */ /* common utilities */
struct usb_request *alloc_ep_req(struct usb_ep *ep, int len);
void free_ep_req(struct usb_ep *ep, struct usb_request *req); void free_ep_req(struct usb_ep *ep, struct usb_request *req);
void disable_endpoints(struct usb_composite_dev *cdev, void disable_endpoints(struct usb_composite_dev *cdev,
struct usb_ep *in, struct usb_ep *out, struct usb_ep *in, struct usb_ep *out,
......
/*
* u_f.c -- USB function utilities for Gadget stack
*
* Copyright (c) 2013 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/usb/gadget.h>
#include "u_f.h"
struct usb_request *alloc_ep_req(struct usb_ep *ep, int len, int default_len)
{
struct usb_request *req;
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
if (req) {
req->length = len ?: default_len;
req->buf = kmalloc(req->length, GFP_ATOMIC);
if (!req->buf) {
usb_ep_free_request(ep, req);
req = NULL;
}
}
return req;
}
EXPORT_SYMBOL(alloc_ep_req);
/*
* u_f.h
*
* Utility definitions for USB functions
*
* Copyright (c) 2013 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifndef __U_F_H__
#define __U_F_H__
struct usb_ep;
struct usb_request;
struct usb_request *alloc_ep_req(struct usb_ep *ep, int len, int default_len);
#endif /* __U_F_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment