u_ether.c 25 KB

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