u_ether.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. #ifdef pr_fmt
  15. #undef pr_fmt
  16. #endif
  17. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/gfp.h>
  21. #include <linux/device.h>
  22. #include <linux/ctype.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_vlan.h>
  26. #include "u_ether.h"
  27. /*
  28. * This component encapsulates the Ethernet link glue needed to provide
  29. * one (!) network link through the USB gadget stack, normally "usb0".
  30. *
  31. * The control and data models are handled by the function driver which
  32. * connects to this code; such as CDC Ethernet (ECM or EEM),
  33. * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
  34. * management.
  35. *
  36. * Link level addressing is handled by this component using module
  37. * parameters; if no such parameters are provided, random link level
  38. * addresses are used. Each end of the link uses one address. The
  39. * host end address is exported in various ways, and is often recorded
  40. * in configuration databases.
  41. *
  42. * The driver which assembles each configuration using such a link is
  43. * responsible for ensuring that each configuration includes at most one
  44. * instance of is network link. (The network layer provides ways for
  45. * this single "physical" link to be used by multiple virtual links.)
  46. */
  47. #define UETH__VERSION "29-May-2008"
  48. static struct workqueue_struct *uether_wq;
  49. static struct workqueue_struct *uether_wq1;
  50. struct eth_dev {
  51. /* lock is held while accessing port_usb
  52. */
  53. spinlock_t lock;
  54. struct gether *port_usb;
  55. struct net_device *net;
  56. struct usb_gadget *gadget;
  57. spinlock_t req_lock; /* guard {tx}_reqs */
  58. spinlock_t reqrx_lock; /* guard {rx}_reqs */
  59. struct list_head tx_reqs, rx_reqs;
  60. unsigned tx_qlen;
  61. /* Minimum number of TX USB request queued to UDC */
  62. #define TX_REQ_THRESHOLD 5
  63. int no_tx_req_used;
  64. int tx_skb_hold_count;
  65. u32 tx_req_bufsize;
  66. struct sk_buff_head rx_frames;
  67. unsigned header_len;
  68. unsigned int ul_max_pkts_per_xfer;
  69. unsigned int dl_max_pkts_per_xfer;
  70. uint32_t dl_max_xfer_size;
  71. struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
  72. int (*unwrap)(struct gether *,
  73. struct sk_buff *skb,
  74. struct sk_buff_head *list);
  75. struct work_struct work;
  76. struct work_struct rx_work;
  77. struct work_struct rx_work1;
  78. unsigned long todo;
  79. #define WORK_RX_MEMORY 0
  80. bool zlp;
  81. u8 host_mac[ETH_ALEN];
  82. };
  83. /*-------------------------------------------------------------------------*/
  84. #define RX_EXTRA 20 /* bytes guarding against rx overflows */
  85. #define DEFAULT_QLEN 2 /* double buffering by default */
  86. static unsigned qmult = 10;
  87. module_param(qmult, uint, S_IRUGO|S_IWUSR);
  88. MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");
  89. static unsigned tx_wakeup_threshold = 13;
  90. module_param(tx_wakeup_threshold, uint, S_IRUGO|S_IWUSR);
  91. MODULE_PARM_DESC(tx_wakeup_threshold, "tx wakeup threshold value");
  92. /* for dual-speed hardware, use deeper queues at high/super speed */
  93. static inline int qlen(struct usb_gadget *gadget)
  94. {
  95. if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
  96. gadget->speed == USB_SPEED_SUPER))
  97. return qmult * DEFAULT_QLEN;
  98. else
  99. return DEFAULT_QLEN;
  100. }
  101. static inline int qlenrx(struct usb_gadget *gadget)
  102. {
  103. if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
  104. gadget->speed == USB_SPEED_SUPER))
  105. return qmult * DEFAULT_QLEN;
  106. else
  107. return DEFAULT_QLEN;
  108. }
  109. /*-------------------------------------------------------------------------*/
  110. /* REVISIT there must be a better way than having two sets
  111. * of debug calls ...
  112. */
  113. #undef DBG
  114. #undef VDBG
  115. #undef ERROR
  116. #undef INFO
  117. #define xprintk(d, level, fmt, args...) \
  118. printk(level "%s: " fmt , (d)->net->name , ## args)
  119. #ifdef DEBUG
  120. #undef DEBUG
  121. #define DBG(dev, fmt, args...) \
  122. xprintk(dev , KERN_DEBUG , fmt , ## args)
  123. #else
  124. #define DBG(dev, fmt, args...) \
  125. do { } while (0)
  126. #endif /* DEBUG */
  127. #ifdef VERBOSE_DEBUG
  128. #define VDBG DBG
  129. #else
  130. #define VDBG(dev, fmt, args...) \
  131. do { } while (0)
  132. #endif /* DEBUG */
  133. #define ERROR(dev, fmt, args...) \
  134. xprintk(dev , KERN_ERR , fmt , ## args)
  135. #define INFO(dev, fmt, args...) \
  136. xprintk(dev , KERN_INFO , fmt , ## args)
  137. /*-------------------------------------------------------------------------*/
  138. unsigned long rndis_test_rx_usb_in = 0 ;
  139. unsigned long rndis_test_rx_net_out = 0 ;
  140. unsigned long rndis_test_rx_nomem = 0 ;
  141. unsigned long rndis_test_rx_error = 0 ;
  142. unsigned long rndis_test_tx_net_in = 0 ;
  143. unsigned long rndis_test_tx_busy = 0 ;
  144. unsigned long rndis_test_tx_usb_out = 0 ;
  145. unsigned long rndis_test_tx_complete = 0 ;
  146. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  147. static int ueth_change_mtu(struct net_device *net, int new_mtu)
  148. {
  149. struct eth_dev *dev = netdev_priv(net);
  150. unsigned long flags;
  151. int status = 0;
  152. /* don't change MTU on "live" link (peer won't know) */
  153. spin_lock_irqsave(&dev->lock, flags);
  154. if (dev->port_usb)
  155. status = -EBUSY;
  156. else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
  157. status = -ERANGE;
  158. else
  159. net->mtu = new_mtu;
  160. spin_unlock_irqrestore(&dev->lock, flags);
  161. pr_debug("[XLOG_INFO][UTHER]ueth_change_mtu to %d, status is %d\n", new_mtu , status);
  162. return status;
  163. }
  164. static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
  165. {
  166. struct eth_dev *dev = netdev_priv(net);
  167. strlcpy(p->driver, "g_ether", sizeof(p->driver));
  168. strlcpy(p->version, UETH__VERSION, sizeof(p->version));
  169. strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
  170. strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
  171. }
  172. /* REVISIT can also support:
  173. * - WOL (by tracking suspends and issuing remote wakeup)
  174. * - msglevel (implies updated messaging)
  175. * - ... probably more ethtool ops
  176. */
  177. static const struct ethtool_ops ops = {
  178. .get_drvinfo = eth_get_drvinfo,
  179. .get_link = ethtool_op_get_link,
  180. };
  181. static void defer_kevent(struct eth_dev *dev, int flag)
  182. {
  183. if (test_and_set_bit(flag, &dev->todo))
  184. return;
  185. if (!schedule_work(&dev->work))
  186. ERROR(dev, "kevent %d may have been dropped\n", flag);
  187. else
  188. DBG(dev, "kevent %d scheduled\n", flag);
  189. }
  190. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  191. static void tx_complete(struct usb_ep *ep, struct usb_request *req);
  192. static int
  193. rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
  194. {
  195. struct sk_buff *skb;
  196. int retval = -ENOMEM;
  197. size_t size = 0;
  198. struct usb_ep *out;
  199. unsigned long flags;
  200. spin_lock_irqsave(&dev->lock, flags);
  201. if (dev->port_usb)
  202. out = dev->port_usb->out_ep;
  203. else
  204. out = NULL;
  205. spin_unlock_irqrestore(&dev->lock, flags);
  206. if (!out)
  207. return -ENOTCONN;
  208. /* Padding up to RX_EXTRA handles minor disagreements with host.
  209. * Normally we use the USB "terminate on short read" convention;
  210. * so allow up to (N*maxpacket), since that memory is normally
  211. * already allocated. Some hardware doesn't deal well with short
  212. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  213. * byte off the end (to force hardware errors on overflow).
  214. *
  215. * RNDIS uses internal framing, and explicitly allows senders to
  216. * pad to end-of-packet. That's potentially nice for speed, but
  217. * means receivers can't recover lost synch on their own (because
  218. * new packets don't only start after a short RX).
  219. */
  220. size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
  221. size += dev->port_usb->header_len;
  222. /*
  223. size += out->maxpacket - 1;
  224. size -= size % out->maxpacket;
  225. */
  226. if (dev->ul_max_pkts_per_xfer)
  227. size *= dev->ul_max_pkts_per_xfer;
  228. if (dev->port_usb->is_fixed)
  229. size = max_t(size_t, size, dev->port_usb->fixed_out_len);
  230. pr_debug("%s: size: %d, mtu: %d, header_len: %d, maxpacket: %d, ul_max_pkts_per_xfer: %d",
  231. __func__, (int)size, dev->net->mtu, dev->port_usb->header_len, out->maxpacket, dev->ul_max_pkts_per_xfer);
  232. skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
  233. if (skb == NULL) {
  234. pr_debug("[XLOG_INFO][UTHER]rx_submit : no rx skb\n");
  235. DBG(dev, "no rx skb\n");
  236. rndis_test_rx_nomem ++ ;
  237. goto enomem;
  238. }
  239. /* Some platforms perform better when IP packets are aligned,
  240. * but on at least one, checksumming fails otherwise. Note:
  241. * RNDIS headers involve variable numbers of LE32 values.
  242. */
  243. skb_reserve(skb, NET_IP_ALIGN);
  244. req->buf = skb->data;
  245. req->length = size;
  246. req->context = skb;
  247. retval = usb_ep_queue(out, req, gfp_flags);
  248. if (retval == -ENOMEM)
  249. enomem:
  250. defer_kevent(dev, WORK_RX_MEMORY);
  251. if (retval) {
  252. DBG(dev, "rx submit --> %d\n", retval);
  253. if (skb)
  254. dev_kfree_skb_any(skb);
  255. }
  256. return retval;
  257. }
  258. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  259. {
  260. struct sk_buff *skb = req->context;
  261. struct eth_dev *dev = ep->driver_data;
  262. int status = req->status;
  263. bool queue = 0;
  264. switch (status) {
  265. /* normal completion */
  266. case 0:
  267. pr_debug("%s: transferred size: %d", __func__, req->actual);
  268. skb_put(skb, req->actual);
  269. if (dev->unwrap) {
  270. unsigned long flags;
  271. spin_lock_irqsave(&dev->lock, flags);
  272. if (dev->port_usb) {
  273. status = dev->unwrap(dev->port_usb,
  274. skb,
  275. &dev->rx_frames);
  276. if (status == -EINVAL)
  277. dev->net->stats.rx_errors++;
  278. else if (status == -EOVERFLOW)
  279. dev->net->stats.rx_over_errors++;
  280. } else {
  281. dev_kfree_skb_any(skb);
  282. status = -ENOTCONN;
  283. }
  284. spin_unlock_irqrestore(&dev->lock, flags);
  285. } else {
  286. skb_queue_tail(&dev->rx_frames, skb);
  287. }
  288. if (!status)
  289. queue = 1;
  290. rndis_test_rx_usb_in ++ ;
  291. break;
  292. /* software-driven interface shutdown */
  293. case -ECONNRESET: /* unlink */
  294. case -ESHUTDOWN: /* disconnect etc */
  295. VDBG(dev, "rx shutdown, code %d\n", status);
  296. goto quiesce;
  297. /* for hardware automagic (such as pxa) */
  298. case -ECONNABORTED: /* endpoint reset */
  299. DBG(dev, "rx %s reset\n", ep->name);
  300. defer_kevent(dev, WORK_RX_MEMORY);
  301. quiesce:
  302. dev_kfree_skb_any(skb);
  303. goto clean;
  304. /* data overrun */
  305. case -EOVERFLOW:
  306. dev->net->stats.rx_over_errors++;
  307. /* FALLTHROUGH */
  308. default:
  309. queue = 1;
  310. dev_kfree_skb_any(skb);
  311. dev->net->stats.rx_errors++;
  312. DBG(dev, "rx status %d\n", status);
  313. break;
  314. }
  315. clean:
  316. spin_lock(&dev->reqrx_lock);
  317. list_add(&req->list, &dev->rx_reqs);
  318. spin_unlock(&dev->reqrx_lock);
  319. if (queue)
  320. {
  321. queue_work(uether_wq, &dev->rx_work);
  322. queue_work(uether_wq1, &dev->rx_work1);
  323. }
  324. }
  325. static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
  326. {
  327. unsigned i;
  328. struct usb_request *req;
  329. bool usb_in;
  330. if (!n)
  331. return -ENOMEM;
  332. /* queue/recycle up to N requests */
  333. i = n;
  334. list_for_each_entry(req, list, list) {
  335. if (i-- == 0)
  336. goto extra;
  337. }
  338. if (ep->desc->bEndpointAddress & USB_DIR_IN)
  339. usb_in = true;
  340. else
  341. usb_in = false;
  342. while (i--) {
  343. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  344. if (!req)
  345. return list_empty(list) ? -ENOMEM : 0;
  346. /* update completion handler */
  347. if (usb_in)
  348. req->complete = tx_complete;
  349. else
  350. req->complete = rx_complete;
  351. list_add(&req->list, list);
  352. }
  353. return 0;
  354. extra:
  355. /* free extras */
  356. for (;;) {
  357. struct list_head *next;
  358. next = req->list.next;
  359. list_del(&req->list);
  360. usb_ep_free_request(ep, req);
  361. if (next == list)
  362. break;
  363. req = container_of(next, struct usb_request, list);
  364. }
  365. return 0;
  366. }
  367. static int alloc_tx_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  368. {
  369. int status;
  370. spin_lock(&dev->req_lock);
  371. status = prealloc(&dev->tx_reqs, link->in_ep, n);
  372. if (status < 0)
  373. goto fail;
  374. goto done;
  375. fail:
  376. DBG(dev, "can't alloc tx requests\n");
  377. pr_debug("[XLOG_INFO][UTHER]alloc_requests : can't alloc requests\n");
  378. done:
  379. spin_unlock(&dev->req_lock);
  380. return status;
  381. }
  382. static int alloc_rx_requests(struct eth_dev *dev, struct gether *link, unsigned n)
  383. {
  384. int status;
  385. spin_lock(&dev->reqrx_lock);
  386. status = prealloc(&dev->rx_reqs, link->out_ep, n);
  387. if (status < 0)
  388. goto fail;
  389. goto done;
  390. fail:
  391. DBG(dev, "can't alloc rx requests\n");
  392. pr_debug("[XLOG_INFO][UTHER]alloc_requests : can't alloc rxrequests\n");
  393. done:
  394. spin_unlock(&dev->reqrx_lock);
  395. return status;
  396. }
  397. static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
  398. {
  399. struct usb_request *req;
  400. unsigned long flags;
  401. int req_cnt = 0;
  402. /* fill unused rxq slots with some skb */
  403. spin_lock_irqsave(&dev->reqrx_lock, flags);
  404. while (!list_empty(&dev->rx_reqs)) {
  405. /* break the nexus of continuous completion and re-submission*/
  406. if (++req_cnt > qlenrx(dev->gadget))
  407. break;
  408. req = container_of(dev->rx_reqs.next,
  409. struct usb_request, list);
  410. list_del_init(&req->list);
  411. spin_unlock_irqrestore(&dev->reqrx_lock, flags);
  412. if (rx_submit(dev, req, gfp_flags) < 0) {
  413. spin_lock_irqsave(&dev->reqrx_lock, flags);
  414. list_add(&req->list, &dev->rx_reqs);
  415. spin_unlock_irqrestore(&dev->reqrx_lock, flags);
  416. defer_kevent(dev, WORK_RX_MEMORY);
  417. return;
  418. }
  419. spin_lock_irqsave(&dev->reqrx_lock, flags);
  420. }
  421. spin_unlock_irqrestore(&dev->reqrx_lock, flags);
  422. }
  423. static void process_rx_w(struct work_struct *work)
  424. {
  425. struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
  426. struct sk_buff *skb;
  427. int status = 0;
  428. if (!dev->port_usb)
  429. return;
  430. while ((skb = skb_dequeue(&dev->rx_frames))) {
  431. if (status < 0
  432. || ETH_HLEN > skb->len
  433. || skb->len > VLAN_ETH_FRAME_LEN) {
  434. dev->net->stats.rx_errors++;
  435. dev->net->stats.rx_length_errors++;
  436. rndis_test_rx_error++ ;
  437. DBG(dev, "rx length %d\n", skb->len);
  438. dev_kfree_skb_any(skb);
  439. continue;
  440. }
  441. skb->protocol = eth_type_trans(skb, dev->net);
  442. dev->net->stats.rx_packets++;
  443. dev->net->stats.rx_bytes += skb->len;
  444. rndis_test_rx_net_out ++ ;
  445. status = netif_rx_ni(skb);
  446. }
  447. /* move to another workthread */
  448. #if 0
  449. if (netif_running(dev->net))
  450. rx_fill(dev, GFP_KERNEL);
  451. #endif
  452. }
  453. static void process_rx_w1(struct work_struct *work)
  454. {
  455. struct eth_dev *dev = container_of(work, struct eth_dev, rx_work1);
  456. if (!dev->port_usb)
  457. return;
  458. if (netif_running(dev->net))
  459. rx_fill(dev, GFP_KERNEL);
  460. }
  461. static void eth_work(struct work_struct *work)
  462. {
  463. struct eth_dev *dev = container_of(work, struct eth_dev, work);
  464. if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
  465. if (netif_running(dev->net))
  466. rx_fill(dev, GFP_KERNEL);
  467. }
  468. if (dev->todo)
  469. DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
  470. }
  471. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  472. {
  473. struct sk_buff *skb;
  474. struct eth_dev *dev;
  475. struct net_device *net;
  476. struct usb_request *new_req;
  477. struct usb_ep *in;
  478. int length;
  479. int retval;
  480. if (!ep->driver_data) {
  481. usb_ep_free_request(ep, req);
  482. return;
  483. }
  484. dev = ep->driver_data;
  485. net = dev->net;
  486. if (!dev->port_usb) {
  487. usb_ep_free_request(ep, req);
  488. return;
  489. }
  490. switch (req->status) {
  491. default:
  492. dev->net->stats.tx_errors++;
  493. VDBG(dev, "tx err %d\n", req->status);
  494. /* FALLTHROUGH */
  495. case -ECONNRESET: /* unlink */
  496. case -ESHUTDOWN: /* disconnect etc */
  497. break;
  498. case 0:
  499. if (!req->zero)
  500. dev->net->stats.tx_bytes += req->length-1;
  501. else
  502. dev->net->stats.tx_bytes += req->length;
  503. }
  504. dev->net->stats.tx_packets++;
  505. rndis_test_tx_complete++ ;
  506. spin_lock(&dev->req_lock);
  507. list_add_tail(&req->list, &dev->tx_reqs);
  508. if (dev->port_usb->multi_pkt_xfer && !req->context) {
  509. dev->no_tx_req_used--;
  510. req->length = 0;
  511. in = dev->port_usb->in_ep;
  512. if (!list_empty(&dev->tx_reqs)) {
  513. new_req = container_of(dev->tx_reqs.next,
  514. struct usb_request, list);
  515. list_del(&new_req->list);
  516. spin_unlock(&dev->req_lock);
  517. if (new_req->length > 0) {
  518. length = new_req->length;
  519. /* NCM requires no zlp if transfer is
  520. * dwNtbInMaxSize */
  521. if (dev->port_usb->is_fixed &&
  522. length == dev->port_usb->fixed_in_len &&
  523. (length % in->maxpacket) == 0)
  524. new_req->zero = 0;
  525. else
  526. new_req->zero = 1;
  527. /* use zlp framing on tx for strict CDC-Ether
  528. * conformance, though any robust network rx
  529. * path ignores extra padding. and some hardware
  530. * doesn't like to write zlps.
  531. */
  532. if (new_req->zero && !dev->zlp &&
  533. (length % in->maxpacket) == 0) {
  534. new_req->zero = 0;
  535. length++;
  536. }
  537. new_req->length = length;
  538. retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
  539. switch (retval) {
  540. default:
  541. DBG(dev, "tx queue err %d\n", retval);
  542. new_req->length = 0;
  543. spin_lock(&dev->req_lock);
  544. list_add_tail(&new_req->list,
  545. &dev->tx_reqs);
  546. spin_unlock(&dev->req_lock);
  547. break;
  548. case 0:
  549. spin_lock(&dev->req_lock);
  550. dev->no_tx_req_used++;
  551. spin_unlock(&dev->req_lock);
  552. net->trans_start = jiffies;
  553. }
  554. } else {
  555. spin_lock(&dev->req_lock);
  556. /*
  557. * Put the idle request at the back of the
  558. * queue. The xmit function will put the
  559. * unfinished request at the beginning of the
  560. * queue.
  561. */
  562. list_add_tail(&new_req->list, &dev->tx_reqs);
  563. spin_unlock(&dev->req_lock);
  564. }
  565. } else {
  566. spin_unlock(&dev->req_lock);
  567. }
  568. } else {
  569. skb = req->context;
  570. /* Is aggregation already enabled and buffers allocated ? */
  571. if (dev->port_usb->multi_pkt_xfer && dev->tx_req_bufsize) {
  572. req->buf = kzalloc(dev->tx_req_bufsize, GFP_ATOMIC);
  573. req->context = NULL;
  574. } else {
  575. req->buf = NULL;
  576. }
  577. spin_unlock(&dev->req_lock);
  578. dev_kfree_skb_any(skb);
  579. }
  580. if (netif_carrier_ok(dev->net))
  581. {
  582. spin_lock(&dev->req_lock);
  583. if(dev->no_tx_req_used < tx_wakeup_threshold)
  584. netif_wake_queue(dev->net);
  585. spin_unlock(&dev->req_lock);
  586. }
  587. }
  588. static inline int is_promisc(u16 cdc_filter)
  589. {
  590. return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  591. }
  592. static int alloc_tx_buffer(struct eth_dev *dev)
  593. {
  594. struct list_head *act;
  595. struct usb_request *req;
  596. dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
  597. (dev->net->mtu
  598. + sizeof(struct ethhdr)
  599. /* size of rndis_packet_msg_type */
  600. + 44
  601. + 22));
  602. list_for_each(act, &dev->tx_reqs) {
  603. req = container_of(act, struct usb_request, list);
  604. if (!req->buf) {
  605. req->buf = kzalloc(dev->tx_req_bufsize,
  606. GFP_ATOMIC);
  607. if (!req->buf)
  608. goto free_buf;
  609. }
  610. /* req->context is not used for multi_pkt_xfers */
  611. req->context = NULL;
  612. }
  613. return 0;
  614. free_buf:
  615. /* tx_req_bufsize = 0 retries mem alloc on next eth_start_xmit */
  616. dev->tx_req_bufsize = 0;
  617. list_for_each(act, &dev->tx_reqs) {
  618. req = container_of(act, struct usb_request, list);
  619. kfree(req->buf);
  620. req->buf = NULL;
  621. }
  622. return -ENOMEM;
  623. }
  624. static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
  625. struct net_device *net)
  626. {
  627. struct eth_dev *dev = netdev_priv(net);
  628. int length = skb->len;
  629. int retval;
  630. struct usb_request *req = NULL;
  631. unsigned long flags;
  632. struct usb_ep *in;
  633. u16 cdc_filter;
  634. bool multi_pkt_xfer = false;
  635. uint32_t max_size = 0;
  636. //ALPS00542120
  637. static unsigned int okCnt = 0, busyCnt = 0;
  638. static int firstShot = 1, diffSec;
  639. static struct timeval tv_last, tv_cur;
  640. spin_lock_irqsave(&dev->lock, flags);
  641. if (dev->port_usb) {
  642. in = dev->port_usb->in_ep;
  643. cdc_filter = dev->port_usb->cdc_filter;
  644. multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
  645. max_size = dev->dl_max_xfer_size;
  646. } else {
  647. in = NULL;
  648. cdc_filter = 0;
  649. }
  650. spin_unlock_irqrestore(&dev->lock, flags);
  651. if (!in) {
  652. dev_kfree_skb_any(skb);
  653. return NETDEV_TX_OK;
  654. }
  655. /* Allocate memory for tx_reqs to support multi packet transfer */
  656. spin_lock_irqsave(&dev->req_lock, flags);
  657. if (multi_pkt_xfer && !dev->tx_req_bufsize) {
  658. retval = alloc_tx_buffer(dev);
  659. if (retval < 0) {
  660. spin_unlock_irqrestore(&dev->req_lock, flags);
  661. return -ENOMEM;
  662. }
  663. }
  664. spin_unlock_irqrestore(&dev->req_lock, flags);
  665. rndis_test_tx_net_in ++ ;
  666. /* apply outgoing CDC or RNDIS filters */
  667. if (!is_promisc(cdc_filter)) {
  668. u8 *dest = skb->data;
  669. if (is_multicast_ether_addr(dest)) {
  670. u16 type;
  671. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  672. * SET_ETHERNET_MULTICAST_FILTERS requests
  673. */
  674. if (is_broadcast_ether_addr(dest))
  675. type = USB_CDC_PACKET_TYPE_BROADCAST;
  676. else
  677. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  678. if (!(cdc_filter & type)) {
  679. dev_kfree_skb_any(skb);
  680. pr_warning("cdc_filter error, cdc_filter is 0x%x , type is 0x%x \n", cdc_filter , type);
  681. return NETDEV_TX_OK;
  682. }
  683. }
  684. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  685. }
  686. spin_lock_irqsave(&dev->req_lock, flags);
  687. /*
  688. * this freelist can be empty if an interrupt triggered disconnect()
  689. * and reconfigured the gadget (shutting down this queue) after the
  690. * network stack decided to xmit but before we got the spinlock.
  691. */
  692. if (list_empty(&dev->tx_reqs)) {
  693. busyCnt++;
  694. do_gettimeofday(&tv_cur);
  695. if(firstShot)
  696. {
  697. tv_last = tv_cur;
  698. firstShot = 0;
  699. printk(KERN_ERR "%s, NETDEV_TX_BUSY returned at firstShot , okCnt : %u, busyCnt : %u\n", __func__, okCnt, busyCnt);
  700. }
  701. else
  702. {
  703. diffSec = tv_cur.tv_sec - tv_last.tv_sec;
  704. if(diffSec >=2 )
  705. {
  706. tv_last = tv_cur;
  707. printk(KERN_ERR "%s, NETDEV_TX_BUSY returned, okCnt : %u, busyCnt : %u\n", __func__, okCnt, busyCnt);
  708. }
  709. }
  710. spin_unlock_irqrestore(&dev->req_lock, flags);
  711. rndis_test_tx_busy ++ ;
  712. return NETDEV_TX_BUSY;
  713. }
  714. okCnt++;
  715. req = container_of(dev->tx_reqs.next, struct usb_request, list);
  716. list_del(&req->list);
  717. /* temporarily stop TX queue when the freelist empties */
  718. if (list_empty(&dev->tx_reqs))
  719. netif_stop_queue(net);
  720. spin_unlock_irqrestore(&dev->req_lock, flags);
  721. /* no buffer copies needed, unless the network stack did it
  722. * or the hardware can't use skb buffers.
  723. * or there's not enough space for extra headers we need
  724. */
  725. spin_lock_irqsave(&dev->lock, flags);
  726. if (dev->wrap) {
  727. if (dev->port_usb)
  728. skb = dev->wrap(dev->port_usb, skb);
  729. if (!skb) {
  730. spin_unlock_irqrestore(&dev->lock, flags);
  731. goto drop;
  732. }
  733. }
  734. if (multi_pkt_xfer) {
  735. pr_debug("req->length:%d header_len:%u\n"
  736. "skb->len:%d skb->data_len:%d\n",
  737. req->length, dev->header_len,
  738. skb->len, skb->data_len);
  739. if (dev->port_usb == NULL)
  740. {
  741. dev_kfree_skb_any(skb);
  742. pr_debug("eth_start_xmit, port_usb becomes NULL\n");
  743. return NETDEV_TX_OK;
  744. }
  745. /* Add RNDIS Header */
  746. memcpy(req->buf + req->length, dev->port_usb->header,
  747. dev->header_len);
  748. /* Increment req length by header size */
  749. req->length += dev->header_len;
  750. spin_unlock_irqrestore(&dev->lock, flags);
  751. /* Copy received IP data from SKB */
  752. memcpy(req->buf + req->length, skb->data, skb->len);
  753. /* Increment req length by skb data length */
  754. req->length += skb->len;
  755. length = req->length;
  756. dev_kfree_skb_any(skb);
  757. spin_lock_irqsave(&dev->req_lock, flags);
  758. dev->tx_skb_hold_count++;
  759. /* if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) { */
  760. if ((dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer)
  761. && (length < (max_size - dev->net->mtu))) {
  762. if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
  763. list_add(&req->list, &dev->tx_reqs);
  764. spin_unlock_irqrestore(&dev->req_lock, flags);
  765. goto success;
  766. }
  767. }
  768. dev->no_tx_req_used++;
  769. dev->tx_skb_hold_count = 0;
  770. spin_unlock_irqrestore(&dev->req_lock, flags);
  771. } else {
  772. spin_unlock_irqrestore(&dev->lock, flags);
  773. length = skb->len;
  774. req->buf = skb->data;
  775. req->context = skb;
  776. }
  777. if (dev->port_usb == NULL)
  778. {
  779. if (!multi_pkt_xfer)
  780. dev_kfree_skb_any(skb);
  781. pr_debug("eth_start_xmit, port_usb becomes NULL\n");
  782. return NETDEV_TX_OK;
  783. }
  784. /* NCM requires no zlp if transfer is dwNtbInMaxSize */
  785. if (dev->port_usb->is_fixed &&
  786. length == dev->port_usb->fixed_in_len &&
  787. (length % in->maxpacket) == 0)
  788. req->zero = 0;
  789. else
  790. req->zero = 1;
  791. /* use zlp framing on tx for strict CDC-Ether conformance,
  792. * though any robust network rx path ignores extra padding.
  793. * and some hardware doesn't like to write zlps.
  794. */
  795. if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
  796. req->zero = 0;
  797. length++;
  798. }
  799. req->length = length;
  800. /* throttle high/super speed IRQ rate back slightly */
  801. if (gadget_is_dualspeed(dev->gadget) &&
  802. (dev->gadget->speed == USB_SPEED_HIGH ||
  803. dev->gadget->speed == USB_SPEED_SUPER)) {
  804. dev->tx_qlen++;
  805. if (dev->tx_qlen == (qmult/2)) {
  806. req->no_interrupt = 0;
  807. dev->tx_qlen = 0;
  808. } else {
  809. req->no_interrupt = 1;
  810. }
  811. } else {
  812. req->no_interrupt = 0;
  813. }
  814. rndis_test_tx_usb_out ++ ;
  815. retval = usb_ep_queue(in, req, GFP_ATOMIC);
  816. switch (retval) {
  817. default:
  818. DBG(dev, "tx queue err %d\n", retval);
  819. pr_debug("[XLOG_INFO][UTHER]eth_start_xmit : tx queue err %d\n", retval);
  820. break;
  821. case 0:
  822. net->trans_start = jiffies;
  823. }
  824. if (retval) {
  825. if (!multi_pkt_xfer)
  826. dev_kfree_skb_any(skb);
  827. else
  828. req->length = 0;
  829. drop:
  830. dev->net->stats.tx_dropped++;
  831. spin_lock_irqsave(&dev->req_lock, flags);
  832. if (list_empty(&dev->tx_reqs))
  833. netif_start_queue(net);
  834. list_add_tail(&req->list, &dev->tx_reqs);
  835. spin_unlock_irqrestore(&dev->req_lock, flags);
  836. }
  837. success:
  838. return NETDEV_TX_OK;
  839. }
  840. /*-------------------------------------------------------------------------*/
  841. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
  842. {
  843. DBG(dev, "%s\n", __func__);
  844. pr_debug("[XLOG_INFO][UTHER]%s\n", __func__);
  845. /* fill the rx queue */
  846. rx_fill(dev, gfp_flags);
  847. /* and open the tx floodgates */
  848. dev->tx_qlen = 0;
  849. netif_wake_queue(dev->net);
  850. }
  851. static int eth_open(struct net_device *net)
  852. {
  853. struct eth_dev *dev = netdev_priv(net);
  854. struct gether *link;
  855. DBG(dev, "%s\n", __func__);
  856. pr_debug("[XLOG_INFO][UTHER]%s\n", __func__);
  857. if (netif_carrier_ok(dev->net))
  858. eth_start(dev, GFP_KERNEL);
  859. spin_lock_irq(&dev->lock);
  860. link = dev->port_usb;
  861. if (link && link->open)
  862. link->open(link);
  863. spin_unlock_irq(&dev->lock);
  864. return 0;
  865. }
  866. static int eth_stop(struct net_device *net)
  867. {
  868. struct eth_dev *dev = netdev_priv(net);
  869. unsigned long flags;
  870. VDBG(dev, "%s\n", __func__);
  871. pr_debug("[XLOG_INFO][UTHER]%s\n", __func__);
  872. netif_stop_queue(net);
  873. DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
  874. dev->net->stats.rx_packets, dev->net->stats.tx_packets,
  875. dev->net->stats.rx_errors, dev->net->stats.tx_errors
  876. );
  877. /* ensure there are no more active requests */
  878. spin_lock_irqsave(&dev->lock, flags);
  879. if (dev->port_usb) {
  880. struct gether *link = dev->port_usb;
  881. const struct usb_endpoint_descriptor *in;
  882. const struct usb_endpoint_descriptor *out;
  883. if (link->close)
  884. link->close(link);
  885. /* NOTE: we have no abort-queue primitive we could use
  886. * to cancel all pending I/O. Instead, we disable then
  887. * reenable the endpoints ... this idiom may leave toggle
  888. * wrong, but that's a self-correcting error.
  889. *
  890. * REVISIT: we *COULD* just let the transfers complete at
  891. * their own pace; the network stack can handle old packets.
  892. * For the moment we leave this here, since it works.
  893. */
  894. in = link->in_ep->desc;
  895. out = link->out_ep->desc;
  896. usb_ep_disable(link->in_ep);
  897. usb_ep_disable(link->out_ep);
  898. if (netif_carrier_ok(net)) {
  899. DBG(dev, "host still using in/out endpoints\n");
  900. link->in_ep->desc = in;
  901. link->out_ep->desc = out;
  902. usb_ep_enable(link->in_ep);
  903. usb_ep_enable(link->out_ep);
  904. }
  905. }
  906. spin_unlock_irqrestore(&dev->lock, flags);
  907. return 0;
  908. }
  909. /*-------------------------------------------------------------------------*/
  910. /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
  911. static char *dev_addr;
  912. module_param(dev_addr, charp, S_IRUGO);
  913. MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
  914. /* this address is invisible to ifconfig */
  915. static char *host_addr;
  916. module_param(host_addr, charp, S_IRUGO);
  917. MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
  918. static int get_ether_addr(const char *str, u8 *dev_addr)
  919. {
  920. if (str) {
  921. unsigned i;
  922. for (i = 0; i < 6; i++) {
  923. unsigned char num;
  924. if ((*str == '.') || (*str == ':'))
  925. str++;
  926. num = hex_to_bin(*str++) << 4;
  927. num |= hex_to_bin(*str++);
  928. dev_addr [i] = num;
  929. }
  930. if (is_valid_ether_addr(dev_addr))
  931. return 0;
  932. }
  933. eth_random_addr(dev_addr);
  934. return 1;
  935. }
  936. static const struct net_device_ops eth_netdev_ops = {
  937. .ndo_open = eth_open,
  938. .ndo_stop = eth_stop,
  939. .ndo_start_xmit = eth_start_xmit,
  940. .ndo_change_mtu = ueth_change_mtu,
  941. .ndo_set_mac_address = eth_mac_addr,
  942. .ndo_validate_addr = eth_validate_addr,
  943. };
  944. static struct device_type gadget_type = {
  945. .name = "gadget",
  946. };
  947. /**
  948. * gether_setup_name - initialize one ethernet-over-usb link
  949. * @g: gadget to associated with these links
  950. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  951. * host side of the link is recorded
  952. * @netname: name for network device (for example, "usb")
  953. * Context: may sleep
  954. *
  955. * This sets up the single network link that may be exported by a
  956. * gadget driver using this framework. The link layer addresses are
  957. * set up using module parameters.
  958. *
  959. * Returns negative errno, or zero on success
  960. */
  961. struct eth_dev *gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
  962. const char *netname)
  963. {
  964. struct eth_dev *dev;
  965. struct net_device *net;
  966. int status;
  967. net = alloc_etherdev(sizeof *dev);
  968. if (!net)
  969. return ERR_PTR(-ENOMEM);
  970. dev = netdev_priv(net);
  971. spin_lock_init(&dev->lock);
  972. spin_lock_init(&dev->req_lock);
  973. spin_lock_init(&dev->reqrx_lock);
  974. INIT_WORK(&dev->work, eth_work);
  975. INIT_WORK(&dev->rx_work, process_rx_w);
  976. INIT_WORK(&dev->rx_work1, process_rx_w1);
  977. INIT_LIST_HEAD(&dev->tx_reqs);
  978. INIT_LIST_HEAD(&dev->rx_reqs);
  979. skb_queue_head_init(&dev->rx_frames);
  980. /* network device setup */
  981. dev->net = net;
  982. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  983. if (get_ether_addr(dev_addr, net->dev_addr))
  984. dev_warn(&g->dev,
  985. "using random %s ethernet address\n", "self");
  986. if (get_ether_addr(host_addr, dev->host_mac))
  987. dev_warn(&g->dev,
  988. "using random %s ethernet address\n", "host");
  989. if (ethaddr)
  990. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  991. net->netdev_ops = &eth_netdev_ops;
  992. net->ethtool_ops = &ops;
  993. dev->gadget = g;
  994. SET_NETDEV_DEV(net, &g->dev);
  995. SET_NETDEV_DEVTYPE(net, &gadget_type);
  996. status = register_netdev(net);
  997. if (status < 0) {
  998. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  999. free_netdev(net);
  1000. dev = ERR_PTR(status);
  1001. } else {
  1002. INFO(dev, "MAC %pM\n", net->dev_addr);
  1003. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  1004. /* two kinds of host-initiated state changes:
  1005. * - iff DATA transfer is active, carrier is "on"
  1006. * - tx queueing enabled if open *and* carrier is "on"
  1007. */
  1008. netif_carrier_off(net);
  1009. }
  1010. return dev;
  1011. }
  1012. void gether_update_dl_max_xfer_size(struct gether *link, uint32_t s)
  1013. {
  1014. struct eth_dev *dev = link->ioport;
  1015. unsigned long flags;
  1016. spin_lock_irqsave(&dev->lock, flags);
  1017. dev->dl_max_xfer_size = s;
  1018. spin_unlock_irqrestore(&dev->lock, flags);
  1019. }
  1020. /**
  1021. * gether_cleanup - remove Ethernet-over-USB device
  1022. * Context: may sleep
  1023. *
  1024. * This is called to free all resources allocated by @gether_setup().
  1025. */
  1026. void gether_cleanup(struct eth_dev *dev)
  1027. {
  1028. if (!dev)
  1029. return;
  1030. unregister_netdev(dev->net);
  1031. flush_work(&dev->work);
  1032. free_netdev(dev->net);
  1033. }
  1034. /**
  1035. * gether_connect - notify network layer that USB link is active
  1036. * @link: the USB link, set up with endpoints, descriptors matching
  1037. * current device speed, and any framing wrapper(s) set up.
  1038. * Context: irqs blocked
  1039. *
  1040. * This is called to activate endpoints and let the network layer know
  1041. * the connection is active ("carrier detect"). It may cause the I/O
  1042. * queues to open and start letting network packets flow, but will in
  1043. * any case activate the endpoints so that they respond properly to the
  1044. * USB host.
  1045. *
  1046. * Verify net_device pointer returned using IS_ERR(). If it doesn't
  1047. * indicate some error code (negative errno), ep->driver_data values
  1048. * have been overwritten.
  1049. */
  1050. struct net_device *gether_connect(struct gether *link)
  1051. {
  1052. struct eth_dev *dev = link->ioport;
  1053. int result = 0;
  1054. if (!dev)
  1055. return ERR_PTR(-EINVAL);
  1056. link->header = kzalloc(sizeof(struct rndis_packet_msg_type),
  1057. GFP_ATOMIC);
  1058. if (!link->header) {
  1059. pr_err("RNDIS header memory allocation failed.\n");
  1060. result = -ENOMEM;
  1061. goto fail;
  1062. }
  1063. pr_debug("[XLOG_INFO][UTHER]%s\n", __func__);
  1064. link->in_ep->driver_data = dev;
  1065. result = usb_ep_enable(link->in_ep);
  1066. if (result != 0) {
  1067. DBG(dev, "enable %s --> %d\n",
  1068. link->in_ep->name, result);
  1069. goto fail0;
  1070. }
  1071. link->out_ep->driver_data = dev;
  1072. result = usb_ep_enable(link->out_ep);
  1073. if (result != 0) {
  1074. DBG(dev, "enable %s --> %d\n",
  1075. link->out_ep->name, result);
  1076. goto fail1;
  1077. }
  1078. if (result == 0) {
  1079. result = alloc_tx_requests(dev, link, qlen(dev->gadget));
  1080. if(result == 0)
  1081. result = alloc_rx_requests(dev, link, qlenrx(dev->gadget));
  1082. }
  1083. if (result == 0) {
  1084. dev->zlp = link->is_zlp_ok;
  1085. DBG(dev, "qlen %d\n", qlen(dev->gadget));
  1086. dev->header_len = link->header_len;
  1087. dev->unwrap = link->unwrap;
  1088. dev->wrap = link->wrap;
  1089. dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
  1090. dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
  1091. dev->dl_max_xfer_size = link->dl_max_transfer_len;
  1092. spin_lock(&dev->lock);
  1093. dev->tx_skb_hold_count = 0;
  1094. dev->no_tx_req_used = 0;
  1095. dev->tx_req_bufsize = 0;
  1096. dev->port_usb = link;
  1097. if (netif_running(dev->net)) {
  1098. if (link->open)
  1099. link->open(link);
  1100. } else {
  1101. if (link->close)
  1102. link->close(link);
  1103. }
  1104. spin_unlock(&dev->lock);
  1105. netif_carrier_on(dev->net);
  1106. if (netif_running(dev->net))
  1107. eth_start(dev, GFP_ATOMIC);
  1108. /* on error, disable any endpoints */
  1109. } else {
  1110. (void) usb_ep_disable(link->out_ep);
  1111. fail1:
  1112. (void) usb_ep_disable(link->in_ep);
  1113. }
  1114. /* caller is responsible for cleanup on error */
  1115. if (result < 0) {
  1116. fail0:
  1117. kfree(link->header);
  1118. fail:
  1119. return ERR_PTR(result);
  1120. }
  1121. return dev->net;
  1122. }
  1123. /**
  1124. * gether_disconnect - notify network layer that USB link is inactive
  1125. * @link: the USB link, on which gether_connect() was called
  1126. * Context: irqs blocked
  1127. *
  1128. * This is called to deactivate endpoints and let the network layer know
  1129. * the connection went inactive ("no carrier").
  1130. *
  1131. * On return, the state is as if gether_connect() had never been called.
  1132. * The endpoints are inactive, and accordingly without active USB I/O.
  1133. * Pointers to endpoint descriptors and endpoint private data are nulled.
  1134. */
  1135. void gether_disconnect(struct gether *link)
  1136. {
  1137. struct eth_dev *dev = link->ioport;
  1138. struct usb_request *req;
  1139. struct sk_buff *skb;
  1140. WARN_ON(!dev);
  1141. if (!dev)
  1142. return;
  1143. DBG(dev, "%s\n", __func__);
  1144. pr_debug("[XLOG_INFO][UTHER]%s\n", __func__);
  1145. rndis_test_rx_usb_in = 0 ;
  1146. rndis_test_rx_net_out = 0 ;
  1147. rndis_test_rx_nomem = 0 ;
  1148. rndis_test_rx_error = 0 ;
  1149. rndis_test_tx_net_in = 0 ;
  1150. rndis_test_tx_busy = 0 ;
  1151. rndis_test_tx_usb_out = 0 ;
  1152. rndis_test_tx_complete = 0 ;
  1153. netif_stop_queue(dev->net);
  1154. netif_carrier_off(dev->net);
  1155. /* disable endpoints, forcing (synchronous) completion
  1156. * of all pending i/o. then free the request objects
  1157. * and forget about the endpoints.
  1158. */
  1159. usb_ep_disable(link->in_ep);
  1160. spin_lock(&dev->req_lock);
  1161. while (!list_empty(&dev->tx_reqs)) {
  1162. req = container_of(dev->tx_reqs.next,
  1163. struct usb_request, list);
  1164. list_del(&req->list);
  1165. spin_unlock(&dev->req_lock);
  1166. if (link->multi_pkt_xfer) {
  1167. kfree(req->buf);
  1168. req->buf = NULL;
  1169. }
  1170. usb_ep_free_request(link->in_ep, req);
  1171. spin_lock(&dev->req_lock);
  1172. }
  1173. /* Free rndis header buffer memory */
  1174. kfree(link->header);
  1175. link->header = NULL;
  1176. spin_unlock(&dev->req_lock);
  1177. link->in_ep->driver_data = NULL;
  1178. link->in_ep->desc = NULL;
  1179. usb_ep_disable(link->out_ep);
  1180. spin_lock(&dev->reqrx_lock);
  1181. while (!list_empty(&dev->rx_reqs)) {
  1182. req = container_of(dev->rx_reqs.next,
  1183. struct usb_request, list);
  1184. list_del(&req->list);
  1185. spin_unlock(&dev->reqrx_lock);
  1186. usb_ep_free_request(link->out_ep, req);
  1187. spin_lock(&dev->reqrx_lock);
  1188. }
  1189. spin_unlock(&dev->reqrx_lock);
  1190. spin_lock(&dev->rx_frames.lock);
  1191. while ((skb = __skb_dequeue(&dev->rx_frames)))
  1192. dev_kfree_skb_any(skb);
  1193. spin_unlock(&dev->rx_frames.lock);
  1194. link->out_ep->driver_data = NULL;
  1195. link->out_ep->desc = NULL;
  1196. /* finish forgetting about this USB link episode */
  1197. dev->header_len = 0;
  1198. dev->unwrap = NULL;
  1199. dev->wrap = NULL;
  1200. spin_lock(&dev->lock);
  1201. dev->port_usb = NULL;
  1202. spin_unlock(&dev->lock);
  1203. }
  1204. static int __init gether_init(void)
  1205. {
  1206. uether_wq = create_singlethread_workqueue("uether");
  1207. if (!uether_wq) {
  1208. pr_err("%s: Unable to create workqueue: uether\n", __func__);
  1209. return -ENOMEM;
  1210. }
  1211. uether_wq1 = create_singlethread_workqueue("uether_rx1");
  1212. if (!uether_wq1) {
  1213. destroy_workqueue(uether_wq);
  1214. pr_err("%s: Unable to create workqueue: uether\n", __func__);
  1215. return -ENOMEM;
  1216. }
  1217. return 0;
  1218. }
  1219. module_init(gether_init);
  1220. static void __exit gether_exit(void)
  1221. {
  1222. destroy_workqueue(uether_wq);
  1223. destroy_workqueue(uether_wq1);
  1224. }
  1225. module_exit(gether_exit);
  1226. MODULE_DESCRIPTION("ethernet over USB driver");
  1227. MODULE_LICENSE("GPL v2");