u_ether.c 24 KB

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