wa-xfer.c 51 KB

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