1 /*
2 * IEEE 1394 for Linux
3 *
4 * Low level (host adapter) management.
5 *
6 * Copyright (C) 1999 Andreas E. Bombe
7 * Copyright (C) 1999 Emanuel Pirker
8 *
9 * This code is licensed under the GPL. See the file COPYING in the root
10 * directory of the kernel sources for details.
11 */
12
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>
18
19 #include "ieee1394_types.h"
20 #include "hosts.h"
21 #include "ieee1394_core.h"
22 #include "highlevel.h"
23
24 LIST_HEAD(hpsb_hosts);
25 DECLARE_MUTEX(hpsb_hosts_lock);
26
dummy_transmit_packet(struct hpsb_host * h,struct hpsb_packet * p)27 static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
28 {
29 return 0;
30 }
31
dummy_devctl(struct hpsb_host * h,enum devctl_cmd c,int arg)32 static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
33 {
34 return -1;
35 }
36
dummy_isoctl(struct hpsb_iso * iso,enum isoctl_cmd command,unsigned long arg)37 static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
38 {
39 return -1;
40 }
41
42 static struct hpsb_host_driver dummy_driver = {
43 .transmit_packet = dummy_transmit_packet,
44 .devctl = dummy_devctl,
45 .isoctl = dummy_isoctl
46 };
47
48 /**
49 * hpsb_ref_host - increase reference count for host controller.
50 * @host: the host controller
51 *
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.
57 */
58
hpsb_ref_host(struct hpsb_host * host)59 int hpsb_ref_host(struct hpsb_host *host)
60 {
61 struct list_head *lh;
62 int retval = 0;
63
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);
68 host->refcount++;
69 retval = 1;
70 break;
71 }
72 }
73 up(&hpsb_hosts_lock);
74
75 return retval;
76 }
77
78 /**
79 * hpsb_unref_host - decrease reference count for host controller.
80 * @host: the host controller
81 *
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.
85 */
86
hpsb_unref_host(struct hpsb_host * host)87 void hpsb_unref_host(struct hpsb_host *host)
88 {
89 host->driver->devctl(host, MODIFY_USAGE, 0);
90
91 down(&hpsb_hosts_lock);
92 host->refcount--;
93
94 if (!host->refcount && host->is_shutdown)
95 kfree(host);
96 up(&hpsb_hosts_lock);
97 }
98
99 /**
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
103 *
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().
110 *
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().
115 *
116 * Return Value: a pointer to the &hpsb_host if succesful, %NULL if
117 * no memory was available.
118 */
119
hpsb_alloc_host(struct hpsb_host_driver * drv,size_t extra)120 struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra)
121 {
122 struct hpsb_host *h;
123 int i;
124
125 h = kmalloc(sizeof(struct hpsb_host) + extra, SLAB_KERNEL);
126 if (!h) return NULL;
127 memset(h, 0, sizeof(struct hpsb_host) + extra);
128
129 h->hostdata = h + 1;
130 h->driver = drv;
131 h->refcount = 1;
132
133 INIT_LIST_HEAD(&h->pending_packets);
134 spin_lock_init(&h->pending_pkt_lock);
135
136 for (i = 0; i < ARRAY_SIZE(h->tpool); i++)
137 HPSB_TPOOL_INIT(&h->tpool[i]);
138
139 atomic_set(&h->generation, 0);
140
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
145
146 h->topology_map = h->csr.topology_map + 3;
147 h->speed_map = (u8 *)(h->csr.speed_map + 2);
148
149 return h;
150 }
151
alloc_hostnum(void)152 static int alloc_hostnum(void)
153 {
154 int hostnum = 0;
155
156 while (1) {
157 struct list_head *lh;
158 int found = 0;
159
160 list_for_each(lh, &hpsb_hosts) {
161 struct hpsb_host *host = list_entry(lh, struct hpsb_host, host_list);
162
163 if (host->id == hostnum) {
164 found = 1;
165 break;
166 }
167 }
168
169 if (!found)
170 return hostnum;
171
172 hostnum++;
173 }
174
175 return 0;
176 }
177
hpsb_add_host(struct hpsb_host * host)178 void hpsb_add_host(struct hpsb_host *host)
179 {
180 down(&hpsb_hosts_lock);
181 host->id = alloc_hostnum();
182 list_add_tail(&host->host_list, &hpsb_hosts);
183 up(&hpsb_hosts_lock);
184
185 highlevel_add_host(host);
186 host->driver->devctl(host, RESET_BUS, LONG_RESET);
187 }
188
hpsb_remove_host(struct hpsb_host * host)189 void hpsb_remove_host(struct hpsb_host *host)
190 {
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);
196
197 highlevel_remove_host(host);
198 }
199