u_ether.c 28 KB

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