wa-xfer.c 51 KB

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