u_ether.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6. * Copyright (C) 2008 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. /* #define VERBOSE_DEBUG */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/gfp.h>
  17. #include <linux/device.h>
  18. #include <linux/ctype.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/if_vlan.h>
  22. #include "u_ether.h"
  23. /*
  24. * This component encapsulates the Ethernet link glue needed to provide
  25. * one (!) network link through the USB gadget stack, normally "usb0".
  26. *
  27. * The control and data models are handled by the function driver which
  28. * connects to this code; such as CDC Ethernet (ECM or EEM),
  29. * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
  30. * management.
  31. *
  32. * Link level addressing is handled by this component using module
  33. * parameters; if no such parameters are provided, random link level
  34. * addresses are used. Each end of the link uses one address. The
  35. * host end address is exported in various ways, and is often recorded
  36. * in configuration databases.
  37. *
  38. * The driver which assembles each configuration using such a link is
  39. * responsible for ensuring that each configuration includes at most one
  40. * instance of is network link. (The network layer provides ways for
  41. * this single "physical" link to be used by multiple virtual links.)
  42. */
  43. #define UETH__VERSION "29-May-2008"
  44. /* Experiments show that both Linux and Windows hosts allow up to 16k
  45. * frame sizes. Set the max size to 15k+52 to prevent allocating 32k
  46. * blocks and still have efficient handling. */
  47. #define GETHER_MAX_ETH_FRAME_LEN 15412
  48. struct eth_dev {
  49. /* lock is held while accessing port_usb
  50. */
  51. spinlock_t lock;
  52. struct gether *port_usb;
  53. struct net_device *net;
  54. struct usb_gadget *gadget;
  55. spinlock_t req_lock; /* guard {rx,tx}_reqs */
  56. struct list_head tx_reqs, rx_reqs;
  57. atomic_t tx_qlen;
  58. struct sk_buff_head rx_frames;
  59. unsigned qmult;
  60. unsigned header_len;
  61. struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
  62. int (*unwrap)(struct gether *,
  63. struct sk_buff *skb,
  64. struct sk_buff_head *list);
  65. struct work_struct work;
  66. unsigned long todo;
  67. #define WORK_RX_MEMORY 0
  68. bool zlp;
  69. u8 host_mac[ETH_ALEN];
  70. u8 dev_mac[ETH_ALEN];
  71. };
  72. /*-------------------------------------------------------------------------*/
  73. #define RX_EXTRA 20 /* bytes guarding against rx overflows */
  74. #define DEFAULT_QLEN 2 /* double buffering by default */
  75. /* for dual-speed hardware, use deeper queues at high/super speed */
  76. static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
  77. {
  78. if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
  79. gadget->speed == USB_SPEED_SUPER))
  80. return qmult * DEFAULT_QLEN;
  81. else
  82. return DEFAULT_QLEN;
  83. }
  84. /*-------------------------------------------------------------------------*/
  85. /* REVISIT there must be a better way than having two sets
  86. * of debug calls ...
  87. */
  88. #undef DBG
  89. #undef VDBG
  90. #undef ERROR
  91. #undef INFO
  92. #define xprintk(d, level, fmt, args...) \
  93. printk(level "%s: " fmt , (d)->net->name , ## args)
  94. #ifdef DEBUG
  95. #undef DEBUG
  96. #define DBG(dev, fmt, args...) \
  97. xprintk(dev , KERN_DEBUG , fmt , ## args)
  98. #else
  99. #define DBG(dev, fmt, args...) \
  100. do { } while (0)
  101. #endif /* DEBUG */
  102. #ifdef VERBOSE_DEBUG
  103. #define VDBG DBG
  104. #else
  105. #define VDBG(dev, fmt, args...) \
  106. do { } while (0)
  107. #endif /* DEBUG */
  108. #define ERROR(dev, fmt, args...) \
  109. xprintk(dev , KERN_ERR , fmt , ## args)
  110. #define INFO(dev, fmt, args...) \
  111. xprintk(dev , KERN_INFO , fmt , ## args)
  112. /*-------------------------------------------------------------------------*/
  113. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  114. static int ueth_change_mtu(struct net_device *net, int new_mtu)
  115. {
  116. if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
  117. return -ERANGE;
  118. net->mtu = new_mtu;
  119. return 0;
  120. }
  121. static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
  122. {
  123. struct eth_dev *dev = netdev_priv(net);
  124. strlcpy(p->driver, "g_ether", sizeof(p->driver));
  125. strlcpy(p->version, UETH__VERSION, sizeof(p->version));
  126. strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
  127. strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
  128. }
  129. /* REVISIT can also support:
  130. * - WOL (by tracking suspends and issuing remote wakeup)
  131. * - msglevel (implies updated messaging)
  132. * - ... probably more ethtool ops
  133. */
  134. static const struct ethtool_ops ops = {
  135. .get_drvinfo = eth_get_drvinfo,
  136. .get_link = ethtool_op_get_link,
  137. };
  138. static void defer_kevent(struct eth_dev *dev, int flag)
  139. {
  140. if (test_and_set_bit(flag, &dev->todo))
  141. return;
  142. if (!schedule_work(&dev->work))
  143. ERROR(dev, "kevent %d may have been dropped\n", flag);
  144. else
  145. DBG(dev, "kevent %d scheduled\n", flag);
  146. }
  147. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  148. static int
  149. rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
  150. {
  151. struct sk_buff *skb;
  152. int retval = -ENOMEM;
  153. size_t size = 0;
  154. struct usb_ep *out;
  155. unsigned long flags;
  156. spin_lock_irqsave(&dev->lock, flags);
  157. if (dev->port_usb)
  158. out = dev->port_usb->out_ep;
  159. else
  160. out = NULL;
  161. spin_unlock_irqrestore(&dev->lock, flags);
  162. if (!out)
  163. return -ENOTCONN;
  164. /* Padding up to RX_EXTRA handles minor disagreements with host.
  165. * Normally we use the USB "terminate on short read" convention;
  166. * so allow up to (N*maxpacket), since that memory is normally
  167. * already allocated. Some hardware doesn't deal well with short
  168. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  169. * byte off the end (to force hardware errors on overflow).
  170. *
  171. * RNDIS uses internal framing, and explicitly allows senders to
  172. * pad to end-of-packet. That's potentially nice for speed, but
  173. * means receivers can't recover lost synch on their own (because
  174. * new packets don't only start after a short RX).
  175. */
  176. size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
  177. size += dev->port_usb->header_len;
  178. size += out->maxpacket - 1;
  179. size -= size % out->maxpacket;
  180. if (dev->port_usb->is_fixed)
  181. size = max_t(size_t, size, dev->port_usb->fixed_out_len);
  182. skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
  183. if (skb == NULL) {
  184. DBG(dev, "no rx skb\n");
  185. goto enomem;
  186. }
  187. /* Some platforms perform better when IP packets are aligned,
  188. * but on at least one, checksumming fails otherwise. Note:
  189. * RNDIS headers involve variable numbers of LE32 values.
  190. */
  191. skb_reserve(skb, NET_IP_ALIGN);
  192. req->buf = skb->data;
  193. req->length = size;
  194. req->complete = rx_complete;
  195. req->context = skb;
  196. retval = usb_ep_queue(out, req, gfp_flags);
  197. if (retval == -ENOMEM)
  198. enomem:
  199. defer_kevent(dev, WORK_RX_MEMORY);
  200. if (retval) {
  201. DBG(dev, "rx submit --> %d\n", retval);
  202. if (skb)
  203. dev_kfree_skb_any(skb);
  204. spin_lock_irqsave(&dev->req_lock, flags);
  205. list_add(&req->list, &dev->rx_reqs);
  206. spin_unlock_irqrestore(&dev->req_lock, flags);
  207. }
  208. return retval;
  209. }
  210. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  211. {
  212. struct sk_buff *skb = req->context, *skb2;
  213. struct eth_dev *dev = ep->driver_data;
  214. int status = req->status;
  215. switch (status) {
  216. /* normal completion */
  217. case 0:
  218. skb_put(skb, req->actual);
  219. if (dev->unwrap) {
  220. unsigned long flags;
  221. spin_lock_irqsave(&dev->lock, flags);
  222. if (dev->port_usb) {
  223. status = dev->unwrap(dev->port_usb,
  224. skb,
  225. &dev->rx_frames);
  226. } else {
  227. dev_kfree_skb_any(skb);
  228. status = -ENOTCONN;
  229. }
  230. spin_unlock_irqrestore(&dev->lock, flags);
  231. } else {
  232. skb_queue_tail(&dev->rx_frames, skb);
  233. }
  234. skb = NULL;
  235. skb2 = skb_dequeue(&dev->rx_frames);
  236. while (skb2) {
  237. if (status < 0
  238. || ETH_HLEN > skb2->len
  239. || skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
  240. dev->net->stats.rx_errors++;
  241. dev->net->stats.rx_length_errors++;
  242. DBG(dev, "rx length %d\n", skb2->len);
  243. dev_kfree_skb_any(skb2);
  244. goto next_frame;
  245. }
  246. skb2->protocol = eth_type_trans(skb2, dev->net);
  247. dev->net->stats.rx_packets++;
  248. dev->net->stats.rx_bytes += skb2->len;
  249. /* no buffer copies needed, unless hardware can't
  250. * use skb buffers.
  251. */
  252. status = netif_rx(skb2);
  253. next_frame:
  254. skb2 = skb_dequeue(&dev->rx_frames);
  255. }
  256. break;
  257. /* software-driven interface shutdown */
  258. case -ECONNRESET: /* unlink */
  259. case -ESHUTDOWN: /* disconnect etc */
  260. VDBG(dev, "rx shutdown, code %d\n", status);
  261. goto quiesce;
  262. /* for hardware automagic (such as pxa) */
  263. case -ECONNABORTED: /* endpoint reset */
  264. DBG(dev, "rx %s reset\n", ep->name);
  265. defer_kevent(dev, WORK_RX_MEMORY);
  266. quiesce:
  267. dev_kfree_skb_any(skb);
  268. goto clean;
  269. /* data overrun */
  270. case -EOVERFLOW:
  271. dev->net->stats.rx_over_errors++;
  272. /* FALLTHROUGH */
  273. default:
  274. dev->net->stats.rx_errors++;
  275. DBG(dev, "rx status %d\n", status);
  276. break;
  277. }
  278. if (skb)
  279. dev_kfree_skb_any(skb);
  280. if (!netif_running(dev->net)) {
  281. clean:
  282. spin_lock(&dev->req_lock);
  283. list_add(&req->list, &dev->rx_reqs);
  284. spin_unlock(&dev->req_lock);
  285. req = NULL;
  286. }
  287. if (req)
  288. rx_submit(dev, req, GFP_ATOMIC);
  289. }
  290. static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
  291. {
  292. unsigned i;
  293. struct usb_request *req;
  294. if (!n)
  295. return -ENOMEM;
  296. /* queue/recycle up to N requests */
  297. i = n;
  298. list_for_each_entry(req, list, list) {
  299. if (i-- == 0)
  300. goto extra;
  301. }
  302. while (i--) {
  303. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  304. if (!req)
  305. return list_empty(list) ? -ENOMEM : 0;
  306. list_add(&req->list, list);
  307. }
  308. return 0;
  309. extra:
  310. /* free extras */
  311. for (;;) {
  312. struct list_head *next;
  313. next = req->list.next;
  314. list_del(&req->list);
  315. usb_ep_free_request(ep, req);
  316. if (next == list)
  317. break;
  318. req = container_of(next, struct usb_request, list);
  319. }
  320. return 0;
  321. }
  322. static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  323. {
  324. int status;
  325. spin_lock(&dev->req_lock);
  326. status = prealloc(&dev->tx_reqs, link->in_ep, n);
  327. if (status < 0)
  328. goto fail;
  329. status = prealloc(&dev->rx_reqs, link->out_ep, n);
  330. if (status < 0)
  331. goto fail;
  332. goto done;
  333. fail:
  334. DBG(dev, "can't alloc requests\n");
  335. done:
  336. spin_unlock(&dev->req_lock);
  337. return status;
  338. }
  339. static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
  340. {
  341. struct usb_request *req;
  342. unsigned long flags;
  343. /* fill unused rxq slots with some skb */
  344. spin_lock_irqsave(&dev->req_lock, flags);
  345. while (!list_empty(&dev->rx_reqs)) {
  346. req = container_of(dev->rx_reqs.next,
  347. struct usb_request, list);
  348. list_del_init(&req->list);
  349. spin_unlock_irqrestore(&dev->req_lock, flags);
  350. if (rx_submit(dev, req, gfp_flags) < 0) {
  351. defer_kevent(dev, WORK_RX_MEMORY);
  352. return;
  353. }
  354. spin_lock_irqsave(&dev->req_lock, flags);
  355. }
  356. spin_unlock_irqrestore(&dev->req_lock, flags);
  357. }
  358. static void eth_work(struct work_struct *work)
  359. {
  360. struct eth_dev *dev = container_of(work, struct eth_dev, work);
  361. if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
  362. if (netif_running(dev->net))
  363. rx_fill(dev, GFP_KERNEL);
  364. }
  365. if (dev->todo)
  366. DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
  367. }
  368. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  369. {
  370. struct sk_buff *skb = req->context;
  371. struct eth_dev *dev = ep->driver_data;
  372. switch (req->status) {
  373. default:
  374. dev->net->stats.tx_errors++;
  375. VDBG(dev, "tx err %d\n", req->status);
  376. /* FALLTHROUGH */
  377. case -ECONNRESET: /* unlink */
  378. case -ESHUTDOWN: /* disconnect etc */
  379. break;
  380. case 0:
  381. dev->net->stats.tx_bytes += skb->len;
  382. }
  383. dev->net->stats.tx_packets++;
  384. spin_lock(&dev->req_lock);
  385. list_add(&req->list, &dev->tx_reqs);
  386. spin_unlock(&dev->req_lock);
  387. dev_kfree_skb_any(skb);
  388. atomic_dec(&dev->tx_qlen);
  389. if (netif_carrier_ok(dev->net))
  390. netif_wake_queue(dev->net);
  391. }
  392. static inline int is_promisc(u16 cdc_filter)
  393. {
  394. return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  395. }
  396. static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
  397. struct net_device *net)
  398. {
  399. struct eth_dev *dev = netdev_priv(net);
  400. int length = 0;
  401. int retval;
  402. struct usb_request *req = NULL;
  403. unsigned long flags;
  404. struct usb_ep *in;
  405. u16 cdc_filter;
  406. spin_lock_irqsave(&dev->lock, flags);
  407. if (dev->port_usb) {
  408. in = dev->port_usb->in_ep;
  409. cdc_filter = dev->port_usb->cdc_filter;
  410. } else {
  411. in = NULL;
  412. cdc_filter = 0;
  413. }
  414. spin_unlock_irqrestore(&dev->lock, flags);
  415. if (skb && !in) {
  416. dev_kfree_skb_any(skb);
  417. return NETDEV_TX_OK;
  418. }
  419. /* apply outgoing CDC or RNDIS filters */
  420. if (skb && !is_promisc(cdc_filter)) {
  421. u8 *dest = skb->data;
  422. if (is_multicast_ether_addr(dest)) {
  423. u16 type;
  424. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  425. * SET_ETHERNET_MULTICAST_FILTERS requests
  426. */
  427. if (is_broadcast_ether_addr(dest))
  428. type = USB_CDC_PACKET_TYPE_BROADCAST;
  429. else
  430. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  431. if (!(cdc_filter & type)) {
  432. dev_kfree_skb_any(skb);
  433. return NETDEV_TX_OK;
  434. }
  435. }
  436. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  437. }
  438. spin_lock_irqsave(&dev->req_lock, flags);
  439. /*
  440. * this freelist can be empty if an interrupt triggered disconnect()
  441. * and reconfigured the gadget (shutting down this queue) after the
  442. * network stack decided to xmit but before we got the spinlock.
  443. */
  444. if (list_empty(&dev->tx_reqs)) {
  445. spin_unlock_irqrestore(&dev->req_lock, flags);
  446. return NETDEV_TX_BUSY;
  447. }
  448. req = container_of(dev->tx_reqs.next, struct usb_request, list);
  449. list_del(&req->list);
  450. /* temporarily stop TX queue when the freelist empties */
  451. if (list_empty(&dev->tx_reqs))
  452. netif_stop_queue(net);
  453. spin_unlock_irqrestore(&dev->req_lock, flags);
  454. /* no buffer copies needed, unless the network stack did it
  455. * or the hardware can't use skb buffers.
  456. * or there's not enough space for extra headers we need
  457. */
  458. if (dev->wrap) {
  459. unsigned long flags;
  460. spin_lock_irqsave(&dev->lock, flags);
  461. if (dev->port_usb)
  462. skb = dev->wrap(dev->port_usb, skb);
  463. spin_unlock_irqrestore(&dev->lock, flags);
  464. if (!skb) {
  465. /* Multi frame CDC protocols may store the frame for
  466. * later which is not a dropped frame.
  467. */
  468. if (dev->port_usb->supports_multi_frame)
  469. goto multiframe;
  470. goto drop;
  471. }
  472. }
  473. length = skb->len;
  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) % dev->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. multiframe:
  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. static int get_ether_addr(const char *str, u8 *dev_addr)
  587. {
  588. if (str) {
  589. unsigned i;
  590. for (i = 0; i < 6; i++) {
  591. unsigned char num;
  592. if ((*str == '.') || (*str == ':'))
  593. str++;
  594. num = hex_to_bin(*str++) << 4;
  595. num |= hex_to_bin(*str++);
  596. dev_addr [i] = num;
  597. }
  598. if (is_valid_ether_addr(dev_addr))
  599. return 0;
  600. }
  601. eth_random_addr(dev_addr);
  602. return 1;
  603. }
  604. static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
  605. {
  606. if (len < 18)
  607. return -EINVAL;
  608. snprintf(str, len, "%pM", dev_addr);
  609. return 18;
  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 an eth_dev pointer on success, or an ERR_PTR on failure.
  635. */
  636. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  637. const char *dev_addr, const char *host_addr,
  638. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
  639. {
  640. struct eth_dev *dev;
  641. struct net_device *net;
  642. int status;
  643. net = alloc_etherdev(sizeof *dev);
  644. if (!net)
  645. return ERR_PTR(-ENOMEM);
  646. dev = netdev_priv(net);
  647. spin_lock_init(&dev->lock);
  648. spin_lock_init(&dev->req_lock);
  649. INIT_WORK(&dev->work, eth_work);
  650. INIT_LIST_HEAD(&dev->tx_reqs);
  651. INIT_LIST_HEAD(&dev->rx_reqs);
  652. skb_queue_head_init(&dev->rx_frames);
  653. /* network device setup */
  654. dev->net = net;
  655. dev->qmult = qmult;
  656. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  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. net->ethtool_ops = &ops;
  667. dev->gadget = g;
  668. SET_NETDEV_DEV(net, &g->dev);
  669. SET_NETDEV_DEVTYPE(net, &gadget_type);
  670. status = register_netdev(net);
  671. if (status < 0) {
  672. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  673. free_netdev(net);
  674. dev = ERR_PTR(status);
  675. } else {
  676. INFO(dev, "MAC %pM\n", net->dev_addr);
  677. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  678. /*
  679. * two kinds of host-initiated state changes:
  680. * - iff DATA transfer is active, carrier is "on"
  681. * - tx queueing enabled if open *and* carrier is "on"
  682. */
  683. netif_carrier_off(net);
  684. }
  685. return dev;
  686. }
  687. EXPORT_SYMBOL_GPL(gether_setup_name);
  688. struct net_device *gether_setup_name_default(const char *netname)
  689. {
  690. struct net_device *net;
  691. struct eth_dev *dev;
  692. net = alloc_etherdev(sizeof(*dev));
  693. if (!net)
  694. return ERR_PTR(-ENOMEM);
  695. dev = netdev_priv(net);
  696. spin_lock_init(&dev->lock);
  697. spin_lock_init(&dev->req_lock);
  698. INIT_WORK(&dev->work, eth_work);
  699. INIT_LIST_HEAD(&dev->tx_reqs);
  700. INIT_LIST_HEAD(&dev->rx_reqs);
  701. skb_queue_head_init(&dev->rx_frames);
  702. /* network device setup */
  703. dev->net = net;
  704. dev->qmult = QMULT_DEFAULT;
  705. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  706. eth_random_addr(dev->dev_mac);
  707. pr_warn("using random %s ethernet address\n", "self");
  708. eth_random_addr(dev->host_mac);
  709. pr_warn("using random %s ethernet address\n", "host");
  710. net->netdev_ops = &eth_netdev_ops;
  711. net->ethtool_ops = &ops;
  712. SET_NETDEV_DEVTYPE(net, &gadget_type);
  713. return net;
  714. }
  715. EXPORT_SYMBOL_GPL(gether_setup_name_default);
  716. int gether_register_netdev(struct net_device *net)
  717. {
  718. struct eth_dev *dev;
  719. struct usb_gadget *g;
  720. struct sockaddr sa;
  721. int status;
  722. if (!net->dev.parent)
  723. return -EINVAL;
  724. dev = netdev_priv(net);
  725. g = dev->gadget;
  726. status = register_netdev(net);
  727. if (status < 0) {
  728. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  729. return status;
  730. } else {
  731. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  732. /* two kinds of host-initiated state changes:
  733. * - iff DATA transfer is active, carrier is "on"
  734. * - tx queueing enabled if open *and* carrier is "on"
  735. */
  736. netif_carrier_off(net);
  737. }
  738. sa.sa_family = net->type;
  739. memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
  740. rtnl_lock();
  741. status = dev_set_mac_address(net, &sa);
  742. rtnl_unlock();
  743. if (status)
  744. pr_warn("cannot set self ethernet address: %d\n", status);
  745. else
  746. INFO(dev, "MAC %pM\n", dev->dev_mac);
  747. return status;
  748. }
  749. EXPORT_SYMBOL_GPL(gether_register_netdev);
  750. void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
  751. {
  752. struct eth_dev *dev;
  753. dev = netdev_priv(net);
  754. dev->gadget = g;
  755. SET_NETDEV_DEV(net, &g->dev);
  756. }
  757. EXPORT_SYMBOL_GPL(gether_set_gadget);
  758. int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
  759. {
  760. struct eth_dev *dev;
  761. u8 new_addr[ETH_ALEN];
  762. dev = netdev_priv(net);
  763. if (get_ether_addr(dev_addr, new_addr))
  764. return -EINVAL;
  765. memcpy(dev->dev_mac, new_addr, ETH_ALEN);
  766. return 0;
  767. }
  768. EXPORT_SYMBOL_GPL(gether_set_dev_addr);
  769. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
  770. {
  771. struct eth_dev *dev;
  772. dev = netdev_priv(net);
  773. return get_ether_addr_str(dev->dev_mac, dev_addr, len);
  774. }
  775. EXPORT_SYMBOL_GPL(gether_get_dev_addr);
  776. int gether_set_host_addr(struct net_device *net, const char *host_addr)
  777. {
  778. struct eth_dev *dev;
  779. u8 new_addr[ETH_ALEN];
  780. dev = netdev_priv(net);
  781. if (get_ether_addr(host_addr, new_addr))
  782. return -EINVAL;
  783. memcpy(dev->host_mac, new_addr, ETH_ALEN);
  784. return 0;
  785. }
  786. EXPORT_SYMBOL_GPL(gether_set_host_addr);
  787. int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
  788. {
  789. struct eth_dev *dev;
  790. dev = netdev_priv(net);
  791. return get_ether_addr_str(dev->host_mac, host_addr, len);
  792. }
  793. EXPORT_SYMBOL_GPL(gether_get_host_addr);
  794. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
  795. {
  796. struct eth_dev *dev;
  797. if (len < 13)
  798. return -EINVAL;
  799. dev = netdev_priv(net);
  800. snprintf(host_addr, len, "%pm", dev->host_mac);
  801. return strlen(host_addr);
  802. }
  803. EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
  804. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
  805. {
  806. struct eth_dev *dev;
  807. dev = netdev_priv(net);
  808. memcpy(host_mac, dev->host_mac, ETH_ALEN);
  809. }
  810. EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
  811. void gether_set_qmult(struct net_device *net, unsigned qmult)
  812. {
  813. struct eth_dev *dev;
  814. dev = netdev_priv(net);
  815. dev->qmult = qmult;
  816. }
  817. EXPORT_SYMBOL_GPL(gether_set_qmult);
  818. unsigned gether_get_qmult(struct net_device *net)
  819. {
  820. struct eth_dev *dev;
  821. dev = netdev_priv(net);
  822. return dev->qmult;
  823. }
  824. EXPORT_SYMBOL_GPL(gether_get_qmult);
  825. int gether_get_ifname(struct net_device *net, char *name, int len)
  826. {
  827. rtnl_lock();
  828. strlcpy(name, netdev_name(net), len);
  829. rtnl_unlock();
  830. return strlen(name);
  831. }
  832. EXPORT_SYMBOL_GPL(gether_get_ifname);
  833. /**
  834. * gether_cleanup - remove Ethernet-over-USB device
  835. * Context: may sleep
  836. *
  837. * This is called to free all resources allocated by @gether_setup().
  838. */
  839. void gether_cleanup(struct eth_dev *dev)
  840. {
  841. if (!dev)
  842. return;
  843. unregister_netdev(dev->net);
  844. flush_work(&dev->work);
  845. free_netdev(dev->net);
  846. }
  847. EXPORT_SYMBOL_GPL(gether_cleanup);
  848. /**
  849. * gether_connect - notify network layer that USB link is active
  850. * @link: the USB link, set up with endpoints, descriptors matching
  851. * current device speed, and any framing wrapper(s) set up.
  852. * Context: irqs blocked
  853. *
  854. * This is called to activate endpoints and let the network layer know
  855. * the connection is active ("carrier detect"). It may cause the I/O
  856. * queues to open and start letting network packets flow, but will in
  857. * any case activate the endpoints so that they respond properly to the
  858. * USB host.
  859. *
  860. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  861. * indicate some error code (negative errno), ep->driver_data values
  862. * have been overwritten.
  863. */
  864. struct net_device *gether_connect(struct gether *link)
  865. {
  866. struct eth_dev *dev = link->ioport;
  867. int result = 0;
  868. if (!dev)
  869. return ERR_PTR(-EINVAL);
  870. link->in_ep->driver_data = dev;
  871. result = usb_ep_enable(link->in_ep);
  872. if (result != 0) {
  873. DBG(dev, "enable %s --> %d\n",
  874. link->in_ep->name, result);
  875. goto fail0;
  876. }
  877. link->out_ep->driver_data = dev;
  878. result = usb_ep_enable(link->out_ep);
  879. if (result != 0) {
  880. DBG(dev, "enable %s --> %d\n",
  881. link->out_ep->name, result);
  882. goto fail1;
  883. }
  884. if (result == 0)
  885. result = alloc_requests(dev, link, qlen(dev->gadget,
  886. dev->qmult));
  887. if (result == 0) {
  888. dev->zlp = link->is_zlp_ok;
  889. DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
  890. dev->header_len = link->header_len;
  891. dev->unwrap = link->unwrap;
  892. dev->wrap = link->wrap;
  893. spin_lock(&dev->lock);
  894. dev->port_usb = link;
  895. if (netif_running(dev->net)) {
  896. if (link->open)
  897. link->open(link);
  898. } else {
  899. if (link->close)
  900. link->close(link);
  901. }
  902. spin_unlock(&dev->lock);
  903. netif_carrier_on(dev->net);
  904. if (netif_running(dev->net))
  905. eth_start(dev, GFP_ATOMIC);
  906. /* on error, disable any endpoints */
  907. } else {
  908. (void) usb_ep_disable(link->out_ep);
  909. fail1:
  910. (void) usb_ep_disable(link->in_ep);
  911. }
  912. fail0:
  913. /* caller is responsible for cleanup on error */
  914. if (result < 0)
  915. return ERR_PTR(result);
  916. return dev->net;
  917. }
  918. EXPORT_SYMBOL_GPL(gether_connect);
  919. /**
  920. * gether_disconnect - notify network layer that USB link is inactive
  921. * @link: the USB link, on which gether_connect() was called
  922. * Context: irqs blocked
  923. *
  924. * This is called to deactivate endpoints and let the network layer know
  925. * the connection went inactive ("no carrier").
  926. *
  927. * On return, the state is as if gether_connect() had never been called.
  928. * The endpoints are inactive, and accordingly without active USB I/O.
  929. * Pointers to endpoint descriptors and endpoint private data are nulled.
  930. */
  931. void gether_disconnect(struct gether *link)
  932. {
  933. struct eth_dev *dev = link->ioport;
  934. struct usb_request *req;
  935. WARN_ON(!dev);
  936. if (!dev)
  937. return;
  938. DBG(dev, "%s\n", __func__);
  939. netif_stop_queue(dev->net);
  940. netif_carrier_off(dev->net);
  941. /* disable endpoints, forcing (synchronous) completion
  942. * of all pending i/o. then free the request objects
  943. * and forget about the endpoints.
  944. */
  945. usb_ep_disable(link->in_ep);
  946. spin_lock(&dev->req_lock);
  947. while (!list_empty(&dev->tx_reqs)) {
  948. req = container_of(dev->tx_reqs.next,
  949. struct usb_request, list);
  950. list_del(&req->list);
  951. spin_unlock(&dev->req_lock);
  952. usb_ep_free_request(link->in_ep, req);
  953. spin_lock(&dev->req_lock);
  954. }
  955. spin_unlock(&dev->req_lock);
  956. link->in_ep->desc = NULL;
  957. usb_ep_disable(link->out_ep);
  958. spin_lock(&dev->req_lock);
  959. while (!list_empty(&dev->rx_reqs)) {
  960. req = container_of(dev->rx_reqs.next,
  961. struct usb_request, list);
  962. list_del(&req->list);
  963. spin_unlock(&dev->req_lock);
  964. usb_ep_free_request(link->out_ep, req);
  965. spin_lock(&dev->req_lock);
  966. }
  967. spin_unlock(&dev->req_lock);
  968. link->out_ep->desc = NULL;
  969. /* finish forgetting about this USB link episode */
  970. dev->header_len = 0;
  971. dev->unwrap = NULL;
  972. dev->wrap = NULL;
  973. spin_lock(&dev->lock);
  974. dev->port_usb = NULL;
  975. spin_unlock(&dev->lock);
  976. }
  977. EXPORT_SYMBOL_GPL(gether_disconnect);
  978. MODULE_LICENSE("GPL");
  979. MODULE_AUTHOR("David Brownell");