David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * u_ether.h -- interface to USB gadget "ethernet link" utilities |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 6 | * Copyright (C) 2008 Nokia Corporation |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifndef __U_ETHER_H |
| 15 | #define __U_ETHER_H |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/usb/composite.h> |
| 20 | #include <linux/usb/cdc.h> |
| 21 | |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 22 | #include "gadget_chips.h" |
| 23 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 24 | #define QMULT_DEFAULT 5 |
| 25 | |
| 26 | /* |
| 27 | * dev_addr: initial value |
| 28 | * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" |
| 29 | * host_addr: this address is invisible to ifconfig |
| 30 | */ |
| 31 | #define USB_ETHERNET_MODULE_PARAMETERS() \ |
| 32 | static unsigned qmult = QMULT_DEFAULT; \ |
| 33 | module_param(qmult, uint, S_IRUGO|S_IWUSR); \ |
| 34 | MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\ |
| 35 | \ |
| 36 | static char *dev_addr; \ |
| 37 | module_param(dev_addr, charp, S_IRUGO); \ |
| 38 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \ |
| 39 | \ |
| 40 | static char *host_addr; \ |
| 41 | module_param(host_addr, charp, S_IRUGO); \ |
| 42 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address") |
| 43 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 44 | struct eth_dev; |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 45 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 46 | /* |
| 47 | * This represents the USB side of an "ethernet" link, managed by a USB |
| 48 | * function which provides control and (maybe) framing. Two functions |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 49 | * in different configurations could share the same ethernet link/netdev, |
| 50 | * using different host interaction models. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 51 | * |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 52 | * There is a current limitation that only one instance of this link may |
| 53 | * be present in any given configuration. When that's a problem, network |
| 54 | * layer facilities can be used to package multiple logical links on this |
| 55 | * single "physical" one. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 56 | */ |
| 57 | struct gether { |
| 58 | struct usb_function func; |
| 59 | |
| 60 | /* updated by gether_{connect,disconnect} */ |
| 61 | struct eth_dev *ioport; |
| 62 | |
| 63 | /* endpoints handle full and/or high speeds */ |
| 64 | struct usb_ep *in_ep; |
| 65 | struct usb_ep *out_ep; |
| 66 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 67 | bool is_zlp_ok; |
| 68 | |
| 69 | u16 cdc_filter; |
| 70 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 71 | /* hooks for added framing, as needed for RNDIS and EEM. */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 72 | u32 header_len; |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 73 | /* NCM requires fixed size bundles */ |
| 74 | bool is_fixed; |
| 75 | u32 fixed_out_len; |
| 76 | u32 fixed_in_len; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 77 | struct sk_buff *(*wrap)(struct gether *port, |
| 78 | struct sk_buff *skb); |
| 79 | int (*unwrap)(struct gether *port, |
| 80 | struct sk_buff *skb, |
| 81 | struct sk_buff_head *list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 82 | |
| 83 | /* called on network open/close */ |
| 84 | void (*open)(struct gether *); |
| 85 | void (*close)(struct gether *); |
| 86 | }; |
| 87 | |
| 88 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
| 89 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
| 90 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ |
| 91 | |USB_CDC_PACKET_TYPE_DIRECTED) |
| 92 | |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 93 | /* variant of gether_setup that allows customizing network device name */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 94 | struct eth_dev *gether_setup_name(struct usb_gadget *g, |
| 95 | const char *dev_addr, const char *host_addr, |
| 96 | u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 97 | |
| 98 | /* netdev setup/teardown as directed by the gadget driver */ |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 99 | /* gether_setup - initialize one ethernet-over-usb link |
| 100 | * @g: gadget to associated with these links |
| 101 | * @ethaddr: NULL, or a buffer in which the ethernet address of the |
| 102 | * host side of the link is recorded |
| 103 | * Context: may sleep |
| 104 | * |
| 105 | * This sets up the single network link that may be exported by a |
| 106 | * gadget driver using this framework. The link layer addresses are |
| 107 | * set up using module parameters. |
| 108 | * |
| 109 | * Returns negative errno, or zero on success |
| 110 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 111 | static inline struct eth_dev *gether_setup(struct usb_gadget *g, |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 112 | const char *dev_addr, const char *host_addr, |
| 113 | u8 ethaddr[ETH_ALEN], unsigned qmult) |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 114 | { |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 115 | return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb"); |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 118 | void gether_cleanup(struct eth_dev *dev); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 119 | |
| 120 | /* connect/disconnect is handled by individual functions */ |
| 121 | struct net_device *gether_connect(struct gether *); |
| 122 | void gether_disconnect(struct gether *); |
| 123 | |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 124 | /* Some controllers can't support CDC Ethernet (ECM) ... */ |
| 125 | static inline bool can_support_ecm(struct usb_gadget *gadget) |
| 126 | { |
| 127 | if (!gadget_supports_altsettings(gadget)) |
| 128 | return false; |
| 129 | |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 130 | /* Everything else is *presumably* fine ... but this is a bit |
| 131 | * chancy, so be **CERTAIN** there are no hardware issues with |
| 132 | * your controller. Add it above if it can't handle CDC. |
| 133 | */ |
| 134 | return true; |
| 135 | } |
| 136 | |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 137 | /* each configuration may bind one instance of an ethernet link */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 138 | int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 139 | struct eth_dev *dev); |
| 140 | int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 141 | struct eth_dev *dev); |
| 142 | int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 143 | struct eth_dev *dev); |
| 144 | int eem_bind_config(struct usb_configuration *c, struct eth_dev *dev); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 145 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 146 | #ifdef USB_ETH_RNDIS |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 147 | |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 148 | int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 149 | u32 vendorID, const char *manufacturer, struct eth_dev *dev); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 150 | |
| 151 | #else |
| 152 | |
| 153 | static inline int |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 154 | rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 155 | u32 vendorID, const char *manufacturer, struct eth_dev *dev) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | #endif |
| 161 | |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 162 | /** |
| 163 | * rndis_bind_config - add RNDIS network link to a configuration |
| 164 | * @c: the configuration to support the network link |
| 165 | * @ethaddr: a buffer in which the ethernet address of the host side |
| 166 | * side of the link was recorded |
| 167 | * Context: single threaded during gadget setup |
| 168 | * |
| 169 | * Returns zero on success, else negative errno. |
| 170 | * |
| 171 | * Caller must have called @gether_setup(). Caller is also responsible |
| 172 | * for calling @gether_cleanup() before module unload. |
| 173 | */ |
| 174 | static inline int rndis_bind_config(struct usb_configuration *c, |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 175 | u8 ethaddr[ETH_ALEN], struct eth_dev *dev) |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 176 | { |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 177 | return rndis_bind_config_vendor(c, ethaddr, 0, NULL, dev); |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 181 | #endif /* __U_ETHER_H */ |