/* Defaults to returning the channel with the desired capability and the lowest reference count when 'n' cannot be satisfied. Must be called under dma_list_mutex.  */
static struct dma_chan* nth_chan(enum dma_transaction_type cap, int n)
/* Defaults to returning the channel with the desired capability and the lowest reference count when 'n' cannot be satisfied. Must be called under dma_list_mutex.  */
static struct dma_chan* nth_chan(enum dma_transaction_type cap, int n)
{
	struct dma_device *device;
	struct dma_chan *chan;
	struct dma_chan *ret = NULL;
	struct dma_chan *min = NULL;
	list_for_each_entry(device, &dma_device_list, global_node) {
		if (!dma_has_cap(cap, device->cap_mask) ||
		    dma_has_cap(DMA_PRIVATE, device->cap_mask))
			continue;
		list_for_each_entry(chan, &device->channels, device_node) {
			if (!chan->client_count)
				continue;
			if (!min)
				min = chan;
			else if (chan->table_count < min->table_count)
				min = chan;
			if (n-- == 0) {
				ret = chan;
				break;
			}
		}
		if (ret)
			break;
	}
	if (!ret)
		ret = min;
	if (ret)
		ret->table_count++;
	return ret;
}