4 * Low level (host adapter) management.
6 * Copyright (C) 1999 Andreas E. Bombe
7 * Copyright (C) 1999 Emanuel Pirker
9 * This code is licensed under the GPL. See the file COPYING in the root
10 * directory of the kernel sources for details.
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/list.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
19 #include "ieee1394_types.h"
21 #include "ieee1394_core.h"
22 #include "highlevel.h"
24 LIST_HEAD(hpsb_hosts);
25 DECLARE_MUTEX(hpsb_hosts_lock);
27 static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
32 static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
37 static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
42 static struct hpsb_host_driver dummy_driver = {
43 .transmit_packet = dummy_transmit_packet,
44 .devctl = dummy_devctl,
45 .isoctl = dummy_isoctl
49 * hpsb_ref_host - increase reference count for host controller.
50 * @host: the host controller
52 * Increase the reference count for the specified host controller.
53 * When holding a reference to a host, the memory allocated for the
54 * host struct will not be freed and the host is guaranteed to be in a
55 * consistent state. The driver may be unloaded or the controller may
56 * be removed (PCMCIA), but the host struct will remain valid.
59 int hpsb_ref_host(struct hpsb_host *host)
64 down(&hpsb_hosts_lock);
65 list_for_each(lh, &hpsb_hosts) {
66 if (host == list_entry(lh, struct hpsb_host, host_list)) {
67 host->driver->devctl(host, MODIFY_USAGE, 1);
79 * hpsb_unref_host - decrease reference count for host controller.
80 * @host: the host controller
82 * Decrease the reference count for the specified host controller.
83 * When the reference count reaches zero, the memory allocated for the
84 * &hpsb_host will be freed.
87 void hpsb_unref_host(struct hpsb_host *host)
89 host->driver->devctl(host, MODIFY_USAGE, 0);
91 down(&hpsb_hosts_lock);
94 if (!host->refcount && host->is_shutdown)
100 * hpsb_alloc_host - allocate a new host controller.
101 * @drv: the driver that will manage the host controller
102 * @extra: number of extra bytes to allocate for the driver
104 * Allocate a &hpsb_host and initialize the general subsystem specific
105 * fields. If the driver needs to store per host data, as drivers
106 * usually do, the amount of memory required can be specified by the
107 * @extra parameter. Once allocated, the driver should initialize the
108 * driver specific parts, enable the controller and make it available
109 * to the general subsystem using hpsb_add_host().
111 * The &hpsb_host is allocated with an single initial reference
112 * belonging to the driver. Once the driver is done with the struct,
113 * for example, when the driver is unloaded, it should release this
114 * reference using hpsb_unref_host().
116 * Return Value: a pointer to the &hpsb_host if succesful, %NULL if
117 * no memory was available.
120 struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra)
125 h = kmalloc(sizeof(struct hpsb_host) + extra, SLAB_KERNEL);
127 memset(h, 0, sizeof(struct hpsb_host) + extra);
133 INIT_LIST_HEAD(&h->pending_packets);
134 spin_lock_init(&h->pending_pkt_lock);
136 for (i = 0; i < ARRAY_SIZE(h->tpool); i++)
137 HPSB_TPOOL_INIT(&h->tpool[i]);
139 atomic_set(&h->generation, 0);
141 init_timer(&h->timeout);
142 h->timeout.data = (unsigned long) h;
143 h->timeout.function = abort_timedouts;
144 h->timeout_interval = HZ / 20; // 50ms by default
146 h->topology_map = h->csr.topology_map + 3;
147 h->speed_map = (u8 *)(h->csr.speed_map + 2);
152 static int alloc_hostnum(void)
157 struct list_head *lh;
160 list_for_each(lh, &hpsb_hosts) {
161 struct hpsb_host *host = list_entry(lh, struct hpsb_host, host_list);
163 if (host->id == hostnum) {
178 void hpsb_add_host(struct hpsb_host *host)
180 down(&hpsb_hosts_lock);
181 host->id = alloc_hostnum();
182 list_add_tail(&host->host_list, &hpsb_hosts);
183 up(&hpsb_hosts_lock);
185 highlevel_add_host(host);
186 host->driver->devctl(host, RESET_BUS, LONG_RESET);
189 void hpsb_remove_host(struct hpsb_host *host)
191 down(&hpsb_hosts_lock);
192 host->is_shutdown = 1;
193 host->driver = &dummy_driver;
194 list_del(&host->host_list);
195 up(&hpsb_hosts_lock);
197 highlevel_remove_host(host);