wa-xfer.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941
  1. /*
  2. * WUSB Wire Adapter
  3. * Data transfer and URB enqueing
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * How transfers work: get a buffer, break it up in segments (segment
  24. * size is a multiple of the maxpacket size). For each segment issue a
  25. * segment request (struct wa_xfer_*), then send the data buffer if
  26. * out or nothing if in (all over the DTO endpoint).
  27. *
  28. * For each submitted segment request, a notification will come over
  29. * the NEP endpoint and a transfer result (struct xfer_result) will
  30. * arrive in the DTI URB. Read it, get the xfer ID, see if there is
  31. * data coming (inbound transfer), schedule a read and handle it.
  32. *
  33. * Sounds simple, it is a pain to implement.
  34. *
  35. *
  36. * ENTRY POINTS
  37. *
  38. * FIXME
  39. *
  40. * LIFE CYCLE / STATE DIAGRAM
  41. *
  42. * FIXME
  43. *
  44. * THIS CODE IS DISGUSTING
  45. *
  46. * Warned you are; it's my second try and still not happy with it.
  47. *
  48. * NOTES:
  49. *
  50. * - No iso
  51. *
  52. * - Supports DMA xfers, control, bulk and maybe interrupt
  53. *
  54. * - Does not recycle unused rpipes
  55. *
  56. * An rpipe is assigned to an endpoint the first time it is used,
  57. * and then it's there, assigned, until the endpoint is disabled
  58. * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the
  59. * rpipe to the endpoint is done under the wa->rpipe_sem semaphore
  60. * (should be a mutex).
  61. *
  62. * Two methods it could be done:
  63. *
  64. * (a) set up a timer every time an rpipe's use count drops to 1
  65. * (which means unused) or when a transfer ends. Reset the
  66. * timer when a xfer is queued. If the timer expires, release
  67. * the rpipe [see rpipe_ep_disable()].
  68. *
  69. * (b) when looking for free rpipes to attach [rpipe_get_by_ep()],
  70. * when none are found go over the list, check their endpoint
  71. * and their activity record (if no last-xfer-done-ts in the
  72. * last x seconds) take it
  73. *
  74. * However, due to the fact that we have a set of limited
  75. * resources (max-segments-at-the-same-time per xfer,
  76. * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end
  77. * we are going to have to rebuild all this based on an scheduler,
  78. * to where we have a list of transactions to do and based on the
  79. * availability of the different required components (blocks,
  80. * rpipes, segment slots, etc), we go scheduling them. Painful.
  81. */
  82. #include <linux/spinlock.h>
  83. #include <linux/slab.h>
  84. #include <linux/hash.h>
  85. #include <linux/ratelimit.h>
  86. #include <linux/export.h>
  87. #include <linux/scatterlist.h>
  88. #include "wa-hc.h"
  89. #include "wusbhc.h"
  90. enum {
  91. /* [WUSB] section 8.3.3 allocates 7 bits for the segment index. */
  92. WA_SEGS_MAX = 128,
  93. };
  94. enum wa_seg_status {
  95. WA_SEG_NOTREADY,
  96. WA_SEG_READY,
  97. WA_SEG_DELAYED,
  98. WA_SEG_SUBMITTED,
  99. WA_SEG_PENDING,
  100. WA_SEG_DTI_PENDING,
  101. WA_SEG_DONE,
  102. WA_SEG_ERROR,
  103. WA_SEG_ABORTED,
  104. };
  105. static void wa_xfer_delayed_run(struct wa_rpipe *);
  106. static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting);
  107. /*
  108. * Life cycle governed by 'struct urb' (the refcount of the struct is
  109. * that of the 'struct urb' and usb_free_urb() would free the whole
  110. * struct).
  111. */
  112. struct wa_seg {
  113. struct urb tr_urb; /* transfer request urb. */
  114. struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */
  115. struct urb *dto_urb; /* for data output. */
  116. struct list_head list_node; /* for rpipe->req_list */
  117. struct wa_xfer *xfer; /* out xfer */
  118. u8 index; /* which segment we are */
  119. int isoc_frame_count; /* number of isoc frames in this segment. */
  120. int isoc_frame_offset; /* starting frame offset in the xfer URB. */
  121. /* Isoc frame that the current transfer buffer corresponds to. */
  122. int isoc_frame_index;
  123. int isoc_size; /* size of all isoc frames sent by this seg. */
  124. enum wa_seg_status status;
  125. ssize_t result; /* bytes xfered or error */
  126. struct wa_xfer_hdr xfer_hdr;
  127. };
  128. static inline void wa_seg_init(struct wa_seg *seg)
  129. {
  130. usb_init_urb(&seg->tr_urb);
  131. /* set the remaining memory to 0. */
  132. memset(((void *)seg) + sizeof(seg->tr_urb), 0,
  133. sizeof(*seg) - sizeof(seg->tr_urb));
  134. }
  135. /*
  136. * Protected by xfer->lock
  137. *
  138. */
  139. struct wa_xfer {
  140. struct kref refcnt;
  141. struct list_head list_node;
  142. spinlock_t lock;
  143. u32 id;
  144. struct wahc *wa; /* Wire adapter we are plugged to */
  145. struct usb_host_endpoint *ep;
  146. struct urb *urb; /* URB we are transferring for */
  147. struct wa_seg **seg; /* transfer segments */
  148. u8 segs, segs_submitted, segs_done;
  149. unsigned is_inbound:1;
  150. unsigned is_dma:1;
  151. size_t seg_size;
  152. int result;
  153. gfp_t gfp; /* allocation mask */
  154. struct wusb_dev *wusb_dev; /* for activity timestamps */
  155. };
  156. static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
  157. struct wa_seg *seg, int curr_iso_frame);
  158. static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
  159. int starting_index, enum wa_seg_status status);
  160. static inline void wa_xfer_init(struct wa_xfer *xfer)
  161. {
  162. kref_init(&xfer->refcnt);
  163. INIT_LIST_HEAD(&xfer->list_node);
  164. spin_lock_init(&xfer->lock);
  165. }
  166. /*
  167. * Destroy a transfer structure
  168. *
  169. * Note that freeing xfer->seg[cnt]->tr_urb will free the containing
  170. * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
  171. */
  172. static void wa_xfer_destroy(struct kref *_xfer)
  173. {
  174. struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
  175. if (xfer->seg) {
  176. unsigned cnt;
  177. for (cnt = 0; cnt < xfer->segs; cnt++) {
  178. struct wa_seg *seg = xfer->seg[cnt];
  179. if (seg) {
  180. usb_free_urb(seg->isoc_pack_desc_urb);
  181. if (seg->dto_urb) {
  182. kfree(seg->dto_urb->sg);
  183. usb_free_urb(seg->dto_urb);
  184. }
  185. usb_free_urb(&seg->tr_urb);
  186. }
  187. }
  188. kfree(xfer->seg);
  189. }
  190. kfree(xfer);
  191. }
  192. static void wa_xfer_get(struct wa_xfer *xfer)
  193. {
  194. kref_get(&xfer->refcnt);
  195. }
  196. static void wa_xfer_put(struct wa_xfer *xfer)
  197. {
  198. kref_put(&xfer->refcnt, wa_xfer_destroy);
  199. }
  200. /*
  201. * Try to get exclusive access to the DTO endpoint resource. Return true
  202. * if successful.
  203. */
  204. static inline int __wa_dto_try_get(struct wahc *wa)
  205. {
  206. return (test_and_set_bit(0, &wa->dto_in_use) == 0);
  207. }
  208. /* Release the DTO endpoint resource. */
  209. static inline void __wa_dto_put(struct wahc *wa)
  210. {
  211. clear_bit_unlock(0, &wa->dto_in_use);
  212. }
  213. /* Service RPIPEs that are waiting on the DTO resource. */
  214. static void wa_check_for_delayed_rpipes(struct wahc *wa)
  215. {
  216. unsigned long flags;
  217. int dto_waiting = 0;
  218. struct wa_rpipe *rpipe;
  219. spin_lock_irqsave(&wa->rpipe_lock, flags);
  220. while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) {
  221. rpipe = list_first_entry(&wa->rpipe_delayed_list,
  222. struct wa_rpipe, list_node);
  223. __wa_xfer_delayed_run(rpipe, &dto_waiting);
  224. /* remove this RPIPE from the list if it is not waiting. */
  225. if (!dto_waiting) {
  226. pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n",
  227. __func__,
  228. le16_to_cpu(rpipe->descr.wRPipeIndex));
  229. list_del_init(&rpipe->list_node);
  230. }
  231. }
  232. spin_unlock_irqrestore(&wa->rpipe_lock, flags);
  233. }
  234. /* add this RPIPE to the end of the delayed RPIPE list. */
  235. static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe)
  236. {
  237. unsigned long flags;
  238. spin_lock_irqsave(&wa->rpipe_lock, flags);
  239. /* add rpipe to the list if it is not already on it. */
  240. if (list_empty(&rpipe->list_node)) {
  241. pr_debug("%s: adding RPIPE %d to the delayed list.\n",
  242. __func__, le16_to_cpu(rpipe->descr.wRPipeIndex));
  243. list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list);
  244. }
  245. spin_unlock_irqrestore(&wa->rpipe_lock, flags);
  246. }
  247. /*
  248. * xfer is referenced
  249. *
  250. * xfer->lock has to be unlocked
  251. *
  252. * We take xfer->lock for setting the result; this is a barrier
  253. * against drivers/usb/core/hcd.c:unlink1() being called after we call
  254. * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
  255. * reference to the transfer.
  256. */
  257. static void wa_xfer_giveback(struct wa_xfer *xfer)
  258. {
  259. unsigned long flags;
  260. spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
  261. list_del_init(&xfer->list_node);
  262. usb_hcd_unlink_urb_from_ep(&(xfer->wa->wusb->usb_hcd), xfer->urb);
  263. spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
  264. /* FIXME: segmentation broken -- kills DWA */
  265. wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
  266. wa_put(xfer->wa);
  267. wa_xfer_put(xfer);
  268. }
  269. /*
  270. * xfer is referenced
  271. *
  272. * xfer->lock has to be unlocked
  273. */
  274. static void wa_xfer_completion(struct wa_xfer *xfer)
  275. {
  276. if (xfer->wusb_dev)
  277. wusb_dev_put(xfer->wusb_dev);
  278. rpipe_put(xfer->ep->hcpriv);
  279. wa_xfer_giveback(xfer);
  280. }
  281. /*
  282. * Initialize a transfer's ID
  283. *
  284. * We need to use a sequential number; if we use the pointer or the
  285. * hash of the pointer, it can repeat over sequential transfers and
  286. * then it will confuse the HWA....wonder why in hell they put a 32
  287. * bit handle in there then.
  288. */
  289. static void wa_xfer_id_init(struct wa_xfer *xfer)
  290. {
  291. xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
  292. }
  293. /* Return the xfer's ID. */
  294. static inline u32 wa_xfer_id(struct wa_xfer *xfer)
  295. {
  296. return xfer->id;
  297. }
  298. /* Return the xfer's ID in transport format (little endian). */
  299. static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
  300. {
  301. return cpu_to_le32(xfer->id);
  302. }
  303. /*
  304. * If transfer is done, wrap it up and return true
  305. *
  306. * xfer->lock has to be locked
  307. */
  308. static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
  309. {
  310. struct device *dev = &xfer->wa->usb_iface->dev;
  311. unsigned result, cnt;
  312. struct wa_seg *seg;
  313. struct urb *urb = xfer->urb;
  314. unsigned found_short = 0;
  315. result = xfer->segs_done == xfer->segs_submitted;
  316. if (result == 0)
  317. goto out;
  318. urb->actual_length = 0;
  319. for (cnt = 0; cnt < xfer->segs; cnt++) {
  320. seg = xfer->seg[cnt];
  321. switch (seg->status) {
  322. case WA_SEG_DONE:
  323. if (found_short && seg->result > 0) {
  324. dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n",
  325. xfer, wa_xfer_id(xfer), cnt,
  326. seg->result);
  327. urb->status = -EINVAL;
  328. goto out;
  329. }
  330. urb->actual_length += seg->result;
  331. if (!(usb_pipeisoc(xfer->urb->pipe))
  332. && seg->result < xfer->seg_size
  333. && cnt != xfer->segs-1)
  334. found_short = 1;
  335. dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d "
  336. "result %zu urb->actual_length %d\n",
  337. xfer, wa_xfer_id(xfer), seg->index, found_short,
  338. seg->result, urb->actual_length);
  339. break;
  340. case WA_SEG_ERROR:
  341. xfer->result = seg->result;
  342. dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zi(0x%08zX)\n",
  343. xfer, wa_xfer_id(xfer), seg->index, seg->result,
  344. seg->result);
  345. goto out;
  346. case WA_SEG_ABORTED:
  347. xfer->result = seg->result;
  348. dev_dbg(dev, "xfer %p ID %08X#%u: ABORTED result %zi(0x%08zX)\n",
  349. xfer, wa_xfer_id(xfer), seg->index, seg->result,
  350. seg->result);
  351. goto out;
  352. default:
  353. dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n",
  354. xfer, wa_xfer_id(xfer), cnt, seg->status);
  355. xfer->result = -EINVAL;
  356. goto out;
  357. }
  358. }
  359. xfer->result = 0;
  360. out:
  361. return result;
  362. }
  363. /*
  364. * Mark the given segment as done. Return true if this completes the xfer.
  365. * This should only be called for segs that have been submitted to an RPIPE.
  366. * Delayed segs are not marked as submitted so they do not need to be marked
  367. * as done when cleaning up.
  368. *
  369. * xfer->lock has to be locked
  370. */
  371. static unsigned __wa_xfer_mark_seg_as_done(struct wa_xfer *xfer,
  372. struct wa_seg *seg, enum wa_seg_status status)
  373. {
  374. seg->status = status;
  375. xfer->segs_done++;
  376. /* check for done. */
  377. return __wa_xfer_is_done(xfer);
  378. }
  379. /*
  380. * Search for a transfer list ID on the HCD's URB list
  381. *
  382. * For 32 bit architectures, we use the pointer itself; for 64 bits, a
  383. * 32-bit hash of the pointer.
  384. *
  385. * @returns NULL if not found.
  386. */
  387. static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
  388. {
  389. unsigned long flags;
  390. struct wa_xfer *xfer_itr;
  391. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  392. list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
  393. if (id == xfer_itr->id) {
  394. wa_xfer_get(xfer_itr);
  395. goto out;
  396. }
  397. }
  398. xfer_itr = NULL;
  399. out:
  400. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  401. return xfer_itr;
  402. }
  403. struct wa_xfer_abort_buffer {
  404. struct urb urb;
  405. struct wahc *wa;
  406. struct wa_xfer_abort cmd;
  407. };
  408. static void __wa_xfer_abort_cb(struct urb *urb)
  409. {
  410. struct wa_xfer_abort_buffer *b = urb->context;
  411. struct wahc *wa = b->wa;
  412. /*
  413. * If the abort request URB failed, then the HWA did not get the abort
  414. * command. Forcibly clean up the xfer without waiting for a Transfer
  415. * Result from the HWA.
  416. */
  417. if (urb->status < 0) {
  418. struct wa_xfer *xfer;
  419. struct device *dev = &wa->usb_iface->dev;
  420. xfer = wa_xfer_get_by_id(wa, le32_to_cpu(b->cmd.dwTransferID));
  421. dev_err(dev, "%s: Transfer Abort request failed. result: %d\n",
  422. __func__, urb->status);
  423. if (xfer) {
  424. unsigned long flags;
  425. int done, seg_index = 0;
  426. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  427. dev_err(dev, "%s: cleaning up xfer %p ID 0x%08X.\n",
  428. __func__, xfer, wa_xfer_id(xfer));
  429. spin_lock_irqsave(&xfer->lock, flags);
  430. /* skip done segs. */
  431. while (seg_index < xfer->segs) {
  432. struct wa_seg *seg = xfer->seg[seg_index];
  433. if ((seg->status == WA_SEG_DONE) ||
  434. (seg->status == WA_SEG_ERROR)) {
  435. ++seg_index;
  436. } else {
  437. break;
  438. }
  439. }
  440. /* mark remaining segs as aborted. */
  441. wa_complete_remaining_xfer_segs(xfer, seg_index,
  442. WA_SEG_ABORTED);
  443. done = __wa_xfer_is_done(xfer);
  444. spin_unlock_irqrestore(&xfer->lock, flags);
  445. if (done)
  446. wa_xfer_completion(xfer);
  447. wa_xfer_delayed_run(rpipe);
  448. wa_xfer_put(xfer);
  449. } else {
  450. dev_err(dev, "%s: xfer ID 0x%08X already gone.\n",
  451. __func__, le32_to_cpu(b->cmd.dwTransferID));
  452. }
  453. }
  454. wa_put(wa); /* taken in __wa_xfer_abort */
  455. usb_put_urb(&b->urb);
  456. }
  457. /*
  458. * Aborts an ongoing transaction
  459. *
  460. * Assumes the transfer is referenced and locked and in a submitted
  461. * state (mainly that there is an endpoint/rpipe assigned).
  462. *
  463. * The callback (see above) does nothing but freeing up the data by
  464. * putting the URB. Because the URB is allocated at the head of the
  465. * struct, the whole space we allocated is kfreed. *
  466. */
  467. static int __wa_xfer_abort(struct wa_xfer *xfer)
  468. {
  469. int result = -ENOMEM;
  470. struct device *dev = &xfer->wa->usb_iface->dev;
  471. struct wa_xfer_abort_buffer *b;
  472. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  473. b = kmalloc(sizeof(*b), GFP_ATOMIC);
  474. if (b == NULL)
  475. goto error_kmalloc;
  476. b->cmd.bLength = sizeof(b->cmd);
  477. b->cmd.bRequestType = WA_XFER_ABORT;
  478. b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
  479. b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
  480. b->wa = wa_get(xfer->wa);
  481. usb_init_urb(&b->urb);
  482. usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
  483. usb_sndbulkpipe(xfer->wa->usb_dev,
  484. xfer->wa->dto_epd->bEndpointAddress),
  485. &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
  486. result = usb_submit_urb(&b->urb, GFP_ATOMIC);
  487. if (result < 0)
  488. goto error_submit;
  489. return result; /* callback frees! */
  490. error_submit:
  491. wa_put(xfer->wa);
  492. if (printk_ratelimit())
  493. dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
  494. xfer, result);
  495. kfree(b);
  496. error_kmalloc:
  497. return result;
  498. }
  499. /*
  500. * Calculate the number of isoc frames starting from isoc_frame_offset
  501. * that will fit a in transfer segment.
  502. */
  503. static int __wa_seg_calculate_isoc_frame_count(struct wa_xfer *xfer,
  504. int isoc_frame_offset, int *total_size)
  505. {
  506. int segment_size = 0, frame_count = 0;
  507. int index = isoc_frame_offset;
  508. struct usb_iso_packet_descriptor *iso_frame_desc =
  509. xfer->urb->iso_frame_desc;
  510. while ((index < xfer->urb->number_of_packets)
  511. && ((segment_size + iso_frame_desc[index].length)
  512. <= xfer->seg_size)) {
  513. /*
  514. * For Alereon HWA devices, only include an isoc frame in an
  515. * out segment if it is physically contiguous with the previous
  516. * frame. This is required because those devices expect
  517. * the isoc frames to be sent as a single USB transaction as
  518. * opposed to one transaction per frame with standard HWA.
  519. */
  520. if ((xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
  521. && (xfer->is_inbound == 0)
  522. && (index > isoc_frame_offset)
  523. && ((iso_frame_desc[index - 1].offset +
  524. iso_frame_desc[index - 1].length) !=
  525. iso_frame_desc[index].offset))
  526. break;
  527. /* this frame fits. count it. */
  528. ++frame_count;
  529. segment_size += iso_frame_desc[index].length;
  530. /* move to the next isoc frame. */
  531. ++index;
  532. }
  533. *total_size = segment_size;
  534. return frame_count;
  535. }
  536. /*
  537. *
  538. * @returns < 0 on error, transfer segment request size if ok
  539. */
  540. static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
  541. enum wa_xfer_type *pxfer_type)
  542. {
  543. ssize_t result;
  544. struct device *dev = &xfer->wa->usb_iface->dev;
  545. size_t maxpktsize;
  546. struct urb *urb = xfer->urb;
  547. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  548. switch (rpipe->descr.bmAttribute & 0x3) {
  549. case USB_ENDPOINT_XFER_CONTROL:
  550. *pxfer_type = WA_XFER_TYPE_CTL;
  551. result = sizeof(struct wa_xfer_ctl);
  552. break;
  553. case USB_ENDPOINT_XFER_INT:
  554. case USB_ENDPOINT_XFER_BULK:
  555. *pxfer_type = WA_XFER_TYPE_BI;
  556. result = sizeof(struct wa_xfer_bi);
  557. break;
  558. case USB_ENDPOINT_XFER_ISOC:
  559. *pxfer_type = WA_XFER_TYPE_ISO;
  560. result = sizeof(struct wa_xfer_hwaiso);
  561. break;
  562. default:
  563. /* never happens */
  564. BUG();
  565. result = -EINVAL; /* shut gcc up */
  566. }
  567. xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
  568. xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
  569. maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
  570. xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
  571. * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
  572. /* Compute the segment size and make sure it is a multiple of
  573. * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
  574. * a check (FIXME) */
  575. if (xfer->seg_size < maxpktsize) {
  576. dev_err(dev,
  577. "HW BUG? seg_size %zu smaller than maxpktsize %zu\n",
  578. xfer->seg_size, maxpktsize);
  579. result = -EINVAL;
  580. goto error;
  581. }
  582. xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
  583. if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) {
  584. int index = 0;
  585. xfer->segs = 0;
  586. /*
  587. * loop over urb->number_of_packets to determine how many
  588. * xfer segments will be needed to send the isoc frames.
  589. */
  590. while (index < urb->number_of_packets) {
  591. int seg_size; /* don't care. */
  592. index += __wa_seg_calculate_isoc_frame_count(xfer,
  593. index, &seg_size);
  594. ++xfer->segs;
  595. }
  596. } else {
  597. xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length,
  598. xfer->seg_size);
  599. if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
  600. xfer->segs = 1;
  601. }
  602. if (xfer->segs > WA_SEGS_MAX) {
  603. dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n",
  604. (urb->transfer_buffer_length/xfer->seg_size),
  605. WA_SEGS_MAX);
  606. result = -EINVAL;
  607. goto error;
  608. }
  609. error:
  610. return result;
  611. }
  612. static void __wa_setup_isoc_packet_descr(
  613. struct wa_xfer_packet_info_hwaiso *packet_desc,
  614. struct wa_xfer *xfer,
  615. struct wa_seg *seg) {
  616. struct usb_iso_packet_descriptor *iso_frame_desc =
  617. xfer->urb->iso_frame_desc;
  618. int frame_index;
  619. /* populate isoc packet descriptor. */
  620. packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO;
  621. packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) +
  622. (sizeof(packet_desc->PacketLength[0]) *
  623. seg->isoc_frame_count));
  624. for (frame_index = 0; frame_index < seg->isoc_frame_count;
  625. ++frame_index) {
  626. int offset_index = frame_index + seg->isoc_frame_offset;
  627. packet_desc->PacketLength[frame_index] =
  628. cpu_to_le16(iso_frame_desc[offset_index].length);
  629. }
  630. }
  631. /* Fill in the common request header and xfer-type specific data. */
  632. static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
  633. struct wa_xfer_hdr *xfer_hdr0,
  634. enum wa_xfer_type xfer_type,
  635. size_t xfer_hdr_size)
  636. {
  637. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  638. struct wa_seg *seg = xfer->seg[0];
  639. xfer_hdr0 = &seg->xfer_hdr;
  640. xfer_hdr0->bLength = xfer_hdr_size;
  641. xfer_hdr0->bRequestType = xfer_type;
  642. xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
  643. xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
  644. xfer_hdr0->bTransferSegment = 0;
  645. switch (xfer_type) {
  646. case WA_XFER_TYPE_CTL: {
  647. struct wa_xfer_ctl *xfer_ctl =
  648. container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
  649. xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
  650. memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
  651. sizeof(xfer_ctl->baSetupData));
  652. break;
  653. }
  654. case WA_XFER_TYPE_BI:
  655. break;
  656. case WA_XFER_TYPE_ISO: {
  657. struct wa_xfer_hwaiso *xfer_iso =
  658. container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr);
  659. struct wa_xfer_packet_info_hwaiso *packet_desc =
  660. ((void *)xfer_iso) + xfer_hdr_size;
  661. /* populate the isoc section of the transfer request. */
  662. xfer_iso->dwNumOfPackets = cpu_to_le32(seg->isoc_frame_count);
  663. /* populate isoc packet descriptor. */
  664. __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
  665. break;
  666. }
  667. default:
  668. BUG();
  669. };
  670. }
  671. /*
  672. * Callback for the OUT data phase of the segment request
  673. *
  674. * Check wa_seg_tr_cb(); most comments also apply here because this
  675. * function does almost the same thing and they work closely
  676. * together.
  677. *
  678. * If the seg request has failed but this DTO phase has succeeded,
  679. * wa_seg_tr_cb() has already failed the segment and moved the
  680. * status to WA_SEG_ERROR, so this will go through 'case 0' and
  681. * effectively do nothing.
  682. */
  683. static void wa_seg_dto_cb(struct urb *urb)
  684. {
  685. struct wa_seg *seg = urb->context;
  686. struct wa_xfer *xfer = seg->xfer;
  687. struct wahc *wa;
  688. struct device *dev;
  689. struct wa_rpipe *rpipe;
  690. unsigned long flags;
  691. unsigned rpipe_ready = 0;
  692. int data_send_done = 1, release_dto = 0, holding_dto = 0;
  693. u8 done = 0;
  694. int result;
  695. /* free the sg if it was used. */
  696. kfree(urb->sg);
  697. urb->sg = NULL;
  698. spin_lock_irqsave(&xfer->lock, flags);
  699. wa = xfer->wa;
  700. dev = &wa->usb_iface->dev;
  701. if (usb_pipeisoc(xfer->urb->pipe)) {
  702. /* Alereon HWA sends all isoc frames in a single transfer. */
  703. if (wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
  704. seg->isoc_frame_index += seg->isoc_frame_count;
  705. else
  706. seg->isoc_frame_index += 1;
  707. if (seg->isoc_frame_index < seg->isoc_frame_count) {
  708. data_send_done = 0;
  709. holding_dto = 1; /* checked in error cases. */
  710. /*
  711. * if this is the last isoc frame of the segment, we
  712. * can release DTO after sending this frame.
  713. */
  714. if ((seg->isoc_frame_index + 1) >=
  715. seg->isoc_frame_count)
  716. release_dto = 1;
  717. }
  718. dev_dbg(dev, "xfer 0x%08X#%u: isoc frame = %d, holding_dto = %d, release_dto = %d.\n",
  719. wa_xfer_id(xfer), seg->index, seg->isoc_frame_index,
  720. holding_dto, release_dto);
  721. }
  722. spin_unlock_irqrestore(&xfer->lock, flags);
  723. switch (urb->status) {
  724. case 0:
  725. spin_lock_irqsave(&xfer->lock, flags);
  726. seg->result += urb->actual_length;
  727. if (data_send_done) {
  728. dev_dbg(dev, "xfer 0x%08X#%u: data out done (%zu bytes)\n",
  729. wa_xfer_id(xfer), seg->index, seg->result);
  730. if (seg->status < WA_SEG_PENDING)
  731. seg->status = WA_SEG_PENDING;
  732. } else {
  733. /* should only hit this for isoc xfers. */
  734. /*
  735. * Populate the dto URB with the next isoc frame buffer,
  736. * send the URB and release DTO if we no longer need it.
  737. */
  738. __wa_populate_dto_urb_isoc(xfer, seg,
  739. seg->isoc_frame_offset + seg->isoc_frame_index);
  740. /* resubmit the URB with the next isoc frame. */
  741. /* take a ref on resubmit. */
  742. wa_xfer_get(xfer);
  743. result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
  744. if (result < 0) {
  745. dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
  746. wa_xfer_id(xfer), seg->index, result);
  747. spin_unlock_irqrestore(&xfer->lock, flags);
  748. goto error_dto_submit;
  749. }
  750. }
  751. spin_unlock_irqrestore(&xfer->lock, flags);
  752. if (release_dto) {
  753. __wa_dto_put(wa);
  754. wa_check_for_delayed_rpipes(wa);
  755. }
  756. break;
  757. case -ECONNRESET: /* URB unlinked; no need to do anything */
  758. case -ENOENT: /* as it was done by the who unlinked us */
  759. if (holding_dto) {
  760. __wa_dto_put(wa);
  761. wa_check_for_delayed_rpipes(wa);
  762. }
  763. break;
  764. default: /* Other errors ... */
  765. dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
  766. wa_xfer_id(xfer), seg->index, urb->status);
  767. goto error_default;
  768. }
  769. /* taken when this URB was submitted. */
  770. wa_xfer_put(xfer);
  771. return;
  772. error_dto_submit:
  773. /* taken on resubmit attempt. */
  774. wa_xfer_put(xfer);
  775. error_default:
  776. spin_lock_irqsave(&xfer->lock, flags);
  777. rpipe = xfer->ep->hcpriv;
  778. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  779. EDC_ERROR_TIMEFRAME)){
  780. dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
  781. wa_reset_all(wa);
  782. }
  783. if (seg->status != WA_SEG_ERROR) {
  784. seg->result = urb->status;
  785. __wa_xfer_abort(xfer);
  786. rpipe_ready = rpipe_avail_inc(rpipe);
  787. done = __wa_xfer_mark_seg_as_done(xfer, seg, WA_SEG_ERROR);
  788. }
  789. spin_unlock_irqrestore(&xfer->lock, flags);
  790. if (holding_dto) {
  791. __wa_dto_put(wa);
  792. wa_check_for_delayed_rpipes(wa);
  793. }
  794. if (done)
  795. wa_xfer_completion(xfer);
  796. if (rpipe_ready)
  797. wa_xfer_delayed_run(rpipe);
  798. /* taken when this URB was submitted. */
  799. wa_xfer_put(xfer);
  800. }
  801. /*
  802. * Callback for the isoc packet descriptor phase of the segment request
  803. *
  804. * Check wa_seg_tr_cb(); most comments also apply here because this
  805. * function does almost the same thing and they work closely
  806. * together.
  807. *
  808. * If the seg request has failed but this phase has succeeded,
  809. * wa_seg_tr_cb() has already failed the segment and moved the
  810. * status to WA_SEG_ERROR, so this will go through 'case 0' and
  811. * effectively do nothing.
  812. */
  813. static void wa_seg_iso_pack_desc_cb(struct urb *urb)
  814. {
  815. struct wa_seg *seg = urb->context;
  816. struct wa_xfer *xfer = seg->xfer;
  817. struct wahc *wa;
  818. struct device *dev;
  819. struct wa_rpipe *rpipe;
  820. unsigned long flags;
  821. unsigned rpipe_ready = 0;
  822. u8 done = 0;
  823. switch (urb->status) {
  824. case 0:
  825. spin_lock_irqsave(&xfer->lock, flags);
  826. wa = xfer->wa;
  827. dev = &wa->usb_iface->dev;
  828. dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
  829. wa_xfer_id(xfer), seg->index);
  830. if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
  831. seg->status = WA_SEG_PENDING;
  832. spin_unlock_irqrestore(&xfer->lock, flags);
  833. break;
  834. case -ECONNRESET: /* URB unlinked; no need to do anything */
  835. case -ENOENT: /* as it was done by the who unlinked us */
  836. break;
  837. default: /* Other errors ... */
  838. spin_lock_irqsave(&xfer->lock, flags);
  839. wa = xfer->wa;
  840. dev = &wa->usb_iface->dev;
  841. rpipe = xfer->ep->hcpriv;
  842. pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
  843. wa_xfer_id(xfer), seg->index, urb->status);
  844. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  845. EDC_ERROR_TIMEFRAME)){
  846. dev_err(dev, "iso xfer: URB max acceptable errors exceeded, resetting device\n");
  847. wa_reset_all(wa);
  848. }
  849. if (seg->status != WA_SEG_ERROR) {
  850. usb_unlink_urb(seg->dto_urb);
  851. seg->result = urb->status;
  852. __wa_xfer_abort(xfer);
  853. rpipe_ready = rpipe_avail_inc(rpipe);
  854. done = __wa_xfer_mark_seg_as_done(xfer, seg,
  855. WA_SEG_ERROR);
  856. }
  857. spin_unlock_irqrestore(&xfer->lock, flags);
  858. if (done)
  859. wa_xfer_completion(xfer);
  860. if (rpipe_ready)
  861. wa_xfer_delayed_run(rpipe);
  862. }
  863. /* taken when this URB was submitted. */
  864. wa_xfer_put(xfer);
  865. }
  866. /*
  867. * Callback for the segment request
  868. *
  869. * If successful transition state (unless already transitioned or
  870. * outbound transfer); otherwise, take a note of the error, mark this
  871. * segment done and try completion.
  872. *
  873. * Note we don't access until we are sure that the transfer hasn't
  874. * been cancelled (ECONNRESET, ENOENT), which could mean that
  875. * seg->xfer could be already gone.
  876. *
  877. * We have to check before setting the status to WA_SEG_PENDING
  878. * because sometimes the xfer result callback arrives before this
  879. * callback (geeeeeeze), so it might happen that we are already in
  880. * another state. As well, we don't set it if the transfer is not inbound,
  881. * as in that case, wa_seg_dto_cb will do it when the OUT data phase
  882. * finishes.
  883. */
  884. static void wa_seg_tr_cb(struct urb *urb)
  885. {
  886. struct wa_seg *seg = urb->context;
  887. struct wa_xfer *xfer = seg->xfer;
  888. struct wahc *wa;
  889. struct device *dev;
  890. struct wa_rpipe *rpipe;
  891. unsigned long flags;
  892. unsigned rpipe_ready;
  893. u8 done = 0;
  894. switch (urb->status) {
  895. case 0:
  896. spin_lock_irqsave(&xfer->lock, flags);
  897. wa = xfer->wa;
  898. dev = &wa->usb_iface->dev;
  899. dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
  900. xfer, wa_xfer_id(xfer), seg->index);
  901. if (xfer->is_inbound &&
  902. seg->status < WA_SEG_PENDING &&
  903. !(usb_pipeisoc(xfer->urb->pipe)))
  904. seg->status = WA_SEG_PENDING;
  905. spin_unlock_irqrestore(&xfer->lock, flags);
  906. break;
  907. case -ECONNRESET: /* URB unlinked; no need to do anything */
  908. case -ENOENT: /* as it was done by the who unlinked us */
  909. break;
  910. default: /* Other errors ... */
  911. spin_lock_irqsave(&xfer->lock, flags);
  912. wa = xfer->wa;
  913. dev = &wa->usb_iface->dev;
  914. rpipe = xfer->ep->hcpriv;
  915. if (printk_ratelimit())
  916. dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
  917. xfer, wa_xfer_id(xfer), seg->index,
  918. urb->status);
  919. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  920. EDC_ERROR_TIMEFRAME)){
  921. dev_err(dev, "DTO: URB max acceptable errors "
  922. "exceeded, resetting device\n");
  923. wa_reset_all(wa);
  924. }
  925. usb_unlink_urb(seg->isoc_pack_desc_urb);
  926. usb_unlink_urb(seg->dto_urb);
  927. seg->result = urb->status;
  928. __wa_xfer_abort(xfer);
  929. rpipe_ready = rpipe_avail_inc(rpipe);
  930. done = __wa_xfer_mark_seg_as_done(xfer, seg, WA_SEG_ERROR);
  931. spin_unlock_irqrestore(&xfer->lock, flags);
  932. if (done)
  933. wa_xfer_completion(xfer);
  934. if (rpipe_ready)
  935. wa_xfer_delayed_run(rpipe);
  936. }
  937. /* taken when this URB was submitted. */
  938. wa_xfer_put(xfer);
  939. }
  940. /*
  941. * Allocate an SG list to store bytes_to_transfer bytes and copy the
  942. * subset of the in_sg that matches the buffer subset
  943. * we are about to transfer.
  944. */
  945. static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
  946. const unsigned int bytes_transferred,
  947. const unsigned int bytes_to_transfer, int *out_num_sgs)
  948. {
  949. struct scatterlist *out_sg;
  950. unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
  951. nents;
  952. struct scatterlist *current_xfer_sg = in_sg;
  953. struct scatterlist *current_seg_sg, *last_seg_sg;
  954. /* skip previously transferred pages. */
  955. while ((current_xfer_sg) &&
  956. (bytes_processed < bytes_transferred)) {
  957. bytes_processed += current_xfer_sg->length;
  958. /* advance the sg if current segment starts on or past the
  959. next page. */
  960. if (bytes_processed <= bytes_transferred)
  961. current_xfer_sg = sg_next(current_xfer_sg);
  962. }
  963. /* the data for the current segment starts in current_xfer_sg.
  964. calculate the offset. */
  965. if (bytes_processed > bytes_transferred) {
  966. offset_into_current_page_data = current_xfer_sg->length -
  967. (bytes_processed - bytes_transferred);
  968. }
  969. /* calculate the number of pages needed by this segment. */
  970. nents = DIV_ROUND_UP((bytes_to_transfer +
  971. offset_into_current_page_data +
  972. current_xfer_sg->offset),
  973. PAGE_SIZE);
  974. out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
  975. if (out_sg) {
  976. sg_init_table(out_sg, nents);
  977. /* copy the portion of the incoming SG that correlates to the
  978. * data to be transferred by this segment to the segment SG. */
  979. last_seg_sg = current_seg_sg = out_sg;
  980. bytes_processed = 0;
  981. /* reset nents and calculate the actual number of sg entries
  982. needed. */
  983. nents = 0;
  984. while ((bytes_processed < bytes_to_transfer) &&
  985. current_seg_sg && current_xfer_sg) {
  986. unsigned int page_len = min((current_xfer_sg->length -
  987. offset_into_current_page_data),
  988. (bytes_to_transfer - bytes_processed));
  989. sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
  990. page_len,
  991. current_xfer_sg->offset +
  992. offset_into_current_page_data);
  993. bytes_processed += page_len;
  994. last_seg_sg = current_seg_sg;
  995. current_seg_sg = sg_next(current_seg_sg);
  996. current_xfer_sg = sg_next(current_xfer_sg);
  997. /* only the first page may require additional offset. */
  998. offset_into_current_page_data = 0;
  999. nents++;
  1000. }
  1001. /* update num_sgs and terminate the list since we may have
  1002. * concatenated pages. */
  1003. sg_mark_end(last_seg_sg);
  1004. *out_num_sgs = nents;
  1005. }
  1006. return out_sg;
  1007. }
  1008. /*
  1009. * Populate DMA buffer info for the isoc dto urb.
  1010. */
  1011. static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
  1012. struct wa_seg *seg, int curr_iso_frame)
  1013. {
  1014. seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1015. seg->dto_urb->sg = NULL;
  1016. seg->dto_urb->num_sgs = 0;
  1017. /* dto urb buffer address pulled from iso_frame_desc. */
  1018. seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
  1019. xfer->urb->iso_frame_desc[curr_iso_frame].offset;
  1020. /* The Alereon HWA sends a single URB with all isoc segs. */
  1021. if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
  1022. seg->dto_urb->transfer_buffer_length = seg->isoc_size;
  1023. else
  1024. seg->dto_urb->transfer_buffer_length =
  1025. xfer->urb->iso_frame_desc[curr_iso_frame].length;
  1026. }
  1027. /*
  1028. * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
  1029. */
  1030. static int __wa_populate_dto_urb(struct wa_xfer *xfer,
  1031. struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
  1032. {
  1033. int result = 0;
  1034. if (xfer->is_dma) {
  1035. seg->dto_urb->transfer_dma =
  1036. xfer->urb->transfer_dma + buf_itr_offset;
  1037. seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1038. seg->dto_urb->sg = NULL;
  1039. seg->dto_urb->num_sgs = 0;
  1040. } else {
  1041. /* do buffer or SG processing. */
  1042. seg->dto_urb->transfer_flags &=
  1043. ~URB_NO_TRANSFER_DMA_MAP;
  1044. /* this should always be 0 before a resubmit. */
  1045. seg->dto_urb->num_mapped_sgs = 0;
  1046. if (xfer->urb->transfer_buffer) {
  1047. seg->dto_urb->transfer_buffer =
  1048. xfer->urb->transfer_buffer +
  1049. buf_itr_offset;
  1050. seg->dto_urb->sg = NULL;
  1051. seg->dto_urb->num_sgs = 0;
  1052. } else {
  1053. seg->dto_urb->transfer_buffer = NULL;
  1054. /*
  1055. * allocate an SG list to store seg_size bytes
  1056. * and copy the subset of the xfer->urb->sg that
  1057. * matches the buffer subset we are about to
  1058. * read.
  1059. */
  1060. seg->dto_urb->sg = wa_xfer_create_subset_sg(
  1061. xfer->urb->sg,
  1062. buf_itr_offset, buf_itr_size,
  1063. &(seg->dto_urb->num_sgs));
  1064. if (!(seg->dto_urb->sg))
  1065. result = -ENOMEM;
  1066. }
  1067. }
  1068. seg->dto_urb->transfer_buffer_length = buf_itr_size;
  1069. return result;
  1070. }
  1071. /*
  1072. * Allocate the segs array and initialize each of them
  1073. *
  1074. * The segments are freed by wa_xfer_destroy() when the xfer use count
  1075. * drops to zero; however, because each segment is given the same life
  1076. * cycle as the USB URB it contains, it is actually freed by
  1077. * usb_put_urb() on the contained USB URB (twisted, eh?).
  1078. */
  1079. static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
  1080. {
  1081. int result, cnt, isoc_frame_offset = 0;
  1082. size_t alloc_size = sizeof(*xfer->seg[0])
  1083. - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
  1084. struct usb_device *usb_dev = xfer->wa->usb_dev;
  1085. const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
  1086. struct wa_seg *seg;
  1087. size_t buf_itr, buf_size, buf_itr_size;
  1088. result = -ENOMEM;
  1089. xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
  1090. if (xfer->seg == NULL)
  1091. goto error_segs_kzalloc;
  1092. buf_itr = 0;
  1093. buf_size = xfer->urb->transfer_buffer_length;
  1094. for (cnt = 0; cnt < xfer->segs; cnt++) {
  1095. size_t iso_pkt_descr_size = 0;
  1096. int seg_isoc_frame_count = 0, seg_isoc_size = 0;
  1097. /*
  1098. * Adjust the size of the segment object to contain space for
  1099. * the isoc packet descriptor buffer.
  1100. */
  1101. if (usb_pipeisoc(xfer->urb->pipe)) {
  1102. seg_isoc_frame_count =
  1103. __wa_seg_calculate_isoc_frame_count(xfer,
  1104. isoc_frame_offset, &seg_isoc_size);
  1105. iso_pkt_descr_size =
  1106. sizeof(struct wa_xfer_packet_info_hwaiso) +
  1107. (seg_isoc_frame_count * sizeof(__le16));
  1108. }
  1109. seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
  1110. GFP_ATOMIC);
  1111. if (seg == NULL)
  1112. goto error_seg_kmalloc;
  1113. wa_seg_init(seg);
  1114. seg->xfer = xfer;
  1115. seg->index = cnt;
  1116. usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
  1117. usb_sndbulkpipe(usb_dev,
  1118. dto_epd->bEndpointAddress),
  1119. &seg->xfer_hdr, xfer_hdr_size,
  1120. wa_seg_tr_cb, seg);
  1121. buf_itr_size = min(buf_size, xfer->seg_size);
  1122. if (usb_pipeisoc(xfer->urb->pipe)) {
  1123. seg->isoc_frame_count = seg_isoc_frame_count;
  1124. seg->isoc_frame_offset = isoc_frame_offset;
  1125. seg->isoc_size = seg_isoc_size;
  1126. /* iso packet descriptor. */
  1127. seg->isoc_pack_desc_urb =
  1128. usb_alloc_urb(0, GFP_ATOMIC);
  1129. if (seg->isoc_pack_desc_urb == NULL)
  1130. goto error_iso_pack_desc_alloc;
  1131. /*
  1132. * The buffer for the isoc packet descriptor starts
  1133. * after the transfer request header in the
  1134. * segment object memory buffer.
  1135. */
  1136. usb_fill_bulk_urb(
  1137. seg->isoc_pack_desc_urb, usb_dev,
  1138. usb_sndbulkpipe(usb_dev,
  1139. dto_epd->bEndpointAddress),
  1140. (void *)(&seg->xfer_hdr) +
  1141. xfer_hdr_size,
  1142. iso_pkt_descr_size,
  1143. wa_seg_iso_pack_desc_cb, seg);
  1144. /* adjust starting frame offset for next seg. */
  1145. isoc_frame_offset += seg_isoc_frame_count;
  1146. }
  1147. if (xfer->is_inbound == 0 && buf_size > 0) {
  1148. /* outbound data. */
  1149. seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
  1150. if (seg->dto_urb == NULL)
  1151. goto error_dto_alloc;
  1152. usb_fill_bulk_urb(
  1153. seg->dto_urb, usb_dev,
  1154. usb_sndbulkpipe(usb_dev,
  1155. dto_epd->bEndpointAddress),
  1156. NULL, 0, wa_seg_dto_cb, seg);
  1157. if (usb_pipeisoc(xfer->urb->pipe)) {
  1158. /*
  1159. * Fill in the xfer buffer information for the
  1160. * first isoc frame. Subsequent frames in this
  1161. * segment will be filled in and sent from the
  1162. * DTO completion routine, if needed.
  1163. */
  1164. __wa_populate_dto_urb_isoc(xfer, seg,
  1165. seg->isoc_frame_offset);
  1166. } else {
  1167. /* fill in the xfer buffer information. */
  1168. result = __wa_populate_dto_urb(xfer, seg,
  1169. buf_itr, buf_itr_size);
  1170. if (result < 0)
  1171. goto error_seg_outbound_populate;
  1172. buf_itr += buf_itr_size;
  1173. buf_size -= buf_itr_size;
  1174. }
  1175. }
  1176. seg->status = WA_SEG_READY;
  1177. }
  1178. return 0;
  1179. /*
  1180. * Free the memory for the current segment which failed to init.
  1181. * Use the fact that cnt is left at were it failed. The remaining
  1182. * segments will be cleaned up by wa_xfer_destroy.
  1183. */
  1184. error_seg_outbound_populate:
  1185. usb_free_urb(xfer->seg[cnt]->dto_urb);
  1186. error_dto_alloc:
  1187. usb_free_urb(xfer->seg[cnt]->isoc_pack_desc_urb);
  1188. error_iso_pack_desc_alloc:
  1189. kfree(xfer->seg[cnt]);
  1190. xfer->seg[cnt] = NULL;
  1191. error_seg_kmalloc:
  1192. error_segs_kzalloc:
  1193. return result;
  1194. }
  1195. /*
  1196. * Allocates all the stuff needed to submit a transfer
  1197. *
  1198. * Breaks the whole data buffer in a list of segments, each one has a
  1199. * structure allocated to it and linked in xfer->seg[index]
  1200. *
  1201. * FIXME: merge setup_segs() and the last part of this function, no
  1202. * need to do two for loops when we could run everything in a
  1203. * single one
  1204. */
  1205. static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
  1206. {
  1207. int result;
  1208. struct device *dev = &xfer->wa->usb_iface->dev;
  1209. enum wa_xfer_type xfer_type = 0; /* shut up GCC */
  1210. size_t xfer_hdr_size, cnt, transfer_size;
  1211. struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
  1212. result = __wa_xfer_setup_sizes(xfer, &xfer_type);
  1213. if (result < 0)
  1214. goto error_setup_sizes;
  1215. xfer_hdr_size = result;
  1216. result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
  1217. if (result < 0) {
  1218. dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
  1219. xfer, xfer->segs, result);
  1220. goto error_setup_segs;
  1221. }
  1222. /* Fill the first header */
  1223. xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
  1224. wa_xfer_id_init(xfer);
  1225. __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
  1226. /* Fill remaining headers */
  1227. xfer_hdr = xfer_hdr0;
  1228. if (xfer_type == WA_XFER_TYPE_ISO) {
  1229. xfer_hdr0->dwTransferLength =
  1230. cpu_to_le32(xfer->seg[0]->isoc_size);
  1231. for (cnt = 1; cnt < xfer->segs; cnt++) {
  1232. struct wa_xfer_packet_info_hwaiso *packet_desc;
  1233. struct wa_seg *seg = xfer->seg[cnt];
  1234. struct wa_xfer_hwaiso *xfer_iso;
  1235. xfer_hdr = &seg->xfer_hdr;
  1236. xfer_iso = container_of(xfer_hdr,
  1237. struct wa_xfer_hwaiso, hdr);
  1238. packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
  1239. /*
  1240. * Copy values from the 0th header. Segment specific
  1241. * values are set below.
  1242. */
  1243. memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
  1244. xfer_hdr->bTransferSegment = cnt;
  1245. xfer_hdr->dwTransferLength =
  1246. cpu_to_le32(seg->isoc_size);
  1247. xfer_iso->dwNumOfPackets =
  1248. cpu_to_le32(seg->isoc_frame_count);
  1249. __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
  1250. seg->status = WA_SEG_READY;
  1251. }
  1252. } else {
  1253. transfer_size = urb->transfer_buffer_length;
  1254. xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
  1255. cpu_to_le32(xfer->seg_size) :
  1256. cpu_to_le32(transfer_size);
  1257. transfer_size -= xfer->seg_size;
  1258. for (cnt = 1; cnt < xfer->segs; cnt++) {
  1259. xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
  1260. memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
  1261. xfer_hdr->bTransferSegment = cnt;
  1262. xfer_hdr->dwTransferLength =
  1263. transfer_size > xfer->seg_size ?
  1264. cpu_to_le32(xfer->seg_size)
  1265. : cpu_to_le32(transfer_size);
  1266. xfer->seg[cnt]->status = WA_SEG_READY;
  1267. transfer_size -= xfer->seg_size;
  1268. }
  1269. }
  1270. xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
  1271. result = 0;
  1272. error_setup_segs:
  1273. error_setup_sizes:
  1274. return result;
  1275. }
  1276. /*
  1277. *
  1278. *
  1279. * rpipe->seg_lock is held!
  1280. */
  1281. static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
  1282. struct wa_seg *seg, int *dto_done)
  1283. {
  1284. int result;
  1285. /* default to done unless we encounter a multi-frame isoc segment. */
  1286. *dto_done = 1;
  1287. /*
  1288. * Take a ref for each segment urb so the xfer cannot disappear until
  1289. * all of the callbacks run.
  1290. */
  1291. wa_xfer_get(xfer);
  1292. /* submit the transfer request. */
  1293. seg->status = WA_SEG_SUBMITTED;
  1294. result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
  1295. if (result < 0) {
  1296. pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
  1297. __func__, xfer, seg->index, result);
  1298. wa_xfer_put(xfer);
  1299. goto error_tr_submit;
  1300. }
  1301. /* submit the isoc packet descriptor if present. */
  1302. if (seg->isoc_pack_desc_urb) {
  1303. wa_xfer_get(xfer);
  1304. result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
  1305. seg->isoc_frame_index = 0;
  1306. if (result < 0) {
  1307. pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
  1308. __func__, xfer, seg->index, result);
  1309. wa_xfer_put(xfer);
  1310. goto error_iso_pack_desc_submit;
  1311. }
  1312. }
  1313. /* submit the out data if this is an out request. */
  1314. if (seg->dto_urb) {
  1315. struct wahc *wa = xfer->wa;
  1316. wa_xfer_get(xfer);
  1317. result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
  1318. if (result < 0) {
  1319. pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
  1320. __func__, xfer, seg->index, result);
  1321. wa_xfer_put(xfer);
  1322. goto error_dto_submit;
  1323. }
  1324. /*
  1325. * If this segment contains more than one isoc frame, hold
  1326. * onto the dto resource until we send all frames.
  1327. * Only applies to non-Alereon devices.
  1328. */
  1329. if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
  1330. && (seg->isoc_frame_count > 1))
  1331. *dto_done = 0;
  1332. }
  1333. rpipe_avail_dec(rpipe);
  1334. return 0;
  1335. error_dto_submit:
  1336. usb_unlink_urb(seg->isoc_pack_desc_urb);
  1337. error_iso_pack_desc_submit:
  1338. usb_unlink_urb(&seg->tr_urb);
  1339. error_tr_submit:
  1340. seg->status = WA_SEG_ERROR;
  1341. seg->result = result;
  1342. *dto_done = 1;
  1343. return result;
  1344. }
  1345. /*
  1346. * Execute more queued request segments until the maximum concurrent allowed.
  1347. * Return true if the DTO resource was acquired and released.
  1348. *
  1349. * The ugly unlock/lock sequence on the error path is needed as the
  1350. * xfer->lock normally nests the seg_lock and not viceversa.
  1351. */
  1352. static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
  1353. {
  1354. int result, dto_acquired = 0, dto_done = 0;
  1355. struct device *dev = &rpipe->wa->usb_iface->dev;
  1356. struct wa_seg *seg;
  1357. struct wa_xfer *xfer;
  1358. unsigned long flags;
  1359. *dto_waiting = 0;
  1360. spin_lock_irqsave(&rpipe->seg_lock, flags);
  1361. while (atomic_read(&rpipe->segs_available) > 0
  1362. && !list_empty(&rpipe->seg_list)
  1363. && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
  1364. seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
  1365. list_node);
  1366. list_del(&seg->list_node);
  1367. xfer = seg->xfer;
  1368. /*
  1369. * Get a reference to the xfer in case the callbacks for the
  1370. * URBs submitted by __wa_seg_submit attempt to complete
  1371. * the xfer before this function completes.
  1372. */
  1373. wa_xfer_get(xfer);
  1374. result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
  1375. /* release the dto resource if this RPIPE is done with it. */
  1376. if (dto_done)
  1377. __wa_dto_put(rpipe->wa);
  1378. dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
  1379. xfer, wa_xfer_id(xfer), seg->index,
  1380. atomic_read(&rpipe->segs_available), result);
  1381. if (unlikely(result < 0)) {
  1382. int done;
  1383. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  1384. spin_lock_irqsave(&xfer->lock, flags);
  1385. __wa_xfer_abort(xfer);
  1386. /*
  1387. * This seg was marked as submitted when it was put on
  1388. * the RPIPE seg_list. Mark it done.
  1389. */
  1390. xfer->segs_done++;
  1391. done = __wa_xfer_is_done(xfer);
  1392. spin_unlock_irqrestore(&xfer->lock, flags);
  1393. if (done)
  1394. wa_xfer_completion(xfer);
  1395. spin_lock_irqsave(&rpipe->seg_lock, flags);
  1396. }
  1397. wa_xfer_put(xfer);
  1398. }
  1399. /*
  1400. * Mark this RPIPE as waiting if dto was not acquired, there are
  1401. * delayed segs and no active transfers to wake us up later.
  1402. */
  1403. if (!dto_acquired && !list_empty(&rpipe->seg_list)
  1404. && (atomic_read(&rpipe->segs_available) ==
  1405. le16_to_cpu(rpipe->descr.wRequests)))
  1406. *dto_waiting = 1;
  1407. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  1408. return dto_done;
  1409. }
  1410. static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
  1411. {
  1412. int dto_waiting;
  1413. int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
  1414. /*
  1415. * If this RPIPE is waiting on the DTO resource, add it to the tail of
  1416. * the waiting list.
  1417. * Otherwise, if the WA DTO resource was acquired and released by
  1418. * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
  1419. * DTO and failed during that time. Check the delayed list and process
  1420. * any waiters. Start searching from the next RPIPE index.
  1421. */
  1422. if (dto_waiting)
  1423. wa_add_delayed_rpipe(rpipe->wa, rpipe);
  1424. else if (dto_done)
  1425. wa_check_for_delayed_rpipes(rpipe->wa);
  1426. }
  1427. /*
  1428. *
  1429. * xfer->lock is taken
  1430. *
  1431. * On failure submitting we just stop submitting and return error;
  1432. * wa_urb_enqueue_b() will execute the completion path
  1433. */
  1434. static int __wa_xfer_submit(struct wa_xfer *xfer)
  1435. {
  1436. int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
  1437. struct wahc *wa = xfer->wa;
  1438. struct device *dev = &wa->usb_iface->dev;
  1439. unsigned cnt;
  1440. struct wa_seg *seg;
  1441. unsigned long flags;
  1442. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  1443. size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
  1444. u8 available;
  1445. u8 empty;
  1446. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  1447. list_add_tail(&xfer->list_node, &wa->xfer_list);
  1448. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  1449. BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
  1450. result = 0;
  1451. spin_lock_irqsave(&rpipe->seg_lock, flags);
  1452. for (cnt = 0; cnt < xfer->segs; cnt++) {
  1453. int delay_seg = 1;
  1454. available = atomic_read(&rpipe->segs_available);
  1455. empty = list_empty(&rpipe->seg_list);
  1456. seg = xfer->seg[cnt];
  1457. if (available && empty) {
  1458. /*
  1459. * Only attempt to acquire DTO if we have a segment
  1460. * to send.
  1461. */
  1462. dto_acquired = __wa_dto_try_get(rpipe->wa);
  1463. if (dto_acquired) {
  1464. delay_seg = 0;
  1465. result = __wa_seg_submit(rpipe, xfer, seg,
  1466. &dto_done);
  1467. dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
  1468. xfer, wa_xfer_id(xfer), cnt, available,
  1469. empty);
  1470. if (dto_done)
  1471. __wa_dto_put(rpipe->wa);
  1472. if (result < 0) {
  1473. __wa_xfer_abort(xfer);
  1474. goto error_seg_submit;
  1475. }
  1476. }
  1477. }
  1478. if (delay_seg) {
  1479. dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
  1480. xfer, wa_xfer_id(xfer), cnt, available, empty);
  1481. seg->status = WA_SEG_DELAYED;
  1482. list_add_tail(&seg->list_node, &rpipe->seg_list);
  1483. }
  1484. xfer->segs_submitted++;
  1485. }
  1486. error_seg_submit:
  1487. /*
  1488. * Mark this RPIPE as waiting if dto was not acquired, there are
  1489. * delayed segs and no active transfers to wake us up later.
  1490. */
  1491. if (!dto_acquired && !list_empty(&rpipe->seg_list)
  1492. && (atomic_read(&rpipe->segs_available) ==
  1493. le16_to_cpu(rpipe->descr.wRequests)))
  1494. dto_waiting = 1;
  1495. spin_unlock_irqrestore(&rpipe->seg_lock, flags);
  1496. if (dto_waiting)
  1497. wa_add_delayed_rpipe(rpipe->wa, rpipe);
  1498. else if (dto_done)
  1499. wa_check_for_delayed_rpipes(rpipe->wa);
  1500. return result;
  1501. }
  1502. /*
  1503. * Second part of a URB/transfer enqueuement
  1504. *
  1505. * Assumes this comes from wa_urb_enqueue() [maybe through
  1506. * wa_urb_enqueue_run()]. At this point:
  1507. *
  1508. * xfer->wa filled and refcounted
  1509. * xfer->ep filled with rpipe refcounted if
  1510. * delayed == 0
  1511. * xfer->urb filled and refcounted (this is the case when called
  1512. * from wa_urb_enqueue() as we come from usb_submit_urb()
  1513. * and when called by wa_urb_enqueue_run(), as we took an
  1514. * extra ref dropped by _run() after we return).
  1515. * xfer->gfp filled
  1516. *
  1517. * If we fail at __wa_xfer_submit(), then we just check if we are done
  1518. * and if so, we run the completion procedure. However, if we are not
  1519. * yet done, we do nothing and wait for the completion handlers from
  1520. * the submitted URBs or from the xfer-result path to kick in. If xfer
  1521. * result never kicks in, the xfer will timeout from the USB code and
  1522. * dequeue() will be called.
  1523. */
  1524. static int wa_urb_enqueue_b(struct wa_xfer *xfer)
  1525. {
  1526. int result;
  1527. unsigned long flags;
  1528. struct urb *urb = xfer->urb;
  1529. struct wahc *wa = xfer->wa;
  1530. struct wusbhc *wusbhc = wa->wusb;
  1531. struct wusb_dev *wusb_dev;
  1532. unsigned done;
  1533. result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
  1534. if (result < 0) {
  1535. pr_err("%s: error_rpipe_get\n", __func__);
  1536. goto error_rpipe_get;
  1537. }
  1538. result = -ENODEV;
  1539. /* FIXME: segmentation broken -- kills DWA */
  1540. mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
  1541. if (urb->dev == NULL) {
  1542. mutex_unlock(&wusbhc->mutex);
  1543. pr_err("%s: error usb dev gone\n", __func__);
  1544. goto error_dev_gone;
  1545. }
  1546. wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
  1547. if (wusb_dev == NULL) {
  1548. mutex_unlock(&wusbhc->mutex);
  1549. dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
  1550. __func__);
  1551. goto error_dev_gone;
  1552. }
  1553. mutex_unlock(&wusbhc->mutex);
  1554. spin_lock_irqsave(&xfer->lock, flags);
  1555. xfer->wusb_dev = wusb_dev;
  1556. result = urb->status;
  1557. if (urb->status != -EINPROGRESS) {
  1558. dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
  1559. goto error_dequeued;
  1560. }
  1561. result = __wa_xfer_setup(xfer, urb);
  1562. if (result < 0) {
  1563. dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
  1564. goto error_xfer_setup;
  1565. }
  1566. /*
  1567. * Get a xfer reference since __wa_xfer_submit starts asynchronous
  1568. * operations that may try to complete the xfer before this function
  1569. * exits.
  1570. */
  1571. wa_xfer_get(xfer);
  1572. result = __wa_xfer_submit(xfer);
  1573. if (result < 0) {
  1574. dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
  1575. goto error_xfer_submit;
  1576. }
  1577. spin_unlock_irqrestore(&xfer->lock, flags);
  1578. wa_xfer_put(xfer);
  1579. return 0;
  1580. /*
  1581. * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
  1582. * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
  1583. * setup().
  1584. */
  1585. error_xfer_setup:
  1586. error_dequeued:
  1587. spin_unlock_irqrestore(&xfer->lock, flags);
  1588. /* FIXME: segmentation broken, kills DWA */
  1589. if (wusb_dev)
  1590. wusb_dev_put(wusb_dev);
  1591. error_dev_gone:
  1592. rpipe_put(xfer->ep->hcpriv);
  1593. error_rpipe_get:
  1594. xfer->result = result;
  1595. return result;
  1596. error_xfer_submit:
  1597. done = __wa_xfer_is_done(xfer);
  1598. xfer->result = result;
  1599. spin_unlock_irqrestore(&xfer->lock, flags);
  1600. if (done)
  1601. wa_xfer_completion(xfer);
  1602. wa_xfer_put(xfer);
  1603. /* return success since the completion routine will run. */
  1604. return 0;
  1605. }
  1606. /*
  1607. * Execute the delayed transfers in the Wire Adapter @wa
  1608. *
  1609. * We need to be careful here, as dequeue() could be called in the
  1610. * middle. That's why we do the whole thing under the
  1611. * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
  1612. * and then checks the list -- so as we would be acquiring in inverse
  1613. * order, we move the delayed list to a separate list while locked and then
  1614. * submit them without the list lock held.
  1615. */
  1616. void wa_urb_enqueue_run(struct work_struct *ws)
  1617. {
  1618. struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
  1619. struct wa_xfer *xfer, *next;
  1620. struct urb *urb;
  1621. LIST_HEAD(tmp_list);
  1622. /* Create a copy of the wa->xfer_delayed_list while holding the lock */
  1623. spin_lock_irq(&wa->xfer_list_lock);
  1624. list_cut_position(&tmp_list, &wa->xfer_delayed_list,
  1625. wa->xfer_delayed_list.prev);
  1626. spin_unlock_irq(&wa->xfer_list_lock);
  1627. /*
  1628. * enqueue from temp list without list lock held since wa_urb_enqueue_b
  1629. * can take xfer->lock as well as lock mutexes.
  1630. */
  1631. list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
  1632. list_del_init(&xfer->list_node);
  1633. urb = xfer->urb;
  1634. if (wa_urb_enqueue_b(xfer) < 0)
  1635. wa_xfer_giveback(xfer);
  1636. usb_put_urb(urb); /* taken when queuing */
  1637. }
  1638. }
  1639. EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
  1640. /*
  1641. * Process the errored transfers on the Wire Adapter outside of interrupt.
  1642. */
  1643. void wa_process_errored_transfers_run(struct work_struct *ws)
  1644. {
  1645. struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
  1646. struct wa_xfer *xfer, *next;
  1647. LIST_HEAD(tmp_list);
  1648. pr_info("%s: Run delayed STALL processing.\n", __func__);
  1649. /* Create a copy of the wa->xfer_errored_list while holding the lock */
  1650. spin_lock_irq(&wa->xfer_list_lock);
  1651. list_cut_position(&tmp_list, &wa->xfer_errored_list,
  1652. wa->xfer_errored_list.prev);
  1653. spin_unlock_irq(&wa->xfer_list_lock);
  1654. /*
  1655. * run rpipe_clear_feature_stalled from temp list without list lock
  1656. * held.
  1657. */
  1658. list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
  1659. struct usb_host_endpoint *ep;
  1660. unsigned long flags;
  1661. struct wa_rpipe *rpipe;
  1662. spin_lock_irqsave(&xfer->lock, flags);
  1663. ep = xfer->ep;
  1664. rpipe = ep->hcpriv;
  1665. spin_unlock_irqrestore(&xfer->lock, flags);
  1666. /* clear RPIPE feature stalled without holding a lock. */
  1667. rpipe_clear_feature_stalled(wa, ep);
  1668. /* complete the xfer. This removes it from the tmp list. */
  1669. wa_xfer_completion(xfer);
  1670. /* check for work. */
  1671. wa_xfer_delayed_run(rpipe);
  1672. }
  1673. }
  1674. EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
  1675. /*
  1676. * Submit a transfer to the Wire Adapter in a delayed way
  1677. *
  1678. * The process of enqueuing involves possible sleeps() [see
  1679. * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
  1680. * in an atomic section, we defer the enqueue_b() call--else we call direct.
  1681. *
  1682. * @urb: We own a reference to it done by the HCI Linux USB stack that
  1683. * will be given up by calling usb_hcd_giveback_urb() or by
  1684. * returning error from this function -> ergo we don't have to
  1685. * refcount it.
  1686. */
  1687. int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
  1688. struct urb *urb, gfp_t gfp)
  1689. {
  1690. int result;
  1691. struct device *dev = &wa->usb_iface->dev;
  1692. struct wa_xfer *xfer;
  1693. unsigned long my_flags;
  1694. unsigned cant_sleep = irqs_disabled() | in_atomic();
  1695. if ((urb->transfer_buffer == NULL)
  1696. && (urb->sg == NULL)
  1697. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  1698. && urb->transfer_buffer_length != 0) {
  1699. dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
  1700. dump_stack();
  1701. }
  1702. spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
  1703. result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
  1704. spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
  1705. if (result < 0)
  1706. goto error_link_urb;
  1707. result = -ENOMEM;
  1708. xfer = kzalloc(sizeof(*xfer), gfp);
  1709. if (xfer == NULL)
  1710. goto error_kmalloc;
  1711. result = -ENOENT;
  1712. if (urb->status != -EINPROGRESS) /* cancelled */
  1713. goto error_dequeued; /* before starting? */
  1714. wa_xfer_init(xfer);
  1715. xfer->wa = wa_get(wa);
  1716. xfer->urb = urb;
  1717. xfer->gfp = gfp;
  1718. xfer->ep = ep;
  1719. urb->hcpriv = xfer;
  1720. dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
  1721. xfer, urb, urb->pipe, urb->transfer_buffer_length,
  1722. urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
  1723. urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
  1724. cant_sleep ? "deferred" : "inline");
  1725. if (cant_sleep) {
  1726. usb_get_urb(urb);
  1727. spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
  1728. list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
  1729. spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
  1730. queue_work(wusbd, &wa->xfer_enqueue_work);
  1731. } else {
  1732. result = wa_urb_enqueue_b(xfer);
  1733. if (result < 0) {
  1734. /*
  1735. * URB submit/enqueue failed. Clean up, return an
  1736. * error and do not run the callback. This avoids
  1737. * an infinite submit/complete loop.
  1738. */
  1739. dev_err(dev, "%s: URB enqueue failed: %d\n",
  1740. __func__, result);
  1741. wa_put(xfer->wa);
  1742. wa_xfer_put(xfer);
  1743. spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
  1744. usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
  1745. spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
  1746. return result;
  1747. }
  1748. }
  1749. return 0;
  1750. error_dequeued:
  1751. kfree(xfer);
  1752. error_kmalloc:
  1753. spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
  1754. usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
  1755. spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
  1756. error_link_urb:
  1757. return result;
  1758. }
  1759. EXPORT_SYMBOL_GPL(wa_urb_enqueue);
  1760. /*
  1761. * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
  1762. * handler] is called.
  1763. *
  1764. * Until a transfer goes successfully through wa_urb_enqueue() it
  1765. * needs to be dequeued with completion calling; when stuck in delayed
  1766. * or before wa_xfer_setup() is called, we need to do completion.
  1767. *
  1768. * not setup If there is no hcpriv yet, that means that that enqueue
  1769. * still had no time to set the xfer up. Because
  1770. * urb->status should be other than -EINPROGRESS,
  1771. * enqueue() will catch that and bail out.
  1772. *
  1773. * If the transfer has gone through setup, we just need to clean it
  1774. * up. If it has gone through submit(), we have to abort it [with an
  1775. * asynch request] and then make sure we cancel each segment.
  1776. *
  1777. */
  1778. int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
  1779. {
  1780. unsigned long flags, flags2;
  1781. struct wa_xfer *xfer;
  1782. struct wa_seg *seg;
  1783. struct wa_rpipe *rpipe;
  1784. unsigned cnt, done = 0, xfer_abort_pending;
  1785. unsigned rpipe_ready = 0;
  1786. int result;
  1787. /* check if it is safe to unlink. */
  1788. spin_lock_irqsave(&wa->xfer_list_lock, flags);
  1789. result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
  1790. if ((result == 0) && urb->hcpriv) {
  1791. /*
  1792. * Get a xfer ref to prevent a race with wa_xfer_giveback
  1793. * cleaning up the xfer while we are working with it.
  1794. */
  1795. wa_xfer_get(urb->hcpriv);
  1796. }
  1797. spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
  1798. if (result)
  1799. return result;
  1800. xfer = urb->hcpriv;
  1801. if (xfer == NULL)
  1802. return -ENOENT;
  1803. spin_lock_irqsave(&xfer->lock, flags);
  1804. pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
  1805. rpipe = xfer->ep->hcpriv;
  1806. if (rpipe == NULL) {
  1807. pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
  1808. __func__, xfer, wa_xfer_id(xfer),
  1809. "Probably already aborted.\n" );
  1810. result = -ENOENT;
  1811. goto out_unlock;
  1812. }
  1813. /*
  1814. * Check for done to avoid racing with wa_xfer_giveback and completing
  1815. * twice.
  1816. */
  1817. if (__wa_xfer_is_done(xfer)) {
  1818. pr_debug("%s: xfer %p id 0x%08X already done.\n", __func__,
  1819. xfer, wa_xfer_id(xfer));
  1820. result = -ENOENT;
  1821. goto out_unlock;
  1822. }
  1823. /* Check the delayed list -> if there, release and complete */
  1824. spin_lock_irqsave(&wa->xfer_list_lock, flags2);
  1825. if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
  1826. goto dequeue_delayed;
  1827. spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
  1828. if (xfer->seg == NULL) /* still hasn't reached */
  1829. goto out_unlock; /* setup(), enqueue_b() completes */
  1830. /* Ok, the xfer is in flight already, it's been setup and submitted.*/
  1831. xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
  1832. /*
  1833. * grab the rpipe->seg_lock here to prevent racing with
  1834. * __wa_xfer_delayed_run.
  1835. */
  1836. spin_lock(&rpipe->seg_lock);
  1837. for (cnt = 0; cnt < xfer->segs; cnt++) {
  1838. seg = xfer->seg[cnt];
  1839. pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
  1840. __func__, wa_xfer_id(xfer), cnt, seg->status);
  1841. switch (seg->status) {
  1842. case WA_SEG_NOTREADY:
  1843. case WA_SEG_READY:
  1844. printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
  1845. xfer, cnt, seg->status);
  1846. WARN_ON(1);
  1847. break;
  1848. case WA_SEG_DELAYED:
  1849. /*
  1850. * delete from rpipe delayed list. If no segments on
  1851. * this xfer have been submitted, __wa_xfer_is_done will
  1852. * trigger a giveback below. Otherwise, the submitted
  1853. * segments will be completed in the DTI interrupt.
  1854. */
  1855. seg->status = WA_SEG_ABORTED;
  1856. seg->result = -ENOENT;
  1857. list_del(&seg->list_node);
  1858. xfer->segs_done++;
  1859. break;
  1860. case WA_SEG_DONE:
  1861. case WA_SEG_ERROR:
  1862. case WA_SEG_ABORTED:
  1863. break;
  1864. /*
  1865. * The buf_in data for a segment in the
  1866. * WA_SEG_DTI_PENDING state is actively being read.
  1867. * Let wa_buf_in_cb handle it since it will be called
  1868. * and will increment xfer->segs_done. Cleaning up
  1869. * here could cause wa_buf_in_cb to access the xfer
  1870. * after it has been completed/freed.
  1871. */
  1872. case WA_SEG_DTI_PENDING:
  1873. break;
  1874. /*
  1875. * In the states below, the HWA device already knows
  1876. * about the transfer. If an abort request was sent,
  1877. * allow the HWA to process it and wait for the
  1878. * results. Otherwise, the DTI state and seg completed
  1879. * counts can get out of sync.
  1880. */
  1881. case WA_SEG_SUBMITTED:
  1882. case WA_SEG_PENDING:
  1883. /*
  1884. * Check if the abort was successfully sent. This could
  1885. * be false if the HWA has been removed but we haven't
  1886. * gotten the disconnect notification yet.
  1887. */
  1888. if (!xfer_abort_pending) {
  1889. seg->status = WA_SEG_ABORTED;
  1890. rpipe_ready = rpipe_avail_inc(rpipe);
  1891. xfer->segs_done++;
  1892. }
  1893. break;
  1894. }
  1895. }
  1896. spin_unlock(&rpipe->seg_lock);
  1897. xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
  1898. done = __wa_xfer_is_done(xfer);
  1899. spin_unlock_irqrestore(&xfer->lock, flags);
  1900. if (done)
  1901. wa_xfer_completion(xfer);
  1902. if (rpipe_ready)
  1903. wa_xfer_delayed_run(rpipe);
  1904. wa_xfer_put(xfer);
  1905. return result;
  1906. out_unlock:
  1907. spin_unlock_irqrestore(&xfer->lock, flags);
  1908. wa_xfer_put(xfer);
  1909. return result;
  1910. dequeue_delayed:
  1911. list_del_init(&xfer->list_node);
  1912. spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
  1913. xfer->result = urb->status;
  1914. spin_unlock_irqrestore(&xfer->lock, flags);
  1915. wa_xfer_giveback(xfer);
  1916. wa_xfer_put(xfer);
  1917. usb_put_urb(urb); /* we got a ref in enqueue() */
  1918. return 0;
  1919. }
  1920. EXPORT_SYMBOL_GPL(wa_urb_dequeue);
  1921. /*
  1922. * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
  1923. * codes
  1924. *
  1925. * Positive errno values are internal inconsistencies and should be
  1926. * flagged louder. Negative are to be passed up to the user in the
  1927. * normal way.
  1928. *
  1929. * @status: USB WA status code -- high two bits are stripped.
  1930. */
  1931. static int wa_xfer_status_to_errno(u8 status)
  1932. {
  1933. int errno;
  1934. u8 real_status = status;
  1935. static int xlat[] = {
  1936. [WA_XFER_STATUS_SUCCESS] = 0,
  1937. [WA_XFER_STATUS_HALTED] = -EPIPE,
  1938. [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
  1939. [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
  1940. [WA_XFER_RESERVED] = EINVAL,
  1941. [WA_XFER_STATUS_NOT_FOUND] = 0,
  1942. [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
  1943. [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
  1944. [WA_XFER_STATUS_ABORTED] = -ENOENT,
  1945. [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
  1946. [WA_XFER_INVALID_FORMAT] = EINVAL,
  1947. [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
  1948. [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
  1949. };
  1950. status &= 0x3f;
  1951. if (status == 0)
  1952. return 0;
  1953. if (status >= ARRAY_SIZE(xlat)) {
  1954. printk_ratelimited(KERN_ERR "%s(): BUG? "
  1955. "Unknown WA transfer status 0x%02x\n",
  1956. __func__, real_status);
  1957. return -EINVAL;
  1958. }
  1959. errno = xlat[status];
  1960. if (unlikely(errno > 0)) {
  1961. printk_ratelimited(KERN_ERR "%s(): BUG? "
  1962. "Inconsistent WA status: 0x%02x\n",
  1963. __func__, real_status);
  1964. errno = -errno;
  1965. }
  1966. return errno;
  1967. }
  1968. /*
  1969. * If a last segment flag and/or a transfer result error is encountered,
  1970. * no other segment transfer results will be returned from the device.
  1971. * Mark the remaining submitted or pending xfers as completed so that
  1972. * the xfer will complete cleanly.
  1973. *
  1974. * xfer->lock must be held
  1975. *
  1976. */
  1977. static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
  1978. int starting_index, enum wa_seg_status status)
  1979. {
  1980. int index;
  1981. struct wa_rpipe *rpipe = xfer->ep->hcpriv;
  1982. for (index = starting_index; index < xfer->segs_submitted; index++) {
  1983. struct wa_seg *current_seg = xfer->seg[index];
  1984. BUG_ON(current_seg == NULL);
  1985. switch (current_seg->status) {
  1986. case WA_SEG_SUBMITTED:
  1987. case WA_SEG_PENDING:
  1988. case WA_SEG_DTI_PENDING:
  1989. rpipe_avail_inc(rpipe);
  1990. /*
  1991. * do not increment RPIPE avail for the WA_SEG_DELAYED case
  1992. * since it has not been submitted to the RPIPE.
  1993. */
  1994. case WA_SEG_DELAYED:
  1995. xfer->segs_done++;
  1996. current_seg->status = status;
  1997. break;
  1998. case WA_SEG_ABORTED:
  1999. break;
  2000. default:
  2001. WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
  2002. __func__, wa_xfer_id(xfer), index,
  2003. current_seg->status);
  2004. break;
  2005. }
  2006. }
  2007. }
  2008. /* Populate the given urb based on the current isoc transfer state. */
  2009. static int __wa_populate_buf_in_urb_isoc(struct wahc *wa,
  2010. struct urb *buf_in_urb, struct wa_xfer *xfer, struct wa_seg *seg)
  2011. {
  2012. int urb_start_frame = seg->isoc_frame_index + seg->isoc_frame_offset;
  2013. int seg_index, total_len = 0, urb_frame_index = urb_start_frame;
  2014. struct usb_iso_packet_descriptor *iso_frame_desc =
  2015. xfer->urb->iso_frame_desc;
  2016. const int dti_packet_size = usb_endpoint_maxp(wa->dti_epd);
  2017. int next_frame_contiguous;
  2018. struct usb_iso_packet_descriptor *iso_frame;
  2019. BUG_ON(buf_in_urb->status == -EINPROGRESS);
  2020. /*
  2021. * If the current frame actual_length is contiguous with the next frame
  2022. * and actual_length is a multiple of the DTI endpoint max packet size,
  2023. * combine the current frame with the next frame in a single URB. This
  2024. * reduces the number of URBs that must be submitted in that case.
  2025. */
  2026. seg_index = seg->isoc_frame_index;
  2027. do {
  2028. next_frame_contiguous = 0;
  2029. iso_frame = &iso_frame_desc[urb_frame_index];
  2030. total_len += iso_frame->actual_length;
  2031. ++urb_frame_index;
  2032. ++seg_index;
  2033. if (seg_index < seg->isoc_frame_count) {
  2034. struct usb_iso_packet_descriptor *next_iso_frame;
  2035. next_iso_frame = &iso_frame_desc[urb_frame_index];
  2036. if ((iso_frame->offset + iso_frame->actual_length) ==
  2037. next_iso_frame->offset)
  2038. next_frame_contiguous = 1;
  2039. }
  2040. } while (next_frame_contiguous
  2041. && ((iso_frame->actual_length % dti_packet_size) == 0));
  2042. /* this should always be 0 before a resubmit. */
  2043. buf_in_urb->num_mapped_sgs = 0;
  2044. buf_in_urb->transfer_dma = xfer->urb->transfer_dma +
  2045. iso_frame_desc[urb_start_frame].offset;
  2046. buf_in_urb->transfer_buffer_length = total_len;
  2047. buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  2048. buf_in_urb->transfer_buffer = NULL;
  2049. buf_in_urb->sg = NULL;
  2050. buf_in_urb->num_sgs = 0;
  2051. buf_in_urb->context = seg;
  2052. /* return the number of frames included in this URB. */
  2053. return seg_index - seg->isoc_frame_index;
  2054. }
  2055. /* Populate the given urb based on the current transfer state. */
  2056. static int wa_populate_buf_in_urb(struct urb *buf_in_urb, struct wa_xfer *xfer,
  2057. unsigned int seg_idx, unsigned int bytes_transferred)
  2058. {
  2059. int result = 0;
  2060. struct wa_seg *seg = xfer->seg[seg_idx];
  2061. BUG_ON(buf_in_urb->status == -EINPROGRESS);
  2062. /* this should always be 0 before a resubmit. */
  2063. buf_in_urb->num_mapped_sgs = 0;
  2064. if (xfer->is_dma) {
  2065. buf_in_urb->transfer_dma = xfer->urb->transfer_dma
  2066. + (seg_idx * xfer->seg_size);
  2067. buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  2068. buf_in_urb->transfer_buffer = NULL;
  2069. buf_in_urb->sg = NULL;
  2070. buf_in_urb->num_sgs = 0;
  2071. } else {
  2072. /* do buffer or SG processing. */
  2073. buf_in_urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
  2074. if (xfer->urb->transfer_buffer) {
  2075. buf_in_urb->transfer_buffer =
  2076. xfer->urb->transfer_buffer
  2077. + (seg_idx * xfer->seg_size);
  2078. buf_in_urb->sg = NULL;
  2079. buf_in_urb->num_sgs = 0;
  2080. } else {
  2081. /* allocate an SG list to store seg_size bytes
  2082. and copy the subset of the xfer->urb->sg
  2083. that matches the buffer subset we are
  2084. about to read. */
  2085. buf_in_urb->sg = wa_xfer_create_subset_sg(
  2086. xfer->urb->sg,
  2087. seg_idx * xfer->seg_size,
  2088. bytes_transferred,
  2089. &(buf_in_urb->num_sgs));
  2090. if (!(buf_in_urb->sg)) {
  2091. buf_in_urb->num_sgs = 0;
  2092. result = -ENOMEM;
  2093. }
  2094. buf_in_urb->transfer_buffer = NULL;
  2095. }
  2096. }
  2097. buf_in_urb->transfer_buffer_length = bytes_transferred;
  2098. buf_in_urb->context = seg;
  2099. return result;
  2100. }
  2101. /*
  2102. * Process a xfer result completion message
  2103. *
  2104. * inbound transfers: need to schedule a buf_in_urb read
  2105. *
  2106. * FIXME: this function needs to be broken up in parts
  2107. */
  2108. static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
  2109. struct wa_xfer_result *xfer_result)
  2110. {
  2111. int result;
  2112. struct device *dev = &wa->usb_iface->dev;
  2113. unsigned long flags;
  2114. unsigned int seg_idx;
  2115. struct wa_seg *seg;
  2116. struct wa_rpipe *rpipe;
  2117. unsigned done = 0;
  2118. u8 usb_status;
  2119. unsigned rpipe_ready = 0;
  2120. unsigned bytes_transferred = le32_to_cpu(xfer_result->dwTransferLength);
  2121. struct urb *buf_in_urb = &(wa->buf_in_urbs[0]);
  2122. spin_lock_irqsave(&xfer->lock, flags);
  2123. seg_idx = xfer_result->bTransferSegment & 0x7f;
  2124. if (unlikely(seg_idx >= xfer->segs))
  2125. goto error_bad_seg;
  2126. seg = xfer->seg[seg_idx];
  2127. rpipe = xfer->ep->hcpriv;
  2128. usb_status = xfer_result->bTransferStatus;
  2129. dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
  2130. xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
  2131. if (seg->status == WA_SEG_ABORTED
  2132. || seg->status == WA_SEG_ERROR) /* already handled */
  2133. goto segment_aborted;
  2134. if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
  2135. seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
  2136. if (seg->status != WA_SEG_PENDING) {
  2137. if (printk_ratelimit())
  2138. dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
  2139. xfer, seg_idx, seg->status);
  2140. seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
  2141. }
  2142. if (usb_status & 0x80) {
  2143. seg->result = wa_xfer_status_to_errno(usb_status);
  2144. dev_err(dev, "DTI: xfer %p 0x%08X:#%u failed (0x%02x)\n",
  2145. xfer, xfer->id, seg->index, usb_status);
  2146. seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
  2147. WA_SEG_ABORTED : WA_SEG_ERROR;
  2148. goto error_complete;
  2149. }
  2150. /* FIXME: we ignore warnings, tally them for stats */
  2151. if (usb_status & 0x40) /* Warning?... */
  2152. usb_status = 0; /* ... pass */
  2153. /*
  2154. * If the last segment bit is set, complete the remaining segments.
  2155. * When the current segment is completed, either in wa_buf_in_cb for
  2156. * transfers with data or below for no data, the xfer will complete.
  2157. */
  2158. if (xfer_result->bTransferSegment & 0x80)
  2159. wa_complete_remaining_xfer_segs(xfer, seg->index + 1,
  2160. WA_SEG_DONE);
  2161. if (usb_pipeisoc(xfer->urb->pipe)
  2162. && (le32_to_cpu(xfer_result->dwNumOfPackets) > 0)) {
  2163. /* set up WA state to read the isoc packet status next. */
  2164. wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
  2165. wa->dti_isoc_xfer_seg = seg_idx;
  2166. wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
  2167. } else if (xfer->is_inbound && !usb_pipeisoc(xfer->urb->pipe)
  2168. && (bytes_transferred > 0)) {
  2169. /* IN data phase: read to buffer */
  2170. seg->status = WA_SEG_DTI_PENDING;
  2171. result = wa_populate_buf_in_urb(buf_in_urb, xfer, seg_idx,
  2172. bytes_transferred);
  2173. if (result < 0)
  2174. goto error_buf_in_populate;
  2175. ++(wa->active_buf_in_urbs);
  2176. result = usb_submit_urb(buf_in_urb, GFP_ATOMIC);
  2177. if (result < 0) {
  2178. --(wa->active_buf_in_urbs);
  2179. goto error_submit_buf_in;
  2180. }
  2181. } else {
  2182. /* OUT data phase or no data, complete it -- */
  2183. seg->result = bytes_transferred;
  2184. rpipe_ready = rpipe_avail_inc(rpipe);
  2185. done = __wa_xfer_mark_seg_as_done(xfer, seg, WA_SEG_DONE);
  2186. }
  2187. spin_unlock_irqrestore(&xfer->lock, flags);
  2188. if (done)
  2189. wa_xfer_completion(xfer);
  2190. if (rpipe_ready)
  2191. wa_xfer_delayed_run(rpipe);
  2192. return;
  2193. error_submit_buf_in:
  2194. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  2195. dev_err(dev, "DTI: URB max acceptable errors "
  2196. "exceeded, resetting device\n");
  2197. wa_reset_all(wa);
  2198. }
  2199. if (printk_ratelimit())
  2200. dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
  2201. xfer, seg_idx, result);
  2202. seg->result = result;
  2203. kfree(buf_in_urb->sg);
  2204. buf_in_urb->sg = NULL;
  2205. error_buf_in_populate:
  2206. __wa_xfer_abort(xfer);
  2207. seg->status = WA_SEG_ERROR;
  2208. error_complete:
  2209. xfer->segs_done++;
  2210. rpipe_ready = rpipe_avail_inc(rpipe);
  2211. wa_complete_remaining_xfer_segs(xfer, seg->index + 1, seg->status);
  2212. done = __wa_xfer_is_done(xfer);
  2213. /*
  2214. * queue work item to clear STALL for control endpoints.
  2215. * Otherwise, let endpoint_reset take care of it.
  2216. */
  2217. if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
  2218. usb_endpoint_xfer_control(&xfer->ep->desc) &&
  2219. done) {
  2220. dev_info(dev, "Control EP stall. Queue delayed work.\n");
  2221. spin_lock(&wa->xfer_list_lock);
  2222. /* move xfer from xfer_list to xfer_errored_list. */
  2223. list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
  2224. spin_unlock(&wa->xfer_list_lock);
  2225. spin_unlock_irqrestore(&xfer->lock, flags);
  2226. queue_work(wusbd, &wa->xfer_error_work);
  2227. } else {
  2228. spin_unlock_irqrestore(&xfer->lock, flags);
  2229. if (done)
  2230. wa_xfer_completion(xfer);
  2231. if (rpipe_ready)
  2232. wa_xfer_delayed_run(rpipe);
  2233. }
  2234. return;
  2235. error_bad_seg:
  2236. spin_unlock_irqrestore(&xfer->lock, flags);
  2237. wa_urb_dequeue(wa, xfer->urb, -ENOENT);
  2238. if (printk_ratelimit())
  2239. dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
  2240. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  2241. dev_err(dev, "DTI: URB max acceptable errors "
  2242. "exceeded, resetting device\n");
  2243. wa_reset_all(wa);
  2244. }
  2245. return;
  2246. segment_aborted:
  2247. /* nothing to do, as the aborter did the completion */
  2248. spin_unlock_irqrestore(&xfer->lock, flags);
  2249. }
  2250. /*
  2251. * Process a isochronous packet status message
  2252. *
  2253. * inbound transfers: need to schedule a buf_in_urb read
  2254. */
  2255. static int wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
  2256. {
  2257. struct device *dev = &wa->usb_iface->dev;
  2258. struct wa_xfer_packet_status_hwaiso *packet_status;
  2259. struct wa_xfer_packet_status_len_hwaiso *status_array;
  2260. struct wa_xfer *xfer;
  2261. unsigned long flags;
  2262. struct wa_seg *seg;
  2263. struct wa_rpipe *rpipe;
  2264. unsigned done = 0, dti_busy = 0, data_frame_count = 0, seg_index;
  2265. unsigned first_frame_index = 0, rpipe_ready = 0;
  2266. int expected_size;
  2267. /* We have a xfer result buffer; check it */
  2268. dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
  2269. urb->actual_length, urb->transfer_buffer);
  2270. packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
  2271. if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
  2272. dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
  2273. packet_status->bPacketType);
  2274. goto error_parse_buffer;
  2275. }
  2276. xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
  2277. if (xfer == NULL) {
  2278. dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
  2279. wa->dti_isoc_xfer_in_progress);
  2280. goto error_parse_buffer;
  2281. }
  2282. spin_lock_irqsave(&xfer->lock, flags);
  2283. if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
  2284. goto error_bad_seg;
  2285. seg = xfer->seg[wa->dti_isoc_xfer_seg];
  2286. rpipe = xfer->ep->hcpriv;
  2287. expected_size = sizeof(*packet_status) +
  2288. (sizeof(packet_status->PacketStatus[0]) *
  2289. seg->isoc_frame_count);
  2290. if (urb->actual_length != expected_size) {
  2291. dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
  2292. urb->actual_length, expected_size);
  2293. goto error_bad_seg;
  2294. }
  2295. if (le16_to_cpu(packet_status->wLength) != expected_size) {
  2296. dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
  2297. le16_to_cpu(packet_status->wLength));
  2298. goto error_bad_seg;
  2299. }
  2300. /* write isoc packet status and lengths back to the xfer urb. */
  2301. status_array = packet_status->PacketStatus;
  2302. xfer->urb->start_frame =
  2303. wa->wusb->usb_hcd.driver->get_frame_number(&wa->wusb->usb_hcd);
  2304. for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
  2305. struct usb_iso_packet_descriptor *iso_frame_desc =
  2306. xfer->urb->iso_frame_desc;
  2307. const int xfer_frame_index =
  2308. seg->isoc_frame_offset + seg_index;
  2309. iso_frame_desc[xfer_frame_index].status =
  2310. wa_xfer_status_to_errno(
  2311. le16_to_cpu(status_array[seg_index].PacketStatus));
  2312. iso_frame_desc[xfer_frame_index].actual_length =
  2313. le16_to_cpu(status_array[seg_index].PacketLength);
  2314. /* track the number of frames successfully transferred. */
  2315. if (iso_frame_desc[xfer_frame_index].actual_length > 0) {
  2316. /* save the starting frame index for buf_in_urb. */
  2317. if (!data_frame_count)
  2318. first_frame_index = seg_index;
  2319. ++data_frame_count;
  2320. }
  2321. }
  2322. if (xfer->is_inbound && data_frame_count) {
  2323. int result, total_frames_read = 0, urb_index = 0;
  2324. struct urb *buf_in_urb;
  2325. /* IN data phase: read to buffer */
  2326. seg->status = WA_SEG_DTI_PENDING;
  2327. /* start with the first frame with data. */
  2328. seg->isoc_frame_index = first_frame_index;
  2329. /* submit up to WA_MAX_BUF_IN_URBS read URBs. */
  2330. do {
  2331. int urb_frame_index, urb_frame_count;
  2332. struct usb_iso_packet_descriptor *iso_frame_desc;
  2333. buf_in_urb = &(wa->buf_in_urbs[urb_index]);
  2334. urb_frame_count = __wa_populate_buf_in_urb_isoc(wa,
  2335. buf_in_urb, xfer, seg);
  2336. /* advance frame index to start of next read URB. */
  2337. seg->isoc_frame_index += urb_frame_count;
  2338. total_frames_read += urb_frame_count;
  2339. ++(wa->active_buf_in_urbs);
  2340. result = usb_submit_urb(buf_in_urb, GFP_ATOMIC);
  2341. /* skip 0-byte frames. */
  2342. urb_frame_index =
  2343. seg->isoc_frame_offset + seg->isoc_frame_index;
  2344. iso_frame_desc =
  2345. &(xfer->urb->iso_frame_desc[urb_frame_index]);
  2346. while ((seg->isoc_frame_index <
  2347. seg->isoc_frame_count) &&
  2348. (iso_frame_desc->actual_length == 0)) {
  2349. ++(seg->isoc_frame_index);
  2350. ++iso_frame_desc;
  2351. }
  2352. ++urb_index;
  2353. } while ((result == 0) && (urb_index < WA_MAX_BUF_IN_URBS)
  2354. && (seg->isoc_frame_index <
  2355. seg->isoc_frame_count));
  2356. if (result < 0) {
  2357. --(wa->active_buf_in_urbs);
  2358. dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
  2359. result);
  2360. wa_reset_all(wa);
  2361. } else if (data_frame_count > total_frames_read)
  2362. /* If we need to read more frames, set DTI busy. */
  2363. dti_busy = 1;
  2364. } else {
  2365. /* OUT transfer or no more IN data, complete it -- */
  2366. rpipe_ready = rpipe_avail_inc(rpipe);
  2367. done = __wa_xfer_mark_seg_as_done(xfer, seg, WA_SEG_DONE);
  2368. }
  2369. spin_unlock_irqrestore(&xfer->lock, flags);
  2370. if (dti_busy)
  2371. wa->dti_state = WA_DTI_BUF_IN_DATA_PENDING;
  2372. else
  2373. wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
  2374. if (done)
  2375. wa_xfer_completion(xfer);
  2376. if (rpipe_ready)
  2377. wa_xfer_delayed_run(rpipe);
  2378. wa_xfer_put(xfer);
  2379. return dti_busy;
  2380. error_bad_seg:
  2381. spin_unlock_irqrestore(&xfer->lock, flags);
  2382. wa_xfer_put(xfer);
  2383. error_parse_buffer:
  2384. return dti_busy;
  2385. }
  2386. /*
  2387. * Callback for the IN data phase
  2388. *
  2389. * If successful transition state; otherwise, take a note of the
  2390. * error, mark this segment done and try completion.
  2391. *
  2392. * Note we don't access until we are sure that the transfer hasn't
  2393. * been cancelled (ECONNRESET, ENOENT), which could mean that
  2394. * seg->xfer could be already gone.
  2395. */
  2396. static void wa_buf_in_cb(struct urb *urb)
  2397. {
  2398. struct wa_seg *seg = urb->context;
  2399. struct wa_xfer *xfer = seg->xfer;
  2400. struct wahc *wa;
  2401. struct device *dev;
  2402. struct wa_rpipe *rpipe;
  2403. unsigned rpipe_ready = 0, isoc_data_frame_count = 0;
  2404. unsigned long flags;
  2405. int resubmit_dti = 0, active_buf_in_urbs;
  2406. u8 done = 0;
  2407. /* free the sg if it was used. */
  2408. kfree(urb->sg);
  2409. urb->sg = NULL;
  2410. spin_lock_irqsave(&xfer->lock, flags);
  2411. wa = xfer->wa;
  2412. dev = &wa->usb_iface->dev;
  2413. --(wa->active_buf_in_urbs);
  2414. active_buf_in_urbs = wa->active_buf_in_urbs;
  2415. rpipe = xfer->ep->hcpriv;
  2416. if (usb_pipeisoc(xfer->urb->pipe)) {
  2417. struct usb_iso_packet_descriptor *iso_frame_desc =
  2418. xfer->urb->iso_frame_desc;
  2419. int seg_index;
  2420. /*
  2421. * Find the next isoc frame with data and count how many
  2422. * frames with data remain.
  2423. */
  2424. seg_index = seg->isoc_frame_index;
  2425. while (seg_index < seg->isoc_frame_count) {
  2426. const int urb_frame_index =
  2427. seg->isoc_frame_offset + seg_index;
  2428. if (iso_frame_desc[urb_frame_index].actual_length > 0) {
  2429. /* save the index of the next frame with data */
  2430. if (!isoc_data_frame_count)
  2431. seg->isoc_frame_index = seg_index;
  2432. ++isoc_data_frame_count;
  2433. }
  2434. ++seg_index;
  2435. }
  2436. }
  2437. spin_unlock_irqrestore(&xfer->lock, flags);
  2438. switch (urb->status) {
  2439. case 0:
  2440. spin_lock_irqsave(&xfer->lock, flags);
  2441. seg->result += urb->actual_length;
  2442. if (isoc_data_frame_count > 0) {
  2443. int result, urb_frame_count;
  2444. /* submit a read URB for the next frame with data. */
  2445. urb_frame_count = __wa_populate_buf_in_urb_isoc(wa, urb,
  2446. xfer, seg);
  2447. /* advance index to start of next read URB. */
  2448. seg->isoc_frame_index += urb_frame_count;
  2449. ++(wa->active_buf_in_urbs);
  2450. result = usb_submit_urb(urb, GFP_ATOMIC);
  2451. if (result < 0) {
  2452. --(wa->active_buf_in_urbs);
  2453. dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
  2454. result);
  2455. wa_reset_all(wa);
  2456. }
  2457. /*
  2458. * If we are in this callback and
  2459. * isoc_data_frame_count > 0, it means that the dti_urb
  2460. * submission was delayed in wa_dti_cb. Once
  2461. * we submit the last buf_in_urb, we can submit the
  2462. * delayed dti_urb.
  2463. */
  2464. resubmit_dti = (isoc_data_frame_count ==
  2465. urb_frame_count);
  2466. } else if (active_buf_in_urbs == 0) {
  2467. dev_dbg(dev,
  2468. "xfer %p 0x%08X#%u: data in done (%zu bytes)\n",
  2469. xfer, wa_xfer_id(xfer), seg->index,
  2470. seg->result);
  2471. rpipe_ready = rpipe_avail_inc(rpipe);
  2472. done = __wa_xfer_mark_seg_as_done(xfer, seg,
  2473. WA_SEG_DONE);
  2474. }
  2475. spin_unlock_irqrestore(&xfer->lock, flags);
  2476. if (done)
  2477. wa_xfer_completion(xfer);
  2478. if (rpipe_ready)
  2479. wa_xfer_delayed_run(rpipe);
  2480. break;
  2481. case -ECONNRESET: /* URB unlinked; no need to do anything */
  2482. case -ENOENT: /* as it was done by the who unlinked us */
  2483. break;
  2484. default: /* Other errors ... */
  2485. /*
  2486. * Error on data buf read. Only resubmit DTI if it hasn't
  2487. * already been done by previously hitting this error or by a
  2488. * successful completion of the previous buf_in_urb.
  2489. */
  2490. resubmit_dti = wa->dti_state != WA_DTI_TRANSFER_RESULT_PENDING;
  2491. spin_lock_irqsave(&xfer->lock, flags);
  2492. if (printk_ratelimit())
  2493. dev_err(dev, "xfer %p 0x%08X#%u: data in error %d\n",
  2494. xfer, wa_xfer_id(xfer), seg->index,
  2495. urb->status);
  2496. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  2497. EDC_ERROR_TIMEFRAME)){
  2498. dev_err(dev, "DTO: URB max acceptable errors "
  2499. "exceeded, resetting device\n");
  2500. wa_reset_all(wa);
  2501. }
  2502. seg->result = urb->status;
  2503. rpipe_ready = rpipe_avail_inc(rpipe);
  2504. if (active_buf_in_urbs == 0)
  2505. done = __wa_xfer_mark_seg_as_done(xfer, seg,
  2506. WA_SEG_ERROR);
  2507. else
  2508. __wa_xfer_abort(xfer);
  2509. spin_unlock_irqrestore(&xfer->lock, flags);
  2510. if (done)
  2511. wa_xfer_completion(xfer);
  2512. if (rpipe_ready)
  2513. wa_xfer_delayed_run(rpipe);
  2514. }
  2515. if (resubmit_dti) {
  2516. int result;
  2517. wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
  2518. result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
  2519. if (result < 0) {
  2520. dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
  2521. result);
  2522. wa_reset_all(wa);
  2523. }
  2524. }
  2525. }
  2526. /*
  2527. * Handle an incoming transfer result buffer
  2528. *
  2529. * Given a transfer result buffer, it completes the transfer (possibly
  2530. * scheduling and buffer in read) and then resubmits the DTI URB for a
  2531. * new transfer result read.
  2532. *
  2533. *
  2534. * The xfer_result DTI URB state machine
  2535. *
  2536. * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
  2537. *
  2538. * We start in OFF mode, the first xfer_result notification [through
  2539. * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
  2540. * read.
  2541. *
  2542. * We receive a buffer -- if it is not a xfer_result, we complain and
  2543. * repost the DTI-URB. If it is a xfer_result then do the xfer seg
  2544. * request accounting. If it is an IN segment, we move to RBI and post
  2545. * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
  2546. * repost the DTI-URB and move to RXR state. if there was no IN
  2547. * segment, it will repost the DTI-URB.
  2548. *
  2549. * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
  2550. * errors) in the URBs.
  2551. */
  2552. static void wa_dti_cb(struct urb *urb)
  2553. {
  2554. int result, dti_busy = 0;
  2555. struct wahc *wa = urb->context;
  2556. struct device *dev = &wa->usb_iface->dev;
  2557. u32 xfer_id;
  2558. u8 usb_status;
  2559. BUG_ON(wa->dti_urb != urb);
  2560. switch (wa->dti_urb->status) {
  2561. case 0:
  2562. if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
  2563. struct wa_xfer_result *xfer_result;
  2564. struct wa_xfer *xfer;
  2565. /* We have a xfer result buffer; check it */
  2566. dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
  2567. urb->actual_length, urb->transfer_buffer);
  2568. if (urb->actual_length != sizeof(*xfer_result)) {
  2569. dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
  2570. urb->actual_length,
  2571. sizeof(*xfer_result));
  2572. break;
  2573. }
  2574. xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
  2575. if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
  2576. dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
  2577. xfer_result->hdr.bLength);
  2578. break;
  2579. }
  2580. if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
  2581. dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
  2582. xfer_result->hdr.bNotifyType);
  2583. break;
  2584. }
  2585. xfer_id = le32_to_cpu(xfer_result->dwTransferID);
  2586. usb_status = xfer_result->bTransferStatus & 0x3f;
  2587. if (usb_status == WA_XFER_STATUS_NOT_FOUND) {
  2588. /* taken care of already */
  2589. dev_dbg(dev, "%s: xfer 0x%08X#%u not found.\n",
  2590. __func__, xfer_id,
  2591. xfer_result->bTransferSegment & 0x7f);
  2592. break;
  2593. }
  2594. xfer = wa_xfer_get_by_id(wa, xfer_id);
  2595. if (xfer == NULL) {
  2596. /* FIXME: transaction not found. */
  2597. dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
  2598. xfer_id, usb_status);
  2599. break;
  2600. }
  2601. wa_xfer_result_chew(wa, xfer, xfer_result);
  2602. wa_xfer_put(xfer);
  2603. } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
  2604. dti_busy = wa_process_iso_packet_status(wa, urb);
  2605. } else {
  2606. dev_err(dev, "DTI Error: unexpected EP state = %d\n",
  2607. wa->dti_state);
  2608. }
  2609. break;
  2610. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  2611. case -ESHUTDOWN: /* going away! */
  2612. dev_dbg(dev, "DTI: going down! %d\n", urb->status);
  2613. goto out;
  2614. default:
  2615. /* Unknown error */
  2616. if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
  2617. EDC_ERROR_TIMEFRAME)) {
  2618. dev_err(dev, "DTI: URB max acceptable errors "
  2619. "exceeded, resetting device\n");
  2620. wa_reset_all(wa);
  2621. goto out;
  2622. }
  2623. if (printk_ratelimit())
  2624. dev_err(dev, "DTI: URB error %d\n", urb->status);
  2625. break;
  2626. }
  2627. /* Resubmit the DTI URB if we are not busy processing isoc in frames. */
  2628. if (!dti_busy) {
  2629. result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
  2630. if (result < 0) {
  2631. dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
  2632. result);
  2633. wa_reset_all(wa);
  2634. }
  2635. }
  2636. out:
  2637. return;
  2638. }
  2639. /*
  2640. * Initialize the DTI URB for reading transfer result notifications and also
  2641. * the buffer-in URB, for reading buffers. Then we just submit the DTI URB.
  2642. */
  2643. int wa_dti_start(struct wahc *wa)
  2644. {
  2645. const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
  2646. struct device *dev = &wa->usb_iface->dev;
  2647. int result = -ENOMEM, index;
  2648. if (wa->dti_urb != NULL) /* DTI URB already started */
  2649. goto out;
  2650. wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
  2651. if (wa->dti_urb == NULL)
  2652. goto error_dti_urb_alloc;
  2653. usb_fill_bulk_urb(
  2654. wa->dti_urb, wa->usb_dev,
  2655. usb_rcvbulkpipe(wa->usb_dev, 0x80 | dti_epd->bEndpointAddress),
  2656. wa->dti_buf, wa->dti_buf_size,
  2657. wa_dti_cb, wa);
  2658. /* init the buf in URBs */
  2659. for (index = 0; index < WA_MAX_BUF_IN_URBS; ++index) {
  2660. usb_fill_bulk_urb(
  2661. &(wa->buf_in_urbs[index]), wa->usb_dev,
  2662. usb_rcvbulkpipe(wa->usb_dev,
  2663. 0x80 | dti_epd->bEndpointAddress),
  2664. NULL, 0, wa_buf_in_cb, wa);
  2665. }
  2666. result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
  2667. if (result < 0) {
  2668. dev_err(dev, "DTI Error: Could not submit DTI URB (%d) resetting\n",
  2669. result);
  2670. goto error_dti_urb_submit;
  2671. }
  2672. out:
  2673. return 0;
  2674. error_dti_urb_submit:
  2675. usb_put_urb(wa->dti_urb);
  2676. wa->dti_urb = NULL;
  2677. error_dti_urb_alloc:
  2678. return result;
  2679. }
  2680. EXPORT_SYMBOL_GPL(wa_dti_start);
  2681. /*
  2682. * Transfer complete notification
  2683. *
  2684. * Called from the notif.c code. We get a notification on EP2 saying
  2685. * that some endpoint has some transfer result data available. We are
  2686. * about to read it.
  2687. *
  2688. * To speed up things, we always have a URB reading the DTI URB; we
  2689. * don't really set it up and start it until the first xfer complete
  2690. * notification arrives, which is what we do here.
  2691. *
  2692. * Follow up in wa_dti_cb(), as that's where the whole state
  2693. * machine starts.
  2694. *
  2695. * @wa shall be referenced
  2696. */
  2697. void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
  2698. {
  2699. struct device *dev = &wa->usb_iface->dev;
  2700. struct wa_notif_xfer *notif_xfer;
  2701. const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
  2702. notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
  2703. BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
  2704. if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
  2705. /* FIXME: hardcoded limitation, adapt */
  2706. dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
  2707. notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
  2708. goto error;
  2709. }
  2710. /* attempt to start the DTI ep processing. */
  2711. if (wa_dti_start(wa) < 0)
  2712. goto error;
  2713. return;
  2714. error:
  2715. wa_reset_all(wa);
  2716. }