xillybus_core.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. /*
  2. * linux/drivers/misc/xillybus_core.c
  3. *
  4. * Copyright 2011 Xillybus Ltd, http://xillybus.com
  5. *
  6. * Driver for the Xillybus FPGA/host framework.
  7. *
  8. * This driver interfaces with a special IP core in an FPGA, setting up
  9. * a pipe between a hardware FIFO in the programmable logic and a device
  10. * file in the host. The number of such pipes and their attributes are
  11. * set up on the logic. This driver detects these automatically and
  12. * creates the device files accordingly.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the smems of the GNU General Public License as published by
  16. * the Free Software Foundation; version 2 of the License.
  17. */
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/fs.h>
  26. #include <linux/cdev.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mutex.h>
  29. #include <linux/crc32.h>
  30. #include <linux/poll.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/workqueue.h>
  34. #include "xillybus.h"
  35. MODULE_DESCRIPTION("Xillybus core functions");
  36. MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
  37. MODULE_VERSION("1.07");
  38. MODULE_ALIAS("xillybus_core");
  39. MODULE_LICENSE("GPL v2");
  40. /* General timeout is 100 ms, rx timeout is 10 ms */
  41. #define XILLY_RX_TIMEOUT (10*HZ/1000)
  42. #define XILLY_TIMEOUT (100*HZ/1000)
  43. #define fpga_msg_ctrl_reg 0x0008
  44. #define fpga_dma_control_reg 0x0020
  45. #define fpga_dma_bufno_reg 0x0024
  46. #define fpga_dma_bufaddr_lowaddr_reg 0x0028
  47. #define fpga_dma_bufaddr_highaddr_reg 0x002c
  48. #define fpga_buf_ctrl_reg 0x0030
  49. #define fpga_buf_offset_reg 0x0034
  50. #define fpga_endian_reg 0x0040
  51. #define XILLYMSG_OPCODE_RELEASEBUF 1
  52. #define XILLYMSG_OPCODE_QUIESCEACK 2
  53. #define XILLYMSG_OPCODE_FIFOEOF 3
  54. #define XILLYMSG_OPCODE_FATAL_ERROR 4
  55. #define XILLYMSG_OPCODE_NONEMPTY 5
  56. static const char xillyname[] = "xillybus";
  57. static struct class *xillybus_class;
  58. /*
  59. * ep_list_lock is the last lock to be taken; No other lock requests are
  60. * allowed while holding it. It merely protects list_of_endpoints, and not
  61. * the endpoints listed in it.
  62. */
  63. static LIST_HEAD(list_of_endpoints);
  64. static struct mutex ep_list_lock;
  65. static struct workqueue_struct *xillybus_wq;
  66. /*
  67. * Locking scheme: Mutexes protect invocations of character device methods.
  68. * If both locks are taken, wr_mutex is taken first, rd_mutex second.
  69. *
  70. * wr_spinlock protects wr_*_buf_idx, wr_empty, wr_sleepy, wr_ready and the
  71. * buffers' end_offset fields against changes made by IRQ handler (and in
  72. * theory, other file request handlers, but the mutex handles that). Nothing
  73. * else.
  74. * They are held for short direct memory manipulations. Needless to say,
  75. * no mutex locking is allowed when a spinlock is held.
  76. *
  77. * rd_spinlock does the same with rd_*_buf_idx, rd_empty and end_offset.
  78. *
  79. * register_mutex is endpoint-specific, and is held when non-atomic
  80. * register operations are performed. wr_mutex and rd_mutex may be
  81. * held when register_mutex is taken, but none of the spinlocks. Note that
  82. * register_mutex doesn't protect against sporadic buf_ctrl_reg writes
  83. * which are unrelated to buf_offset_reg, since they are harmless.
  84. *
  85. * Blocking on the wait queues is allowed with mutexes held, but not with
  86. * spinlocks.
  87. *
  88. * Only interruptible blocking is allowed on mutexes and wait queues.
  89. *
  90. * All in all, the locking order goes (with skips allowed, of course):
  91. * wr_mutex -> rd_mutex -> register_mutex -> wr_spinlock -> rd_spinlock
  92. */
  93. static void malformed_message(struct xilly_endpoint *endpoint, u32 *buf)
  94. {
  95. int opcode;
  96. int msg_channel, msg_bufno, msg_data, msg_dir;
  97. opcode = (buf[0] >> 24) & 0xff;
  98. msg_dir = buf[0] & 1;
  99. msg_channel = (buf[0] >> 1) & 0x7ff;
  100. msg_bufno = (buf[0] >> 12) & 0x3ff;
  101. msg_data = buf[1] & 0xfffffff;
  102. dev_warn(endpoint->dev,
  103. "Malformed message (skipping): opcode=%d, channel=%03x, dir=%d, bufno=%03x, data=%07x\n",
  104. opcode, msg_channel, msg_dir, msg_bufno, msg_data);
  105. }
  106. /*
  107. * xillybus_isr assumes the interrupt is allocated exclusively to it,
  108. * which is the natural case MSI and several other hardware-oriented
  109. * interrupts. Sharing is not allowed.
  110. */
  111. irqreturn_t xillybus_isr(int irq, void *data)
  112. {
  113. struct xilly_endpoint *ep = data;
  114. u32 *buf;
  115. unsigned int buf_size;
  116. int i;
  117. int opcode;
  118. unsigned int msg_channel, msg_bufno, msg_data, msg_dir;
  119. struct xilly_channel *channel;
  120. buf = ep->msgbuf_addr;
  121. buf_size = ep->msg_buf_size/sizeof(u32);
  122. ep->ephw->hw_sync_sgl_for_cpu(ep,
  123. ep->msgbuf_dma_addr,
  124. ep->msg_buf_size,
  125. DMA_FROM_DEVICE);
  126. for (i = 0; i < buf_size; i += 2) {
  127. if (((buf[i+1] >> 28) & 0xf) != ep->msg_counter) {
  128. malformed_message(ep, &buf[i]);
  129. dev_warn(ep->dev,
  130. "Sending a NACK on counter %x (instead of %x) on entry %d\n",
  131. ((buf[i+1] >> 28) & 0xf),
  132. ep->msg_counter,
  133. i/2);
  134. if (++ep->failed_messages > 10) {
  135. dev_err(ep->dev,
  136. "Lost sync with interrupt messages. Stopping.\n");
  137. } else {
  138. ep->ephw->hw_sync_sgl_for_device(
  139. ep,
  140. ep->msgbuf_dma_addr,
  141. ep->msg_buf_size,
  142. DMA_FROM_DEVICE);
  143. iowrite32(0x01, /* Message NACK */
  144. ep->registers + fpga_msg_ctrl_reg);
  145. }
  146. return IRQ_HANDLED;
  147. } else if (buf[i] & (1 << 22)) /* Last message */
  148. break;
  149. }
  150. if (i >= buf_size) {
  151. dev_err(ep->dev, "Bad interrupt message. Stopping.\n");
  152. return IRQ_HANDLED;
  153. }
  154. buf_size = i + 2;
  155. for (i = 0; i < buf_size; i += 2) { /* Scan through messages */
  156. opcode = (buf[i] >> 24) & 0xff;
  157. msg_dir = buf[i] & 1;
  158. msg_channel = (buf[i] >> 1) & 0x7ff;
  159. msg_bufno = (buf[i] >> 12) & 0x3ff;
  160. msg_data = buf[i+1] & 0xfffffff;
  161. switch (opcode) {
  162. case XILLYMSG_OPCODE_RELEASEBUF:
  163. if ((msg_channel > ep->num_channels) ||
  164. (msg_channel == 0)) {
  165. malformed_message(ep, &buf[i]);
  166. break;
  167. }
  168. channel = ep->channels[msg_channel];
  169. if (msg_dir) { /* Write channel */
  170. if (msg_bufno >= channel->num_wr_buffers) {
  171. malformed_message(ep, &buf[i]);
  172. break;
  173. }
  174. spin_lock(&channel->wr_spinlock);
  175. channel->wr_buffers[msg_bufno]->end_offset =
  176. msg_data;
  177. channel->wr_fpga_buf_idx = msg_bufno;
  178. channel->wr_empty = 0;
  179. channel->wr_sleepy = 0;
  180. spin_unlock(&channel->wr_spinlock);
  181. wake_up_interruptible(&channel->wr_wait);
  182. } else {
  183. /* Read channel */
  184. if (msg_bufno >= channel->num_rd_buffers) {
  185. malformed_message(ep, &buf[i]);
  186. break;
  187. }
  188. spin_lock(&channel->rd_spinlock);
  189. channel->rd_fpga_buf_idx = msg_bufno;
  190. channel->rd_full = 0;
  191. spin_unlock(&channel->rd_spinlock);
  192. wake_up_interruptible(&channel->rd_wait);
  193. if (!channel->rd_synchronous)
  194. queue_delayed_work(
  195. xillybus_wq,
  196. &channel->rd_workitem,
  197. XILLY_RX_TIMEOUT);
  198. }
  199. break;
  200. case XILLYMSG_OPCODE_NONEMPTY:
  201. if ((msg_channel > ep->num_channels) ||
  202. (msg_channel == 0) || (!msg_dir) ||
  203. !ep->channels[msg_channel]->wr_supports_nonempty) {
  204. malformed_message(ep, &buf[i]);
  205. break;
  206. }
  207. channel = ep->channels[msg_channel];
  208. if (msg_bufno >= channel->num_wr_buffers) {
  209. malformed_message(ep, &buf[i]);
  210. break;
  211. }
  212. spin_lock(&channel->wr_spinlock);
  213. if (msg_bufno == channel->wr_host_buf_idx)
  214. channel->wr_ready = 1;
  215. spin_unlock(&channel->wr_spinlock);
  216. wake_up_interruptible(&channel->wr_ready_wait);
  217. break;
  218. case XILLYMSG_OPCODE_QUIESCEACK:
  219. ep->idtlen = msg_data;
  220. wake_up_interruptible(&ep->ep_wait);
  221. break;
  222. case XILLYMSG_OPCODE_FIFOEOF:
  223. if ((msg_channel > ep->num_channels) ||
  224. (msg_channel == 0) || (!msg_dir) ||
  225. !ep->channels[msg_channel]->num_wr_buffers) {
  226. malformed_message(ep, &buf[i]);
  227. break;
  228. }
  229. channel = ep->channels[msg_channel];
  230. spin_lock(&channel->wr_spinlock);
  231. channel->wr_eof = msg_bufno;
  232. channel->wr_sleepy = 0;
  233. channel->wr_hangup = channel->wr_empty &&
  234. (channel->wr_host_buf_idx == msg_bufno);
  235. spin_unlock(&channel->wr_spinlock);
  236. wake_up_interruptible(&channel->wr_wait);
  237. break;
  238. case XILLYMSG_OPCODE_FATAL_ERROR:
  239. ep->fatal_error = 1;
  240. wake_up_interruptible(&ep->ep_wait); /* For select() */
  241. dev_err(ep->dev,
  242. "FPGA reported a fatal error. This means that the low-level communication with the device has failed. This hardware problem is most likely unrelated to Xillybus (neither kernel module nor FPGA core), but reports are still welcome. All I/O is aborted.\n");
  243. break;
  244. default:
  245. malformed_message(ep, &buf[i]);
  246. break;
  247. }
  248. }
  249. ep->ephw->hw_sync_sgl_for_device(ep,
  250. ep->msgbuf_dma_addr,
  251. ep->msg_buf_size,
  252. DMA_FROM_DEVICE);
  253. ep->msg_counter = (ep->msg_counter + 1) & 0xf;
  254. ep->failed_messages = 0;
  255. iowrite32(0x03, ep->registers + fpga_msg_ctrl_reg); /* Message ACK */
  256. return IRQ_HANDLED;
  257. }
  258. EXPORT_SYMBOL(xillybus_isr);
  259. /*
  260. * A few trivial memory management functions.
  261. * NOTE: These functions are used only on probe and remove, and therefore
  262. * no locks are applied!
  263. */
  264. static void xillybus_autoflush(struct work_struct *work);
  265. struct xilly_alloc_state {
  266. void *salami;
  267. int left_of_salami;
  268. int nbuffer;
  269. enum dma_data_direction direction;
  270. u32 regdirection;
  271. };
  272. static int xilly_get_dma_buffers(struct xilly_endpoint *ep,
  273. struct xilly_alloc_state *s,
  274. struct xilly_buffer **buffers,
  275. int bufnum, int bytebufsize)
  276. {
  277. int i, rc;
  278. dma_addr_t dma_addr;
  279. struct device *dev = ep->dev;
  280. struct xilly_buffer *this_buffer = NULL; /* Init to silence warning */
  281. if (buffers) { /* Not the message buffer */
  282. this_buffer = devm_kcalloc(dev, bufnum,
  283. sizeof(struct xilly_buffer),
  284. GFP_KERNEL);
  285. if (!this_buffer)
  286. return -ENOMEM;
  287. }
  288. for (i = 0; i < bufnum; i++) {
  289. /*
  290. * Buffers are expected in descending size order, so there
  291. * is either enough space for this buffer or none at all.
  292. */
  293. if ((s->left_of_salami < bytebufsize) &&
  294. (s->left_of_salami > 0)) {
  295. dev_err(ep->dev,
  296. "Corrupt buffer allocation in IDT. Aborting.\n");
  297. return -ENODEV;
  298. }
  299. if (s->left_of_salami == 0) {
  300. int allocorder, allocsize;
  301. allocsize = PAGE_SIZE;
  302. allocorder = 0;
  303. while (bytebufsize > allocsize) {
  304. allocsize *= 2;
  305. allocorder++;
  306. }
  307. s->salami = (void *) devm_get_free_pages(
  308. dev,
  309. GFP_KERNEL | __GFP_DMA32 | __GFP_ZERO,
  310. allocorder);
  311. if (!s->salami)
  312. return -ENOMEM;
  313. s->left_of_salami = allocsize;
  314. }
  315. rc = ep->ephw->map_single(ep, s->salami,
  316. bytebufsize, s->direction,
  317. &dma_addr);
  318. if (rc)
  319. return rc;
  320. iowrite32((u32) (dma_addr & 0xffffffff),
  321. ep->registers + fpga_dma_bufaddr_lowaddr_reg);
  322. iowrite32(((u32) ((((u64) dma_addr) >> 32) & 0xffffffff)),
  323. ep->registers + fpga_dma_bufaddr_highaddr_reg);
  324. if (buffers) { /* Not the message buffer */
  325. this_buffer->addr = s->salami;
  326. this_buffer->dma_addr = dma_addr;
  327. buffers[i] = this_buffer++;
  328. iowrite32(s->regdirection | s->nbuffer++,
  329. ep->registers + fpga_dma_bufno_reg);
  330. } else {
  331. ep->msgbuf_addr = s->salami;
  332. ep->msgbuf_dma_addr = dma_addr;
  333. ep->msg_buf_size = bytebufsize;
  334. iowrite32(s->regdirection,
  335. ep->registers + fpga_dma_bufno_reg);
  336. }
  337. s->left_of_salami -= bytebufsize;
  338. s->salami += bytebufsize;
  339. }
  340. return 0;
  341. }
  342. static int xilly_setupchannels(struct xilly_endpoint *ep,
  343. unsigned char *chandesc,
  344. int entries)
  345. {
  346. struct device *dev = ep->dev;
  347. int i, entry, rc;
  348. struct xilly_channel *channel;
  349. int channelnum, bufnum, bufsize, format, is_writebuf;
  350. int bytebufsize;
  351. int synchronous, allowpartial, exclusive_open, seekable;
  352. int supports_nonempty;
  353. int msg_buf_done = 0;
  354. struct xilly_alloc_state rd_alloc = {
  355. .salami = NULL,
  356. .left_of_salami = 0,
  357. .nbuffer = 1,
  358. .direction = DMA_TO_DEVICE,
  359. .regdirection = 0,
  360. };
  361. struct xilly_alloc_state wr_alloc = {
  362. .salami = NULL,
  363. .left_of_salami = 0,
  364. .nbuffer = 1,
  365. .direction = DMA_FROM_DEVICE,
  366. .regdirection = 0x80000000,
  367. };
  368. channel = devm_kcalloc(dev, ep->num_channels,
  369. sizeof(struct xilly_channel), GFP_KERNEL);
  370. if (!channel)
  371. return -ENOMEM;
  372. ep->channels = devm_kcalloc(dev, ep->num_channels + 1,
  373. sizeof(struct xilly_channel *),
  374. GFP_KERNEL);
  375. if (!ep->channels)
  376. return -ENOMEM;
  377. ep->channels[0] = NULL; /* Channel 0 is message buf. */
  378. /* Initialize all channels with defaults */
  379. for (i = 1; i <= ep->num_channels; i++) {
  380. channel->wr_buffers = NULL;
  381. channel->rd_buffers = NULL;
  382. channel->num_wr_buffers = 0;
  383. channel->num_rd_buffers = 0;
  384. channel->wr_fpga_buf_idx = -1;
  385. channel->wr_host_buf_idx = 0;
  386. channel->wr_host_buf_pos = 0;
  387. channel->wr_empty = 1;
  388. channel->wr_ready = 0;
  389. channel->wr_sleepy = 1;
  390. channel->rd_fpga_buf_idx = 0;
  391. channel->rd_host_buf_idx = 0;
  392. channel->rd_host_buf_pos = 0;
  393. channel->rd_full = 0;
  394. channel->wr_ref_count = 0;
  395. channel->rd_ref_count = 0;
  396. spin_lock_init(&channel->wr_spinlock);
  397. spin_lock_init(&channel->rd_spinlock);
  398. mutex_init(&channel->wr_mutex);
  399. mutex_init(&channel->rd_mutex);
  400. init_waitqueue_head(&channel->rd_wait);
  401. init_waitqueue_head(&channel->wr_wait);
  402. init_waitqueue_head(&channel->wr_ready_wait);
  403. INIT_DELAYED_WORK(&channel->rd_workitem, xillybus_autoflush);
  404. channel->endpoint = ep;
  405. channel->chan_num = i;
  406. channel->log2_element_size = 0;
  407. ep->channels[i] = channel++;
  408. }
  409. for (entry = 0; entry < entries; entry++, chandesc += 4) {
  410. struct xilly_buffer **buffers = NULL;
  411. is_writebuf = chandesc[0] & 0x01;
  412. channelnum = (chandesc[0] >> 1) | ((chandesc[1] & 0x0f) << 7);
  413. format = (chandesc[1] >> 4) & 0x03;
  414. allowpartial = (chandesc[1] >> 6) & 0x01;
  415. synchronous = (chandesc[1] >> 7) & 0x01;
  416. bufsize = 1 << (chandesc[2] & 0x1f);
  417. bufnum = 1 << (chandesc[3] & 0x0f);
  418. exclusive_open = (chandesc[2] >> 7) & 0x01;
  419. seekable = (chandesc[2] >> 6) & 0x01;
  420. supports_nonempty = (chandesc[2] >> 5) & 0x01;
  421. if ((channelnum > ep->num_channels) ||
  422. ((channelnum == 0) && !is_writebuf)) {
  423. dev_err(ep->dev,
  424. "IDT requests channel out of range. Aborting.\n");
  425. return -ENODEV;
  426. }
  427. channel = ep->channels[channelnum]; /* NULL for msg channel */
  428. if (!is_writebuf || channelnum > 0) {
  429. channel->log2_element_size = ((format > 2) ?
  430. 2 : format);
  431. bytebufsize = channel->rd_buf_size = bufsize *
  432. (1 << channel->log2_element_size);
  433. buffers = devm_kcalloc(dev, bufnum,
  434. sizeof(struct xilly_buffer *),
  435. GFP_KERNEL);
  436. if (!buffers)
  437. return -ENOMEM;
  438. } else {
  439. bytebufsize = bufsize << 2;
  440. }
  441. if (!is_writebuf) {
  442. channel->num_rd_buffers = bufnum;
  443. channel->rd_allow_partial = allowpartial;
  444. channel->rd_synchronous = synchronous;
  445. channel->rd_exclusive_open = exclusive_open;
  446. channel->seekable = seekable;
  447. channel->rd_buffers = buffers;
  448. rc = xilly_get_dma_buffers(ep, &rd_alloc, buffers,
  449. bufnum, bytebufsize);
  450. } else if (channelnum > 0) {
  451. channel->num_wr_buffers = bufnum;
  452. channel->seekable = seekable;
  453. channel->wr_supports_nonempty = supports_nonempty;
  454. channel->wr_allow_partial = allowpartial;
  455. channel->wr_synchronous = synchronous;
  456. channel->wr_exclusive_open = exclusive_open;
  457. channel->wr_buffers = buffers;
  458. rc = xilly_get_dma_buffers(ep, &wr_alloc, buffers,
  459. bufnum, bytebufsize);
  460. } else {
  461. rc = xilly_get_dma_buffers(ep, &wr_alloc, NULL,
  462. bufnum, bytebufsize);
  463. msg_buf_done++;
  464. }
  465. if (rc)
  466. return -ENOMEM;
  467. }
  468. if (!msg_buf_done) {
  469. dev_err(ep->dev,
  470. "Corrupt IDT: No message buffer. Aborting.\n");
  471. return -ENODEV;
  472. }
  473. return 0;
  474. }
  475. static int xilly_scan_idt(struct xilly_endpoint *endpoint,
  476. struct xilly_idt_handle *idt_handle)
  477. {
  478. int count = 0;
  479. unsigned char *idt = endpoint->channels[1]->wr_buffers[0]->addr;
  480. unsigned char *end_of_idt = idt + endpoint->idtlen - 4;
  481. unsigned char *scan;
  482. int len;
  483. scan = idt;
  484. idt_handle->idt = idt;
  485. scan++; /* Skip version number */
  486. while ((scan <= end_of_idt) && *scan) {
  487. while ((scan <= end_of_idt) && *scan++)
  488. /* Do nothing, just scan thru string */;
  489. count++;
  490. }
  491. scan++;
  492. if (scan > end_of_idt) {
  493. dev_err(endpoint->dev,
  494. "IDT device name list overflow. Aborting.\n");
  495. return -ENODEV;
  496. }
  497. idt_handle->chandesc = scan;
  498. len = endpoint->idtlen - (3 + ((int) (scan - idt)));
  499. if (len & 0x03) {
  500. dev_err(endpoint->dev,
  501. "Corrupt IDT device name list. Aborting.\n");
  502. return -ENODEV;
  503. }
  504. idt_handle->entries = len >> 2;
  505. endpoint->num_channels = count;
  506. return 0;
  507. }
  508. static int xilly_obtain_idt(struct xilly_endpoint *endpoint)
  509. {
  510. struct xilly_channel *channel;
  511. unsigned char *version;
  512. long t;
  513. channel = endpoint->channels[1]; /* This should be generated ad-hoc */
  514. channel->wr_sleepy = 1;
  515. iowrite32(1 |
  516. (3 << 24), /* Opcode 3 for channel 0 = Send IDT */
  517. endpoint->registers + fpga_buf_ctrl_reg);
  518. t = wait_event_interruptible_timeout(channel->wr_wait,
  519. (!channel->wr_sleepy),
  520. XILLY_TIMEOUT);
  521. if (t <= 0) {
  522. dev_err(endpoint->dev, "Failed to obtain IDT. Aborting.\n");
  523. if (endpoint->fatal_error)
  524. return -EIO;
  525. return -ENODEV;
  526. }
  527. endpoint->ephw->hw_sync_sgl_for_cpu(
  528. channel->endpoint,
  529. channel->wr_buffers[0]->dma_addr,
  530. channel->wr_buf_size,
  531. DMA_FROM_DEVICE);
  532. if (channel->wr_buffers[0]->end_offset != endpoint->idtlen) {
  533. dev_err(endpoint->dev,
  534. "IDT length mismatch (%d != %d). Aborting.\n",
  535. channel->wr_buffers[0]->end_offset, endpoint->idtlen);
  536. return -ENODEV;
  537. }
  538. if (crc32_le(~0, channel->wr_buffers[0]->addr,
  539. endpoint->idtlen+1) != 0) {
  540. dev_err(endpoint->dev, "IDT failed CRC check. Aborting.\n");
  541. return -ENODEV;
  542. }
  543. version = channel->wr_buffers[0]->addr;
  544. /* Check version number. Accept anything below 0x82 for now. */
  545. if (*version > 0x82) {
  546. dev_err(endpoint->dev,
  547. "No support for IDT version 0x%02x. Maybe the xillybus driver needs an upgarde. Aborting.\n",
  548. *version);
  549. return -ENODEV;
  550. }
  551. return 0;
  552. }
  553. static ssize_t xillybus_read(struct file *filp, char __user *userbuf,
  554. size_t count, loff_t *f_pos)
  555. {
  556. ssize_t rc;
  557. unsigned long flags;
  558. int bytes_done = 0;
  559. int no_time_left = 0;
  560. long deadline, left_to_sleep;
  561. struct xilly_channel *channel = filp->private_data;
  562. int empty, reached_eof, exhausted, ready;
  563. /* Initializations are there only to silence warnings */
  564. int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
  565. int waiting_bufidx;
  566. if (channel->endpoint->fatal_error)
  567. return -EIO;
  568. deadline = jiffies + 1 + XILLY_RX_TIMEOUT;
  569. rc = mutex_lock_interruptible(&channel->wr_mutex);
  570. if (rc)
  571. return rc;
  572. while (1) { /* Note that we may drop mutex within this loop */
  573. int bytes_to_do = count - bytes_done;
  574. spin_lock_irqsave(&channel->wr_spinlock, flags);
  575. empty = channel->wr_empty;
  576. ready = !empty || channel->wr_ready;
  577. if (!empty) {
  578. bufidx = channel->wr_host_buf_idx;
  579. bufpos = channel->wr_host_buf_pos;
  580. howmany = ((channel->wr_buffers[bufidx]->end_offset
  581. + 1) << channel->log2_element_size)
  582. - bufpos;
  583. /* Update wr_host_* to its post-operation state */
  584. if (howmany > bytes_to_do) {
  585. bufferdone = 0;
  586. howmany = bytes_to_do;
  587. channel->wr_host_buf_pos += howmany;
  588. } else {
  589. bufferdone = 1;
  590. channel->wr_host_buf_pos = 0;
  591. if (bufidx == channel->wr_fpga_buf_idx) {
  592. channel->wr_empty = 1;
  593. channel->wr_sleepy = 1;
  594. channel->wr_ready = 0;
  595. }
  596. if (bufidx >= (channel->num_wr_buffers - 1))
  597. channel->wr_host_buf_idx = 0;
  598. else
  599. channel->wr_host_buf_idx++;
  600. }
  601. }
  602. /*
  603. * Marking our situation after the possible changes above,
  604. * for use after releasing the spinlock.
  605. *
  606. * empty = empty before change
  607. * exhasted = empty after possible change
  608. */
  609. reached_eof = channel->wr_empty &&
  610. (channel->wr_host_buf_idx == channel->wr_eof);
  611. channel->wr_hangup = reached_eof;
  612. exhausted = channel->wr_empty;
  613. waiting_bufidx = channel->wr_host_buf_idx;
  614. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  615. if (!empty) { /* Go on, now without the spinlock */
  616. if (bufpos == 0) /* Position zero means it's virgin */
  617. channel->endpoint->ephw->hw_sync_sgl_for_cpu(
  618. channel->endpoint,
  619. channel->wr_buffers[bufidx]->dma_addr,
  620. channel->wr_buf_size,
  621. DMA_FROM_DEVICE);
  622. if (copy_to_user(
  623. userbuf,
  624. channel->wr_buffers[bufidx]->addr
  625. + bufpos, howmany))
  626. rc = -EFAULT;
  627. userbuf += howmany;
  628. bytes_done += howmany;
  629. if (bufferdone) {
  630. channel->endpoint->ephw->hw_sync_sgl_for_device(
  631. channel->endpoint,
  632. channel->wr_buffers[bufidx]->dma_addr,
  633. channel->wr_buf_size,
  634. DMA_FROM_DEVICE);
  635. /*
  636. * Tell FPGA the buffer is done with. It's an
  637. * atomic operation to the FPGA, so what
  638. * happens with other channels doesn't matter,
  639. * and the certain channel is protected with
  640. * the channel-specific mutex.
  641. */
  642. iowrite32(1 | (channel->chan_num << 1) |
  643. (bufidx << 12),
  644. channel->endpoint->registers +
  645. fpga_buf_ctrl_reg);
  646. }
  647. if (rc) {
  648. mutex_unlock(&channel->wr_mutex);
  649. return rc;
  650. }
  651. }
  652. /* This includes a zero-count return = EOF */
  653. if ((bytes_done >= count) || reached_eof)
  654. break;
  655. if (!exhausted)
  656. continue; /* More in RAM buffer(s)? Just go on. */
  657. if ((bytes_done > 0) &&
  658. (no_time_left ||
  659. (channel->wr_synchronous && channel->wr_allow_partial)))
  660. break;
  661. /*
  662. * Nonblocking read: The "ready" flag tells us that the FPGA
  663. * has data to send. In non-blocking mode, if it isn't on,
  664. * just return. But if there is, we jump directly to the point
  665. * where we ask for the FPGA to send all it has, and wait
  666. * until that data arrives. So in a sense, we *do* block in
  667. * nonblocking mode, but only for a very short time.
  668. */
  669. if (!no_time_left && (filp->f_flags & O_NONBLOCK)) {
  670. if (bytes_done > 0)
  671. break;
  672. if (ready)
  673. goto desperate;
  674. rc = -EAGAIN;
  675. break;
  676. }
  677. if (!no_time_left || (bytes_done > 0)) {
  678. /*
  679. * Note that in case of an element-misaligned read
  680. * request, offsetlimit will include the last element,
  681. * which will be partially read from.
  682. */
  683. int offsetlimit = ((count - bytes_done) - 1) >>
  684. channel->log2_element_size;
  685. int buf_elements = channel->wr_buf_size >>
  686. channel->log2_element_size;
  687. /*
  688. * In synchronous mode, always send an offset limit.
  689. * Just don't send a value too big.
  690. */
  691. if (channel->wr_synchronous) {
  692. /* Don't request more than one buffer */
  693. if (channel->wr_allow_partial &&
  694. (offsetlimit >= buf_elements))
  695. offsetlimit = buf_elements - 1;
  696. /* Don't request more than all buffers */
  697. if (!channel->wr_allow_partial &&
  698. (offsetlimit >=
  699. (buf_elements * channel->num_wr_buffers)))
  700. offsetlimit = buf_elements *
  701. channel->num_wr_buffers - 1;
  702. }
  703. /*
  704. * In asynchronous mode, force early flush of a buffer
  705. * only if that will allow returning a full count. The
  706. * "offsetlimit < ( ... )" rather than "<=" excludes
  707. * requesting a full buffer, which would obviously
  708. * cause a buffer transmission anyhow
  709. */
  710. if (channel->wr_synchronous ||
  711. (offsetlimit < (buf_elements - 1))) {
  712. mutex_lock(&channel->endpoint->register_mutex);
  713. iowrite32(offsetlimit,
  714. channel->endpoint->registers +
  715. fpga_buf_offset_reg);
  716. iowrite32(1 | (channel->chan_num << 1) |
  717. (2 << 24) | /* 2 = offset limit */
  718. (waiting_bufidx << 12),
  719. channel->endpoint->registers +
  720. fpga_buf_ctrl_reg);
  721. mutex_unlock(&channel->endpoint->
  722. register_mutex);
  723. }
  724. }
  725. /*
  726. * If partial completion is disallowed, there is no point in
  727. * timeout sleeping. Neither if no_time_left is set and
  728. * there's no data.
  729. */
  730. if (!channel->wr_allow_partial ||
  731. (no_time_left && (bytes_done == 0))) {
  732. /*
  733. * This do-loop will run more than once if another
  734. * thread reasserted wr_sleepy before we got the mutex
  735. * back, so we try again.
  736. */
  737. do {
  738. mutex_unlock(&channel->wr_mutex);
  739. if (wait_event_interruptible(
  740. channel->wr_wait,
  741. (!channel->wr_sleepy)))
  742. goto interrupted;
  743. if (mutex_lock_interruptible(
  744. &channel->wr_mutex))
  745. goto interrupted;
  746. } while (channel->wr_sleepy);
  747. continue;
  748. interrupted: /* Mutex is not held if got here */
  749. if (channel->endpoint->fatal_error)
  750. return -EIO;
  751. if (bytes_done)
  752. return bytes_done;
  753. if (filp->f_flags & O_NONBLOCK)
  754. return -EAGAIN; /* Don't admit snoozing */
  755. return -EINTR;
  756. }
  757. left_to_sleep = deadline - ((long) jiffies);
  758. /*
  759. * If our time is out, skip the waiting. We may miss wr_sleepy
  760. * being deasserted but hey, almost missing the train is like
  761. * missing it.
  762. */
  763. if (left_to_sleep > 0) {
  764. left_to_sleep =
  765. wait_event_interruptible_timeout(
  766. channel->wr_wait,
  767. (!channel->wr_sleepy),
  768. left_to_sleep);
  769. if (left_to_sleep > 0) /* wr_sleepy deasserted */
  770. continue;
  771. if (left_to_sleep < 0) { /* Interrupt */
  772. mutex_unlock(&channel->wr_mutex);
  773. if (channel->endpoint->fatal_error)
  774. return -EIO;
  775. if (bytes_done)
  776. return bytes_done;
  777. return -EINTR;
  778. }
  779. }
  780. desperate:
  781. no_time_left = 1; /* We're out of sleeping time. Desperate! */
  782. if (bytes_done == 0) {
  783. /*
  784. * Reaching here means that we allow partial return,
  785. * that we've run out of time, and that we have
  786. * nothing to return.
  787. * So tell the FPGA to send anything it has or gets.
  788. */
  789. iowrite32(1 | (channel->chan_num << 1) |
  790. (3 << 24) | /* Opcode 3, flush it all! */
  791. (waiting_bufidx << 12),
  792. channel->endpoint->registers +
  793. fpga_buf_ctrl_reg);
  794. }
  795. /*
  796. * Reaching here means that we *do* have data in the buffer,
  797. * but the "partial" flag disallows returning less than
  798. * required. And we don't have as much. So loop again,
  799. * which is likely to end up blocking indefinitely until
  800. * enough data has arrived.
  801. */
  802. }
  803. mutex_unlock(&channel->wr_mutex);
  804. if (channel->endpoint->fatal_error)
  805. return -EIO;
  806. if (rc)
  807. return rc;
  808. return bytes_done;
  809. }
  810. /*
  811. * The timeout argument takes values as follows:
  812. * >0 : Flush with timeout
  813. * ==0 : Flush, and wait idefinitely for the flush to complete
  814. * <0 : Autoflush: Flush only if there's a single buffer occupied
  815. */
  816. static int xillybus_myflush(struct xilly_channel *channel, long timeout)
  817. {
  818. int rc;
  819. unsigned long flags;
  820. int end_offset_plus1;
  821. int bufidx, bufidx_minus1;
  822. int i;
  823. int empty;
  824. int new_rd_host_buf_pos;
  825. if (channel->endpoint->fatal_error)
  826. return -EIO;
  827. rc = mutex_lock_interruptible(&channel->rd_mutex);
  828. if (rc)
  829. return rc;
  830. /*
  831. * Don't flush a closed channel. This can happen when the work queued
  832. * autoflush thread fires off after the file has closed. This is not
  833. * an error, just something to dismiss.
  834. */
  835. if (!channel->rd_ref_count)
  836. goto done;
  837. bufidx = channel->rd_host_buf_idx;
  838. bufidx_minus1 = (bufidx == 0) ?
  839. channel->num_rd_buffers - 1 :
  840. bufidx - 1;
  841. end_offset_plus1 = channel->rd_host_buf_pos >>
  842. channel->log2_element_size;
  843. new_rd_host_buf_pos = channel->rd_host_buf_pos -
  844. (end_offset_plus1 << channel->log2_element_size);
  845. /* Submit the current buffer if it's nonempty */
  846. if (end_offset_plus1) {
  847. unsigned char *tail = channel->rd_buffers[bufidx]->addr +
  848. (end_offset_plus1 << channel->log2_element_size);
  849. /* Copy unflushed data, so we can put it in next buffer */
  850. for (i = 0; i < new_rd_host_buf_pos; i++)
  851. channel->rd_leftovers[i] = *tail++;
  852. spin_lock_irqsave(&channel->rd_spinlock, flags);
  853. /* Autoflush only if a single buffer is occupied */
  854. if ((timeout < 0) &&
  855. (channel->rd_full ||
  856. (bufidx_minus1 != channel->rd_fpga_buf_idx))) {
  857. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  858. /*
  859. * A new work item may be queued by the ISR exactly
  860. * now, since the execution of a work item allows the
  861. * queuing of a new one while it's running.
  862. */
  863. goto done;
  864. }
  865. /* The 4th element is never needed for data, so it's a flag */
  866. channel->rd_leftovers[3] = (new_rd_host_buf_pos != 0);
  867. /* Set up rd_full to reflect a certain moment's state */
  868. if (bufidx == channel->rd_fpga_buf_idx)
  869. channel->rd_full = 1;
  870. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  871. if (bufidx >= (channel->num_rd_buffers - 1))
  872. channel->rd_host_buf_idx = 0;
  873. else
  874. channel->rd_host_buf_idx++;
  875. channel->endpoint->ephw->hw_sync_sgl_for_device(
  876. channel->endpoint,
  877. channel->rd_buffers[bufidx]->dma_addr,
  878. channel->rd_buf_size,
  879. DMA_TO_DEVICE);
  880. mutex_lock(&channel->endpoint->register_mutex);
  881. iowrite32(end_offset_plus1 - 1,
  882. channel->endpoint->registers + fpga_buf_offset_reg);
  883. iowrite32((channel->chan_num << 1) | /* Channel ID */
  884. (2 << 24) | /* Opcode 2, submit buffer */
  885. (bufidx << 12),
  886. channel->endpoint->registers + fpga_buf_ctrl_reg);
  887. mutex_unlock(&channel->endpoint->register_mutex);
  888. } else if (bufidx == 0) {
  889. bufidx = channel->num_rd_buffers - 1;
  890. } else {
  891. bufidx--;
  892. }
  893. channel->rd_host_buf_pos = new_rd_host_buf_pos;
  894. if (timeout < 0)
  895. goto done; /* Autoflush */
  896. /*
  897. * bufidx is now the last buffer written to (or equal to
  898. * rd_fpga_buf_idx if buffer was never written to), and
  899. * channel->rd_host_buf_idx the one after it.
  900. *
  901. * If bufidx == channel->rd_fpga_buf_idx we're either empty or full.
  902. */
  903. while (1) { /* Loop waiting for draining of buffers */
  904. spin_lock_irqsave(&channel->rd_spinlock, flags);
  905. if (bufidx != channel->rd_fpga_buf_idx)
  906. channel->rd_full = 1; /*
  907. * Not really full,
  908. * but needs waiting.
  909. */
  910. empty = !channel->rd_full;
  911. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  912. if (empty)
  913. break;
  914. /*
  915. * Indefinite sleep with mutex taken. With data waiting for
  916. * flushing user should not be surprised if open() for write
  917. * sleeps.
  918. */
  919. if (timeout == 0)
  920. wait_event_interruptible(channel->rd_wait,
  921. (!channel->rd_full));
  922. else if (wait_event_interruptible_timeout(
  923. channel->rd_wait,
  924. (!channel->rd_full),
  925. timeout) == 0) {
  926. dev_warn(channel->endpoint->dev,
  927. "Timed out while flushing. Output data may be lost.\n");
  928. rc = -ETIMEDOUT;
  929. break;
  930. }
  931. if (channel->rd_full) {
  932. rc = -EINTR;
  933. break;
  934. }
  935. }
  936. done:
  937. mutex_unlock(&channel->rd_mutex);
  938. if (channel->endpoint->fatal_error)
  939. return -EIO;
  940. return rc;
  941. }
  942. static int xillybus_flush(struct file *filp, fl_owner_t id)
  943. {
  944. if (!(filp->f_mode & FMODE_WRITE))
  945. return 0;
  946. return xillybus_myflush(filp->private_data, HZ); /* 1 second timeout */
  947. }
  948. static void xillybus_autoflush(struct work_struct *work)
  949. {
  950. struct delayed_work *workitem = container_of(
  951. work, struct delayed_work, work);
  952. struct xilly_channel *channel = container_of(
  953. workitem, struct xilly_channel, rd_workitem);
  954. int rc;
  955. rc = xillybus_myflush(channel, -1);
  956. if (rc == -EINTR)
  957. dev_warn(channel->endpoint->dev,
  958. "Autoflush failed because work queue thread got a signal.\n");
  959. else if (rc)
  960. dev_err(channel->endpoint->dev,
  961. "Autoflush failed under weird circumstances.\n");
  962. }
  963. static ssize_t xillybus_write(struct file *filp, const char __user *userbuf,
  964. size_t count, loff_t *f_pos)
  965. {
  966. ssize_t rc;
  967. unsigned long flags;
  968. int bytes_done = 0;
  969. struct xilly_channel *channel = filp->private_data;
  970. int full, exhausted;
  971. /* Initializations are there only to silence warnings */
  972. int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
  973. int end_offset_plus1 = 0;
  974. if (channel->endpoint->fatal_error)
  975. return -EIO;
  976. rc = mutex_lock_interruptible(&channel->rd_mutex);
  977. if (rc)
  978. return rc;
  979. while (1) {
  980. int bytes_to_do = count - bytes_done;
  981. spin_lock_irqsave(&channel->rd_spinlock, flags);
  982. full = channel->rd_full;
  983. if (!full) {
  984. bufidx = channel->rd_host_buf_idx;
  985. bufpos = channel->rd_host_buf_pos;
  986. howmany = channel->rd_buf_size - bufpos;
  987. /*
  988. * Update rd_host_* to its state after this operation.
  989. * count=0 means committing the buffer immediately,
  990. * which is like flushing, but not necessarily block.
  991. */
  992. if ((howmany > bytes_to_do) &&
  993. (count ||
  994. ((bufpos >> channel->log2_element_size) == 0))) {
  995. bufferdone = 0;
  996. howmany = bytes_to_do;
  997. channel->rd_host_buf_pos += howmany;
  998. } else {
  999. bufferdone = 1;
  1000. if (count) {
  1001. end_offset_plus1 =
  1002. channel->rd_buf_size >>
  1003. channel->log2_element_size;
  1004. channel->rd_host_buf_pos = 0;
  1005. } else {
  1006. unsigned char *tail;
  1007. int i;
  1008. howmany = 0;
  1009. end_offset_plus1 = bufpos >>
  1010. channel->log2_element_size;
  1011. channel->rd_host_buf_pos -=
  1012. end_offset_plus1 <<
  1013. channel->log2_element_size;
  1014. tail = channel->
  1015. rd_buffers[bufidx]->addr +
  1016. (end_offset_plus1 <<
  1017. channel->log2_element_size);
  1018. for (i = 0;
  1019. i < channel->rd_host_buf_pos;
  1020. i++)
  1021. channel->rd_leftovers[i] =
  1022. *tail++;
  1023. }
  1024. if (bufidx == channel->rd_fpga_buf_idx)
  1025. channel->rd_full = 1;
  1026. if (bufidx >= (channel->num_rd_buffers - 1))
  1027. channel->rd_host_buf_idx = 0;
  1028. else
  1029. channel->rd_host_buf_idx++;
  1030. }
  1031. }
  1032. /*
  1033. * Marking our situation after the possible changes above,
  1034. * for use after releasing the spinlock.
  1035. *
  1036. * full = full before change
  1037. * exhasted = full after possible change
  1038. */
  1039. exhausted = channel->rd_full;
  1040. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1041. if (!full) { /* Go on, now without the spinlock */
  1042. unsigned char *head =
  1043. channel->rd_buffers[bufidx]->addr;
  1044. int i;
  1045. if ((bufpos == 0) || /* Zero means it's virgin */
  1046. (channel->rd_leftovers[3] != 0)) {
  1047. channel->endpoint->ephw->hw_sync_sgl_for_cpu(
  1048. channel->endpoint,
  1049. channel->rd_buffers[bufidx]->dma_addr,
  1050. channel->rd_buf_size,
  1051. DMA_TO_DEVICE);
  1052. /* Virgin, but leftovers are due */
  1053. for (i = 0; i < bufpos; i++)
  1054. *head++ = channel->rd_leftovers[i];
  1055. channel->rd_leftovers[3] = 0; /* Clear flag */
  1056. }
  1057. if (copy_from_user(
  1058. channel->rd_buffers[bufidx]->addr + bufpos,
  1059. userbuf, howmany))
  1060. rc = -EFAULT;
  1061. userbuf += howmany;
  1062. bytes_done += howmany;
  1063. if (bufferdone) {
  1064. channel->endpoint->ephw->hw_sync_sgl_for_device(
  1065. channel->endpoint,
  1066. channel->rd_buffers[bufidx]->dma_addr,
  1067. channel->rd_buf_size,
  1068. DMA_TO_DEVICE);
  1069. mutex_lock(&channel->endpoint->register_mutex);
  1070. iowrite32(end_offset_plus1 - 1,
  1071. channel->endpoint->registers +
  1072. fpga_buf_offset_reg);
  1073. iowrite32((channel->chan_num << 1) |
  1074. (2 << 24) | /* 2 = submit buffer */
  1075. (bufidx << 12),
  1076. channel->endpoint->registers +
  1077. fpga_buf_ctrl_reg);
  1078. mutex_unlock(&channel->endpoint->
  1079. register_mutex);
  1080. channel->rd_leftovers[3] =
  1081. (channel->rd_host_buf_pos != 0);
  1082. }
  1083. if (rc) {
  1084. mutex_unlock(&channel->rd_mutex);
  1085. if (channel->endpoint->fatal_error)
  1086. return -EIO;
  1087. if (!channel->rd_synchronous)
  1088. queue_delayed_work(
  1089. xillybus_wq,
  1090. &channel->rd_workitem,
  1091. XILLY_RX_TIMEOUT);
  1092. return rc;
  1093. }
  1094. }
  1095. if (bytes_done >= count)
  1096. break;
  1097. if (!exhausted)
  1098. continue; /* If there's more space, just go on */
  1099. if ((bytes_done > 0) && channel->rd_allow_partial)
  1100. break;
  1101. /*
  1102. * Indefinite sleep with mutex taken. With data waiting for
  1103. * flushing, user should not be surprised if open() for write
  1104. * sleeps.
  1105. */
  1106. if (filp->f_flags & O_NONBLOCK) {
  1107. rc = -EAGAIN;
  1108. break;
  1109. }
  1110. if (wait_event_interruptible(channel->rd_wait,
  1111. (!channel->rd_full))) {
  1112. mutex_unlock(&channel->rd_mutex);
  1113. if (channel->endpoint->fatal_error)
  1114. return -EIO;
  1115. if (bytes_done)
  1116. return bytes_done;
  1117. return -EINTR;
  1118. }
  1119. }
  1120. mutex_unlock(&channel->rd_mutex);
  1121. if (!channel->rd_synchronous)
  1122. queue_delayed_work(xillybus_wq,
  1123. &channel->rd_workitem,
  1124. XILLY_RX_TIMEOUT);
  1125. if (channel->endpoint->fatal_error)
  1126. return -EIO;
  1127. if (rc)
  1128. return rc;
  1129. if ((channel->rd_synchronous) && (bytes_done > 0)) {
  1130. rc = xillybus_myflush(filp->private_data, 0); /* No timeout */
  1131. if (rc && (rc != -EINTR))
  1132. return rc;
  1133. }
  1134. return bytes_done;
  1135. }
  1136. static int xillybus_open(struct inode *inode, struct file *filp)
  1137. {
  1138. int rc = 0;
  1139. unsigned long flags;
  1140. int minor = iminor(inode);
  1141. int major = imajor(inode);
  1142. struct xilly_endpoint *ep_iter, *endpoint = NULL;
  1143. struct xilly_channel *channel;
  1144. mutex_lock(&ep_list_lock);
  1145. list_for_each_entry(ep_iter, &list_of_endpoints, ep_list) {
  1146. if ((ep_iter->major == major) &&
  1147. (minor >= ep_iter->lowest_minor) &&
  1148. (minor < (ep_iter->lowest_minor +
  1149. ep_iter->num_channels))) {
  1150. endpoint = ep_iter;
  1151. break;
  1152. }
  1153. }
  1154. mutex_unlock(&ep_list_lock);
  1155. if (!endpoint) {
  1156. pr_err("xillybus: open() failed to find a device for major=%d and minor=%d\n",
  1157. major, minor);
  1158. return -ENODEV;
  1159. }
  1160. if (endpoint->fatal_error)
  1161. return -EIO;
  1162. channel = endpoint->channels[1 + minor - endpoint->lowest_minor];
  1163. filp->private_data = channel;
  1164. /*
  1165. * It gets complicated because:
  1166. * 1. We don't want to take a mutex we don't have to
  1167. * 2. We don't want to open one direction if the other will fail.
  1168. */
  1169. if ((filp->f_mode & FMODE_READ) && (!channel->num_wr_buffers))
  1170. return -ENODEV;
  1171. if ((filp->f_mode & FMODE_WRITE) && (!channel->num_rd_buffers))
  1172. return -ENODEV;
  1173. if ((filp->f_mode & FMODE_READ) && (filp->f_flags & O_NONBLOCK) &&
  1174. (channel->wr_synchronous || !channel->wr_allow_partial ||
  1175. !channel->wr_supports_nonempty)) {
  1176. dev_err(endpoint->dev,
  1177. "open() failed: O_NONBLOCK not allowed for read on this device\n");
  1178. return -ENODEV;
  1179. }
  1180. if ((filp->f_mode & FMODE_WRITE) && (filp->f_flags & O_NONBLOCK) &&
  1181. (channel->rd_synchronous || !channel->rd_allow_partial)) {
  1182. dev_err(endpoint->dev,
  1183. "open() failed: O_NONBLOCK not allowed for write on this device\n");
  1184. return -ENODEV;
  1185. }
  1186. /*
  1187. * Note: open() may block on getting mutexes despite O_NONBLOCK.
  1188. * This shouldn't occur normally, since multiple open of the same
  1189. * file descriptor is almost always prohibited anyhow
  1190. * (*_exclusive_open is normally set in real-life systems).
  1191. */
  1192. if (filp->f_mode & FMODE_READ) {
  1193. rc = mutex_lock_interruptible(&channel->wr_mutex);
  1194. if (rc)
  1195. return rc;
  1196. }
  1197. if (filp->f_mode & FMODE_WRITE) {
  1198. rc = mutex_lock_interruptible(&channel->rd_mutex);
  1199. if (rc)
  1200. goto unlock_wr;
  1201. }
  1202. if ((filp->f_mode & FMODE_READ) &&
  1203. (channel->wr_ref_count != 0) &&
  1204. (channel->wr_exclusive_open)) {
  1205. rc = -EBUSY;
  1206. goto unlock;
  1207. }
  1208. if ((filp->f_mode & FMODE_WRITE) &&
  1209. (channel->rd_ref_count != 0) &&
  1210. (channel->rd_exclusive_open)) {
  1211. rc = -EBUSY;
  1212. goto unlock;
  1213. }
  1214. if (filp->f_mode & FMODE_READ) {
  1215. if (channel->wr_ref_count == 0) { /* First open of file */
  1216. /* Move the host to first buffer */
  1217. spin_lock_irqsave(&channel->wr_spinlock, flags);
  1218. channel->wr_host_buf_idx = 0;
  1219. channel->wr_host_buf_pos = 0;
  1220. channel->wr_fpga_buf_idx = -1;
  1221. channel->wr_empty = 1;
  1222. channel->wr_ready = 0;
  1223. channel->wr_sleepy = 1;
  1224. channel->wr_eof = -1;
  1225. channel->wr_hangup = 0;
  1226. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  1227. iowrite32(1 | (channel->chan_num << 1) |
  1228. (4 << 24) | /* Opcode 4, open channel */
  1229. ((channel->wr_synchronous & 1) << 23),
  1230. channel->endpoint->registers +
  1231. fpga_buf_ctrl_reg);
  1232. }
  1233. channel->wr_ref_count++;
  1234. }
  1235. if (filp->f_mode & FMODE_WRITE) {
  1236. if (channel->rd_ref_count == 0) { /* First open of file */
  1237. /* Move the host to first buffer */
  1238. spin_lock_irqsave(&channel->rd_spinlock, flags);
  1239. channel->rd_host_buf_idx = 0;
  1240. channel->rd_host_buf_pos = 0;
  1241. channel->rd_leftovers[3] = 0; /* No leftovers. */
  1242. channel->rd_fpga_buf_idx = channel->num_rd_buffers - 1;
  1243. channel->rd_full = 0;
  1244. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1245. iowrite32((channel->chan_num << 1) |
  1246. (4 << 24), /* Opcode 4, open channel */
  1247. channel->endpoint->registers +
  1248. fpga_buf_ctrl_reg);
  1249. }
  1250. channel->rd_ref_count++;
  1251. }
  1252. unlock:
  1253. if (filp->f_mode & FMODE_WRITE)
  1254. mutex_unlock(&channel->rd_mutex);
  1255. unlock_wr:
  1256. if (filp->f_mode & FMODE_READ)
  1257. mutex_unlock(&channel->wr_mutex);
  1258. if (!rc && (!channel->seekable))
  1259. return nonseekable_open(inode, filp);
  1260. return rc;
  1261. }
  1262. static int xillybus_release(struct inode *inode, struct file *filp)
  1263. {
  1264. unsigned long flags;
  1265. struct xilly_channel *channel = filp->private_data;
  1266. int buf_idx;
  1267. int eof;
  1268. if (channel->endpoint->fatal_error)
  1269. return -EIO;
  1270. if (filp->f_mode & FMODE_WRITE) {
  1271. mutex_lock(&channel->rd_mutex);
  1272. channel->rd_ref_count--;
  1273. if (channel->rd_ref_count == 0) {
  1274. /*
  1275. * We rely on the kernel calling flush()
  1276. * before we get here.
  1277. */
  1278. iowrite32((channel->chan_num << 1) | /* Channel ID */
  1279. (5 << 24), /* Opcode 5, close channel */
  1280. channel->endpoint->registers +
  1281. fpga_buf_ctrl_reg);
  1282. }
  1283. mutex_unlock(&channel->rd_mutex);
  1284. }
  1285. if (filp->f_mode & FMODE_READ) {
  1286. mutex_lock(&channel->wr_mutex);
  1287. channel->wr_ref_count--;
  1288. if (channel->wr_ref_count == 0) {
  1289. iowrite32(1 | (channel->chan_num << 1) |
  1290. (5 << 24), /* Opcode 5, close channel */
  1291. channel->endpoint->registers +
  1292. fpga_buf_ctrl_reg);
  1293. /*
  1294. * This is crazily cautious: We make sure that not
  1295. * only that we got an EOF (be it because we closed
  1296. * the channel or because of a user's EOF), but verify
  1297. * that it's one beyond the last buffer arrived, so
  1298. * we have no leftover buffers pending before wrapping
  1299. * up (which can only happen in asynchronous channels,
  1300. * BTW)
  1301. */
  1302. while (1) {
  1303. spin_lock_irqsave(&channel->wr_spinlock,
  1304. flags);
  1305. buf_idx = channel->wr_fpga_buf_idx;
  1306. eof = channel->wr_eof;
  1307. channel->wr_sleepy = 1;
  1308. spin_unlock_irqrestore(&channel->wr_spinlock,
  1309. flags);
  1310. /*
  1311. * Check if eof points at the buffer after
  1312. * the last one the FPGA submitted. Note that
  1313. * no EOF is marked by negative eof.
  1314. */
  1315. buf_idx++;
  1316. if (buf_idx == channel->num_wr_buffers)
  1317. buf_idx = 0;
  1318. if (buf_idx == eof)
  1319. break;
  1320. /*
  1321. * Steal extra 100 ms if awaken by interrupt.
  1322. * This is a simple workaround for an
  1323. * interrupt pending when entering, which would
  1324. * otherwise result in declaring the hardware
  1325. * non-responsive.
  1326. */
  1327. if (wait_event_interruptible(
  1328. channel->wr_wait,
  1329. (!channel->wr_sleepy)))
  1330. msleep(100);
  1331. if (channel->wr_sleepy) {
  1332. mutex_unlock(&channel->wr_mutex);
  1333. dev_warn(channel->endpoint->dev,
  1334. "Hardware failed to respond to close command, therefore left in messy state.\n");
  1335. return -EINTR;
  1336. }
  1337. }
  1338. }
  1339. mutex_unlock(&channel->wr_mutex);
  1340. }
  1341. return 0;
  1342. }
  1343. static loff_t xillybus_llseek(struct file *filp, loff_t offset, int whence)
  1344. {
  1345. struct xilly_channel *channel = filp->private_data;
  1346. loff_t pos = filp->f_pos;
  1347. int rc = 0;
  1348. /*
  1349. * Take both mutexes not allowing interrupts, since it seems like
  1350. * common applications don't expect an -EINTR here. Besides, multiple
  1351. * access to a single file descriptor on seekable devices is a mess
  1352. * anyhow.
  1353. */
  1354. if (channel->endpoint->fatal_error)
  1355. return -EIO;
  1356. mutex_lock(&channel->wr_mutex);
  1357. mutex_lock(&channel->rd_mutex);
  1358. switch (whence) {
  1359. case SEEK_SET:
  1360. pos = offset;
  1361. break;
  1362. case SEEK_CUR:
  1363. pos += offset;
  1364. break;
  1365. case SEEK_END:
  1366. pos = offset; /* Going to the end => to the beginning */
  1367. break;
  1368. default:
  1369. rc = -EINVAL;
  1370. goto end;
  1371. }
  1372. /* In any case, we must finish on an element boundary */
  1373. if (pos & ((1 << channel->log2_element_size) - 1)) {
  1374. rc = -EINVAL;
  1375. goto end;
  1376. }
  1377. mutex_lock(&channel->endpoint->register_mutex);
  1378. iowrite32(pos >> channel->log2_element_size,
  1379. channel->endpoint->registers + fpga_buf_offset_reg);
  1380. iowrite32((channel->chan_num << 1) |
  1381. (6 << 24), /* Opcode 6, set address */
  1382. channel->endpoint->registers + fpga_buf_ctrl_reg);
  1383. mutex_unlock(&channel->endpoint->register_mutex);
  1384. end:
  1385. mutex_unlock(&channel->rd_mutex);
  1386. mutex_unlock(&channel->wr_mutex);
  1387. if (rc) /* Return error after releasing mutexes */
  1388. return rc;
  1389. filp->f_pos = pos;
  1390. /*
  1391. * Since seekable devices are allowed only when the channel is
  1392. * synchronous, we assume that there is no data pending in either
  1393. * direction (which holds true as long as no concurrent access on the
  1394. * file descriptor takes place).
  1395. * The only thing we may need to throw away is leftovers from partial
  1396. * write() flush.
  1397. */
  1398. channel->rd_leftovers[3] = 0;
  1399. return pos;
  1400. }
  1401. static unsigned int xillybus_poll(struct file *filp, poll_table *wait)
  1402. {
  1403. struct xilly_channel *channel = filp->private_data;
  1404. unsigned int mask = 0;
  1405. unsigned long flags;
  1406. poll_wait(filp, &channel->endpoint->ep_wait, wait);
  1407. /*
  1408. * poll() won't play ball regarding read() channels which
  1409. * aren't asynchronous and support the nonempty message. Allowing
  1410. * that will create situations where data has been delivered at
  1411. * the FPGA, and users expecting select() to wake up, which it may
  1412. * not.
  1413. */
  1414. if (!channel->wr_synchronous && channel->wr_supports_nonempty) {
  1415. poll_wait(filp, &channel->wr_wait, wait);
  1416. poll_wait(filp, &channel->wr_ready_wait, wait);
  1417. spin_lock_irqsave(&channel->wr_spinlock, flags);
  1418. if (!channel->wr_empty || channel->wr_ready)
  1419. mask |= POLLIN | POLLRDNORM;
  1420. if (channel->wr_hangup)
  1421. /*
  1422. * Not POLLHUP, because its behavior is in the
  1423. * mist, and POLLIN does what we want: Wake up
  1424. * the read file descriptor so it sees EOF.
  1425. */
  1426. mask |= POLLIN | POLLRDNORM;
  1427. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  1428. }
  1429. /*
  1430. * If partial data write is disallowed on a write() channel,
  1431. * it's pointless to ever signal OK to write, because is could
  1432. * block despite some space being available.
  1433. */
  1434. if (channel->rd_allow_partial) {
  1435. poll_wait(filp, &channel->rd_wait, wait);
  1436. spin_lock_irqsave(&channel->rd_spinlock, flags);
  1437. if (!channel->rd_full)
  1438. mask |= POLLOUT | POLLWRNORM;
  1439. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1440. }
  1441. if (channel->endpoint->fatal_error)
  1442. mask |= POLLERR;
  1443. return mask;
  1444. }
  1445. static const struct file_operations xillybus_fops = {
  1446. .owner = THIS_MODULE,
  1447. .read = xillybus_read,
  1448. .write = xillybus_write,
  1449. .open = xillybus_open,
  1450. .flush = xillybus_flush,
  1451. .release = xillybus_release,
  1452. .llseek = xillybus_llseek,
  1453. .poll = xillybus_poll,
  1454. };
  1455. static int xillybus_init_chrdev(struct xilly_endpoint *endpoint,
  1456. const unsigned char *idt)
  1457. {
  1458. int rc;
  1459. dev_t dev;
  1460. int devnum, i, minor, major;
  1461. char devname[48];
  1462. struct device *device;
  1463. rc = alloc_chrdev_region(&dev, 0, /* minor start */
  1464. endpoint->num_channels,
  1465. xillyname);
  1466. if (rc) {
  1467. dev_warn(endpoint->dev, "Failed to obtain major/minors");
  1468. return rc;
  1469. }
  1470. endpoint->major = major = MAJOR(dev);
  1471. endpoint->lowest_minor = minor = MINOR(dev);
  1472. cdev_init(&endpoint->cdev, &xillybus_fops);
  1473. endpoint->cdev.owner = endpoint->ephw->owner;
  1474. rc = cdev_add(&endpoint->cdev, MKDEV(major, minor),
  1475. endpoint->num_channels);
  1476. if (rc) {
  1477. dev_warn(endpoint->dev, "Failed to add cdev. Aborting.\n");
  1478. goto unregister_chrdev;
  1479. }
  1480. idt++;
  1481. for (i = minor, devnum = 0;
  1482. devnum < endpoint->num_channels;
  1483. devnum++, i++) {
  1484. snprintf(devname, sizeof(devname)-1, "xillybus_%s", idt);
  1485. devname[sizeof(devname)-1] = 0; /* Should never matter */
  1486. while (*idt++)
  1487. /* Skip to next */;
  1488. device = device_create(xillybus_class,
  1489. NULL,
  1490. MKDEV(major, i),
  1491. NULL,
  1492. "%s", devname);
  1493. if (IS_ERR(device)) {
  1494. dev_warn(endpoint->dev,
  1495. "Failed to create %s device. Aborting.\n",
  1496. devname);
  1497. rc = -ENODEV;
  1498. goto unroll_device_create;
  1499. }
  1500. }
  1501. dev_info(endpoint->dev, "Created %d device files.\n",
  1502. endpoint->num_channels);
  1503. return 0; /* succeed */
  1504. unroll_device_create:
  1505. devnum--; i--;
  1506. for (; devnum >= 0; devnum--, i--)
  1507. device_destroy(xillybus_class, MKDEV(major, i));
  1508. cdev_del(&endpoint->cdev);
  1509. unregister_chrdev:
  1510. unregister_chrdev_region(MKDEV(major, minor), endpoint->num_channels);
  1511. return rc;
  1512. }
  1513. static void xillybus_cleanup_chrdev(struct xilly_endpoint *endpoint)
  1514. {
  1515. int minor;
  1516. for (minor = endpoint->lowest_minor;
  1517. minor < (endpoint->lowest_minor + endpoint->num_channels);
  1518. minor++)
  1519. device_destroy(xillybus_class, MKDEV(endpoint->major, minor));
  1520. cdev_del(&endpoint->cdev);
  1521. unregister_chrdev_region(MKDEV(endpoint->major,
  1522. endpoint->lowest_minor),
  1523. endpoint->num_channels);
  1524. dev_info(endpoint->dev, "Removed %d device files.\n",
  1525. endpoint->num_channels);
  1526. }
  1527. struct xilly_endpoint *xillybus_init_endpoint(struct pci_dev *pdev,
  1528. struct device *dev,
  1529. struct xilly_endpoint_hardware
  1530. *ephw)
  1531. {
  1532. struct xilly_endpoint *endpoint;
  1533. endpoint = devm_kzalloc(dev, sizeof(*endpoint), GFP_KERNEL);
  1534. if (!endpoint)
  1535. return NULL;
  1536. endpoint->pdev = pdev;
  1537. endpoint->dev = dev;
  1538. endpoint->ephw = ephw;
  1539. endpoint->msg_counter = 0x0b;
  1540. endpoint->failed_messages = 0;
  1541. endpoint->fatal_error = 0;
  1542. init_waitqueue_head(&endpoint->ep_wait);
  1543. mutex_init(&endpoint->register_mutex);
  1544. return endpoint;
  1545. }
  1546. EXPORT_SYMBOL(xillybus_init_endpoint);
  1547. static int xilly_quiesce(struct xilly_endpoint *endpoint)
  1548. {
  1549. long t;
  1550. endpoint->idtlen = -1;
  1551. iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
  1552. endpoint->registers + fpga_dma_control_reg);
  1553. t = wait_event_interruptible_timeout(endpoint->ep_wait,
  1554. (endpoint->idtlen >= 0),
  1555. XILLY_TIMEOUT);
  1556. if (t <= 0) {
  1557. dev_err(endpoint->dev,
  1558. "Failed to quiesce the device on exit.\n");
  1559. return -ENODEV;
  1560. }
  1561. return 0;
  1562. }
  1563. int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
  1564. {
  1565. int rc;
  1566. long t;
  1567. void *bootstrap_resources;
  1568. int idtbuffersize = (1 << PAGE_SHIFT);
  1569. struct device *dev = endpoint->dev;
  1570. /*
  1571. * The bogus IDT is used during bootstrap for allocating the initial
  1572. * message buffer, and then the message buffer and space for the IDT
  1573. * itself. The initial message buffer is of a single page's size, but
  1574. * it's soon replaced with a more modest one (and memory is freed).
  1575. */
  1576. unsigned char bogus_idt[8] = { 1, 224, (PAGE_SHIFT)-2, 0,
  1577. 3, 192, PAGE_SHIFT, 0 };
  1578. struct xilly_idt_handle idt_handle;
  1579. /*
  1580. * Writing the value 0x00000001 to Endianness register signals which
  1581. * endianness this processor is using, so the FPGA can swap words as
  1582. * necessary.
  1583. */
  1584. iowrite32(1, endpoint->registers + fpga_endian_reg);
  1585. /* Bootstrap phase I: Allocate temporary message buffer */
  1586. bootstrap_resources = devres_open_group(dev, NULL, GFP_KERNEL);
  1587. if (!bootstrap_resources)
  1588. return -ENOMEM;
  1589. endpoint->num_channels = 0;
  1590. rc = xilly_setupchannels(endpoint, bogus_idt, 1);
  1591. if (rc)
  1592. return rc;
  1593. /* Clear the message subsystem (and counter in particular) */
  1594. iowrite32(0x04, endpoint->registers + fpga_msg_ctrl_reg);
  1595. endpoint->idtlen = -1;
  1596. /*
  1597. * Set DMA 32/64 bit mode, quiesce the device (?!) and get IDT
  1598. * buffer size.
  1599. */
  1600. iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
  1601. endpoint->registers + fpga_dma_control_reg);
  1602. t = wait_event_interruptible_timeout(endpoint->ep_wait,
  1603. (endpoint->idtlen >= 0),
  1604. XILLY_TIMEOUT);
  1605. if (t <= 0) {
  1606. dev_err(endpoint->dev, "No response from FPGA. Aborting.\n");
  1607. return -ENODEV;
  1608. }
  1609. /* Enable DMA */
  1610. iowrite32((u32) (0x0002 | (endpoint->dma_using_dac & 0x0001)),
  1611. endpoint->registers + fpga_dma_control_reg);
  1612. /* Bootstrap phase II: Allocate buffer for IDT and obtain it */
  1613. while (endpoint->idtlen >= idtbuffersize) {
  1614. idtbuffersize *= 2;
  1615. bogus_idt[6]++;
  1616. }
  1617. endpoint->num_channels = 1;
  1618. rc = xilly_setupchannels(endpoint, bogus_idt, 2);
  1619. if (rc)
  1620. goto failed_idt;
  1621. rc = xilly_obtain_idt(endpoint);
  1622. if (rc)
  1623. goto failed_idt;
  1624. rc = xilly_scan_idt(endpoint, &idt_handle);
  1625. if (rc)
  1626. goto failed_idt;
  1627. devres_close_group(dev, bootstrap_resources);
  1628. /* Bootstrap phase III: Allocate buffers according to IDT */
  1629. rc = xilly_setupchannels(endpoint,
  1630. idt_handle.chandesc,
  1631. idt_handle.entries);
  1632. if (rc)
  1633. goto failed_idt;
  1634. /*
  1635. * endpoint is now completely configured. We put it on the list
  1636. * available to open() before registering the char device(s)
  1637. */
  1638. mutex_lock(&ep_list_lock);
  1639. list_add_tail(&endpoint->ep_list, &list_of_endpoints);
  1640. mutex_unlock(&ep_list_lock);
  1641. rc = xillybus_init_chrdev(endpoint, idt_handle.idt);
  1642. if (rc)
  1643. goto failed_chrdevs;
  1644. devres_release_group(dev, bootstrap_resources);
  1645. return 0;
  1646. failed_chrdevs:
  1647. mutex_lock(&ep_list_lock);
  1648. list_del(&endpoint->ep_list);
  1649. mutex_unlock(&ep_list_lock);
  1650. failed_idt:
  1651. xilly_quiesce(endpoint);
  1652. flush_workqueue(xillybus_wq);
  1653. return rc;
  1654. }
  1655. EXPORT_SYMBOL(xillybus_endpoint_discovery);
  1656. void xillybus_endpoint_remove(struct xilly_endpoint *endpoint)
  1657. {
  1658. xillybus_cleanup_chrdev(endpoint);
  1659. mutex_lock(&ep_list_lock);
  1660. list_del(&endpoint->ep_list);
  1661. mutex_unlock(&ep_list_lock);
  1662. xilly_quiesce(endpoint);
  1663. /*
  1664. * Flushing is done upon endpoint release to prevent access to memory
  1665. * just about to be released. This makes the quiesce complete.
  1666. */
  1667. flush_workqueue(xillybus_wq);
  1668. }
  1669. EXPORT_SYMBOL(xillybus_endpoint_remove);
  1670. static int __init xillybus_init(void)
  1671. {
  1672. mutex_init(&ep_list_lock);
  1673. xillybus_class = class_create(THIS_MODULE, xillyname);
  1674. if (IS_ERR(xillybus_class))
  1675. return PTR_ERR(xillybus_class);
  1676. xillybus_wq = alloc_workqueue(xillyname, 0, 0);
  1677. if (!xillybus_wq) {
  1678. class_destroy(xillybus_class);
  1679. return -ENOMEM;
  1680. }
  1681. return 0;
  1682. }
  1683. static void __exit xillybus_exit(void)
  1684. {
  1685. /* flush_workqueue() was called for each endpoint released */
  1686. destroy_workqueue(xillybus_wq);
  1687. class_destroy(xillybus_class);
  1688. }
  1689. module_init(xillybus_init);
  1690. module_exit(xillybus_exit);