u_ether.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
  198. if (skb == NULL) {
  199. DBG(dev, "no rx skb\n");
  200. goto enomem;
  201. }
  202. /* Some platforms perform better when IP packets are aligned,
  203. * but on at least one, checksumming fails otherwise. Note:
  204. * RNDIS headers involve variable numbers of LE32 values.
  205. */
  206. skb_reserve(skb, NET_IP_ALIGN);
  207. req->buf = skb->data;
  208. req->length = size;
  209. req->complete = rx_complete;
  210. req->context = skb;
  211. retval = usb_ep_queue(out, req, gfp_flags);
  212. if (retval == -ENOMEM)
  213. enomem:
  214. defer_kevent(dev, WORK_RX_MEMORY);
  215. if (retval) {
  216. DBG(dev, "rx submit --> %d\n", retval);
  217. if (skb)
  218. dev_kfree_skb_any(skb);
  219. spin_lock_irqsave(&dev->req_lock, flags);
  220. list_add(&req->list, &dev->rx_reqs);
  221. spin_unlock_irqrestore(&dev->req_lock, flags);
  222. }
  223. return retval;
  224. }
  225. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  226. {
  227. struct sk_buff *skb = req->context, *skb2;
  228. struct eth_dev *dev = ep->driver_data;
  229. int status = req->status;
  230. switch (status) {
  231. /* normal completion */
  232. case 0:
  233. skb_put(skb, req->actual);
  234. if (dev->unwrap) {
  235. unsigned long flags;
  236. spin_lock_irqsave(&dev->lock, flags);
  237. if (dev->port_usb) {
  238. status = dev->unwrap(dev->port_usb,
  239. skb,
  240. &dev->rx_frames);
  241. } else {
  242. dev_kfree_skb_any(skb);
  243. status = -ENOTCONN;
  244. }
  245. spin_unlock_irqrestore(&dev->lock, flags);
  246. } else {
  247. skb_queue_tail(&dev->rx_frames, skb);
  248. }
  249. skb = NULL;
  250. skb2 = skb_dequeue(&dev->rx_frames);
  251. while (skb2) {
  252. if (status < 0
  253. || ETH_HLEN > skb2->len
  254. || skb2->len > ETH_FRAME_LEN) {
  255. dev->net->stats.rx_errors++;
  256. dev->net->stats.rx_length_errors++;
  257. DBG(dev, "rx length %d\n", skb2->len);
  258. dev_kfree_skb_any(skb2);
  259. goto next_frame;
  260. }
  261. skb2->protocol = eth_type_trans(skb2, dev->net);
  262. dev->net->stats.rx_packets++;
  263. dev->net->stats.rx_bytes += skb2->len;
  264. /* no buffer copies needed, unless hardware can't
  265. * use skb buffers.
  266. */
  267. status = netif_rx(skb2);
  268. next_frame:
  269. skb2 = skb_dequeue(&dev->rx_frames);
  270. }
  271. break;
  272. /* software-driven interface shutdown */
  273. case -ECONNRESET: /* unlink */
  274. case -ESHUTDOWN: /* disconnect etc */
  275. VDBG(dev, "rx shutdown, code %d\n", status);
  276. goto quiesce;
  277. /* for hardware automagic (such as pxa) */
  278. case -ECONNABORTED: /* endpoint reset */
  279. DBG(dev, "rx %s reset\n", ep->name);
  280. defer_kevent(dev, WORK_RX_MEMORY);
  281. quiesce:
  282. dev_kfree_skb_any(skb);
  283. goto clean;
  284. /* data overrun */
  285. case -EOVERFLOW:
  286. dev->net->stats.rx_over_errors++;
  287. /* FALLTHROUGH */
  288. default:
  289. dev->net->stats.rx_errors++;
  290. DBG(dev, "rx status %d\n", status);
  291. break;
  292. }
  293. if (skb)
  294. dev_kfree_skb_any(skb);
  295. if (!netif_running(dev->net)) {
  296. clean:
  297. spin_lock(&dev->req_lock);
  298. list_add(&req->list, &dev->rx_reqs);
  299. spin_unlock(&dev->req_lock);
  300. req = NULL;
  301. }
  302. if (req)
  303. rx_submit(dev, req, GFP_ATOMIC);
  304. }
  305. static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
  306. {
  307. unsigned i;
  308. struct usb_request *req;
  309. if (!n)
  310. return -ENOMEM;
  311. /* queue/recycle up to N requests */
  312. i = n;
  313. list_for_each_entry(req, list, list) {
  314. if (i-- == 0)
  315. goto extra;
  316. }
  317. while (i--) {
  318. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  319. if (!req)
  320. return list_empty(list) ? -ENOMEM : 0;
  321. list_add(&req->list, list);
  322. }
  323. return 0;
  324. extra:
  325. /* free extras */
  326. for (;;) {
  327. struct list_head *next;
  328. next = req->list.next;
  329. list_del(&req->list);
  330. usb_ep_free_request(ep, req);
  331. if (next == list)
  332. break;
  333. req = container_of(next, struct usb_request, list);
  334. }
  335. return 0;
  336. }
  337. static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  338. {
  339. int status;
  340. spin_lock(&dev->req_lock);
  341. status = prealloc(&dev->tx_reqs, link->in_ep, n);
  342. if (status < 0)
  343. goto fail;
  344. status = prealloc(&dev->rx_reqs, link->out_ep, n);
  345. if (status < 0)
  346. goto fail;
  347. goto done;
  348. fail:
  349. DBG(dev, "can't alloc requests\n");
  350. done:
  351. spin_unlock(&dev->req_lock);
  352. return status;
  353. }
  354. static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
  355. {
  356. struct usb_request *req;
  357. unsigned long flags;
  358. /* fill unused rxq slots with some skb */
  359. spin_lock_irqsave(&dev->req_lock, flags);
  360. while (!list_empty(&dev->rx_reqs)) {
  361. req = container_of(dev->rx_reqs.next,
  362. struct usb_request, list);
  363. list_del_init(&req->list);
  364. spin_unlock_irqrestore(&dev->req_lock, flags);
  365. if (rx_submit(dev, req, gfp_flags) < 0) {
  366. defer_kevent(dev, WORK_RX_MEMORY);
  367. return;
  368. }
  369. spin_lock_irqsave(&dev->req_lock, flags);
  370. }
  371. spin_unlock_irqrestore(&dev->req_lock, flags);
  372. }
  373. static void eth_work(struct work_struct *work)
  374. {
  375. struct eth_dev *dev = container_of(work, struct eth_dev, work);
  376. if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
  377. if (netif_running(dev->net))
  378. rx_fill(dev, GFP_KERNEL);
  379. }
  380. if (dev->todo)
  381. DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
  382. }
  383. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  384. {
  385. struct sk_buff *skb = req->context;
  386. struct eth_dev *dev = ep->driver_data;
  387. switch (req->status) {
  388. default:
  389. dev->net->stats.tx_errors++;
  390. VDBG(dev, "tx err %d\n", req->status);
  391. /* FALLTHROUGH */
  392. case -ECONNRESET: /* unlink */
  393. case -ESHUTDOWN: /* disconnect etc */
  394. break;
  395. case 0:
  396. dev->net->stats.tx_bytes += skb->len;
  397. }
  398. dev->net->stats.tx_packets++;
  399. spin_lock(&dev->req_lock);
  400. list_add(&req->list, &dev->tx_reqs);
  401. spin_unlock(&dev->req_lock);
  402. dev_kfree_skb_any(skb);
  403. atomic_dec(&dev->tx_qlen);
  404. if (netif_carrier_ok(dev->net))
  405. netif_wake_queue(dev->net);
  406. }
  407. static inline int is_promisc(u16 cdc_filter)
  408. {
  409. return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  410. }
  411. static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
  412. struct net_device *net)
  413. {
  414. struct eth_dev *dev = netdev_priv(net);
  415. int length = skb->len;
  416. int retval;
  417. struct usb_request *req = NULL;
  418. unsigned long flags;
  419. struct usb_ep *in;
  420. u16 cdc_filter;
  421. spin_lock_irqsave(&dev->lock, flags);
  422. if (dev->port_usb) {
  423. in = dev->port_usb->in_ep;
  424. cdc_filter = dev->port_usb->cdc_filter;
  425. } else {
  426. in = NULL;
  427. cdc_filter = 0;
  428. }
  429. spin_unlock_irqrestore(&dev->lock, flags);
  430. if (!in) {
  431. dev_kfree_skb_any(skb);
  432. return NETDEV_TX_OK;
  433. }
  434. /* apply outgoing CDC or RNDIS filters */
  435. if (!is_promisc(cdc_filter)) {
  436. u8 *dest = skb->data;
  437. if (is_multicast_ether_addr(dest)) {
  438. u16 type;
  439. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  440. * SET_ETHERNET_MULTICAST_FILTERS requests
  441. */
  442. if (is_broadcast_ether_addr(dest))
  443. type = USB_CDC_PACKET_TYPE_BROADCAST;
  444. else
  445. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  446. if (!(cdc_filter & type)) {
  447. dev_kfree_skb_any(skb);
  448. return NETDEV_TX_OK;
  449. }
  450. }
  451. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  452. }
  453. spin_lock_irqsave(&dev->req_lock, flags);
  454. /*
  455. * this freelist can be empty if an interrupt triggered disconnect()
  456. * and reconfigured the gadget (shutting down this queue) after the
  457. * network stack decided to xmit but before we got the spinlock.
  458. */
  459. if (list_empty(&dev->tx_reqs)) {
  460. spin_unlock_irqrestore(&dev->req_lock, flags);
  461. return NETDEV_TX_BUSY;
  462. }
  463. req = container_of(dev->tx_reqs.next, struct usb_request, list);
  464. list_del(&req->list);
  465. /* temporarily stop TX queue when the freelist empties */
  466. if (list_empty(&dev->tx_reqs))
  467. netif_stop_queue(net);
  468. spin_unlock_irqrestore(&dev->req_lock, flags);
  469. /* no buffer copies needed, unless the network stack did it
  470. * or the hardware can't use skb buffers.
  471. * or there's not enough space for extra headers we need
  472. */
  473. if (dev->wrap) {
  474. unsigned long flags;
  475. spin_lock_irqsave(&dev->lock, flags);
  476. if (dev->port_usb)
  477. skb = dev->wrap(dev->port_usb, skb);
  478. spin_unlock_irqrestore(&dev->lock, flags);
  479. if (!skb)
  480. goto drop;
  481. length = skb->len;
  482. }
  483. req->buf = skb->data;
  484. req->context = skb;
  485. req->complete = tx_complete;
  486. /* use zlp framing on tx for strict CDC-Ether conformance,
  487. * though any robust network rx path ignores extra padding.
  488. * and some hardware doesn't like to write zlps.
  489. */
  490. req->zero = 1;
  491. if (!dev->zlp && (length % in->maxpacket) == 0)
  492. length++;
  493. req->length = length;
  494. /* throttle highspeed IRQ rate back slightly */
  495. if (gadget_is_dualspeed(dev->gadget))
  496. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  497. ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
  498. : 0;
  499. retval = usb_ep_queue(in, req, GFP_ATOMIC);
  500. switch (retval) {
  501. default:
  502. DBG(dev, "tx queue err %d\n", retval);
  503. break;
  504. case 0:
  505. net->trans_start = jiffies;
  506. atomic_inc(&dev->tx_qlen);
  507. }
  508. if (retval) {
  509. dev_kfree_skb_any(skb);
  510. drop:
  511. dev->net->stats.tx_dropped++;
  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. if (link->close)
  559. link->close(link);
  560. /* NOTE: we have no abort-queue primitive we could use
  561. * to cancel all pending I/O. Instead, we disable then
  562. * reenable the endpoints ... this idiom may leave toggle
  563. * wrong, but that's a self-correcting error.
  564. *
  565. * REVISIT: we *COULD* just let the transfers complete at
  566. * their own pace; the network stack can handle old packets.
  567. * For the moment we leave this here, since it works.
  568. */
  569. usb_ep_disable(link->in_ep);
  570. usb_ep_disable(link->out_ep);
  571. if (netif_carrier_ok(net)) {
  572. DBG(dev, "host still using in/out endpoints\n");
  573. usb_ep_enable(link->in_ep, link->in);
  574. usb_ep_enable(link->out_ep, link->out);
  575. }
  576. }
  577. spin_unlock_irqrestore(&dev->lock, flags);
  578. return 0;
  579. }
  580. /*-------------------------------------------------------------------------*/
  581. /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
  582. static char *dev_addr;
  583. module_param(dev_addr, charp, S_IRUGO);
  584. MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
  585. /* this address is invisible to ifconfig */
  586. static char *host_addr;
  587. module_param(host_addr, charp, S_IRUGO);
  588. MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
  589. static u8 __init nibble(unsigned char c)
  590. {
  591. if (isdigit(c))
  592. return c - '0';
  593. c = toupper(c);
  594. if (isxdigit(c))
  595. return 10 + c - 'A';
  596. return 0;
  597. }
  598. static int __init get_ether_addr(const char *str, u8 *dev_addr)
  599. {
  600. if (str) {
  601. unsigned i;
  602. for (i = 0; i < 6; i++) {
  603. unsigned char num;
  604. if ((*str == '.') || (*str == ':'))
  605. str++;
  606. num = nibble(*str++) << 4;
  607. num |= (nibble(*str++));
  608. dev_addr [i] = num;
  609. }
  610. if (is_valid_ether_addr(dev_addr))
  611. return 0;
  612. }
  613. random_ether_addr(dev_addr);
  614. return 1;
  615. }
  616. static struct eth_dev *the_dev;
  617. static const struct net_device_ops eth_netdev_ops = {
  618. .ndo_open = eth_open,
  619. .ndo_stop = eth_stop,
  620. .ndo_start_xmit = eth_start_xmit,
  621. .ndo_change_mtu = ueth_change_mtu,
  622. .ndo_set_mac_address = eth_mac_addr,
  623. .ndo_validate_addr = eth_validate_addr,
  624. };
  625. static struct device_type gadget_type = {
  626. .name = "gadget",
  627. };
  628. /**
  629. * gether_setup - initialize one ethernet-over-usb link
  630. * @g: gadget to associated with these links
  631. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  632. * host side of the link is recorded
  633. * Context: may sleep
  634. *
  635. * This sets up the single network link that may be exported by a
  636. * gadget driver using this framework. The link layer addresses are
  637. * set up using module parameters.
  638. *
  639. * Returns negative errno, or zero on success
  640. */
  641. int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
  642. {
  643. struct eth_dev *dev;
  644. struct net_device *net;
  645. int status;
  646. if (the_dev)
  647. return -EBUSY;
  648. net = alloc_etherdev(sizeof *dev);
  649. if (!net)
  650. return -ENOMEM;
  651. dev = netdev_priv(net);
  652. spin_lock_init(&dev->lock);
  653. spin_lock_init(&dev->req_lock);
  654. INIT_WORK(&dev->work, eth_work);
  655. INIT_LIST_HEAD(&dev->tx_reqs);
  656. INIT_LIST_HEAD(&dev->rx_reqs);
  657. skb_queue_head_init(&dev->rx_frames);
  658. /* network device setup */
  659. dev->net = net;
  660. strcpy(net->name, "usb%d");
  661. if (get_ether_addr(dev_addr, net->dev_addr))
  662. dev_warn(&g->dev,
  663. "using random %s ethernet address\n", "self");
  664. if (get_ether_addr(host_addr, dev->host_mac))
  665. dev_warn(&g->dev,
  666. "using random %s ethernet address\n", "host");
  667. if (ethaddr)
  668. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  669. net->netdev_ops = &eth_netdev_ops;
  670. SET_ETHTOOL_OPS(net, &ops);
  671. /* two kinds of host-initiated state changes:
  672. * - iff DATA transfer is active, carrier is "on"
  673. * - tx queueing enabled if open *and* carrier is "on"
  674. */
  675. netif_stop_queue(net);
  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. free_netdev(the_dev->net);
  703. /* assuming we used keventd, it must quiesce too */
  704. flush_scheduled_work();
  705. the_dev = NULL;
  706. }
  707. /**
  708. * gether_connect - notify network layer that USB link is active
  709. * @link: the USB link, set up with endpoints, descriptors matching
  710. * current device speed, and any framing wrapper(s) set up.
  711. * Context: irqs blocked
  712. *
  713. * This is called to activate endpoints and let the network layer know
  714. * the connection is active ("carrier detect"). It may cause the I/O
  715. * queues to open and start letting network packets flow, but will in
  716. * any case activate the endpoints so that they respond properly to the
  717. * USB host.
  718. *
  719. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  720. * indicate some error code (negative errno), ep->driver_data values
  721. * have been overwritten.
  722. */
  723. struct net_device *gether_connect(struct gether *link)
  724. {
  725. struct eth_dev *dev = the_dev;
  726. int result = 0;
  727. if (!dev)
  728. return ERR_PTR(-EINVAL);
  729. link->in_ep->driver_data = dev;
  730. result = usb_ep_enable(link->in_ep, link->in);
  731. if (result != 0) {
  732. DBG(dev, "enable %s --> %d\n",
  733. link->in_ep->name, result);
  734. goto fail0;
  735. }
  736. link->out_ep->driver_data = dev;
  737. result = usb_ep_enable(link->out_ep, link->out);
  738. if (result != 0) {
  739. DBG(dev, "enable %s --> %d\n",
  740. link->out_ep->name, result);
  741. goto fail1;
  742. }
  743. if (result == 0)
  744. result = alloc_requests(dev, link, qlen(dev->gadget));
  745. if (result == 0) {
  746. dev->zlp = link->is_zlp_ok;
  747. DBG(dev, "qlen %d\n", qlen(dev->gadget));
  748. dev->header_len = link->header_len;
  749. dev->unwrap = link->unwrap;
  750. dev->wrap = link->wrap;
  751. spin_lock(&dev->lock);
  752. dev->port_usb = link;
  753. link->ioport = dev;
  754. if (netif_running(dev->net)) {
  755. if (link->open)
  756. link->open(link);
  757. } else {
  758. if (link->close)
  759. link->close(link);
  760. }
  761. spin_unlock(&dev->lock);
  762. netif_carrier_on(dev->net);
  763. if (netif_running(dev->net))
  764. eth_start(dev, GFP_ATOMIC);
  765. /* on error, disable any endpoints */
  766. } else {
  767. (void) usb_ep_disable(link->out_ep);
  768. fail1:
  769. (void) usb_ep_disable(link->in_ep);
  770. }
  771. fail0:
  772. /* caller is responsible for cleanup on error */
  773. if (result < 0)
  774. return ERR_PTR(result);
  775. return dev->net;
  776. }
  777. /**
  778. * gether_disconnect - notify network layer that USB link is inactive
  779. * @link: the USB link, on which gether_connect() was called
  780. * Context: irqs blocked
  781. *
  782. * This is called to deactivate endpoints and let the network layer know
  783. * the connection went inactive ("no carrier").
  784. *
  785. * On return, the state is as if gether_connect() had never been called.
  786. * The endpoints are inactive, and accordingly without active USB I/O.
  787. * Pointers to endpoint descriptors and endpoint private data are nulled.
  788. */
  789. void gether_disconnect(struct gether *link)
  790. {
  791. struct eth_dev *dev = link->ioport;
  792. struct usb_request *req;
  793. WARN_ON(!dev);
  794. if (!dev)
  795. return;
  796. DBG(dev, "%s\n", __func__);
  797. netif_stop_queue(dev->net);
  798. netif_carrier_off(dev->net);
  799. /* disable endpoints, forcing (synchronous) completion
  800. * of all pending i/o. then free the request objects
  801. * and forget about the endpoints.
  802. */
  803. usb_ep_disable(link->in_ep);
  804. spin_lock(&dev->req_lock);
  805. while (!list_empty(&dev->tx_reqs)) {
  806. req = container_of(dev->tx_reqs.next,
  807. struct usb_request, list);
  808. list_del(&req->list);
  809. spin_unlock(&dev->req_lock);
  810. usb_ep_free_request(link->in_ep, req);
  811. spin_lock(&dev->req_lock);
  812. }
  813. spin_unlock(&dev->req_lock);
  814. link->in_ep->driver_data = NULL;
  815. link->in = NULL;
  816. usb_ep_disable(link->out_ep);
  817. spin_lock(&dev->req_lock);
  818. while (!list_empty(&dev->rx_reqs)) {
  819. req = container_of(dev->rx_reqs.next,
  820. struct usb_request, list);
  821. list_del(&req->list);
  822. spin_unlock(&dev->req_lock);
  823. usb_ep_free_request(link->out_ep, req);
  824. spin_lock(&dev->req_lock);
  825. }
  826. spin_unlock(&dev->req_lock);
  827. link->out_ep->driver_data = NULL;
  828. link->out = NULL;
  829. /* finish forgetting about this USB link episode */
  830. dev->header_len = 0;
  831. dev->unwrap = NULL;
  832. dev->wrap = NULL;
  833. spin_lock(&dev->lock);
  834. dev->port_usb = NULL;
  835. link->ioport = NULL;
  836. spin_unlock(&dev->lock);
  837. }