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