Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * WHCI UWB Multi-interface Controller enumerator.
  3 *
  4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5 *
  6 * This file is released under the GNU GPL v2.
  7 */
  8#include <linux/delay.h>
  9#include <linux/kernel.h>
 
 10#include <linux/pci.h>
 11#include <linux/dma-mapping.h>
 12#include <linux/slab.h>
 13#include <linux/uwb/whci.h>
 14#include <linux/uwb/umc.h>
 15
 16struct whci_card {
 17	struct pci_dev *pci;
 18	void __iomem *uwbbase;
 19	u8 n_caps;
 20	struct umc_dev *devs[0];
 21};
 22
 23
 24/* Fix faulty HW :( */
 25static
 26u64 whci_capdata_quirks(struct whci_card *card, u64 capdata)
 27{
 28	u64 capdata_orig = capdata;
 29	struct pci_dev *pci_dev = card->pci;
 30	if (pci_dev->vendor == PCI_VENDOR_ID_INTEL
 31	    && (pci_dev->device == 0x0c3b || pci_dev->device == 0004)
 32	    && pci_dev->class == 0x0d1010) {
 33		switch (UWBCAPDATA_TO_CAP_ID(capdata)) {
 34			/* WLP capability has 0x100 bytes of aperture */
 35		case 0x80:
 36			capdata |= 0x40 << 8; break;
 37			/* WUSB capability has 0x80 bytes of aperture
 38			 * and ID is 1 */
 39		case 0x02:
 40			capdata &= ~0xffff;
 41			capdata |= 0x2001;
 42			break;
 43		}
 44	}
 45	if (capdata_orig != capdata)
 46		dev_warn(&pci_dev->dev,
 47			 "PCI v%04x d%04x c%06x#%02x: "
 48			 "corrected capdata from %016Lx to %016Lx\n",
 49			 pci_dev->vendor, pci_dev->device, pci_dev->class,
 50			 (unsigned)UWBCAPDATA_TO_CAP_ID(capdata),
 51			 (unsigned long long)capdata_orig,
 52			 (unsigned long long)capdata);
 53	return capdata;
 54}
 55
 56
 57/**
 58 * whci_wait_for - wait for a WHCI register to be set
 59 *
 60 * Polls (for at most @max_ms ms) until '*@reg & @mask == @result'.
 61 */
 62int whci_wait_for(struct device *dev, u32 __iomem *reg, u32 mask, u32 result,
 63	unsigned long max_ms, const char *tag)
 64{
 65	unsigned t = 0;
 66	u32 val;
 67	for (;;) {
 68		val = le_readl(reg);
 69		if ((val & mask) == result)
 70			break;
 71		if (t >= max_ms) {
 72			dev_err(dev, "%s timed out\n", tag);
 73			return -ETIMEDOUT;
 74		}
 75		msleep(10);
 76		t += 10;
 77	}
 78	return 0;
 79}
 80EXPORT_SYMBOL_GPL(whci_wait_for);
 81
 82
 83/*
 84 * NOTE: the capinfo and capdata registers are slightly different
 85 *       (size and cap-id fields). So for cap #0, we need to fill
 86 *       in. Size comes from the size of the register block
 87 *       (statically calculated); cap_id comes from nowhere, we use
 88 *       zero, that is reserved, for the radio controller, because
 89 *       none was defined at the spec level.
 90 */
 91static int whci_add_cap(struct whci_card *card, int n)
 92{
 93	struct umc_dev *umc;
 94	u64 capdata;
 95	int bar, err;
 96
 97	umc = umc_device_create(&card->pci->dev, n);
 98	if (umc == NULL)
 99		return -ENOMEM;
100
101	capdata = le_readq(card->uwbbase + UWBCAPDATA(n));
102
103	bar = UWBCAPDATA_TO_BAR(capdata) << 1;
104
105	capdata = whci_capdata_quirks(card, capdata);
106	/* Capability 0 is the radio controller. It's size is 32
107	 * bytes (WHCI0.95[2.3, T2-9]). */
108	umc->version         = UWBCAPDATA_TO_VERSION(capdata);
109	umc->cap_id          = n == 0 ? 0 : UWBCAPDATA_TO_CAP_ID(capdata);
110	umc->bar	     = bar;
111	umc->resource.start  = pci_resource_start(card->pci, bar)
112		+ UWBCAPDATA_TO_OFFSET(capdata);
113	umc->resource.end    = umc->resource.start
114		+ (n == 0 ? 0x20 : UWBCAPDATA_TO_SIZE(capdata)) - 1;
115	umc->resource.name   = dev_name(&umc->dev);
116	umc->resource.flags  = card->pci->resource[bar].flags;
117	umc->resource.parent = &card->pci->resource[bar];
118	umc->irq             = card->pci->irq;
119
120	err = umc_device_register(umc);
121	if (err < 0)
122		goto error;
123	card->devs[n] = umc;
124	return 0;
125
126error:
127	kfree(umc);
128	return err;
129}
130
131static void whci_del_cap(struct whci_card *card, int n)
132{
133	struct umc_dev *umc = card->devs[n];
134
135	if (umc != NULL)
136		umc_device_unregister(umc);
137}
138
139static int whci_n_caps(struct pci_dev *pci)
140{
141	void __iomem *uwbbase;
142	u64 capinfo;
143
144	uwbbase = pci_iomap(pci, 0, 8);
145	if (!uwbbase)
146		return -ENOMEM;
147	capinfo = le_readq(uwbbase + UWBCAPINFO);
148	pci_iounmap(pci, uwbbase);
149
150	return UWBCAPINFO_TO_N_CAPS(capinfo);
151}
152
153static int whci_probe(struct pci_dev *pci, const struct pci_device_id *id)
154{
155	struct whci_card *card;
156	int err, n_caps, n;
157
158	err = pci_enable_device(pci);
159	if (err < 0)
160		goto error;
161	pci_enable_msi(pci);
162	pci_set_master(pci);
163	err = -ENXIO;
164	if (!pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
165		pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
166	else if (!pci_set_dma_mask(pci, DMA_BIT_MASK(32)))
167		pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
168	else
169		goto error_dma;
170
171	err = n_caps = whci_n_caps(pci);
172	if (n_caps < 0)
173		goto error_ncaps;
174
175	err = -ENOMEM;
176	card = kzalloc(sizeof(struct whci_card)
177		       + sizeof(struct whci_dev *) * (n_caps + 1),
178		       GFP_KERNEL);
179	if (card == NULL)
180		goto error_kzalloc;
181	card->pci = pci;
182	card->n_caps = n_caps;
183
184	err = -EBUSY;
185	if (!request_mem_region(pci_resource_start(pci, 0),
186				UWBCAPDATA_SIZE(card->n_caps),
187				"whci (capability data)"))
188		goto error_request_memregion;
189	err = -ENOMEM;
190	card->uwbbase = pci_iomap(pci, 0, UWBCAPDATA_SIZE(card->n_caps));
191	if (!card->uwbbase)
192		goto error_iomap;
193
194	/* Add each capability. */
195	for (n = 0; n <= card->n_caps; n++) {
196		err = whci_add_cap(card, n);
197		if (err < 0 && n == 0) {
198			dev_err(&pci->dev, "cannot bind UWB radio controller:"
199				" %d\n", err);
200			goto error_bind;
201		}
202		if (err < 0)
203			dev_warn(&pci->dev, "warning: cannot bind capability "
204				 "#%u: %d\n", n, err);
205	}
206	pci_set_drvdata(pci, card);
207	return 0;
208
209error_bind:
210	pci_iounmap(pci, card->uwbbase);
211error_iomap:
212	release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
213error_request_memregion:
214	kfree(card);
215error_kzalloc:
216error_ncaps:
217error_dma:
218	pci_disable_msi(pci);
219	pci_disable_device(pci);
220error:
221	return err;
222}
223
224static void whci_remove(struct pci_dev *pci)
225{
226	struct whci_card *card = pci_get_drvdata(pci);
227	int n;
228
229	pci_set_drvdata(pci, NULL);
230	/* Unregister each capability in reverse (so the master device
231	 * is unregistered last). */
232	for (n = card->n_caps; n >= 0 ; n--)
233		whci_del_cap(card, n);
234	pci_iounmap(pci, card->uwbbase);
235	release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
236	kfree(card);
237	pci_disable_msi(pci);
238	pci_disable_device(pci);
239}
240
241static struct pci_device_id whci_id_table[] = {
242	{ PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
243	{ 0 },
244};
245MODULE_DEVICE_TABLE(pci, whci_id_table);
246
247
248static struct pci_driver whci_driver = {
249	.name     = "whci",
250	.id_table = whci_id_table,
251	.probe    = whci_probe,
252	.remove   = whci_remove,
253};
254
255static int __init whci_init(void)
256{
257	return pci_register_driver(&whci_driver);
258}
259
260static void __exit whci_exit(void)
261{
262	pci_unregister_driver(&whci_driver);
263}
264
265module_init(whci_init);
266module_exit(whci_exit);
267
268MODULE_DESCRIPTION("WHCI UWB Multi-interface Controller enumerator");
269MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
270MODULE_LICENSE("GPL");
v3.15
  1/*
  2 * WHCI UWB Multi-interface Controller enumerator.
  3 *
  4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5 *
  6 * This file is released under the GNU GPL v2.
  7 */
  8#include <linux/delay.h>
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/pci.h>
 12#include <linux/dma-mapping.h>
 13#include <linux/slab.h>
 14#include <linux/uwb/whci.h>
 15#include <linux/uwb/umc.h>
 16
 17struct whci_card {
 18	struct pci_dev *pci;
 19	void __iomem *uwbbase;
 20	u8 n_caps;
 21	struct umc_dev *devs[0];
 22};
 23
 24
 25/* Fix faulty HW :( */
 26static
 27u64 whci_capdata_quirks(struct whci_card *card, u64 capdata)
 28{
 29	u64 capdata_orig = capdata;
 30	struct pci_dev *pci_dev = card->pci;
 31	if (pci_dev->vendor == PCI_VENDOR_ID_INTEL
 32	    && (pci_dev->device == 0x0c3b || pci_dev->device == 0004)
 33	    && pci_dev->class == 0x0d1010) {
 34		switch (UWBCAPDATA_TO_CAP_ID(capdata)) {
 35			/* WLP capability has 0x100 bytes of aperture */
 36		case 0x80:
 37			capdata |= 0x40 << 8; break;
 38			/* WUSB capability has 0x80 bytes of aperture
 39			 * and ID is 1 */
 40		case 0x02:
 41			capdata &= ~0xffff;
 42			capdata |= 0x2001;
 43			break;
 44		}
 45	}
 46	if (capdata_orig != capdata)
 47		dev_warn(&pci_dev->dev,
 48			 "PCI v%04x d%04x c%06x#%02x: "
 49			 "corrected capdata from %016Lx to %016Lx\n",
 50			 pci_dev->vendor, pci_dev->device, pci_dev->class,
 51			 (unsigned)UWBCAPDATA_TO_CAP_ID(capdata),
 52			 (unsigned long long)capdata_orig,
 53			 (unsigned long long)capdata);
 54	return capdata;
 55}
 56
 57
 58/**
 59 * whci_wait_for - wait for a WHCI register to be set
 60 *
 61 * Polls (for at most @max_ms ms) until '*@reg & @mask == @result'.
 62 */
 63int whci_wait_for(struct device *dev, u32 __iomem *reg, u32 mask, u32 result,
 64	unsigned long max_ms, const char *tag)
 65{
 66	unsigned t = 0;
 67	u32 val;
 68	for (;;) {
 69		val = le_readl(reg);
 70		if ((val & mask) == result)
 71			break;
 72		if (t >= max_ms) {
 73			dev_err(dev, "%s timed out\n", tag);
 74			return -ETIMEDOUT;
 75		}
 76		msleep(10);
 77		t += 10;
 78	}
 79	return 0;
 80}
 81EXPORT_SYMBOL_GPL(whci_wait_for);
 82
 83
 84/*
 85 * NOTE: the capinfo and capdata registers are slightly different
 86 *       (size and cap-id fields). So for cap #0, we need to fill
 87 *       in. Size comes from the size of the register block
 88 *       (statically calculated); cap_id comes from nowhere, we use
 89 *       zero, that is reserved, for the radio controller, because
 90 *       none was defined at the spec level.
 91 */
 92static int whci_add_cap(struct whci_card *card, int n)
 93{
 94	struct umc_dev *umc;
 95	u64 capdata;
 96	int bar, err;
 97
 98	umc = umc_device_create(&card->pci->dev, n);
 99	if (umc == NULL)
100		return -ENOMEM;
101
102	capdata = le_readq(card->uwbbase + UWBCAPDATA(n));
103
104	bar = UWBCAPDATA_TO_BAR(capdata) << 1;
105
106	capdata = whci_capdata_quirks(card, capdata);
107	/* Capability 0 is the radio controller. It's size is 32
108	 * bytes (WHCI0.95[2.3, T2-9]). */
109	umc->version         = UWBCAPDATA_TO_VERSION(capdata);
110	umc->cap_id          = n == 0 ? 0 : UWBCAPDATA_TO_CAP_ID(capdata);
111	umc->bar	     = bar;
112	umc->resource.start  = pci_resource_start(card->pci, bar)
113		+ UWBCAPDATA_TO_OFFSET(capdata);
114	umc->resource.end    = umc->resource.start
115		+ (n == 0 ? 0x20 : UWBCAPDATA_TO_SIZE(capdata)) - 1;
116	umc->resource.name   = dev_name(&umc->dev);
117	umc->resource.flags  = card->pci->resource[bar].flags;
118	umc->resource.parent = &card->pci->resource[bar];
119	umc->irq             = card->pci->irq;
120
121	err = umc_device_register(umc);
122	if (err < 0)
123		goto error;
124	card->devs[n] = umc;
125	return 0;
126
127error:
128	kfree(umc);
129	return err;
130}
131
132static void whci_del_cap(struct whci_card *card, int n)
133{
134	struct umc_dev *umc = card->devs[n];
135
136	if (umc != NULL)
137		umc_device_unregister(umc);
138}
139
140static int whci_n_caps(struct pci_dev *pci)
141{
142	void __iomem *uwbbase;
143	u64 capinfo;
144
145	uwbbase = pci_iomap(pci, 0, 8);
146	if (!uwbbase)
147		return -ENOMEM;
148	capinfo = le_readq(uwbbase + UWBCAPINFO);
149	pci_iounmap(pci, uwbbase);
150
151	return UWBCAPINFO_TO_N_CAPS(capinfo);
152}
153
154static int whci_probe(struct pci_dev *pci, const struct pci_device_id *id)
155{
156	struct whci_card *card;
157	int err, n_caps, n;
158
159	err = pci_enable_device(pci);
160	if (err < 0)
161		goto error;
162	pci_enable_msi(pci);
163	pci_set_master(pci);
164	err = -ENXIO;
165	if (!pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
166		pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
167	else if (!pci_set_dma_mask(pci, DMA_BIT_MASK(32)))
168		pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
169	else
170		goto error_dma;
171
172	err = n_caps = whci_n_caps(pci);
173	if (n_caps < 0)
174		goto error_ncaps;
175
176	err = -ENOMEM;
177	card = kzalloc(sizeof(struct whci_card)
178		       + sizeof(struct whci_dev *) * (n_caps + 1),
179		       GFP_KERNEL);
180	if (card == NULL)
181		goto error_kzalloc;
182	card->pci = pci;
183	card->n_caps = n_caps;
184
185	err = -EBUSY;
186	if (!request_mem_region(pci_resource_start(pci, 0),
187				UWBCAPDATA_SIZE(card->n_caps),
188				"whci (capability data)"))
189		goto error_request_memregion;
190	err = -ENOMEM;
191	card->uwbbase = pci_iomap(pci, 0, UWBCAPDATA_SIZE(card->n_caps));
192	if (!card->uwbbase)
193		goto error_iomap;
194
195	/* Add each capability. */
196	for (n = 0; n <= card->n_caps; n++) {
197		err = whci_add_cap(card, n);
198		if (err < 0 && n == 0) {
199			dev_err(&pci->dev, "cannot bind UWB radio controller:"
200				" %d\n", err);
201			goto error_bind;
202		}
203		if (err < 0)
204			dev_warn(&pci->dev, "warning: cannot bind capability "
205				 "#%u: %d\n", n, err);
206	}
207	pci_set_drvdata(pci, card);
208	return 0;
209
210error_bind:
211	pci_iounmap(pci, card->uwbbase);
212error_iomap:
213	release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
214error_request_memregion:
215	kfree(card);
216error_kzalloc:
217error_ncaps:
218error_dma:
219	pci_disable_msi(pci);
220	pci_disable_device(pci);
221error:
222	return err;
223}
224
225static void whci_remove(struct pci_dev *pci)
226{
227	struct whci_card *card = pci_get_drvdata(pci);
228	int n;
229
230	pci_set_drvdata(pci, NULL);
231	/* Unregister each capability in reverse (so the master device
232	 * is unregistered last). */
233	for (n = card->n_caps; n >= 0 ; n--)
234		whci_del_cap(card, n);
235	pci_iounmap(pci, card->uwbbase);
236	release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
237	kfree(card);
238	pci_disable_msi(pci);
239	pci_disable_device(pci);
240}
241
242static struct pci_device_id whci_id_table[] = {
243	{ PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
244	{ 0 },
245};
246MODULE_DEVICE_TABLE(pci, whci_id_table);
247
248
249static struct pci_driver whci_driver = {
250	.name     = "whci",
251	.id_table = whci_id_table,
252	.probe    = whci_probe,
253	.remove   = whci_remove,
254};
255
256module_pci_driver(whci_driver);
 
 
 
 
 
 
 
 
 
 
 
 
257MODULE_DESCRIPTION("WHCI UWB Multi-interface Controller enumerator");
258MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
259MODULE_LICENSE("GPL");