u_ether.h 8.7 KB

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