xillyusb.c 51 KB

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