u_ether.c 24 KB

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