Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | /* * IEEE 1394 for Linux * * Low level (host adapter) management. * * Copyright (C) 1999 Andreas E. Bombe * Copyright (C) 1999 Emanuel Pirker * * This code is licensed under the GPL. See the file COPYING in the root * directory of the kernel sources for details. */ #include <linux/config.h> #include <linux/module.h> #include <linux/types.h> #include <linux/list.h> #include <linux/init.h> #include <linux/slab.h> #include "ieee1394_types.h" #include "hosts.h" #include "ieee1394_core.h" #include "highlevel.h" LIST_HEAD(hpsb_hosts); DECLARE_MUTEX(hpsb_hosts_lock); static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p) { return 0; } static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg) { return -1; } static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg) { return -1; } static struct hpsb_host_driver dummy_driver = { .transmit_packet = dummy_transmit_packet, .devctl = dummy_devctl, .isoctl = dummy_isoctl }; /** * hpsb_ref_host - increase reference count for host controller. * @host: the host controller * * Increase the reference count for the specified host controller. * When holding a reference to a host, the memory allocated for the * host struct will not be freed and the host is guaranteed to be in a * consistent state. The driver may be unloaded or the controller may * be removed (PCMCIA), but the host struct will remain valid. */ int hpsb_ref_host(struct hpsb_host *host) { struct list_head *lh; int retval = 0; down(&hpsb_hosts_lock); list_for_each(lh, &hpsb_hosts) { if (host == list_entry(lh, struct hpsb_host, host_list)) { if (try_module_get(host->driver->owner)) { atomic_inc(&host->refcount); retval = 1; } break; } } up(&hpsb_hosts_lock); return retval; } /** * hpsb_unref_host - decrease reference count for host controller. * @host: the host controller * * Decrease the reference count for the specified host controller. * When the reference count reaches zero, the memory allocated for the * &hpsb_host will be freed. */ void hpsb_unref_host(struct hpsb_host *host) { module_put(host->driver->owner); down(&hpsb_hosts_lock); if (atomic_dec_and_test(&host->refcount) && host->is_shutdown) device_unregister(&host->device); up(&hpsb_hosts_lock); } /** * hpsb_alloc_host - allocate a new host controller. * @drv: the driver that will manage the host controller * @extra: number of extra bytes to allocate for the driver * * Allocate a &hpsb_host and initialize the general subsystem specific * fields. If the driver needs to store per host data, as drivers * usually do, the amount of memory required can be specified by the * @extra parameter. Once allocated, the driver should initialize the * driver specific parts, enable the controller and make it available * to the general subsystem using hpsb_add_host(). * * The &hpsb_host is allocated with an single initial reference * belonging to the driver. Once the driver is done with the struct, * for example, when the driver is unloaded, it should release this * reference using hpsb_unref_host(). * * Return Value: a pointer to the &hpsb_host if succesful, %NULL if * no memory was available. */ struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra) { struct hpsb_host *h; int i; h = kmalloc(sizeof(struct hpsb_host) + extra, SLAB_KERNEL); if (!h) return NULL; memset(h, 0, sizeof(struct hpsb_host) + extra); h->hostdata = h + 1; h->driver = drv; atomic_set(&h->refcount, 1); INIT_LIST_HEAD(&h->pending_packets); spin_lock_init(&h->pending_pkt_lock); for (i = 0; i < ARRAY_SIZE(h->tpool); i++) HPSB_TPOOL_INIT(&h->tpool[i]); atomic_set(&h->generation, 0); init_timer(&h->timeout); h->timeout.data = (unsigned long) h; h->timeout.function = abort_timedouts; h->timeout_interval = HZ / 20; // 50ms by default h->topology_map = h->csr.topology_map + 3; h->speed_map = (u8 *)(h->csr.speed_map + 2); return h; } static int alloc_hostnum(void) { int hostnum = 0; while (1) { struct list_head *lh; int found = 0; list_for_each(lh, &hpsb_hosts) { struct hpsb_host *host = list_entry(lh, struct hpsb_host, host_list); if (host->id == hostnum) { found = 1; break; } } if (!found) return hostnum; hostnum++; } return 0; } void hpsb_add_host(struct hpsb_host *host) { down(&hpsb_hosts_lock); host->id = alloc_hostnum(); list_add_tail(&host->host_list, &hpsb_hosts); up(&hpsb_hosts_lock); highlevel_add_host(host); host->driver->devctl(host, RESET_BUS, LONG_RESET); } void hpsb_remove_host(struct hpsb_host *host) { down(&hpsb_hosts_lock); host->is_shutdown = 1; host->driver = &dummy_driver; list_del(&host->host_list); up(&hpsb_hosts_lock); highlevel_remove_host(host); } |