u_ether.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
  4. *
  5. * Copyright (C) 2003-2005,2008 David Brownell
  6. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  7. * Copyright (C) 2008 Nokia Corporation
  8. */
  9. /* #define VERBOSE_DEBUG */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/gfp.h>
  13. #include <linux/device.h>
  14. #include <linux/ctype.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/string_helpers.h>
  19. #include <linux/usb/composite.h>
  20. #include "u_ether.h"
  21. /*
  22. * This component encapsulates the Ethernet link glue needed to provide
  23. * one (!) network link through the USB gadget stack, normally "usb0".
  24. *
  25. * The control and data models are handled by the function driver which
  26. * connects to this code; such as CDC Ethernet (ECM or EEM),
  27. * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
  28. * management.
  29. *
  30. * Link level addressing is handled by this component using module
  31. * parameters; if no such parameters are provided, random link level
  32. * addresses are used. Each end of the link uses one address. The
  33. * host end address is exported in various ways, and is often recorded
  34. * in configuration databases.
  35. *
  36. * The driver which assembles each configuration using such a link is
  37. * responsible for ensuring that each configuration includes at most one
  38. * instance of is network link. (The network layer provides ways for
  39. * this single "physical" link to be used by multiple virtual links.)
  40. */
  41. #define UETH__VERSION "29-May-2008"
  42. /* Experiments show that both Linux and Windows hosts allow up to 16k
  43. * frame sizes. Set the max MTU size to 15k+52 to prevent allocating 32k
  44. * blocks and still have efficient handling. */
  45. #define GETHER_MAX_MTU_SIZE 15412
  46. #define GETHER_MAX_ETH_FRAME_LEN (GETHER_MAX_MTU_SIZE + ETH_HLEN)
  47. struct eth_dev {
  48. /* lock is held while accessing port_usb
  49. */
  50. spinlock_t lock;
  51. struct gether *port_usb;
  52. struct net_device *net;
  53. struct usb_gadget *gadget;
  54. spinlock_t req_lock; /* guard {rx,tx}_reqs */
  55. struct list_head tx_reqs, rx_reqs;
  56. atomic_t tx_qlen;
  57. struct sk_buff_head rx_frames;
  58. unsigned qmult;
  59. unsigned header_len;
  60. struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
  61. int (*unwrap)(struct gether *,
  62. struct sk_buff *skb,
  63. struct sk_buff_head *list);
  64. struct work_struct work;
  65. unsigned long todo;
  66. #define WORK_RX_MEMORY 0
  67. bool zlp;
  68. bool no_skb_reserve;
  69. bool ifname_set;
  70. u8 host_mac[ETH_ALEN];
  71. u8 dev_mac[ETH_ALEN];
  72. };
  73. /*-------------------------------------------------------------------------*/
  74. #define RX_EXTRA 20 /* bytes guarding against rx overflows */
  75. #define DEFAULT_QLEN 2 /* double buffering by default */
  76. /* use deeper queues at high/super speed */
  77. static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
  78. {
  79. if (gadget->speed == USB_SPEED_HIGH || gadget->speed >= USB_SPEED_SUPER)
  80. return qmult * DEFAULT_QLEN;
  81. else
  82. return DEFAULT_QLEN;
  83. }
  84. /*-------------------------------------------------------------------------*/
  85. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  86. static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
  87. {
  88. struct eth_dev *dev = netdev_priv(net);
  89. strscpy(p->driver, "g_ether", sizeof(p->driver));
  90. strscpy(p->version, UETH__VERSION, sizeof(p->version));
  91. strscpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
  92. strscpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
  93. }
  94. /* REVISIT can also support:
  95. * - WOL (by tracking suspends and issuing remote wakeup)
  96. * - msglevel (implies updated messaging)
  97. * - ... probably more ethtool ops
  98. */
  99. static const struct ethtool_ops ops = {
  100. .get_drvinfo = eth_get_drvinfo,
  101. .get_link = ethtool_op_get_link,
  102. };
  103. static void defer_kevent(struct eth_dev *dev, int flag)
  104. {
  105. if (test_and_set_bit(flag, &dev->todo))
  106. return;
  107. if (!schedule_work(&dev->work))
  108. ERROR(dev, "kevent %d may have been dropped\n", flag);
  109. else
  110. DBG(dev, "kevent %d scheduled\n", flag);
  111. }
  112. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  113. static int
  114. rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
  115. {
  116. struct usb_gadget *g = dev->gadget;
  117. struct sk_buff *skb;
  118. int retval = -ENOMEM;
  119. size_t size = 0;
  120. struct usb_ep *out;
  121. unsigned long flags;
  122. spin_lock_irqsave(&dev->lock, flags);
  123. if (dev->port_usb)
  124. out = dev->port_usb->out_ep;
  125. else
  126. out = NULL;
  127. if (!out)
  128. {
  129. spin_unlock_irqrestore(&dev->lock, flags);
  130. return -ENOTCONN;
  131. }
  132. /* Padding up to RX_EXTRA handles minor disagreements with host.
  133. * Normally we use the USB "terminate on short read" convention;
  134. * so allow up to (N*maxpacket), since that memory is normally
  135. * already allocated. Some hardware doesn't deal well with short
  136. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  137. * byte off the end (to force hardware errors on overflow).
  138. *
  139. * RNDIS uses internal framing, and explicitly allows senders to
  140. * pad to end-of-packet. That's potentially nice for speed, but
  141. * means receivers can't recover lost synch on their own (because
  142. * new packets don't only start after a short RX).
  143. */
  144. size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
  145. size += dev->port_usb->header_len;
  146. if (g->quirk_ep_out_aligned_size) {
  147. size += out->maxpacket - 1;
  148. size -= size % out->maxpacket;
  149. }
  150. if (dev->port_usb->is_fixed)
  151. size = max_t(size_t, size, dev->port_usb->fixed_out_len);
  152. spin_unlock_irqrestore(&dev->lock, flags);
  153. skb = __netdev_alloc_skb(dev->net, size + NET_IP_ALIGN, gfp_flags);
  154. if (skb == NULL) {
  155. DBG(dev, "no rx skb\n");
  156. goto enomem;
  157. }
  158. /* Some platforms perform better when IP packets are aligned,
  159. * but on at least one, checksumming fails otherwise. Note:
  160. * RNDIS headers involve variable numbers of LE32 values.
  161. */
  162. if (likely(!dev->no_skb_reserve))
  163. skb_reserve(skb, NET_IP_ALIGN);
  164. req->buf = skb->data;
  165. req->length = size;
  166. req->complete = rx_complete;
  167. req->context = skb;
  168. retval = usb_ep_queue(out, req, gfp_flags);
  169. if (retval == -ENOMEM)
  170. enomem:
  171. defer_kevent(dev, WORK_RX_MEMORY);
  172. if (retval) {
  173. DBG(dev, "rx submit --> %d\n", retval);
  174. if (skb)
  175. dev_kfree_skb_any(skb);
  176. spin_lock_irqsave(&dev->req_lock, flags);
  177. list_add(&req->list, &dev->rx_reqs);
  178. spin_unlock_irqrestore(&dev->req_lock, flags);
  179. }
  180. return retval;
  181. }
  182. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  183. {
  184. struct sk_buff *skb = req->context, *skb2;
  185. struct eth_dev *dev = ep->driver_data;
  186. int status = req->status;
  187. switch (status) {
  188. /* normal completion */
  189. case 0:
  190. skb_put(skb, req->actual);
  191. if (dev->unwrap) {
  192. unsigned long flags;
  193. spin_lock_irqsave(&dev->lock, flags);
  194. if (dev->port_usb) {
  195. status = dev->unwrap(dev->port_usb,
  196. skb,
  197. &dev->rx_frames);
  198. } else {
  199. dev_kfree_skb_any(skb);
  200. status = -ENOTCONN;
  201. }
  202. spin_unlock_irqrestore(&dev->lock, flags);
  203. } else {
  204. skb_queue_tail(&dev->rx_frames, skb);
  205. }
  206. skb = NULL;
  207. skb2 = skb_dequeue(&dev->rx_frames);
  208. while (skb2) {
  209. if (status < 0
  210. || ETH_HLEN > skb2->len
  211. || skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
  212. dev->net->stats.rx_errors++;
  213. dev->net->stats.rx_length_errors++;
  214. DBG(dev, "rx length %d\n", skb2->len);
  215. dev_kfree_skb_any(skb2);
  216. goto next_frame;
  217. }
  218. skb2->protocol = eth_type_trans(skb2, dev->net);
  219. dev->net->stats.rx_packets++;
  220. dev->net->stats.rx_bytes += skb2->len;
  221. /* no buffer copies needed, unless hardware can't
  222. * use skb buffers.
  223. */
  224. status = netif_rx(skb2);
  225. next_frame:
  226. skb2 = skb_dequeue(&dev->rx_frames);
  227. }
  228. break;
  229. /* software-driven interface shutdown */
  230. case -ECONNRESET: /* unlink */
  231. case -ESHUTDOWN: /* disconnect etc */
  232. VDBG(dev, "rx shutdown, code %d\n", status);
  233. goto quiesce;
  234. /* for hardware automagic (such as pxa) */
  235. case -ECONNABORTED: /* endpoint reset */
  236. DBG(dev, "rx %s reset\n", ep->name);
  237. defer_kevent(dev, WORK_RX_MEMORY);
  238. quiesce:
  239. dev_kfree_skb_any(skb);
  240. goto clean;
  241. /* data overrun */
  242. case -EOVERFLOW:
  243. dev->net->stats.rx_over_errors++;
  244. fallthrough;
  245. default:
  246. dev->net->stats.rx_errors++;
  247. DBG(dev, "rx status %d\n", status);
  248. break;
  249. }
  250. if (skb)
  251. dev_kfree_skb_any(skb);
  252. if (!netif_running(dev->net)) {
  253. clean:
  254. spin_lock(&dev->req_lock);
  255. list_add(&req->list, &dev->rx_reqs);
  256. spin_unlock(&dev->req_lock);
  257. req = NULL;
  258. }
  259. if (req)
  260. rx_submit(dev, req, GFP_ATOMIC);
  261. }
  262. static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
  263. {
  264. unsigned i;
  265. struct usb_request *req;
  266. if (!n)
  267. return -ENOMEM;
  268. /* queue/recycle up to N requests */
  269. i = n;
  270. list_for_each_entry(req, list, list) {
  271. if (i-- == 0)
  272. goto extra;
  273. }
  274. while (i--) {
  275. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  276. if (!req)
  277. return list_empty(list) ? -ENOMEM : 0;
  278. list_add(&req->list, list);
  279. }
  280. return 0;
  281. extra:
  282. /* free extras */
  283. for (;;) {
  284. struct list_head *next;
  285. next = req->list.next;
  286. list_del(&req->list);
  287. usb_ep_free_request(ep, req);
  288. if (next == list)
  289. break;
  290. req = container_of(next, struct usb_request, list);
  291. }
  292. return 0;
  293. }
  294. static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  295. {
  296. int status;
  297. spin_lock(&dev->req_lock);
  298. status = prealloc(&dev->tx_reqs, link->in_ep, n);
  299. if (status < 0)
  300. goto fail;
  301. status = prealloc(&dev->rx_reqs, link->out_ep, n);
  302. if (status < 0)
  303. goto fail;
  304. goto done;
  305. fail:
  306. DBG(dev, "can't alloc requests\n");
  307. done:
  308. spin_unlock(&dev->req_lock);
  309. return status;
  310. }
  311. static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
  312. {
  313. struct usb_request *req;
  314. unsigned long flags;
  315. /* fill unused rxq slots with some skb */
  316. spin_lock_irqsave(&dev->req_lock, flags);
  317. while (!list_empty(&dev->rx_reqs)) {
  318. req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
  319. list_del_init(&req->list);
  320. spin_unlock_irqrestore(&dev->req_lock, flags);
  321. if (rx_submit(dev, req, gfp_flags) < 0) {
  322. defer_kevent(dev, WORK_RX_MEMORY);
  323. return;
  324. }
  325. spin_lock_irqsave(&dev->req_lock, flags);
  326. }
  327. spin_unlock_irqrestore(&dev->req_lock, flags);
  328. }
  329. static void eth_work(struct work_struct *work)
  330. {
  331. struct eth_dev *dev = container_of(work, struct eth_dev, work);
  332. if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
  333. if (netif_running(dev->net))
  334. rx_fill(dev, GFP_KERNEL);
  335. }
  336. if (dev->todo)
  337. DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
  338. }
  339. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  340. {
  341. struct sk_buff *skb = req->context;
  342. struct eth_dev *dev = ep->driver_data;
  343. switch (req->status) {
  344. default:
  345. dev->net->stats.tx_errors++;
  346. VDBG(dev, "tx err %d\n", req->status);
  347. fallthrough;
  348. case -ECONNRESET: /* unlink */
  349. case -ESHUTDOWN: /* disconnect etc */
  350. dev_kfree_skb_any(skb);
  351. break;
  352. case 0:
  353. dev->net->stats.tx_bytes += skb->len;
  354. dev_consume_skb_any(skb);
  355. }
  356. dev->net->stats.tx_packets++;
  357. spin_lock(&dev->req_lock);
  358. list_add(&req->list, &dev->tx_reqs);
  359. spin_unlock(&dev->req_lock);
  360. atomic_dec(&dev->tx_qlen);
  361. if (netif_carrier_ok(dev->net))
  362. netif_wake_queue(dev->net);
  363. }
  364. static inline int is_promisc(u16 cdc_filter)
  365. {
  366. return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  367. }
  368. static int ether_wakeup_host(struct gether *port)
  369. {
  370. int ret;
  371. struct usb_function *func = &port->func;
  372. struct usb_gadget *gadget = func->config->cdev->gadget;
  373. if (func->func_suspended)
  374. ret = usb_func_wakeup(func);
  375. else
  376. ret = usb_gadget_wakeup(gadget);
  377. return ret;
  378. }
  379. static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
  380. struct net_device *net)
  381. {
  382. struct eth_dev *dev = netdev_priv(net);
  383. int length = 0;
  384. int retval;
  385. struct usb_request *req = NULL;
  386. unsigned long flags;
  387. struct usb_ep *in;
  388. u16 cdc_filter;
  389. spin_lock_irqsave(&dev->lock, flags);
  390. if (dev->port_usb) {
  391. in = dev->port_usb->in_ep;
  392. cdc_filter = dev->port_usb->cdc_filter;
  393. } else {
  394. in = NULL;
  395. cdc_filter = 0;
  396. }
  397. if (dev->port_usb && dev->port_usb->is_suspend) {
  398. DBG(dev, "Port suspended. Triggering wakeup\n");
  399. netif_stop_queue(net);
  400. spin_unlock_irqrestore(&dev->lock, flags);
  401. ether_wakeup_host(dev->port_usb);
  402. return NETDEV_TX_BUSY;
  403. }
  404. spin_unlock_irqrestore(&dev->lock, flags);
  405. if (!in) {
  406. if (skb)
  407. dev_kfree_skb_any(skb);
  408. return NETDEV_TX_OK;
  409. }
  410. /* apply outgoing CDC or RNDIS filters */
  411. if (skb && !is_promisc(cdc_filter)) {
  412. u8 *dest = skb->data;
  413. if (is_multicast_ether_addr(dest)) {
  414. u16 type;
  415. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  416. * SET_ETHERNET_MULTICAST_FILTERS requests
  417. */
  418. if (is_broadcast_ether_addr(dest))
  419. type = USB_CDC_PACKET_TYPE_BROADCAST;
  420. else
  421. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  422. if (!(cdc_filter & type)) {
  423. dev_kfree_skb_any(skb);
  424. return NETDEV_TX_OK;
  425. }
  426. }
  427. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  428. }
  429. spin_lock_irqsave(&dev->req_lock, flags);
  430. /*
  431. * this freelist can be empty if an interrupt triggered disconnect()
  432. * and reconfigured the gadget (shutting down this queue) after the
  433. * network stack decided to xmit but before we got the spinlock.
  434. */
  435. if (list_empty(&dev->tx_reqs)) {
  436. spin_unlock_irqrestore(&dev->req_lock, flags);
  437. return NETDEV_TX_BUSY;
  438. }
  439. req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
  440. list_del(&req->list);
  441. /* temporarily stop TX queue when the freelist empties */
  442. if (list_empty(&dev->tx_reqs))
  443. netif_stop_queue(net);
  444. spin_unlock_irqrestore(&dev->req_lock, flags);
  445. /* no buffer copies needed, unless the network stack did it
  446. * or the hardware can't use skb buffers.
  447. * or there's not enough space for extra headers we need
  448. */
  449. if (dev->wrap) {
  450. unsigned long flags;
  451. spin_lock_irqsave(&dev->lock, flags);
  452. if (dev->port_usb)
  453. skb = dev->wrap(dev->port_usb, skb);
  454. spin_unlock_irqrestore(&dev->lock, flags);
  455. if (!skb) {
  456. /* Multi frame CDC protocols may store the frame for
  457. * later which is not a dropped frame.
  458. */
  459. if (dev->port_usb &&
  460. dev->port_usb->supports_multi_frame)
  461. goto multiframe;
  462. goto drop;
  463. }
  464. }
  465. length = skb->len;
  466. req->buf = skb->data;
  467. req->context = skb;
  468. req->complete = tx_complete;
  469. /* NCM requires no zlp if transfer is dwNtbInMaxSize */
  470. if (dev->port_usb &&
  471. dev->port_usb->is_fixed &&
  472. length == dev->port_usb->fixed_in_len &&
  473. (length % in->maxpacket) == 0)
  474. req->zero = 0;
  475. else
  476. req->zero = 1;
  477. /* use zlp framing on tx for strict CDC-Ether conformance,
  478. * though any robust network rx path ignores extra padding.
  479. * and some hardware doesn't like to write zlps.
  480. */
  481. if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
  482. length++;
  483. req->length = length;
  484. retval = usb_ep_queue(in, req, GFP_ATOMIC);
  485. switch (retval) {
  486. default:
  487. DBG(dev, "tx queue err %d\n", retval);
  488. break;
  489. case 0:
  490. netif_trans_update(net);
  491. atomic_inc(&dev->tx_qlen);
  492. }
  493. if (retval) {
  494. dev_kfree_skb_any(skb);
  495. drop:
  496. dev->net->stats.tx_dropped++;
  497. multiframe:
  498. spin_lock_irqsave(&dev->req_lock, flags);
  499. if (list_empty(&dev->tx_reqs))
  500. netif_start_queue(net);
  501. list_add(&req->list, &dev->tx_reqs);
  502. spin_unlock_irqrestore(&dev->req_lock, flags);
  503. }
  504. return NETDEV_TX_OK;
  505. }
  506. /*-------------------------------------------------------------------------*/
  507. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
  508. {
  509. DBG(dev, "%s\n", __func__);
  510. /* fill the rx queue */
  511. rx_fill(dev, gfp_flags);
  512. /* and open the tx floodgates */
  513. atomic_set(&dev->tx_qlen, 0);
  514. netif_wake_queue(dev->net);
  515. }
  516. static int eth_open(struct net_device *net)
  517. {
  518. struct eth_dev *dev = netdev_priv(net);
  519. struct gether *link;
  520. DBG(dev, "%s\n", __func__);
  521. if (netif_carrier_ok(dev->net))
  522. eth_start(dev, GFP_KERNEL);
  523. spin_lock_irq(&dev->lock);
  524. link = dev->port_usb;
  525. if (link && link->open)
  526. link->open(link);
  527. spin_unlock_irq(&dev->lock);
  528. return 0;
  529. }
  530. static int eth_stop(struct net_device *net)
  531. {
  532. struct eth_dev *dev = netdev_priv(net);
  533. unsigned long flags;
  534. VDBG(dev, "%s\n", __func__);
  535. netif_stop_queue(net);
  536. DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
  537. dev->net->stats.rx_packets, dev->net->stats.tx_packets,
  538. dev->net->stats.rx_errors, dev->net->stats.tx_errors
  539. );
  540. /* ensure there are no more active requests */
  541. spin_lock_irqsave(&dev->lock, flags);
  542. if (dev->port_usb) {
  543. struct gether *link = dev->port_usb;
  544. const struct usb_endpoint_descriptor *in;
  545. const struct usb_endpoint_descriptor *out;
  546. if (link->close)
  547. link->close(link);
  548. /* NOTE: we have no abort-queue primitive we could use
  549. * to cancel all pending I/O. Instead, we disable then
  550. * reenable the endpoints ... this idiom may leave toggle
  551. * wrong, but that's a self-correcting error.
  552. *
  553. * REVISIT: we *COULD* just let the transfers complete at
  554. * their own pace; the network stack can handle old packets.
  555. * For the moment we leave this here, since it works.
  556. */
  557. in = link->in_ep->desc;
  558. out = link->out_ep->desc;
  559. usb_ep_disable(link->in_ep);
  560. usb_ep_disable(link->out_ep);
  561. if (netif_carrier_ok(net)) {
  562. DBG(dev, "host still using in/out endpoints\n");
  563. link->in_ep->desc = in;
  564. link->out_ep->desc = out;
  565. usb_ep_enable(link->in_ep);
  566. usb_ep_enable(link->out_ep);
  567. }
  568. }
  569. spin_unlock_irqrestore(&dev->lock, flags);
  570. return 0;
  571. }
  572. /*-------------------------------------------------------------------------*/
  573. static int get_ether_addr(const char *str, u8 *dev_addr)
  574. {
  575. if (str) {
  576. unsigned i;
  577. for (i = 0; i < 6; i++) {
  578. unsigned char num;
  579. if ((*str == '.') || (*str == ':'))
  580. str++;
  581. num = hex_to_bin(*str++) << 4;
  582. num |= hex_to_bin(*str++);
  583. dev_addr [i] = num;
  584. }
  585. if (is_valid_ether_addr(dev_addr))
  586. return 0;
  587. }
  588. eth_random_addr(dev_addr);
  589. return 1;
  590. }
  591. static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
  592. {
  593. if (len < 18)
  594. return -EINVAL;
  595. snprintf(str, len, "%pM", dev_addr);
  596. return 18;
  597. }
  598. static const struct net_device_ops eth_netdev_ops = {
  599. .ndo_open = eth_open,
  600. .ndo_stop = eth_stop,
  601. .ndo_start_xmit = eth_start_xmit,
  602. .ndo_set_mac_address = eth_mac_addr,
  603. .ndo_validate_addr = eth_validate_addr,
  604. };
  605. static const struct device_type gadget_type = {
  606. .name = "gadget",
  607. };
  608. /*
  609. * gether_setup_name - initialize one ethernet-over-usb link
  610. * @g: gadget to associated with these links
  611. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  612. * host side of the link is recorded
  613. * @netname: name for network device (for example, "usb")
  614. * Context: may sleep
  615. *
  616. * This sets up the single network link that may be exported by a
  617. * gadget driver using this framework. The link layer addresses are
  618. * set up using module parameters.
  619. *
  620. * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
  621. */
  622. struct eth_dev *gether_setup_name(struct usb_gadget *g,
  623. const char *dev_addr, const char *host_addr,
  624. u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
  625. {
  626. struct eth_dev *dev;
  627. struct net_device *net;
  628. int status;
  629. u8 addr[ETH_ALEN];
  630. net = alloc_etherdev(sizeof *dev);
  631. if (!net)
  632. return ERR_PTR(-ENOMEM);
  633. dev = netdev_priv(net);
  634. spin_lock_init(&dev->lock);
  635. spin_lock_init(&dev->req_lock);
  636. INIT_WORK(&dev->work, eth_work);
  637. INIT_LIST_HEAD(&dev->tx_reqs);
  638. INIT_LIST_HEAD(&dev->rx_reqs);
  639. skb_queue_head_init(&dev->rx_frames);
  640. /* network device setup */
  641. dev->net = net;
  642. dev->qmult = qmult;
  643. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  644. if (get_ether_addr(dev_addr, addr)) {
  645. net->addr_assign_type = NET_ADDR_RANDOM;
  646. dev_warn(&g->dev,
  647. "using random %s ethernet address\n", "self");
  648. } else {
  649. net->addr_assign_type = NET_ADDR_SET;
  650. }
  651. eth_hw_addr_set(net, addr);
  652. if (get_ether_addr(host_addr, dev->host_mac))
  653. dev_warn(&g->dev,
  654. "using random %s ethernet address\n", "host");
  655. if (ethaddr)
  656. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  657. net->netdev_ops = &eth_netdev_ops;
  658. net->ethtool_ops = &ops;
  659. /* MTU range: 14 - 15412 */
  660. net->min_mtu = ETH_HLEN;
  661. net->max_mtu = GETHER_MAX_MTU_SIZE;
  662. dev->gadget = g;
  663. SET_NETDEV_DEV(net, &g->dev);
  664. SET_NETDEV_DEVTYPE(net, &gadget_type);
  665. status = register_netdev(net);
  666. if (status < 0) {
  667. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  668. free_netdev(net);
  669. dev = ERR_PTR(status);
  670. } else {
  671. INFO(dev, "MAC %pM\n", net->dev_addr);
  672. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  673. /*
  674. * two kinds of host-initiated state changes:
  675. * - iff DATA transfer is active, carrier is "on"
  676. * - tx queueing enabled if open *and* carrier is "on"
  677. */
  678. netif_carrier_off(net);
  679. }
  680. return dev;
  681. }
  682. EXPORT_SYMBOL_GPL(gether_setup_name);
  683. struct net_device *gether_setup_name_default(const char *netname)
  684. {
  685. struct net_device *net;
  686. struct eth_dev *dev;
  687. net = alloc_etherdev(sizeof(*dev));
  688. if (!net)
  689. return ERR_PTR(-ENOMEM);
  690. dev = netdev_priv(net);
  691. spin_lock_init(&dev->lock);
  692. spin_lock_init(&dev->req_lock);
  693. INIT_WORK(&dev->work, eth_work);
  694. INIT_LIST_HEAD(&dev->tx_reqs);
  695. INIT_LIST_HEAD(&dev->rx_reqs);
  696. skb_queue_head_init(&dev->rx_frames);
  697. /* network device setup */
  698. dev->net = net;
  699. dev->qmult = QMULT_DEFAULT;
  700. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  701. eth_random_addr(dev->dev_mac);
  702. /* by default we always have a random MAC address */
  703. net->addr_assign_type = NET_ADDR_RANDOM;
  704. eth_random_addr(dev->host_mac);
  705. net->netdev_ops = &eth_netdev_ops;
  706. net->ethtool_ops = &ops;
  707. SET_NETDEV_DEVTYPE(net, &gadget_type);
  708. /* MTU range: 14 - 15412 */
  709. net->min_mtu = ETH_HLEN;
  710. net->max_mtu = GETHER_MAX_MTU_SIZE;
  711. return net;
  712. }
  713. EXPORT_SYMBOL_GPL(gether_setup_name_default);
  714. int gether_register_netdev(struct net_device *net)
  715. {
  716. struct eth_dev *dev;
  717. struct usb_gadget *g;
  718. int status;
  719. if (!net->dev.parent)
  720. return -EINVAL;
  721. dev = netdev_priv(net);
  722. g = dev->gadget;
  723. eth_hw_addr_set(net, dev->dev_mac);
  724. status = register_netdev(net);
  725. if (status < 0) {
  726. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  727. return status;
  728. } else {
  729. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  730. INFO(dev, "MAC %pM\n", dev->dev_mac);
  731. /* two kinds of host-initiated state changes:
  732. * - iff DATA transfer is active, carrier is "on"
  733. * - tx queueing enabled if open *and* carrier is "on"
  734. */
  735. netif_carrier_off(net);
  736. }
  737. return status;
  738. }
  739. EXPORT_SYMBOL_GPL(gether_register_netdev);
  740. void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
  741. {
  742. struct eth_dev *dev;
  743. dev = netdev_priv(net);
  744. dev->gadget = g;
  745. SET_NETDEV_DEV(net, &g->dev);
  746. }
  747. EXPORT_SYMBOL_GPL(gether_set_gadget);
  748. int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
  749. {
  750. struct eth_dev *dev;
  751. u8 new_addr[ETH_ALEN];
  752. dev = netdev_priv(net);
  753. if (get_ether_addr(dev_addr, new_addr))
  754. return -EINVAL;
  755. memcpy(dev->dev_mac, new_addr, ETH_ALEN);
  756. net->addr_assign_type = NET_ADDR_SET;
  757. return 0;
  758. }
  759. EXPORT_SYMBOL_GPL(gether_set_dev_addr);
  760. int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
  761. {
  762. struct eth_dev *dev;
  763. int ret;
  764. dev = netdev_priv(net);
  765. ret = get_ether_addr_str(dev->dev_mac, dev_addr, len);
  766. if (ret + 1 < len) {
  767. dev_addr[ret++] = '\n';
  768. dev_addr[ret] = '\0';
  769. }
  770. return ret;
  771. }
  772. EXPORT_SYMBOL_GPL(gether_get_dev_addr);
  773. int gether_set_host_addr(struct net_device *net, const char *host_addr)
  774. {
  775. struct eth_dev *dev;
  776. u8 new_addr[ETH_ALEN];
  777. dev = netdev_priv(net);
  778. if (get_ether_addr(host_addr, new_addr))
  779. return -EINVAL;
  780. memcpy(dev->host_mac, new_addr, ETH_ALEN);
  781. return 0;
  782. }
  783. EXPORT_SYMBOL_GPL(gether_set_host_addr);
  784. int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
  785. {
  786. struct eth_dev *dev;
  787. int ret;
  788. dev = netdev_priv(net);
  789. ret = get_ether_addr_str(dev->host_mac, host_addr, len);
  790. if (ret + 1 < len) {
  791. host_addr[ret++] = '\n';
  792. host_addr[ret] = '\0';
  793. }
  794. return ret;
  795. }
  796. EXPORT_SYMBOL_GPL(gether_get_host_addr);
  797. int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
  798. {
  799. struct eth_dev *dev;
  800. if (len < 13)
  801. return -EINVAL;
  802. dev = netdev_priv(net);
  803. snprintf(host_addr, len, "%pm", dev->host_mac);
  804. string_upper(host_addr, host_addr);
  805. return strlen(host_addr);
  806. }
  807. EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
  808. void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
  809. {
  810. struct eth_dev *dev;
  811. dev = netdev_priv(net);
  812. memcpy(host_mac, dev->host_mac, ETH_ALEN);
  813. }
  814. EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
  815. void gether_set_qmult(struct net_device *net, unsigned qmult)
  816. {
  817. struct eth_dev *dev;
  818. dev = netdev_priv(net);
  819. dev->qmult = qmult;
  820. }
  821. EXPORT_SYMBOL_GPL(gether_set_qmult);
  822. unsigned gether_get_qmult(struct net_device *net)
  823. {
  824. struct eth_dev *dev;
  825. dev = netdev_priv(net);
  826. return dev->qmult;
  827. }
  828. EXPORT_SYMBOL_GPL(gether_get_qmult);
  829. int gether_get_ifname(struct net_device *net, char *name, int len)
  830. {
  831. struct eth_dev *dev = netdev_priv(net);
  832. int ret;
  833. rtnl_lock();
  834. ret = scnprintf(name, len, "%s\n",
  835. dev->ifname_set ? net->name : netdev_name(net));
  836. rtnl_unlock();
  837. return ret;
  838. }
  839. EXPORT_SYMBOL_GPL(gether_get_ifname);
  840. int gether_set_ifname(struct net_device *net, const char *name, int len)
  841. {
  842. struct eth_dev *dev = netdev_priv(net);
  843. char tmp[IFNAMSIZ];
  844. const char *p;
  845. if (name[len - 1] == '\n')
  846. len--;
  847. if (len >= sizeof(tmp))
  848. return -E2BIG;
  849. strscpy(tmp, name, len + 1);
  850. if (!dev_valid_name(tmp))
  851. return -EINVAL;
  852. /* Require exactly one %d, so binding will not fail with EEXIST. */
  853. p = strchr(name, '%');
  854. if (!p || p[1] != 'd' || strchr(p + 2, '%'))
  855. return -EINVAL;
  856. strscpy(net->name, tmp);
  857. dev->ifname_set = true;
  858. return 0;
  859. }
  860. EXPORT_SYMBOL_GPL(gether_set_ifname);
  861. void gether_suspend(struct gether *link)
  862. {
  863. struct eth_dev *dev = link->ioport;
  864. unsigned long flags;
  865. if (!dev)
  866. return;
  867. if (atomic_read(&dev->tx_qlen)) {
  868. /*
  869. * There is a transfer in progress. So we trigger a remote
  870. * wakeup to inform the host.
  871. */
  872. if (!ether_wakeup_host(dev->port_usb))
  873. return;
  874. }
  875. spin_lock_irqsave(&dev->lock, flags);
  876. link->is_suspend = true;
  877. spin_unlock_irqrestore(&dev->lock, flags);
  878. }
  879. EXPORT_SYMBOL_GPL(gether_suspend);
  880. void gether_resume(struct gether *link)
  881. {
  882. struct eth_dev *dev = link->ioport;
  883. unsigned long flags;
  884. if (!dev)
  885. return;
  886. if (netif_queue_stopped(dev->net))
  887. netif_start_queue(dev->net);
  888. spin_lock_irqsave(&dev->lock, flags);
  889. link->is_suspend = false;
  890. spin_unlock_irqrestore(&dev->lock, flags);
  891. }
  892. EXPORT_SYMBOL_GPL(gether_resume);
  893. /*
  894. * gether_cleanup - remove Ethernet-over-USB device
  895. * Context: may sleep
  896. *
  897. * This is called to free all resources allocated by @gether_setup().
  898. */
  899. void gether_cleanup(struct eth_dev *dev)
  900. {
  901. if (!dev)
  902. return;
  903. unregister_netdev(dev->net);
  904. flush_work(&dev->work);
  905. free_netdev(dev->net);
  906. }
  907. EXPORT_SYMBOL_GPL(gether_cleanup);
  908. /**
  909. * gether_connect - notify network layer that USB link is active
  910. * @link: the USB link, set up with endpoints, descriptors matching
  911. * current device speed, and any framing wrapper(s) set up.
  912. * Context: irqs blocked
  913. *
  914. * This is called to activate endpoints and let the network layer know
  915. * the connection is active ("carrier detect"). It may cause the I/O
  916. * queues to open and start letting network packets flow, but will in
  917. * any case activate the endpoints so that they respond properly to the
  918. * USB host.
  919. *
  920. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  921. * indicate some error code (negative errno), ep->driver_data values
  922. * have been overwritten.
  923. */
  924. struct net_device *gether_connect(struct gether *link)
  925. {
  926. struct eth_dev *dev = link->ioport;
  927. int result = 0;
  928. if (!dev)
  929. return ERR_PTR(-EINVAL);
  930. link->in_ep->driver_data = dev;
  931. result = usb_ep_enable(link->in_ep);
  932. if (result != 0) {
  933. DBG(dev, "enable %s --> %d\n",
  934. link->in_ep->name, result);
  935. goto fail0;
  936. }
  937. link->out_ep->driver_data = dev;
  938. result = usb_ep_enable(link->out_ep);
  939. if (result != 0) {
  940. DBG(dev, "enable %s --> %d\n",
  941. link->out_ep->name, result);
  942. goto fail1;
  943. }
  944. if (result == 0)
  945. result = alloc_requests(dev, link, qlen(dev->gadget,
  946. dev->qmult));
  947. if (result == 0) {
  948. dev->zlp = link->is_zlp_ok;
  949. dev->no_skb_reserve = gadget_avoids_skb_reserve(dev->gadget);
  950. DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
  951. dev->header_len = link->header_len;
  952. dev->unwrap = link->unwrap;
  953. dev->wrap = link->wrap;
  954. spin_lock(&dev->lock);
  955. dev->port_usb = link;
  956. if (netif_running(dev->net)) {
  957. if (link->open)
  958. link->open(link);
  959. } else {
  960. if (link->close)
  961. link->close(link);
  962. }
  963. spin_unlock(&dev->lock);
  964. netif_carrier_on(dev->net);
  965. if (netif_running(dev->net))
  966. eth_start(dev, GFP_ATOMIC);
  967. /* on error, disable any endpoints */
  968. } else {
  969. (void) usb_ep_disable(link->out_ep);
  970. fail1:
  971. (void) usb_ep_disable(link->in_ep);
  972. }
  973. fail0:
  974. /* caller is responsible for cleanup on error */
  975. if (result < 0)
  976. return ERR_PTR(result);
  977. return dev->net;
  978. }
  979. EXPORT_SYMBOL_GPL(gether_connect);
  980. /**
  981. * gether_disconnect - notify network layer that USB link is inactive
  982. * @link: the USB link, on which gether_connect() was called
  983. * Context: irqs blocked
  984. *
  985. * This is called to deactivate endpoints and let the network layer know
  986. * the connection went inactive ("no carrier").
  987. *
  988. * On return, the state is as if gether_connect() had never been called.
  989. * The endpoints are inactive, and accordingly without active USB I/O.
  990. * Pointers to endpoint descriptors and endpoint private data are nulled.
  991. */
  992. void gether_disconnect(struct gether *link)
  993. {
  994. struct eth_dev *dev = link->ioport;
  995. struct usb_request *req;
  996. WARN_ON(!dev);
  997. if (!dev)
  998. return;
  999. DBG(dev, "%s\n", __func__);
  1000. netif_stop_queue(dev->net);
  1001. netif_carrier_off(dev->net);
  1002. /* disable endpoints, forcing (synchronous) completion
  1003. * of all pending i/o. then free the request objects
  1004. * and forget about the endpoints.
  1005. */
  1006. usb_ep_disable(link->in_ep);
  1007. spin_lock(&dev->req_lock);
  1008. while (!list_empty(&dev->tx_reqs)) {
  1009. req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
  1010. list_del(&req->list);
  1011. spin_unlock(&dev->req_lock);
  1012. usb_ep_free_request(link->in_ep, req);
  1013. spin_lock(&dev->req_lock);
  1014. }
  1015. spin_unlock(&dev->req_lock);
  1016. link->in_ep->desc = NULL;
  1017. usb_ep_disable(link->out_ep);
  1018. spin_lock(&dev->req_lock);
  1019. while (!list_empty(&dev->rx_reqs)) {
  1020. req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
  1021. list_del(&req->list);
  1022. spin_unlock(&dev->req_lock);
  1023. usb_ep_free_request(link->out_ep, req);
  1024. spin_lock(&dev->req_lock);
  1025. }
  1026. spin_unlock(&dev->req_lock);
  1027. link->out_ep->desc = NULL;
  1028. /* finish forgetting about this USB link episode */
  1029. dev->header_len = 0;
  1030. dev->unwrap = NULL;
  1031. dev->wrap = NULL;
  1032. spin_lock(&dev->lock);
  1033. dev->port_usb = NULL;
  1034. link->is_suspend = false;
  1035. spin_unlock(&dev->lock);
  1036. }
  1037. EXPORT_SYMBOL_GPL(gether_disconnect);
  1038. MODULE_DESCRIPTION("Ethernet-over-USB link layer utilities for Gadget stack");
  1039. MODULE_LICENSE("GPL");
  1040. MODULE_AUTHOR("David Brownell");