xillyusb.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2020 Xillybus Ltd, http://xillybus.com
  4. *
  5. * Driver for the XillyUSB FPGA/host framework.
  6. *
  7. * This driver interfaces with a special IP core in an FPGA, setting up
  8. * a pipe between a hardware FIFO in the programmable logic and a device
  9. * file in the host. The number of such pipes and their attributes are
  10. * set up on the logic. This driver detects these automatically and
  11. * creates the device files accordingly.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <linux/device.h>
  17. #include <linux/module.h>
  18. #include <asm/byteorder.h>
  19. #include <linux/io.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/sched.h>
  22. #include <linux/fs.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/crc32.h>
  27. #include <linux/poll.h>
  28. #include <linux/delay.h>
  29. #include <linux/usb.h>
  30. #include "xillybus_class.h"
  31. MODULE_DESCRIPTION("Driver for XillyUSB FPGA IP Core");
  32. MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
  33. MODULE_ALIAS("xillyusb");
  34. MODULE_LICENSE("GPL v2");
  35. #define XILLY_RX_TIMEOUT (10 * HZ / 1000)
  36. #define XILLY_RESPONSE_TIMEOUT (500 * HZ / 1000)
  37. #define BUF_SIZE_ORDER 4
  38. #define BUFNUM 8
  39. #define LOG2_IDT_FIFO_SIZE 16
  40. #define LOG2_INITIAL_FIFO_BUF_SIZE 16
  41. #define MSG_EP_NUM 1
  42. #define IN_EP_NUM 1
  43. static const char xillyname[] = "xillyusb";
  44. static unsigned int fifo_buf_order;
  45. static struct workqueue_struct *wakeup_wq;
  46. #define USB_VENDOR_ID_XILINX 0x03fd
  47. #define USB_VENDOR_ID_ALTERA 0x09fb
  48. #define USB_PRODUCT_ID_XILLYUSB 0xebbe
  49. static const struct usb_device_id xillyusb_table[] = {
  50. { USB_DEVICE(USB_VENDOR_ID_XILINX, USB_PRODUCT_ID_XILLYUSB) },
  51. { USB_DEVICE(USB_VENDOR_ID_ALTERA, USB_PRODUCT_ID_XILLYUSB) },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(usb, xillyusb_table);
  55. struct xillyusb_dev;
  56. struct xillyfifo {
  57. unsigned int bufsize; /* In bytes, always a power of 2 */
  58. unsigned int bufnum;
  59. unsigned int size; /* Lazy: Equals bufsize * bufnum */
  60. unsigned int buf_order;
  61. int fill; /* Number of bytes in the FIFO */
  62. spinlock_t lock;
  63. wait_queue_head_t waitq;
  64. unsigned int readpos;
  65. unsigned int readbuf;
  66. unsigned int writepos;
  67. unsigned int writebuf;
  68. char **mem;
  69. };
  70. struct xillyusb_channel;
  71. struct xillyusb_endpoint {
  72. struct xillyusb_dev *xdev;
  73. struct mutex ep_mutex; /* serialize operations on endpoint */
  74. struct list_head buffers;
  75. struct list_head filled_buffers;
  76. spinlock_t buffers_lock; /* protect these two lists */
  77. unsigned int order;
  78. unsigned int buffer_size;
  79. unsigned int fill_mask;
  80. int outstanding_urbs;
  81. struct usb_anchor anchor;
  82. struct xillyfifo fifo;
  83. struct work_struct workitem;
  84. bool shutting_down;
  85. bool drained;
  86. bool wake_on_drain;
  87. u8 ep_num;
  88. };
  89. struct xillyusb_channel {
  90. struct xillyusb_dev *xdev;
  91. struct xillyfifo *in_fifo;
  92. struct xillyusb_endpoint *out_ep;
  93. struct mutex lock; /* protect @out_ep, @in_fifo, bit fields below */
  94. struct mutex in_mutex; /* serialize fops on FPGA to host stream */
  95. struct mutex out_mutex; /* serialize fops on host to FPGA stream */
  96. wait_queue_head_t flushq;
  97. int chan_idx;
  98. u32 in_consumed_bytes;
  99. u32 in_current_checkpoint;
  100. u32 out_bytes;
  101. unsigned int in_log2_element_size;
  102. unsigned int out_log2_element_size;
  103. unsigned int in_log2_fifo_size;
  104. unsigned int out_log2_fifo_size;
  105. unsigned int read_data_ok; /* EOF not arrived (yet) */
  106. unsigned int poll_used;
  107. unsigned int flushing;
  108. unsigned int flushed;
  109. unsigned int canceled;
  110. /* Bit fields protected by @lock except for initialization */
  111. unsigned readable:1;
  112. unsigned writable:1;
  113. unsigned open_for_read:1;
  114. unsigned open_for_write:1;
  115. unsigned in_synchronous:1;
  116. unsigned out_synchronous:1;
  117. unsigned in_seekable:1;
  118. unsigned out_seekable:1;
  119. };
  120. struct xillybuffer {
  121. struct list_head entry;
  122. struct xillyusb_endpoint *ep;
  123. void *buf;
  124. unsigned int len;
  125. };
  126. struct xillyusb_dev {
  127. struct xillyusb_channel *channels;
  128. struct usb_device *udev;
  129. struct device *dev; /* For dev_err() and such */
  130. struct kref kref;
  131. struct workqueue_struct *workq;
  132. int error;
  133. spinlock_t error_lock; /* protect @error */
  134. struct work_struct wakeup_workitem;
  135. int num_channels;
  136. struct xillyusb_endpoint *msg_ep;
  137. struct xillyusb_endpoint *in_ep;
  138. struct mutex msg_mutex; /* serialize opcode transmission */
  139. int in_bytes_left;
  140. int leftover_chan_num;
  141. unsigned int in_counter;
  142. struct mutex process_in_mutex; /* synchronize wakeup_all() */
  143. };
  144. /*
  145. * kref_mutex is used in xillyusb_open() to prevent the xillyusb_dev
  146. * struct from being freed during the gap between being found by
  147. * xillybus_find_inode() and having its reference count incremented.
  148. */
  149. static DEFINE_MUTEX(kref_mutex);
  150. /* FPGA to host opcodes */
  151. enum {
  152. OPCODE_DATA = 0,
  153. OPCODE_QUIESCE_ACK = 1,
  154. OPCODE_EOF = 2,
  155. OPCODE_REACHED_CHECKPOINT = 3,
  156. OPCODE_CANCELED_CHECKPOINT = 4,
  157. };
  158. /* Host to FPGA opcodes */
  159. enum {
  160. OPCODE_QUIESCE = 0,
  161. OPCODE_REQ_IDT = 1,
  162. OPCODE_SET_CHECKPOINT = 2,
  163. OPCODE_CLOSE = 3,
  164. OPCODE_SET_PUSH = 4,
  165. OPCODE_UPDATE_PUSH = 5,
  166. OPCODE_CANCEL_CHECKPOINT = 6,
  167. OPCODE_SET_ADDR = 7,
  168. };
  169. /*
  170. * fifo_write() and fifo_read() are NOT reentrant (i.e. concurrent multiple
  171. * calls to each on the same FIFO is not allowed) however it's OK to have
  172. * threads calling each of the two functions once on the same FIFO, and
  173. * at the same time.
  174. */
  175. static int fifo_write(struct xillyfifo *fifo,
  176. const void *data, unsigned int len,
  177. int (*copier)(void *, const void *, int))
  178. {
  179. unsigned int done = 0;
  180. unsigned int todo = len;
  181. unsigned int nmax;
  182. unsigned int writepos = fifo->writepos;
  183. unsigned int writebuf = fifo->writebuf;
  184. unsigned long flags;
  185. int rc;
  186. nmax = fifo->size - READ_ONCE(fifo->fill);
  187. while (1) {
  188. unsigned int nrail = fifo->bufsize - writepos;
  189. unsigned int n = min(todo, nmax);
  190. if (n == 0) {
  191. spin_lock_irqsave(&fifo->lock, flags);
  192. fifo->fill += done;
  193. spin_unlock_irqrestore(&fifo->lock, flags);
  194. fifo->writepos = writepos;
  195. fifo->writebuf = writebuf;
  196. return done;
  197. }
  198. if (n > nrail)
  199. n = nrail;
  200. rc = (*copier)(fifo->mem[writebuf] + writepos, data + done, n);
  201. if (rc)
  202. return rc;
  203. done += n;
  204. todo -= n;
  205. writepos += n;
  206. nmax -= n;
  207. if (writepos == fifo->bufsize) {
  208. writepos = 0;
  209. writebuf++;
  210. if (writebuf == fifo->bufnum)
  211. writebuf = 0;
  212. }
  213. }
  214. }
  215. static int fifo_read(struct xillyfifo *fifo,
  216. void *data, unsigned int len,
  217. int (*copier)(void *, const void *, int))
  218. {
  219. unsigned int done = 0;
  220. unsigned int todo = len;
  221. unsigned int fill;
  222. unsigned int readpos = fifo->readpos;
  223. unsigned int readbuf = fifo->readbuf;
  224. unsigned long flags;
  225. int rc;
  226. /*
  227. * The spinlock here is necessary, because otherwise fifo->fill
  228. * could have been increased by fifo_write() after writing data
  229. * to the buffer, but this data would potentially not have been
  230. * visible on this thread at the time the updated fifo->fill was.
  231. * That could lead to reading invalid data.
  232. */
  233. spin_lock_irqsave(&fifo->lock, flags);
  234. fill = fifo->fill;
  235. spin_unlock_irqrestore(&fifo->lock, flags);
  236. while (1) {
  237. unsigned int nrail = fifo->bufsize - readpos;
  238. unsigned int n = min(todo, fill);
  239. if (n == 0) {
  240. spin_lock_irqsave(&fifo->lock, flags);
  241. fifo->fill -= done;
  242. spin_unlock_irqrestore(&fifo->lock, flags);
  243. fifo->readpos = readpos;
  244. fifo->readbuf = readbuf;
  245. return done;
  246. }
  247. if (n > nrail)
  248. n = nrail;
  249. rc = (*copier)(data + done, fifo->mem[readbuf] + readpos, n);
  250. if (rc)
  251. return rc;
  252. done += n;
  253. todo -= n;
  254. readpos += n;
  255. fill -= n;
  256. if (readpos == fifo->bufsize) {
  257. readpos = 0;
  258. readbuf++;
  259. if (readbuf == fifo->bufnum)
  260. readbuf = 0;
  261. }
  262. }
  263. }
  264. /*
  265. * These three wrapper functions are used as the @copier argument to
  266. * fifo_write() and fifo_read(), so that they can work directly with
  267. * user memory as well.
  268. */
  269. static int xilly_copy_from_user(void *dst, const void *src, int n)
  270. {
  271. if (copy_from_user(dst, (const void __user *)src, n))
  272. return -EFAULT;
  273. return 0;
  274. }
  275. static int xilly_copy_to_user(void *dst, const void *src, int n)
  276. {
  277. if (copy_to_user((void __user *)dst, src, n))
  278. return -EFAULT;
  279. return 0;
  280. }
  281. static int xilly_memcpy(void *dst, const void *src, int n)
  282. {
  283. memcpy(dst, src, n);
  284. return 0;
  285. }
  286. static int fifo_init(struct xillyfifo *fifo,
  287. unsigned int log2_size)
  288. {
  289. unsigned int log2_bufnum;
  290. unsigned int buf_order;
  291. int i;
  292. unsigned int log2_fifo_buf_size;
  293. retry:
  294. log2_fifo_buf_size = fifo_buf_order + PAGE_SHIFT;
  295. if (log2_size > log2_fifo_buf_size) {
  296. log2_bufnum = log2_size - log2_fifo_buf_size;
  297. buf_order = fifo_buf_order;
  298. fifo->bufsize = 1 << log2_fifo_buf_size;
  299. } else {
  300. log2_bufnum = 0;
  301. buf_order = (log2_size > PAGE_SHIFT) ?
  302. log2_size - PAGE_SHIFT : 0;
  303. fifo->bufsize = 1 << log2_size;
  304. }
  305. fifo->bufnum = 1 << log2_bufnum;
  306. fifo->size = fifo->bufnum * fifo->bufsize;
  307. fifo->buf_order = buf_order;
  308. fifo->mem = kmalloc_array(fifo->bufnum, sizeof(void *), GFP_KERNEL);
  309. if (!fifo->mem)
  310. return -ENOMEM;
  311. for (i = 0; i < fifo->bufnum; i++) {
  312. fifo->mem[i] = (void *)
  313. __get_free_pages(GFP_KERNEL, buf_order);
  314. if (!fifo->mem[i])
  315. goto memfail;
  316. }
  317. fifo->fill = 0;
  318. fifo->readpos = 0;
  319. fifo->readbuf = 0;
  320. fifo->writepos = 0;
  321. fifo->writebuf = 0;
  322. spin_lock_init(&fifo->lock);
  323. init_waitqueue_head(&fifo->waitq);
  324. return 0;
  325. memfail:
  326. for (i--; i >= 0; i--)
  327. free_pages((unsigned long)fifo->mem[i], buf_order);
  328. kfree(fifo->mem);
  329. fifo->mem = NULL;
  330. if (fifo_buf_order) {
  331. fifo_buf_order--;
  332. goto retry;
  333. } else {
  334. return -ENOMEM;
  335. }
  336. }
  337. static void fifo_mem_release(struct xillyfifo *fifo)
  338. {
  339. int i;
  340. if (!fifo->mem)
  341. return;
  342. for (i = 0; i < fifo->bufnum; i++)
  343. free_pages((unsigned long)fifo->mem[i], fifo->buf_order);
  344. kfree(fifo->mem);
  345. }
  346. /*
  347. * When endpoint_quiesce() returns, the endpoint has no URBs submitted,
  348. * won't accept any new URB submissions, and its related work item doesn't
  349. * and won't run anymore.
  350. */
  351. static void endpoint_quiesce(struct xillyusb_endpoint *ep)
  352. {
  353. mutex_lock(&ep->ep_mutex);
  354. ep->shutting_down = true;
  355. mutex_unlock(&ep->ep_mutex);
  356. usb_kill_anchored_urbs(&ep->anchor);
  357. cancel_work_sync(&ep->workitem);
  358. }
  359. /*
  360. * Note that endpoint_dealloc() also frees fifo memory (if allocated), even
  361. * though endpoint_alloc doesn't allocate that memory.
  362. */
  363. static void endpoint_dealloc(struct xillyusb_endpoint *ep)
  364. {
  365. struct list_head *this, *next;
  366. fifo_mem_release(&ep->fifo);
  367. /* Join @filled_buffers with @buffers to free these entries too */
  368. list_splice(&ep->filled_buffers, &ep->buffers);
  369. list_for_each_safe(this, next, &ep->buffers) {
  370. struct xillybuffer *xb =
  371. list_entry(this, struct xillybuffer, entry);
  372. free_pages((unsigned long)xb->buf, ep->order);
  373. kfree(xb);
  374. }
  375. kfree(ep);
  376. }
  377. static struct xillyusb_endpoint
  378. *endpoint_alloc(struct xillyusb_dev *xdev,
  379. u8 ep_num,
  380. void (*work)(struct work_struct *),
  381. unsigned int order,
  382. int bufnum)
  383. {
  384. int i;
  385. struct xillyusb_endpoint *ep;
  386. ep = kzalloc_obj(*ep);
  387. if (!ep)
  388. return NULL;
  389. INIT_LIST_HEAD(&ep->buffers);
  390. INIT_LIST_HEAD(&ep->filled_buffers);
  391. spin_lock_init(&ep->buffers_lock);
  392. mutex_init(&ep->ep_mutex);
  393. init_usb_anchor(&ep->anchor);
  394. INIT_WORK(&ep->workitem, work);
  395. ep->order = order;
  396. ep->buffer_size = 1 << (PAGE_SHIFT + order);
  397. ep->outstanding_urbs = 0;
  398. ep->drained = true;
  399. ep->wake_on_drain = false;
  400. ep->xdev = xdev;
  401. ep->ep_num = ep_num;
  402. ep->shutting_down = false;
  403. for (i = 0; i < bufnum; i++) {
  404. struct xillybuffer *xb;
  405. unsigned long addr;
  406. xb = kzalloc_obj(*xb);
  407. if (!xb) {
  408. endpoint_dealloc(ep);
  409. return NULL;
  410. }
  411. addr = __get_free_pages(GFP_KERNEL, order);
  412. if (!addr) {
  413. kfree(xb);
  414. endpoint_dealloc(ep);
  415. return NULL;
  416. }
  417. xb->buf = (void *)addr;
  418. xb->ep = ep;
  419. list_add_tail(&xb->entry, &ep->buffers);
  420. }
  421. return ep;
  422. }
  423. static void cleanup_dev(struct kref *kref)
  424. {
  425. struct xillyusb_dev *xdev =
  426. container_of(kref, struct xillyusb_dev, kref);
  427. if (xdev->in_ep)
  428. endpoint_dealloc(xdev->in_ep);
  429. if (xdev->msg_ep)
  430. endpoint_dealloc(xdev->msg_ep);
  431. if (xdev->workq)
  432. destroy_workqueue(xdev->workq);
  433. usb_put_dev(xdev->udev);
  434. kfree(xdev->channels); /* Argument may be NULL, and that's fine */
  435. kfree(xdev);
  436. }
  437. /*
  438. * @process_in_mutex is taken to ensure that bulk_in_work() won't call
  439. * process_bulk_in() after wakeup_all()'s execution: The latter zeroes all
  440. * @read_data_ok entries, which will make process_bulk_in() report false
  441. * errors if executed. The mechanism relies on that xdev->error is assigned
  442. * a non-zero value by report_io_error() prior to queueing wakeup_all(),
  443. * which prevents bulk_in_work() from calling process_bulk_in().
  444. */
  445. static void wakeup_all(struct work_struct *work)
  446. {
  447. int i;
  448. struct xillyusb_dev *xdev = container_of(work, struct xillyusb_dev,
  449. wakeup_workitem);
  450. mutex_lock(&xdev->process_in_mutex);
  451. for (i = 0; i < xdev->num_channels; i++) {
  452. struct xillyusb_channel *chan = &xdev->channels[i];
  453. mutex_lock(&chan->lock);
  454. if (chan->in_fifo) {
  455. /*
  456. * Fake an EOF: Even if such arrives, it won't be
  457. * processed.
  458. */
  459. chan->read_data_ok = 0;
  460. wake_up_interruptible(&chan->in_fifo->waitq);
  461. }
  462. if (chan->out_ep)
  463. wake_up_interruptible(&chan->out_ep->fifo.waitq);
  464. mutex_unlock(&chan->lock);
  465. wake_up_interruptible(&chan->flushq);
  466. }
  467. mutex_unlock(&xdev->process_in_mutex);
  468. wake_up_interruptible(&xdev->msg_ep->fifo.waitq);
  469. kref_put(&xdev->kref, cleanup_dev);
  470. }
  471. static void report_io_error(struct xillyusb_dev *xdev,
  472. int errcode)
  473. {
  474. unsigned long flags;
  475. bool do_once = false;
  476. spin_lock_irqsave(&xdev->error_lock, flags);
  477. if (!xdev->error) {
  478. xdev->error = errcode;
  479. do_once = true;
  480. }
  481. spin_unlock_irqrestore(&xdev->error_lock, flags);
  482. if (do_once) {
  483. kref_get(&xdev->kref); /* xdev is used by work item */
  484. queue_work(wakeup_wq, &xdev->wakeup_workitem);
  485. }
  486. }
  487. /*
  488. * safely_assign_in_fifo() changes the value of chan->in_fifo and ensures
  489. * the previous pointer is never used after its return.
  490. */
  491. static void safely_assign_in_fifo(struct xillyusb_channel *chan,
  492. struct xillyfifo *fifo)
  493. {
  494. mutex_lock(&chan->lock);
  495. chan->in_fifo = fifo;
  496. mutex_unlock(&chan->lock);
  497. flush_work(&chan->xdev->in_ep->workitem);
  498. }
  499. static void bulk_in_completer(struct urb *urb)
  500. {
  501. struct xillybuffer *xb = urb->context;
  502. struct xillyusb_endpoint *ep = xb->ep;
  503. unsigned long flags;
  504. if (urb->status) {
  505. if (!(urb->status == -ENOENT ||
  506. urb->status == -ECONNRESET ||
  507. urb->status == -ESHUTDOWN))
  508. report_io_error(ep->xdev, -EIO);
  509. spin_lock_irqsave(&ep->buffers_lock, flags);
  510. list_add_tail(&xb->entry, &ep->buffers);
  511. ep->outstanding_urbs--;
  512. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  513. return;
  514. }
  515. xb->len = urb->actual_length;
  516. spin_lock_irqsave(&ep->buffers_lock, flags);
  517. list_add_tail(&xb->entry, &ep->filled_buffers);
  518. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  519. if (!ep->shutting_down)
  520. queue_work(ep->xdev->workq, &ep->workitem);
  521. }
  522. static void bulk_out_completer(struct urb *urb)
  523. {
  524. struct xillybuffer *xb = urb->context;
  525. struct xillyusb_endpoint *ep = xb->ep;
  526. unsigned long flags;
  527. if (urb->status &&
  528. (!(urb->status == -ENOENT ||
  529. urb->status == -ECONNRESET ||
  530. urb->status == -ESHUTDOWN)))
  531. report_io_error(ep->xdev, -EIO);
  532. spin_lock_irqsave(&ep->buffers_lock, flags);
  533. list_add_tail(&xb->entry, &ep->buffers);
  534. ep->outstanding_urbs--;
  535. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  536. if (!ep->shutting_down)
  537. queue_work(ep->xdev->workq, &ep->workitem);
  538. }
  539. static void try_queue_bulk_in(struct xillyusb_endpoint *ep)
  540. {
  541. struct xillyusb_dev *xdev = ep->xdev;
  542. struct xillybuffer *xb;
  543. struct urb *urb;
  544. int rc;
  545. unsigned long flags;
  546. unsigned int bufsize = ep->buffer_size;
  547. mutex_lock(&ep->ep_mutex);
  548. if (ep->shutting_down || xdev->error)
  549. goto done;
  550. while (1) {
  551. spin_lock_irqsave(&ep->buffers_lock, flags);
  552. if (list_empty(&ep->buffers)) {
  553. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  554. goto done;
  555. }
  556. xb = list_first_entry(&ep->buffers, struct xillybuffer, entry);
  557. list_del(&xb->entry);
  558. ep->outstanding_urbs++;
  559. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  560. urb = usb_alloc_urb(0, GFP_KERNEL);
  561. if (!urb) {
  562. report_io_error(xdev, -ENOMEM);
  563. goto relist;
  564. }
  565. usb_fill_bulk_urb(urb, xdev->udev,
  566. usb_rcvbulkpipe(xdev->udev, ep->ep_num),
  567. xb->buf, bufsize, bulk_in_completer, xb);
  568. usb_anchor_urb(urb, &ep->anchor);
  569. rc = usb_submit_urb(urb, GFP_KERNEL);
  570. if (rc) {
  571. report_io_error(xdev, (rc == -ENOMEM) ? -ENOMEM :
  572. -EIO);
  573. goto unanchor;
  574. }
  575. usb_free_urb(urb); /* This just decrements reference count */
  576. }
  577. unanchor:
  578. usb_unanchor_urb(urb);
  579. usb_free_urb(urb);
  580. relist:
  581. spin_lock_irqsave(&ep->buffers_lock, flags);
  582. list_add_tail(&xb->entry, &ep->buffers);
  583. ep->outstanding_urbs--;
  584. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  585. done:
  586. mutex_unlock(&ep->ep_mutex);
  587. }
  588. static void try_queue_bulk_out(struct xillyusb_endpoint *ep)
  589. {
  590. struct xillyfifo *fifo = &ep->fifo;
  591. struct xillyusb_dev *xdev = ep->xdev;
  592. struct xillybuffer *xb;
  593. struct urb *urb;
  594. int rc;
  595. unsigned int fill;
  596. unsigned long flags;
  597. bool do_wake = false;
  598. mutex_lock(&ep->ep_mutex);
  599. if (ep->shutting_down || xdev->error)
  600. goto done;
  601. fill = READ_ONCE(fifo->fill) & ep->fill_mask;
  602. while (1) {
  603. int count;
  604. unsigned int max_read;
  605. spin_lock_irqsave(&ep->buffers_lock, flags);
  606. /*
  607. * Race conditions might have the FIFO filled while the
  608. * endpoint is marked as drained here. That doesn't matter,
  609. * because the sole purpose of @drained is to ensure that
  610. * certain data has been sent on the USB channel before
  611. * shutting it down. Hence knowing that the FIFO appears
  612. * to be empty with no outstanding URBs at some moment
  613. * is good enough.
  614. */
  615. if (!fill) {
  616. ep->drained = !ep->outstanding_urbs;
  617. if (ep->drained && ep->wake_on_drain)
  618. do_wake = true;
  619. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  620. goto done;
  621. }
  622. ep->drained = false;
  623. if ((fill < ep->buffer_size && ep->outstanding_urbs) ||
  624. list_empty(&ep->buffers)) {
  625. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  626. goto done;
  627. }
  628. xb = list_first_entry(&ep->buffers, struct xillybuffer, entry);
  629. list_del(&xb->entry);
  630. ep->outstanding_urbs++;
  631. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  632. max_read = min(fill, ep->buffer_size);
  633. count = fifo_read(&ep->fifo, xb->buf, max_read, xilly_memcpy);
  634. /*
  635. * xilly_memcpy always returns 0 => fifo_read can't fail =>
  636. * count > 0
  637. */
  638. urb = usb_alloc_urb(0, GFP_KERNEL);
  639. if (!urb) {
  640. report_io_error(xdev, -ENOMEM);
  641. goto relist;
  642. }
  643. usb_fill_bulk_urb(urb, xdev->udev,
  644. usb_sndbulkpipe(xdev->udev, ep->ep_num),
  645. xb->buf, count, bulk_out_completer, xb);
  646. usb_anchor_urb(urb, &ep->anchor);
  647. rc = usb_submit_urb(urb, GFP_KERNEL);
  648. if (rc) {
  649. report_io_error(xdev, (rc == -ENOMEM) ? -ENOMEM :
  650. -EIO);
  651. goto unanchor;
  652. }
  653. usb_free_urb(urb); /* This just decrements reference count */
  654. fill -= count;
  655. do_wake = true;
  656. }
  657. unanchor:
  658. usb_unanchor_urb(urb);
  659. usb_free_urb(urb);
  660. relist:
  661. spin_lock_irqsave(&ep->buffers_lock, flags);
  662. list_add_tail(&xb->entry, &ep->buffers);
  663. ep->outstanding_urbs--;
  664. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  665. done:
  666. mutex_unlock(&ep->ep_mutex);
  667. if (do_wake)
  668. wake_up_interruptible(&fifo->waitq);
  669. }
  670. static void bulk_out_work(struct work_struct *work)
  671. {
  672. struct xillyusb_endpoint *ep = container_of(work,
  673. struct xillyusb_endpoint,
  674. workitem);
  675. try_queue_bulk_out(ep);
  676. }
  677. static int process_in_opcode(struct xillyusb_dev *xdev,
  678. int opcode,
  679. int chan_num)
  680. {
  681. struct xillyusb_channel *chan;
  682. struct device *dev = xdev->dev;
  683. int chan_idx = chan_num >> 1;
  684. if (chan_idx >= xdev->num_channels) {
  685. dev_err(dev, "Received illegal channel ID %d from FPGA\n",
  686. chan_num);
  687. return -EIO;
  688. }
  689. chan = &xdev->channels[chan_idx];
  690. switch (opcode) {
  691. case OPCODE_EOF:
  692. if (!chan->read_data_ok) {
  693. dev_err(dev, "Received unexpected EOF for channel %d\n",
  694. chan_num);
  695. return -EIO;
  696. }
  697. /*
  698. * A write memory barrier ensures that the FIFO's fill level
  699. * is visible before read_data_ok turns zero, so the data in
  700. * the FIFO isn't missed by the consumer.
  701. */
  702. smp_wmb();
  703. WRITE_ONCE(chan->read_data_ok, 0);
  704. wake_up_interruptible(&chan->in_fifo->waitq);
  705. break;
  706. case OPCODE_REACHED_CHECKPOINT:
  707. chan->flushing = 0;
  708. wake_up_interruptible(&chan->flushq);
  709. break;
  710. case OPCODE_CANCELED_CHECKPOINT:
  711. chan->canceled = 1;
  712. wake_up_interruptible(&chan->flushq);
  713. break;
  714. default:
  715. dev_err(dev, "Received illegal opcode %d from FPGA\n",
  716. opcode);
  717. return -EIO;
  718. }
  719. return 0;
  720. }
  721. static int process_bulk_in(struct xillybuffer *xb)
  722. {
  723. struct xillyusb_endpoint *ep = xb->ep;
  724. struct xillyusb_dev *xdev = ep->xdev;
  725. struct device *dev = xdev->dev;
  726. int dws = xb->len >> 2;
  727. __le32 *p = xb->buf;
  728. u32 ctrlword;
  729. struct xillyusb_channel *chan;
  730. struct xillyfifo *fifo;
  731. int chan_num = 0, opcode;
  732. int chan_idx;
  733. int bytes, count, dwconsume;
  734. int in_bytes_left = 0;
  735. int rc;
  736. if ((dws << 2) != xb->len) {
  737. dev_err(dev, "Received BULK IN transfer with %d bytes, not a multiple of 4\n",
  738. xb->len);
  739. return -EIO;
  740. }
  741. if (xdev->in_bytes_left) {
  742. bytes = min(xdev->in_bytes_left, dws << 2);
  743. in_bytes_left = xdev->in_bytes_left - bytes;
  744. chan_num = xdev->leftover_chan_num;
  745. goto resume_leftovers;
  746. }
  747. while (dws) {
  748. ctrlword = le32_to_cpu(*p++);
  749. dws--;
  750. chan_num = ctrlword & 0xfff;
  751. count = (ctrlword >> 12) & 0x3ff;
  752. opcode = (ctrlword >> 24) & 0xf;
  753. if (opcode != OPCODE_DATA) {
  754. unsigned int in_counter = xdev->in_counter++ & 0x3ff;
  755. if (count != in_counter) {
  756. dev_err(dev, "Expected opcode counter %d, got %d\n",
  757. in_counter, count);
  758. return -EIO;
  759. }
  760. rc = process_in_opcode(xdev, opcode, chan_num);
  761. if (rc)
  762. return rc;
  763. continue;
  764. }
  765. bytes = min(count + 1, dws << 2);
  766. in_bytes_left = count + 1 - bytes;
  767. resume_leftovers:
  768. chan_idx = chan_num >> 1;
  769. if (!(chan_num & 1) || chan_idx >= xdev->num_channels ||
  770. !xdev->channels[chan_idx].read_data_ok) {
  771. dev_err(dev, "Received illegal channel ID %d from FPGA\n",
  772. chan_num);
  773. return -EIO;
  774. }
  775. chan = &xdev->channels[chan_idx];
  776. fifo = chan->in_fifo;
  777. if (unlikely(!fifo))
  778. return -EIO; /* We got really unexpected data */
  779. if (bytes != fifo_write(fifo, p, bytes, xilly_memcpy)) {
  780. dev_err(dev, "Misbehaving FPGA overflowed an upstream FIFO!\n");
  781. return -EIO;
  782. }
  783. wake_up_interruptible(&fifo->waitq);
  784. dwconsume = (bytes + 3) >> 2;
  785. dws -= dwconsume;
  786. p += dwconsume;
  787. }
  788. xdev->in_bytes_left = in_bytes_left;
  789. xdev->leftover_chan_num = chan_num;
  790. return 0;
  791. }
  792. static void bulk_in_work(struct work_struct *work)
  793. {
  794. struct xillyusb_endpoint *ep =
  795. container_of(work, struct xillyusb_endpoint, workitem);
  796. struct xillyusb_dev *xdev = ep->xdev;
  797. unsigned long flags;
  798. struct xillybuffer *xb;
  799. bool consumed = false;
  800. int rc = 0;
  801. mutex_lock(&xdev->process_in_mutex);
  802. spin_lock_irqsave(&ep->buffers_lock, flags);
  803. while (1) {
  804. if (rc || list_empty(&ep->filled_buffers)) {
  805. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  806. mutex_unlock(&xdev->process_in_mutex);
  807. if (rc)
  808. report_io_error(xdev, rc);
  809. else if (consumed)
  810. try_queue_bulk_in(ep);
  811. return;
  812. }
  813. xb = list_first_entry(&ep->filled_buffers, struct xillybuffer,
  814. entry);
  815. list_del(&xb->entry);
  816. spin_unlock_irqrestore(&ep->buffers_lock, flags);
  817. consumed = true;
  818. if (!xdev->error)
  819. rc = process_bulk_in(xb);
  820. spin_lock_irqsave(&ep->buffers_lock, flags);
  821. list_add_tail(&xb->entry, &ep->buffers);
  822. ep->outstanding_urbs--;
  823. }
  824. }
  825. static int xillyusb_send_opcode(struct xillyusb_dev *xdev,
  826. int chan_num, char opcode, u32 data)
  827. {
  828. struct xillyusb_endpoint *ep = xdev->msg_ep;
  829. struct xillyfifo *fifo = &ep->fifo;
  830. __le32 msg[2];
  831. int rc = 0;
  832. msg[0] = cpu_to_le32((chan_num & 0xfff) |
  833. ((opcode & 0xf) << 24));
  834. msg[1] = cpu_to_le32(data);
  835. mutex_lock(&xdev->msg_mutex);
  836. /*
  837. * The wait queue is woken with the interruptible variant, so the
  838. * wait function matches, however returning because of an interrupt
  839. * will mess things up considerably, in particular when the caller is
  840. * the release method. And the xdev->error part prevents being stuck
  841. * forever in the event of a bizarre hardware bug: Pull the USB plug.
  842. */
  843. while (wait_event_interruptible(fifo->waitq,
  844. fifo->fill <= (fifo->size - 8) ||
  845. xdev->error))
  846. ; /* Empty loop */
  847. if (xdev->error) {
  848. rc = xdev->error;
  849. goto unlock_done;
  850. }
  851. fifo_write(fifo, (void *)msg, 8, xilly_memcpy);
  852. try_queue_bulk_out(ep);
  853. unlock_done:
  854. mutex_unlock(&xdev->msg_mutex);
  855. return rc;
  856. }
  857. /*
  858. * Note that flush_downstream() merely waits for the data to arrive to
  859. * the application logic at the FPGA -- unlike PCIe Xillybus' counterpart,
  860. * it does nothing to make it happen (and neither is it necessary).
  861. *
  862. * This function is not reentrant for the same @chan, but this is covered
  863. * by the fact that for any given @chan, it's called either by the open,
  864. * write, llseek and flush fops methods, which can't run in parallel (and the
  865. * write + flush and llseek method handlers are protected with out_mutex).
  866. *
  867. * chan->flushed is there to avoid multiple flushes at the same position,
  868. * in particular as a result of programs that close the file descriptor
  869. * e.g. after a dup2() for redirection.
  870. */
  871. static int flush_downstream(struct xillyusb_channel *chan,
  872. long timeout,
  873. bool interruptible)
  874. {
  875. struct xillyusb_dev *xdev = chan->xdev;
  876. int chan_num = chan->chan_idx << 1;
  877. long deadline, left_to_sleep;
  878. int rc;
  879. if (chan->flushed)
  880. return 0;
  881. deadline = jiffies + 1 + timeout;
  882. if (chan->flushing) {
  883. long cancel_deadline = jiffies + 1 + XILLY_RESPONSE_TIMEOUT;
  884. chan->canceled = 0;
  885. rc = xillyusb_send_opcode(xdev, chan_num,
  886. OPCODE_CANCEL_CHECKPOINT, 0);
  887. if (rc)
  888. return rc; /* Only real error, never -EINTR */
  889. /* Ignoring interrupts. Cancellation must be handled */
  890. while (!chan->canceled) {
  891. left_to_sleep = cancel_deadline - ((long)jiffies);
  892. if (left_to_sleep <= 0) {
  893. report_io_error(xdev, -EIO);
  894. return -EIO;
  895. }
  896. rc = wait_event_interruptible_timeout(chan->flushq,
  897. chan->canceled ||
  898. xdev->error,
  899. left_to_sleep);
  900. if (xdev->error)
  901. return xdev->error;
  902. }
  903. }
  904. chan->flushing = 1;
  905. /*
  906. * The checkpoint is given in terms of data elements, not bytes. As
  907. * a result, if less than an element's worth of data is stored in the
  908. * FIFO, it's not flushed, including the flush before closing, which
  909. * means that such data is lost. This is consistent with PCIe Xillybus.
  910. */
  911. rc = xillyusb_send_opcode(xdev, chan_num,
  912. OPCODE_SET_CHECKPOINT,
  913. chan->out_bytes >>
  914. chan->out_log2_element_size);
  915. if (rc)
  916. return rc; /* Only real error, never -EINTR */
  917. if (!timeout) {
  918. while (chan->flushing) {
  919. rc = wait_event_interruptible(chan->flushq,
  920. !chan->flushing ||
  921. xdev->error);
  922. if (xdev->error)
  923. return xdev->error;
  924. if (interruptible && rc)
  925. return -EINTR;
  926. }
  927. goto done;
  928. }
  929. while (chan->flushing) {
  930. left_to_sleep = deadline - ((long)jiffies);
  931. if (left_to_sleep <= 0)
  932. return -ETIMEDOUT;
  933. rc = wait_event_interruptible_timeout(chan->flushq,
  934. !chan->flushing ||
  935. xdev->error,
  936. left_to_sleep);
  937. if (xdev->error)
  938. return xdev->error;
  939. if (interruptible && rc < 0)
  940. return -EINTR;
  941. }
  942. done:
  943. chan->flushed = 1;
  944. return 0;
  945. }
  946. /* request_read_anything(): Ask the FPGA for any little amount of data */
  947. static int request_read_anything(struct xillyusb_channel *chan,
  948. char opcode)
  949. {
  950. struct xillyusb_dev *xdev = chan->xdev;
  951. unsigned int sh = chan->in_log2_element_size;
  952. int chan_num = (chan->chan_idx << 1) | 1;
  953. u32 mercy = chan->in_consumed_bytes + (2 << sh) - 1;
  954. return xillyusb_send_opcode(xdev, chan_num, opcode, mercy >> sh);
  955. }
  956. static int xillyusb_open(struct inode *inode, struct file *filp)
  957. {
  958. struct xillyusb_dev *xdev;
  959. struct xillyusb_channel *chan;
  960. struct xillyfifo *in_fifo = NULL;
  961. struct xillyusb_endpoint *out_ep = NULL;
  962. int rc;
  963. int index;
  964. mutex_lock(&kref_mutex);
  965. rc = xillybus_find_inode(inode, (void **)&xdev, &index);
  966. if (rc) {
  967. mutex_unlock(&kref_mutex);
  968. return rc;
  969. }
  970. kref_get(&xdev->kref);
  971. mutex_unlock(&kref_mutex);
  972. chan = &xdev->channels[index];
  973. filp->private_data = chan;
  974. mutex_lock(&chan->lock);
  975. rc = -ENODEV;
  976. if (xdev->error)
  977. goto unmutex_fail;
  978. if (((filp->f_mode & FMODE_READ) && !chan->readable) ||
  979. ((filp->f_mode & FMODE_WRITE) && !chan->writable))
  980. goto unmutex_fail;
  981. if ((filp->f_flags & O_NONBLOCK) && (filp->f_mode & FMODE_READ) &&
  982. chan->in_synchronous) {
  983. dev_err(xdev->dev,
  984. "open() failed: O_NONBLOCK not allowed for read on this device\n");
  985. goto unmutex_fail;
  986. }
  987. if ((filp->f_flags & O_NONBLOCK) && (filp->f_mode & FMODE_WRITE) &&
  988. chan->out_synchronous) {
  989. dev_err(xdev->dev,
  990. "open() failed: O_NONBLOCK not allowed for write on this device\n");
  991. goto unmutex_fail;
  992. }
  993. rc = -EBUSY;
  994. if (((filp->f_mode & FMODE_READ) && chan->open_for_read) ||
  995. ((filp->f_mode & FMODE_WRITE) && chan->open_for_write))
  996. goto unmutex_fail;
  997. if (filp->f_mode & FMODE_READ)
  998. chan->open_for_read = 1;
  999. if (filp->f_mode & FMODE_WRITE)
  1000. chan->open_for_write = 1;
  1001. mutex_unlock(&chan->lock);
  1002. if (filp->f_mode & FMODE_WRITE) {
  1003. out_ep = endpoint_alloc(xdev,
  1004. (chan->chan_idx + 2) | USB_DIR_OUT,
  1005. bulk_out_work, BUF_SIZE_ORDER, BUFNUM);
  1006. if (!out_ep) {
  1007. rc = -ENOMEM;
  1008. goto unopen;
  1009. }
  1010. rc = fifo_init(&out_ep->fifo, chan->out_log2_fifo_size);
  1011. if (rc)
  1012. goto late_unopen;
  1013. out_ep->fill_mask = -(1 << chan->out_log2_element_size);
  1014. chan->out_bytes = 0;
  1015. chan->flushed = 0;
  1016. /*
  1017. * Sending a flush request to a previously closed stream
  1018. * effectively opens it, and also waits until the command is
  1019. * confirmed by the FPGA. The latter is necessary because the
  1020. * data is sent through a separate BULK OUT endpoint, and the
  1021. * xHCI controller is free to reorder transmissions.
  1022. *
  1023. * This can't go wrong unless there's a serious hardware error
  1024. * (or the computer is stuck for 500 ms?)
  1025. */
  1026. rc = flush_downstream(chan, XILLY_RESPONSE_TIMEOUT, false);
  1027. if (rc == -ETIMEDOUT) {
  1028. rc = -EIO;
  1029. report_io_error(xdev, rc);
  1030. }
  1031. if (rc)
  1032. goto late_unopen;
  1033. }
  1034. if (filp->f_mode & FMODE_READ) {
  1035. in_fifo = kzalloc_obj(*in_fifo);
  1036. if (!in_fifo) {
  1037. rc = -ENOMEM;
  1038. goto late_unopen;
  1039. }
  1040. rc = fifo_init(in_fifo, chan->in_log2_fifo_size);
  1041. if (rc) {
  1042. kfree(in_fifo);
  1043. goto late_unopen;
  1044. }
  1045. }
  1046. mutex_lock(&chan->lock);
  1047. if (in_fifo) {
  1048. chan->in_fifo = in_fifo;
  1049. chan->read_data_ok = 1;
  1050. }
  1051. if (out_ep)
  1052. chan->out_ep = out_ep;
  1053. mutex_unlock(&chan->lock);
  1054. if (in_fifo) {
  1055. u32 in_checkpoint = 0;
  1056. if (!chan->in_synchronous)
  1057. in_checkpoint = in_fifo->size >>
  1058. chan->in_log2_element_size;
  1059. chan->in_consumed_bytes = 0;
  1060. chan->poll_used = 0;
  1061. chan->in_current_checkpoint = in_checkpoint;
  1062. rc = xillyusb_send_opcode(xdev, (chan->chan_idx << 1) | 1,
  1063. OPCODE_SET_CHECKPOINT,
  1064. in_checkpoint);
  1065. if (rc) /* Failure guarantees that opcode wasn't sent */
  1066. goto unfifo;
  1067. /*
  1068. * In non-blocking mode, request the FPGA to send any data it
  1069. * has right away. Otherwise, the first read() will always
  1070. * return -EAGAIN, which is OK strictly speaking, but ugly.
  1071. * Checking and unrolling if this fails isn't worth the
  1072. * effort -- the error is propagated to the first read()
  1073. * anyhow.
  1074. */
  1075. if (filp->f_flags & O_NONBLOCK)
  1076. request_read_anything(chan, OPCODE_SET_PUSH);
  1077. }
  1078. return 0;
  1079. unfifo:
  1080. chan->read_data_ok = 0;
  1081. safely_assign_in_fifo(chan, NULL);
  1082. fifo_mem_release(in_fifo);
  1083. kfree(in_fifo);
  1084. if (out_ep) {
  1085. mutex_lock(&chan->lock);
  1086. chan->out_ep = NULL;
  1087. mutex_unlock(&chan->lock);
  1088. }
  1089. late_unopen:
  1090. if (out_ep)
  1091. endpoint_dealloc(out_ep);
  1092. unopen:
  1093. mutex_lock(&chan->lock);
  1094. if (filp->f_mode & FMODE_READ)
  1095. chan->open_for_read = 0;
  1096. if (filp->f_mode & FMODE_WRITE)
  1097. chan->open_for_write = 0;
  1098. mutex_unlock(&chan->lock);
  1099. kref_put(&xdev->kref, cleanup_dev);
  1100. return rc;
  1101. unmutex_fail:
  1102. kref_put(&xdev->kref, cleanup_dev);
  1103. mutex_unlock(&chan->lock);
  1104. return rc;
  1105. }
  1106. static ssize_t xillyusb_read(struct file *filp, char __user *userbuf,
  1107. size_t count, loff_t *f_pos)
  1108. {
  1109. struct xillyusb_channel *chan = filp->private_data;
  1110. struct xillyusb_dev *xdev = chan->xdev;
  1111. struct xillyfifo *fifo = chan->in_fifo;
  1112. int chan_num = (chan->chan_idx << 1) | 1;
  1113. long deadline, left_to_sleep;
  1114. int bytes_done = 0;
  1115. bool sent_set_push = false;
  1116. int rc;
  1117. deadline = jiffies + 1 + XILLY_RX_TIMEOUT;
  1118. rc = mutex_lock_interruptible(&chan->in_mutex);
  1119. if (rc)
  1120. return rc;
  1121. while (1) {
  1122. u32 fifo_checkpoint_bytes, complete_checkpoint_bytes;
  1123. u32 complete_checkpoint, fifo_checkpoint;
  1124. u32 checkpoint;
  1125. s32 diff, leap;
  1126. unsigned int sh = chan->in_log2_element_size;
  1127. bool checkpoint_for_complete;
  1128. rc = fifo_read(fifo, (__force void *)userbuf + bytes_done,
  1129. count - bytes_done, xilly_copy_to_user);
  1130. if (rc < 0)
  1131. break;
  1132. bytes_done += rc;
  1133. chan->in_consumed_bytes += rc;
  1134. left_to_sleep = deadline - ((long)jiffies);
  1135. /*
  1136. * Some 32-bit arithmetic that may wrap. Note that
  1137. * complete_checkpoint is rounded up to the closest element
  1138. * boundary, because the read() can't be completed otherwise.
  1139. * fifo_checkpoint_bytes is rounded down, because it protects
  1140. * in_fifo from overflowing.
  1141. */
  1142. fifo_checkpoint_bytes = chan->in_consumed_bytes + fifo->size;
  1143. complete_checkpoint_bytes =
  1144. chan->in_consumed_bytes + count - bytes_done;
  1145. fifo_checkpoint = fifo_checkpoint_bytes >> sh;
  1146. complete_checkpoint =
  1147. (complete_checkpoint_bytes + (1 << sh) - 1) >> sh;
  1148. diff = (fifo_checkpoint - complete_checkpoint) << sh;
  1149. if (chan->in_synchronous && diff >= 0) {
  1150. checkpoint = complete_checkpoint;
  1151. checkpoint_for_complete = true;
  1152. } else {
  1153. checkpoint = fifo_checkpoint;
  1154. checkpoint_for_complete = false;
  1155. }
  1156. leap = (checkpoint - chan->in_current_checkpoint) << sh;
  1157. /*
  1158. * To prevent flooding of OPCODE_SET_CHECKPOINT commands as
  1159. * data is consumed, it's issued only if it moves the
  1160. * checkpoint by at least an 8th of the FIFO's size, or if
  1161. * it's necessary to complete the number of bytes requested by
  1162. * the read() call.
  1163. *
  1164. * chan->read_data_ok is checked to spare an unnecessary
  1165. * submission after receiving EOF, however it's harmless if
  1166. * such slips away.
  1167. */
  1168. if (chan->read_data_ok &&
  1169. (leap > (fifo->size >> 3) ||
  1170. (checkpoint_for_complete && leap > 0))) {
  1171. chan->in_current_checkpoint = checkpoint;
  1172. rc = xillyusb_send_opcode(xdev, chan_num,
  1173. OPCODE_SET_CHECKPOINT,
  1174. checkpoint);
  1175. if (rc)
  1176. break;
  1177. }
  1178. if (bytes_done == count ||
  1179. (left_to_sleep <= 0 && bytes_done))
  1180. break;
  1181. /*
  1182. * Reaching here means that the FIFO was empty when
  1183. * fifo_read() returned, but not necessarily right now. Error
  1184. * and EOF are checked and reported only now, so that no data
  1185. * that managed its way to the FIFO is lost.
  1186. */
  1187. if (!READ_ONCE(chan->read_data_ok)) { /* FPGA has sent EOF */
  1188. /* Has data slipped into the FIFO since fifo_read()? */
  1189. smp_rmb();
  1190. if (READ_ONCE(fifo->fill))
  1191. continue;
  1192. rc = 0;
  1193. break;
  1194. }
  1195. if (xdev->error) {
  1196. rc = xdev->error;
  1197. break;
  1198. }
  1199. if (filp->f_flags & O_NONBLOCK) {
  1200. rc = -EAGAIN;
  1201. break;
  1202. }
  1203. if (!sent_set_push) {
  1204. rc = xillyusb_send_opcode(xdev, chan_num,
  1205. OPCODE_SET_PUSH,
  1206. complete_checkpoint);
  1207. if (rc)
  1208. break;
  1209. sent_set_push = true;
  1210. }
  1211. if (left_to_sleep > 0) {
  1212. /*
  1213. * Note that when xdev->error is set (e.g. when the
  1214. * device is unplugged), read_data_ok turns zero and
  1215. * fifo->waitq is awaken.
  1216. * Therefore no special attention to xdev->error.
  1217. */
  1218. rc = wait_event_interruptible_timeout
  1219. (fifo->waitq,
  1220. fifo->fill || !chan->read_data_ok,
  1221. left_to_sleep);
  1222. } else { /* bytes_done == 0 */
  1223. /* Tell FPGA to send anything it has */
  1224. rc = request_read_anything(chan, OPCODE_UPDATE_PUSH);
  1225. if (rc)
  1226. break;
  1227. rc = wait_event_interruptible
  1228. (fifo->waitq,
  1229. fifo->fill || !chan->read_data_ok);
  1230. }
  1231. if (rc < 0) {
  1232. rc = -EINTR;
  1233. break;
  1234. }
  1235. }
  1236. if (((filp->f_flags & O_NONBLOCK) || chan->poll_used) &&
  1237. !READ_ONCE(fifo->fill))
  1238. request_read_anything(chan, OPCODE_SET_PUSH);
  1239. mutex_unlock(&chan->in_mutex);
  1240. if (bytes_done)
  1241. return bytes_done;
  1242. return rc;
  1243. }
  1244. static int xillyusb_flush(struct file *filp, fl_owner_t id)
  1245. {
  1246. struct xillyusb_channel *chan = filp->private_data;
  1247. int rc;
  1248. if (!(filp->f_mode & FMODE_WRITE))
  1249. return 0;
  1250. rc = mutex_lock_interruptible(&chan->out_mutex);
  1251. if (rc)
  1252. return rc;
  1253. /*
  1254. * One second's timeout on flushing. Interrupts are ignored, because if
  1255. * the user pressed CTRL-C, that interrupt will still be in flight by
  1256. * the time we reach here, and the opportunity to flush is lost.
  1257. */
  1258. rc = flush_downstream(chan, HZ, false);
  1259. mutex_unlock(&chan->out_mutex);
  1260. if (rc == -ETIMEDOUT) {
  1261. /* The things you do to use dev_warn() and not pr_warn() */
  1262. struct xillyusb_dev *xdev = chan->xdev;
  1263. mutex_lock(&chan->lock);
  1264. if (!xdev->error)
  1265. dev_warn(xdev->dev,
  1266. "Timed out while flushing. Output data may be lost.\n");
  1267. mutex_unlock(&chan->lock);
  1268. }
  1269. return rc;
  1270. }
  1271. static ssize_t xillyusb_write(struct file *filp, const char __user *userbuf,
  1272. size_t count, loff_t *f_pos)
  1273. {
  1274. struct xillyusb_channel *chan = filp->private_data;
  1275. struct xillyusb_dev *xdev = chan->xdev;
  1276. struct xillyfifo *fifo = &chan->out_ep->fifo;
  1277. int rc;
  1278. rc = mutex_lock_interruptible(&chan->out_mutex);
  1279. if (rc)
  1280. return rc;
  1281. while (1) {
  1282. if (xdev->error) {
  1283. rc = xdev->error;
  1284. break;
  1285. }
  1286. if (count == 0)
  1287. break;
  1288. rc = fifo_write(fifo, (__force void *)userbuf, count,
  1289. xilly_copy_from_user);
  1290. if (rc != 0)
  1291. break;
  1292. if (filp->f_flags & O_NONBLOCK) {
  1293. rc = -EAGAIN;
  1294. break;
  1295. }
  1296. if (wait_event_interruptible
  1297. (fifo->waitq,
  1298. fifo->fill != fifo->size || xdev->error)) {
  1299. rc = -EINTR;
  1300. break;
  1301. }
  1302. }
  1303. if (rc < 0)
  1304. goto done;
  1305. chan->out_bytes += rc;
  1306. if (rc) {
  1307. try_queue_bulk_out(chan->out_ep);
  1308. chan->flushed = 0;
  1309. }
  1310. if (chan->out_synchronous) {
  1311. int flush_rc = flush_downstream(chan, 0, true);
  1312. if (flush_rc && !rc)
  1313. rc = flush_rc;
  1314. }
  1315. done:
  1316. mutex_unlock(&chan->out_mutex);
  1317. return rc;
  1318. }
  1319. static int xillyusb_release(struct inode *inode, struct file *filp)
  1320. {
  1321. struct xillyusb_channel *chan = filp->private_data;
  1322. struct xillyusb_dev *xdev = chan->xdev;
  1323. int rc_read = 0, rc_write = 0;
  1324. if (filp->f_mode & FMODE_READ) {
  1325. struct xillyfifo *in_fifo = chan->in_fifo;
  1326. rc_read = xillyusb_send_opcode(xdev, (chan->chan_idx << 1) | 1,
  1327. OPCODE_CLOSE, 0);
  1328. /*
  1329. * If rc_read is nonzero, xdev->error indicates a global
  1330. * device error. The error is reported later, so that
  1331. * resources are freed.
  1332. *
  1333. * Looping on wait_event_interruptible() kinda breaks the idea
  1334. * of being interruptible, and this should have been
  1335. * wait_event(). Only it's being waken with
  1336. * wake_up_interruptible() for the sake of other uses. If
  1337. * there's a global device error, chan->read_data_ok is
  1338. * deasserted and the wait queue is awaken, so this is covered.
  1339. */
  1340. while (wait_event_interruptible(in_fifo->waitq,
  1341. !chan->read_data_ok))
  1342. ; /* Empty loop */
  1343. safely_assign_in_fifo(chan, NULL);
  1344. fifo_mem_release(in_fifo);
  1345. kfree(in_fifo);
  1346. mutex_lock(&chan->lock);
  1347. chan->open_for_read = 0;
  1348. mutex_unlock(&chan->lock);
  1349. }
  1350. if (filp->f_mode & FMODE_WRITE) {
  1351. struct xillyusb_endpoint *ep = chan->out_ep;
  1352. /*
  1353. * chan->flushing isn't zeroed. If the pre-release flush timed
  1354. * out, a cancel request will be sent before the next
  1355. * OPCODE_SET_CHECKPOINT (i.e. when the file is opened again).
  1356. * This is despite that the FPGA forgets about the checkpoint
  1357. * request as the file closes. Still, in an exceptional race
  1358. * condition, the FPGA could send an OPCODE_REACHED_CHECKPOINT
  1359. * just before closing that would reach the host after the
  1360. * file has re-opened.
  1361. */
  1362. mutex_lock(&chan->lock);
  1363. chan->out_ep = NULL;
  1364. mutex_unlock(&chan->lock);
  1365. endpoint_quiesce(ep);
  1366. endpoint_dealloc(ep);
  1367. /* See comments on rc_read above */
  1368. rc_write = xillyusb_send_opcode(xdev, chan->chan_idx << 1,
  1369. OPCODE_CLOSE, 0);
  1370. mutex_lock(&chan->lock);
  1371. chan->open_for_write = 0;
  1372. mutex_unlock(&chan->lock);
  1373. }
  1374. kref_put(&xdev->kref, cleanup_dev);
  1375. return rc_read ? rc_read : rc_write;
  1376. }
  1377. /*
  1378. * Xillybus' API allows device nodes to be seekable, giving the user
  1379. * application access to a RAM array on the FPGA (or logic emulating it).
  1380. */
  1381. static loff_t xillyusb_llseek(struct file *filp, loff_t offset, int whence)
  1382. {
  1383. struct xillyusb_channel *chan = filp->private_data;
  1384. struct xillyusb_dev *xdev = chan->xdev;
  1385. loff_t pos = filp->f_pos;
  1386. int rc = 0;
  1387. unsigned int log2_element_size = chan->readable ?
  1388. chan->in_log2_element_size : chan->out_log2_element_size;
  1389. /*
  1390. * Take both mutexes not allowing interrupts, since it seems like
  1391. * common applications don't expect an -EINTR here. Besides, multiple
  1392. * access to a single file descriptor on seekable devices is a mess
  1393. * anyhow.
  1394. */
  1395. mutex_lock(&chan->out_mutex);
  1396. mutex_lock(&chan->in_mutex);
  1397. switch (whence) {
  1398. case SEEK_SET:
  1399. pos = offset;
  1400. break;
  1401. case SEEK_CUR:
  1402. pos += offset;
  1403. break;
  1404. case SEEK_END:
  1405. pos = offset; /* Going to the end => to the beginning */
  1406. break;
  1407. default:
  1408. rc = -EINVAL;
  1409. goto end;
  1410. }
  1411. /* In any case, we must finish on an element boundary */
  1412. if (pos & ((1 << log2_element_size) - 1)) {
  1413. rc = -EINVAL;
  1414. goto end;
  1415. }
  1416. rc = xillyusb_send_opcode(xdev, chan->chan_idx << 1,
  1417. OPCODE_SET_ADDR,
  1418. pos >> log2_element_size);
  1419. if (rc)
  1420. goto end;
  1421. if (chan->writable) {
  1422. chan->flushed = 0;
  1423. rc = flush_downstream(chan, HZ, false);
  1424. }
  1425. end:
  1426. mutex_unlock(&chan->out_mutex);
  1427. mutex_unlock(&chan->in_mutex);
  1428. if (rc) /* Return error after releasing mutexes */
  1429. return rc;
  1430. filp->f_pos = pos;
  1431. return pos;
  1432. }
  1433. static __poll_t xillyusb_poll(struct file *filp, poll_table *wait)
  1434. {
  1435. struct xillyusb_channel *chan = filp->private_data;
  1436. __poll_t mask = 0;
  1437. if (chan->in_fifo)
  1438. poll_wait(filp, &chan->in_fifo->waitq, wait);
  1439. if (chan->out_ep)
  1440. poll_wait(filp, &chan->out_ep->fifo.waitq, wait);
  1441. /*
  1442. * If this is the first time poll() is called, and the file is
  1443. * readable, set the relevant flag. Also tell the FPGA to send all it
  1444. * has, to kickstart the mechanism that ensures there's always some
  1445. * data in in_fifo unless the stream is dry end-to-end. Note that the
  1446. * first poll() may not return a EPOLLIN, even if there's data on the
  1447. * FPGA. Rather, the data will arrive soon, and trigger the relevant
  1448. * wait queue.
  1449. */
  1450. if (!chan->poll_used && chan->in_fifo) {
  1451. chan->poll_used = 1;
  1452. request_read_anything(chan, OPCODE_SET_PUSH);
  1453. }
  1454. /*
  1455. * poll() won't play ball regarding read() channels which
  1456. * are synchronous. Allowing that will create situations where data has
  1457. * been delivered at the FPGA, and users expecting select() to wake up,
  1458. * which it may not. So make it never work.
  1459. */
  1460. if (chan->in_fifo && !chan->in_synchronous &&
  1461. (READ_ONCE(chan->in_fifo->fill) || !chan->read_data_ok))
  1462. mask |= EPOLLIN | EPOLLRDNORM;
  1463. if (chan->out_ep &&
  1464. (READ_ONCE(chan->out_ep->fifo.fill) != chan->out_ep->fifo.size))
  1465. mask |= EPOLLOUT | EPOLLWRNORM;
  1466. if (chan->xdev->error)
  1467. mask |= EPOLLERR;
  1468. return mask;
  1469. }
  1470. static const struct file_operations xillyusb_fops = {
  1471. .owner = THIS_MODULE,
  1472. .read = xillyusb_read,
  1473. .write = xillyusb_write,
  1474. .open = xillyusb_open,
  1475. .flush = xillyusb_flush,
  1476. .release = xillyusb_release,
  1477. .llseek = xillyusb_llseek,
  1478. .poll = xillyusb_poll,
  1479. };
  1480. static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
  1481. {
  1482. struct usb_device *udev = xdev->udev;
  1483. /* Verify that device has the two fundamental bulk in/out endpoints */
  1484. if (usb_pipe_type_check(udev, usb_sndbulkpipe(udev, MSG_EP_NUM)) ||
  1485. usb_pipe_type_check(udev, usb_rcvbulkpipe(udev, IN_EP_NUM)))
  1486. return -ENODEV;
  1487. xdev->msg_ep = endpoint_alloc(xdev, MSG_EP_NUM | USB_DIR_OUT,
  1488. bulk_out_work, 1, 2);
  1489. if (!xdev->msg_ep)
  1490. return -ENOMEM;
  1491. if (fifo_init(&xdev->msg_ep->fifo, 13)) /* 8 kiB */
  1492. goto dealloc;
  1493. xdev->msg_ep->fill_mask = -8; /* 8 bytes granularity */
  1494. xdev->in_ep = endpoint_alloc(xdev, IN_EP_NUM | USB_DIR_IN,
  1495. bulk_in_work, BUF_SIZE_ORDER, BUFNUM);
  1496. if (!xdev->in_ep)
  1497. goto dealloc;
  1498. try_queue_bulk_in(xdev->in_ep);
  1499. return 0;
  1500. dealloc:
  1501. endpoint_dealloc(xdev->msg_ep); /* Also frees FIFO mem if allocated */
  1502. xdev->msg_ep = NULL;
  1503. return -ENOMEM;
  1504. }
  1505. static int setup_channels(struct xillyusb_dev *xdev,
  1506. __le16 *chandesc,
  1507. int num_channels)
  1508. {
  1509. struct usb_device *udev = xdev->udev;
  1510. struct xillyusb_channel *chan, *new_channels;
  1511. int i;
  1512. chan = kzalloc_objs(*chan, num_channels);
  1513. if (!chan)
  1514. return -ENOMEM;
  1515. new_channels = chan;
  1516. for (i = 0; i < num_channels; i++, chan++) {
  1517. unsigned int in_desc = le16_to_cpu(*chandesc++);
  1518. unsigned int out_desc = le16_to_cpu(*chandesc++);
  1519. chan->xdev = xdev;
  1520. mutex_init(&chan->in_mutex);
  1521. mutex_init(&chan->out_mutex);
  1522. mutex_init(&chan->lock);
  1523. init_waitqueue_head(&chan->flushq);
  1524. chan->chan_idx = i;
  1525. if (in_desc & 0x80) { /* Entry is valid */
  1526. chan->readable = 1;
  1527. chan->in_synchronous = !!(in_desc & 0x40);
  1528. chan->in_seekable = !!(in_desc & 0x20);
  1529. chan->in_log2_element_size = in_desc & 0x0f;
  1530. chan->in_log2_fifo_size = ((in_desc >> 8) & 0x1f) + 16;
  1531. }
  1532. /*
  1533. * A downstream channel should never exist above index 13,
  1534. * as it would request a nonexistent BULK endpoint > 15.
  1535. * In the peculiar case that it does, it's ignored silently.
  1536. */
  1537. if ((out_desc & 0x80) && i < 14) { /* Entry is valid */
  1538. if (usb_pipe_type_check(udev,
  1539. usb_sndbulkpipe(udev, i + 2))) {
  1540. dev_err(xdev->dev,
  1541. "Missing BULK OUT endpoint %d\n",
  1542. i + 2);
  1543. kfree(new_channels);
  1544. return -ENODEV;
  1545. }
  1546. chan->writable = 1;
  1547. chan->out_synchronous = !!(out_desc & 0x40);
  1548. chan->out_seekable = !!(out_desc & 0x20);
  1549. chan->out_log2_element_size = out_desc & 0x0f;
  1550. chan->out_log2_fifo_size =
  1551. ((out_desc >> 8) & 0x1f) + 16;
  1552. }
  1553. }
  1554. xdev->channels = new_channels;
  1555. return 0;
  1556. }
  1557. static int xillyusb_discovery(struct usb_interface *interface)
  1558. {
  1559. int rc;
  1560. struct xillyusb_dev *xdev = usb_get_intfdata(interface);
  1561. __le16 bogus_chandesc[2];
  1562. struct xillyfifo idt_fifo;
  1563. struct xillyusb_channel *chan;
  1564. unsigned int idt_len, names_offset;
  1565. unsigned char *idt;
  1566. int num_channels;
  1567. rc = xillyusb_send_opcode(xdev, ~0, OPCODE_QUIESCE, 0);
  1568. if (rc) {
  1569. dev_err(&interface->dev, "Failed to send quiesce request. Aborting.\n");
  1570. return rc;
  1571. }
  1572. /* Phase I: Set up one fake upstream channel and obtain IDT */
  1573. /* Set up a fake IDT with one async IN stream */
  1574. bogus_chandesc[0] = cpu_to_le16(0x80);
  1575. bogus_chandesc[1] = cpu_to_le16(0);
  1576. rc = setup_channels(xdev, bogus_chandesc, 1);
  1577. if (rc)
  1578. return rc;
  1579. rc = fifo_init(&idt_fifo, LOG2_IDT_FIFO_SIZE);
  1580. if (rc)
  1581. return rc;
  1582. chan = xdev->channels;
  1583. chan->in_fifo = &idt_fifo;
  1584. chan->read_data_ok = 1;
  1585. xdev->num_channels = 1;
  1586. rc = xillyusb_send_opcode(xdev, ~0, OPCODE_REQ_IDT, 0);
  1587. if (rc) {
  1588. dev_err(&interface->dev, "Failed to send IDT request. Aborting.\n");
  1589. goto unfifo;
  1590. }
  1591. rc = wait_event_interruptible_timeout(idt_fifo.waitq,
  1592. !chan->read_data_ok,
  1593. XILLY_RESPONSE_TIMEOUT);
  1594. if (xdev->error) {
  1595. rc = xdev->error;
  1596. goto unfifo;
  1597. }
  1598. if (rc < 0) {
  1599. rc = -EINTR; /* Interrupt on probe method? Interesting. */
  1600. goto unfifo;
  1601. }
  1602. if (chan->read_data_ok) {
  1603. rc = -ETIMEDOUT;
  1604. dev_err(&interface->dev, "No response from FPGA. Aborting.\n");
  1605. goto unfifo;
  1606. }
  1607. idt_len = READ_ONCE(idt_fifo.fill);
  1608. idt = kmalloc(idt_len, GFP_KERNEL);
  1609. if (!idt) {
  1610. rc = -ENOMEM;
  1611. goto unfifo;
  1612. }
  1613. fifo_read(&idt_fifo, idt, idt_len, xilly_memcpy);
  1614. if (crc32_le(~0, idt, idt_len) != 0) {
  1615. dev_err(&interface->dev, "IDT failed CRC check. Aborting.\n");
  1616. rc = -ENODEV;
  1617. goto unidt;
  1618. }
  1619. if (*idt > 0x90) {
  1620. dev_err(&interface->dev, "No support for IDT version 0x%02x. Maybe the xillyusb driver needs an upgrade. Aborting.\n",
  1621. (int)*idt);
  1622. rc = -ENODEV;
  1623. goto unidt;
  1624. }
  1625. /* Phase II: Set up the streams as defined in IDT */
  1626. num_channels = le16_to_cpu(*((__le16 *)(idt + 1)));
  1627. names_offset = 3 + num_channels * 4;
  1628. idt_len -= 4; /* Exclude CRC */
  1629. if (idt_len < names_offset) {
  1630. dev_err(&interface->dev, "IDT too short. This is exceptionally weird, because its CRC is OK\n");
  1631. rc = -ENODEV;
  1632. goto unidt;
  1633. }
  1634. rc = setup_channels(xdev, (void *)idt + 3, num_channels);
  1635. if (rc)
  1636. goto unidt;
  1637. /*
  1638. * Except for wildly misbehaving hardware, or if it was disconnected
  1639. * just after responding with the IDT, there is no reason for any
  1640. * work item to be running now. To be sure that xdev->channels
  1641. * is updated on anything that might run in parallel, flush the
  1642. * device's workqueue and the wakeup work item. This rarely
  1643. * does anything.
  1644. */
  1645. flush_workqueue(xdev->workq);
  1646. flush_work(&xdev->wakeup_workitem);
  1647. xdev->num_channels = num_channels;
  1648. fifo_mem_release(&idt_fifo);
  1649. kfree(chan);
  1650. rc = xillybus_init_chrdev(&interface->dev, &xillyusb_fops,
  1651. THIS_MODULE, xdev,
  1652. idt + names_offset,
  1653. idt_len - names_offset,
  1654. num_channels,
  1655. xillyname, true);
  1656. kfree(idt);
  1657. return rc;
  1658. unidt:
  1659. kfree(idt);
  1660. unfifo:
  1661. safely_assign_in_fifo(chan, NULL);
  1662. fifo_mem_release(&idt_fifo);
  1663. return rc;
  1664. }
  1665. static int xillyusb_probe(struct usb_interface *interface,
  1666. const struct usb_device_id *id)
  1667. {
  1668. struct xillyusb_dev *xdev;
  1669. int rc;
  1670. xdev = kzalloc_obj(*xdev);
  1671. if (!xdev)
  1672. return -ENOMEM;
  1673. kref_init(&xdev->kref);
  1674. mutex_init(&xdev->process_in_mutex);
  1675. mutex_init(&xdev->msg_mutex);
  1676. xdev->udev = usb_get_dev(interface_to_usbdev(interface));
  1677. xdev->dev = &interface->dev;
  1678. xdev->error = 0;
  1679. spin_lock_init(&xdev->error_lock);
  1680. xdev->in_counter = 0;
  1681. xdev->in_bytes_left = 0;
  1682. xdev->workq = alloc_workqueue(xillyname, WQ_HIGHPRI | WQ_UNBOUND, 0);
  1683. if (!xdev->workq) {
  1684. dev_err(&interface->dev, "Failed to allocate work queue\n");
  1685. rc = -ENOMEM;
  1686. goto fail;
  1687. }
  1688. INIT_WORK(&xdev->wakeup_workitem, wakeup_all);
  1689. usb_set_intfdata(interface, xdev);
  1690. rc = xillyusb_setup_base_eps(xdev);
  1691. if (rc)
  1692. goto fail;
  1693. rc = xillyusb_discovery(interface);
  1694. if (rc)
  1695. goto latefail;
  1696. return 0;
  1697. latefail:
  1698. endpoint_quiesce(xdev->in_ep);
  1699. endpoint_quiesce(xdev->msg_ep);
  1700. fail:
  1701. usb_set_intfdata(interface, NULL);
  1702. kref_put(&xdev->kref, cleanup_dev);
  1703. return rc;
  1704. }
  1705. static void xillyusb_disconnect(struct usb_interface *interface)
  1706. {
  1707. struct xillyusb_dev *xdev = usb_get_intfdata(interface);
  1708. struct xillyusb_endpoint *msg_ep = xdev->msg_ep;
  1709. struct xillyfifo *fifo = &msg_ep->fifo;
  1710. int rc;
  1711. int i;
  1712. xillybus_cleanup_chrdev(xdev, &interface->dev);
  1713. /*
  1714. * Try to send OPCODE_QUIESCE, which will fail silently if the device
  1715. * was disconnected, but makes sense on module unload.
  1716. */
  1717. msg_ep->wake_on_drain = true;
  1718. xillyusb_send_opcode(xdev, ~0, OPCODE_QUIESCE, 0);
  1719. /*
  1720. * If the device has been disconnected, sending the opcode causes
  1721. * a global device error with xdev->error, if such error didn't
  1722. * occur earlier. Hence timing out means that the USB link is fine,
  1723. * but somehow the message wasn't sent. Should never happen.
  1724. */
  1725. rc = wait_event_interruptible_timeout(fifo->waitq,
  1726. msg_ep->drained || xdev->error,
  1727. XILLY_RESPONSE_TIMEOUT);
  1728. if (!rc)
  1729. dev_err(&interface->dev,
  1730. "Weird timeout condition on sending quiesce request.\n");
  1731. report_io_error(xdev, -ENODEV); /* Discourage further activity */
  1732. /*
  1733. * This device driver is declared with soft_unbind set, or else
  1734. * sending OPCODE_QUIESCE above would always fail. The price is
  1735. * that the USB framework didn't kill outstanding URBs, so it has
  1736. * to be done explicitly before returning from this call.
  1737. */
  1738. for (i = 0; i < xdev->num_channels; i++) {
  1739. struct xillyusb_channel *chan = &xdev->channels[i];
  1740. /*
  1741. * Lock taken to prevent chan->out_ep from changing. It also
  1742. * ensures xillyusb_open() and xillyusb_flush() don't access
  1743. * xdev->dev after being nullified below.
  1744. */
  1745. mutex_lock(&chan->lock);
  1746. if (chan->out_ep)
  1747. endpoint_quiesce(chan->out_ep);
  1748. mutex_unlock(&chan->lock);
  1749. }
  1750. endpoint_quiesce(xdev->in_ep);
  1751. endpoint_quiesce(xdev->msg_ep);
  1752. usb_set_intfdata(interface, NULL);
  1753. xdev->dev = NULL;
  1754. mutex_lock(&kref_mutex);
  1755. kref_put(&xdev->kref, cleanup_dev);
  1756. mutex_unlock(&kref_mutex);
  1757. }
  1758. static struct usb_driver xillyusb_driver = {
  1759. .name = xillyname,
  1760. .id_table = xillyusb_table,
  1761. .probe = xillyusb_probe,
  1762. .disconnect = xillyusb_disconnect,
  1763. .soft_unbind = 1,
  1764. };
  1765. static int __init xillyusb_init(void)
  1766. {
  1767. int rc = 0;
  1768. wakeup_wq = alloc_workqueue(xillyname, WQ_UNBOUND, 0);
  1769. if (!wakeup_wq)
  1770. return -ENOMEM;
  1771. if (LOG2_INITIAL_FIFO_BUF_SIZE > PAGE_SHIFT)
  1772. fifo_buf_order = LOG2_INITIAL_FIFO_BUF_SIZE - PAGE_SHIFT;
  1773. else
  1774. fifo_buf_order = 0;
  1775. rc = usb_register(&xillyusb_driver);
  1776. if (rc)
  1777. destroy_workqueue(wakeup_wq);
  1778. return rc;
  1779. }
  1780. static void __exit xillyusb_exit(void)
  1781. {
  1782. usb_deregister(&xillyusb_driver);
  1783. destroy_workqueue(wakeup_wq);
  1784. }
  1785. module_init(xillyusb_init);
  1786. module_exit(xillyusb_exit);