u_ether.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. bool is_suspend;
  71. };
  72. #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
  73. |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
  74. |USB_CDC_PACKET_TYPE_PROMISCUOUS \
  75. |USB_CDC_PACKET_TYPE_DIRECTED)
  76. /* variant of gether_setup that allows customizing network device name */
  77. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  78. const char *dev_addr, const char *host_addr,
  79. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
  80. /* netdev setup/teardown as directed by the gadget driver */
  81. /* gether_setup - initialize one ethernet-over-usb link
  82. * @g: gadget to associated with these links
  83. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  84. * host side of the link is recorded
  85. * Context: may sleep
  86. *
  87. * This sets up the single network link that may be exported by a
  88. * gadget driver using this framework. The link layer addresses are
  89. * set up using module parameters.
  90. *
  91. * Returns a eth_dev pointer on success, or an ERR_PTR on failure
  92. */
  93. static inline struct eth_dev *gether_setup(struct usb_gadget *g,
  94. const char *dev_addr, const char *host_addr,
  95. u8 ethaddr[ETH_ALEN], unsigned qmult)
  96. {
  97. return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
  98. }
  99. /*
  100. * variant of gether_setup_default that allows customizing
  101. * network device name
  102. */
  103. struct net_device *gether_setup_name_default(const char *netname);
  104. /*
  105. * gether_register_netdev - register the net device
  106. * @net: net device to register
  107. *
  108. * Registers the net device associated with this ethernet-over-usb link
  109. *
  110. */
  111. int gether_register_netdev(struct net_device *net);
  112. /* gether_setup_default - initialize one ethernet-over-usb link
  113. * Context: may sleep
  114. *
  115. * This sets up the single network link that may be exported by a
  116. * gadget driver using this framework. The link layer addresses
  117. * are set to random values.
  118. *
  119. * Returns negative errno, or zero on success
  120. */
  121. static inline struct net_device *gether_setup_default(void)
  122. {
  123. return gether_setup_name_default("usb");
  124. }
  125. /**
  126. * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
  127. * @net: device representing this link
  128. * @g: the gadget to initialize with
  129. *
  130. * This associates one ethernet-over-usb link with a gadget.
  131. */
  132. void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
  133. /**
  134. * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
  135. * @net: device representing this link
  136. * @dev_addr: eth address of this device
  137. *
  138. * This sets the device-side Ethernet address of this ethernet-over-usb link
  139. * if dev_addr is correct.
  140. * Returns negative errno if the new address is incorrect.
  141. */
  142. int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
  143. /**
  144. * gether_get_dev_addr - get an ethernet-over-usb link eth address
  145. * @net: device representing this link
  146. * @dev_addr: place to store device's eth address
  147. * @len: length of the @dev_addr buffer
  148. *
  149. * This gets the device-side Ethernet address of this ethernet-over-usb link.
  150. * Returns zero on success, else negative errno.
  151. */
  152. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
  153. /**
  154. * gether_set_host_addr - initialize an ethernet-over-usb link with host address
  155. * @net: device representing this link
  156. * @host_addr: eth address of the host
  157. *
  158. * This sets the host-side Ethernet address of this ethernet-over-usb link
  159. * if host_addr is correct.
  160. * Returns negative errno if the new address is incorrect.
  161. */
  162. int gether_set_host_addr(struct net_device *net, const char *host_addr);
  163. /**
  164. * gether_get_host_addr - get an ethernet-over-usb link host address
  165. * @net: device representing this link
  166. * @host_addr: place to store eth address of the host
  167. * @len: length of the @host_addr buffer
  168. *
  169. * This gets the host-side Ethernet address of this ethernet-over-usb link.
  170. * Returns zero on success, else negative errno.
  171. */
  172. int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
  173. /**
  174. * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
  175. * @net: device representing this link
  176. * @host_addr: place to store eth address of the host
  177. * @len: length of the @host_addr buffer
  178. *
  179. * This gets the CDC formatted host-side Ethernet address of this
  180. * ethernet-over-usb link.
  181. * Returns zero on success, else negative errno.
  182. */
  183. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
  184. /**
  185. * gether_get_host_addr_u8 - get an ethernet-over-usb link host address
  186. * @net: device representing this link
  187. * @host_mac: place to store the eth address of the host
  188. *
  189. * This gets the binary formatted host-side Ethernet address of this
  190. * ethernet-over-usb link.
  191. */
  192. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]);
  193. /**
  194. * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
  195. * @net: device representing this link
  196. * @qmult: queue multiplier
  197. *
  198. * This sets the queue length multiplier of this ethernet-over-usb link.
  199. * For higher speeds use longer queues.
  200. */
  201. void gether_set_qmult(struct net_device *net, unsigned qmult);
  202. /**
  203. * gether_get_qmult - get an ethernet-over-usb link multiplier
  204. * @net: device representing this link
  205. *
  206. * This gets the queue length multiplier of this ethernet-over-usb link.
  207. */
  208. unsigned gether_get_qmult(struct net_device *net);
  209. /**
  210. * gether_get_ifname - get an ethernet-over-usb link interface name
  211. * @net: device representing this link
  212. * @name: place to store the interface name
  213. * @len: length of the @name buffer
  214. *
  215. * This gets the interface name of this ethernet-over-usb link.
  216. * Returns zero on success, else negative errno.
  217. */
  218. int gether_get_ifname(struct net_device *net, char *name, int len);
  219. /**
  220. * gether_set_ifname - set an ethernet-over-usb link interface name
  221. * @net: device representing this link
  222. * @name: new interface name
  223. * @len: length of @name
  224. *
  225. * This sets the interface name of this ethernet-over-usb link.
  226. * A single terminating newline, if any, is ignored.
  227. * Returns zero on success, else negative errno.
  228. */
  229. int gether_set_ifname(struct net_device *net, const char *name, int len);
  230. void gether_cleanup(struct eth_dev *dev);
  231. void gether_suspend(struct gether *link);
  232. void gether_resume(struct gether *link);
  233. /* connect/disconnect is handled by individual functions */
  234. struct net_device *gether_connect(struct gether *);
  235. void gether_disconnect(struct gether *);
  236. /* Some controllers can't support CDC Ethernet (ECM) ... */
  237. static inline bool can_support_ecm(struct usb_gadget *gadget)
  238. {
  239. if (!gadget_is_altset_supported(gadget))
  240. return false;
  241. /* Everything else is *presumably* fine ... but this is a bit
  242. * chancy, so be **CERTAIN** there are no hardware issues with
  243. * your controller. Add it above if it can't handle CDC.
  244. */
  245. return true;
  246. }
  247. /* peak (theoretical) bulk transfer rate in bits-per-second */
  248. static inline unsigned int gether_bitrate(struct usb_gadget *g)
  249. {
  250. if (g->speed >= USB_SPEED_SUPER_PLUS)
  251. return 4250000000U;
  252. if (g->speed == USB_SPEED_SUPER)
  253. return 3750000000U;
  254. else if (g->speed == USB_SPEED_HIGH)
  255. return 13 * 512 * 8 * 1000 * 8;
  256. else
  257. return 19 * 64 * 1 * 1000 * 8;
  258. }
  259. #endif /* __U_ETHER_H */