u_ether.h 8.7 KB

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