wa-xfer.c 46 KB

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