wa-xfer.c 46 KB

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