sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hid / intel-ish-hid / ishtp / client-buffers.c
blobb9b917d2d50db3fedaa17ce8f6fcb3a05e2f38c3
1 /*
2 * ISHTP Ring Buffers
4 * Copyright (c) 2003-2016, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
17 #include <linux/slab.h>
18 #include "client.h"
20 /**
21 * ishtp_cl_alloc_rx_ring() - Allocate RX ring buffers
22 * @cl: client device instance
24 * Allocate and initialize RX ring buffers
26 * Return: 0 on success else -ENOMEM
28 int ishtp_cl_alloc_rx_ring(struct ishtp_cl *cl)
30 size_t len = cl->device->fw_client->props.max_msg_length;
31 int j;
32 struct ishtp_cl_rb *rb;
33 int ret = 0;
34 unsigned long flags;
36 for (j = 0; j < cl->rx_ring_size; ++j) {
37 rb = ishtp_io_rb_init(cl);
38 if (!rb) {
39 ret = -ENOMEM;
40 goto out;
42 ret = ishtp_io_rb_alloc_buf(rb, len);
43 if (ret)
44 goto out;
45 spin_lock_irqsave(&cl->free_list_spinlock, flags);
46 list_add_tail(&rb->list, &cl->free_rb_list.list);
47 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
50 return 0;
52 out:
53 dev_err(&cl->device->dev, "error in allocating Rx buffers\n");
54 ishtp_cl_free_rx_ring(cl);
55 return ret;
58 /**
59 * ishtp_cl_alloc_tx_ring() - Allocate TX ring buffers
60 * @cl: client device instance
62 * Allocate and initialize TX ring buffers
64 * Return: 0 on success else -ENOMEM
66 int ishtp_cl_alloc_tx_ring(struct ishtp_cl *cl)
68 size_t len = cl->device->fw_client->props.max_msg_length;
69 int j;
70 unsigned long flags;
72 /* Allocate pool to free Tx bufs */
73 for (j = 0; j < cl->tx_ring_size; ++j) {
74 struct ishtp_cl_tx_ring *tx_buf;
76 tx_buf = kzalloc(sizeof(struct ishtp_cl_tx_ring), GFP_KERNEL);
77 if (!tx_buf)
78 goto out;
80 tx_buf->send_buf.data = kmalloc(len, GFP_KERNEL);
81 if (!tx_buf->send_buf.data) {
82 kfree(tx_buf);
83 goto out;
86 spin_lock_irqsave(&cl->tx_free_list_spinlock, flags);
87 list_add_tail(&tx_buf->list, &cl->tx_free_list.list);
88 spin_unlock_irqrestore(&cl->tx_free_list_spinlock, flags);
90 return 0;
91 out:
92 dev_err(&cl->device->dev, "error in allocating Tx pool\n");
93 ishtp_cl_free_rx_ring(cl);
94 return -ENOMEM;
97 /**
98 * ishtp_cl_free_rx_ring() - Free RX ring buffers
99 * @cl: client device instance
101 * Free RX ring buffers
103 void ishtp_cl_free_rx_ring(struct ishtp_cl *cl)
105 struct ishtp_cl_rb *rb;
106 unsigned long flags;
108 /* release allocated memory - pass over free_rb_list */
109 spin_lock_irqsave(&cl->free_list_spinlock, flags);
110 while (!list_empty(&cl->free_rb_list.list)) {
111 rb = list_entry(cl->free_rb_list.list.next, struct ishtp_cl_rb,
112 list);
113 list_del(&rb->list);
114 kfree(rb->buffer.data);
115 kfree(rb);
117 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
118 /* release allocated memory - pass over in_process_list */
119 spin_lock_irqsave(&cl->in_process_spinlock, flags);
120 while (!list_empty(&cl->in_process_list.list)) {
121 rb = list_entry(cl->in_process_list.list.next,
122 struct ishtp_cl_rb, list);
123 list_del(&rb->list);
124 kfree(rb->buffer.data);
125 kfree(rb);
127 spin_unlock_irqrestore(&cl->in_process_spinlock, flags);
131 * ishtp_cl_free_tx_ring() - Free TX ring buffers
132 * @cl: client device instance
134 * Free TX ring buffers
136 void ishtp_cl_free_tx_ring(struct ishtp_cl *cl)
138 struct ishtp_cl_tx_ring *tx_buf;
139 unsigned long flags;
141 spin_lock_irqsave(&cl->tx_free_list_spinlock, flags);
142 /* release allocated memory - pass over tx_free_list */
143 while (!list_empty(&cl->tx_free_list.list)) {
144 tx_buf = list_entry(cl->tx_free_list.list.next,
145 struct ishtp_cl_tx_ring, list);
146 list_del(&tx_buf->list);
147 kfree(tx_buf->send_buf.data);
148 kfree(tx_buf);
150 spin_unlock_irqrestore(&cl->tx_free_list_spinlock, flags);
152 spin_lock_irqsave(&cl->tx_list_spinlock, flags);
153 /* release allocated memory - pass over tx_list */
154 while (!list_empty(&cl->tx_list.list)) {
155 tx_buf = list_entry(cl->tx_list.list.next,
156 struct ishtp_cl_tx_ring, list);
157 list_del(&tx_buf->list);
158 kfree(tx_buf->send_buf.data);
159 kfree(tx_buf);
161 spin_unlock_irqrestore(&cl->tx_list_spinlock, flags);
165 * ishtp_io_rb_free() - Free IO request block
166 * @rb: IO request block
168 * Free io request block memory
170 void ishtp_io_rb_free(struct ishtp_cl_rb *rb)
172 if (rb == NULL)
173 return;
175 kfree(rb->buffer.data);
176 kfree(rb);
180 * ishtp_io_rb_init() - Allocate and init IO request block
181 * @cl: client device instance
183 * Allocate and initialize request block
185 * Return: Allocted IO request block pointer
187 struct ishtp_cl_rb *ishtp_io_rb_init(struct ishtp_cl *cl)
189 struct ishtp_cl_rb *rb;
191 rb = kzalloc(sizeof(struct ishtp_cl_rb), GFP_KERNEL);
192 if (!rb)
193 return NULL;
195 INIT_LIST_HEAD(&rb->list);
196 rb->cl = cl;
197 rb->buf_idx = 0;
198 return rb;
202 * ishtp_io_rb_alloc_buf() - Allocate and init response buffer
203 * @rb: IO request block
204 * @length: length of response buffer
206 * Allocate respose buffer
208 * Return: 0 on success else -ENOMEM
210 int ishtp_io_rb_alloc_buf(struct ishtp_cl_rb *rb, size_t length)
212 if (!rb)
213 return -EINVAL;
215 if (length == 0)
216 return 0;
218 rb->buffer.data = kmalloc(length, GFP_KERNEL);
219 if (!rb->buffer.data)
220 return -ENOMEM;
222 rb->buffer.size = length;
223 return 0;
227 * ishtp_cl_io_rb_recycle() - Recycle IO request blocks
228 * @rb: IO request block
230 * Re-append rb to its client's free list and send flow control if needed
232 * Return: 0 on success else -EFAULT
234 int ishtp_cl_io_rb_recycle(struct ishtp_cl_rb *rb)
236 struct ishtp_cl *cl;
237 int rets = 0;
238 unsigned long flags;
240 if (!rb || !rb->cl)
241 return -EFAULT;
243 cl = rb->cl;
244 spin_lock_irqsave(&cl->free_list_spinlock, flags);
245 list_add_tail(&rb->list, &cl->free_rb_list.list);
246 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
249 * If we returned the first buffer to empty 'free' list,
250 * send flow control
252 if (!cl->out_flow_ctrl_creds)
253 rets = ishtp_cl_read_start(cl);
255 return rets;
257 EXPORT_SYMBOL(ishtp_cl_io_rb_recycle);