Lines Matching +full:channel +full:-
1 // SPDX-License-Identifier: GPL-2.0-only
20 #include <linux/dma-mapping.h>
75 * register_mutex is endpoint-specific, and is held when non-atomic
87 * wr_mutex -> rd_mutex -> register_mutex -> wr_spinlock -> rd_spinlock
101 dev_warn(endpoint->dev, in malformed_message()
102 "Malformed message (skipping): opcode=%d, channel=%03x, dir=%d, bufno=%03x, data=%07x\n", in malformed_message()
108 * which is the natural case MSI and several other hardware-oriented
120 struct xilly_channel *channel; in xillybus_isr() local
122 buf = ep->msgbuf_addr; in xillybus_isr()
123 buf_size = ep->msg_buf_size/sizeof(u32); in xillybus_isr()
125 dma_sync_single_for_cpu(ep->dev, ep->msgbuf_dma_addr, in xillybus_isr()
126 ep->msg_buf_size, DMA_FROM_DEVICE); in xillybus_isr()
129 if (((buf[i+1] >> 28) & 0xf) != ep->msg_counter) { in xillybus_isr()
131 dev_warn(ep->dev, in xillybus_isr()
134 ep->msg_counter, in xillybus_isr()
137 if (++ep->failed_messages > 10) { in xillybus_isr()
138 dev_err(ep->dev, in xillybus_isr()
141 dma_sync_single_for_device(ep->dev, in xillybus_isr()
142 ep->msgbuf_dma_addr, in xillybus_isr()
143 ep->msg_buf_size, in xillybus_isr()
147 ep->registers + fpga_msg_ctrl_reg); in xillybus_isr()
155 dev_err(ep->dev, "Bad interrupt message. Stopping.\n"); in xillybus_isr()
171 if ((msg_channel > ep->num_channels) || in xillybus_isr()
177 channel = ep->channels[msg_channel]; in xillybus_isr()
179 if (msg_dir) { /* Write channel */ in xillybus_isr()
180 if (msg_bufno >= channel->num_wr_buffers) { in xillybus_isr()
184 spin_lock(&channel->wr_spinlock); in xillybus_isr()
185 channel->wr_buffers[msg_bufno]->end_offset = in xillybus_isr()
187 channel->wr_fpga_buf_idx = msg_bufno; in xillybus_isr()
188 channel->wr_empty = 0; in xillybus_isr()
189 channel->wr_sleepy = 0; in xillybus_isr()
190 spin_unlock(&channel->wr_spinlock); in xillybus_isr()
192 wake_up_interruptible(&channel->wr_wait); in xillybus_isr()
195 /* Read channel */ in xillybus_isr()
197 if (msg_bufno >= channel->num_rd_buffers) { in xillybus_isr()
202 spin_lock(&channel->rd_spinlock); in xillybus_isr()
203 channel->rd_fpga_buf_idx = msg_bufno; in xillybus_isr()
204 channel->rd_full = 0; in xillybus_isr()
205 spin_unlock(&channel->rd_spinlock); in xillybus_isr()
207 wake_up_interruptible(&channel->rd_wait); in xillybus_isr()
208 if (!channel->rd_synchronous) in xillybus_isr()
211 &channel->rd_workitem, in xillybus_isr()
217 if ((msg_channel > ep->num_channels) || in xillybus_isr()
219 !ep->channels[msg_channel]->wr_supports_nonempty) { in xillybus_isr()
224 channel = ep->channels[msg_channel]; in xillybus_isr()
226 if (msg_bufno >= channel->num_wr_buffers) { in xillybus_isr()
230 spin_lock(&channel->wr_spinlock); in xillybus_isr()
231 if (msg_bufno == channel->wr_host_buf_idx) in xillybus_isr()
232 channel->wr_ready = 1; in xillybus_isr()
233 spin_unlock(&channel->wr_spinlock); in xillybus_isr()
235 wake_up_interruptible(&channel->wr_ready_wait); in xillybus_isr()
239 ep->idtlen = msg_data; in xillybus_isr()
240 wake_up_interruptible(&ep->ep_wait); in xillybus_isr()
244 if ((msg_channel > ep->num_channels) || in xillybus_isr()
246 !ep->channels[msg_channel]->num_wr_buffers) { in xillybus_isr()
250 channel = ep->channels[msg_channel]; in xillybus_isr()
251 spin_lock(&channel->wr_spinlock); in xillybus_isr()
252 channel->wr_eof = msg_bufno; in xillybus_isr()
253 channel->wr_sleepy = 0; in xillybus_isr()
255 channel->wr_hangup = channel->wr_empty && in xillybus_isr()
256 (channel->wr_host_buf_idx == msg_bufno); in xillybus_isr()
258 spin_unlock(&channel->wr_spinlock); in xillybus_isr()
260 wake_up_interruptible(&channel->wr_wait); in xillybus_isr()
264 ep->fatal_error = 1; in xillybus_isr()
265 wake_up_interruptible(&ep->ep_wait); /* For select() */ in xillybus_isr()
266 dev_err(ep->dev, in xillybus_isr()
267 …"FPGA reported a fatal error. This means that the low-level communication with the device has fail… in xillybus_isr()
275 dma_sync_single_for_device(ep->dev, ep->msgbuf_dma_addr, in xillybus_isr()
276 ep->msg_buf_size, DMA_FROM_DEVICE); in xillybus_isr()
278 ep->msg_counter = (ep->msg_counter + 1) & 0xf; in xillybus_isr()
279 ep->failed_messages = 0; in xillybus_isr()
280 iowrite32(0x03, ep->registers + fpga_msg_ctrl_reg); /* Message ACK */ in xillybus_isr()
306 dma_unmap_single(data->device, data->dma_addr, in xilly_unmap()
307 data->size, data->direction); in xilly_unmap()
324 return -ENOMEM; in xilly_map_single()
326 addr = dma_map_single(ep->dev, ptr, size, direction); in xilly_map_single()
328 if (dma_mapping_error(ep->dev, addr)) { in xilly_map_single()
330 return -ENODEV; in xilly_map_single()
333 this->device = ep->dev; in xilly_map_single()
334 this->dma_addr = addr; in xilly_map_single()
335 this->size = size; in xilly_map_single()
336 this->direction = direction; in xilly_map_single()
340 return devm_add_action_or_reset(ep->dev, xilly_unmap, this); in xilly_map_single()
350 struct device *dev = ep->dev; in xilly_get_dma_buffers()
358 return -ENOMEM; in xilly_get_dma_buffers()
367 if ((s->left_of_salami < bytebufsize) && in xilly_get_dma_buffers()
368 (s->left_of_salami > 0)) { in xilly_get_dma_buffers()
369 dev_err(ep->dev, in xilly_get_dma_buffers()
371 return -ENODEV; in xilly_get_dma_buffers()
374 if (s->left_of_salami == 0) { in xilly_get_dma_buffers()
384 s->salami = (void *) devm_get_free_pages( in xilly_get_dma_buffers()
388 if (!s->salami) in xilly_get_dma_buffers()
389 return -ENOMEM; in xilly_get_dma_buffers()
391 s->left_of_salami = allocsize; in xilly_get_dma_buffers()
394 rc = xilly_map_single(ep, s->salami, in xilly_get_dma_buffers()
395 bytebufsize, s->direction, in xilly_get_dma_buffers()
401 ep->registers + fpga_dma_bufaddr_lowaddr_reg); in xilly_get_dma_buffers()
403 ep->registers + fpga_dma_bufaddr_highaddr_reg); in xilly_get_dma_buffers()
406 this_buffer->addr = s->salami; in xilly_get_dma_buffers()
407 this_buffer->dma_addr = dma_addr; in xilly_get_dma_buffers()
410 iowrite32(s->regdirection | s->nbuffer++, in xilly_get_dma_buffers()
411 ep->registers + fpga_dma_bufno_reg); in xilly_get_dma_buffers()
413 ep->msgbuf_addr = s->salami; in xilly_get_dma_buffers()
414 ep->msgbuf_dma_addr = dma_addr; in xilly_get_dma_buffers()
415 ep->msg_buf_size = bytebufsize; in xilly_get_dma_buffers()
417 iowrite32(s->regdirection, in xilly_get_dma_buffers()
418 ep->registers + fpga_dma_bufno_reg); in xilly_get_dma_buffers()
421 s->left_of_salami -= bytebufsize; in xilly_get_dma_buffers()
422 s->salami += bytebufsize; in xilly_get_dma_buffers()
431 struct device *dev = ep->dev; in xilly_setupchannels()
433 struct xilly_channel *channel; in xilly_setupchannels() local
456 channel = devm_kcalloc(dev, ep->num_channels, in xilly_setupchannels()
458 if (!channel) in xilly_setupchannels()
459 return -ENOMEM; in xilly_setupchannels()
461 ep->channels = devm_kcalloc(dev, ep->num_channels + 1, in xilly_setupchannels()
464 if (!ep->channels) in xilly_setupchannels()
465 return -ENOMEM; in xilly_setupchannels()
467 ep->channels[0] = NULL; /* Channel 0 is message buf. */ in xilly_setupchannels()
471 for (i = 1; i <= ep->num_channels; i++) { in xilly_setupchannels()
472 channel->wr_buffers = NULL; in xilly_setupchannels()
473 channel->rd_buffers = NULL; in xilly_setupchannels()
474 channel->num_wr_buffers = 0; in xilly_setupchannels()
475 channel->num_rd_buffers = 0; in xilly_setupchannels()
476 channel->wr_fpga_buf_idx = -1; in xilly_setupchannels()
477 channel->wr_host_buf_idx = 0; in xilly_setupchannels()
478 channel->wr_host_buf_pos = 0; in xilly_setupchannels()
479 channel->wr_empty = 1; in xilly_setupchannels()
480 channel->wr_ready = 0; in xilly_setupchannels()
481 channel->wr_sleepy = 1; in xilly_setupchannels()
482 channel->rd_fpga_buf_idx = 0; in xilly_setupchannels()
483 channel->rd_host_buf_idx = 0; in xilly_setupchannels()
484 channel->rd_host_buf_pos = 0; in xilly_setupchannels()
485 channel->rd_full = 0; in xilly_setupchannels()
486 channel->wr_ref_count = 0; in xilly_setupchannels()
487 channel->rd_ref_count = 0; in xilly_setupchannels()
489 spin_lock_init(&channel->wr_spinlock); in xilly_setupchannels()
490 spin_lock_init(&channel->rd_spinlock); in xilly_setupchannels()
491 mutex_init(&channel->wr_mutex); in xilly_setupchannels()
492 mutex_init(&channel->rd_mutex); in xilly_setupchannels()
493 init_waitqueue_head(&channel->rd_wait); in xilly_setupchannels()
494 init_waitqueue_head(&channel->wr_wait); in xilly_setupchannels()
495 init_waitqueue_head(&channel->wr_ready_wait); in xilly_setupchannels()
497 INIT_DELAYED_WORK(&channel->rd_workitem, xillybus_autoflush); in xilly_setupchannels()
499 channel->endpoint = ep; in xilly_setupchannels()
500 channel->chan_num = i; in xilly_setupchannels()
502 channel->log2_element_size = 0; in xilly_setupchannels()
504 ep->channels[i] = channel++; in xilly_setupchannels()
521 if ((channelnum > ep->num_channels) || in xilly_setupchannels()
523 dev_err(ep->dev, in xilly_setupchannels()
524 "IDT requests channel out of range. Aborting.\n"); in xilly_setupchannels()
525 return -ENODEV; in xilly_setupchannels()
528 channel = ep->channels[channelnum]; /* NULL for msg channel */ in xilly_setupchannels()
531 channel->log2_element_size = ((format > 2) ? in xilly_setupchannels()
535 (1 << channel->log2_element_size); in xilly_setupchannels()
541 return -ENOMEM; in xilly_setupchannels()
547 channel->num_rd_buffers = bufnum; in xilly_setupchannels()
548 channel->rd_buf_size = bytebufsize; in xilly_setupchannels()
549 channel->rd_allow_partial = allowpartial; in xilly_setupchannels()
550 channel->rd_synchronous = synchronous; in xilly_setupchannels()
551 channel->rd_exclusive_open = exclusive_open; in xilly_setupchannels()
552 channel->seekable = seekable; in xilly_setupchannels()
554 channel->rd_buffers = buffers; in xilly_setupchannels()
558 channel->num_wr_buffers = bufnum; in xilly_setupchannels()
559 channel->wr_buf_size = bytebufsize; in xilly_setupchannels()
561 channel->seekable = seekable; in xilly_setupchannels()
562 channel->wr_supports_nonempty = supports_nonempty; in xilly_setupchannels()
564 channel->wr_allow_partial = allowpartial; in xilly_setupchannels()
565 channel->wr_synchronous = synchronous; in xilly_setupchannels()
566 channel->wr_exclusive_open = exclusive_open; in xilly_setupchannels()
568 channel->wr_buffers = buffers; in xilly_setupchannels()
578 return -ENOMEM; in xilly_setupchannels()
582 dev_err(ep->dev, in xilly_setupchannels()
584 return -ENODEV; in xilly_setupchannels()
593 unsigned char *idt = endpoint->channels[1]->wr_buffers[0]->addr; in xilly_scan_idt()
594 unsigned char *end_of_idt = idt + endpoint->idtlen - 4; in xilly_scan_idt()
599 idt_handle->names = scan; in xilly_scan_idt()
607 idt_handle->names_len = scan - idt_handle->names; in xilly_scan_idt()
612 dev_err(endpoint->dev, in xilly_scan_idt()
614 return -ENODEV; in xilly_scan_idt()
616 idt_handle->chandesc = scan; in xilly_scan_idt()
618 len = endpoint->idtlen - (3 + ((int) (scan - idt))); in xilly_scan_idt()
621 dev_err(endpoint->dev, in xilly_scan_idt()
623 return -ENODEV; in xilly_scan_idt()
626 idt_handle->entries = len >> 2; in xilly_scan_idt()
627 endpoint->num_channels = count; in xilly_scan_idt()
634 struct xilly_channel *channel; in xilly_obtain_idt() local
638 channel = endpoint->channels[1]; /* This should be generated ad-hoc */ in xilly_obtain_idt()
640 channel->wr_sleepy = 1; in xilly_obtain_idt()
643 (3 << 24), /* Opcode 3 for channel 0 = Send IDT */ in xilly_obtain_idt()
644 endpoint->registers + fpga_buf_ctrl_reg); in xilly_obtain_idt()
646 t = wait_event_interruptible_timeout(channel->wr_wait, in xilly_obtain_idt()
647 (!channel->wr_sleepy), in xilly_obtain_idt()
651 dev_err(endpoint->dev, "Failed to obtain IDT. Aborting.\n"); in xilly_obtain_idt()
653 if (endpoint->fatal_error) in xilly_obtain_idt()
654 return -EIO; in xilly_obtain_idt()
656 return -ENODEV; in xilly_obtain_idt()
659 dma_sync_single_for_cpu(channel->endpoint->dev, in xilly_obtain_idt()
660 channel->wr_buffers[0]->dma_addr, in xilly_obtain_idt()
661 channel->wr_buf_size, in xilly_obtain_idt()
664 if (channel->wr_buffers[0]->end_offset != endpoint->idtlen) { in xilly_obtain_idt()
665 dev_err(endpoint->dev, in xilly_obtain_idt()
667 channel->wr_buffers[0]->end_offset, endpoint->idtlen); in xilly_obtain_idt()
668 return -ENODEV; in xilly_obtain_idt()
671 if (crc32_le(~0, channel->wr_buffers[0]->addr, in xilly_obtain_idt()
672 endpoint->idtlen+1) != 0) { in xilly_obtain_idt()
673 dev_err(endpoint->dev, "IDT failed CRC check. Aborting.\n"); in xilly_obtain_idt()
674 return -ENODEV; in xilly_obtain_idt()
677 version = channel->wr_buffers[0]->addr; in xilly_obtain_idt()
681 dev_err(endpoint->dev, in xilly_obtain_idt()
684 return -ENODEV; in xilly_obtain_idt()
698 struct xilly_channel *channel = filp->private_data; in xillybus_read() local
706 if (channel->endpoint->fatal_error) in xillybus_read()
707 return -EIO; in xillybus_read()
711 rc = mutex_lock_interruptible(&channel->wr_mutex); in xillybus_read()
716 int bytes_to_do = count - bytes_done; in xillybus_read()
718 spin_lock_irqsave(&channel->wr_spinlock, flags); in xillybus_read()
720 empty = channel->wr_empty; in xillybus_read()
721 ready = !empty || channel->wr_ready; in xillybus_read()
724 bufidx = channel->wr_host_buf_idx; in xillybus_read()
725 bufpos = channel->wr_host_buf_pos; in xillybus_read()
726 howmany = ((channel->wr_buffers[bufidx]->end_offset in xillybus_read()
727 + 1) << channel->log2_element_size) in xillybus_read()
728 - bufpos; in xillybus_read()
730 /* Update wr_host_* to its post-operation state */ in xillybus_read()
735 channel->wr_host_buf_pos += howmany; in xillybus_read()
739 channel->wr_host_buf_pos = 0; in xillybus_read()
741 if (bufidx == channel->wr_fpga_buf_idx) { in xillybus_read()
742 channel->wr_empty = 1; in xillybus_read()
743 channel->wr_sleepy = 1; in xillybus_read()
744 channel->wr_ready = 0; in xillybus_read()
747 if (bufidx >= (channel->num_wr_buffers - 1)) in xillybus_read()
748 channel->wr_host_buf_idx = 0; in xillybus_read()
750 channel->wr_host_buf_idx++; in xillybus_read()
762 reached_eof = channel->wr_empty && in xillybus_read()
763 (channel->wr_host_buf_idx == channel->wr_eof); in xillybus_read()
764 channel->wr_hangup = reached_eof; in xillybus_read()
765 exhausted = channel->wr_empty; in xillybus_read()
766 waiting_bufidx = channel->wr_host_buf_idx; in xillybus_read()
768 spin_unlock_irqrestore(&channel->wr_spinlock, flags); in xillybus_read()
773 dma_sync_single_for_cpu(channel->endpoint->dev, in xillybus_read()
774 channel->wr_buffers[bufidx]->dma_addr, in xillybus_read()
775 channel->wr_buf_size, in xillybus_read()
780 channel->wr_buffers[bufidx]->addr in xillybus_read()
782 rc = -EFAULT; in xillybus_read()
788 dma_sync_single_for_device(channel->endpoint->dev, in xillybus_read()
789 channel->wr_buffers[bufidx]->dma_addr, in xillybus_read()
790 channel->wr_buf_size, in xillybus_read()
797 * and the certain channel is protected with in xillybus_read()
798 * the channel-specific mutex. in xillybus_read()
801 iowrite32(1 | (channel->chan_num << 1) | in xillybus_read()
803 channel->endpoint->registers + in xillybus_read()
808 mutex_unlock(&channel->wr_mutex); in xillybus_read()
813 /* This includes a zero-count return = EOF */ in xillybus_read()
822 (channel->wr_synchronous && channel->wr_allow_partial))) in xillybus_read()
827 * has data to send. In non-blocking mode, if it isn't on, in xillybus_read()
834 if (!no_time_left && (filp->f_flags & O_NONBLOCK)) { in xillybus_read()
841 rc = -EAGAIN; in xillybus_read()
847 * Note that in case of an element-misaligned read in xillybus_read()
851 int offsetlimit = ((count - bytes_done) - 1) >> in xillybus_read()
852 channel->log2_element_size; in xillybus_read()
853 int buf_elements = channel->wr_buf_size >> in xillybus_read()
854 channel->log2_element_size; in xillybus_read()
861 if (channel->wr_synchronous) { in xillybus_read()
863 if (channel->wr_allow_partial && in xillybus_read()
865 offsetlimit = buf_elements - 1; in xillybus_read()
868 if (!channel->wr_allow_partial && in xillybus_read()
870 (buf_elements * channel->num_wr_buffers))) in xillybus_read()
872 channel->num_wr_buffers - 1; in xillybus_read()
883 if (channel->wr_synchronous || in xillybus_read()
884 (offsetlimit < (buf_elements - 1))) { in xillybus_read()
885 mutex_lock(&channel->endpoint->register_mutex); in xillybus_read()
888 channel->endpoint->registers + in xillybus_read()
891 iowrite32(1 | (channel->chan_num << 1) | in xillybus_read()
894 channel->endpoint->registers + in xillybus_read()
897 mutex_unlock(&channel->endpoint-> in xillybus_read()
908 if (!channel->wr_allow_partial || in xillybus_read()
911 * This do-loop will run more than once if another in xillybus_read()
917 mutex_unlock(&channel->wr_mutex); in xillybus_read()
920 channel->wr_wait, in xillybus_read()
921 (!channel->wr_sleepy))) in xillybus_read()
925 &channel->wr_mutex)) in xillybus_read()
927 } while (channel->wr_sleepy); in xillybus_read()
932 if (channel->endpoint->fatal_error) in xillybus_read()
933 return -EIO; in xillybus_read()
936 if (filp->f_flags & O_NONBLOCK) in xillybus_read()
937 return -EAGAIN; /* Don't admit snoozing */ in xillybus_read()
938 return -EINTR; in xillybus_read()
941 left_to_sleep = deadline - ((long) jiffies); in xillybus_read()
952 channel->wr_wait, in xillybus_read()
953 (!channel->wr_sleepy), in xillybus_read()
960 mutex_unlock(&channel->wr_mutex); in xillybus_read()
961 if (channel->endpoint->fatal_error) in xillybus_read()
962 return -EIO; in xillybus_read()
965 return -EINTR; in xillybus_read()
980 iowrite32(1 | (channel->chan_num << 1) | in xillybus_read()
983 channel->endpoint->registers + in xillybus_read()
996 mutex_unlock(&channel->wr_mutex); in xillybus_read()
998 if (channel->endpoint->fatal_error) in xillybus_read()
999 return -EIO; in xillybus_read()
1014 static int xillybus_myflush(struct xilly_channel *channel, long timeout) in xillybus_myflush() argument
1025 if (channel->endpoint->fatal_error) in xillybus_myflush()
1026 return -EIO; in xillybus_myflush()
1027 rc = mutex_lock_interruptible(&channel->rd_mutex); in xillybus_myflush()
1032 * Don't flush a closed channel. This can happen when the work queued in xillybus_myflush()
1037 if (!channel->rd_ref_count) in xillybus_myflush()
1040 bufidx = channel->rd_host_buf_idx; in xillybus_myflush()
1043 channel->num_rd_buffers - 1 : in xillybus_myflush()
1044 bufidx - 1; in xillybus_myflush()
1046 end_offset_plus1 = channel->rd_host_buf_pos >> in xillybus_myflush()
1047 channel->log2_element_size; in xillybus_myflush()
1049 new_rd_host_buf_pos = channel->rd_host_buf_pos - in xillybus_myflush()
1050 (end_offset_plus1 << channel->log2_element_size); in xillybus_myflush()
1054 unsigned char *tail = channel->rd_buffers[bufidx]->addr + in xillybus_myflush()
1055 (end_offset_plus1 << channel->log2_element_size); in xillybus_myflush()
1059 channel->rd_leftovers[i] = *tail++; in xillybus_myflush()
1061 spin_lock_irqsave(&channel->rd_spinlock, flags); in xillybus_myflush()
1066 (channel->rd_full || in xillybus_myflush()
1067 (bufidx_minus1 != channel->rd_fpga_buf_idx))) { in xillybus_myflush()
1068 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_myflush()
1078 channel->rd_leftovers[3] = (new_rd_host_buf_pos != 0); in xillybus_myflush()
1082 if (bufidx == channel->rd_fpga_buf_idx) in xillybus_myflush()
1083 channel->rd_full = 1; in xillybus_myflush()
1084 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_myflush()
1086 if (bufidx >= (channel->num_rd_buffers - 1)) in xillybus_myflush()
1087 channel->rd_host_buf_idx = 0; in xillybus_myflush()
1089 channel->rd_host_buf_idx++; in xillybus_myflush()
1091 dma_sync_single_for_device(channel->endpoint->dev, in xillybus_myflush()
1092 channel->rd_buffers[bufidx]->dma_addr, in xillybus_myflush()
1093 channel->rd_buf_size, in xillybus_myflush()
1096 mutex_lock(&channel->endpoint->register_mutex); in xillybus_myflush()
1098 iowrite32(end_offset_plus1 - 1, in xillybus_myflush()
1099 channel->endpoint->registers + fpga_buf_offset_reg); in xillybus_myflush()
1101 iowrite32((channel->chan_num << 1) | /* Channel ID */ in xillybus_myflush()
1104 channel->endpoint->registers + fpga_buf_ctrl_reg); in xillybus_myflush()
1106 mutex_unlock(&channel->endpoint->register_mutex); in xillybus_myflush()
1108 bufidx = channel->num_rd_buffers - 1; in xillybus_myflush()
1110 bufidx--; in xillybus_myflush()
1113 channel->rd_host_buf_pos = new_rd_host_buf_pos; in xillybus_myflush()
1121 * channel->rd_host_buf_idx the one after it. in xillybus_myflush()
1123 * If bufidx == channel->rd_fpga_buf_idx we're either empty or full. in xillybus_myflush()
1127 spin_lock_irqsave(&channel->rd_spinlock, flags); in xillybus_myflush()
1129 if (bufidx != channel->rd_fpga_buf_idx) in xillybus_myflush()
1130 channel->rd_full = 1; /* in xillybus_myflush()
1135 empty = !channel->rd_full; in xillybus_myflush()
1137 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_myflush()
1148 wait_event_interruptible(channel->rd_wait, in xillybus_myflush()
1149 (!channel->rd_full)); in xillybus_myflush()
1152 channel->rd_wait, in xillybus_myflush()
1153 (!channel->rd_full), in xillybus_myflush()
1155 dev_warn(channel->endpoint->dev, in xillybus_myflush()
1158 rc = -ETIMEDOUT; in xillybus_myflush()
1162 if (channel->rd_full) { in xillybus_myflush()
1163 rc = -EINTR; in xillybus_myflush()
1169 mutex_unlock(&channel->rd_mutex); in xillybus_myflush()
1171 if (channel->endpoint->fatal_error) in xillybus_myflush()
1172 return -EIO; in xillybus_myflush()
1179 if (!(filp->f_mode & FMODE_WRITE)) in xillybus_flush()
1182 return xillybus_myflush(filp->private_data, HZ); /* 1 second timeout */ in xillybus_flush()
1189 struct xilly_channel *channel = container_of( in xillybus_autoflush() local
1193 rc = xillybus_myflush(channel, -1); in xillybus_autoflush()
1194 if (rc == -EINTR) in xillybus_autoflush()
1195 dev_warn(channel->endpoint->dev, in xillybus_autoflush()
1198 dev_err(channel->endpoint->dev, in xillybus_autoflush()
1208 struct xilly_channel *channel = filp->private_data; in xillybus_write() local
1216 if (channel->endpoint->fatal_error) in xillybus_write()
1217 return -EIO; in xillybus_write()
1219 rc = mutex_lock_interruptible(&channel->rd_mutex); in xillybus_write()
1224 int bytes_to_do = count - bytes_done; in xillybus_write()
1226 spin_lock_irqsave(&channel->rd_spinlock, flags); in xillybus_write()
1228 full = channel->rd_full; in xillybus_write()
1231 bufidx = channel->rd_host_buf_idx; in xillybus_write()
1232 bufpos = channel->rd_host_buf_pos; in xillybus_write()
1233 howmany = channel->rd_buf_size - bufpos; in xillybus_write()
1243 ((bufpos >> channel->log2_element_size) == 0))) { in xillybus_write()
1247 channel->rd_host_buf_pos += howmany; in xillybus_write()
1253 channel->rd_buf_size >> in xillybus_write()
1254 channel->log2_element_size; in xillybus_write()
1255 channel->rd_host_buf_pos = 0; in xillybus_write()
1263 channel->log2_element_size; in xillybus_write()
1265 channel->rd_host_buf_pos -= in xillybus_write()
1267 channel->log2_element_size; in xillybus_write()
1269 tail = channel-> in xillybus_write()
1270 rd_buffers[bufidx]->addr + in xillybus_write()
1272 channel->log2_element_size); in xillybus_write()
1275 i < channel->rd_host_buf_pos; in xillybus_write()
1277 channel->rd_leftovers[i] = in xillybus_write()
1281 if (bufidx == channel->rd_fpga_buf_idx) in xillybus_write()
1282 channel->rd_full = 1; in xillybus_write()
1284 if (bufidx >= (channel->num_rd_buffers - 1)) in xillybus_write()
1285 channel->rd_host_buf_idx = 0; in xillybus_write()
1287 channel->rd_host_buf_idx++; in xillybus_write()
1299 exhausted = channel->rd_full; in xillybus_write()
1301 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_write()
1305 channel->rd_buffers[bufidx]->addr; in xillybus_write()
1309 (channel->rd_leftovers[3] != 0)) { in xillybus_write()
1310 dma_sync_single_for_cpu(channel->endpoint->dev, in xillybus_write()
1311 channel->rd_buffers[bufidx]->dma_addr, in xillybus_write()
1312 channel->rd_buf_size, in xillybus_write()
1317 *head++ = channel->rd_leftovers[i]; in xillybus_write()
1319 channel->rd_leftovers[3] = 0; /* Clear flag */ in xillybus_write()
1323 channel->rd_buffers[bufidx]->addr + bufpos, in xillybus_write()
1325 rc = -EFAULT; in xillybus_write()
1331 dma_sync_single_for_device(channel->endpoint->dev, in xillybus_write()
1332 channel->rd_buffers[bufidx]->dma_addr, in xillybus_write()
1333 channel->rd_buf_size, in xillybus_write()
1336 mutex_lock(&channel->endpoint->register_mutex); in xillybus_write()
1338 iowrite32(end_offset_plus1 - 1, in xillybus_write()
1339 channel->endpoint->registers + in xillybus_write()
1342 iowrite32((channel->chan_num << 1) | in xillybus_write()
1345 channel->endpoint->registers + in xillybus_write()
1348 mutex_unlock(&channel->endpoint-> in xillybus_write()
1351 channel->rd_leftovers[3] = in xillybus_write()
1352 (channel->rd_host_buf_pos != 0); in xillybus_write()
1356 mutex_unlock(&channel->rd_mutex); in xillybus_write()
1358 if (channel->endpoint->fatal_error) in xillybus_write()
1359 return -EIO; in xillybus_write()
1361 if (!channel->rd_synchronous) in xillybus_write()
1364 &channel->rd_workitem, in xillybus_write()
1377 if ((bytes_done > 0) && channel->rd_allow_partial) in xillybus_write()
1386 if (filp->f_flags & O_NONBLOCK) { in xillybus_write()
1387 rc = -EAGAIN; in xillybus_write()
1391 if (wait_event_interruptible(channel->rd_wait, in xillybus_write()
1392 (!channel->rd_full))) { in xillybus_write()
1393 mutex_unlock(&channel->rd_mutex); in xillybus_write()
1395 if (channel->endpoint->fatal_error) in xillybus_write()
1396 return -EIO; in xillybus_write()
1400 return -EINTR; in xillybus_write()
1404 mutex_unlock(&channel->rd_mutex); in xillybus_write()
1406 if (!channel->rd_synchronous) in xillybus_write()
1408 &channel->rd_workitem, in xillybus_write()
1411 if (channel->endpoint->fatal_error) in xillybus_write()
1412 return -EIO; in xillybus_write()
1417 if ((channel->rd_synchronous) && (bytes_done > 0)) { in xillybus_write()
1418 rc = xillybus_myflush(filp->private_data, 0); /* No timeout */ in xillybus_write()
1420 if (rc && (rc != -EINTR)) in xillybus_write()
1432 struct xilly_channel *channel; in xillybus_open() local
1439 if (endpoint->fatal_error) in xillybus_open()
1440 return -EIO; in xillybus_open()
1442 channel = endpoint->channels[1 + index]; in xillybus_open()
1443 filp->private_data = channel; in xillybus_open()
1451 if ((filp->f_mode & FMODE_READ) && (!channel->num_wr_buffers)) in xillybus_open()
1452 return -ENODEV; in xillybus_open()
1454 if ((filp->f_mode & FMODE_WRITE) && (!channel->num_rd_buffers)) in xillybus_open()
1455 return -ENODEV; in xillybus_open()
1457 if ((filp->f_mode & FMODE_READ) && (filp->f_flags & O_NONBLOCK) && in xillybus_open()
1458 (channel->wr_synchronous || !channel->wr_allow_partial || in xillybus_open()
1459 !channel->wr_supports_nonempty)) { in xillybus_open()
1460 dev_err(endpoint->dev, in xillybus_open()
1462 return -ENODEV; in xillybus_open()
1465 if ((filp->f_mode & FMODE_WRITE) && (filp->f_flags & O_NONBLOCK) && in xillybus_open()
1466 (channel->rd_synchronous || !channel->rd_allow_partial)) { in xillybus_open()
1467 dev_err(endpoint->dev, in xillybus_open()
1469 return -ENODEV; in xillybus_open()
1476 * (*_exclusive_open is normally set in real-life systems). in xillybus_open()
1479 if (filp->f_mode & FMODE_READ) { in xillybus_open()
1480 rc = mutex_lock_interruptible(&channel->wr_mutex); in xillybus_open()
1485 if (filp->f_mode & FMODE_WRITE) { in xillybus_open()
1486 rc = mutex_lock_interruptible(&channel->rd_mutex); in xillybus_open()
1491 if ((filp->f_mode & FMODE_READ) && in xillybus_open()
1492 (channel->wr_ref_count != 0) && in xillybus_open()
1493 (channel->wr_exclusive_open)) { in xillybus_open()
1494 rc = -EBUSY; in xillybus_open()
1498 if ((filp->f_mode & FMODE_WRITE) && in xillybus_open()
1499 (channel->rd_ref_count != 0) && in xillybus_open()
1500 (channel->rd_exclusive_open)) { in xillybus_open()
1501 rc = -EBUSY; in xillybus_open()
1505 if (filp->f_mode & FMODE_READ) { in xillybus_open()
1506 if (channel->wr_ref_count == 0) { /* First open of file */ in xillybus_open()
1508 spin_lock_irqsave(&channel->wr_spinlock, flags); in xillybus_open()
1509 channel->wr_host_buf_idx = 0; in xillybus_open()
1510 channel->wr_host_buf_pos = 0; in xillybus_open()
1511 channel->wr_fpga_buf_idx = -1; in xillybus_open()
1512 channel->wr_empty = 1; in xillybus_open()
1513 channel->wr_ready = 0; in xillybus_open()
1514 channel->wr_sleepy = 1; in xillybus_open()
1515 channel->wr_eof = -1; in xillybus_open()
1516 channel->wr_hangup = 0; in xillybus_open()
1518 spin_unlock_irqrestore(&channel->wr_spinlock, flags); in xillybus_open()
1520 iowrite32(1 | (channel->chan_num << 1) | in xillybus_open()
1521 (4 << 24) | /* Opcode 4, open channel */ in xillybus_open()
1522 ((channel->wr_synchronous & 1) << 23), in xillybus_open()
1523 channel->endpoint->registers + in xillybus_open()
1527 channel->wr_ref_count++; in xillybus_open()
1530 if (filp->f_mode & FMODE_WRITE) { in xillybus_open()
1531 if (channel->rd_ref_count == 0) { /* First open of file */ in xillybus_open()
1533 spin_lock_irqsave(&channel->rd_spinlock, flags); in xillybus_open()
1534 channel->rd_host_buf_idx = 0; in xillybus_open()
1535 channel->rd_host_buf_pos = 0; in xillybus_open()
1536 channel->rd_leftovers[3] = 0; /* No leftovers. */ in xillybus_open()
1537 channel->rd_fpga_buf_idx = channel->num_rd_buffers - 1; in xillybus_open()
1538 channel->rd_full = 0; in xillybus_open()
1540 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_open()
1542 iowrite32((channel->chan_num << 1) | in xillybus_open()
1543 (4 << 24), /* Opcode 4, open channel */ in xillybus_open()
1544 channel->endpoint->registers + in xillybus_open()
1548 channel->rd_ref_count++; in xillybus_open()
1552 if (filp->f_mode & FMODE_WRITE) in xillybus_open()
1553 mutex_unlock(&channel->rd_mutex); in xillybus_open()
1555 if (filp->f_mode & FMODE_READ) in xillybus_open()
1556 mutex_unlock(&channel->wr_mutex); in xillybus_open()
1558 if (!rc && (!channel->seekable)) in xillybus_open()
1567 struct xilly_channel *channel = filp->private_data; in xillybus_release() local
1572 if (channel->endpoint->fatal_error) in xillybus_release()
1573 return -EIO; in xillybus_release()
1575 if (filp->f_mode & FMODE_WRITE) { in xillybus_release()
1576 mutex_lock(&channel->rd_mutex); in xillybus_release()
1578 channel->rd_ref_count--; in xillybus_release()
1580 if (channel->rd_ref_count == 0) { in xillybus_release()
1586 iowrite32((channel->chan_num << 1) | /* Channel ID */ in xillybus_release()
1587 (5 << 24), /* Opcode 5, close channel */ in xillybus_release()
1588 channel->endpoint->registers + in xillybus_release()
1591 mutex_unlock(&channel->rd_mutex); in xillybus_release()
1594 if (filp->f_mode & FMODE_READ) { in xillybus_release()
1595 mutex_lock(&channel->wr_mutex); in xillybus_release()
1597 channel->wr_ref_count--; in xillybus_release()
1599 if (channel->wr_ref_count == 0) { in xillybus_release()
1600 iowrite32(1 | (channel->chan_num << 1) | in xillybus_release()
1601 (5 << 24), /* Opcode 5, close channel */ in xillybus_release()
1602 channel->endpoint->registers + in xillybus_release()
1608 * the channel or because of a user's EOF), but verify in xillybus_release()
1616 spin_lock_irqsave(&channel->wr_spinlock, in xillybus_release()
1618 buf_idx = channel->wr_fpga_buf_idx; in xillybus_release()
1619 eof = channel->wr_eof; in xillybus_release()
1620 channel->wr_sleepy = 1; in xillybus_release()
1621 spin_unlock_irqrestore(&channel->wr_spinlock, in xillybus_release()
1631 if (buf_idx == channel->num_wr_buffers) in xillybus_release()
1642 * non-responsive. in xillybus_release()
1646 channel->wr_wait, in xillybus_release()
1647 (!channel->wr_sleepy))) in xillybus_release()
1650 if (channel->wr_sleepy) { in xillybus_release()
1651 mutex_unlock(&channel->wr_mutex); in xillybus_release()
1652 dev_warn(channel->endpoint->dev, in xillybus_release()
1654 return -EINTR; in xillybus_release()
1659 mutex_unlock(&channel->wr_mutex); in xillybus_release()
1667 struct xilly_channel *channel = filp->private_data; in xillybus_llseek() local
1668 loff_t pos = filp->f_pos; in xillybus_llseek()
1673 * common applications don't expect an -EINTR here. Besides, multiple in xillybus_llseek()
1678 if (channel->endpoint->fatal_error) in xillybus_llseek()
1679 return -EIO; in xillybus_llseek()
1681 mutex_lock(&channel->wr_mutex); in xillybus_llseek()
1682 mutex_lock(&channel->rd_mutex); in xillybus_llseek()
1695 rc = -EINVAL; in xillybus_llseek()
1700 if (pos & ((1 << channel->log2_element_size) - 1)) { in xillybus_llseek()
1701 rc = -EINVAL; in xillybus_llseek()
1705 mutex_lock(&channel->endpoint->register_mutex); in xillybus_llseek()
1707 iowrite32(pos >> channel->log2_element_size, in xillybus_llseek()
1708 channel->endpoint->registers + fpga_buf_offset_reg); in xillybus_llseek()
1710 iowrite32((channel->chan_num << 1) | in xillybus_llseek()
1712 channel->endpoint->registers + fpga_buf_ctrl_reg); in xillybus_llseek()
1714 mutex_unlock(&channel->endpoint->register_mutex); in xillybus_llseek()
1717 mutex_unlock(&channel->rd_mutex); in xillybus_llseek()
1718 mutex_unlock(&channel->wr_mutex); in xillybus_llseek()
1723 filp->f_pos = pos; in xillybus_llseek()
1726 * Since seekable devices are allowed only when the channel is in xillybus_llseek()
1734 channel->rd_leftovers[3] = 0; in xillybus_llseek()
1741 struct xilly_channel *channel = filp->private_data; in xillybus_poll() local
1745 poll_wait(filp, &channel->endpoint->ep_wait, wait); in xillybus_poll()
1755 if (!channel->wr_synchronous && channel->wr_supports_nonempty) { in xillybus_poll()
1756 poll_wait(filp, &channel->wr_wait, wait); in xillybus_poll()
1757 poll_wait(filp, &channel->wr_ready_wait, wait); in xillybus_poll()
1759 spin_lock_irqsave(&channel->wr_spinlock, flags); in xillybus_poll()
1760 if (!channel->wr_empty || channel->wr_ready) in xillybus_poll()
1763 if (channel->wr_hangup) in xillybus_poll()
1770 spin_unlock_irqrestore(&channel->wr_spinlock, flags); in xillybus_poll()
1774 * If partial data write is disallowed on a write() channel, in xillybus_poll()
1779 if (channel->rd_allow_partial) { in xillybus_poll()
1780 poll_wait(filp, &channel->rd_wait, wait); in xillybus_poll()
1782 spin_lock_irqsave(&channel->rd_spinlock, flags); in xillybus_poll()
1783 if (!channel->rd_full) in xillybus_poll()
1785 spin_unlock_irqrestore(&channel->rd_spinlock, flags); in xillybus_poll()
1788 if (channel->endpoint->fatal_error) in xillybus_poll()
1813 endpoint->dev = dev; in xillybus_init_endpoint()
1814 endpoint->msg_counter = 0x0b; in xillybus_init_endpoint()
1815 endpoint->failed_messages = 0; in xillybus_init_endpoint()
1816 endpoint->fatal_error = 0; in xillybus_init_endpoint()
1818 init_waitqueue_head(&endpoint->ep_wait); in xillybus_init_endpoint()
1819 mutex_init(&endpoint->register_mutex); in xillybus_init_endpoint()
1829 endpoint->idtlen = -1; in xilly_quiesce()
1831 iowrite32((u32) (endpoint->dma_using_dac & 0x0001), in xilly_quiesce()
1832 endpoint->registers + fpga_dma_control_reg); in xilly_quiesce()
1834 t = wait_event_interruptible_timeout(endpoint->ep_wait, in xilly_quiesce()
1835 (endpoint->idtlen >= 0), in xilly_quiesce()
1838 dev_err(endpoint->dev, in xilly_quiesce()
1840 return -ENODEV; in xilly_quiesce()
1852 struct device *dev = endpoint->dev; in xillybus_endpoint_discovery()
1861 unsigned char bogus_idt[8] = { 1, 224, (PAGE_SHIFT)-2, 0, in xillybus_endpoint_discovery()
1871 iowrite32(1, endpoint->registers + fpga_endian_reg); in xillybus_endpoint_discovery()
1877 return -ENOMEM; in xillybus_endpoint_discovery()
1879 endpoint->num_channels = 0; in xillybus_endpoint_discovery()
1886 iowrite32(0x04, endpoint->registers + fpga_msg_ctrl_reg); in xillybus_endpoint_discovery()
1888 endpoint->idtlen = -1; in xillybus_endpoint_discovery()
1894 iowrite32((u32) (endpoint->dma_using_dac & 0x0001), in xillybus_endpoint_discovery()
1895 endpoint->registers + fpga_dma_control_reg); in xillybus_endpoint_discovery()
1897 t = wait_event_interruptible_timeout(endpoint->ep_wait, in xillybus_endpoint_discovery()
1898 (endpoint->idtlen >= 0), in xillybus_endpoint_discovery()
1901 dev_err(endpoint->dev, "No response from FPGA. Aborting.\n"); in xillybus_endpoint_discovery()
1902 return -ENODEV; in xillybus_endpoint_discovery()
1906 iowrite32((u32) (0x0002 | (endpoint->dma_using_dac & 0x0001)), in xillybus_endpoint_discovery()
1907 endpoint->registers + fpga_dma_control_reg); in xillybus_endpoint_discovery()
1910 while (endpoint->idtlen >= idtbuffersize) { in xillybus_endpoint_discovery()
1915 endpoint->num_channels = 1; in xillybus_endpoint_discovery()
1940 endpoint->owner, endpoint, in xillybus_endpoint_discovery()
1943 endpoint->num_channels, in xillybus_endpoint_discovery()
1963 xillybus_cleanup_chrdev(endpoint, endpoint->dev); in xillybus_endpoint_remove()
1979 return -ENOMEM; in xillybus_init()