u_ether.h 9.8 KB

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