usb: gadget: android: Update android gadget driver

* Functions and the device descriptor are configured from user space:
echo 0       > /sys/class/android_usb/android0/enable
echo adb,acm > /sys/class/android_usb/android0/functions
echo 2       > /sys/class/android_usb/android0/f_acm/instances
echo 1       > /sys/class/android_usb/android0/enable

* Driver does not require platform data anymore

* Moved function initialization to android.c instead of each
function file

* Replaced switches by uevents

Signed-off-by: Benoit Goby <benoit@android.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>
Change-Id: If5ad9267c111ad0a442f0d87a0d31082dc5381b6
diff --git a/drivers/usb/gadget/f_adb.c b/drivers/usb/gadget/f_adb.c
index 7cfa39c..fe4455e 100644
--- a/drivers/usb/gadget/f_adb.c
+++ b/drivers/usb/gadget/f_adb.c
@@ -15,9 +15,6 @@
  *
  */
 
-/* #define DEBUG */
-/* #define VERBOSE_DEBUG */
-
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/poll.h>
@@ -30,14 +27,12 @@
 #include <linux/device.h>
 #include <linux/miscdevice.h>
 
-#include <linux/usb/android_composite.h>
-
-#define BULK_BUFFER_SIZE           4096
+#define ADB_BULK_BUFFER_SIZE           4096
 
 /* number of tx requests to allocate */
 #define TX_REQ_MAX 4
 
-static const char shortname[] = "android_adb";
+static const char adb_shortname[] = "android_adb";
 
 struct adb_dev {
 	struct usb_function function;
@@ -120,9 +115,7 @@
 /* temporary variable used between adb_open() and adb_gadget_bind() */
 static struct adb_dev *_adb_dev;
 
-static atomic_t adb_enable_excl;
-
-static inline struct adb_dev *func_to_dev(struct usb_function *f)
+static inline struct adb_dev *func_to_adb(struct usb_function *f)
 {
 	return container_of(f, struct adb_dev, function);
 }
@@ -152,7 +145,7 @@
 	}
 }
 
-static inline int _lock(atomic_t *excl)
+static inline int adb_lock(atomic_t *excl)
 {
 	if (atomic_inc_return(excl) == 1) {
 		return 0;
@@ -162,13 +155,13 @@
 	}
 }
 
-static inline void _unlock(atomic_t *excl)
+static inline void adb_unlock(atomic_t *excl)
 {
 	atomic_dec(excl);
 }
 
 /* add a request to the tail of a list */
-void req_put(struct adb_dev *dev, struct list_head *head,
+void adb_req_put(struct adb_dev *dev, struct list_head *head,
 		struct usb_request *req)
 {
 	unsigned long flags;
@@ -179,7 +172,7 @@
 }
 
 /* remove a request from the head of a list */
-struct usb_request *req_get(struct adb_dev *dev, struct list_head *head)
+struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
 {
 	unsigned long flags;
 	struct usb_request *req;
@@ -202,7 +195,7 @@
 	if (req->status != 0)
 		dev->error = 1;
 
-	req_put(dev, &dev->tx_idle, req);
+	adb_req_put(dev, &dev->tx_idle, req);
 
 	wake_up(&dev->write_wq);
 }
@@ -218,7 +211,7 @@
 	wake_up(&dev->read_wq);
 }
 
