u_ether.c 28 KB

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