u_ether.c 25 KB

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