-static int __init create_bulk_endpoints(struct adb_dev *dev,
+static int adb_create_bulk_endpoints(struct adb_dev *dev,
 				struct usb_endpoint_descriptor *in_desc,
 				struct usb_endpoint_descriptor *out_desc)
 {
@@ -248,18 +241,18 @@
 	dev->ep_out = ep;
 
 	/* now allocate requests for our endpoints */
-	req = adb_request_new(dev->ep_out, BULK_BUFFER_SIZE);
+	req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
 	if (!req)
 		goto fail;
 	req->complete = adb_complete_out;
 	dev->rx_req = req;
 
 	for (i = 0; i < TX_REQ_MAX; i++) {
-		req = adb_request_new(dev->ep_in, BULK_BUFFER_SIZE);
+		req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
 		if (!req)
 			goto fail;
 		req->complete = adb_complete_in;
-		req_put(dev, &dev->tx_idle, req);
+		adb_req_put(dev, &dev->tx_idle, req);
 	}
 
 	return 0;
@@ -273,26 +266,27 @@
 				size_t count, loff_t *pos)
 {
 	struct adb_dev *dev = fp->private_data;
-	struct usb_composite_dev *cdev = dev->cdev;
 	struct usb_request *req;
 	int r = count, xfer;
 	int ret;
 
-	DBG(cdev, "adb_read(%d)\n", count);
+	pr_debug("adb_read(%d)\n", count);
+	if (!_adb_dev)
+		return -ENODEV;
 
-	if (count > BULK_BUFFER_SIZE)
+	if (count > ADB_BULK_BUFFER_SIZE)
 		return -EINVAL;
 
-	if (_lock(&dev->read_excl))
+	if (adb_lock(&dev->read_excl))
 		return -EBUSY;
 
 	/* we will block until we're online */
 	while (!(dev->online || dev->error)) {
-		DBG(cdev, "adb_read: waiting for online state\n");
+		pr_debug("adb_read: waiting for online state\n");
 		ret = wait_event_interruptible(dev->read_wq,
 				(dev->online || dev->error));
 		if (ret < 0) {
-			_unlock(&dev->read_excl);
+			adb_unlock(&dev->read_excl);
 			return ret;
 		}
 	}
@@ -308,12 +302,12 @@
 	dev->rx_done = 0;
 	ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
 	if (ret < 0) {
-		DBG(cdev, "adb_read: failed to queue req %p (%d)\n", req, ret);
+		pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
 		r = -EIO;
 		dev->error = 1;
 		goto done;
 	} else {
-		DBG(cdev, "rx %p queue\n", req);
+		pr_debug("rx %p queue\n", req);
 	}
 
 	/* wait for a request to complete */
@@ -329,16 +323,17 @@
 		if (req->actual == 0)
 			goto requeue_req;
 
-		DBG(cdev, "rx %p %d\n", req, req->actual);
+		pr_debug("rx %p %d\n", req, req->actual);
 		xfer = (req->actual < count) ? req->actual : count;
 		if (copy_to_user(buf, req->buf, xfer))
 			r = -EFAULT;
+
 	} else
 		r = -EIO;
 
 done:
-	_unlock(&dev->read_excl);
-	DBG(cdev, "adb_read returning %d\n", r);
+	adb_unlock(&dev->read_excl);
+	pr_debug("adb_read returning %d\n", r);
 	return r;
 }
 
