wa-xfer.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. * WUSB Wire Adapter
  3. * Data transfer and URB enqueing
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * How transfers work: get a buffer, break it up in segments (segment
  24. * size is a multiple of the maxpacket size). For each segment issue a
  25. * segment request (struct wa_xfer_*), then send the data buffer if
  26. * out or nothing if in (all over the DTO endpoint).
  27. *
  28. * For each submitted segment request, a notification will come over
  29. * the NEP endpoint and a transfer result (struct xfer_result) will
  30. * arrive in the DTI URB. Read it, get the xfer ID, see if there is
  31. * data coming (inbound transfer), schedule a read and handle it.
  32. *
  33. * Sounds simple, it is a pain to implement.
  34. *
  35. *
  36. * ENTRY POINTS
  37. *
  38. * FIXME
  39. *
  40. * LIFE CYCLE / STATE DIAGRAM
  41. *
  42. * FIXME
  43. *
  44. * THIS CODE IS DISGUSTING
  45. *
  46. * Warned you are; it's my second try and still not happy with it.
  47. *
  48. * NOTES:
  49. *
  50. * - No iso
  51. *
  52. * - Supports DMA xfers, control, bulk and maybe interrupt
  53. *
  54. * - Does not recycle unused rpipes
  55. *
  56. * An rpipe is assigned to an endpoint the first time it is used,
  57. * and then it's there, assigned, until the endpoint is disabled
  58. * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the
  59. * rpipe to the endpoint is done under the wa->rpipe_sem semaphore
  60. * (should be a mutex).
  61. *
  62. * Two methods it could be done:
  63. *
  64. * (a) set up a timer every time an rpipe's use count drops to 1
  65. * (which means unused) or when a transfer ends. Reset the
  66. * timer when a xfer is queued. If the timer expires, release
  67. * the rpipe [see rpipe_ep_disable()].
  68. *
  69. * (b) when looking for free rpipes to attach [rpipe_get_by_ep()],
  70. * when none are found go over the list, check their endpoint
  71. * and their activity record (if no last-xfer-done-ts in the
  72. * last x seconds) take it
  73. *
  74. * However, due to the fact that we have a set of limited
  75. * resources (max-segments-at-the-same-time per xfer,
  76. * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end
  77. * we are going to have to rebuild all this based on an scheduler,
  78. * to where we have a list of transactions to do and based on the
  79. * availability of the different required components (blocks,
  80. * rpipes, segment slots, etc), we go scheduling them. Painful.
  81. */
  82. #include <linux/init.h>
  83. #include <linux/spinlock.h>
  84. #include <linux/slab.h>
  85. #include <linux/hash.h>
  86. #include <linux/ratelimit.h>
  87. #include "wa-hc.h"
  88. #include "wusbhc.h"
  89. enum {
  90. WA_SEGS_MAX = 255,
  91. };
  92. enum wa_seg_status {
  93. WA_SEG_NOTREADY,
  94. WA_SEG_READY,
  95. WA_SEG_DELAYED,
  96. WA_SEG_SUBMITTED,
  97. WA_SEG_PENDING,
  98. WA_SEG_DTI_PENDING,
  99. WA_SEG_DONE,
  100. WA_SEG_ERROR,
  101. WA_SEG_ABORTED,
  102. };
  103. static void wa_xfer_delayed_run(struct wa_rpipe *);
  104. /*
  105. * Life cycle governed by 'struct urb' (the refcount of the struct is
  106. * that of the 'struct urb' and usb_free_urb() would free the whole
  107. * struct).
  108. */
  109. struct wa_seg {
  110. struct urb urb;
  111. struct urb *dto_urb; /* for data output? */
  112. struct list_head list_node; /* for rpipe->req_list */
  113. struct wa_xfer *xfer; /* out xfer */
  114. u8 index; /* which segment we are */
  115. enum wa_seg_status status;
  116. ssize_t result; /* bytes xfered or error */
  117. struct wa_xfer_hdr xfer_hdr;
  118. u8 xfer_extra[]; /* xtra space for xfer_hdr_ctl */
  119. };
  120. static void wa_seg_init(struct wa_seg *seg)
  121. {
  122. /* usb_init_urb() repeats a lot of work, so we do it here */
  123. kref_init(&seg->urb.kref);
  124. }
  125. /*
  126. * Protected by xfer->lock
  127. *
  128. */
  129. struct wa_xfer {
  130. struct kref refcnt;
  131. struct list_head list_node;
  132. spinlock_t lock;
  133. u32 id;
  134. struct wahc *wa; /* Wire adapter we are plugged to */
  135. struct usb_host_endpoint *ep;
  136. struct urb *urb; /* URB we are transferring for */
  137. struct wa_seg **seg; /* transfer segments */
  138. u8 segs, segs_submitted, segs_done;
  139. unsigned is_inbound:1;
  140. unsigned is_dma:1;
  141. size_t seg_size;
  142. int result;
  143. gfp_t gfp; /* allocation mask */
  144. struct wusb_dev *wusb_dev; /* for activity timestamps */
  145. };
  146. static inline void wa_xfer_init(struct wa_xfer *xfer)
  147. {
  148. kref_init(&xfer->refcnt);
  149. INIT_LIST_HEAD(&xfer->list_node);
  150. spin_lock_init(&xfer->lock);
  151. }
  152. /*
  153. * Destroy a transfer structure
  154. *
  155. * Note that the xfer->seg[index] thingies follow the URB life cycle,
  156. * so we need to put them, not free them.
  157. */
  158. static void wa_xfer_destroy(struct kref *_xfer)
  159. {
  160. struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
  161. if (xfer->seg) {
  162. unsigned cnt;
  163. for (cnt = 0; cnt < xfer->segs; cnt++) {
  164. if (xfer->is_inbound)
  165. usb_put_urb(xfer->seg[cnt]->dto_urb);
  166. usb_put_urb(&xfer->seg[cnt]->urb);
  167. }
  168. }
  169. kfree(xfer);
  170. }
  171. static void wa_xfer_get(struct wa_xfer *xfer)
  172. {
  173. kref_get(&xfer->refcnt);
  174. }
  175. static void wa_xfer_put(struct wa_xfer *xfer)
  176. {
  177. kref_put(&xfer->refcnt, wa_xfer_destroy);
  178. }
  179. /*
  180. * xfer is referenced
  181. *
  182. * xfer->lock has to be unlocked
  183. *
  184. * We take xfer->lock for setting the result; this is a barrier
  185. * against drivers/usb/core/hcd.c:unlink1() being called after we call
  186. * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
  187. * reference to the transfer.
  188. */
  189. static void wa_xfer_giveback(struct wa_xfer *xfer)
  190. {
  191. unsigned long flags;
  192. spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
  193. list_del_init(&xfer->list_node);
  194. spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
  195. /* FIXME: segmentation broken -- kills DWA */
  196. wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
  197. wa_put(xfer->wa);
  198. wa_xfer_put(xfer);
  199. }
  200. /*
  201. * xfer is referenced
  202. *
  203. * xfer->lock has to be unlocked
  204. */
  205. static void wa_xfer_completion(struct wa_xfer *xfer)
  206. {
  207. if (xfer->wusb_dev)
  208. wusb_dev_put(xfer->wusb_dev);
  209. rpipe_put(xfer->ep->hcpriv);
  210. wa_xfer_giveback(xfer);
  211. }
  212. /*
  213. * If transfer is done, wrap it up and return true
  214. *
  215. * xfer->lock has to be locked
  216. */
  217. static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
  218. {
  219. struct device *dev = &xfer->wa->usb_iface->dev;
  220. unsigned result, cnt;
  221. struct wa_seg *seg;
  222. struct urb *urb = xfer->urb;
  223. unsigned found_short = 0;
  224. result = xfer->segs_done == xfer->segs_submitted;
  225. if (result == 0)
  226. goto out;
  227. urb->actual_length = 0;
  228. for (cnt = 0; cnt < xfer->segs; cnt++) {
  229. seg = xfer->seg[cnt];
  230. switch (seg->status) {
  231. case WA_SEG_DONE:
  232. if (found_short && seg->result > 0) {
  233. dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n",
  234. xfer, cnt, seg->result);
  235. urb->status = -EINVAL;
  236. goto out;
  237. }
  238. urb->actual_length += seg->result;
  239. if (seg->result < xfer->seg_size
  240. && cnt != xfer->segs-1)
  241. found_short = 1;
  242. dev_dbg(dev, "xfer %p#%u: DONE short %d "
  243. "result %zu urb->actual_length %d\n",
  244. xfer, seg->index, found_short, seg->result,
  245. urb->actual_length);
  246. break;
  247. case WA_SEG_ERROR:
  248. xfer->result = seg->result;
  249. dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n",
  250. xfer, seg->index, seg->result);
  251. goto out;
  252. case WA_SEG_ABORTED:
  253. dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n",
  254. xfer, seg->index, urb->status);
  255. xfer->result = urb->status;
  256. goto out;
  257. default:
  258. dev_warn(dev, "xfer %p#%u: is_done bad state %d\n",
  259. xfer, cnt, seg->status);
  260. xfer->result = -EINVAL;
  261. goto out;
  262. }
  263. }
  264. xfer->result = 0;
  265. out:
  266. return result;
  267. }
  268. /*
  269. * Initialize a transfer's ID
  270. *
  271. * We need to use a sequential number; if we use the pointer or the
  272. * hash of the pointer, it can repeat over sequential transfers and
  273. * then it will confuse the HWA....wonder why in hell they put a 32
  274. * bit handle in there then.
  275. */
  276. static void wa_xfer_id_init(struct wa_xfer *xfer)
  277. {
  278. xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
  279. }
  280. /*
  281. * Return the xfer's ID associated with xfer
  282. *
  283. * Need to generate a
  284. */
  285. static u32 wa_xfer_id(struct wa_xfer *xfer)
  286. {
  287. return xfer->id;
  288. }
  289. /*
  290. * Search for a transfer list ID on the HCD's URB list
  291. *
  292. * For 32 bit architectures, we use the pointer itself; for 64 bits, a
  293. * 32-bit hash of the pointer.
  294. *
  295. * @returns NULL if not found.
  296. */
  297. static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
  298. {
  299. unsigned long flags;
  300. struct wa_xfer *xfer_itr;
  301. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  302. list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
  303. if (id == xfer_itr->id) {
  304. wa_xfer_get(xfer_itr);
  305. goto out;
  306. }
  307. }
  308. xfer_itr = NULL;
  309. out:
  310. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  311. return xfer_itr;
  312. }
  313. struct wa_xfer_abort_buffer {
  314. struct urb urb;
  315. struct wa_xfer_abort cmd;
  316. };
  317. static void __wa_xfer_abort_cb(struct urb *urb)
  318. {
  319. struct wa_xfer_abort_buffer *b = urb->context;
  320. usb_put_urb(&b->urb);
  321. }
  322. /*
  323. * Aborts an ongoing transaction
  324. *
  325. * Assumes the transfer is referenced and locked and in a submitted
  326. * state (mainly that there is an endpoint/rpipe assigned).
  327. *
  328. * The callback (see above) does nothing but freeing up the data by
  329. * putting the URB. Because the URB is allocated at the head of the
  330. * struct, the whole space we allocated is kfreed.
  331. *
  332. * We'll get an 'aborted transaction' xfer result on DTI, that'll
  333. * politely ignore because at this point the transaction has been
  334. * marked as aborted already.
  335. */
  336. static void __wa_xfer_abort(struct wa_xfer *xfer)
  337. {
  338. int result;
  339. struct device *dev = &xfer->wa->usb_iface->dev;
  340. struct wa_xfer_abort_buffer *b;
  341. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  342. b = kmalloc(sizeof(*b), GFP_ATOMIC);
  343. if (b == NULL)
  344. goto error_kmalloc;
  345. b->cmd.bLength = sizeof(b->cmd);
  346. b->cmd.bRequestType = WA_XFER_ABORT;
  347. b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
  348. b->cmd.dwTransferID = wa_xfer_id(xfer);
  349. usb_init_urb(&b->urb);
  350. usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
  351. usb_sndbulkpipe(xfer->wa->usb_dev,
  352. xfer->wa->dto_epd->bEndpointAddress),
  353. &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
  354. result = usb_submit_urb(&b->urb, GFP_ATOMIC);
  355. if (result < 0)
  356. goto error_submit;
  357. return; /* callback frees! */
  358. error_submit:
  359. if (printk_ratelimit())
  360. dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
  361. xfer, result);
  362. kfree(b);
  363. error_kmalloc:
  364. return;
  365. }
  366. /*
  367. *
  368. * @returns < 0 on error, transfer segment request size if ok
  369. */
  370. static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
  371. enum wa_xfer_type *pxfer_type)
  372. {
  373. ssize_t result;
  374. struct device *dev = &xfer->wa->usb_iface->dev;
  375. size_t maxpktsize;
  376. struct urb *urb = xfer->urb;
  377. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  378. switch (rpipe->descr.bmAttribute & 0x3) {
  379. case USB_ENDPOINT_XFER_CONTROL:
  380. *pxfer_type = WA_XFER_TYPE_CTL;
  381. result = sizeof(struct wa_xfer_ctl);
  382. break;
  383. case USB_ENDPOINT_XFER_INT:
  384. case USB_ENDPOINT_XFER_BULK:
  385. *pxfer_type = WA_XFER_TYPE_BI;
  386. result = sizeof(struct wa_xfer_bi);
  387. break;
  388. case USB_ENDPOINT_XFER_ISOC:
  389. dev_err(dev, "FIXME: ISOC not implemented\n");
  390. result = -ENOSYS;
  391. goto error;
  392. default:
  393. /* never happens */
  394. BUG();
  395. result = -EINVAL; /* shut gcc up */
  396. };
  397. xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
  398. xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
  399. xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
  400. * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
  401. /* Compute the segment size and make sure it is a multiple of
  402. * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
  403. * a check (FIXME) */
  404. maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
  405. if (xfer->seg_size < maxpktsize) {
  406. dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
  407. "%zu\n", xfer->seg_size, maxpktsize);
  408. result = -EINVAL;
  409. goto error;
  410. }
  411. xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
  412. xfer->segs = (urb->transfer_buffer_length + xfer->seg_size - 1)
  413. / xfer->seg_size;
  414. if (xfer->segs >= WA_SEGS_MAX) {
  415. dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
  416. (int)(urb->transfer_buffer_length / xfer->seg_size),
  417. WA_SEGS_MAX);
  418. result = -EINVAL;
  419. goto error;
  420. }
  421. if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
  422. xfer->segs = 1;
  423. error:
  424. return result;
  425. }
  426. /* Fill in the common request header and xfer-type specific data. */
  427. static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
  428. struct wa_xfer_hdr *xfer_hdr0,
  429. enum wa_xfer_type xfer_type,
  430. size_t xfer_hdr_size)
  431. {
  432. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  433. xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
  434. xfer_hdr0->bLength = xfer_hdr_size;
  435. xfer_hdr0->bRequestType = xfer_type;
  436. xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
  437. xfer_hdr0->dwTransferID = wa_xfer_id(xfer);
  438. xfer_hdr0->bTransferSegment = 0;
  439. switch (xfer_type) {
  440. case WA_XFER_TYPE_CTL: {
  441. struct wa_xfer_ctl *xfer_ctl =
  442. container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
  443. xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
  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 succeeded,
  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 successful 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. printk_ratelimited(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. printk_ratelimited(KERN_ERR "%s(): BUG? "
  1155. "Inconsistent WA status: 0x%02x\n",
  1156. __func__, real_status);
  1157. errno = -errno;
  1158. }
  1159. return errno;
  1160. }
  1161. /*
  1162. * Process a xfer result completion message
  1163. *
  1164. * inbound transfers: need to schedule a DTI read
  1165. *
  1166. * FIXME: this functio needs to be broken up in parts
  1167. */
  1168. static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer)
  1169. {
  1170. int result;
  1171. struct device *dev = &wa->usb_iface->dev;
  1172. unsigned long flags;
  1173. u8 seg_idx;
  1174. struct wa_seg *seg;
  1175. struct wa_rpipe *rpipe;
  1176. struct wa_xfer_result *xfer_result = wa->xfer_result;
  1177. u8 done = 0;
  1178. u8 usb_status;
  1179. unsigned rpipe_ready = 0;
  1180. spin_lock_irqsave(&xfer->lock, flags);
  1181. seg_idx = xfer_result->bTransferSegment & 0x7f;
  1182. if (unlikely(seg_idx >= xfer->segs))
  1183. goto error_bad_seg;
  1184. seg = xfer->seg[seg_idx];
  1185. rpipe = xfer->ep->hcpriv;
  1186. usb_status = xfer_result->bTransferStatus;
  1187. dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg %u)\n",
  1188. xfer, seg_idx, usb_status, seg->status);
  1189. if (seg->status == WA_SEG_ABORTED
  1190. || seg->status == WA_SEG_ERROR) /* already handled */
  1191. goto segment_aborted;
  1192. if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
  1193. seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
  1194. if (seg->status != WA_SEG_PENDING) {
  1195. if (printk_ratelimit())
  1196. dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
  1197. xfer, seg_idx, seg->status);
  1198. seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
  1199. }
  1200. if (usb_status & 0x80) {
  1201. seg->result = wa_xfer_status_to_errno(usb_status);
  1202. dev_err(dev, "DTI: xfer %p#%u failed (0x%02x)\n",
  1203. xfer, seg->index, usb_status);
  1204. goto error_complete;
  1205. }
  1206. /* FIXME: we ignore warnings, tally them for stats */
  1207. if (usb_status & 0x40) /* Warning?... */
  1208. usb_status = 0; /* ... pass */
  1209. if (xfer->is_inbound) { /* IN data phase: read to buffer */
  1210. seg->status = WA_SEG_DTI_PENDING;
  1211. BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
  1212. if (xfer->is_dma) {
  1213. wa->buf_in_urb->transfer_dma =
  1214. xfer->urb->transfer_dma
  1215. + seg_idx * xfer->seg_size;
  1216. wa->buf_in_urb->transfer_flags
  1217. |= URB_NO_TRANSFER_DMA_MAP;
  1218. } else {
  1219. wa->buf_in_urb->transfer_buffer =
  1220. xfer->urb->transfer_buffer
  1221. + seg_idx * xfer->seg_size;
  1222. wa->buf_in_urb->transfer_flags
  1223. &= ~URB_NO_TRANSFER_DMA_MAP;
  1224. }
  1225. wa->buf_in_urb->transfer_buffer_length =
  1226. le32_to_cpu(xfer_result->dwTransferLength);
  1227. wa->buf_in_urb->context = seg;
  1228. result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
  1229. if (result < 0)
  1230. goto error_submit_buf_in;
  1231. } else {
  1232. /* OUT data phase, complete it -- */
  1233. seg->status = WA_SEG_DONE;
  1234. seg->result = le32_to_cpu(xfer_result->dwTransferLength);
  1235. xfer->segs_done++;
  1236. rpipe_ready = rpipe_avail_inc(rpipe);
  1237. done = __wa_xfer_is_done(xfer);
  1238. }
  1239. spin_unlock_irqrestore(&xfer->lock, flags);
  1240. if (done)
  1241. wa_xfer_completion(xfer);
  1242. if (rpipe_ready)
  1243. wa_xfer_delayed_run(rpipe);
  1244. return;
  1245. error_submit_buf_in:
  1246. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  1247. dev_err(dev, "DTI: URB max acceptable errors "
  1248. "exceeded, resetting device\n");
  1249. wa_reset_all(wa);
  1250. }
  1251. if (printk_ratelimit())
  1252. dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
  1253. xfer, seg_idx, result);
  1254. seg->result = result;
  1255. error_complete:
  1256. seg->status = WA_SEG_ERROR;
  1257. xfer->segs_done++;
  1258. rpipe_ready = rpipe_avail_inc(rpipe);
  1259. __wa_xfer_abort(xfer);
  1260. done = __wa_xfer_is_done(xfer);
  1261. spin_unlock_irqrestore(&xfer->lock, flags);
  1262. if (done)
  1263. wa_xfer_completion(xfer);
  1264. if (rpipe_ready)
  1265. wa_xfer_delayed_run(rpipe);
  1266. return;
  1267. error_bad_seg:
  1268. spin_unlock_irqrestore(&xfer->lock, flags);
  1269. wa_urb_dequeue(wa, xfer->urb);
  1270. if (printk_ratelimit())
  1271. dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
  1272. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  1273. dev_err(dev, "DTI: URB max acceptable errors "
  1274. "exceeded, resetting device\n");
  1275. wa_reset_all(wa);
  1276. }
  1277. return;
  1278. segment_aborted:
  1279. /* nothing to do, as the aborter did the completion */
  1280. spin_unlock_irqrestore(&xfer->lock, flags);
  1281. }
  1282. /*
  1283. * Callback for the IN data phase
  1284. *
  1285. * If successful transition state; otherwise, take a note of the
  1286. * error, mark this segment done and try completion.
  1287. *
  1288. * Note we don't access until we are sure that the transfer hasn't
  1289. * been cancelled (ECONNRESET, ENOENT), which could mean that
  1290. * seg->xfer could be already gone.
  1291. */
  1292. static void wa_buf_in_cb(struct urb *urb)
  1293. {
  1294. struct wa_seg *seg = urb->context;
  1295. struct wa_xfer *xfer = seg->xfer;
  1296. struct wahc *wa;
  1297. struct device *dev;
  1298. struct wa_rpipe *rpipe;
  1299. unsigned rpipe_ready;
  1300. unsigned long flags;
  1301. u8 done = 0;
  1302. switch (urb->status) {
  1303. case 0:
  1304. spin_lock_irqsave(&xfer->lock, flags);
  1305. wa = xfer->wa;
  1306. dev = &wa->usb_iface->dev;
  1307. rpipe = xfer->ep->hcpriv;
  1308. dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
  1309. xfer, seg->index, (size_t)urb->actual_length);
  1310. seg->status = WA_SEG_DONE;
  1311. seg->result = urb->actual_length;
  1312. xfer->segs_done++;
  1313. rpipe_ready = rpipe_avail_inc(rpipe);
  1314. done = __wa_xfer_is_done(xfer);
  1315. spin_unlock_irqrestore(&xfer->lock, flags);
  1316. if (done)
  1317. wa_xfer_completion(xfer);
  1318. if (rpipe_ready)
  1319. wa_xfer_delayed_run(rpipe);
  1320. break;
  1321. case -ECONNRESET: /* URB unlinked; no need to do anything */
  1322. case -ENOENT: /* as it was done by the who unlinked us */
  1323. break;
  1324. default: /* Other errors ... */
  1325. spin_lock_irqsave(&xfer->lock, flags);
  1326. wa = xfer->wa;
  1327. dev = &wa->usb_iface->dev;
  1328. rpipe = xfer->ep->hcpriv;
  1329. if (printk_ratelimit())
  1330. dev_err(dev, "xfer %p#%u: data in error %d\n",
  1331. xfer, seg->index, urb->status);
  1332. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  1333. EDC_ERROR_TIMEFRAME)){
  1334. dev_err(dev, "DTO: URB max acceptable errors "
  1335. "exceeded, resetting device\n");
  1336. wa_reset_all(wa);
  1337. }
  1338. seg->status = WA_SEG_ERROR;
  1339. seg->result = urb->status;
  1340. xfer->segs_done++;
  1341. rpipe_ready = rpipe_avail_inc(rpipe);
  1342. __wa_xfer_abort(xfer);
  1343. done = __wa_xfer_is_done(xfer);
  1344. spin_unlock_irqrestore(&xfer->lock, flags);
  1345. if (done)
  1346. wa_xfer_completion(xfer);
  1347. if (rpipe_ready)
  1348. wa_xfer_delayed_run(rpipe);
  1349. }
  1350. }
  1351. /*
  1352. * Handle an incoming transfer result buffer
  1353. *
  1354. * Given a transfer result buffer, it completes the transfer (possibly
  1355. * scheduling and buffer in read) and then resubmits the DTI URB for a
  1356. * new transfer result read.
  1357. *
  1358. *
  1359. * The xfer_result DTI URB state machine
  1360. *
  1361. * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
  1362. *
  1363. * We start in OFF mode, the first xfer_result notification [through
  1364. * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
  1365. * read.
  1366. *
  1367. * We receive a buffer -- if it is not a xfer_result, we complain and
  1368. * repost the DTI-URB. If it is a xfer_result then do the xfer seg
  1369. * request accounting. If it is an IN segment, we move to RBI and post
  1370. * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
  1371. * repost the DTI-URB and move to RXR state. if there was no IN
  1372. * segment, it will repost the DTI-URB.
  1373. *
  1374. * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
  1375. * errors) in the URBs.
  1376. */
  1377. static void wa_xfer_result_cb(struct urb *urb)
  1378. {
  1379. int result;
  1380. struct wahc *wa = urb->context;
  1381. struct device *dev = &wa->usb_iface->dev;
  1382. struct wa_xfer_result *xfer_result;
  1383. u32 xfer_id;
  1384. struct wa_xfer *xfer;
  1385. u8 usb_status;
  1386. BUG_ON(wa->dti_urb != urb);
  1387. switch (wa->dti_urb->status) {
  1388. case 0:
  1389. /* We have a xfer result buffer; check it */
  1390. dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
  1391. urb->actual_length, urb->transfer_buffer);
  1392. if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
  1393. dev_err(dev, "DTI Error: xfer result--bad size "
  1394. "xfer result (%d bytes vs %zu needed)\n",
  1395. urb->actual_length, sizeof(*xfer_result));
  1396. break;
  1397. }
  1398. xfer_result = wa->xfer_result;
  1399. if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
  1400. dev_err(dev, "DTI Error: xfer result--"
  1401. "bad header length %u\n",
  1402. xfer_result->hdr.bLength);
  1403. break;
  1404. }
  1405. if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
  1406. dev_err(dev, "DTI Error: xfer result--"
  1407. "bad header type 0x%02x\n",
  1408. xfer_result->hdr.bNotifyType);
  1409. break;
  1410. }
  1411. usb_status = xfer_result->bTransferStatus & 0x3f;
  1412. if (usb_status == WA_XFER_STATUS_ABORTED
  1413. || usb_status == WA_XFER_STATUS_NOT_FOUND)
  1414. /* taken care of already */
  1415. break;
  1416. xfer_id = xfer_result->dwTransferID;
  1417. xfer = wa_xfer_get_by_id(wa, xfer_id);
  1418. if (xfer == NULL) {
  1419. /* FIXME: transaction might have been cancelled */
  1420. dev_err(dev, "DTI Error: xfer result--"
  1421. "unknown xfer 0x%08x (status 0x%02x)\n",
  1422. xfer_id, usb_status);
  1423. break;
  1424. }
  1425. wa_xfer_result_chew(wa, xfer);
  1426. wa_xfer_put(xfer);
  1427. break;
  1428. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  1429. case -ESHUTDOWN: /* going away! */
  1430. dev_dbg(dev, "DTI: going down! %d\n", urb->status);
  1431. goto out;
  1432. default:
  1433. /* Unknown error */
  1434. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
  1435. EDC_ERROR_TIMEFRAME)) {
  1436. dev_err(dev, "DTI: URB max acceptable errors "
  1437. "exceeded, resetting device\n");
  1438. wa_reset_all(wa);
  1439. goto out;
  1440. }
  1441. if (printk_ratelimit())
  1442. dev_err(dev, "DTI: URB error %d\n", urb->status);
  1443. break;
  1444. }
  1445. /* Resubmit the DTI URB */
  1446. result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
  1447. if (result < 0) {
  1448. dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
  1449. "resetting\n", result);
  1450. wa_reset_all(wa);
  1451. }
  1452. out:
  1453. return;
  1454. }
  1455. /*
  1456. * Transfer complete notification
  1457. *
  1458. * Called from the notif.c code. We get a notification on EP2 saying
  1459. * that some endpoint has some transfer result data available. We are
  1460. * about to read it.
  1461. *
  1462. * To speed up things, we always have a URB reading the DTI URB; we
  1463. * don't really set it up and start it until the first xfer complete
  1464. * notification arrives, which is what we do here.
  1465. *
  1466. * Follow up in wa_xfer_result_cb(), as that's where the whole state
  1467. * machine starts.
  1468. *
  1469. * So here we just initialize the DTI URB for reading transfer result
  1470. * notifications and also the buffer-in URB, for reading buffers. Then
  1471. * we just submit the DTI URB.
  1472. *
  1473. * @wa shall be referenced
  1474. */
  1475. void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
  1476. {
  1477. int result;
  1478. struct device *dev = &wa->usb_iface->dev;
  1479. struct wa_notif_xfer *notif_xfer;
  1480. const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
  1481. notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
  1482. BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
  1483. if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
  1484. /* FIXME: hardcoded limitation, adapt */
  1485. dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
  1486. notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
  1487. goto error;
  1488. }
  1489. if (wa->dti_urb != NULL) /* DTI URB already started */
  1490. goto out;
  1491. wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
  1492. if (wa->dti_urb == NULL) {
  1493. dev_err(dev, "Can't allocate DTI URB\n");
  1494. goto error_dti_urb_alloc;
  1495. }
  1496. usb_fill_bulk_urb(
  1497. wa->dti_urb, wa->usb_dev,
  1498. usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
  1499. wa->xfer_result, wa->xfer_result_size,
  1500. wa_xfer_result_cb, wa);
  1501. wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1502. if (wa->buf_in_urb == NULL) {
  1503. dev_err(dev, "Can't allocate BUF-IN URB\n");
  1504. goto error_buf_in_urb_alloc;
  1505. }
  1506. usb_fill_bulk_urb(
  1507. wa->buf_in_urb, wa->usb_dev,
  1508. usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
  1509. NULL, 0, wa_buf_in_cb, wa);
  1510. result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
  1511. if (result < 0) {
  1512. dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
  1513. "resetting\n", result);
  1514. goto error_dti_urb_submit;
  1515. }
  1516. out:
  1517. return;
  1518. error_dti_urb_submit:
  1519. usb_put_urb(wa->buf_in_urb);
  1520. error_buf_in_urb_alloc:
  1521. usb_put_urb(wa->dti_urb);
  1522. wa->dti_urb = NULL;
  1523. error_dti_urb_alloc:
  1524. error:
  1525. wa_reset_all(wa);
  1526. }