wa-xfer.c 46 KB

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