u_ether.c 24 KB

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