2017-11-03 17:28:30 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2010-12-08 18:12:06 +07:00
|
|
|
/*
|
|
|
|
* ncm.c -- NCM gadget driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Nokia Corporation
|
|
|
|
* Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
|
|
|
|
*
|
|
|
|
* The driver borrows from ether.c which is:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2005,2008 David Brownell
|
|
|
|
* Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
|
|
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
/* #define VERBOSE_DEBUG */
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2012-09-07 01:11:27 +07:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/usb/composite.h>
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
#include "u_ether.h"
|
2013-05-23 14:22:07 +07:00
|
|
|
#include "u_ncm.h"
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
#define DRIVER_DESC "NCM Gadget"
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
|
|
|
|
* Instead: allocate your own, using normal USB-IF procedures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Thanks to NetChip Technologies for donating this product ID.
|
|
|
|
* It's for devices with only CDC Ethernet configurations.
|
|
|
|
*/
|
|
|
|
#define CDC_VENDOR_NUM 0x0525 /* NetChip */
|
|
|
|
#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
2012-09-10 20:01:53 +07:00
|
|
|
USB_GADGET_COMPOSITE_OPTIONS();
|
2010-12-08 18:12:06 +07:00
|
|
|
|
usb: gadget: u_ether: convert into module
u_ether.c has been #include'd by all gadgets which implement
USB Ethernet functions. In order to add configfs support,
the f_ecm.c, f_eem.c, f_ncm.c, f_subset.c, f_rndis.c need to be
converted into modules and must not be #include'd. Consequently,
the u_ether.c needs to be a module too, in a manner similar
to u_serial.c. The resulting module should not take any parameters,
so they are pushed to the current users of it, that is ether.c,
g_ffs.c, multi.c, ncm.c, nokia.c.
Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>
2013-05-23 14:22:03 +07:00
|
|
|
USB_ETHERNET_MODULE_PARAMETERS();
|
|
|
|
|
2010-12-08 18:12:06 +07:00
|
|
|
static struct usb_device_descriptor device_desc = {
|
|
|
|
.bLength = sizeof device_desc,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
|
|
|
|
2015-10-20 23:33:13 +07:00
|
|
|
/* .bcdUSB = DYNAMIC */
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
.bDeviceClass = USB_CLASS_COMM,
|
|
|
|
.bDeviceSubClass = 0,
|
|
|
|
.bDeviceProtocol = 0,
|
|
|
|
/* .bMaxPacketSize0 = f(hardware) */
|
|
|
|
|
|
|
|
/* Vendor and product id defaults change according to what configs
|
|
|
|
* we support. (As does bNumConfigurations.) These values can
|
|
|
|
* also be overridden by module parameters.
|
|
|
|
*/
|
|
|
|
.idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
|
|
|
|
.idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
|
|
|
|
/* .bcdDevice = f(hardware) */
|
|
|
|
/* .iManufacturer = DYNAMIC */
|
|
|
|
/* .iProduct = DYNAMIC */
|
|
|
|
/* NO SERIAL NUMBER */
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
2015-07-09 14:18:57 +07:00
|
|
|
static const struct usb_descriptor_header *otg_desc[2];
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
/* string IDs are assigned dynamically */
|
|
|
|
static struct usb_string strings_dev[] = {
|
2012-09-10 20:01:58 +07:00
|
|
|
[USB_GADGET_MANUFACTURER_IDX].s = "",
|
2012-09-07 01:11:21 +07:00
|
|
|
[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
|
|
|
|
[USB_GADGET_SERIAL_IDX].s = "",
|
2010-12-08 18:12:06 +07:00
|
|
|
{ } /* end of list */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings stringtab_dev = {
|
|
|
|
.language = 0x0409, /* en-us */
|
|
|
|
.strings = strings_dev,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings *dev_strings[] = {
|
|
|
|
&stringtab_dev,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2013-05-23 14:22:07 +07:00
|
|
|
static struct usb_function_instance *f_ncm_inst;
|
|
|
|
static struct usb_function *f_ncm;
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2015-04-11 05:14:21 +07:00
|
|
|
static int ncm_do_config(struct usb_configuration *c)
|
2010-12-08 18:12:06 +07:00
|
|
|
{
|
2013-05-23 14:22:07 +07:00
|
|
|
int status;
|
|
|
|
|
2010-12-08 18:12:06 +07:00
|
|
|
/* FIXME alloc iConfiguration string, set it in c->strings */
|
|
|
|
|
|
|
|
if (gadget_is_otg(c->cdev->gadget)) {
|
|
|
|
c->descriptors = otg_desc;
|
|
|
|
c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
|
|
|
|
}
|
|
|
|
|
2013-05-23 14:22:07 +07:00
|
|
|
f_ncm = usb_get_function(f_ncm_inst);
|
2018-01-19 05:25:51 +07:00
|
|
|
if (IS_ERR(f_ncm))
|
|
|
|
return PTR_ERR(f_ncm);
|
2013-05-23 14:22:07 +07:00
|
|
|
|
|
|
|
status = usb_add_function(c, f_ncm);
|
|
|
|
if (status < 0) {
|
|
|
|
usb_put_function(f_ncm);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-12-08 18:12:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct usb_configuration ncm_config_driver = {
|
|
|
|
/* .label = f(hardware) */
|
|
|
|
.label = "CDC Ethernet (NCM)",
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
/* .iConfiguration = DYNAMIC */
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2015-04-11 05:14:21 +07:00
|
|
|
static int gncm_bind(struct usb_composite_dev *cdev)
|
2010-12-08 18:12:06 +07:00
|
|
|
{
|
|
|
|
struct usb_gadget *gadget = cdev->gadget;
|
2013-05-23 14:22:07 +07:00
|
|
|
struct f_ncm_opts *ncm_opts;
|
2010-12-08 18:12:06 +07:00
|
|
|
int status;
|
|
|
|
|
2013-05-23 14:22:07 +07:00
|
|
|
f_ncm_inst = usb_get_function_instance("ncm");
|
|
|
|
if (IS_ERR(f_ncm_inst))
|
|
|
|
return PTR_ERR(f_ncm_inst);
|
|
|
|
|
|
|
|
ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst);
|
|
|
|
|
|
|
|
gether_set_qmult(ncm_opts->net, qmult);
|
|
|
|
if (!gether_set_host_addr(ncm_opts->net, host_addr))
|
|
|
|
pr_info("using host ethernet address: %s", host_addr);
|
|
|
|
if (!gether_set_dev_addr(ncm_opts->net, dev_addr))
|
|
|
|
pr_info("using self ethernet address: %s", dev_addr);
|
2010-12-08 18:12:06 +07:00
|
|
|
|
|
|
|
/* Allocate string descriptor numbers ... note that string
|
|
|
|
* contents can be overridden by the composite_dev glue.
|
|
|
|
*/
|
|
|
|
|
2012-09-07 01:11:16 +07:00
|
|
|
status = usb_string_ids_tab(cdev, strings_dev);
|
2010-12-08 18:12:06 +07:00
|
|
|
if (status < 0)
|
|
|
|
goto fail;
|
2012-09-07 01:11:21 +07:00
|
|
|
device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
|
|
|
|
device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
|
2010-12-08 18:12:06 +07:00
|
|
|
|
2015-07-09 14:18:57 +07:00
|
|
|
if (gadget_is_otg(gadget) && !otg_desc[0]) {
|
|
|
|
struct usb_descriptor_header *usb_desc;
|
|
|
|
|
|
|
|
usb_desc = usb_otg_descriptor_alloc(gadget);
|
|
|
|
if (!usb_desc)
|
|
|
|
goto fail;
|
|
|
|
usb_otg_descriptor_init(gadget, usb_desc);
|
|
|
|
otg_desc[0] = usb_desc;
|
|
|
|
otg_desc[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-08 18:12:06 +07:00
|
|
|
status = usb_add_config(cdev, &ncm_config_driver,
|
|
|
|
ncm_do_config);
|
|
|
|
if (status < 0)
|
2015-07-09 14:18:57 +07:00
|
|
|
goto fail1;
|
2010-12-08 18:12:06 +07:00
|
|
|
|
2012-09-10 20:01:53 +07:00
|
|
|
usb_composite_overwrite_options(cdev, &coverwrite);
|
2010-12-08 18:12:06 +07:00
|
|
|
dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2015-07-09 14:18:57 +07:00
|
|
|
fail1:
|
|
|
|
kfree(otg_desc[0]);
|
|
|
|
otg_desc[0] = NULL;
|
2010-12-08 18:12:06 +07:00
|
|
|
fail:
|
2013-05-23 14:22:07 +07:00
|
|
|
usb_put_function_instance(f_ncm_inst);
|
2010-12-08 18:12:06 +07:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-04-11 05:14:21 +07:00
|
|
|
static int gncm_unbind(struct usb_composite_dev *cdev)
|
2010-12-08 18:12:06 +07:00
|
|
|
{
|
2013-05-23 14:22:07 +07:00
|
|
|
if (!IS_ERR_OR_NULL(f_ncm))
|
|
|
|
usb_put_function(f_ncm);
|
|
|
|
if (!IS_ERR_OR_NULL(f_ncm_inst))
|
|
|
|
usb_put_function_instance(f_ncm_inst);
|
2015-07-09 14:18:57 +07:00
|
|
|
kfree(otg_desc[0]);
|
|
|
|
otg_desc[0] = NULL;
|
|
|
|
|
2010-12-08 18:12:06 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 05:14:21 +07:00
|
|
|
static struct usb_composite_driver ncm_driver = {
|
2010-12-08 18:12:06 +07:00
|
|
|
.name = "g_ncm",
|
|
|
|
.dev = &device_desc,
|
|
|
|
.strings = dev_strings,
|
2011-06-29 20:41:49 +07:00
|
|
|
.max_speed = USB_SPEED_HIGH,
|
2012-09-07 01:11:04 +07:00
|
|
|
.bind = gncm_bind,
|
2015-04-11 05:14:21 +07:00
|
|
|
.unbind = gncm_unbind,
|
2010-12-08 18:12:06 +07:00
|
|
|
};
|
|
|
|
|
2014-07-09 23:09:56 +07:00
|
|
|
module_usb_composite_driver(ncm_driver);
|
|
|
|
|
2010-12-08 18:12:06 +07:00
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_AUTHOR("Yauheni Kaliuta");
|
|
|
|
MODULE_LICENSE("GPL");
|