u_ether.c 25 KB

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