Skip to contentSkip to conversation

MA USB drivers cover letter

PATCH Superseded Stephanie Wallick 5 participants 27 messages Nov 3, 2014 – Nov 12, 2014 1 message not in the archive Staging

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover MA USB drivers cover letter

Media Agnostic (MA) USB enables the USB protocol to be used over a wide range of physical media. MA USB is a relatively new protocol and is currently unsupported in the Linux kernel. This patch set adds the following drivers with the following capabilities:

1) MA USB Host:
- provides functionality of a USB Host Controller.
- implements MA USB protocol for a MA USB host.
- provides MA USB packet transport over TCP

2) MA USB Device:
- provides functionality of a USB Device Controller.
- implements MA USB protocol for a MA USB device.
- provides MA USB packet transport over TCP

Driver overview:

The MA USB Host and Device stacks each consists of two drivers. The first driver is the Media Agnostic (MA) layer, which interfaces with the existing USB stack and implements the MA USB protocol as defined in the MA USB Specification Release 1.0. The second driver is the Media Specific (MS) layer. The MS layer interfaces with the network stack to send and receive MA USB packets via TCP.

When an MA USB driver is loaded, the MA layer loads first and registers itself as a USB host controller with the USB Core. The MS layer then loads and registers itself with the MA layer. Once the MS layer is registered, the MA layer initiates creation of a communication channel for managment/control packets. In the case of the MA USB host, the driver the opens a TCP socket and polls periodically for a device. In the case of the MA USB device, the driver identifies the MA USB host by IP address, then looks for and binds to the open TCP socket of the MA USB host. When a connection event is initiated, the MA USB host notifies the USB core and normal USB enumeration and configuration results with each respective USB packet translated into its MA USB equivalent.

Tools:

1) various scripts are included to help load and unload the MA USB host and device drivers.

2) a utility is included to help connect, disconnect, and test the MA USB host and device drivers.

References:

MA USB Specification v1.0 available at www.usb.org/developers/docs/devclass_docs/

Stephanie Wallick (10):
added media agnostic (MA) USB HCD driver
added media agnostic (MA) USB HCD roothubs
added media agnostic (MA) data structures and handling added media agnostic (MA) USB packet handling
added media specific (MS) TCP drivers
added media agnostic (MA) UDC
added media agnostic (MA) USB management packet handling added media agnostic (MA) USB data packet handling added tools for building/loading media agnostic (MA) USB drivers added kernel build, configuration, and TODO files

 drivers/staging/Kconfig                            |    2 +
 drivers/staging/Makefile                           |    1 +
 drivers/staging/mausb/Kconfig                      |   16 +
 drivers/staging/mausb/Makefile                     |    3 +
 drivers/staging/mausb/TODO                         |   14 +
 drivers/staging/mausb/drivers/Kconfig              |   34 +
 drivers/staging/mausb/drivers/Makefile             |   20 +
 drivers/staging/mausb/drivers/mausb_const.h        |  109 ++
 drivers/staging/mausb/drivers/mausb_hcd.c          |  970 +++++++++++++
 drivers/staging/mausb/drivers/mausb_hcd.h          |  171 +++
 drivers/staging/mausb/drivers/mausb_hub.c          |  851 +++++++++++
 drivers/staging/mausb/drivers/mausb_hub.h          |  128 ++
 drivers/staging/mausb/drivers/mausb_ioctl.c        |  373 +++++
 drivers/staging/mausb/drivers/mausb_ioctl.h        |   99 ++
 drivers/staging/mausb/drivers/mausb_mem-host.c     |  404 ++++++
 drivers/staging/mausb/drivers/mausb_mem-host.h     |   74 +
 drivers/staging/mausb/drivers/mausb_mem.c          |  844 +++++++++++
 drivers/staging/mausb/drivers/mausb_mem.h          |  509 +++++++
 drivers/staging/mausb/drivers/mausb_mgmt.c         |  888 ++++++++++++
 drivers/staging/mausb/drivers/mausb_mgmt.h         |   90 ++
 drivers/staging/mausb/drivers/mausb_msapi.c        |  110 ++
 drivers/staging/mausb/drivers/mausb_msapi.h        |  232 +++
 drivers/staging/mausb/drivers/mausb_pkt.c          | 1038 ++++++++++++++
 drivers/staging/mausb/drivers/mausb_pkt.h          |  914 ++++++++++++
 drivers/staging/mausb/drivers/mausb_state.h        |  184 +++
 drivers/staging/mausb/drivers/mausb_tcp-device.c   |  147 ++
 drivers/staging/mausb/drivers/mausb_tcp-host.c     |  144 ++
 drivers/staging/mausb/drivers/mausb_tcp.c          |  446 ++++++
 drivers/staging/mausb/drivers/mausb_tcp.h          |  129 ++
 drivers/staging/mausb/drivers/mausb_tx-device.c    |  847 +++++++++++
 drivers/staging/mausb/drivers/mausb_tx-host.c      | 1211 ++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tx.c           |  318 +++++
 drivers/staging/mausb/drivers/mausb_tx.h           |  129 ++
 drivers/staging/mausb/drivers/mausb_udc.c          | 1488 ++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_udc.h          |  147 ++
 .../staging/mausb/scripts/build_load_connect.sh    |   69 +
 drivers/staging/mausb/scripts/load_gzero.sh        |    5 +
 drivers/staging/mausb/scripts/load_script.sh       |  125 ++
 drivers/staging/mausb/scripts/modprobify.sh        |   10 +
 drivers/staging/mausb/scripts/unload_all.sh        |   15 +
 drivers/staging/mausb/scripts/unload_gzero.sh      |    5 +
 drivers/staging/mausb/tools/mausb-util/Makefile    |   14 +
 drivers/staging/mausb/tools/mausb-util/README      |   30 +
 drivers/staging/mausb/tools/mausb-util/config.mk   |   17 +
 .../staging/mausb/tools/mausb-util/src/Android.mk  |   13 +
 .../staging/mausb/tools/mausb-util/src/Makefile    |   18 +
 .../staging/mausb/tools/mausb-util/src/connect.c   |   72 +
 .../staging/mausb/tools/mausb-util/src/connect.h   |   22 +
 drivers/staging/mausb/tools/mausb-util/src/mausb.c |  200 +++
 drivers/staging/mausb/tools/mausb-util/src/mausb.h |   64 +
 .../mausb/tools/mausb-util/src/mausb_ioctl.h       |   37 +
 drivers/staging/mausb/tools/mausb-util/src/utils.c |   94 ++
 52 files changed, 13894 insertions(+)
 create mode 100644 drivers/staging/mausb/Kconfig
 create mode 100644 drivers/staging/mausb/Makefile
 create mode 100644 drivers/staging/mausb/TODO
 create mode 100644 drivers/staging/mausb/drivers/Kconfig
 create mode 100644 drivers/staging/mausb/drivers/Makefile
 create mode 100755 drivers/staging/mausb/drivers/mausb_const.h
 create mode 100755 drivers/staging/mausb/drivers/mausb_hcd.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_hcd.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_hub.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_hub.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem-host.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem.h
 create mode 100755 drivers/staging/mausb/drivers/mausb_mgmt.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mgmt.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_pkt.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_pkt.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_state.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_udc.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_udc.h
 create mode 100644 drivers/staging/mausb/scripts/build_load_connect.sh
 create mode 100644 drivers/staging/mausb/scripts/load_gzero.sh
 create mode 100644 drivers/staging/mausb/scripts/load_script.sh
 create mode 100644 drivers/staging/mausb/scripts/modprobify.sh
 create mode 100644 drivers/staging/mausb/scripts/unload_all.sh
 create mode 100644 drivers/staging/mausb/scripts/unload_gzero.sh
 create mode 100644 drivers/staging/mausb/tools/mausb-util/Makefile
 create mode 100644 drivers/staging/mausb/tools/mausb-util/README
 create mode 100644 drivers/staging/mausb/tools/mausb-util/config.mk
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/Android.mk
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/Makefile
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/connect.c
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/connect.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb.c
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/utils.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/10 added media agnostic (MA) USB HCD driver

This is where we interface with the existing USB stack and implement the functionality of a USB host controller driver. From the host's perspective, we appear as just another USB host controller. However, instead of passing traffic along a wired USB bus, the driver hands USB packets off for transport per Media Agnostic USB protocol.

---
 drivers/staging/mausb/drivers/mausb_hcd.c | 970 ++++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_hcd.h | 171 ++++++
 2 files changed, 1141 insertions(+)
 create mode 100755 drivers/staging/mausb/drivers/mausb_hcd.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_hcd.h
