u_ether.c 25 KB

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