u_ether.c 29 KB

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