wa-xfer.c 53 KB

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