u_ether.c 29 KB

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