wa-xfer.c 74 KB

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