Staging: remove at76_usb wireless driver.
[linux-2.6/linux-2.6-openrd.git] / drivers / staging / heci / heci_interface.c
blob03e1df1a88a08c16a3b8b7ab26c952e9d9a057f9
1 /*
2 * Part of Intel(R) Manageability Engine Interface Linux driver
4 * Copyright (c) 2003 - 2008 Intel Corp.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
18 * 3. Neither the names of the above-listed copyright holders nor the names
19 * of any contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * Alternatively, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2 as published by the Free
24 * Software Foundation.
26 * NO WARRANTY
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
42 #include "heci.h"
43 #include "heci_interface.h"
46 /**
47 * heci_set_csr_register - write H_CSR register to the heci device,
48 * and ignore the H_IS bit for it is write-one-to-zero.
50 * @dev: device object for our driver
52 void heci_set_csr_register(struct iamt_heci_device *dev)
54 if ((dev->host_hw_state & H_IS) == H_IS)
55 dev->host_hw_state &= ~H_IS;
56 write_heci_register(dev, H_CSR, dev->host_hw_state);
57 dev->host_hw_state = read_heci_register(dev, H_CSR);
60 /**
61 * heci_csr_enable_interrupts - enable heci device interrupts
63 * @dev: device object for our driver
65 void heci_csr_enable_interrupts(struct iamt_heci_device *dev)
67 dev->host_hw_state |= H_IE;
68 heci_set_csr_register(dev);
71 /**
72 * heci_csr_disable_interrupts - disable heci device interrupts
74 * @dev: device object for our driver
76 void heci_csr_disable_interrupts(struct iamt_heci_device *dev)
78 dev->host_hw_state &= ~H_IE;
79 heci_set_csr_register(dev);
82 /**
83 * heci_csr_clear_his - clear H_IS bit in H_CSR
85 * @dev: device object for our driver
87 void heci_csr_clear_his(struct iamt_heci_device *dev)
89 write_heci_register(dev, H_CSR, dev->host_hw_state);
90 dev->host_hw_state = read_heci_register(dev, H_CSR);
93 /**
94 * _host_get_filled_slots - get number of device filled buffer slots
96 * @device: the device structure
98 * returns numer of filled slots
100 static unsigned char _host_get_filled_slots(const struct iamt_heci_device *dev)
102 char read_ptr, write_ptr;
104 read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
105 write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
107 return (unsigned char) (write_ptr - read_ptr);
111 * host_buffer_is_empty - check if host buffer is empty.
113 * @dev: device object for our driver
115 * returns 1 if empty, 0 - otherwise.
117 int host_buffer_is_empty(struct iamt_heci_device *dev)
119 unsigned char filled_slots;
121 dev->host_hw_state = read_heci_register(dev, H_CSR);
122 filled_slots = _host_get_filled_slots(dev);
124 if (filled_slots > 0)
125 return 0;
127 return 1;
131 * count_empty_write_slots - count write empty slots.
133 * @dev: device object for our driver
135 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
137 __s32 count_empty_write_slots(const struct iamt_heci_device *dev)
139 unsigned char buffer_depth, filled_slots, empty_slots;
141 buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
142 filled_slots = _host_get_filled_slots(dev);
143 empty_slots = buffer_depth - filled_slots;
145 if (filled_slots > buffer_depth) {
146 /* overflow */
147 return -ESLOTS_OVERFLOW;
150 return (__s32) empty_slots;
154 * heci_write_message - write a message to heci device.
156 * @dev: device object for our driver
157 * @heci_hdr: header of message
158 * @write_buffer: message buffer will be write
159 * @write_length: message size will be write
161 * returns 1 if success, 0 - otherwise.
163 int heci_write_message(struct iamt_heci_device *dev,
164 struct heci_msg_hdr *header,
165 unsigned char *write_buffer,
166 unsigned long write_length)
168 __u32 temp_msg = 0;
169 unsigned long bytes_written = 0;
170 unsigned char buffer_depth, filled_slots, empty_slots;
171 unsigned long dw_to_write;
173 dev->host_hw_state = read_heci_register(dev, H_CSR);
174 DBG("host_hw_state = 0x%08x.\n", dev->host_hw_state);
175 DBG("heci_write_message header=%08x.\n", *((__u32 *) header));
176 buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
177 filled_slots = _host_get_filled_slots(dev);
178 empty_slots = buffer_depth - filled_slots;
179 DBG("filled = %hu, empty = %hu.\n", filled_slots, empty_slots);
181 dw_to_write = ((write_length + 3) / 4);
183 if (dw_to_write > empty_slots)
184 return 0;
186 write_heci_register(dev, H_CB_WW, *((__u32 *) header));
188 while (write_length >= 4) {
189 write_heci_register(dev, H_CB_WW,
190 *(__u32 *) (write_buffer + bytes_written));
191 bytes_written += 4;
192 write_length -= 4;
195 if (write_length > 0) {
196 memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
197 write_heci_register(dev, H_CB_WW, temp_msg);
200 dev->host_hw_state |= H_IG;
201 heci_set_csr_register(dev);
202 dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
203 if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
204 return 0;
206 dev->write_hang = 0;
207 return 1;
211 * count_full_read_slots - count read full slots.
213 * @dev: device object for our driver
215 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
217 __s32 count_full_read_slots(struct iamt_heci_device *dev)
219 char read_ptr, write_ptr;
220 unsigned char buffer_depth, filled_slots;
222 dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
223 buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
224 read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
225 write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
226 filled_slots = (unsigned char) (write_ptr - read_ptr);
228 if (filled_slots > buffer_depth) {
229 /* overflow */
230 return -ESLOTS_OVERFLOW;
233 DBG("filled_slots =%08x \n", filled_slots);
234 return (__s32) filled_slots;
238 * heci_read_slots - read a message from heci device.
240 * @dev: device object for our driver
241 * @buffer: message buffer will be write
242 * @buffer_length: message size will be read
244 void heci_read_slots(struct iamt_heci_device *dev,
245 unsigned char *buffer, unsigned long buffer_length)
247 __u32 i = 0;
248 unsigned char temp_buf[sizeof(__u32)];
250 while (buffer_length >= sizeof(__u32)) {
251 ((__u32 *) buffer)[i] = read_heci_register(dev, ME_CB_RW);
252 DBG("buffer[%d]= %d\n", i, ((__u32 *) buffer)[i]);
253 i++;
254 buffer_length -= sizeof(__u32);
257 if (buffer_length > 0) {
258 *((__u32 *) &temp_buf) = read_heci_register(dev, ME_CB_RW);
259 memcpy(&buffer[i * 4], temp_buf, buffer_length);
262 dev->host_hw_state |= H_IG;
263 heci_set_csr_register(dev);
267 * flow_ctrl_creds - check flow_control credentials.
269 * @dev: device object for our driver
270 * @file_ext: private data of the file object
272 * returns 1 if flow_ctrl_creds >0, 0 - otherwise.
274 int flow_ctrl_creds(struct iamt_heci_device *dev,
275 struct heci_file_private *file_ext)
277 __u8 i;
279 if (!dev->num_heci_me_clients)
280 return 0;
282 if (file_ext == NULL)
283 return 0;
285 if (file_ext->flow_ctrl_creds > 0)
286 return 1;
288 for (i = 0; i < dev->num_heci_me_clients; i++) {
289 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
290 if (dev->me_clients[i].flow_ctrl_creds > 0) {
291 BUG_ON(dev->me_clients[i].props.single_recv_buf
292 == 0);
293 return 1;
295 return 0;
298 BUG();
299 return 0;
303 * flow_ctrl_reduce - reduce flow_control.
305 * @dev: device object for our driver
306 * @file_ext: private data of the file object
308 void flow_ctrl_reduce(struct iamt_heci_device *dev,
309 struct heci_file_private *file_ext)
311 __u8 i;
313 if (!dev->num_heci_me_clients)
314 return;
316 for (i = 0; i < dev->num_heci_me_clients; i++) {
317 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
318 if (dev->me_clients[i].props.single_recv_buf != 0) {
319 BUG_ON(dev->me_clients[i].flow_ctrl_creds <= 0);
320 dev->me_clients[i].flow_ctrl_creds--;
321 } else {
322 BUG_ON(file_ext->flow_ctrl_creds <= 0);
323 file_ext->flow_ctrl_creds--;
325 return;
328 BUG();
332 * heci_send_flow_control - send flow control to fw.
334 * @dev: device object for our driver
335 * @file_ext: private data of the file object
337 * returns 1 if success, 0 - otherwise.
339 int heci_send_flow_control(struct iamt_heci_device *dev,
340 struct heci_file_private *file_ext)
342 struct heci_msg_hdr *heci_hdr;
343 struct hbm_flow_control *heci_flow_control;
345 heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
346 heci_hdr->host_addr = 0;
347 heci_hdr->me_addr = 0;
348 heci_hdr->length = sizeof(struct hbm_flow_control);
349 heci_hdr->msg_complete = 1;
350 heci_hdr->reserved = 0;
352 heci_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
353 memset(heci_flow_control, 0, sizeof(heci_flow_control));
354 heci_flow_control->host_addr = file_ext->host_client_id;
355 heci_flow_control->me_addr = file_ext->me_client_id;
356 heci_flow_control->cmd.cmd = HECI_FLOW_CONTROL_CMD;
357 memset(heci_flow_control->reserved, 0,
358 sizeof(heci_flow_control->reserved));
359 DBG("sending flow control host client = %d, me client = %d\n",
360 file_ext->host_client_id, file_ext->me_client_id);
361 if (!heci_write_message(dev, heci_hdr,
362 (unsigned char *) heci_flow_control,
363 sizeof(struct hbm_flow_control)))
364 return 0;
366 return 1;
371 * other_client_is_connecting - check if other
372 * client with the same client id is connected.
374 * @dev: device object for our driver
375 * @file_ext: private data of the file object
377 * returns 1 if other client is connected, 0 - otherwise.
379 int other_client_is_connecting(struct iamt_heci_device *dev,
380 struct heci_file_private *file_ext)
382 struct heci_file_private *file_pos = NULL;
383 struct heci_file_private *file_next = NULL;
385 list_for_each_entry_safe(file_pos, file_next, &dev->file_list, link) {
386 if ((file_pos->state == HECI_FILE_CONNECTING)
387 && (file_pos != file_ext)
388 && file_ext->me_client_id == file_pos->me_client_id)
389 return 1;
392 return 0;
396 * heci_send_wd - send watch dog message to fw.
398 * @dev: device object for our driver
400 * returns 1 if success, 0 - otherwise.
402 int heci_send_wd(struct iamt_heci_device *dev)
404 struct heci_msg_hdr *heci_hdr;
406 heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
407 heci_hdr->host_addr = dev->wd_file_ext.host_client_id;
408 heci_hdr->me_addr = dev->wd_file_ext.me_client_id;
409 heci_hdr->msg_complete = 1;
410 heci_hdr->reserved = 0;
412 if (!memcmp(dev->wd_data, heci_start_wd_params,
413 HECI_WD_PARAMS_SIZE)) {
414 heci_hdr->length = HECI_START_WD_DATA_SIZE;
415 } else {
416 BUG_ON(memcmp(dev->wd_data, heci_stop_wd_params,
417 HECI_WD_PARAMS_SIZE));
418 heci_hdr->length = HECI_WD_PARAMS_SIZE;
421 if (!heci_write_message(dev, heci_hdr, dev->wd_data, heci_hdr->length))
422 return 0;
424 return 1;
428 * heci_disconnect - send disconnect message to fw.
430 * @dev: device object for our driver
431 * @file_ext: private data of the file object
433 * returns 1 if success, 0 - otherwise.
435 int heci_disconnect(struct iamt_heci_device *dev,
436 struct heci_file_private *file_ext)
438 struct heci_msg_hdr *heci_hdr;
439 struct hbm_client_disconnect_request *heci_cli_disconnect;
441 heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
442 heci_hdr->host_addr = 0;
443 heci_hdr->me_addr = 0;
444 heci_hdr->length = sizeof(struct hbm_client_disconnect_request);
445 heci_hdr->msg_complete = 1;
446 heci_hdr->reserved = 0;
448 heci_cli_disconnect =
449 (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
450 memset(heci_cli_disconnect, 0, sizeof(heci_cli_disconnect));
451 heci_cli_disconnect->host_addr = file_ext->host_client_id;
452 heci_cli_disconnect->me_addr = file_ext->me_client_id;
453 heci_cli_disconnect->cmd.cmd = CLIENT_DISCONNECT_REQ_CMD;
454 heci_cli_disconnect->reserved[0] = 0;
456 if (!heci_write_message(dev, heci_hdr,
457 (unsigned char *) heci_cli_disconnect,
458 sizeof(struct hbm_client_disconnect_request)))
459 return 0;
461 return 1;
465 * heci_connect - send connect message to fw.
467 * @dev: device object for our driver
468 * @file_ext: private data of the file object
470 * returns 1 if success, 0 - otherwise.
472 int heci_connect(struct iamt_heci_device *dev,
473 struct heci_file_private *file_ext)
475 struct heci_msg_hdr *heci_hdr;
476 struct hbm_client_connect_request *heci_cli_connect;
478 heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
479 heci_hdr->host_addr = 0;
480 heci_hdr->me_addr = 0;
481 heci_hdr->length = sizeof(struct hbm_client_connect_request);
482 heci_hdr->msg_complete = 1;
483 heci_hdr->reserved = 0;
485 heci_cli_connect =
486 (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
487 heci_cli_connect->host_addr = file_ext->host_client_id;
488 heci_cli_connect->me_addr = file_ext->me_client_id;
489 heci_cli_connect->cmd.cmd = CLIENT_CONNECT_REQ_CMD;
490 heci_cli_connect->reserved = 0;
492 if (!heci_write_message(dev, heci_hdr,
493 (unsigned char *) heci_cli_connect,
494 sizeof(struct hbm_client_connect_request)))
495 return 0;
497 return 1;