source: mainline/uspace/drv/bus/usb/uhci/pci.c@ d46b13d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d46b13d was 45a9cf4, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

uhci,ohci: Fix memory leak.

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[ea991e84]1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
[79ae36dd]28
[ea991e84]29/**
[17ceb72]30 * @addtogroup drvusbuhcihc
[ea991e84]31 * @{
32 */
33/**
34 * @file
35 * PCI related functions needed by the UHCI driver.
36 */
[79ae36dd]37
[ea991e84]38#include <errno.h>
39#include <assert.h>
40#include <devman.h>
41#include <device/hw_res.h>
42
[2d33eb5]43#include <usb/debug.h>
[ab414a6]44#include <pci_dev_iface.h>
[2d33eb5]45
[c56dbe0]46#include "pci.h"
47
[ea993d18]48/** Get I/O address of registers and IRQ for given device.
[ea991e84]49 *
50 * @param[in] dev Device asking for the addresses.
51 * @param[out] io_reg_address Base address of the I/O range.
52 * @param[out] io_reg_size Size of the I/O range.
53 * @param[out] irq_no IRQ assigned to the device.
54 * @return Error code.
55 */
[e247d83]56int pci_get_my_registers(const ddf_dev_t *dev,
[ea993d18]57 uintptr_t *io_reg_address, size_t *io_reg_size, int *irq_no)
[ea991e84]58{
[e247d83]59 assert(dev);
60 assert(io_reg_address);
61 assert(io_reg_size);
62 assert(irq_no);
[5d915b7]63
[79ae36dd]64 async_sess_t *parent_sess =
65 devman_parent_device_connect(EXCHANGE_SERIALIZE, dev->handle,
66 IPC_FLAG_BLOCKING);
67 if (!parent_sess)
68 return ENOMEM;
[5d915b7]69
[ea991e84]70 hw_resource_list_t hw_resources;
[45a9cf4]71 const int rc = hw_res_get_resource_list(parent_sess, &hw_resources);
72 async_hangup(parent_sess);
[ea991e84]73 if (rc != EOK) {
[e247d83]74 return rc;
[ea991e84]75 }
[5d915b7]76
[ea991e84]77 uintptr_t io_address = 0;
78 size_t io_size = 0;
79 bool io_found = false;
[45a9cf4]80
[ea991e84]81 int irq = 0;
82 bool irq_found = false;
[45a9cf4]83
[ea991e84]84 size_t i;
85 for (i = 0; i < hw_resources.count; i++) {
[e247d83]86 const hw_resource_t *res = &hw_resources.resources[i];
[79ae36dd]87 switch (res->type) {
[4b4e163]88 case INTERRUPT:
89 irq = res->res.interrupt.irq;
90 irq_found = true;
91 usb_log_debug2("Found interrupt: %d.\n", irq);
92 break;
93 case IO_RANGE:
94 io_address = res->res.io_range.address;
95 io_size = res->res.io_range.size;
[4125b7d]96 usb_log_debug2("Found io: %" PRIx64" %zu.\n",
[4b4e163]97 res->res.io_range.address, res->res.io_range.size);
98 io_found = true;
[ea993d18]99 break;
[4b4e163]100 default:
101 break;
[ea991e84]102 }
103 }
[45a9cf4]104 free(hw_resources.resources);
[5d915b7]105
[e247d83]106 if (!io_found || !irq_found)
107 return ENOENT;
[5d915b7]108
[2d33eb5]109 *io_reg_address = io_address;
110 *io_reg_size = io_size;
111 *irq_no = irq;
[5d915b7]112
[e247d83]113 return EOK;
[ea991e84]114}
[5d915b7]115/*----------------------------------------------------------------------------*/
[17ceb72]116/** Call the PCI driver with a request to enable interrupts
[a7e2f0d]117 *
118 * @param[in] device Device asking for interrupts
119 * @return Error code.
120 */
[e247d83]121int pci_enable_interrupts(const ddf_dev_t *device)
[fb78ae72]122{
[79ae36dd]123 async_sess_t *parent_sess =
124 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
125 IPC_FLAG_BLOCKING);
126 if (!parent_sess)
127 return ENOMEM;
[5d915b7]128
[79ae36dd]129 const bool enabled = hw_res_enable_interrupt(parent_sess);
130 async_hangup(parent_sess);
[5d915b7]131
[fb78ae72]132 return enabled ? EOK : EIO;
133}
[5d915b7]134/*----------------------------------------------------------------------------*/
[17ceb72]135/** Call the PCI driver with a request to clear legacy support register
[a7e2f0d]136 *
137 * @param[in] device Device asking to disable interrupts
138 * @return Error code.
139 */
[e247d83]140int pci_disable_legacy(const ddf_dev_t *device)
[ab414a6]141{
142 assert(device);
[3afb758]143
[79ae36dd]144 async_sess_t *parent_sess =
145 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
146 IPC_FLAG_BLOCKING);
147 if (!parent_sess)
148 return ENOMEM;
[3afb758]149
[ea993d18]150 /* See UHCI design guide for these values p.45,
[b375bb8]151 * write all WC bits in USB legacy register */
[e247d83]152 const sysarg_t address = 0xc0;
153 const sysarg_t value = 0xaf00;
[3afb758]154
[79ae36dd]155 async_exch_t *exch = async_exchange_begin(parent_sess);
[3afb758]156
[79ae36dd]157 const int rc = async_req_3_0(exch, DEV_IFACE_ID(PCI_DEV_IFACE),
[ab414a6]158 IPC_M_CONFIG_SPACE_WRITE_16, address, value);
[3afb758]159
[79ae36dd]160 async_exchange_end(exch);
161 async_hangup(parent_sess);
[3afb758]162
[4b4e163]163 return rc;
[ab414a6]164}
[ea991e84]165
[c56dbe0]166/**
167 * @}
168 */
Note: See TracBrowser for help on using the repository browser.