u_ether.c 29 KB

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