wa-xfer.c 46 KB

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