u_ether.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_ether.h -- interface to USB gadget "ethernet link" utilities
  4. *
  5. * Copyright (C) 2003-2005,2008 David Brownell
  6. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  7. * Copyright (C) 2008 Nokia Corporation
  8. */
  9. #ifndef __U_ETHER_H
  10. #define __U_ETHER_H
  11. #include <linux/err.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/usb/composite.h>
  14. #include <linux/usb/cdc.h>
  15. #include <linux/netdevice.h>
  16. #define QMULT_DEFAULT 5
  17. /*
  18. * dev_addr: initial value
  19. * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx"
  20. * host_addr: this address is invisible to ifconfig
  21. */
  22. #define USB_ETHERNET_MODULE_PARAMETERS() \
  23. static unsigned qmult = QMULT_DEFAULT; \
  24. module_param(qmult, uint, S_IRUGO|S_IWUSR); \
  25. MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\
  26. \
  27. static char *dev_addr; \
  28. module_param(dev_addr, charp, S_IRUGO); \
  29. MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \
  30. \
  31. static char *host_addr; \
  32. module_param(host_addr, charp, S_IRUGO); \
  33. MODULE_PARM_DESC(host_addr, "Host Ethernet Address")
  34. struct eth_dev;
  35. /*
  36. * This represents the USB side of an "ethernet" link, managed by a USB
  37. * function which provides control and (maybe) framing. Two functions
  38. * in different configurations could share the same ethernet link/netdev,
  39. * using different host interaction models.
  40. *
  41. * There is a current limitation that only one instance of this link may
  42. * be present in any given configuration. When that's a problem, network
  43. * layer facilities can be used to package multiple logical links on this
  44. * single "physical" one.
  45. */
  46. struct gether {
  47. struct usb_function func;
  48. /* updated by gether_{connect,disconnect} */
  49. struct eth_dev *ioport;
  50. /* endpoints handle full and/or high speeds */
  51. struct usb_ep *in_ep;
  52. struct usb_ep *out_ep;
  53. bool is_zlp_ok;
  54. u16 cdc_filter;
  55. /* hooks for added framing, as needed for RNDIS and EEM. */
  56. u32 header_len;
  57. /* NCM requires fixed size bundles */
  58. bool is_fixed;
  59. u32 fixed_out_len;
  60. u32 fixed_in_len;
  61. bool supports_multi_frame;
  62. struct sk_buff *(*wrap)(struct gether *port,
  63. struct sk_buff *skb);
  64. int (*unwrap)(struct gether *port,
  65. struct sk_buff *skb,
  66. struct sk_buff_head *list);
  67. /* called on network open/close */
  68. void (*open)(struct gether *);
  69. void (*close)(struct gether *);
  70. };
  71. #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
  72. |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
  73. |USB_CDC_PACKET_TYPE_PROMISCUOUS \
  74. |USB_CDC_PACKET_TYPE_DIRECTED)
  75. /* variant of gether_setup that allows customizing network device name */
  76. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  77. const char *dev_addr, const char *host_addr,
  78. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
  79. /* netdev setup/teardown as directed by the gadget driver */
  80. /* gether_setup - initialize one ethernet-over-usb link
  81. * @g: gadget to associated with these links
  82. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  83. * host side of the link is recorded
  84. * Context: may sleep
  85. *
  86. * This sets up the single network link that may be exported by a
  87. * gadget driver using this framework. The link layer addresses are
  88. * set up using module parameters.
  89. *
  90. * Returns a eth_dev pointer on success, or an ERR_PTR on failure
  91. */
  92. static inline struct eth_dev *gether_setup(struct usb_gadget *g,
  93. const char *dev_addr, const char *host_addr,
  94. u8 ethaddr[ETH_ALEN], unsigned qmult)
  95. {
  96. return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
  97. }
  98. /*
  99. * variant of gether_setup_default that allows customizing
  100. * network device name
  101. */
  102. struct net_device *gether_setup_name_default(const char *netname);
  103. /*
  104. * gether_register_netdev - register the net device
  105. * @net: net device to register
  106. *
  107. * Registers the net device associated with this ethernet-over-usb link
  108. *
  109. */
  110. int gether_register_netdev(struct net_device *net);
  111. /* gether_setup_default - initialize one ethernet-over-usb link
  112. * Context: may sleep
  113. *
  114. * This sets up the single network link that may be exported by a
  115. * gadget driver using this framework. The link layer addresses
  116. * are set to random values.
  117. *
  118. * Returns negative errno, or zero on success
  119. */
  120. static inline struct net_device *gether_setup_default(void)
  121. {
  122. return gether_setup_name_default("usb");
  123. }
  124. /**
  125. * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
  126. * @net: device representing this link
  127. * @g: the gadget to initialize with
  128. *
  129. * This associates one ethernet-over-usb link with a gadget.
  130. */
  131. void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
  132. /**
  133. * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
  134. * @net: device representing this link
  135. * @dev_addr: eth address of this device
  136. *
  137. * This sets the device-side Ethernet address of this ethernet-over-usb link
  138. * if dev_addr is correct.
  139. * Returns negative errno if the new address is incorrect.
  140. */
  141. int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
  142. /**
  143. * gether_get_dev_addr - get an ethernet-over-usb link eth address
  144. * @net: device representing this link
  145. * @dev_addr: place to store device's eth address
  146. * @len: length of the @dev_addr buffer
  147. *
  148. * This gets the device-side Ethernet address of this ethernet-over-usb link.
  149. * Returns zero on success, else negative errno.
  150. */
  151. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
  152. /**
  153. * gether_set_host_addr - initialize an ethernet-over-usb link with host address
  154. * @net: device representing this link
  155. * @host_addr: eth address of the host
  156. *
  157. * This sets the host-side Ethernet address of this ethernet-over-usb link
  158. * if host_addr is correct.
  159. * Returns negative errno if the new address is incorrect.
  160. */
  161. int gether_set_host_addr(struct net_device *net, const char *host_addr);
  162. /**
  163. * gether_get_host_addr - get an ethernet-over-usb link host address
  164. * @net: device representing this link
  165. * @host_addr: place to store eth address of the host
  166. * @len: length of the @host_addr buffer
  167. *
  168. * This gets the host-side Ethernet address of this ethernet-over-usb link.
  169. * Returns zero on success, else negative errno.
  170. */
  171. int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
  172. /**
  173. * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
  174. * @net: device representing this link
  175. * @host_addr: place to store eth address of the host
  176. * @len: length of the @host_addr buffer
  177. *
  178. * This gets the CDC formatted host-side Ethernet address of this
  179. * ethernet-over-usb link.
  180. * Returns zero on success, else negative errno.
  181. */
  182. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
  183. /**
  184. * gether_get_host_addr_u8 - get an ethernet-over-usb link host address
  185. * @net: device representing this link
  186. * @host_mac: place to store the eth address of the host
  187. *
  188. * This gets the binary formatted host-side Ethernet address of this
  189. * ethernet-over-usb link.
  190. */
  191. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]);
  192. /**
  193. * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
  194. * @net: device representing this link
  195. * @qmult: queue multiplier
  196. *
  197. * This sets the queue length multiplier of this ethernet-over-usb link.
  198. * For higher speeds use longer queues.
  199. */
  200. void gether_set_qmult(struct net_device *net, unsigned qmult);
  201. /**
  202. * gether_get_qmult - get an ethernet-over-usb link multiplier
  203. * @net: device representing this link
  204. *
  205. * This gets the queue length multiplier of this ethernet-over-usb link.
  206. */
  207. unsigned gether_get_qmult(struct net_device *net);
  208. /**
  209. * gether_get_ifname - get an ethernet-over-usb link interface name
  210. * @net: device representing this link
  211. * @name: place to store the interface name
  212. * @len: length of the @name buffer
  213. *
  214. * This gets the interface name of this ethernet-over-usb link.
  215. * Returns zero on success, else negative errno.
  216. */
  217. int gether_get_ifname(struct net_device *net, char *name, int len);
  218. void gether_cleanup(struct eth_dev *dev);
  219. /* connect/disconnect is handled by individual functions */
  220. struct net_device *gether_connect(struct gether *);
  221. void gether_disconnect(struct gether *);
  222. /* Some controllers can't support CDC Ethernet (ECM) ... */
  223. static inline bool can_support_ecm(struct usb_gadget *gadget)
  224. {
  225. if (!gadget_is_altset_supported(gadget))
  226. return false;
  227. /* Everything else is *presumably* fine ... but this is a bit
  228. * chancy, so be **CERTAIN** there are no hardware issues with
  229. * your controller. Add it above if it can't handle CDC.
  230. */
  231. return true;
  232. }
  233. #endif /* __U_ETHER_H */