u_ether.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 &&
  469. dev->port_usb->supports_multi_frame)
  470. goto multiframe;
  471. goto drop;
  472. }
  473. }
  474. length = skb->len;
  475. req->buf = skb->data;
  476. req->context = skb;
  477. req->complete = tx_complete;
  478. /* NCM requires no zlp if transfer is dwNtbInMaxSize */
  479. if (dev->port_usb->is_fixed &&
  480. length == dev->port_usb->fixed_in_len &&
  481. (length % in->maxpacket) == 0)
  482. req->zero = 0;
  483. else
  484. req->zero = 1;
  485. /* use zlp framing on tx for strict CDC-Ether conformance,
  486. * though any robust network rx path ignores extra padding.
  487. * and some hardware doesn't like to write zlps.
  488. */
  489. if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
  490. length++;
  491. req->length = length;
  492. /* throttle high/super speed IRQ rate back slightly */
  493. if (gadget_is_dualspeed(dev->gadget))
  494. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH ||
  495. dev->gadget->speed == USB_SPEED_SUPER)
  496. ? ((atomic_read(&dev->tx_qlen) % dev->qmult) != 0)
  497. : 0;
  498. retval = usb_ep_queue(in, req, GFP_ATOMIC);
  499. switch (retval) {
  500. default:
  501. DBG(dev, "tx queue err %d\n", retval);
  502. break;
  503. case 0:
  504. netif_trans_update(net);
  505. atomic_inc(&dev->tx_qlen);
  506. }
  507. if (retval) {
  508. dev_kfree_skb_any(skb);
  509. drop:
  510. dev->net->stats.tx_dropped++;
  511. multiframe:
  512. spin_lock_irqsave(&dev->req_lock, flags);
  513. if (list_empty(&dev->tx_reqs))
  514. netif_start_queue(net);
  515. list_add(&req->list, &dev->tx_reqs);
  516. spin_unlock_irqrestore(&dev->req_lock, flags);
  517. }
  518. return NETDEV_TX_OK;
  519. }
  520. /*-------------------------------------------------------------------------*/
  521. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
  522. {
  523. DBG(dev, "%s\n", __func__);
  524. /* fill the rx queue */
  525. rx_fill(dev, gfp_flags);
  526. /* and open the tx floodgates */
  527. atomic_set(&dev->tx_qlen, 0);
  528. netif_wake_queue(dev->net);
  529. }
  530. static int eth_open(struct net_device *net)
  531. {
  532. struct eth_dev *dev = netdev_priv(net);
  533. struct gether *link;
  534. DBG(dev, "%s\n", __func__);
  535. if (netif_carrier_ok(dev->net))
  536. eth_start(dev, GFP_KERNEL);
  537. spin_lock_irq(&dev->lock);
  538. link = dev->port_usb;
  539. if (link && link->open)
  540. link->open(link);
  541. spin_unlock_irq(&dev->lock);
  542. return 0;
  543. }
  544. static int eth_stop(struct net_device *net)
  545. {
  546. struct eth_dev *dev = netdev_priv(net);
  547. unsigned long flags;
  548. VDBG(dev, "%s\n", __func__);
  549. netif_stop_queue(net);
  550. DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
  551. dev->net->stats.rx_packets, dev->net->stats.tx_packets,
  552. dev->net->stats.rx_errors, dev->net->stats.tx_errors
  553. );
  554. /* ensure there are no more active requests */
  555. spin_lock_irqsave(&dev->lock, flags);
  556. if (dev->port_usb) {
  557. struct gether *link = dev->port_usb;
  558. const struct usb_endpoint_descriptor *in;
  559. const struct usb_endpoint_descriptor *out;
  560. if (link->close)
  561. link->close(link);
  562. /* NOTE: we have no abort-queue primitive we could use
  563. * to cancel all pending I/O. Instead, we disable then
  564. * reenable the endpoints ... this idiom may leave toggle
  565. * wrong, but that's a self-correcting error.
  566. *
  567. * REVISIT: we *COULD* just let the transfers complete at
  568. * their own pace; the network stack can handle old packets.
  569. * For the moment we leave this here, since it works.
  570. */
  571. in = link->in_ep->desc;
  572. out = link->out_ep->desc;
  573. usb_ep_disable(link->in_ep);
  574. usb_ep_disable(link->out_ep);
  575. if (netif_carrier_ok(net)) {
  576. DBG(dev, "host still using in/out endpoints\n");
  577. link->in_ep->desc = in;
  578. link->out_ep->desc = out;
  579. usb_ep_enable(link->in_ep);
  580. usb_ep_enable(link->out_ep);
  581. }
  582. }
  583. spin_unlock_irqrestore(&dev->lock, flags);
  584. return 0;
  585. }
  586. /*-------------------------------------------------------------------------*/
  587. static int get_ether_addr(const char *str, u8 *dev_addr)
  588. {
  589. if (str) {
  590. unsigned i;
  591. for (i = 0; i < 6; i++) {
  592. unsigned char num;
  593. if ((*str == '.') || (*str == ':'))
  594. str++;
  595. num = hex_to_bin(*str++) << 4;
  596. num |= hex_to_bin(*str++);
  597. dev_addr [i] = num;
  598. }
  599. if (is_valid_ether_addr(dev_addr))
  600. return 0;
  601. }
  602. eth_random_addr(dev_addr);
  603. return 1;
  604. }
  605. static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
  606. {
  607. if (len < 18)
  608. return -EINVAL;
  609. snprintf(str, len, "%pM", dev_addr);
  610. return 18;
  611. }
  612. static const struct net_device_ops eth_netdev_ops = {
  613. .ndo_open = eth_open,
  614. .ndo_stop = eth_stop,
  615. .ndo_start_xmit = eth_start_xmit,
  616. .ndo_change_mtu = ueth_change_mtu,
  617. .ndo_set_mac_address = eth_mac_addr,
  618. .ndo_validate_addr = eth_validate_addr,
  619. };
  620. static struct device_type gadget_type = {
  621. .name = "gadget",
  622. };
  623. /**
  624. * gether_setup_name - initialize one ethernet-over-usb link
  625. * @g: gadget to associated with these links
  626. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  627. * host side of the link is recorded
  628. * @netname: name for network device (for example, "usb")
  629. * Context: may sleep
  630. *
  631. * This sets up the single network link that may be exported by a
  632. * gadget driver using this framework. The link layer addresses are
  633. * set up using module parameters.
  634. *
  635. * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
  636. */
  637. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  638. const char *dev_addr, const char *host_addr,
  639. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
  640. {
  641. struct eth_dev *dev;
  642. struct net_device *net;
  643. int status;
  644. net = alloc_etherdev(sizeof *dev);
  645. if (!net)
  646. return ERR_PTR(-ENOMEM);
  647. dev = netdev_priv(net);
  648. spin_lock_init(&dev->lock);
  649. spin_lock_init(&dev->req_lock);
  650. INIT_WORK(&dev->work, eth_work);
  651. INIT_LIST_HEAD(&dev->tx_reqs);
  652. INIT_LIST_HEAD(&dev->rx_reqs);
  653. skb_queue_head_init(&dev->rx_frames);
  654. /* network device setup */
  655. dev->net = net;
  656. dev->qmult = qmult;
  657. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  658. if (get_ether_addr(dev_addr, net->dev_addr))
  659. dev_warn(&g->dev,
  660. "using random %s ethernet address\n", "self");
  661. if (get_ether_addr(host_addr, dev->host_mac))
  662. dev_warn(&g->dev,
  663. "using random %s ethernet address\n", "host");
  664. if (ethaddr)
  665. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  666. net->netdev_ops = &eth_netdev_ops;
  667. net->ethtool_ops = &ops;
  668. dev->gadget = g;
  669. SET_NETDEV_DEV(net, &g->dev);
  670. SET_NETDEV_DEVTYPE(net, &gadget_type);
  671. status = register_netdev(net);
  672. if (status < 0) {
  673. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  674. free_netdev(net);
  675. dev = ERR_PTR(status);
  676. } else {
  677. INFO(dev, "MAC %pM\n", net->dev_addr);
  678. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  679. /*
  680. * two kinds of host-initiated state changes:
  681. * - iff DATA transfer is active, carrier is "on"
  682. * - tx queueing enabled if open *and* carrier is "on"
  683. */
  684. netif_carrier_off(net);
  685. }
  686. return dev;
  687. }
  688. EXPORT_SYMBOL_GPL(gether_setup_name);
  689. struct net_device *gether_setup_name_default(const char *netname)
  690. {
  691. struct net_device *net;
  692. struct eth_dev *dev;
  693. net = alloc_etherdev(sizeof(*dev));
  694. if (!net)
  695. return ERR_PTR(-ENOMEM);
  696. dev = netdev_priv(net);
  697. spin_lock_init(&dev->lock);
  698. spin_lock_init(&dev->req_lock);
  699. INIT_WORK(&dev->work, eth_work);
  700. INIT_LIST_HEAD(&dev->tx_reqs);
  701. INIT_LIST_HEAD(&dev->rx_reqs);
  702. skb_queue_head_init(&dev->rx_frames);
  703. /* network device setup */
  704. dev->net = net;
  705. dev->qmult = QMULT_DEFAULT;
  706. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  707. eth_random_addr(dev->dev_mac);
  708. pr_warn("using random %s ethernet address\n", "self");
  709. eth_random_addr(dev->host_mac);
  710. pr_warn("using random %s ethernet address\n", "host");
  711. net->netdev_ops = &eth_netdev_ops;
  712. net->ethtool_ops = &ops;
  713. SET_NETDEV_DEVTYPE(net, &gadget_type);
  714. return net;
  715. }
  716. EXPORT_SYMBOL_GPL(gether_setup_name_default);
  717. int gether_register_netdev(struct net_device *net)
  718. {
  719. struct eth_dev *dev;
  720. struct usb_gadget *g;
  721. struct sockaddr sa;
  722. int status;
  723. if (!net->dev.parent)
  724. return -EINVAL;
  725. dev = netdev_priv(net);
  726. g = dev->gadget;
  727. status = register_netdev(net);
  728. if (status < 0) {
  729. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  730. return status;
  731. } else {
  732. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  733. /* two kinds of host-initiated state changes:
  734. * - iff DATA transfer is active, carrier is "on"
  735. * - tx queueing enabled if open *and* carrier is "on"
  736. */
  737. netif_carrier_off(net);
  738. }
  739. sa.sa_family = net->type;
  740. memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
  741. rtnl_lock();
  742. status = dev_set_mac_address(net, &sa);
  743. rtnl_unlock();
  744. if (status)
  745. pr_warn("cannot set self ethernet address: %d\n", status);
  746. else
  747. INFO(dev, "MAC %pM\n", dev->dev_mac);
  748. return status;
  749. }
  750. EXPORT_SYMBOL_GPL(gether_register_netdev);
  751. void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
  752. {
  753. struct eth_dev *dev;
  754. dev = netdev_priv(net);
  755. dev->gadget = g;
  756. SET_NETDEV_DEV(net, &g->dev);
  757. }
  758. EXPORT_SYMBOL_GPL(gether_set_gadget);
  759. int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
  760. {
  761. struct eth_dev *dev;
  762. u8 new_addr[ETH_ALEN];
  763. dev = netdev_priv(net);
  764. if (get_ether_addr(dev_addr, new_addr))
  765. return -EINVAL;
  766. memcpy(dev->dev_mac, new_addr, ETH_ALEN);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL_GPL(gether_set_dev_addr);
  770. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
  771. {
  772. struct eth_dev *dev;
  773. dev = netdev_priv(net);
  774. return get_ether_addr_str(dev->dev_mac, dev_addr, len);
  775. }
  776. EXPORT_SYMBOL_GPL(gether_get_dev_addr);
  777. int gether_set_host_addr(struct net_device *net, const char *host_addr)
  778. {
  779. struct eth_dev *dev;
  780. u8 new_addr[ETH_ALEN];
  781. dev = netdev_priv(net);
  782. if (get_ether_addr(host_addr, new_addr))
  783. return -EINVAL;
  784. memcpy(dev->host_mac, new_addr, ETH_ALEN);
  785. return 0;
  786. }
  787. EXPORT_SYMBOL_GPL(gether_set_host_addr);
  788. int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
  789. {
  790. struct eth_dev *dev;
  791. dev = netdev_priv(net);
  792. return get_ether_addr_str(dev->host_mac, host_addr, len);
  793. }
  794. EXPORT_SYMBOL_GPL(gether_get_host_addr);
  795. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
  796. {
  797. struct eth_dev *dev;
  798. if (len < 13)
  799. return -EINVAL;
  800. dev = netdev_priv(net);
  801. snprintf(host_addr, len, "%pm", dev->host_mac);
  802. return strlen(host_addr);
  803. }
  804. EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
  805. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
  806. {
  807. struct eth_dev *dev;
  808. dev = netdev_priv(net);
  809. memcpy(host_mac, dev->host_mac, ETH_ALEN);
  810. }
  811. EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
  812. void gether_set_qmult(struct net_device *net, unsigned qmult)
  813. {
  814. struct eth_dev *dev;
  815. dev = netdev_priv(net);
  816. dev->qmult = qmult;
  817. }
  818. EXPORT_SYMBOL_GPL(gether_set_qmult);
  819. unsigned gether_get_qmult(struct net_device *net)
  820. {
  821. struct eth_dev *dev;
  822. dev = netdev_priv(net);
  823. return dev->qmult;
  824. }
  825. EXPORT_SYMBOL_GPL(gether_get_qmult);
  826. int gether_get_ifname(struct net_device *net, char *name, int len)
  827. {
  828. rtnl_lock();
  829. strlcpy(name, netdev_name(net), len);
  830. rtnl_unlock();
  831. return strlen(name);
  832. }
  833. EXPORT_SYMBOL_GPL(gether_get_ifname);
  834. /**
  835. * gether_cleanup - remove Ethernet-over-USB device
  836. * Context: may sleep
  837. *
  838. * This is called to free all resources allocated by @gether_setup().
  839. */
  840. void gether_cleanup(struct eth_dev *dev)
  841. {
  842. if (!dev)
  843. return;
  844. unregister_netdev(dev->net);
  845. flush_work(&dev->work);
  846. free_netdev(dev->net);
  847. }
  848. EXPORT_SYMBOL_GPL(gether_cleanup);
  849. /**
  850. * gether_connect - notify network layer that USB link is active
  851. * @link: the USB link, set up with endpoints, descriptors matching
  852. * current device speed, and any framing wrapper(s) set up.
  853. * Context: irqs blocked
  854. *
  855. * This is called to activate endpoints and let the network layer know
  856. * the connection is active ("carrier detect"). It may cause the I/O
  857. * queues to open and start letting network packets flow, but will in
  858. * any case activate the endpoints so that they respond properly to the
  859. * USB host.
  860. *
  861. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  862. * indicate some error code (negative errno), ep->driver_data values
  863. * have been overwritten.
  864. */
  865. struct net_device *gether_connect(struct gether *link)
  866. {
  867. struct eth_dev *dev = link->ioport;
  868. int result = 0;
  869. if (!dev)
  870. return ERR_PTR(-EINVAL);
  871. link->in_ep->driver_data = dev;
  872. result = usb_ep_enable(link->in_ep);
  873. if (result != 0) {
  874. DBG(dev, "enable %s --> %d\n",
  875. link->in_ep->name, result);
  876. goto fail0;
  877. }
  878. link->out_ep->driver_data = dev;
  879. result = usb_ep_enable(link->out_ep);
  880. if (result != 0) {
  881. DBG(dev, "enable %s --> %d\n",
  882. link->out_ep->name, result);
  883. goto fail1;
  884. }
  885. if (result == 0)
  886. result = alloc_requests(dev, link, qlen(dev->gadget,
  887. dev->qmult));
  888. if (result == 0) {
  889. dev->zlp = link->is_zlp_ok;
  890. DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
  891. dev->header_len = link->header_len;
  892. dev->unwrap = link->unwrap;
  893. dev->wrap = link->wrap;
  894. spin_lock(&dev->lock);
  895. dev->port_usb = link;
  896. if (netif_running(dev->net)) {
  897. if (link->open)
  898. link->open(link);
  899. } else {
  900. if (link->close)
  901. link->close(link);
  902. }
  903. spin_unlock(&dev->lock);
  904. netif_carrier_on(dev->net);
  905. if (netif_running(dev->net))
  906. eth_start(dev, GFP_ATOMIC);
  907. /* on error, disable any endpoints */
  908. } else {
  909. (void) usb_ep_disable(link->out_ep);
  910. fail1:
  911. (void) usb_ep_disable(link->in_ep);
  912. }
  913. fail0:
  914. /* caller is responsible for cleanup on error */
  915. if (result < 0)
  916. return ERR_PTR(result);
  917. return dev->net;
  918. }
  919. EXPORT_SYMBOL_GPL(gether_connect);
  920. /**
  921. * gether_disconnect - notify network layer that USB link is inactive
  922. * @link: the USB link, on which gether_connect() was called
  923. * Context: irqs blocked
  924. *
  925. * This is called to deactivate endpoints and let the network layer know
  926. * the connection went inactive ("no carrier").
  927. *
  928. * On return, the state is as if gether_connect() had never been called.
  929. * The endpoints are inactive, and accordingly without active USB I/O.
  930. * Pointers to endpoint descriptors and endpoint private data are nulled.
  931. */
  932. void gether_disconnect(struct gether *link)
  933. {
  934. struct eth_dev *dev = link->ioport;
  935. struct usb_request *req;
  936. WARN_ON(!dev);
  937. if (!dev)
  938. return;
  939. DBG(dev, "%s\n", __func__);
  940. netif_stop_queue(dev->net);
  941. netif_carrier_off(dev->net);
  942. /* disable endpoints, forcing (synchronous) completion
  943. * of all pending i/o. then free the request objects
  944. * and forget about the endpoints.
  945. */
  946. usb_ep_disable(link->in_ep);
  947. spin_lock(&dev->req_lock);
  948. while (!list_empty(&dev->tx_reqs)) {
  949. req = container_of(dev->tx_reqs.next,
  950. struct usb_request, list);
  951. list_del(&req->list);
  952. spin_unlock(&dev->req_lock);
  953. usb_ep_free_request(link->in_ep, req);
  954. spin_lock(&dev->req_lock);
  955. }
  956. spin_unlock(&dev->req_lock);
  957. link->in_ep->desc = NULL;
  958. usb_ep_disable(link->out_ep);
  959. spin_lock(&dev->req_lock);
  960. while (!list_empty(&dev->rx_reqs)) {
  961. req = container_of(dev->rx_reqs.next,
  962. struct usb_request, list);
  963. list_del(&req->list);
  964. spin_unlock(&dev->req_lock);
  965. usb_ep_free_request(link->out_ep, req);
  966. spin_lock(&dev->req_lock);
  967. }
  968. spin_unlock(&dev->req_lock);
  969. link->out_ep->desc = NULL;
  970. /* finish forgetting about this USB link episode */
  971. dev->header_len = 0;
  972. dev->unwrap = NULL;
  973. dev->wrap = NULL;
  974. spin_lock(&dev->lock);
  975. dev->port_usb = NULL;
  976. spin_unlock(&dev->lock);
  977. }
  978. EXPORT_SYMBOL_GPL(gether_disconnect);
  979. MODULE_LICENSE("GPL");
  980. MODULE_AUTHOR("David Brownell");