u_ether.c 29 KB

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