wa-xfer.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. /*
  2. * WUSB Wire Adapter
  3. * Data transfer and URB enqueing
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * How transfers work: get a buffer, break it up in segments (segment
  24. * size is a multiple of the maxpacket size). For each segment issue a
  25. * segment request (struct wa_xfer_*), then send the data buffer if
  26. * out or nothing if in (all over the DTO endpoint).
  27. *
  28. * For each submitted segment request, a notification will come over
  29. * the NEP endpoint and a transfer result (struct xfer_result) will
  30. * arrive in the DTI URB. Read it, get the xfer ID, see if there is
  31. * data coming (inbound transfer), schedule a read and handle it.
  32. *
  33. * Sounds simple, it is a pain to implement.
  34. *
  35. *
  36. * ENTRY POINTS
  37. *
  38. * FIXME
  39. *
  40. * LIFE CYCLE / STATE DIAGRAM
  41. *
  42. * FIXME
  43. *
  44. * THIS CODE IS DISGUSTING
  45. *
  46. * Warned you are; it's my second try and still not happy with it.
  47. *
  48. * NOTES:
  49. *
  50. * - No iso
  51. *
  52. * - Supports DMA xfers, control, bulk and maybe interrupt
  53. *
  54. * - Does not recycle unused rpipes
  55. *
  56. * An rpipe is assigned to an endpoint the first time it is used,
  57. * and then it's there, assigned, until the endpoint is disabled
  58. * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the
  59. * rpipe to the endpoint is done under the wa->rpipe_sem semaphore
  60. * (should be a mutex).
  61. *
  62. * Two methods it could be done:
  63. *
  64. * (a) set up a timer every time an rpipe's use count drops to 1
  65. * (which means unused) or when a transfer ends. Reset the
  66. * timer when a xfer is queued. If the timer expires, release
  67. * the rpipe [see rpipe_ep_disable()].
  68. *
  69. * (b) when looking for free rpipes to attach [rpipe_get_by_ep()],
  70. * when none are found go over the list, check their endpoint
  71. * and their activity record (if no last-xfer-done-ts in the
  72. * last x seconds) take it
  73. *
  74. * However, due to the fact that we have a set of limited
  75. * resources (max-segments-at-the-same-time per xfer,
  76. * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end
  77. * we are going to have to rebuild all this based on an scheduler,
  78. * to where we have a list of transactions to do and based on the
  79. * availability of the different required components (blocks,
  80. * rpipes, segment slots, etc), we go scheduling them. Painful.
  81. */
  82. #include <linux/init.h>
  83. #include <linux/spinlock.h>
  84. #include <linux/slab.h>
  85. #include <linux/hash.h>
  86. #include <linux/ratelimit.h>
  87. #include <linux/export.h>
  88. #include <linux/scatterlist.h>
  89. #include "wa-hc.h"
  90. #include "wusbhc.h"
  91. enum {
  92. WA_SEGS_MAX = 255,
  93. };
  94. enum wa_seg_status {
  95. WA_SEG_NOTREADY,
  96. WA_SEG_READY,
  97. WA_SEG_DELAYED,
  98. WA_SEG_SUBMITTED,
  99. WA_SEG_PENDING,
  100. WA_SEG_DTI_PENDING,
  101. WA_SEG_DONE,
  102. WA_SEG_ERROR,
  103. WA_SEG_ABORTED,
  104. };
  105. static void wa_xfer_delayed_run(struct wa_rpipe *);
  106. /*
  107. * Life cycle governed by 'struct urb' (the refcount of the struct is
  108. * that of the 'struct urb' and usb_free_urb() would free the whole
  109. * struct).
  110. */
  111. struct wa_seg {
  112. struct urb tr_urb; /* transfer request urb. */
  113. struct urb *dto_urb; /* for data output. */
  114. struct list_head list_node; /* for rpipe->req_list */
  115. struct wa_xfer *xfer; /* out xfer */
  116. u8 index; /* which segment we are */
  117. enum wa_seg_status status;
  118. ssize_t result; /* bytes xfered or error */
  119. struct wa_xfer_hdr xfer_hdr;
  120. u8 xfer_extra[]; /* xtra space for xfer_hdr_ctl */
  121. };
  122. static inline void wa_seg_init(struct wa_seg *seg)
  123. {
  124. usb_init_urb(&seg->tr_urb);
  125. /* set the remaining memory to 0. */
  126. memset(((void *)seg) + sizeof(seg->tr_urb), 0,
  127. sizeof(*seg) - sizeof(seg->tr_urb));
  128. }
  129. /*
  130. * Protected by xfer->lock
  131. *
  132. */
  133. struct wa_xfer {
  134. struct kref refcnt;
  135. struct list_head list_node;
  136. spinlock_t lock;
  137. u32 id;
  138. struct wahc *wa; /* Wire adapter we are plugged to */
  139. struct usb_host_endpoint *ep;
  140. struct urb *urb; /* URB we are transferring for */
  141. struct wa_seg **seg; /* transfer segments */
  142. u8 segs, segs_submitted, segs_done;
  143. unsigned is_inbound:1;
  144. unsigned is_dma:1;
  145. size_t seg_size;
  146. int result;
  147. gfp_t gfp; /* allocation mask */
  148. struct wusb_dev *wusb_dev; /* for activity timestamps */
  149. };
  150. static inline void wa_xfer_init(struct wa_xfer *xfer)
  151. {
  152. kref_init(&xfer->refcnt);
  153. INIT_LIST_HEAD(&xfer->list_node);
  154. spin_lock_init(&xfer->lock);
  155. }
  156. /*
  157. * Destroy a transfer structure
  158. *
  159. * Note that freeing xfer->seg[cnt]->urb will free the containing
  160. * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
  161. */
  162. static void wa_xfer_destroy(struct kref *_xfer)
  163. {
  164. struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
  165. if (xfer->seg) {
  166. unsigned cnt;
  167. for (cnt = 0; cnt < xfer->segs; cnt++) {
  168. usb_free_urb(xfer->seg[cnt]->dto_urb);
  169. usb_free_urb(&xfer->seg[cnt]->tr_urb);
  170. }
  171. }
  172. kfree(xfer);
  173. }
  174. static void wa_xfer_get(struct wa_xfer *xfer)
  175. {
  176. kref_get(&xfer->refcnt);
  177. }
  178. static void wa_xfer_put(struct wa_xfer *xfer)
  179. {
  180. kref_put(&xfer->refcnt, wa_xfer_destroy);
  181. }
  182. /*
  183. * xfer is referenced
  184. *
  185. * xfer->lock has to be unlocked
  186. *
  187. * We take xfer->lock for setting the result; this is a barrier
  188. * against drivers/usb/core/hcd.c:unlink1() being called after we call
  189. * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
  190. * reference to the transfer.
  191. */
  192. static void wa_xfer_giveback(struct wa_xfer *xfer)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
  196. list_del_init(&xfer->list_node);
  197. spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
  198. /* FIXME: segmentation broken -- kills DWA */
  199. wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
  200. wa_put(xfer->wa);
  201. wa_xfer_put(xfer);
  202. }
  203. /*
  204. * xfer is referenced
  205. *
  206. * xfer->lock has to be unlocked
  207. */
  208. static void wa_xfer_completion(struct wa_xfer *xfer)
  209. {
  210. if (xfer->wusb_dev)
  211. wusb_dev_put(xfer->wusb_dev);
  212. rpipe_put(xfer->ep->hcpriv);
  213. wa_xfer_giveback(xfer);
  214. }
  215. /*
  216. * If transfer is done, wrap it up and return true
  217. *
  218. * xfer->lock has to be locked
  219. */
  220. static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
  221. {
  222. struct device *dev = &xfer->wa->usb_iface->dev;
  223. unsigned result, cnt;
  224. struct wa_seg *seg;
  225. struct urb *urb = xfer->urb;
  226. unsigned found_short = 0;
  227. result = xfer->segs_done == xfer->segs_submitted;
  228. if (result == 0)
  229. goto out;
  230. urb->actual_length = 0;
  231. for (cnt = 0; cnt < xfer->segs; cnt++) {
  232. seg = xfer->seg[cnt];
  233. switch (seg->status) {
  234. case WA_SEG_DONE:
  235. if (found_short && seg->result > 0) {
  236. dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n",
  237. xfer, cnt, seg->result);
  238. urb->status = -EINVAL;
  239. goto out;
  240. }
  241. urb->actual_length += seg->result;
  242. if (seg->result < xfer->seg_size
  243. && cnt != xfer->segs-1)
  244. found_short = 1;
  245. dev_dbg(dev, "xfer %p#%u: DONE short %d "
  246. "result %zu urb->actual_length %d\n",
  247. xfer, seg->index, found_short, seg->result,
  248. urb->actual_length);
  249. break;
  250. case WA_SEG_ERROR:
  251. xfer->result = seg->result;
  252. dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n",
  253. xfer, seg->index, seg->result);
  254. goto out;
  255. case WA_SEG_ABORTED:
  256. dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n",
  257. xfer, seg->index, urb->status);
  258. xfer->result = urb->status;
  259. goto out;
  260. default:
  261. dev_warn(dev, "xfer %p#%u: is_done bad state %d\n",
  262. xfer, cnt, seg->status);
  263. xfer->result = -EINVAL;
  264. goto out;
  265. }
  266. }
  267. xfer->result = 0;
  268. out:
  269. return result;
  270. }
  271. /*
  272. * Initialize a transfer's ID
  273. *
  274. * We need to use a sequential number; if we use the pointer or the
  275. * hash of the pointer, it can repeat over sequential transfers and
  276. * then it will confuse the HWA....wonder why in hell they put a 32
  277. * bit handle in there then.
  278. */
  279. static void wa_xfer_id_init(struct wa_xfer *xfer)
  280. {
  281. xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
  282. }
  283. /*
  284. * Return the xfer's ID associated with xfer
  285. *
  286. * Need to generate a
  287. */
  288. static u32 wa_xfer_id(struct wa_xfer *xfer)
  289. {
  290. return xfer->id;
  291. }
  292. /*
  293. * Search for a transfer list ID on the HCD's URB list
  294. *
  295. * For 32 bit architectures, we use the pointer itself; for 64 bits, a
  296. * 32-bit hash of the pointer.
  297. *
  298. * @returns NULL if not found.
  299. */
  300. static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
  301. {
  302. unsigned long flags;
  303. struct wa_xfer *xfer_itr;
  304. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  305. list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
  306. if (id == xfer_itr->id) {
  307. wa_xfer_get(xfer_itr);
  308. goto out;
  309. }
  310. }
  311. xfer_itr = NULL;
  312. out:
  313. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  314. return xfer_itr;
  315. }
  316. struct wa_xfer_abort_buffer {
  317. struct urb urb;
  318. struct wa_xfer_abort cmd;
  319. };
  320. static void __wa_xfer_abort_cb(struct urb *urb)
  321. {
  322. struct wa_xfer_abort_buffer *b = urb->context;
  323. usb_put_urb(&b->urb);
  324. }
  325. /*
  326. * Aborts an ongoing transaction
  327. *
  328. * Assumes the transfer is referenced and locked and in a submitted
  329. * state (mainly that there is an endpoint/rpipe assigned).
  330. *
  331. * The callback (see above) does nothing but freeing up the data by
  332. * putting the URB. Because the URB is allocated at the head of the
  333. * struct, the whole space we allocated is kfreed.
  334. *
  335. * We'll get an 'aborted transaction' xfer result on DTI, that'll
  336. * politely ignore because at this point the transaction has been
  337. * marked as aborted already.
  338. */
  339. static void __wa_xfer_abort(struct wa_xfer *xfer)
  340. {
  341. int result;
  342. struct device *dev = &xfer->wa->usb_iface->dev;
  343. struct wa_xfer_abort_buffer *b;
  344. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  345. b = kmalloc(sizeof(*b), GFP_ATOMIC);
  346. if (b == NULL)
  347. goto error_kmalloc;
  348. b->cmd.bLength = sizeof(b->cmd);
  349. b->cmd.bRequestType = WA_XFER_ABORT;
  350. b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
  351. b->cmd.dwTransferID = wa_xfer_id(xfer);
  352. usb_init_urb(&b->urb);
  353. usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
  354. usb_sndbulkpipe(xfer->wa->usb_dev,
  355. xfer->wa->dto_epd->bEndpointAddress),
  356. &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
  357. result = usb_submit_urb(&b->urb, GFP_ATOMIC);
  358. if (result < 0)
  359. goto error_submit;
  360. return; /* callback frees! */
  361. error_submit:
  362. if (printk_ratelimit())
  363. dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
  364. xfer, result);
  365. kfree(b);
  366. error_kmalloc:
  367. return;
  368. }
  369. /*
  370. *
  371. * @returns < 0 on error, transfer segment request size if ok
  372. */
  373. static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
  374. enum wa_xfer_type *pxfer_type)
  375. {
  376. ssize_t result;
  377. struct device *dev = &xfer->wa->usb_iface->dev;
  378. size_t maxpktsize;
  379. struct urb *urb = xfer->urb;
  380. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  381. switch (rpipe->descr.bmAttribute & 0x3) {
  382. case USB_ENDPOINT_XFER_CONTROL:
  383. *pxfer_type = WA_XFER_TYPE_CTL;
  384. result = sizeof(struct wa_xfer_ctl);
  385. break;
  386. case USB_ENDPOINT_XFER_INT:
  387. case USB_ENDPOINT_XFER_BULK:
  388. *pxfer_type = WA_XFER_TYPE_BI;
  389. result = sizeof(struct wa_xfer_bi);
  390. break;
  391. case USB_ENDPOINT_XFER_ISOC:
  392. dev_err(dev, "FIXME: ISOC not implemented\n");
  393. result = -ENOSYS;
  394. goto error;
  395. default:
  396. /* never happens */
  397. BUG();
  398. result = -EINVAL; /* shut gcc up */
  399. };
  400. xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
  401. xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
  402. xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
  403. * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
  404. /* Compute the segment size and make sure it is a multiple of
  405. * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
  406. * a check (FIXME) */
  407. maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
  408. if (xfer->seg_size < maxpktsize) {
  409. dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
  410. "%zu\n", xfer->seg_size, maxpktsize);
  411. result = -EINVAL;
  412. goto error;
  413. }
  414. xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
  415. xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size);
  416. if (xfer->segs >= WA_SEGS_MAX) {
  417. dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
  418. (int)(urb->transfer_buffer_length / xfer->seg_size),
  419. WA_SEGS_MAX);
  420. result = -EINVAL;
  421. goto error;
  422. }
  423. if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
  424. xfer->segs = 1;
  425. error:
  426. return result;
  427. }
  428. /* Fill in the common request header and xfer-type specific data. */
  429. static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
  430. struct wa_xfer_hdr *xfer_hdr0,
  431. enum wa_xfer_type xfer_type,
  432. size_t xfer_hdr_size)
  433. {
  434. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  435. xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
  436. xfer_hdr0->bLength = xfer_hdr_size;
  437. xfer_hdr0->bRequestType = xfer_type;
  438. xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
  439. xfer_hdr0->dwTransferID = wa_xfer_id(xfer);
  440. xfer_hdr0->bTransferSegment = 0;
  441. switch (xfer_type) {
  442. case WA_XFER_TYPE_CTL: {
  443. struct wa_xfer_ctl *xfer_ctl =
  444. container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
  445. xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
  446. memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
  447. sizeof(xfer_ctl->baSetupData));
  448. break;
  449. }
  450. case WA_XFER_TYPE_BI:
  451. break;
  452. case WA_XFER_TYPE_ISO:
  453. printk(KERN_ERR "FIXME: ISOC not implemented\n");
  454. default:
  455. BUG();
  456. };
  457. }
  458. /*
  459. * Callback for the OUT data phase of the segment request
  460. *
  461. * Check wa_seg_tr_cb(); most comments also apply here because this
  462. * function does almost the same thing and they work closely
  463. * together.
  464. *
  465. * If the seg request has failed but this DTO phase has succeeded,
  466. * wa_seg_tr_cb() has already failed the segment and moved the
  467. * status to WA_SEG_ERROR, so this will go through 'case 0' and
  468. * effectively do nothing.
  469. */
  470. static void wa_seg_dto_cb(struct urb *urb)
  471. {
  472. struct wa_seg *seg = urb->context;
  473. struct wa_xfer *xfer = seg->xfer;
  474. struct wahc *wa;
  475. struct device *dev;
  476. struct wa_rpipe *rpipe;
  477. unsigned long flags;
  478. unsigned rpipe_ready = 0;
  479. u8 done = 0;
  480. switch (urb->status) {
  481. case 0:
  482. spin_lock_irqsave(&xfer->lock, flags);
  483. wa = xfer->wa;
  484. dev = &wa->usb_iface->dev;
  485. dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n",
  486. xfer, seg->index, urb->actual_length);
  487. if (seg->status < WA_SEG_PENDING)
  488. seg->status = WA_SEG_PENDING;
  489. seg->result = urb->actual_length;
  490. spin_unlock_irqrestore(&xfer->lock, flags);
  491. break;
  492. case -ECONNRESET: /* URB unlinked; no need to do anything */
  493. case -ENOENT: /* as it was done by the who unlinked us */
  494. break;
  495. default: /* Other errors ... */
  496. spin_lock_irqsave(&xfer->lock, flags);
  497. wa = xfer->wa;
  498. dev = &wa->usb_iface->dev;
  499. rpipe = xfer->ep->hcpriv;
  500. dev_dbg(dev, "xfer %p#%u: data out error %d\n",
  501. xfer, seg->index, urb->status);
  502. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  503. EDC_ERROR_TIMEFRAME)){
  504. dev_err(dev, "DTO: URB max acceptable errors "
  505. "exceeded, resetting device\n");
  506. wa_reset_all(wa);
  507. }
  508. if (seg->status != WA_SEG_ERROR) {
  509. seg->status = WA_SEG_ERROR;
  510. seg->result = urb->status;
  511. xfer->segs_done++;
  512. __wa_xfer_abort(xfer);
  513. rpipe_ready = rpipe_avail_inc(rpipe);
  514. done = __wa_xfer_is_done(xfer);
  515. }
  516. spin_unlock_irqrestore(&xfer->lock, flags);
  517. if (done)
  518. wa_xfer_completion(xfer);
  519. if (rpipe_ready)
  520. wa_xfer_delayed_run(rpipe);
  521. }
  522. }
  523. /*
  524. * Callback for the segment request
  525. *
  526. * If successful transition state (unless already transitioned or
  527. * outbound transfer); otherwise, take a note of the error, mark this
  528. * segment done and try completion.
  529. *
  530. * Note we don't access until we are sure that the transfer hasn't
  531. * been cancelled (ECONNRESET, ENOENT), which could mean that
  532. * seg->xfer could be already gone.
  533. *
  534. * We have to check before setting the status to WA_SEG_PENDING
  535. * because sometimes the xfer result callback arrives before this
  536. * callback (geeeeeeze), so it might happen that we are already in
  537. * another state. As well, we don't set it if the transfer is inbound,
  538. * as in that case, wa_seg_dto_cb will do it when the OUT data phase
  539. * finishes.
  540. */
  541. static void wa_seg_tr_cb(struct urb *urb)
  542. {
  543. struct wa_seg *seg = urb->context;
  544. struct wa_xfer *xfer = seg->xfer;
  545. struct wahc *wa;
  546. struct device *dev;
  547. struct wa_rpipe *rpipe;
  548. unsigned long flags;
  549. unsigned rpipe_ready;
  550. u8 done = 0;
  551. switch (urb->status) {
  552. case 0:
  553. spin_lock_irqsave(&xfer->lock, flags);
  554. wa = xfer->wa;
  555. dev = &wa->usb_iface->dev;
  556. dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index);
  557. if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
  558. seg->status = WA_SEG_PENDING;
  559. spin_unlock_irqrestore(&xfer->lock, flags);
  560. break;
  561. case -ECONNRESET: /* URB unlinked; no need to do anything */
  562. case -ENOENT: /* as it was done by the who unlinked us */
  563. break;
  564. default: /* Other errors ... */
  565. spin_lock_irqsave(&xfer->lock, flags);
  566. wa = xfer->wa;
  567. dev = &wa->usb_iface->dev;
  568. rpipe = xfer->ep->hcpriv;
  569. if (printk_ratelimit())
  570. dev_err(dev, "xfer %p#%u: request error %d\n",
  571. xfer, seg->index, urb->status);
  572. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  573. EDC_ERROR_TIMEFRAME)){
  574. dev_err(dev, "DTO: URB max acceptable errors "
  575. "exceeded, resetting device\n");
  576. wa_reset_all(wa);
  577. }
  578. usb_unlink_urb(seg->dto_urb);
  579. seg->status = WA_SEG_ERROR;
  580. seg->result = urb->status;
  581. xfer->segs_done++;
  582. __wa_xfer_abort(xfer);
  583. rpipe_ready = rpipe_avail_inc(rpipe);
  584. done = __wa_xfer_is_done(xfer);
  585. spin_unlock_irqrestore(&xfer->lock, flags);
  586. if (done)
  587. wa_xfer_completion(xfer);
  588. if (rpipe_ready)
  589. wa_xfer_delayed_run(rpipe);
  590. }
  591. }
  592. /* allocate an SG list to store bytes_to_transfer bytes and copy the
  593. * subset of the in_sg that matches the buffer subset
  594. * we are about to transfer. */
  595. static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
  596. const unsigned int bytes_transferred,
  597. const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
  598. {
  599. struct scatterlist *out_sg;
  600. unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
  601. nents;
  602. struct scatterlist *current_xfer_sg = in_sg;
  603. struct scatterlist *current_seg_sg, *last_seg_sg;
  604. /* skip previously transferred pages. */
  605. while ((current_xfer_sg) &&
  606. (bytes_processed < bytes_transferred)) {
  607. bytes_processed += current_xfer_sg->length;
  608. /* advance the sg if current segment starts on or past the
  609. next page. */
  610. if (bytes_processed <= bytes_transferred)
  611. current_xfer_sg = sg_next(current_xfer_sg);
  612. }
  613. /* the data for the current segment starts in current_xfer_sg.
  614. calculate the offset. */
  615. if (bytes_processed > bytes_transferred) {
  616. offset_into_current_page_data = current_xfer_sg->length -
  617. (bytes_processed - bytes_transferred);
  618. }
  619. /* calculate the number of pages needed by this segment. */
  620. nents = DIV_ROUND_UP((bytes_to_transfer +
  621. offset_into_current_page_data +
  622. current_xfer_sg->offset),
  623. PAGE_SIZE);
  624. out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
  625. if (out_sg) {
  626. sg_init_table(out_sg, nents);
  627. /* copy the portion of the incoming SG that correlates to the
  628. * data to be transferred by this segment to the segment SG. */
  629. last_seg_sg = current_seg_sg = out_sg;
  630. bytes_processed = 0;
  631. /* reset nents and calculate the actual number of sg entries
  632. needed. */
  633. nents = 0;
  634. while ((bytes_processed < bytes_to_transfer) &&
  635. current_seg_sg && current_xfer_sg) {
  636. unsigned int page_len = min((current_xfer_sg->length -
  637. offset_into_current_page_data),
  638. (bytes_to_transfer - bytes_processed));
  639. sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
  640. page_len,
  641. current_xfer_sg->offset +
  642. offset_into_current_page_data);
  643. bytes_processed += page_len;
  644. last_seg_sg = current_seg_sg;
  645. current_seg_sg = sg_next(current_seg_sg);
  646. current_xfer_sg = sg_next(current_xfer_sg);
  647. /* only the first page may require additional offset. */
  648. offset_into_current_page_data = 0;
  649. nents++;
  650. }
  651. /* update num_sgs and terminate the list since we may have
  652. * concatenated pages. */
  653. sg_mark_end(last_seg_sg);
  654. *out_num_sgs = nents;
  655. }
  656. return out_sg;
  657. }
  658. /*
  659. * Allocate the segs array and initialize each of them
  660. *
  661. * The segments are freed by wa_xfer_destroy() when the xfer use count
  662. * drops to zero; however, because each segment is given the same life
  663. * cycle as the USB URB it contains, it is actually freed by
  664. * usb_put_urb() on the contained USB URB (twisted, eh?).
  665. */
  666. static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
  667. {
  668. int result, cnt;
  669. size_t alloc_size = sizeof(*xfer->seg[0])
  670. - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
  671. struct usb_device *usb_dev = xfer->wa->usb_dev;
  672. const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
  673. struct wa_seg *seg;
  674. size_t buf_itr, buf_size, buf_itr_size;
  675. result = -ENOMEM;
  676. xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
  677. if (xfer->seg == NULL)
  678. goto error_segs_kzalloc;
  679. buf_itr = 0;
  680. buf_size = xfer->urb->transfer_buffer_length;
  681. for (cnt = 0; cnt < xfer->segs; cnt++) {
  682. seg = xfer->seg[cnt] = kmalloc(alloc_size, GFP_ATOMIC);
  683. if (seg == NULL)
  684. goto error_seg_kmalloc;
  685. wa_seg_init(seg);
  686. seg->xfer = xfer;
  687. seg->index = cnt;
  688. usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
  689. usb_sndbulkpipe(usb_dev,
  690. dto_epd->bEndpointAddress),
  691. &seg->xfer_hdr, xfer_hdr_size,
  692. wa_seg_tr_cb, seg);
  693. buf_itr_size = min(buf_size, xfer->seg_size);
  694. if (xfer->is_inbound == 0 && buf_size > 0) {
  695. /* outbound data. */
  696. seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
  697. if (seg->dto_urb == NULL)
  698. goto error_dto_alloc;
  699. usb_fill_bulk_urb(
  700. seg->dto_urb, usb_dev,
  701. usb_sndbulkpipe(usb_dev,
  702. dto_epd->bEndpointAddress),
  703. NULL, 0, wa_seg_dto_cb, seg);
  704. if (xfer->is_dma) {
  705. seg->dto_urb->transfer_dma =
  706. xfer->urb->transfer_dma + buf_itr;
  707. seg->dto_urb->transfer_flags |=
  708. URB_NO_TRANSFER_DMA_MAP;
  709. seg->dto_urb->transfer_buffer = NULL;
  710. seg->dto_urb->sg = NULL;
  711. seg->dto_urb->num_sgs = 0;
  712. } else {
  713. /* do buffer or SG processing. */
  714. seg->dto_urb->transfer_flags &=
  715. ~URB_NO_TRANSFER_DMA_MAP;
  716. /* this should always be 0 before a resubmit. */
  717. seg->dto_urb->num_mapped_sgs = 0;
  718. if (xfer->urb->transfer_buffer) {
  719. seg->dto_urb->transfer_buffer =
  720. xfer->urb->transfer_buffer +
  721. buf_itr;
  722. seg->dto_urb->sg = NULL;
  723. seg->dto_urb->num_sgs = 0;
  724. } else {
  725. /* allocate an SG list to store seg_size
  726. bytes and copy the subset of the
  727. xfer->urb->sg that matches the
  728. buffer subset we are about to read.
  729. */
  730. seg->dto_urb->sg =
  731. wa_xfer_create_subset_sg(
  732. xfer->urb->sg,
  733. buf_itr, buf_itr_size,
  734. &(seg->dto_urb->num_sgs));
  735. if (!(seg->dto_urb->sg)) {
  736. seg->dto_urb->num_sgs = 0;
  737. goto error_sg_alloc;
  738. }
  739. seg->dto_urb->transfer_buffer = NULL;
  740. }
  741. }
  742. seg->dto_urb->transfer_buffer_length = buf_itr_size;
  743. }
  744. seg->status = WA_SEG_READY;
  745. buf_itr += buf_itr_size;
  746. buf_size -= buf_itr_size;
  747. }
  748. return 0;
  749. error_sg_alloc:
  750. usb_free_urb(xfer->seg[cnt]->dto_urb);
  751. error_dto_alloc:
  752. kfree(xfer->seg[cnt]);
  753. cnt--;
  754. error_seg_kmalloc:
  755. /* use the fact that cnt is left at were it failed */
  756. for (; cnt >= 0; cnt--) {
  757. if (xfer->seg[cnt] && xfer->is_inbound == 0) {
  758. usb_free_urb(xfer->seg[cnt]->dto_urb);
  759. kfree(xfer->seg[cnt]->dto_urb->sg);
  760. }
  761. kfree(xfer->seg[cnt]);
  762. }
  763. error_segs_kzalloc:
  764. return result;
  765. }
  766. /*
  767. * Allocates all the stuff needed to submit a transfer
  768. *
  769. * Breaks the whole data buffer in a list of segments, each one has a
  770. * structure allocated to it and linked in xfer->seg[index]
  771. *
  772. * FIXME: merge setup_segs() and the last part of this function, no
  773. * need to do two for loops when we could run everything in a
  774. * single one
  775. */
  776. static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
  777. {
  778. int result;
  779. struct device *dev = &xfer->wa->usb_iface->dev;
  780. enum wa_xfer_type xfer_type = 0; /* shut up GCC */
  781. size_t xfer_hdr_size, cnt, transfer_size;
  782. struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
  783. result = __wa_xfer_setup_sizes(xfer, &xfer_type);
  784. if (result < 0)
  785. goto error_setup_sizes;
  786. xfer_hdr_size = result;
  787. result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
  788. if (result < 0) {
  789. dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
  790. xfer, xfer->segs, result);
  791. goto error_setup_segs;
  792. }
  793. /* Fill the first header */
  794. xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
  795. wa_xfer_id_init(xfer);
  796. __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
  797. /* Fill remainig headers */
  798. xfer_hdr = xfer_hdr0;
  799. transfer_size = urb->transfer_buffer_length;
  800. xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
  801. xfer->seg_size : transfer_size;
  802. transfer_size -= xfer->seg_size;
  803. for (cnt = 1; cnt < xfer->segs; cnt++) {
  804. xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
  805. memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
  806. xfer_hdr->bTransferSegment = cnt;
  807. xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ?
  808. cpu_to_le32(xfer->seg_size)
  809. : cpu_to_le32(transfer_size);
  810. xfer->seg[cnt]->status = WA_SEG_READY;
  811. transfer_size -= xfer->seg_size;
  812. }
  813. xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
  814. result = 0;
  815. error_setup_segs:
  816. error_setup_sizes:
  817. return result;
  818. }
  819. /*
  820. *
  821. *
  822. * rpipe->seg_lock is held!
  823. */
  824. static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
  825. struct wa_seg *seg)
  826. {
  827. int result;
  828. /* submit the transfer request. */
  829. result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
  830. if (result < 0) {
  831. printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n",
  832. xfer, seg->index, result);
  833. goto error_seg_submit;
  834. }
  835. /* submit the out data if this is an out request. */
  836. if (seg->dto_urb) {
  837. result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
  838. if (result < 0) {
  839. printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n",
  840. xfer, seg->index, result);
  841. goto error_dto_submit;
  842. }
  843. }
  844. seg->status = WA_SEG_SUBMITTED;
  845. rpipe_avail_dec(rpipe);
  846. return 0;
  847. error_dto_submit:
  848. usb_unlink_urb(&seg->tr_urb);
  849. error_seg_submit:
  850. seg->status = WA_SEG_ERROR;
  851. seg->result = result;
  852. return result;
  853. }
  854. /*
  855. * Execute more queued request segments until the maximum concurrent allowed
  856. *
  857. * The ugly unlock/lock sequence on the error path is needed as the
  858. * xfer->lock normally nests the seg_lock and not viceversa.
  859. *
  860. */
  861. static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
  862. {
  863. int result;
  864. struct device *dev = &rpipe->wa->usb_iface->dev;
  865. struct wa_seg *seg;
  866. struct wa_xfer *xfer;
  867. unsigned long flags;
  868. spin_lock_irqsave(&rpipe->seg_lock, flags);
  869. while (atomic_read(&rpipe->segs_available) > 0
  870. && !list_empty(&rpipe->seg_list)) {
  871. seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
  872. list_node);
  873. list_del(&seg->list_node);
  874. xfer = seg->xfer;
  875. result = __wa_seg_submit(rpipe, xfer, seg);
  876. dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n",
  877. xfer, seg->index, atomic_read(&rpipe->segs_available), result);
  878. if (unlikely(result < 0)) {
  879. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  880. spin_lock_irqsave(&xfer->lock, flags);
  881. __wa_xfer_abort(xfer);
  882. xfer->segs_done++;
  883. spin_unlock_irqrestore(&xfer->lock, flags);
  884. spin_lock_irqsave(&rpipe->seg_lock, flags);
  885. }
  886. }
  887. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  888. }
  889. /*
  890. *
  891. * xfer->lock is taken
  892. *
  893. * On failure submitting we just stop submitting and return error;
  894. * wa_urb_enqueue_b() will execute the completion path
  895. */
  896. static int __wa_xfer_submit(struct wa_xfer *xfer)
  897. {
  898. int result;
  899. struct wahc *wa = xfer->wa;
  900. struct device *dev = &wa->usb_iface->dev;
  901. unsigned cnt;
  902. struct wa_seg *seg;
  903. unsigned long flags;
  904. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  905. size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
  906. u8 available;
  907. u8 empty;
  908. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  909. list_add_tail(&xfer->list_node, &wa->xfer_list);
  910. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  911. BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
  912. result = 0;
  913. spin_lock_irqsave(&rpipe->seg_lock, flags);
  914. for (cnt = 0; cnt < xfer->segs; cnt++) {
  915. available = atomic_read(&rpipe->segs_available);
  916. empty = list_empty(&rpipe->seg_list);
  917. seg = xfer->seg[cnt];
  918. dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n",
  919. xfer, cnt, available, empty,
  920. available == 0 || !empty ? "delayed" : "submitted");
  921. if (available == 0 || !empty) {
  922. dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt);
  923. seg->status = WA_SEG_DELAYED;
  924. list_add_tail(&seg->list_node, &rpipe->seg_list);
  925. } else {
  926. result = __wa_seg_submit(rpipe, xfer, seg);
  927. if (result < 0) {
  928. __wa_xfer_abort(xfer);
  929. goto error_seg_submit;
  930. }
  931. }
  932. xfer->segs_submitted++;
  933. }
  934. error_seg_submit:
  935. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  936. return result;
  937. }
  938. /*
  939. * Second part of a URB/transfer enqueuement
  940. *
  941. * Assumes this comes from wa_urb_enqueue() [maybe through
  942. * wa_urb_enqueue_run()]. At this point:
  943. *
  944. * xfer->wa filled and refcounted
  945. * xfer->ep filled with rpipe refcounted if
  946. * delayed == 0
  947. * xfer->urb filled and refcounted (this is the case when called
  948. * from wa_urb_enqueue() as we come from usb_submit_urb()
  949. * and when called by wa_urb_enqueue_run(), as we took an
  950. * extra ref dropped by _run() after we return).
  951. * xfer->gfp filled
  952. *
  953. * If we fail at __wa_xfer_submit(), then we just check if we are done
  954. * and if so, we run the completion procedure. However, if we are not
  955. * yet done, we do nothing and wait for the completion handlers from
  956. * the submitted URBs or from the xfer-result path to kick in. If xfer
  957. * result never kicks in, the xfer will timeout from the USB code and
  958. * dequeue() will be called.
  959. */
  960. static void wa_urb_enqueue_b(struct wa_xfer *xfer)
  961. {
  962. int result;
  963. unsigned long flags;
  964. struct urb *urb = xfer->urb;
  965. struct wahc *wa = xfer->wa;
  966. struct wusbhc *wusbhc = wa->wusb;
  967. struct wusb_dev *wusb_dev;
  968. unsigned done;
  969. result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
  970. if (result < 0)
  971. goto error_rpipe_get;
  972. result = -ENODEV;
  973. /* FIXME: segmentation broken -- kills DWA */
  974. mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
  975. if (urb->dev == NULL) {
  976. mutex_unlock(&wusbhc->mutex);
  977. goto error_dev_gone;
  978. }
  979. wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
  980. if (wusb_dev == NULL) {
  981. mutex_unlock(&wusbhc->mutex);
  982. goto error_dev_gone;
  983. }
  984. mutex_unlock(&wusbhc->mutex);
  985. spin_lock_irqsave(&xfer->lock, flags);
  986. xfer->wusb_dev = wusb_dev;
  987. result = urb->status;
  988. if (urb->status != -EINPROGRESS)
  989. goto error_dequeued;
  990. result = __wa_xfer_setup(xfer, urb);
  991. if (result < 0)
  992. goto error_xfer_setup;
  993. result = __wa_xfer_submit(xfer);
  994. if (result < 0)
  995. goto error_xfer_submit;
  996. spin_unlock_irqrestore(&xfer->lock, flags);
  997. return;
  998. /* this is basically wa_xfer_completion() broken up wa_xfer_giveback()
  999. * does a wa_xfer_put() that will call wa_xfer_destroy() and clean
  1000. * upundo setup().
  1001. */
  1002. error_xfer_setup:
  1003. error_dequeued:
  1004. spin_unlock_irqrestore(&xfer->lock, flags);
  1005. /* FIXME: segmentation broken, kills DWA */
  1006. if (wusb_dev)
  1007. wusb_dev_put(wusb_dev);
  1008. error_dev_gone:
  1009. rpipe_put(xfer->ep->hcpriv);
  1010. error_rpipe_get:
  1011. xfer->result = result;
  1012. wa_xfer_giveback(xfer);
  1013. return;
  1014. error_xfer_submit:
  1015. done = __wa_xfer_is_done(xfer);
  1016. xfer->result = result;
  1017. spin_unlock_irqrestore(&xfer->lock, flags);
  1018. if (done)
  1019. wa_xfer_completion(xfer);
  1020. }
  1021. /*
  1022. * Execute the delayed transfers in the Wire Adapter @wa
  1023. *
  1024. * We need to be careful here, as dequeue() could be called in the
  1025. * middle. That's why we do the whole thing under the
  1026. * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
  1027. * and then checks the list -- so as we would be acquiring in inverse
  1028. * order, we move the delayed list to a separate list while locked and then
  1029. * submit them without the list lock held.
  1030. */
  1031. void wa_urb_enqueue_run(struct work_struct *ws)
  1032. {
  1033. struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
  1034. struct wa_xfer *xfer, *next;
  1035. struct urb *urb;
  1036. LIST_HEAD(tmp_list);
  1037. /* Create a copy of the wa->xfer_delayed_list while holding the lock */
  1038. spin_lock_irq(&wa->xfer_list_lock);
  1039. list_cut_position(&tmp_list, &wa->xfer_delayed_list,
  1040. wa->xfer_delayed_list.prev);
  1041. spin_unlock_irq(&wa->xfer_list_lock);
  1042. /*
  1043. * enqueue from temp list without list lock held since wa_urb_enqueue_b
  1044. * can take xfer->lock as well as lock mutexes.
  1045. */
  1046. list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
  1047. list_del_init(&xfer->list_node);
  1048. urb = xfer->urb;
  1049. wa_urb_enqueue_b(xfer);
  1050. usb_put_urb(urb); /* taken when queuing */
  1051. }
  1052. }
  1053. EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
  1054. /*
  1055. * Process the errored transfers on the Wire Adapter outside of interrupt.
  1056. */
  1057. void wa_process_errored_transfers_run(struct work_struct *ws)
  1058. {
  1059. struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
  1060. struct wa_xfer *xfer, *next;
  1061. LIST_HEAD(tmp_list);
  1062. pr_info("%s: Run delayed STALL processing.\n", __func__);
  1063. /* Create a copy of the wa->xfer_errored_list while holding the lock */
  1064. spin_lock_irq(&wa->xfer_list_lock);
  1065. list_cut_position(&tmp_list, &wa->xfer_errored_list,
  1066. wa->xfer_errored_list.prev);
  1067. spin_unlock_irq(&wa->xfer_list_lock);
  1068. /*
  1069. * run rpipe_clear_feature_stalled from temp list without list lock
  1070. * held.
  1071. */
  1072. list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
  1073. struct usb_host_endpoint *ep;
  1074. unsigned long flags;
  1075. struct wa_rpipe *rpipe;
  1076. spin_lock_irqsave(&xfer->lock, flags);
  1077. ep = xfer->ep;
  1078. rpipe = ep->hcpriv;
  1079. spin_unlock_irqrestore(&xfer->lock, flags);
  1080. /* clear RPIPE feature stalled without holding a lock. */
  1081. rpipe_clear_feature_stalled(wa, ep);
  1082. /* complete the xfer. This removes it from the tmp list. */
  1083. wa_xfer_completion(xfer);
  1084. /* check for work. */
  1085. wa_xfer_delayed_run(rpipe);
  1086. }
  1087. }
  1088. EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
  1089. /*
  1090. * Submit a transfer to the Wire Adapter in a delayed way
  1091. *
  1092. * The process of enqueuing involves possible sleeps() [see
  1093. * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
  1094. * in an atomic section, we defer the enqueue_b() call--else we call direct.
  1095. *
  1096. * @urb: We own a reference to it done by the HCI Linux USB stack that
  1097. * will be given up by calling usb_hcd_giveback_urb() or by
  1098. * returning error from this function -> ergo we don't have to
  1099. * refcount it.
  1100. */
  1101. int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
  1102. struct urb *urb, gfp_t gfp)
  1103. {
  1104. int result;
  1105. struct device *dev = &wa->usb_iface->dev;
  1106. struct wa_xfer *xfer;
  1107. unsigned long my_flags;
  1108. unsigned cant_sleep = irqs_disabled() | in_atomic();
  1109. if ((urb->transfer_buffer == NULL)
  1110. && (urb->sg == NULL)
  1111. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1112. && urb->transfer_buffer_length != 0) {
  1113. dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
  1114. dump_stack();
  1115. }
  1116. result = -ENOMEM;
  1117. xfer = kzalloc(sizeof(*xfer), gfp);
  1118. if (xfer == NULL)
  1119. goto error_kmalloc;
  1120. result = -ENOENT;
  1121. if (urb->status != -EINPROGRESS) /* cancelled */
  1122. goto error_dequeued; /* before starting? */
  1123. wa_xfer_init(xfer);
  1124. xfer->wa = wa_get(wa);
  1125. xfer->urb = urb;
  1126. xfer->gfp = gfp;
  1127. xfer->ep = ep;
  1128. urb->hcpriv = xfer;
  1129. dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
  1130. xfer, urb, urb->pipe, urb->transfer_buffer_length,
  1131. urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
  1132. urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
  1133. cant_sleep ? "deferred" : "inline");
  1134. if (cant_sleep) {
  1135. usb_get_urb(urb);
  1136. spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
  1137. list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
  1138. spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
  1139. queue_work(wusbd, &wa->xfer_enqueue_work);
  1140. } else {
  1141. wa_urb_enqueue_b(xfer);
  1142. }
  1143. return 0;
  1144. error_dequeued:
  1145. kfree(xfer);
  1146. error_kmalloc:
  1147. return result;
  1148. }
  1149. EXPORT_SYMBOL_GPL(wa_urb_enqueue);
  1150. /*
  1151. * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
  1152. * handler] is called.
  1153. *
  1154. * Until a transfer goes successfully through wa_urb_enqueue() it
  1155. * needs to be dequeued with completion calling; when stuck in delayed
  1156. * or before wa_xfer_setup() is called, we need to do completion.
  1157. *
  1158. * not setup If there is no hcpriv yet, that means that that enqueue
  1159. * still had no time to set the xfer up. Because
  1160. * urb->status should be other than -EINPROGRESS,
  1161. * enqueue() will catch that and bail out.
  1162. *
  1163. * If the transfer has gone through setup, we just need to clean it
  1164. * up. If it has gone through submit(), we have to abort it [with an
  1165. * asynch request] and then make sure we cancel each segment.
  1166. *
  1167. */
  1168. int wa_urb_dequeue(struct wahc *wa, struct urb *urb)
  1169. {
  1170. unsigned long flags, flags2;
  1171. struct wa_xfer *xfer;
  1172. struct wa_seg *seg;
  1173. struct wa_rpipe *rpipe;
  1174. unsigned cnt;
  1175. unsigned rpipe_ready = 0;
  1176. xfer = urb->hcpriv;
  1177. if (xfer == NULL) {
  1178. /*
  1179. * Nothing setup yet enqueue will see urb->status !=
  1180. * -EINPROGRESS (by hcd layer) and bail out with
  1181. * error, no need to do completion
  1182. */
  1183. BUG_ON(urb->status == -EINPROGRESS);
  1184. goto out;
  1185. }
  1186. spin_lock_irqsave(&xfer->lock, flags);
  1187. rpipe = xfer->ep->hcpriv;
  1188. if (rpipe == NULL) {
  1189. pr_debug("%s: xfer id 0x%08X has no RPIPE. %s",
  1190. __func__, wa_xfer_id(xfer),
  1191. "Probably already aborted.\n" );
  1192. goto out_unlock;
  1193. }
  1194. /* Check the delayed list -> if there, release and complete */
  1195. spin_lock_irqsave(&wa->xfer_list_lock, flags2);
  1196. if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
  1197. goto dequeue_delayed;
  1198. spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
  1199. if (xfer->seg == NULL) /* still hasn't reached */
  1200. goto out_unlock; /* setup(), enqueue_b() completes */
  1201. /* Ok, the xfer is in flight already, it's been setup and submitted.*/
  1202. __wa_xfer_abort(xfer);
  1203. for (cnt = 0; cnt < xfer->segs; cnt++) {
  1204. seg = xfer->seg[cnt];
  1205. switch (seg->status) {
  1206. case WA_SEG_NOTREADY:
  1207. case WA_SEG_READY:
  1208. printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
  1209. xfer, cnt, seg->status);
  1210. WARN_ON(1);
  1211. break;
  1212. case WA_SEG_DELAYED:
  1213. seg->status = WA_SEG_ABORTED;
  1214. spin_lock_irqsave(&rpipe->seg_lock, flags2);
  1215. list_del(&seg->list_node);
  1216. xfer->segs_done++;
  1217. rpipe_ready = rpipe_avail_inc(rpipe);
  1218. spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
  1219. break;
  1220. case WA_SEG_SUBMITTED:
  1221. seg->status = WA_SEG_ABORTED;
  1222. usb_unlink_urb(&seg->tr_urb);
  1223. if (xfer->is_inbound == 0)
  1224. usb_unlink_urb(seg->dto_urb);
  1225. xfer->segs_done++;
  1226. rpipe_ready = rpipe_avail_inc(rpipe);
  1227. break;
  1228. case WA_SEG_PENDING:
  1229. seg->status = WA_SEG_ABORTED;
  1230. xfer->segs_done++;
  1231. rpipe_ready = rpipe_avail_inc(rpipe);
  1232. break;
  1233. case WA_SEG_DTI_PENDING:
  1234. usb_unlink_urb(wa->dti_urb);
  1235. seg->status = WA_SEG_ABORTED;
  1236. xfer->segs_done++;
  1237. rpipe_ready = rpipe_avail_inc(rpipe);
  1238. break;
  1239. case WA_SEG_DONE:
  1240. case WA_SEG_ERROR:
  1241. case WA_SEG_ABORTED:
  1242. break;
  1243. }
  1244. }
  1245. xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
  1246. __wa_xfer_is_done(xfer);
  1247. spin_unlock_irqrestore(&xfer->lock, flags);
  1248. wa_xfer_completion(xfer);
  1249. if (rpipe_ready)
  1250. wa_xfer_delayed_run(rpipe);
  1251. return 0;
  1252. out_unlock:
  1253. spin_unlock_irqrestore(&xfer->lock, flags);
  1254. out:
  1255. return 0;
  1256. dequeue_delayed:
  1257. list_del_init(&xfer->list_node);
  1258. spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
  1259. xfer->result = urb->status;
  1260. spin_unlock_irqrestore(&xfer->lock, flags);
  1261. wa_xfer_giveback(xfer);
  1262. usb_put_urb(urb); /* we got a ref in enqueue() */
  1263. return 0;
  1264. }
  1265. EXPORT_SYMBOL_GPL(wa_urb_dequeue);
  1266. /*
  1267. * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
  1268. * codes
  1269. *
  1270. * Positive errno values are internal inconsistencies and should be
  1271. * flagged louder. Negative are to be passed up to the user in the
  1272. * normal way.
  1273. *
  1274. * @status: USB WA status code -- high two bits are stripped.
  1275. */
  1276. static int wa_xfer_status_to_errno(u8 status)
  1277. {
  1278. int errno;
  1279. u8 real_status = status;
  1280. static int xlat[] = {
  1281. [WA_XFER_STATUS_SUCCESS] = 0,
  1282. [WA_XFER_STATUS_HALTED] = -EPIPE,
  1283. [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
  1284. [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
  1285. [WA_XFER_RESERVED] = EINVAL,
  1286. [WA_XFER_STATUS_NOT_FOUND] = 0,
  1287. [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
  1288. [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
  1289. [WA_XFER_STATUS_ABORTED] = -EINTR,
  1290. [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
  1291. [WA_XFER_INVALID_FORMAT] = EINVAL,
  1292. [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
  1293. [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
  1294. };
  1295. status &= 0x3f;
  1296. if (status == 0)
  1297. return 0;
  1298. if (status >= ARRAY_SIZE(xlat)) {
  1299. printk_ratelimited(KERN_ERR "%s(): BUG? "
  1300. "Unknown WA transfer status 0x%02x\n",
  1301. __func__, real_status);
  1302. return -EINVAL;
  1303. }
  1304. errno = xlat[status];
  1305. if (unlikely(errno > 0)) {
  1306. printk_ratelimited(KERN_ERR "%s(): BUG? "
  1307. "Inconsistent WA status: 0x%02x\n",
  1308. __func__, real_status);
  1309. errno = -errno;
  1310. }
  1311. return errno;
  1312. }
  1313. /*
  1314. * Process a xfer result completion message
  1315. *
  1316. * inbound transfers: need to schedule a DTI read
  1317. *
  1318. * FIXME: this function needs to be broken up in parts
  1319. */
  1320. static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
  1321. struct wa_xfer_result *xfer_result)
  1322. {
  1323. int result;
  1324. struct device *dev = &wa->usb_iface->dev;
  1325. unsigned long flags;
  1326. u8 seg_idx;
  1327. struct wa_seg *seg;
  1328. struct wa_rpipe *rpipe;
  1329. unsigned done = 0;
  1330. u8 usb_status;
  1331. unsigned rpipe_ready = 0;
  1332. spin_lock_irqsave(&xfer->lock, flags);
  1333. seg_idx = xfer_result->bTransferSegment & 0x7f;
  1334. if (unlikely(seg_idx >= xfer->segs))
  1335. goto error_bad_seg;
  1336. seg = xfer->seg[seg_idx];
  1337. rpipe = xfer->ep->hcpriv;
  1338. usb_status = xfer_result->bTransferStatus;
  1339. dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n",
  1340. xfer, seg_idx, usb_status, seg->status);
  1341. if (seg->status == WA_SEG_ABORTED
  1342. || seg->status == WA_SEG_ERROR) /* already handled */
  1343. goto segment_aborted;
  1344. if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
  1345. seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
  1346. if (seg->status != WA_SEG_PENDING) {
  1347. if (printk_ratelimit())
  1348. dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
  1349. xfer, seg_idx, seg->status);
  1350. seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
  1351. }
  1352. if (usb_status & 0x80) {
  1353. seg->result = wa_xfer_status_to_errno(usb_status);
  1354. dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
  1355. xfer, xfer->id, seg->index, usb_status);
  1356. goto error_complete;
  1357. }
  1358. /* FIXME: we ignore warnings, tally them for stats */
  1359. if (usb_status & 0x40) /* Warning?... */
  1360. usb_status = 0; /* ... pass */
  1361. if (xfer->is_inbound) { /* IN data phase: read to buffer */
  1362. seg->status = WA_SEG_DTI_PENDING;
  1363. BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
  1364. /* this should always be 0 before a resubmit. */
  1365. wa->buf_in_urb->num_mapped_sgs = 0;
  1366. if (xfer->is_dma) {
  1367. wa->buf_in_urb->transfer_dma =
  1368. xfer->urb->transfer_dma
  1369. + (seg_idx * xfer->seg_size);
  1370. wa->buf_in_urb->transfer_flags
  1371. |= URB_NO_TRANSFER_DMA_MAP;
  1372. wa->buf_in_urb->transfer_buffer = NULL;
  1373. wa->buf_in_urb->sg = NULL;
  1374. wa->buf_in_urb->num_sgs = 0;
  1375. } else {
  1376. /* do buffer or SG processing. */
  1377. wa->buf_in_urb->transfer_flags
  1378. &= ~URB_NO_TRANSFER_DMA_MAP;
  1379. if (xfer->urb->transfer_buffer) {
  1380. wa->buf_in_urb->transfer_buffer =
  1381. xfer->urb->transfer_buffer
  1382. + (seg_idx * xfer->seg_size);
  1383. wa->buf_in_urb->sg = NULL;
  1384. wa->buf_in_urb->num_sgs = 0;
  1385. } else {
  1386. /* allocate an SG list to store seg_size bytes
  1387. and copy the subset of the xfer->urb->sg
  1388. that matches the buffer subset we are
  1389. about to read. */
  1390. wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
  1391. xfer->urb->sg,
  1392. seg_idx * xfer->seg_size,
  1393. le32_to_cpu(
  1394. xfer_result->dwTransferLength),
  1395. &(wa->buf_in_urb->num_sgs));
  1396. if (!(wa->buf_in_urb->sg)) {
  1397. wa->buf_in_urb->num_sgs = 0;
  1398. goto error_sg_alloc;
  1399. }
  1400. wa->buf_in_urb->transfer_buffer = NULL;
  1401. }
  1402. }
  1403. wa->buf_in_urb->transfer_buffer_length =
  1404. le32_to_cpu(xfer_result->dwTransferLength);
  1405. wa->buf_in_urb->context = seg;
  1406. result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
  1407. if (result < 0)
  1408. goto error_submit_buf_in;
  1409. } else {
  1410. /* OUT data phase, complete it -- */
  1411. seg->status = WA_SEG_DONE;
  1412. seg->result = le32_to_cpu(xfer_result->dwTransferLength);
  1413. xfer->segs_done++;
  1414. rpipe_ready = rpipe_avail_inc(rpipe);
  1415. done = __wa_xfer_is_done(xfer);
  1416. }
  1417. spin_unlock_irqrestore(&xfer->lock, flags);
  1418. if (done)
  1419. wa_xfer_completion(xfer);
  1420. if (rpipe_ready)
  1421. wa_xfer_delayed_run(rpipe);
  1422. return;
  1423. error_submit_buf_in:
  1424. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  1425. dev_err(dev, "DTI: URB max acceptable errors "
  1426. "exceeded, resetting device\n");
  1427. wa_reset_all(wa);
  1428. }
  1429. if (printk_ratelimit())
  1430. dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
  1431. xfer, seg_idx, result);
  1432. seg->result = result;
  1433. kfree(wa->buf_in_urb->sg);
  1434. error_sg_alloc:
  1435. __wa_xfer_abort(xfer);
  1436. error_complete:
  1437. seg->status = WA_SEG_ERROR;
  1438. xfer->segs_done++;
  1439. rpipe_ready = rpipe_avail_inc(rpipe);
  1440. done = __wa_xfer_is_done(xfer);
  1441. /*
  1442. * queue work item to clear STALL for control endpoints.
  1443. * Otherwise, let endpoint_reset take care of it.
  1444. */
  1445. if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
  1446. usb_endpoint_xfer_control(&xfer->ep->desc) &&
  1447. done) {
  1448. dev_info(dev, "Control EP stall. Queue delayed work.\n");
  1449. spin_lock_irq(&wa->xfer_list_lock);
  1450. /* move xfer from xfer_list to xfer_errored_list. */
  1451. list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
  1452. spin_unlock_irq(&wa->xfer_list_lock);
  1453. spin_unlock_irqrestore(&xfer->lock, flags);
  1454. queue_work(wusbd, &wa->xfer_error_work);
  1455. } else {
  1456. spin_unlock_irqrestore(&xfer->lock, flags);
  1457. if (done)
  1458. wa_xfer_completion(xfer);
  1459. if (rpipe_ready)
  1460. wa_xfer_delayed_run(rpipe);
  1461. }
  1462. return;
  1463. error_bad_seg:
  1464. spin_unlock_irqrestore(&xfer->lock, flags);
  1465. wa_urb_dequeue(wa, xfer->urb);
  1466. if (printk_ratelimit())
  1467. dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
  1468. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  1469. dev_err(dev, "DTI: URB max acceptable errors "
  1470. "exceeded, resetting device\n");
  1471. wa_reset_all(wa);
  1472. }
  1473. return;
  1474. segment_aborted:
  1475. /* nothing to do, as the aborter did the completion */
  1476. spin_unlock_irqrestore(&xfer->lock, flags);
  1477. }
  1478. /*
  1479. * Callback for the IN data phase
  1480. *
  1481. * If successful transition state; otherwise, take a note of the
  1482. * error, mark this segment done and try completion.
  1483. *
  1484. * Note we don't access until we are sure that the transfer hasn't
  1485. * been cancelled (ECONNRESET, ENOENT), which could mean that
  1486. * seg->xfer could be already gone.
  1487. */
  1488. static void wa_buf_in_cb(struct urb *urb)
  1489. {
  1490. struct wa_seg *seg = urb->context;
  1491. struct wa_xfer *xfer = seg->xfer;
  1492. struct wahc *wa;
  1493. struct device *dev;
  1494. struct wa_rpipe *rpipe;
  1495. unsigned rpipe_ready;
  1496. unsigned long flags;
  1497. u8 done = 0;
  1498. /* free the sg if it was used. */
  1499. kfree(urb->sg);
  1500. urb->sg = NULL;
  1501. switch (urb->status) {
  1502. case 0:
  1503. spin_lock_irqsave(&xfer->lock, flags);
  1504. wa = xfer->wa;
  1505. dev = &wa->usb_iface->dev;
  1506. rpipe = xfer->ep->hcpriv;
  1507. dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
  1508. xfer, seg->index, (size_t)urb->actual_length);
  1509. seg->status = WA_SEG_DONE;
  1510. seg->result = urb->actual_length;
  1511. xfer->segs_done++;
  1512. rpipe_ready = rpipe_avail_inc(rpipe);
  1513. done = __wa_xfer_is_done(xfer);
  1514. spin_unlock_irqrestore(&xfer->lock, flags);
  1515. if (done)
  1516. wa_xfer_completion(xfer);
  1517. if (rpipe_ready)
  1518. wa_xfer_delayed_run(rpipe);
  1519. break;
  1520. case -ECONNRESET: /* URB unlinked; no need to do anything */
  1521. case -ENOENT: /* as it was done by the who unlinked us */
  1522. break;
  1523. default: /* Other errors ... */
  1524. spin_lock_irqsave(&xfer->lock, flags);
  1525. wa = xfer->wa;
  1526. dev = &wa->usb_iface->dev;
  1527. rpipe = xfer->ep->hcpriv;
  1528. if (printk_ratelimit())
  1529. dev_err(dev, "xfer %p#%u: data in error %d\n",
  1530. xfer, seg->index, urb->status);
  1531. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  1532. EDC_ERROR_TIMEFRAME)){
  1533. dev_err(dev, "DTO: URB max acceptable errors "
  1534. "exceeded, resetting device\n");
  1535. wa_reset_all(wa);
  1536. }
  1537. seg->status = WA_SEG_ERROR;
  1538. seg->result = urb->status;
  1539. xfer->segs_done++;
  1540. rpipe_ready = rpipe_avail_inc(rpipe);
  1541. __wa_xfer_abort(xfer);
  1542. done = __wa_xfer_is_done(xfer);
  1543. spin_unlock_irqrestore(&xfer->lock, flags);
  1544. if (done)
  1545. wa_xfer_completion(xfer);
  1546. if (rpipe_ready)
  1547. wa_xfer_delayed_run(rpipe);
  1548. }
  1549. }
  1550. /*
  1551. * Handle an incoming transfer result buffer
  1552. *
  1553. * Given a transfer result buffer, it completes the transfer (possibly
  1554. * scheduling and buffer in read) and then resubmits the DTI URB for a
  1555. * new transfer result read.
  1556. *
  1557. *
  1558. * The xfer_result DTI URB state machine
  1559. *
  1560. * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
  1561. *
  1562. * We start in OFF mode, the first xfer_result notification [through
  1563. * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
  1564. * read.
  1565. *
  1566. * We receive a buffer -- if it is not a xfer_result, we complain and
  1567. * repost the DTI-URB. If it is a xfer_result then do the xfer seg
  1568. * request accounting. If it is an IN segment, we move to RBI and post
  1569. * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
  1570. * repost the DTI-URB and move to RXR state. if there was no IN
  1571. * segment, it will repost the DTI-URB.
  1572. *
  1573. * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
  1574. * errors) in the URBs.
  1575. */
  1576. static void wa_dti_cb(struct urb *urb)
  1577. {
  1578. int result;
  1579. struct wahc *wa = urb->context;
  1580. struct device *dev = &wa->usb_iface->dev;
  1581. struct wa_xfer_result *xfer_result;
  1582. u32 xfer_id;
  1583. struct wa_xfer *xfer;
  1584. u8 usb_status;
  1585. BUG_ON(wa->dti_urb != urb);
  1586. switch (wa->dti_urb->status) {
  1587. case 0:
  1588. /* We have a xfer result buffer; check it */
  1589. dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
  1590. urb->actual_length, urb->transfer_buffer);
  1591. if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
  1592. dev_err(dev, "DTI Error: xfer result--bad size "
  1593. "xfer result (%d bytes vs %zu needed)\n",
  1594. urb->actual_length, sizeof(*xfer_result));
  1595. break;
  1596. }
  1597. xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
  1598. if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
  1599. dev_err(dev, "DTI Error: xfer result--"
  1600. "bad header length %u\n",
  1601. xfer_result->hdr.bLength);
  1602. break;
  1603. }
  1604. if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
  1605. dev_err(dev, "DTI Error: xfer result--"
  1606. "bad header type 0x%02x\n",
  1607. xfer_result->hdr.bNotifyType);
  1608. break;
  1609. }
  1610. usb_status = xfer_result->bTransferStatus & 0x3f;
  1611. if (usb_status == WA_XFER_STATUS_NOT_FOUND)
  1612. /* taken care of already */
  1613. break;
  1614. xfer_id = xfer_result->dwTransferID;
  1615. xfer = wa_xfer_get_by_id(wa, xfer_id);
  1616. if (xfer == NULL) {
  1617. /* FIXME: transaction might have been cancelled */
  1618. dev_err(dev, "DTI Error: xfer result--"
  1619. "unknown xfer 0x%08x (status 0x%02x)\n",
  1620. xfer_id, usb_status);
  1621. break;
  1622. }
  1623. wa_xfer_result_chew(wa, xfer, xfer_result);
  1624. wa_xfer_put(xfer);
  1625. break;
  1626. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  1627. case -ESHUTDOWN: /* going away! */
  1628. dev_dbg(dev, "DTI: going down! %d\n", urb->status);
  1629. goto out;
  1630. default:
  1631. /* Unknown error */
  1632. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
  1633. EDC_ERROR_TIMEFRAME)) {
  1634. dev_err(dev, "DTI: URB max acceptable errors "
  1635. "exceeded, resetting device\n");
  1636. wa_reset_all(wa);
  1637. goto out;
  1638. }
  1639. if (printk_ratelimit())
  1640. dev_err(dev, "DTI: URB error %d\n", urb->status);
  1641. break;
  1642. }
  1643. /* Resubmit the DTI URB */
  1644. result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
  1645. if (result < 0) {
  1646. dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
  1647. "resetting\n", result);
  1648. wa_reset_all(wa);
  1649. }
  1650. out:
  1651. return;
  1652. }
  1653. /*
  1654. * Transfer complete notification
  1655. *
  1656. * Called from the notif.c code. We get a notification on EP2 saying
  1657. * that some endpoint has some transfer result data available. We are
  1658. * about to read it.
  1659. *
  1660. * To speed up things, we always have a URB reading the DTI URB; we
  1661. * don't really set it up and start it until the first xfer complete
  1662. * notification arrives, which is what we do here.
  1663. *
  1664. * Follow up in wa_dti_cb(), as that's where the whole state
  1665. * machine starts.
  1666. *
  1667. * So here we just initialize the DTI URB for reading transfer result
  1668. * notifications and also the buffer-in URB, for reading buffers. Then
  1669. * we just submit the DTI URB.
  1670. *
  1671. * @wa shall be referenced
  1672. */
  1673. void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
  1674. {
  1675. int result;
  1676. struct device *dev = &wa->usb_iface->dev;
  1677. struct wa_notif_xfer *notif_xfer;
  1678. const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
  1679. notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
  1680. BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
  1681. if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
  1682. /* FIXME: hardcoded limitation, adapt */
  1683. dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
  1684. notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
  1685. goto error;
  1686. }
  1687. if (wa->dti_urb != NULL) /* DTI URB already started */
  1688. goto out;
  1689. wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
  1690. if (wa->dti_urb == NULL) {
  1691. dev_err(dev, "Can't allocate DTI URB\n");
  1692. goto error_dti_urb_alloc;
  1693. }
  1694. usb_fill_bulk_urb(
  1695. wa->dti_urb, wa->usb_dev,
  1696. usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
  1697. wa->dti_buf, wa->dti_buf_size,
  1698. wa_dti_cb, wa);
  1699. wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1700. if (wa->buf_in_urb == NULL) {
  1701. dev_err(dev, "Can't allocate BUF-IN URB\n");
  1702. goto error_buf_in_urb_alloc;
  1703. }
  1704. usb_fill_bulk_urb(
  1705. wa->buf_in_urb, wa->usb_dev,
  1706. usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
  1707. NULL, 0, wa_buf_in_cb, wa);
  1708. result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
  1709. if (result < 0) {
  1710. dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
  1711. "resetting\n", result);
  1712. goto error_dti_urb_submit;
  1713. }
  1714. out:
  1715. return;
  1716. error_dti_urb_submit:
  1717. usb_put_urb(wa->buf_in_urb);
  1718. error_buf_in_urb_alloc:
  1719. usb_put_urb(wa->dti_urb);
  1720. wa->dti_urb = NULL;
  1721. error_dti_urb_alloc:
  1722. error:
  1723. wa_reset_all(wa);
  1724. }