u_ether.c 29 KB

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