sl811_cs.c revision 50db3fdbbc98260fb538c1cc3f8cc597ba7bffe7
1/*
2 * PCMCIA driver for SL811HS (as found in REX-CFU1U)
3 * Filename: sl811_cs.c
4 * Author:   Yukio Yamamoto
5 *
6 *  Port to sl811-hcd and 2.6.x by
7 *    Botond Botyanszki <boti@rocketmail.com>
8 *    Simon Pickering
9 *
10 *  Last update: 2005-05-12
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/sched.h>
17#include <linux/ptrace.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/timer.h>
21#include <linux/ioport.h>
22#include <linux/platform_device.h>
23
24#include <pcmcia/cs_types.h>
25#include <pcmcia/cs.h>
26#include <pcmcia/cistpl.h>
27#include <pcmcia/cisreg.h>
28#include <pcmcia/ds.h>
29
30#include <linux/usb_sl811.h>
31
32MODULE_AUTHOR("Botond Botyanszki");
33MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
34MODULE_LICENSE("GPL");
35
36
37/*====================================================================*/
38/* MACROS                                                             */
39/*====================================================================*/
40
41#if defined(DEBUG) || defined(PCMCIA_DEBUG)
42
43static int pc_debug = 0;
44module_param(pc_debug, int, 0644);
45
46#define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
47
48#else
49#define DBG(n, args...) do{}while(0)
50#endif	/* no debugging */
51
52#define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
53
54#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
55
56#define CS_CHECK(fn, ret) \
57	do { \
58		last_fn = (fn); \
59		if ((last_ret = (ret)) != 0) \
60			goto cs_failed; \
61	} while (0)
62
63/*====================================================================*/
64/* VARIABLES                                                          */
65/*====================================================================*/
66
67static const char driver_name[DEV_NAME_LEN]  = "sl811_cs";
68
69typedef struct local_info_t {
70	dev_link_t		link;
71	dev_node_t		node;
72} local_info_t;
73
74static void sl811_cs_release(dev_link_t * link);
75
76/*====================================================================*/
77
78static void release_platform_dev(struct device * dev)
79{
80	DBG(0, "sl811_cs platform_dev release\n");
81	dev->parent = NULL;
82}
83
84static struct sl811_platform_data platform_data = {
85	.potpg		= 100,
86	.power		= 50,		/* == 100mA */
87	// .reset	= ... FIXME:  invoke CF reset on the card
88};
89
90static struct resource resources[] = {
91	[0] = {
92		.flags	= IORESOURCE_IRQ,
93	},
94	[1] = {
95		// .name   = "address",
96		.flags	= IORESOURCE_IO,
97	},
98	[2] = {
99		// .name   = "data",
100		.flags	= IORESOURCE_IO,
101	},
102};
103
104extern struct platform_driver sl811h_driver;
105
106static struct platform_device platform_dev = {
107	.id			= -1,
108	.dev = {
109		.platform_data = &platform_data,
110		.release       = release_platform_dev,
111	},
112	.resource		= resources,
113	.num_resources		= ARRAY_SIZE(resources),
114};
115
116static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
117{
118	if (platform_dev.dev.parent)
119		return -EBUSY;
120	platform_dev.dev.parent = parent;
121
122	/* finish seting up the platform device */
123	resources[0].start = irq;
124
125	resources[1].start = base_addr;
126	resources[1].end = base_addr;
127
128	resources[2].start = base_addr + 1;
129	resources[2].end   = base_addr + 1;
130
131	/* The driver core will probe for us.  We know sl811-hcd has been
132	 * initialized already because of the link order dependency created
133	 * by referencing "sl811h_driver".
134	 */
135	platform_dev.name = sl811h_driver.driver.name;
136	return platform_device_register(&platform_dev);
137}
138
139/*====================================================================*/
140
141static void sl811_cs_detach(struct pcmcia_device *p_dev)
142{
143	dev_link_t *link = dev_to_instance(p_dev);
144
145	DBG(0, "sl811_cs_detach(0x%p)\n", link);
146
147	link->state &= ~DEV_PRESENT;
148	if (link->state & DEV_CONFIG)
149		sl811_cs_release(link);
150
151	/* This points to the parent local_info_t struct */
152	kfree(link->priv);
153}
154
155static void sl811_cs_release(dev_link_t * link)
156{
157	DBG(0, "sl811_cs_release(0x%p)\n", link);
158
159	pcmcia_disable_device(link->handle);
160	platform_device_unregister(&platform_dev);
161}
162
163static void sl811_cs_config(dev_link_t *link)
164{
165	client_handle_t		handle = link->handle;
166	struct device		*parent = &handle_to_dev(handle);
167	local_info_t		*dev = link->priv;
168	tuple_t			tuple;
169	cisparse_t		parse;
170	int			last_fn, last_ret;
171	u_char			buf[64];
172	config_info_t		conf;
173	cistpl_cftable_entry_t	dflt = { 0 };
174
175	DBG(0, "sl811_cs_config(0x%p)\n", link);
176
177	tuple.DesiredTuple = CISTPL_CONFIG;
178	tuple.Attributes = 0;
179	tuple.TupleData = buf;
180	tuple.TupleDataMax = sizeof(buf);
181	tuple.TupleOffset = 0;
182	CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
183	CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
184	CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
185	link->conf.ConfigBase = parse.config.base;
186	link->conf.Present = parse.config.rmask[0];
187
188	/* Configure card */
189	link->state |= DEV_CONFIG;
190
191	/* Look up the current Vcc */
192	CS_CHECK(GetConfigurationInfo,
193			pcmcia_get_configuration_info(handle, &conf));
194	link->conf.Vcc = conf.Vcc;
195
196	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
197	CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
198	while (1) {
199		cistpl_cftable_entry_t	*cfg = &(parse.cftable_entry);
200
201		if (pcmcia_get_tuple_data(handle, &tuple) != 0
202				|| pcmcia_parse_tuple(handle, &tuple, &parse)
203						!= 0)
204			goto next_entry;
205
206		if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
207			dflt = *cfg;
208		}
209
210		if (cfg->index == 0)
211			goto next_entry;
212
213		link->conf.ConfigIndex = cfg->index;
214
215		/* Use power settings for Vcc and Vpp if present */
216		/*  Note that the CIS values need to be rescaled */
217		if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
218			if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
219					!= conf.Vcc)
220				goto next_entry;
221		} else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
222			if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
223					!= conf.Vcc)
224				goto next_entry;
225		}
226
227		if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
228			link->conf.Vpp1 = link->conf.Vpp2 =
229				cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
230		else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
231			link->conf.Vpp1 = link->conf.Vpp2 =
232				dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
233
234		/* we need an interrupt */
235		if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
236			link->conf.Attributes |= CONF_ENABLE_IRQ;
237
238		/* IO window settings */
239		link->io.NumPorts1 = link->io.NumPorts2 = 0;
240		if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
241			cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
242
243			link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
244			link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
245			link->io.BasePort1 = io->win[0].base;
246			link->io.NumPorts1 = io->win[0].len;
247
248			if (pcmcia_request_io(link->handle, &link->io) != 0)
249				goto next_entry;
250		}
251		break;
252
253next_entry:
254		pcmcia_disable_device(handle);
255		last_ret = pcmcia_get_next_tuple(handle, &tuple);
256	}
257
258	/* require an IRQ and two registers */
259	if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
260		goto cs_failed;
261	if (link->conf.Attributes & CONF_ENABLE_IRQ)
262		CS_CHECK(RequestIRQ,
263			pcmcia_request_irq(link->handle, &link->irq));
264	else
265		goto cs_failed;
266
267	CS_CHECK(RequestConfiguration,
268		pcmcia_request_configuration(link->handle, &link->conf));
269
270	sprintf(dev->node.dev_name, driver_name);
271	dev->node.major = dev->node.minor = 0;
272	link->dev = &dev->node;
273
274	printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
275	       dev->node.dev_name, link->conf.ConfigIndex,
276	       link->conf.Vcc/10, link->conf.Vcc%10);
277	if (link->conf.Vpp1)
278		printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
279	printk(", irq %d", link->irq.AssignedIRQ);
280	printk(", io 0x%04x-0x%04x", link->io.BasePort1,
281	       link->io.BasePort1+link->io.NumPorts1-1);
282	printk("\n");
283
284	link->state &= ~DEV_CONFIG_PENDING;
285
286	if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
287			< 0) {
288cs_failed:
289		printk("sl811_cs_config failed\n");
290		cs_error(link->handle, last_fn, last_ret);
291		sl811_cs_release(link);
292		link->state &= ~DEV_CONFIG_PENDING;
293	}
294}
295
296static int sl811_suspend(struct pcmcia_device *dev)
297{
298	dev_link_t *link = dev_to_instance(dev);
299
300	link->state |= DEV_SUSPEND;
301	if (link->state & DEV_CONFIG)
302		pcmcia_release_configuration(link->handle);
303
304	return 0;
305}
306
307static int sl811_resume(struct pcmcia_device *dev)
308{
309	dev_link_t *link = dev_to_instance(dev);
310
311	link->state &= ~DEV_SUSPEND;
312	if (link->state & DEV_CONFIG)
313		pcmcia_request_configuration(link->handle, &link->conf);
314
315	return 0;
316}
317
318static int sl811_cs_attach(struct pcmcia_device *p_dev)
319{
320	local_info_t *local;
321	dev_link_t *link;
322
323	local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
324	if (!local)
325		return -ENOMEM;
326	memset(local, 0, sizeof(local_info_t));
327	link = &local->link;
328	link->priv = local;
329
330	/* Initialize */
331	link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
332	link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
333	link->irq.Handler = NULL;
334
335	link->conf.Attributes = 0;
336	link->conf.Vcc = 33;
337	link->conf.IntType = INT_MEMORY_AND_IO;
338
339	link->handle = p_dev;
340	p_dev->instance = link;
341
342	link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
343	sl811_cs_config(link);
344
345	return 0;
346}
347
348static struct pcmcia_device_id sl811_ids[] = {
349	PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
350	PCMCIA_DEVICE_NULL,
351};
352MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
353
354static struct pcmcia_driver sl811_cs_driver = {
355	.owner		= THIS_MODULE,
356	.drv		= {
357		.name	= (char *)driver_name,
358	},
359	.probe		= sl811_cs_attach,
360	.remove		= sl811_cs_detach,
361	.id_table	= sl811_ids,
362	.suspend	= sl811_suspend,
363	.resume		= sl811_resume,
364};
365
366/*====================================================================*/
367
368static int __init init_sl811_cs(void)
369{
370	return pcmcia_register_driver(&sl811_cs_driver);
371}
372module_init(init_sl811_cs);
373
374static void __exit exit_sl811_cs(void)
375{
376	pcmcia_unregister_driver(&sl811_cs_driver);
377}
378module_exit(exit_sl811_cs);
379