u_ether.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void gether_cleanup(struct eth_dev *dev);
  102. /* connect/disconnect is handled by individual functions */
  103. struct net_device *gether_connect(struct gether *);
  104. void gether_disconnect(struct gether *);
  105. /* Some controllers can't support CDC Ethernet (ECM) ... */
  106. static inline bool can_support_ecm(struct usb_gadget *gadget)
  107. {
  108. if (!gadget_supports_altsettings(gadget))
  109. return false;
  110. /* Everything else is *presumably* fine ... but this is a bit
  111. * chancy, so be **CERTAIN** there are no hardware issues with
  112. * your controller. Add it above if it can't handle CDC.
  113. */
  114. return true;
  115. }
  116. /* each configuration may bind one instance of an ethernet link */
  117. int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  118. struct eth_dev *dev);
  119. int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  120. struct eth_dev *dev);
  121. int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  122. struct eth_dev *dev);
  123. int eem_bind_config(struct usb_configuration *c, struct eth_dev *dev);
  124. #ifdef USB_ETH_RNDIS
  125. int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  126. u32 vendorID, const char *manufacturer, struct eth_dev *dev);
  127. #else
  128. static inline int
  129. rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  130. u32 vendorID, const char *manufacturer, struct eth_dev *dev)
  131. {
  132. return 0;
  133. }
  134. #endif
  135. /**
  136. * rndis_bind_config - add RNDIS network link to a configuration
  137. * @c: the configuration to support the network link
  138. * @ethaddr: a buffer in which the ethernet address of the host side
  139. * side of the link was recorded
  140. * Context: single threaded during gadget setup
  141. *
  142. * Returns zero on success, else negative errno.
  143. *
  144. * Caller must have called @gether_setup(). Caller is also responsible
  145. * for calling @gether_cleanup() before module unload.
  146. */
  147. static inline int rndis_bind_config(struct usb_configuration *c,
  148. u8 ethaddr[ETH_ALEN], struct eth_dev *dev)
  149. {
  150. return rndis_bind_config_vendor(c, ethaddr, 0, NULL, dev);
  151. }
  152. #endif /* __U_ETHER_H */