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