wa-xfer.c 65 KB

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