aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/cb710
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/misc/cb710')
-rw-r--r--drivers/misc/cb710/Kconfig21
-rw-r--r--drivers/misc/cb710/Makefile4
-rw-r--r--drivers/misc/cb710/core.c357
-rw-r--r--drivers/misc/cb710/debug.c119
-rw-r--r--drivers/misc/cb710/sgbuf2.c150
5 files changed, 651 insertions, 0 deletions
diff --git a/drivers/misc/cb710/Kconfig b/drivers/misc/cb710/Kconfig
new file mode 100644
index 000000000000..0b55079774c8
--- /dev/null
+++ b/drivers/misc/cb710/Kconfig
@@ -0,0 +1,21 @@
1config CB710_CORE
2 tristate "ENE CB710/720 Flash memory card reader support"
3 depends on PCI
4 help
5 This option enables support for PCI ENE CB710/720 Flash memory card
6 reader found in some laptops (ie. some versions of HP Compaq nx9500).
7
8 You will also have to select some flash card format drivers (MMC/SD,
9 MemoryStick).
10
11 This driver can also be built as a module. If so, the module
12 will be called cb710.
13
14config CB710_DEBUG
15 bool "Enable driver debugging"
16 depends on CB710_CORE != n
17 default n
18 help
19 This is an option for use by developers; most people should
20 say N here. This adds a lot of debugging output to dmesg.
21
diff --git a/drivers/misc/cb710/Makefile b/drivers/misc/cb710/Makefile
new file mode 100644
index 000000000000..62d51acfecaf
--- /dev/null
+++ b/drivers/misc/cb710/Makefile
@@ -0,0 +1,4 @@
1obj-$(CONFIG_CB710_CORE) += cb710.o
2
3cb710-y := core.o sgbuf2.o
4cb710-$(CONFIG_CB710_DEBUG) += debug.o
diff --git a/drivers/misc/cb710/core.c b/drivers/misc/cb710/core.c
new file mode 100644
index 000000000000..b14eab0f2ba5
--- /dev/null
+++ b/drivers/misc/cb710/core.c
@@ -0,0 +1,357 @@
1/*
2 * cb710/core.c
3 *
4 * Copyright by Michał Mirosław, 2008-2009
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/pci.h>
14#include <linux/spinlock.h>
15#include <linux/idr.h>
16#include <linux/cb710.h>
17
18static DEFINE_IDA(cb710_ida);
19static DEFINE_SPINLOCK(cb710_ida_lock);
20
21void cb710_pci_update_config_reg(struct pci_dev *pdev,
22 int reg, uint32_t mask, uint32_t xor)
23{
24 u32 rval;
25
26 pci_read_config_dword(pdev, reg, &rval);
27 rval = (rval & mask) ^ xor;
28 pci_write_config_dword(pdev, reg, rval);
29}
30EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
31
32/* Some magic writes based on Windows driver init code */
33static int __devinit cb710_pci_configure(struct pci_dev *pdev)
34{
35 unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
36 struct pci_dev *pdev0 = pci_get_slot(pdev->bus, devfn);
37 u32 val;
38
39 cb710_pci_update_config_reg(pdev, 0x48,
40 ~0x000000FF, 0x0000003F);
41
42 pci_read_config_dword(pdev, 0x48, &val);
43 if (val & 0x80000000)
44 return 0;
45
46 if (!pdev0)
47 return -ENODEV;
48
49 if (pdev0->vendor == PCI_VENDOR_ID_ENE
50 && pdev0->device == PCI_DEVICE_ID_ENE_720) {
51 cb710_pci_update_config_reg(pdev0, 0x8C,
52 ~0x00F00000, 0x00100000);
53 cb710_pci_update_config_reg(pdev0, 0xB0,
54 ~0x08000000, 0x08000000);
55 }
56
57 cb710_pci_update_config_reg(pdev0, 0x8C,
58 ~0x00000F00, 0x00000200);
59 cb710_pci_update_config_reg(pdev0, 0x90,
60 ~0x00060000, 0x00040000);
61
62 pci_dev_put(pdev0);
63
64 return 0;
65}
66
67static irqreturn_t cb710_irq_handler(int irq, void *data)
68{
69 struct cb710_chip *chip = data;
70 struct cb710_slot *slot = &chip->slot[0];
71 irqreturn_t handled = IRQ_NONE;
72 unsigned nr;
73
74 spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
75
76 for (nr = chip->slots; nr; ++slot, --nr) {
77 cb710_irq_handler_t handler_func = slot->irq_handler;
78 if (handler_func && handler_func(slot))
79 handled = IRQ_HANDLED;
80 }
81
82 spin_unlock(&chip->irq_lock);
83
84 return handled;
85}
86
87static void cb710_release_slot(struct device *dev)
88{
89#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
90 struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
91 struct cb710_chip *chip = cb710_slot_to_chip(slot);
92
93 /* slot struct can be freed now */
94 atomic_dec(&chip->slot_refs_count);
95#endif
96}
97
98static int __devinit cb710_register_slot(struct cb710_chip *chip,
99 unsigned slot_mask, unsigned io_offset, const char *name)
100{
101 int nr = chip->slots;
102 struct cb710_slot *slot = &chip->slot[nr];
103 int err;
104
105 dev_dbg(cb710_chip_dev(chip),
106 "register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
107 name, chip->platform_id, nr, slot_mask, io_offset);
108
109 /* slot->irq_handler == NULL here; this needs to be
110 * seen before platform_device_register() */
111 ++chip->slots;
112 smp_wmb();
113
114 slot->iobase = chip->iobase + io_offset;
115 slot->pdev.name = name;
116 slot->pdev.id = chip->platform_id;
117 slot->pdev.dev.parent = &chip->pdev->dev;
118 slot->pdev.dev.release = cb710_release_slot;
119
120 err = platform_device_register(&slot->pdev);
121
122#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
123 atomic_inc(&chip->slot_refs_count);
124#endif
125
126 if (err) {
127 /* device_initialize() called from platform_device_register()
128 * wants this on error path */
129 platform_device_put(&slot->pdev);
130
131 /* slot->irq_handler == NULL here anyway, so no lock needed */
132 --chip->slots;
133 return err;
134 }
135
136 chip->slot_mask |= slot_mask;
137
138 return 0;
139}
140
141static void cb710_unregister_slot(struct cb710_chip *chip,
142 unsigned slot_mask)
143{
144 int nr = chip->slots - 1;
145
146 if (!(chip->slot_mask & slot_mask))
147 return;
148
149 platform_device_unregister(&chip->slot[nr].pdev);
150
151 /* complementary to spin_unlock() in cb710_set_irq_handler() */
152 smp_rmb();
153 BUG_ON(chip->slot[nr].irq_handler != NULL);
154
155 /* slot->irq_handler == NULL here, so no lock needed */
156 --chip->slots;
157 chip->slot_mask &= ~slot_mask;
158}
159
160void cb710_set_irq_handler(struct cb710_slot *slot,
161 cb710_irq_handler_t handler)
162{
163 struct cb710_chip *chip = cb710_slot_to_chip(slot);
164 unsigned long flags;
165
166 spin_lock_irqsave(&chip->irq_lock, flags);
167 slot->irq_handler = handler;
168 spin_unlock_irqrestore(&chip->irq_lock, flags);
169}
170EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
171
172#ifdef CONFIG_PM
173
174static int cb710_suspend(struct pci_dev *pdev, pm_message_t state)
175{
176 struct cb710_chip *chip = pci_get_drvdata(pdev);
177
178 free_irq(pdev->irq, chip);
179 pci_save_state(pdev);
180 pci_disable_device(pdev);
181 if (state.event & PM_EVENT_SLEEP)
182 pci_set_power_state(pdev, PCI_D3cold);
183 return 0;
184}
185
186static int cb710_resume(struct pci_dev *pdev)
187{
188 struct cb710_chip *chip = pci_get_drvdata(pdev);
189 int err;
190
191 pci_set_power_state(pdev, PCI_D0);
192 pci_restore_state(pdev);
193 err = pcim_enable_device(pdev);
194 if (err)
195 return err;
196
197 return devm_request_irq(&pdev->dev, pdev->irq,
198 cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
199}
200
201#endif /* CONFIG_PM */
202
203static int __devinit cb710_probe(struct pci_dev *pdev,
204 const struct pci_device_id *ent)
205{
206 struct cb710_chip *chip;
207 unsigned long flags;
208 u32 val;
209 int err;
210 int n = 0;
211
212 err = cb710_pci_configure(pdev);
213 if (err)
214 return err;
215
216 /* this is actually magic... */
217 pci_read_config_dword(pdev, 0x48, &val);
218 if (!(val & 0x80000000)) {
219 pci_write_config_dword(pdev, 0x48, val|0x71000000);
220 pci_read_config_dword(pdev, 0x48, &val);
221 }
222
223 dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
224 if (!(val & 0x70000000))
225 return -ENODEV;
226 val = (val >> 28) & 7;
227 if (val & CB710_SLOT_MMC)
228 ++n;
229 if (val & CB710_SLOT_MS)
230 ++n;
231 if (val & CB710_SLOT_SM)
232 ++n;
233
234 chip = devm_kzalloc(&pdev->dev,
235 sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
236 if (!chip)
237 return -ENOMEM;
238
239 err = pcim_enable_device(pdev);
240 if (err)
241 return err;
242
243 err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
244 if (err)
245 return err;
246
247 chip->pdev = pdev;
248 chip->iobase = pcim_iomap_table(pdev)[0];
249
250 pci_set_drvdata(pdev, chip);
251
252 err = devm_request_irq(&pdev->dev, pdev->irq,
253 cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
254 if (err)
255 return err;
256
257 do {
258 if (!ida_pre_get(&cb710_ida, GFP_KERNEL))
259 return -ENOMEM;
260
261 spin_lock_irqsave(&cb710_ida_lock, flags);
262 err = ida_get_new(&cb710_ida, &chip->platform_id);
263 spin_unlock_irqrestore(&cb710_ida_lock, flags);
264
265 if (err && err != -EAGAIN)
266 return err;
267 } while (err);
268
269
270 dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
271 chip->platform_id, chip->iobase, pdev->irq);
272
273 if (val & CB710_SLOT_MMC) { /* MMC/SD slot */
274 err = cb710_register_slot(chip,
275 CB710_SLOT_MMC, 0x00, "cb710-mmc");
276 if (err)
277 return err;
278 }
279
280 if (val & CB710_SLOT_MS) { /* MemoryStick slot */
281 err = cb710_register_slot(chip,
282 CB710_SLOT_MS, 0x40, "cb710-ms");
283 if (err)
284 goto unreg_mmc;
285 }
286
287 if (val & CB710_SLOT_SM) { /* SmartMedia slot */
288 err = cb710_register_slot(chip,
289 CB710_SLOT_SM, 0x60, "cb710-sm");
290 if (err)
291 goto unreg_ms;
292 }
293
294 return 0;
295unreg_ms:
296 cb710_unregister_slot(chip, CB710_SLOT_MS);
297unreg_mmc:
298 cb710_unregister_slot(chip, CB710_SLOT_MMC);
299
300#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
301 BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
302#endif
303 return err;
304}
305
306static void __devexit cb710_remove_one(struct pci_dev *pdev)
307{
308 struct cb710_chip *chip = pci_get_drvdata(pdev);
309 unsigned long flags;
310
311 cb710_unregister_slot(chip, CB710_SLOT_SM);
312 cb710_unregister_slot(chip, CB710_SLOT_MS);
313 cb710_unregister_slot(chip, CB710_SLOT_MMC);
314#ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
315 BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
316#endif
317
318 spin_lock_irqsave(&cb710_ida_lock, flags);
319 ida_remove(&cb710_ida, chip->platform_id);
320 spin_unlock_irqrestore(&cb710_ida_lock, flags);
321}
322
323static const struct pci_device_id cb710_pci_tbl[] = {
324 { PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
325 PCI_ANY_ID, PCI_ANY_ID, },
326 { 0, }
327};
328
329static struct pci_driver cb710_driver = {
330 .name = KBUILD_MODNAME,
331 .id_table = cb710_pci_tbl,
332 .probe = cb710_probe,
333 .remove = __devexit_p(cb710_remove_one),
334#ifdef CONFIG_PM
335 .suspend = cb710_suspend,
336 .resume = cb710_resume,
337#endif
338};
339
340static int __init cb710_init_module(void)
341{
342 return pci_register_driver(&cb710_driver);
343}
344
345static void __exit cb710_cleanup_module(void)
346{
347 pci_unregister_driver(&cb710_driver);
348 ida_destroy(&cb710_ida);
349}
350
351module_init(cb710_init_module);
352module_exit(cb710_cleanup_module);
353
354MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
355MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
356MODULE_LICENSE("GPL");
357MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);
diff --git a/drivers/misc/cb710/debug.c b/drivers/misc/cb710/debug.c
new file mode 100644
index 000000000000..9da11bc620af
--- /dev/null
+++ b/drivers/misc/cb710/debug.c
@@ -0,0 +1,119 @@
1/*
2 * cb710/debug.c
3 *
4 * Copyright by Michał Mirosław, 2008-2009
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/cb710.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14
15#define CB710_REG_COUNT 0x80
16
17static const u16 allow[CB710_REG_COUNT/16] = {
18 0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
19 0xFFF0, 0xFFFF, 0xFFFF, 0xFFFF,
20};
21static const char *const prefix[ARRAY_SIZE(allow)] = {
22 "MMC", "MMC", "MMC", "MMC",
23 "MS?", "MS?", "SM?", "SM?"
24};
25
26static inline int allow_reg_read(unsigned block, unsigned offset, unsigned bits)
27{
28 unsigned mask = (1 << bits/8) - 1;
29 offset *= bits/8;
30 return ((allow[block] >> offset) & mask) == mask;
31}
32
33#define CB710_READ_REGS_TEMPLATE(t) \
34static void cb710_read_regs_##t(void __iomem *iobase, \
35 u##t *reg, unsigned select) \
36{ \
37 unsigned i, j; \
38 \
39 for (i = 0; i < ARRAY_SIZE(allow); ++i, reg += 16/(t/8)) { \
40 if (!select & (1 << i)) \
41 continue; \
42 \
43 for (j = 0; j < 0x10/(t/8); ++j) { \
44 if (!allow_reg_read(i, j, t)) \
45 continue; \
46 reg[j] = ioread##t(iobase \
47 + (i << 4) + (j * (t/8))); \
48 } \
49 } \
50}
51
52static const char cb710_regf_8[] = "%02X";
53static const char cb710_regf_16[] = "%04X";
54static const char cb710_regf_32[] = "%08X";
55static const char cb710_xes[] = "xxxxxxxx";
56
57#define CB710_DUMP_REGS_TEMPLATE(t) \
58static void cb710_dump_regs_##t(struct device *dev, \
59 const u##t *reg, unsigned select) \
60{ \
61 const char *const xp = &cb710_xes[8 - t/4]; \
62 const char *const format = cb710_regf_##t; \
63 \
64 char msg[100], *p; \
65 unsigned i, j; \
66 \
67 for (i = 0; i < ARRAY_SIZE(allow); ++i, reg += 16/(t/8)) { \
68 if (!(select & (1 << i))) \
69 continue; \
70 p = msg; \
71 for (j = 0; j < 0x10/(t/8); ++j) { \
72 *p++ = ' '; \
73 if (j == 8/(t/8)) \
74 *p++ = ' '; \
75 if (allow_reg_read(i, j, t)) \
76 p += sprintf(p, format, reg[j]); \
77 else \
78 p += sprintf(p, "%s", xp); \
79 } \
80 dev_dbg(dev, "%s 0x%02X %s\n", prefix[i], i << 4, msg); \
81 } \
82}
83
84#define CB710_READ_AND_DUMP_REGS_TEMPLATE(t) \
85static void cb710_read_and_dump_regs_##t(struct cb710_chip *chip, \
86 unsigned select) \
87{ \
88 u##t regs[CB710_REG_COUNT/sizeof(u##t)]; \
89 \
90 memset(&regs, 0, sizeof(regs)); \
91 cb710_read_regs_##t(chip->iobase, regs, select); \
92 cb710_dump_regs_##t(cb710_chip_dev(chip), regs, select); \
93}
94
95#define CB710_REG_ACCESS_TEMPLATES(t) \
96 CB710_READ_REGS_TEMPLATE(t) \
97 CB710_DUMP_REGS_TEMPLATE(t) \
98 CB710_READ_AND_DUMP_REGS_TEMPLATE(t)
99
100CB710_REG_ACCESS_TEMPLATES(8)
101CB710_REG_ACCESS_TEMPLATES(16)
102CB710_REG_ACCESS_TEMPLATES(32)
103
104void cb710_dump_regs(struct cb710_chip *chip, unsigned select)
105{
106 if (!(select & CB710_DUMP_REGS_MASK))
107 select = CB710_DUMP_REGS_ALL;
108 if (!(select & CB710_DUMP_ACCESS_MASK))
109 select |= CB710_DUMP_ACCESS_8;
110
111 if (select & CB710_DUMP_ACCESS_32)
112 cb710_read_and_dump_regs_32(chip, select);
113 if (select & CB710_DUMP_ACCESS_16)
114 cb710_read_and_dump_regs_16(chip, select);
115 if (select & CB710_DUMP_ACCESS_8)
116 cb710_read_and_dump_regs_8(chip, select);
117}
118EXPORT_SYMBOL_GPL(cb710_dump_regs);
119
diff --git a/drivers/misc/cb710/sgbuf2.c b/drivers/misc/cb710/sgbuf2.c
new file mode 100644
index 000000000000..d38a7acdb6ec
--- /dev/null
+++ b/drivers/misc/cb710/sgbuf2.c
@@ -0,0 +1,150 @@
1/*
2 * cb710/sgbuf2.c
3 *
4 * Copyright by Michał Mirosław, 2008-2009
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/cb710.h>
13
14static bool sg_dwiter_next(struct sg_mapping_iter *miter)
15{
16 if (sg_miter_next(miter)) {
17 miter->consumed = 0;
18 return true;
19 } else
20 return false;
21}
22
23static bool sg_dwiter_is_at_end(struct sg_mapping_iter *miter)
24{
25 return miter->length == miter->consumed && !sg_dwiter_next(miter);
26}
27
28static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter *miter)
29{
30 size_t len, left = 4;
31 uint32_t data;
32 void *addr = &data;
33
34 do {
35 len = min(miter->length - miter->consumed, left);
36 memcpy(addr, miter->addr + miter->consumed, len);
37 miter->consumed += len;
38 left -= len;
39 if (!left)
40 return data;
41 addr += len;
42 } while (sg_dwiter_next(miter));
43
44 memset(addr, 0, left);
45 return data;
46}
47
48static inline bool needs_unaligned_copy(const void *ptr)
49{
50#ifdef HAVE_EFFICIENT_UNALIGNED_ACCESS
51 return false;
52#else
53 return ((ptr - NULL) & 3) != 0;
54#endif
55}
56
57static bool sg_dwiter_get_next_block(struct sg_mapping_iter *miter, uint32_t **ptr)
58{
59 size_t len;
60
61 if (sg_dwiter_is_at_end(miter))
62 return true;
63
64 len = miter->length - miter->consumed;
65
66 if (likely(len >= 4 && !needs_unaligned_copy(
67 miter->addr + miter->consumed))) {
68 *ptr = miter->addr + miter->consumed;
69 miter->consumed += 4;
70 return true;
71 }
72
73 return false;
74}
75
76/**
77 * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
78 * @miter: sg mapping iterator used for reading
79 *
80 * Description:
81 * Returns 32-bit word starting at byte pointed to by @miter@
82 * handling any alignment issues. Bytes past the buffer's end
83 * are not accessed (read) but are returned as zeroes. @miter@
84 * is advanced by 4 bytes or to the end of buffer whichever is
85 * closer.
86 *
87 * Context:
88 * Same requirements as in sg_miter_next().
89 *
90 * Returns:
91 * 32-bit word just read.
92 */
93uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter *miter)
94{
95 uint32_t *ptr = NULL;
96
97 if (likely(sg_dwiter_get_next_block(miter, &ptr)))
98 return ptr ? *ptr : 0;
99
100 return sg_dwiter_read_buffer(miter);
101}
102EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block);
103
104static void sg_dwiter_write_slow(struct sg_mapping_iter *miter, uint32_t data)
105{
106 size_t len, left = 4;
107 void *addr = &data;
108
109 do {
110 len = min(miter->length - miter->consumed, left);
111 memcpy(miter->addr, addr, len);
112 miter->consumed += len;
113 left -= len;
114 if (!left)
115 return;
116 addr += len;
117 flush_kernel_dcache_page(miter->page);
118 } while (sg_dwiter_next(miter));
119}
120
121/**
122 * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
123 * @miter: sg mapping iterator used for writing
124 *
125 * Description:
126 * Writes 32-bit word starting at byte pointed to by @miter@
127 * handling any alignment issues. Bytes which would be written
128 * past the buffer's end are silently discarded. @miter@ is
129 * advanced by 4 bytes or to the end of buffer whichever is closer.
130 *
131 * Context:
132 * Same requirements as in sg_miter_next().
133 */
134void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter *miter, uint32_t data)
135{
136 uint32_t *ptr = NULL;
137
138 if (likely(sg_dwiter_get_next_block(miter, &ptr))) {
139 if (ptr)
140 *ptr = data;
141 else
142 return;
143 } else
144 sg_dwiter_write_slow(miter, data);
145
146 if (miter->length == miter->consumed)
147 flush_kernel_dcache_page(miter->page);
148}
149EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block);
150