u_ether.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
  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. /* #define VERBOSE_DEBUG */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/gfp.h>
  13. #include <linux/device.h>
  14. #include <linux/ctype.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/if_vlan.h>
  18. #include "u_ether.h"
  19. /*
  20. * This component encapsulates the Ethernet link glue needed to provide
  21. * one (!) network link through the USB gadget stack, normally "usb0".
  22. *
  23. * The control and data models are handled by the function driver which
  24. * connects to this code; such as CDC Ethernet (ECM or EEM),
  25. * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
  26. * management.
  27. *
  28. * Link level addressing is handled by this component using module
  29. * parameters; if no such parameters are provided, random link level
  30. * addresses are used. Each end of the link uses one address. The
  31. * host end address is exported in various ways, and is often recorded
  32. * in configuration databases.
  33. *
  34. * The driver which assembles each configuration using such a link is
  35. * responsible for ensuring that each configuration includes at most one
  36. * instance of is network link. (The network layer provides ways for
  37. * this single "physical" link to be used by multiple virtual links.)
  38. */
  39. #define UETH__VERSION "29-May-2008"
  40. /* Experiments show that both Linux and Windows hosts allow up to 16k
  41. * frame sizes. Set the max MTU size to 15k+52 to prevent allocating 32k
  42. * blocks and still have efficient handling. */
  43. #define GETHER_MAX_MTU_SIZE 15412
  44. #define GETHER_MAX_ETH_FRAME_LEN (GETHER_MAX_MTU_SIZE + ETH_HLEN)
  45. struct eth_dev {
  46. /* lock is held while accessing port_usb
  47. */
  48. spinlock_t lock;
  49. struct gether *port_usb;
  50. struct net_device *net;
  51. struct usb_gadget *gadget;
  52. spinlock_t req_lock; /* guard {rx,tx}_reqs */
  53. struct list_head tx_reqs, rx_reqs;
  54. atomic_t tx_qlen;
  55. struct sk_buff_head rx_frames;
  56. unsigned qmult;
  57. unsigned header_len;
  58. struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
  59. int (*unwrap)(struct gether *,
  60. struct sk_buff *skb,
  61. struct sk_buff_head *list);
  62. struct work_struct work;
  63. unsigned long todo;
  64. #define WORK_RX_MEMORY 0
  65. bool zlp;
  66. bool no_skb_reserve;
  67. u8 host_mac[ETH_ALEN];
  68. u8 dev_mac[ETH_ALEN];
  69. };
  70. /*-------------------------------------------------------------------------*/
  71. #define RX_EXTRA 20 /* bytes guarding against rx overflows */
  72. #define DEFAULT_QLEN 2 /* double buffering by default */
  73. /* for dual-speed hardware, use deeper queues at high/super speed */
  74. static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
  75. {
  76. if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
  77. gadget->speed >= USB_SPEED_SUPER))
  78. return qmult * DEFAULT_QLEN;
  79. else
  80. return DEFAULT_QLEN;
  81. }
  82. /*-------------------------------------------------------------------------*/
  83. /* REVISIT there must be a better way than having two sets
  84. * of debug calls ...
  85. */
  86. #undef DBG
  87. #undef VDBG
  88. #undef ERROR
  89. #undef INFO
  90. #define xprintk(d, level, fmt, args...) \
  91. printk(level "%s: " fmt , (d)->net->name , ## args)
  92. #ifdef DEBUG
  93. #undef DEBUG
  94. #define DBG(dev, fmt, args...) \
  95. xprintk(dev , KERN_DEBUG , fmt , ## args)
  96. #else
  97. #define DBG(dev, fmt, args...) \
  98. do { } while (0)
  99. #endif /* DEBUG */
  100. #ifdef VERBOSE_DEBUG
  101. #define VDBG DBG
  102. #else
  103. #define VDBG(dev, fmt, args...) \
  104. do { } while (0)
  105. #endif /* DEBUG */
  106. #define ERROR(dev, fmt, args...) \
  107. xprintk(dev , KERN_ERR , fmt , ## args)
  108. #define INFO(dev, fmt, args...) \
  109. xprintk(dev , KERN_INFO , fmt , ## args)
  110. /*-------------------------------------------------------------------------*/
  111. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  112. static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
  113. {
  114. struct eth_dev *dev = netdev_priv(net);
  115. strlcpy(p->driver, "g_ether", sizeof(p->driver));
  116. strlcpy(p->version, UETH__VERSION, sizeof(p->version));
  117. strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
  118. strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
  119. }
  120. /* REVISIT can also support:
  121. * - WOL (by tracking suspends and issuing remote wakeup)
  122. * - msglevel (implies updated messaging)
  123. * - ... probably more ethtool ops
  124. */
  125. static const struct ethtool_ops ops = {
  126. .get_drvinfo = eth_get_drvinfo,
  127. .get_link = ethtool_op_get_link,
  128. };
  129. static void defer_kevent(struct eth_dev *dev, int flag)
  130. {
  131. if (test_and_set_bit(flag, &dev->todo))
  132. return;
  133. if (!schedule_work(&dev->work))
  134. ERROR(dev, "kevent %d may have been dropped\n", flag);
  135. else
  136. DBG(dev, "kevent %d scheduled\n", flag);
  137. }
  138. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  139. static int
  140. rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
  141. {
  142. struct usb_gadget *g = dev->gadget;
  143. struct sk_buff *skb;
  144. int retval = -ENOMEM;
  145. size_t size = 0;
  146. struct usb_ep *out;
  147. unsigned long flags;
  148. spin_lock_irqsave(&dev->lock, flags);
  149. if (dev->port_usb)
  150. out = dev->port_usb->out_ep;
  151. else
  152. out = NULL;
  153. if (!out)
  154. {
  155. spin_unlock_irqrestore(&dev->lock, flags);
  156. return -ENOTCONN;
  157. }
  158. /* Padding up to RX_EXTRA handles minor disagreements with host.
  159. * Normally we use the USB "terminate on short read" convention;
  160. * so allow up to (N*maxpacket), since that memory is normally
  161. * already allocated. Some hardware doesn't deal well with short
  162. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  163. * byte off the end (to force hardware errors on overflow).
  164. *
  165. * RNDIS uses internal framing, and explicitly allows senders to
  166. * pad to end-of-packet. That's potentially nice for speed, but
  167. * means receivers can't recover lost synch on their own (because
  168. * new packets don't only start after a short RX).
  169. */
  170. size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
  171. size += dev->port_usb->header_len;
  172. if (g->quirk_ep_out_aligned_size) {
  173. size += out->maxpacket - 1;
  174. size -= size % out->maxpacket;
  175. }
  176. if (dev->port_usb->is_fixed)
  177. size = max_t(size_t, size, dev->port_usb->fixed_out_len);
  178. spin_unlock_irqrestore(&dev->lock, flags);
  179. skb = __netdev_alloc_skb(dev->net, size + NET_IP_ALIGN, gfp_flags);
  180. if (skb == NULL) {
  181. DBG(dev, "no rx skb\n");
  182. goto enomem;
  183. }
  184. /* Some platforms perform better when IP packets are aligned,
  185. * but on at least one, checksumming fails otherwise. Note:
  186. * RNDIS headers involve variable numbers of LE32 values.
  187. */
  188. if (likely(!dev->no_skb_reserve))
  189. skb_reserve(skb, NET_IP_ALIGN);
  190. req->buf = skb->data;
  191. req->length = size;
  192. req->complete = rx_complete;
  193. req->context = skb;
  194. retval = usb_ep_queue(out, req, gfp_flags);
  195. if (retval == -ENOMEM)
  196. enomem:
  197. defer_kevent(dev, WORK_RX_MEMORY);
  198. if (retval) {
  199. DBG(dev, "rx submit --> %d\n", retval);
  200. if (skb)
  201. dev_kfree_skb_any(skb);
  202. spin_lock_irqsave(&dev->req_lock, flags);
  203. list_add(&req->list, &dev->rx_reqs);
  204. spin_unlock_irqrestore(&dev->req_lock, flags);
  205. }
  206. return retval;
  207. }
  208. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  209. {
  210. struct sk_buff *skb = req->context, *skb2;
  211. struct eth_dev *dev = ep->driver_data;
  212. int status = req->status;
  213. switch (status) {
  214. /* normal completion */
  215. case 0:
  216. skb_put(skb, req->actual);
  217. if (dev->unwrap) {
  218. unsigned long flags;
  219. spin_lock_irqsave(&dev->lock, flags);
  220. if (dev->port_usb) {
  221. status = dev->unwrap(dev->port_usb,
  222. skb,
  223. &dev->rx_frames);
  224. } else {
  225. dev_kfree_skb_any(skb);
  226. status = -ENOTCONN;
  227. }
  228. spin_unlock_irqrestore(&dev->lock, flags);
  229. } else {
  230. skb_queue_tail(&dev->rx_frames, skb);
  231. }
  232. skb = NULL;
  233. skb2 = skb_dequeue(&dev->rx_frames);
  234. while (skb2) {
  235. if (status < 0
  236. || ETH_HLEN > skb2->len
  237. || skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
  238. dev->net->stats.rx_errors++;
  239. dev->net->stats.rx_length_errors++;
  240. DBG(dev, "rx length %d\n", skb2->len);
  241. dev_kfree_skb_any(skb2);
  242. goto next_frame;
  243. }
  244. skb2->protocol = eth_type_trans(skb2, dev->net);
  245. dev->net->stats.rx_packets++;
  246. dev->net->stats.rx_bytes += skb2->len;
  247. /* no buffer copies needed, unless hardware can't
  248. * use skb buffers.
  249. */
  250. status = netif_rx(skb2);
  251. next_frame:
  252. skb2 = skb_dequeue(&dev->rx_frames);
  253. }
  254. break;
  255. /* software-driven interface shutdown */
  256. case -ECONNRESET: /* unlink */
  257. case -ESHUTDOWN: /* disconnect etc */
  258. VDBG(dev, "rx shutdown, code %d\n", status);
  259. goto quiesce;
  260. /* for hardware automagic (such as pxa) */
  261. case -ECONNABORTED: /* endpoint reset */
  262. DBG(dev, "rx %s reset\n", ep->name);
  263. defer_kevent(dev, WORK_RX_MEMORY);
  264. quiesce:
  265. dev_kfree_skb_any(skb);
  266. goto clean;
  267. /* data overrun */
  268. case -EOVERFLOW:
  269. dev->net->stats.rx_over_errors++;
  270. /* FALLTHROUGH */
  271. default:
  272. dev->net->stats.rx_errors++;
  273. DBG(dev, "rx status %d\n", status);
  274. break;
  275. }
  276. if (skb)
  277. dev_kfree_skb_any(skb);
  278. if (!netif_running(dev->net)) {
  279. clean:
  280. spin_lock(&dev->req_lock);
  281. list_add(&req->list, &dev->rx_reqs);
  282. spin_unlock(&dev->req_lock);
  283. req = NULL;
  284. }
  285. if (req)
  286. rx_submit(dev, req, GFP_ATOMIC);
  287. }
  288. static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
  289. {
  290. unsigned i;
  291. struct usb_request *req;
  292. if (!n)
  293. return -ENOMEM;
  294. /* queue/recycle up to N requests */
  295. i = n;
  296. list_for_each_entry(req, list, list) {
  297. if (i-- == 0)
  298. goto extra;
  299. }
  300. while (i--) {
  301. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  302. if (!req)
  303. return list_empty(list) ? -ENOMEM : 0;
  304. list_add(&req->list, list);
  305. }
  306. return 0;
  307. extra:
  308. /* free extras */
  309. for (;;) {
  310. struct list_head *next;
  311. next = req->list.next;
  312. list_del(&req->list);
  313. usb_ep_free_request(ep, req);
  314. if (next == list)
  315. break;
  316. req = container_of(next, struct usb_request, list);
  317. }
  318. return 0;
  319. }
  320. static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  321. {
  322. int status;
  323. spin_lock(&dev->req_lock);
  324. status = prealloc(&dev->tx_reqs, link->in_ep, n);
  325. if (status < 0)
  326. goto fail;
  327. status = prealloc(&dev->rx_reqs, link->out_ep, n);
  328. if (status < 0)
  329. goto fail;
  330. goto done;
  331. fail:
  332. DBG(dev, "can't alloc requests\n");
  333. done:
  334. spin_unlock(&dev->req_lock);
  335. return status;
  336. }
  337. static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
  338. {
  339. struct usb_request *req;
  340. unsigned long flags;
  341. /* fill unused rxq slots with some skb */
  342. spin_lock_irqsave(&dev->req_lock, flags);
  343. while (!list_empty(&dev->rx_reqs)) {
  344. req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
  345. list_del_init(&req->list);
  346. spin_unlock_irqrestore(&dev->req_lock, flags);
  347. if (rx_submit(dev, req, gfp_flags) < 0) {
  348. defer_kevent(dev, WORK_RX_MEMORY);
  349. return;
  350. }
  351. spin_lock_irqsave(&dev->req_lock, flags);
  352. }
  353. spin_unlock_irqrestore(&dev->req_lock, flags);
  354. }
  355. static void eth_work(struct work_struct *work)
  356. {
  357. struct eth_dev *dev = container_of(work, struct eth_dev, work);
  358. if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
  359. if (netif_running(dev->net))
  360. rx_fill(dev, GFP_KERNEL);
  361. }
  362. if (dev->todo)
  363. DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
  364. }
  365. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  366. {
  367. struct sk_buff *skb = req->context;
  368. struct eth_dev *dev = ep->driver_data;
  369. switch (req->status) {
  370. default:
  371. dev->net->stats.tx_errors++;
  372. VDBG(dev, "tx err %d\n", req->status);
  373. /* FALLTHROUGH */
  374. case -ECONNRESET: /* unlink */
  375. case -ESHUTDOWN: /* disconnect etc */
  376. dev_kfree_skb_any(skb);
  377. break;
  378. case 0:
  379. dev->net->stats.tx_bytes += skb->len;
  380. dev_consume_skb_any(skb);
  381. }
  382. dev->net->stats.tx_packets++;
  383. spin_lock(&dev->req_lock);
  384. list_add(&req->list, &dev->tx_reqs);
  385. spin_unlock(&dev->req_lock);
  386. atomic_dec(&dev->tx_qlen);
  387. if (netif_carrier_ok(dev->net))
  388. netif_wake_queue(dev->net);
  389. }
  390. static inline int is_promisc(u16 cdc_filter)
  391. {
  392. return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  393. }
  394. static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
  395. struct net_device *net)
  396. {
  397. struct eth_dev *dev = netdev_priv(net);
  398. int length = 0;
  399. int retval;
  400. struct usb_request *req = NULL;
  401. unsigned long flags;
  402. struct usb_ep *in;
  403. u16 cdc_filter;
  404. spin_lock_irqsave(&dev->lock, flags);
  405. if (dev->port_usb) {
  406. in = dev->port_usb->in_ep;
  407. cdc_filter = dev->port_usb->cdc_filter;
  408. } else {
  409. in = NULL;
  410. cdc_filter = 0;
  411. }
  412. spin_unlock_irqrestore(&dev->lock, flags);
  413. if (skb && !in) {
  414. dev_kfree_skb_any(skb);
  415. return NETDEV_TX_OK;
  416. }
  417. /* apply outgoing CDC or RNDIS filters */
  418. if (skb && !is_promisc(cdc_filter)) {
  419. u8 *dest = skb->data;
  420. if (is_multicast_ether_addr(dest)) {
  421. u16 type;
  422. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  423. * SET_ETHERNET_MULTICAST_FILTERS requests
  424. */
  425. if (is_broadcast_ether_addr(dest))
  426. type = USB_CDC_PACKET_TYPE_BROADCAST;
  427. else
  428. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  429. if (!(cdc_filter & type)) {
  430. dev_kfree_skb_any(skb);
  431. return NETDEV_TX_OK;
  432. }
  433. }
  434. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  435. }
  436. spin_lock_irqsave(&dev->req_lock, flags);
  437. /*
  438. * this freelist can be empty if an interrupt triggered disconnect()
  439. * and reconfigured the gadget (shutting down this queue) after the
  440. * network stack decided to xmit but before we got the spinlock.
  441. */
  442. if (list_empty(&dev->tx_reqs)) {
  443. spin_unlock_irqrestore(&dev->req_lock, flags);
  444. return NETDEV_TX_BUSY;
  445. }
  446. req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
  447. list_del(&req->list);
  448. /* temporarily stop TX queue when the freelist empties */
  449. if (list_empty(&dev->tx_reqs))
  450. netif_stop_queue(net);
  451. spin_unlock_irqrestore(&dev->req_lock, flags);
  452. /* no buffer copies needed, unless the network stack did it
  453. * or the hardware can't use skb buffers.
  454. * or there's not enough space for extra headers we need
  455. */
  456. if (dev->wrap) {
  457. unsigned long flags;
  458. spin_lock_irqsave(&dev->lock, flags);
  459. if (dev->port_usb)
  460. skb = dev->wrap(dev->port_usb, skb);
  461. spin_unlock_irqrestore(&dev->lock, flags);
  462. if (!skb) {
  463. /* Multi frame CDC protocols may store the frame for
  464. * later which is not a dropped frame.
  465. */
  466. if (dev->port_usb &&
  467. dev->port_usb->supports_multi_frame)
  468. goto multiframe;
  469. goto drop;
  470. }
  471. }
  472. length = skb->len;
  473. req->buf = skb->data;
  474. req->context = skb;
  475. req->complete = tx_complete;
  476. /* NCM requires no zlp if transfer is dwNtbInMaxSize */
  477. if (dev->port_usb &&
  478. dev->port_usb->is_fixed &&
  479. length == dev->port_usb->fixed_in_len &&
  480. (length % in->maxpacket) == 0)
  481. req->zero = 0;
  482. else
  483. req->zero = 1;
  484. /* use zlp framing on tx for strict CDC-Ether conformance,
  485. * though any robust network rx path ignores extra padding.
  486. * and some hardware doesn't like to write zlps.
  487. */
  488. if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
  489. length++;
  490. req->length = length;
  491. retval = usb_ep_queue(in, req, GFP_ATOMIC);
  492. switch (retval) {
  493. default:
  494. DBG(dev, "tx queue err %d\n", retval);
  495. break;
  496. case 0:
  497. netif_trans_update(net);
  498. atomic_inc(&dev->tx_qlen);
  499. }
  500. if (retval) {
  501. dev_kfree_skb_any(skb);
  502. drop:
  503. dev->net->stats.tx_dropped++;
  504. multiframe:
  505. spin_lock_irqsave(&dev->req_lock, flags);
  506. if (list_empty(&dev->tx_reqs))
  507. netif_start_queue(net);
  508. list_add(&req->list, &dev->tx_reqs);
  509. spin_unlock_irqrestore(&dev->req_lock, flags);
  510. }
  511. return NETDEV_TX_OK;
  512. }
  513. /*-------------------------------------------------------------------------*/
  514. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
  515. {
  516. DBG(dev, "%s\n", __func__);
  517. /* fill the rx queue */
  518. rx_fill(dev, gfp_flags);
  519. /* and open the tx floodgates */
  520. atomic_set(&dev->tx_qlen, 0);
  521. netif_wake_queue(dev->net);
  522. }
  523. static int eth_open(struct net_device *net)
  524. {
  525. struct eth_dev *dev = netdev_priv(net);
  526. struct gether *link;
  527. DBG(dev, "%s\n", __func__);
  528. if (netif_carrier_ok(dev->net))
  529. eth_start(dev, GFP_KERNEL);
  530. spin_lock_irq(&dev->lock);
  531. link = dev->port_usb;
  532. if (link && link->open)
  533. link->open(link);
  534. spin_unlock_irq(&dev->lock);
  535. return 0;
  536. }
  537. static int eth_stop(struct net_device *net)
  538. {
  539. struct eth_dev *dev = netdev_priv(net);
  540. unsigned long flags;
  541. VDBG(dev, "%s\n", __func__);
  542. netif_stop_queue(net);
  543. DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
  544. dev->net->stats.rx_packets, dev->net->stats.tx_packets,
  545. dev->net->stats.rx_errors, dev->net->stats.tx_errors
  546. );
  547. /* ensure there are no more active requests */
  548. spin_lock_irqsave(&dev->lock, flags);
  549. if (dev->port_usb) {
  550. struct gether *link = dev->port_usb;
  551. const struct usb_endpoint_descriptor *in;
  552. const struct usb_endpoint_descriptor *out;
  553. if (link->close)
  554. link->close(link);
  555. /* NOTE: we have no abort-queue primitive we could use
  556. * to cancel all pending I/O. Instead, we disable then
  557. * reenable the endpoints ... this idiom may leave toggle
  558. * wrong, but that's a self-correcting error.
  559. *
  560. * REVISIT: we *COULD* just let the transfers complete at
  561. * their own pace; the network stack can handle old packets.
  562. * For the moment we leave this here, since it works.
  563. */
  564. in = link->in_ep->desc;
  565. out = link->out_ep->desc;
  566. usb_ep_disable(link->in_ep);
  567. usb_ep_disable(link->out_ep);
  568. if (netif_carrier_ok(net)) {
  569. DBG(dev, "host still using in/out endpoints\n");
  570. link->in_ep->desc = in;
  571. link->out_ep->desc = out;
  572. usb_ep_enable(link->in_ep);
  573. usb_ep_enable(link->out_ep);
  574. }
  575. }
  576. spin_unlock_irqrestore(&dev->lock, flags);
  577. return 0;
  578. }
  579. /*-------------------------------------------------------------------------*/
  580. static int get_ether_addr(const char *str, u8 *dev_addr)
  581. {
  582. if (str) {
  583. unsigned i;
  584. for (i = 0; i < 6; i++) {
  585. unsigned char num;
  586. if ((*str == '.') || (*str == ':'))
  587. str++;
  588. num = hex_to_bin(*str++) << 4;
  589. num |= hex_to_bin(*str++);
  590. dev_addr [i] = num;
  591. }
  592. if (is_valid_ether_addr(dev_addr))
  593. return 0;
  594. }
  595. eth_random_addr(dev_addr);
  596. return 1;
  597. }
  598. static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
  599. {
  600. if (len < 18)
  601. return -EINVAL;
  602. snprintf(str, len, "%pM", dev_addr);
  603. return 18;
  604. }
  605. static const struct net_device_ops eth_netdev_ops = {
  606. .ndo_open = eth_open,
  607. .ndo_stop = eth_stop,
  608. .ndo_start_xmit = eth_start_xmit,
  609. .ndo_set_mac_address = eth_mac_addr,
  610. .ndo_validate_addr = eth_validate_addr,
  611. };
  612. static struct device_type gadget_type = {
  613. .name = "gadget",
  614. };
  615. /**
  616. * gether_setup_name - initialize one ethernet-over-usb link
  617. * @g: gadget to associated with these links
  618. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  619. * host side of the link is recorded
  620. * @netname: name for network device (for example, "usb")
  621. * Context: may sleep
  622. *
  623. * This sets up the single network link that may be exported by a
  624. * gadget driver using this framework. The link layer addresses are
  625. * set up using module parameters.
  626. *
  627. * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
  628. */
  629. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  630. const char *dev_addr, const char *host_addr,
  631. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
  632. {
  633. struct eth_dev *dev;
  634. struct net_device *net;
  635. int status;
  636. net = alloc_etherdev(sizeof *dev);
  637. if (!net)
  638. return ERR_PTR(-ENOMEM);
  639. dev = netdev_priv(net);
  640. spin_lock_init(&dev->lock);
  641. spin_lock_init(&dev->req_lock);
  642. INIT_WORK(&dev->work, eth_work);
  643. INIT_LIST_HEAD(&dev->tx_reqs);
  644. INIT_LIST_HEAD(&dev->rx_reqs);
  645. skb_queue_head_init(&dev->rx_frames);
  646. /* network device setup */
  647. dev->net = net;
  648. dev->qmult = qmult;
  649. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  650. if (get_ether_addr(dev_addr, net->dev_addr))
  651. dev_warn(&g->dev,
  652. "using random %s ethernet address\n", "self");
  653. if (get_ether_addr(host_addr, dev->host_mac))
  654. dev_warn(&g->dev,
  655. "using random %s ethernet address\n", "host");
  656. if (ethaddr)
  657. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  658. net->netdev_ops = &eth_netdev_ops;
  659. net->ethtool_ops = &ops;
  660. /* MTU range: 14 - 15412 */
  661. net->min_mtu = ETH_HLEN;
  662. net->max_mtu = GETHER_MAX_MTU_SIZE;
  663. dev->gadget = g;
  664. SET_NETDEV_DEV(net, &g->dev);
  665. SET_NETDEV_DEVTYPE(net, &gadget_type);
  666. status = register_netdev(net);
  667. if (status < 0) {
  668. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  669. free_netdev(net);
  670. dev = ERR_PTR(status);
  671. } else {
  672. INFO(dev, "MAC %pM\n", net->dev_addr);
  673. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  674. /*
  675. * two kinds of host-initiated state changes:
  676. * - iff DATA transfer is active, carrier is "on"
  677. * - tx queueing enabled if open *and* carrier is "on"
  678. */
  679. netif_carrier_off(net);
  680. }
  681. return dev;
  682. }
  683. EXPORT_SYMBOL_GPL(gether_setup_name);
  684. struct net_device *gether_setup_name_default(const char *netname)
  685. {
  686. struct net_device *net;
  687. struct eth_dev *dev;
  688. net = alloc_etherdev(sizeof(*dev));
  689. if (!net)
  690. return ERR_PTR(-ENOMEM);
  691. dev = netdev_priv(net);
  692. spin_lock_init(&dev->lock);
  693. spin_lock_init(&dev->req_lock);
  694. INIT_WORK(&dev->work, eth_work);
  695. INIT_LIST_HEAD(&dev->tx_reqs);
  696. INIT_LIST_HEAD(&dev->rx_reqs);
  697. skb_queue_head_init(&dev->rx_frames);
  698. /* network device setup */
  699. dev->net = net;
  700. dev->qmult = QMULT_DEFAULT;
  701. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  702. eth_random_addr(dev->dev_mac);
  703. pr_warn("using random %s ethernet address\n", "self");
  704. eth_random_addr(dev->host_mac);
  705. pr_warn("using random %s ethernet address\n", "host");
  706. net->netdev_ops = &eth_netdev_ops;
  707. net->ethtool_ops = &ops;
  708. SET_NETDEV_DEVTYPE(net, &gadget_type);
  709. /* MTU range: 14 - 15412 */
  710. net->min_mtu = ETH_HLEN;
  711. net->max_mtu = GETHER_MAX_MTU_SIZE;
  712. return net;
  713. }
  714. EXPORT_SYMBOL_GPL(gether_setup_name_default);
  715. int gether_register_netdev(struct net_device *net)
  716. {
  717. struct eth_dev *dev;
  718. struct usb_gadget *g;
  719. struct sockaddr sa;
  720. int status;
  721. if (!net->dev.parent)
  722. return -EINVAL;
  723. dev = netdev_priv(net);
  724. g = dev->gadget;
  725. status = register_netdev(net);
  726. if (status < 0) {
  727. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  728. return status;
  729. } else {
  730. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  731. /* two kinds of host-initiated state changes:
  732. * - iff DATA transfer is active, carrier is "on"
  733. * - tx queueing enabled if open *and* carrier is "on"
  734. */
  735. netif_carrier_off(net);
  736. }
  737. sa.sa_family = net->type;
  738. memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
  739. rtnl_lock();
  740. status = dev_set_mac_address(net, &sa);
  741. rtnl_unlock();
  742. if (status)
  743. pr_warn("cannot set self ethernet address: %d\n", status);
  744. else
  745. INFO(dev, "MAC %pM\n", dev->dev_mac);
  746. return status;
  747. }
  748. EXPORT_SYMBOL_GPL(gether_register_netdev);
  749. void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
  750. {
  751. struct eth_dev *dev;
  752. dev = netdev_priv(net);
  753. dev->gadget = g;
  754. SET_NETDEV_DEV(net, &g->dev);
  755. }
  756. EXPORT_SYMBOL_GPL(gether_set_gadget);
  757. int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
  758. {
  759. struct eth_dev *dev;
  760. u8 new_addr[ETH_ALEN];
  761. dev = netdev_priv(net);
  762. if (get_ether_addr(dev_addr, new_addr))
  763. return -EINVAL;
  764. memcpy(dev->dev_mac, new_addr, ETH_ALEN);
  765. return 0;
  766. }
  767. EXPORT_SYMBOL_GPL(gether_set_dev_addr);
  768. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
  769. {
  770. struct eth_dev *dev;
  771. int ret;
  772. dev = netdev_priv(net);
  773. ret = get_ether_addr_str(dev->dev_mac, dev_addr, len);
  774. if (ret + 1 < len) {
  775. dev_addr[ret++] = '\n';
  776. dev_addr[ret] = '\0';
  777. }
  778. return ret;
  779. }
  780. EXPORT_SYMBOL_GPL(gether_get_dev_addr);
  781. int gether_set_host_addr(struct net_device *net, const char *host_addr)
  782. {
  783. struct eth_dev *dev;
  784. u8 new_addr[ETH_ALEN];
  785. dev = netdev_priv(net);
  786. if (get_ether_addr(host_addr, new_addr))
  787. return -EINVAL;
  788. memcpy(dev->host_mac, new_addr, ETH_ALEN);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL_GPL(gether_set_host_addr);
  792. int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
  793. {
  794. struct eth_dev *dev;
  795. int ret;
  796. dev = netdev_priv(net);
  797. ret = get_ether_addr_str(dev->host_mac, host_addr, len);
  798. if (ret + 1 < len) {
  799. host_addr[ret++] = '\n';
  800. host_addr[ret] = '\0';
  801. }
  802. return ret;
  803. }
  804. EXPORT_SYMBOL_GPL(gether_get_host_addr);
  805. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
  806. {
  807. struct eth_dev *dev;
  808. if (len < 13)
  809. return -EINVAL;
  810. dev = netdev_priv(net);
  811. snprintf(host_addr, len, "%pm", dev->host_mac);
  812. return strlen(host_addr);
  813. }
  814. EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
  815. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
  816. {
  817. struct eth_dev *dev;
  818. dev = netdev_priv(net);
  819. memcpy(host_mac, dev->host_mac, ETH_ALEN);
  820. }
  821. EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
  822. void gether_set_qmult(struct net_device *net, unsigned qmult)
  823. {
  824. struct eth_dev *dev;
  825. dev = netdev_priv(net);
  826. dev->qmult = qmult;
  827. }
  828. EXPORT_SYMBOL_GPL(gether_set_qmult);
  829. unsigned gether_get_qmult(struct net_device *net)
  830. {
  831. struct eth_dev *dev;
  832. dev = netdev_priv(net);
  833. return dev->qmult;
  834. }
  835. EXPORT_SYMBOL_GPL(gether_get_qmult);
  836. int gether_get_ifname(struct net_device *net, char *name, int len)
  837. {
  838. int ret;
  839. rtnl_lock();
  840. ret = snprintf(name, len, "%s\n", netdev_name(net));
  841. rtnl_unlock();
  842. return ret < len ? ret : len;
  843. }
  844. EXPORT_SYMBOL_GPL(gether_get_ifname);
  845. /**
  846. * gether_cleanup - remove Ethernet-over-USB device
  847. * Context: may sleep
  848. *
  849. * This is called to free all resources allocated by @gether_setup().
  850. */
  851. void gether_cleanup(struct eth_dev *dev)
  852. {
  853. if (!dev)
  854. return;
  855. unregister_netdev(dev->net);
  856. flush_work(&dev->work);
  857. free_netdev(dev->net);
  858. }
  859. EXPORT_SYMBOL_GPL(gether_cleanup);
  860. /**
  861. * gether_connect - notify network layer that USB link is active
  862. * @link: the USB link, set up with endpoints, descriptors matching
  863. * current device speed, and any framing wrapper(s) set up.
  864. * Context: irqs blocked
  865. *
  866. * This is called to activate endpoints and let the network layer know
  867. * the connection is active ("carrier detect"). It may cause the I/O
  868. * queues to open and start letting network packets flow, but will in
  869. * any case activate the endpoints so that they respond properly to the
  870. * USB host.
  871. *
  872. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  873. * indicate some error code (negative errno), ep->driver_data values
  874. * have been overwritten.
  875. */
  876. struct net_device *gether_connect(struct gether *link)
  877. {
  878. struct eth_dev *dev = link->ioport;
  879. int result = 0;
  880. if (!dev)
  881. return ERR_PTR(-EINVAL);
  882. link->in_ep->driver_data = dev;
  883. result = usb_ep_enable(link->in_ep);
  884. if (result != 0) {
  885. DBG(dev, "enable %s --> %d\n",
  886. link->in_ep->name, result);
  887. goto fail0;
  888. }
  889. link->out_ep->driver_data = dev;
  890. result = usb_ep_enable(link->out_ep);
  891. if (result != 0) {
  892. DBG(dev, "enable %s --> %d\n",
  893. link->out_ep->name, result);
  894. goto fail1;
  895. }
  896. if (result == 0)
  897. result = alloc_requests(dev, link, qlen(dev->gadget,
  898. dev->qmult));
  899. if (result == 0) {
  900. dev->zlp = link->is_zlp_ok;
  901. dev->no_skb_reserve = gadget_avoids_skb_reserve(dev->gadget);
  902. DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
  903. dev->header_len = link->header_len;
  904. dev->unwrap = link->unwrap;
  905. dev->wrap = link->wrap;
  906. spin_lock(&dev->lock);
  907. dev->port_usb = link;
  908. if (netif_running(dev->net)) {
  909. if (link->open)
  910. link->open(link);
  911. } else {
  912. if (link->close)
  913. link->close(link);
  914. }
  915. spin_unlock(&dev->lock);
  916. netif_carrier_on(dev->net);
  917. if (netif_running(dev->net))
  918. eth_start(dev, GFP_ATOMIC);
  919. /* on error, disable any endpoints */
  920. } else {
  921. (void) usb_ep_disable(link->out_ep);
  922. fail1:
  923. (void) usb_ep_disable(link->in_ep);
  924. }
  925. fail0:
  926. /* caller is responsible for cleanup on error */
  927. if (result < 0)
  928. return ERR_PTR(result);
  929. return dev->net;
  930. }
  931. EXPORT_SYMBOL_GPL(gether_connect);
  932. /**
  933. * gether_disconnect - notify network layer that USB link is inactive
  934. * @link: the USB link, on which gether_connect() was called
  935. * Context: irqs blocked
  936. *
  937. * This is called to deactivate endpoints and let the network layer know
  938. * the connection went inactive ("no carrier").
  939. *
  940. * On return, the state is as if gether_connect() had never been called.
  941. * The endpoints are inactive, and accordingly without active USB I/O.
  942. * Pointers to endpoint descriptors and endpoint private data are nulled.
  943. */
  944. void gether_disconnect(struct gether *link)
  945. {
  946. struct eth_dev *dev = link->ioport;
  947. struct usb_request *req;
  948. WARN_ON(!dev);
  949. if (!dev)
  950. return;
  951. DBG(dev, "%s\n", __func__);
  952. netif_stop_queue(dev->net);
  953. netif_carrier_off(dev->net);
  954. /* disable endpoints, forcing (synchronous) completion
  955. * of all pending i/o. then free the request objects
  956. * and forget about the endpoints.
  957. */
  958. usb_ep_disable(link->in_ep);
  959. spin_lock(&dev->req_lock);
  960. while (!list_empty(&dev->tx_reqs)) {
  961. req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
  962. list_del(&req->list);
  963. spin_unlock(&dev->req_lock);
  964. usb_ep_free_request(link->in_ep, req);
  965. spin_lock(&dev->req_lock);
  966. }
  967. spin_unlock(&dev->req_lock);
  968. link->in_ep->desc = NULL;
  969. usb_ep_disable(link->out_ep);
  970. spin_lock(&dev->req_lock);
  971. while (!list_empty(&dev->rx_reqs)) {
  972. req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
  973. list_del(&req->list);
  974. spin_unlock(&dev->req_lock);
  975. usb_ep_free_request(link->out_ep, req);
  976. spin_lock(&dev->req_lock);
  977. }
  978. spin_unlock(&dev->req_lock);
  979. link->out_ep->desc = NULL;
  980. /* finish forgetting about this USB link episode */
  981. dev->header_len = 0;
  982. dev->unwrap = NULL;
  983. dev->wrap = NULL;
  984. spin_lock(&dev->lock);
  985. dev->port_usb = NULL;
  986. spin_unlock(&dev->lock);
  987. }
  988. EXPORT_SYMBOL_GPL(gether_disconnect);
  989. MODULE_LICENSE("GPL");
  990. MODULE_AUTHOR("David Brownell");