wa-xfer.c 56 KB

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