1 /*
2  * IEEE 1394 for Linux
3  *
4  * kernel ISO transmission/reception
5  *
6  * Copyright (C) 2002 Maas Digital LLC
7  *
8  * This code is licensed under the GPL.  See the file COPYING in the root
9  * directory of the kernel sources for details.
10  */
11 
12 #ifndef IEEE1394_ISO_H
13 #define IEEE1394_ISO_H
14 
15 #include "hosts.h"
16 #include "dma.h"
17 
18 /* high-level ISO interface */
19 
20 /* This API sends and receives isochronous packets on a large,
21    virtually-contiguous kernel memory buffer. The buffer may be mapped
22    into a user-space process for zero-copy transmission and reception.
23 
24    There are no explicit boundaries between packets in the buffer. A
25    packet may be transmitted or received at any location. However,
26    low-level drivers may impose certain restrictions on alignment or
27    size of packets. (e.g. in OHCI no packet may cross a page boundary,
28    and packets should be quadlet-aligned)
29 */
30 
31 /* Packet descriptor - the API maintains a ring buffer of these packet
32    descriptors in kernel memory (hpsb_iso.infos[]).  */
33 
34 struct hpsb_iso_packet_info {
35 	/* offset of data payload relative to the first byte of the buffer */
36 	__u32 offset;
37 
38 	/* length of the data payload, in bytes (not including the isochronous header) */
39 	__u16 len;
40 
41 	/* (recv only) the cycle number (mod 8000) on which the packet was received */
42 	__u16 cycle;
43 
44 	/* (recv only) channel on which the packet was received */
45 	__u8 channel;
46 
47 	/* 2-bit 'tag' and 4-bit 'sy' fields of the isochronous header */
48 	__u8 tag;
49 	__u8 sy;
50 };
51 
52 enum hpsb_iso_type { HPSB_ISO_RECV = 0, HPSB_ISO_XMIT = 1 };
53 
54 struct hpsb_iso {
55 	enum hpsb_iso_type type;
56 
57 	/* pointer to low-level driver and its private data */
58 	struct hpsb_host *host;
59 	void *hostdata;
60 
61 	/* a function to be called (from interrupt context) after
62            outgoing packets have been sent, or incoming packets have
63            arrived */
64 	void (*callback)(struct hpsb_iso*);
65 
66 	/* wait for buffer space */
67 	wait_queue_head_t waitq;
68 
69 	int speed; /* IEEE1394_SPEED_100, 200, or 400 */
70 	int channel; /* -1 if multichannel */
71 
72 	/* greatest # of packets between interrupts - controls
73 	   the maximum latency of the buffer */
74 	int irq_interval;
75 
76 	/* the buffer for packet data payloads */
77 	struct dma_region data_buf;
78 
79 	/* size of data_buf, in bytes (always a multiple of PAGE_SIZE) */
80 	unsigned int buf_size;
81 
82 	/* # of packets in the ringbuffer */
83 	unsigned int buf_packets;
84 
85 	/* protects packet cursors */
86 	spinlock_t lock;
87 
88 	/* the index of the next packet that will be produced
89 	   or consumed by the user */
90 	int first_packet;
91 
92 	/* the index of the next packet that will be transmitted
93 	   or received by the 1394 hardware */
94 	int pkt_dma;
95 
96 	/* how many packets, starting at first_packet:
97 	   (transmit) are ready to be filled with data
98 	   (receive)  contain received data */
99 	int n_ready_packets;
100 
101 	/* how many times the buffer has overflowed or underflowed */
102 	atomic_t overflows;
103 
104 	/* private flags to track initialization progress */
105 #define HPSB_ISO_DRIVER_INIT     (1<<0)
106 #define HPSB_ISO_DRIVER_STARTED  (1<<1)
107 	unsigned int flags;
108 
109 	/* # of packets left to prebuffer (xmit only) */
110 	int prebuffer;
111 
112 	/* starting cycle for DMA (xmit only) */
113 	int start_cycle;
114 
115 	/* cycle at which next packet will be transmitted,
116 	   -1 if not known */
117 	int xmit_cycle;
118 
119 	/* ringbuffer of packet descriptors in regular kernel memory
120 	 * XXX Keep this last, since we use over-allocated memory from
121 	 * this entry to fill this field. */
122 	struct hpsb_iso_packet_info *infos;
123 };
124 
125 /* functions available to high-level drivers (e.g. raw1394) */
126 
127 /* allocate the buffer and DMA context */
128 
129 struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
130 				    unsigned int data_buf_size,
131 				    unsigned int buf_packets,
132 				    int channel,
133 				    int speed,
134 				    int irq_interval,
135 				    void (*callback)(struct hpsb_iso*));
136 
137 /* note: if channel = -1, multi-channel receive is enabled */
138 struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
139 				    unsigned int data_buf_size,
140 				    unsigned int buf_packets,
141 				    int channel,
142 				    int irq_interval,
143 				    void (*callback)(struct hpsb_iso*));
144 
145 /* multi-channel only */
146 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel);
147 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel);
148 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask);
149 
150 /* start/stop DMA */
151 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int start_on_cycle, int prebuffer);
152 int hpsb_iso_recv_start(struct hpsb_iso *iso, int start_on_cycle, int tag_mask, int sync);
153 void hpsb_iso_stop(struct hpsb_iso *iso);
154 
155 /* deallocate buffer and DMA context */
156 void hpsb_iso_shutdown(struct hpsb_iso *iso);
157 
158 /* queue a packet for transmission. 'offset' is relative to the beginning of the
159    DMA buffer, where the packet's data payload should already have been placed */
160 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy);
161 
162 /* wait until all queued packets have been transmitted to the bus */
163 int hpsb_iso_xmit_sync(struct hpsb_iso *iso);
164 
165 /* N packets have been read out of the buffer, re-use the buffer space */
166 int  hpsb_iso_recv_release_packets(struct hpsb_iso *recv, unsigned int n_packets);
167 
168 /* check for arrival of new packets immediately (even if irq_interval
169    has not yet been reached) */
170 int hpsb_iso_recv_flush(struct hpsb_iso *iso);
171 
172 /* returns # of packets ready to send or receive */
173 int hpsb_iso_n_ready(struct hpsb_iso *iso);
174 
175 /* the following are callbacks available to low-level drivers */
176 
177 /* call after a packet has been transmitted to the bus (interrupt context is OK)
178    'cycle' is the _exact_ cycle the packet was sent on
179    'error' should be non-zero if some sort of error occurred when sending the packet
180 */
181 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error);
182 
183 /* call after a packet has been received (interrupt context OK) */
184 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
185 			      u16 cycle, u8 channel, u8 tag, u8 sy);
186 
187 /* call to wake waiting processes after buffer space has opened up. */
188 void hpsb_iso_wake(struct hpsb_iso *iso);
189 
190 #endif /* IEEE1394_ISO_H */
191