u_ether.c 29 KB

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