1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2020 Xillybus Ltd, http://xillybus.com
5 * Driver for the XillyUSB FPGA/host framework.
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.
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <asm/byteorder.h>
21 #include <linux/interrupt.h>
22 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/mutex.h>
26 #include <linux/workqueue.h>
27 #include <linux/crc32.h>
28 #include <linux/poll.h>
29 #include <linux/delay.h>
30 #include <linux/usb.h>
32 #include "xillybus_class.h"
34 MODULE_DESCRIPTION("Driver for XillyUSB FPGA IP Core");
35 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
36 MODULE_ALIAS("xillyusb");
37 MODULE_LICENSE("GPL v2");
39 #define XILLY_RX_TIMEOUT (10 * HZ / 1000)
40 #define XILLY_RESPONSE_TIMEOUT (500 * HZ / 1000)
42 #define BUF_SIZE_ORDER 4
44 #define LOG2_IDT_FIFO_SIZE 16
45 #define LOG2_INITIAL_FIFO_BUF_SIZE 16
50 static const char xillyname[] = "xillyusb";
52 static unsigned int fifo_buf_order;
53 static struct workqueue_struct *wakeup_wq;
55 #define USB_VENDOR_ID_XILINX 0x03fd
56 #define USB_VENDOR_ID_ALTERA 0x09fb
58 #define USB_PRODUCT_ID_XILLYUSB 0xebbe
60 static const struct usb_device_id xillyusb_table[] = {
61 { USB_DEVICE(USB_VENDOR_ID_XILINX, USB_PRODUCT_ID_XILLYUSB) },
62 { USB_DEVICE(USB_VENDOR_ID_ALTERA, USB_PRODUCT_ID_XILLYUSB) },
66 MODULE_DEVICE_TABLE(usb, xillyusb_table);
71 unsigned int bufsize; /* In bytes, always a power of 2 */
73 unsigned int size; /* Lazy: Equals bufsize * bufnum */
74 unsigned int buf_order;
76 int fill; /* Number of bytes in the FIFO */
78 wait_queue_head_t waitq;
82 unsigned int writepos;
83 unsigned int writebuf;
87 struct xillyusb_channel;
89 struct xillyusb_endpoint {
90 struct xillyusb_dev *xdev;
92 struct mutex ep_mutex; /* serialize operations on endpoint */
94 struct list_head buffers;
95 struct list_head filled_buffers;
96 spinlock_t buffers_lock; /* protect these two lists */
99 unsigned int buffer_size;
101 unsigned int fill_mask;
103 int outstanding_urbs;
105 struct usb_anchor anchor;
107 struct xillyfifo fifo;
109 struct work_struct workitem;
118 struct xillyusb_channel {
119 struct xillyusb_dev *xdev;
121 struct xillyfifo *in_fifo;
122 struct xillyusb_endpoint *out_ep;
123 struct mutex lock; /* protect @out_ep, @in_fifo, bit fields below */
125 struct mutex in_mutex; /* serialize fops on FPGA to host stream */
126 struct mutex out_mutex; /* serialize fops on host to FPGA stream */
127 wait_queue_head_t flushq;
131 u32 in_consumed_bytes;
132 u32 in_current_checkpoint;
135 unsigned int in_log2_element_size;
136 unsigned int out_log2_element_size;
137 unsigned int in_log2_fifo_size;
138 unsigned int out_log2_fifo_size;
140 unsigned int read_data_ok; /* EOF not arrived (yet) */
141 unsigned int poll_used;
142 unsigned int flushing;
143 unsigned int flushed;
144 unsigned int canceled;
146 /* Bit fields protected by @lock except for initialization */
149 unsigned open_for_read:1;
150 unsigned open_for_write:1;
151 unsigned in_synchronous:1;
152 unsigned out_synchronous:1;
153 unsigned in_seekable:1;
154 unsigned out_seekable:1;
158 struct list_head entry;
159 struct xillyusb_endpoint *ep;
164 struct xillyusb_dev {
165 struct xillyusb_channel *channels;
167 struct usb_device *udev;
168 struct device *dev; /* For dev_err() and such */
170 struct workqueue_struct *workq;
173 spinlock_t error_lock; /* protect @error */
174 struct work_struct wakeup_workitem;
178 struct xillyusb_endpoint *msg_ep;
179 struct xillyusb_endpoint *in_ep;
181 struct mutex msg_mutex; /* serialize opcode transmission */
183 int leftover_chan_num;
184 unsigned int in_counter;
185 struct mutex process_in_mutex; /* synchronize wakeup_all() */
189 * kref_mutex is used in xillyusb_open() to prevent the xillyusb_dev
190 * struct from being freed during the gap between being found by
191 * xillybus_find_inode() and having its reference count incremented.
194 static DEFINE_MUTEX(kref_mutex);
196 /* FPGA to host opcodes */
199 OPCODE_QUIESCE_ACK = 1,
201 OPCODE_REACHED_CHECKPOINT = 3,
202 OPCODE_CANCELED_CHECKPOINT = 4,
205 /* Host to FPGA opcodes */
209 OPCODE_SET_CHECKPOINT = 2,
212 OPCODE_UPDATE_PUSH = 5,
213 OPCODE_CANCEL_CHECKPOINT = 6,
218 * fifo_write() and fifo_read() are NOT reentrant (i.e. concurrent multiple
219 * calls to each on the same FIFO is not allowed) however it's OK to have
220 * threads calling each of the two functions once on the same FIFO, and
224 static int fifo_write(struct xillyfifo *fifo,
225 const void *data, unsigned int len,
226 int (*copier)(void *, const void *, int))
228 unsigned int done = 0;
229 unsigned int todo = len;
231 unsigned int writepos = fifo->writepos;
232 unsigned int writebuf = fifo->writebuf;
236 nmax = fifo->size - READ_ONCE(fifo->fill);
239 unsigned int nrail = fifo->bufsize - writepos;
240 unsigned int n = min(todo, nmax);
243 spin_lock_irqsave(&fifo->lock, flags);
245 spin_unlock_irqrestore(&fifo->lock, flags);
247 fifo->writepos = writepos;
248 fifo->writebuf = writebuf;
256 rc = (*copier)(fifo->mem[writebuf] + writepos, data + done, n);
267 if (writepos == fifo->bufsize) {
271 if (writebuf == fifo->bufnum)
277 static int fifo_read(struct xillyfifo *fifo,
278 void *data, unsigned int len,
279 int (*copier)(void *, const void *, int))
281 unsigned int done = 0;
282 unsigned int todo = len;
284 unsigned int readpos = fifo->readpos;
285 unsigned int readbuf = fifo->readbuf;
290 * The spinlock here is necessary, because otherwise fifo->fill
291 * could have been increased by fifo_write() after writing data
292 * to the buffer, but this data would potentially not have been
293 * visible on this thread at the time the updated fifo->fill was.
294 * That could lead to reading invalid data.
297 spin_lock_irqsave(&fifo->lock, flags);
299 spin_unlock_irqrestore(&fifo->lock, flags);
302 unsigned int nrail = fifo->bufsize - readpos;
303 unsigned int n = min(todo, fill);
306 spin_lock_irqsave(&fifo->lock, flags);
308 spin_unlock_irqrestore(&fifo->lock, flags);
310 fifo->readpos = readpos;
311 fifo->readbuf = readbuf;
319 rc = (*copier)(data + done, fifo->mem[readbuf] + readpos, n);
330 if (readpos == fifo->bufsize) {
334 if (readbuf == fifo->bufnum)
341 * These three wrapper functions are used as the @copier argument to
342 * fifo_write() and fifo_read(), so that they can work directly with
343 * user memory as well.
346 static int xilly_copy_from_user(void *dst, const void *src, int n)
348 if (copy_from_user(dst, (const void __user *)src, n))
354 static int xilly_copy_to_user(void *dst, const void *src, int n)
356 if (copy_to_user((void __user *)dst, src, n))
362 static int xilly_memcpy(void *dst, const void *src, int n)
369 static int fifo_init(struct xillyfifo *fifo,
370 unsigned int log2_size)
372 unsigned int log2_bufnum;
373 unsigned int buf_order;
376 unsigned int log2_fifo_buf_size;
379 log2_fifo_buf_size = fifo_buf_order + PAGE_SHIFT;
381 if (log2_size > log2_fifo_buf_size) {
382 log2_bufnum = log2_size - log2_fifo_buf_size;
383 buf_order = fifo_buf_order;
384 fifo->bufsize = 1 << log2_fifo_buf_size;
387 buf_order = (log2_size > PAGE_SHIFT) ?
388 log2_size - PAGE_SHIFT : 0;
389 fifo->bufsize = 1 << log2_size;
392 fifo->bufnum = 1 << log2_bufnum;
393 fifo->size = fifo->bufnum * fifo->bufsize;
394 fifo->buf_order = buf_order;
396 fifo->mem = kmalloc_array(fifo->bufnum, sizeof(void *), GFP_KERNEL);
401 for (i = 0; i < fifo->bufnum; i++) {
402 fifo->mem[i] = (void *)
403 __get_free_pages(GFP_KERNEL, buf_order);
414 spin_lock_init(&fifo->lock);
415 init_waitqueue_head(&fifo->waitq);
419 for (i--; i >= 0; i--)
420 free_pages((unsigned long)fifo->mem[i], buf_order);
425 if (fifo_buf_order) {
433 static void fifo_mem_release(struct xillyfifo *fifo)
440 for (i = 0; i < fifo->bufnum; i++)
441 free_pages((unsigned long)fifo->mem[i], fifo->buf_order);
447 * When endpoint_quiesce() returns, the endpoint has no URBs submitted,
448 * won't accept any new URB submissions, and its related work item doesn't
449 * and won't run anymore.
452 static void endpoint_quiesce(struct xillyusb_endpoint *ep)
454 mutex_lock(&ep->ep_mutex);
455 ep->shutting_down = true;
456 mutex_unlock(&ep->ep_mutex);
458 usb_kill_anchored_urbs(&ep->anchor);
459 cancel_work_sync(&ep->workitem);
463 * Note that endpoint_dealloc() also frees fifo memory (if allocated), even
464 * though endpoint_alloc doesn't allocate that memory.
467 static void endpoint_dealloc(struct xillyusb_endpoint *ep)
469 struct list_head *this, *next;
471 fifo_mem_release(&ep->fifo);
473 /* Join @filled_buffers with @buffers to free these entries too */
474 list_splice(&ep->filled_buffers, &ep->buffers);
476 list_for_each_safe(this, next, &ep->buffers) {
477 struct xillybuffer *xb =
478 list_entry(this, struct xillybuffer, entry);
480 free_pages((unsigned long)xb->buf, ep->order);
487 static struct xillyusb_endpoint
488 *endpoint_alloc(struct xillyusb_dev *xdev,
490 void (*work)(struct work_struct *),
496 struct xillyusb_endpoint *ep;
498 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
503 INIT_LIST_HEAD(&ep->buffers);
504 INIT_LIST_HEAD(&ep->filled_buffers);
506 spin_lock_init(&ep->buffers_lock);
507 mutex_init(&ep->ep_mutex);
509 init_usb_anchor(&ep->anchor);
510 INIT_WORK(&ep->workitem, work);
513 ep->buffer_size = 1 << (PAGE_SHIFT + order);
514 ep->outstanding_urbs = 0;
516 ep->wake_on_drain = false;
519 ep->shutting_down = false;
521 for (i = 0; i < bufnum; i++) {
522 struct xillybuffer *xb;
525 xb = kzalloc(sizeof(*xb), GFP_KERNEL);
528 endpoint_dealloc(ep);
532 addr = __get_free_pages(GFP_KERNEL, order);
536 endpoint_dealloc(ep);
540 xb->buf = (void *)addr;
542 list_add_tail(&xb->entry, &ep->buffers);
547 static void cleanup_dev(struct kref *kref)
549 struct xillyusb_dev *xdev =
550 container_of(kref, struct xillyusb_dev, kref);
553 endpoint_dealloc(xdev->in_ep);
556 endpoint_dealloc(xdev->msg_ep);
559 destroy_workqueue(xdev->workq);
561 usb_put_dev(xdev->udev);
562 kfree(xdev->channels); /* Argument may be NULL, and that's fine */
567 * @process_in_mutex is taken to ensure that bulk_in_work() won't call
568 * process_bulk_in() after wakeup_all()'s execution: The latter zeroes all
569 * @read_data_ok entries, which will make process_bulk_in() report false
570 * errors if executed. The mechanism relies on that xdev->error is assigned
571 * a non-zero value by report_io_error() prior to queueing wakeup_all(),
572 * which prevents bulk_in_work() from calling process_bulk_in().
575 static void wakeup_all(struct work_struct *work)
578 struct xillyusb_dev *xdev = container_of(work, struct xillyusb_dev,
581 mutex_lock(&xdev->process_in_mutex);
583 for (i = 0; i < xdev->num_channels; i++) {
584 struct xillyusb_channel *chan = &xdev->channels[i];
586 mutex_lock(&chan->lock);
590 * Fake an EOF: Even if such arrives, it won't be
593 chan->read_data_ok = 0;
594 wake_up_interruptible(&chan->in_fifo->waitq);
598 wake_up_interruptible(&chan->out_ep->fifo.waitq);
600 mutex_unlock(&chan->lock);
602 wake_up_interruptible(&chan->flushq);
605 mutex_unlock(&xdev->process_in_mutex);
607 wake_up_interruptible(&xdev->msg_ep->fifo.waitq);
609 kref_put(&xdev->kref, cleanup_dev);
612 static void report_io_error(struct xillyusb_dev *xdev,
616 bool do_once = false;
618 spin_lock_irqsave(&xdev->error_lock, flags);
620 xdev->error = errcode;
623 spin_unlock_irqrestore(&xdev->error_lock, flags);
626 kref_get(&xdev->kref); /* xdev is used by work item */
627 queue_work(wakeup_wq, &xdev->wakeup_workitem);
632 * safely_assign_in_fifo() changes the value of chan->in_fifo and ensures
633 * the previous pointer is never used after its return.
636 static void safely_assign_in_fifo(struct xillyusb_channel *chan,
637 struct xillyfifo *fifo)
639 mutex_lock(&chan->lock);
640 chan->in_fifo = fifo;
641 mutex_unlock(&chan->lock);
643 flush_work(&chan->xdev->in_ep->workitem);
646 static void bulk_in_completer(struct urb *urb)
648 struct xillybuffer *xb = urb->context;
649 struct xillyusb_endpoint *ep = xb->ep;
653 if (!(urb->status == -ENOENT ||
654 urb->status == -ECONNRESET ||
655 urb->status == -ESHUTDOWN))
656 report_io_error(ep->xdev, -EIO);
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);
666 xb->len = urb->actual_length;
668 spin_lock_irqsave(&ep->buffers_lock, flags);
669 list_add_tail(&xb->entry, &ep->filled_buffers);
670 spin_unlock_irqrestore(&ep->buffers_lock, flags);
672 if (!ep->shutting_down)
673 queue_work(ep->xdev->workq, &ep->workitem);
676 static void bulk_out_completer(struct urb *urb)
678 struct xillybuffer *xb = urb->context;
679 struct xillyusb_endpoint *ep = xb->ep;
683 (!(urb->status == -ENOENT ||
684 urb->status == -ECONNRESET ||
685 urb->status == -ESHUTDOWN)))
686 report_io_error(ep->xdev, -EIO);
688 spin_lock_irqsave(&ep->buffers_lock, flags);
689 list_add_tail(&xb->entry, &ep->buffers);
690 ep->outstanding_urbs--;
691 spin_unlock_irqrestore(&ep->buffers_lock, flags);
693 if (!ep->shutting_down)
694 queue_work(ep->xdev->workq, &ep->workitem);
697 static void try_queue_bulk_in(struct xillyusb_endpoint *ep)
699 struct xillyusb_dev *xdev = ep->xdev;
700 struct xillybuffer *xb;
705 unsigned int bufsize = ep->buffer_size;
707 mutex_lock(&ep->ep_mutex);
709 if (ep->shutting_down || xdev->error)
713 spin_lock_irqsave(&ep->buffers_lock, flags);
715 if (list_empty(&ep->buffers)) {
716 spin_unlock_irqrestore(&ep->buffers_lock, flags);
720 xb = list_first_entry(&ep->buffers, struct xillybuffer, entry);
721 list_del(&xb->entry);
722 ep->outstanding_urbs++;
724 spin_unlock_irqrestore(&ep->buffers_lock, flags);
726 urb = usb_alloc_urb(0, GFP_KERNEL);
728 report_io_error(xdev, -ENOMEM);
732 usb_fill_bulk_urb(urb, xdev->udev,
733 usb_rcvbulkpipe(xdev->udev, ep->ep_num),
734 xb->buf, bufsize, bulk_in_completer, xb);
736 usb_anchor_urb(urb, &ep->anchor);
738 rc = usb_submit_urb(urb, GFP_KERNEL);
741 report_io_error(xdev, (rc == -ENOMEM) ? -ENOMEM :
746 usb_free_urb(urb); /* This just decrements reference count */
750 usb_unanchor_urb(urb);
754 spin_lock_irqsave(&ep->buffers_lock, flags);
755 list_add_tail(&xb->entry, &ep->buffers);
756 ep->outstanding_urbs--;
757 spin_unlock_irqrestore(&ep->buffers_lock, flags);
760 mutex_unlock(&ep->ep_mutex);
763 static void try_queue_bulk_out(struct xillyusb_endpoint *ep)
765 struct xillyfifo *fifo = &ep->fifo;
766 struct xillyusb_dev *xdev = ep->xdev;
767 struct xillybuffer *xb;
773 bool do_wake = false;
775 mutex_lock(&ep->ep_mutex);
777 if (ep->shutting_down || xdev->error)
780 fill = READ_ONCE(fifo->fill) & ep->fill_mask;
784 unsigned int max_read;
786 spin_lock_irqsave(&ep->buffers_lock, flags);
789 * Race conditions might have the FIFO filled while the
790 * endpoint is marked as drained here. That doesn't matter,
791 * because the sole purpose of @drained is to ensure that
792 * certain data has been sent on the USB channel before
793 * shutting it down. Hence knowing that the FIFO appears
794 * to be empty with no outstanding URBs at some moment
799 ep->drained = !ep->outstanding_urbs;
800 if (ep->drained && ep->wake_on_drain)
803 spin_unlock_irqrestore(&ep->buffers_lock, flags);
809 if ((fill < ep->buffer_size && ep->outstanding_urbs) ||
810 list_empty(&ep->buffers)) {
811 spin_unlock_irqrestore(&ep->buffers_lock, flags);
815 xb = list_first_entry(&ep->buffers, struct xillybuffer, entry);
816 list_del(&xb->entry);
817 ep->outstanding_urbs++;
819 spin_unlock_irqrestore(&ep->buffers_lock, flags);
821 max_read = min(fill, ep->buffer_size);
823 count = fifo_read(&ep->fifo, xb->buf, max_read, xilly_memcpy);
826 * xilly_memcpy always returns 0 => fifo_read can't fail =>
830 urb = usb_alloc_urb(0, GFP_KERNEL);
832 report_io_error(xdev, -ENOMEM);
836 usb_fill_bulk_urb(urb, xdev->udev,
837 usb_sndbulkpipe(xdev->udev, ep->ep_num),
838 xb->buf, count, bulk_out_completer, xb);
840 usb_anchor_urb(urb, &ep->anchor);
842 rc = usb_submit_urb(urb, GFP_KERNEL);
845 report_io_error(xdev, (rc == -ENOMEM) ? -ENOMEM :
850 usb_free_urb(urb); /* This just decrements reference count */
857 usb_unanchor_urb(urb);
861 spin_lock_irqsave(&ep->buffers_lock, flags);
862 list_add_tail(&xb->entry, &ep->buffers);
863 ep->outstanding_urbs--;
864 spin_unlock_irqrestore(&ep->buffers_lock, flags);
867 mutex_unlock(&ep->ep_mutex);
870 wake_up_interruptible(&fifo->waitq);
873 static void bulk_out_work(struct work_struct *work)
875 struct xillyusb_endpoint *ep = container_of(work,
876 struct xillyusb_endpoint,
878 try_queue_bulk_out(ep);
881 static int process_in_opcode(struct xillyusb_dev *xdev,
885 struct xillyusb_channel *chan;
886 struct device *dev = xdev->dev;
887 int chan_idx = chan_num >> 1;
889 if (chan_idx >= xdev->num_channels) {
890 dev_err(dev, "Received illegal channel ID %d from FPGA\n",
895 chan = &xdev->channels[chan_idx];
899 if (!chan->read_data_ok) {
900 dev_err(dev, "Received unexpected EOF for channel %d\n",
906 * A write memory barrier ensures that the FIFO's fill level
907 * is visible before read_data_ok turns zero, so the data in
908 * the FIFO isn't missed by the consumer.
911 WRITE_ONCE(chan->read_data_ok, 0);
912 wake_up_interruptible(&chan->in_fifo->waitq);
915 case OPCODE_REACHED_CHECKPOINT:
917 wake_up_interruptible(&chan->flushq);
920 case OPCODE_CANCELED_CHECKPOINT:
922 wake_up_interruptible(&chan->flushq);
926 dev_err(dev, "Received illegal opcode %d from FPGA\n",
934 static int process_bulk_in(struct xillybuffer *xb)
936 struct xillyusb_endpoint *ep = xb->ep;
937 struct xillyusb_dev *xdev = ep->xdev;
938 struct device *dev = xdev->dev;
939 int dws = xb->len >> 2;
942 struct xillyusb_channel *chan;
943 struct xillyfifo *fifo;
944 int chan_num = 0, opcode;
946 int bytes, count, dwconsume;
947 int in_bytes_left = 0;
950 if ((dws << 2) != xb->len) {
951 dev_err(dev, "Received BULK IN transfer with %d bytes, not a multiple of 4\n",
956 if (xdev->in_bytes_left) {
957 bytes = min(xdev->in_bytes_left, dws << 2);
958 in_bytes_left = xdev->in_bytes_left - bytes;
959 chan_num = xdev->leftover_chan_num;
960 goto resume_leftovers;
964 ctrlword = le32_to_cpu(*p++);
967 chan_num = ctrlword & 0xfff;
968 count = (ctrlword >> 12) & 0x3ff;
969 opcode = (ctrlword >> 24) & 0xf;
971 if (opcode != OPCODE_DATA) {
972 unsigned int in_counter = xdev->in_counter++ & 0x3ff;
974 if (count != in_counter) {
975 dev_err(dev, "Expected opcode counter %d, got %d\n",
980 rc = process_in_opcode(xdev, opcode, chan_num);
988 bytes = min(count + 1, dws << 2);
989 in_bytes_left = count + 1 - bytes;
992 chan_idx = chan_num >> 1;
994 if (!(chan_num & 1) || chan_idx >= xdev->num_channels ||
995 !xdev->channels[chan_idx].read_data_ok) {
996 dev_err(dev, "Received illegal channel ID %d from FPGA\n",
1000 chan = &xdev->channels[chan_idx];
1002 fifo = chan->in_fifo;
1004 if (unlikely(!fifo))
1005 return -EIO; /* We got really unexpected data */
1007 if (bytes != fifo_write(fifo, p, bytes, xilly_memcpy)) {
1008 dev_err(dev, "Misbehaving FPGA overflowed an upstream FIFO!\n");
1012 wake_up_interruptible(&fifo->waitq);
1014 dwconsume = (bytes + 3) >> 2;
1019 xdev->in_bytes_left = in_bytes_left;
1020 xdev->leftover_chan_num = chan_num;
1024 static void bulk_in_work(struct work_struct *work)
1026 struct xillyusb_endpoint *ep =
1027 container_of(work, struct xillyusb_endpoint, workitem);
1028 struct xillyusb_dev *xdev = ep->xdev;
1029 unsigned long flags;
1030 struct xillybuffer *xb;
1031 bool consumed = false;
1034 mutex_lock(&xdev->process_in_mutex);
1036 spin_lock_irqsave(&ep->buffers_lock, flags);
1039 if (rc || list_empty(&ep->filled_buffers)) {
1040 spin_unlock_irqrestore(&ep->buffers_lock, flags);
1041 mutex_unlock(&xdev->process_in_mutex);
1044 report_io_error(xdev, rc);
1046 try_queue_bulk_in(ep);
1051 xb = list_first_entry(&ep->filled_buffers, struct xillybuffer,
1053 list_del(&xb->entry);
1055 spin_unlock_irqrestore(&ep->buffers_lock, flags);
1060 rc = process_bulk_in(xb);
1062 spin_lock_irqsave(&ep->buffers_lock, flags);
1063 list_add_tail(&xb->entry, &ep->buffers);
1064 ep->outstanding_urbs--;
1068 static int xillyusb_send_opcode(struct xillyusb_dev *xdev,
1069 int chan_num, char opcode, u32 data)
1071 struct xillyusb_endpoint *ep = xdev->msg_ep;
1072 struct xillyfifo *fifo = &ep->fifo;
1077 msg[0] = cpu_to_le32((chan_num & 0xfff) |
1078 ((opcode & 0xf) << 24));
1079 msg[1] = cpu_to_le32(data);
1081 mutex_lock(&xdev->msg_mutex);
1084 * The wait queue is woken with the interruptible variant, so the
1085 * wait function matches, however returning because of an interrupt
1086 * will mess things up considerably, in particular when the caller is
1087 * the release method. And the xdev->error part prevents being stuck
1088 * forever in the event of a bizarre hardware bug: Pull the USB plug.
1091 while (wait_event_interruptible(fifo->waitq,
1092 fifo->fill <= (fifo->size - 8) ||
1101 fifo_write(fifo, (void *)msg, 8, xilly_memcpy);
1103 try_queue_bulk_out(ep);
1106 mutex_unlock(&xdev->msg_mutex);
1112 * Note that flush_downstream() merely waits for the data to arrive to
1113 * the application logic at the FPGA -- unlike PCIe Xillybus' counterpart,
1114 * it does nothing to make it happen (and neither is it necessary).
1116 * This function is not reentrant for the same @chan, but this is covered
1117 * by the fact that for any given @chan, it's called either by the open,
1118 * write, llseek and flush fops methods, which can't run in parallel (and the
1119 * write + flush and llseek method handlers are protected with out_mutex).
1121 * chan->flushed is there to avoid multiple flushes at the same position,
1122 * in particular as a result of programs that close the file descriptor
1123 * e.g. after a dup2() for redirection.
1126 static int flush_downstream(struct xillyusb_channel *chan,
1130 struct xillyusb_dev *xdev = chan->xdev;
1131 int chan_num = chan->chan_idx << 1;
1132 long deadline, left_to_sleep;
1138 deadline = jiffies + 1 + timeout;
1140 if (chan->flushing) {
1141 long cancel_deadline = jiffies + 1 + XILLY_RESPONSE_TIMEOUT;
1144 rc = xillyusb_send_opcode(xdev, chan_num,
1145 OPCODE_CANCEL_CHECKPOINT, 0);
1148 return rc; /* Only real error, never -EINTR */
1150 /* Ignoring interrupts. Cancellation must be handled */
1151 while (!chan->canceled) {
1152 left_to_sleep = cancel_deadline - ((long)jiffies);
1154 if (left_to_sleep <= 0) {
1155 report_io_error(xdev, -EIO);
1159 rc = wait_event_interruptible_timeout(chan->flushq,
1172 * The checkpoint is given in terms of data elements, not bytes. As
1173 * a result, if less than an element's worth of data is stored in the
1174 * FIFO, it's not flushed, including the flush before closing, which
1175 * means that such data is lost. This is consistent with PCIe Xillybus.
1178 rc = xillyusb_send_opcode(xdev, chan_num,
1179 OPCODE_SET_CHECKPOINT,
1181 chan->out_log2_element_size);
1184 return rc; /* Only real error, never -EINTR */
1187 while (chan->flushing) {
1188 rc = wait_event_interruptible(chan->flushq,
1194 if (interruptible && rc)
1201 while (chan->flushing) {
1202 left_to_sleep = deadline - ((long)jiffies);
1204 if (left_to_sleep <= 0)
1207 rc = wait_event_interruptible_timeout(chan->flushq,
1215 if (interruptible && rc < 0)
1224 /* request_read_anything(): Ask the FPGA for any little amount of data */
1225 static int request_read_anything(struct xillyusb_channel *chan,
1228 struct xillyusb_dev *xdev = chan->xdev;
1229 unsigned int sh = chan->in_log2_element_size;
1230 int chan_num = (chan->chan_idx << 1) | 1;
1231 u32 mercy = chan->in_consumed_bytes + (2 << sh) - 1;
1233 return xillyusb_send_opcode(xdev, chan_num, opcode, mercy >> sh);
1236 static int xillyusb_open(struct inode *inode, struct file *filp)
1238 struct xillyusb_dev *xdev;
1239 struct xillyusb_channel *chan;
1240 struct xillyfifo *in_fifo = NULL;
1241 struct xillyusb_endpoint *out_ep = NULL;
1245 mutex_lock(&kref_mutex);
1247 rc = xillybus_find_inode(inode, (void **)&xdev, &index);
1249 mutex_unlock(&kref_mutex);
1253 kref_get(&xdev->kref);
1254 mutex_unlock(&kref_mutex);
1256 chan = &xdev->channels[index];
1257 filp->private_data = chan;
1259 mutex_lock(&chan->lock);
1266 if (((filp->f_mode & FMODE_READ) && !chan->readable) ||
1267 ((filp->f_mode & FMODE_WRITE) && !chan->writable))
1270 if ((filp->f_flags & O_NONBLOCK) && (filp->f_mode & FMODE_READ) &&
1271 chan->in_synchronous) {
1273 "open() failed: O_NONBLOCK not allowed for read on this device\n");
1277 if ((filp->f_flags & O_NONBLOCK) && (filp->f_mode & FMODE_WRITE) &&
1278 chan->out_synchronous) {
1280 "open() failed: O_NONBLOCK not allowed for write on this device\n");
1286 if (((filp->f_mode & FMODE_READ) && chan->open_for_read) ||
1287 ((filp->f_mode & FMODE_WRITE) && chan->open_for_write))
1290 if (filp->f_mode & FMODE_READ)
1291 chan->open_for_read = 1;
1293 if (filp->f_mode & FMODE_WRITE)
1294 chan->open_for_write = 1;
1296 mutex_unlock(&chan->lock);
1298 if (filp->f_mode & FMODE_WRITE) {
1299 out_ep = endpoint_alloc(xdev,
1300 (chan->chan_idx + 2) | USB_DIR_OUT,
1301 bulk_out_work, BUF_SIZE_ORDER, BUFNUM);
1308 rc = fifo_init(&out_ep->fifo, chan->out_log2_fifo_size);
1313 out_ep->fill_mask = -(1 << chan->out_log2_element_size);
1314 chan->out_bytes = 0;
1318 * Sending a flush request to a previously closed stream
1319 * effectively opens it, and also waits until the command is
1320 * confirmed by the FPGA. The latter is necessary because the
1321 * data is sent through a separate BULK OUT endpoint, and the
1322 * xHCI controller is free to reorder transmissions.
1324 * This can't go wrong unless there's a serious hardware error
1325 * (or the computer is stuck for 500 ms?)
1327 rc = flush_downstream(chan, XILLY_RESPONSE_TIMEOUT, false);
1329 if (rc == -ETIMEDOUT) {
1331 report_io_error(xdev, rc);
1338 if (filp->f_mode & FMODE_READ) {
1339 in_fifo = kzalloc(sizeof(*in_fifo), GFP_KERNEL);
1346 rc = fifo_init(in_fifo, chan->in_log2_fifo_size);
1354 mutex_lock(&chan->lock);
1356 chan->in_fifo = in_fifo;
1357 chan->read_data_ok = 1;
1360 chan->out_ep = out_ep;
1361 mutex_unlock(&chan->lock);
1364 u32 in_checkpoint = 0;
1366 if (!chan->in_synchronous)
1367 in_checkpoint = in_fifo->size >>
1368 chan->in_log2_element_size;
1370 chan->in_consumed_bytes = 0;
1371 chan->poll_used = 0;
1372 chan->in_current_checkpoint = in_checkpoint;
1373 rc = xillyusb_send_opcode(xdev, (chan->chan_idx << 1) | 1,
1374 OPCODE_SET_CHECKPOINT,
1377 if (rc) /* Failure guarantees that opcode wasn't sent */
1381 * In non-blocking mode, request the FPGA to send any data it
1382 * has right away. Otherwise, the first read() will always
1383 * return -EAGAIN, which is OK strictly speaking, but ugly.
1384 * Checking and unrolling if this fails isn't worth the
1385 * effort -- the error is propagated to the first read()
1388 if (filp->f_flags & O_NONBLOCK)
1389 request_read_anything(chan, OPCODE_SET_PUSH);
1395 chan->read_data_ok = 0;
1396 safely_assign_in_fifo(chan, NULL);
1397 fifo_mem_release(in_fifo);
1401 mutex_lock(&chan->lock);
1402 chan->out_ep = NULL;
1403 mutex_unlock(&chan->lock);
1408 endpoint_dealloc(out_ep);
1411 mutex_lock(&chan->lock);
1413 if (filp->f_mode & FMODE_READ)
1414 chan->open_for_read = 0;
1416 if (filp->f_mode & FMODE_WRITE)
1417 chan->open_for_write = 0;
1419 mutex_unlock(&chan->lock);
1421 kref_put(&xdev->kref, cleanup_dev);
1426 kref_put(&xdev->kref, cleanup_dev);
1427 mutex_unlock(&chan->lock);
1431 static ssize_t xillyusb_read(struct file *filp, char __user *userbuf,
1432 size_t count, loff_t *f_pos)
1434 struct xillyusb_channel *chan = filp->private_data;
1435 struct xillyusb_dev *xdev = chan->xdev;
1436 struct xillyfifo *fifo = chan->in_fifo;
1437 int chan_num = (chan->chan_idx << 1) | 1;
1439 long deadline, left_to_sleep;
1441 bool sent_set_push = false;
1444 deadline = jiffies + 1 + XILLY_RX_TIMEOUT;
1446 rc = mutex_lock_interruptible(&chan->in_mutex);
1452 u32 fifo_checkpoint_bytes, complete_checkpoint_bytes;
1453 u32 complete_checkpoint, fifo_checkpoint;
1456 unsigned int sh = chan->in_log2_element_size;
1457 bool checkpoint_for_complete;
1459 rc = fifo_read(fifo, (__force void *)userbuf + bytes_done,
1460 count - bytes_done, xilly_copy_to_user);
1466 chan->in_consumed_bytes += rc;
1468 left_to_sleep = deadline - ((long)jiffies);
1471 * Some 32-bit arithmetic that may wrap. Note that
1472 * complete_checkpoint is rounded up to the closest element
1473 * boundary, because the read() can't be completed otherwise.
1474 * fifo_checkpoint_bytes is rounded down, because it protects
1475 * in_fifo from overflowing.
1478 fifo_checkpoint_bytes = chan->in_consumed_bytes + fifo->size;
1479 complete_checkpoint_bytes =
1480 chan->in_consumed_bytes + count - bytes_done;
1482 fifo_checkpoint = fifo_checkpoint_bytes >> sh;
1483 complete_checkpoint =
1484 (complete_checkpoint_bytes + (1 << sh) - 1) >> sh;
1486 diff = (fifo_checkpoint - complete_checkpoint) << sh;
1488 if (chan->in_synchronous && diff >= 0) {
1489 checkpoint = complete_checkpoint;
1490 checkpoint_for_complete = true;
1492 checkpoint = fifo_checkpoint;
1493 checkpoint_for_complete = false;
1496 leap = (checkpoint - chan->in_current_checkpoint) << sh;
1499 * To prevent flooding of OPCODE_SET_CHECKPOINT commands as
1500 * data is consumed, it's issued only if it moves the
1501 * checkpoint by at least an 8th of the FIFO's size, or if
1502 * it's necessary to complete the number of bytes requested by
1505 * chan->read_data_ok is checked to spare an unnecessary
1506 * submission after receiving EOF, however it's harmless if
1510 if (chan->read_data_ok &&
1511 (leap > (fifo->size >> 3) ||
1512 (checkpoint_for_complete && leap > 0))) {
1513 chan->in_current_checkpoint = checkpoint;
1514 rc = xillyusb_send_opcode(xdev, chan_num,
1515 OPCODE_SET_CHECKPOINT,
1522 if (bytes_done == count ||
1523 (left_to_sleep <= 0 && bytes_done))
1527 * Reaching here means that the FIFO was empty when
1528 * fifo_read() returned, but not necessarily right now. Error
1529 * and EOF are checked and reported only now, so that no data
1530 * that managed its way to the FIFO is lost.
1533 if (!READ_ONCE(chan->read_data_ok)) { /* FPGA has sent EOF */
1534 /* Has data slipped into the FIFO since fifo_read()? */
1536 if (READ_ONCE(fifo->fill))
1548 if (filp->f_flags & O_NONBLOCK) {
1553 if (!sent_set_push) {
1554 rc = xillyusb_send_opcode(xdev, chan_num,
1556 complete_checkpoint);
1561 sent_set_push = true;
1564 if (left_to_sleep > 0) {
1566 * Note that when xdev->error is set (e.g. when the
1567 * device is unplugged), read_data_ok turns zero and
1568 * fifo->waitq is awaken.
1569 * Therefore no special attention to xdev->error.
1572 rc = wait_event_interruptible_timeout
1574 fifo->fill || !chan->read_data_ok,
1576 } else { /* bytes_done == 0 */
1577 /* Tell FPGA to send anything it has */
1578 rc = request_read_anything(chan, OPCODE_UPDATE_PUSH);
1583 rc = wait_event_interruptible
1585 fifo->fill || !chan->read_data_ok);
1594 if (((filp->f_flags & O_NONBLOCK) || chan->poll_used) &&
1595 !READ_ONCE(fifo->fill))
1596 request_read_anything(chan, OPCODE_SET_PUSH);
1598 mutex_unlock(&chan->in_mutex);
1606 static int xillyusb_flush(struct file *filp, fl_owner_t id)
1608 struct xillyusb_channel *chan = filp->private_data;
1611 if (!(filp->f_mode & FMODE_WRITE))
1614 rc = mutex_lock_interruptible(&chan->out_mutex);
1620 * One second's timeout on flushing. Interrupts are ignored, because if
1621 * the user pressed CTRL-C, that interrupt will still be in flight by
1622 * the time we reach here, and the opportunity to flush is lost.
1624 rc = flush_downstream(chan, HZ, false);
1626 mutex_unlock(&chan->out_mutex);
1628 if (rc == -ETIMEDOUT) {
1629 /* The things you do to use dev_warn() and not pr_warn() */
1630 struct xillyusb_dev *xdev = chan->xdev;
1632 mutex_lock(&chan->lock);
1635 "Timed out while flushing. Output data may be lost.\n");
1636 mutex_unlock(&chan->lock);
1642 static ssize_t xillyusb_write(struct file *filp, const char __user *userbuf,
1643 size_t count, loff_t *f_pos)
1645 struct xillyusb_channel *chan = filp->private_data;
1646 struct xillyusb_dev *xdev = chan->xdev;
1647 struct xillyfifo *fifo = &chan->out_ep->fifo;
1650 rc = mutex_lock_interruptible(&chan->out_mutex);
1664 rc = fifo_write(fifo, (__force void *)userbuf, count,
1665 xilly_copy_from_user);
1670 if (filp->f_flags & O_NONBLOCK) {
1675 if (wait_event_interruptible
1677 fifo->fill != fifo->size || xdev->error)) {
1686 chan->out_bytes += rc;
1689 try_queue_bulk_out(chan->out_ep);
1693 if (chan->out_synchronous) {
1694 int flush_rc = flush_downstream(chan, 0, true);
1696 if (flush_rc && !rc)
1701 mutex_unlock(&chan->out_mutex);
1706 static int xillyusb_release(struct inode *inode, struct file *filp)
1708 struct xillyusb_channel *chan = filp->private_data;
1709 struct xillyusb_dev *xdev = chan->xdev;
1710 int rc_read = 0, rc_write = 0;
1712 if (filp->f_mode & FMODE_READ) {
1713 struct xillyfifo *in_fifo = chan->in_fifo;
1715 rc_read = xillyusb_send_opcode(xdev, (chan->chan_idx << 1) | 1,
1718 * If rc_read is nonzero, xdev->error indicates a global
1719 * device error. The error is reported later, so that
1720 * resources are freed.
1722 * Looping on wait_event_interruptible() kinda breaks the idea
1723 * of being interruptible, and this should have been
1724 * wait_event(). Only it's being waken with
1725 * wake_up_interruptible() for the sake of other uses. If
1726 * there's a global device error, chan->read_data_ok is
1727 * deasserted and the wait queue is awaken, so this is covered.
1730 while (wait_event_interruptible(in_fifo->waitq,
1731 !chan->read_data_ok))
1734 safely_assign_in_fifo(chan, NULL);
1735 fifo_mem_release(in_fifo);
1738 mutex_lock(&chan->lock);
1739 chan->open_for_read = 0;
1740 mutex_unlock(&chan->lock);
1743 if (filp->f_mode & FMODE_WRITE) {
1744 struct xillyusb_endpoint *ep = chan->out_ep;
1746 * chan->flushing isn't zeroed. If the pre-release flush timed
1747 * out, a cancel request will be sent before the next
1748 * OPCODE_SET_CHECKPOINT (i.e. when the file is opened again).
1749 * This is despite that the FPGA forgets about the checkpoint
1750 * request as the file closes. Still, in an exceptional race
1751 * condition, the FPGA could send an OPCODE_REACHED_CHECKPOINT
1752 * just before closing that would reach the host after the
1753 * file has re-opened.
1756 mutex_lock(&chan->lock);
1757 chan->out_ep = NULL;
1758 mutex_unlock(&chan->lock);
1760 endpoint_quiesce(ep);
1761 endpoint_dealloc(ep);
1763 /* See comments on rc_read above */
1764 rc_write = xillyusb_send_opcode(xdev, chan->chan_idx << 1,
1767 mutex_lock(&chan->lock);
1768 chan->open_for_write = 0;
1769 mutex_unlock(&chan->lock);
1772 kref_put(&xdev->kref, cleanup_dev);
1774 return rc_read ? rc_read : rc_write;
1778 * Xillybus' API allows device nodes to be seekable, giving the user
1779 * application access to a RAM array on the FPGA (or logic emulating it).
1782 static loff_t xillyusb_llseek(struct file *filp, loff_t offset, int whence)
1784 struct xillyusb_channel *chan = filp->private_data;
1785 struct xillyusb_dev *xdev = chan->xdev;
1786 loff_t pos = filp->f_pos;
1788 unsigned int log2_element_size = chan->readable ?
1789 chan->in_log2_element_size : chan->out_log2_element_size;
1792 * Take both mutexes not allowing interrupts, since it seems like
1793 * common applications don't expect an -EINTR here. Besides, multiple
1794 * access to a single file descriptor on seekable devices is a mess
1798 mutex_lock(&chan->out_mutex);
1799 mutex_lock(&chan->in_mutex);
1809 pos = offset; /* Going to the end => to the beginning */
1816 /* In any case, we must finish on an element boundary */
1817 if (pos & ((1 << log2_element_size) - 1)) {
1822 rc = xillyusb_send_opcode(xdev, chan->chan_idx << 1,
1824 pos >> log2_element_size);
1829 if (chan->writable) {
1831 rc = flush_downstream(chan, HZ, false);
1835 mutex_unlock(&chan->out_mutex);
1836 mutex_unlock(&chan->in_mutex);
1838 if (rc) /* Return error after releasing mutexes */
1846 static __poll_t xillyusb_poll(struct file *filp, poll_table *wait)
1848 struct xillyusb_channel *chan = filp->private_data;
1852 poll_wait(filp, &chan->in_fifo->waitq, wait);
1855 poll_wait(filp, &chan->out_ep->fifo.waitq, wait);
1858 * If this is the first time poll() is called, and the file is
1859 * readable, set the relevant flag. Also tell the FPGA to send all it
1860 * has, to kickstart the mechanism that ensures there's always some
1861 * data in in_fifo unless the stream is dry end-to-end. Note that the
1862 * first poll() may not return a EPOLLIN, even if there's data on the
1863 * FPGA. Rather, the data will arrive soon, and trigger the relevant
1867 if (!chan->poll_used && chan->in_fifo) {
1868 chan->poll_used = 1;
1869 request_read_anything(chan, OPCODE_SET_PUSH);
1873 * poll() won't play ball regarding read() channels which
1874 * are synchronous. Allowing that will create situations where data has
1875 * been delivered at the FPGA, and users expecting select() to wake up,
1876 * which it may not. So make it never work.
1879 if (chan->in_fifo && !chan->in_synchronous &&
1880 (READ_ONCE(chan->in_fifo->fill) || !chan->read_data_ok))
1881 mask |= EPOLLIN | EPOLLRDNORM;
1884 (READ_ONCE(chan->out_ep->fifo.fill) != chan->out_ep->fifo.size))
1885 mask |= EPOLLOUT | EPOLLWRNORM;
1887 if (chan->xdev->error)
1893 static const struct file_operations xillyusb_fops = {
1894 .owner = THIS_MODULE,
1895 .read = xillyusb_read,
1896 .write = xillyusb_write,
1897 .open = xillyusb_open,
1898 .flush = xillyusb_flush,
1899 .release = xillyusb_release,
1900 .llseek = xillyusb_llseek,
1901 .poll = xillyusb_poll,
1904 static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
1906 struct usb_device *udev = xdev->udev;
1908 /* Verify that device has the two fundamental bulk in/out endpoints */
1909 if (usb_pipe_type_check(udev, usb_sndbulkpipe(udev, MSG_EP_NUM)) ||
1910 usb_pipe_type_check(udev, usb_rcvbulkpipe(udev, IN_EP_NUM)))
1913 xdev->msg_ep = endpoint_alloc(xdev, MSG_EP_NUM | USB_DIR_OUT,
1914 bulk_out_work, 1, 2);
1918 if (fifo_init(&xdev->msg_ep->fifo, 13)) /* 8 kiB */
1921 xdev->msg_ep->fill_mask = -8; /* 8 bytes granularity */
1923 xdev->in_ep = endpoint_alloc(xdev, IN_EP_NUM | USB_DIR_IN,
1924 bulk_in_work, BUF_SIZE_ORDER, BUFNUM);
1928 try_queue_bulk_in(xdev->in_ep);
1933 endpoint_dealloc(xdev->msg_ep); /* Also frees FIFO mem if allocated */
1934 xdev->msg_ep = NULL;
1938 static int setup_channels(struct xillyusb_dev *xdev,
1942 struct usb_device *udev = xdev->udev;
1943 struct xillyusb_channel *chan, *new_channels;
1946 chan = kcalloc(num_channels, sizeof(*chan), GFP_KERNEL);
1950 new_channels = chan;
1952 for (i = 0; i < num_channels; i++, chan++) {
1953 unsigned int in_desc = le16_to_cpu(*chandesc++);
1954 unsigned int out_desc = le16_to_cpu(*chandesc++);
1957 mutex_init(&chan->in_mutex);
1958 mutex_init(&chan->out_mutex);
1959 mutex_init(&chan->lock);
1960 init_waitqueue_head(&chan->flushq);
1964 if (in_desc & 0x80) { /* Entry is valid */
1966 chan->in_synchronous = !!(in_desc & 0x40);
1967 chan->in_seekable = !!(in_desc & 0x20);
1968 chan->in_log2_element_size = in_desc & 0x0f;
1969 chan->in_log2_fifo_size = ((in_desc >> 8) & 0x1f) + 16;
1973 * A downstream channel should never exist above index 13,
1974 * as it would request a nonexistent BULK endpoint > 15.
1975 * In the peculiar case that it does, it's ignored silently.
1978 if ((out_desc & 0x80) && i < 14) { /* Entry is valid */
1979 if (usb_pipe_type_check(udev,
1980 usb_sndbulkpipe(udev, i + 2))) {
1982 "Missing BULK OUT endpoint %d\n",
1984 kfree(new_channels);
1989 chan->out_synchronous = !!(out_desc & 0x40);
1990 chan->out_seekable = !!(out_desc & 0x20);
1991 chan->out_log2_element_size = out_desc & 0x0f;
1992 chan->out_log2_fifo_size =
1993 ((out_desc >> 8) & 0x1f) + 16;
1997 xdev->channels = new_channels;
2001 static int xillyusb_discovery(struct usb_interface *interface)
2004 struct xillyusb_dev *xdev = usb_get_intfdata(interface);
2005 __le16 bogus_chandesc[2];
2006 struct xillyfifo idt_fifo;
2007 struct xillyusb_channel *chan;
2008 unsigned int idt_len, names_offset;
2012 rc = xillyusb_send_opcode(xdev, ~0, OPCODE_QUIESCE, 0);
2015 dev_err(&interface->dev, "Failed to send quiesce request. Aborting.\n");
2019 /* Phase I: Set up one fake upstream channel and obtain IDT */
2021 /* Set up a fake IDT with one async IN stream */
2022 bogus_chandesc[0] = cpu_to_le16(0x80);
2023 bogus_chandesc[1] = cpu_to_le16(0);
2025 rc = setup_channels(xdev, bogus_chandesc, 1);
2030 rc = fifo_init(&idt_fifo, LOG2_IDT_FIFO_SIZE);
2035 chan = xdev->channels;
2037 chan->in_fifo = &idt_fifo;
2038 chan->read_data_ok = 1;
2040 xdev->num_channels = 1;
2042 rc = xillyusb_send_opcode(xdev, ~0, OPCODE_REQ_IDT, 0);
2045 dev_err(&interface->dev, "Failed to send IDT request. Aborting.\n");
2049 rc = wait_event_interruptible_timeout(idt_fifo.waitq,
2050 !chan->read_data_ok,
2051 XILLY_RESPONSE_TIMEOUT);
2059 rc = -EINTR; /* Interrupt on probe method? Interesting. */
2063 if (chan->read_data_ok) {
2065 dev_err(&interface->dev, "No response from FPGA. Aborting.\n");
2069 idt_len = READ_ONCE(idt_fifo.fill);
2070 idt = kmalloc(idt_len, GFP_KERNEL);
2077 fifo_read(&idt_fifo, idt, idt_len, xilly_memcpy);
2079 if (crc32_le(~0, idt, idt_len) != 0) {
2080 dev_err(&interface->dev, "IDT failed CRC check. Aborting.\n");
2086 dev_err(&interface->dev, "No support for IDT version 0x%02x. Maybe the xillyusb driver needs an upgrade. Aborting.\n",
2092 /* Phase II: Set up the streams as defined in IDT */
2094 num_channels = le16_to_cpu(*((__le16 *)(idt + 1)));
2095 names_offset = 3 + num_channels * 4;
2096 idt_len -= 4; /* Exclude CRC */
2098 if (idt_len < names_offset) {
2099 dev_err(&interface->dev, "IDT too short. This is exceptionally weird, because its CRC is OK\n");
2104 rc = setup_channels(xdev, (void *)idt + 3, num_channels);
2110 * Except for wildly misbehaving hardware, or if it was disconnected
2111 * just after responding with the IDT, there is no reason for any
2112 * work item to be running now. To be sure that xdev->channels
2113 * is updated on anything that might run in parallel, flush the
2114 * device's workqueue and the wakeup work item. This rarely
2117 flush_workqueue(xdev->workq);
2118 flush_work(&xdev->wakeup_workitem);
2120 xdev->num_channels = num_channels;
2122 fifo_mem_release(&idt_fifo);
2125 rc = xillybus_init_chrdev(&interface->dev, &xillyusb_fops,
2128 idt_len - names_offset,
2140 safely_assign_in_fifo(chan, NULL);
2141 fifo_mem_release(&idt_fifo);
2146 static int xillyusb_probe(struct usb_interface *interface,
2147 const struct usb_device_id *id)
2149 struct xillyusb_dev *xdev;
2152 xdev = kzalloc(sizeof(*xdev), GFP_KERNEL);
2156 kref_init(&xdev->kref);
2157 mutex_init(&xdev->process_in_mutex);
2158 mutex_init(&xdev->msg_mutex);
2160 xdev->udev = usb_get_dev(interface_to_usbdev(interface));
2161 xdev->dev = &interface->dev;
2163 spin_lock_init(&xdev->error_lock);
2164 xdev->in_counter = 0;
2165 xdev->in_bytes_left = 0;
2166 xdev->workq = alloc_workqueue(xillyname, WQ_HIGHPRI, 0);
2169 dev_err(&interface->dev, "Failed to allocate work queue\n");
2174 INIT_WORK(&xdev->wakeup_workitem, wakeup_all);
2176 usb_set_intfdata(interface, xdev);
2178 rc = xillyusb_setup_base_eps(xdev);
2182 rc = xillyusb_discovery(interface);
2189 endpoint_quiesce(xdev->in_ep);
2190 endpoint_quiesce(xdev->msg_ep);
2193 usb_set_intfdata(interface, NULL);
2194 kref_put(&xdev->kref, cleanup_dev);
2198 static void xillyusb_disconnect(struct usb_interface *interface)
2200 struct xillyusb_dev *xdev = usb_get_intfdata(interface);
2201 struct xillyusb_endpoint *msg_ep = xdev->msg_ep;
2202 struct xillyfifo *fifo = &msg_ep->fifo;
2206 xillybus_cleanup_chrdev(xdev, &interface->dev);
2209 * Try to send OPCODE_QUIESCE, which will fail silently if the device
2210 * was disconnected, but makes sense on module unload.
2213 msg_ep->wake_on_drain = true;
2214 xillyusb_send_opcode(xdev, ~0, OPCODE_QUIESCE, 0);
2217 * If the device has been disconnected, sending the opcode causes
2218 * a global device error with xdev->error, if such error didn't
2219 * occur earlier. Hence timing out means that the USB link is fine,
2220 * but somehow the message wasn't sent. Should never happen.
2223 rc = wait_event_interruptible_timeout(fifo->waitq,
2224 msg_ep->drained || xdev->error,
2225 XILLY_RESPONSE_TIMEOUT);
2228 dev_err(&interface->dev,
2229 "Weird timeout condition on sending quiesce request.\n");
2231 report_io_error(xdev, -ENODEV); /* Discourage further activity */
2234 * This device driver is declared with soft_unbind set, or else
2235 * sending OPCODE_QUIESCE above would always fail. The price is
2236 * that the USB framework didn't kill outstanding URBs, so it has
2237 * to be done explicitly before returning from this call.
2240 for (i = 0; i < xdev->num_channels; i++) {
2241 struct xillyusb_channel *chan = &xdev->channels[i];
2244 * Lock taken to prevent chan->out_ep from changing. It also
2245 * ensures xillyusb_open() and xillyusb_flush() don't access
2246 * xdev->dev after being nullified below.
2248 mutex_lock(&chan->lock);
2250 endpoint_quiesce(chan->out_ep);
2251 mutex_unlock(&chan->lock);
2254 endpoint_quiesce(xdev->in_ep);
2255 endpoint_quiesce(xdev->msg_ep);
2257 usb_set_intfdata(interface, NULL);
2261 mutex_lock(&kref_mutex);
2262 kref_put(&xdev->kref, cleanup_dev);
2263 mutex_unlock(&kref_mutex);
2266 static struct usb_driver xillyusb_driver = {
2268 .id_table = xillyusb_table,
2269 .probe = xillyusb_probe,
2270 .disconnect = xillyusb_disconnect,
2274 static int __init xillyusb_init(void)
2278 wakeup_wq = alloc_workqueue(xillyname, 0, 0);
2282 if (LOG2_INITIAL_FIFO_BUF_SIZE > PAGE_SHIFT)
2283 fifo_buf_order = LOG2_INITIAL_FIFO_BUF_SIZE - PAGE_SHIFT;
2287 rc = usb_register(&xillyusb_driver);
2290 destroy_workqueue(wakeup_wq);
2295 static void __exit xillyusb_exit(void)
2297 usb_deregister(&xillyusb_driver);
2299 destroy_workqueue(wakeup_wq);
2302 module_init(xillyusb_init);
2303 module_exit(xillyusb_exit);