@@ -346,19 +341,20 @@
 				 size_t count, loff_t *pos)
 {
 	struct adb_dev *dev = fp->private_data;
-	struct usb_composite_dev *cdev = dev->cdev;
 	struct usb_request *req = 0;
 	int r = count, xfer;
 	int ret;
 
-	DBG(cdev, "adb_write(%d)\n", count);
+	if (!_adb_dev)
+		return -ENODEV;
+	pr_debug("adb_write(%d)\n", count);
 
-	if (_lock(&dev->write_excl))
+	if (adb_lock(&dev->write_excl))
 		return -EBUSY;
 
 	while (count > 0) {
 		if (dev->error) {
-			DBG(cdev, "adb_write dev->error\n");
+			pr_debug("adb_write dev->error\n");
 			r = -EIO;
 			break;
 		}
@@ -366,7 +362,7 @@
 		/* get an idle tx request to use */
 		req = 0;
 		ret = wait_event_interruptible(dev->write_wq,
-			((req = req_get(dev, &dev->tx_idle)) || dev->error));
+			(req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
 
 		if (ret < 0) {
 			r = ret;
@@ -374,8 +370,8 @@
 		}
 
 		if (req != 0) {
-			if (count > BULK_BUFFER_SIZE)
-				xfer = BULK_BUFFER_SIZE;
+			if (count > ADB_BULK_BUFFER_SIZE)
+				xfer = ADB_BULK_BUFFER_SIZE;
 			else
 				xfer = count;
 			if (copy_from_user(req->buf, buf, xfer)) {
@@ -386,7 +382,7 @@
 			req->length = xfer;
 			ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
 			if (ret < 0) {
-				DBG(cdev, "adb_write: xfer error %d\n", ret);
+				pr_debug("adb_write: xfer error %d\n", ret);
 				dev->error = 1;
 				r = -EIO;
 				break;
@@ -401,17 +397,20 @@
 	}
 
 	if (req)
-		req_put(dev, &dev->tx_idle, req);
+		adb_req_put(dev, &dev->tx_idle, req);
 
-	_unlock(&dev->write_excl);
-	DBG(cdev, "adb_write returning %d\n", r);
+	adb_unlock(&dev->write_excl);
+	pr_debug("adb_write returning %d\n", r);
 	return r;
 }
 
 static int adb_open(struct inode *ip, struct file *fp)
 {
 	printk(KERN_INFO "adb_open\n");
-	if (_lock(&_adb_dev->open_excl))
+	if (!_adb_dev)
+		return -ENODEV;
+
+	if (adb_lock(&_adb_dev->open_excl))
 		return -EBUSY;
 
 	fp->private_data = _adb_dev;
@@ -425,7 +424,7 @@
 static int adb_release(struct inode *ip, struct file *fp)
 {
 	printk(KERN_INFO "adb_release\n");
-	_unlock(&_adb_dev->open_excl);
+	adb_unlock(&_adb_dev->open_excl);
 	return 0;
 }
 
@@ -440,48 +439,18 @@
 
 static struct miscdevice adb_device = {
 	.minor = MISC_DYNAMIC_MINOR,
-	.name = shortname,
+	.name = adb_shortname,
 	.fops = &adb_fops,
 };
 
-static int adb_enable_open(struct inode *ip, struct file *fp)
-{
-	if (atomic_inc_return(&adb_enable_excl) != 1) {
-		atomic_dec(&adb_enable_excl);
-		return -EBUSY;
-	}
 
-	printk(KERN_INFO "enabling adb\n");
-	android_enable_function(&_adb_dev->function, 1);
 
-	return 0;
-}
-
-static int adb_enable_release(struct inode *ip, struct file *fp)
-{
-	printk(KERN_INFO "disabling adb\n");
-	android_enable_function(&_adb_dev->function, 0);
-	atomic_dec(&adb_enable_excl);
-	return 0;
-}
-
-static const struct file_operations adb_enable_fops = {
-	.owner =   THIS_MODULE,
-	.open =    adb_enable_open,
-	.release = adb_enable_release,
-};
-
-static struct miscdevice adb_enable_device = {
-	.minor = MISC_DYNAMIC_MINOR,
-	.name = "android_adb_enable",
-	.fops = &adb_enable_fops,
-};
 
 static int
 adb_function_bind(struct usb_configuration *c, struct usb_function *f)
 {
 	struct usb_composite_dev *cdev = c->cdev;
-	struct adb_dev	*dev = func_to_dev(f);
+	struct adb_dev	*dev = func_to_adb(f);
 	int			id;
 	int			ret;
 
@@ -495,7 +464,7 @@
 	adb_interface_desc.bInterfaceNumber = id;
 
 	/* allocate endpoints */
-	ret = create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
+	ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
 			&adb_fullspeed_out_desc);
 	if (ret)
 		return ret;
@@ -517,29 +486,24 @@
 static void
 adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
 {
-	struct adb_dev	*dev = func_to_dev(f);
+	struct adb_dev	*dev = func_to_adb(f);
 	struct usb_request *req;
 
-	spin_lock_irq(&dev->lock);
-
-	adb_request_free(dev->rx_req, dev->ep_out);
-	while ((req = req_get(dev, &dev->tx_idle)))
-		adb_request_free(req, dev->ep_in);
 
 	dev->online = 0;
 	dev->error = 1;
-	spin_unlock_irq(&dev->lock);
 
-	misc_deregister(&adb_device);
-	misc_deregister(&adb_enable_device);
-	kfree(_adb_dev);
-	_adb_dev = NULL;
+	wake_up(&dev->read_wq);
+
+	adb_request_free(dev->rx_req, dev->ep_out);
+	while ((req = adb_req_get(dev, &dev->tx_idle)))
+		adb_request_free(req, dev->ep_in);
 }
 
 static int adb_function_set_alt(struct usb_function *f,
 		unsigned intf, unsigned alt)
 {
-	struct adb_dev	*dev = func_to_dev(f);
+	struct adb_dev	*dev = func_to_adb(f);
 	struct usb_composite_dev *cdev = f->config->cdev;
 	int ret;
 
@@ -567,10 +531,10 @@
 
 static void adb_function_disable(struct usb_function *f)
 {
-	struct adb_dev	*dev = func_to_dev(f);
+	struct adb_dev	*dev = func_to_adb(f);
 	struct usb_composite_dev	*cdev = dev->cdev;
 
-	DBG(cdev, "adb_function_disable\n");
+	DBG(cdev, "adb_function_disable cdev %p\n", cdev);
 	dev->online = 0;
 	dev->error = 1;
 	usb_ep_disable(dev->ep_in);
@@ -584,11 +548,27 @@
 
 static int adb_bind_config(struct usb_configuration *c)
 {
-	struct adb_dev *dev;
-	int ret;
+	struct adb_dev *dev = _adb_dev;
 
 	printk(KERN_INFO "adb_bind_config\n");
 
+	dev->cdev = c->cdev;
+	dev->function.name = "adb";
+	dev->function.descriptors = fs_adb_descs;
+	dev->function.hs_descriptors = hs_adb_descs;
+	dev->function.bind = adb_function_bind;
+	dev->function.unbind = adb_function_unbind;
+	dev->function.set_alt = adb_function_set_alt;
+	dev->function.disable = adb_function_disable;
+
+	return usb_add_function(c, &dev->function);
+}
+
+static int adb_setup(void)
+{
+	struct adb_dev *dev;
+	int ret;
+
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	if (!dev)
 		return -ENOMEM;
@@ -604,53 +584,24 @@
 
 	INIT_LIST_HEAD(&dev->tx_idle);
 
-	dev->cdev = c->cdev;
-	dev->function.name = "adb";
-	dev->function.descriptors = fs_adb_descs;
-	dev->function.hs_descriptors = hs_adb_descs;
-	dev->function.bind = adb_function_bind;
-	dev->function.unbind = adb_function_unbind;
-	dev->function.set_alt = adb_function_set_alt;
-	dev->function.disable = adb_function_disable;
-
-	/* start disabled */
-	dev->function.disabled = 1;
-
-	/* _adb_dev must be set before calling usb_gadget_register_driver */
 	_adb_dev = dev;
 
 	ret = misc_register(&adb_device);
 	if (ret)
-		goto err1;
-	ret = misc_register(&adb_enable_device);
-	if (ret)
-		goto err2;
-
-	ret = usb_add_function(c, &dev->function);
-	if (ret)
-		goto err3;
+		goto err;
 
 	return 0;
 
-err3:
-	misc_deregister(&adb_enable_device);
-err2:
-	misc_deregister(&adb_device);
-err1:
+err:
 	kfree(dev);
 	printk(KERN_ERR "adb gadget driver failed to initialize\n");
 	return ret;
 }
 
-static struct android_usb_function adb_function = {
-	.name = "adb",
-	.bind_config = adb_bind_config,
-};
-
-static int __init init(void)
+static void adb_cleanup(void)
 {
-	printk(KERN_INFO "f_adb init\n");
-	android_register_function(&adb_function);
-	return 0;
+	misc_deregister(&adb_device);
+
+	kfree(_adb_dev);
+	_adb_dev = NULL;
 }
-module_init(init);