drivers/staging/mausb/drivers/mausb_hcd.c new file +970 -0
new file mode 100755
index 0000000..03e8f0f
@@ -0,0 +1,970 @@
+/* Name:	mausb_hcd.c
+ * Description: Creates and initializes a virtual USB host controller driver
+ *	        for the Media Agnostic USB host driver.
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define DEBUG
+
+#include <linux/init.h>
+#include <linux/wait.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
+#include <linux/platform_device.h>
+#include <linux/usb/ch11.h>
+#include <linux/pm_runtime.h>
+#include <linux/usb/gadget.h>
+#include <linux/kthread.h>
+#include <linux/random.h>
+
+#include "mausb_hcd.h"
+#include "mausb_hub.h"
+#include "mausb_pkt.h"
+#include "mausb_mem-host.h"
+#include "mausb_msapi.h"
+#include "mausb_mgmt.h"
+#include "mausb_state.h"
+#include "mausb_tx.h"
+
+static struct platform_device mausb_pdev;
+
+struct api_context {
+	struct completion	done;
+	int			status;
+};
+
+/*
+ * pointer conversion functions
+ */
+inline struct mausb_hcd *usb_hcd_to_mausb_hcd(struct usb_hcd *hcd)
+{
+	if (usb_hcd_is_primary_hcd(hcd))
+		return *((struct mausb_hcd **) (hcd->hcd_priv));
+	else
+		return *((struct mausb_hcd **) (hcd->primary_hcd->hcd_priv));
+}
+
+inline struct usb_hcd *mausb_hcd_to_usb_hcd(struct mausb_hcd *mhcd)
+{
+	if (mhcd->shared_hcd && !usb_hcd_is_primary_hcd(mhcd->shared_hcd))
+		return mhcd->shared_hcd;
+	else
+		return mhcd->usb_hcd;
+}
+
+inline struct device *mausb_hcd_to_dev(struct mausb_hcd *mhcd)
+{
+	return mausb_hcd_to_usb_hcd(mhcd)->self.controller;
+}
+
+inline struct mausb_urb *usb_urb_to_mausb_urb(struct urb *urb)
+{
+	return (struct mausb_urb *) urb->hcpriv;
+}
+/* ----------------------------------------------------------------- */
+
+/**
+ * @maurb:	Media agnostic structure with URB to release.
+ * @status:	Status for URB that is getting released.
+ *
+ * Removes an URB from the queue, deletes the media agnostic information in
+ * the urb, and gives the URB back to the HCD. Caller must be holding the
+ * driver's spinlock.
+ */
+void mausb_unlink_giveback_urb(struct mausb_urb *maurb, int status)
+{
+	struct urb		*urb;
+	struct usb_hcd		*hcd;
+	struct api_context	*ctx = NULL;
+	struct mausb_hcd	*mhcd = platform_get_drvdata(&mausb_pdev);
+	unsigned long		irq_flags;
+
+	if (!mhcd) {
+		printk(KERN_ERR "%s: No MA USB HCD\n", __func__);
+		return;
+	}
+
+	hcd = mausb_hcd_to_usb_hcd(mhcd);
+
+	spin_lock_irqsave(&mhcd->giveback_lock, irq_flags);
+
+	if (!maurb) {
+		mausb_err(mhcd, "%s: no maurb\n", __func__);
+		spin_unlock_irqrestore(&mhcd->giveback_lock, irq_flags);
+		return;
+	} else {
+		urb = maurb->urb;
+		ctx = urb->context;
+	}
+
+	if (!urb) {
+		mausb_err(mhcd, "%s: no urb\n", __func__);
+		mausb_internal_drop_maurb(maurb, mhcd);
+		spin_unlock_irqrestore(&mhcd->giveback_lock, irq_flags);
+		return;
+	}
+
+	mausb_dbg(mhcd, "%s: returning urb with status %i\n", __func__, status);
+
+	usb_hcd_unlink_urb_from_ep(hcd, urb);
+	usb_hcd_giveback_urb(hcd, urb, status);
+
+	/* remove the mausb-specific data */
+	mausb_internal_drop_maurb(maurb, mhcd);
+
+	spin_unlock_irqrestore(&mhcd->giveback_lock, irq_flags);
+}
+
+/**
+ * Adds an URB to the endpoint queue then calls the URB handler. URB is wrapped
+ * in media agnostic structure before being enqueued.
+ */
+static int mausb_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
+		gfp_t memflags)
+{
+	int			ret = 0;
+	struct mausb_urb	*maurb;
+	struct mausb_hcd	*mhcd;
+	struct mausb_host_ep	*ep;
+	unsigned long		irq_flags;
+
+	if (!hcd || !urb) {
+		printk(KERN_ERR "%s: no %s\n", __func__,
+			(hcd ? "urb" : "USB hcd"));
+	}
+
+	mhcd = usb_hcd_to_mausb_hcd(hcd);
+	ep   = usb_to_ma_endpoint(urb->ep);
+
+	if (!ep || !mhcd) {
+		mausb_err(mhcd, "%s: no %s\n", __func__,
+			(ep ? "MA USB HCD" : "endpoint"));
+		return -EINVAL;
+	}
+
+	if (urb->status != -EINPROGRESS) {
+		mausb_err(mhcd, "%s: urb already unlinked, status is %i\n",
+			__func__, urb->status);
+		return urb->status;
+	}
+
+	/* If the endpoint isn't activated, we can't enqueue anything. */
+	if (MAUSB_EP_HANDLE_UNASSIGNED == ep->ep_handle_state) {
+		mausb_err(mhcd, "%s: endpoint handle unassigned\n", __func__);
+		return -EPIPE;
+	}
+
+	if (USB_SPEED_FULL != urb->dev->speed) /* suppress checks */
+		ep->max_pkt = usb_endpoint_maxp(&urb->ep->desc);
+
+	/* initialize the maurb */
+	maurb = mausb_alloc_maurb(ep, memflags);
+	if (!maurb) {
+		mausb_err(mhcd, "could not allocate memory for MA USB urb\n");
+		return -ENOMEM;
+	}
+
+	/* set maurb member values */
+	maurb->urb = urb;
+	urb->hcpriv = maurb;
+
+	/* submit urb to hcd and add to endpoint queue */
+	ret = usb_hcd_link_urb_to_ep(hcd, urb);
+	if (ret < 0) {
+		mausb_err(mhcd, "urb enqueue failed: error %d\n", ret);
+		usb_hcd_unlink_urb_from_ep(hcd, urb);
+		return ret;
+	}
+
+	/* get usb device and increment reference counter */
+	if (!mhcd->udev) {
+		mhcd->udev = urb->dev;
+		usb_get_dev(mhcd->udev);
+	}
+
+	/* add urb to queue list */
+	spin_lock_irqsave(&ep->ep_lock, irq_flags);
+	list_add_tail(&maurb->urb_list, &ep->urb_list);
+	spin_unlock_irqrestore(&ep->ep_lock, irq_flags);
+
+	/* add urb to ma hcd urb list */
+	spin_lock_irqsave(&mhcd->urb_list_lock, irq_flags);
+	list_add_tail(&maurb->ma_hcd_urb_list, &mhcd->enqueue_urb_list);
+	spin_unlock_irqrestore(&mhcd->urb_list_lock, irq_flags);
+
+	/* send to MA transfer process */
+	wake_up(&mhcd->waitq);
+
+	return ret;
+}
+
+/**
+ * Dequeues an URB.
+ */
+static int mausb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
+{
+	int			ret	= 0;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_host_ep	*ep = usb_to_ma_endpoint(urb->ep);
+	struct mausb_urb	*maurb = usb_urb_to_mausb_urb(urb);
+	unsigned long		irq_flags;
+
+	/* For debugging - we want to know who initiated URB dequeue. */
+	dump_stack();
+
+	if (ep->active_transfer == maurb) {
+		ret = mausb_tx_dev_mgmt_req_ep(CancelTransferReq,
+			&mhcd->ma_dev.mgmt, maurb->dev, true, ep);
+
+		spin_lock_irqsave(&ep->ep_lock, irq_flags);
+		ep->active_transfer = NULL;
+		ep->state.tx_pending = 0;
+		spin_unlock_irqrestore(&ep->ep_lock, irq_flags);
+	}
+
+	/*
+	 * Make sure urb hasn't been unlinked or already completed.
+	 * Dequeue must fail if usb_hcd_check_unlink_urb() fails.
+	 */
+	spin_lock_irqsave(&ep->ep_lock, irq_flags);
+	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
+
+	if (ret < 0) {
+		spin_unlock_irqrestore(&ep->ep_lock, irq_flags);
+		mausb_err(mhcd, "%s: urb already unlinked or completed, %d\n",
+			__func__, ret);
+		return ret;
+	}
+
+	/* check to make sure our urb queue is not empty */
+	if (list_empty(&ep->urb_list)) {
+		spin_unlock_irqrestore(&ep->ep_lock, irq_flags);
+		mausb_err(mhcd, "%s: urb queue is empty\n", __func__);
+		return -ENXIO;
+	}
+
+	spin_unlock_irqrestore(&ep->ep_lock, irq_flags);
+
+	/* now we can give back URB */
+	spin_lock_irqsave(&mhcd->hcd_lock, irq_flags);
+	mausb_unlink_giveback_urb(maurb, -EINPROGRESS);
+	spin_unlock_irqrestore(&mhcd->hcd_lock, irq_flags);
+
+	return ret;
+}
+
+/**
+ * Called by usb core to suspend the bus. Once suspended, hcd waits in
+ * that state until it is resumed by mausb_bus_resume().
+ *
+ * Note: sections are commented out to accomidate devices that don't yet
+ * support SleepReq packets.
+ *
+ * Always returns zero.
+ */
+static int mausb_bus_suspend(struct usb_hcd *hcd)
+{
+/*	int			ret = 0; */
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+/*	struct mausb_dev	*top_dev = mhcd->ma_dev.top_dev; */
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+/*	if (NULL != top_dev) {
+		ret = mausb_tx_dev_mgmt_req(SleepReq,
+			&mhcd->ma_dev.mgmt, top_dev, true);
+		if (ret < 0)
+			return ret;
+	}
+*/
+	/* stop polling */
+	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
+
+	/*
+	 * Avoid suspend/resume race condition by making sure there aren't any
+	 * devices in the middle of resuming. If a connected device is
+	 * resuming, suspend should fail.
+	 */
+	if (mhcd->resuming) {
+		mausb_err(mhcd, "%s: suspend failed, device is resuming\n",
+			 __func__);
+		return -EBUSY;
+	}
+
+	mausb_dbg(mhcd, "%s: suspending mausb driver\n", __func__);
+
+	roothub->rh_state = MAUSB_RH_SUSPEND;
+	hcd->state = HC_STATE_SUSPENDED;
+
+	return 0;
+}
+
+/**
+ * Called by usb core to resume hcd after bus suspension.
+ *
+ * Returns 0 if resume was successful, otherwise returns negative error
+ * value.
+ */
+static int mausb_bus_resume(struct usb_hcd *hcd)
+{
+	int ret = 0;
+	struct mausb_hcd *mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_dev *top_dev = mhcd->ma_dev.top_dev;
+	struct mausb_root_hub	 *roothub = usb_hcd_to_roothub(hcd);
+
+	if (!HCD_HW_ACCESSIBLE(hcd)) {
+		mausb_err(mhcd, "%s: hcd not fully powered\n", __func__);
+		return -ESHUTDOWN;
+	}
+
+	mausb_dbg(mhcd, "%s: resuming mausb driver\n", __func__);
+
+	hcd->state = HC_STATE_RUNNING;
+	roothub->rh_state = MAUSB_RH_RUNNING;
+
+	if (NULL != top_dev) {
+		ret = mausb_tx_dev_mgmt_req(WakeReq, &mhcd->ma_dev.mgmt,
+			top_dev, true);
+
+		if (ret < 0) {
+			mausb_err(mhcd, "%s: WakeReq was unsuccessful"
+				" (error %i)\n", __func__, ret);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * Returns the hardware-chosen device address.
+ */
+static int mausb_address_device(struct usb_hcd *hcd, struct usb_device *udev)
+{
+	int			ret = 0;
+	struct ma_dev		*ma_dev;
+	struct mausb_dev	*mausb_dev;
+	struct mausb_host_ep	*ma_ep;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+
+	if (NULL == mhcd)
+		return -EINVAL;
+
+	ma_dev = &mhcd->ma_dev;
+	if (NULL == ma_dev)
+		return -EINVAL;
+
+	mausb_dev = mausb_find_dev_host(ma_dev, udev);
+	if (NULL == mausb_dev)
+		return -ENODEV;
+
+	ret = mausb_tx_dev_mgmt_req(USBDevHandleReq, &ma_dev->mgmt,
+		mausb_dev, true);
+
+	if (-ETIMEDOUT == ret) {
+		mausb_err(mhcd, "USBDevHandleReq timed out\n");
+	} else if (0 > ret) {
+		mausb_err(mhcd, "USBDevHandleReq failed with error %i\n", ret);
+	} else {
+		ret = mausb_tx_dev_mgmt_req(EPHandleReq, &ma_dev->mgmt,
+			mausb_dev, true);
+
+		if (-ETIMEDOUT == ret) {
+			mausb_err(mhcd, "EPHandleReq timed out\n");
+		} else if (0 > ret) {
+			mausb_err(mhcd, "EPHandleReq failed with error %i\n",
+					ret);
+		} else {
+			ret = mausb_tx_dev_mgmt_req(SetUSBDevAddrReq,
+				&ma_dev->mgmt, mausb_dev, true);
+
+			if (-ETIMEDOUT == ret) {
+				mausb_err(mhcd, "SetUSBDevAddrReq timed out\n");
+			} else if (0 > ret) {
+				mausb_err(mhcd, "SetUSBDevAddrReq failed with"
+					" error %i\n", ret);
+			} else {
+				ma_ep = mausb_find_ep_host(&udev->ep0,
+					mausb_dev);
+				ret = mausb_tx_dev_mgmt_req_ep(ModifyEP0Req,
+					&ma_dev->mgmt, mausb_dev, true, ma_ep);
+			}
+		}
+	}
+
+	/* TODO: handle failed enumeration */
+
+	return 0;
+}
+
+/**
+ * Resets everything to default values. Always returns zero.
+ */
+static int mausb_reset(struct usb_hcd *hcd)
+{
+	struct mausb_hcd	 *mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	 *roothub = usb_hcd_to_roothub(hcd);
+
+	mausb_dbg(mhcd, "%s: resetting MA USB host driver . . .\n", __func__);
+
+	roothub->rh_state = MAUSB_RH_RESETTING;
+
+	if (mausb_is_ss_hcd(hcd)) {
+		hcd->speed = HCD_USB3;
+		hcd->self.root_hub->speed = USB_SPEED_SUPER;
+	} else {
+		hcd->speed = HCD_USB2;
+		hcd->self.root_hub->speed = USB_SPEED_HIGH;
+		hcd->has_tt = 1;
+	}
+
+	hcd->uses_new_polling = 1;
+	hcd->self.sg_tablesize = 0;
+
+	mausb_init_port_status(&mhcd->root_hub);
+	mausb_init_port_status(&mhcd->shared_root_hub);
+
+	return 0;
+}
+
+int mausb_hcd_thread(void *data)
+{
+	struct mausb_hcd *mhcd = (struct mausb_hcd *)data;
+	struct mausb_urb *ma_urb, *next_ma_urb;
+	struct mausb_host_ep *ma_ep;
+	unsigned long irq_flags;
+
+	while (!kthread_should_stop()) {
+		spin_lock_irqsave(&mhcd->urb_list_lock, irq_flags);
+		list_for_each_entry_safe(ma_urb, next_ma_urb,
+			&mhcd->enqueue_urb_list, ma_hcd_urb_list) {
+
+			list_move(&ma_urb->ma_hcd_urb_list,
+				&mhcd->transfer_urb_list);
+
+			spin_unlock_irqrestore(&mhcd->urb_list_lock, irq_flags);
+
+			ma_ep = ma_urb->ep;
+			start_ma_transfer(ma_ep, ma_urb,
+				usb_pipein(ma_urb->urb->pipe));
+
+			spin_lock_irqsave(&mhcd->urb_list_lock, irq_flags);
+		}
+		spin_unlock_irqrestore(&mhcd->urb_list_lock, irq_flags);
+
+		wait_event_interruptible(mhcd->waitq,
+			!list_empty(&mhcd->enqueue_urb_list) ||
+			kthread_should_stop());
+	}
+
+	do_exit(0);
+	return 0;
+}
+
+/**
+ * Tells the hcd to start running. Always returns zero.
+ */
+static int mausb_start(struct usb_hcd *hcd)
+{
+	struct mausb_hcd	 *mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	 *roothub = usb_hcd_to_roothub(hcd);
+
+	mausb_dbg(mhcd, "%s: starting MA USB driver . . .\n", __func__);
+
+	hcd->state = HC_STATE_RUNNING;
+	roothub->rh_state = MAUSB_RH_RUNNING;
+
+	spin_lock_init(&mhcd->hcd_lock);
+	INIT_LIST_HEAD(&mhcd->ma_dev.dev_list);
+	INIT_LIST_HEAD(&mhcd->enqueue_urb_list);
+	spin_lock_init(&mhcd->urb_list_lock);
+	INIT_LIST_HEAD(&mhcd->transfer_urb_list);
+	spin_lock_init(&mhcd->giveback_lock);
+	init_waitqueue_head(&mhcd->waitq);
+	mhcd->ma_hcd_task_data = (void *)mhcd;
+	mhcd->ma_hcd_task = kthread_run(mausb_hcd_thread,
+		mhcd->ma_hcd_task_data, "mausb_hcd_thread");
+
+	return 0;
+}
+
+/**
+ * Called when driver is removed (specifically during usb_remove_hcd). This is
+ * where we tell the hcd to stop writing memory and doing I/O.
+ */
+static void mausb_stop(struct usb_hcd *hcd)
+{
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+
+	if (mhcd->ma_hcd_task != NULL) {
+		kthread_stop(mhcd->ma_hcd_task);
+		mhcd->ma_hcd_task = NULL;
+	}
+
+	mausb_dbg(mhcd, "%s: stopping mausb driver . . .\n", __func__);
+}
+
+static const struct hc_driver mausb_hc_driver = {
+	.description = "mausb-hcd",
+	.product_desc = "mausb pseudo-host controller",
+	.hcd_priv_size = sizeof(struct mausb_hcd),
+	.irq = NULL,
+	.flags = HCD_USB3,
+	.reset = &mausb_reset,
+	.start = &mausb_start,
+	.stop = &mausb_stop,
+	.urb_enqueue = &mausb_urb_enqueue,
+	.urb_dequeue = &mausb_urb_dequeue,
+	.hub_status_data = &mausb_hub_status_data,
+	.hub_control = &mausb_hub_control,
+	.bus_suspend = &mausb_bus_suspend,
+	.bus_resume = &mausb_bus_resume,
+	.alloc_dev = &mausb_alloc_dev,
+	.free_dev =  &mausb_free_dev,
+	.add_endpoint =    &mausb_add_endpoint,
+	.drop_endpoint =   &mausb_drop_endpoint,
+	.check_bandwidth = &mausb_check_bandwidth,
+	.reset_bandwidth = &mausb_reset_bandwidth,
+	.address_device =  &mausb_address_device,
+};
+
+/**
+ * Sets the host_bos fields to the proper initial values per USB3 spec,
+ * section 10.13.1.
+ */
+static int mausb_set_host_bos(struct mausb_host_bos *host_bos)
+{
+	/* BOS Descriptor */
+	host_bos->bos_des.bLength         = USB_DT_BOS_SIZE;
+	host_bos->bos_des.bDescriptorType = USB_DT_BOS;
+	host_bos->bos_des.wTotalLength    = sizeof(struct mausb_host_bos);
+	host_bos->bos_des.bNumDeviceCaps  = MAUSB_HOST_NUM_DEV_CAPS;
+
+	/* SuperSpeed USB Device Capability */
+	host_bos->ss_cap_des.bLength               = USB_DT_USB_SS_CAP_SIZE;
+	host_bos->ss_cap_des.bDescriptorType       = USB_DT_DEVICE_CAPABILITY;
+	host_bos->ss_cap_des.bDevCapabilityType    = USB_SS_CAP_TYPE;
+	host_bos->ss_cap_des.bmAttributes          = USB_LTM_SUPPORT;
+	host_bos->ss_cap_des.wSpeedSupported       = USB_5GBPS_OPERATION;
+	host_bos->ss_cap_des.bFunctionalitySupport = MAUSB_SUPER_SPEED;
+	host_bos->ss_cap_des.bU1devExitLat         = MAUSB_HOST_U1_DEV_EXIT_LAT;
+	host_bos->ss_cap_des.bU2DevExitLat         = MAUSB_HOST_U2_DEV_EXIT_LAT;
+
+	return 0;
+}
+
+/**
+ * @req:	The incoming MA USB management request packet to handle.
+ * @resp:	The MA USB management response packet to send out.
+ * @mgmt:	The MA USB HCD's management structure.
+ *
+ * This function handles management requests to the HCD. It parses the packet
+ * and makes the necessary function calls into the system.
+ *
+ * Note: this function only completes the packet type, status and
+ * request-specific data-fields. the remainder of the fields are set
+ * by mausb_rx_mgmt_req
+ */
+static int hcd_mgmt_req_switch(struct mausb_mgmt_pkt *req,
+		struct mausb_mgmt_pkt *resp, struct mausb_mgmt *mgmt)
+{
+	int ret = 0;
+
+	switch (req->common.pkt_type) {
+
+	/* TODO: handle unimplemented request types */
+	case RemoteWakeReq:
+	case DevInitDisconnectReq:
+		break;
+	case PingReq:
+		resp->common.pkt_type   = PingResp;
+		resp->common.pkt_status = NO_ERROR;
+		break;
+	/* invalid packet type */
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+static int mausb_transfer_mgmt_packet(struct ms_pkt *ms_pkt, void *context);
+
+/**
+ * Generates the mass_id used to initialize an MA USB device. Uses a random
+ * number generator to choose a random id between 1 and 254.
+ *
+ * Returns the random id chosen.
+ */
+static __u8 get_mass_id(void)
+{
+       __u8 id;
+
+       get_random_bytes(&id, sizeof(id));
+
+       /* Note: this function is twice as likely to pick 1 or 2 */
+       id = (id % (MASS_ID_MAX - MASS_ID_MIN + 1) + MASS_ID_MIN);
+
+       return id;
+}
+
+/**
+ * Initializes MA USB hcd roothub descriptors and ports.
+ *
+ * Always returns zero.
+ */
+static int create_mausb_hcd(struct mausb_hcd *mausb_hcd)
+{
+	if (mausb_hcd == NULL)
+		return -ENOMEM;
+
+	/* set the roothub descriptor to the proper initial values */
+	mausb_set_hub_descriptor(&mausb_hcd->root_hub.descriptor);
+	mausb_set_ss_hub_descriptor(&mausb_hcd->shared_root_hub.descriptor);
+
+	/* set the host_bos to the proper values */
+	mausb_set_host_bos(&mausb_hcd->host_bos);
+
+	/* initialize port status values */
+	mausb_init_port_status(&mausb_hcd->root_hub);
+	mausb_init_ss_port_status(&mausb_hcd->shared_root_hub);
+
+	/* initialize the MA USB device structure */
+	mausb_init_ma_device(&mausb_hcd->ma_dev, MAUSB_DEV_ADDR, get_mass_id(),
+		mausb_hcd, NULL, &hcd_mgmt_req_switch, &mausb_device_connect);
+
+	return 0;
+}
+
+/**
+ * probe function
+ */
+static int mausb_probe(struct platform_device *mausb_pdev)
+{
+	int			ret = 0;
+	const char		*bus_name = "MAUSB";
+	unsigned int		mausb_irqnum = 0;
+	struct usb_hcd		*usb_hcd;
+	struct mausb_hcd	*mhcd;
+
+	/* create our USB 2.0 roothub */
+	usb_hcd = usb_create_hcd(&mausb_hc_driver, &mausb_pdev->dev, bus_name);
+	if (usb_hcd == NULL) {
+		dev_err(&mausb_pdev->dev, "%s: could not create USB HCD\n",
+			__func__);
+		return -ENOMEM;
+	}
+
+	/* create our data structure that holds MA USB data */
+	mhcd = kzalloc(sizeof(struct mausb_hcd), GFP_KERNEL);
+	ret = create_mausb_hcd(mhcd);
+	if (ret < 0) {
+		dev_err(&mausb_pdev->dev, "%s: could not create MA USB HCD"
+			" (error %i)\n", __func__, ret);
+
+		if (ret != -ENOMEM)
+			kfree(mhcd);
+
+		return ret;
+	}
+
+	/*
+	 * Everyone exchanges pointers so we can move between data types with
+	 * minimal pointer juju.
+	 */
+	platform_set_drvdata(mausb_pdev, mhcd);
+	*((struct mausb_hcd **) usb_hcd->hcd_priv) = mhcd;
+	mhcd->usb_hcd = usb_hcd;
+
+	/*
+	 * Finish initializing generic members of USB 2.0 HCD, register the bus,
+	 * request IRQ line, call driver's reset() and start() routines.
+	 */
+	ret = usb_add_hcd(usb_hcd, mausb_irqnum, IRQF_SHARED);
+	if (ret) {
+		dev_err(&mausb_pdev->dev, "%s: could not add USB HCD"
+			" (error %i)\n", __func__, ret);
+
+		usb_remove_hcd(usb_hcd);
+		kfree(mhcd);
+
+		return ret;
+	}
+
+	/* add our USB 3.0 roothub */
+	mhcd->shared_hcd = usb_create_shared_hcd(&mausb_hc_driver,
+		&mausb_pdev->dev, bus_name, usb_hcd);
+
+	if (mhcd->shared_hcd == NULL) {
+		dev_dbg(&mausb_pdev->dev,
+			"%s: could not create shared USB HCD\n", __func__);
+
+		usb_remove_hcd(usb_hcd);
+		kfree(mhcd);
+
+		return -ENOMEM;
+	}
+
+	ret = usb_add_hcd(mhcd->shared_hcd, mausb_irqnum, IRQF_SHARED);
+
+	if (ret) {
+		dev_err(&mausb_pdev->dev, "%s: could not add shared"
+			" USB HCD (error %i)\n", __func__, ret);
+
+		usb_remove_hcd(usb_hcd);
+		kfree(mhcd);
+	}
+
+	return ret;
+}
+
+/**
+ * remove function
+ */
+static int mausb_remove(struct platform_device *mausb_pdev)
+{
+	int			ret = 0;
+	struct usb_hcd		*hcd, *shared_hcd;
+	struct mausb_hcd	*mhcd = platform_get_drvdata(mausb_pdev);
+
+	if (mhcd == NULL) {
+		dev_err(&mausb_pdev->dev,
+			"%s:MA USB HCD not found\n", __func__);
+		return 0;
+	}
+
+	hcd = mhcd->usb_hcd;
+	shared_hcd = mhcd->shared_hcd;
+
+	if (mhcd->shared_hcd) {
+		usb_remove_hcd(mhcd->shared_hcd);
+		usb_put_hcd(mhcd->shared_hcd);
+		mhcd->shared_hcd = NULL;
+	}
+
+	usb_remove_hcd(hcd);
+	usb_put_hcd(hcd);
+
+	kfree(mhcd);
+
+	return ret;
+}
+
+/**
+ * Release function.
+ */
+static void mausb_dev_release(struct device *dev)
+{
+	/* TODO: if we dynamically allocate anything, free it here */
+}
+
+static struct platform_driver mausb_driver = {
+	.probe = mausb_probe,
+	.remove = mausb_remove,
+	.driver = {
+		.name = MAUSB_NAME,
+		.owner = THIS_MODULE,
+	},
+};
+
+static struct platform_device mausb_pdev = {
+	.name = MAUSB_NAME,
+	.id = 0,
+};
+
+/**
+ * @pkt		The incoming MA USB packet to parse.
+ * @context	The MA USB HCD's management structure.
+ *
+ * Used to transfer MA USB management packets to and from an MA USB device.
+ */
+static int mausb_transfer_mgmt_packet(struct ms_pkt *ms_pkt, void *context)
+{
+	int			ret;
+	struct mausb_mgmt	*mgmt;
+	struct mausb_hcd	*mhcd = platform_get_drvdata(&mausb_pdev);
+	struct mausb_pkt	*pkt = mausb_pkt_from_ms_pkt_ma_dev(ms_pkt,
+					&mhcd->ma_dev, GFP_ATOMIC);
+
+	if ((NULL == context) || (NULL == pkt)) {
+		mausb_err(mhcd, "%s: received NULL input\n", __func__);
+		return -EFAULT;
+	}
+
+	mgmt = (struct mausb_mgmt *) context;
+
+	ret = mausb_rx_mgmt(pkt, mgmt);
+
+	return ret;
+}
+
+/**
+ * @drv:	The media specific driver structure that provides an interface
+ *		for the media agnostic driver.
+ *
+ * Registers a media specific driver with the MA USB HCD.
+ */
+struct mausb_ma_drv *mausb_register_ms_driver(struct mausb_ms_drv *drv)
+{
+	struct mausb_hcd	*mhcd = platform_get_drvdata(&mausb_pdev);
+	unsigned long		irq_flags;
+
+	spin_lock_irqsave(&mhcd->hcd_lock, irq_flags);
+	mhcd->ma_dev.ms_driver = drv;
+	spin_unlock_irqrestore(&mhcd->hcd_lock, irq_flags);
+
+	/* Open the management channel */
+	mausb_add_mgmt_channel(&mhcd->ma_dev.mgmt, drv,
+			 &mausb_transfer_mgmt_packet, &mausb_mgmt_pkt_sent);
+
+	return &mhcd->ma_dev.ma_drv;
+}
+EXPORT_SYMBOL(mausb_register_ms_driver);
+
+/**
+ * Called by a media specific driver to indicate that an MA USB device has been
+ * connected and is ready to receive MA USB packets.
+ */
+int mausb_device_connect(int on)
+{
+	int status = 0;
+	/* TODO: don't want portnum hard coded when we add more ports */
+	int portnum = 0;
+	struct mausb_hcd	*mhcd = platform_get_drvdata(&mausb_pdev);
+
+	/*  connection event */
+	if (on) {
+		/* reset MA USB device */
+		status = mausb_tx_dev_mgmt_req(DevResetReq, &mhcd->ma_dev.mgmt,
+			NULL, true);
+
+		if (status < 0) {
+			mausb_err(mhcd, "%s: cannot reset MA device,"
+				" error %i\n", __func__, status);
+			return status;
+		}
+
+		/* send CapResp packet */
+		if (0 <= status) {
+			status = mausb_tx_dev_mgmt_req(CapReq,
+				&mhcd->ma_dev.mgmt, NULL, true);
+		}
+
+		if (status < 0) {
+			mausb_err(mhcd, "%s: cannot get device capabilities,"
+				" error %i\n", __func__, status);
+			return status;
+		}
+	}
+
+	status = mausb_connect(mhcd, portnum, on);
+
+	return status;
+}
+
+/**
+ * initialization function
+ */
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);
+	if (ret < 0) {
+		printk(KERN_DEBUG "%s: failed to register HC driver: "
+			" error number %d\n", __func__, ret);
+
+	} else {
+		/* register HCD device */
+		ret = platform_device_register(&mausb_pdev);
+
+		if (ret < 0) {
+			printk(KERN_DEBUG "%s: failed to register HC device:"
+				"error number %d\n", __func__, ret);
+			platform_driver_unregister(&mausb_driver);
+		} else {
+			/* direct the release function (for exiting) */
+			mausb_pdev.dev.release = &mausb_dev_release;
+
+			if (ret < 0) {
+				printk(KERN_DEBUG "failed to register HC"
+					" chardev: error number %d\n", ret);
+				platform_driver_unregister(&mausb_driver);
+				platform_device_unregister(&mausb_pdev);
+			}
+		}
+	}
+
+	return ret;
+}
+module_init(mausb_hcd_init);
+
+/**
+ * Exit function.
+ */
+static void mausb_hcd_exit(void)
+{
+	/* deregister HC device */
+	platform_device_unregister(&mausb_pdev);
+
+	/* deregister HC driver */
+	platform_driver_unregister(&mausb_driver);
+}
+module_exit(mausb_hcd_exit);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_AUTHOR("Intel");
+MODULE_DESCRIPTION("MA USB driver");
drivers/staging/mausb/drivers/mausb_hcd.h new file +171 -0

171 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 1 line · Stephanie Wallick

+#define DEBUG

I doubt you want this in the driver enabled by default :(

author
#3 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 5 lines · Greg KH
Quoted 1 line · Stephanie Wallick

+#define DEBUG

I doubt you want this in the driver enabled by default :(

Thank you for catching, will remove in the next patch version.

maintainer
#4 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

In reply to Stephanie Wallick
Quoted 1 line · Stephanie Wallick

+EXPORT_SYMBOL(mausb_register_ms_driver);

EXPORT_SYMBOL_GPL()? I have to ask...

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

drivers/staging/mausb/drivers/mausb_hcd.c line 928

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +3 -0
+	if (ret < 0) {
+		printk(KERN_DEBUG "%s: failed to register HC driver: "
+			" error number %d\n", __func__, ret);

pr_err()?

return here, that way you don't need:

Review

Quoted 1 line
(quoted diff) +1 -0
+	} else {

This indentation.

drivers/staging/mausb/drivers/mausb_hcd.c line 932

Quoted 2 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +2 -0
+		/* register HCD device */
+		ret = platform_device_register(&mausb_pdev);

But again, why is this a platform device? What platform resources does it have / require?

drivers/staging/mausb/drivers/mausb_hcd.c line 936

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+
+		if (ret < 0) {
+			printk(KERN_DEBUG "%s: failed to register HC device:"
+				"error number %d\n", __func__, ret);

pr_err()?

drivers/staging/mausb/drivers/mausb_hcd.c line 940

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+			platform_driver_unregister(&mausb_driver);
+		} else {
+			/* direct the release function (for exiting) */
+			mausb_pdev.dev.release = &mausb_dev_release;

That seems like a serious hack, why do you need to do this in this manner?

drivers/staging/mausb/drivers/mausb_hcd.c line 944

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+
+			if (ret < 0) {
+				printk(KERN_DEBUG "failed to register HC"
+					" chardev: error number %d\n", ret);

pr_err()?

thanks,

greg k-h

author
#5 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 5 lines · Greg KH
Quoted 1 line · Stephanie Wallick

+EXPORT_SYMBOL(mausb_register_ms_driver);

EXPORT_SYMBOL_GPL()? I have to ask...

The source is dual-licenced under BSD and GPL. It was our understanding that dual-licensed should use EXPORT_SYMBOL() instead. Is that wrong?

Quoted 10 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

The driver doesn't require platform resources. It looks like a host controller driver but communicates over the network instead of to a physical host controller. There is no MA USB-specific hardware.

Should we use a struct device instead of a struct platform_device?

Quoted 6 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 928

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +3 -0
+	if (ret < 0) {
+		printk(KERN_DEBUG "%s: failed to register HC driver: "
+			" error number %d\n", __func__, ret);

pr_err()?

Will change all printk() to pr_err() in next patch.

Quoted 6 lines

return here, that way you don't need:

Review

Quoted 1 line
(quoted diff) +1 -0
+	} else {

This indentation.

Will fix in next patch.

Quoted 6 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 932

Quoted 2 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +2 -0
+		/* register HCD device */
+		ret = platform_device_register(&mausb_pdev);

But again, why is this a platform device? What platform resources does it have / require?

See above.

Quoted 7 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 936

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+
+		if (ret < 0) {
+			printk(KERN_DEBUG "%s: failed to register HC device:"
+				"error number %d\n", __func__, ret);

pr_err()?

See above.

Quoted 8 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 940

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+			platform_driver_unregister(&mausb_driver);
+		} else {
+			/* direct the release function (for exiting) */
+			mausb_pdev.dev.release = &mausb_dev_release;

That seems like a serious hack, why do you need to do this in this manner?

This will go away when we get rid of the platform device.

Quoted 7 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 944

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +4 -0
+
+			if (ret < 0) {
+				printk(KERN_DEBUG "failed to register HC"
+					" chardev: error number %d\n", ret);

pr_err()?

See above.

Quoted 3 lines

thanks,

greg k-h

Thanks,
Stephanie

maintainer
#6 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 8 lines · steph
Quoted 5 lines · Greg KH
Quoted 1 line · Stephanie Wallick

+EXPORT_SYMBOL(mausb_register_ms_driver);

EXPORT_SYMBOL_GPL()? I have to ask...

The source is dual-licenced under BSD and GPL. It was our understanding that dual-licensed should use EXPORT_SYMBOL() instead. Is that wrong?

Talk to your company lawyers please to get confirmation of what you want to do here, I can't answer this question, I just have to ask...

Quoted 10 lines
Quoted 10 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

Quoted 5 lines

The driver doesn't require platform resources. It looks like a host controller driver but communicates over the network instead of to a physical host controller. There is no MA USB-specific hardware.

Should we use a struct device instead of a struct platform_device?

Yes, please make it a "virtual" device.

thanks,

greg k-h

author
#7 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 5 lines · Greg KH
Quoted 4 lines · steph
Quoted 3 lines · Greg KH
Quoted 1 line · Stephanie Wallick

+EXPORT_SYMBOL(mausb_register_ms_driver);

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

EXPORT_SYMBOL_GPL()? I have to ask...

The source is dual-licenced under BSD and GPL. It was our understanding that dual-licensed should use EXPORT_SYMBOL() instead. Is that wrong?

Talk to your company lawyers please to get confirmation of what you want to do here, I can't answer this question, I just have to ask...

We have permission to go forward with the dual BSD/GPL license. I will leave as is unless there is a future issue.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

Quoted 8 lines
Quoted 5 lines

The driver doesn't require platform resources. It looks like a host controller driver but communicates over the network instead of to a physical host controller. There is no MA USB-specific hardware.

Should we use a struct device instead of a struct platform_device?

Yes, please make it a "virtual" device.

Will do.

Thanks,
Stephanie

#8 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

In reply to Greg KH
Quoted 3 lines · Greg KH
Quoted 2 lines · steph
Quoted 1 line · Greg KH

On Mon, Nov 03, 2014 at 12:42:48PM -0800, Stephanie Wallick wrote:

[snip]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

Quoted 8 lines
Quoted 5 lines

The driver doesn't require platform resources. It looks like a host controller driver but communicates over the network instead of to a physical host controller. There is no MA USB-specific hardware.

Should we use a struct device instead of a struct platform_device?

Yes, please make it a "virtual" device.

Is it OK for our virtual host controller to use struct platform_device?

The other virtual host controllers (usbip/vhci_hcd.c & gadget/udc/dummy_hcd.c) use the platform_device struct. Unless I am missing something, it doesn't look like the other virtual host controllers use platform resources.

If it is not ok, is there a good example somewhere of a virtual non-platform device?

Thank You,
Sean O. Stalley

maintainer
#9 lore raw

Re: [PATCH 01/10] added media agnostic (MA) USB HCD driver

Quoted 24 lines · sostalle
Quoted 3 lines · Greg KH
Quoted 2 lines · steph
Quoted 1 line · Greg KH

On Mon, Nov 03, 2014 at 12:42:48PM -0800, Stephanie Wallick wrote:

[snip]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/staging/mausb/drivers/mausb_hcd.c line 925

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_hcd.c new file +6 -0
+static int mausb_hcd_init(void)
+{
+	int ret;
+
+	/* register HCD driver */
+	ret = platform_driver_register(&mausb_driver);

Why is this a platform driver? How does this relate to platform hardware?

Quoted 8 lines
Quoted 5 lines

The driver doesn't require platform resources. It looks like a host controller driver but communicates over the network instead of to a physical host controller. There is no MA USB-specific hardware.

Should we use a struct device instead of a struct platform_device?

Yes, please make it a "virtual" device.

Quoted 8 lines

Is it OK for our virtual host controller to use struct platform_device?

The other virtual host controllers (usbip/vhci_hcd.c & gadget/udc/dummy_hcd.c) use the platform_device struct. Unless I am missing something, it doesn't look like the other virtual host controllers use platform resources.

If it is not ok, is there a good example somewhere of a virtual non-platform device?

If your device is not really a platform device (i.e. no platform resources), then just create a virtual device with a call to device_create() and don't pass in a parent pointer. But you need to create a class for it, which is a pain, but shouldn't be that hard.

Hope this helps,

greg k-h

#10 lore raw

PATCH 2/10 added media agnostic (MA) USB HCD roothubs

In reply to Stephanie Wallick

This is where we implement USB 2.0 and 3.0 roothubs. From the host's perspective, hub state is set and tracked just like any other USB roothub. Likewise, requests to the roothub appear to be handled like any other wired USB request.

---
 drivers/staging/mausb/drivers/mausb_hub.c | 851 ++++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_hub.h | 128 +++++
 2 files changed, 979 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_hub.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_hub.h
drivers/staging/mausb/drivers/mausb_hub.c new file +851 -0
new file mode 100644
index 0000000..aebca54
@@ -0,0 +1,851 @@
+/* Name:         mausb_hub.c
+ * Description:  mausb hub structures and functions
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define DEBUG
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
+#include <linux/platform_device.h>
+#include <linux/usb/ch11.h>
+#include <linux/usb/ch9.h>
+
+#include "mausb_hub.h"
+#include "mausb_hcd.h"
+
+/**
+ * Returns MA USB roothub data structure for a USB HCD.
+ */
+struct mausb_root_hub *usb_hcd_to_roothub(struct usb_hcd *hcd)
+{
+	struct mausb_hcd *mhcd = usb_hcd_to_mausb_hcd(hcd);
+
+	if (usb_hcd_is_primary_hcd(hcd))
+		return &mhcd->root_hub;
+	else
+		return &mhcd->shared_root_hub;
+}
+
+/**
+ * Returns true if the given is the superspeed HCD. Note: The primary HCD is
+ * High Speed and the shared HCD is SuperSpeed.
+ */
+bool mausb_is_ss_hcd(struct usb_hcd *hcd)
+{
+	if (usb_hcd_is_primary_hcd(hcd))
+		return false;
+	else
+		return true;
+}
+
+/**
+ * Sets downstream port link state.
+ *
+ * @mhcd:	MA USB hcd that owns port.
+ * @state:	Link state to be set (see USB 3.0 spec for link state values).
+ * @portnum:	Port number of link state to be set.
+ */
+void set_link_state(struct mausb_root_hub *roothub, int state, int portnum)
+{
+	struct usb_port_status   *port_stat;
+
+	port_stat = &roothub->port_status[portnum];
+
+		port_stat->wPortStatus &= LINK_STATE_MASK;
+		port_stat->wPortStatus |= state;
+}
+
+/**
+ * Generates connect or disconnect event for a roothub.
+ *
+ * @hcd:	HCD associated with roothub.
+ * @do_connect: 1 for connect, 0 for disconnect.
+ * @_portnum:	Port number of connected device.
+ * @is_SS:	True if roothub is SuperSpeed or higher, otherwise false.
+ */
+int port_connect(struct usb_hcd *hcd, int do_connect, int _portnum, bool is_SS)
+{
+	int			portnum = _portnum;
+	struct usb_port_status	*port_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+	if (portnum >= MAUSB_ROOTHUB_NUM_PORTS) {
+		mausb_err(mhcd, "%s: port number %i is too large (max = %i)\n",
+			__func__, portnum, MAUSB_ROOTHUB_NUM_PORTS);
+		return -EINVAL;
+	}
+
+	/* if new connection, assign first empty port */
+	if (do_connect > 0) {
+		portnum = 0;
+		while (roothub->port_status[portnum].wPortStatus
+			& USB_PORT_STAT_CONNECTION) {
+			portnum++;
+		}
+
+		mausb_dbg(mhcd, "%s: assigned MA USB port number %d\n",
+		__func__, portnum);
+	}
+
+	/*
+	 * If new connection, we assigned portnum above, otherwise use portnum
+	 * passed in.
+	 */
+	port_stat = &roothub->port_status[portnum];
+
+	/*
+	 * Make sure port is powered. If not, clear status and disable.
+	 * Note that POWER bit is different for USB 2.0 and 3.0 hubs.
+	 */
+	if (is_SS) {
+		if ((port_stat->wPortStatus & USB_SS_PORT_STAT_POWER) == 0) {
+			port_stat->wPortStatus = 0;
+			port_stat->wPortChange = 0;
+			set_link_state(roothub, USB_SS_PORT_LS_SS_DISABLED,
+				portnum);
+
+			goto update;
+		}
+	} else {
+		if ((port_stat->wPortStatus & USB_PORT_STAT_POWER) == 0) {
+			port_stat->wPortStatus = 0;
+			port_stat->wPortChange = 0;
+
+			goto update;
+		}
+	}
+
+	/* disconnect if previously connected */
+	if (do_connect == 0 || mhcd->disabled) {
+		if ((port_stat->wPortStatus & USB_PORT_STAT_CONNECTION) == 1) {
+			port_stat->wPortStatus &= ~(USB_PORT_STAT_CONNECTION);
+			port_stat->wPortStatus &= ~(USB_PORT_STAT_ENABLE);
+			port_stat->wPortChange |= USB_PORT_STAT_C_CONNECTION;
+			if (is_SS)
+				set_link_state(roothub,
+					USB_SS_PORT_LS_RX_DETECT, portnum);
+		}
+	}
+	/* set bits for a new connection */
+	else if ((port_stat->wPortStatus & USB_PORT_STAT_CONNECTION) == 0) {
+		port_stat->wPortStatus |= USB_PORT_STAT_ENABLE;
+		port_stat->wPortStatus |= USB_PORT_STAT_CONNECTION;
+		port_stat->wPortChange |= USB_PORT_STAT_C_CONNECTION;
+	}
+
+update:
+	mausb_dbg(mhcd, "%s: port status is 0x%x port change is 0x%x\n",
+		__func__, port_stat->wPortStatus, port_stat->wPortChange);
+
+	/* let core know if there is a change in port status */
+	if (port_stat->wPortChange) {
+		set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
+		usb_hcd_poll_rh_status(hcd);
+	}
+
+	return portnum;
+}
+
+/**
+ * @mhcd:       MA USB hcd with connect/disconnect event.
+ * @portnum:	MA USB port number with event.
+ * @do_connect: Determines whether or not a connection is set or removed.
+ *
+ * Called to initiate either a device connect or disconnect event. A do_connect
+ * value of 0 clears a connection. A do_connect value greater than 0 sets a
+ * connection. A do_connect value less than 0 produces an error.
+ *
+ * Returns port number. If connection, port number is newly assigned port
+ * number, otherwise it is the port number passed in as an argument.
+ */
+int mausb_connect(struct mausb_hcd *mhcd, int portnum, int do_connect)
+{
+	struct usb_hcd		*hcd = container_of((void *)mhcd,
+					struct usb_hcd, hcd_priv);
+	struct usb_hcd		*ss_hcd = mhcd->shared_hcd;
+
+	if (!mhcd->ma_dev.ms_driver) {
+		mausb_err(mhcd, "%s: no media specific driver\n", __func__);
+		return -ENODEV;
+	}
+
+	if (do_connect < 0) {
+		mausb_err(mhcd, "%s: invalid connection request %d\n", __func__,
+			do_connect);
+		return -EINVAL;
+	}
+
+	if (mhcd->ma_dev.ma_cap.dev_type > MAUSB_DEV_TYPE_SS_HUB) {
+		mausb_err(mhcd, "%s: invalid MA USB device type %i\n", __func__,
+			mhcd->ma_dev.ma_cap.dev_type);
+		return -EINVAL;
+	}
+
+	/*
+	 * One connection event for MA devices. Connect SS and SS+ devices to
+	 * 3.0 root hub and non-SS devices to 2.0 root hub.
+	 */
+	else if (mhcd->ma_dev.ma_cap.dev_type == MAUSB_DEV_TYPE_INTEGRATED) {
+		if (!mhcd->ma_dev.speed) {
+			mausb_err(mhcd, "%s: Speed Capability Descriptor not"
+				" found - non-hub MA USB devices require"
+				" a Speed Capability Descriptor\n", __func__);
+			return -EPROTO;
+		}
+
+		mausb_dbg(mhcd, "%s: %s MA USB device\n", __func__,
+			do_connect ? "connecting" : "disconnecting");
+
+		if (mhcd->ma_dev.speed->speed == MAUSB_DEV_CAP_SPEED_SUPER ||
+		    mhcd->ma_dev.speed->speed == MAUSB_DEV_CAP_SPEED_SS_PLUS) {
+			portnum = port_connect(ss_hcd, do_connect, portnum,
+					       true);
+		} else {
+			portnum = port_connect(hcd, do_connect, portnum, false);
+		}
+	}
+
+	/*
+	 * Hubs require two connection events - one for the integrated
+	 * USB 3.0 hub and one for the integrated USB 2.0 hub.
+	 */
+	else {
+		mausb_dbg(mhcd, "%s: %s MA USB hub\n", __func__,
+			do_connect ? "connecting" : "disconnecting");
+
+		portnum = port_connect(hcd, do_connect, portnum, false);
+		portnum = port_connect(ss_hcd, do_connect, portnum, true);
+	}
+
+	return 0;
+}
+
+/**
+ * Called by usb core when polling for a port status change.
+ *
+ * @hcd:	USB HCD being polled.
+ * @buf:	Holds port status changes (if any).
+ *
+ * Returns zero if there is no status change, otherwise returns number of
+ * bytes in buf. When there is a status change on a port, the bit indexed
+ * at the port number + 1 (e.g. bit 2 for port 1) is set in the buffer.
+ */
+int mausb_hub_status_data(struct usb_hcd *hcd, char *buf)
+{
+	int                      i;
+	u16                      port_change = 0;
+	u32                      status = 0;
+	int                      ret = 1;
+	struct mausb_hcd	 *mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	 *roothub = usb_hcd_to_roothub(hcd);
+
+	/*
+	 * Buf should never be more that 2 bytes. USB 3.0 hubs cannot have
+	 * more than 15 downstream ports.
+	 */
+	buf[0] = 0;
+	if (MAUSB_ROOTHUB_NUM_PORTS > 7) {
+		buf[1] = 0;
+		ret++;
+	}
+
+	for (i = 0; i < MAUSB_ROOTHUB_NUM_PORTS; i++) {
+		port_change = roothub->port_status[i].wPortChange;
+		if (port_change)
+			status |= (1 << (i + 1));
+	}
+
+	mausb_dbg(mhcd, "%s: hub status is 0x%x\n", __func__, status);
+
+	/* hcd might be suspended, resume if there is a status change */
+	if (mhcd->disabled == 0) {
+		if ((hcd->state == HC_STATE_SUSPENDED) && status)
+			usb_hcd_resume_root_hub(hcd);
+	}
+
+	memcpy(buf, (char *)&status, ret);
+
+	return status ? ret : 0;
+}
+
+/**
+ * Sets the bitfields in the hub descriptor of the 2.0 root hub. Always
+ * returns zero.
+ */
+int mausb_set_hub_descriptor(struct usb_hub_descriptor *hub_des)
+{
+	/* set the values to the default */
+	hub_des->bDescLength          = sizeof(struct usb_hub_descriptor);
+	hub_des->bDescriptorType      = USB_DT_HUB;
+	hub_des->bNbrPorts            = MAUSB_ROOTHUB_NUM_PORTS;
+	hub_des->wHubCharacteristics  = MAUSB_ROOTHUB_CHAR;
+	hub_des->bPwrOn2PwrGood       = MAUSB_ROOTHUB_PWR_ON_2_PWR_GOOD;
+	hub_des->bHubContrCurrent     = MAUSB_ROOTHUB_CONTR_CURRENT;
+
+	return 0;
+}
+
+/**
+ * Sets the bitfields in the hub descriptor of the 3.0 root hub. Always
+ * returns zero.
+ */
+int mausb_set_ss_hub_descriptor(struct usb_hub_descriptor *hub_des)
+{
+	/* set the values to the default */
+	hub_des->bDescLength          = sizeof(struct usb_hub_descriptor);
+	hub_des->bDescriptorType      = USB_DT_SS_HUB;
+	hub_des->bNbrPorts            = MAUSB_ROOTHUB_NUM_PORTS;
+	hub_des->wHubCharacteristics  = MAUSB_ROOTHUB_CHAR;
+	hub_des->bPwrOn2PwrGood       = MAUSB_ROOTHUB_PWR_ON_2_PWR_GOOD;
+	hub_des->bHubContrCurrent     = MAUSB_ROOTHUB_CONTR_CURRENT;
+
+	/* USB3-specific parameters */
+	hub_des->u.ss.bHubHdrDecLat   = MAUSB_ROOTHUB_HDR_DEC_LAT;
+	hub_des->u.ss.wHubDelay       = MAUSB_ROOTHUB_DELAY;
+	hub_des->u.ss.DeviceRemovable = MAUSB_ALL_DEV_REMOVABLE;
+
+	return 0;
+}
+
+/**
+ * handles GetHubDescriptor requests to the root hub.
+ *
+ * @hcd:	USB HCD that owns root hub.
+ * @hub_des:    Hub descriptor to be copied.
+ * @wLength:    Maximum length that can be copied without overflowing
+ *		the buffer.
+ *
+ * Returns length of what was actually copied into the mausb hcd root hub
+ * descriptor. Note that this may be less than the total size of hub_des due
+ * to buffer size contraints.
+ */
+static int mausb_get_hub_descriptor(struct usb_hcd *hcd,
+		struct usb_hub_descriptor *hub_des, u16 wLength)
+{
+	struct mausb_hcd *mhcd = usb_hcd_to_mausb_hcd(hcd);
+
+	/* Don't overflow the buffer, and don't read more than you have. */
+	unsigned int cpyLength = min_t(u16, wLength,
+				       sizeof(struct usb_hub_descriptor));
+
+	if (mausb_is_ss_hcd(hcd))
+		memcpy(hub_des, &(mhcd->shared_root_hub.descriptor), cpyLength);
+	else
+		memcpy(hub_des, &(mhcd->root_hub.descriptor), cpyLength);
+
+	return cpyLength;
+}
+
+/**
+ * Handles requests to hub for the BOS.
+ *
+ * @ma_hcd:	MA USB HCD that owns hub.
+ * @bos:	MA USB BOS descriptor.
+ * @wLength:	Maximum length that can be copied without overflowing
+ *		the buffer.
+ *
+ * Returns length of what was actually copied into the MA USB hcd BOS
+ * descriptor. Note that this may be less than the total size of the BOS due
+ * to buffer size contraints.
+ */
+static int mausb_get_bos(struct mausb_hcd *ma_hcd,
+		struct mausb_host_bos *bos, u16 wLength)
+{
+	/* Don't overflow the buffer, and don't read more than you have. */
+	unsigned int cpyLength = min_t(u16, wLength,
+				       sizeof(struct mausb_host_bos));
+
+	memcpy(bos, &(ma_hcd->host_bos), cpyLength);
+
+	return cpyLength;
+}
+
+/**
+ * Initializes FS root hub port status values for a given MA USB roothub.
+ *
+ * All ports are initally powered and put in link state rx.detect to wait for
+ * a connection (or other event that changes link state). All other bits are
+ * cleared.
+ */
+int mausb_init_port_status(struct mausb_root_hub *mausb_rh)
+{
+	int	portnum = 0;
+
+	while (portnum < MAUSB_ROOTHUB_NUM_PORTS) {
+
+		/* set port power bit only */
+		mausb_rh->port_status[portnum].wPortStatus = 0x0;
+		mausb_rh->port_status[portnum].wPortStatus |=
+			USB_PORT_STAT_POWER;
+
+		/* make sure to start with no status changes */
+		mausb_rh->port_status[portnum].wPortChange = 0x0;
+		portnum++;
+	}
+
+	mausb_rh->rh_state = MAUSB_RH_RUNNING;
+
+	return 0;
+}
+
+/**
+ * Initializes SS root hub port status values for a given MA USb roothub.
+ *
+ * All ports are initally powered and put in link state rx.detect to wait for
+ * a connection (or other event that changes link state). All other bits are
+ * cleared.
+ */
+int mausb_init_ss_port_status(struct mausb_root_hub *mausb_rh)
+{
+	int	portnum = 0;
+
+	while (portnum < MAUSB_ROOTHUB_NUM_PORTS) {
+
+		/* set port power bit and put in link state rx.detect */
+		mausb_rh->port_status[portnum].wPortStatus = 0x0;
+		mausb_rh->port_status[portnum].wPortStatus |=
+			USB_SS_PORT_STAT_POWER;
+		mausb_rh->port_status[portnum].wPortStatus |=
+			USB_SS_PORT_LS_RX_DETECT;
+
+		/* make sure to start with no status changes */
+		mausb_rh->port_status[portnum].wPortChange = 0x0;
+		portnum++;
+	}
+
+	mausb_rh->rh_state = MAUSB_RH_RUNNING;
+
+	return 0;
+}
+
+/**
+ * Handles GetHubStatus request to root hub.
+ *
+ * @mhcd:	MA USB hcd that owns hub.
+ * @data:	Holds hub status to return to caller.
+ *
+ * Reads hub status from mhcd and gives it back to the caller via data.
+ * Returns the number of bytes copied into data.
+ */
+static int mausb_get_hub_status(struct usb_hcd *hcd, char *data)
+{
+	struct usb_hub_status   *hub_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+	hub_stat = &roothub->status;
+	mausb_dbg(mhcd, "%s: hub status is 0x%x hub change is 0x%x\n",
+		__func__, hub_stat->wHubStatus, hub_stat->wHubChange);
+
+	((u16 *)data)[0] = cpu_to_le16(hub_stat->wHubStatus);
+	((u16 *)data)[1] = cpu_to_le16(hub_stat->wHubChange);
+
+	 return sizeof(hub_stat->wHubStatus) + sizeof(hub_stat->wHubChange);
+}
+
+/**
+ * Handles ClearHubFeature request to the root hub.
+ *
+ * @mhcd:	MA USB hcd that owns hub.
+ * @wValue:	Value that determines which feature is being cleared (see USB
+ *		3.0 spec for supported values).
+ *
+ * Returns 0 if sucessful, otherwise returns error to indicate invalid request.
+ */
+static int mausb_clear_hub_feature(struct usb_hcd *hcd, u16 wValue)
+{
+	int			err = 0;
+	struct usb_hub_status	*hub_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+	hub_stat = &roothub->status;
+
+	switch (wValue) {
+	case C_HUB_LOCAL_POWER:
+		mausb_dbg(mhcd, "clearing HUB_LOCAL_POWER\n");
+		hub_stat->wHubChange &= ~(HUB_STATUS_LOCAL_POWER);
+		break;
+	case C_HUB_OVER_CURRENT:
+		mausb_dbg(mhcd, "clearing HUB_OVER_CURRENT\n");
+		hub_stat->wHubChange &= ~(HUB_STATUS_OVERCURRENT);
+		break;
+	default:
+		err = -EINVAL;
+		break;
+	}
+
+	return err;
+}
+
+/**
+ * Handles GetPortStatus request to root hub.
+ *
+ * @mhcd:    	MA USB hcd that owns hub.
+ * @portnum:	Port number for which status is requested.
+ * @data:    	Holds port status to return to caller.
+ *
+ * Reads port status from mhcd and returns it to caller via data. Also
+ * completes a port reset.
+ *
+ * Returns the number of bytes copied into data.
+ */
+static int mausb_get_port_status(struct usb_hcd *hcd, int portnum, char *data)
+{
+	struct usb_port_status	*port_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+	/*
+	 * Note: from USB core perspective ports are 1-indexed, mhcd uses
+	 * zero-indexing.
+	 */
+	port_stat = &roothub->port_status[portnum - 1];
+
+	/*
+	 * USB core sends a GetPortStatus request to complete a port reset.
+	 * After reset, port is enabled and link state is U0 (3.0 only).
+	 */
+	if (port_stat->wPortStatus & USB_PORT_STAT_RESET) {
+		port_stat->wPortStatus &= ~(USB_PORT_STAT_RESET);
+		port_stat->wPortStatus |= USB_PORT_STAT_ENABLE;
+
+		if (mausb_is_ss_hcd(hcd))
+			set_link_state(roothub, USB_SS_PORT_LS_U0, portnum - 1);
+	}
+
+	((u16 *)data)[0] = cpu_to_le16(port_stat->wPortStatus);
+	((u16 *)data)[1] = cpu_to_le16(port_stat->wPortChange);
+
+	mausb_dbg(mhcd, "%s: Port status is 0x%x Port change is 0x%x\n",
+		__func__, ((u16 *)data)[0], port_stat->wPortChange);
+
+	return sizeof(port_stat->wPortStatus) + sizeof(port_stat->wPortChange);
+}
+
+/**
+ * Handles SetPortFeature request to root hub.
+ *
+ * @mhcd:    	MA USB hcd that owns hub.
+ * @portnum:	Port number for which feature set is requested (port
+ *		number is from usb core perspective, i.e. not zero-indexed).
+ * @wValue:	Feature to be set (see USB 3.0 spec for supported values).
+ * @wIndex:	Contains link state to be set (ignored for requests other
+ *              than set link state).
+ *
+ * Sets the bit associated with a particular feature in the status bit field
+ * of the port specified by portnum. Note that ports are zero indexed in the
+ * hub, but indexed starting with one in the usb core (e.g. port 2 from the
+ * core's perspective is port 1 to the hub).
+ *
+ * Returns zero for all supported requests. Returns an error value if a feature
+ * is not supported.
+ */
+static int mausb_set_port_feature(struct usb_hcd *hcd, int portnum, u16 wValue,
+		u16 wIndex)
+{
+	int			err = 0;
+	u8			link_state = 0;
+	struct usb_port_status	*port_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	*roothub = usb_hcd_to_roothub(hcd);
+
+	port_stat = &roothub->port_status[portnum - 1];
+
+	switch (wValue) {
+	case USB_PORT_FEAT_RESET:
+		mausb_dbg(mhcd, "setting PORT_RESET feature\n");
+		port_stat->wPortStatus |= USB_PORT_STAT_RESET;
+		port_stat->wPortChange |= USB_PORT_STAT_C_RESET;
+		break;
+	case USB_PORT_FEAT_BH_PORT_RESET:
+		mausb_dbg(mhcd, "setting BH_PORT_RESET\n");
+		break;
+	case USB_PORT_FEAT_POWER:
+		mausb_dbg(mhcd, "setting PORT_POWER feature\n");
+		port_stat->wPortStatus |= USB_SS_PORT_STAT_POWER;
+		set_link_state(roothub, USB_SS_PORT_LS_RX_DETECT, portnum - 1);
+		break;
+	case USB_PORT_FEAT_U1_TIMEOUT:
+		mausb_dbg(mhcd, "setting PORT_U1_TIMEOUT\n");
+		break;
+	case USB_PORT_FEAT_U2_TIMEOUT:
+		mausb_dbg(mhcd, "setting PORT_U2_TIMEOUT\n");
+		break;
+	case USB_PORT_FEAT_LINK_STATE:
+		link_state = wIndex >> 8;
+		if (link_state == SS_DISABLED) {
+			mausb_dbg(mhcd, "setting link state to ss.disabled\n");
+			mhcd->disabled = 1;
+			clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
+			set_link_state(roothub, USB_SS_PORT_LS_SS_DISABLED,
+				portnum - 1);
+			mausb_connect(mhcd, portnum - 1, 0);
+		}
+
+		else if (link_state == RX_DETECT) {
+			mausb_dbg(mhcd, "setting link state to rx.detect\n");
+			mhcd->disabled = 0;
+			set_link_state(roothub, USB_SS_PORT_LS_RX_DETECT,
+				portnum - 1);
+		}
+	break;
+	case USB_PORT_FEAT_REMOTE_WAKE_MASK:
+		mausb_dbg(mhcd, "setting PORT_REMOTE_WAKE_MASK\n");
+	break;
+	case USB_PORT_FEAT_FORCE_LINKPM_ACCEPT:
+		mausb_dbg(mhcd, "setting FORCE_LINKPM_ACCEPT\n");
+	break;
+	default:
+		 err = -EINVAL;
+	break;
+	}
+
+	return err;
+}
+
+/**
+ * Handles ClearPortFeature request to the root hub.
+ *
+ * @mhcd:     	MA USB hcd that owns hub.
+ * @portnum:	Port number for which feature clear is requested (port
+ *              number is from usb core perspective, i.e. not zero-indexed).
+ * @wValue:	Feature to clear (see USB 3.0 spec for supported values).
+ *
+ * Clears the bit associated with a particular feature in the status bit field
+ * of the port specified by portnum. Note that ports are zero indexed in the
+ * hub, but indexed starting with one in the usb core (e.g. port 2 from the
+ * core's perspective is port 1 to the hub).
+ *
+ * Returns zero for all supported requests. Returns an error value if a feature
+ * is not supported.
+ */
+static int mausb_clear_port_feature(struct usb_hcd *hcd, int portnum,
+		u16 wValue)
+{
+	int			 err = 0;
+	struct usb_port_status	*port_stat;
+	struct mausb_hcd	*mhcd = usb_hcd_to_mausb_hcd(hcd);
+	struct mausb_root_hub	 *roothub = usb_hcd_to_roothub(hcd);
+
+	port_stat = &roothub->port_status[portnum - 1];
+
+	switch (wValue) {
+	case USB_PORT_FEAT_POWER:
+		mausb_dbg(mhcd, "clearing PORT_POWER feature\n");
+		port_stat->wPortStatus &= ~(USB_SS_PORT_STAT_POWER);
+		break;
+	case USB_PORT_FEAT_C_CONNECTION:
+		mausb_dbg(mhcd, "clearing PORT_C_CONNECTION feature\n");
+		port_stat->wPortChange &= ~(USB_PORT_STAT_C_CONNECTION);
+		break;
+	case USB_PORT_FEAT_C_RESET:
+		mausb_dbg(mhcd, "clearing PORT_RESET feature\n");
+		port_stat->wPortStatus &= ~(USB_PORT_STAT_RESET);
+		port_stat->wPortChange &= ~(USB_PORT_STAT_C_RESET);
+		/* prevent excessive polling after reset */
+		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
+		break;
+	case USB_PORT_FEAT_C_OVER_CURRENT:
+		mausb_dbg(mhcd, "clearing PORT_OVER_CURRENT feature\n");
+		port_stat->wPortChange &= ~(USB_PORT_STAT_C_OVERCURRENT);
+	case USB_PORT_FEAT_C_PORT_LINK_STATE:
+	case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
+	case USB_PORT_FEAT_C_BH_PORT_RESET:
+		break;
+	default:
+		err = -EINVAL;
+		break;
+	}
+
+	return err;
+}
+
+/**
+ * Handles getDescriptor requests to the roothub.
+ *
+ * @ma_hcd:	MA USB hcd that owns hub.
+ * @wValue:	Contains descriptor type in upper byte (see USB 3.0 spec for
+ *              descriptor types).
+ * @buf:	Holds descriptor to return to caller.
+ * @wLength:	Expected descriptor length.
+ *
+ * If successful, returns actual length of descriptor in buf. Otherwise returns
+ * error value.
+ */
+static int mausb_get_descriptor(struct mausb_hcd *ma_hcd, u16 wValue,
+		char *buf, u16 wLength)
+{
+	int	ret;
+
+	/*
+	 * per USB spec, section 9.4.3, the DT is contained in the upper byte
+	 * of wValue
+	 */
+	u8  bDesType = (wValue >> 8);
+
+	switch (bDesType) {
+	case USB_DT_DEVICE:
+		mausb_err(ma_hcd, "USB_DT_DEVICE not supported\n");
+		ret = -EPIPE;
+		break;
+	case USB_DT_CONFIG:
+		ret = 0;
+		break;
+	case USB_DT_BOS:
+		ret = mausb_get_bos(ma_hcd, (struct mausb_host_bos *) buf,
+			wLength);
+		mausb_dbg(ma_hcd, "%s: bos copied to buffer, wTotalLength %x\n",
+			__func__,
+			((struct mausb_host_bos *) buf)->bos_des.wTotalLength);
+		break;
+	case USB_DT_STRING:
+		ret = 0;
+		break;
+	default: /* invalid request */
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+/**
+ * Handles requests from usb core to hub.
+ *
+ * @hcd:	USB hcd that owns hub.
+ * @typeReq:	Request type (see USB 3.0 spec for request types).
+ * @wValue:	Feature selector for hub/port feature requests,
+ *		descriptor type and index for hub descriptor requests,
+ *		hub depth for hub depth requests,zero for all other requests.
+ * @wIndex:	Port number for port requests, zero or language id for hub
+ *		descriptor requests, zero for all other requests.
+ * @buf:	Buffer for requested descriptor or status (if applicable).
+ * @wLength:	Descriptor length for descriptor requests, otherwise zero.
+ *
+ * Called by the USB core to make a request to the root hub. mausb_hub_control
+ * parses the request and routes it to the appropriate mausb helper function.
+ *
+ * If successful, returns descriptor length for descriptor request or zero for
+ * other requests. Otherwise returns error value.
+ */
+int mausb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
+		u16 wIndex, char *buf, u16 wLength)
+{
+	int		err = 0;
+	int		portnum = (wIndex & 0xFF);
+	struct		mausb_hcd *mhcd = usb_hcd_to_mausb_hcd(hcd);
+
+	if (portnum > MAUSB_ROOTHUB_NUM_PORTS) {
+		mausb_err(mhcd, "port number %d is invalid\n", portnum);
+		return -EINVAL;
+	}
+
+	switch (typeReq) {
+	case MAUSB_REQ_GET_DES:
+		err = mausb_get_descriptor(usb_hcd_to_mausb_hcd(hcd),
+			wValue, buf, wLength);
+		err = sizeof(struct mausb_host_bos);
+		break;
+	case ClearHubFeature:
+		mausb_dbg(mhcd, "clearing hub feature %d\n", wValue);
+		err = mausb_clear_hub_feature(hcd, wValue);
+		break;
+	case ClearPortFeature:
+		mausb_dbg(mhcd, "clearing port %d feature %d\n", portnum,
+			wValue);
+		err = mausb_clear_port_feature(hcd, portnum, wValue);
+		break;
+	case GetHubDescriptor:
+		err = mausb_get_hub_descriptor(hcd,
+			(struct usb_hub_descriptor *) buf, wLength);
+		break;
+	case GetHubStatus:
+		mausb_dbg(mhcd, "getting hub status\n");
+		err = mausb_get_hub_status(hcd, buf);
+		break;
+	case GetPortStatus:
+		mausb_dbg(mhcd, "getting port %d status\n", portnum);
+		err = mausb_get_port_status(hcd, portnum, buf);
+		break;
+	/* SetHubFeature support is optional (only used for diagnostics) */
+	case SetHubFeature:
+		err = 0;
+		break;
+	case SetPortFeature:
+		mausb_dbg(mhcd, "setting port %d feature %d\n", portnum,
+			wValue);
+		mausb_set_port_feature(hcd, portnum, wValue, wIndex);
+		break;
+	case SetHubDepth:
+		err = 0;
+		break;
+	case GetPortErrorCount:
+		err = 0;
+		break;
+	default:
+		err = -EINVAL;
+		break;
+	}
+
+	return err;
+}
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_AUTHOR("Intel");
+MODULE_DESCRIPTION("mausb driver");
drivers/staging/mausb/drivers/mausb_hub.h new file +128 -0

128 lines not on this page · raw message

Signature · 1 line
1.9.1
#11 lore raw

PATCH 3/10 added media agnostic (MA) data structures and handling

In reply to Stephanie Wallick

This is where we create, store and handle endpoint and device structures that are specific to the MA USB drivers. Each MA USB structure maps 1:1 with it's corresponding USB structure (e.g. there is one MA USB endpoint per USB endpoint).

---
 drivers/staging/mausb/drivers/mausb_const.h    | 109 ++++
 drivers/staging/mausb/drivers/mausb_mem-host.c | 404 ++++++++++++
 drivers/staging/mausb/drivers/mausb_mem-host.h |  74 +++
 drivers/staging/mausb/drivers/mausb_mem.c      | 844 +++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_mem.h      | 509 +++++++++++++++
 drivers/staging/mausb/drivers/mausb_state.h    | 184 ++++++
 6 files changed, 2124 insertions(+)
 create mode 100755 drivers/staging/mausb/drivers/mausb_const.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem-host.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mem.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_state.h
drivers/staging/mausb/drivers/mausb_const.h new file +109 -0
new file mode 100755
index 0000000..1089f81
@@ -0,0 +1,109 @@
+/* Name:	mausb_const.h
+ * Description: This header describes the Media Agnostic USB constants
+ *              that must be known by the both the host and device side drivers.
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MAUSB_CONST_H
+#define __MAUSB_CONST_H
+
+/* MA USB protocol constants per Table 68 of the MA USB Spec */
+#define MAUSB_DATA_CHANNEL_DELAY	400	/* temp value */
+#define MAUSB_MGMT_CHANNEL_DELAY	((100 * HZ)/1000 + 1)
+#define MAUSB_MGMT_RESPONSE_DELAY	((5 * HZ)/1000 + 1) /* 5 msec */
+#define MAUSB_MGMT_RT_DELAY \
+	((MAUSB_MGMT_RESPONSE_DELAY + (2 * MAUSB_MGMT_CHANNEL_DELAY)))
+#define MAUSB_TRANSFER_RESPONSE_TIME	10	/* 10 msec */
+#define MAUSB_TRANSFER_TIMEOUT \
+	(MAUSB_TRANSFER_RESPONSE_TIME + (2 * MAUSB_DATA_CHANNEL_DELAY))
+#define MAUSB_TRANSFER_KEEP_ALIVE \
+	(MAUSB_TRANSFER_RESPONSE_TIME + MAUSB_DATA_CHANNEL_DELAY)
+#define MAUSB_DEFAULT_KEEP_ALIVE	 0
+#define MAUSB_MAX_TRANSFER_LIFETIME	 1000	/* 1 sec */
+#define MAUSB_TRANSFER_REPEAT_TIME	 10	/* 10 msec */
+
+#define MAUSB_MAX_REQ_ID		 ((1 << 8) - 1)
+#define MAUSB_MAX_SEQ_NUM		 ((1 << 24) - 1)
+#define MAUSB_MAX_DIALOG_TOKEN		 ((1 << 10) - 1)
+#define MAUSB_MIN_CONTROL_BUF_SIZE	 4104	/* Bytes */
+
+
+/* MA USB protocol variables per Table 69 of the MA USB Spec */
+#define MAUSB_BULK_TRANSFER_RETRIES	 10	/* min value is 5 */
+#define MAUSB_CONTROL_TRANSFER_RETRIES	 10	/* min value is 5 */
+#define MAUSB_INTERRUPT_TRANSFER_RETRIES 10	/* min value is 3 */
+#define MAUSB_MGMT_TRANSFER_RETRIES	 4      /* min value is 4 */
+#define MAUSB_TRANSFER_SETUP_RETRIES	 4	/* min value is 4 */
+
+
+/* MA USB constants not explicitly defined in MA USB Spec */
+#define MAUSB_HALF_REQ_ID		 ((MAUSB_MAX_REQ_ID + 1) >> 2)
+#define MAUSB_HALF_SEQ_NUM		 ((MAUSB_MAX_SEQ_NUM + 1) >> 2)
+#define MAUSB_MAX_OUTSTANDING_SEQ_NUM	 (1 << 23)
+
+/* Largest packet size (in bytes) that the medium can handle */
+#define MAUSB_MAX_PACKET_SIZE	300
+
+/* Used to parse get_status control requests */
+#define EP_REQ (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
+#define EP_IN_REQ (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
+#define DEV_IN_REQ (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
+#define INTF_IN_REQ (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
+
+/* Used for generating the mass_id for the device */
+#define MASS_ID_MIN	1
+#define MASS_ID_MAX	254
+
+
+#endif
drivers/staging/mausb/drivers/mausb_mem-host.c new file +404 -0

404 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_mem-host.h new file +74 -0

74 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_mem.c new file +844 -0

844 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_mem.h new file +509 -0

509 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_state.h new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
1.9.1
#12 lore raw

PATCH 4/10 added media agnostic (MA) USB packet handling

In reply to Stephanie Wallick

This is where we handle MA USB packets. The structure and types of MA USB packets are defined in the MA USB specification. When an MA USB driver receives a USB packet, it translates it into a MA USB packet (or packets if urb exceeds maximum USB packet size). When an MA USB packet is received, the data from that packet is given to the waiting USB packet and the USB packet is released.

---
 drivers/staging/mausb/drivers/mausb_pkt.c | 1038 +++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_pkt.h |  914 +++++++++++++++++++++++++
 2 files changed, 1952 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_pkt.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_pkt.h
drivers/staging/mausb/drivers/mausb_pkt.c new file +1038 -0
new file mode 100644
index 0000000..c78cfc2
@@ -0,0 +1,1038 @@
+/* name:	mausb_pkt.c
+ * description: MA USB packet helper functions
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/slab.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb.h>
+
+#include "mausb_mem.h"
+#include "mausb_pkt.h"
+#include "mausb_const.h"
+
+/**
+ * Translates numerical packet type into corresponding string (for printing).
+ */
+const char *mausb_type_to_string(enum mausb_pkt_type type)
+{
+	switch (type) {
+	case CapReq:
+		return "CapReq";
+	case CapResp:
+		return "CapResp";
+	case USBDevHandleReq:
+		return "USBDevHandleReq";
+	case USBDevHandleResp:
+		return "USBDevHandleResp";
+	case EPHandleReq:
+		return "EPHandleReq";
+	case EPHandleResp:
+		return "EPHandleResp";
+	case EPActivateReq:
+		return "EPActivateReq";
+	case EPActivateResp:
+		return "EPActivateResp";
+	case EPInactivateReq:
+		return "EPInactivateReq";
+	case EPInactivateResp:
+		return "EPInactivateResp";
+	case EPResetReq:
+		return "EPResetReq";
+	case EPResetResp:
+		return "EPResetResp";
+	case EPClearTransferReq:
+		return "EPClearTransferReq";
+	case EPClearTransferResp:
+		return "EPClearTransferResp";
+	case EPHandleDeleteReq:
+		return "EPHandleDeleteReq";
+	case EPHandleDeleteResp:
+		return "EPHandleDeleteResp";
+	case DevResetReq:
+		return "DevResetReq";
+	case DevResetResp:
+		return "DevResetResp";
+	case ModifyEP0Req:
+		return "ModifyEP0Req";
+	case ModifyEP0Resp:
+		return "ModifyEP0Resp";
+	case SetUSBDevAddrReq:
+		return "SetUSBDevAddrReq";
+	case SetUSBDevAddrResp:
+		return "SetUSBDevAddrResp";
+	case UpdateDevReq:
+		return "UpdateDevReq";
+	case UpdateDevResp:
+		return "UpdateDevResp";
+	case USBDevDisconnectReq:
+		return "USBDevDisconnectReq";
+	case USBDevDisconnectResp:
+		return "USBDevDisconnectResp";
+	case USBSuspendReq:
+		return "USBSuspendReq";
+	case USBSuspendResp:
+		return "USBSuspendResp";
+	case USBResumeReq:
+		return "USBResumeReq";
+	case USBResumeResp:
+		return "USBResumeResp";
+	case RemoteWakeReq:
+		return "RemoteWakeReq";
+	case RemoteWakeResp:
+		return "RemoteWakeResp";
+	case PingReq:
+		return "PingReq";
+	case PingResp:
+		return "PingResp";
+	case DevDisconnectReq:
+		return "DevDisconnectReq";
+	case DevDisconnectResp:
+		return "DevDisconnectResp";
+	case DevInitDisconnectReq:
+		return "DevInitDisconnectReq";
+	case DevInitDisconnectResp:
+		return "DevInitDisconnectResp";
+	case SyncReq:
+		return "SyncReq";
+	case SyncResp:
+		return "SyncResp";
+	case CancelTransferReq:
+		return "CancelTransferReq";
+	case CancelTransferResp:
+		return "CancelTransferResp";
+	case EPOpenStreamReq:
+		return "EPOpenStreamReq";
+	case EPOpenStreamResp:
+		return "EPOpenStreamResp";
+	case EPCloseStreamReq:
+		return "EPCloseStreamReq";
+	case EPCloseStreamResp:
+		return "EPCloseStreamResp";
+	case USBDevResetReq:
+		return "USBDevResetReq";
+	case USBDevResetResp:
+		return "USBDevResetResp";
+	case DevNotificationReq:
+		return "DevNotificationReq";
+	case DevNotificationResp:
+		return "DevNotificationResp";
+	case EPSetKeepAliveReq:
+		return "EPSetKeepAliveReq";
+	case EPSetKeepAliveResp:
+		return "EPSetKeepAliveResp";
+	case GetPortBWReq:
+		return "GetPortBWReq";
+	case GetPortBWResp:
+		return "GetPortBWResp";
+	case SleepReq:
+		return "SleepReq";
+	case SleepResp:
+		return "SleepResp";
+	case WakeReq:
+		return "WakeReq";
+	case WakeResp:
+		return "WakeResp";
+	case VendorSpecificReq:
+		return "VendorSpecificReq";
+	case VendorSpecificResp:
+		return "VendorSpecificResp";
+	case TransferSetupReq:
+		return "TransferSetupReq";
+	case TransferSetupResp:
+		return "TransferSetupResp";
+	case TransferTearDownConf:
+		return "TransferTearDownConf";
+	case TransferReq:
+		return "TransferReq";
+	case TransferResp:
+		return "TransferResp";
+	case TransferAck:
+		return "TransferAck";
+	case IsochTransferReq:
+		return "IsochTransferReq";
+	case IsochTransferResp:
+		return "IsochTransferResp";
+	}
+
+	return "unknown";
+}
+EXPORT_SYMBOL(mausb_type_to_string);
+
+/**
+ * Translates a negative errno to a MAUSB status. Returns a packet status value.
+ */
+enum mausb_pkt_status mausb_errno_to_ma_status(int errno)
+{
+	switch (errno) {
+	case 0: /* no error */
+		return SUCCESS;
+	case -EINPROGRESS:
+		return TRANSFER_PENDING;
+	case -ESTRPIPE:
+		return TRANSFER_EP_STALL;
+	/* TODO: map all relevant ma status' */
+	default:
+		return UNSUCCESSFUL;
+	} /* end switch */
+
+}
+EXPORT_SYMBOL(mausb_errno_to_ma_status);
+
+/**
+ * Translates an URB status to a MAUSB status
+ */
+int mausb_to_urb_status(enum mausb_pkt_status pkt_status)
+{
+	switch (pkt_status) {
+	case SUCCESS: /* no error */
+		return 0;
+	case TRANSFER_PENDING:
+		return -EINPROGRESS;
+	case TRANSFER_EP_STALL:
+		return -ESTRPIPE;
+	/* TODO: map all relevant urb statuses */
+	default:
+		return -EIO; /* generic I/O error */
+	} /* end switch */
+
+}
+EXPORT_SYMBOL(mausb_to_urb_status);
+
+/**
+ * Determines if the packet is a response packet. Returns true if the packet is
+ * a response packet. Returns false otherwise.
+ */
+bool mausb_pkt_resp(struct mausb_pkt *pkt)
+{
+	return pkt->common->pkt_type & MAUSB_PKT_RESP_FLAG;
+}
+
+/**
+ * Determines if a given packet is a management packet.
+ */
+static bool mausb_pkt_is_mgmt(struct mausb_pkt_common *common)
+{
+	if ((common->pkt_type & MAUSB_PKT_TYPE_MASK) == MAUSB_PKT_TYPE_MGMT)
+		return true;
+	else
+		return false;
+}
+
+
+/**
+ * Returns the sum of the sequnce number & the value added. Handles sequence
+ * number wraparound.
+ */
+u32 mausb_seq_num_add(u32 seq_num, int val)
+{
+	return (seq_num + val) % (MAUSB_MAX_SEQ_NUM + 1);
+}
+EXPORT_SYMBOL(mausb_seq_num_add);
+
+/**
+ * Increments a sequence number value by one.
+ */
+void mausb_increment_seq_num(u32 *seq_num)
+{
+	*seq_num = mausb_seq_num_add(*seq_num, 1);
+}
+
+/**
+ * Compares two sequence numbers. Returns true if a is less than b.
+ * Handles sequence number wraparound.
+ */
+bool mausb_seq_num_lt(u32 a, u32 b)
+{
+
+	if (a > MAUSB_MAX_SEQ_NUM || b > MAUSB_MAX_SEQ_NUM) {
+		mapkt_warn("%s: input out of range\n", __func__);
+		return false;
+	}
+
+	if (b < MAUSB_HALF_SEQ_NUM) { /* b is in the lower half */
+
+		if ((b > a) || ((b + MAUSB_HALF_SEQ_NUM) < a))
+			return true;
+		else
+			return false;
+
+	} else { /* b is in the upper half */
+
+		if ((b > a) && (b - MAUSB_HALF_SEQ_NUM) < a)
+			return true;
+		else
+			return false;
+	}
+
+	/* we should never get here */
+	BUG();
+	return false;
+}
+EXPORT_SYMBOL(mausb_seq_num_lt);
+
+/**
+ * Sequence number less than or equal to.
+ */
+bool mausb_seq_num_lt_eq(u32 a, u32 b)
+{
+	return mausb_seq_num_lt(b, a) ? false : true;
+}
+
+/**
+ * Sequence number greater than or equal to.
+ */
+bool mausb_seq_num_gt_eq(u32 a, u32 b)
+{
+	return mausb_seq_num_lt(a, b) ? false : true;
+}
+
+/**
+ * Sequence number greater than.
+ */
+bool mausb_seq_num_gt(u32 a, u32 b)
+{
+	return mausb_seq_num_lt(b, a);
+}
+EXPORT_SYMBOL(mausb_seq_num_gt);
+
+/**
+ * Returns the sum of the request id and the value added. Handles request id
+ * wraparound.
+ */
+u8 mausb_req_id_add(u8 req_id, int val)
+{
+	return (req_id + val) % (MAUSB_MAX_REQ_ID + 1);
+}
+EXPORT_SYMBOL(mausb_req_id_add);
+
+/**
+ * Compares 2 request IDs. Returns true if a is less than b. Handles request id
+ * wraparound.
+ */
+bool mausb_req_id_lt(u8 a, u8 b)
+{
+
+	if (a > MAUSB_MAX_REQ_ID || b > MAUSB_MAX_REQ_ID) {
+		mapkt_warn("%s: input out of range\n", __func__);
+		return false;
+	}
+
+	if (b < MAUSB_HALF_REQ_ID) { /* b is in the lower half */
+
+		if ((b > a) || ((b + MAUSB_HALF_REQ_ID) < a))
+			return true;
+		else
+			return false;
+
+	} else { /* b is in the upper half */
+
+		if ((b > a) && (b - MAUSB_HALF_REQ_ID) < a)
+			return true;
+		else
+			return false;
+	}
+
+	/* we should never get here */
+	BUG();
+	return false;
+}
+EXPORT_SYMBOL(mausb_req_id_lt);
+
+bool mausb_req_id_gt(u8 a, u8 b)
+{
+	return mausb_req_id_lt(b, a);
+}
+
+bool mausb_req_id_gt_eq(u8 a, u8 b)
+{
+	return mausb_req_id_lt(a, b) ? false : true;
+}
+EXPORT_SYMBOL(mausb_req_id_gt_eq);
+
+/**
+ * Finds an endpoint descriptor inside of an endpoint handle request
+ */
+struct usb_endpoint_descriptor *mausb_ep_handle_req_get_ep_des(
+	struct mausb_EPHandleReq_flds *req, int i)
+{
+
+	if (i >= le32_to_cpu(req->num_ep_des)) {
+		mapkt_warn("%s: endpoint descriptor not in packet\n", __func__);
+		return NULL;
+	}
+
+	if (MAUSB_EP_REQ_SIZE == le32_to_cpu(req->size_ep_des)) {
+		return (struct usb_endpoint_descriptor *)
+					&req->ep_des[i].ep_des;
+
+	} else if (MAUSB_SS_EP_REQ_SIZE == le32_to_cpu(req->size_ep_des)) {
+		return (struct usb_endpoint_descriptor *)
+					 &req->ss_ep_des[i].ep_des;
+
+	} else {
+		mapkt_err("%s: unsupported size_ep_des value (%u)\n",
+			  __func__, le32_to_cpu(req->size_ep_des));
+	}
+
+	return NULL;
+}
+EXPORT_SYMBOL(mausb_ep_handle_req_get_ep_des);
+
+static struct mausb_pkt_common *mausb_header_from_ms_pkt(struct ms_pkt *pkt)
+{
+	struct mausb_pkt_common *header;
+
+	if (pkt->kvec[0].iov_len < sizeof(*header)) {
+		mapkt_err("%s: first entry too small for header\n", __func__);
+		header = NULL;
+	} else
+		header = pkt->kvec[0].iov_base;
+
+	return header;
+}
+
+/**
+ * Accepts an MA USB packet for the MA device and forwards it to the correct
+ * handler based on packet type and endpoint/device handle.
+ */
+int mausb_pkt_dmux(struct ms_pkt *pkt, void *context)
+{
+	int				ret = -EINVAL;
+	struct mausb_pkt_common		*header = mausb_header_from_ms_pkt(pkt);
+	struct ma_dev			*ma_dev = context_to_ma_dev(context);
+	struct mausb_host_ep		*ma_ep = NULL;
+	struct mausb_pkt_transfer	*transfer = NULL;
+
+	if (mausb_pkt_is_mgmt(header)) {
+		transfer = &ma_dev->mgmt.tx_pair.to_ma;
+
+	} else {
+		/* otherwise, find the endpoint it belongs to */
+		ma_ep = mausb_find_ep_from_handle(header->ep_handle,
+			ma_dev);
+
+		if (NULL != ma_ep)
+			transfer = &ma_ep->tx_pair.to_ma;
+	}
+
+	if (NULL != transfer)
+		ret = transfer->transfer_packet(pkt, transfer->context);
+
+	return ret;
+}
+
+/**
+ * Packet destructor for packets generated by the MA driver. For packets that we
+ * have created, we want to free the header but not the data buffers (since the
+ * buffers belong to the URB).
+ */
+static void mausb_pkt_destructor(struct ms_pkt *ms_pkt)
+{
+	struct mausb_pkt *ma_pkt = ms_pkt_to_mausb_pkt(ms_pkt);
+
+	kfree(ma_pkt->common);
+
+	return;
+}
+
+/**
+ * Allocates space for given packet type.
+ */
+struct mausb_pkt *mausb_alloc_pkt(enum mausb_pkt_type pkt_type, int *status,
+		gfp_t memflags)
+{
+	int			size;
+	int			ret = 0;
+	struct mausb_pkt	*pkt;
+
+	/* determine header length */
+	switch (pkt_type & MAUSB_PKT_TYPE_MASK) {
+
+	case MAUSB_PKT_TYPE_MGMT:
+		size = sizeof(struct mausb_mgmt_pkt);
+		break;
+	case MAUSB_PKT_TYPE_CTRL:
+		size = sizeof(struct mausb_ctrl_pkt);
+		break;
+	case MAUSB_PKT_TYPE_DATA:
+		size = sizeof(struct mausb_iso_dph);
+		break;
+	default:
+		mapkt_err("%s: could not determine packet type\n", __func__);
+		if (NULL != status)
+			*status = -EINVAL;
+		return NULL;
+	}
+
+	pkt = kzalloc(sizeof(struct mausb_pkt), memflags);
+	if (NULL == pkt) {
+		ret = -ENOMEM;
+		pkt = NULL;
+	} else {
+		pkt->common = kzalloc(size, memflags);
+		if (NULL == pkt->common) {
+			mapkt_err("could not allocate memory for MA USB"
+				" packet header of length %i\n", size);
+			kfree(pkt);
+			ret = -ENOMEM;
+			pkt = NULL;
+		}
+
+		pkt->ms_pkt_destructor = &mausb_pkt_destructor;
+		pkt->orig_pkt = &pkt->pkt;
+	}
+
+	if (NULL != status)
+		*status = ret;
+
+	return pkt;
+}
+
+/**
+ * Deletes the given data packet
+ */
+void mausb_free_pkt(struct mausb_pkt *pkt)
+{
+	/* kill the buffers (when necessary) */
+	pkt->ms_pkt_destructor(pkt->orig_pkt);
+	kfree(pkt);
+}
+EXPORT_SYMBOL(mausb_free_pkt);
+
+/**
+ * Returns the length of the header or a negative errno.
+ */
+int mausb_pkt_header_length(struct mausb_pkt_common *header)
+{
+	int length;
+
+	switch (header->pkt_type & MAUSB_PKT_TYPE_MASK) {
+
+	case MAUSB_PKT_TYPE_MGMT:
+	case MAUSB_PKT_TYPE_CTRL:
+		length = le16_to_cpu(header->length);
+		break;
+	case MAUSB_PKT_TYPE_DATA:
+		length = sizeof(struct mausb_dph);
+		break;
+	default:
+		mapkt_err("%s:invalid packet type\n", __func__);
+		length = -EINVAL;
+		break;
+	}
+
+	return length;
+}
+EXPORT_SYMBOL(mausb_pkt_header_length);
+
+/**
+ * Returns a pointer to a buffer of specified length that is
+ * found offset a given number of bytes into the ms_pkt.
+ *
+ * If a contigious buffer can be found, this function returns
+ * the virtual address of this buffer. otherwise, it returns null
+ */
+static void *mausb_get_buf_from_ms_pkt(struct ms_pkt *pkt,
+		int offset, int buf_length)
+{
+	int		i;
+	int		page_offset = offset;
+	char		*ret = NULL;
+	struct kvec	*current_kvec;
+
+	for (i = 0; i < pkt->nents; ++i) {
+		current_kvec = &pkt->kvec[i];
+
+		/* if the buffer is in this kvec entry */
+		if (page_offset < current_kvec->iov_len) {
+
+			/* and the whole buffer is in current_kvec */
+			if (page_offset + buf_length <= current_kvec->iov_len) {
+				ret = (char *) current_kvec->iov_base;
+				ret += page_offset;
+			}
+
+			return ret;
+		}
+
+		page_offset -= current_kvec->iov_len;
+	}
+
+	if (0 != page_offset) {
+		mapkt_err("%s: offset of %i Bytes is greater than packet"
+			" length\n", __func__, page_offset);
+	}
+	return NULL;
+}
+
+bool mausb_pkt_has_setup_data(struct mausb_dph *header)
+{
+	/*
+	 * Only control transfer requests with sequence number 0 should have
+	 * setup data - nothing else should ever carry setup data.
+	 */
+	if (header->common.pkt_type == TransferReq) {
+		if ((header->eps_tflags & MAUSB_PKT_TFLAG_TYPE_MASK)
+			== MAUSB_PKT_TFLAG_TYPE_CTRL
+			&& le32_to_cpu(header->seq_num) == 0)
+			return true;
+	}
+
+	return false;
+}
+EXPORT_SYMBOL(mausb_pkt_has_setup_data);
+
+/**
+ * Gets an mausb_pkt structure from a given ms_pkt structure.
+ *
+ * can only be used if this driver created the packet i.e. if you just received
+ * an ms_pkt from another machine, it will not be part of a mausb_pkt.
+ */
+struct mausb_pkt *ms_pkt_to_mausb_pkt(struct ms_pkt *ms_pkt)
+{
+	return container_of(ms_pkt, struct mausb_pkt, pkt);
+}
+
+/**
+ * Creates a mausb_pkt structure from a given ms_pkt structure. Used for parsing
+ * incoming packets.
+ */
+struct mausb_pkt *mausb_pkt_from_ms_pkt(struct ms_pkt *ms_pkt,
+	void (*ms_pkt_destructor)(struct ms_pkt *ms_pkt), gfp_t mem_flags)
+{
+	struct mausb_pkt	*pkt;
+	int			current_offset;
+	int			packet_length;
+
+	pkt = kzalloc(sizeof(struct mausb_pkt), mem_flags);
+	if (NULL == pkt)
+		return NULL;
+
+	pkt->pkt = *ms_pkt;
+	pkt->orig_pkt = ms_pkt;
+	pkt->ms_pkt_destructor = ms_pkt_destructor;
+
+	/* the first portion is always the header */
+	pkt->common = mausb_header_from_ms_pkt(ms_pkt);
+	current_offset = mausb_pkt_header_length(pkt->common);
+	packet_length = pkt->common->length;
+
+	/* checks to see if header is contigious */
+	if (NULL == mausb_get_buf_from_ms_pkt(ms_pkt, 0, current_offset)) {
+
+		mapkt_err("%s: packet header bigger than entry (header is %i"
+			" bytes)\n", __func__, current_offset);
+		kfree(pkt);
+		return NULL;
+	}
+
+	/*
+	 * NOTE: Currently control and management packets must be contigious.
+	 *       data packet headers must also be contigious.
+	 *       (ie: the data must all be in 1 ms_pkt entry).
+	 *
+	 * TODO: handle when a control/management is in more than 1
+	 *       kvec entry
+	 */
+
+	if (MAUSB_PKT_TYPE_DATA ==
+		(pkt->common->pkt_type & MAUSB_PKT_TYPE_MASK)) {
+
+		if (mausb_pkt_has_setup_data(pkt->data)) {
+			pkt->setup = mausb_get_buf_from_ms_pkt(ms_pkt,
+				current_offset, sizeof(*pkt->setup));
+
+			if (NULL == pkt->setup) {
+				mapkt_err("%s: could not get setup data\n",
+					__func__);
+			} else {
+				current_offset += sizeof(*pkt->setup);
+			}
+		}
+
+		if (packet_length > current_offset) {
+			pkt->buffer = mausb_get_buf_from_ms_pkt(ms_pkt,
+				current_offset, packet_length - current_offset);
+
+			/*
+			 * if the remaining data was contigious, treat it as a
+			 * buffer
+			 */
+			if (NULL != pkt->buffer) {
+				pkt->buffer_length =
+					packet_length - current_offset;
+				current_offset += pkt->buffer_length;
+			}
+
+		} else if (packet_length < current_offset) {
+			mapkt_dbg("%s: packet_length:%i, current_offset:%i\n",
+				__func__, packet_length, current_offset);
+		}
+	}
+
+	/* checks to see if there is data after packet */
+	if (NULL != mausb_get_buf_from_ms_pkt(ms_pkt, current_offset, 0))
+		mapkt_warn("%s: Warning: data exists after packet\n", __func__);
+
+	return pkt;
+
+}
+EXPORT_SYMBOL(mausb_pkt_from_ms_pkt);
+
+/**
+ * Fills the ms_pkt of a given mausb_pkt based on fields in the mausb_pkt struct.
+ */
+int mausb_pkt_fill_ms_pkt(struct mausb_pkt *pkt, gfp_t mem_flags)
+{
+	struct ms_pkt		*ms_pkt = &pkt->pkt;
+	unsigned int		ent_length;
+
+	ms_pkt->nents = 0;
+
+	if (NULL != pkt->common) {
+		/* add the header */
+		ent_length = mausb_pkt_header_length(pkt->common);
+		if (0 >= ent_length) {
+			return -EINVAL;
+		}
+
+	} else { /* We should ALWAYS have a header, and we dont... */
+		mapkt_err("%s: header not found\n", __func__);
+		return -EINVAL;
+	}
+
+	ms_pkt->kvec[ms_pkt->nents].iov_base = pkt->common;
+	ms_pkt->kvec[ms_pkt->nents].iov_len = ent_length;
+	ms_pkt->nents += 1;
+
+	/* If this packet should have data, send it */
+	if (pkt->common->pkt_type == TransferReq ||
+		pkt->common->pkt_type == TransferResp){
+
+		/* add the setup field */
+		if (mausb_pkt_has_setup_data(pkt->data)) {
+			ms_pkt->kvec[ms_pkt->nents].iov_base = pkt->setup;
+			ms_pkt->kvec[ms_pkt->nents].iov_len =
+						sizeof(*pkt->setup);
+			ms_pkt->nents += 1;
+
+		} else if (NULL != pkt->setup) {
+			mapkt_warn("%s: Warning: packet has Setup field, but "
+				"flags in header are not set\n", __func__);
+		}
+
+		if (NULL != pkt->buffer && pkt->nents > 0) {
+			mapkt_err("%s: packet has scatterlist and databuffer\n"
+				, __func__);
+			return -EINVAL;
+		}
+
+		/* add the data buffer */
+		if (NULL != pkt->buffer) {
+			ms_pkt->kvec[ms_pkt->nents].iov_base = pkt->buffer;
+			ms_pkt->kvec[ms_pkt->nents].iov_len =
+				pkt->buffer_length;
+			ms_pkt->nents += 1;
+		}
+	}
+
+	mapkt_dbg("%s: pkt filled with %i entries\n", __func__, ms_pkt->nents);
+
+	return 0;
+}
+
+/**
+ * Calculates the total length of data contained in an ms_pkt (in bytes).
+ * Returns the length of the kvec, or 0 on an error.
+ */
+static int mausb_ms_data_length(struct ms_pkt *pkt)
+{
+	int		i;
+	int		total_length;
+	struct kvec	*current_kvec;
+
+	for (i = 0; i < pkt->nents; ++i) {
+		current_kvec = &pkt->kvec[i];
+		if (NULL == current_kvec)
+			return -EINVAL;
+		else
+			total_length += current_kvec->iov_len;
+	}
+
+	return total_length;
+}
+
+/**
+ * Calculates the length of the datafield in a given datapacket.
+ */
+static int mausb_pkt_datafield_length(struct mausb_pkt *pkt)
+{
+	int length = 0;
+
+	if (NULL != pkt->buffer)
+		length += pkt->buffer_length;
+	if (0 != pkt->nents)
+		length += mausb_ms_data_length(&pkt->pkt);
+
+	return length;
+}
+
+/**
+ * Parses the packet fields to determine how long the packet is. Returns the
+ * packet length on sucess, or a negative errno on error.
+ */
+int mausb_pkt_length(struct mausb_pkt *pkt)
+{
+	int length;
+
+	/* all packets have this field */
+	switch (pkt->common->pkt_type & MAUSB_PKT_TYPE_MASK) {
+
+	/* Management packets */
+	case MAUSB_PKT_TYPE_MGMT:
+		length = sizeof(*pkt->common);
+		length += MAUSB_MGMT_TOKEN_PAD_LEN;
+
+		/* subtypes without additional data */
+		switch (pkt->common->pkt_type) {
+		case DevResetReq:
+		case DevResetResp:
+		case UpdateDevResp:
+		case DevDisconnectReq:
+		case DevDisconnectResp:
+		case USBDevDisconnectReq:
+		case USBDevDisconnectResp:
+		case SleepReq:
+		case SleepResp:
+		case WakeReq:
+		case WakeResp:
+		case RemoteWakeReq:
+		case RemoteWakeResp:
+		case PingReq:
+		case PingResp:
+		case DevInitDisconnectReq:
+		case DevInitDisconnectResp:
+		case SyncResp:
+			break;
+
+		/* subtypes with constant length additional data */
+		case CapReq:
+			length += sizeof(pkt->mgmt->cap_req);
+			break;
+		case CapResp:
+			length += sizeof(pkt->mgmt->cap_resp);
+			/* also include length of any descriptors */
+			length += pkt->mgmt->cap_resp.desc_length;
+			break;
+		case USBDevHandleReq:
+			length += sizeof(pkt->mgmt->usb_dev_handle_req);
+			break;
+		case USBDevHandleResp:
+			length += sizeof(pkt->mgmt->usb_dev_handle_resp);
+			break;
+		case ModifyEP0Req:
+			length += sizeof(pkt->mgmt->modify_ep0_req);
+			break;
+		case ModifyEP0Resp:
+			length += sizeof(pkt->mgmt->modify_ep0_resp);
+			break;
+		case SetUSBDevAddrReq:
+			length += sizeof(pkt->mgmt->set_dev_addr_req);
+			break;
+		case SetUSBDevAddrResp:
+			length += sizeof(pkt->mgmt->set_dev_addr_resp);
+			break;
+		case UpdateDevReq:
+			length += sizeof(pkt->mgmt->update_dev_req);
+			break;
+		case SyncReq:
+			length += sizeof(pkt->mgmt->synch_req);
+			break;
+		case EPCloseStreamReq:
+			length += sizeof(pkt->mgmt->ep_close_stream_req);
+			break;
+		case CancelTransferReq:
+			length += sizeof(pkt->mgmt->cancel_transfer_req);
+			break;
+		case CancelTransferResp:
+			length += sizeof(pkt->mgmt->cancel_transfer_resp);
+			break;
+		case EPOpenStreamReq:
+			length += sizeof(pkt->mgmt->ep_open_stream_req);
+			break;
+		/* subtypes with variable length additional data */
+		case EPHandleReq:
+			length += sizeof(__u32);
+			length += pkt->mgmt->ep_handle_req.num_ep_des *
+				pkt->mgmt->ep_handle_req.size_ep_des;
+			break;
+		case EPHandleResp:
+			length += sizeof(__u32);
+			length += pkt->mgmt->ep_handle_resp.num_ep_des *
+				sizeof(struct mausb_ep_des);
+			break;
+		case EPActivateReq:
+		case EPActivateResp:
+		case EPInactivateReq:
+		case EPInactivateResp:
+		case EPResetReq:
+		case EPResetResp:
+		case EPClearTransferReq:
+		case EPClearTransferResp:
+		case EPHandleDeleteReq: /* FALLTHROUGH */
+		case EPHandleDeleteResp:
+			length += sizeof(__u32);
+			length += pkt->mgmt->ep_handle_delete.num_ep
+				* sizeof(struct mausb_ep_handle);
+			break;
+		case EPCloseStreamResp:
+		case USBDevResetReq:
+		case USBDevResetResp:
+		case EPOpenStreamResp:
+		case VendorSpecificReq:
+		case VendorSpecificResp:
+			mapkt_err("%s: cannot determine length of %s packet -"
+				" packet type not yet handled\n", __func__,
+				mausb_type_to_string(pkt->common->pkt_type));
+			BUG();
+			break;
+		default:
+			mapkt_err("%s: invalid management packet type %x\n",
+				__func__, pkt->common->pkt_type);
+			BUG();
+			break;
+		}
+
+		break;
+
+	/* Control Packets */
+	case MAUSB_PKT_TYPE_CTRL:
+		length = sizeof(struct mausb_ctrl_pkt);
+		switch (pkt->common->pkt_type) {
+		case TransferSetupReq:
+			length += sizeof(struct mausb_TransferSetupReq_flds);
+			break;
+		case TransferSetupResp:
+			break;
+		case TransferTearDownConf:
+			break;
+		default:
+			mapkt_err("%s: invalid control packet type %x\n",
+				__func__, pkt->common->pkt_type);
+			BUG();
+			break;
+		}
+		break;
+
+	/* Data Packets */
+	case MAUSB_PKT_TYPE_DATA:
+
+		length = sizeof(struct mausb_dph);
+
+		switch (pkt->common->pkt_type) {
+		case TransferReq:
+		case TransferResp: /* FALLTHROUGH */
+			if (mausb_pkt_has_setup_data(pkt->data))
+				length += sizeof(*pkt->setup);
+
+			length += mausb_pkt_datafield_length(pkt);
+			break;
+		case TransferAck:
+			break;
+		case IsochTransferReq:
+		case IsochTransferResp: /* FALLTHROUGH */
+			length = sizeof(struct mausb_iso_dph);
+			length += mausb_pkt_datafield_length(pkt);
+
+			break;
+		default:
+			mapkt_err("%s: invalid data packet type %x\n", __func__,
+				pkt->common->pkt_type);
+			BUG();
+			break;
+		}
+		break;
+
+	default:
+		mapkt_err("%s: invalid packet type %x\n", __func__,
+			pkt->common->pkt_type);
+		BUG();
+		break;
+	}
+
+	return length;
+}
+EXPORT_SYMBOL(mausb_pkt_length);
+
+/**
+ * Finds and returns the memory location offset a number of bytes from a
+ * CapResp packet. Used to find or place device capability descriptors in
+ * a CapResp packet.
+ */
+struct mausb_dev_cap_desc *cap_desc_ptr_increment(struct mausb_mgmt_pkt *resp,
+						  int offset)
+{
+	struct mausb_dev_cap_desc *desc;
+	char  *temp;
+
+	temp = (char *) resp;
+	temp += offset;
+
+	desc  =  (struct mausb_dev_cap_desc *) (temp);
+
+	return desc;
+}
+EXPORT_SYMBOL(cap_desc_ptr_increment);
+
drivers/staging/mausb/drivers/mausb_pkt.h new file +914 -0

914 lines not on this page · raw message

Signature · 1 line
1.9.1
#13 lore raw

PATCH 5/10 added media specific (MS) TCP drivers

In reply to Stephanie Wallick

This is where we handle media specific packets and transport. The MS driver interfaces with a media agnostic (MA) driver via a series of transfer pairs. Transfer pairs consist of a set of functions to pass MA USB packets back and forth between MA and MS drivers. There is one transfer pair per device endpoint and one transfer pair for control/management traffic. When the MA driver needs to send an MA USB packet, it hands the packet off to the MS layer where the packet is converted into an MS form and sent via TCP over the underlying ethernet or wireless medium. When the MS driver receives a packet, it converts it into an MA USB packet and hands it off the the MA driver for handling.

In addition, the MS driver provides an interface to inititate connection events. Because there are no physical MA USB ports in an MA USB host, the host must be notified via software when a device is connected.

Lastly, the MS driver contains a number of ioctl functions that are used by a utility to adjust medium-related driver parameters and connect or disconnect the MA USB host and device drivers.

---
 drivers/staging/mausb/drivers/mausb_ioctl.c      | 373 +++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_ioctl.h      |  99 +++++
 drivers/staging/mausb/drivers/mausb_msapi.c      | 110 ++++++
 drivers/staging/mausb/drivers/mausb_msapi.h      | 232 ++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp-device.c | 147 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp-host.c   | 144 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp.c        | 446 +++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp.h        | 129 +++++++
 8 files changed, 1680 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.h
drivers/staging/mausb/drivers/mausb_ioctl.c new file +373 -0
new file mode 100644
index 0000000..0c6c6bd
@@ -0,0 +1,373 @@
+/* Name:         mausb_ioctl.c
+ * Description:  ioctl functions for MA USB media specific driver
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/ioctl.h>
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/usb/gadget.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <asm/uaccess.h>
+#include "mausb_hcd.h"
+#include "mausb_udc.h"
+#include "mausb_ioctl.h"
+
+/* #define MAUSB_PRINT_IOCTL_MAGIC */
+
+static int ma_open;
+static char message[BUFFER];
+static char *message_point;
+
+/**
+ * This function is used to send a message to the user, in other words, the
+ * calling process. It basically copies the message one byte at a time.
+ *
+ * @msg:	The message to be sent to the user.
+ * @buffer:	The buffer in which to put the message. This buffer was given to
+ *	    	us to fill.
+ */
+void to_user(char *msg, long unsigned int buffer)
+{
+	int length = (int)strlen(msg);
+	int bytes = 0;
+
+	while (length && *msg) {
+		put_user(*(msg++), (char *)buffer++);
+		length--;
+		bytes++;
+	}
+
+	put_user('\0', (char *)buffer + bytes);
+}
+
+/**
+ * This function is used to open the device file in order to read/write
+ * from/to it.
+ *
+ * @inode:	Struct with various information that is passed in when this
+ *		function is called. We don't need to use it for our purposes.
+ * @file:	The file to be opened.
+ */
+static int mausb_open(struct inode *inode, struct file *file)
+{
+	if (ma_open)
+		return -EBUSY;
+	ma_open++;
+	message_point = message;
+	try_module_get(THIS_MODULE);
+
+	return 0;
+}
+
+/**
+ * This function is used to close the device file.
+ *
+ * @inode:	Struct with various information that is passed in when this
+ *		function is called. We don't need to use it for our purposes.
+ * @file:	The file to be closed.
+ */
+static int mausb_release(struct inode *inode, struct file *file)
+{
+	ma_open--;
+	module_put(THIS_MODULE);
+
+	return 0;
+}
+
+/**
+ * This function is used to read from the device file. From the perspective of
+ * the device, the user is reading information from us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer to put the message into.
+ * @length:	The max length to be read.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_read(struct file *file, char __user *buffer,
+		size_t length, loff_t *offset)
+{
+	int bytes_read = 0;
+
+	if (*message_point == 0)
+		return 0;
+	while (length && *message_point) {
+		put_user(*(message_point++), buffer++);
+		length--;
+		bytes_read++;
+	}
+
+	return bytes_read;
+}
+
+/**
+ * This function is used to write to the device file. From the perspective of
+ * the device, the user is writing information to us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer that holds the message.
+ * @length:	The length of the message to be written.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_write(struct file *file, const char __user *buffer,
+		size_t length, loff_t *offset)
+{
+	int i;
+
+	for (i = 0; i < length && i < BUFFER; i++)
+		get_user(message[i], buffer + i);
+	message_point = message;
+
+	return i;
+}
+
+/**
+ * This function is used to execute ioctl commands, determined by ioctl_func.
+ *
+ * @file:	  The device file. We don't use it directly, but it's passed in.
+ * @ioctl_func:	  This value determines which ioctl function will be used.
+ * @ioctl_buffer: This buffer is used to transfer data to/from the device.
+ */
+long mausb_ioctl(struct file *file, unsigned int ioctl_func,
+		unsigned long ioctl_buffer)
+{
+	int bytes = 0;
+	char *msg, *ip_addr;
+	char chr;
+	int ret, value;
+	unsigned long int  long_ret;
+
+	switch (ioctl_func) {
+	case IOCTL_SET_MSG:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		break;
+	case IOCTL_GET_MSG:
+		bytes = mausb_read(file, (char *)ioctl_buffer, 99, 0);
+		put_user('\0', (char *)ioctl_buffer + bytes);
+		break;
+	case IOCTL_GET_VRSN:
+		to_user(DRIVER_VERSION, ioctl_buffer);
+		break;
+	case IOCTL_GET_NAME:
+		to_user(MAUSB_NAME, ioctl_buffer);
+		break;
+	case IOCTL_GADGET_C:
+		ret = gadget_connection(1);
+		if (ret >= 0)
+			to_user("g_zero connect process complete", ioctl_buffer);
+		else
+			to_user("g_zero connect process failed", ioctl_buffer);
+		break;
+	case IOCTL_GADGET_D:
+		ret = gadget_connection(0);
+		if (ret >= 0)
+			to_user("g_zero disconnect process complete",
+				ioctl_buffer);
+		else
+			to_user("g_zero disconnect process failed",
+				ioctl_buffer);
+		break;
+	case IOCTL_MED_DELAY:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_medium_delay(value);
+		sprintf(message_point, "DELAY VALUE: ms: %d, jiffies: %d\n",
+			value, ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_IP:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		ip_addr = kmalloc(strlen(message_point)+1, GFP_KERNEL);
+		if (!ip_addr) {
+			printk(KERN_ALERT "Memory allocation failed!\n");
+			break;
+		}
+		strcpy(ip_addr, message_point);
+		sprintf(message_point, "Connecting to ...\nIP Address: %s\n",
+			ip_addr);
+		to_user(message_point, ioctl_buffer);
+		kfree(ip_addr);
+		break;
+	case IOCTL_SET_PORT:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_port_no(value);
+		sprintf(message_point, "PORT NUMBER:%d, Returned %i\n", value,
+			ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_IP_DECIMAL:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoul((const char *)message_point, 0, &long_ret) != 0) {
+			/* TODO: handle error */
+		}
+
+		ret = set_ip_addr(long_ret);
+		sprintf(message_point, "\nDecimal Value:%lx returned %i\n",
+			long_ret, ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_MAC:
+		{
+			u8 *mac = kmalloc(6, GFP_KERNEL);
+			u8 *buf = (u8 __user *)ioctl_buffer;
+			int i, ret;
+			if (!mac) {
+				pr_err("Memory allocation failed!\n");
+				break;
+			}
+			ret = copy_from_user(mac, buf, 6);
+			if (ret) {
+				pr_err("copy_from_user failed\n");
+				kfree(mac);
+				break;
+			}
+			for (i = 0; i < ETH_ALEN; i++)
+				pr_info("mac[%d]=0x%x\n", i, mac[i]);
+			ret = set_mac_addr(mac);
+			if (ret)
+				pr_err("unable to set MAC addr\n");
+			kfree(mac);
+			break;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * This struct creates links with our implementations of various entry point
+ * functions.
+ */
+const struct file_operations fops = {
+	.open = mausb_open,
+	.release = mausb_release,
+	.read = mausb_read,
+	.write = mausb_write,
+	.unlocked_ioctl = mausb_ioctl
+};
+
+/**
+ * Registers a character device using our device file. This function is called
+ * in the mausb_hcd_init function.
+ */
+int reg_chrdev()
+{
+	int ret;
+
+#ifdef MAUSB_PRINT_IOCTL_MAGIC
+
+	printk(KERN_DEBUG "Printing IOCTL magic numbers:\n");
+	printk(KERN_DEBUG "IOCTL_SET_MSG        = %u\n", IOCTL_SET_MSG);
+	printk(KERN_DEBUG "IOCTL_GET_MSG        = %u\n", IOCTL_GET_MSG);
+	printk(KERN_DEBUG "IOCTL_GET_VRSN       = %u\n", IOCTL_GET_VRSN);
+	printk(KERN_DEBUG "IOCTL_GET_NAME       = %u\n", IOCTL_GET_NAME);
+	printk(KERN_DEBUG "IOCTL_GADGET_C       = %u\n", IOCTL_GADGET_C);
+	printk(KERN_DEBUG "IOCTL_GADGET_D       = %u\n", IOCTL_GADGET_D);
+	printk(KERN_DEBUG "IOCTL_MED_DELAY      = %u\n", IOCTL_MED_DELAY);
+	printk(KERN_DEBUG "IOCTL_SET_IP         = %u\n", IOCTL_SET_IP);
+	printk(KERN_DEBUG "IOCTL_SET_PORT       = %u\n", IOCTL_SET_PORT);
+	printk(KERN_DEBUG "IOCTL_SET_IP_DECIMAL = %u\n", IOCTL_SET_IP_DECIMAL);
+	printk(KERN_DEBUG "IOCTL_SET_MAC        = %u\n", IOCTL_SET_MAC);
+
+#endif
+
+	ret = register_chrdev(MAJOR_NUM, MAUSB_NAME, &fops);
+
+	if (ret < 0)
+		printk(KERN_ALERT "Registering mausb failed with %d\n", ret);
+	else
+		printk(KERN_INFO "%s registeration complete. Major device"
+			" number %d.\n", MAUSB_NAME, MAJOR_NUM);
+
+	return ret;
+}
+
+/**
+ * Unregisters the character device when the hcd is unregistered. As hinted,
+ * this function is called in the mausb_hcd_exit function.
+ */
+void unreg_chrdev()
+{
+	unregister_chrdev(MAJOR_NUM, MAUSB_NAME);
+}
drivers/staging/mausb/drivers/mausb_ioctl.h new file +99 -0

99 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_msapi.c new file +110 -0

110 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_msapi.h new file +232 -0

232 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tcp-device.c new file +147 -0

147 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tcp-host.c new file +144 -0

144 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tcp.c new file +446 -0

446 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tcp.h new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
1.9.1
#14 lore raw

Re: [PATCH 05/10] added media specific (MS) TCP drivers

Quoted 45 lines · Stephanie Wallick

This is where we handle media specific packets and transport. The MS driver interfaces with a media agnostic (MA) driver via a series of transfer pairs. Transfer pairs consist of a set of functions to pass MA USB packets back and forth between MA and MS drivers. There is one transfer pair per device endpoint and one transfer pair for control/management traffic. When the MA driver needs to send an MA USB packet, it hands the packet off to the MS layer where the packet is converted into an MS form and sent via TCP over the underlying ethernet or wireless medium. When the MS driver receives a packet, it converts it into an MA USB packet and hands it off the the MA driver for handling.

In addition, the MS driver provides an interface to inititate connection events. Because there are no physical MA USB ports in an MA USB host, the host must be notified via software when a device is connected.

Lastly, the MS driver contains a number of ioctl functions that are used by a utility to adjust medium-related driver parameters and connect or disconnect the MA USB host and device drivers.

---
 drivers/staging/mausb/drivers/mausb_ioctl.c      | 373 +++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_ioctl.h      |  99 +++++
 drivers/staging/mausb/drivers/mausb_msapi.c      | 110 ++++++
 drivers/staging/mausb/drivers/mausb_msapi.h      | 232 ++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp-device.c | 147 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp-host.c   | 144 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp.c        | 446 +++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp.h        | 129 +++++++
 8 files changed, 1680 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.h
drivers/staging/mausb/drivers/mausb_ioctl.c new file +0 -0
new file mode 100644
index 0000000..0c6c6bd

[...]

Quoted 8 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +8 -0
+/**
+ * This function is used to send a message to the user, in other words, the
+ * calling process. It basically copies the message one byte at a time.
+ *
+ * @msg:	The message to be sent to the user.
+ * @buffer:	The buffer in which to put the message. This buffer was given to
+ *	    	us to fill.
+ */

drivers/staging/mausb/drivers/mausb_ioctl.c line 93

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+void to_user(char *msg, long unsigned int buffer)
+{
+	int length = (int)strlen(msg);
+	int bytes = 0;
+
+	while (length && *msg) {
+		put_user(*(msg++), (char *)buffer++);
+		length--;
+		bytes++;
+	}

Any reason not to use copy_to_user here? That way, access_ok would only need to be executed once for the whole range.

In any case, the return value of put_user/copy_to_user will need to be checked.

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +3 -0
+
+	put_user('\0', (char *)buffer + bytes);
+}

[...]

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +12 -0
+/**
+ * This function is used to read from the device file. From the perspective of
+ * the device, the user is reading information from us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer to put the message into.
+ * @length:	The max length to be read.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_read(struct file *file, char __user *buffer,
+		size_t length, loff_t *offset)

drivers/staging/mausb/drivers/mausb_ioctl.c line 153

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+{
+	int bytes_read = 0;
+
+	if (*message_point == 0)
+		return 0;
+	while (length && *message_point) {
+		put_user(*(message_point++), buffer++);
+		length--;
+		bytes_read++;
+	}

See comment for to_user above. Why not use copy_to_user?

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +11 -0
+
+	return bytes_read;
+}
+
+/**
+ * This function is used to write to the device file. From the perspective of
+ * the device, the user is writing information to us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer that holds the message.

drivers/staging/mausb/drivers/mausb_ioctl.c line 174

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+ * @length:	The length of the message to be written.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_write(struct file *file, const char __user *buffer,
+		size_t length, loff_t *offset)
+{
+	int i;
+
+	for (i = 0; i < length && i < BUFFER; i++)
+		get_user(message[i], buffer + i);

copy_from_user? In any case, check the return value here as well.

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +5 -0
+	message_point = message;
+
+	return i;
+}
+

drivers/staging/mausb/drivers/mausb_ioctl.c line 189

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+/**
+ * This function is used to execute ioctl commands, determined by ioctl_func.
+ *
+ * @file:	  The device file. We don't use it directly, but it's passed in.
+ * @ioctl_func:	  This value determines which ioctl function will be used.
+ * @ioctl_buffer: This buffer is used to transfer data to/from the device.
+ */
+long mausb_ioctl(struct file *file, unsigned int ioctl_func,
+		unsigned long ioctl_buffer)
+{

This entire function needs return value checks for put_user/get_user.

Quoted 53 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +53 -0
+	int bytes = 0;
+	char *msg, *ip_addr;
+	char chr;
+	int ret, value;
+	unsigned long int  long_ret;
+
+	switch (ioctl_func) {
+	case IOCTL_SET_MSG:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		break;
+	case IOCTL_GET_MSG:
+		bytes = mausb_read(file, (char *)ioctl_buffer, 99, 0);
+		put_user('\0', (char *)ioctl_buffer + bytes);
+		break;
+	case IOCTL_GET_VRSN:
+		to_user(DRIVER_VERSION, ioctl_buffer);
+		break;
+	case IOCTL_GET_NAME:
+		to_user(MAUSB_NAME, ioctl_buffer);
+		break;
+	case IOCTL_GADGET_C:
+		ret = gadget_connection(1);
+		if (ret >= 0)
+			to_user("g_zero connect process complete", ioctl_buffer);
+		else
+			to_user("g_zero connect process failed", ioctl_buffer);
+		break;
+	case IOCTL_GADGET_D:
+		ret = gadget_connection(0);
+		if (ret >= 0)
+			to_user("g_zero disconnect process complete",
+				ioctl_buffer);
+		else
+			to_user("g_zero disconnect process failed",
+				ioctl_buffer);
+		break;
+	case IOCTL_MED_DELAY:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_medium_delay(value);
+		sprintf(message_point, "DELAY VALUE: ms: %d, jiffies: %d\n",
+			value, ret);
+		to_user(message_point, ioctl_buffer);

drivers/staging/mausb/drivers/mausb_ioctl.c line 252

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+		break;
+	case IOCTL_SET_IP:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		ip_addr = kmalloc(strlen(message_point)+1, GFP_KERNEL);
+		if (!ip_addr) {
+			printk(KERN_ALERT "Memory allocation failed!\n");

No need to print an error message for memory allocation failures, kmalloc will take care of printing a more extensive message.

Quoted 34 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +34 -0
+			break;
+		}
+		strcpy(ip_addr, message_point);
+		sprintf(message_point, "Connecting to ...\nIP Address: %s\n",
+			ip_addr);
+		to_user(message_point, ioctl_buffer);
+		kfree(ip_addr);
+		break;
+	case IOCTL_SET_PORT:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_port_no(value);
+		sprintf(message_point, "PORT NUMBER:%d, Returned %i\n", value,
+			ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_IP_DECIMAL:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoul((const char *)message_point, 0, &long_ret) != 0) {
+			/* TODO: handle error */
+		}
+
+		ret = set_ip_addr(long_ret);
+		sprintf(message_point, "\nDecimal Value:%lx returned %i\n",

drivers/staging/mausb/drivers/mausb_ioctl.c line 296

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+			long_ret, ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_MAC:
+		{
+			u8 *mac = kmalloc(6, GFP_KERNEL);
+			u8 *buf = (u8 __user *)ioctl_buffer;
+			int i, ret;
+			if (!mac) {
+				pr_err("Memory allocation failed!\n");

See comment above. Since this is only a 6 byte buffer, it's probably easier to just allocate it on the stack.

Quoted 9 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +9 -0
+				break;
+			}
+			ret = copy_from_user(mac, buf, 6);
+			if (ret) {
+				pr_err("copy_from_user failed\n");
+				kfree(mac);
+				break;
+			}
+			for (i = 0; i < ETH_ALEN; i++)

drivers/staging/mausb/drivers/mausb_ioctl.c line 315

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+				pr_info("mac[%d]=0x%x\n", i, mac[i]);
+			ret = set_mac_addr(mac);
+			if (ret)
+				pr_err("unable to set MAC addr\n");
+			kfree(mac);
+			break;
+		}
+	}
+
+	return 0;

You probably want to return an error here in case anything went wrong above or if the ioctl number is invalid.

Quoted 1 line

+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +0 -0
new file mode 100644
index 0000000..9dd8fa5

[...]

Quoted 7 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +7 -0
+/**
+ * Frees the given ms_pkt and associated buffers. This function is not
+ * necessary to use the API, but could be useful on both sides of the interface.
+ */
+void mausb_free_ms_pkt(struct ms_pkt *pkt)
+{
+	int i;

drivers/staging/mausb/drivers/mausb_msapi.c line 84

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +10 -0
+	void *current_buf;
+
+	for (i = 0; i < pkt->nents; ++i) {
+		current_buf = pkt[i].kvec->iov_base;
+		if (NULL != current_buf) {
+			kfree(current_buf);
+		} else {
+			printk(KERN_DEBUG "%s: cannot find buffer for "
+				"kvec #%i in ms_pkt at %p\n",
+				__func__, i, pkt->kvec);

pr_debug()

Quoted 13 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +13 -0
+		}
+	}
+
+	kfree(pkt);
+
+	return;
+}
+EXPORT_SYMBOL(mausb_free_ms_pkt);
+
+/**
+ * Calculates the total length of the data in a ms_pkt. Returns the total
+ * length of the data in the ms_pkt, or a negative errno.
+ */

drivers/staging/mausb/drivers/mausb_msapi.c line 107

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +10 -0
+int mausb_ms_pkt_length(struct ms_pkt *pkt)
+{
+	int i;
+	int total_length = 0;
+
+	for (i = 0; i < pkt->nents; ++i)
+		total_length += pkt[i].kvec->iov_len;
+
+	printk(KERN_DEBUG "%s: total *kvec length: %i\n", __func__,
+		total_length);

pr_debug()

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +3 -0
+
+	return total_length;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +0 -0
new file mode 100644
index 0000000..28978a0

[...]

Quoted 7 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +7 -0
+static int mausb_tcp_device_connect(int on)
+{
+	int ret;
+
+	if (on && dev_tcp_medium->socket == NULL) {
+		ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
+			&dev_tcp_medium->socket);

drivers/staging/mausb/drivers/mausb_tcp-device.c line 102

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +10 -0
+
+		if (0 > ret) /* TODO: real errorchecking */
+			return ret;
+
+		do {
+			ret = kernel_connect(dev_tcp_medium->socket,
+				&dev_tcp_medium->addr,
+				sizeof(dev_tcp_medium->addr_in), O_RDWR);
+			printk(KERN_DEBUG "%s:kernel_connect returned %i\n",
+				__func__, ret);

pr_debug(), here any in several other places across the file...

drivers/staging/mausb/drivers/mausb_tcp-device.c line 114

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +12 -0
+
+			if (0 > ret) {
+				/* poll until we can connect sucessfully */
+				msleep(MAUSB_TCP_DEV_CONNECT_POLL_MS);
+			}
+
+
+		} while (0 > ret);
+
+		/*spawn off a listening thread */
+		dev_tcp_medium->recv_task = kthread_run(mausb_tcp_device_thread,
+			NULL, "mausb_tcp_device_thread");

kthread_run might return an ERR_PTR which needs to be handled here.

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +6 -0
+	}
+
+	ret = dev_tcp_medium->ma_driver->device_connect(on);
+
+	return ret;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +0 -0
new file mode 100644
index 0000000..0302031

[...]

drivers/staging/mausb/drivers/mausb_tcp-host.c line 92

Quoted 8 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +8 -0
+static int mausb_tcp_host_connect(int on)
+{
+	int ret;
+
+	if (on) {
+		ret = kernel_bind(host_tcp_medium->setup_socket,
+			&host_tcp_medium->addr,
+			sizeof(host_tcp_medium->addr_in));

Missing error handling.

drivers/staging/mausb/drivers/mausb_tcp-host.c line 95

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +3 -0
+
+		ret = kernel_listen(host_tcp_medium->setup_socket,
+			MAUSB_TCP_MAX_NUM_CHANNELS);

Missing error handling.

drivers/staging/mausb/drivers/mausb_tcp-host.c line 105

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +10 -0
+		printk(KERN_DEBUG "%s: kernel_listen returned %i\n",
+			__func__, ret);
+
+		ret = kernel_accept(host_tcp_medium->setup_socket,
+				    &host_tcp_medium->socket, 0);
+		printk(KERN_DEBUG "%s:kernel_accept returned %i\n",
+			__func__, ret);
+
+		if (0 > ret)
+			return ret;

kernel_accept might return negative values in case of an error, which needs to be handled properly here.

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +12 -0
+
+		if (NULL == host_tcp_medium->recv_task) {
+			host_tcp_medium->recv_task = kthread_run(
+				mausb_tcp_host_thread, NULL,
+				"mausb_tcp_host_thread");
+		}
+	}
+
+	ret = host_tcp_medium->ma_driver->device_connect(on);
+
+	return ret;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +0 -0
new file mode 100644
index 0000000..291139e

[...]

drivers/staging/mausb/drivers/mausb_tcp.c line 323

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +11 -0
+int mausb_tcp_receive_loop(struct mausb_tcp_medium *tcp_medium)
+{
+	struct msghdr msg;
+	struct ms_pkt *pkt;
+	int data_rcvd = 0;
+
+	mausb_tcp_init_msg(&msg);
+
+	while (!kthread_should_stop()) {
+
+		pkt = kzalloc(sizeof(struct ms_pkt), GFP_KERNEL);

Missing return value check.

Quoted 31 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +31 -0
+
+		printk(KERN_DEBUG "%s: preparing to receive data\n",
+			__func__);
+
+		data_rcvd = mausb_tcp_receive_packet(tcp_medium, &msg, pkt);
+
+		if (0 >= data_rcvd) {
+			printk(KERN_DEBUG "%s: received no data (err %i)\n",
+				__func__, data_rcvd);
+
+			sock_release(tcp_medium->socket);
+			return data_rcvd;
+
+		} else {
+			printk(KERN_DEBUG "%s: received %i bytes\n",
+				__func__, data_rcvd);
+		}
+
+		if (data_rcvd > 0) {
+			mausb_transfer_packet(pkt,
+				&tcp_medium->ma_driver->pkt_dmux);
+		}
+
+		data_rcvd = 0;
+	}
+
+	sock_release(tcp_medium->socket);
+
+	return 0;
+}
+EXPORT_SYMBOL(mausb_tcp_receive_loop);

[...]

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +4 -0
+/**
+ * initialization function
+ */
+struct mausb_tcp_medium *alloc_init_mausb_tcp_medium(

drivers/staging/mausb/drivers/mausb_tcp.c line 387

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +10 -0
+	enum mausb_tcp_module_type type)
+{
+	struct mausb_tcp_medium *medium;
+	int ret;
+
+	printk(KERN_DEBUG "%s\n", __func__);
+
+	medium = kzalloc(sizeof(struct mausb_tcp_medium), GFP_KERNEL);
+	if (NULL == medium) {
+		printk(KERN_DEBUG "%s: memory allocation failed\n", __func__);

No error message needed, kmalloc will take care of it.

drivers/staging/mausb/drivers/mausb_tcp.c line 392

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +5 -0
+		return NULL;
+	}
+
+	ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
+		&medium->setup_socket);

Error handling is missing.

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +11 -0
+
+	medium->addr_in.sin_family = AF_INET;
+	medium->addr_in.sin_port = htons(MAUSB_TCP_PORT_HOST);
+
+	spin_lock_init(&medium->lock);
+
+	tcp_medium[type] = medium;
+
+	return medium;
+}
+EXPORT_SYMBOL(alloc_init_mausb_tcp_medium);
maintainer
#15 lore raw

Re: [PATCH 05/10] added media specific (MS) TCP drivers

Quoted 71 lines · Tobias Klauser
Quoted 45 lines · Stephanie Wallick

This is where we handle media specific packets and transport. The MS driver interfaces with a media agnostic (MA) driver via a series of transfer pairs. Transfer pairs consist of a set of functions to pass MA USB packets back and forth between MA and MS drivers. There is one transfer pair per device endpoint and one transfer pair for control/management traffic. When the MA driver needs to send an MA USB packet, it hands the packet off to the MS layer where the packet is converted into an MS form and sent via TCP over the underlying ethernet or wireless medium. When the MS driver receives a packet, it converts it into an MA USB packet and hands it off the the MA driver for handling.

In addition, the MS driver provides an interface to inititate connection events. Because there are no physical MA USB ports in an MA USB host, the host must be notified via software when a device is connected.

Lastly, the MS driver contains a number of ioctl functions that are used by a utility to adjust medium-related driver parameters and connect or disconnect the MA USB host and device drivers.

---
 drivers/staging/mausb/drivers/mausb_ioctl.c      | 373 +++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_ioctl.h      |  99 +++++
 drivers/staging/mausb/drivers/mausb_msapi.c      | 110 ++++++
 drivers/staging/mausb/drivers/mausb_msapi.h      | 232 ++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp-device.c | 147 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp-host.c   | 144 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp.c        | 446 +++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp.h        | 129 +++++++
 8 files changed, 1680 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.h
drivers/staging/mausb/drivers/mausb_ioctl.c new file +0 -0
new file mode 100644
index 0000000..0c6c6bd

[...]

Quoted 8 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +8 -0
+/**
+ * This function is used to send a message to the user, in other words, the
+ * calling process. It basically copies the message one byte at a time.
+ *
+ * @msg:	The message to be sent to the user.
+ * @buffer:	The buffer in which to put the message. This buffer was given to
+ *	    	us to fill.
+ */

drivers/staging/mausb/drivers/mausb_ioctl.c line 93

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+void to_user(char *msg, long unsigned int buffer)
+{
+	int length = (int)strlen(msg);
+	int bytes = 0;
+
+	while (length && *msg) {
+		put_user(*(msg++), (char *)buffer++);
+		length--;
+		bytes++;
+	}

Any reason not to use copy_to_user here? That way, access_ok would only need to be executed once for the whole range.

Quoted 2 lines

In any case, the return value of put_user/copy_to_user will need to be checked.

Never use put_user if you can help it, this whole function should go away, and copy_to_user() should be used at the caller sites instead as you point out.

thanks,

greg k-h

#16 lore raw

Re: [PATCH 05/10] added media specific (MS) TCP drivers

In reply to Tobias Klauser

Thank You for reviewing our code.

I believe most of the problems you pointed out in mausb_ioctl.c were addressed in [V2 PATCH 5/10]. I am working on adding the proper error checking to the TCP drivers.

Greg has requested that we clean up our code internally before submitting another patchset to the mailing list. I will make sure we fix the problems you pointed out, but it may be a while before you see another patchset.

Thanks,
Sean

Quoted 547 lines · Tobias Klauser
Quoted 45 lines · Stephanie Wallick

This is where we handle media specific packets and transport. The MS driver interfaces with a media agnostic (MA) driver via a series of transfer pairs. Transfer pairs consist of a set of functions to pass MA USB packets back and forth between MA and MS drivers. There is one transfer pair per device endpoint and one transfer pair for control/management traffic. When the MA driver needs to send an MA USB packet, it hands the packet off to the MS layer where the packet is converted into an MS form and sent via TCP over the underlying ethernet or wireless medium. When the MS driver receives a packet, it converts it into an MA USB packet and hands it off the the MA driver for handling.

In addition, the MS driver provides an interface to inititate connection events. Because there are no physical MA USB ports in an MA USB host, the host must be notified via software when a device is connected.

Lastly, the MS driver contains a number of ioctl functions that are used by a utility to adjust medium-related driver parameters and connect or disconnect the MA USB host and device drivers.

---
 drivers/staging/mausb/drivers/mausb_ioctl.c      | 373 +++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_ioctl.h      |  99 +++++
 drivers/staging/mausb/drivers/mausb_msapi.c      | 110 ++++++
 drivers/staging/mausb/drivers/mausb_msapi.h      | 232 ++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp-device.c | 147 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp-host.c   | 144 ++++++++
 drivers/staging/mausb/drivers/mausb_tcp.c        | 446 +++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tcp.h        | 129 +++++++
 8 files changed, 1680 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_msapi.h
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tcp.h
drivers/staging/mausb/drivers/mausb_ioctl.c new file +0 -0
new file mode 100644
index 0000000..0c6c6bd

[...]

Quoted 8 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +8 -0
+/**
+ * This function is used to send a message to the user, in other words, the
+ * calling process. It basically copies the message one byte at a time.
+ *
+ * @msg:	The message to be sent to the user.
+ * @buffer:	The buffer in which to put the message. This buffer was given to
+ *	    	us to fill.
+ */

drivers/staging/mausb/drivers/mausb_ioctl.c line 93

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+void to_user(char *msg, long unsigned int buffer)
+{
+	int length = (int)strlen(msg);
+	int bytes = 0;
+
+	while (length && *msg) {
+		put_user(*(msg++), (char *)buffer++);
+		length--;
+		bytes++;
+	}

Any reason not to use copy_to_user here? That way, access_ok would only need to be executed once for the whole range.

In any case, the return value of put_user/copy_to_user will need to be checked.

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +3 -0
+
+	put_user('\0', (char *)buffer + bytes);
+}

[...]

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +12 -0
+/**
+ * This function is used to read from the device file. From the perspective of
+ * the device, the user is reading information from us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer to put the message into.
+ * @length:	The max length to be read.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_read(struct file *file, char __user *buffer,
+		size_t length, loff_t *offset)

drivers/staging/mausb/drivers/mausb_ioctl.c line 153

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+{
+	int bytes_read = 0;
+
+	if (*message_point == 0)
+		return 0;
+	while (length && *message_point) {
+		put_user(*(message_point++), buffer++);
+		length--;
+		bytes_read++;
+	}

See comment for to_user above. Why not use copy_to_user?

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +11 -0
+
+	return bytes_read;
+}
+
+/**
+ * This function is used to write to the device file. From the perspective of
+ * the device, the user is writing information to us. This is one of the
+ * entry points to this module.
+ *
+ * @file:	The device file. We don't use it directly, but it's passed in.
+ * @buffer:	The buffer that holds the message.

drivers/staging/mausb/drivers/mausb_ioctl.c line 174

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+ * @length:	The length of the message to be written.
+ * @offset:	File offset, which we don't use but it is passed in nontheless.
+ */
+static ssize_t mausb_write(struct file *file, const char __user *buffer,
+		size_t length, loff_t *offset)
+{
+	int i;
+
+	for (i = 0; i < length && i < BUFFER; i++)
+		get_user(message[i], buffer + i);

copy_from_user? In any case, check the return value here as well.

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +5 -0
+	message_point = message;
+
+	return i;
+}
+

drivers/staging/mausb/drivers/mausb_ioctl.c line 189

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+/**
+ * This function is used to execute ioctl commands, determined by ioctl_func.
+ *
+ * @file:	  The device file. We don't use it directly, but it's passed in.
+ * @ioctl_func:	  This value determines which ioctl function will be used.
+ * @ioctl_buffer: This buffer is used to transfer data to/from the device.
+ */
+long mausb_ioctl(struct file *file, unsigned int ioctl_func,
+		unsigned long ioctl_buffer)
+{

This entire function needs return value checks for put_user/get_user.

Quoted 53 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +53 -0
+	int bytes = 0;
+	char *msg, *ip_addr;
+	char chr;
+	int ret, value;
+	unsigned long int  long_ret;
+
+	switch (ioctl_func) {
+	case IOCTL_SET_MSG:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		break;
+	case IOCTL_GET_MSG:
+		bytes = mausb_read(file, (char *)ioctl_buffer, 99, 0);
+		put_user('\0', (char *)ioctl_buffer + bytes);
+		break;
+	case IOCTL_GET_VRSN:
+		to_user(DRIVER_VERSION, ioctl_buffer);
+		break;
+	case IOCTL_GET_NAME:
+		to_user(MAUSB_NAME, ioctl_buffer);
+		break;
+	case IOCTL_GADGET_C:
+		ret = gadget_connection(1);
+		if (ret >= 0)
+			to_user("g_zero connect process complete", ioctl_buffer);
+		else
+			to_user("g_zero connect process failed", ioctl_buffer);
+		break;
+	case IOCTL_GADGET_D:
+		ret = gadget_connection(0);
+		if (ret >= 0)
+			to_user("g_zero disconnect process complete",
+				ioctl_buffer);
+		else
+			to_user("g_zero disconnect process failed",
+				ioctl_buffer);
+		break;
+	case IOCTL_MED_DELAY:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_medium_delay(value);
+		sprintf(message_point, "DELAY VALUE: ms: %d, jiffies: %d\n",
+			value, ret);
+		to_user(message_point, ioctl_buffer);

drivers/staging/mausb/drivers/mausb_ioctl.c line 252

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+		break;
+	case IOCTL_SET_IP:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		ip_addr = kmalloc(strlen(message_point)+1, GFP_KERNEL);
+		if (!ip_addr) {
+			printk(KERN_ALERT "Memory allocation failed!\n");

No need to print an error message for memory allocation failures, kmalloc will take care of printing a more extensive message.

Quoted 34 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +34 -0
+			break;
+		}
+		strcpy(ip_addr, message_point);
+		sprintf(message_point, "Connecting to ...\nIP Address: %s\n",
+			ip_addr);
+		to_user(message_point, ioctl_buffer);
+		kfree(ip_addr);
+		break;
+	case IOCTL_SET_PORT:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoint((const char *)message_point, 0, &value) != 0) {
+			/* TODO: handle error */
+		}
+		ret = set_port_no(value);
+		sprintf(message_point, "PORT NUMBER:%d, Returned %i\n", value,
+			ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_IP_DECIMAL:
+		msg = (char *)ioctl_buffer;
+		get_user(chr, msg);
+		for (bytes = 0; chr && bytes < BUFFER; bytes++, msg++)
+			get_user(chr, msg);
+		mausb_write(file, (char *)ioctl_buffer, bytes, 0);
+		if (kstrtoul((const char *)message_point, 0, &long_ret) != 0) {
+			/* TODO: handle error */
+		}
+
+		ret = set_ip_addr(long_ret);
+		sprintf(message_point, "\nDecimal Value:%lx returned %i\n",

drivers/staging/mausb/drivers/mausb_ioctl.c line 296

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+			long_ret, ret);
+		to_user(message_point, ioctl_buffer);
+		break;
+	case IOCTL_SET_MAC:
+		{
+			u8 *mac = kmalloc(6, GFP_KERNEL);
+			u8 *buf = (u8 __user *)ioctl_buffer;
+			int i, ret;
+			if (!mac) {
+				pr_err("Memory allocation failed!\n");

See comment above. Since this is only a 6 byte buffer, it's probably easier to just allocate it on the stack.

Quoted 9 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +9 -0
+				break;
+			}
+			ret = copy_from_user(mac, buf, 6);
+			if (ret) {
+				pr_err("copy_from_user failed\n");
+				kfree(mac);
+				break;
+			}
+			for (i = 0; i < ETH_ALEN; i++)

drivers/staging/mausb/drivers/mausb_ioctl.c line 315

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_ioctl.c new file +10 -0
+				pr_info("mac[%d]=0x%x\n", i, mac[i]);
+			ret = set_mac_addr(mac);
+			if (ret)
+				pr_err("unable to set MAC addr\n");
+			kfree(mac);
+			break;
+		}
+	}
+
+	return 0;

You probably want to return an error here in case anything went wrong above or if the ioctl number is invalid.

Quoted 1 line

+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +0 -0
new file mode 100644
index 0000000..9dd8fa5

[...]

Quoted 7 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +7 -0
+/**
+ * Frees the given ms_pkt and associated buffers. This function is not
+ * necessary to use the API, but could be useful on both sides of the interface.
+ */
+void mausb_free_ms_pkt(struct ms_pkt *pkt)
+{
+	int i;

drivers/staging/mausb/drivers/mausb_msapi.c line 84

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +10 -0
+	void *current_buf;
+
+	for (i = 0; i < pkt->nents; ++i) {
+		current_buf = pkt[i].kvec->iov_base;
+		if (NULL != current_buf) {
+			kfree(current_buf);
+		} else {
+			printk(KERN_DEBUG "%s: cannot find buffer for "
+				"kvec #%i in ms_pkt at %p\n",
+				__func__, i, pkt->kvec);

pr_debug()

Quoted 13 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +13 -0
+		}
+	}
+
+	kfree(pkt);
+
+	return;
+}
+EXPORT_SYMBOL(mausb_free_ms_pkt);
+
+/**
+ * Calculates the total length of the data in a ms_pkt. Returns the total
+ * length of the data in the ms_pkt, or a negative errno.
+ */

drivers/staging/mausb/drivers/mausb_msapi.c line 107

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +10 -0
+int mausb_ms_pkt_length(struct ms_pkt *pkt)
+{
+	int i;
+	int total_length = 0;
+
+	for (i = 0; i < pkt->nents; ++i)
+		total_length += pkt[i].kvec->iov_len;
+
+	printk(KERN_DEBUG "%s: total *kvec length: %i\n", __func__,
+		total_length);

pr_debug()

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_msapi.c new file +3 -0
+
+	return total_length;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +0 -0
new file mode 100644
index 0000000..28978a0

[...]

Quoted 7 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +7 -0
+static int mausb_tcp_device_connect(int on)
+{
+	int ret;
+
+	if (on && dev_tcp_medium->socket == NULL) {
+		ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
+			&dev_tcp_medium->socket);

drivers/staging/mausb/drivers/mausb_tcp-device.c line 102

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +10 -0
+
+		if (0 > ret) /* TODO: real errorchecking */
+			return ret;
+
+		do {
+			ret = kernel_connect(dev_tcp_medium->socket,
+				&dev_tcp_medium->addr,
+				sizeof(dev_tcp_medium->addr_in), O_RDWR);
+			printk(KERN_DEBUG "%s:kernel_connect returned %i\n",
+				__func__, ret);

pr_debug(), here any in several other places across the file...

drivers/staging/mausb/drivers/mausb_tcp-device.c line 114

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +12 -0
+
+			if (0 > ret) {
+				/* poll until we can connect sucessfully */
+				msleep(MAUSB_TCP_DEV_CONNECT_POLL_MS);
+			}
+
+
+		} while (0 > ret);
+
+		/*spawn off a listening thread */
+		dev_tcp_medium->recv_task = kthread_run(mausb_tcp_device_thread,
+			NULL, "mausb_tcp_device_thread");

kthread_run might return an ERR_PTR which needs to be handled here.

Quoted 6 lines
drivers/staging/mausb/drivers/mausb_tcp-device.c new file +6 -0
+	}
+
+	ret = dev_tcp_medium->ma_driver->device_connect(on);
+
+	return ret;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +0 -0
new file mode 100644
index 0000000..0302031

[...]

drivers/staging/mausb/drivers/mausb_tcp-host.c line 92

Quoted 8 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +8 -0
+static int mausb_tcp_host_connect(int on)
+{
+	int ret;
+
+	if (on) {
+		ret = kernel_bind(host_tcp_medium->setup_socket,
+			&host_tcp_medium->addr,
+			sizeof(host_tcp_medium->addr_in));

Missing error handling.

drivers/staging/mausb/drivers/mausb_tcp-host.c line 95

Quoted 3 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +3 -0
+
+		ret = kernel_listen(host_tcp_medium->setup_socket,
+			MAUSB_TCP_MAX_NUM_CHANNELS);

Missing error handling.

drivers/staging/mausb/drivers/mausb_tcp-host.c line 105

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +10 -0
+		printk(KERN_DEBUG "%s: kernel_listen returned %i\n",
+			__func__, ret);
+
+		ret = kernel_accept(host_tcp_medium->setup_socket,
+				    &host_tcp_medium->socket, 0);
+		printk(KERN_DEBUG "%s:kernel_accept returned %i\n",
+			__func__, ret);
+
+		if (0 > ret)
+			return ret;

kernel_accept might return negative values in case of an error, which needs to be handled properly here.

Quoted 12 lines
drivers/staging/mausb/drivers/mausb_tcp-host.c new file +12 -0
+
+		if (NULL == host_tcp_medium->recv_task) {
+			host_tcp_medium->recv_task = kthread_run(
+				mausb_tcp_host_thread, NULL,
+				"mausb_tcp_host_thread");
+		}
+	}
+
+	ret = host_tcp_medium->ma_driver->device_connect(on);
+
+	return ret;
+}

[...]

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +0 -0
new file mode 100644
index 0000000..291139e

[...]

drivers/staging/mausb/drivers/mausb_tcp.c line 323

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +11 -0
+int mausb_tcp_receive_loop(struct mausb_tcp_medium *tcp_medium)
+{
+	struct msghdr msg;
+	struct ms_pkt *pkt;
+	int data_rcvd = 0;
+
+	mausb_tcp_init_msg(&msg);
+
+	while (!kthread_should_stop()) {
+
+		pkt = kzalloc(sizeof(struct ms_pkt), GFP_KERNEL);

Missing return value check.

Quoted 31 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +31 -0
+
+		printk(KERN_DEBUG "%s: preparing to receive data\n",
+			__func__);
+
+		data_rcvd = mausb_tcp_receive_packet(tcp_medium, &msg, pkt);
+
+		if (0 >= data_rcvd) {
+			printk(KERN_DEBUG "%s: received no data (err %i)\n",
+				__func__, data_rcvd);
+
+			sock_release(tcp_medium->socket);
+			return data_rcvd;
+
+		} else {
+			printk(KERN_DEBUG "%s: received %i bytes\n",
+				__func__, data_rcvd);
+		}
+
+		if (data_rcvd > 0) {
+			mausb_transfer_packet(pkt,
+				&tcp_medium->ma_driver->pkt_dmux);
+		}
+
+		data_rcvd = 0;
+	}
+
+	sock_release(tcp_medium->socket);
+
+	return 0;
+}
+EXPORT_SYMBOL(mausb_tcp_receive_loop);

[...]

Quoted 4 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +4 -0
+/**
+ * initialization function
+ */
+struct mausb_tcp_medium *alloc_init_mausb_tcp_medium(

drivers/staging/mausb/drivers/mausb_tcp.c line 387

Quoted 10 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +10 -0
+	enum mausb_tcp_module_type type)
+{
+	struct mausb_tcp_medium *medium;
+	int ret;
+
+	printk(KERN_DEBUG "%s\n", __func__);
+
+	medium = kzalloc(sizeof(struct mausb_tcp_medium), GFP_KERNEL);
+	if (NULL == medium) {
+		printk(KERN_DEBUG "%s: memory allocation failed\n", __func__);

No error message needed, kmalloc will take care of it.

drivers/staging/mausb/drivers/mausb_tcp.c line 392

Quoted 5 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +5 -0
+		return NULL;
+	}
+
+	ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
+		&medium->setup_socket);

Error handling is missing.

Quoted 11 lines
drivers/staging/mausb/drivers/mausb_tcp.c new file +11 -0
+
+	medium->addr_in.sin_family = AF_INET;
+	medium->addr_in.sin_port = htons(MAUSB_TCP_PORT_HOST);
+
+	spin_lock_init(&medium->lock);
+
+	tcp_medium[type] = medium;
+
+	return medium;
+}
+EXPORT_SYMBOL(alloc_init_mausb_tcp_medium);
#17 lore raw

PATCH 6/10 added media agnostic (MA) UDC

In reply to Stephanie Wallick

This is where we implement the behavior of a USB device controller for the MA USB device-side driver. The MA UDC interfaces with a gadget driver and appears to the driver as a regular UDC. However, instead of sending USB packets over a wired USB bus, the MA UDC hands MA USB packets off to a media specific layer for transport.

---
 drivers/staging/mausb/drivers/mausb_udc.c | 1488 +++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_udc.h |  147 +++
 2 files changed, 1635 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_udc.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_udc.h
drivers/staging/mausb/drivers/mausb_udc.c new file +1488 -0
new file mode 100644
index 0000000..dd8bd02
@@ -0,0 +1,1488 @@
+/* name:	mausb_udc.c
+ * description: Implements a USB device controller(UDC) for interfacing with
+ *		gadget drivers. The gadget driver uses this interface to return
+ *		descriptors and to implement configuration and data transfer
+ *		protocols with the pHCD. The UDC also allocates and initializes
+ *		endpoints to support gadget/pHCD interfacing.
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define DEBUG
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
+#include <linux/platform_device.h>
+#include <linux/usb/ch11.h>
+#include <linux/usb/gadget.h>
+#include <linux/device.h>
+#include <linux/usb/composite.h>
+#include <linux/spinlock.h>
+
+#include "mausb_udc.h"
+#include "mausb_mgmt.h"
+#include "mausb_mem.h"
+#include "mausb_msapi.h"
+#include "mausb_tx.h"
+
+static const char	 ep0[] = "ep0";
+static const char *const ep_name[] = {ep0, "ep1", "ep2"};
+
+static struct platform_device udc_pdev;
+
+void udc_dev_release(struct device *dev)
+{
+	/* free any dynamically allocated structures here */
+}
+
+static inline struct mausb_udc *gadget_to_udc(struct usb_gadget *gadget)
+{
+	return container_of(gadget, struct mausb_udc, gadget);
+}
+
+static inline struct mausb_udc *gadget_dev_to_mausb_udc(struct device *dev)
+{
+	return container_of(dev, struct mausb_udc, gadget.dev);
+}
+
+static inline struct mausb_request *usb_req_to_mausb_req(
+		struct usb_request *req)
+{
+	return container_of(req, struct mausb_request, req);
+}
+
+static inline struct mausb_udc *mausb_ma_dev_to_udc(struct ma_dev *ma_dev)
+{
+	return container_of(ma_dev, struct mausb_udc, ma_dev);
+}
+
+/*
+ * forward declarations
+ */
+static int set_or_clear_gadget_feature(struct usb_ctrlrequest *ctrlreq,
+		struct mausb_udc *udc, int set);
+static int get_gadget_status(struct usb_ctrlrequest *ctrlreq,
+		struct mausb_udc *udc);
+
+/**
+ * Finds a matching endpoint for a given address.
+ */
+static struct usb_ep *get_ep(struct mausb_udc *udc, u8 address)
+{
+	int			i;
+	struct usb_ep		*ep;
+
+	/* address is 0 (ignore direction bit flag) */
+	if ((address & ~USB_DIR_IN) == 0)
+		return &udc->usb_ep[0].dev_ep;
+
+	/* otherwise look for match */
+	for (i = 1; i < MAUDC_MAX_NUM_EP; i++) {
+
+		ep = &udc->usb_ep[i].dev_ep;
+
+		if (ep->address == address)
+			return ep;
+	}
+
+	maudc_dbg(udc, "%s: no endpoint found for address 0x%x\n",
+		__func__, address);
+
+	return NULL;
+}
+
+/**
+ * Called by gadget driver to enable endpoint.
+ */
+static int mausb_ep_enable(struct usb_ep *ep,
+		const struct usb_endpoint_descriptor *desc)
+{
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_host_ep		*ma_ep = mausb_find_ep_by_desc(desc,
+						udc->mausb_dev);
+
+	maudc_dbg(udc, "%s: enabling gadget endpoint %s\n",
+		__func__, ep->name);
+
+	/* ep0 is control endpoint, don't try and configure it */
+	if (ep->name == ep0) {
+		maudc_err(udc, "%s: cannot configure ep0\n", __func__);
+		return -EINVAL;
+	}
+
+	/*
+	 * Get maximum packet size from descriptor and set for ep.
+	 * Bits 0->10 of wMaxPacketSize = maximum packet size for HS and
+	 * FS devices maximum packet size for SS devices = 1024.
+	 */
+	ep->maxpacket = EP_MAX_PACKET;
+
+	if (NULL != ma_ep)
+		mausb_link_ma_ep_to_usb_ep(ma_ep, ep);
+
+	return 0;
+}
+
+/**
+ * Called by gadget driver to disable endpoint.
+ */
+static int mausb_ep_disable(struct usb_ep *ep)
+{
+	struct mausb_udc *udc = platform_get_drvdata(&udc_pdev);
+
+	if (!ep) {
+		maudc_dbg(udc, "%s: no endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	maudc_dbg(udc, "%s: disabling gadget endpoint %s\n",
+		__func__, ep->name);
+
+	return 0;
+}
+
+/**
+ * Called by gadget driver to allocate a request packet.
+ */
+static struct usb_request *mausb_ep_alloc_request(struct usb_ep *ep,
+		gfp_t mem_flags)
+{
+	struct mausb_request *req;
+
+	if (!ep)
+		return NULL;
+
+	req = kzalloc(sizeof(*req), mem_flags);
+	if (!req)
+		return NULL;
+
+	INIT_LIST_HEAD(&req->usb_req_list);
+
+	return &req->req;
+}
+
+/**
+ * Called by gadget driver to free a request packet.
+ */
+static void mausb_ep_free_request(struct usb_ep *ep, struct usb_request *req)
+{
+	struct mausb_request *mreq;
+
+	if (!ep || !req)
+		return;
+
+	mreq = usb_req_to_mausb_req(req);
+	kfree(mreq);
+}
+
+/**
+ * Called by gadget driver to queue a request.
+ */
+static int mausb_ep_queue(struct usb_ep *ep, struct usb_request *req,
+		gfp_t mem_flags)
+{
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_request		*ma_req;
+	struct mausb_gadget_ep		*gadget_ep;
+
+	if (!ep) {
+		maudc_err(udc, "%s: no endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	if (!req) {
+		maudc_err(udc, "%s: no USB request\n", __func__);
+		return -EINVAL;
+	}
+
+	ma_req = usb_req_to_mausb_req(req);
+	gadget_ep = usb_ep_to_mausb_gadget_ep(ep);
+
+	req->status = -EINPROGRESS;
+	req->actual = 0;
+
+	maudc_dbg(udc, "%s: queueing request to ep %i at 0x%p with data at "
+		"0x%p\n", __func__, usb_endpoint_num(ep->desc), ep, req->buf);
+
+	list_add_tail(&ma_req->usb_req_list, &gadget_ep->usb_req_list);
+
+	return 0;
+}
+
+/**
+ * Called by gadget driver to dequeue a request.
+ */
+static int mausb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
+{
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_request		*ma_req;
+
+	if (!ep) {
+		maudc_err(udc, "%s: no endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	if (!req) {
+		maudc_err(udc, "%s: no USB request\n", __func__);
+		return -EINVAL;
+	}
+
+	ma_req = usb_req_to_mausb_req(req);
+
+	list_del(&ma_req->usb_req_list);
+
+	maudc_dbg(udc, "%s: dequeueing request\n", __func__);
+
+	return 0;
+}
+
+/**
+ * Halts or clears a halt of an endpoint.
+ */
+static int mausb_ep_set_halt(struct usb_ep *ep, int value)
+{
+	struct mausb_udc 	*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_host_ep	*ma_ep = mausb_find_ep_dev(ep, udc->mausb_dev);
+
+	if (NULL == ma_ep) {
+		maudc_err(udc, "%s: no MA USB endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	/* probably don't want to halt default control ep */
+	if (ma_ep->dev_ep->name == ep0) {
+		maudc_err(udc, "%s: cannot halt ep0!\n", __func__);
+		return -EINVAL;
+	}
+
+	if (value) {
+		maudc_dbg(udc, "%s: halting ep\n", __func__);
+		ma_ep->halted = 1;
+	}
+
+	else {
+		maudc_dbg(udc, "%s: clearing ep halt\n", __func__);
+		ma_ep->halted = 0;
+	}
+
+	return 0;
+}
+
+/**
+ * Wedges an endpoint; wedge causes request to clear halt from host to be
+ * ignored.
+ */
+static int mausb_ep_set_wedge(struct usb_ep *ep)
+{
+	struct mausb_udc	*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_host_ep	*ma_ep = mausb_find_ep_dev(ep, udc->mausb_dev);
+
+	if (NULL == ma_ep) {
+		maudc_err(udc, "%s: no MA USB endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	/* wedging default control ep is probably bad */
+	if (ma_ep->dev_ep->name == ep0) {
+		maudc_dbg(udc, "%s: cannot wedge ep0\n", __func__);
+		return -EINVAL;
+	}
+
+	ma_ep->halted = 1;
+	ma_ep->wedged = 1;
+
+	return 0;
+}
+
+static struct usb_ep_ops mausb_ep_ops = {
+	.enable = mausb_ep_enable,
+	.disable = mausb_ep_disable,
+	.alloc_request = mausb_ep_alloc_request,
+	.free_request = mausb_ep_free_request,
+	.queue = mausb_ep_queue,
+	.dequeue = mausb_ep_dequeue,
+	.set_halt = mausb_ep_set_halt,
+	.set_wedge = mausb_ep_set_wedge
+};
+
+/*---------------------- transfer operations ------------------------------*/
+
+/**
+ * Handles control requests from usb core. Returns 0 if request is fully
+ * handled, otherwise returns the length of the transfer
+ */
+static int handle_ma_control_request(struct mausb_host_ep *ep,
+		struct usb_ctrlrequest *ctrlreq)
+{
+	int			ret = 0;
+	struct mausb_udc	*udc = platform_get_drvdata(&udc_pdev);
+
+	if (!ctrlreq) {
+		maudc_err(udc, "%s: no control request\n", __func__);
+		return -EINVAL;
+	}
+
+	/*
+	 * UDC handles set/clear feature, get status, and set address
+	 * requests, anything else is passed to gadget driver.
+	 */
+	switch (ctrlreq->bRequest) {
+	case USB_REQ_CLEAR_FEATURE:
+		maudc_dbg(udc, "USB_REQ: clearing feature\n");
+		ret = set_or_clear_gadget_feature(ctrlreq, udc, 0);
+		break;
+
+	case USB_REQ_GET_STATUS:
+		maudc_dbg(udc, "USB_REQ: getting status\n");
+		ret = get_gadget_status(ctrlreq, udc);
+		break;
+
+	case USB_REQ_SET_FEATURE:
+		maudc_dbg(udc, "USB_REQ: setting feature\n");
+		ret = set_or_clear_gadget_feature(ctrlreq, udc, 1);
+		break;
+
+	default: /* pass the request to the gadget driver */
+		maudc_dbg(udc, "USB_REQ: forwarding REQ #%i to gadget driver\n",
+				ctrlreq->bRequest);
+		if (NULL != udc->driver)
+			ret = udc->driver->setup(&udc->gadget, ctrlreq);
+		else
+			maudc_err(udc, "gadget driver not found\n");
+
+		if (ret >= 0)
+			ret = ctrlreq->wLength;
+		break;
+	}
+
+	return ret;
+}
+
+/**
+ * Transfers contents of request and endpoint buffers.
+ */
+int do_ma_transfer(struct mausb_host_ep *ep, struct mausb_pkt *tx_req,
+		bool dir_in)
+{
+	int				i = 0;
+	int				out_copy_length = 0;
+	int				ret, host_len, device_len;
+	int				status = -EINPROGRESS;
+	int				length = 0;
+	struct mausb_transfer_state	*tx_state = &ep->active_transfer->state;
+	struct mausb_request		*req, *next;
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	void				*epbuf, *rbuf;
+
+	/* look for setup data */
+	if (tx_req->setup)
+		ret = handle_ma_control_request(ep, tx_req->setup);
+
+	if (NULL == ep->usb_req_list)
+		return -ENOTCONN;
+
+	if (list_empty(ep->usb_req_list) && !in_interrupt()) {
+		maudc_dbg(udc, "wait gadget function driver submit request\n");
+		while (list_empty(ep->usb_req_list) &&
+		       i <= MAUSB_GADGET_TIMEOUT) {
+			msleep(1);
+			i++;
+		}
+	}
+	if (i > MAUSB_GADGET_TIMEOUT) {
+		maudc_dbg(udc, "Wait gadget request time out\n");
+		return -ETIMEDOUT;
+	}
+
+ loop_request_queue:
+	/* take care of each request in request queue */
+	list_for_each_entry_safe(req, next, ep->usb_req_list, usb_req_list) {
+
+		if (dir_in)
+			host_len = tx_state->rem_size - ep->actual_length;
+		else
+			host_len = tx_req->buffer_length - out_copy_length;
+		device_len = req->req.length - req->req.actual;
+		length = min(host_len, device_len);
+
+		/* there is something to transfer so do transfer */
+		if (length > 0) {
+
+			rbuf = req->req.buf + req->req.actual;
+
+			/*
+			 * If IN transfer, copy req buffer contents into
+			 * ep buffer. Vice versa for OUT transfer.
+			 */
+			if (dir_in) {
+				epbuf = ep->buffer + ep->actual_length;
+				if (epbuf)
+					memcpy(epbuf, rbuf, length);
+				else
+					maudc_err(udc, "%s: no ep buffer\n",
+						__func__);
+			} else {
+				/*
+				 * For bulk OUT, we can't use ep->actual_length
+				 * as indicator because ep->buffer for every
+				 * transfer request is only "rx_buf_size" bytes
+				 * length.
+				 */
+				epbuf = ep->buffer + out_copy_length;
+				memcpy(rbuf, epbuf, length);
+				out_copy_length += length;
+			}
+			ep->actual_length += length;
+			req->req.actual += length;
+		}
+		/* zero length packet ends a read */
+		else {
+			req->req.status = 0;
+			status = 0;
+		}
+
+		/*
+		 * A transfer usually ends when buffer contents are completely
+		 * transferred. But, look out for for the zero packet transfer
+		 * flag. If flag is set, bulk OUT transfers need to wait for
+		 * zero length packet (short packet) for completion.
+		 */
+		if ((req->req.length == req->req.actual) ||
+		    /* bulk IN short packet */
+		    (dir_in && (req->req.actual % ep->dev_ep->maxpacket)) ||
+		    /* bulk OUT transfer all data requested by host urb*/
+		    (!dir_in && ep->active_transfer->transfer_size
+		     == ep->actual_length) ||
+		    /* bulk OUT last request */
+		    (!dir_in && req->req.zero)) {
+			req->req.status = 0;
+			/* device completion */
+			list_del_init(&req->usb_req_list);
+			req->req.complete(ep->dev_ep, &req->req);
+		}
+
+		if (tx_state->rem_size == 0)
+			status = 0;
+
+		/*
+		 * Host completion - end when nothing left to read/write,
+		 * otherwise keep going through queue.
+		 */
+		if (status != -EINPROGRESS)
+			break;
+		/* bulk IN/OUT transfers last request serves for one host urb */
+		if (tx_state->rem_size == req->req.actual) {
+			maudc_dbg(udc, "bulk IN/OUT last request return\n");
+			break;
+		}
+		/* bulk IN short packet */
+		if (dir_in && (req->req.actual % ep->dev_ep->maxpacket)) {
+			maudc_dbg(udc, "bulk IN short packet return\n");
+			break;
+		}
+		/*
+		 * Bulk OUT transfer contains multiple bulk OUT MA requests.
+		 * So for bulk out MA request just return to serve subsequent
+		 * bulk out MA request.
+		 */
+		if (!dir_in && host_len == length) {
+			maudc_dbg(udc, "normal bulk OUT MA request return\n");
+			break;
+		}
+		/*
+		 * Gadget driver should transfer more data to
+		 * serve bulk IN transfer, then wait for more requests.
+		 */
+		if (dir_in && (req->req.actual % ep->dev_ep->maxpacket == 0) &&
+		    (tx_state->rem_size - ep->actual_length > 0)
+		    && !req->req.zero) {
+			if (list_empty(ep->usb_req_list)) {
+				maudc_dbg(udc, "bulk IN: gadget request queue"
+					" is empty - waiting . . .\n");
+				while (list_empty(ep->usb_req_list))
+					msleep(1);
+			}
+			goto loop_request_queue;
+		}
+		/*
+		 * Doesn't copy all data in MA USB bulk OUT request.
+		 * Wait for additional gadget requests
+		 * to serve this MA USB bulk OUT request.
+		 */
+		if (!dir_in && (host_len > length)) {
+			if (list_empty(ep->usb_req_list)) {
+				maudc_dbg(udc, "bulk OUT: gadget request"
+					" is queue empty - waiting . . .\n");
+				while (list_empty(ep->usb_req_list))
+					msleep(1);
+			}
+			goto loop_request_queue;
+		}
+	}
+
+	return ep->actual_length;
+}
+
+
+/*------------------------- gadget operations -------------------------*/
+
+/**
+ * Handles control request to get device status.
+ */
+static int get_gadget_status(struct usb_ctrlrequest *ctrlreq,
+		struct mausb_udc *udc)
+{
+	int			ret = 0;
+	char			*buf;
+	unsigned int		address = le16_to_cpu(ctrlreq->wIndex);
+	struct usb_ep		*usb_ep = get_ep(udc, address);
+	struct mausb_host_ep	*ma_ep = mausb_find_ep_dev(usb_ep,
+					udc->mausb_dev);
+
+	if (!ma_ep) {
+		maudc_err(udc, "%s: cannot find endpoint\n", __func__);
+		return -EINVAL;
+	}
+
+	buf = (char *)ma_ep->buffer;
+
+	maudc_dbg(udc, "%s: responding to request type %i\n",
+			__func__, ctrlreq->bRequestType);
+
+	/* requesting device status */
+	switch (ctrlreq->bRequestType) {
+	case DEV_IN_REQ:
+		buf[0] = udc->dev_status;
+		ma_ep->actual_length = 1;
+		break;
+
+	/* requesting an endpoint status */
+	case EP_IN_REQ:
+		buf[0] = ma_ep->halted;
+		ma_ep->actual_length = 1;
+		break;
+
+	/* requesting interface status */
+	case INTF_IN_REQ:
+		buf[0] = 0;
+		break;
+
+	default:
+		maudc_dbg(udc, "%s: invalid request type %i\n",
+			__func__, ctrlreq->bRequestType);
+		ret = -EOPNOTSUPP;
+		break;
+	}
+
+	return ret;
+}
+
+/**
+ * Handles control request to set a gadget device feature.
+ */
+static int set_or_clear_gadget_feature(struct usb_ctrlrequest *ctrlreq,
+		struct mausb_udc *udc, int set)
+{
+	int			ret = 0;
+	unsigned int		address = le16_to_cpu(ctrlreq->wIndex);
+	struct usb_ep		*usb_ep = get_ep(udc, address);
+	struct mausb_host_ep	*ma_ep  = mausb_find_ep_dev(usb_ep,
+					udc->mausb_dev);
+
+	if (ctrlreq->bRequestType == EP_REQ) {
+
+		if (!ma_ep)
+			ret = -EINVAL;
+		else if (ma_ep->dev_ep->name == udc->gadget.ep0->name) {
+			maudc_err(udc, "%s: cannot halt ep0!\n", __func__);
+			ret = -EINVAL;
+		} else {
+			ma_ep->halted = set;
+			maudc_dbg(udc, "%s: %s halt %s\n", __func__,
+				ma_ep->dev_ep->name, set ? "set" : "cleared");
+		}
+	}
+
+	return ret;
+}
+
+/**
+ * Handles incoming management packets. Used to pass an MA USB management
+ * packet to the device.
+ */
+static int maudc_transfer_mgmt_packet(struct ms_pkt *ms_pkt, void *context)
+{
+	int			ret;
+	struct mausb_pkt	*pkt;
+	struct mausb_udc	*udc  = platform_get_drvdata(&udc_pdev);
+	struct mausb_mgmt	*mgmt;
+
+	if ((NULL == context) || (NULL == ms_pkt)) {
+		maudc_err(udc, "%s: received NULL %s\n", __func__,
+			context ? "packet" : "context");
+		return -EFAULT;
+	}
+
+	mgmt = (struct mausb_mgmt *) context;
+
+	pkt = mausb_pkt_from_ms_pkt(ms_pkt,
+			udc->ma_dev.ms_driver->ops->pkt_destructor, GFP_ATOMIC);
+
+	maudc_dbg(udc, "%s: received %s packet\n", __func__,
+		mausb_type_to_string(pkt->common->pkt_type));
+
+	ret = mausb_rx_mgmt(pkt, mgmt);
+
+	return ret;
+}
+
+/**
+ * Suspends gadget..Called when a MAUSBDeviceSleepReq management packet is
+ * received.
+ */
+static void mausb_device_suspend(void)
+{
+	struct mausb_udc *udc = platform_get_drvdata(&udc_pdev);
+
+	if (udc->driver)
+		udc->driver->suspend(&udc->gadget);
+}
+
+/**
+ * Resumes gadget. Called by mausb_bus_resume in hcd to resume a connected
+ * device.
+ */
+static void mausb_device_resume(void)
+{
+	struct mausb_udc *udc = platform_get_drvdata(&udc_pdev);
+
+	if (udc->driver)
+		udc->driver->resume(&udc->gadget);
+}
+
+/**
+ * Manages USB device connection to host. Called for connect and disconnect
+ * events.
+ */
+static int mausb_udc_pullup(struct usb_gadget *gadget, int is_on)
+{
+	struct mausb_udc *udc = gadget_to_udc(gadget);
+
+	udc->usb_ep[0].dev_ep.maxpacket = 64;
+	udc->gadget.speed = udc->driver->max_speed;
+	udc->pullup = is_on;
+
+	return 0;
+}
+
+/**
+ * Assigns an MA USB ep handle to the given endpoint.
+ */
+static void maudc_assign_ep_handle(struct mausb_host_ep *ma_ep,
+		struct usb_endpoint_descriptor *ep_des)
+{
+	struct mausb_ep_handle *handle = &ma_ep->ep_handle;
+
+	handle->dir = usb_endpoint_dir_in(ep_des) ? 1 : 0;
+	handle->ep_num = usb_endpoint_num(ep_des);
+	handle->dev_addr = MAUDC_DEV_ADDR;
+	handle->bus_num = MAUSB_VIRTUAL_BUS_NUM;
+
+	ma_ep->ep_handle_state = MAUSB_EP_HANDLE_ACTIVE;
+}
+
+/**
+ * Adds a media-agnostic endpoint datastructure to the UDC for the given
+ * endpoint.
+ */
+static int maudc_add_ma_ep(struct usb_endpoint_descriptor *ep_des,
+		struct mausb_dev *mausb_dev, struct mausb_host_ep **mausb_ep)
+{
+	int			ret = -EINVAL;
+	struct mausb_host_ep	*ma_ep;
+	struct mausb_ms_drv	*drv = mausb_dev->ma_dev->ms_driver;
+	struct mausb_udc	*udc = platform_get_drvdata(&udc_pdev);
+	struct usb_ep		*usb_ep = get_ep(udc, ep_des->bEndpointAddress);
+	unsigned long		irq_flags;
+	int (*transfer_pkt)(struct ms_pkt *pkt, void *context);
+
+	if (USB_ENDPOINT_XFER_CONTROL == usb_endpoint_type(ep_des)) {
+
+		maudc_dbg(udc, "%s: adding control endpoint %i...\n",
+			__func__, usb_endpoint_num(ep_des));
+		transfer_pkt = &receive_ma_packet_control;
+
+	} else if (usb_endpoint_dir_in(ep_des)) {
+		maudc_dbg(udc, "%s: adding IN endpoint %i...\n",
+			__func__, usb_endpoint_num(ep_des));
+		transfer_pkt = &receive_ma_packet_IN;
+
+	} else if (usb_endpoint_dir_out(ep_des)) {
+		maudc_dbg(udc, "%s: adding OUT endpoint %i...\n",
+			__func__, usb_endpoint_num(ep_des));
+		transfer_pkt = &receive_ma_packet_OUT;
+
+	} else {
+		maudc_dbg(udc, "%s: unknown endpoint type\n", __func__);
+		return -EINVAL;
+	}
+
+	mausb_internal_add_ep(mausb_dev, NULL, usb_ep, &ma_ep,
+		&device_transfer_timeout, GFP_ATOMIC);
+
+	if (NULL == ma_ep)
+		return -ENOMEM;
+
+	maudc_dbg(udc, "%s: added endpoint at 0x%p\n", __func__, ma_ep);
+
+	spin_lock_irqsave(&ma_ep->ep_lock, irq_flags);
+
+	maudc_assign_ep_handle(ma_ep, ep_des);
+	ret = mausb_add_data_channel(drv, ma_ep, transfer_pkt);
+
+	ma_ep->gadget = mausb_dev->gadget;
+
+	/*
+	 * Note: The UDC uses a single MA URB to track the transfer
+	 * State variables for the active transfer.
+	 */
+	ma_ep->active_transfer = mausb_alloc_maurb(ma_ep, GFP_ATOMIC);
+
+	if (NULL != mausb_ep)
+		*mausb_ep = ma_ep;
+
+	spin_unlock_irqrestore(&ma_ep->ep_lock, irq_flags);
+
+	return ret;
+}
+
+/**
+ * Registers a media specific driver with the media agnostic driver.
+ */
+struct mausb_ma_drv *maudc_register_ms_driver(struct mausb_ms_drv *drv)
+{
+	struct mausb_udc	*udc = platform_get_drvdata(&udc_pdev);
+	unsigned long		irq_flags;
+
+	spin_lock_irqsave(&udc->udc_lock, irq_flags);
+	udc->ma_dev.ms_driver = drv;
+	spin_unlock_irqrestore(&udc->udc_lock, irq_flags);
+
+	/* open the management channel */
+	maudc_dbg(udc, "%s: adding Managment channel...\n", __func__);
+	mausb_add_mgmt_channel(&udc->ma_dev.mgmt, drv,
+		&maudc_transfer_mgmt_packet, &mausb_mgmt_pkt_sent);
+
+	return &udc->ma_dev.ma_drv;
+}
+EXPORT_SYMBOL(maudc_register_ms_driver);
+
+int check_for_device(int dont_use)
+{
+	struct mausb_udc *udc = platform_get_drvdata(&udc_pdev);
+
+	if (udc->driver != NULL)
+		return 1;
+
+	return 0;
+}
+
+static int mausb_udc_start(struct usb_gadget *gadget,
+		struct usb_gadget_driver *driver)
+{
+	struct mausb_udc	*udc = gadget_to_udc(gadget);
+
+	maudc_dbg(udc, "%s: binding gadget '%s' driver\n",
+		__func__, driver->driver.name);
+
+	udc->driver = driver;
+
+	return 0;
+}
+
+static int mausb_udc_stop(struct usb_gadget *gadget,
+		struct usb_gadget_driver *driver)
+{
+	struct mausb_udc	*udc = gadget_to_udc(gadget);
+
+	maudc_dbg(udc, "%s: stopping udc\n", __func__);
+
+	udc->driver = NULL;
+
+	return 0;
+}
+
+/**
+ * API to UDC operations that do not involve endpoints or i/o.
+ */
+static struct usb_gadget_ops mudc_ops = {
+	.pullup = &mausb_udc_pullup,
+	.udc_start = &mausb_udc_start,
+	.udc_stop = &mausb_udc_stop
+};
+
+/**
+ * Appends a Capabilities Descriptor to the end of a CapResp packet.
+ *
+ * @resp:	     CapResp to that will carry descriptor.
+ * @type:	     Descriptor type to add.
+ *		     Note: make sure to use values from MA USB Spec, Table 14.
+ * @offset:          Total length of other Device Capability Descriptors
+ *		     in resp.
+ *
+ * Returns length of added descriptor.
+ */
+static int add_cap_desc(struct mausb_mgmt_pkt *resp, int type, int offset)
+{
+	int			ret = 0;
+	int			ttl_offset;
+	struct			mausb_dev_cap_desc *desc;
+	struct mausb_udc	*udc = platform_get_drvdata(&udc_pdev);
+
+	ttl_offset = MAUSB_PKT_HEADER_SIZE +
+			sizeof(struct mausb_CapResp_flds) + offset;
+	desc = cap_desc_ptr_increment(resp, ttl_offset);
+
+	switch (type) {
+	case MAUSB_DEV_CAP_SPEED:
+		desc->length = MAUSB_DEV_CAP_SPEED_LENGTH;
+		desc->cap_type = MAUSB_DEV_CAP_SPEED;
+		desc->speed.speed = MAUSB_DEV_CAP_SPEED_SUPER;
+		/* TODO: assign speed fields meaningful values */
+		desc->speed.lse = 1;
+		desc->speed.st = 2;
+		desc->speed.lane_count = 3;
+		desc->speed.link_protocol = 3;
+		desc->speed.lsm = 234;
+		ret = desc->length;
+		break;
+	case MAUSB_DEV_CAP_POUT:
+		break;
+	case MAUSB_DEV_CAP_ISO:
+		break;
+	case MAUSB_DEV_CAP_SYNC:
+		break;
+	case MAUSB_DEV_CAP_CONT_ID:
+		break;
+	case MAUSB_DEV_CAP_LINK_SLEEP:
+		desc->length = MAUSB_DEV_CAP_LINK_SLEEP_LENGTH;
+		desc->cap_type = MAUSB_DEV_CAP_LINK_SLEEP;
+		desc->link_sleep.link_sleep_cap = 0;
+		ret = desc->length;
+		break;
+	default:
+		maudc_err(udc, "%s: invalid Device Capability Type %i\n",
+			__func__, type);
+		break;
+	}
+
+	return ret;
+}
+
+/**
+ * Finds an MA USB endpoint descriptor inside of an endpoint handle response.
+ */
+static void maudc_fill_ma_ep_desc(struct mausb_ep_des *ma_ep_des,
+		struct usb_endpoint_descriptor *ep_des,
+		struct mausb_host_ep *ma_ep)
+{
+	ma_ep_des->ep_handle.handle = ma_ep->ep_handle.handle;
+	ma_ep_des->dir = usb_endpoint_dir_in(ep_des) ? 1 : 0;
+	ma_ep_des->iso = usb_endpoint_xfer_isoc(ep_des) ? 1 : 0;
+	ma_ep_des->l_man = 0;
+	ma_ep_des->valid = 0;
+	ma_ep_des->rsvd_0 = 0;
+
+	if ((usb_endpoint_dir_out(ep_des) && !usb_endpoint_xfer_isoc(ep_des))
+			|| usb_endpoint_xfer_control(ep_des)) {
+		ma_ep_des->ccu = 1;
+	} else
+		ma_ep_des->ccu = 0;
+
+	ma_ep_des->rsvd_1 = 0;
+
+	if (usb_endpoint_dir_out(ep_des) || usb_endpoint_xfer_control(ep_des))
+		ma_ep_des->buffer_size = ma_ep->state.rx_buf_size;
+	else
+		ma_ep_des->buffer_size = 0;
+
+	if (usb_endpoint_xfer_isoc(ep_des)) {
+		/* TODO: iso transfers */
+		/* ma_ep_des->iso_prog_dly = 0; */
+		/* ma_ep_des->iso_resp_dly = 0; */
+	} else {
+		ma_ep_des->iso_prog_dly = 0;
+		ma_ep_des->iso_resp_dly = 0;
+	}
+}
+
+/**
+ * Handles incoming EPHandleReq packets and fills EPHandleResp fields.
+ */
+static enum mausb_pkt_status maudc_handle_ep_req(struct mausb_dev *mausb_dev,
+		struct mausb_EPHandleReq_flds *req,
+		struct mausb_EPHandleResp_flds *resp)
+{
+	int				i;
+	enum mausb_pkt_status		status = UNSUCCESSFUL;
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	struct usb_endpoint_descriptor	*ep_des;
+	struct usb_ep			*usb_ep;
+	struct mausb_host_ep		*ma_ep;
+
+	resp->num_ep_des = req->num_ep_des;
+
+	/* TODO: Sanity check; make sure we dont look past end of packet */
+	for (i = 0; i < req->num_ep_des; ++i) {
+
+		ep_des = mausb_ep_handle_req_get_ep_des(req, i);
+
+		if (NULL != ep_des) {
+
+			ma_ep = mausb_find_ep_by_address(
+				ep_des->bEndpointAddress, mausb_dev);
+
+			if (NULL != ma_ep) {
+				maudc_warn(udc, "%s: endpoint already exists"
+					"  resetting . . .\n", __func__);
+				mausb_internal_drop_ep(ma_ep);
+			}
+
+			maudc_add_ma_ep(ep_des, mausb_dev, &ma_ep);
+
+			if (NULL == ma_ep) {
+				maudc_err(udc, "%s: failed to add endpoint"
+					" for entry %i\n", __func__, i);
+			}
+
+			maudc_fill_ma_ep_desc(&resp->ep_des[i], ep_des, ma_ep);
+
+			usb_ep = get_ep(udc, ep_des->bEndpointAddress);
+
+			if (NULL != usb_ep)
+				mausb_link_ma_ep_to_usb_ep(ma_ep, usb_ep);
+
+			/* Per spec, if at least 1 endpoint assignment
+			 * is sucessful, the packet returns success.
+			 */
+			status = SUCCESS;
+		}
+	}
+
+	return status;
+}
+
+/**
+ * Handles incoming EPHandleDeleteReq packets and fills EPHandleDeleteResp
+ * fields.
+ */
+static enum mausb_pkt_status maudc_handle_ep_del_req(
+		struct mausb_dev *mausb_dev,
+		struct mausb_EPHandleDelete_flds *req,
+		struct mausb_EPHandleDelete_flds *resp)
+{
+	int				i;
+	int				ret = 0;
+	enum mausb_pkt_status		status = UNSUCCESSFUL;
+	struct mausb_udc		*udc = platform_get_drvdata(&udc_pdev);
+	struct mausb_host_ep		*ma_ep = NULL;
+	struct mausb_ep_handle		*req_handle;
+
+	resp->num_ep = 0;
+
+	/* TODO: Sanity check; make sure we dont look past end of packet */
+	for (i = 0; i < req->num_ep; ++i) {
+		req_handle = &req->ep_handle[i];
+
+		ma_ep = mausb_find_ep_by_handle(req_handle, mausb_dev);
+
+		ret = mausb_internal_drop_ep(ma_ep);
+
+		if (0 <= ret) {
+			/*
+			 * per spec, the packet returns sucess if it can
+			 * delete at least 1 endpoint
+			 */
+			status = SUCCESS;
+
+		} else {
+			resp->ep_handle[resp->num_ep].handle
+						= req_handle->handle;
+			resp->num_ep++;
+
+			maudc_err(udc, "%s: could not delete endpoint with"
+				" handle %x\n", __func__, req_handle->handle);
+		}
+	}
+
+	return status;
+}
+
+/**
+ * Inactivate endpoints in EPInactivateReq packet and fill
+ * EPInactivateResp fields.
+ */
+static enum mausb_pkt_status maudc_handle_ep_inactivate(
+		struct mausb_EPInactivateReq_flds *req,
+		struct mausb_EPInactivateResp_flds *resp,
+		struct mausb_dev *dev)
+{
+	int			i = 0;
+	int			failures = 0;
+	enum			mausb_pkt_status status = SUCCESS;
+	struct mausb_host_ep	*ma_ep = NULL;
+	struct mausb_ep_handle	*req_handle;
+
+	for (i = 0; i < req->num_ep_handles; ++i) {
+		req_handle = &req->ep_handle[i];
+		ma_ep = mausb_find_ep_by_handle(req_handle, dev);
+
+		if (!ma_ep) {
+			resp->ep_handle[failures] = *req_handle;
+			status = UNSUCCESSFUL;
+			failures++;
+		} else
+			ma_ep->ep_handle_state = MAUSB_EP_HANDLE_INACTIVE;
+	}
+
+	resp->num_ep_with_error = failures;
+
+	return status;
+}
+
+/**
+ * This function handles management requests to the UDC. It parses the packet
+ * and makes the necessary function calls into the system.
+ *
+ * @req:	The incoming request to handle.
+ * @resp: 	The response packet to send out.
+ * @mgmt:	The MA USB management structure associated with transfer.
+ *
+ * Note: this function only fills the packet type, status and
+ * request-specific data-fields. The remainder of the fields are set by
+ * mausb_rx_mgmt_req.
+ */
+static int udc_mgmt_req_switch(struct mausb_mgmt_pkt *req,
+		struct mausb_mgmt_pkt *resp, struct mausb_mgmt *mgmt)
+{
+	int			ret = -EINVAL;
+	int			desc_count = 0;
+	int			offset = 0;
+	struct ma_dev		*ma_dev = mausb_mgmt_to_ma_dev(mgmt);
+	struct mausb_udc	*udc = mausb_ma_dev_to_udc(ma_dev);
+
+	/* always set the device handle */
+	req->common.dev_handle = udc->mausb_dev->dev_handle;
+
+	switch (req->common.pkt_type) {
+
+	case CapReq:
+		/* non-hub devices require a Speed Capability Descriptor */
+		ret = add_cap_desc(resp, MAUSB_DEV_CAP_SPEED, offset);
+		if (ret <= 0) {
+			maudc_err(udc, "%s: could not add speed capability "
+				"descriptor\n", __func__);
+		} else {
+			desc_count++;
+			offset += ret;
+		}
+
+		ret = add_cap_desc(resp, MAUSB_DEV_CAP_LINK_SLEEP, offset);
+		if (ret <= 0) {
+			maudc_err(udc, "%s; could not add link sleep"
+				 " capability descriptor\n", __func__);
+		} else {
+			desc_count++;
+			offset += ret;
+		}
+
+		/*
+		 * TODO: if want to add more device cap descriptors, this would
+		 * be a good place to do it.
+		 */
+
+		resp->common.pkt_type = CapResp;
+		resp->common.pkt_status = NO_ERROR;
+		resp->cap_resp.num_ep = CAP_MAX_EP_NUM;
+		resp->cap_resp.num_dev = CAP_MAX_DEV_NUM;
+		resp->cap_resp.num_stream = EP_MAX_STREAMS;
+		resp->cap_resp.dev_type = CAP_DEV_TYPE;
+		resp->cap_resp.desc_count = desc_count;
+		resp->cap_resp.desc_length = offset;
+		resp->cap_resp.max_tx_reqs = CAP_MAX_TX_REQS;
+		resp->cap_resp.max_mgmt_reqs = CAP_MAX_MGMT_REQS;
+
+		ret = 0;
+		break;
+	case USBDevHandleReq:
+		udc->mausb_dev->dev_handle = MAUDC_DEV_HANDLE;
+
+		resp->common.pkt_type     = USBDevHandleResp;
+		resp->common.pkt_status   = NO_ERROR;
+		resp->usb_dev_handle_resp = MAUDC_DEV_HANDLE;
+
+		ret = 0;
+		break;
+	case EPHandleReq:
+		resp->common.pkt_type   = EPHandleResp;
+		resp->usb_dev_handle_resp = udc->mausb_dev->dev_handle;
+		resp->common.pkt_status = maudc_handle_ep_req(udc->mausb_dev,
+			&req->ep_handle_req, &resp->ep_handle_resp);
+		ret = 0;
+		break;
+	case EPActivateReq:
+		break;
+	case EPInactivateReq:
+		resp->common.pkt_type   = EPInactivateResp;
+		resp->common.pkt_status = maudc_handle_ep_inactivate(
+						&req->ep_inactivate_req,
+						&resp->ep_inactivate_resp,
+						udc->mausb_dev);
+		ret = 0;
+		break;
+	case EPResetReq:
+		break;
+	case EPClearTransferReq:
+		break;
+	case EPHandleDeleteReq:
+		resp->common.pkt_type   = EPHandleDeleteResp;
+
+		resp->common.pkt_status = maudc_handle_ep_del_req(
+			udc->mausb_dev, &req->ep_handle_delete,
+					&resp->ep_handle_delete);
+		ret = 0;
+		break;
+	case DevResetReq:
+		resp->common.pkt_type   = DevResetResp;
+
+		ma_dev->ma_dev_addr = req->common.ma_dev_addr;
+		ma_dev->mass_id = req->common.mass_id;
+
+		ret = 0;
+
+		if (0 == ret) {
+			resp->common.pkt_status = NO_ERROR;
+		} else {
+			resp->common.pkt_status =
+				mausb_errno_to_ma_status(ret);
+		}
+		break;
+	case ModifyEP0Req:
+		resp->common.pkt_type   = ModifyEP0Resp;
+		resp->common.pkt_status = SUCCESS;
+
+		/*
+		 * TODO: fix so to spec - for now we don't track USB device
+		 * state and can't do proper checks.
+		 */
+		if (req->modify_ep0_req.ep_handle.dev_addr == 0) {
+			/* && USB device is in addressed state) { */
+			resp->modify_ep0_resp.ep_handle.handle =
+				req->modify_ep0_req.ep_handle.handle
+					| (MAUDC_DEV_ADDR << 5);
+		} else {
+			resp->modify_ep0_resp.ep_handle.handle = 0;
+		}
+
+		ret = 0;
+		break;
+	case SetUSBDevAddrReq:
+		resp->common.pkt_type   = SetUSBDevAddrResp;
+		resp->common.pkt_status = NO_ERROR;
+		udc->mausb_dev->dev_address = MAUDC_DEV_ADDR;
+
+		resp->set_dev_addr_resp.dev_addr = udc->mausb_dev->dev_address;
+		ret = 0;
+		break;
+	case UpdateDevReq:
+		break;
+	case USBDevDisconnectReq:
+		resp->common.pkt_type   = USBDevDisconnectResp;
+		resp->common.dev_handle	= req->common.dev_handle;
+		resp->common.pkt_status = NO_ERROR;
+		ret = 0;
+		break;
+	case DevDisconnectReq:
+		resp->common.pkt_type   = DevDisconnectResp;
+		resp->common.dev_handle	= 0;
+		resp->common.pkt_status = NO_ERROR;
+		ret = 0;
+		break;
+	case SleepReq:
+		resp->common.pkt_type   = SleepResp;
+		resp->common.pkt_status = NO_ERROR;
+		mausb_device_suspend();
+		ret = 0;
+		break;
+	case WakeReq:
+		resp->common.pkt_type   = WakeResp;
+		resp->common.pkt_status = NO_ERROR;
+		mausb_device_resume();
+		ret = 0;
+		break;
+	case PingReq:
+		break;
+	case SyncReq:
+		break;
+	case CancelTransferReq:
+		resp->common.pkt_type   = CancelTransferResp;
+		resp->common.pkt_status = NO_ERROR;
+		resp->cancel_transfer_resp.ep_handle =
+				req->cancel_transfer_req.ep_handle;
+		resp->cancel_transfer_resp.stream_id =
+				req->cancel_transfer_req.stream_id;
+		resp->cancel_transfer_resp.req_id =
+				req->cancel_transfer_req.req_id;
+		/* TODO: cancel transfer and set fields below accordingly */
+		resp->cancel_transfer_resp.cancel_status = 1;
+		resp->cancel_transfer_resp.deliv_seq_num = 0;
+		resp->cancel_transfer_resp.deliv_byte_offset = 0;
+		ret = 0;
+		break;
+	case EPOpenStreamReq:
+		break;
+	case EPCloseStreamReq:
+		break;
+	case USBDevResetReq:
+		break;
+	case VendorSpecificReq:
+		break;
+
+	/* Invalid packet type */
+	default:
+		maudc_err(udc, "%s: invalid packet type\n", __func__);
+		ret = -EBADMSG;
+		break;
+	}
+
+	return ret;
+}
+
+/**
+ * MA USB ep0 endpoint descriptor for a super speed endpoint
+ * per spec section 7.3.2.2.
+ */
+static struct usb_endpoint_descriptor mausb_ep0_desc = {
+
+	.bLength = USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType = USB_DT_ENDPOINT,
+	.bEndpointAddress = 0x00,
+	.bmAttributes = USB_ENDPOINT_XFER_CONTROL,
+	.wMaxPacketSize = 512,
+	.bInterval = 0
+};
+
+/**
+ * UDC probe function
+ */
+int mausb_udc_probe(struct platform_device *pdev)
+{
+	int				ret = 0;
+	int				i;
+	struct mausb_udc		*udc;
+	struct usb_ep			*usb_ep;
+
+	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
+	if (!udc)
+		return -ENOMEM;
+
+	udc->udc_lock = __SPIN_LOCK_UNLOCKED(udc->udc_lock);
+	/* initialize gadget structure */
+	udc->gadget.name = UDC_NAME;
+	udc->gadget.ops = &mudc_ops;
+	udc->gadget.speed = USB_SPEED_UNKNOWN;
+	udc->gadget.max_speed = USB_SPEED_SUPER;
+	udc->gadget.dev.parent = &pdev->dev;
+
+	/* set head for list of gadget endpoints   */
+	INIT_LIST_HEAD(&udc->gadget.ep_list);
+
+	/* initalize the ma_dev structure */
+	mausb_init_ma_device(&udc->ma_dev, 0, 0,
+		NULL, udc, &udc_mgmt_req_switch, &check_for_device);
+
+	udc->mausb_dev = mausb_internal_alloc_dev(&udc->ma_dev, NULL,
+		&udc->gadget);
+
+	for (i = 0; i < MAUDC_MAX_NUM_EP; i++) {
+		usb_ep = &udc->usb_ep[i].dev_ep;
+
+		usb_ep->name = ep_name[i];
+		usb_ep->ops = &mausb_ep_ops;
+		usb_ep->maxpacket = EP_MAX_PACKET;
+		usb_ep->max_streams = EP_MAX_STREAMS;
+
+		list_add_tail(&usb_ep->ep_list, &udc->gadget.ep_list);
+
+		INIT_LIST_HEAD(&udc->usb_ep[i].usb_req_list);
+	}
+
+	/* give gadget driver ep0 then remove from list*/
+	udc->usb_ep[0].dev_ep.desc = &mausb_ep0_desc;
+	udc->gadget.ep0 = &udc->usb_ep[0].dev_ep;
+	list_del_init(&udc->usb_ep[0].dev_ep.ep_list);
+
+	/* add a new gadget to the udc class driver list */
+	ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "%s: could not add gadget UDC, error %d\n",
+			__func__, ret);
+	} else
+		platform_set_drvdata(pdev, udc);
+
+	return ret;
+}
+
+int mausb_udc_remove(struct platform_device *pdev)
+{
+	struct mausb_udc *udc = platform_get_drvdata(pdev);
+
+	dev_dbg(&pdev->dev, "%s: removing udc\n", __func__);
+
+	usb_del_gadget_udc(&udc->gadget);
+
+	return 0;
+}
+
+int mausb_udc_suspend(struct platform_device *pdev, pm_message_t state)
+{
+	struct mausb_udc *udc = platform_get_drvdata(pdev);
+
+	dev_dbg(&pdev->dev, "%s: suspending udc\n", __func__);
+
+	udc->suspended = 1;
+
+	return 0;
+}
+
+int mausb_udc_resume(struct platform_device *pdev)
+{
+	struct mausb_udc *udc = platform_get_drvdata(pdev);
+
+	dev_dbg(&pdev->dev, "%s: resuming udc\n", __func__);
+
+	udc->suspended = 0;
+
+	return 0;
+}
+
+/**
+ * platform structures for USB Device Controller (UDC)
+ */
+static struct platform_device udc_pdev = {
+	.name = UDC_NAME,
+	.id = 0
+};
+
+static struct platform_driver udc_driver = {
+	.probe = mausb_udc_probe,
+	.remove = mausb_udc_remove,
+	.suspend = mausb_udc_suspend,
+	.resume = mausb_udc_resume,
+	.driver = {
+		.name = UDC_NAME,
+		.owner = THIS_MODULE
+	}
+};
+
+/**
+ * initialization function
+ */
+static int mausb_udc_init(void)
+{
+	int ret;
+
+	printk(KERN_DEBUG "loading MA USB UDC . . .\n");
+
+	/* register UDC driver */
+	ret = platform_driver_register(&udc_driver);
+	if (ret < 0) {
+		printk(KERN_DEBUG "failed to register UDC driver - "
+			"error # %d\n", ret);
+	} else {
+		/* register UDC device */
+		ret = platform_device_register(&udc_pdev);
+		if (ret < 0) {
+			printk(KERN_DEBUG "failed to register UDC device - "
+				"error # %d\n", ret);
+			platform_driver_unregister(&udc_driver);
+
+		} else {
+			/* direct the release function (for exiting) */
+			udc_pdev.dev.release = &udc_dev_release;
+		}
+	}
+
+	return ret;
+}
+module_init(mausb_udc_init);
+
+/**
+ * exit function
+ */
+static void mausb_udc_exit(void)
+{
+	/* deregister UDC device */
+	platform_device_unregister(&udc_pdev);
+
+	/* deregister UDC driver */
+	platform_driver_unregister(&udc_driver);
+
+}
+module_exit(mausb_udc_exit);
+
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_AUTHOR("Intel");
+MODULE_DESCRIPTION("mausb_udc");
drivers/staging/mausb/drivers/mausb_udc.h new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
1.9.1
#18 lore raw

PATCH 7/10 added media agnostic (MA) USB management packet handling

In reply to Stephanie Wallick

The Media Agnostic USB Specification outlines a number of managment packet types for management and control functions. Each function is initiated with a particular type of managment request packet and completed with the corresponding management response packet. This is where we fill the fields for outgoing management packets and parse and handle incoming management packets.

---
 drivers/staging/mausb/drivers/mausb_mgmt.c | 888 +++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_mgmt.h |  90 +++
 2 files changed, 978 insertions(+)
 create mode 100755 drivers/staging/mausb/drivers/mausb_mgmt.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_mgmt.h
drivers/staging/mausb/drivers/mausb_mgmt.c new file +888 -0
new file mode 100755
index 0000000..7301ae3
@@ -0,0 +1,888 @@
+/* name:	mausb_mgmt.c
+ * description: Handles management packet operations common to both
+ *              hosts and devices. This includes parsing incoming packets,
+ *              as well as creating outgoing packets.
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/usb.h>
+#include <linux/slab.h>
+
+#include "mausb_pkt.h"
+#include "mausb_mgmt.h"
+#include "mausb_mem.h"
+#include "mausb_msapi.h"
+#include "mausb_const.h"
+
+/**
+ * Returns the ma_dev structure for a given device.
+ */
+struct ma_dev *mausb_mgmt_to_ma_dev(struct mausb_mgmt *mgmt)
+{
+	return container_of(mgmt, struct ma_dev, mgmt);
+}
+EXPORT_SYMBOL(mausb_mgmt_to_ma_dev);
+
+/**
+ * Opens a connection to the Media Specific driver to pass management packets
+ * to a particular MA device.
+ *
+ * The caller should be holding the lock protecting the MA device's data.
+ *
+ * Returns 0 on success, or a negative errno on failure.
+ *
+ * @mgmt:		The struct for the management channel to the MA device.
+ *              	after this function
+ * @drv:		The specific medium we are communicating with
+ * @transfer_pkt:	A pointer to the function that should be called by the
+ *                    	MS driver to pass incoming management packets to the
+ *                    	MA driver.
+ * @pkt_sent:		Completion callback for outgoing management packets.
+ *			This function will be called by the MS driver once a
+ *			packet has been sent to the medium to indicate that
+ *			it is done with the packet. Note: the completion
+ *			callback can be disabled by setting this to NULL.
+ */
+int mausb_add_mgmt_channel(struct mausb_mgmt *mgmt, struct mausb_ms_drv *drv,
+		int  (*transfer_pkt)(struct ms_pkt *pkt, void *context),
+		void (*pkt_sent)(struct ms_pkt *pkt))
+{
+	int				ret = 0;
+	struct mausb_transfer_pair	tx_pair;
+	unsigned long			irq_flags;
+
+	tx_pair.to_ma.transfer_packet = transfer_pkt;
+	tx_pair.pkt_sent = pkt_sent;
+	tx_pair.to_ma.context = mgmt;
+	tx_pair.handle = MAUSB_MGMT_HANDLE;
+
+	ret = drv->ops->add_transfer_pair(&tx_pair);
+
+	if (ret >= 0) {
+		spin_lock_irqsave(mgmt->ma_dev_lock, irq_flags);
+		mgmt->tx_pair = tx_pair;
+		spin_unlock_irqrestore(mgmt->ma_dev_lock, irq_flags);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL(mausb_add_mgmt_channel);
+
+/**
+ * Fills the fields in an endpoint handle req.
+ *
+ * @dev:	The device struct that contains the endpoints to open.
+ * @req_flds:	The req_flds to fill.
+ *
+ * Note: this function can only be used on the host side.
+ */
+int mausb_fill_ep_handle_req_flds(struct mausb_dev *mausb_dev,
+		struct mausb_mgmt_pkt *req, bool add)
+{
+	int				   i = 0;
+	struct mausb_host_ep		   *ma_ep;
+	unsigned long			   irq_flags;
+	struct mausb_ss_ep_req		   *ss_ep_des;
+	struct mausb_ep_req		   *ep_des;
+	struct mausb_EPHandleReq_flds	   *req_flds = &req->ep_handle_req;
+	struct mausb_EPHandleDelete_flds   *del_flds = &req->ep_handle_delete;
+
+	req_flds->num_ep_des = 0;
+
+	if (USB_SPEED_SUPER == mausb_dev->dev->speed)
+		req_flds->size_ep_des = cpu_to_le32(MAUSB_SS_EP_REQ_SIZE);
+	else
+		req_flds->size_ep_des = cpu_to_le32(MAUSB_EP_REQ_SIZE);
+
+	/* for every endpoint */
+	spin_lock_irqsave(&mausb_dev->dev_lock, irq_flags);
+	list_for_each_entry(ma_ep, &mausb_dev->ep_list, ep_list) {
+		spin_unlock_irqrestore(&mausb_dev->dev_lock, irq_flags);
+		spin_lock_irqsave(&ma_ep->ep_lock, irq_flags);
+
+		/* if we need to add this endpoint */
+		if (add && (MAUSB_EP_HANDLE_UNASSIGNED
+					== ma_ep->ep_handle_state)) {
+
+			if (USB_SPEED_SUPER == mausb_dev->dev->speed &&
+				ma_ep->ep->desc.bEndpointAddress != 0x00) {
+				ss_ep_des = &req_flds->ss_ep_des[i];
+
+				memcpy(&ss_ep_des->ep_des,
+					&ma_ep->ep->desc,
+					USB_DT_ENDPOINT_SIZE);
+
+				memcpy(&ss_ep_des->ss_ep_comp_des,
+					&ma_ep->ep->ss_ep_comp,
+					USB_DT_SS_EP_COMP_SIZE);
+			} else {
+				ep_des = &req_flds->ep_des[i];
+
+				memcpy(&ep_des->ep_des,
+					&ma_ep->ep->desc,
+					USB_DT_ENDPOINT_SIZE);
+			}
+
+			if (ma_ep->ep->desc.bEndpointAddress == 0x00)
+				req_flds->size_ep_des = MAUSB_EP_REQ_SIZE;
+
+			i++;
+			req_flds->num_ep_des = i;
+
+		/* if we need to delete this endpoint */
+		} else if (!add &&
+			(MAUSB_EP_HANDLE_DELETED == ma_ep->ep_handle_state)) {
+
+			del_flds->ep_handle[i].handle =
+				cpu_to_le16(ma_ep->ep_handle.handle);
+
+			i++;
+			req_flds->num_ep_des = i;
+		}
+
+		spin_unlock_irqrestore(&ma_ep->ep_lock, irq_flags);
+
+		spin_lock_irqsave(&mausb_dev->dev_lock, irq_flags);
+	}
+
+	spin_unlock_irqrestore(&mausb_dev->dev_lock, irq_flags);
+
+	if (i <= 0)
+		return -ENODEV;
+
+	return 0;
+}
+
+/**
+ * Looks for inactive endpoints and fills EPInactivateReq packet fields
+ * accordingly.
+ */
+int mausb_fill_ep_inactivate_req_flds(struct mausb_dev *mausb_dev,
+		struct mausb_mgmt_pkt *req)
+{
+	int				    i = 0;
+	struct mausb_host_ep		    *ma_ep;
+	unsigned long			    irq_flags;
+	struct mausb_EPInactivateReq_flds   *req_flds = &req->ep_inactivate_req;
+
+	/*
+	 * Grab any inactive endpoints. After EPInactivateReq is sent, inactive
+	 * endoints will be marked for deletion.
+	 */
+	spin_lock_irqsave(&mausb_dev->dev_lock, irq_flags);
+	list_for_each_entry(ma_ep, &mausb_dev->ep_list, ep_list) {
+		spin_unlock_irqrestore(&mausb_dev->dev_lock, irq_flags);
+		spin_lock_irqsave(&ma_ep->ep_lock, irq_flags);
+
+		/* If endpoint has been inactivated, include it in packet. */
+		if (ma_ep->ep_handle_state == MAUSB_EP_HANDLE_INACTIVE) {
+			req_flds->ep_handle[i] = ma_ep->ep_handle;
+			i++;
+			req_flds->num_ep_handles = i;
+			ma_ep->ep_handle_state = MAUSB_EP_HANDLE_DELETED;
+		}
+
+		spin_unlock_irqrestore(&ma_ep->ep_lock, irq_flags);
+		spin_lock_irqsave(&mausb_dev->dev_lock, irq_flags);
+	}
+
+	/*TODO: set suspend flag when needed */
+
+	spin_unlock_irqrestore(&mausb_dev->dev_lock, irq_flags);
+
+	return req_flds->num_ep_handles;
+}
+
+/**
+ * Transmits a mausb management packet req and awaits a resp
+ *
+ * Caller should be holding the lock in the mausb_mgmt structure.
+ *
+ * This function transmits a MA USB management request packet and waits for a
+ * response. If a response packet is received, this function will return
+ * 0 with valid data in the resp. If the request timed out, will return
+ * -ETIMEDOUT.
+ *
+ * Note: since the calling process sleeps, this function CANNOT be called
+ *       from an interrupt context.
+ *
+ * @req:	A pointer to the MA USB transferRequest packet to be sent.
+ * @resp:	A pointer to the MA USB transferResponse packet received.
+ * @ma_mgmt:	The Ma USB device's management structure.
+ */
+static int internal_mausb_tx_mgmt_req(struct mausb_pkt *req,
+		struct mausb_pkt **resp, struct mausb_mgmt *ma_mgmt)
+{
+	int				retries = 0;
+	int				rem_time = 0;
+	struct mausb_mgmt_req_list	*req_entry;
+	struct mausb_pkt_transfer	*transfer;
+	unsigned long			irq_flags = 0;
+
+	if (NULL == ma_mgmt->tx_pair.to_ms.transfer_packet)
+		return -EPIPE;
+	else
+		transfer = &ma_mgmt->tx_pair.to_ms;
+
+	req_entry = kzalloc(sizeof(struct mausb_mgmt_req_list), GFP_ATOMIC);
+	if (!req_entry)
+		return -ENOMEM;
+
+	req_entry->mgmt_req = req;
+	req_entry->mgmt_resp = NULL;
+	init_completion(&req_entry->resp_rcvd);
+	INIT_LIST_HEAD(&req_entry->req_list);
+
+	list_add_tail(&req_entry->req_list, &ma_mgmt->req_list);
+
+	req->common->length = cpu_to_le16(mausb_pkt_length(req));
+
+	ma_mgmt->token = (ma_mgmt->token % MAUSB_MGMT_MAX_TOKEN) + 1;
+	req->mgmt->token = ma_mgmt->token;
+
+	/* until we time out or complete */
+	for (retries = 0; (retries <= MAUSB_MGMT_TRANSFER_RETRIES) &&
+			(NULL == req_entry->mgmt_resp); retries++) {
+
+		/* if this is a retry */
+		if (retries > 0)
+			req->common->ver_flags |= MAUSB_PKT_FLAG_RETRY;
+
+		spin_unlock_irqrestore(ma_mgmt->ma_dev_lock, irq_flags);
+
+		mausb_pkt_fill_ms_pkt(req, GFP_ATOMIC);
+
+		mamgmt_dbg("%s: sending %s packet\n", __func__,
+			mausb_type_to_string(req->common->pkt_type));
+		transfer->transfer_packet(&req->pkt, transfer->context);
+
+		rem_time = wait_for_completion_interruptible_timeout(
+			&req_entry->resp_rcvd, MAUSB_MGMT_RT_DELAY);
+
+		spin_lock_irqsave(ma_mgmt->ma_dev_lock, irq_flags);
+	}
+
+	list_del(&req_entry->req_list);
+	*resp = req_entry->mgmt_resp;
+
+	kfree(req_entry);
+
+	if (rem_time == 0) /* if we timed out */
+		return -ETIMEDOUT;
+	else if (rem_time < 0) /* if an error happened */
+		return rem_time;
+	else /* return based on the status of the response */
+		return 0;
+}
+
+/**
+ * Accepts the EPHandleResp and EPHandleReq Fields and updates
+ * endpoint(s) state based on the Resp Fields.
+ */
+static int mausb_handle_ep_resp(struct mausb_dev *mausb_dev,
+		struct mausb_EPHandleReq_flds *req,
+		struct mausb_EPHandleResp_flds *resp)
+{
+	int				i;
+	int				ret = -EINVAL;
+	struct mausb_host_ep		*ma_ep;
+	struct usb_endpoint_descriptor	*ep_des;
+
+	if (le32_to_cpu(req->num_ep_des) != le32_to_cpu(resp->num_ep_des))
+		return -EINVAL;
+
+	for (i = 0; i < resp->num_ep_des; ++i) {
+
+		ep_des = mausb_ep_handle_req_get_ep_des(req, i);
+		if (NULL != ep_des) {
+
+			ma_ep = mausb_find_ep_by_desc(ep_des, mausb_dev);
+			if (ma_ep != NULL)
+				ret = mausb_update_ep(ma_ep, &resp->ep_des[i]);
+			else {
+				mamgmt_err("%s: could not find endpoint"
+				" for handle #%i (%x)\n", __func__, i,
+				le16_to_cpu(resp->ep_des[i].ep_handle.handle));
+			}
+		}
+	}
+
+	return ret;
+}
+
+/**
+ * Accepts the EPHandleDeleteReq and EPHandleDeleteResp Fields and updates
+ * the endpoint(s) state based on the Resp Fields.
+ */
+static int mausb_handle_ep_del_resp(struct mausb_dev *mausb_dev,
+		struct mausb_EPHandleDelete_flds *req,
+		struct mausb_EPHandleDelete_flds *resp)
+{
+	int ret			      = 0;
+	int			      i, j;
+	bool			      drop_failed;
+	struct mausb_host_ep	      *ma_ep;
+	struct mausb_ep_handle	      *req_handle;
+	struct mausb_ms_drv	      *ms_driver = mausb_dev->ma_dev->ms_driver;
+
+	/* for every endpoint in the req */
+	for (i = 0; i < req->num_ep; ++i) {
+
+		drop_failed = false;
+		req_handle = &req->ep_handle[i];
+
+		for (j = 0; j < le32_to_cpu(resp->num_ep); ++j) {
+			if (req_handle->handle ==
+				le16_to_cpu(resp->ep_handle[j].handle))
+
+				drop_failed = true;
+		}
+
+		if (false == drop_failed) {
+
+			ma_ep = mausb_find_ep_by_handle(req_handle, mausb_dev);
+
+			ret = ms_driver->ops->drop_transfer_pair(
+				ma_ep->ep_handle.handle,
+				ma_ep->tx_pair.to_ms.context);
+
+			if (0 <= ret) {
+				/* drop the endpoint locally */
+				ret = mausb_internal_drop_ep(ma_ep);
+			}
+		}
+	}
+
+	return ret;
+}
+
+/**
+ * Determines the speed to report in the USBDevHandleReq.
+ */
+static __u8 mausb_get_dev_speed(struct mausb_dev *dev)
+{
+	switch (dev->dev->speed) {
+	case USB_SPEED_LOW:
+		return MAUSB_LOW_SPEED;
+	case USB_SPEED_FULL:
+		return MAUSB_FULL_SPEED;
+	case USB_SPEED_HIGH:
+		return MAUSB_HIGH_SPEED;
+	case USB_SPEED_SUPER:
+		return MAUSB_SUPER_SPEED;
+	/* TODO: SUPER_SPEED_PLUS */
+	default:
+		return MAUSB_INVALID_SPEED;
+	}
+}
+
+/**
+ * Parses all descriptors for a given CapResp packet and stores data in
+ * corresponding device data structure.
+ *
+ * Returns number of descriptors read.
+ */
+int parse_cap_desc(struct mausb_pkt *resp, struct ma_dev *ma_dev)
+{
+	int			   ret = 0;
+	int			   offset = 0;
+	int			   desc_count = resp->mgmt->cap_resp.desc_count;
+	struct mausb_dev_cap_desc  *desc;
+
+	offset = MAUSB_PKT_HEADER_SIZE + sizeof(struct mausb_CapResp_flds);
+	desc = cap_desc_ptr_increment(resp->mgmt, offset);
+
+	if (resp->common->pkt_type != CapResp) {
+		mamgmt_err("%s: packet is type %i (not a CapResp)\n", __func__,
+			resp->common->pkt_type);
+		return -EINVAL;
+	}
+
+	while (desc_count) {
+		if (desc == NULL) {
+			mamgmt_err("%s: no descriptor to read\n", __func__);
+			return ret;
+		}
+
+		ret++;
+
+		switch (desc->cap_type) {
+		case MAUSB_DEV_CAP_SPEED:
+			ma_dev->speed = kzalloc(
+				sizeof(struct mausb_dev_cap_speed),
+				GFP_ATOMIC);
+			ma_dev->speed->speed = desc->speed.speed;
+			ma_dev->speed->lse = desc->speed.lse;
+			ma_dev->speed->st = desc->speed.st;
+			ma_dev->speed->lane_count =
+				desc->speed.lane_count;
+			ma_dev->speed->link_protocol =
+				desc->speed.link_protocol;
+			ma_dev->speed->lsm = desc->speed.lsm;
+			break;
+		case MAUSB_DEV_CAP_POUT:
+			ma_dev->pout = kzalloc(
+				sizeof(struct mausb_dev_cap_pout),
+				GFP_ATOMIC);
+			ma_dev->pout->elastic_buf_cap =
+				desc->pout.elastic_buf_cap;
+			ma_dev->pout->drop_notif =
+				desc->pout.drop_notif;
+			break;
+		case MAUSB_DEV_CAP_ISO:
+			ma_dev->pout = kzalloc(
+				sizeof(struct mausb_dev_cap_iso),
+				GFP_ATOMIC);
+			ma_dev->iso->iso_alignment =
+				desc->iso.iso_alignment;
+			break;
+		case MAUSB_DEV_CAP_SYNC:
+			ma_dev->pout = kzalloc(
+				sizeof(struct mausb_dev_cap_sync),
+				GFP_ATOMIC);
+			ma_dev->sync->media_time_avail =
+				desc->sync.media_time_avail;
+			ma_dev->sync->timestamp_req =
+				desc->sync.timestamp_req;
+			break;
+		case MAUSB_DEV_CAP_CONT_ID:
+			ma_dev->pout = kzalloc(
+				sizeof(struct mausb_dev_cap_cont_id),
+				GFP_ATOMIC);
+			ma_dev->cont_id->cont_id_upper =
+				desc->cont_id.cont_id_upper;
+			ma_dev->cont_id->cont_id_lower =
+				desc->cont_id.cont_id_lower;
+			break;
+		case MAUSB_DEV_CAP_LINK_SLEEP:
+			ma_dev->link_sleep = kzalloc(
+				sizeof(struct mausb_dev_cap_link_sleep),
+				GFP_ATOMIC);
+			ma_dev->link_sleep->link_sleep_cap =
+				desc->link_sleep.link_sleep_cap;
+			break;
+		default:
+			mamgmt_err("%s: unknown device capability descriptor"
+				" type %i\n", __func__, desc->cap_type);
+			ret--;
+			break;
+		}
+
+		desc_count--;
+		offset += desc->length;
+		desc = cap_desc_ptr_increment(resp->mgmt, offset);
+	}
+
+	return ret;
+}
+
+/**
+ * Transmits a management packet of the specified type for the given device.
+ *
+ * @type:	The type of MA USB management request packet being sent.
+ * @mgmt:	The management data we need to send the packet.
+ * @dev:	The device this packet is associated with. Set to NULL if
+ *	        there is no associated device (ex: for MAUSBDevReset).
+ * @host:	Set TRUE to indicate we are a host, set to FALSE if we are
+ *              a device.
+ */
+int mausb_tx_dev_mgmt_req(enum mausb_pkt_type type, struct mausb_mgmt *mgmt,
+		struct mausb_dev *dev, bool host)
+{
+	return mausb_tx_dev_mgmt_req_ep(type, mgmt, dev, host, NULL);
+}
+EXPORT_SYMBOL(mausb_tx_dev_mgmt_req);
+
+/**
+ * Only necessary to call directly if you need to specify an EP, otherwise
+ * mausb_tx_dev_mgmt_req will work.
+ */
+int mausb_tx_dev_mgmt_req_ep(enum mausb_pkt_type type, struct mausb_mgmt *mgmt,
+		struct mausb_dev *dev, bool host, struct mausb_host_ep *ma_ep)
+{
+	int			ret = 0;
+	struct mausb_pkt	*req;
+	struct mausb_pkt	*resp = NULL;
+	struct ma_dev		*ma_dev = mausb_mgmt_to_ma_dev(mgmt);
+	struct mausb_dev	*hub;
+	unsigned long		irq_flags;
+
+	req = mausb_alloc_pkt(MAUSB_PKT_TYPE_MGMT, &ret, GFP_ATOMIC);
+	if (!req)
+		return -ENOMEM;
+
+	spin_lock_irqsave(mgmt->ma_dev_lock, irq_flags);
+
+	req->common->ver_flags  |= MAUSB_VERSION_1_0;
+	req->common->pkt_type    = type;
+	if (NULL != dev)
+		req->common->dev_handle  = cpu_to_le16(dev->dev_handle);
+
+	req->common->ma_dev_addr = ma_dev->ma_dev_addr;
+	req->common->mass_id     = ma_dev->mass_id;
+	req->common->pkt_status  = NO_ERROR;
+
+	if (host)
+		req->common->ver_flags  |= MAUSB_PKT_FLAG_HOST;
+
+	switch (type) {
+	case CapReq:
+		req->mgmt->cap_req.max_mgmt_reqs = MAUSB_MGMT_MAX_TOKEN;
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+
+		if (0 <= ret) {
+			ma_dev->ma_cap.num_ep = resp->mgmt->cap_resp.num_ep;
+			ma_dev->ma_cap.num_dev = resp->mgmt->cap_resp.num_dev;
+			ma_dev->ma_cap.num_stream =
+					resp->mgmt->cap_resp.num_stream;
+			ma_dev->ma_cap.dev_type = resp->mgmt->cap_resp.dev_type;
+			ma_dev->ma_cap.max_tx_reqs =
+					resp->mgmt->cap_resp.max_tx_reqs;
+			ma_dev->ma_cap.max_mgmt_reqs =
+					resp->mgmt->cap_resp.max_mgmt_reqs;
+
+			/* look for descriptors */
+			if (resp->mgmt->cap_resp.desc_count > 0) {
+				ret = parse_cap_desc(resp, ma_dev);
+
+				if (ret != resp->mgmt->cap_resp.desc_count) {
+					mamgmt_err("%s: error reading"
+						" capability descriptors\n",
+						__func__);
+				}
+			}
+		}
+		break;
+
+	case EPHandleReq:
+		mausb_fill_ep_handle_req_flds(dev, req->mgmt, MAUSB_ADD);
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		if (0 <= ret) {
+			ret = mausb_handle_ep_resp(dev,
+				&req->mgmt->ep_handle_req,
+				&resp->mgmt->ep_handle_resp);
+		}
+		break;
+	case EPActivateReq:
+	case EPInactivateReq:
+		ret = mausb_fill_ep_inactivate_req_flds(dev, req->mgmt);
+		if (ret > 0)
+			ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		break;
+	case EPResetReq:
+	case EPClearTransferReq:
+		break;
+	case EPHandleDeleteReq:
+		mausb_fill_ep_handle_req_flds(dev, req->mgmt, MAUSB_DEL);
+
+		if (0 < le32_to_cpu(req->mgmt->ep_handle_delete.num_ep)) {
+			ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+
+			mausb_handle_ep_del_resp(dev,
+				&req->mgmt->ep_handle_delete,
+				&resp->mgmt->ep_handle_delete);
+		}
+		break;
+	case USBDevHandleReq:
+		req->mgmt->usb_dev_handle_req.dock_hub
+			= cpu_to_le16(mgmt->hub_dev_handle);
+		req->mgmt->usb_dev_handle_req.usb_rt_str
+			= cpu_to_le32(dev->dev->route);
+		req->mgmt->usb_dev_handle_req.speed =
+			cpu_to_le32(mausb_get_dev_speed(dev));
+		req->common->dev_handle = 0;
+
+		spin_unlock_irqrestore(mgmt->ma_dev_lock, irq_flags);
+		hub = mausb_find_dev(ma_dev, dev->dev->parent, NULL, NULL);
+		spin_lock_irqsave(mgmt->ma_dev_lock, irq_flags);
+
+		if (NULL != hub) {
+			/* This will work if the device is connected directly to
+			 * the dock, but not with a hub connected to the dock...
+			 * well... unless the hub in between is slower than high
+			 * speed. */
+			/* TODO: have the parent hub point to the proper hub for
+			 *       all conditions */
+			req->mgmt->usb_dev_handle_req.parent_hub =
+				hub->dev_handle;
+			req->mgmt->usb_dev_handle_req.parent_hub_port =
+				dev->dev->portnum;
+		} else {
+			/* parent_hub_port not set for SS */
+			mamgmt_dbg("%s: could not find parent hub at %p\n",
+				__func__, dev->dev->parent);
+		}
+
+		/* TODO: all the remaining fields */
+		req->mgmt->usb_dev_handle_req.mtt = 0;
+
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+
+		if (resp && (NULL != dev))
+			dev->dev_handle =
+				le16_to_cpu(resp->mgmt->usb_dev_handle_resp);
+		break;
+
+	case DevResetReq:
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		break;
+	case ModifyEP0Req:
+		if (NULL == ma_ep) {
+			mamgmt_err("%s: no ep0 for USB device at %p\n",
+				__func__, dev);
+		} else {
+			req->mgmt->modify_ep0_req.ep_handle = ma_ep->ep_handle;
+			req->mgmt->modify_ep0_req.max_pkt_size =
+				usb_endpoint_maxp(&ma_ep->ep->desc);
+		}
+
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		if (0 <= ret) {
+			if (SUCCESS == resp->common->pkt_status) {
+				if (resp->mgmt->modify_ep0_resp.ep_handle.handle) {
+					ma_ep->ep_handle = resp->mgmt->modify_ep0_resp.ep_handle;
+				}
+			} else
+				ret = -EIO;
+		}
+		break;
+	case SetUSBDevAddrReq:
+		/* TODO: verify timer value */
+		req->mgmt->set_dev_addr_req.resp_timeout =
+			cpu_to_le16(MAUSB_MGMT_RT_DELAY * (1000/HZ));
+
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+
+		if (0 <= ret) {
+			if (SUCCESS == resp->common->pkt_status) {
+				dev->dev_address =
+					le32_to_cpu(
+					resp->mgmt->set_dev_addr_resp.dev_addr);
+			} else
+				ret = -EIO;
+		}
+		break;
+	case UpdateDevReq:
+		break;
+	case USBDevDisconnectReq:
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		break;
+	case DevDisconnectReq:
+		req->common->dev_handle = 0;
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		break;
+	case SleepReq:         /* fallthrough */
+	case WakeReq:
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		/* not everyone supports power management yet */
+		/* if (SUCCESS != resp->common->pkt_status)
+			ret = -EBUSY; */
+		break;
+	case RemoteWakeReq:
+	case PingReq:
+	case DevInitDisconnectReq:
+	case SyncReq:
+		break;
+	case CancelTransferReq:
+		req->mgmt->cancel_transfer_req.ep_handle = ma_ep->ep_handle;
+		/* TODO: stream IDs (where applicable) */
+		req->mgmt->cancel_transfer_req.req_id =
+				ma_ep->state.active_request_id;
+		ret = internal_mausb_tx_mgmt_req(req, &resp, mgmt);
+		/* TODO: handle Responses */
+		break;
+	case USBDevResetReq:
+	case VendorSpecificReq:
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	if (resp != NULL)
+		mausb_free_pkt(resp);
+
+	spin_unlock_irqrestore(mgmt->ma_dev_lock, irq_flags);
+	mausb_free_pkt(req);
+
+	return ret;
+}
+EXPORT_SYMBOL(mausb_tx_dev_mgmt_req_ep);
+
+/**
+ * Parses an incoming management packet resp.
+ *
+ * This function parses an incoming response packet and connects it to
+ * its associated request.
+ *
+ * @resp:	A pointer to the MA USB management response packet received.
+ * @ma_mgmt:	The MA USB device's management structure.
+ */
+int mausb_rx_mgmt_resp(struct mausb_pkt *resp, struct mausb_mgmt *ma_mgmt)
+{
+	struct mausb_mgmt_req_list	*ma_mgmt_req;
+	unsigned long			irq_flags;
+
+	spin_lock_irqsave(ma_mgmt->ma_dev_lock, irq_flags);
+
+	list_for_each_entry(ma_mgmt_req, &ma_mgmt->req_list, req_list) {
+
+		/* if we found the request that triggered this response */
+		if (ma_mgmt_req->mgmt_req->mgmt->token == resp->mgmt->token) {
+			ma_mgmt_req->mgmt_resp = resp;
+
+			complete_all(&ma_mgmt_req->resp_rcvd);
+			spin_unlock_irqrestore(ma_mgmt->ma_dev_lock, irq_flags);
+			return 0;
+		}
+	}
+
+	/* response given was not requested */
+	spin_unlock_irqrestore(ma_mgmt->ma_dev_lock, irq_flags);
+
+	mausb_free_pkt(resp);
+
+	return -EINVAL;
+}
+
+/**
+ * This function parses an incoming request packet and generates the
+ * appropriate response packet.
+ *
+ * @resp:	A pointer to the MA USB management request packet received.
+ * @ma_mgmt:	The MA USB device's management structure.
+ */
+int mausb_rx_mgmt_req(struct mausb_pkt *req, struct mausb_mgmt *mgmt)
+{
+	int				ret;
+	struct mausb_pkt		*resp;
+	struct ma_dev			*ma_dev = mausb_mgmt_to_ma_dev(mgmt);
+	struct mausb_pkt_transfer	*transfer = &mgmt->tx_pair.to_ms;
+	unsigned long			irq_flags;
+
+	resp = mausb_alloc_pkt(MAUSB_PKT_TYPE_MGMT, &ret, GFP_ATOMIC);
+	if (!resp)
+		return -ENOMEM;
+
+	spin_lock_irqsave(mgmt->ma_dev_lock, irq_flags);
+
+	ret = mgmt->req_switch(req->mgmt, resp->mgmt, mgmt);
+
+	if (ret >= 0) {
+		/* set the values common to all response packets */
+		resp->common->ver_flags |= MAUSB_VERSION_1_0;
+		/* type set above */
+		resp->common->length      = cpu_to_le16(mausb_pkt_length(resp));
+		/* device handle set above */
+		resp->common->ma_dev_addr = ma_dev->ma_dev_addr;
+		resp->common->mass_id     = ma_dev->mass_id;
+		/* pkt_status set above */
+		resp->mgmt->token         = req->mgmt->token;
+
+		mausb_pkt_fill_ms_pkt(resp, GFP_ATOMIC);
+		mamgmt_dbg("%s: sending %s packet\n", __func__,
+			mausb_type_to_string(resp->common->pkt_type));
+
+		spin_unlock_irqrestore(mgmt->ma_dev_lock, irq_flags);
+		transfer->transfer_packet(&resp->pkt, transfer->context);
+		spin_lock_irqsave(mgmt->ma_dev_lock, irq_flags);
+
+	} else {
+		/* we don't know how to handle this packet */
+		mamgmt_dbg("%s: packet type %s not supported\n", __func__,
+			mausb_type_to_string(resp->common->pkt_type));
+		mausb_free_pkt(resp);
+	}
+
+	spin_unlock_irqrestore(mgmt->ma_dev_lock, irq_flags);
+	return ret;
+}
+
+/**
+ * This function parses an incoming management packet, determines
+ * if it is a request or response, and passes the packet to the
+ * proper handler.
+ *
+ * @mgmt_pkt:	A pointer to the incoming MA USB management packet.
+ * @ma_mgmt:	The MA USB device's management structure.
+ */
+int mausb_rx_mgmt(struct mausb_pkt *mgmt_pkt, struct mausb_mgmt *mgmt)
+{
+	mamgmt_dbg("%s: %s packet received\n", __func__,
+		mausb_type_to_string(mgmt_pkt->common->pkt_type));
+
+	if (mausb_pkt_resp(mgmt_pkt))
+		return mausb_rx_mgmt_resp(mgmt_pkt, mgmt);
+	else
+		return mausb_rx_mgmt_req(mgmt_pkt, mgmt);
+}
+EXPORT_SYMBOL(mausb_rx_mgmt);
+
+/**
+ * Handles sent callback for management packets.
+ *
+ * Request packets are held until a response is received and should not be
+ * freed until then. Response packets can be freed as soon as they are sent out.
+ */
+void mausb_mgmt_pkt_sent(struct ms_pkt *ms_pkt)
+{
+	struct mausb_pkt *pkt;
+
+	pkt = ms_pkt_to_mausb_pkt(ms_pkt);
+
+	if (mausb_pkt_resp(pkt))
+		mausb_free_pkt(pkt);
+}
+EXPORT_SYMBOL(mausb_mgmt_pkt_sent);
+
drivers/staging/mausb/drivers/mausb_mgmt.h new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
1.9.1
#19 lore raw

PATCH 8/10 added media agnostic (MA) USB data packet handling

In reply to Stephanie Wallick

The MA USB Specification outlines packet types and a protocol for bulk and interrupt transfers. This is where we implement that protocol. MA USB transfers are initiated by the host via a TransferRequest packet. The host then either sends data to the device via subsequent TransferRequest packets (OUT transfer) or receives data from the device via TransferResponse packets (IN transfer). Each data transfer is identified by a Request ID number that increments up for each new transfer. One URB maps to one MA USB transfer. A transfer can consist of one or more MA USB packets depending on total transfer size. Each MA USB packet is assigned a Sequence Number with sequence numbers incrementing up for each new packet. The host sends a TransferAck packet to acknowledge the end of a transfer or when requested to do so by the device.

---
 drivers/staging/mausb/drivers/mausb_tx-device.c |  847 ++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tx-host.c   | 1211 +++++++++++++++++++++++
 drivers/staging/mausb/drivers/mausb_tx.c        |  318 ++++++
 drivers/staging/mausb/drivers/mausb_tx.h        |  129 +++
 4 files changed, 2505 insertions(+)
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx-device.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx-host.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx.c
 create mode 100644 drivers/staging/mausb/drivers/mausb_tx.h
drivers/staging/mausb/drivers/mausb_tx-device.c new file +847 -0
new file mode 100644
index 0000000..a14df54
@@ -0,0 +1,847 @@
+/* Name:        mausb_tx_device.c
+ * Description: implements MA USB transfers on device side
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * Contact Information:
+ * Sean Stalley, [email protected]
+ * Stephanie Wallick, [email protected]
+ * 2111 NE 25th Avenue
+ * Hillsboro, Oregon 97124
+ *
+ * BSD LICENSE
+ *
+ * Copyright(c) 2014 Intel Corporation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+   * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in
+      the documentation and/or other materials provided with the
+      distribution.
+    * Neither the name of Intel Corporation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define DEBUG
+
+#include "mausb_hcd.h"
+#include "mausb_udc.h"
+#include "mausb_pkt.h"
+#include "mausb_tx.h"
+
+#include <linux/kthread.h>
+
+int device_transfer_timeout(void *data)
+{
+	struct mausb_host_ep		*ep = (struct mausb_host_ep *) data;
+	struct mausb_udc		*udc = mausb_host_ep_to_maudc(ep);
+
+	while (!kthread_should_stop()) {
+		/*
+		 * make sure actual transfer timeout (i.e. not thread
+		 * setup)
+		 */
+		if (ep->tx_timed_out) {
+			if (ep->state.retry_counter > 0) {
+				maudc_dbg(udc, "%s: device timed out,"
+				      "  resending packet %i\n",
+				       __func__, ep->state.seq_number);
+
+				device_OUT_send_txResp(
+					&ep->active_transfer->state,
+					&ep->state,
+					ep->state.active_request_id,
+					ep->state.seq_number,
+					SUCCESS,
+					false, false, false);
+
+				ep->state.retry_counter--;
+			} else {
+				/*
+				 * TODO: start ping protocol per 5.2.2, if
+				 * successful reset timer and retries. For now
+				 * we just treat as if failed.
+				 */
+				ep->active_transfer->state.transfer_error =
+					true;
+				return -ETIMEDOUT;
+			}
+
+			ep->tx_timed_out = false;
+		}
+
+		/* put thread to sleep to wait for next timeout event */
+		wait_event_interruptible(ep->host_ep_wq,
+			kthread_should_stop() || ep->tx_timed_out);
+	}
+
+	return 0;
+}
+
+
+/*-------------------------IN transfer ------------------------*/
+
+/**
+ * Device-side IN transfer initialization process.
+ *
+ * @ tx_state:	Transfer state to be initialized.
+ * @ ep_state:	Endpoint state to be initialized.
+ * @ tx_req:	TransferReq packet from host that intiated transfer.
+ *
+ * Called every time a transferReq packet is received. Initializes transfer
+ * management variables.
+ */
+void device_init_IN_transfer(struct mausb_transfer_state *tx_state,
+		struct mausb_ep_state *ep_state, struct mausb_pkt *tx_req)
+{
+	struct mausb_host_ep *ep = mausb_state_to_ep(ep_state);
+
+	tx_state->transfer_error = false;
+	tx_state->transfer_complete = false;
+	tx_state->rem_size = le32_to_cpu(tx_req->data->remaining_size_credit);
+
+	ep_state->keep_alive_timer = MAUSB_TRANSFER_KEEP_ALIVE;
+	ep_state->retry_counter = MAUSB_BULK_TRANSFER_RETRIES;
+	ep_state->earliest_unacked = le32_to_cpu(tx_req->data->seq_num);
+	ep_state->active_request_id = tx_req->data->req_id;
+	ep_state->request_id++;
+
+	ep->actual_length = 0;
+
+	/* free old buffer if not previously freed */
+	if (ep->buffer)
+		kfree(ep->buffer);
+
+	/* alocate new buffer for this transfer */
+	ep->buffer = kzalloc(tx_state->rem_size, GFP_ATOMIC);
+}
+
+/* Determines if packet request id or sequence number is outside of the
+ * acceptable range
+ *
+ * Returns true if the packet sequence number or request id is invalid,
+ * otherwise returns false.
+ */
+bool is_invalid_IN_request(struct mausb_ep_state *ep_state,
+		struct mausb_pkt *tx_req)
+{
+	u8			r = tx_req->data->req_id;
+	u32			SN = le32_to_cpu(tx_req->data->seq_num);
+	bool			invalid = false;
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(
+					mausb_state_to_ep(ep_state));
+
+	if ((ep_state->earliest_request_id - MAUSB_HALF_REQ_ID) <= r) {
+		if (r < ep_state->earliest_request_id) {
+			maudc_err(udc, "%s: too many outstanding requests\n",
+				__func__);
+			invalid = true;
+		}
+	}
+
+	if ((ep_state->earliest_unacked - MAUSB_HALF_SEQ_NUM) <= SN) {
+		if (SN < ep_state->earliest_unacked) {
+			maudc_err(udc, "%s: too many unacknowledged packets\n",
+				__func__);
+			invalid = true;
+		}
+	}
+
+	if (ep_state->seq_number < SN) {
+		if (SN < (ep_state->seq_number + MAUSB_HALF_SEQ_NUM)) {
+			maudc_err(udc, "%s: too many outstanding sequence"
+			       " numbers\n", __func__);
+			invalid = true;
+		}
+
+	}
+
+	return invalid;
+}
+
+/*
+ * transferReq reception process outlined in MA USB spec.
+ */
+int device_IN_rcvd_txReq(struct mausb_ep_state *ep_state,
+		struct mausb_transfer_state *tx_state,
+		struct mausb_pkt *tx_req)
+{
+	u8			r = tx_req->data->req_id;
+	u32			SN  = le32_to_cpu(tx_req->data->seq_num);
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+
+	if (is_invalid_IN_request(ep_state, tx_req)) {
+		mausb_drop_packet(ep, tx_req);
+		device_IN_send_null_txResp(ep, ep_state->request_id,
+					   INVALID_REQUEST);
+	}
+
+	ep_state->earliest_unacked = SN;
+
+	if (tx_state->eot_detected && !tx_state->transfer_error) {
+		if (tx_state->last_transfer_sn < SN)
+			tx_state->transfer_complete = true;
+		else
+		ep_state->earliest_request_id = r;
+	}
+
+	if (r == ep_state->active_request_id && ep_state->delayed) {
+		maudc_dbg(udc, "%s: transfer %i is delayed, sending txResp with"
+		      " status = TRANSFER_PENDING\n", __func__, r);
+		device_IN_send_null_txResp(ep, r, TRANSFER_PENDING);
+	}
+
+	if (r == ep_state->request_id)
+		device_init_IN_transfer(tx_state, ep_state, tx_req);
+
+	return 0;
+}
+
+/* transferAck packet reception process for device side.
+ *
+ * @tx_state:	Transfer state variables for associated transfer.
+ * @ep_state:	Endpoint state variables for associated transfer.
+ * @tx_ack:	TransferAck packet being received.
+ *
+ * Called when device side driver receives a transferAck packet. Caller should
+ * verify that the request_id in the transferAck packet is valid before
+ * calling this function.
+ */
+int device_IN_ack_rcvd(struct mausb_transfer_state *tx_state,
+		struct mausb_ep_state *ep_state, struct mausb_pkt *tx_ack)
+{
+	int			ret = 0;
+	u8			r = tx_ack->data->req_id;
+	u8			u = ep_state->active_request_id;
+	u32			SN = le32_to_cpu(tx_ack->data->seq_num);
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+	bool			tx_complete = (!tx_state->transfer_error)
+					&& (tx_state->last_transfer_sn < SN);
+	bool			tx_complete_with_error =
+					(tx_state->transfer_error) && (u == r);
+	unsigned long		irq_flags = 0;
+
+	if (is_invalid_IN_request(ep_state, tx_ack))
+		tx_ack->data->common.pkt_status = DROPPED_PACKET;
+
+	switch (tx_ack->data->common.pkt_status) {
+	case DROPPED_PACKET:
+		maudc_dbg(udc, "%s: status = DROPPED_PACKET\n", __func__);
+		mausb_drop_packet(ep, tx_ack);
+		break;
+	case TRANSFER_PENDING:
+		maudc_dbg(udc, "%s: status = TRANSFER_PENDING\n", __func__);
+		if (r != ep_state->active_request_id)
+			mausb_drop_packet(ep, tx_ack);
+		/* else can start power saving measures */
+		break;
+	case MISSING_SEQUENCE_NUMBER:
+		maudc_dbg(udc, "%s: status = MISSING_SEQUENCE_NUMBER\n",
+			__func__);
+		mausb_resend_multi_packets(ep, &ep->resp_list,
+			le32_to_cpu(tx_ack->data->seq_num), irq_flags);
+		break;
+	case SUCCESS:
+		maudc_dbg(udc, "%s: status = SUCCESS\n", __func__);
+		ep_state->earliest_unacked = SN + 1;
+
+		if (tx_state->eot_detected) {
+			if (tx_complete || tx_complete_with_error)
+				tx_state->transfer_complete = true;
+			else
+				ep_state->earliest_request_id = u;
+
+			mausb_cleanup_ma_packets(ep,
+				le32_to_cpu(tx_ack->data->seq_num),
+				tx_ack->data->req_id);
+			kfree(ep->buffer);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return ret;
+}
+
+/* Submit a transferResp packet to host with no payload (used for errors).
+ *
+ * Per spec, sequence number field in a null transferResp packet is always
+ * reserved and set to 0
+ */
+int device_IN_send_null_txResp(struct mausb_host_ep *ep, u8 req_id, int status)
+{
+	struct mausb_pkt		*tx_resp;
+	struct mausb_transfer_state	*tx_state = &ep->active_transfer->state;
+
+	tx_resp = mausb_create_dp(NULL, ep->active_transfer, MAUSB_IN,
+		MAUSB_DEV, GFP_ATOMIC);
+
+	if (!tx_resp)
+		return -ENOMEM;
+
+	list_add_tail(&tx_resp->pkt_list, &ep->resp_list);
+
+	mausb_fill_pkt_ep(tx_resp, ep);
+
+	tx_resp->data->common.pkt_type = TransferResp;
+	tx_resp->data->req_id = req_id;
+	tx_resp->data->seq_num = 0;
+	tx_resp->buffer_length = 0;
+	tx_resp->data->remaining_size_credit = cpu_to_le32(tx_state->rem_size);
+	tx_resp->data->common.pkt_status = status;
+	tx_resp->common->length = cpu_to_le32(mausb_pkt_length(tx_resp));
+
+	mausb_transfer_data_pkt(tx_resp, ep, GFP_ATOMIC);
+
+	return 0;
+}
+
+/**
+ * Submit a transferResp packet with payload to host (normal data transfer).
+ */
+int device_IN_send_txResp_with_payload(struct mausb_host_ep *ep)
+{
+	int				payload = 0;
+	int				max_buffer_length = 0;
+	int				ep_rem_size = ep->actual_length;
+	struct mausb_ep_state		*ep_state = &ep->state;
+	struct mausb_transfer_state	*tx_state = &ep->active_transfer->state;
+	struct mausb_pkt		*tx_resp;
+	struct mausb_udc		*udc = mausb_host_ep_to_maudc(ep);
+
+	while (ep_rem_size > 0 || ep_state->seq_number == 0)  {
+
+		tx_resp = mausb_create_dp(NULL, ep->active_transfer, MAUSB_IN,
+					  MAUSB_DEV, GFP_ATOMIC);
+
+		if (!tx_resp)
+			return -ENOMEM;
+
+		list_add_tail(&tx_resp->pkt_list, &ep->resp_list);
+
+		mausb_fill_pkt_ep(tx_resp, ep);
+
+		tx_resp->data->common.pkt_type = TransferResp;
+		tx_resp->data->req_id = ep_state->active_request_id;
+		tx_resp->data->seq_num = cpu_to_le32(ep_state->seq_number);
+		tx_resp->data->remaining_size_credit =
+			cpu_to_le32(tx_state->rem_size);
+
+		max_buffer_length =
+			MAUSB_MAX_PACKET_SIZE - sizeof(tx_resp->data);
+
+		if (max_buffer_length <= 0) {
+			maudc_err(udc, "%s: packet buffer error: length"
+			       " = %i\n", __func__, max_buffer_length);
+		} else if (tx_state->rem_size <= 0) {
+			maudc_err(udc, "%s: packet buffer error: rem_size"
+			       " = %i\n", __func__, tx_state->rem_size);
+			ep->buffer = NULL;
+		} else {
+			payload = min_t(int, max_buffer_length,
+					ep_rem_size);
+		}
+
+		tx_resp->buffer = ep->buffer;
+		tx_resp->buffer_length = payload;
+		tx_resp->common->length = cpu_to_le16(mausb_pkt_length(tx_resp));
+
+		/* note: setting ARQ flag for error is optional */
+		if (tx_state->transfer_error) {
+			maudc_err(udc, "%s: transfer error with %i bytes "
+			       "remaining\n", __func__, tx_state->rem_size);
+
+			tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_EOT;
+			tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_ARQ;
+			tx_resp->data->common.pkt_status = UNSUCCESSFUL;
+
+			/*
+			 * TODO: take corrective action - e.g. clear all
+			 * transfers and restart endpoint
+			 */
+		}
+
+		tx_state->rem_size -= payload;
+		ep_rem_size -= payload;
+		/* note: setting ARQ flag for completion is optional */
+		if (tx_state->rem_size == 0 || ep_rem_size == 0) {
+			tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_EOT;
+			tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_ARQ;
+			tx_resp->data->common.pkt_status = SUCCESS;
+
+			ep_state->active_request_id++;
+		}
+
+		maudc_dbg(udc, "%s: sending transferResp packet with sequence"
+			" number %i for tranfer %i with %i bytes\n", __func__,
+			le32_to_cpu(tx_resp->data->seq_num),
+			le32_to_cpu(tx_resp->data->req_id), payload);
+
+		mausb_transfer_data_pkt(tx_resp, ep, GFP_ATOMIC);
+
+		ep_state->seq_number++;
+
+	}
+
+	return 0;
+}
+
+/*------------------ protocol-managed OUT transfer --------------------*/
+
+bool should_drop_OUT_txReq(struct mausb_ep_state *ep_state,
+		struct mausb_transfer_state *tx_state, struct mausb_pkt *tx_req)
+{
+	u8			r = tx_req->data->req_id;
+	u32			SN = le32_to_cpu(tx_req->data->seq_num);
+	int			payload_size = tx_req->buffer_length;
+	int			status = SUCCESS;
+	bool			drop = false;
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+
+	if (SN > ep_state->seq_number) {
+		maudc_err(udc, "%s: sequence number %i is too large "
+			"(expected < %i)\n", __func__, SN,
+			ep_state->seq_number);
+		drop = true;
+		status = MISSING_SEQUENCE_NUMBER;
+	} else if (SN < ep_state->seq_number) {
+		/* per spec, don't have to send response for this case */
+		maudc_err(udc, "%s: sequence number %i is too small "
+			"(expected >= %i)\n", __func__, SN,
+			ep_state->seq_number);
+		drop = true;
+	} else if (r < ep_state->earliest_request_id) {
+		maudc_err(udc, "%s: request ID %i is too small "
+			"(expected > %i)\n", __func__, r,
+			ep_state->earliest_request_id);
+		drop = true;
+		status = INVALID_REQUEST;
+	} else if (r > ep_state->request_id) {
+		/* account for wraparound */
+		if (ep_state->request_id != 0 && r != MAUSB_MAX_REQ_ID) {
+			maudc_err(udc, "%s: request ID %i is too large "
+				"(expected < %i)\n", __func__, r,
+				ep_state->request_id);
+			drop = true;
+			status = INVALID_REQUEST;
+		}
+	} else if ((payload_size + ep_state->occupancy)
+			> ep_state->rx_buf_size) {
+		maudc_err(udc, "%s: payload (%i bytes) is too large for buffer"
+			" (%i bytes)\n", __func__, payload_size,
+			ep_state->occupancy);
+		drop = true;
+		status = TRANSFER_DATA_BUFFER_ERROR;
+	}
+
+	/*
+	 * send txResp to host with appropriate status if txReq is going to
+	 * get dropped
+	 */
+	if (status) {
+		device_OUT_send_txResp(tx_state, ep_state,
+			ep_state->request_id, ep_state->seq_number, status,
+			false, false, false);
+	}
+
+	return drop;
+}
+
+/**
+ * Handles receipt of a transferRequest packet.
+ *
+ * @tx_state: State variables for associated transfer.
+ * @ep_state: State variables for associated device endpoint.
+ * @tx_req:   TransferRequest packet being received.
+ */
+int device_OUT_txReq_rcvd(struct mausb_transfer_state *tx_state,
+		struct mausb_ep_state *ep_state, struct mausb_pkt *tx_req)
+{
+	u8			req_id = tx_req->data->req_id;
+	u32			SN = le32_to_cpu(tx_req->data->seq_num);
+	int			ret = 0;
+	int			payload_size = tx_req->buffer_length;
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+
+	maudc_dbg(udc, "%s: received transferReq packet %i for transfer %i\n",
+		__func__, SN, req_id);
+
+	if (should_drop_OUT_txReq(ep_state, tx_state, tx_req)) {
+		maudc_err(udc, "%s: dropping packet %i\n", __func__, SN);
+		mausb_drop_packet(ep, tx_req);
+		return 0;
+	}
+
+	/* start new transfer */
+	if (req_id == (ep_state->request_id)) {
+		tx_state->transfer_error = false;
+		tx_state->transfer_complete = false;
+		tx_state->eot_detected = false;
+		tx_state->rem_size =
+			le32_to_cpu(tx_req->data->remaining_size_credit);
+		tx_state->payload_size = 0;
+		ep->active_transfer->transfer_size = tx_state->rem_size;
+		ep_state->active_request_id = req_id;
+		INIT_LIST_HEAD(&ep->req_list);
+		INIT_LIST_HEAD(&ep->resp_list);
+
+		ep->actual_length = 0;
+
+		ep_state->request_id =
+			mausb_req_id_add(ep_state->request_id, 1);
+	}
+
+	ep->buffer = tx_req->buffer;
+	ep_state->occupancy += payload_size;
+	ret = device_OUT_deliver_payload(tx_req, ep_state, tx_state);
+	ep_state->occupancy -= payload_size;
+
+	ep_state->seq_number++;
+
+	return 0;
+}
+
+/**
+ * Transmits a transferReq or transferAck packet.
+ */
+int device_OUT_send_txResp(struct mausb_transfer_state *tx_state,
+		struct mausb_ep_state *ep_state, u32 req_id, u32 seq_num,
+		int status, bool retry, bool eot, bool arq)
+{
+	struct mausb_host_ep *ep = mausb_state_to_ep(ep_state);
+	struct mausb_pkt *tx_resp = mausb_create_dp(NULL, ep->active_transfer,
+				     MAUSB_OUT, MAUSB_DEV, GFP_ATOMIC);
+	struct mausb_udc *udc = mausb_host_ep_to_maudc(ep);
+
+	if (!tx_resp)
+		return -ENOMEM;
+
+	mausb_fill_pkt_ep(tx_resp, ep);
+
+	tx_resp->data->common.pkt_type = TransferResp;
+	tx_resp->data->req_id = req_id;
+	tx_resp->data->seq_num = cpu_to_le32(seq_num);
+	tx_resp->data->common.pkt_status = status;
+	tx_resp->data->remaining_size_credit =
+		cpu_to_le32(ep_state->rx_buf_size - ep_state->occupancy);
+
+	list_add_tail(&tx_resp->pkt_list, &ep->resp_list);
+
+
+	tx_resp->common->length = cpu_to_le16(mausb_pkt_length(tx_resp));
+
+	if (retry)
+		tx_resp->common->ver_flags |= MAUSB_PKT_FLAG_RETRY;
+
+	if (eot)
+		tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_EOT;
+
+	if (arq)
+		tx_resp->data->eps_tflags |= MAUSB_PKT_TFLAG_ARQ;
+
+	maudc_dbg(udc, "%s: sending transferResp packet %i for transfer %i\n",
+		__func__, le32_to_cpu(tx_resp->data->seq_num),
+		tx_resp->data->req_id);
+
+	/* send transferResp packet to host */
+	mausb_transfer_data_pkt(tx_resp, ep, GFP_ATOMIC);
+
+	return 0;
+}
+
+/**
+ * Handles receipt of a transferAck packet.
+ *
+ * Caller should ensure that transfer complete and error flags are not set.
+ * Note that what to do isn't defined in spec - we are on our own for this one!
+ */
+void device_OUT_ack_rcvd(struct mausb_transfer_state *tx_state,
+		struct mausb_ep_state *ep_state,
+		struct mausb_pkt *tx_ack)
+{
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+
+	maudc_dbg(udc, "%s: received transferAck packet for transfer %i\n",
+		__func__, tx_ack->data->req_id);
+
+	ep_state->occupancy = 0;
+	mausb_cleanup_ma_packets(ep, le32_to_cpu(tx_ack->data->seq_num),
+		tx_ack->data->req_id);
+}
+
+/*
+ * Copy payload from transferRequest into device buffer.
+ *
+ * @tx_req:   TransferRequest with payload to be delivered.
+ * @ep_state: State variables for associated device endpoint.
+ * @tx_state: State variables for associated transfer.
+ *
+ * Caller should ensure that transfer complete and error flags are not set.
+ * This function also includes payload delivery process outlined in spec.
+ */
+int device_OUT_deliver_payload(struct mausb_pkt *tx_req,
+		struct mausb_ep_state *ep_state,
+		struct mausb_transfer_state *tx_state)
+{
+	u32			req_sn = le32_to_cpu(tx_req->data->seq_num);
+	int			payload_size;
+	struct mausb_host_ep	*ep = mausb_state_to_ep(ep_state);
+	struct mausb_udc	*udc = mausb_host_ep_to_maudc(ep);
+
+	/* payload delivery process */
+	payload_size = do_ma_transfer(ep, tx_req, MAUSB_OUT);
+
+	/* payload delivery confirmation process */
+	if (payload_size < 0) {
+		maudc_err(udc, "%s: transfer error %i\n", __func__,
+			payload_size);
+
+		device_OUT_send_txResp(tx_state, ep_state, tx_req->data->req_id,
+			le32_to_cpu(tx_req->data->seq_num), UNSUCCESSFUL,
+				       false, true, false);
+	}
+
+	else {
+		ep_state->earliest_unacked = req_sn + 1;
+		tx_state->payload_size = payload_size - tx_state->payload_size;
+		tx_state->rem_size =
+			le32_to_cpu(tx_req->data->remaining_size_credit);
+		tx_state->payload_size = payload_size;
+
+		maudc_dbg(udc, "%s: delivered %i bytes with %i remaining\n",
+		       __func__, payload_size, tx_state->rem_size);
+
+		/* control response packet */
+		if (tx_req->setup) {
+			device_OUT_send_txResp(tx_state, ep_state,
+				tx_req->data->req_id,
+				le32_to_cpu(tx_req->data->seq_num),
+				SUCCESS, false, true, true);
+
+		/* end of transfer response packet */
+		} else if (tx_state->rem_size == 0) {
+			tx_state->last_transfer_sn = req_sn;
+
+			device_OUT_send_txResp(tx_state, ep_state,
+				tx_req->data->req_id,
+				le32_to_cpu(tx_req->data->seq_num),
+				SUCCESS, false, true, true);
+		} else if (tx_req->data->eps_tflags & MAUSB_PKT_TFLAG_ARQ) {
+			device_OUT_send_txResp(tx_state, ep_state,
+				tx_req->data->req_id,
+				le32_to_cpu(tx_req->data->seq_num),
+				SUCCESS, false, false, true);
+		}
+	}
+
+	return 0;
+}
+
+/*------------------ transfer interfaces ----------------------------*/
+
+/**
+ * Called to receive both transferRequest and transferAck packets.
+ * For transferRequests, a new transfer is initiated - transfer state values
+ * are initialized and a request is send to the device to get data. For
+ * transferAcks, a transfer is ended (either successfully or with error,
+ * depending on transferAck packet status).
+ */
+int receive_ma_packet_IN(struct ms_pkt *ms_pkt, void *context)
+{
+	int				ret = 0;
+	struct mausb_host_ep		*ep;
+	struct mausb_pkt		*pkt;
+	struct mausb_udc		*udc;
+
+	if ((NULL == context) || (NULL == ms_pkt)) {
+		printk(KERN_ERR "%s: received NULL %s\n", __func__,
+			context ? "packet" : "context");
+		return -EFAULT;
+	}
+
+	ep = (struct mausb_host_ep *) context;
+	del_timer(&ep->timer);
+
+	pkt = mausb_pkt_from_ms_pkt_ep(ms_pkt, ep, GFP_ATOMIC);
+	list_add_tail(&pkt->pkt_list, &ep->req_list);
+
+	udc = mausb_host_ep_to_maudc(ep);
+
+	if (mausb_is_a_retry(ep, &ep->resp_list, pkt))
+		return 0;
+
+	switch (pkt->data->common.pkt_type) {
+	case TransferReq:
+		maudc_dbg(udc, "%s: received transferReq %i for transfer %i\n",
+			__func__, le32_to_cpu(pkt->data->seq_num),
+			pkt->data->req_id);
+		device_IN_rcvd_txReq(&ep->state, &ep->active_transfer->state,
+			pkt);
+		ret = do_ma_transfer(ep, pkt, MAUSB_IN);
+		ret = device_IN_send_txResp_with_payload(ep);
+		break;
+	case TransferAck:
+		maudc_dbg(udc, "%s: received transferAck for transfer %i\n",
+			__func__, pkt->data->req_id);
+		ret = device_IN_ack_rcvd(&ep->active_transfer->state,
+			&ep->state, pkt);
+		break;
+	default:
+		maudc_err(udc, "%s: invalid packet type/subtype %i\n",
+			__func__, pkt->data->common.pkt_type);
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+/**
+ * Receives both transferReq and transferAck packets for OUT transfers. When a
+ * packet is received, it is put on a list for handling. If the endpoint is not
+ * already handling a packet, it starts iterating through the list of received
+ * packets.
+ */
+int receive_ma_packet_OUT(struct ms_pkt *ms_pkt, void *context)
+{
+	int				ret = 0;
+	struct mausb_host_ep		*ep;
+	struct mausb_pkt		*pkt;
+	struct mausb_udc		*udc;
+
+	if ((NULL == context) || (NULL == ms_pkt)) {
+		printk(KERN_ERR "%s: received NULL %s\n", __func__,
+			context ? "packet" : "context");
+		return -EFAULT;
+	}
+
+	ep = (struct mausb_host_ep *) context;
+	del_timer(&ep->timer);
+
+	pkt = mausb_pkt_from_ms_pkt_ep(ms_pkt, ep, GFP_ATOMIC);
+	list_add_tail(&pkt->pkt_list, &ep->req_list);
+
+	udc = mausb_host_ep_to_maudc(ep);
+
+	if (mausb_is_a_retry(ep, &ep->resp_list, pkt))
+		return 0;
+
+	/* determine if packet is a transferRequest */
+	if (pkt->data->common.pkt_type == TransferReq) {
+		maudc_dbg(udc, "%s: received transferReq %i for transfer %i\n",
+			__func__, le32_to_cpu(pkt->data->seq_num),
+			pkt->data->req_id);
+		ret = device_OUT_txReq_rcvd(&ep->active_transfer->state,
+			&ep->state, pkt);
+
+	/* if not a transferReq, it should be a transferAck */
+	} else if (pkt->data->common.pkt_type == TransferAck) {
+		maudc_dbg(udc, "%s: received transferAck for transfer %i\n",
+			__func__, pkt->data->req_id);
+		device_OUT_ack_rcvd(&ep->active_transfer->state, &ep->state,
+			pkt);
+	} else {
+		/* TODO: deal with invalid packet type */
+	}
+
+	return ret;
+}
+
+/**
+ * Called when a transferReq is received on a control endpoint.
+ *
+ * @ms_pkt:	Packet being received.
+ * @context:	Device endpoint associated with transfer (this should always
+ *		be ep0).
+ */
+int receive_ma_packet_control(struct ms_pkt *ms_pkt, void *context)
+{
+	int				ret = 0;
+	struct mausb_host_ep		*ep;
+	struct mausb_pkt		*pkt;
+	struct mausb_udc		*udc;
+
+	if ((NULL == context) || (NULL == ms_pkt)) {
+		printk(KERN_ERR "%s: received NULL %s\n", __func__,
+			context ? "packet" : "context");
+		return -EFAULT;
+	}
+
+	ep = (struct mausb_host_ep *) context;
+	del_timer(&ep->timer);
+
+	pkt = mausb_pkt_from_ms_pkt_ep(ms_pkt, ep, GFP_ATOMIC);
+	udc = mausb_host_ep_to_maudc(ep);
+
+	/*
+	 * A new control transfer begins with a transfer request carrying setup
+	 * data and sequence number 0.
+	 */
+	if (pkt->setup) {
+		ep->state.seq_number = 0;
+		ep->actual_length = 0;
+		ep->active_transfer->state.rem_size =
+			le32_to_cpu(pkt->data->remaining_size_credit);
+
+		/*
+		 * Determine if this is a control IN or OUT - if there is data
+		 * in the packet, or the host is requesting no data, we are
+		 * an OUT.
+		 */
+		if (0 != pkt->buffer_length ||
+		    0 == le32_to_cpu(pkt->data->remaining_size_credit)) {
+
+			ep->control_dir = MAUSB_OUT;
+
+		} else
+			ep->control_dir = MAUSB_IN;
+	}
+
+	kfree(pkt);
+	pkt = NULL;
+
+	if (MAUSB_IN == ep->control_dir)
+		ret = receive_ma_packet_IN(ms_pkt, context);
+	else
+		ret = receive_ma_packet_OUT(ms_pkt, context);
+
+	return ret;
+}
+
drivers/staging/mausb/drivers/mausb_tx-host.c new file +1211 -0

1,211 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tx.c new file +318 -0

318 lines not on this page · raw message

drivers/staging/mausb/drivers/mausb_tx.h new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
1.9.1
#20 lore raw

PATCH 9/10 added tools for building/loading media agnostic (MA) USB drivers

In reply to Stephanie Wallick

Adds various scripts for building, loading and unloading the MA USB drivers and a utility that can be used for connecting and disconnecting the host and device drivers.

---
 .../staging/mausb/scripts/build_load_connect.sh    |  69 +++++++
 drivers/staging/mausb/scripts/load_gzero.sh        |   5 +
 drivers/staging/mausb/scripts/load_script.sh       | 125 +++++++++++++
 drivers/staging/mausb/scripts/modprobify.sh        |  10 ++
 drivers/staging/mausb/scripts/unload_all.sh        |  15 ++
 drivers/staging/mausb/scripts/unload_gzero.sh      |   5 +
 drivers/staging/mausb/tools/mausb-util/Makefile    |  14 ++
 drivers/staging/mausb/tools/mausb-util/README      |  30 ++++
 drivers/staging/mausb/tools/mausb-util/config.mk   |  17 ++
 .../staging/mausb/tools/mausb-util/src/Android.mk  |  13 ++
 .../staging/mausb/tools/mausb-util/src/Makefile    |  18 ++
 .../staging/mausb/tools/mausb-util/src/connect.c   |  72 ++++++++
 .../staging/mausb/tools/mausb-util/src/connect.h   |  22 +++
 drivers/staging/mausb/tools/mausb-util/src/mausb.c | 200 +++++++++++++++++++++
 drivers/staging/mausb/tools/mausb-util/src/mausb.h |  64 +++++++
 .../mausb/tools/mausb-util/src/mausb_ioctl.h       |  37 ++++
 drivers/staging/mausb/tools/mausb-util/src/utils.c |  94 ++++++++++
 17 files changed, 810 insertions(+)
 create mode 100644 drivers/staging/mausb/scripts/build_load_connect.sh
 create mode 100644 drivers/staging/mausb/scripts/load_gzero.sh
 create mode 100644 drivers/staging/mausb/scripts/load_script.sh
 create mode 100644 drivers/staging/mausb/scripts/modprobify.sh
 create mode 100644 drivers/staging/mausb/scripts/unload_all.sh
 create mode 100644 drivers/staging/mausb/scripts/unload_gzero.sh
 create mode 100644 drivers/staging/mausb/tools/mausb-util/Makefile
 create mode 100644 drivers/staging/mausb/tools/mausb-util/README
 create mode 100644 drivers/staging/mausb/tools/mausb-util/config.mk
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/Android.mk
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/Makefile
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/connect.c
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/connect.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb.c
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/mausb_ioctl.h
 create mode 100644 drivers/staging/mausb/tools/mausb-util/src/utils.c
drivers/staging/mausb/scripts/build_load_connect.sh new file +69 -0
new file mode 100644
index 0000000..d055241
@@ -0,0 +1,69 @@
+#!/bin/bash
+
+side="invalid"
+mode="invalid"
+addr="invalid"
+
+while getopts "hdbtsa:" opt; do
+	case "$opt" in
+	h) side="-h"
+	   ;;
+	d) side="-d"
+	   ;;
+	b) side="-b"
+	   ;;
+	t) mode="-t"
+	   ;;
+	s) mode="-s"
+	   ;;
+	a) addr=$OPTARG
+	   ;;
+	esac
+done
+
+
+if [ "$side" == "invalid" ]
+then
+	echo $side
+	echo "please choose host (-h) or device (-d)"
+	exit
+fi
+
+if [ "$mode" == "invalid" ]
+then
+	echo $mode
+	echo "please choose tcp (-t) or snap (-s)"
+	exit
+fi
+
+if [ "$addr" == "invalid" ]
+then
+	echo $addr
+	echo "please enter a valid address (-a 1.2.3.4, -a 01:23:45:67:89:0a)"
+	exit
+fi
+
+
+cd ../
+# make clean && make
+make -j8
+cd drivers/
+
+wireshark -k -Y tcp -i eth0 &
+
+sudo ../scripts/load_script.sh $side $mode
+
+sudo cat /proc/modules | grep ma
+
+if [ "$mode" == "-s" ]
+then
+	echo "connecting mausb"
+	../tools/mausb-util/mausb -c -m llc -a $addr
+fi
+
+if [ "$mode" == "-t" ]
+then
+	echo "connecting mausb"
+	../tools/mausb-util/mausb -c -m ip -p 9001 -a $addr
+fi
+
drivers/staging/mausb/scripts/load_gzero.sh new file +5 -0
new file mode 100644
index 0000000..9266199
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+modprobe g_zero
+
+echo g_zero load process complete
\ No newline at end of file
drivers/staging/mausb/scripts/load_script.sh new file +125 -0

125 lines not on this page · raw message

drivers/staging/mausb/scripts/modprobify.sh new file +10 -0
new file mode 100644
index 0000000..b5f16ab
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+
+# note: this scripted needs to be run as a superuser to work
+
+# copy the LKM into the module library
+cp ./*.ko /lib/modules/$(uname -r)/kernel/drivers/usb/
+
+# depmod so the kernel can figure out its dependacies
+depmod -A
drivers/staging/mausb/scripts/unload_all.sh new file +15 -0
new file mode 100644
index 0000000..26af664
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+modprobe -r g_zero
+
+echo g_zero unload process complete
+
+./../tools/mausb-util/mausb -d
+
+echo mausb disconnect complete
+
+rmmod matcp_dev
+rmmod matcp_host
+rmmod matcp_core
+rmmod maudc
+rmmod mausb
drivers/staging/mausb/scripts/unload_gzero.sh new file +5 -0
new file mode 100644
index 0000000..e506b2d
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+modprobe -r g_zero
+
+echo g_zero unload process complete
\ No newline at end of file
drivers/staging/mausb/tools/mausb-util/Makefile new file +14 -0
new file mode 100644
index 0000000..abd5fe0
@@ -0,0 +1,14 @@
+include config.mk
+
+.PHONY: $(BINARY_NAME) all clean
+
+all: $(BINARY_NAME)
+
+$(BINARY_NAME):
+	$(MAKE) -C src/
+	cp -rf src/$(BINARY_NAME) .
+
+
+clean:
+	$(MAKE) -C src/ clean
+	rm -rf $(BINARY_NAME) *~
drivers/staging/mausb/tools/mausb-util/README new file +30 -0
new file mode 100644
index 0000000..967f00e
@@ -0,0 +1,30 @@
+#    #   ##   #    #  ####  #####     #    # ##### # #      # ##### #   #
+##  ##  #  #  #    # #      #    #    #    #   #   # #      #   #    # #
+# ## # #    # #    #  ####  #####     #    #   #   # #      #   #     #
+#    # ###### #    #      # #    #    #    #   #   # #      #   #     #
+#    # #    # #    # #    # #    #    #    #   #   # #      #   #     #
+#    # #    #  ####   ####  #####      ####    #   # ###### #   #     #
+
+
+Author: Aymen Zayet
+License: GPL2
+Version: 0.1
+
+1. Description
+--------------
+This tool includes many commands that help to test / debug the MAUSB devices.
+mausb bin provides to the host a way to connect and disconnect from the MAUSB device.
+
+2. Usage
+--------
+on the device side , run :
+>> mausb --mode ip --connect --addr <<HOST_IP_ADDR>>
+
+from the host side, run :
+>> mausb --mode ip --connect --addr 0.0.0.0
+
+3. Documentation
+----------------
+
+4. Miscellaneous
+----------------
drivers/staging/mausb/tools/mausb-util/config.mk new file +17 -0
new file mode 100644
index 0000000..ea5f1de
@@ -0,0 +1,17 @@
+SRCDIR          = $(shell pwd)
+DESTDIR         = /usr/local/bin
+
+CC              = gcc
+CFLAGS          = -g -ggdb -D __DEBUG
+LDFLAGS         =
+SHELL           = /bin/sh
+#CFLAGS         = -O2 -fomit-frame-pointer
+
+PROJECT_NAME    = mausb
+PROJECT_VERSION = 0.1
+
+BINARY_NAME     = $(PROJECT_NAME)
+
+SOURCE_FILES    = utils.c connect.c mausb.c
+
+OBJECT_FILES    = $(SOURCE_FILES:.c=.o)
drivers/staging/mausb/tools/mausb-util/src/Android.mk new file +13 -0
new file mode 100644
index 0000000..881c5fc
@@ -0,0 +1,13 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+LOCAL_CFLAGS += -Wall -g
+LOCAL_LDFLAGS +=
+LOCAL_LDLIBS += -lpthread -lm -ldl
+LOCAL_SRC_FILES:= \
+	mausb.c \
+	utils.c \
+	connect.c
+LOCAL_C_INCLUDES += linux/modules/ma-usb/drivers
+LOCAL_MODULE := mausb
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
drivers/staging/mausb/tools/mausb-util/src/Makefile new file +18 -0
new file mode 100644
index 0000000..deb98a6
@@ -0,0 +1,18 @@
+include ../config.mk
+
+all: $(BINARY_NAME)
+
+$(BINARY_NAME): $(OBJECT_FILES)
+	@echo -n "(LD)" $@ " "
+	@$(CC) $(LDFLAGS) $(CFLAGS) $^ -o $@
+	@echo OK
+
+%.o: %.c
+	@echo -n "(CC)" $@ " "
+	@$(CC) $(CFLAGS) -c $< -o $@
+	@echo OK
+
+clean:
+	rm -rf *.[oas] .*.flags *.ko .*.cmd .*.d .*.tmp *.mod.c .tmp_versions Module*.symv\
+ers
+	rm -f $(BINARY_NAME) *~
\ No newline at end of file
drivers/staging/mausb/tools/mausb-util/src/connect.c new file +72 -0

72 lines not on this page · raw message

drivers/staging/mausb/tools/mausb-util/src/connect.h new file +22 -0
new file mode 100644
index 0000000..30bd1c5
@@ -0,0 +1,22 @@
+/*
+  (c) Aymen Zayet ([email protected])
+
+  This file is part of mausb utility.
+
+  mausb is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  mausb is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with mausb. If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef CONNECT_H_
+#define CONNECT_H_
+
+#endif /* CONNECT_H_ */
drivers/staging/mausb/tools/mausb-util/src/mausb.c new file +200 -0

200 lines not on this page · raw message

drivers/staging/mausb/tools/mausb-util/src/mausb.h new file +64 -0

64 lines not on this page · raw message

drivers/staging/mausb/tools/mausb-util/src/mausb_ioctl.h new file +37 -0
new file mode 100644
index 0000000..1cf8d50
@@ -0,0 +1,37 @@
+#ifndef MAUSB_IOCTL_H
+#define MAUSB_IOCTL_H
+
+#define BUFFER 80
+#define DRIVER_VERSION "Alpha 0.0.25"
+#define MAJOR_NUM 100
+
+/* These define the ioctl functions that can be used */
+#define IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *)
+
+#define IOCTL_GET_MSG _IOR(MAJOR_NUM, 1, char *)
+
+#define IOCTL_GET_VRSN _IOR(MAJOR_NUM, 2, char *)
+
+#define IOCTL_GET_NAME _IOR(MAJOR_NUM, 3, char *)
+
+#define IOCTL_GADGET_C _IOR(MAJOR_NUM, 4, char *)
+
+#define IOCTL_GADGET_D _IOR(MAJOR_NUM, 5, char *)
+
+#define IOCTL_MED_DELAY _IOR(MAJOR_NUM, 6, char *)
+
+#define IOCTL_SET_IP _IOR(MAJOR_NUM, 7, char *)
+
+#define IOCTL_SET_PORT _IOR(MAJOR_NUM, 8, char *)
+
+#define IOCTL_SET_IP_DECIMAL _IOR(MAJOR_NUM, 9, char *)
+
+#define IOCTL_SET_MAC _IOR(MAJOR_NUM, 10, char *)
+
+/* This is the location where the device file will be created. It is used to
+ * read/write to in order to communicate to and from the device */
+#define DEVICE_FILE_NAME "/dev/mausb"
+
+#define ETH_ALEN 6
+
+#endif
drivers/staging/mausb/tools/mausb-util/src/utils.c new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
1.9.1
#21 lore raw

PATCH 10/10 added kernel build, configuration, and TODO files

In reply to Stephanie Wallick
---
 drivers/staging/Kconfig                |  2 ++
 drivers/staging/Makefile               |  1 +
 drivers/staging/mausb/Kconfig          | 16 ++++++++++++++++
 drivers/staging/mausb/Makefile         |  3 +++
 drivers/staging/mausb/TODO             | 14 ++++++++++++++
 drivers/staging/mausb/drivers/Kconfig  | 34 ++++++++++++++++++++++++++++++++++
 drivers/staging/mausb/drivers/Makefile | 20 ++++++++++++++++++++
 7 files changed, 90 insertions(+)
 create mode 100644 drivers/staging/mausb/Kconfig
 create mode 100644 drivers/staging/mausb/Makefile
 create mode 100644 drivers/staging/mausb/TODO
 create mode 100644 drivers/staging/mausb/drivers/Kconfig
 create mode 100644 drivers/staging/mausb/drivers/Makefile
drivers/staging/Kconfig +2 -0
index 35b494f..f57621b 100644
@@ -24,6 +24,8 @@ menuconfig STAGING
 
 if STAGING
 
+source "drivers/staging/mausb/Kconfig"
+
 source "drivers/staging/et131x/Kconfig"
 
 source "drivers/staging/slicoss/Kconfig"
drivers/staging/Makefile +1 -0
index e66a5db..7615c85 100644
@@ -51,3 +51,4 @@ obj-$(CONFIG_GS_FPGABOOT)	+= gs_fpgaboot/
 obj-$(CONFIG_BT_NOKIA_H4P)	+= nokia_h4p/
 obj-$(CONFIG_CRYPTO_SKEIN)	+= skein/
 obj-$(CONFIG_UNISYSSPAR)	+= unisys/
+obj-$(CONFIG_MAUSB)		+= mausb/
drivers/staging/mausb/Kconfig new file +16 -0
new file mode 100644
index 0000000..095c08b
@@ -0,0 +1,16 @@
+
+menuconfig MAUSB
+	bool "MA USB drivers"
+	depends on USB
+	---help---
+	  This option allows you to select from the various MA USB
+	  drivers. Note that a media-specific driver (e.g. tcp or SNAP)
+	  is needed in addition to the media agnostic host or device driver.
+
+
+if MAUSB
+
+source "drivers/staging/mausb/drivers/Kconfig"
+
+endif
+
drivers/staging/mausb/Makefile new file +3 -0
new file mode 100644
index 0000000..b559322
@@ -0,0 +1,3 @@
+#obj-$(CONFIG_MAUSB) += drivers/
+obj-y += drivers/
+
drivers/staging/mausb/TODO new file +14 -0
new file mode 100644
index 0000000..dd124ed
@@ -0,0 +1,14 @@
+TODO:
+	- checkpatch.pl cleanups
+	- address miscellaneous "TODO" statements in code
+	- add support for multiple media agnostic (MA) devices
+	- add/improve support for unimplemented packet types
+	- handle errors and failure with more grace and less crash
+
+Please send patches to Greg Kroah-Hartman <[email protected]> and cc:
+
+	Sean Stalley <[email protected]>
+	Stephanie Wallick <[email protected]>
+
+
+
drivers/staging/mausb/drivers/Kconfig new file +34 -0
new file mode 100644
index 0000000..9e12e22
@@ -0,0 +1,34 @@
+config MA_CORE
+	tristate "MA USB core"
+	---help---
+	  This builds ma_core module.
+
+config MAUSB_HOST
+	tristate "MA USB host"
+	depends on MA_CORE
+	---help---
+	  This builds MA USB host driver module.
+
+config MAUSB_DEVICE
+	tristate "MA USB device"
+	depends on MA_CORE && USB_GADGET
+	---help---
+	  This builds MA USB device driver module.
+
+config MATCP_CORE
+	tristate "MA USB tcp core"
+	---help---
+	  This builds tcp_core module.
+
+config MATCP_HOST
+	tristate "MA USB host tcp"
+	depends on MATCP_CORE
+	---help---
+	  This builds tcp_host module.
+
+config MATCP_DEVICE
+	tristate "MA USB device tcp"
+	depends on MATCP_CORE
+	---help---
+	  This builds tcp_dev module.
+
drivers/staging/mausb/drivers/Makefile new file +20 -0
new file mode 100644
index 0000000..a38491e
@@ -0,0 +1,20 @@
+#MY_CFLAGS =-g -O0 -DEBUG_OUTPUT
+
+obj-$(CONFIG_MA_CORE) += ma_core.o
+ma_core-y := mausb_pkt.o mausb_tx.o mausb_msapi.o mausb_mem.o mausb_mgmt.o
+
+obj-$(CONFIG_MAUSB_HOST) += mausb.o
+mausb-y := mausb_hcd.o mausb_hub.o mausb_tx-host.o mausb_mem-host.o
+
+obj-$(CONFIG_MAUSB_DEVICE) += maudc.o
+maudc-y := mausb_udc.o mausb_tx-device.o
+
+obj-$(CONFIG_MATCP_HOST) += matcp_host.o
+matcp_host-y := mausb_tcp-host.o
+
+obj-$(CONFIG_MATCP_DEVICE) += matcp_dev.o
+matcp_dev-y := mausb_tcp-device.o
+
+obj-$(CONFIG_MATCP_CORE) += matcp_core.o
+matcp_core-y := mausb_tcp.o mausb_ioctl.o
+
Signature · 1 line
1.9.1
maintainer
#22 lore raw

Re: [PATCH 10/10] added kernel build, configuration, and TODO files

drivers/staging/mausb/drivers/Makefile line 1

Quoted 4 lines · Stephanie Wallick
drivers/staging/mausb/drivers/Makefile new file +1 -0
@@ -0,0 +1,20 @@
+#MY_CFLAGS =-g -O0 -DEBUG_OUTPUT

Please remove that line, it's not ever to be used.

maintainer
#23 lore raw

Re: [PATCH 10/10] added kernel build, configuration, and TODO files

In reply to Stephanie Wallick

drivers/staging/mausb/TODO line 2

Quoted 5 lines · Stephanie Wallick
drivers/staging/mausb/TODO new file +2 -0
@@ -0,0 +1,14 @@
+TODO:
+	- checkpatch.pl cleanups

Is this still true?

And why does this need to be a staging driver? What's wrong with making it a "real" driver now?

drivers/staging/mausb/TODO line 8

Quoted 6 lines
drivers/staging/mausb/TODO new file +6 -0
+	- address miscellaneous "TODO" statements in code
+	- add support for multiple media agnostic (MA) devices
+	- add/improve support for unimplemented packet types
+	- handle errors and failure with more grace and less crash
+
+Please send patches to Greg Kroah-Hartman <[email protected]> and cc:

You didn't use this email address...

drivers/staging/mausb/TODO line 12

Quoted 3 lines
drivers/staging/mausb/TODO new file +3 -0
+	Sean Stalley <[email protected]>
+	Stephanie Wallick <[email protected]>
+

Just make a MAINTAINERS entry please for this info.

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#25 lore raw

Re: [PATCH 10/10] added kernel build, configuration, and TODO files

Quoted 11 lines · sostalle
Quoted 10 lines · Greg KH

drivers/staging/mausb/TODO line 2

Quoted 6 lines · Stephanie Wallick
drivers/staging/mausb/TODO new file +2 -0
@@ -0,0 +1,14 @@
+TODO:
+	- checkpatch.pl cleanups

Is this still true?

Quoted 2 lines

checkpatch.pl reports:
total: 7 errors, 208 warnings, 10489 lines checked

Ok, fair enough.

Quoted 15 lines
Quoted 3 lines

And why does this need to be a staging driver? What's wrong with making it a "real" driver now?

The short answer is: We don't think it's ready to be "real".

We didn't want to release it as a "real" driver because:
- It's not stable enough yet.
- It doesn't implement all the features required by the MA USB spec.
- The MA USB spec isn't standalone. It requires a supplemental spec to define media-specific behavior, like how MA USB packets are encapsulated, how to discover MA USB devices on a network, etc. So far, no media-specific specifications have been published.
- There are no commercially-available Media Agnostic USB devices.

Quoted 5 lines

We wanted to release it to staging now because:
- We have received requests from multiple companies for our host MA USB driver. Putting it in staging seemed the best way to distribute it.
- We feel that releasing what we have to staging now could promote collaboration on one driver & help prevent duplicate work.

Ok, please include that information in the first email, it's much more helpful :)

Can you resend this series, and cc: the linux-usb mailing list, and address the "what about usbip?" questions as well?

thanks,

greg k-h

#26 lore raw
In reply to Stephanie Wallick

[added [email protected] Cc]

Quoted 10 lines · Stephanie Wallick

Media Agnostic (MA) USB enables the USB protocol to be used over a wide range of physical media. MA USB is a relatively new protocol and is currently unsupported in the Linux kernel. This patch set adds the following drivers with the following capabilities:

1) MA USB Host:
- provides functionality of a USB Host Controller.
- implements MA USB protocol for a MA USB host.
- provides MA USB packet transport over TCP

Quoted 4 lines

2) MA USB Device:
- provides functionality of a USB Device Controller.
- implements MA USB protocol for a MA USB device.
- provides MA USB packet transport over TCP

Nice. But don't you think this deserves the attention of the linux-usb mailing list? Yes, Greg's attention is of course good to have too :-) But I believe this is of interest to more people, who might not read everything on linux-kernel.

Has there been any thought/discussion about the relationship to the existing usbip drivers, which just moved out of staging? Is a usbip userspace compatibility layer feasible sometime in the future?

Bjørn

#27 lore raw

[with a valid Message-ID]

Quoted 19 lines · Bjørn Mork

[added [email protected] Cc]

Quoted 10 lines · Stephanie Wallick

Media Agnostic (MA) USB enables the USB protocol to be used over a wide range of physical media. MA USB is a relatively new protocol and is currently unsupported in the Linux kernel. This patch set adds the following drivers with the following capabilities:

1) MA USB Host:
- provides functionality of a USB Host Controller.
- implements MA USB protocol for a MA USB host.
- provides MA USB packet transport over TCP

Quoted 4 lines

2) MA USB Device:
- provides functionality of a USB Device Controller.
- implements MA USB protocol for a MA USB device.
- provides MA USB packet transport over TCP

Quoted 4 lines

Nice. But don't you think this deserves the attention of the linux-usb mailing list? Yes, Greg's attention is of course good to have too :-) But I believe this is of interest to more people, who might not read everything on linux-kernel.

This is probably more relevant/interesting to the people on linux-usb. Since we were trying to get this driver into staging, we thought linux-kernel was the right place to send the patches. We can send the next set out to linux-usb as well.

Quoted 3 lines

Has there been any thought/discussion about the relationship to the existing usbip drivers, which just moved out of staging? Is a usbip userspace compatibility layer feasible sometime in the future?

That discussion hasn't happened. usbip has a different protocol than MA USB, so much of the code cannot be reused.

I just briefly looked into the userspace code for usbip. It looks like the userspace compatibility layer is just a utility for establishing the TCP connection between the host and device & choosing what devices to connect, correct? We have a similar utility (see patch 9/10) which could probably be replaced by the usbip userspace utilities.

Thanks,
Sean