u_ether.c 30 KB

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