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