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