47 | | /* Provide a mechanism for allocating iso channels to MOTU interfaces. |
---|
48 | | * |
---|
49 | | * FIXME: This is overly simplistic at present since it assumes there are no |
---|
50 | | * other users of iso channels on the firewire bus except MOTU interfaces. |
---|
51 | | * It does however allow more than one MOTU interface to exist on the same |
---|
52 | | * bus. For now the first MOTU discovered will be allocated iso channels 0 |
---|
53 | | * (send) and 1 (receive) which mirrors what the official driver appears to |
---|
54 | | * do. Ultimately we need code to query the IRM for iso channels. |
---|
| 49 | /* Provide a mechanism for allocating iso channels and bandwidth to MOTU |
---|
| 50 | * interfaces. |
---|
61 | | * Returns the next available channel for ISO receive. If there are no more |
---|
62 | | * available -1 is returned. Currently the odd channels (starting from 1) |
---|
63 | | * are used for iso receive. No provision is made to reuse previously |
---|
64 | | * allocated channels in the event that the applicable interface has been |
---|
65 | | * removed - this will change in future to use the IRM. |
---|
| 55 | * Allocates an iso channel for use by the interface in a similar way to |
---|
| 56 | * libiec61883. Returns -1 on error (due to there being no free channels) |
---|
| 57 | * or an allocated channel number. |
---|
| 58 | * FIXME: As in libiec61883, channel 63 is not requested; this is either a |
---|
| 59 | * bug or it's omitted since that's the channel preferred by video devices. |
---|
81 | | if (next_iso_send_channel_num < 64) { |
---|
82 | | next_iso_send_channel_num+=2; |
---|
83 | | return next_iso_send_channel_num-2; |
---|
84 | | } |
---|
85 | | return -1; |
---|
| 75 | if (channel < 0) |
---|
| 76 | return 0; |
---|
| 77 | if (raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE)!=0) |
---|
| 78 | return -1; |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | static signed int get_iso_bandwidth_avail(raw1394handle_t handle) { |
---|
| 83 | /* |
---|
| 84 | * Returns the current value of the `bandwidth available' register on |
---|
| 85 | * the IRM, or -1 on error. |
---|
| 86 | */ |
---|
| 87 | quadlet_t buffer; |
---|
| 88 | signed int result = raw1394_read (handle, raw1394_get_irm_id (handle), |
---|
| 89 | CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE, |
---|
| 90 | sizeof (quadlet_t), &buffer); |
---|
| 91 | |
---|
| 92 | if (result < 0) |
---|
| 93 | return -1; |
---|
| 94 | return ntohl(buffer); |
---|
| 125 | // Free ieee1394 bus resources if they have been allocated |
---|
| 126 | if (m_1394Service != NULL) { |
---|
| 127 | raw1394handle_t handle = m_1394Service->getHandle(); |
---|
| 128 | if (m_bandwidth >= 0) |
---|
| 129 | if (raw1394_bandwidth_modify(handle, m_bandwidth, RAW1394_MODIFY_FREE) < 0) |
---|
| 130 | debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free bandwidth of %d\n", m_bandwidth); |
---|
| 131 | if (m_iso_recv_channel >= 0) |
---|
| 132 | if (raw1394_channel_modify(handle, m_iso_recv_channel, RAW1394_MODIFY_FREE) < 0) |
---|
| 133 | debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel); |
---|
| 134 | if (m_iso_send_channel >= 0) |
---|
| 135 | if (raw1394_channel_modify(handle, m_iso_send_channel, RAW1394_MODIFY_FREE) < 0) |
---|
| 136 | debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel); |
---|
| 137 | } |
---|
| 278 | |
---|
| 279 | // Assign iso channels if not already done |
---|
| 280 | if (m_iso_recv_channel < 0) |
---|
| 281 | m_iso_recv_channel = allocate_iso_channel(handle); |
---|
| 282 | if (m_iso_send_channel < 0) |
---|
| 283 | m_iso_send_channel = allocate_iso_channel(handle); |
---|
| 284 | |
---|
| 285 | debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n", |
---|
| 286 | m_iso_recv_channel, m_iso_send_channel); |
---|
| 287 | |
---|
| 288 | if (m_iso_recv_channel<0 || m_iso_send_channel<0) { |
---|
| 289 | debugFatal("Could not allocate iso channels!\n"); |
---|
| 290 | return false; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | // Allocate bandwidth if not previously done. |
---|
| 294 | // FIXME: The bandwidth allocation calculation can probably be |
---|
| 295 | // refined somewhat since this is currently based on a rudimentary |
---|
| 296 | // understanding of the iso protocol. |
---|
| 297 | // Currently we assume the following. |
---|
| 298 | // * Ack/iso gap = 0.05 us |
---|
| 299 | // * DATA_PREFIX = 0.16 us |
---|
| 300 | // * DATA_END = 0.26 us |
---|
| 301 | // These numbers are the worst-case figures given in the ieee1394 |
---|
| 302 | // standard. This gives approximately 0.5 us of overheads per |
---|
| 303 | // packet - around 25 bandwidth allocation units (from the ieee1394 |
---|
| 304 | // standard 1 bandwidth allocation unit is 125/6144 us). We further |
---|
| 305 | // assume the MOTU is running at S400 (which it should be) so one |
---|
| 306 | // allocation unit is equivalent to 1 transmitted byte; thus the |
---|
| 307 | // bandwidth allocation required for the packets themselves is just |
---|
| 308 | // the size of the packet. We allocate based on the maximum packet |
---|
| 309 | // size (1160 bytes at 192 kHz) so the sampling frequency can be |
---|
| 310 | // changed dynamically if this ends up being useful in future. |
---|
| 311 | m_bandwidth = 25 + 1160; |
---|
| 312 | debugOutput(DEBUG_LEVEL_VERBOSE, "Available bandwidth: %d\n", |
---|
| 313 | get_iso_bandwidth_avail(handle)); |
---|
| 314 | if (raw1394_bandwidth_modify(handle, m_bandwidth, RAW1394_MODIFY_ALLOC) < 0) { |
---|
| 315 | debugFatal("Could not allocate bandwidth of %d\n", m_bandwidth); |
---|
| 316 | m_bandwidth = -1; |
---|
| 317 | return false; |
---|
| 318 | } |
---|
| 319 | debugOutput(DEBUG_LEVEL_VERBOSE, |
---|
| 320 | "allocated bandwidth of %d for MOTU device\n", m_bandwidth); |
---|
| 321 | debugOutput(DEBUG_LEVEL_VERBOSE, |
---|
| 322 | "remaining bandwidth: %d\n", get_iso_bandwidth_avail(handle)); |
---|